vlan.c 21 KB

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