vlan.c 17 KB

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