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