br_if.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Userspace interface
  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/netpoll.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/if_arp.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/if_ether.h>
  23. #include <linux/slab.h>
  24. #include <net/sock.h>
  25. #include "br_private.h"
  26. /*
  27. * Determine initial path cost based on speed.
  28. * using recommendations from 802.1d standard
  29. *
  30. * Since driver might sleep need to not be holding any locks.
  31. */
  32. static int port_cost(struct net_device *dev)
  33. {
  34. struct ethtool_cmd ecmd;
  35. if (!__ethtool_get_settings(dev, &ecmd)) {
  36. switch (ethtool_cmd_speed(&ecmd)) {
  37. case SPEED_10000:
  38. return 2;
  39. case SPEED_1000:
  40. return 4;
  41. case SPEED_100:
  42. return 19;
  43. case SPEED_10:
  44. return 100;
  45. }
  46. }
  47. /* Old silly heuristics based on name */
  48. if (!strncmp(dev->name, "lec", 3))
  49. return 7;
  50. if (!strncmp(dev->name, "plip", 4))
  51. return 2500;
  52. return 100; /* assume old 10Mbps */
  53. }
  54. /* Check for port carrier transistions. */
  55. void br_port_carrier_check(struct net_bridge_port *p)
  56. {
  57. struct net_device *dev = p->dev;
  58. struct net_bridge *br = p->br;
  59. if (netif_running(dev) && netif_carrier_ok(dev))
  60. p->path_cost = port_cost(dev);
  61. if (!netif_running(br->dev))
  62. return;
  63. spin_lock_bh(&br->lock);
  64. if (netif_running(dev) && netif_carrier_ok(dev)) {
  65. if (p->state == BR_STATE_DISABLED)
  66. br_stp_enable_port(p);
  67. } else {
  68. if (p->state != BR_STATE_DISABLED)
  69. br_stp_disable_port(p);
  70. }
  71. spin_unlock_bh(&br->lock);
  72. }
  73. static void release_nbp(struct kobject *kobj)
  74. {
  75. struct net_bridge_port *p
  76. = container_of(kobj, struct net_bridge_port, kobj);
  77. kfree(p);
  78. }
  79. static struct kobj_type brport_ktype = {
  80. #ifdef CONFIG_SYSFS
  81. .sysfs_ops = &brport_sysfs_ops,
  82. #endif
  83. .release = release_nbp,
  84. };
  85. static void destroy_nbp(struct net_bridge_port *p)
  86. {
  87. struct net_device *dev = p->dev;
  88. p->br = NULL;
  89. p->dev = NULL;
  90. dev_put(dev);
  91. kobject_put(&p->kobj);
  92. }
  93. static void destroy_nbp_rcu(struct rcu_head *head)
  94. {
  95. struct net_bridge_port *p =
  96. container_of(head, struct net_bridge_port, rcu);
  97. destroy_nbp(p);
  98. }
  99. /* Delete port(interface) from bridge is done in two steps.
  100. * via RCU. First step, marks device as down. That deletes
  101. * all the timers and stops new packets from flowing through.
  102. *
  103. * Final cleanup doesn't occur until after all CPU's finished
  104. * processing packets.
  105. *
  106. * Protected from multiple admin operations by RTNL mutex
  107. */
  108. static void del_nbp(struct net_bridge_port *p)
  109. {
  110. struct net_bridge *br = p->br;
  111. struct net_device *dev = p->dev;
  112. sysfs_remove_link(br->ifobj, p->dev->name);
  113. dev_set_promiscuity(dev, -1);
  114. spin_lock_bh(&br->lock);
  115. br_stp_disable_port(p);
  116. spin_unlock_bh(&br->lock);
  117. br_ifinfo_notify(RTM_DELLINK, p);
  118. br_fdb_delete_by_port(br, p, 1);
  119. list_del_rcu(&p->list);
  120. dev->priv_flags &= ~IFF_BRIDGE_PORT;
  121. netdev_rx_handler_unregister(dev);
  122. synchronize_net();
  123. netdev_set_master(dev, NULL);
  124. br_multicast_del_port(p);
  125. kobject_uevent(&p->kobj, KOBJ_REMOVE);
  126. kobject_del(&p->kobj);
  127. br_netpoll_disable(p);
  128. call_rcu(&p->rcu, destroy_nbp_rcu);
  129. }
  130. /* Delete bridge device */
  131. void br_dev_delete(struct net_device *dev, struct list_head *head)
  132. {
  133. struct net_bridge *br = netdev_priv(dev);
  134. struct net_bridge_port *p, *n;
  135. list_for_each_entry_safe(p, n, &br->port_list, list) {
  136. del_nbp(p);
  137. }
  138. br_fdb_delete_by_port(br, NULL, 1);
  139. del_timer_sync(&br->gc_timer);
  140. br_sysfs_delbr(br->dev);
  141. unregister_netdevice_queue(br->dev, head);
  142. }
  143. /* find an available port number */
  144. static int find_portno(struct net_bridge *br)
  145. {
  146. int index;
  147. struct net_bridge_port *p;
  148. unsigned long *inuse;
  149. inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
  150. GFP_KERNEL);
  151. if (!inuse)
  152. return -ENOMEM;
  153. set_bit(0, inuse); /* zero is reserved */
  154. list_for_each_entry(p, &br->port_list, list) {
  155. set_bit(p->port_no, inuse);
  156. }
  157. index = find_first_zero_bit(inuse, BR_MAX_PORTS);
  158. kfree(inuse);
  159. return (index >= BR_MAX_PORTS) ? -EXFULL : index;
  160. }
  161. /* called with RTNL but without bridge lock */
  162. static struct net_bridge_port *new_nbp(struct net_bridge *br,
  163. struct net_device *dev)
  164. {
  165. int index;
  166. struct net_bridge_port *p;
  167. index = find_portno(br);
  168. if (index < 0)
  169. return ERR_PTR(index);
  170. p = kzalloc(sizeof(*p), GFP_KERNEL);
  171. if (p == NULL)
  172. return ERR_PTR(-ENOMEM);
  173. p->br = br;
  174. dev_hold(dev);
  175. p->dev = dev;
  176. p->path_cost = port_cost(dev);
  177. p->priority = 0x8000 >> BR_PORT_BITS;
  178. p->port_no = index;
  179. p->flags = 0;
  180. br_init_port(p);
  181. p->state = BR_STATE_DISABLED;
  182. br_stp_port_timer_init(p);
  183. br_multicast_add_port(p);
  184. return p;
  185. }
  186. int br_add_bridge(struct net *net, const char *name)
  187. {
  188. struct net_device *dev;
  189. int res;
  190. dev = alloc_netdev(sizeof(struct net_bridge), name,
  191. br_dev_setup);
  192. if (!dev)
  193. return -ENOMEM;
  194. dev_net_set(dev, net);
  195. dev->rtnl_link_ops = &br_link_ops;
  196. res = register_netdev(dev);
  197. if (res)
  198. free_netdev(dev);
  199. return res;
  200. }
  201. int br_del_bridge(struct net *net, const char *name)
  202. {
  203. struct net_device *dev;
  204. int ret = 0;
  205. rtnl_lock();
  206. dev = __dev_get_by_name(net, name);
  207. if (dev == NULL)
  208. ret = -ENXIO; /* Could not find device */
  209. else if (!(dev->priv_flags & IFF_EBRIDGE)) {
  210. /* Attempt to delete non bridge device! */
  211. ret = -EPERM;
  212. }
  213. else if (dev->flags & IFF_UP) {
  214. /* Not shutdown yet. */
  215. ret = -EBUSY;
  216. }
  217. else
  218. br_dev_delete(dev, NULL);
  219. rtnl_unlock();
  220. return ret;
  221. }
  222. /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
  223. int br_min_mtu(const struct net_bridge *br)
  224. {
  225. const struct net_bridge_port *p;
  226. int mtu = 0;
  227. ASSERT_RTNL();
  228. if (list_empty(&br->port_list))
  229. mtu = ETH_DATA_LEN;
  230. else {
  231. list_for_each_entry(p, &br->port_list, list) {
  232. if (!mtu || p->dev->mtu < mtu)
  233. mtu = p->dev->mtu;
  234. }
  235. }
  236. return mtu;
  237. }
  238. /*
  239. * Recomputes features using slave's features
  240. */
  241. netdev_features_t br_features_recompute(struct net_bridge *br,
  242. netdev_features_t features)
  243. {
  244. struct net_bridge_port *p;
  245. netdev_features_t mask;
  246. if (list_empty(&br->port_list))
  247. return features;
  248. mask = features;
  249. features &= ~NETIF_F_ONE_FOR_ALL;
  250. list_for_each_entry(p, &br->port_list, list) {
  251. features = netdev_increment_features(features,
  252. p->dev->features, mask);
  253. }
  254. return features;
  255. }
  256. /* called with RTNL */
  257. int br_add_if(struct net_bridge *br, struct net_device *dev)
  258. {
  259. struct net_bridge_port *p;
  260. int err = 0;
  261. bool changed_addr;
  262. /* Don't allow bridging non-ethernet like devices */
  263. if ((dev->flags & IFF_LOOPBACK) ||
  264. dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
  265. !is_valid_ether_addr(dev->dev_addr))
  266. return -EINVAL;
  267. /* No bridging of bridges */
  268. if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit)
  269. return -ELOOP;
  270. /* Device is already being bridged */
  271. if (br_port_exists(dev))
  272. return -EBUSY;
  273. /* No bridging devices that dislike that (e.g. wireless) */
  274. if (dev->priv_flags & IFF_DONT_BRIDGE)
  275. return -EOPNOTSUPP;
  276. p = new_nbp(br, dev);
  277. if (IS_ERR(p))
  278. return PTR_ERR(p);
  279. call_netdevice_notifiers(NETDEV_JOIN, dev);
  280. err = dev_set_promiscuity(dev, 1);
  281. if (err)
  282. goto put_back;
  283. err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
  284. SYSFS_BRIDGE_PORT_ATTR);
  285. if (err)
  286. goto err1;
  287. err = br_sysfs_addif(p);
  288. if (err)
  289. goto err2;
  290. if (br_netpoll_info(br) && ((err = br_netpoll_enable(p))))
  291. goto err3;
  292. err = netdev_set_master(dev, br->dev);
  293. if (err)
  294. goto err3;
  295. err = netdev_rx_handler_register(dev, br_handle_frame, p);
  296. if (err)
  297. goto err4;
  298. dev->priv_flags |= IFF_BRIDGE_PORT;
  299. dev_disable_lro(dev);
  300. list_add_rcu(&p->list, &br->port_list);
  301. netdev_update_features(br->dev);
  302. spin_lock_bh(&br->lock);
  303. changed_addr = br_stp_recalculate_bridge_id(br);
  304. if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
  305. (br->dev->flags & IFF_UP))
  306. br_stp_enable_port(p);
  307. spin_unlock_bh(&br->lock);
  308. br_ifinfo_notify(RTM_NEWLINK, p);
  309. if (changed_addr)
  310. call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
  311. dev_set_mtu(br->dev, br_min_mtu(br));
  312. if (br_fdb_insert(br, p, dev->dev_addr))
  313. netdev_err(dev, "failed insert local address bridge forwarding table\n");
  314. kobject_uevent(&p->kobj, KOBJ_ADD);
  315. return 0;
  316. err4:
  317. netdev_set_master(dev, NULL);
  318. err3:
  319. sysfs_remove_link(br->ifobj, p->dev->name);
  320. err2:
  321. kobject_put(&p->kobj);
  322. p = NULL; /* kobject_put frees */
  323. err1:
  324. dev_set_promiscuity(dev, -1);
  325. put_back:
  326. dev_put(dev);
  327. kfree(p);
  328. return err;
  329. }
  330. /* called with RTNL */
  331. int br_del_if(struct net_bridge *br, struct net_device *dev)
  332. {
  333. struct net_bridge_port *p;
  334. bool changed_addr;
  335. p = br_port_get_rtnl(dev);
  336. if (!p || p->br != br)
  337. return -EINVAL;
  338. del_nbp(p);
  339. spin_lock_bh(&br->lock);
  340. changed_addr = br_stp_recalculate_bridge_id(br);
  341. spin_unlock_bh(&br->lock);
  342. if (changed_addr)
  343. call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
  344. netdev_update_features(br->dev);
  345. return 0;
  346. }
  347. void __net_exit br_net_exit(struct net *net)
  348. {
  349. struct net_device *dev;
  350. LIST_HEAD(list);
  351. rtnl_lock();
  352. for_each_netdev(net, dev)
  353. if (dev->priv_flags & IFF_EBRIDGE)
  354. br_dev_delete(dev, &list);
  355. unregister_netdevice_many(&list);
  356. rtnl_unlock();
  357. }