vlan.c 19 KB

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