br_device.c 9.5 KB

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