br_device.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Device handling code
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/netpoll.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/list.h>
  19. #include <linux/netfilter_bridge.h>
  20. #include <asm/uaccess.h>
  21. #include "br_private.h"
  22. #define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \
  23. NETIF_F_GSO_MASK | NETIF_F_HW_CSUM)
  24. const struct nf_br_ops __rcu *nf_br_ops __read_mostly;
  25. EXPORT_SYMBOL_GPL(nf_br_ops);
  26. static struct lock_class_key bridge_netdev_addr_lock_key;
  27. /* net device transmit always called with BH disabled */
  28. netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  29. {
  30. struct net_bridge *br = netdev_priv(dev);
  31. const unsigned char *dest = skb->data;
  32. struct net_bridge_fdb_entry *dst;
  33. struct net_bridge_mdb_entry *mdst;
  34. struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
  35. const struct nf_br_ops *nf_ops;
  36. u16 vid = 0;
  37. rcu_read_lock();
  38. nf_ops = rcu_dereference(nf_br_ops);
  39. if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) {
  40. rcu_read_unlock();
  41. return NETDEV_TX_OK;
  42. }
  43. u64_stats_update_begin(&brstats->syncp);
  44. brstats->tx_packets++;
  45. brstats->tx_bytes += skb->len;
  46. u64_stats_update_end(&brstats->syncp);
  47. #ifdef CONFIG_NET_SWITCHDEV
  48. skb->offload_fwd_mark = 0;
  49. #endif
  50. BR_INPUT_SKB_CB(skb)->brdev = dev;
  51. skb_reset_mac_header(skb);
  52. skb_pull(skb, ETH_HLEN);
  53. if (!br_allowed_ingress(br, br_vlan_group_rcu(br), skb, &vid))
  54. goto out;
  55. if (is_broadcast_ether_addr(dest)) {
  56. br_flood(br, skb, BR_PKT_BROADCAST, false, true);
  57. } else if (is_multicast_ether_addr(dest)) {
  58. if (unlikely(netpoll_tx_running(dev))) {
  59. br_flood(br, skb, BR_PKT_MULTICAST, false, true);
  60. goto out;
  61. }
  62. if (br_multicast_rcv(br, NULL, skb, vid)) {
  63. kfree_skb(skb);
  64. goto out;
  65. }
  66. mdst = br_mdb_get(br, skb, vid);
  67. if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
  68. br_multicast_querier_exists(br, eth_hdr(skb)))
  69. br_multicast_flood(mdst, skb, false, true);
  70. else
  71. br_flood(br, skb, BR_PKT_MULTICAST, false, true);
  72. } else if ((dst = __br_fdb_get(br, dest, vid)) != NULL) {
  73. br_forward(dst->dst, skb, false, true);
  74. } else {
  75. br_flood(br, skb, BR_PKT_UNICAST, false, true);
  76. }
  77. out:
  78. rcu_read_unlock();
  79. return NETDEV_TX_OK;
  80. }
  81. static void br_set_lockdep_class(struct net_device *dev)
  82. {
  83. lockdep_set_class(&dev->addr_list_lock, &bridge_netdev_addr_lock_key);
  84. }
  85. static int br_dev_init(struct net_device *dev)
  86. {
  87. struct net_bridge *br = netdev_priv(dev);
  88. int err;
  89. br->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  90. if (!br->stats)
  91. return -ENOMEM;
  92. err = br_vlan_init(br);
  93. if (err) {
  94. free_percpu(br->stats);
  95. return err;
  96. }
  97. err = br_multicast_init_stats(br);
  98. if (err) {
  99. free_percpu(br->stats);
  100. br_vlan_flush(br);
  101. }
  102. br_set_lockdep_class(dev);
  103. return err;
  104. }
  105. static int br_dev_open(struct net_device *dev)
  106. {
  107. struct net_bridge *br = netdev_priv(dev);
  108. netdev_update_features(dev);
  109. netif_start_queue(dev);
  110. br_stp_enable_bridge(br);
  111. br_multicast_open(br);
  112. return 0;
  113. }
  114. static void br_dev_set_multicast_list(struct net_device *dev)
  115. {
  116. }
  117. static void br_dev_change_rx_flags(struct net_device *dev, int change)
  118. {
  119. if (change & IFF_PROMISC)
  120. br_manage_promisc(netdev_priv(dev));
  121. }
  122. static int br_dev_stop(struct net_device *dev)
  123. {
  124. struct net_bridge *br = netdev_priv(dev);
  125. br_stp_disable_bridge(br);
  126. br_multicast_stop(br);
  127. netif_stop_queue(dev);
  128. return 0;
  129. }
  130. static struct rtnl_link_stats64 *br_get_stats64(struct net_device *dev,
  131. struct rtnl_link_stats64 *stats)
  132. {
  133. struct net_bridge *br = netdev_priv(dev);
  134. struct pcpu_sw_netstats tmp, sum = { 0 };
  135. unsigned int cpu;
  136. for_each_possible_cpu(cpu) {
  137. unsigned int start;
  138. const struct pcpu_sw_netstats *bstats
  139. = per_cpu_ptr(br->stats, cpu);
  140. do {
  141. start = u64_stats_fetch_begin_irq(&bstats->syncp);
  142. memcpy(&tmp, bstats, sizeof(tmp));
  143. } while (u64_stats_fetch_retry_irq(&bstats->syncp, start));
  144. sum.tx_bytes += tmp.tx_bytes;
  145. sum.tx_packets += tmp.tx_packets;
  146. sum.rx_bytes += tmp.rx_bytes;
  147. sum.rx_packets += tmp.rx_packets;
  148. }
  149. stats->tx_bytes = sum.tx_bytes;
  150. stats->tx_packets = sum.tx_packets;
  151. stats->rx_bytes = sum.rx_bytes;
  152. stats->rx_packets = sum.rx_packets;
  153. return stats;
  154. }
  155. static int br_change_mtu(struct net_device *dev, int new_mtu)
  156. {
  157. struct net_bridge *br = netdev_priv(dev);
  158. if (new_mtu < 68 || new_mtu > br_min_mtu(br))
  159. return -EINVAL;
  160. dev->mtu = new_mtu;
  161. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  162. /* remember the MTU in the rtable for PMTU */
  163. dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
  164. #endif
  165. return 0;
  166. }
  167. /* Allow setting mac address to any valid ethernet address. */
  168. static int br_set_mac_address(struct net_device *dev, void *p)
  169. {
  170. struct net_bridge *br = netdev_priv(dev);
  171. struct sockaddr *addr = p;
  172. if (!is_valid_ether_addr(addr->sa_data))
  173. return -EADDRNOTAVAIL;
  174. spin_lock_bh(&br->lock);
  175. if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) {
  176. /* Mac address will be changed in br_stp_change_bridge_id(). */
  177. br_stp_change_bridge_id(br, addr->sa_data);
  178. }
  179. spin_unlock_bh(&br->lock);
  180. return 0;
  181. }
  182. static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  183. {
  184. strlcpy(info->driver, "bridge", sizeof(info->driver));
  185. strlcpy(info->version, BR_VERSION, sizeof(info->version));
  186. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  187. strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
  188. }
  189. static netdev_features_t br_fix_features(struct net_device *dev,
  190. netdev_features_t features)
  191. {
  192. struct net_bridge *br = netdev_priv(dev);
  193. return br_features_recompute(br, features);
  194. }
  195. #ifdef CONFIG_NET_POLL_CONTROLLER
  196. static void br_poll_controller(struct net_device *br_dev)
  197. {
  198. }
  199. static void br_netpoll_cleanup(struct net_device *dev)
  200. {
  201. struct net_bridge *br = netdev_priv(dev);
  202. struct net_bridge_port *p;
  203. list_for_each_entry(p, &br->port_list, list)
  204. br_netpoll_disable(p);
  205. }
  206. static int __br_netpoll_enable(struct net_bridge_port *p)
  207. {
  208. struct netpoll *np;
  209. int err;
  210. np = kzalloc(sizeof(*p->np), GFP_KERNEL);
  211. if (!np)
  212. return -ENOMEM;
  213. err = __netpoll_setup(np, p->dev);
  214. if (err) {
  215. kfree(np);
  216. return err;
  217. }
  218. p->np = np;
  219. return err;
  220. }
  221. int br_netpoll_enable(struct net_bridge_port *p)
  222. {
  223. if (!p->br->dev->npinfo)
  224. return 0;
  225. return __br_netpoll_enable(p);
  226. }
  227. static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
  228. {
  229. struct net_bridge *br = netdev_priv(dev);
  230. struct net_bridge_port *p;
  231. int err = 0;
  232. list_for_each_entry(p, &br->port_list, list) {
  233. if (!p->dev)
  234. continue;
  235. err = __br_netpoll_enable(p);
  236. if (err)
  237. goto fail;
  238. }
  239. out:
  240. return err;
  241. fail:
  242. br_netpoll_cleanup(dev);
  243. goto out;
  244. }
  245. void br_netpoll_disable(struct net_bridge_port *p)
  246. {
  247. struct netpoll *np = p->np;
  248. if (!np)
  249. return;
  250. p->np = NULL;
  251. __netpoll_free_async(np);
  252. }
  253. #endif
  254. static int br_add_slave(struct net_device *dev, struct net_device *slave_dev)
  255. {
  256. struct net_bridge *br = netdev_priv(dev);
  257. return br_add_if(br, slave_dev);
  258. }
  259. static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
  260. {
  261. struct net_bridge *br = netdev_priv(dev);
  262. return br_del_if(br, slave_dev);
  263. }
  264. static const struct ethtool_ops br_ethtool_ops = {
  265. .get_drvinfo = br_getinfo,
  266. .get_link = ethtool_op_get_link,
  267. };
  268. static const struct net_device_ops br_netdev_ops = {
  269. .ndo_open = br_dev_open,
  270. .ndo_stop = br_dev_stop,
  271. .ndo_init = br_dev_init,
  272. .ndo_start_xmit = br_dev_xmit,
  273. .ndo_get_stats64 = br_get_stats64,
  274. .ndo_set_mac_address = br_set_mac_address,
  275. .ndo_set_rx_mode = br_dev_set_multicast_list,
  276. .ndo_change_rx_flags = br_dev_change_rx_flags,
  277. .ndo_change_mtu = br_change_mtu,
  278. .ndo_do_ioctl = br_dev_ioctl,
  279. #ifdef CONFIG_NET_POLL_CONTROLLER
  280. .ndo_netpoll_setup = br_netpoll_setup,
  281. .ndo_netpoll_cleanup = br_netpoll_cleanup,
  282. .ndo_poll_controller = br_poll_controller,
  283. #endif
  284. .ndo_add_slave = br_add_slave,
  285. .ndo_del_slave = br_del_slave,
  286. .ndo_fix_features = br_fix_features,
  287. .ndo_neigh_construct = netdev_default_l2upper_neigh_construct,
  288. .ndo_neigh_destroy = netdev_default_l2upper_neigh_destroy,
  289. .ndo_fdb_add = br_fdb_add,
  290. .ndo_fdb_del = br_fdb_delete,
  291. .ndo_fdb_dump = br_fdb_dump,
  292. .ndo_bridge_getlink = br_getlink,
  293. .ndo_bridge_setlink = br_setlink,
  294. .ndo_bridge_dellink = br_dellink,
  295. .ndo_features_check = passthru_features_check,
  296. };
  297. static void br_dev_free(struct net_device *dev)
  298. {
  299. struct net_bridge *br = netdev_priv(dev);
  300. free_percpu(br->stats);
  301. free_netdev(dev);
  302. }
  303. static struct device_type br_type = {
  304. .name = "bridge",
  305. };
  306. void br_dev_setup(struct net_device *dev)
  307. {
  308. struct net_bridge *br = netdev_priv(dev);
  309. eth_hw_addr_random(dev);
  310. ether_setup(dev);
  311. dev->netdev_ops = &br_netdev_ops;
  312. dev->destructor = br_dev_free;
  313. dev->ethtool_ops = &br_ethtool_ops;
  314. SET_NETDEV_DEVTYPE(dev, &br_type);
  315. dev->priv_flags = IFF_EBRIDGE | IFF_NO_QUEUE;
  316. dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL |
  317. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  318. dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
  319. NETIF_F_HW_VLAN_STAG_TX;
  320. dev->vlan_features = COMMON_FEATURES;
  321. br->dev = dev;
  322. spin_lock_init(&br->lock);
  323. INIT_LIST_HEAD(&br->port_list);
  324. spin_lock_init(&br->hash_lock);
  325. br->bridge_id.prio[0] = 0x80;
  326. br->bridge_id.prio[1] = 0x00;
  327. ether_addr_copy(br->group_addr, eth_reserved_addr_base);
  328. br->stp_enabled = BR_NO_STP;
  329. br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
  330. br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
  331. br->designated_root = br->bridge_id;
  332. br->bridge_max_age = br->max_age = 20 * HZ;
  333. br->bridge_hello_time = br->hello_time = 2 * HZ;
  334. br->bridge_forward_delay = br->forward_delay = 15 * HZ;
  335. br->ageing_time = BR_DEFAULT_AGEING_TIME;
  336. br_netfilter_rtable_init(br);
  337. br_stp_timer_init(br);
  338. br_multicast_init(br);
  339. }