br_if.c 13 KB

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