br_vlan.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_INIT_POINTER(v->parent.port->vlan_info, NULL);
  84. else
  85. RCU_INIT_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_INIT_POINTER(v->parent.port->vlan_info, NULL);
  97. else
  98. RCU_INIT_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. goto drop;
  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. goto drop;
  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. drop:
  186. kfree_skb(skb);
  187. return false;
  188. }
  189. /* Called under RCU. */
  190. bool br_allowed_egress(struct net_bridge *br,
  191. const struct net_port_vlans *v,
  192. const struct sk_buff *skb)
  193. {
  194. u16 vid;
  195. if (!br->vlan_enabled)
  196. return true;
  197. if (!v)
  198. return false;
  199. br_vlan_get_tag(skb, &vid);
  200. if (test_bit(vid, v->vlan_bitmap))
  201. return true;
  202. return false;
  203. }
  204. /* Must be protected by RTNL.
  205. * Must be called with vid in range from 1 to 4094 inclusive.
  206. */
  207. int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
  208. {
  209. struct net_port_vlans *pv = NULL;
  210. int err;
  211. ASSERT_RTNL();
  212. pv = rtnl_dereference(br->vlan_info);
  213. if (pv)
  214. return __vlan_add(pv, vid, flags);
  215. /* Create port vlan infomration
  216. */
  217. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  218. if (!pv)
  219. return -ENOMEM;
  220. pv->parent.br = br;
  221. err = __vlan_add(pv, vid, flags);
  222. if (err)
  223. goto out;
  224. rcu_assign_pointer(br->vlan_info, pv);
  225. return 0;
  226. out:
  227. kfree(pv);
  228. return err;
  229. }
  230. /* Must be protected by RTNL.
  231. * Must be called with vid in range from 1 to 4094 inclusive.
  232. */
  233. int br_vlan_delete(struct net_bridge *br, u16 vid)
  234. {
  235. struct net_port_vlans *pv;
  236. ASSERT_RTNL();
  237. pv = rtnl_dereference(br->vlan_info);
  238. if (!pv)
  239. return -EINVAL;
  240. br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
  241. __vlan_del(pv, vid);
  242. return 0;
  243. }
  244. void br_vlan_flush(struct net_bridge *br)
  245. {
  246. struct net_port_vlans *pv;
  247. ASSERT_RTNL();
  248. pv = rtnl_dereference(br->vlan_info);
  249. if (!pv)
  250. return;
  251. __vlan_flush(pv);
  252. }
  253. bool br_vlan_find(struct net_bridge *br, u16 vid)
  254. {
  255. struct net_port_vlans *pv;
  256. bool found = false;
  257. rcu_read_lock();
  258. pv = rcu_dereference(br->vlan_info);
  259. if (!pv)
  260. goto out;
  261. if (test_bit(vid, pv->vlan_bitmap))
  262. found = true;
  263. out:
  264. rcu_read_unlock();
  265. return found;
  266. }
  267. int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
  268. {
  269. if (!rtnl_trylock())
  270. return restart_syscall();
  271. if (br->vlan_enabled == val)
  272. goto unlock;
  273. br->vlan_enabled = val;
  274. unlock:
  275. rtnl_unlock();
  276. return 0;
  277. }
  278. /* Must be protected by RTNL.
  279. * Must be called with vid in range from 1 to 4094 inclusive.
  280. */
  281. int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
  282. {
  283. struct net_port_vlans *pv = NULL;
  284. int err;
  285. ASSERT_RTNL();
  286. pv = rtnl_dereference(port->vlan_info);
  287. if (pv)
  288. return __vlan_add(pv, vid, flags);
  289. /* Create port vlan infomration
  290. */
  291. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  292. if (!pv) {
  293. err = -ENOMEM;
  294. goto clean_up;
  295. }
  296. pv->port_idx = port->port_no;
  297. pv->parent.port = port;
  298. err = __vlan_add(pv, vid, flags);
  299. if (err)
  300. goto clean_up;
  301. rcu_assign_pointer(port->vlan_info, pv);
  302. return 0;
  303. clean_up:
  304. kfree(pv);
  305. return err;
  306. }
  307. /* Must be protected by RTNL.
  308. * Must be called with vid in range from 1 to 4094 inclusive.
  309. */
  310. int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
  311. {
  312. struct net_port_vlans *pv;
  313. ASSERT_RTNL();
  314. pv = rtnl_dereference(port->vlan_info);
  315. if (!pv)
  316. return -EINVAL;
  317. br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
  318. return __vlan_del(pv, vid);
  319. }
  320. void nbp_vlan_flush(struct net_bridge_port *port)
  321. {
  322. struct net_port_vlans *pv;
  323. u16 vid;
  324. ASSERT_RTNL();
  325. pv = rtnl_dereference(port->vlan_info);
  326. if (!pv)
  327. return;
  328. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
  329. vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
  330. __vlan_flush(pv);
  331. }
  332. bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
  333. {
  334. struct net_port_vlans *pv;
  335. bool found = false;
  336. rcu_read_lock();
  337. pv = rcu_dereference(port->vlan_info);
  338. if (!pv)
  339. goto out;
  340. if (test_bit(vid, pv->vlan_bitmap))
  341. found = true;
  342. out:
  343. rcu_read_unlock();
  344. return found;
  345. }