vlan.c 20 KB

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