vlan.c 20 KB

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