br_device.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 <linux/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. struct net_bridge_fdb_entry *dst;
  32. struct net_bridge_mdb_entry *mdst;
  33. struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
  34. const struct nf_br_ops *nf_ops;
  35. const unsigned char *dest;
  36. struct ethhdr *eth;
  37. u16 vid = 0;
  38. rcu_read_lock();
  39. nf_ops = rcu_dereference(nf_br_ops);
  40. if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) {
  41. rcu_read_unlock();
  42. return NETDEV_TX_OK;
  43. }
  44. u64_stats_update_begin(&brstats->syncp);
  45. brstats->tx_packets++;
  46. brstats->tx_bytes += skb->len;
  47. u64_stats_update_end(&brstats->syncp);
  48. br_switchdev_frame_unmark(skb);
  49. BR_INPUT_SKB_CB(skb)->brdev = dev;
  50. skb_reset_mac_header(skb);
  51. eth = eth_hdr(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_ENABLED(CONFIG_INET) &&
  56. (eth->h_proto == htons(ETH_P_ARP) ||
  57. eth->h_proto == htons(ETH_P_RARP)) &&
  58. br->neigh_suppress_enabled) {
  59. br_do_proxy_suppress_arp(skb, br, vid, NULL);
  60. } else if (IS_ENABLED(CONFIG_IPV6) &&
  61. skb->protocol == htons(ETH_P_IPV6) &&
  62. br->neigh_suppress_enabled &&
  63. pskb_may_pull(skb, sizeof(struct ipv6hdr) +
  64. sizeof(struct nd_msg)) &&
  65. ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
  66. struct nd_msg *msg, _msg;
  67. msg = br_is_nd_neigh_msg(skb, &_msg);
  68. if (msg)
  69. br_do_suppress_nd(skb, br, vid, NULL, msg);
  70. }
  71. dest = eth_hdr(skb)->h_dest;
  72. if (is_broadcast_ether_addr(dest)) {
  73. br_flood(br, skb, BR_PKT_BROADCAST, false, true);
  74. } else if (is_multicast_ether_addr(dest)) {
  75. if (unlikely(netpoll_tx_running(dev))) {
  76. br_flood(br, skb, BR_PKT_MULTICAST, false, true);
  77. goto out;
  78. }
  79. if (br_multicast_rcv(br, NULL, skb, vid)) {
  80. kfree_skb(skb);
  81. goto out;
  82. }
  83. mdst = br_mdb_get(br, skb, vid);
  84. if ((mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb)) &&
  85. br_multicast_querier_exists(br, eth_hdr(skb)))
  86. br_multicast_flood(mdst, skb, false, true);
  87. else
  88. br_flood(br, skb, BR_PKT_MULTICAST, false, true);
  89. } else if ((dst = br_fdb_find_rcu(br, dest, vid)) != NULL) {
  90. br_forward(dst->dst, skb, false, true);
  91. } else {
  92. br_flood(br, skb, BR_PKT_UNICAST, false, true);
  93. }
  94. out:
  95. rcu_read_unlock();
  96. return NETDEV_TX_OK;
  97. }
  98. static void br_set_lockdep_class(struct net_device *dev)
  99. {
  100. lockdep_set_class(&dev->addr_list_lock, &bridge_netdev_addr_lock_key);
  101. }
  102. static int br_dev_init(struct net_device *dev)
  103. {
  104. struct net_bridge *br = netdev_priv(dev);
  105. int err;
  106. br->stats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  107. if (!br->stats)
  108. return -ENOMEM;
  109. err = br_fdb_hash_init(br);
  110. if (err) {
  111. free_percpu(br->stats);
  112. return err;
  113. }
  114. err = br_vlan_init(br);
  115. if (err) {
  116. free_percpu(br->stats);
  117. br_fdb_hash_fini(br);
  118. return err;
  119. }
  120. err = br_multicast_init_stats(br);
  121. if (err) {
  122. free_percpu(br->stats);
  123. br_vlan_flush(br);
  124. br_fdb_hash_fini(br);
  125. }
  126. br_set_lockdep_class(dev);
  127. return err;
  128. }
  129. static void br_dev_uninit(struct net_device *dev)
  130. {
  131. struct net_bridge *br = netdev_priv(dev);
  132. br_multicast_dev_del(br);
  133. br_multicast_uninit_stats(br);
  134. br_vlan_flush(br);
  135. br_fdb_hash_fini(br);
  136. free_percpu(br->stats);
  137. }
  138. static int br_dev_open(struct net_device *dev)
  139. {
  140. struct net_bridge *br = netdev_priv(dev);
  141. netdev_update_features(dev);
  142. netif_start_queue(dev);
  143. br_stp_enable_bridge(br);
  144. br_multicast_open(br);
  145. return 0;
  146. }
  147. static void br_dev_set_multicast_list(struct net_device *dev)
  148. {
  149. }
  150. static void br_dev_change_rx_flags(struct net_device *dev, int change)
  151. {
  152. if (change & IFF_PROMISC)
  153. br_manage_promisc(netdev_priv(dev));
  154. }
  155. static int br_dev_stop(struct net_device *dev)
  156. {
  157. struct net_bridge *br = netdev_priv(dev);
  158. br_stp_disable_bridge(br);
  159. br_multicast_stop(br);
  160. netif_stop_queue(dev);
  161. return 0;
  162. }
  163. static void br_get_stats64(struct net_device *dev,
  164. struct rtnl_link_stats64 *stats)
  165. {
  166. struct net_bridge *br = netdev_priv(dev);
  167. struct pcpu_sw_netstats tmp, sum = { 0 };
  168. unsigned int cpu;
  169. for_each_possible_cpu(cpu) {
  170. unsigned int start;
  171. const struct pcpu_sw_netstats *bstats
  172. = per_cpu_ptr(br->stats, cpu);
  173. do {
  174. start = u64_stats_fetch_begin_irq(&bstats->syncp);
  175. memcpy(&tmp, bstats, sizeof(tmp));
  176. } while (u64_stats_fetch_retry_irq(&bstats->syncp, start));
  177. sum.tx_bytes += tmp.tx_bytes;
  178. sum.tx_packets += tmp.tx_packets;
  179. sum.rx_bytes += tmp.rx_bytes;
  180. sum.rx_packets += tmp.rx_packets;
  181. }
  182. stats->tx_bytes = sum.tx_bytes;
  183. stats->tx_packets = sum.tx_packets;
  184. stats->rx_bytes = sum.rx_bytes;
  185. stats->rx_packets = sum.rx_packets;
  186. }
  187. static int br_change_mtu(struct net_device *dev, int new_mtu)
  188. {
  189. struct net_bridge *br = netdev_priv(dev);
  190. dev->mtu = new_mtu;
  191. /* this flag will be cleared if the MTU was automatically adjusted */
  192. br->mtu_set_by_user = true;
  193. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  194. /* remember the MTU in the rtable for PMTU */
  195. dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
  196. #endif
  197. return 0;
  198. }
  199. /* Allow setting mac address to any valid ethernet address. */
  200. static int br_set_mac_address(struct net_device *dev, void *p)
  201. {
  202. struct net_bridge *br = netdev_priv(dev);
  203. struct sockaddr *addr = p;
  204. if (!is_valid_ether_addr(addr->sa_data))
  205. return -EADDRNOTAVAIL;
  206. /* dev_set_mac_addr() can be called by a master device on bridge's
  207. * NETDEV_UNREGISTER, but since it's being destroyed do nothing
  208. */
  209. if (dev->reg_state != NETREG_REGISTERED)
  210. return -EBUSY;
  211. spin_lock_bh(&br->lock);
  212. if (!ether_addr_equal(dev->dev_addr, addr->sa_data)) {
  213. /* Mac address will be changed in br_stp_change_bridge_id(). */
  214. br_stp_change_bridge_id(br, addr->sa_data);
  215. }
  216. spin_unlock_bh(&br->lock);
  217. return 0;
  218. }
  219. static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  220. {
  221. strlcpy(info->driver, "bridge", sizeof(info->driver));
  222. strlcpy(info->version, BR_VERSION, sizeof(info->version));
  223. strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
  224. strlcpy(info->bus_info, "N/A", sizeof(info->bus_info));
  225. }
  226. static netdev_features_t br_fix_features(struct net_device *dev,
  227. netdev_features_t features)
  228. {
  229. struct net_bridge *br = netdev_priv(dev);
  230. return br_features_recompute(br, features);
  231. }
  232. #ifdef CONFIG_NET_POLL_CONTROLLER
  233. static void br_poll_controller(struct net_device *br_dev)
  234. {
  235. }
  236. static void br_netpoll_cleanup(struct net_device *dev)
  237. {
  238. struct net_bridge *br = netdev_priv(dev);
  239. struct net_bridge_port *p;
  240. list_for_each_entry(p, &br->port_list, list)
  241. br_netpoll_disable(p);
  242. }
  243. static int __br_netpoll_enable(struct net_bridge_port *p)
  244. {
  245. struct netpoll *np;
  246. int err;
  247. np = kzalloc(sizeof(*p->np), GFP_KERNEL);
  248. if (!np)
  249. return -ENOMEM;
  250. err = __netpoll_setup(np, p->dev);
  251. if (err) {
  252. kfree(np);
  253. return err;
  254. }
  255. p->np = np;
  256. return err;
  257. }
  258. int br_netpoll_enable(struct net_bridge_port *p)
  259. {
  260. if (!p->br->dev->npinfo)
  261. return 0;
  262. return __br_netpoll_enable(p);
  263. }
  264. static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
  265. {
  266. struct net_bridge *br = netdev_priv(dev);
  267. struct net_bridge_port *p;
  268. int err = 0;
  269. list_for_each_entry(p, &br->port_list, list) {
  270. if (!p->dev)
  271. continue;
  272. err = __br_netpoll_enable(p);
  273. if (err)
  274. goto fail;
  275. }
  276. out:
  277. return err;
  278. fail:
  279. br_netpoll_cleanup(dev);
  280. goto out;
  281. }
  282. void br_netpoll_disable(struct net_bridge_port *p)
  283. {
  284. struct netpoll *np = p->np;
  285. if (!np)
  286. return;
  287. p->np = NULL;
  288. __netpoll_free_async(np);
  289. }
  290. #endif
  291. static int br_add_slave(struct net_device *dev, struct net_device *slave_dev,
  292. struct netlink_ext_ack *extack)
  293. {
  294. struct net_bridge *br = netdev_priv(dev);
  295. return br_add_if(br, slave_dev, extack);
  296. }
  297. static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
  298. {
  299. struct net_bridge *br = netdev_priv(dev);
  300. return br_del_if(br, slave_dev);
  301. }
  302. static const struct ethtool_ops br_ethtool_ops = {
  303. .get_drvinfo = br_getinfo,
  304. .get_link = ethtool_op_get_link,
  305. };
  306. static const struct net_device_ops br_netdev_ops = {
  307. .ndo_open = br_dev_open,
  308. .ndo_stop = br_dev_stop,
  309. .ndo_init = br_dev_init,
  310. .ndo_uninit = br_dev_uninit,
  311. .ndo_start_xmit = br_dev_xmit,
  312. .ndo_get_stats64 = br_get_stats64,
  313. .ndo_set_mac_address = br_set_mac_address,
  314. .ndo_set_rx_mode = br_dev_set_multicast_list,
  315. .ndo_change_rx_flags = br_dev_change_rx_flags,
  316. .ndo_change_mtu = br_change_mtu,
  317. .ndo_do_ioctl = br_dev_ioctl,
  318. #ifdef CONFIG_NET_POLL_CONTROLLER
  319. .ndo_netpoll_setup = br_netpoll_setup,
  320. .ndo_netpoll_cleanup = br_netpoll_cleanup,
  321. .ndo_poll_controller = br_poll_controller,
  322. #endif
  323. .ndo_add_slave = br_add_slave,
  324. .ndo_del_slave = br_del_slave,
  325. .ndo_fix_features = br_fix_features,
  326. .ndo_fdb_add = br_fdb_add,
  327. .ndo_fdb_del = br_fdb_delete,
  328. .ndo_fdb_dump = br_fdb_dump,
  329. .ndo_bridge_getlink = br_getlink,
  330. .ndo_bridge_setlink = br_setlink,
  331. .ndo_bridge_dellink = br_dellink,
  332. .ndo_features_check = passthru_features_check,
  333. };
  334. static struct device_type br_type = {
  335. .name = "bridge",
  336. };
  337. void br_dev_setup(struct net_device *dev)
  338. {
  339. struct net_bridge *br = netdev_priv(dev);
  340. eth_hw_addr_random(dev);
  341. ether_setup(dev);
  342. dev->netdev_ops = &br_netdev_ops;
  343. dev->needs_free_netdev = true;
  344. dev->ethtool_ops = &br_ethtool_ops;
  345. SET_NETDEV_DEVTYPE(dev, &br_type);
  346. dev->priv_flags = IFF_EBRIDGE | IFF_NO_QUEUE;
  347. dev->features = COMMON_FEATURES | NETIF_F_LLTX | NETIF_F_NETNS_LOCAL |
  348. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
  349. dev->hw_features = COMMON_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
  350. NETIF_F_HW_VLAN_STAG_TX;
  351. dev->vlan_features = COMMON_FEATURES;
  352. br->dev = dev;
  353. spin_lock_init(&br->lock);
  354. INIT_LIST_HEAD(&br->port_list);
  355. INIT_HLIST_HEAD(&br->fdb_list);
  356. spin_lock_init(&br->hash_lock);
  357. br->bridge_id.prio[0] = 0x80;
  358. br->bridge_id.prio[1] = 0x00;
  359. ether_addr_copy(br->group_addr, eth_stp_addr);
  360. br->stp_enabled = BR_NO_STP;
  361. br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
  362. br->group_fwd_mask_required = BR_GROUPFWD_DEFAULT;
  363. br->designated_root = br->bridge_id;
  364. br->bridge_max_age = br->max_age = 20 * HZ;
  365. br->bridge_hello_time = br->hello_time = 2 * HZ;
  366. br->bridge_forward_delay = br->forward_delay = 15 * HZ;
  367. br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME;
  368. dev->max_mtu = ETH_MAX_MTU;
  369. br_netfilter_rtable_init(br);
  370. br_stp_timer_init(br);
  371. br_multicast_init(br);
  372. INIT_DELAYED_WORK(&br->gc_work, br_fdb_cleanup);
  373. }