vlan_core.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. skb->vlan_tci = 0;
  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_filter_push_vids(struct vlan_info *vlan_info, __be16 proto)
  187. {
  188. struct net_device *real_dev = vlan_info->real_dev;
  189. struct vlan_vid_info *vlan_vid_info;
  190. int err;
  191. list_for_each_entry(vlan_vid_info, &vlan_info->vid_list, list) {
  192. if (vlan_vid_info->proto == proto) {
  193. err = vlan_add_rx_filter_info(real_dev, proto,
  194. vlan_vid_info->vid);
  195. if (err)
  196. goto unwind;
  197. }
  198. }
  199. return 0;
  200. unwind:
  201. list_for_each_entry_continue_reverse(vlan_vid_info,
  202. &vlan_info->vid_list, list) {
  203. if (vlan_vid_info->proto == proto)
  204. vlan_kill_rx_filter_info(real_dev, proto,
  205. vlan_vid_info->vid);
  206. }
  207. return err;
  208. }
  209. EXPORT_SYMBOL(vlan_filter_push_vids);
  210. void vlan_filter_drop_vids(struct vlan_info *vlan_info, __be16 proto)
  211. {
  212. struct vlan_vid_info *vlan_vid_info;
  213. list_for_each_entry(vlan_vid_info, &vlan_info->vid_list, list)
  214. if (vlan_vid_info->proto == proto)
  215. vlan_kill_rx_filter_info(vlan_info->real_dev,
  216. vlan_vid_info->proto,
  217. vlan_vid_info->vid);
  218. }
  219. EXPORT_SYMBOL(vlan_filter_drop_vids);
  220. static int __vlan_vid_add(struct vlan_info *vlan_info, __be16 proto, u16 vid,
  221. struct vlan_vid_info **pvid_info)
  222. {
  223. struct net_device *dev = vlan_info->real_dev;
  224. struct vlan_vid_info *vid_info;
  225. int err;
  226. vid_info = vlan_vid_info_alloc(proto, vid);
  227. if (!vid_info)
  228. return -ENOMEM;
  229. err = vlan_add_rx_filter_info(dev, proto, vid);
  230. if (err) {
  231. kfree(vid_info);
  232. return err;
  233. }
  234. list_add(&vid_info->list, &vlan_info->vid_list);
  235. vlan_info->nr_vids++;
  236. *pvid_info = vid_info;
  237. return 0;
  238. }
  239. int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
  240. {
  241. struct vlan_info *vlan_info;
  242. struct vlan_vid_info *vid_info;
  243. bool vlan_info_created = false;
  244. int err;
  245. ASSERT_RTNL();
  246. vlan_info = rtnl_dereference(dev->vlan_info);
  247. if (!vlan_info) {
  248. vlan_info = vlan_info_alloc(dev);
  249. if (!vlan_info)
  250. return -ENOMEM;
  251. vlan_info_created = true;
  252. }
  253. vid_info = vlan_vid_info_get(vlan_info, proto, vid);
  254. if (!vid_info) {
  255. err = __vlan_vid_add(vlan_info, proto, vid, &vid_info);
  256. if (err)
  257. goto out_free_vlan_info;
  258. }
  259. vid_info->refcount++;
  260. if (vlan_info_created)
  261. rcu_assign_pointer(dev->vlan_info, vlan_info);
  262. return 0;
  263. out_free_vlan_info:
  264. if (vlan_info_created)
  265. kfree(vlan_info);
  266. return err;
  267. }
  268. EXPORT_SYMBOL(vlan_vid_add);
  269. static void __vlan_vid_del(struct vlan_info *vlan_info,
  270. struct vlan_vid_info *vid_info)
  271. {
  272. struct net_device *dev = vlan_info->real_dev;
  273. __be16 proto = vid_info->proto;
  274. u16 vid = vid_info->vid;
  275. int err;
  276. err = vlan_kill_rx_filter_info(dev, proto, vid);
  277. if (err)
  278. pr_warn("failed to kill vid %04x/%d for device %s\n",
  279. proto, vid, dev->name);
  280. list_del(&vid_info->list);
  281. kfree(vid_info);
  282. vlan_info->nr_vids--;
  283. }
  284. void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
  285. {
  286. struct vlan_info *vlan_info;
  287. struct vlan_vid_info *vid_info;
  288. ASSERT_RTNL();
  289. vlan_info = rtnl_dereference(dev->vlan_info);
  290. if (!vlan_info)
  291. return;
  292. vid_info = vlan_vid_info_get(vlan_info, proto, vid);
  293. if (!vid_info)
  294. return;
  295. vid_info->refcount--;
  296. if (vid_info->refcount == 0) {
  297. __vlan_vid_del(vlan_info, vid_info);
  298. if (vlan_info->nr_vids == 0) {
  299. RCU_INIT_POINTER(dev->vlan_info, NULL);
  300. call_rcu(&vlan_info->rcu, vlan_info_rcu_free);
  301. }
  302. }
  303. }
  304. EXPORT_SYMBOL(vlan_vid_del);
  305. int vlan_vids_add_by_dev(struct net_device *dev,
  306. const struct net_device *by_dev)
  307. {
  308. struct vlan_vid_info *vid_info;
  309. struct vlan_info *vlan_info;
  310. int err;
  311. ASSERT_RTNL();
  312. vlan_info = rtnl_dereference(by_dev->vlan_info);
  313. if (!vlan_info)
  314. return 0;
  315. list_for_each_entry(vid_info, &vlan_info->vid_list, list) {
  316. err = vlan_vid_add(dev, vid_info->proto, vid_info->vid);
  317. if (err)
  318. goto unwind;
  319. }
  320. return 0;
  321. unwind:
  322. list_for_each_entry_continue_reverse(vid_info,
  323. &vlan_info->vid_list,
  324. list) {
  325. vlan_vid_del(dev, vid_info->proto, vid_info->vid);
  326. }
  327. return err;
  328. }
  329. EXPORT_SYMBOL(vlan_vids_add_by_dev);
  330. void vlan_vids_del_by_dev(struct net_device *dev,
  331. const struct net_device *by_dev)
  332. {
  333. struct vlan_vid_info *vid_info;
  334. struct vlan_info *vlan_info;
  335. ASSERT_RTNL();
  336. vlan_info = rtnl_dereference(by_dev->vlan_info);
  337. if (!vlan_info)
  338. return;
  339. list_for_each_entry(vid_info, &vlan_info->vid_list, list)
  340. vlan_vid_del(dev, vid_info->proto, vid_info->vid);
  341. }
  342. EXPORT_SYMBOL(vlan_vids_del_by_dev);
  343. bool vlan_uses_dev(const struct net_device *dev)
  344. {
  345. struct vlan_info *vlan_info;
  346. ASSERT_RTNL();
  347. vlan_info = rtnl_dereference(dev->vlan_info);
  348. if (!vlan_info)
  349. return false;
  350. return vlan_info->grp.nr_vlan_devs ? true : false;
  351. }
  352. EXPORT_SYMBOL(vlan_uses_dev);