vlan.c 18 KB

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