br_vlan.c 8.4 KB

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