vlan.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/capability.h>
  22. #include <linux/module.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/slab.h>
  26. #include <linux/init.h>
  27. #include <linux/rculist.h>
  28. #include <net/p8022.h>
  29. #include <net/arp.h>
  30. #include <linux/rtnetlink.h>
  31. #include <linux/notifier.h>
  32. #include <net/rtnetlink.h>
  33. #include <net/net_namespace.h>
  34. #include <net/netns/generic.h>
  35. #include <linux/uaccess.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. unsigned int vlan_net_id __read_mostly;
  42. const char vlan_fullname[] = "802.1Q VLAN Support";
  43. const char vlan_version[] = DRV_VERSION;
  44. /* End of global variables definitions. */
  45. static int vlan_group_prealloc_vid(struct vlan_group *vg,
  46. __be16 vlan_proto, u16 vlan_id)
  47. {
  48. struct net_device **array;
  49. unsigned int pidx, vidx;
  50. unsigned int size;
  51. ASSERT_RTNL();
  52. pidx = vlan_proto_idx(vlan_proto);
  53. vidx = vlan_id / VLAN_GROUP_ARRAY_PART_LEN;
  54. array = vg->vlan_devices_arrays[pidx][vidx];
  55. if (array != NULL)
  56. return 0;
  57. size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
  58. array = kzalloc(size, GFP_KERNEL);
  59. if (array == NULL)
  60. return -ENOBUFS;
  61. vg->vlan_devices_arrays[pidx][vidx] = array;
  62. return 0;
  63. }
  64. void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
  65. {
  66. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  67. struct net_device *real_dev = vlan->real_dev;
  68. struct vlan_info *vlan_info;
  69. struct vlan_group *grp;
  70. u16 vlan_id = vlan->vlan_id;
  71. ASSERT_RTNL();
  72. vlan_info = rtnl_dereference(real_dev->vlan_info);
  73. BUG_ON(!vlan_info);
  74. grp = &vlan_info->grp;
  75. grp->nr_vlan_devs--;
  76. if (vlan->flags & VLAN_FLAG_MVRP)
  77. vlan_mvrp_request_leave(dev);
  78. if (vlan->flags & VLAN_FLAG_GVRP)
  79. vlan_gvrp_request_leave(dev);
  80. vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, NULL);
  81. netdev_upper_dev_unlink(real_dev, dev);
  82. /* Because unregister_netdevice_queue() makes sure at least one rcu
  83. * grace period is respected before device freeing,
  84. * we dont need to call synchronize_net() here.
  85. */
  86. unregister_netdevice_queue(dev, head);
  87. if (grp->nr_vlan_devs == 0) {
  88. vlan_mvrp_uninit_applicant(real_dev);
  89. vlan_gvrp_uninit_applicant(real_dev);
  90. }
  91. vlan_vid_del(real_dev, vlan->vlan_proto, vlan_id);
  92. /* Get rid of the vlan's reference to real_dev */
  93. dev_put(real_dev);
  94. }
  95. int vlan_check_real_dev(struct net_device *real_dev,
  96. __be16 protocol, u16 vlan_id,
  97. struct netlink_ext_ack *extack)
  98. {
  99. const char *name = real_dev->name;
  100. if (real_dev->features & NETIF_F_VLAN_CHALLENGED) {
  101. pr_info("VLANs not supported on %s\n", name);
  102. NL_SET_ERR_MSG_MOD(extack, "VLANs not supported on device");
  103. return -EOPNOTSUPP;
  104. }
  105. if (vlan_find_dev(real_dev, protocol, vlan_id) != NULL) {
  106. NL_SET_ERR_MSG_MOD(extack, "VLAN device already exists");
  107. return -EEXIST;
  108. }
  109. return 0;
  110. }
  111. int register_vlan_dev(struct net_device *dev, struct netlink_ext_ack *extack)
  112. {
  113. struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
  114. struct net_device *real_dev = vlan->real_dev;
  115. u16 vlan_id = vlan->vlan_id;
  116. struct vlan_info *vlan_info;
  117. struct vlan_group *grp;
  118. int err;
  119. err = vlan_vid_add(real_dev, vlan->vlan_proto, vlan_id);
  120. if (err)
  121. return err;
  122. vlan_info = rtnl_dereference(real_dev->vlan_info);
  123. /* vlan_info should be there now. vlan_vid_add took care of it */
  124. BUG_ON(!vlan_info);
  125. grp = &vlan_info->grp;
  126. if (grp->nr_vlan_devs == 0) {
  127. err = vlan_gvrp_init_applicant(real_dev);
  128. if (err < 0)
  129. goto out_vid_del;
  130. err = vlan_mvrp_init_applicant(real_dev);
  131. if (err < 0)
  132. goto out_uninit_gvrp;
  133. }
  134. err = vlan_group_prealloc_vid(grp, vlan->vlan_proto, vlan_id);
  135. if (err < 0)
  136. goto out_uninit_mvrp;
  137. vlan->nest_level = dev_get_nest_level(real_dev) + 1;
  138. err = register_netdevice(dev);
  139. if (err < 0)
  140. goto out_uninit_mvrp;
  141. err = netdev_upper_dev_link(real_dev, dev, extack);
  142. if (err)
  143. goto out_unregister_netdev;
  144. /* Account for reference in struct vlan_dev_priv */
  145. dev_hold(real_dev);
  146. netif_stacked_transfer_operstate(real_dev, dev);
  147. linkwatch_fire_event(dev); /* _MUST_ call rfc2863_policy() */
  148. /* So, got the sucker initialized, now lets place
  149. * it into our local structure.
  150. */
  151. vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, dev);
  152. grp->nr_vlan_devs++;
  153. return 0;
  154. out_unregister_netdev:
  155. unregister_netdevice(dev);
  156. out_uninit_mvrp:
  157. if (grp->nr_vlan_devs == 0)
  158. vlan_mvrp_uninit_applicant(real_dev);
  159. out_uninit_gvrp:
  160. if (grp->nr_vlan_devs == 0)
  161. vlan_gvrp_uninit_applicant(real_dev);
  162. out_vid_del:
  163. vlan_vid_del(real_dev, vlan->vlan_proto, vlan_id);
  164. return err;
  165. }
  166. /* Attach a VLAN device to a mac address (ie Ethernet Card).
  167. * Returns 0 if the device was created or a negative error code otherwise.
  168. */
  169. static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
  170. {
  171. struct net_device *new_dev;
  172. struct vlan_dev_priv *vlan;
  173. struct net *net = dev_net(real_dev);
  174. struct vlan_net *vn = net_generic(net, vlan_net_id);
  175. char name[IFNAMSIZ];
  176. int err;
  177. if (vlan_id >= VLAN_VID_MASK)
  178. return -ERANGE;
  179. err = vlan_check_real_dev(real_dev, htons(ETH_P_8021Q), vlan_id,
  180. NULL);
  181. if (err < 0)
  182. return err;
  183. /* Gotta set up the fields for the device. */
  184. switch (vn->name_type) {
  185. case VLAN_NAME_TYPE_RAW_PLUS_VID:
  186. /* name will look like: eth1.0005 */
  187. snprintf(name, IFNAMSIZ, "%s.%.4i", real_dev->name, vlan_id);
  188. break;
  189. case VLAN_NAME_TYPE_PLUS_VID_NO_PAD:
  190. /* Put our vlan.VID in the name.
  191. * Name will look like: vlan5
  192. */
  193. snprintf(name, IFNAMSIZ, "vlan%i", vlan_id);
  194. break;
  195. case VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD:
  196. /* Put our vlan.VID in the name.
  197. * Name will look like: eth0.5
  198. */
  199. snprintf(name, IFNAMSIZ, "%s.%i", real_dev->name, vlan_id);
  200. break;
  201. case VLAN_NAME_TYPE_PLUS_VID:
  202. /* Put our vlan.VID in the name.
  203. * Name will look like: vlan0005
  204. */
  205. default:
  206. snprintf(name, IFNAMSIZ, "vlan%.4i", vlan_id);
  207. }
  208. new_dev = alloc_netdev(sizeof(struct vlan_dev_priv), name,
  209. NET_NAME_UNKNOWN, vlan_setup);
  210. if (new_dev == NULL)
  211. return -ENOBUFS;
  212. dev_net_set(new_dev, net);
  213. /* need 4 bytes for extra VLAN header info,
  214. * hope the underlying device can handle it.
  215. */
  216. new_dev->mtu = real_dev->mtu;
  217. vlan = vlan_dev_priv(new_dev);
  218. vlan->vlan_proto = htons(ETH_P_8021Q);
  219. vlan->vlan_id = vlan_id;
  220. vlan->real_dev = real_dev;
  221. vlan->dent = NULL;
  222. vlan->flags = VLAN_FLAG_REORDER_HDR;
  223. new_dev->rtnl_link_ops = &vlan_link_ops;
  224. err = register_vlan_dev(new_dev, NULL);
  225. if (err < 0)
  226. goto out_free_newdev;
  227. return 0;
  228. out_free_newdev:
  229. if (new_dev->reg_state == NETREG_UNINITIALIZED)
  230. free_netdev(new_dev);
  231. return err;
  232. }
  233. static void vlan_sync_address(struct net_device *dev,
  234. struct net_device *vlandev)
  235. {
  236. struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
  237. /* May be called without an actual change */
  238. if (ether_addr_equal(vlan->real_dev_addr, dev->dev_addr))
  239. return;
  240. /* vlan continues to inherit address of lower device */
  241. if (vlan_dev_inherit_address(vlandev, dev))
  242. goto out;
  243. /* vlan address was different from the old address and is equal to
  244. * the new address */
  245. if (!ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) &&
  246. ether_addr_equal(vlandev->dev_addr, dev->dev_addr))
  247. dev_uc_del(dev, vlandev->dev_addr);
  248. /* vlan address was equal to the old address and is different from
  249. * the new address */
  250. if (ether_addr_equal(vlandev->dev_addr, vlan->real_dev_addr) &&
  251. !ether_addr_equal(vlandev->dev_addr, dev->dev_addr))
  252. dev_uc_add(dev, vlandev->dev_addr);
  253. out:
  254. ether_addr_copy(vlan->real_dev_addr, dev->dev_addr);
  255. }
  256. static void vlan_transfer_features(struct net_device *dev,
  257. struct net_device *vlandev)
  258. {
  259. struct vlan_dev_priv *vlan = vlan_dev_priv(vlandev);
  260. vlandev->gso_max_size = dev->gso_max_size;
  261. vlandev->gso_max_segs = dev->gso_max_segs;
  262. if (vlan_hw_offload_capable(dev->features, vlan->vlan_proto))
  263. vlandev->hard_header_len = dev->hard_header_len;
  264. else
  265. vlandev->hard_header_len = dev->hard_header_len + VLAN_HLEN;
  266. #if IS_ENABLED(CONFIG_FCOE)
  267. vlandev->fcoe_ddp_xid = dev->fcoe_ddp_xid;
  268. #endif
  269. vlandev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  270. vlandev->priv_flags |= (vlan->real_dev->priv_flags & IFF_XMIT_DST_RELEASE);
  271. netdev_update_features(vlandev);
  272. }
  273. static int __vlan_device_event(struct net_device *dev, unsigned long event)
  274. {
  275. int err = 0;
  276. switch (event) {
  277. case NETDEV_CHANGENAME:
  278. vlan_proc_rem_dev(dev);
  279. err = vlan_proc_add_dev(dev);
  280. break;
  281. case NETDEV_REGISTER:
  282. err = vlan_proc_add_dev(dev);
  283. break;
  284. case NETDEV_UNREGISTER:
  285. vlan_proc_rem_dev(dev);
  286. break;
  287. }
  288. return err;
  289. }
  290. static int vlan_device_event(struct notifier_block *unused, unsigned long event,
  291. void *ptr)
  292. {
  293. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  294. struct vlan_group *grp;
  295. struct vlan_info *vlan_info;
  296. int i, flgs;
  297. struct net_device *vlandev;
  298. struct vlan_dev_priv *vlan;
  299. bool last = false;
  300. LIST_HEAD(list);
  301. int err;
  302. if (is_vlan_dev(dev)) {
  303. int err = __vlan_device_event(dev, event);
  304. if (err)
  305. return notifier_from_errno(err);
  306. }
  307. if ((event == NETDEV_UP) &&
  308. (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) {
  309. pr_info("adding VLAN 0 to HW filter on device %s\n",
  310. dev->name);
  311. vlan_vid_add(dev, htons(ETH_P_8021Q), 0);
  312. }
  313. if (event == NETDEV_DOWN &&
  314. (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
  315. vlan_vid_del(dev, htons(ETH_P_8021Q), 0);
  316. vlan_info = rtnl_dereference(dev->vlan_info);
  317. if (!vlan_info)
  318. goto out;
  319. grp = &vlan_info->grp;
  320. /* It is OK that we do not hold the group lock right now,
  321. * as we run under the RTNL lock.
  322. */
  323. switch (event) {
  324. case NETDEV_CHANGE:
  325. /* Propagate real device state to vlan devices */
  326. vlan_group_for_each_dev(grp, i, vlandev)
  327. netif_stacked_transfer_operstate(dev, vlandev);
  328. break;
  329. case NETDEV_CHANGEADDR:
  330. /* Adjust unicast filters on underlying device */
  331. vlan_group_for_each_dev(grp, i, vlandev) {
  332. flgs = vlandev->flags;
  333. if (!(flgs & IFF_UP))
  334. continue;
  335. vlan_sync_address(dev, vlandev);
  336. }
  337. break;
  338. case NETDEV_CHANGEMTU:
  339. vlan_group_for_each_dev(grp, i, vlandev) {
  340. if (vlandev->mtu <= dev->mtu)
  341. continue;
  342. dev_set_mtu(vlandev, dev->mtu);
  343. }
  344. break;
  345. case NETDEV_FEAT_CHANGE:
  346. /* Propagate device features to underlying device */
  347. vlan_group_for_each_dev(grp, i, vlandev)
  348. vlan_transfer_features(dev, vlandev);
  349. break;
  350. case NETDEV_DOWN: {
  351. struct net_device *tmp;
  352. LIST_HEAD(close_list);
  353. /* Put all VLANs for this dev in the down state too. */
  354. vlan_group_for_each_dev(grp, i, vlandev) {
  355. flgs = vlandev->flags;
  356. if (!(flgs & IFF_UP))
  357. continue;
  358. vlan = vlan_dev_priv(vlandev);
  359. if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
  360. list_add(&vlandev->close_list, &close_list);
  361. }
  362. dev_close_many(&close_list, false);
  363. list_for_each_entry_safe(vlandev, tmp, &close_list, close_list) {
  364. netif_stacked_transfer_operstate(dev, vlandev);
  365. list_del_init(&vlandev->close_list);
  366. }
  367. list_del(&close_list);
  368. break;
  369. }
  370. case NETDEV_UP:
  371. /* Put all VLANs for this dev in the up state too. */
  372. vlan_group_for_each_dev(grp, i, vlandev) {
  373. flgs = dev_get_flags(vlandev);
  374. if (flgs & IFF_UP)
  375. continue;
  376. vlan = vlan_dev_priv(vlandev);
  377. if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
  378. dev_change_flags(vlandev, flgs | IFF_UP);
  379. netif_stacked_transfer_operstate(dev, vlandev);
  380. }
  381. break;
  382. case NETDEV_UNREGISTER:
  383. /* twiddle thumbs on netns device moves */
  384. if (dev->reg_state != NETREG_UNREGISTERING)
  385. break;
  386. vlan_group_for_each_dev(grp, i, vlandev) {
  387. /* removal of last vid destroys vlan_info, abort
  388. * afterwards */
  389. if (vlan_info->nr_vids == 1)
  390. last = true;
  391. unregister_vlan_dev(vlandev, &list);
  392. if (last)
  393. break;
  394. }
  395. unregister_netdevice_many(&list);
  396. break;
  397. case NETDEV_PRE_TYPE_CHANGE:
  398. /* Forbid underlaying device to change its type. */
  399. if (vlan_uses_dev(dev))
  400. return NOTIFY_BAD;
  401. break;
  402. case NETDEV_NOTIFY_PEERS:
  403. case NETDEV_BONDING_FAILOVER:
  404. case NETDEV_RESEND_IGMP:
  405. /* Propagate to vlan devices */
  406. vlan_group_for_each_dev(grp, i, vlandev)
  407. call_netdevice_notifiers(event, vlandev);
  408. break;
  409. case NETDEV_CVLAN_FILTER_PUSH_INFO:
  410. err = vlan_filter_push_vids(vlan_info, htons(ETH_P_8021Q));
  411. if (err)
  412. return notifier_from_errno(err);
  413. break;
  414. case NETDEV_CVLAN_FILTER_DROP_INFO:
  415. vlan_filter_drop_vids(vlan_info, htons(ETH_P_8021Q));
  416. break;
  417. case NETDEV_SVLAN_FILTER_PUSH_INFO:
  418. err = vlan_filter_push_vids(vlan_info, htons(ETH_P_8021AD));
  419. if (err)
  420. return notifier_from_errno(err);
  421. break;
  422. case NETDEV_SVLAN_FILTER_DROP_INFO:
  423. vlan_filter_drop_vids(vlan_info, htons(ETH_P_8021AD));
  424. break;
  425. }
  426. out:
  427. return NOTIFY_DONE;
  428. }
  429. static struct notifier_block vlan_notifier_block __read_mostly = {
  430. .notifier_call = vlan_device_event,
  431. };
  432. /*
  433. * VLAN IOCTL handler.
  434. * o execute requested action or pass command to the device driver
  435. * arg is really a struct vlan_ioctl_args __user *.
  436. */
  437. static int vlan_ioctl_handler(struct net *net, void __user *arg)
  438. {
  439. int err;
  440. struct vlan_ioctl_args args;
  441. struct net_device *dev = NULL;
  442. if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
  443. return -EFAULT;
  444. /* Null terminate this sucker, just in case. */
  445. args.device1[sizeof(args.device1) - 1] = 0;
  446. args.u.device2[sizeof(args.u.device2) - 1] = 0;
  447. rtnl_lock();
  448. switch (args.cmd) {
  449. case SET_VLAN_INGRESS_PRIORITY_CMD:
  450. case SET_VLAN_EGRESS_PRIORITY_CMD:
  451. case SET_VLAN_FLAG_CMD:
  452. case ADD_VLAN_CMD:
  453. case DEL_VLAN_CMD:
  454. case GET_VLAN_REALDEV_NAME_CMD:
  455. case GET_VLAN_VID_CMD:
  456. err = -ENODEV;
  457. dev = __dev_get_by_name(net, args.device1);
  458. if (!dev)
  459. goto out;
  460. err = -EINVAL;
  461. if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
  462. goto out;
  463. }
  464. switch (args.cmd) {
  465. case SET_VLAN_INGRESS_PRIORITY_CMD:
  466. err = -EPERM;
  467. if (!ns_capable(net->user_ns, 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 (!ns_capable(net->user_ns, 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 (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  485. break;
  486. err = vlan_dev_change_flags(dev,
  487. args.vlan_qos ? args.u.flag : 0,
  488. args.u.flag);
  489. break;
  490. case SET_VLAN_NAME_TYPE_CMD:
  491. err = -EPERM;
  492. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  493. break;
  494. if (args.u.name_type < VLAN_NAME_TYPE_HIGHEST) {
  495. struct vlan_net *vn;
  496. vn = net_generic(net, vlan_net_id);
  497. vn->name_type = args.u.name_type;
  498. err = 0;
  499. } else {
  500. err = -EINVAL;
  501. }
  502. break;
  503. case ADD_VLAN_CMD:
  504. err = -EPERM;
  505. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  506. break;
  507. err = register_vlan_device(dev, args.u.VID);
  508. break;
  509. case DEL_VLAN_CMD:
  510. err = -EPERM;
  511. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  512. break;
  513. unregister_vlan_dev(dev, NULL);
  514. err = 0;
  515. break;
  516. case GET_VLAN_REALDEV_NAME_CMD:
  517. err = 0;
  518. vlan_dev_get_realdev_name(dev, args.u.device2);
  519. if (copy_to_user(arg, &args,
  520. sizeof(struct vlan_ioctl_args)))
  521. err = -EFAULT;
  522. break;
  523. case GET_VLAN_VID_CMD:
  524. err = 0;
  525. args.u.VID = vlan_dev_vlan_id(dev);
  526. if (copy_to_user(arg, &args,
  527. sizeof(struct vlan_ioctl_args)))
  528. err = -EFAULT;
  529. break;
  530. default:
  531. err = -EOPNOTSUPP;
  532. break;
  533. }
  534. out:
  535. rtnl_unlock();
  536. return err;
  537. }
  538. static struct sk_buff *vlan_gro_receive(struct list_head *head,
  539. struct sk_buff *skb)
  540. {
  541. const struct packet_offload *ptype;
  542. unsigned int hlen, off_vlan;
  543. struct sk_buff *pp = NULL;
  544. struct vlan_hdr *vhdr;
  545. struct sk_buff *p;
  546. __be16 type;
  547. int flush = 1;
  548. off_vlan = skb_gro_offset(skb);
  549. hlen = off_vlan + sizeof(*vhdr);
  550. vhdr = skb_gro_header_fast(skb, off_vlan);
  551. if (skb_gro_header_hard(skb, hlen)) {
  552. vhdr = skb_gro_header_slow(skb, hlen, off_vlan);
  553. if (unlikely(!vhdr))
  554. goto out;
  555. }
  556. type = vhdr->h_vlan_encapsulated_proto;
  557. rcu_read_lock();
  558. ptype = gro_find_receive_by_type(type);
  559. if (!ptype)
  560. goto out_unlock;
  561. flush = 0;
  562. list_for_each_entry(p, head, list) {
  563. struct vlan_hdr *vhdr2;
  564. if (!NAPI_GRO_CB(p)->same_flow)
  565. continue;
  566. vhdr2 = (struct vlan_hdr *)(p->data + off_vlan);
  567. if (compare_vlan_header(vhdr, vhdr2))
  568. NAPI_GRO_CB(p)->same_flow = 0;
  569. }
  570. skb_gro_pull(skb, sizeof(*vhdr));
  571. skb_gro_postpull_rcsum(skb, vhdr, sizeof(*vhdr));
  572. pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
  573. out_unlock:
  574. rcu_read_unlock();
  575. out:
  576. skb_gro_flush_final(skb, pp, flush);
  577. return pp;
  578. }
  579. static int vlan_gro_complete(struct sk_buff *skb, int nhoff)
  580. {
  581. struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data + nhoff);
  582. __be16 type = vhdr->h_vlan_encapsulated_proto;
  583. struct packet_offload *ptype;
  584. int err = -ENOENT;
  585. rcu_read_lock();
  586. ptype = gro_find_complete_by_type(type);
  587. if (ptype)
  588. err = ptype->callbacks.gro_complete(skb, nhoff + sizeof(*vhdr));
  589. rcu_read_unlock();
  590. return err;
  591. }
  592. static struct packet_offload vlan_packet_offloads[] __read_mostly = {
  593. {
  594. .type = cpu_to_be16(ETH_P_8021Q),
  595. .priority = 10,
  596. .callbacks = {
  597. .gro_receive = vlan_gro_receive,
  598. .gro_complete = vlan_gro_complete,
  599. },
  600. },
  601. {
  602. .type = cpu_to_be16(ETH_P_8021AD),
  603. .priority = 10,
  604. .callbacks = {
  605. .gro_receive = vlan_gro_receive,
  606. .gro_complete = vlan_gro_complete,
  607. },
  608. },
  609. };
  610. static int __net_init vlan_init_net(struct net *net)
  611. {
  612. struct vlan_net *vn = net_generic(net, vlan_net_id);
  613. int err;
  614. vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
  615. err = vlan_proc_init(net);
  616. return err;
  617. }
  618. static void __net_exit vlan_exit_net(struct net *net)
  619. {
  620. vlan_proc_cleanup(net);
  621. }
  622. static struct pernet_operations vlan_net_ops = {
  623. .init = vlan_init_net,
  624. .exit = vlan_exit_net,
  625. .id = &vlan_net_id,
  626. .size = sizeof(struct vlan_net),
  627. };
  628. static int __init vlan_proto_init(void)
  629. {
  630. int err;
  631. unsigned int i;
  632. pr_info("%s v%s\n", vlan_fullname, vlan_version);
  633. err = register_pernet_subsys(&vlan_net_ops);
  634. if (err < 0)
  635. goto err0;
  636. err = register_netdevice_notifier(&vlan_notifier_block);
  637. if (err < 0)
  638. goto err2;
  639. err = vlan_gvrp_init();
  640. if (err < 0)
  641. goto err3;
  642. err = vlan_mvrp_init();
  643. if (err < 0)
  644. goto err4;
  645. err = vlan_netlink_init();
  646. if (err < 0)
  647. goto err5;
  648. for (i = 0; i < ARRAY_SIZE(vlan_packet_offloads); i++)
  649. dev_add_offload(&vlan_packet_offloads[i]);
  650. vlan_ioctl_set(vlan_ioctl_handler);
  651. return 0;
  652. err5:
  653. vlan_mvrp_uninit();
  654. err4:
  655. vlan_gvrp_uninit();
  656. err3:
  657. unregister_netdevice_notifier(&vlan_notifier_block);
  658. err2:
  659. unregister_pernet_subsys(&vlan_net_ops);
  660. err0:
  661. return err;
  662. }
  663. static void __exit vlan_cleanup_module(void)
  664. {
  665. unsigned int i;
  666. vlan_ioctl_set(NULL);
  667. for (i = 0; i < ARRAY_SIZE(vlan_packet_offloads); i++)
  668. dev_remove_offload(&vlan_packet_offloads[i]);
  669. vlan_netlink_fini();
  670. unregister_netdevice_notifier(&vlan_notifier_block);
  671. unregister_pernet_subsys(&vlan_net_ops);
  672. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  673. vlan_mvrp_uninit();
  674. vlan_gvrp_uninit();
  675. }
  676. module_init(vlan_proto_init);
  677. module_exit(vlan_cleanup_module);
  678. MODULE_LICENSE("GPL");
  679. MODULE_VERSION(DRV_VERSION);