br_vlan.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 int __vlan_add(struct net_port_vlans *v, u16 vid)
  7. {
  8. int err;
  9. if (test_bit(vid, v->vlan_bitmap))
  10. return -EEXIST;
  11. if (v->port_idx && vid) {
  12. struct net_device *dev = v->parent.port->dev;
  13. /* Add VLAN to the device filter if it is supported.
  14. * Stricly speaking, this is not necessary now, since devices
  15. * are made promiscuous by the bridge, but if that ever changes
  16. * this code will allow tagged traffic to enter the bridge.
  17. */
  18. if (dev->features & NETIF_F_HW_VLAN_FILTER) {
  19. err = dev->netdev_ops->ndo_vlan_rx_add_vid(dev, vid);
  20. if (err)
  21. return err;
  22. }
  23. }
  24. set_bit(vid, v->vlan_bitmap);
  25. return 0;
  26. }
  27. static int __vlan_del(struct net_port_vlans *v, u16 vid)
  28. {
  29. if (!test_bit(vid, v->vlan_bitmap))
  30. return -EINVAL;
  31. if (v->port_idx && vid) {
  32. struct net_device *dev = v->parent.port->dev;
  33. if (dev->features & NETIF_F_HW_VLAN_FILTER)
  34. dev->netdev_ops->ndo_vlan_rx_kill_vid(dev, vid);
  35. }
  36. clear_bit(vid, v->vlan_bitmap);
  37. if (bitmap_empty(v->vlan_bitmap, BR_VLAN_BITMAP_LEN)) {
  38. if (v->port_idx)
  39. rcu_assign_pointer(v->parent.port->vlan_info, NULL);
  40. else
  41. rcu_assign_pointer(v->parent.br->vlan_info, NULL);
  42. kfree_rcu(v, rcu);
  43. }
  44. return 0;
  45. }
  46. static void __vlan_flush(struct net_port_vlans *v)
  47. {
  48. bitmap_zero(v->vlan_bitmap, BR_VLAN_BITMAP_LEN);
  49. if (v->port_idx)
  50. rcu_assign_pointer(v->parent.port->vlan_info, NULL);
  51. else
  52. rcu_assign_pointer(v->parent.br->vlan_info, NULL);
  53. kfree_rcu(v, rcu);
  54. }
  55. /* Called under RCU */
  56. bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
  57. struct sk_buff *skb)
  58. {
  59. u16 vid;
  60. /* If VLAN filtering is disabled on the bridge, all packets are
  61. * permitted.
  62. */
  63. if (!br->vlan_enabled)
  64. return true;
  65. /* If there are no vlan in the permitted list, all packets are
  66. * rejected.
  67. */
  68. if (!v)
  69. return false;
  70. br_vlan_get_tag(skb, &vid);
  71. if (test_bit(vid, v->vlan_bitmap))
  72. return true;
  73. return false;
  74. }
  75. /* Called under RCU. */
  76. bool br_allowed_egress(struct net_bridge *br,
  77. const struct net_port_vlans *v,
  78. const struct sk_buff *skb)
  79. {
  80. u16 vid;
  81. if (!br->vlan_enabled)
  82. return true;
  83. if (!v)
  84. return false;
  85. br_vlan_get_tag(skb, &vid);
  86. if (test_bit(vid, v->vlan_bitmap))
  87. return true;
  88. return false;
  89. }
  90. /* Must be protected by RTNL */
  91. int br_vlan_add(struct net_bridge *br, u16 vid)
  92. {
  93. struct net_port_vlans *pv = NULL;
  94. int err;
  95. ASSERT_RTNL();
  96. pv = rtnl_dereference(br->vlan_info);
  97. if (pv)
  98. return __vlan_add(pv, vid);
  99. /* Create port vlan infomration
  100. */
  101. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  102. if (!pv)
  103. return -ENOMEM;
  104. pv->parent.br = br;
  105. err = __vlan_add(pv, vid);
  106. if (err)
  107. goto out;
  108. rcu_assign_pointer(br->vlan_info, pv);
  109. return 0;
  110. out:
  111. kfree(pv);
  112. return err;
  113. }
  114. /* Must be protected by RTNL */
  115. int br_vlan_delete(struct net_bridge *br, u16 vid)
  116. {
  117. struct net_port_vlans *pv;
  118. ASSERT_RTNL();
  119. pv = rtnl_dereference(br->vlan_info);
  120. if (!pv)
  121. return -EINVAL;
  122. __vlan_del(pv, vid);
  123. return 0;
  124. }
  125. void br_vlan_flush(struct net_bridge *br)
  126. {
  127. struct net_port_vlans *pv;
  128. ASSERT_RTNL();
  129. pv = rtnl_dereference(br->vlan_info);
  130. if (!pv)
  131. return;
  132. __vlan_flush(pv);
  133. }
  134. int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val)
  135. {
  136. if (!rtnl_trylock())
  137. return restart_syscall();
  138. if (br->vlan_enabled == val)
  139. goto unlock;
  140. br->vlan_enabled = val;
  141. unlock:
  142. rtnl_unlock();
  143. return 0;
  144. }
  145. /* Must be protected by RTNL */
  146. int nbp_vlan_add(struct net_bridge_port *port, u16 vid)
  147. {
  148. struct net_port_vlans *pv = NULL;
  149. int err;
  150. ASSERT_RTNL();
  151. pv = rtnl_dereference(port->vlan_info);
  152. if (pv)
  153. return __vlan_add(pv, vid);
  154. /* Create port vlan infomration
  155. */
  156. pv = kzalloc(sizeof(*pv), GFP_KERNEL);
  157. if (!pv) {
  158. err = -ENOMEM;
  159. goto clean_up;
  160. }
  161. pv->port_idx = port->port_no;
  162. pv->parent.port = port;
  163. err = __vlan_add(pv, vid);
  164. if (err)
  165. goto clean_up;
  166. rcu_assign_pointer(port->vlan_info, pv);
  167. return 0;
  168. clean_up:
  169. kfree(pv);
  170. return err;
  171. }
  172. /* Must be protected by RTNL */
  173. int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
  174. {
  175. struct net_port_vlans *pv;
  176. ASSERT_RTNL();
  177. pv = rtnl_dereference(port->vlan_info);
  178. if (!pv)
  179. return -EINVAL;
  180. return __vlan_del(pv, vid);
  181. }
  182. void nbp_vlan_flush(struct net_bridge_port *port)
  183. {
  184. struct net_port_vlans *pv;
  185. ASSERT_RTNL();
  186. pv = rtnl_dereference(port->vlan_info);
  187. if (!pv)
  188. return;
  189. __vlan_flush(pv);
  190. }