br_device.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. brstats->tx_packets++;
  37. brstats->tx_bytes += skb->len;
  38. BR_INPUT_SKB_CB(skb)->brdev = dev;
  39. skb_reset_mac_header(skb);
  40. skb_pull(skb, ETH_HLEN);
  41. rcu_read_lock();
  42. if (is_multicast_ether_addr(dest)) {
  43. if (br_multicast_rcv(br, NULL, skb))
  44. goto out;
  45. mdst = br_mdb_get(br, skb);
  46. if (mdst || BR_INPUT_SKB_CB_MROUTERS_ONLY(skb))
  47. br_multicast_deliver(mdst, skb);
  48. else
  49. br_flood_deliver(br, skb);
  50. } else if ((dst = __br_fdb_get(br, dest)) != NULL)
  51. br_deliver(dst->dst, skb);
  52. else
  53. br_flood_deliver(br, skb);
  54. out:
  55. rcu_read_unlock();
  56. return NETDEV_TX_OK;
  57. }
  58. static int br_dev_open(struct net_device *dev)
  59. {
  60. struct net_bridge *br = netdev_priv(dev);
  61. br_features_recompute(br);
  62. netif_start_queue(dev);
  63. br_stp_enable_bridge(br);
  64. br_multicast_open(br);
  65. return 0;
  66. }
  67. static void br_dev_set_multicast_list(struct net_device *dev)
  68. {
  69. }
  70. static int br_dev_stop(struct net_device *dev)
  71. {
  72. struct net_bridge *br = netdev_priv(dev);
  73. br_stp_disable_bridge(br);
  74. br_multicast_stop(br);
  75. netif_stop_queue(dev);
  76. return 0;
  77. }
  78. static struct net_device_stats *br_get_stats(struct net_device *dev)
  79. {
  80. struct net_bridge *br = netdev_priv(dev);
  81. struct net_device_stats *stats = &dev->stats;
  82. struct br_cpu_netstats sum = { 0 };
  83. unsigned int cpu;
  84. for_each_possible_cpu(cpu) {
  85. const struct br_cpu_netstats *bstats
  86. = per_cpu_ptr(br->stats, cpu);
  87. sum.tx_bytes += bstats->tx_bytes;
  88. sum.tx_packets += bstats->tx_packets;
  89. sum.rx_bytes += bstats->rx_bytes;
  90. sum.rx_packets += bstats->rx_packets;
  91. }
  92. stats->tx_bytes = sum.tx_bytes;
  93. stats->tx_packets = sum.tx_packets;
  94. stats->rx_bytes = sum.rx_bytes;
  95. stats->rx_packets = sum.rx_packets;
  96. return stats;
  97. }
  98. static int br_change_mtu(struct net_device *dev, int new_mtu)
  99. {
  100. struct net_bridge *br = netdev_priv(dev);
  101. if (new_mtu < 68 || new_mtu > br_min_mtu(br))
  102. return -EINVAL;
  103. dev->mtu = new_mtu;
  104. #ifdef CONFIG_BRIDGE_NETFILTER
  105. /* remember the MTU in the rtable for PMTU */
  106. br->fake_rtable.u.dst.metrics[RTAX_MTU - 1] = new_mtu;
  107. #endif
  108. return 0;
  109. }
  110. /* Allow setting mac address to any valid ethernet address. */
  111. static int br_set_mac_address(struct net_device *dev, void *p)
  112. {
  113. struct net_bridge *br = netdev_priv(dev);
  114. struct sockaddr *addr = p;
  115. if (!is_valid_ether_addr(addr->sa_data))
  116. return -EINVAL;
  117. spin_lock_bh(&br->lock);
  118. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  119. br_stp_change_bridge_id(br, addr->sa_data);
  120. br->flags |= BR_SET_MAC_ADDR;
  121. spin_unlock_bh(&br->lock);
  122. return 0;
  123. }
  124. static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  125. {
  126. strcpy(info->driver, "bridge");
  127. strcpy(info->version, BR_VERSION);
  128. strcpy(info->fw_version, "N/A");
  129. strcpy(info->bus_info, "N/A");
  130. }
  131. static int br_set_sg(struct net_device *dev, u32 data)
  132. {
  133. struct net_bridge *br = netdev_priv(dev);
  134. if (data)
  135. br->feature_mask |= NETIF_F_SG;
  136. else
  137. br->feature_mask &= ~NETIF_F_SG;
  138. br_features_recompute(br);
  139. return 0;
  140. }
  141. static int br_set_tso(struct net_device *dev, u32 data)
  142. {
  143. struct net_bridge *br = netdev_priv(dev);
  144. if (data)
  145. br->feature_mask |= NETIF_F_TSO;
  146. else
  147. br->feature_mask &= ~NETIF_F_TSO;
  148. br_features_recompute(br);
  149. return 0;
  150. }
  151. static int br_set_tx_csum(struct net_device *dev, u32 data)
  152. {
  153. struct net_bridge *br = netdev_priv(dev);
  154. if (data)
  155. br->feature_mask |= NETIF_F_NO_CSUM;
  156. else
  157. br->feature_mask &= ~NETIF_F_ALL_CSUM;
  158. br_features_recompute(br);
  159. return 0;
  160. }
  161. #ifdef CONFIG_NET_POLL_CONTROLLER
  162. static bool br_devices_support_netpoll(struct net_bridge *br)
  163. {
  164. struct net_bridge_port *p;
  165. bool ret = true;
  166. int count = 0;
  167. unsigned long flags;
  168. spin_lock_irqsave(&br->lock, flags);
  169. list_for_each_entry(p, &br->port_list, list) {
  170. count++;
  171. if ((p->dev->priv_flags & IFF_DISABLE_NETPOLL) ||
  172. !p->dev->netdev_ops->ndo_poll_controller)
  173. ret = false;
  174. }
  175. spin_unlock_irqrestore(&br->lock, flags);
  176. return count != 0 && ret;
  177. }
  178. void br_netpoll_cleanup(struct net_device *dev)
  179. {
  180. struct net_bridge *br = netdev_priv(dev);
  181. struct net_bridge_port *p, *n;
  182. const struct net_device_ops *ops;
  183. br->dev->npinfo = NULL;
  184. list_for_each_entry_safe(p, n, &br->port_list, list) {
  185. if (p->dev) {
  186. ops = p->dev->netdev_ops;
  187. if (ops->ndo_netpoll_cleanup)
  188. ops->ndo_netpoll_cleanup(p->dev);
  189. else
  190. p->dev->npinfo = NULL;
  191. }
  192. }
  193. }
  194. void br_netpoll_disable(struct net_bridge *br,
  195. struct net_device *dev)
  196. {
  197. if (br_devices_support_netpoll(br))
  198. br->dev->priv_flags &= ~IFF_DISABLE_NETPOLL;
  199. if (dev->netdev_ops->ndo_netpoll_cleanup)
  200. dev->netdev_ops->ndo_netpoll_cleanup(dev);
  201. else
  202. dev->npinfo = NULL;
  203. }
  204. void br_netpoll_enable(struct net_bridge *br,
  205. struct net_device *dev)
  206. {
  207. if (br_devices_support_netpoll(br)) {
  208. br->dev->priv_flags &= ~IFF_DISABLE_NETPOLL;
  209. if (br->dev->npinfo)
  210. dev->npinfo = br->dev->npinfo;
  211. } else if (!(br->dev->priv_flags & IFF_DISABLE_NETPOLL)) {
  212. br->dev->priv_flags |= IFF_DISABLE_NETPOLL;
  213. br_info(br,"new device %s does not support netpoll (disabling)",
  214. dev->name);
  215. }
  216. }
  217. #endif
  218. static const struct ethtool_ops br_ethtool_ops = {
  219. .get_drvinfo = br_getinfo,
  220. .get_link = ethtool_op_get_link,
  221. .get_tx_csum = ethtool_op_get_tx_csum,
  222. .set_tx_csum = br_set_tx_csum,
  223. .get_sg = ethtool_op_get_sg,
  224. .set_sg = br_set_sg,
  225. .get_tso = ethtool_op_get_tso,
  226. .set_tso = br_set_tso,
  227. .get_ufo = ethtool_op_get_ufo,
  228. .set_ufo = ethtool_op_set_ufo,
  229. .get_flags = ethtool_op_get_flags,
  230. };
  231. static const struct net_device_ops br_netdev_ops = {
  232. .ndo_open = br_dev_open,
  233. .ndo_stop = br_dev_stop,
  234. .ndo_start_xmit = br_dev_xmit,
  235. .ndo_get_stats = br_get_stats,
  236. .ndo_set_mac_address = br_set_mac_address,
  237. .ndo_set_multicast_list = br_dev_set_multicast_list,
  238. .ndo_change_mtu = br_change_mtu,
  239. .ndo_do_ioctl = br_dev_ioctl,
  240. #ifdef CONFIG_NET_POLL_CONTROLLER
  241. .ndo_netpoll_cleanup = br_netpoll_cleanup,
  242. #endif
  243. };
  244. static void br_dev_free(struct net_device *dev)
  245. {
  246. struct net_bridge *br = netdev_priv(dev);
  247. free_percpu(br->stats);
  248. free_netdev(dev);
  249. }
  250. void br_dev_setup(struct net_device *dev)
  251. {
  252. random_ether_addr(dev->dev_addr);
  253. ether_setup(dev);
  254. dev->netdev_ops = &br_netdev_ops;
  255. dev->destructor = br_dev_free;
  256. SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
  257. dev->tx_queue_len = 0;
  258. dev->priv_flags = IFF_EBRIDGE;
  259. dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
  260. NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX |
  261. NETIF_F_NETNS_LOCAL | NETIF_F_GSO;
  262. }