vlan.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * INET 802.1Q VLAN
  3. * Ethernet-type device handling.
  4. *
  5. * Authors: Ben Greear <greearb@candelatech.com>
  6. * Please send support related email to: vlan@scry.wanfear.com
  7. * VLAN Home Page: http://www.candelatech.com/~greear/vlan.html
  8. *
  9. * Fixes:
  10. * Fix for packet capture - Nick Eggleston <nick@dccinc.com>;
  11. * Add HW acceleration hooks - David S. Miller <davem@redhat.com>;
  12. * Correct all the locking - David S. Miller <davem@redhat.com>;
  13. * Use hash table for VLAN groups - David S. Miller <davem@redhat.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <asm/uaccess.h> /* for copy_from_user */
  21. #include <linux/module.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/skbuff.h>
  24. #include <net/datalink.h>
  25. #include <linux/mm.h>
  26. #include <linux/in.h>
  27. #include <linux/init.h>
  28. #include <net/p8022.h>
  29. #include <net/arp.h>
  30. #include <linux/rtnetlink.h>
  31. #include <linux/notifier.h>
  32. #include <linux/if_vlan.h>
  33. #include "vlan.h"
  34. #include "vlanproc.h"
  35. #define DRV_VERSION "1.8"
  36. /* Global VLAN variables */
  37. /* Our listing of VLAN group(s) */
  38. static struct hlist_head vlan_group_hash[VLAN_GRP_HASH_SIZE];
  39. #define vlan_grp_hashfn(IDX) ((((IDX) >> VLAN_GRP_HASH_SHIFT) ^ (IDX)) & VLAN_GRP_HASH_MASK)
  40. static char vlan_fullname[] = "802.1Q VLAN Support";
  41. static char vlan_version[] = DRV_VERSION;
  42. static char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
  43. static char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
  44. static int vlan_device_event(struct notifier_block *, unsigned long, void *);
  45. static int vlan_ioctl_handler(void __user *);
  46. static int unregister_vlan_dev(struct net_device *, unsigned short );
  47. static struct notifier_block vlan_notifier_block = {
  48. .notifier_call = vlan_device_event,
  49. };
  50. /* These may be changed at run-time through IOCTLs */
  51. /* Determines interface naming scheme. */
  52. unsigned short vlan_name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
  53. static struct packet_type vlan_packet_type = {
  54. .type = __constant_htons(ETH_P_8021Q),
  55. .func = vlan_skb_recv, /* VLAN receive method */
  56. };
  57. /* Bits of netdev state that are propagated from real device to virtual */
  58. #define VLAN_LINK_STATE_MASK \
  59. ((1<<__LINK_STATE_PRESENT)|(1<<__LINK_STATE_NOCARRIER))
  60. /* End of global variables definitions. */
  61. /*
  62. * Function vlan_proto_init (pro)
  63. *
  64. * Initialize VLAN protocol layer,
  65. *
  66. */
  67. static int __init vlan_proto_init(void)
  68. {
  69. int err;
  70. printk(VLAN_INF "%s v%s %s\n",
  71. vlan_fullname, vlan_version, vlan_copyright);
  72. printk(VLAN_INF "All bugs added by %s\n",
  73. vlan_buggyright);
  74. /* proc file system initialization */
  75. err = vlan_proc_init();
  76. if (err < 0) {
  77. printk(KERN_ERR
  78. "%s %s: can't create entry in proc filesystem!\n",
  79. __FUNCTION__, VLAN_NAME);
  80. return err;
  81. }
  82. dev_add_pack(&vlan_packet_type);
  83. /* Register us to receive netdevice events */
  84. err = register_netdevice_notifier(&vlan_notifier_block);
  85. if (err < 0) {
  86. dev_remove_pack(&vlan_packet_type);
  87. vlan_proc_cleanup();
  88. return err;
  89. }
  90. vlan_ioctl_set(vlan_ioctl_handler);
  91. return 0;
  92. }
  93. /* Cleanup all vlan devices
  94. * Note: devices that have been registered that but not
  95. * brought up will exist but have no module ref count.
  96. */
  97. static void __exit vlan_cleanup_devices(void)
  98. {
  99. struct net_device *dev, *nxt;
  100. rtnl_lock();
  101. for (dev = dev_base; dev; dev = nxt) {
  102. nxt = dev->next;
  103. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  104. unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
  105. VLAN_DEV_INFO(dev)->vlan_id);
  106. unregister_netdevice(dev);
  107. }
  108. }
  109. rtnl_unlock();
  110. }
  111. /*
  112. * Module 'remove' entry point.
  113. * o delete /proc/net/router directory and static entries.
  114. */
  115. static void __exit vlan_cleanup_module(void)
  116. {
  117. int i;
  118. vlan_ioctl_set(NULL);
  119. /* Un-register us from receiving netdevice events */
  120. unregister_netdevice_notifier(&vlan_notifier_block);
  121. dev_remove_pack(&vlan_packet_type);
  122. vlan_cleanup_devices();
  123. /* This table must be empty if there are no module
  124. * references left.
  125. */
  126. for (i = 0; i < VLAN_GRP_HASH_SIZE; i++) {
  127. BUG_ON(!hlist_empty(&vlan_group_hash[i]));
  128. }
  129. vlan_proc_cleanup();
  130. synchronize_net();
  131. }
  132. module_init(vlan_proto_init);
  133. module_exit(vlan_cleanup_module);
  134. /* Must be invoked with RCU read lock (no preempt) */
  135. static struct vlan_group *__vlan_find_group(int real_dev_ifindex)
  136. {
  137. struct vlan_group *grp;
  138. struct hlist_node *n;
  139. int hash = vlan_grp_hashfn(real_dev_ifindex);
  140. hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
  141. if (grp->real_dev_ifindex == real_dev_ifindex)
  142. return grp;
  143. }
  144. return NULL;
  145. }
  146. /* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
  147. *
  148. * Must be invoked with RCU read lock (no preempt)
  149. */
  150. struct net_device *__find_vlan_dev(struct net_device *real_dev,
  151. unsigned short VID)
  152. {
  153. struct vlan_group *grp = __vlan_find_group(real_dev->ifindex);
  154. if (grp)
  155. return grp->vlan_devices[VID];
  156. return NULL;
  157. }
  158. static void vlan_rcu_free(struct rcu_head *rcu)
  159. {
  160. kfree(container_of(rcu, struct vlan_group, rcu));
  161. }
  162. /* This returns 0 if everything went fine.
  163. * It will return 1 if the group was killed as a result.
  164. * A negative return indicates failure.
  165. *
  166. * The RTNL lock must be held.
  167. */
  168. static int unregister_vlan_dev(struct net_device *real_dev,
  169. unsigned short vlan_id)
  170. {
  171. struct net_device *dev = NULL;
  172. int real_dev_ifindex = real_dev->ifindex;
  173. struct vlan_group *grp;
  174. int i, ret;
  175. #ifdef VLAN_DEBUG
  176. printk(VLAN_DBG "%s: VID: %i\n", __FUNCTION__, vlan_id);
  177. #endif
  178. /* sanity check */
  179. if (vlan_id >= VLAN_VID_MASK)
  180. return -EINVAL;
  181. ASSERT_RTNL();
  182. grp = __vlan_find_group(real_dev_ifindex);
  183. ret = 0;
  184. if (grp) {
  185. dev = grp->vlan_devices[vlan_id];
  186. if (dev) {
  187. /* Remove proc entry */
  188. vlan_proc_rem_dev(dev);
  189. /* Take it out of our own structures, but be sure to
  190. * interlock with HW accelerating devices or SW vlan
  191. * input packet processing.
  192. */
  193. if (real_dev->features &
  194. (NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER)) {
  195. real_dev->vlan_rx_kill_vid(real_dev, vlan_id);
  196. }
  197. grp->vlan_devices[vlan_id] = NULL;
  198. synchronize_net();
  199. /* Caller unregisters (and if necessary, puts)
  200. * VLAN device, but we get rid of the reference to
  201. * real_dev here.
  202. */
  203. dev_put(real_dev);
  204. /* If the group is now empty, kill off the
  205. * group.
  206. */
  207. for (i = 0; i < VLAN_VID_MASK; i++)
  208. if (grp->vlan_devices[i])
  209. break;
  210. if (i == VLAN_VID_MASK) {
  211. if (real_dev->features & NETIF_F_HW_VLAN_RX)
  212. real_dev->vlan_rx_register(real_dev, NULL);
  213. hlist_del_rcu(&grp->hlist);
  214. /* Free the group, after all cpu's are done. */
  215. call_rcu(&grp->rcu, vlan_rcu_free);
  216. grp = NULL;
  217. ret = 1;
  218. }
  219. }
  220. }
  221. return ret;
  222. }
  223. static int unregister_vlan_device(const char *vlan_IF_name)
  224. {
  225. struct net_device *dev = NULL;
  226. int ret;
  227. dev = dev_get_by_name(vlan_IF_name);
  228. ret = -EINVAL;
  229. if (dev) {
  230. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  231. rtnl_lock();
  232. ret = unregister_vlan_dev(VLAN_DEV_INFO(dev)->real_dev,
  233. VLAN_DEV_INFO(dev)->vlan_id);
  234. dev_put(dev);
  235. unregister_netdevice(dev);
  236. rtnl_unlock();
  237. if (ret == 1)
  238. ret = 0;
  239. } else {
  240. printk(VLAN_ERR
  241. "%s: ERROR: Tried to remove a non-vlan device "
  242. "with VLAN code, name: %s priv_flags: %hX\n",
  243. __FUNCTION__, dev->name, dev->priv_flags);
  244. dev_put(dev);
  245. ret = -EPERM;
  246. }
  247. } else {
  248. #ifdef VLAN_DEBUG
  249. printk(VLAN_DBG "%s: WARNING: Could not find dev.\n", __FUNCTION__);
  250. #endif
  251. ret = -EINVAL;
  252. }
  253. return ret;
  254. }
  255. static void vlan_setup(struct net_device *new_dev)
  256. {
  257. SET_MODULE_OWNER(new_dev);
  258. /* new_dev->ifindex = 0; it will be set when added to
  259. * the global list.
  260. * iflink is set as well.
  261. */
  262. new_dev->get_stats = vlan_dev_get_stats;
  263. /* Make this thing known as a VLAN device */
  264. new_dev->priv_flags |= IFF_802_1Q_VLAN;
  265. /* Set us up to have no queue, as the underlying Hardware device
  266. * can do all the queueing we could want.
  267. */
  268. new_dev->tx_queue_len = 0;
  269. /* set up method calls */
  270. new_dev->change_mtu = vlan_dev_change_mtu;
  271. new_dev->open = vlan_dev_open;
  272. new_dev->stop = vlan_dev_stop;
  273. new_dev->set_mac_address = vlan_dev_set_mac_address;
  274. new_dev->set_multicast_list = vlan_dev_set_multicast_list;
  275. new_dev->destructor = free_netdev;
  276. new_dev->do_ioctl = vlan_dev_ioctl;
  277. }
  278. /* Attach a VLAN device to a mac address (ie Ethernet Card).
  279. * Returns the device that was created, or NULL if there was
  280. * an error of some kind.
  281. */
  282. static struct net_device *register_vlan_device(const char *eth_IF_name,
  283. unsigned short VLAN_ID)
  284. {
  285. struct vlan_group *grp;
  286. struct net_device *new_dev;
  287. struct net_device *real_dev; /* the ethernet device */
  288. char name[IFNAMSIZ];
  289. #ifdef VLAN_DEBUG
  290. printk(VLAN_DBG "%s: if_name -:%s:- vid: %i\n",
  291. __FUNCTION__, eth_IF_name, VLAN_ID);
  292. #endif
  293. if (VLAN_ID >= VLAN_VID_MASK)
  294. goto out_ret_null;
  295. /* find the device relating to eth_IF_name. */
  296. real_dev = dev_get_by_name(eth_IF_name);
  297. if (!real_dev)
  298. goto out_ret_null;
  299. if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
  300. printk(VLAN_DBG "%s: VLANs not supported on %s.\n",
  301. __FUNCTION__, real_dev->name);
  302. goto out_put_dev;
  303. }
  304. if ((real_dev->features & NETIF_F_HW_VLAN_RX) &&
  305. (real_dev->vlan_rx_register == NULL ||
  306. real_dev->vlan_rx_kill_vid == NULL)) {
  307. printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
  308. __FUNCTION__, real_dev->name);
  309. goto out_put_dev;
  310. }
  311. if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
  312. (real_dev->vlan_rx_add_vid == NULL ||
  313. real_dev->vlan_rx_kill_vid == NULL)) {
  314. printk(VLAN_DBG "%s: Device %s has buggy VLAN hw accel.\n",
  315. __FUNCTION__, real_dev->name);
  316. goto out_put_dev;
  317. }
  318. /* From this point on, all the data structures must remain
  319. * consistent.
  320. */
  321. rtnl_lock();
  322. /* The real device must be up and operating in order to
  323. * assosciate a VLAN device with it.
  324. */
  325. if (!(real_dev->flags & IFF_UP))
  326. goto out_unlock;
  327. if (__find_vlan_dev(real_dev, VLAN_ID) != NULL) {
  328. /* was already registered. */
  329. printk(VLAN_DBG "%s: ALREADY had VLAN registered\n", __FUNCTION__);
  330. goto out_unlock;
  331. }
  332. /* Gotta set up the fields for the device. */
  333. #ifdef VLAN_DEBUG
  334. printk(VLAN_DBG "About to allocate name, vlan_name_type: %i\n",
  335. vlan_name_type);
  336. #endif
  337. switch (vlan_name_type) {
  338. case VLAN_NAME_TYPE_RAW_PLUS_VID:
  339. /* name will look like: eth1.0005 */
  340. snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, VLAN_ID);
  341. break;
  342. case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
  343. /* Put our vlan.VID in the name.
  344. * Name will look like: vlan5
  345. */
  346. snprintf(name, IFNAMSIZ, "vlan%i", VLAN_ID);
  347. break;
  348. case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
  349. /* Put our vlan.VID in the name.
  350. * Name will look like: eth0.5
  351. */
  352. snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, VLAN_ID);
  353. break;
  354. case VLAN_NAME_TYPE_PLUS_VID:
  355. /* Put our vlan.VID in the name.
  356. * Name will look like: vlan0005
  357. */
  358. default:
  359. snprintf(name, IFNAMSIZ, "vlan%.4i", VLAN_ID);
  360. };
  361. new_dev = alloc_netdev(sizeof(struct vlan_dev_info), name,
  362. vlan_setup);
  363. if (new_dev == NULL)
  364. goto out_unlock;
  365. #ifdef VLAN_DEBUG
  366. printk(VLAN_DBG "Allocated new name -:%s:-\n", new_dev->name);
  367. #endif
  368. /* IFF_BROADCAST|IFF_MULTICAST; ??? */
  369. new_dev->flags = real_dev->flags;
  370. new_dev->flags &= ~IFF_UP;
  371. new_dev->state = real_dev->state & VLAN_LINK_STATE_MASK;
  372. /* need 4 bytes for extra VLAN header info,
  373. * hope the underlying device can handle it.
  374. */
  375. new_dev->mtu = real_dev->mtu;
  376. /* TODO: maybe just assign it to be ETHERNET? */
  377. new_dev->type = real_dev->type;
  378. new_dev->hard_header_len = real_dev->hard_header_len;
  379. if (!(real_dev->features & NETIF_F_HW_VLAN_TX)) {
  380. /* Regular ethernet + 4 bytes (18 total). */
  381. new_dev->hard_header_len += VLAN_HLEN;
  382. }
  383. VLAN_MEM_DBG("new_dev->priv malloc, addr: %p size: %i\n",
  384. new_dev->priv,
  385. sizeof(struct vlan_dev_info));
  386. memcpy(new_dev->broadcast, real_dev->broadcast, real_dev->addr_len);
  387. memcpy(new_dev->dev_addr, real_dev->dev_addr, real_dev->addr_len);
  388. new_dev->addr_len = real_dev->addr_len;
  389. if (real_dev->features & NETIF_F_HW_VLAN_TX) {
  390. new_dev->hard_header = real_dev->hard_header;
  391. new_dev->hard_start_xmit = vlan_dev_hwaccel_hard_start_xmit;
  392. new_dev->rebuild_header = real_dev->rebuild_header;
  393. } else {
  394. new_dev->hard_header = vlan_dev_hard_header;
  395. new_dev->hard_start_xmit = vlan_dev_hard_start_xmit;
  396. new_dev->rebuild_header = vlan_dev_rebuild_header;
  397. }
  398. new_dev->hard_header_parse = real_dev->hard_header_parse;
  399. VLAN_DEV_INFO(new_dev)->vlan_id = VLAN_ID; /* 1 through VLAN_VID_MASK */
  400. VLAN_DEV_INFO(new_dev)->real_dev = real_dev;
  401. VLAN_DEV_INFO(new_dev)->dent = NULL;
  402. VLAN_DEV_INFO(new_dev)->flags = 1;
  403. #ifdef VLAN_DEBUG
  404. printk(VLAN_DBG "About to go find the group for idx: %i\n",
  405. real_dev->ifindex);
  406. #endif
  407. if (register_netdevice(new_dev))
  408. goto out_free_newdev;
  409. /* So, got the sucker initialized, now lets place
  410. * it into our local structure.
  411. */
  412. grp = __vlan_find_group(real_dev->ifindex);
  413. /* Note, we are running under the RTNL semaphore
  414. * so it cannot "appear" on us.
  415. */
  416. if (!grp) { /* need to add a new group */
  417. grp = kmalloc(sizeof(struct vlan_group), GFP_KERNEL);
  418. if (!grp)
  419. goto out_free_unregister;
  420. /* printk(KERN_ALERT "VLAN REGISTER: Allocated new group.\n"); */
  421. memset(grp, 0, sizeof(struct vlan_group));
  422. grp->real_dev_ifindex = real_dev->ifindex;
  423. hlist_add_head_rcu(&grp->hlist,
  424. &vlan_group_hash[vlan_grp_hashfn(real_dev->ifindex)]);
  425. if (real_dev->features & NETIF_F_HW_VLAN_RX)
  426. real_dev->vlan_rx_register(real_dev, grp);
  427. }
  428. grp->vlan_devices[VLAN_ID] = new_dev;
  429. if (vlan_proc_add_dev(new_dev)<0)/* create it's proc entry */
  430. printk(KERN_WARNING "VLAN: failed to add proc entry for %s\n",
  431. new_dev->name);
  432. if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
  433. real_dev->vlan_rx_add_vid(real_dev, VLAN_ID);
  434. rtnl_unlock();
  435. #ifdef VLAN_DEBUG
  436. printk(VLAN_DBG "Allocated new device successfully, returning.\n");
  437. #endif
  438. return new_dev;
  439. out_free_unregister:
  440. unregister_netdev(new_dev);
  441. goto out_unlock;
  442. out_free_newdev:
  443. free_netdev(new_dev);
  444. out_unlock:
  445. rtnl_unlock();
  446. out_put_dev:
  447. dev_put(real_dev);
  448. out_ret_null:
  449. return NULL;
  450. }
  451. static int vlan_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
  452. {
  453. struct net_device *dev = ptr;
  454. struct vlan_group *grp = __vlan_find_group(dev->ifindex);
  455. int i, flgs;
  456. struct net_device *vlandev;
  457. if (!grp)
  458. goto out;
  459. /* It is OK that we do not hold the group lock right now,
  460. * as we run under the RTNL lock.
  461. */
  462. switch (event) {
  463. case NETDEV_CHANGE:
  464. /* Propagate real device state to vlan devices */
  465. flgs = dev->state & VLAN_LINK_STATE_MASK;
  466. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  467. vlandev = grp->vlan_devices[i];
  468. if (!vlandev)
  469. continue;
  470. if (netif_carrier_ok(dev)) {
  471. if (!netif_carrier_ok(vlandev))
  472. netif_carrier_on(vlandev);
  473. } else {
  474. if (netif_carrier_ok(vlandev))
  475. netif_carrier_off(vlandev);
  476. }
  477. if ((vlandev->state & VLAN_LINK_STATE_MASK) != flgs) {
  478. vlandev->state = (vlandev->state &~ VLAN_LINK_STATE_MASK)
  479. | flgs;
  480. netdev_state_change(vlandev);
  481. }
  482. }
  483. break;
  484. case NETDEV_DOWN:
  485. /* Put all VLANs for this dev in the down state too. */
  486. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  487. vlandev = grp->vlan_devices[i];
  488. if (!vlandev)
  489. continue;
  490. flgs = vlandev->flags;
  491. if (!(flgs & IFF_UP))
  492. continue;
  493. dev_change_flags(vlandev, flgs & ~IFF_UP);
  494. }
  495. break;
  496. case NETDEV_UP:
  497. /* Put all VLANs for this dev in the up state too. */
  498. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  499. vlandev = grp->vlan_devices[i];
  500. if (!vlandev)
  501. continue;
  502. flgs = vlandev->flags;
  503. if (flgs & IFF_UP)
  504. continue;
  505. dev_change_flags(vlandev, flgs | IFF_UP);
  506. }
  507. break;
  508. case NETDEV_UNREGISTER:
  509. /* Delete all VLANs for this dev. */
  510. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  511. int ret;
  512. vlandev = grp->vlan_devices[i];
  513. if (!vlandev)
  514. continue;
  515. ret = unregister_vlan_dev(dev,
  516. VLAN_DEV_INFO(vlandev)->vlan_id);
  517. unregister_netdevice(vlandev);
  518. /* Group was destroyed? */
  519. if (ret == 1)
  520. break;
  521. }
  522. break;
  523. };
  524. out:
  525. return NOTIFY_DONE;
  526. }
  527. /*
  528. * VLAN IOCTL handler.
  529. * o execute requested action or pass command to the device driver
  530. * arg is really a struct vlan_ioctl_args __user *.
  531. */
  532. static int vlan_ioctl_handler(void __user *arg)
  533. {
  534. int err = 0;
  535. unsigned short vid = 0;
  536. struct vlan_ioctl_args args;
  537. if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
  538. return -EFAULT;
  539. /* Null terminate this sucker, just in case. */
  540. args.device1[23] = 0;
  541. args.u.device2[23] = 0;
  542. #ifdef VLAN_DEBUG
  543. printk(VLAN_DBG "%s: args.cmd: %x\n", __FUNCTION__, args.cmd);
  544. #endif
  545. switch (args.cmd) {
  546. case SET_VLAN_INGRESS_PRIORITY_CMD:
  547. if (!capable(CAP_NET_ADMIN))
  548. return -EPERM;
  549. err = vlan_dev_set_ingress_priority(args.device1,
  550. args.u.skb_priority,
  551. args.vlan_qos);
  552. break;
  553. case SET_VLAN_EGRESS_PRIORITY_CMD:
  554. if (!capable(CAP_NET_ADMIN))
  555. return -EPERM;
  556. err = vlan_dev_set_egress_priority(args.device1,
  557. args.u.skb_priority,
  558. args.vlan_qos);
  559. break;
  560. case SET_VLAN_FLAG_CMD:
  561. if (!capable(CAP_NET_ADMIN))
  562. return -EPERM;
  563. err = vlan_dev_set_vlan_flag(args.device1,
  564. args.u.flag,
  565. args.vlan_qos);
  566. break;
  567. case SET_VLAN_NAME_TYPE_CMD:
  568. if (!capable(CAP_NET_ADMIN))
  569. return -EPERM;
  570. if ((args.u.name_type >= 0) &&
  571. (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
  572. vlan_name_type = args.u.name_type;
  573. err = 0;
  574. } else {
  575. err = -EINVAL;
  576. }
  577. break;
  578. case ADD_VLAN_CMD:
  579. if (!capable(CAP_NET_ADMIN))
  580. return -EPERM;
  581. /* we have been given the name of the Ethernet Device we want to
  582. * talk to: args.dev1 We also have the
  583. * VLAN ID: args.u.VID
  584. */
  585. if (register_vlan_device(args.device1, args.u.VID)) {
  586. err = 0;
  587. } else {
  588. err = -EINVAL;
  589. }
  590. break;
  591. case DEL_VLAN_CMD:
  592. if (!capable(CAP_NET_ADMIN))
  593. return -EPERM;
  594. /* Here, the args.dev1 is the actual VLAN we want
  595. * to get rid of.
  596. */
  597. err = unregister_vlan_device(args.device1);
  598. break;
  599. case GET_VLAN_INGRESS_PRIORITY_CMD:
  600. /* TODO: Implement
  601. err = vlan_dev_get_ingress_priority(args);
  602. if (copy_to_user((void*)arg, &args,
  603. sizeof(struct vlan_ioctl_args))) {
  604. err = -EFAULT;
  605. }
  606. */
  607. err = -EINVAL;
  608. break;
  609. case GET_VLAN_EGRESS_PRIORITY_CMD:
  610. /* TODO: Implement
  611. err = vlan_dev_get_egress_priority(args.device1, &(args.args);
  612. if (copy_to_user((void*)arg, &args,
  613. sizeof(struct vlan_ioctl_args))) {
  614. err = -EFAULT;
  615. }
  616. */
  617. err = -EINVAL;
  618. break;
  619. case GET_VLAN_REALDEV_NAME_CMD:
  620. err = vlan_dev_get_realdev_name(args.device1, args.u.device2);
  621. if (copy_to_user(arg, &args,
  622. sizeof(struct vlan_ioctl_args))) {
  623. err = -EFAULT;
  624. }
  625. break;
  626. case GET_VLAN_VID_CMD:
  627. err = vlan_dev_get_vid(args.device1, &vid);
  628. args.u.VID = vid;
  629. if (copy_to_user(arg, &args,
  630. sizeof(struct vlan_ioctl_args))) {
  631. err = -EFAULT;
  632. }
  633. break;
  634. default:
  635. /* pass on to underlying device instead?? */
  636. printk(VLAN_DBG "%s: Unknown VLAN CMD: %x \n",
  637. __FUNCTION__, args.cmd);
  638. return -EINVAL;
  639. };
  640. return err;
  641. }
  642. MODULE_LICENSE("GPL");
  643. MODULE_VERSION(DRV_VERSION);