br_vlan.c 8.2 KB

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