vlan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. const char vlan_fullname[] = "802.1Q VLAN Support";
  43. const char vlan_version[] = DRV_VERSION;
  44. static const char vlan_copyright[] = "Ben Greear <greearb@candelatech.com>";
  45. static const char vlan_buggyright[] = "David S. Miller <davem@redhat.com>";
  46. static struct packet_type vlan_packet_type __read_mostly = {
  47. .type = cpu_to_be16(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. const struct net_device_ops *ops = real_dev->netdev_ops;
  120. struct vlan_group *grp;
  121. u16 vlan_id = vlan->vlan_id;
  122. ASSERT_RTNL();
  123. grp = __vlan_find_group(real_dev);
  124. BUG_ON(!grp);
  125. /* Take it out of our own structures, but be sure to interlock with
  126. * HW accelerating devices or SW vlan input packet processing.
  127. */
  128. if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
  129. ops->ndo_vlan_rx_kill_vid(real_dev, vlan_id);
  130. vlan_group_set_device(grp, vlan_id, NULL);
  131. grp->nr_vlans--;
  132. synchronize_net();
  133. unregister_netdevice(dev);
  134. /* If the group is now empty, kill off the group. */
  135. if (grp->nr_vlans == 0) {
  136. vlan_gvrp_uninit_applicant(real_dev);
  137. if (real_dev->features & NETIF_F_HW_VLAN_RX)
  138. ops->ndo_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. }
  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, u16 vlan_id)
  166. {
  167. const char *name = real_dev->name;
  168. const struct net_device_ops *ops = real_dev->netdev_ops;
  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) && !ops->ndo_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. (!ops->ndo_vlan_rx_add_vid || !ops->ndo_vlan_rx_kill_vid)) {
  179. pr_info("8021q: Device %s has buggy VLAN hw accel\n", name);
  180. return -EOPNOTSUPP;
  181. }
  182. if (__find_vlan_dev(real_dev, vlan_id) != NULL)
  183. return -EEXIST;
  184. return 0;
  185. }
  186. int register_vlan_dev(struct net_device *dev)
  187. {
  188. struct vlan_dev_info *vlan = vlan_dev_info(dev);
  189. struct net_device *real_dev = vlan->real_dev;
  190. const struct net_device_ops *ops = real_dev->netdev_ops;
  191. u16 vlan_id = vlan->vlan_id;
  192. struct vlan_group *grp, *ngrp = NULL;
  193. int err;
  194. grp = __vlan_find_group(real_dev);
  195. if (!grp) {
  196. ngrp = grp = vlan_group_alloc(real_dev);
  197. if (!grp)
  198. return -ENOBUFS;
  199. err = vlan_gvrp_init_applicant(real_dev);
  200. if (err < 0)
  201. goto out_free_group;
  202. }
  203. err = vlan_group_prealloc_vid(grp, vlan_id);
  204. if (err < 0)
  205. goto out_uninit_applicant;
  206. err = register_netdevice(dev);
  207. if (err < 0)
  208. goto out_uninit_applicant;
  209. /* Account for reference in struct vlan_dev_info */
  210. dev_hold(real_dev);
  211. vlan_transfer_operstate(real_dev, dev);
  212. linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
  213. /* So, got the sucker initialized, now lets place
  214. * it into our local structure.
  215. */
  216. vlan_group_set_device(grp, vlan_id, dev);
  217. grp->nr_vlans++;
  218. if (ngrp && real_dev->features & NETIF_F_HW_VLAN_RX)
  219. ops->ndo_vlan_rx_register(real_dev, ngrp);
  220. if (real_dev->features & NETIF_F_HW_VLAN_FILTER)
  221. ops->ndo_vlan_rx_add_vid(real_dev, vlan_id);
  222. return 0;
  223. out_uninit_applicant:
  224. if (ngrp)
  225. vlan_gvrp_uninit_applicant(real_dev);
  226. out_free_group:
  227. if (ngrp)
  228. vlan_group_free(ngrp);
  229. return err;
  230. }
  231. /* Attach a VLAN device to a mac address (ie Ethernet Card).
  232. * Returns 0 if the device was created or a negative error code otherwise.
  233. */
  234. static int register_vlan_device(struct net_device *real_dev, u16 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_mq(sizeof(struct vlan_dev_info), name,
  272. vlan_setup, real_dev->num_tx_queues);
  273. if (new_dev == NULL)
  274. return -ENOBUFS;
  275. new_dev->real_num_tx_queues = real_dev->real_num_tx_queues;
  276. dev_net_set(new_dev, net);
  277. /* need 4 bytes for extra VLAN header info,
  278. * hope the underlying device can handle it.
  279. */
  280. new_dev->mtu = real_dev->mtu;
  281. vlan_dev_info(new_dev)->vlan_id = vlan_id;
  282. vlan_dev_info(new_dev)->real_dev = real_dev;
  283. vlan_dev_info(new_dev)->dent = NULL;
  284. vlan_dev_info(new_dev)->flags = VLAN_FLAG_REORDER_HDR;
  285. new_dev->rtnl_link_ops = &vlan_link_ops;
  286. err = register_vlan_dev(new_dev);
  287. if (err < 0)
  288. goto out_free_newdev;
  289. return 0;
  290. out_free_newdev:
  291. free_netdev(new_dev);
  292. return err;
  293. }
  294. static void vlan_sync_address(struct net_device *dev,
  295. struct net_device *vlandev)
  296. {
  297. struct vlan_dev_info *vlan = vlan_dev_info(vlandev);
  298. /* May be called without an actual change */
  299. if (!compare_ether_addr(vlan->real_dev_addr, dev->dev_addr))
  300. return;
  301. /* vlan address was different from the old address and is equal to
  302. * the new address */
  303. if (compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
  304. !compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
  305. dev_unicast_delete(dev, vlandev->dev_addr);
  306. /* vlan address was equal to the old address and is different from
  307. * the new address */
  308. if (!compare_ether_addr(vlandev->dev_addr, vlan->real_dev_addr) &&
  309. compare_ether_addr(vlandev->dev_addr, dev->dev_addr))
  310. dev_unicast_add(dev, vlandev->dev_addr);
  311. memcpy(vlan->real_dev_addr, dev->dev_addr, ETH_ALEN);
  312. }
  313. static void vlan_transfer_features(struct net_device *dev,
  314. struct net_device *vlandev)
  315. {
  316. unsigned long old_features = vlandev->features;
  317. vlandev->features &= ~dev->vlan_features;
  318. vlandev->features |= dev->features & dev->vlan_features;
  319. vlandev->gso_max_size = dev->gso_max_size;
  320. #if defined(CONFIG_FCOE) || defined(CONFIG_FCOE_MODULE)
  321. vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
  322. #endif
  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_CHANGEMTU:
  383. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  384. vlandev = vlan_group_get_device(grp, i);
  385. if (!vlandev)
  386. continue;
  387. if (vlandev->mtu <= dev->mtu)
  388. continue;
  389. dev_set_mtu(vlandev, dev->mtu);
  390. }
  391. break;
  392. case NETDEV_FEAT_CHANGE:
  393. /* Propagate device features to underlying device */
  394. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  395. vlandev = vlan_group_get_device(grp, i);
  396. if (!vlandev)
  397. continue;
  398. vlan_transfer_features(dev, vlandev);
  399. }
  400. break;
  401. case NETDEV_DOWN:
  402. /* Put all VLANs for this dev in the down state too. */
  403. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  404. vlandev = vlan_group_get_device(grp, i);
  405. if (!vlandev)
  406. continue;
  407. flgs = vlandev->flags;
  408. if (!(flgs & IFF_UP))
  409. continue;
  410. dev_change_flags(vlandev, flgs & ~IFF_UP);
  411. vlan_transfer_operstate(dev, vlandev);
  412. }
  413. break;
  414. case NETDEV_UP:
  415. /* Put all VLANs for this dev in the up state too. */
  416. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  417. vlandev = vlan_group_get_device(grp, i);
  418. if (!vlandev)
  419. continue;
  420. flgs = vlandev->flags;
  421. if (flgs & IFF_UP)
  422. continue;
  423. dev_change_flags(vlandev, flgs | IFF_UP);
  424. vlan_transfer_operstate(dev, vlandev);
  425. }
  426. break;
  427. case NETDEV_UNREGISTER:
  428. /* Delete all VLANs for this dev. */
  429. for (i = 0; i < VLAN_GROUP_ARRAY_LEN; i++) {
  430. vlandev = vlan_group_get_device(grp, i);
  431. if (!vlandev)
  432. continue;
  433. /* unregistration of last vlan destroys group, abort
  434. * afterwards */
  435. if (grp->nr_vlans == 1)
  436. i = VLAN_GROUP_ARRAY_LEN;
  437. unregister_vlan_dev(vlandev);
  438. }
  439. break;
  440. }
  441. out:
  442. return NOTIFY_DONE;
  443. }
  444. static struct notifier_block vlan_notifier_block __read_mostly = {
  445. .notifier_call = vlan_device_event,
  446. };
  447. /*
  448. * VLAN IOCTL handler.
  449. * o execute requested action or pass command to the device driver
  450. * arg is really a struct vlan_ioctl_args __user *.
  451. */
  452. static int vlan_ioctl_handler(struct net *net, void __user *arg)
  453. {
  454. int err;
  455. struct vlan_ioctl_args args;
  456. struct net_device *dev = NULL;
  457. if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
  458. return -EFAULT;
  459. /* Null terminate this sucker, just in case. */
  460. args.device1[23] = 0;
  461. args.u.device2[23] = 0;
  462. rtnl_lock();
  463. switch (args.cmd) {
  464. case SET_VLAN_INGRESS_PRIORITY_CMD:
  465. case SET_VLAN_EGRESS_PRIORITY_CMD:
  466. case SET_VLAN_FLAG_CMD:
  467. case ADD_VLAN_CMD:
  468. case DEL_VLAN_CMD:
  469. case GET_VLAN_REALDEV_NAME_CMD:
  470. case GET_VLAN_VID_CMD:
  471. err = -ENODEV;
  472. dev = __dev_get_by_name(net, args.device1);
  473. if (!dev)
  474. goto out;
  475. err = -EINVAL;
  476. if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
  477. goto out;
  478. }
  479. switch (args.cmd) {
  480. case SET_VLAN_INGRESS_PRIORITY_CMD:
  481. err = -EPERM;
  482. if (!capable(CAP_NET_ADMIN))
  483. break;
  484. vlan_dev_set_ingress_priority(dev,
  485. args.u.skb_priority,
  486. args.vlan_qos);
  487. err = 0;
  488. break;
  489. case SET_VLAN_EGRESS_PRIORITY_CMD:
  490. err = -EPERM;
  491. if (!capable(CAP_NET_ADMIN))
  492. break;
  493. err = vlan_dev_set_egress_priority(dev,
  494. args.u.skb_priority,
  495. args.vlan_qos);
  496. break;
  497. case SET_VLAN_FLAG_CMD:
  498. err = -EPERM;
  499. if (!capable(CAP_NET_ADMIN))
  500. break;
  501. err = vlan_dev_change_flags(dev,
  502. args.vlan_qos ? args.u.flag : 0,
  503. args.u.flag);
  504. break;
  505. case SET_VLAN_NAME_TYPE_CMD:
  506. err = -EPERM;
  507. if (!capable(CAP_NET_ADMIN))
  508. break;
  509. if ((args.u.name_type >= 0) &&
  510. (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
  511. struct vlan_net *vn;
  512. vn = net_generic(net, vlan_net_id);
  513. vn->name_type = args.u.name_type;
  514. err = 0;
  515. } else {
  516. err = -EINVAL;
  517. }
  518. break;
  519. case ADD_VLAN_CMD:
  520. err = -EPERM;
  521. if (!capable(CAP_NET_ADMIN))
  522. break;
  523. err = register_vlan_device(dev, args.u.VID);
  524. break;
  525. case DEL_VLAN_CMD:
  526. err = -EPERM;
  527. if (!capable(CAP_NET_ADMIN))
  528. break;
  529. unregister_vlan_dev(dev);
  530. err = 0;
  531. break;
  532. case GET_VLAN_REALDEV_NAME_CMD:
  533. err = 0;
  534. vlan_dev_get_realdev_name(dev, args.u.device2);
  535. if (copy_to_user(arg, &args,
  536. sizeof(struct vlan_ioctl_args)))
  537. err = -EFAULT;
  538. break;
  539. case GET_VLAN_VID_CMD:
  540. err = 0;
  541. args.u.VID = vlan_dev_vlan_id(dev);
  542. if (copy_to_user(arg, &args,
  543. sizeof(struct vlan_ioctl_args)))
  544. err = -EFAULT;
  545. break;
  546. default:
  547. err = -EOPNOTSUPP;
  548. break;
  549. }
  550. out:
  551. rtnl_unlock();
  552. return err;
  553. }
  554. static int vlan_init_net(struct net *net)
  555. {
  556. int err;
  557. struct vlan_net *vn;
  558. err = -ENOMEM;
  559. vn = kzalloc(sizeof(struct vlan_net), GFP_KERNEL);
  560. if (vn == NULL)
  561. goto err_alloc;
  562. err = net_assign_generic(net, vlan_net_id, vn);
  563. if (err < 0)
  564. goto err_assign;
  565. vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
  566. err = vlan_proc_init(net);
  567. if (err < 0)
  568. goto err_proc;
  569. return 0;
  570. err_proc:
  571. /* nothing */
  572. err_assign:
  573. kfree(vn);
  574. err_alloc:
  575. return err;
  576. }
  577. static void vlan_exit_net(struct net *net)
  578. {
  579. struct vlan_net *vn;
  580. vn = net_generic(net, vlan_net_id);
  581. rtnl_kill_links(net, &vlan_link_ops);
  582. vlan_proc_cleanup(net);
  583. kfree(vn);
  584. }
  585. static struct pernet_operations vlan_net_ops = {
  586. .init = vlan_init_net,
  587. .exit = vlan_exit_net,
  588. };
  589. static int __init vlan_proto_init(void)
  590. {
  591. int err;
  592. pr_info("%s v%s %s\n", vlan_fullname, vlan_version, vlan_copyright);
  593. pr_info("All bugs added by %s\n", vlan_buggyright);
  594. err = register_pernet_gen_device(&vlan_net_id, &vlan_net_ops);
  595. if (err < 0)
  596. goto err0;
  597. err = register_netdevice_notifier(&vlan_notifier_block);
  598. if (err < 0)
  599. goto err2;
  600. err = vlan_gvrp_init();
  601. if (err < 0)
  602. goto err3;
  603. err = vlan_netlink_init();
  604. if (err < 0)
  605. goto err4;
  606. dev_add_pack(&vlan_packet_type);
  607. vlan_ioctl_set(vlan_ioctl_handler);
  608. return 0;
  609. err4:
  610. vlan_gvrp_uninit();
  611. err3:
  612. unregister_netdevice_notifier(&vlan_notifier_block);
  613. err2:
  614. unregister_pernet_gen_device(vlan_net_id, &vlan_net_ops);
  615. err0:
  616. return err;
  617. }
  618. static void __exit vlan_cleanup_module(void)
  619. {
  620. unsigned int i;
  621. vlan_ioctl_set(NULL);
  622. vlan_netlink_fini();
  623. unregister_netdevice_notifier(&vlan_notifier_block);
  624. dev_remove_pack(&vlan_packet_type);
  625. /* This table must be empty if there are no module references left. */
  626. for (i = 0; i < VLAN_GRP_HASH_SIZE; i++)
  627. BUG_ON(!hlist_empty(&vlan_group_hash[i]));
  628. unregister_pernet_gen_device(vlan_net_id, &vlan_net_ops);
  629. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  630. vlan_gvrp_uninit();
  631. }
  632. module_init(vlan_proto_init);
  633. module_exit(vlan_cleanup_module);
  634. MODULE_LICENSE("GPL");
  635. MODULE_VERSION(DRV_VERSION);