vlan.c 21 KB

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