vlan.c 19 KB

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