br_vlan.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. struct sk_buff *br_handle_vlan(struct net_bridge *br,
  102. const struct net_port_vlans *pv,
  103. struct sk_buff *skb)
  104. {
  105. u16 vid;
  106. if (!br->vlan_enabled)
  107. goto out;
  108. /* Vlan filter table must be configured at this point. The
  109. * only exception is the bridge is set in promisc mode and the
  110. * packet is destined for the bridge device. In this case
  111. * pass the packet as is.
  112. */
  113. if (!pv) {
  114. if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
  115. goto out;
  116. } else {
  117. kfree_skb(skb);
  118. return NULL;
  119. }
  120. }
  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->vlan_tci = 0;
  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. /* If vlan tx offload is disabled on bridge device and frame was
  147. * sent from vlan device on the bridge device, it does not have
  148. * HW accelerated vlan tag.
  149. */
  150. if (unlikely(!vlan_tx_tag_present(skb) &&
  151. (skb->protocol == htons(ETH_P_8021Q) ||
  152. skb->protocol == htons(ETH_P_8021AD)))) {
  153. skb = vlan_untag(skb);
  154. if (unlikely(!skb))
  155. return false;
  156. }
  157. err = br_vlan_get_tag(skb, vid);
  158. if (!*vid) {
  159. u16 pvid = br_get_pvid(v);
  160. /* Frame had a tag with VID 0 or did not have a tag.
  161. * See if pvid is set on this port. That tells us which
  162. * vlan untagged or priority-tagged traffic belongs to.
  163. */
  164. if (pvid == VLAN_N_VID)
  165. return false;
  166. /* PVID is set on this port. Any untagged or priority-tagged
  167. * ingress frame is considered to belong to this vlan.
  168. */
  169. *vid = pvid;
  170. if (likely(err))
  171. /* Untagged Frame. */
  172. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), pvid);
  173. else
  174. /* Priority-tagged Frame.
  175. * At this point, We know that skb->vlan_tci had
  176. * VLAN_TAG_PRESENT bit and its VID field was 0x000.
  177. * We update only VID field and preserve PCP field.
  178. */
  179. skb->vlan_tci |= pvid;
  180. return true;
  181. }
  182. /* Frame had a valid vlan tag. See if vlan is allowed */
  183. if (test_bit(*vid, v->vlan_bitmap))
  184. return true;
  185. return false;
  186. }
  187. /* Called under RCU. */
  188. bool br_allowed_egress(struct net_bridge *br,
  189. const struct net_port_vlans *v,
  190. const struct sk_buff *skb)
  191. {
  192. u16 vid;
  193. if (!br->vlan_enabled)
  194. return true;
  195. if (!v)
  196. return false;
  197. br_vlan_get_tag(skb, &vid);
  198. if (test_bit(vid, v->vlan_bitmap))
  199. return true;
  200. return false;
  201. }
  202. /* Must be protected by RTNL.
  203. * Must be called with vid in range from 1 to 4094 inclusive.
  204. */
  205. int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
  206. {
  207. struct net_port_vlans *pv = NULL;
  208. int err;
  209. ASSERT_RTNL();
  210. pv = rtnl_dereference(br->vlan_info);
  211. if (pv)
  212. return __vlan_add(pv, vid, flags);
  213. /* Create port vlan infomration
  214. */
  215. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  216. if (!pv)
  217. return -ENOMEM;
  218. pv->parent.br = br;
  219. err = __vlan_add(pv, vid, flags);
  220. if (err)
  221. goto out;
  222. rcu_assign_pointer(br->vlan_info, pv);
  223. return 0;
  224. out:
  225. kfree(pv);
  226. return err;
  227. }
  228. /* Must be protected by RTNL.
  229. * Must be called with vid in range from 1 to 4094 inclusive.
  230. */
  231. int br_vlan_delete(struct net_bridge *br, u16 vid)
  232. {
  233. struct net_port_vlans *pv;
  234. ASSERT_RTNL();
  235. pv = rtnl_dereference(br->vlan_info);
  236. if (!pv)
  237. return -EINVAL;
  238. br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
  239. __vlan_del(pv, vid);
  240. return 0;
  241. }
  242. void br_vlan_flush(struct net_bridge *br)
  243. {
  244. struct net_port_vlans *pv;
  245. ASSERT_RTNL();
  246. pv = rtnl_dereference(br->vlan_info);
  247. if (!pv)
  248. return;
  249. __vlan_flush(pv);
  250. }
  251. bool br_vlan_find(struct net_bridge *br, u16 vid)
  252. {
  253. struct net_port_vlans *pv;
  254. bool found = false;
  255. rcu_read_lock();
  256. pv = rcu_dereference(br->vlan_info);
  257. if (!pv)
  258. goto out;
  259. if (test_bit(vid, pv->vlan_bitmap))
  260. found = true;
  261. out:
  262. rcu_read_unlock();
  263. return found;
  264. }
  265. int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
  266. {
  267. if (!rtnl_trylock())
  268. return restart_syscall();
  269. if (br->vlan_enabled == val)
  270. goto unlock;
  271. br->vlan_enabled = val;
  272. unlock:
  273. rtnl_unlock();
  274. return 0;
  275. }
  276. /* Must be protected by RTNL.
  277. * Must be called with vid in range from 1 to 4094 inclusive.
  278. */
  279. int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
  280. {
  281. struct net_port_vlans *pv = NULL;
  282. int err;
  283. ASSERT_RTNL();
  284. pv = rtnl_dereference(port->vlan_info);
  285. if (pv)
  286. return __vlan_add(pv, vid, flags);
  287. /* Create port vlan infomration
  288. */
  289. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  290. if (!pv) {
  291. err = -ENOMEM;
  292. goto clean_up;
  293. }
  294. pv->port_idx = port->port_no;
  295. pv->parent.port = port;
  296. err = __vlan_add(pv, vid, flags);
  297. if (err)
  298. goto clean_up;
  299. rcu_assign_pointer(port->vlan_info, pv);
  300. return 0;
  301. clean_up:
  302. kfree(pv);
  303. return err;
  304. }
  305. /* Must be protected by RTNL.
  306. * Must be called with vid in range from 1 to 4094 inclusive.
  307. */
  308. int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
  309. {
  310. struct net_port_vlans *pv;
  311. ASSERT_RTNL();
  312. pv = rtnl_dereference(port->vlan_info);
  313. if (!pv)
  314. return -EINVAL;
  315. br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
  316. return __vlan_del(pv, vid);
  317. }
  318. void nbp_vlan_flush(struct net_bridge_port *port)
  319. {
  320. struct net_port_vlans *pv;
  321. u16 vid;
  322. ASSERT_RTNL();
  323. pv = rtnl_dereference(port->vlan_info);
  324. if (!pv)
  325. return;
  326. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
  327. vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
  328. __vlan_flush(pv);
  329. }
  330. bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
  331. {
  332. struct net_port_vlans *pv;
  333. bool found = false;
  334. rcu_read_lock();
  335. pv = rcu_dereference(port->vlan_info);
  336. if (!pv)
  337. goto out;
  338. if (test_bit(vid, pv->vlan_bitmap))
  339. found = true;
  340. out:
  341. rcu_read_unlock();
  342. return found;
  343. }