br_vlan.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 void __vlan_add_pvid(struct net_port_vlans *v, u16 vid)
  7. {
  8. if (v->pvid == vid)
  9. return;
  10. smp_wmb();
  11. v->pvid = vid;
  12. }
  13. static void __vlan_delete_pvid(struct net_port_vlans *v, u16 vid)
  14. {
  15. if (v->pvid != vid)
  16. return;
  17. smp_wmb();
  18. v->pvid = 0;
  19. }
  20. static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
  21. {
  22. struct net_bridge_port *p = NULL;
  23. struct net_bridge *br;
  24. struct net_device *dev;
  25. int err;
  26. if (test_bit(vid, v->vlan_bitmap)) {
  27. if (flags & BRIDGE_VLAN_INFO_PVID)
  28. __vlan_add_pvid(v, vid);
  29. return 0;
  30. }
  31. if (vid) {
  32. if (v->port_idx) {
  33. p = v->parent.port;
  34. br = p->br;
  35. dev = p->dev;
  36. } else {
  37. br = v->parent.br;
  38. dev = br->dev;
  39. }
  40. if (p && (dev->features & NETIF_F_HW_VLAN_FILTER)) {
  41. /* Add VLAN to the device filter if it is supported.
  42. * Stricly speaking, this is not necessary now, since
  43. * devices are made promiscuous by the bridge, but if
  44. * that ever changes this code will allow tagged
  45. * traffic to enter the bridge.
  46. */
  47. err = dev->netdev_ops->ndo_vlan_rx_add_vid(dev, vid);
  48. if (err)
  49. return err;
  50. }
  51. err = br_fdb_insert(br, p, dev->dev_addr, vid);
  52. if (err) {
  53. br_err(br, "failed insert local address into bridge "
  54. "forwarding table\n");
  55. goto out_filt;
  56. }
  57. }
  58. set_bit(vid, v->vlan_bitmap);
  59. v->num_vlans++;
  60. if (flags & BRIDGE_VLAN_INFO_PVID)
  61. __vlan_add_pvid(v, vid);
  62. return 0;
  63. out_filt:
  64. if (p && (dev->features & NETIF_F_HW_VLAN_FILTER))
  65. dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, vid);
  66. return err;
  67. }
  68. static int __vlan_del(struct net_port_vlans *v, u16 vid)
  69. {
  70. if (!test_bit(vid, v->vlan_bitmap))
  71. return -EINVAL;
  72. __vlan_delete_pvid(v, vid);
  73. if (v->port_idx && vid) {
  74. struct net_device *dev = v->parent.port->dev;
  75. if (dev->features & NETIF_F_HW_VLAN_FILTER)
  76. dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, vid);
  77. }
  78. clear_bit(vid, v->vlan_bitmap);
  79. v->num_vlans--;
  80. if (bitmap_empty(v->vlan_bitmap, BR_VLAN_BITMAP_LEN)) {
  81. if (v->port_idx)
  82. rcu_assign_pointer(v->parent.port->vlan_info, NULL);
  83. else
  84. rcu_assign_pointer(v->parent.br->vlan_info, NULL);
  85. kfree_rcu(v, rcu);
  86. }
  87. return 0;
  88. }
  89. static void __vlan_flush(struct net_port_vlans *v)
  90. {
  91. smp_wmb();
  92. v->pvid = 0;
  93. bitmap_zero(v->vlan_bitmap, BR_VLAN_BITMAP_LEN);
  94. if (v->port_idx)
  95. rcu_assign_pointer(v->parent.port->vlan_info, NULL);
  96. else
  97. rcu_assign_pointer(v->parent.br->vlan_info, NULL);
  98. kfree_rcu(v, rcu);
  99. }
  100. /* Strip the tag from the packet. Will return skb with tci set 0. */
  101. static struct sk_buff *br_vlan_untag(struct sk_buff *skb)
  102. {
  103. if (skb->protocol != htons(ETH_P_8021Q)) {
  104. skb->vlan_tci = 0;
  105. return skb;
  106. }
  107. skb->vlan_tci = 0;
  108. skb = vlan_untag(skb);
  109. if (skb)
  110. skb->vlan_tci = 0;
  111. return skb;
  112. }
  113. struct sk_buff *br_handle_vlan(struct net_bridge *br,
  114. const struct net_port_vlans *pv,
  115. struct sk_buff *skb)
  116. {
  117. u16 vid;
  118. if (!br->vlan_enabled)
  119. goto out;
  120. /* At this point, we know that the frame was filtered and contains
  121. * a valid vlan id. If the vlan id matches the pvid of current port
  122. * send untagged; otherwise, send taged.
  123. */
  124. br_vlan_get_tag(skb, &vid);
  125. if (vid == br_get_pvid(pv))
  126. skb = br_vlan_untag(skb);
  127. else {
  128. /* Egress policy says "send tagged". If output device
  129. * is the bridge, we need to add the VLAN header
  130. * ourselves since we'll be going through the RX path.
  131. * Sending to ports puts the frame on the TX path and
  132. * we let dev_hard_start_xmit() add the header.
  133. */
  134. if (skb->protocol != htons(ETH_P_8021Q) &&
  135. pv->port_idx == 0) {
  136. /* vlan_put_tag expects skb->data to point to
  137. * mac header.
  138. */
  139. skb_push(skb, ETH_HLEN);
  140. skb = __vlan_put_tag(skb, skb->vlan_tci);
  141. if (!skb)
  142. goto out;
  143. /* put skb->data back to where it was */
  144. skb_pull(skb, ETH_HLEN);
  145. skb->vlan_tci = 0;
  146. }
  147. }
  148. out:
  149. return skb;
  150. }
  151. /* Called under RCU */
  152. bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
  153. struct sk_buff *skb, u16 *vid)
  154. {
  155. /* If VLAN filtering is disabled on the bridge, all packets are
  156. * permitted.
  157. */
  158. if (!br->vlan_enabled)
  159. return true;
  160. /* If there are no vlan in the permitted list, all packets are
  161. * rejected.
  162. */
  163. if (!v)
  164. return false;
  165. if (br_vlan_get_tag(skb, vid)) {
  166. u16 pvid = br_get_pvid(v);
  167. /* Frame did not have a tag. See if pvid is set
  168. * on this port. That tells us which vlan untagged
  169. * traffic belongs to.
  170. */
  171. if (pvid == VLAN_N_VID)
  172. return false;
  173. /* PVID is set on this port. Any untagged ingress
  174. * frame is considered to belong to this vlan.
  175. */
  176. __vlan_hwaccel_put_tag(skb, pvid);
  177. return true;
  178. }
  179. /* Frame had a valid vlan tag. See if vlan is allowed */
  180. if (test_bit(*vid, v->vlan_bitmap))
  181. return true;
  182. return false;
  183. }
  184. /* Called under RCU. */
  185. bool br_allowed_egress(struct net_bridge *br,
  186. const struct net_port_vlans *v,
  187. const struct sk_buff *skb)
  188. {
  189. u16 vid;
  190. if (!br->vlan_enabled)
  191. return true;
  192. if (!v)
  193. return false;
  194. br_vlan_get_tag(skb, &vid);
  195. if (test_bit(vid, v->vlan_bitmap))
  196. return true;
  197. return false;
  198. }
  199. /* Must be protected by RTNL */
  200. int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
  201. {
  202. struct net_port_vlans *pv = NULL;
  203. int err;
  204. ASSERT_RTNL();
  205. pv = rtnl_dereference(br->vlan_info);
  206. if (pv)
  207. return __vlan_add(pv, vid, flags);
  208. /* Create port vlan infomration
  209. */
  210. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  211. if (!pv)
  212. return -ENOMEM;
  213. pv->parent.br = br;
  214. err = __vlan_add(pv, vid, flags);
  215. if (err)
  216. goto out;
  217. rcu_assign_pointer(br->vlan_info, pv);
  218. return 0;
  219. out:
  220. kfree(pv);
  221. return err;
  222. }
  223. /* Must be protected by RTNL */
  224. int br_vlan_delete(struct net_bridge *br, u16 vid)
  225. {
  226. struct net_port_vlans *pv;
  227. ASSERT_RTNL();
  228. pv = rtnl_dereference(br->vlan_info);
  229. if (!pv)
  230. return -EINVAL;
  231. if (vid) {
  232. /* If the VID !=0 remove fdb for this vid. VID 0 is special
  233. * in that it's the default and is always there in the fdb.
  234. */
  235. spin_lock_bh(&br->hash_lock);
  236. fdb_delete_by_addr(br, br->dev->dev_addr, vid);
  237. spin_unlock_bh(&br->hash_lock);
  238. }
  239. __vlan_del(pv, vid);
  240. return 0;
  241. }
  242. void br_vlan_flush(struct net_bridge *br)
  243. {
  244. struct net_port_vlans *pv;
  245. ASSERT_RTNL();
  246. pv = rtnl_dereference(br->vlan_info);
  247. if (!pv)
  248. return;
  249. __vlan_flush(pv);
  250. }
  251. int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
  252. {
  253. if (!rtnl_trylock())
  254. return restart_syscall();
  255. if (br->vlan_enabled == val)
  256. goto unlock;
  257. br->vlan_enabled = val;
  258. unlock:
  259. rtnl_unlock();
  260. return 0;
  261. }
  262. /* Must be protected by RTNL */
  263. int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
  264. {
  265. struct net_port_vlans *pv = NULL;
  266. int err;
  267. ASSERT_RTNL();
  268. pv = rtnl_dereference(port->vlan_info);
  269. if (pv)
  270. return __vlan_add(pv, vid, flags);
  271. /* Create port vlan infomration
  272. */
  273. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  274. if (!pv) {
  275. err = -ENOMEM;
  276. goto clean_up;
  277. }
  278. pv->port_idx = port->port_no;
  279. pv->parent.port = port;
  280. err = __vlan_add(pv, vid, flags);
  281. if (err)
  282. goto clean_up;
  283. rcu_assign_pointer(port->vlan_info, pv);
  284. return 0;
  285. clean_up:
  286. kfree(pv);
  287. return err;
  288. }
  289. /* Must be protected by RTNL */
  290. int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
  291. {
  292. struct net_port_vlans *pv;
  293. ASSERT_RTNL();
  294. pv = rtnl_dereference(port->vlan_info);
  295. if (!pv)
  296. return -EINVAL;
  297. if (vid) {
  298. /* If the VID !=0 remove fdb for this vid. VID 0 is special
  299. * in that it's the default and is always there in the fdb.
  300. */
  301. spin_lock_bh(&port->br->hash_lock);
  302. fdb_delete_by_addr(port->br, port->dev->dev_addr, vid);
  303. spin_unlock_bh(&port->br->hash_lock);
  304. }
  305. return __vlan_del(pv, vid);
  306. }
  307. void nbp_vlan_flush(struct net_bridge_port *port)
  308. {
  309. struct net_port_vlans *pv;
  310. ASSERT_RTNL();
  311. pv = rtnl_dereference(port->vlan_info);
  312. if (!pv)
  313. return;
  314. __vlan_flush(pv);
  315. }
  316. bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
  317. {
  318. struct net_port_vlans *pv;
  319. bool found = false;
  320. rcu_read_lock();
  321. pv = rcu_dereference(port->vlan_info);
  322. if (!pv)
  323. goto out;
  324. if (test_bit(vid, pv->vlan_bitmap))
  325. found = true;
  326. out:
  327. rcu_read_unlock();
  328. return found;
  329. }