vlan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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: netdev@vger.kernel.org
  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 <linux/capability.h>
  21. #include <linux/module.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/rculist.h>
  27. #include <net/p8022.h>
  28. #include <net/arp.h>
  29. #include <linux/rtnetlink.h>
  30. #include <linux/notifier.h>
  31. #include <net/rtnetlink.h>
  32. #include <net/net_namespace.h>
  33. #include <net/netns/generic.h>
  34. #include <asm/uaccess.h>
  35. #include <linux/if_vlan.h>
  36. #include "vlan.h"
  37. #include "vlanproc.h"
  38. #define DRV_VERSION "1.8"
  39. /* Global VLAN variables */
  40. int vlan_net_id __read_mostly;
  41. /* Our listing of VLAN group(s) */
  42. static struct hlist_head vlan_group_hash[VLAN_GRP_HASH_SIZE];
  43. const char vlan_fullname[] = "802.1Q VLAN Support";
  44. const char vlan_version[] = DRV_VERSION;
  45. static const char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
  46. static const char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
  47. static struct packet_type vlan_packet_type __read_mostly = {
  48. .type = cpu_to_be16(ETH_P_8021Q),
  49. .func = vlan_skb_recv, /* VLAN receive method */
  50. };
  51. /* End of global variables definitions. */
  52. static inline unsigned int vlan_grp_hashfn(unsigned int idx)
  53. {
  54. return ((idx >> VLAN_GRP_HASH_SHIFT) ^ idx) & VLAN_GRP_HASH_MASK;
  55. }
  56. /* Must be invoked with RCU read lock (no preempt) */
  57. static struct vlan_group *__vlan_find_group(struct net_device *real_dev)
  58. {
  59. struct vlan_group *grp;
  60. struct hlist_node *n;
  61. int hash = vlan_grp_hashfn(real_dev->ifindex);
  62. hlist_for_each_entry_rcu(grp, n, &vlan_group_hash[hash], hlist) {
  63. if (grp->real_dev == real_dev)
  64. return grp;
  65. }
  66. return NULL;
  67. }
  68. /* Find the protocol handler. Assumes VID < VLAN_VID_MASK.
  69. *
  70. * Must be invoked with RCU read lock (no preempt)
  71. */
  72. struct net_device *__find_vlan_dev(struct net_device *real_dev, u16 vlan_id)
  73. {
  74. struct vlan_group *grp = __vlan_find_group(real_dev);
  75. if (grp)
  76. return vlan_group_get_device(grp, vlan_id);
  77. return NULL;
  78. }
  79. static void vlan_group_free(struct vlan_group *grp)
  80. {
  81. int i;
  82. for (i = 0; i < VLAN_GROUP_ARRAY_SPLIT_PARTS; i++)
  83. kfree(grp->vlan_devices_arrays[i]);
  84. kfree(grp);
  85. }
  86. static struct vlan_group *vlan_group_alloc(struct net_device *real_dev)
  87. {
  88. struct vlan_group *grp;
  89. grp = kzalloc(sizeof(struct vlan_group), GFP_KERNEL);
  90. if (!grp)
  91. return NULL;
  92. grp->real_dev = real_dev;
  93. hlist_add_head_rcu(&grp->hlist,
  94. &vlan_group_hash[vlan_grp_hashfn(real_dev->ifindex)]);
  95. return grp;
  96. }
  97. static int vlan_group_prealloc_vid(struct vlan_group *vg, u16 vlan_id)
  98. {
  99. struct net_device **array;
  100. unsigned int size;
  101. ASSERT_RTNL();
  102. array = vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN];
  103. if (array != NULL)
  104. return 0;
  105. size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
  106. array = kzalloc(size, GFP_KERNEL);
  107. if (array == NULL)
  108. return -ENOBUFS;
  109. vg->vlan_devices_arrays[vlan_id / VLAN_GROUP_ARRAY_PART_LEN] = array;
  110. return 0;
  111. }
  112. static void vlan_rcu_free(struct rcu_head *rcu)
  113. {
  114. vlan_group_free(container_of(rcu, struct vlan_group, rcu));
  115. }
  116. void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
  117. {
  118. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  119. struct net_device *real_dev = vlan->real_dev;
  120. const struct net_device_ops *ops = real_dev->netdev_ops;
  121. struct vlan_group *grp;
  122. u16 vlan_id = vlan->vlan_id;
  123. ASSERT_RTNL();
  124. grp = __vlan_find_group(real_dev);
  125. BUG_ON(!grp);
  126. /* Take it out of our own structures, but be sure to interlock with
  127. * HW accelerating devices or SW vlan input packet processing.
  128. */
  129. if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
  130. ops->ndo_vlan_rx_kill_vid(real_dev, vlan_id);
  131. grp->nr_vlans--;
  132. vlan_group_set_device(grp, vlan_id, NULL);
  133. if (!grp->killall)
  134. synchronize_net();
  135. unregister_netdevice_queue(dev, head);
  136. /* If the group is now empty, kill off the group. */
  137. if (grp->nr_vlans == 0) {
  138. vlan_gvrp_uninit_applicant(real_dev);
  139. if (real_dev->features & NETIF_F_HW_VLAN_RX)
  140. ops->ndo_vlan_rx_register(real_dev, NULL);
  141. hlist_del_rcu(&grp->hlist);
  142. /* Free the group, after all cpu's are done. */
  143. call_rcu(&grp->rcu, vlan_rcu_free);
  144. }
  145. /* Get rid of the vlan's reference to real_dev */
  146. dev_put(real_dev);
  147. }
  148. int vlan_check_real_dev(struct net_device *real_dev, u16 vlan_id)
  149. {
  150. const char *name = real_dev->name;
  151. const struct net_device_ops *ops = real_dev->netdev_ops;
  152. if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
  153. pr_info("8021q: VLANs not supported on %s\n", name);
  154. return -EOPNOTSUPP;
  155. }
  156. if ((real_dev->features & NETIF_F_HW_VLAN_RX) && !ops->ndo_vlan_rx_register) {
  157. pr_info("8021q: device %s has buggy VLAN hw accel\n", name);
  158. return -EOPNOTSUPP;
  159. }
  160. if ((real_dev->features & NETIF_F_HW_VLAN_FILTER) &&
  161. (!ops->ndo_vlan_rx_add_vid || !ops->ndo_vlan_rx_kill_vid)) {
  162. pr_info("8021q: Device %s has buggy VLAN hw accel\n", name);
  163. return -EOPNOTSUPP;
  164. }
  165. if (__find_vlan_dev(real_dev, vlan_id) != NULL)
  166. return -EEXIST;
  167. return 0;
  168. }
  169. int register_vlan_dev(struct net_device *dev)
  170. {
  171. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  172. struct net_device *real_dev = vlan->real_dev;
  173. const struct net_device_ops *ops = real_dev->netdev_ops;
  174. u16 vlan_id = vlan->vlan_id;
  175. struct vlan_group *grp, *ngrp = NULL;
  176. int err;
  177. grp = __vlan_find_group(real_dev);
  178. if (!grp) {
  179. ngrp = grp = vlan_group_alloc(real_dev);
  180. if (!grp)
  181. return -ENOBUFS;
  182. err = vlan_gvrp_init_applicant(real_dev);
  183. if (err < 0)
  184. goto out_free_group;
  185. }
  186. err = vlan_group_prealloc_vid(grp, vlan_id);
  187. if (err < 0)
  188. goto out_uninit_applicant;
  189. err = register_netdevice(dev);
  190. if (err < 0)
  191. goto out_uninit_applicant;
  192. /* Account for reference in struct vlan_dev_info */
  193. dev_hold(real_dev);
  194. netif_stacked_transfer_operstate(real_dev, dev);
  195. linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
  196. /* So, got the sucker initialized, now lets place
  197. * it into our local structure.
  198. */
  199. vlan_group_set_device(grp, vlan_id, dev);
  200. grp->nr_vlans++;
  201. if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX)
  202. ops->ndo_vlan_rx_register(real_dev, ngrp);
  203. if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
  204. ops->ndo_vlan_rx_add_vid(real_dev, vlan_id);
  205. return 0;
  206. out_uninit_applicant:
  207. if (ngrp)
  208. vlan_gvrp_uninit_applicant(real_dev);
  209. out_free_group:
  210. if (ngrp) {
  211. hlist_del_rcu(&ngrp->hlist);
  212. /* Free the group, after all cpu's are done. */
  213. call_rcu(&ngrp->rcu, vlan_rcu_free);
  214. }
  215. return err;
  216. }
  217. /* Attach a VLAN device to a mac address (ie Ethernet Card).
  218. * Returns 0 if the device was created or a negative error code otherwise.
  219. */
  220. static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
  221. {
  222. struct net_device *new_dev;
  223. struct net *net = dev_net(real_dev);
  224. struct vlan_net *vn = net_generic(net, vlan_net_id);
  225. char name[IFNAMSIZ];
  226. int err;
  227. if (vlan_id >= VLAN_VID_MASK)
  228. return -ERANGE;
  229. err = vlan_check_real_dev(real_dev, vlan_id);
  230. if (err < 0)
  231. return err;
  232. /* Gotta set up the fields for the device. */
  233. switch (vn->name_type) {
  234. case VLAN_NAME_TYPE_RAW_PLUS_VID:
  235. /* name will look like: eth1.0005 */
  236. snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
  237. break;
  238. case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
  239. /* Put our vlan.VID in the name.
  240. * Name will look like: vlan5
  241. */
  242. snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
  243. break;
  244. case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
  245. /* Put our vlan.VID in the name.
  246. * Name will look like: eth0.5
  247. */
  248. snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
  249. break;
  250. case VLAN_NAME_TYPE_PLUS_VID:
  251. /* Put our vlan.VID in the name.
  252. * Name will look like: vlan0005
  253. */
  254. default:
  255. snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
  256. }
  257. new_dev = alloc_netdev_mq(sizeof(struct vlan_dev_info), name,
  258. vlan_setup, real_dev->num_tx_queues);
  259. if (new_dev == NULL)
  260. return -ENOBUFS;
  261. new_dev->real_num_tx_queues = real_dev->real_num_tx_queues;
  262. dev_net_set(new_dev, net);
  263. /* need 4 bytes for extra VLAN header info,
  264. * hope the underlying device can handle it.
  265. */
  266. new_dev->mtu = real_dev->mtu;
  267. vlan_dev_info(new_dev)->vlan_id = vlan_id;
  268. vlan_dev_info(new_dev)->real_dev = real_dev;
  269. vlan_dev_info(new_dev)->dent = NULL;
  270. vlan_dev_info(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
  271. new_dev->rtnl_link_ops = &vlan_link_ops;
  272. err = register_vlan_dev(new_dev);
  273. if (err < 0)
  274. goto out_free_newdev;
  275. return 0;
  276. out_free_newdev:
  277. free_netdev(new_dev);
  278. return err;
  279. }
  280. static void vlan_sync_address(struct net_device *dev,
  281. struct net_device *vlandev)
  282. {
  283. struct vlan_dev_info *vlan = vlan_dev_info(vlandev);
  284. /* May be called without an actual change */
  285. if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
  286. return;
  287. /* vlan address was different from the old address and is equal to
  288. * the new address */
  289. if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
  290. !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
  291. dev_unicast_delete(dev, vlandev->dev_addr);
  292. /* vlan address was equal to the old address and is different from
  293. * the new address */
  294. if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
  295. compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
  296. dev_unicast_add(dev, vlandev->dev_addr);
  297. memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
  298. }
  299. static void vlan_transfer_features(struct net_device *dev,
  300. struct net_device *vlandev)
  301. {
  302. unsigned long old_features = vlandev->features;
  303. vlandev->features &= ~dev->vlan_features;
  304. vlandev->features |= dev->features & dev->vlan_features;
  305. vlandev->gso_max_size = dev->gso_max_size;
  306. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  307. vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
  308. #endif
  309. vlandev->real_num_tx_queues = dev->real_num_tx_queues;
  310. BUG_ON(vlandev->real_num_tx_queues > vlandev->num_tx_queues);
  311. if (old_features != vlandev->features)
  312. netdev_features_change(vlandev);
  313. }
  314. static void __vlan_device_event(struct net_device *dev, unsigned long event)
  315. {
  316. switch (event) {
  317. case NETDEV_CHANGENAME:
  318. vlan_proc_rem_dev(dev);
  319. if (vlan_proc_add_dev(dev) < 0)
  320. pr_warning("8021q: failed to change proc name for %s\n",
  321. dev->name);
  322. break;
  323. case NETDEV_REGISTER:
  324. if (vlan_proc_add_dev(dev) < 0)
  325. pr_warning("8021q: failed to add proc entry for %s\n",
  326. dev->name);
  327. break;
  328. case NETDEV_UNREGISTER:
  329. vlan_proc_rem_dev(dev);
  330. break;
  331. }
  332. }
  333. static int vlan_device_event(struct notifier_block *unused, unsigned long event,
  334. void *ptr)
  335. {
  336. struct net_device *dev = ptr;
  337. struct vlan_group *grp;
  338. int i, flgs;
  339. struct net_device *vlandev;
  340. struct vlan_dev_info *vlan;
  341. LIST_HEAD(list);
  342. if (is_vlan_dev(dev))
  343. __vlan_device_event(dev, event);
  344. grp = __vlan_find_group(dev);
  345. if (!grp)
  346. goto out;
  347. /* It is OK that we do not hold the group lock right now,
  348. * as we run under the RTNL lock.
  349. */
  350. switch (event) {
  351. case NETDEV_CHANGE:
  352. /* Propagate real device state to vlan devices */
  353. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  354. vlandev = vlan_group_get_device(grp, i);
  355. if (!vlandev)
  356. continue;
  357. netif_stacked_transfer_operstate(dev, vlandev);
  358. }
  359. break;
  360. case NETDEV_CHANGEADDR:
  361. /* Adjust unicast filters on underlying device */
  362. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  363. vlandev = vlan_group_get_device(grp, i);
  364. if (!vlandev)
  365. continue;
  366. flgs = vlandev->flags;
  367. if (!(flgs & IFF_UP))
  368. continue;
  369. vlan_sync_address(dev, vlandev);
  370. }
  371. break;
  372. case NETDEV_CHANGEMTU:
  373. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  374. vlandev = vlan_group_get_device(grp, i);
  375. if (!vlandev)
  376. continue;
  377. if (vlandev->mtu <= dev->mtu)
  378. continue;
  379. dev_set_mtu(vlandev, dev->mtu);
  380. }
  381. break;
  382. case NETDEV_FEAT_CHANGE:
  383. /* Propagate device features to underlying device */
  384. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  385. vlandev = vlan_group_get_device(grp, i);
  386. if (!vlandev)
  387. continue;
  388. vlan_transfer_features(dev, vlandev);
  389. }
  390. break;
  391. case NETDEV_DOWN:
  392. /* Put all VLANs for this dev in the down state too. */
  393. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  394. vlandev = vlan_group_get_device(grp, i);
  395. if (!vlandev)
  396. continue;
  397. flgs = vlandev->flags;
  398. if (!(flgs & IFF_UP))
  399. continue;
  400. vlan = vlan_dev_info(vlandev);
  401. if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
  402. dev_change_flags(vlandev, flgs & ~IFF_UP);
  403. netif_stacked_transfer_operstate(dev, vlandev);
  404. }
  405. break;
  406. case NETDEV_UP:
  407. /* Put all VLANs for this dev in the up state too. */
  408. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  409. vlandev = vlan_group_get_device(grp, i);
  410. if (!vlandev)
  411. continue;
  412. flgs = vlandev->flags;
  413. if (flgs & IFF_UP)
  414. continue;
  415. vlan = vlan_dev_info(vlandev);
  416. if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
  417. dev_change_flags(vlandev, flgs | IFF_UP);
  418. netif_stacked_transfer_operstate(dev, vlandev);
  419. }
  420. break;
  421. case NETDEV_UNREGISTER:
  422. /* Delete all VLANs for this dev. */
  423. grp->killall = 1;
  424. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  425. vlandev = vlan_group_get_device(grp, i);
  426. if (!vlandev)
  427. continue;
  428. /* unregistration of last vlan destroys group, abort
  429. * afterwards */
  430. if (grp->nr_vlans == 1)
  431. i = VLAN_GROUP_ARRAY_LEN;
  432. unregister_vlan_dev(vlandev, &list);
  433. }
  434. unregister_netdevice_many(&list);
  435. break;
  436. }
  437. out:
  438. return NOTIFY_DONE;
  439. }
  440. static struct notifier_block vlan_notifier_block __read_mostly = {
  441. .notifier_call = vlan_device_event,
  442. };
  443. /*
  444. * VLAN IOCTL handler.
  445. * o execute requested action or pass command to the device driver
  446. * arg is really a struct vlan_ioctl_args __user *.
  447. */
  448. static int vlan_ioctl_handler(struct net *net, void __user *arg)
  449. {
  450. int err;
  451. struct vlan_ioctl_args args;
  452. struct net_device *dev = NULL;
  453. if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
  454. return -EFAULT;
  455. /* Null terminate this sucker, just in case. */
  456. args.device1[23] = 0;
  457. args.u.device2[23] = 0;
  458. rtnl_lock();
  459. switch (args.cmd) {
  460. case SET_VLAN_INGRESS_PRIORITY_CMD:
  461. case SET_VLAN_EGRESS_PRIORITY_CMD:
  462. case SET_VLAN_FLAG_CMD:
  463. case ADD_VLAN_CMD:
  464. case DEL_VLAN_CMD:
  465. case GET_VLAN_REALDEV_NAME_CMD:
  466. case GET_VLAN_VID_CMD:
  467. err = -ENODEV;
  468. dev = __dev_get_by_name(net, args.device1);
  469. if (!dev)
  470. goto out;
  471. err = -EINVAL;
  472. if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
  473. goto out;
  474. }
  475. switch (args.cmd) {
  476. case SET_VLAN_INGRESS_PRIORITY_CMD:
  477. err = -EPERM;
  478. if (!capable(CAP_NET_ADMIN))
  479. break;
  480. vlan_dev_set_ingress_priority(dev,
  481. args.u.skb_priority,
  482. args.vlan_qos);
  483. err = 0;
  484. break;
  485. case SET_VLAN_EGRESS_PRIORITY_CMD:
  486. err = -EPERM;
  487. if (!capable(CAP_NET_ADMIN))
  488. break;
  489. err = vlan_dev_set_egress_priority(dev,
  490. args.u.skb_priority,
  491. args.vlan_qos);
  492. break;
  493. case SET_VLAN_FLAG_CMD:
  494. err = -EPERM;
  495. if (!capable(CAP_NET_ADMIN))
  496. break;
  497. err = vlan_dev_change_flags(dev,
  498. args.vlan_qos ? args.u.flag : 0,
  499. args.u.flag);
  500. break;
  501. case SET_VLAN_NAME_TYPE_CMD:
  502. err = -EPERM;
  503. if (!capable(CAP_NET_ADMIN))
  504. break;
  505. if ((args.u.name_type >= 0) &&
  506. (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
  507. struct vlan_net *vn;
  508. vn = net_generic(net, vlan_net_id);
  509. vn->name_type = args.u.name_type;
  510. err = 0;
  511. } else {
  512. err = -EINVAL;
  513. }
  514. break;
  515. case ADD_VLAN_CMD:
  516. err = -EPERM;
  517. if (!capable(CAP_NET_ADMIN))
  518. break;
  519. err = register_vlan_device(dev, args.u.VID);
  520. break;
  521. case DEL_VLAN_CMD:
  522. err = -EPERM;
  523. if (!capable(CAP_NET_ADMIN))
  524. break;
  525. unregister_vlan_dev(dev, NULL);
  526. err = 0;
  527. break;
  528. case GET_VLAN_REALDEV_NAME_CMD:
  529. err = 0;
  530. vlan_dev_get_realdev_name(dev, args.u.device2);
  531. if (copy_to_user(arg, &args,
  532. sizeof(struct vlan_ioctl_args)))
  533. err = -EFAULT;
  534. break;
  535. case GET_VLAN_VID_CMD:
  536. err = 0;
  537. args.u.VID = vlan_dev_vlan_id(dev);
  538. if (copy_to_user(arg, &args,
  539. sizeof(struct vlan_ioctl_args)))
  540. err = -EFAULT;
  541. break;
  542. default:
  543. err = -EOPNOTSUPP;
  544. break;
  545. }
  546. out:
  547. rtnl_unlock();
  548. return err;
  549. }
  550. static int __net_init vlan_init_net(struct net *net)
  551. {
  552. struct vlan_net *vn = net_generic(net, vlan_net_id);
  553. int err;
  554. vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
  555. err = vlan_proc_init(net);
  556. return err;
  557. }
  558. static void __net_exit vlan_exit_net(struct net *net)
  559. {
  560. vlan_proc_cleanup(net);
  561. }
  562. static struct pernet_operations vlan_net_ops = {
  563. .init = vlan_init_net,
  564. .exit = vlan_exit_net,
  565. .id = &vlan_net_id,
  566. .size = sizeof(struct vlan_net),
  567. };
  568. static int __init vlan_proto_init(void)
  569. {
  570. int err;
  571. pr_info("%s v%s %s\n", vlan_fullname, vlan_version, vlan_copyright);
  572. pr_info("All bugs added by %s\n", vlan_buggyright);
  573. err = register_pernet_subsys(&vlan_net_ops);
  574. if (err < 0)
  575. goto err0;
  576. err = register_netdevice_notifier(&vlan_notifier_block);
  577. if (err < 0)
  578. goto err2;
  579. err = vlan_gvrp_init();
  580. if (err < 0)
  581. goto err3;
  582. err = vlan_netlink_init();
  583. if (err < 0)
  584. goto err4;
  585. dev_add_pack(&vlan_packet_type);
  586. vlan_ioctl_set(vlan_ioctl_handler);
  587. return 0;
  588. err4:
  589. vlan_gvrp_uninit();
  590. err3:
  591. unregister_netdevice_notifier(&vlan_notifier_block);
  592. err2:
  593. unregister_pernet_subsys(&vlan_net_ops);
  594. err0:
  595. return err;
  596. }
  597. static void __exit vlan_cleanup_module(void)
  598. {
  599. unsigned int i;
  600. vlan_ioctl_set(NULL);
  601. vlan_netlink_fini();
  602. unregister_netdevice_notifier(&vlan_notifier_block);
  603. dev_remove_pack(&vlan_packet_type);
  604. /* This table must be empty if there are no module references left. */
  605. for (i = 0; i < VLAN_GRP_HASH_SIZE; i++)
  606. BUG_ON(!hlist_empty(&vlan_group_hash[i]));
  607. unregister_pernet_subsys(&vlan_net_ops);
  608. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  609. vlan_gvrp_uninit();
  610. }
  611. module_init(vlan_proto_init);
  612. module_exit(vlan_cleanup_module);
  613. MODULE_LICENSE("GPL");
  614. MODULE_VERSION(DRV_VERSION);