br_if.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 <linux/if_vlan.h>
  26. #include "br_private.h"
  27. /*
  28. * Determine initial path cost based on speed.
  29. * using recommendations from 802.1d standard
  30. *
  31. * Since driver might sleep need to not be holding any locks.
  32. */
  33. static int port_cost(struct net_device *dev)
  34. {
  35. struct ethtool_cmd ecmd;
  36. if (!__ethtool_get_settings(dev, &ecmd)) {
  37. switch (ethtool_cmd_speed(&ecmd)) {
  38. case SPEED_10000:
  39. return 2;
  40. case SPEED_1000:
  41. return 4;
  42. case SPEED_100:
  43. return 19;
  44. case SPEED_10:
  45. return 100;
  46. }
  47. }
  48. /* Old silly heuristics based on name */
  49. if (!strncmp(dev->name, "lec", 3))
  50. return 7;
  51. if (!strncmp(dev->name, "plip", 4))
  52. return 2500;
  53. return 100; /* assume old 10Mbps */
  54. }
  55. /* Check for port carrier transitions. */
  56. void br_port_carrier_check(struct net_bridge_port *p)
  57. {
  58. struct net_device *dev = p->dev;
  59. struct net_bridge *br = p->br;
  60. if (!(p->flags & BR_ADMIN_COST) &&
  61. netif_running(dev) && netif_oper_up(dev))
  62. p->path_cost = port_cost(dev);
  63. if (!netif_running(br->dev))
  64. return;
  65. spin_lock_bh(&br->lock);
  66. if (netif_running(dev) && netif_oper_up(dev)) {
  67. if (p->state == BR_STATE_DISABLED)
  68. br_stp_enable_port(p);
  69. } else {
  70. if (p->state != BR_STATE_DISABLED)
  71. br_stp_disable_port(p);
  72. }
  73. spin_unlock_bh(&br->lock);
  74. }
  75. static void br_port_set_promisc(struct net_bridge_port *p)
  76. {
  77. int err = 0;
  78. if (br_promisc_port(p))
  79. return;
  80. err = dev_set_promiscuity(p->dev, 1);
  81. if (err)
  82. return;
  83. br_fdb_unsync_static(p->br, p);
  84. p->flags |= BR_PROMISC;
  85. }
  86. static void br_port_clear_promisc(struct net_bridge_port *p)
  87. {
  88. int err;
  89. /* Check if the port is already non-promisc or if it doesn't
  90. * support UNICAST filtering. Without unicast filtering support
  91. * we'll end up re-enabling promisc mode anyway, so just check for
  92. * it here.
  93. */
  94. if (!br_promisc_port(p) || !(p->dev->priv_flags & IFF_UNICAST_FLT))
  95. return;
  96. /* Since we'll be clearing the promisc mode, program the port
  97. * first so that we don't have interruption in traffic.
  98. */
  99. err = br_fdb_sync_static(p->br, p);
  100. if (err)
  101. return;
  102. dev_set_promiscuity(p->dev, -1);
  103. p->flags &= ~BR_PROMISC;
  104. }
  105. /* When a port is added or removed or when certain port flags
  106. * change, this function is called to automatically manage
  107. * promiscuity setting of all the bridge ports. We are always called
  108. * under RTNL so can skip using rcu primitives.
  109. */
  110. void br_manage_promisc(struct net_bridge *br)
  111. {
  112. struct net_bridge_port *p;
  113. bool set_all = false;
  114. /* If vlan filtering is disabled or bridge interface is placed
  115. * into promiscuous mode, place all ports in promiscuous mode.
  116. */
  117. if ((br->dev->flags & IFF_PROMISC) || !br_vlan_enabled(br))
  118. set_all = true;
  119. list_for_each_entry(p, &br->port_list, list) {
  120. if (set_all) {
  121. br_port_set_promisc(p);
  122. } else {
  123. /* If the number of auto-ports is <= 1, then all other
  124. * ports will have their output configuration
  125. * statically specified through fdbs. Since ingress
  126. * on the auto-port becomes forwarding/egress to other
  127. * ports and egress configuration is statically known,
  128. * we can say that ingress configuration of the
  129. * auto-port is also statically known.
  130. * This lets us disable promiscuous mode and write
  131. * this config to hw.
  132. */
  133. if (br->auto_cnt == 0 ||
  134. (br->auto_cnt == 1 && br_auto_port(p)))
  135. br_port_clear_promisc(p);
  136. else
  137. br_port_set_promisc(p);
  138. }
  139. }
  140. }
  141. static void nbp_update_port_count(struct net_bridge *br)
  142. {
  143. struct net_bridge_port *p;
  144. u32 cnt = 0;
  145. list_for_each_entry(p, &br->port_list, list) {
  146. if (br_auto_port(p))
  147. cnt++;
  148. }
  149. if (br->auto_cnt != cnt) {
  150. br->auto_cnt = cnt;
  151. br_manage_promisc(br);
  152. }
  153. }
  154. static void nbp_delete_promisc(struct net_bridge_port *p)
  155. {
  156. /* If port is currently promiscuous, unset promiscuity.
  157. * Otherwise, it is a static port so remove all addresses
  158. * from it.
  159. */
  160. dev_set_allmulti(p->dev, -1);
  161. if (br_promisc_port(p))
  162. dev_set_promiscuity(p->dev, -1);
  163. else
  164. br_fdb_unsync_static(p->br, p);
  165. }
  166. static void release_nbp(struct kobject *kobj)
  167. {
  168. struct net_bridge_port *p
  169. = container_of(kobj, struct net_bridge_port, kobj);
  170. kfree(p);
  171. }
  172. static struct kobj_type brport_ktype = {
  173. #ifdef CONFIG_SYSFS
  174. .sysfs_ops = &brport_sysfs_ops,
  175. #endif
  176. .release = release_nbp,
  177. };
  178. static void destroy_nbp(struct net_bridge_port *p)
  179. {
  180. struct net_device *dev = p->dev;
  181. p->br = NULL;
  182. p->dev = NULL;
  183. dev_put(dev);
  184. kobject_put(&p->kobj);
  185. }
  186. static void destroy_nbp_rcu(struct rcu_head *head)
  187. {
  188. struct net_bridge_port *p =
  189. container_of(head, struct net_bridge_port, rcu);
  190. destroy_nbp(p);
  191. }
  192. /* Delete port(interface) from bridge is done in two steps.
  193. * via RCU. First step, marks device as down. That deletes
  194. * all the timers and stops new packets from flowing through.
  195. *
  196. * Final cleanup doesn't occur until after all CPU's finished
  197. * processing packets.
  198. *
  199. * Protected from multiple admin operations by RTNL mutex
  200. */
  201. static void del_nbp(struct net_bridge_port *p)
  202. {
  203. struct net_bridge *br = p->br;
  204. struct net_device *dev = p->dev;
  205. sysfs_remove_link(br->ifobj, p->dev->name);
  206. nbp_delete_promisc(p);
  207. spin_lock_bh(&br->lock);
  208. br_stp_disable_port(p);
  209. spin_unlock_bh(&br->lock);
  210. br_ifinfo_notify(RTM_DELLINK, p);
  211. list_del_rcu(&p->list);
  212. nbp_vlan_flush(p);
  213. br_fdb_delete_by_port(br, p, 1);
  214. nbp_update_port_count(br);
  215. netdev_upper_dev_unlink(dev, br->dev);
  216. dev->priv_flags &= ~IFF_BRIDGE_PORT;
  217. netdev_rx_handler_unregister(dev);
  218. br_multicast_del_port(p);
  219. kobject_uevent(&p->kobj, KOBJ_REMOVE);
  220. kobject_del(&p->kobj);
  221. br_netpoll_disable(p);
  222. call_rcu(&p->rcu, destroy_nbp_rcu);
  223. }
  224. /* Delete bridge device */
  225. void br_dev_delete(struct net_device *dev, struct list_head *head)
  226. {
  227. struct net_bridge *br = netdev_priv(dev);
  228. struct net_bridge_port *p, *n;
  229. list_for_each_entry_safe(p, n, &br->port_list, list) {
  230. del_nbp(p);
  231. }
  232. br_fdb_delete_by_port(br, NULL, 1);
  233. br_vlan_flush(br);
  234. del_timer_sync(&br->gc_timer);
  235. br_sysfs_delbr(br->dev);
  236. unregister_netdevice_queue(br->dev, head);
  237. }
  238. /* find an available port number */
  239. static int find_portno(struct net_bridge *br)
  240. {
  241. int index;
  242. struct net_bridge_port *p;
  243. unsigned long *inuse;
  244. inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
  245. GFP_KERNEL);
  246. if (!inuse)
  247. return -ENOMEM;
  248. set_bit(0, inuse); /* zero is reserved */
  249. list_for_each_entry(p, &br->port_list, list) {
  250. set_bit(p->port_no, inuse);
  251. }
  252. index = find_first_zero_bit(inuse, BR_MAX_PORTS);
  253. kfree(inuse);
  254. return (index >= BR_MAX_PORTS) ? -EXFULL : index;
  255. }
  256. /* called with RTNL but without bridge lock */
  257. static struct net_bridge_port *new_nbp(struct net_bridge *br,
  258. struct net_device *dev)
  259. {
  260. int index;
  261. struct net_bridge_port *p;
  262. index = find_portno(br);
  263. if (index < 0)
  264. return ERR_PTR(index);
  265. p = kzalloc(sizeof(*p), GFP_KERNEL);
  266. if (p == NULL)
  267. return ERR_PTR(-ENOMEM);
  268. p->br = br;
  269. dev_hold(dev);
  270. p->dev = dev;
  271. p->path_cost = port_cost(dev);
  272. p->priority = 0x8000 >> BR_PORT_BITS;
  273. p->port_no = index;
  274. p->flags = BR_LEARNING | BR_FLOOD;
  275. br_init_port(p);
  276. br_set_state(p, BR_STATE_DISABLED);
  277. br_stp_port_timer_init(p);
  278. br_multicast_add_port(p);
  279. return p;
  280. }
  281. int br_add_bridge(struct net *net, const char *name)
  282. {
  283. struct net_device *dev;
  284. int res;
  285. dev = alloc_netdev(sizeof(struct net_bridge), name, NET_NAME_UNKNOWN,
  286. br_dev_setup);
  287. if (!dev)
  288. return -ENOMEM;
  289. dev_net_set(dev, net);
  290. dev->rtnl_link_ops = &br_link_ops;
  291. res = register_netdev(dev);
  292. if (res)
  293. free_netdev(dev);
  294. return res;
  295. }
  296. int br_del_bridge(struct net *net, const char *name)
  297. {
  298. struct net_device *dev;
  299. int ret = 0;
  300. rtnl_lock();
  301. dev = __dev_get_by_name(net, name);
  302. if (dev == NULL)
  303. ret = -ENXIO; /* Could not find device */
  304. else if (!(dev->priv_flags & IFF_EBRIDGE)) {
  305. /* Attempt to delete non bridge device! */
  306. ret = -EPERM;
  307. }
  308. else if (dev->flags & IFF_UP) {
  309. /* Not shutdown yet. */
  310. ret = -EBUSY;
  311. }
  312. else
  313. br_dev_delete(dev, NULL);
  314. rtnl_unlock();
  315. return ret;
  316. }
  317. /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
  318. int br_min_mtu(const struct net_bridge *br)
  319. {
  320. const struct net_bridge_port *p;
  321. int mtu = 0;
  322. ASSERT_RTNL();
  323. if (list_empty(&br->port_list))
  324. mtu = ETH_DATA_LEN;
  325. else {
  326. list_for_each_entry(p, &br->port_list, list) {
  327. if (!mtu || p->dev->mtu < mtu)
  328. mtu = p->dev->mtu;
  329. }
  330. }
  331. return mtu;
  332. }
  333. /*
  334. * Recomputes features using slave's features
  335. */
  336. netdev_features_t br_features_recompute(struct net_bridge *br,
  337. netdev_features_t features)
  338. {
  339. struct net_bridge_port *p;
  340. netdev_features_t mask;
  341. if (list_empty(&br->port_list))
  342. return features;
  343. mask = features;
  344. features &= ~NETIF_F_ONE_FOR_ALL;
  345. list_for_each_entry(p, &br->port_list, list) {
  346. features = netdev_increment_features(features,
  347. p->dev->features, mask);
  348. }
  349. return features;
  350. }
  351. /* called with RTNL */
  352. int br_add_if(struct net_bridge *br, struct net_device *dev)
  353. {
  354. struct net_bridge_port *p;
  355. int err = 0;
  356. bool changed_addr;
  357. /* Don't allow bridging non-ethernet like devices */
  358. if ((dev->flags & IFF_LOOPBACK) ||
  359. dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
  360. !is_valid_ether_addr(dev->dev_addr))
  361. return -EINVAL;
  362. /* No bridging of bridges */
  363. if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit)
  364. return -ELOOP;
  365. /* Device is already being bridged */
  366. if (br_port_exists(dev))
  367. return -EBUSY;
  368. /* No bridging devices that dislike that (e.g. wireless) */
  369. if (dev->priv_flags & IFF_DONT_BRIDGE)
  370. return -EOPNOTSUPP;
  371. p = new_nbp(br, dev);
  372. if (IS_ERR(p))
  373. return PTR_ERR(p);
  374. call_netdevice_notifiers(NETDEV_JOIN, dev);
  375. err = dev_set_allmulti(dev, 1);
  376. if (err)
  377. goto put_back;
  378. err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
  379. SYSFS_BRIDGE_PORT_ATTR);
  380. if (err)
  381. goto err1;
  382. err = br_sysfs_addif(p);
  383. if (err)
  384. goto err2;
  385. err = br_netpoll_enable(p);
  386. if (err)
  387. goto err3;
  388. err = netdev_rx_handler_register(dev, br_handle_frame, p);
  389. if (err)
  390. goto err4;
  391. dev->priv_flags |= IFF_BRIDGE_PORT;
  392. err = netdev_master_upper_dev_link(dev, br->dev);
  393. if (err)
  394. goto err5;
  395. dev_disable_lro(dev);
  396. list_add_rcu(&p->list, &br->port_list);
  397. nbp_update_port_count(br);
  398. netdev_update_features(br->dev);
  399. if (br->dev->needed_headroom < dev->needed_headroom)
  400. br->dev->needed_headroom = dev->needed_headroom;
  401. if (br_fdb_insert(br, p, dev->dev_addr, 0))
  402. netdev_err(dev, "failed insert local address bridge forwarding table\n");
  403. if (nbp_vlan_init(p))
  404. netdev_err(dev, "failed to initialize vlan filtering on this port\n");
  405. spin_lock_bh(&br->lock);
  406. changed_addr = br_stp_recalculate_bridge_id(br);
  407. if (netif_running(dev) && netif_oper_up(dev) &&
  408. (br->dev->flags & IFF_UP))
  409. br_stp_enable_port(p);
  410. spin_unlock_bh(&br->lock);
  411. br_ifinfo_notify(RTM_NEWLINK, p);
  412. if (changed_addr)
  413. call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
  414. dev_set_mtu(br->dev, br_min_mtu(br));
  415. kobject_uevent(&p->kobj, KOBJ_ADD);
  416. return 0;
  417. err5:
  418. dev->priv_flags &= ~IFF_BRIDGE_PORT;
  419. netdev_rx_handler_unregister(dev);
  420. err4:
  421. br_netpoll_disable(p);
  422. err3:
  423. sysfs_remove_link(br->ifobj, p->dev->name);
  424. err2:
  425. kobject_put(&p->kobj);
  426. p = NULL; /* kobject_put frees */
  427. err1:
  428. dev_set_allmulti(dev, -1);
  429. put_back:
  430. dev_put(dev);
  431. kfree(p);
  432. return err;
  433. }
  434. /* called with RTNL */
  435. int br_del_if(struct net_bridge *br, struct net_device *dev)
  436. {
  437. struct net_bridge_port *p;
  438. bool changed_addr;
  439. p = br_port_get_rtnl(dev);
  440. if (!p || p->br != br)
  441. return -EINVAL;
  442. /* Since more than one interface can be attached to a bridge,
  443. * there still maybe an alternate path for netconsole to use;
  444. * therefore there is no reason for a NETDEV_RELEASE event.
  445. */
  446. del_nbp(p);
  447. spin_lock_bh(&br->lock);
  448. changed_addr = br_stp_recalculate_bridge_id(br);
  449. spin_unlock_bh(&br->lock);
  450. if (changed_addr)
  451. call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
  452. netdev_update_features(br->dev);
  453. return 0;
  454. }
  455. void br_port_flags_change(struct net_bridge_port *p, unsigned long mask)
  456. {
  457. struct net_bridge *br = p->br;
  458. if (mask & BR_AUTO_MASK)
  459. nbp_update_port_count(br);
  460. }