br_vlan.c 8.3 KB

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