vlan_core.c 8.1 KB

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