vlan_core.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include <linux/skbuff.h>
  2. #include <linux/netdevice.h>
  3. #include <linux/if_vlan.h>
  4. #include <linux/netpoll.h>
  5. #include <linux/export.h>
  6. #include "vlan.h"
  7. bool vlan_do_receive(struct sk_buff **skbp)
  8. {
  9. struct sk_buff *skb = *skbp;
  10. __be16 vlan_proto = skb->vlan_proto;
  11. u16 vlan_id = skb_vlan_tag_get_id(skb);
  12. struct net_device *vlan_dev;
  13. struct vlan_pcpu_stats *rx_stats;
  14. vlan_dev = vlan_find_dev(skb->dev, vlan_proto, vlan_id);
  15. if (!vlan_dev)
  16. return false;
  17. skb = *skbp = skb_share_check(skb, GFP_ATOMIC);
  18. if (unlikely(!skb))
  19. return false;
  20. skb->dev = vlan_dev;
  21. if (unlikely(skb->pkt_type == PACKET_OTHERHOST)) {
  22. /* Our lower layer thinks this is not local, let's make sure.
  23. * This allows the VLAN to have a different MAC than the
  24. * underlying device, and still route correctly. */
  25. if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, vlan_dev->dev_addr))
  26. skb->pkt_type = PACKET_HOST;
  27. }
  28. if (!(vlan_dev_priv(vlan_dev)->flags & VLAN_FLAG_REORDER_HDR) &&
  29. !netif_is_macvlan_port(vlan_dev) &&
  30. !netif_is_bridge_port(vlan_dev)) {
  31. unsigned int offset = skb->data - skb_mac_header(skb);
  32. /*
  33. * vlan_insert_tag expect skb->data pointing to mac header.
  34. * So change skb->data before calling it and change back to
  35. * original position later
  36. */
  37. skb_push(skb, offset);
  38. skb = *skbp = vlan_insert_tag(skb, skb->vlan_proto,
  39. skb->vlan_tci);
  40. if (!skb)
  41. return false;
  42. skb_pull(skb, offset + VLAN_HLEN);
  43. skb_reset_mac_len(skb);
  44. }
  45. skb->priority = vlan_get_ingress_priority(vlan_dev, skb->vlan_tci);
  46. skb->vlan_tci = 0;
  47. rx_stats = this_cpu_ptr(vlan_dev_priv(vlan_dev)->vlan_pcpu_stats);
  48. u64_stats_update_begin(&rx_stats->syncp);
  49. rx_stats->rx_packets++;
  50. rx_stats->rx_bytes += skb->len;
  51. if (skb->pkt_type == PACKET_MULTICAST)
  52. rx_stats->rx_multicast++;
  53. u64_stats_update_end(&rx_stats->syncp);
  54. return true;
  55. }
  56. /* Must be invoked with rcu_read_lock. */
  57. struct net_device *__vlan_find_dev_deep_rcu(struct net_device *dev,
  58. __be16 vlan_proto, u16 vlan_id)
  59. {
  60. struct vlan_info *vlan_info = rcu_dereference(dev->vlan_info);
  61. if (vlan_info) {
  62. return vlan_group_get_device(&vlan_info->grp,
  63. vlan_proto, vlan_id);
  64. } else {
  65. /*
  66. * Lower devices of master uppers (bonding, team) do not have
  67. * grp assigned to themselves. Grp is assigned to upper device
  68. * instead.
  69. */
  70. struct net_device *upper_dev;
  71. upper_dev = netdev_master_upper_dev_get_rcu(dev);
  72. if (upper_dev)
  73. return __vlan_find_dev_deep_rcu(upper_dev,
  74. vlan_proto, vlan_id);
  75. }
  76. return NULL;
  77. }
  78. EXPORT_SYMBOL(__vlan_find_dev_deep_rcu);
  79. struct net_device *vlan_dev_real_dev(const struct net_device *dev)
  80. {
  81. struct net_device *ret = vlan_dev_priv(dev)->real_dev;
  82. while (is_vlan_dev(ret))
  83. ret = vlan_dev_priv(ret)->real_dev;
  84. return ret;
  85. }
  86. EXPORT_SYMBOL(vlan_dev_real_dev);
  87. u16 vlan_dev_vlan_id(const struct net_device *dev)
  88. {
  89. return vlan_dev_priv(dev)->vlan_id;
  90. }
  91. EXPORT_SYMBOL(vlan_dev_vlan_id);
  92. __be16 vlan_dev_vlan_proto(const struct net_device *dev)
  93. {
  94. return vlan_dev_priv(dev)->vlan_proto;
  95. }
  96. EXPORT_SYMBOL(vlan_dev_vlan_proto);
  97. /*
  98. * vlan info and vid list
  99. */
  100. static void vlan_group_free(struct vlan_group *grp)
  101. {
  102. int i, j;
  103. for (i = 0; i < VLAN_PROTO_NUM; i++)
  104. for (j = 0; j < VLAN_GROUP_ARRAY_SPLIT_PARTS; j++)
  105. kfree(grp->vlan_devices_arrays[i][j]);
  106. }
  107. static void vlan_info_free(struct vlan_info *vlan_info)
  108. {
  109. vlan_group_free(&vlan_info->grp);
  110. kfree(vlan_info);
  111. }
  112. static void vlan_info_rcu_free(struct rcu_head *rcu)
  113. {
  114. vlan_info_free(container_of(rcu, struct vlan_info, rcu));
  115. }
  116. static struct vlan_info *vlan_info_alloc(struct net_device *dev)
  117. {
  118. struct vlan_info *vlan_info;
  119. vlan_info = kzalloc(sizeof(struct vlan_info), GFP_KERNEL);
  120. if (!vlan_info)
  121. return NULL;
  122. vlan_info->real_dev = dev;
  123. INIT_LIST_HEAD(&vlan_info->vid_list);
  124. return vlan_info;
  125. }
  126. struct vlan_vid_info {
  127. struct list_head list;
  128. __be16 proto;
  129. u16 vid;
  130. int refcount;
  131. };
  132. static bool vlan_hw_filter_capable(const struct net_device *dev,
  133. const struct vlan_vid_info *vid_info)
  134. {
  135. if (vid_info->proto == htons(ETH_P_8021Q) &&
  136. dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
  137. return true;
  138. if (vid_info->proto == htons(ETH_P_8021AD) &&
  139. dev->features & NETIF_F_HW_VLAN_STAG_FILTER)
  140. return true;
  141. return false;
  142. }
  143. static struct vlan_vid_info *vlan_vid_info_get(struct vlan_info *vlan_info,
  144. __be16 proto, u16 vid)
  145. {
  146. struct vlan_vid_info *vid_info;
  147. list_for_each_entry(vid_info, &vlan_info->vid_list, list) {
  148. if (vid_info->proto == proto && vid_info->vid == vid)
  149. return vid_info;
  150. }
  151. return NULL;
  152. }
  153. static struct vlan_vid_info *vlan_vid_info_alloc(__be16 proto, u16 vid)
  154. {
  155. struct vlan_vid_info *vid_info;
  156. vid_info = kzalloc(sizeof(struct vlan_vid_info), GFP_KERNEL);
  157. if (!vid_info)
  158. return NULL;
  159. vid_info->proto = proto;
  160. vid_info->vid = vid;
  161. return vid_info;
  162. }
  163. static int __vlan_vid_add(struct vlan_info *vlan_info, __be16 proto, u16 vid,
  164. struct vlan_vid_info **pvid_info)
  165. {
  166. struct net_device *dev = vlan_info->real_dev;
  167. const struct net_device_ops *ops = dev->netdev_ops;
  168. struct vlan_vid_info *vid_info;
  169. int err;
  170. vid_info = vlan_vid_info_alloc(proto, vid);
  171. if (!vid_info)
  172. return -ENOMEM;
  173. if (vlan_hw_filter_capable(dev, vid_info)) {
  174. if (netif_device_present(dev))
  175. err = ops->ndo_vlan_rx_add_vid(dev, proto, vid);
  176. else
  177. err = -ENODEV;
  178. if (err) {
  179. kfree(vid_info);
  180. return err;
  181. }
  182. }
  183. list_add(&vid_info->list, &vlan_info->vid_list);
  184. vlan_info->nr_vids++;
  185. *pvid_info = vid_info;
  186. return 0;
  187. }
  188. int vlan_vid_add(struct net_device *dev, __be16 proto, u16 vid)
  189. {
  190. struct vlan_info *vlan_info;
  191. struct vlan_vid_info *vid_info;
  192. bool vlan_info_created = false;
  193. int err;
  194. ASSERT_RTNL();
  195. vlan_info = rtnl_dereference(dev->vlan_info);
  196. if (!vlan_info) {
  197. vlan_info = vlan_info_alloc(dev);
  198. if (!vlan_info)
  199. return -ENOMEM;
  200. vlan_info_created = true;
  201. }
  202. vid_info = vlan_vid_info_get(vlan_info, proto, vid);
  203. if (!vid_info) {
  204. err = __vlan_vid_add(vlan_info, proto, vid, &vid_info);
  205. if (err)
  206. goto out_free_vlan_info;
  207. }
  208. vid_info->refcount++;
  209. if (vlan_info_created)
  210. rcu_assign_pointer(dev->vlan_info, vlan_info);
  211. return 0;
  212. out_free_vlan_info:
  213. if (vlan_info_created)
  214. kfree(vlan_info);
  215. return err;
  216. }
  217. EXPORT_SYMBOL(vlan_vid_add);
  218. static void __vlan_vid_del(struct vlan_info *vlan_info,
  219. struct vlan_vid_info *vid_info)
  220. {
  221. struct net_device *dev = vlan_info->real_dev;
  222. const struct net_device_ops *ops = dev->netdev_ops;
  223. __be16 proto = vid_info->proto;
  224. u16 vid = vid_info->vid;
  225. int err;
  226. if (vlan_hw_filter_capable(dev, vid_info)) {
  227. if (netif_device_present(dev))
  228. err = ops->ndo_vlan_rx_kill_vid(dev, proto, vid);
  229. else
  230. err = -ENODEV;
  231. if (err) {
  232. pr_warn("failed to kill vid %04x/%d for device %s\n",
  233. proto, vid, dev->name);
  234. }
  235. }
  236. list_del(&vid_info->list);
  237. kfree(vid_info);
  238. vlan_info->nr_vids--;
  239. }
  240. void vlan_vid_del(struct net_device *dev, __be16 proto, u16 vid)
  241. {
  242. struct vlan_info *vlan_info;
  243. struct vlan_vid_info *vid_info;
  244. ASSERT_RTNL();
  245. vlan_info = rtnl_dereference(dev->vlan_info);
  246. if (!vlan_info)
  247. return;
  248. vid_info = vlan_vid_info_get(vlan_info, proto, vid);
  249. if (!vid_info)
  250. return;
  251. vid_info->refcount--;
  252. if (vid_info->refcount == 0) {
  253. __vlan_vid_del(vlan_info, vid_info);
  254. if (vlan_info->nr_vids == 0) {
  255. RCU_INIT_POINTER(dev->vlan_info, NULL);
  256. call_rcu(&vlan_info->rcu, vlan_info_rcu_free);
  257. }
  258. }
  259. }
  260. EXPORT_SYMBOL(vlan_vid_del);
  261. int vlan_vids_add_by_dev(struct net_device *dev,
  262. const struct net_device *by_dev)
  263. {
  264. struct vlan_vid_info *vid_info;
  265. struct vlan_info *vlan_info;
  266. int err;
  267. ASSERT_RTNL();
  268. vlan_info = rtnl_dereference(by_dev->vlan_info);
  269. if (!vlan_info)
  270. return 0;
  271. list_for_each_entry(vid_info, &vlan_info->vid_list, list) {
  272. err = vlan_vid_add(dev, vid_info->proto, vid_info->vid);
  273. if (err)
  274. goto unwind;
  275. }
  276. return 0;
  277. unwind:
  278. list_for_each_entry_continue_reverse(vid_info,
  279. &vlan_info->vid_list,
  280. list) {
  281. vlan_vid_del(dev, vid_info->proto, vid_info->vid);
  282. }
  283. return err;
  284. }
  285. EXPORT_SYMBOL(vlan_vids_add_by_dev);
  286. void vlan_vids_del_by_dev(struct net_device *dev,
  287. const struct net_device *by_dev)
  288. {
  289. struct vlan_vid_info *vid_info;
  290. struct vlan_info *vlan_info;
  291. ASSERT_RTNL();
  292. vlan_info = rtnl_dereference(by_dev->vlan_info);
  293. if (!vlan_info)
  294. return;
  295. list_for_each_entry(vid_info, &vlan_info->vid_list, list)
  296. vlan_vid_del(dev, vid_info->proto, vid_info->vid);
  297. }
  298. EXPORT_SYMBOL(vlan_vids_del_by_dev);
  299. bool vlan_uses_dev(const struct net_device *dev)
  300. {
  301. struct vlan_info *vlan_info;
  302. ASSERT_RTNL();
  303. vlan_info = rtnl_dereference(dev->vlan_info);
  304. if (!vlan_info)
  305. return false;
  306. return vlan_info->grp.nr_vlan_devs ? true : false;
  307. }
  308. EXPORT_SYMBOL(vlan_uses_dev);