br_vlan.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #include <linux/kernel.h>
  2. #include <linux/netdevice.h>
  3. #include <linux/rtnetlink.h>
  4. #include <linux/slab.h>
  5. #include "br_private.h"
  6. static int __vlan_add(struct net_port_vlans *v, u16 vid)
  7. {
  8. int err;
  9. if (test_bit(vid, v->vlan_bitmap))
  10. return -EEXIST;
  11. if (v->port_idx && vid) {
  12. struct net_device *dev = v->parent.port->dev;
  13. /* Add VLAN to the device filter if it is supported.
  14. * Stricly speaking, this is not necessary now, since devices
  15. * are made promiscuous by the bridge, but if that ever changes
  16. * this code will allow tagged traffic to enter the bridge.
  17. */
  18. if (dev->features & NETIF_F_HW_VLAN_FILTER) {
  19. err = dev->netdev_ops->ndo_vlan_rx_add_vid(dev, vid);
  20. if (err)
  21. return err;
  22. }
  23. }
  24. set_bit(vid, v->vlan_bitmap);
  25. v->num_vlans++;
  26. return 0;
  27. }
  28. static int __vlan_del(struct net_port_vlans *v, u16 vid)
  29. {
  30. if (!test_bit(vid, v->vlan_bitmap))
  31. return -EINVAL;
  32. if (v->port_idx && vid) {
  33. struct net_device *dev = v->parent.port->dev;
  34. if (dev->features & NETIF_F_HW_VLAN_FILTER)
  35. dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, vid);
  36. }
  37. clear_bit(vid, v->vlan_bitmap);
  38. v->num_vlans--;
  39. if (bitmap_empty(v->vlan_bitmap, BR_VLAN_BITMAP_LEN)) {
  40. if (v->port_idx)
  41. rcu_assign_pointer(v->parent.port->vlan_info, NULL);
  42. else
  43. rcu_assign_pointer(v->parent.br->vlan_info, NULL);
  44. kfree_rcu(v, rcu);
  45. }
  46. return 0;
  47. }
  48. static void __vlan_flush(struct net_port_vlans *v)
  49. {
  50. bitmap_zero(v->vlan_bitmap, BR_VLAN_BITMAP_LEN);
  51. if (v->port_idx)
  52. rcu_assign_pointer(v->parent.port->vlan_info, NULL);
  53. else
  54. rcu_assign_pointer(v->parent.br->vlan_info, NULL);
  55. kfree_rcu(v, rcu);
  56. }
  57. /* Strip the tag from the packet. Will return skb with tci set 0. */
  58. static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
  59. {
  60. if (skb->protocol != htons(ETH_P_8021Q)) {
  61. skb->vlan_tci = 0;
  62. return skb;
  63. }
  64. skb->vlan_tci = 0;
  65. skb = vlan_untag(skb);
  66. if (skb)
  67. skb->vlan_tci = 0;
  68. return skb;
  69. }
  70. struct sk_buff *br_handle_vlan(struct net_bridge *br,
  71. const struct net_port_vlans *pv,
  72. struct sk_buff *skb)
  73. {
  74. u16 vid;
  75. if (!br->vlan_enabled)
  76. goto out;
  77. /* At this point, we know that the frame was filtered and contains
  78. * a valid vlan id. If the vlan id matches the pvid of current port
  79. * send untagged; otherwise, send taged.
  80. */
  81. br_vlan_get_tag(skb, &vid);
  82. if (vid == br_get_pvid(pv))
  83. skb = br_vlan_untag(skb);
  84. else {
  85. /* Egress policy says "send tagged". If output device
  86. * is the bridge, we need to add the VLAN header
  87. * ourselves since we'll be going through the RX path.
  88. * Sending to ports puts the frame on the TX path and
  89. * we let dev_hard_start_xmit() add the header.
  90. */
  91. if (skb->protocol != htons(ETH_P_8021Q) &&
  92. pv->port_idx == 0) {
  93. /* vlan_put_tag expects skb->data to point to
  94. * mac header.
  95. */
  96. skb_push(skb, ETH_HLEN);
  97. skb = __vlan_put_tag(skb, skb->vlan_tci);
  98. if (!skb)
  99. goto out;
  100. /* put skb->data back to where it was */
  101. skb_pull(skb, ETH_HLEN);
  102. skb->vlan_tci = 0;
  103. }
  104. }
  105. out:
  106. return skb;
  107. }
  108. /* Called under RCU */
  109. bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
  110. struct sk_buff *skb, u16 *vid)
  111. {
  112. /* If VLAN filtering is disabled on the bridge, all packets are
  113. * permitted.
  114. */
  115. if (!br->vlan_enabled)
  116. return true;
  117. /* If there are no vlan in the permitted list, all packets are
  118. * rejected.
  119. */
  120. if (!v)
  121. return false;
  122. if (br_vlan_get_tag(skb, vid)) {
  123. u16 pvid = br_get_pvid(v);
  124. /* Frame did not have a tag. See if pvid is set
  125. * on this port. That tells us which vlan untagged
  126. * traffic belongs to.
  127. */
  128. if (pvid == VLAN_N_VID)
  129. return false;
  130. /* PVID is set on this port. Any untagged ingress
  131. * frame is considered to belong to this vlan.
  132. */
  133. __vlan_hwaccel_put_tag(skb, pvid);
  134. return true;
  135. }
  136. /* Frame had a valid vlan tag. See if vlan is allowed */
  137. if (test_bit(*vid, v->vlan_bitmap))
  138. return true;
  139. return false;
  140. }
  141. /* Called under RCU. */
  142. bool br_allowed_egress(struct net_bridge *br,
  143. const struct net_port_vlans *v,
  144. const struct sk_buff *skb)
  145. {
  146. u16 vid;
  147. if (!br->vlan_enabled)
  148. return true;
  149. if (!v)
  150. return false;
  151. br_vlan_get_tag(skb, &vid);
  152. if (test_bit(vid, v->vlan_bitmap))
  153. return true;
  154. return false;
  155. }
  156. /* Must be protected by RTNL */
  157. int br_vlan_add(struct net_bridge *br, u16 vid)
  158. {
  159. struct net_port_vlans *pv = NULL;
  160. int err;
  161. ASSERT_RTNL();
  162. pv = rtnl_dereference(br->vlan_info);
  163. if (pv)
  164. return __vlan_add(pv, vid);
  165. /* Create port vlan infomration
  166. */
  167. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  168. if (!pv)
  169. return -ENOMEM;
  170. pv->parent.br = br;
  171. err = __vlan_add(pv, vid);
  172. if (err)
  173. goto out;
  174. rcu_assign_pointer(br->vlan_info, pv);
  175. return 0;
  176. out:
  177. kfree(pv);
  178. return err;
  179. }
  180. /* Must be protected by RTNL */
  181. int br_vlan_delete(struct net_bridge *br, u16 vid)
  182. {
  183. struct net_port_vlans *pv;
  184. ASSERT_RTNL();
  185. pv = rtnl_dereference(br->vlan_info);
  186. if (!pv)
  187. return -EINVAL;
  188. __vlan_del(pv, vid);
  189. return 0;
  190. }
  191. void br_vlan_flush(struct net_bridge *br)
  192. {
  193. struct net_port_vlans *pv;
  194. ASSERT_RTNL();
  195. pv = rtnl_dereference(br->vlan_info);
  196. if (!pv)
  197. return;
  198. __vlan_flush(pv);
  199. }
  200. int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
  201. {
  202. if (!rtnl_trylock())
  203. return restart_syscall();
  204. if (br->vlan_enabled == val)
  205. goto unlock;
  206. br->vlan_enabled = val;
  207. unlock:
  208. rtnl_unlock();
  209. return 0;
  210. }
  211. /* Must be protected by RTNL */
  212. int nbp_vlan_add(struct net_bridge_port *port, u16 vid)
  213. {
  214. struct net_port_vlans *pv = NULL;
  215. int err;
  216. ASSERT_RTNL();
  217. pv = rtnl_dereference(port->vlan_info);
  218. if (pv)
  219. return __vlan_add(pv, vid);
  220. /* Create port vlan infomration
  221. */
  222. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  223. if (!pv) {
  224. err = -ENOMEM;
  225. goto clean_up;
  226. }
  227. pv->port_idx = port->port_no;
  228. pv->parent.port = port;
  229. err = __vlan_add(pv, vid);
  230. if (err)
  231. goto clean_up;
  232. rcu_assign_pointer(port->vlan_info, pv);
  233. return 0;
  234. clean_up:
  235. kfree(pv);
  236. return err;
  237. }
  238. /* Must be protected by RTNL */
  239. int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
  240. {
  241. struct net_port_vlans *pv;
  242. ASSERT_RTNL();
  243. pv = rtnl_dereference(port->vlan_info);
  244. if (!pv)
  245. return -EINVAL;
  246. return __vlan_del(pv, vid);
  247. }
  248. void nbp_vlan_flush(struct net_bridge_port *port)
  249. {
  250. struct net_port_vlans *pv;
  251. ASSERT_RTNL();
  252. pv = rtnl_dereference(port->vlan_info);
  253. if (!pv)
  254. return;
  255. __vlan_flush(pv);
  256. }