vlan.c 17 KB

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