vlan_core.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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,
  139. const struct vlan_vid_info *vid_info)
  140. {
  141. if (vid_info->proto == htons(ETH_P_8021Q) &&
  142. dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
  143. return true;
  144. if (vid_info->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_vid_add(struct vlan_info *vlan_info, __be16 proto, u16 vid,
  170. struct vlan_vid_info **pvid_info)
  171. {
  172. struct net_device *dev = vlan_info->real_dev;
  173. const struct net_device_ops *ops = dev->netdev_ops;
  174. struct vlan_vid_info *vid_info;
  175. int err;
  176. vid_info = vlan_vid_info_alloc(proto, vid);
  177. if (!vid_info)
  178. return -ENOMEM;
  179. if (vlan_hw_filter_capable(dev, vid_info)) {
  180. if (netif_device_present(dev))
  181. err = ops->ndo_vlan_rx_add_vid(dev, proto, vid);
  182. else
  183. err = -ENODEV;
  184. if (err) {
  185. kfree(vid_info);
  186. return err;
  187. }
  188. }
  189. list_add(&vid_info->list, &vlan_info->vid_list);
  190. vlan_info->nr_vids++;
  191. *pvid_info = vid_info;
  192. return 0;
  193. }
  194. int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
  195. {
  196. struct vlan_info *vlan_info;
  197. struct vlan_vid_info *vid_info;
  198. bool vlan_info_created = false;
  199. int err;
  200. ASSERT_RTNL();
  201. vlan_info = rtnl_dereference(dev->vlan_info);
  202. if (!vlan_info) {
  203. vlan_info = vlan_info_alloc(dev);
  204. if (!vlan_info)
  205. return -ENOMEM;
  206. vlan_info_created = true;
  207. }
  208. vid_info = vlan_vid_info_get(vlan_info, proto, vid);
  209. if (!vid_info) {
  210. err = __vlan_vid_add(vlan_info, proto, vid, &vid_info);
  211. if (err)
  212. goto out_free_vlan_info;
  213. }
  214. vid_info->refcount++;
  215. if (vlan_info_created)
  216. rcu_assign_pointer(dev->vlan_info, vlan_info);
  217. return 0;
  218. out_free_vlan_info:
  219. if (vlan_info_created)
  220. kfree(vlan_info);
  221. return err;
  222. }
  223. EXPORT_SYMBOL(vlan_vid_add);
  224. static void __vlan_vid_del(struct vlan_info *vlan_info,
  225. struct vlan_vid_info *vid_info)
  226. {
  227. struct net_device *dev = vlan_info->real_dev;
  228. const struct net_device_ops *ops = dev->netdev_ops;
  229. __be16 proto = vid_info->proto;
  230. u16 vid = vid_info->vid;
  231. int err;
  232. if (vlan_hw_filter_capable(dev, vid_info)) {
  233. if (netif_device_present(dev))
  234. err = ops->ndo_vlan_rx_kill_vid(dev, proto, vid);
  235. else
  236. err = -ENODEV;
  237. if (err) {
  238. pr_warn("failed to kill vid %04x/%d for device %s\n",
  239. proto, vid, dev->name);
  240. }
  241. }
  242. list_del(&vid_info->list);
  243. kfree(vid_info);
  244. vlan_info->nr_vids--;
  245. }
  246. void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
  247. {
  248. struct vlan_info *vlan_info;
  249. struct vlan_vid_info *vid_info;
  250. ASSERT_RTNL();
  251. vlan_info = rtnl_dereference(dev->vlan_info);
  252. if (!vlan_info)
  253. return;
  254. vid_info = vlan_vid_info_get(vlan_info, proto, vid);
  255. if (!vid_info)
  256. return;
  257. vid_info->refcount--;
  258. if (vid_info->refcount == 0) {
  259. __vlan_vid_del(vlan_info, vid_info);
  260. if (vlan_info->nr_vids == 0) {
  261. RCU_INIT_POINTER(dev->vlan_info, NULL);
  262. call_rcu(&vlan_info->rcu, vlan_info_rcu_free);
  263. }
  264. }
  265. }
  266. EXPORT_SYMBOL(vlan_vid_del);
  267. int vlan_vids_add_by_dev(struct net_device *dev,
  268. const struct net_device *by_dev)
  269. {
  270. struct vlan_vid_info *vid_info;
  271. struct vlan_info *vlan_info;
  272. int err;
  273. ASSERT_RTNL();
  274. vlan_info = rtnl_dereference(by_dev->vlan_info);
  275. if (!vlan_info)
  276. return 0;
  277. list_for_each_entry(vid_info, &vlan_info->vid_list, list) {
  278. err = vlan_vid_add(dev, vid_info->proto, vid_info->vid);
  279. if (err)
  280. goto unwind;
  281. }
  282. return 0;
  283. unwind:
  284. list_for_each_entry_continue_reverse(vid_info,
  285. &vlan_info->vid_list,
  286. list) {
  287. vlan_vid_del(dev, vid_info->proto, vid_info->vid);
  288. }
  289. return err;
  290. }
  291. EXPORT_SYMBOL(vlan_vids_add_by_dev);
  292. void vlan_vids_del_by_dev(struct net_device *dev,
  293. const struct net_device *by_dev)
  294. {
  295. struct vlan_vid_info *vid_info;
  296. struct vlan_info *vlan_info;
  297. ASSERT_RTNL();
  298. vlan_info = rtnl_dereference(by_dev->vlan_info);
  299. if (!vlan_info)
  300. return;
  301. list_for_each_entry(vid_info, &vlan_info->vid_list, list)
  302. vlan_vid_del(dev, vid_info->proto, vid_info->vid);
  303. }
  304. EXPORT_SYMBOL(vlan_vids_del_by_dev);
  305. bool vlan_uses_dev(const struct net_device *dev)
  306. {
  307. struct vlan_info *vlan_info;
  308. ASSERT_RTNL();
  309. vlan_info = rtnl_dereference(dev->vlan_info);
  310. if (!vlan_info)
  311. return false;
  312. return vlan_info->grp.nr_vlan_devs ? true : false;
  313. }
  314. EXPORT_SYMBOL(vlan_uses_dev);