br_device.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/etherdevice.h>
  16. #include <linux/ethtool.h>
  17. #include <asm/uaccess.h>
  18. #include "br_private.h"
  19. /* net device transmit always called with no BH (preempt_disabled) */
  20. int br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  21. {
  22. struct net_bridge *br = netdev_priv(dev);
  23. const unsigned char *dest = skb->data;
  24. struct net_bridge_fdb_entry *dst;
  25. dev->stats.tx_packets++;
  26. dev->stats.tx_bytes += skb->len;
  27. skb_reset_mac_header(skb);
  28. skb_pull(skb, ETH_HLEN);
  29. if (dest[0] & 1)
  30. br_flood_deliver(br, skb);
  31. else if ((dst = __br_fdb_get(br, dest)) != NULL)
  32. br_deliver(dst->dst, skb);
  33. else
  34. br_flood_deliver(br, skb);
  35. return 0;
  36. }
  37. static int br_dev_open(struct net_device *dev)
  38. {
  39. struct net_bridge *br = netdev_priv(dev);
  40. br_features_recompute(br);
  41. netif_start_queue(dev);
  42. br_stp_enable_bridge(br);
  43. return 0;
  44. }
  45. static void br_dev_set_multicast_list(struct net_device *dev)
  46. {
  47. }
  48. static int br_dev_stop(struct net_device *dev)
  49. {
  50. br_stp_disable_bridge(netdev_priv(dev));
  51. netif_stop_queue(dev);
  52. return 0;
  53. }
  54. static int br_change_mtu(struct net_device *dev, int new_mtu)
  55. {
  56. struct net_bridge *br = netdev_priv(dev);
  57. if (new_mtu < 68 || new_mtu > br_min_mtu(br))
  58. return -EINVAL;
  59. dev->mtu = new_mtu;
  60. #ifdef CONFIG_BRIDGE_NETFILTER
  61. /* remember the MTU in the rtable for PMTU */
  62. br->fake_rtable.u.dst.metrics[RTAX_MTU - 1] = new_mtu;
  63. #endif
  64. return 0;
  65. }
  66. /* Allow setting mac address to any valid ethernet address. */
  67. static int br_set_mac_address(struct net_device *dev, void *p)
  68. {
  69. struct net_bridge *br = netdev_priv(dev);
  70. struct sockaddr *addr = p;
  71. if (!is_valid_ether_addr(addr->sa_data))
  72. return -EINVAL;
  73. spin_lock_bh(&br->lock);
  74. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  75. br_stp_change_bridge_id(br, addr->sa_data);
  76. br->flags |= BR_SET_MAC_ADDR;
  77. spin_unlock_bh(&br->lock);
  78. return 0;
  79. }
  80. static void br_getinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  81. {
  82. strcpy(info->driver, "bridge");
  83. strcpy(info->version, BR_VERSION);
  84. strcpy(info->fw_version, "N/A");
  85. strcpy(info->bus_info, "N/A");
  86. }
  87. static int br_set_sg(struct net_device *dev, u32 data)
  88. {
  89. struct net_bridge *br = netdev_priv(dev);
  90. if (data)
  91. br->feature_mask |= NETIF_F_SG;
  92. else
  93. br->feature_mask &= ~NETIF_F_SG;
  94. br_features_recompute(br);
  95. return 0;
  96. }
  97. static int br_set_tso(struct net_device *dev, u32 data)
  98. {
  99. struct net_bridge *br = netdev_priv(dev);
  100. if (data)
  101. br->feature_mask |= NETIF_F_TSO;
  102. else
  103. br->feature_mask &= ~NETIF_F_TSO;
  104. br_features_recompute(br);
  105. return 0;
  106. }
  107. static int br_set_tx_csum(struct net_device *dev, u32 data)
  108. {
  109. struct net_bridge *br = netdev_priv(dev);
  110. if (data)
  111. br->feature_mask |= NETIF_F_NO_CSUM;
  112. else
  113. br->feature_mask &= ~NETIF_F_ALL_CSUM;
  114. br_features_recompute(br);
  115. return 0;
  116. }
  117. static const struct ethtool_ops br_ethtool_ops = {
  118. .get_drvinfo = br_getinfo,
  119. .get_link = ethtool_op_get_link,
  120. .get_tx_csum = ethtool_op_get_tx_csum,
  121. .set_tx_csum = br_set_tx_csum,
  122. .get_sg = ethtool_op_get_sg,
  123. .set_sg = br_set_sg,
  124. .get_tso = ethtool_op_get_tso,
  125. .set_tso = br_set_tso,
  126. .get_ufo = ethtool_op_get_ufo,
  127. .get_flags = ethtool_op_get_flags,
  128. };
  129. static const struct net_device_ops br_netdev_ops = {
  130. .ndo_open = br_dev_open,
  131. .ndo_stop = br_dev_stop,
  132. .ndo_start_xmit = br_dev_xmit,
  133. .ndo_set_mac_address = br_set_mac_address,
  134. .ndo_set_multicast_list = br_dev_set_multicast_list,
  135. .ndo_change_mtu = br_change_mtu,
  136. .ndo_do_ioctl = br_dev_ioctl,
  137. };
  138. void br_dev_setup(struct net_device *dev)
  139. {
  140. random_ether_addr(dev->dev_addr);
  141. ether_setup(dev);
  142. dev->netdev_ops = &br_netdev_ops;
  143. dev->destructor = free_netdev;
  144. SET_ETHTOOL_OPS(dev, &br_ethtool_ops);
  145. dev->tx_queue_len = 0;
  146. dev->priv_flags = IFF_EBRIDGE;
  147. dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
  148. NETIF_F_GSO_MASK | NETIF_F_NO_CSUM | NETIF_F_LLTX |
  149. NETIF_F_NETNS_LOCAL | NETIF_F_GSO;
  150. }