vlan.c 17 KB

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