vlan_core.c 13 KB

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