vlan.c 17 KB

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