br_vlan.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
  228. __vlan_del(pv, vid);
  229. return 0;
  230. }
  231. void br_vlan_flush(struct net_bridge *br)
  232. {
  233. struct net_port_vlans *pv;
  234. ASSERT_RTNL();
  235. pv = rtnl_dereference(br->vlan_info);
  236. if (!pv)
  237. return;
  238. __vlan_flush(pv);
  239. }
  240. bool br_vlan_find(struct net_bridge *br, u16 vid)
  241. {
  242. struct net_port_vlans *pv;
  243. bool found = false;
  244. rcu_read_lock();
  245. pv = rcu_dereference(br->vlan_info);
  246. if (!pv)
  247. goto out;
  248. if (test_bit(vid, pv->vlan_bitmap))
  249. found = true;
  250. out:
  251. rcu_read_unlock();
  252. return found;
  253. }
  254. int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
  255. {
  256. if (!rtnl_trylock())
  257. return restart_syscall();
  258. if (br->vlan_enabled == val)
  259. goto unlock;
  260. br->vlan_enabled = val;
  261. unlock:
  262. rtnl_unlock();
  263. return 0;
  264. }
  265. /* Must be protected by RTNL.
  266. * Must be called with vid in range from 1 to 4094 inclusive.
  267. */
  268. int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
  269. {
  270. struct net_port_vlans *pv = NULL;
  271. int err;
  272. ASSERT_RTNL();
  273. pv = rtnl_dereference(port->vlan_info);
  274. if (pv)
  275. return __vlan_add(pv, vid, flags);
  276. /* Create port vlan infomration
  277. */
  278. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  279. if (!pv) {
  280. err = -ENOMEM;
  281. goto clean_up;
  282. }
  283. pv->port_idx = port->port_no;
  284. pv->parent.port = port;
  285. err = __vlan_add(pv, vid, flags);
  286. if (err)
  287. goto clean_up;
  288. rcu_assign_pointer(port->vlan_info, pv);
  289. return 0;
  290. clean_up:
  291. kfree(pv);
  292. return err;
  293. }
  294. /* Must be protected by RTNL.
  295. * Must be called with vid in range from 1 to 4094 inclusive.
  296. */
  297. int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
  298. {
  299. struct net_port_vlans *pv;
  300. ASSERT_RTNL();
  301. pv = rtnl_dereference(port->vlan_info);
  302. if (!pv)
  303. return -EINVAL;
  304. br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
  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. u16 vid;
  311. ASSERT_RTNL();
  312. pv = rtnl_dereference(port->vlan_info);
  313. if (!pv)
  314. return;
  315. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
  316. vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
  317. __vlan_flush(pv);
  318. }
  319. bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
  320. {
  321. struct net_port_vlans *pv;
  322. bool found = false;
  323. rcu_read_lock();
  324. pv = rcu_dereference(port->vlan_info);
  325. if (!pv)
  326. goto out;
  327. if (test_bit(vid, pv->vlan_bitmap))
  328. found = true;
  329. out:
  330. rcu_read_unlock();
  331. return found;
  332. }