br_vlan.c 8.9 KB

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