br_vlan.c 7.8 KB

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