vlan.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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) + 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. 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);
  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. netdev_update_features(vlandev);
  270. }
  271. static int __vlan_device_event(struct net_device *dev, unsigned long event)
  272. {
  273. int err = 0;
  274. switch (event) {
  275. case NETDEV_CHANGENAME:
  276. vlan_proc_rem_dev(dev);
  277. err = vlan_proc_add_dev(dev);
  278. break;
  279. case NETDEV_REGISTER:
  280. err = vlan_proc_add_dev(dev);
  281. break;
  282. case NETDEV_UNREGISTER:
  283. vlan_proc_rem_dev(dev);
  284. break;
  285. }
  286. return err;
  287. }
  288. static int vlan_device_event(struct notifier_block *unused, unsigned long event,
  289. void *ptr)
  290. {
  291. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  292. struct vlan_group *grp;
  293. struct vlan_info *vlan_info;
  294. int i, flgs;
  295. struct net_device *vlandev;
  296. struct vlan_dev_priv *vlan;
  297. bool last = false;
  298. LIST_HEAD(list);
  299. if (is_vlan_dev(dev)) {
  300. int err = __vlan_device_event(dev, event);
  301. if (err)
  302. return notifier_from_errno(err);
  303. }
  304. if ((event == NETDEV_UP) &&
  305. (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) {
  306. pr_info("adding VLAN 0 to HW filter on device %s\n",
  307. dev->name);
  308. vlan_vid_add(dev, htons(ETH_P_8021Q), 0);
  309. }
  310. vlan_info = rtnl_dereference(dev->vlan_info);
  311. if (!vlan_info)
  312. goto out;
  313. grp = &vlan_info->grp;
  314. /* It is OK that we do not hold the group lock right now,
  315. * as we run under the RTNL lock.
  316. */
  317. switch (event) {
  318. case NETDEV_CHANGE:
  319. /* Propagate real device state to vlan devices */
  320. vlan_group_for_each_dev(grp, i, vlandev)
  321. netif_stacked_transfer_operstate(dev, vlandev);
  322. break;
  323. case NETDEV_CHANGEADDR:
  324. /* Adjust unicast filters on underlying device */
  325. vlan_group_for_each_dev(grp, i, vlandev) {
  326. flgs = vlandev->flags;
  327. if (!(flgs & IFF_UP))
  328. continue;
  329. vlan_sync_address(dev, vlandev);
  330. }
  331. break;
  332. case NETDEV_CHANGEMTU:
  333. vlan_group_for_each_dev(grp, i, vlandev) {
  334. if (vlandev->mtu <= dev->mtu)
  335. continue;
  336. dev_set_mtu(vlandev, dev->mtu);
  337. }
  338. break;
  339. case NETDEV_FEAT_CHANGE:
  340. /* Propagate device features to underlying device */
  341. vlan_group_for_each_dev(grp, i, vlandev)
  342. vlan_transfer_features(dev, vlandev);
  343. break;
  344. case NETDEV_DOWN: {
  345. struct net_device *tmp;
  346. LIST_HEAD(close_list);
  347. if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
  348. vlan_vid_del(dev, htons(ETH_P_8021Q), 0);
  349. /* Put all VLANs for this dev in the down state too. */
  350. vlan_group_for_each_dev(grp, i, vlandev) {
  351. flgs = vlandev->flags;
  352. if (!(flgs & IFF_UP))
  353. continue;
  354. vlan = vlan_dev_priv(vlandev);
  355. if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
  356. list_add(&vlandev->close_list, &close_list);
  357. }
  358. dev_close_many(&close_list, false);
  359. list_for_each_entry_safe(vlandev, tmp, &close_list, close_list) {
  360. netif_stacked_transfer_operstate(dev, vlandev);
  361. list_del_init(&vlandev->close_list);
  362. }
  363. list_del(&close_list);
  364. break;
  365. }
  366. case NETDEV_UP:
  367. /* Put all VLANs for this dev in the up state too. */
  368. vlan_group_for_each_dev(grp, i, vlandev) {
  369. flgs = dev_get_flags(vlandev);
  370. if (flgs & IFF_UP)
  371. continue;
  372. vlan = vlan_dev_priv(vlandev);
  373. if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
  374. dev_change_flags(vlandev, flgs | IFF_UP);
  375. netif_stacked_transfer_operstate(dev, vlandev);
  376. }
  377. break;
  378. case NETDEV_UNREGISTER:
  379. /* twiddle thumbs on netns device moves */
  380. if (dev->reg_state != NETREG_UNREGISTERING)
  381. break;
  382. vlan_group_for_each_dev(grp, i, vlandev) {
  383. /* removal of last vid destroys vlan_info, abort
  384. * afterwards */
  385. if (vlan_info->nr_vids == 1)
  386. last = true;
  387. unregister_vlan_dev(vlandev, &list);
  388. if (last)
  389. break;
  390. }
  391. unregister_netdevice_many(&list);
  392. break;
  393. case NETDEV_PRE_TYPE_CHANGE:
  394. /* Forbid underlaying device to change its type. */
  395. if (vlan_uses_dev(dev))
  396. return NOTIFY_BAD;
  397. break;
  398. case NETDEV_NOTIFY_PEERS:
  399. case NETDEV_BONDING_FAILOVER:
  400. case NETDEV_RESEND_IGMP:
  401. /* Propagate to vlan devices */
  402. vlan_group_for_each_dev(grp, i, vlandev)
  403. call_netdevice_notifiers(event, vlandev);
  404. break;
  405. }
  406. out:
  407. return NOTIFY_DONE;
  408. }
  409. static struct notifier_block vlan_notifier_block __read_mostly = {
  410. .notifier_call = vlan_device_event,
  411. };
  412. /*
  413. * VLAN IOCTL handler.
  414. * o execute requested action or pass command to the device driver
  415. * arg is really a struct vlan_ioctl_args __user *.
  416. */
  417. static int vlan_ioctl_handler(struct net *net, void __user *arg)
  418. {
  419. int err;
  420. struct vlan_ioctl_args args;
  421. struct net_device *dev = NULL;
  422. if (copy_from_user(&args, arg, sizeof(struct vlan_ioctl_args)))
  423. return -EFAULT;
  424. /* Null terminate this sucker, just in case. */
  425. args.device1[23] = 0;
  426. args.u.device2[23] = 0;
  427. rtnl_lock();
  428. switch (args.cmd) {
  429. case SET_VLAN_INGRESS_PRIORITY_CMD:
  430. case SET_VLAN_EGRESS_PRIORITY_CMD:
  431. case SET_VLAN_FLAG_CMD:
  432. case ADD_VLAN_CMD:
  433. case DEL_VLAN_CMD:
  434. case GET_VLAN_REALDEV_NAME_CMD:
  435. case GET_VLAN_VID_CMD:
  436. err = -ENODEV;
  437. dev = __dev_get_by_name(net, args.device1);
  438. if (!dev)
  439. goto out;
  440. err = -EINVAL;
  441. if (args.cmd != ADD_VLAN_CMD && !is_vlan_dev(dev))
  442. goto out;
  443. }
  444. switch (args.cmd) {
  445. case SET_VLAN_INGRESS_PRIORITY_CMD:
  446. err = -EPERM;
  447. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  448. break;
  449. vlan_dev_set_ingress_priority(dev,
  450. args.u.skb_priority,
  451. args.vlan_qos);
  452. err = 0;
  453. break;
  454. case SET_VLAN_EGRESS_PRIORITY_CMD:
  455. err = -EPERM;
  456. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  457. break;
  458. err = vlan_dev_set_egress_priority(dev,
  459. args.u.skb_priority,
  460. args.vlan_qos);
  461. break;
  462. case SET_VLAN_FLAG_CMD:
  463. err = -EPERM;
  464. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  465. break;
  466. err = vlan_dev_change_flags(dev,
  467. args.vlan_qos ? args.u.flag : 0,
  468. args.u.flag);
  469. break;
  470. case SET_VLAN_NAME_TYPE_CMD:
  471. err = -EPERM;
  472. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  473. break;
  474. if ((args.u.name_type >= 0) &&
  475. (args.u.name_type < VLAN_NAME_TYPE_HIGHEST)) {
  476. struct vlan_net *vn;
  477. vn = net_generic(net, vlan_net_id);
  478. vn->name_type = args.u.name_type;
  479. err = 0;
  480. } else {
  481. err = -EINVAL;
  482. }
  483. break;
  484. case ADD_VLAN_CMD:
  485. err = -EPERM;
  486. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  487. break;
  488. err = register_vlan_device(dev, args.u.VID);
  489. break;
  490. case DEL_VLAN_CMD:
  491. err = -EPERM;
  492. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  493. break;
  494. unregister_vlan_dev(dev, NULL);
  495. err = 0;
  496. break;
  497. case GET_VLAN_REALDEV_NAME_CMD:
  498. err = 0;
  499. vlan_dev_get_realdev_name(dev, args.u.device2);
  500. if (copy_to_user(arg, &args,
  501. sizeof(struct vlan_ioctl_args)))
  502. err = -EFAULT;
  503. break;
  504. case GET_VLAN_VID_CMD:
  505. err = 0;
  506. args.u.VID = vlan_dev_vlan_id(dev);
  507. if (copy_to_user(arg, &args,
  508. sizeof(struct vlan_ioctl_args)))
  509. err = -EFAULT;
  510. break;
  511. default:
  512. err = -EOPNOTSUPP;
  513. break;
  514. }
  515. out:
  516. rtnl_unlock();
  517. return err;
  518. }
  519. static struct sk_buff **vlan_gro_receive(struct sk_buff **head,
  520. struct sk_buff *skb)
  521. {
  522. struct sk_buff *p, **pp = NULL;
  523. struct vlan_hdr *vhdr;
  524. unsigned int hlen, off_vlan;
  525. const struct packet_offload *ptype;
  526. __be16 type;
  527. int flush = 1;
  528. off_vlan = skb_gro_offset(skb);
  529. hlen = off_vlan + sizeof(*vhdr);
  530. vhdr = skb_gro_header_fast(skb, off_vlan);
  531. if (skb_gro_header_hard(skb, hlen)) {
  532. vhdr = skb_gro_header_slow(skb, hlen, off_vlan);
  533. if (unlikely(!vhdr))
  534. goto out;
  535. }
  536. type = vhdr->h_vlan_encapsulated_proto;
  537. rcu_read_lock();
  538. ptype = gro_find_receive_by_type(type);
  539. if (!ptype)
  540. goto out_unlock;
  541. flush = 0;
  542. for (p = *head; p; p = p->next) {
  543. struct vlan_hdr *vhdr2;
  544. if (!NAPI_GRO_CB(p)->same_flow)
  545. continue;
  546. vhdr2 = (struct vlan_hdr *)(p->data + off_vlan);
  547. if (compare_vlan_header(vhdr, vhdr2))
  548. NAPI_GRO_CB(p)->same_flow = 0;
  549. }
  550. skb_gro_pull(skb, sizeof(*vhdr));
  551. skb_gro_postpull_rcsum(skb, vhdr, sizeof(*vhdr));
  552. pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
  553. out_unlock:
  554. rcu_read_unlock();
  555. out:
  556. NAPI_GRO_CB(skb)->flush |= flush;
  557. return pp;
  558. }
  559. static int vlan_gro_complete(struct sk_buff *skb, int nhoff)
  560. {
  561. struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data + nhoff);
  562. __be16 type = vhdr->h_vlan_encapsulated_proto;
  563. struct packet_offload *ptype;
  564. int err = -ENOENT;
  565. rcu_read_lock();
  566. ptype = gro_find_complete_by_type(type);
  567. if (ptype)
  568. err = ptype->callbacks.gro_complete(skb, nhoff + sizeof(*vhdr));
  569. rcu_read_unlock();
  570. return err;
  571. }
  572. static struct packet_offload vlan_packet_offloads[] __read_mostly = {
  573. {
  574. .type = cpu_to_be16(ETH_P_8021Q),
  575. .priority = 10,
  576. .callbacks = {
  577. .gro_receive = vlan_gro_receive,
  578. .gro_complete = vlan_gro_complete,
  579. },
  580. },
  581. {
  582. .type = cpu_to_be16(ETH_P_8021AD),
  583. .priority = 10,
  584. .callbacks = {
  585. .gro_receive = vlan_gro_receive,
  586. .gro_complete = vlan_gro_complete,
  587. },
  588. },
  589. };
  590. static int __net_init vlan_init_net(struct net *net)
  591. {
  592. struct vlan_net *vn = net_generic(net, vlan_net_id);
  593. int err;
  594. vn->name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD;
  595. err = vlan_proc_init(net);
  596. return err;
  597. }
  598. static void __net_exit vlan_exit_net(struct net *net)
  599. {
  600. vlan_proc_cleanup(net);
  601. }
  602. static struct pernet_operations vlan_net_ops = {
  603. .init = vlan_init_net,
  604. .exit = vlan_exit_net,
  605. .id = &vlan_net_id,
  606. .size = sizeof(struct vlan_net),
  607. };
  608. static int __init vlan_proto_init(void)
  609. {
  610. int err;
  611. unsigned int i;
  612. pr_info("%s v%s\n", vlan_fullname, vlan_version);
  613. err = register_pernet_subsys(&vlan_net_ops);
  614. if (err < 0)
  615. goto err0;
  616. err = register_netdevice_notifier(&vlan_notifier_block);
  617. if (err < 0)
  618. goto err2;
  619. err = vlan_gvrp_init();
  620. if (err < 0)
  621. goto err3;
  622. err = vlan_mvrp_init();
  623. if (err < 0)
  624. goto err4;
  625. err = vlan_netlink_init();
  626. if (err < 0)
  627. goto err5;
  628. for (i = 0; i < ARRAY_SIZE(vlan_packet_offloads); i++)
  629. dev_add_offload(&vlan_packet_offloads[i]);
  630. vlan_ioctl_set(vlan_ioctl_handler);
  631. return 0;
  632. err5:
  633. vlan_mvrp_uninit();
  634. err4:
  635. vlan_gvrp_uninit();
  636. err3:
  637. unregister_netdevice_notifier(&vlan_notifier_block);
  638. err2:
  639. unregister_pernet_subsys(&vlan_net_ops);
  640. err0:
  641. return err;
  642. }
  643. static void __exit vlan_cleanup_module(void)
  644. {
  645. unsigned int i;
  646. vlan_ioctl_set(NULL);
  647. for (i = 0; i < ARRAY_SIZE(vlan_packet_offloads); i++)
  648. dev_remove_offload(&vlan_packet_offloads[i]);
  649. vlan_netlink_fini();
  650. unregister_netdevice_notifier(&vlan_notifier_block);
  651. unregister_pernet_subsys(&vlan_net_ops);
  652. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  653. vlan_mvrp_uninit();
  654. vlan_gvrp_uninit();
  655. }
  656. module_init(vlan_proto_init);
  657. module_exit(vlan_cleanup_module);
  658. MODULE_LICENSE("GPL");
  659. MODULE_VERSION(DRV_VERSION);