br_device.c 7.3 KB

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