br_device.c 10.0 KB

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