br_vlan.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. #include <linux/kernel.h>
  2. #include <linux/netdevice.h>
  3. #include <linux/rtnetlink.h>
  4. #include <linux/slab.h>
  5. #include <net/switchdev.h>
  6. #include "br_private.h"
  7. static void __vlan_add_pvid(struct net_port_vlans *v, u16 vid)
  8. {
  9. if (v->pvid == vid)
  10. return;
  11. smp_wmb();
  12. v->pvid = vid;
  13. }
  14. static void __vlan_delete_pvid(struct net_port_vlans *v, u16 vid)
  15. {
  16. if (v->pvid != vid)
  17. return;
  18. smp_wmb();
  19. v->pvid = 0;
  20. }
  21. static void __vlan_add_flags(struct net_port_vlans *v, u16 vid, u16 flags)
  22. {
  23. if (flags & BRIDGE_VLAN_INFO_PVID)
  24. __vlan_add_pvid(v, vid);
  25. else
  26. __vlan_delete_pvid(v, vid);
  27. if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
  28. set_bit(vid, v->untagged_bitmap);
  29. else
  30. clear_bit(vid, v->untagged_bitmap);
  31. }
  32. static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
  33. u16 vid, u16 flags)
  34. {
  35. const struct net_device_ops *ops = dev->netdev_ops;
  36. int err;
  37. /* If driver uses VLAN ndo ops, use 8021q to install vid
  38. * on device, otherwise try switchdev ops to install vid.
  39. */
  40. if (ops->ndo_vlan_rx_add_vid) {
  41. err = vlan_vid_add(dev, br->vlan_proto, vid);
  42. } else {
  43. struct switchdev_obj vlan_obj = {
  44. .id = SWITCHDEV_OBJ_PORT_VLAN,
  45. .u.vlan = {
  46. .flags = flags,
  47. .vid_begin = vid,
  48. .vid_end = vid,
  49. },
  50. };
  51. err = switchdev_port_obj_add(dev, &vlan_obj);
  52. if (err == -EOPNOTSUPP)
  53. err = 0;
  54. }
  55. return err;
  56. }
  57. static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
  58. {
  59. struct net_bridge_port *p = NULL;
  60. struct net_bridge *br;
  61. struct net_device *dev;
  62. int err;
  63. if (test_bit(vid, v->vlan_bitmap)) {
  64. __vlan_add_flags(v, vid, flags);
  65. return 0;
  66. }
  67. if (v->port_idx) {
  68. p = v->parent.port;
  69. br = p->br;
  70. dev = p->dev;
  71. } else {
  72. br = v->parent.br;
  73. dev = br->dev;
  74. }
  75. if (p) {
  76. /* Add VLAN to the device filter if it is supported.
  77. * This ensures tagged traffic enters the bridge when
  78. * promiscuous mode is disabled by br_manage_promisc().
  79. */
  80. err = __vlan_vid_add(dev, br, vid, flags);
  81. if (err)
  82. return err;
  83. }
  84. err = br_fdb_insert(br, p, dev->dev_addr, vid);
  85. if (err) {
  86. br_err(br, "failed insert local address into bridge "
  87. "forwarding table\n");
  88. goto out_filt;
  89. }
  90. set_bit(vid, v->vlan_bitmap);
  91. v->num_vlans++;
  92. __vlan_add_flags(v, vid, flags);
  93. return 0;
  94. out_filt:
  95. if (p)
  96. vlan_vid_del(dev, br->vlan_proto, vid);
  97. return err;
  98. }
  99. static int __vlan_vid_del(struct net_device *dev, struct net_bridge *br,
  100. u16 vid)
  101. {
  102. const struct net_device_ops *ops = dev->netdev_ops;
  103. int err = 0;
  104. /* If driver uses VLAN ndo ops, use 8021q to delete vid
  105. * on device, otherwise try switchdev ops to delete vid.
  106. */
  107. if (ops->ndo_vlan_rx_kill_vid) {
  108. vlan_vid_del(dev, br->vlan_proto, vid);
  109. } else {
  110. struct switchdev_obj vlan_obj = {
  111. .id = SWITCHDEV_OBJ_PORT_VLAN,
  112. .u.vlan = {
  113. .vid_begin = vid,
  114. .vid_end = vid,
  115. },
  116. };
  117. err = switchdev_port_obj_del(dev, &vlan_obj);
  118. if (err == -EOPNOTSUPP)
  119. err = 0;
  120. }
  121. return err;
  122. }
  123. static int __vlan_del(struct net_port_vlans *v, u16 vid)
  124. {
  125. if (!test_bit(vid, v->vlan_bitmap))
  126. return -EINVAL;
  127. __vlan_delete_pvid(v, vid);
  128. clear_bit(vid, v->untagged_bitmap);
  129. if (v->port_idx) {
  130. struct net_bridge_port *p = v->parent.port;
  131. int err;
  132. err = __vlan_vid_del(p->dev, p->br, vid);
  133. if (err)
  134. return err;
  135. }
  136. clear_bit(vid, v->vlan_bitmap);
  137. v->num_vlans--;
  138. if (bitmap_empty(v->vlan_bitmap, VLAN_N_VID)) {
  139. if (v->port_idx)
  140. RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
  141. else
  142. RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
  143. kfree_rcu(v, rcu);
  144. }
  145. return 0;
  146. }
  147. static void __vlan_flush(struct net_port_vlans *v)
  148. {
  149. smp_wmb();
  150. v->pvid = 0;
  151. bitmap_zero(v->vlan_bitmap, VLAN_N_VID);
  152. if (v->port_idx)
  153. RCU_INIT_POINTER(v->parent.port->vlan_info, NULL);
  154. else
  155. RCU_INIT_POINTER(v->parent.br->vlan_info, NULL);
  156. kfree_rcu(v, rcu);
  157. }
  158. struct sk_buff *br_handle_vlan(struct net_bridge *br,
  159. const struct net_port_vlans *pv,
  160. struct sk_buff *skb)
  161. {
  162. u16 vid;
  163. /* If this packet was not filtered at input, let it pass */
  164. if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
  165. goto out;
  166. /* Vlan filter table must be configured at this point. The
  167. * only exception is the bridge is set in promisc mode and the
  168. * packet is destined for the bridge device. In this case
  169. * pass the packet as is.
  170. */
  171. if (!pv) {
  172. if ((br->dev->flags & IFF_PROMISC) && skb->dev == br->dev) {
  173. goto out;
  174. } else {
  175. kfree_skb(skb);
  176. return NULL;
  177. }
  178. }
  179. /* At this point, we know that the frame was filtered and contains
  180. * a valid vlan id. If the vlan id is set in the untagged bitmap,
  181. * send untagged; otherwise, send tagged.
  182. */
  183. br_vlan_get_tag(skb, &vid);
  184. if (test_bit(vid, pv->untagged_bitmap))
  185. skb->vlan_tci = 0;
  186. out:
  187. return skb;
  188. }
  189. /* Called under RCU */
  190. bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
  191. struct sk_buff *skb, u16 *vid)
  192. {
  193. bool tagged;
  194. __be16 proto;
  195. /* If VLAN filtering is disabled on the bridge, all packets are
  196. * permitted.
  197. */
  198. if (!br->vlan_enabled) {
  199. BR_INPUT_SKB_CB(skb)->vlan_filtered = false;
  200. return true;
  201. }
  202. /* If there are no vlan in the permitted list, all packets are
  203. * rejected.
  204. */
  205. if (!v)
  206. goto drop;
  207. BR_INPUT_SKB_CB(skb)->vlan_filtered = true;
  208. proto = br->vlan_proto;
  209. /* If vlan tx offload is disabled on bridge device and frame was
  210. * sent from vlan device on the bridge device, it does not have
  211. * HW accelerated vlan tag.
  212. */
  213. if (unlikely(!skb_vlan_tag_present(skb) &&
  214. skb->protocol == proto)) {
  215. skb = skb_vlan_untag(skb);
  216. if (unlikely(!skb))
  217. return false;
  218. }
  219. if (!br_vlan_get_tag(skb, vid)) {
  220. /* Tagged frame */
  221. if (skb->vlan_proto != proto) {
  222. /* Protocol-mismatch, empty out vlan_tci for new tag */
  223. skb_push(skb, ETH_HLEN);
  224. skb = vlan_insert_tag_set_proto(skb, skb->vlan_proto,
  225. skb_vlan_tag_get(skb));
  226. if (unlikely(!skb))
  227. return false;
  228. skb_pull(skb, ETH_HLEN);
  229. skb_reset_mac_len(skb);
  230. *vid = 0;
  231. tagged = false;
  232. } else {
  233. tagged = true;
  234. }
  235. } else {
  236. /* Untagged frame */
  237. tagged = false;
  238. }
  239. if (!*vid) {
  240. u16 pvid = br_get_pvid(v);
  241. /* Frame had a tag with VID 0 or did not have a tag.
  242. * See if pvid is set on this port. That tells us which
  243. * vlan untagged or priority-tagged traffic belongs to.
  244. */
  245. if (!pvid)
  246. goto drop;
  247. /* PVID is set on this port. Any untagged or priority-tagged
  248. * ingress frame is considered to belong to this vlan.
  249. */
  250. *vid = pvid;
  251. if (likely(!tagged))
  252. /* Untagged Frame. */
  253. __vlan_hwaccel_put_tag(skb, proto, pvid);
  254. else
  255. /* Priority-tagged Frame.
  256. * At this point, We know that skb->vlan_tci had
  257. * VLAN_TAG_PRESENT bit and its VID field was 0x000.
  258. * We update only VID field and preserve PCP field.
  259. */
  260. skb->vlan_tci |= pvid;
  261. return true;
  262. }
  263. /* Frame had a valid vlan tag. See if vlan is allowed */
  264. if (test_bit(*vid, v->vlan_bitmap))
  265. return true;
  266. drop:
  267. kfree_skb(skb);
  268. return false;
  269. }
  270. /* Called under RCU. */
  271. bool br_allowed_egress(struct net_bridge *br,
  272. const struct net_port_vlans *v,
  273. const struct sk_buff *skb)
  274. {
  275. u16 vid;
  276. /* If this packet was not filtered at input, let it pass */
  277. if (!BR_INPUT_SKB_CB(skb)->vlan_filtered)
  278. return true;
  279. if (!v)
  280. return false;
  281. br_vlan_get_tag(skb, &vid);
  282. if (test_bit(vid, v->vlan_bitmap))
  283. return true;
  284. return false;
  285. }
  286. /* Called under RCU */
  287. bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid)
  288. {
  289. struct net_bridge *br = p->br;
  290. struct net_port_vlans *v;
  291. /* If filtering was disabled at input, let it pass. */
  292. if (!br->vlan_enabled)
  293. return true;
  294. v = rcu_dereference(p->vlan_info);
  295. if (!v)
  296. return false;
  297. if (!br_vlan_get_tag(skb, vid) && skb->vlan_proto != br->vlan_proto)
  298. *vid = 0;
  299. if (!*vid) {
  300. *vid = br_get_pvid(v);
  301. if (!*vid)
  302. return false;
  303. return true;
  304. }
  305. if (test_bit(*vid, v->vlan_bitmap))
  306. return true;
  307. return false;
  308. }
  309. /* Must be protected by RTNL.
  310. * Must be called with vid in range from 1 to 4094 inclusive.
  311. */
  312. int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
  313. {
  314. struct net_port_vlans *pv = NULL;
  315. int err;
  316. ASSERT_RTNL();
  317. pv = rtnl_dereference(br->vlan_info);
  318. if (pv)
  319. return __vlan_add(pv, vid, flags);
  320. /* Create port vlan infomration
  321. */
  322. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  323. if (!pv)
  324. return -ENOMEM;
  325. pv->parent.br = br;
  326. err = __vlan_add(pv, vid, flags);
  327. if (err)
  328. goto out;
  329. rcu_assign_pointer(br->vlan_info, pv);
  330. return 0;
  331. out:
  332. kfree(pv);
  333. return err;
  334. }
  335. /* Must be protected by RTNL.
  336. * Must be called with vid in range from 1 to 4094 inclusive.
  337. */
  338. int br_vlan_delete(struct net_bridge *br, u16 vid)
  339. {
  340. struct net_port_vlans *pv;
  341. ASSERT_RTNL();
  342. pv = rtnl_dereference(br->vlan_info);
  343. if (!pv)
  344. return -EINVAL;
  345. br_fdb_find_delete_local(br, NULL, br->dev->dev_addr, vid);
  346. __vlan_del(pv, vid);
  347. return 0;
  348. }
  349. void br_vlan_flush(struct net_bridge *br)
  350. {
  351. struct net_port_vlans *pv;
  352. ASSERT_RTNL();
  353. pv = rtnl_dereference(br->vlan_info);
  354. if (!pv)
  355. return;
  356. __vlan_flush(pv);
  357. }
  358. bool br_vlan_find(struct net_bridge *br, u16 vid)
  359. {
  360. struct net_port_vlans *pv;
  361. bool found = false;
  362. rcu_read_lock();
  363. pv = rcu_dereference(br->vlan_info);
  364. if (!pv)
  365. goto out;
  366. if (test_bit(vid, pv->vlan_bitmap))
  367. found = true;
  368. out:
  369. rcu_read_unlock();
  370. return found;
  371. }
  372. /* Must be protected by RTNL. */
  373. static void recalculate_group_addr(struct net_bridge *br)
  374. {
  375. if (br->group_addr_set)
  376. return;
  377. spin_lock_bh(&br->lock);
  378. if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q)) {
  379. /* Bridge Group Address */
  380. br->group_addr[5] = 0x00;
  381. } else { /* vlan_enabled && ETH_P_8021AD */
  382. /* Provider Bridge Group Address */
  383. br->group_addr[5] = 0x08;
  384. }
  385. spin_unlock_bh(&br->lock);
  386. }
  387. /* Must be protected by RTNL. */
  388. void br_recalculate_fwd_mask(struct net_bridge *br)
  389. {
  390. if (!br->vlan_enabled || br->vlan_proto == htons(ETH_P_8021Q))
  391. br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
  392. else /* vlan_enabled && ETH_P_8021AD */
  393. br->group_fwd_mask_required = BR_GROUPFWD_8021AD &
  394. ~(1u << br->group_addr[5]);
  395. }
  396. int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
  397. {
  398. if (br->vlan_enabled == val)
  399. return 0;
  400. br->vlan_enabled = val;
  401. br_manage_promisc(br);
  402. recalculate_group_addr(br);
  403. br_recalculate_fwd_mask(br);
  404. return 0;
  405. }
  406. int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
  407. {
  408. if (!rtnl_trylock())
  409. return restart_syscall();
  410. __br_vlan_filter_toggle(br, val);
  411. rtnl_unlock();
  412. return 0;
  413. }
  414. int __br_vlan_set_proto(struct net_bridge *br, __be16 proto)
  415. {
  416. int err = 0;
  417. struct net_bridge_port *p;
  418. struct net_port_vlans *pv;
  419. __be16 oldproto;
  420. u16 vid, errvid;
  421. if (br->vlan_proto == proto)
  422. return 0;
  423. /* Add VLANs for the new proto to the device filter. */
  424. list_for_each_entry(p, &br->port_list, list) {
  425. pv = rtnl_dereference(p->vlan_info);
  426. if (!pv)
  427. continue;
  428. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
  429. err = vlan_vid_add(p->dev, proto, vid);
  430. if (err)
  431. goto err_filt;
  432. }
  433. }
  434. oldproto = br->vlan_proto;
  435. br->vlan_proto = proto;
  436. recalculate_group_addr(br);
  437. br_recalculate_fwd_mask(br);
  438. /* Delete VLANs for the old proto from the device filter. */
  439. list_for_each_entry(p, &br->port_list, list) {
  440. pv = rtnl_dereference(p->vlan_info);
  441. if (!pv)
  442. continue;
  443. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
  444. vlan_vid_del(p->dev, oldproto, vid);
  445. }
  446. return 0;
  447. err_filt:
  448. errvid = vid;
  449. for_each_set_bit(vid, pv->vlan_bitmap, errvid)
  450. vlan_vid_del(p->dev, proto, vid);
  451. list_for_each_entry_continue_reverse(p, &br->port_list, list) {
  452. pv = rtnl_dereference(p->vlan_info);
  453. if (!pv)
  454. continue;
  455. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
  456. vlan_vid_del(p->dev, proto, vid);
  457. }
  458. return err;
  459. }
  460. int br_vlan_set_proto(struct net_bridge *br, unsigned long val)
  461. {
  462. int err;
  463. if (val != ETH_P_8021Q && val != ETH_P_8021AD)
  464. return -EPROTONOSUPPORT;
  465. if (!rtnl_trylock())
  466. return restart_syscall();
  467. err = __br_vlan_set_proto(br, htons(val));
  468. rtnl_unlock();
  469. return err;
  470. }
  471. static bool vlan_default_pvid(struct net_port_vlans *pv, u16 vid)
  472. {
  473. return pv && vid == pv->pvid && test_bit(vid, pv->untagged_bitmap);
  474. }
  475. static void br_vlan_disable_default_pvid(struct net_bridge *br)
  476. {
  477. struct net_bridge_port *p;
  478. u16 pvid = br->default_pvid;
  479. /* Disable default_pvid on all ports where it is still
  480. * configured.
  481. */
  482. if (vlan_default_pvid(br_get_vlan_info(br), pvid))
  483. br_vlan_delete(br, pvid);
  484. list_for_each_entry(p, &br->port_list, list) {
  485. if (vlan_default_pvid(nbp_get_vlan_info(p), pvid))
  486. nbp_vlan_delete(p, pvid);
  487. }
  488. br->default_pvid = 0;
  489. }
  490. static int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid)
  491. {
  492. struct net_bridge_port *p;
  493. u16 old_pvid;
  494. int err = 0;
  495. unsigned long *changed;
  496. changed = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
  497. GFP_KERNEL);
  498. if (!changed)
  499. return -ENOMEM;
  500. old_pvid = br->default_pvid;
  501. /* Update default_pvid config only if we do not conflict with
  502. * user configuration.
  503. */
  504. if ((!old_pvid || vlan_default_pvid(br_get_vlan_info(br), old_pvid)) &&
  505. !br_vlan_find(br, pvid)) {
  506. err = br_vlan_add(br, pvid,
  507. BRIDGE_VLAN_INFO_PVID |
  508. BRIDGE_VLAN_INFO_UNTAGGED);
  509. if (err)
  510. goto out;
  511. br_vlan_delete(br, old_pvid);
  512. set_bit(0, changed);
  513. }
  514. list_for_each_entry(p, &br->port_list, list) {
  515. /* Update default_pvid config only if we do not conflict with
  516. * user configuration.
  517. */
  518. if ((old_pvid &&
  519. !vlan_default_pvid(nbp_get_vlan_info(p), old_pvid)) ||
  520. nbp_vlan_find(p, pvid))
  521. continue;
  522. err = nbp_vlan_add(p, pvid,
  523. BRIDGE_VLAN_INFO_PVID |
  524. BRIDGE_VLAN_INFO_UNTAGGED);
  525. if (err)
  526. goto err_port;
  527. nbp_vlan_delete(p, old_pvid);
  528. set_bit(p->port_no, changed);
  529. }
  530. br->default_pvid = pvid;
  531. out:
  532. kfree(changed);
  533. return err;
  534. err_port:
  535. list_for_each_entry_continue_reverse(p, &br->port_list, list) {
  536. if (!test_bit(p->port_no, changed))
  537. continue;
  538. if (old_pvid)
  539. nbp_vlan_add(p, old_pvid,
  540. BRIDGE_VLAN_INFO_PVID |
  541. BRIDGE_VLAN_INFO_UNTAGGED);
  542. nbp_vlan_delete(p, pvid);
  543. }
  544. if (test_bit(0, changed)) {
  545. if (old_pvid)
  546. br_vlan_add(br, old_pvid,
  547. BRIDGE_VLAN_INFO_PVID |
  548. BRIDGE_VLAN_INFO_UNTAGGED);
  549. br_vlan_delete(br, pvid);
  550. }
  551. goto out;
  552. }
  553. int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val)
  554. {
  555. u16 pvid = val;
  556. int err = 0;
  557. if (val >= VLAN_VID_MASK)
  558. return -EINVAL;
  559. if (!rtnl_trylock())
  560. return restart_syscall();
  561. if (pvid == br->default_pvid)
  562. goto unlock;
  563. /* Only allow default pvid change when filtering is disabled */
  564. if (br->vlan_enabled) {
  565. pr_info_once("Please disable vlan filtering to change default_pvid\n");
  566. err = -EPERM;
  567. goto unlock;
  568. }
  569. if (!pvid)
  570. br_vlan_disable_default_pvid(br);
  571. else
  572. err = __br_vlan_set_default_pvid(br, pvid);
  573. unlock:
  574. rtnl_unlock();
  575. return err;
  576. }
  577. int br_vlan_init(struct net_bridge *br)
  578. {
  579. br->vlan_proto = htons(ETH_P_8021Q);
  580. br->default_pvid = 1;
  581. return br_vlan_add(br, 1,
  582. BRIDGE_VLAN_INFO_PVID | BRIDGE_VLAN_INFO_UNTAGGED);
  583. }
  584. /* Must be protected by RTNL.
  585. * Must be called with vid in range from 1 to 4094 inclusive.
  586. */
  587. int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
  588. {
  589. struct net_port_vlans *pv = NULL;
  590. int err;
  591. ASSERT_RTNL();
  592. pv = rtnl_dereference(port->vlan_info);
  593. if (pv)
  594. return __vlan_add(pv, vid, flags);
  595. /* Create port vlan infomration
  596. */
  597. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  598. if (!pv) {
  599. err = -ENOMEM;
  600. goto clean_up;
  601. }
  602. pv->port_idx = port->port_no;
  603. pv->parent.port = port;
  604. err = __vlan_add(pv, vid, flags);
  605. if (err)
  606. goto clean_up;
  607. rcu_assign_pointer(port->vlan_info, pv);
  608. return 0;
  609. clean_up:
  610. kfree(pv);
  611. return err;
  612. }
  613. /* Must be protected by RTNL.
  614. * Must be called with vid in range from 1 to 4094 inclusive.
  615. */
  616. int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
  617. {
  618. struct net_port_vlans *pv;
  619. ASSERT_RTNL();
  620. pv = rtnl_dereference(port->vlan_info);
  621. if (!pv)
  622. return -EINVAL;
  623. br_fdb_find_delete_local(port->br, port, port->dev->dev_addr, vid);
  624. br_fdb_delete_by_port(port->br, port, vid, 0);
  625. return __vlan_del(pv, vid);
  626. }
  627. void nbp_vlan_flush(struct net_bridge_port *port)
  628. {
  629. struct net_port_vlans *pv;
  630. u16 vid;
  631. ASSERT_RTNL();
  632. pv = rtnl_dereference(port->vlan_info);
  633. if (!pv)
  634. return;
  635. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
  636. vlan_vid_del(port->dev, port->br->vlan_proto, vid);
  637. __vlan_flush(pv);
  638. }
  639. bool nbp_vlan_find(struct net_bridge_port *port, u16 vid)
  640. {
  641. struct net_port_vlans *pv;
  642. bool found = false;
  643. rcu_read_lock();
  644. pv = rcu_dereference(port->vlan_info);
  645. if (!pv)
  646. goto out;
  647. if (test_bit(vid, pv->vlan_bitmap))
  648. found = true;
  649. out:
  650. rcu_read_unlock();
  651. return found;
  652. }
  653. int nbp_vlan_init(struct net_bridge_port *p)
  654. {
  655. return p->br->default_pvid ?
  656. nbp_vlan_add(p, p->br->default_pvid,
  657. BRIDGE_VLAN_INFO_PVID |
  658. BRIDGE_VLAN_INFO_UNTAGGED) :
  659. 0;
  660. }