vlan_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/skbuff.h>
  3. #include <linux/netdevice.h>
  4. #include <linux/if_vlan.h>
  5. #include <linux/netpoll.h>
  6. #include <linux/export.h>
  7. #include <net/gro.h>
  8. #include "vlan.h"
  9. bool vlan_do_receive(struct sk_buff **skbp)
  10. {
  11. struct sk_buff *skb = *skbp;
  12. __be16 vlan_proto = skb->vlan_proto;
  13. u16 vlan_id = skb_vlan_tag_get_id(skb);
  14. struct net_device *vlan_dev;
  15. struct vlan_pcpu_stats *rx_stats;
  16. vlan_dev = vlan_find_dev(skb->dev, vlan_proto, vlan_id);
  17. if (!vlan_dev)
  18. return false;
  19. skb = *skbp = skb_share_check(skb, GFP_ATOMIC);
  20. if (unlikely(!skb))
  21. return false;
  22. if (unlikely(!(vlan_dev->flags & IFF_UP))) {
  23. kfree_skb(skb);
  24. *skbp = NULL;
  25. return false;
  26. }
  27. skb->dev = vlan_dev;
  28. if (unlikely(skb->pkt_type == PACKET_OTHERHOST)) {
  29. /* Our lower layer thinks this is not local, let's make sure.
  30. * This allows the VLAN to have a different MAC than the
  31. * underlying device, and still route correctly. */
  32. if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, vlan_dev->dev_addr))
  33. skb->pkt_type = PACKET_HOST;
  34. }
  35. if (!(vlan_dev_priv(vlan_dev)->flags & VLAN_FLAG_REORDER_HDR) &&
  36. !netif_is_macvlan_port(vlan_dev) &&
  37. !netif_is_bridge_port(vlan_dev)) {
  38. unsigned int offset = skb->data - skb_mac_header(skb);
  39. /*
  40. * vlan_insert_tag expect skb->data pointing to mac header.
  41. * So change skb->data before calling it and change back to
  42. * original position later
  43. */
  44. skb_push(skb, offset);
  45. skb = *skbp = vlan_insert_inner_tag(skb, skb->vlan_proto,
  46. skb->vlan_tci, skb->mac_len);
  47. if (!skb)
  48. return false;
  49. skb_pull(skb, offset + VLAN_HLEN);
  50. skb_reset_mac_len(skb);
  51. }
  52. skb->priority = vlan_get_ingress_priority(vlan_dev, skb->vlan_tci);
  53. __vlan_hwaccel_clear_tag(skb);
  54. rx_stats = this_cpu_ptr(vlan_dev_priv(vlan_dev)->vlan_pcpu_stats);
  55. u64_stats_update_begin(&rx_stats->syncp);
  56. u64_stats_inc(&rx_stats->rx_packets);
  57. u64_stats_add(&rx_stats->rx_bytes, skb->len);
  58. if (skb->pkt_type == PACKET_MULTICAST)
  59. u64_stats_inc(&rx_stats->rx_multicast);
  60. u64_stats_update_end(&rx_stats->syncp);
  61. return true;
  62. }
  63. /* Must be invoked with rcu_read_lock. */
  64. struct net_device *__vlan_find_dev_deep_rcu(struct net_device *dev,
  65. __be16 vlan_proto, u16 vlan_id)
  66. {
  67. struct vlan_info *vlan_info = rcu_dereference(dev->vlan_info);
  68. if (vlan_info) {
  69. return vlan_group_get_device(&vlan_info->grp,
  70. vlan_proto, vlan_id);
  71. } else {
  72. /*
  73. * Lower devices of master uppers (bonding, team) do not have
  74. * grp assigned to themselves. Grp is assigned to upper device
  75. * instead.
  76. */
  77. struct net_device *upper_dev;
  78. upper_dev = netdev_master_upper_dev_get_rcu(dev);
  79. if (upper_dev)
  80. return __vlan_find_dev_deep_rcu(upper_dev,
  81. vlan_proto, vlan_id);
  82. }
  83. return NULL;
  84. }
  85. EXPORT_SYMBOL(__vlan_find_dev_deep_rcu);
  86. struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  87. {
  88. struct net_device *ret = vlan_dev_priv(dev)->real_dev;
  89. while (is_vlan_dev(ret))
  90. ret = vlan_dev_priv(ret)->real_dev;
  91. return ret;
  92. }
  93. EXPORT_SYMBOL(vlan_dev_real_dev);
  94. u16 vlan_dev_vlan_id(const struct net_device *dev)
  95. {
  96. return vlan_dev_priv(dev)->vlan_id;
  97. }
  98. EXPORT_SYMBOL(vlan_dev_vlan_id);
  99. __be16 vlan_dev_vlan_proto(const struct net_device *dev)
  100. {
  101. return vlan_dev_priv(dev)->vlan_proto;
  102. }
  103. EXPORT_SYMBOL(vlan_dev_vlan_proto);
  104. /*
  105. * vlan info and vid list
  106. */
  107. static void vlan_group_free(struct vlan_group *grp)
  108. {
  109. int i, j;
  110. for (i = 0; i < VLAN_PROTO_NUM; i++)
  111. for (j = 0; j < VLAN_GROUP_ARRAY_SPLIT_PARTS; j++)
  112. kfree(grp->vlan_devices_arrays[i][j]);
  113. }
  114. static void vlan_info_free(struct vlan_info *vlan_info)
  115. {
  116. vlan_group_free(&vlan_info->grp);
  117. kfree(vlan_info);
  118. }
  119. static void vlan_info_rcu_free(struct rcu_head *rcu)
  120. {
  121. vlan_info_free(container_of(rcu, struct vlan_info, rcu));
  122. }
  123. static struct vlan_info *vlan_info_alloc(struct net_device *dev)
  124. {
  125. struct vlan_info *vlan_info;
  126. vlan_info = kzalloc(sizeof(struct vlan_info), GFP_KERNEL);
  127. if (!vlan_info)
  128. return NULL;
  129. vlan_info->real_dev = dev;
  130. INIT_LIST_HEAD(&vlan_info->vid_list);
  131. return vlan_info;
  132. }
  133. struct vlan_vid_info {
  134. struct list_head list;
  135. __be16 proto;
  136. u16 vid;
  137. int refcount;
  138. };
  139. static bool vlan_hw_filter_capable(const struct net_device *dev, __be16 proto)
  140. {
  141. if (proto == htons(ETH_P_8021Q) &&
  142. dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
  143. return true;
  144. if (proto == htons(ETH_P_8021AD) &&
  145. dev->features & NETIF_F_HW_VLAN_STAG_FILTER)
  146. return true;
  147. return false;
  148. }
  149. static struct vlan_vid_info *vlan_vid_info_get(struct vlan_info *vlan_info,
  150. __be16 proto, u16 vid)
  151. {
  152. struct vlan_vid_info *vid_info;
  153. list_for_each_entry(vid_info, &vlan_info->vid_list, list) {
  154. if (vid_info->proto == proto && vid_info->vid == vid)
  155. return vid_info;
  156. }
  157. return NULL;
  158. }
  159. static struct vlan_vid_info *vlan_vid_info_alloc(__be16 proto, u16 vid)
  160. {
  161. struct vlan_vid_info *vid_info;
  162. vid_info = kzalloc(sizeof(struct vlan_vid_info), GFP_KERNEL);
  163. if (!vid_info)
  164. return NULL;
  165. vid_info->proto = proto;
  166. vid_info->vid = vid;
  167. return vid_info;
  168. }
  169. static int vlan_add_rx_filter_info(struct net_device *dev, __be16 proto, u16 vid)
  170. {
  171. if (!vlan_hw_filter_capable(dev, proto))
  172. return 0;
  173. if (netif_device_present(dev))
  174. return dev->netdev_ops->ndo_vlan_rx_add_vid(dev, proto, vid);
  175. else
  176. return -ENODEV;
  177. }
  178. static int vlan_kill_rx_filter_info(struct net_device *dev, __be16 proto, u16 vid)
  179. {
  180. if (!vlan_hw_filter_capable(dev, proto))
  181. return 0;
  182. if (netif_device_present(dev))
  183. return dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, proto, vid);
  184. else
  185. return -ENODEV;
  186. }
  187. int vlan_for_each(struct net_device *dev,
  188. int (*action)(struct net_device *dev, int vid, void *arg),
  189. void *arg)
  190. {
  191. struct vlan_vid_info *vid_info;
  192. struct vlan_info *vlan_info;
  193. struct net_device *vdev;
  194. int ret;
  195. ASSERT_RTNL();
  196. vlan_info = rtnl_dereference(dev->vlan_info);
  197. if (!vlan_info)
  198. return 0;
  199. list_for_each_entry(vid_info, &vlan_info->vid_list, list) {
  200. vdev = vlan_group_get_device(&vlan_info->grp, vid_info->proto,
  201. vid_info->vid);
  202. ret = action(vdev, vid_info->vid, arg);
  203. if (ret)
  204. return ret;
  205. }
  206. return 0;
  207. }
  208. EXPORT_SYMBOL(vlan_for_each);
  209. int vlan_filter_push_vids(struct vlan_info *vlan_info, __be16 proto)
  210. {
  211. struct net_device *real_dev = vlan_info->real_dev;
  212. struct vlan_vid_info *vlan_vid_info;
  213. int err;
  214. list_for_each_entry(vlan_vid_info, &vlan_info->vid_list, list) {
  215. if (vlan_vid_info->proto == proto) {
  216. err = vlan_add_rx_filter_info(real_dev, proto,
  217. vlan_vid_info->vid);
  218. if (err)
  219. goto unwind;
  220. }
  221. }
  222. return 0;
  223. unwind:
  224. list_for_each_entry_continue_reverse(vlan_vid_info,
  225. &vlan_info->vid_list, list) {
  226. if (vlan_vid_info->proto == proto)
  227. vlan_kill_rx_filter_info(real_dev, proto,
  228. vlan_vid_info->vid);
  229. }
  230. return err;
  231. }
  232. EXPORT_SYMBOL(vlan_filter_push_vids);
  233. void vlan_filter_drop_vids(struct vlan_info *vlan_info, __be16 proto)
  234. {
  235. struct vlan_vid_info *vlan_vid_info;
  236. list_for_each_entry(vlan_vid_info, &vlan_info->vid_list, list)
  237. if (vlan_vid_info->proto == proto)
  238. vlan_kill_rx_filter_info(vlan_info->real_dev,
  239. vlan_vid_info->proto,
  240. vlan_vid_info->vid);
  241. }
  242. EXPORT_SYMBOL(vlan_filter_drop_vids);
  243. static int __vlan_vid_add(struct vlan_info *vlan_info, __be16 proto, u16 vid,
  244. struct vlan_vid_info **pvid_info)
  245. {
  246. struct net_device *dev = vlan_info->real_dev;
  247. struct vlan_vid_info *vid_info;
  248. int err;
  249. vid_info = vlan_vid_info_alloc(proto, vid);
  250. if (!vid_info)
  251. return -ENOMEM;
  252. err = vlan_add_rx_filter_info(dev, proto, vid);
  253. if (err) {
  254. kfree(vid_info);
  255. return err;
  256. }
  257. list_add(&vid_info->list, &vlan_info->vid_list);
  258. vlan_info->nr_vids++;
  259. *pvid_info = vid_info;
  260. return 0;
  261. }
  262. int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
  263. {
  264. struct vlan_info *vlan_info;
  265. struct vlan_vid_info *vid_info;
  266. bool vlan_info_created = false;
  267. int err;
  268. ASSERT_RTNL();
  269. vlan_info = rtnl_dereference(dev->vlan_info);
  270. if (!vlan_info) {
  271. vlan_info = vlan_info_alloc(dev);
  272. if (!vlan_info)
  273. return -ENOMEM;
  274. vlan_info_created = true;
  275. }
  276. vid_info = vlan_vid_info_get(vlan_info, proto, vid);
  277. if (!vid_info) {
  278. err = __vlan_vid_add(vlan_info, proto, vid, &vid_info);
  279. if (err)
  280. goto out_free_vlan_info;
  281. }
  282. vid_info->refcount++;
  283. if (vlan_info_created)
  284. rcu_assign_pointer(dev->vlan_info, vlan_info);
  285. return 0;
  286. out_free_vlan_info:
  287. if (vlan_info_created)
  288. kfree(vlan_info);
  289. return err;
  290. }
  291. EXPORT_SYMBOL(vlan_vid_add);
  292. static void __vlan_vid_del(struct vlan_info *vlan_info,
  293. struct vlan_vid_info *vid_info)
  294. {
  295. struct net_device *dev = vlan_info->real_dev;
  296. __be16 proto = vid_info->proto;
  297. u16 vid = vid_info->vid;
  298. int err;
  299. err = vlan_kill_rx_filter_info(dev, proto, vid);
  300. if (err && dev->reg_state != NETREG_UNREGISTERING)
  301. netdev_warn(dev, "failed to kill vid %04x/%d\n", proto, vid);
  302. list_del(&vid_info->list);
  303. kfree(vid_info);
  304. vlan_info->nr_vids--;
  305. }
  306. void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
  307. {
  308. struct vlan_info *vlan_info;
  309. struct vlan_vid_info *vid_info;
  310. ASSERT_RTNL();
  311. vlan_info = rtnl_dereference(dev->vlan_info);
  312. if (!vlan_info)
  313. return;
  314. vid_info = vlan_vid_info_get(vlan_info, proto, vid);
  315. if (!vid_info)
  316. return;
  317. vid_info->refcount--;
  318. if (vid_info->refcount == 0) {
  319. __vlan_vid_del(vlan_info, vid_info);
  320. if (vlan_info->nr_vids == 0) {
  321. RCU_INIT_POINTER(dev->vlan_info, NULL);
  322. call_rcu(&vlan_info->rcu, vlan_info_rcu_free);
  323. }
  324. }
  325. }
  326. EXPORT_SYMBOL(vlan_vid_del);
  327. int vlan_vids_add_by_dev(struct net_device *dev,
  328. const struct net_device *by_dev)
  329. {
  330. struct vlan_vid_info *vid_info;
  331. struct vlan_info *vlan_info;
  332. int err;
  333. ASSERT_RTNL();
  334. vlan_info = rtnl_dereference(by_dev->vlan_info);
  335. if (!vlan_info)
  336. return 0;
  337. list_for_each_entry(vid_info, &vlan_info->vid_list, list) {
  338. if (!vlan_hw_filter_capable(by_dev, vid_info->proto))
  339. continue;
  340. err = vlan_vid_add(dev, vid_info->proto, vid_info->vid);
  341. if (err)
  342. goto unwind;
  343. }
  344. return 0;
  345. unwind:
  346. list_for_each_entry_continue_reverse(vid_info,
  347. &vlan_info->vid_list,
  348. list) {
  349. if (!vlan_hw_filter_capable(by_dev, vid_info->proto))
  350. continue;
  351. vlan_vid_del(dev, vid_info->proto, vid_info->vid);
  352. }
  353. return err;
  354. }
  355. EXPORT_SYMBOL(vlan_vids_add_by_dev);
  356. void vlan_vids_del_by_dev(struct net_device *dev,
  357. const struct net_device *by_dev)
  358. {
  359. struct vlan_vid_info *vid_info;
  360. struct vlan_info *vlan_info;
  361. ASSERT_RTNL();
  362. vlan_info = rtnl_dereference(by_dev->vlan_info);
  363. if (!vlan_info)
  364. return;
  365. list_for_each_entry(vid_info, &vlan_info->vid_list, list) {
  366. if (!vlan_hw_filter_capable(by_dev, vid_info->proto))
  367. continue;
  368. vlan_vid_del(dev, vid_info->proto, vid_info->vid);
  369. }
  370. }
  371. EXPORT_SYMBOL(vlan_vids_del_by_dev);
  372. bool vlan_uses_dev(const struct net_device *dev)
  373. {
  374. struct vlan_info *vlan_info;
  375. ASSERT_RTNL();
  376. vlan_info = rtnl_dereference(dev->vlan_info);
  377. if (!vlan_info)
  378. return false;
  379. return vlan_info->grp.nr_vlan_devs ? true : false;
  380. }
  381. EXPORT_SYMBOL(vlan_uses_dev);
  382. static struct sk_buff *vlan_gro_receive(struct list_head *head,
  383. struct sk_buff *skb)
  384. {
  385. const struct packet_offload *ptype;
  386. unsigned int hlen, off_vlan;
  387. struct sk_buff *pp = NULL;
  388. struct vlan_hdr *vhdr;
  389. struct sk_buff *p;
  390. __be16 type;
  391. int flush = 1;
  392. off_vlan = skb_gro_offset(skb);
  393. hlen = off_vlan + sizeof(*vhdr);
  394. vhdr = skb_gro_header(skb, hlen, off_vlan);
  395. if (unlikely(!vhdr))
  396. goto out;
  397. NAPI_GRO_CB(skb)->network_offsets[NAPI_GRO_CB(skb)->encap_mark] = hlen;
  398. type = vhdr->h_vlan_encapsulated_proto;
  399. ptype = gro_find_receive_by_type(type);
  400. if (!ptype)
  401. goto out;
  402. flush = 0;
  403. list_for_each_entry(p, head, list) {
  404. struct vlan_hdr *vhdr2;
  405. if (!NAPI_GRO_CB(p)->same_flow)
  406. continue;
  407. vhdr2 = (struct vlan_hdr *)(p->data + off_vlan);
  408. if (compare_vlan_header(vhdr, vhdr2))
  409. NAPI_GRO_CB(p)->same_flow = 0;
  410. }
  411. skb_gro_pull(skb, sizeof(*vhdr));
  412. skb_gro_postpull_rcsum(skb, vhdr, sizeof(*vhdr));
  413. pp = indirect_call_gro_receive_inet(ptype->callbacks.gro_receive,
  414. ipv6_gro_receive, inet_gro_receive,
  415. head, skb);
  416. out:
  417. skb_gro_flush_final(skb, pp, flush);
  418. return pp;
  419. }
  420. static int vlan_gro_complete(struct sk_buff *skb, int nhoff)
  421. {
  422. struct vlan_hdr *vhdr = (struct vlan_hdr *)(skb->data + nhoff);
  423. __be16 type = vhdr->h_vlan_encapsulated_proto;
  424. struct packet_offload *ptype;
  425. int err = -ENOENT;
  426. ptype = gro_find_complete_by_type(type);
  427. if (ptype)
  428. err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
  429. ipv6_gro_complete, inet_gro_complete,
  430. skb, nhoff + sizeof(*vhdr));
  431. return err;
  432. }
  433. static struct packet_offload vlan_packet_offloads[] __read_mostly = {
  434. {
  435. .type = cpu_to_be16(ETH_P_8021Q),
  436. .priority = 10,
  437. .callbacks = {
  438. .gro_receive = vlan_gro_receive,
  439. .gro_complete = vlan_gro_complete,
  440. },
  441. },
  442. {
  443. .type = cpu_to_be16(ETH_P_8021AD),
  444. .priority = 10,
  445. .callbacks = {
  446. .gro_receive = vlan_gro_receive,
  447. .gro_complete = vlan_gro_complete,
  448. },
  449. },
  450. };
  451. static int __init vlan_offload_init(void)
  452. {
  453. unsigned int i;
  454. for (i = 0; i < ARRAY_SIZE(vlan_packet_offloads); i++)
  455. dev_add_offload(&vlan_packet_offloads[i]);
  456. return 0;
  457. }
  458. fs_initcall(vlan_offload_init);