br_device.c 4.4 KB

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