br_vlan.c 16 KB

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