vlan.c 18 KB

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