vlan.c 18 KB

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