br_device.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. /* net device transmit always called with BH disabled */
  23. netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  24. {
  25. struct net_bridge *br = netdev_priv(dev);
  26. const unsigned char *dest = skb->data;
  27. struct net_bridge_fdb_entry *dst;
  28. struct net_bridge_mdb_entry *mdst;
  29. struct br_cpu_netstats *brstats = this_cpu_ptr(br->stats);
  30. #ifdef CONFIG_BRIDGE_NETFILTER
  31. if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) {
  32. br_nf_pre_routing_finish_bridge_slow(skb);
  33. return NETDEV_TX_OK;
  34. }
  35. #endif
  36. BR_INPUT_SKB_CB(skb)->brdev = dev;
  37. skb_reset_mac_header(skb);
  38. skb_pull(skb, ETH_HLEN);
  39. u64_stats_update_begin(&brstats->syncp);
  40. brstats->tx_packets++;
  41. /* Exclude ETH_HLEN from byte stats for consistency with Rx chain */
  42. brstats->tx_bytes += skb->len;
  43. u64_stats_update_end(&brstats->syncp);
  44. rcu_read_lock();
  45. if (is_broadcast_ether_addr(dest))
  46. br_flood_deliver(br, skb);
  47. else if (is_multicast_ether_addr(dest)) {
  48. if (unlikely(netpoll_tx_running(dev))) {
  49. br_flood_deliver(br, skb);
  50. goto out;
  51. }
  52. if (br_multicast_rcv(br, NULL, skb)) {
  53. kfree_skb(skb);
  54. goto out;
  55. }
  56. mdst = br_mdb_get(br, skb);
  57. if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb))
  58. br_multicast_deliver(mdst, skb);
  59. else
  60. br_flood_deliver(br, skb);
  61. } else if ((dst = __br_fdb_get(br, dest)) != NULL)
  62. br_deliver(dst->dst, skb);
  63. else
  64. br_flood_deliver(br, skb);
  65. out:
  66. rcu_read_unlock();
  67. return NETDEV_TX_OK;
  68. }
  69. static int br_dev_init(struct net_device *dev)
  70. {
  71. struct net_bridge *br = netdev_priv(dev);
  72. int i;
  73. br->stats = alloc_percpu(struct br_cpu_netstats);
  74. if (!br->stats)
  75. return -ENOMEM;
  76. for_each_possible_cpu(i) {
  77. struct br_cpu_netstats *br_dev_stats;
  78. br_dev_stats = per_cpu_ptr(br->stats, i);
  79. u64_stats_init(&br_dev_stats->syncp);
  80. }
  81. return 0;
  82. }
  83. static int br_dev_open(struct net_device *dev)
  84. {
  85. struct net_bridge *br = netdev_priv(dev);
  86. netdev_update_features(dev);
  87. netif_start_queue(dev);
  88. br_stp_enable_bridge(br);
  89. br_multicast_open(br);
  90. return 0;
  91. }
  92. static void br_dev_set_multicast_list(struct net_device *dev)
  93. {
  94. }
  95. static int br_dev_stop(struct net_device *dev)
  96. {
  97. struct net_bridge *br = netdev_priv(dev);
  98. br_stp_disable_bridge(br);
  99. br_multicast_stop(br);
  100. netif_stop_queue(dev);
  101. return 0;
  102. }
  103. static struct rtnl_link_stats64 *br_get_stats64(struct net_device *dev,
  104. struct rtnl_link_stats64 *stats)
  105. {
  106. struct net_bridge *br = netdev_priv(dev);
  107. struct br_cpu_netstats tmp, sum = { 0 };
  108. unsigned int cpu;
  109. for_each_possible_cpu(cpu) {
  110. unsigned int start;
  111. const struct br_cpu_netstats *bstats
  112. = per_cpu_ptr(br->stats, cpu);
  113. do {
  114. start = u64_stats_fetch_begin(&bstats->syncp);
  115. memcpy(&tmp, bstats, sizeof(tmp));
  116. } while (u64_stats_fetch_retry(&bstats->syncp, start));
  117. sum.tx_bytes += tmp.tx_bytes;
  118. sum.tx_packets += tmp.tx_packets;
  119. sum.rx_bytes += tmp.rx_bytes;
  120. sum.rx_packets += tmp.rx_packets;
  121. }
  122. stats->tx_bytes = sum.tx_bytes;
  123. stats->tx_packets = sum.tx_packets;
  124. stats->rx_bytes = sum.rx_bytes;
  125. stats->rx_packets = sum.rx_packets;
  126. return stats;
  127. }
  128. static int br_change_mtu(struct net_device *dev, int new_mtu)
  129. {
  130. struct net_bridge *br = netdev_priv(dev);
  131. if (new_mtu < 68 || new_mtu > br_min_mtu(br))
  132. return -EINVAL;
  133. dev->mtu = new_mtu;
  134. #ifdef CONFIG_BRIDGE_NETFILTER
  135. /* remember the MTU in the rtable for PMTU */
  136. dst_metric_set(&br->fake_rtable.dst, RTAX_MTU, new_mtu);
  137. #endif
  138. return 0;
  139. }
  140. /* Allow setting mac address to any valid ethernet address. */
  141. static int br_set_mac_address(struct net_device *dev, void *p)
  142. {
  143. struct net_bridge *br = netdev_priv(dev);
  144. struct sockaddr *addr = p;
  145. if (!is_valid_ether_addr(addr->sa_data))
  146. return -EADDRNOTAVAIL;
  147. spin_lock_bh(&br->lock);
  148. if (compare_ether_addr(dev->dev_addr, addr->sa_data)) {
  149. dev->addr_assign_type &= ~NET_ADDR_RANDOM;
  150. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  151. br_fdb_change_mac_address(br, addr->sa_data);
  152. br_stp_change_bridge_id(br, addr->sa_data);
  153. }
  154. br->flags |= BR_SET_MAC_ADDR;
  155. spin_unlock_bh(&br->lock);
  156. return 0;
  157. }
  158. static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  159. {
  160. strcpy(info->driver, "bridge");
  161. strcpy(info->version, BR_VERSION);
  162. strcpy(info->fw_version, "N/A");
  163. strcpy(info->bus_info, "N/A");
  164. }
  165. static netdev_features_t br_fix_features(struct net_device *dev,
  166. netdev_features_t features)
  167. {
  168. struct net_bridge *br = netdev_priv(dev);
  169. return br_features_recompute(br, features);
  170. }
  171. #ifdef CONFIG_NET_POLL_CONTROLLER
  172. static void br_poll_controller(struct net_device *br_dev)
  173. {
  174. }
  175. static void br_netpoll_cleanup(struct net_device *dev)
  176. {
  177. struct net_bridge *br = netdev_priv(dev);
  178. struct net_bridge_port *p, *n;
  179. list_for_each_entry_safe(p, n, &br->port_list, list) {
  180. br_netpoll_disable(p);
  181. }
  182. }
  183. static int br_netpoll_setup(struct net_device *dev, struct netpoll_info *ni)
  184. {
  185. struct net_bridge *br = netdev_priv(dev);
  186. struct net_bridge_port *p, *n;
  187. int err = 0;
  188. list_for_each_entry_safe(p, n, &br->port_list, list) {
  189. if (!p->dev)
  190. continue;
  191. err = br_netpoll_enable(p);
  192. if (err)
  193. goto fail;
  194. }
  195. out:
  196. return err;
  197. fail:
  198. br_netpoll_cleanup(dev);
  199. goto out;
  200. }
  201. int br_netpoll_enable(struct net_bridge_port *p)
  202. {
  203. struct netpoll *np;
  204. int err = 0;
  205. np = kzalloc(sizeof(*p->np), GFP_KERNEL);
  206. err = -ENOMEM;
  207. if (!np)
  208. goto out;
  209. np->dev = p->dev;
  210. strlcpy(np->dev_name, p->dev->name, IFNAMSIZ);
  211. err = __netpoll_setup(np);
  212. if (err) {
  213. kfree(np);
  214. goto out;
  215. }
  216. p->np = np;
  217. out:
  218. return err;
  219. }
  220. void br_netpoll_disable(struct net_bridge_port *p)
  221. {
  222. struct netpoll *np = p->np;
  223. if (!np)
  224. return;
  225. p->np = NULL;
  226. /* Wait for transmitting packets to finish before freeing. */
  227. synchronize_rcu_bh();
  228. __netpoll_cleanup(np);
  229. kfree(np);
  230. }
  231. #endif
  232. static int br_add_slave(struct net_device *dev, struct net_device *slave_dev)
  233. {
  234. struct net_bridge *br = netdev_priv(dev);
  235. return br_add_if(br, slave_dev);
  236. }
  237. static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
  238. {
  239. struct net_bridge *br = netdev_priv(dev);
  240. return br_del_if(br, slave_dev);
  241. }
  242. static const struct ethtool_ops br_ethtool_ops = {
  243. .get_drvinfo = br_getinfo,
  244. .get_link = ethtool_op_get_link,
  245. };
  246. static const struct net_device_ops br_netdev_ops = {
  247. .ndo_open = br_dev_open,
  248. .ndo_stop = br_dev_stop,
  249. .ndo_init = br_dev_init,
  250. .ndo_start_xmit = br_dev_xmit,
  251. .ndo_get_stats64 = br_get_stats64,
  252. .ndo_set_mac_address = br_set_mac_address,
  253. .ndo_set_rx_mode = br_dev_set_multicast_list,
  254. .ndo_change_mtu = br_change_mtu,
  255. .ndo_do_ioctl = br_dev_ioctl,
  256. #ifdef CONFIG_NET_POLL_CONTROLLER
  257. .ndo_netpoll_setup = br_netpoll_setup,
  258. .ndo_netpoll_cleanup = br_netpoll_cleanup,
  259. .ndo_poll_controller = br_poll_controller,
  260. #endif
  261. .ndo_add_slave = br_add_slave,
  262. .ndo_del_slave = br_del_slave,
  263. .ndo_fix_features = br_fix_features,
  264. };
  265. static void br_dev_free(struct net_device *dev)
  266. {
  267. struct net_bridge *br = netdev_priv(dev);
  268. free_percpu(br->stats);
  269. free_netdev(dev);
  270. }
  271. static struct device_type br_type = {
  272. .name = "bridge",
  273. };
  274. void br_dev_setup(struct net_device *dev)
  275. {
  276. struct net_bridge *br = netdev_priv(dev);
  277. eth_hw_addr_random(dev);
  278. ether_setup(dev);
  279. dev->netdev_ops = &br_netdev_ops;
  280. dev->destructor = br_dev_free;
  281. SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
  282. SET_NETDEV_DEVTYPE(dev, &br_type);
  283. dev->tx_queue_len = 0;
  284. dev->priv_flags = IFF_EBRIDGE;
  285. dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
  286. NETIF_F_GSO_MASK | NETIF_F_HW_CSUM | NETIF_F_LLTX |
  287. NETIF_F_NETNS_LOCAL | NETIF_F_HW_VLAN_TX;
  288. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
  289. NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
  290. NETIF_F_HW_VLAN_TX;
  291. br->dev = dev;
  292. spin_lock_init(&br->lock);
  293. INIT_LIST_HEAD(&br->port_list);
  294. spin_lock_init(&br->hash_lock);
  295. br->bridge_id.prio[0] = 0x80;
  296. br->bridge_id.prio[1] = 0x00;
  297. memcpy(br->group_addr, br_group_address, ETH_ALEN);
  298. br->stp_enabled = BR_NO_STP;
  299. br->group_fwd_mask = BR_GROUPFWD_DEFAULT;
  300. br->designated_root = br->bridge_id;
  301. br->bridge_max_age = br->max_age = 20 * HZ;
  302. br->bridge_hello_time = br->hello_time = 2 * HZ;
  303. br->bridge_forward_delay = br->forward_delay = 15 * HZ;
  304. br->ageing_time = 300 * HZ;
  305. br_netfilter_rtable_init(br);
  306. br_stp_timer_init(br);
  307. br_multicast_init(br);
  308. }