vlan.c 20 KB

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