vlan.c 20 KB

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