br_if.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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_link_ksettings ecmd;
  37. if (!__ethtool_get_link_ksettings(dev, &ecmd)) {
  38. switch (ecmd.base.speed) {
  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. static unsigned get_max_headroom(struct net_bridge *br)
  194. {
  195. unsigned max_headroom = 0;
  196. struct net_bridge_port *p;
  197. list_for_each_entry(p, &br->port_list, list) {
  198. unsigned dev_headroom = netdev_get_fwd_headroom(p->dev);
  199. if (dev_headroom > max_headroom)
  200. max_headroom = dev_headroom;
  201. }
  202. return max_headroom;
  203. }
  204. static void update_headroom(struct net_bridge *br, int new_hr)
  205. {
  206. struct net_bridge_port *p;
  207. list_for_each_entry(p, &br->port_list, list)
  208. netdev_set_rx_headroom(p->dev, new_hr);
  209. br->dev->needed_headroom = new_hr;
  210. }
  211. /* Delete port(interface) from bridge is done in two steps.
  212. * via RCU. First step, marks device as down. That deletes
  213. * all the timers and stops new packets from flowing through.
  214. *
  215. * Final cleanup doesn't occur until after all CPU's finished
  216. * processing packets.
  217. *
  218. * Protected from multiple admin operations by RTNL mutex
  219. */
  220. static void del_nbp(struct net_bridge_port *p)
  221. {
  222. struct net_bridge *br = p->br;
  223. struct net_device *dev = p->dev;
  224. sysfs_remove_link(br->ifobj, p->dev->name);
  225. nbp_delete_promisc(p);
  226. spin_lock_bh(&br->lock);
  227. br_stp_disable_port(p);
  228. spin_unlock_bh(&br->lock);
  229. br_ifinfo_notify(RTM_DELLINK, p);
  230. list_del_rcu(&p->list);
  231. if (netdev_get_fwd_headroom(dev) == br->dev->needed_headroom)
  232. update_headroom(br, get_max_headroom(br));
  233. netdev_reset_rx_headroom(dev);
  234. nbp_vlan_flush(p);
  235. br_fdb_delete_by_port(br, p, 0, 1);
  236. switchdev_deferred_process();
  237. nbp_update_port_count(br);
  238. netdev_upper_dev_unlink(dev, br->dev);
  239. dev->priv_flags &= ~IFF_BRIDGE_PORT;
  240. netdev_rx_handler_unregister(dev);
  241. br_multicast_del_port(p);
  242. kobject_uevent(&p->kobj, KOBJ_REMOVE);
  243. kobject_del(&p->kobj);
  244. br_netpoll_disable(p);
  245. call_rcu(&p->rcu, destroy_nbp_rcu);
  246. }
  247. /* Delete bridge device */
  248. void br_dev_delete(struct net_device *dev, struct list_head *head)
  249. {
  250. struct net_bridge *br = netdev_priv(dev);
  251. struct net_bridge_port *p, *n;
  252. list_for_each_entry_safe(p, n, &br->port_list, list) {
  253. del_nbp(p);
  254. }
  255. br_fdb_delete_by_port(br, NULL, 0, 1);
  256. br_vlan_flush(br);
  257. br_multicast_dev_del(br);
  258. del_timer_sync(&br->gc_timer);
  259. br_sysfs_delbr(br->dev);
  260. unregister_netdevice_queue(br->dev, head);
  261. }
  262. /* find an available port number */
  263. static int find_portno(struct net_bridge *br)
  264. {
  265. int index;
  266. struct net_bridge_port *p;
  267. unsigned long *inuse;
  268. inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long),
  269. GFP_KERNEL);
  270. if (!inuse)
  271. return -ENOMEM;
  272. set_bit(0, inuse); /* zero is reserved */
  273. list_for_each_entry(p, &br->port_list, list) {
  274. set_bit(p->port_no, inuse);
  275. }
  276. index = find_first_zero_bit(inuse, BR_MAX_PORTS);
  277. kfree(inuse);
  278. return (index >= BR_MAX_PORTS) ? -EXFULL : index;
  279. }
  280. /* called with RTNL but without bridge lock */
  281. static struct net_bridge_port *new_nbp(struct net_bridge *br,
  282. struct net_device *dev)
  283. {
  284. struct net_bridge_port *p;
  285. int index, err;
  286. index = find_portno(br);
  287. if (index < 0)
  288. return ERR_PTR(index);
  289. p = kzalloc(sizeof(*p), GFP_KERNEL);
  290. if (p == NULL)
  291. return ERR_PTR(-ENOMEM);
  292. p->br = br;
  293. dev_hold(dev);
  294. p->dev = dev;
  295. p->path_cost = port_cost(dev);
  296. p->priority = 0x8000 >> BR_PORT_BITS;
  297. p->port_no = index;
  298. p->flags = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD;
  299. br_init_port(p);
  300. br_set_state(p, BR_STATE_DISABLED);
  301. br_stp_port_timer_init(p);
  302. err = br_multicast_add_port(p);
  303. if (err) {
  304. dev_put(dev);
  305. kfree(p);
  306. p = ERR_PTR(err);
  307. }
  308. return p;
  309. }
  310. int br_add_bridge(struct net *net, const char *name)
  311. {
  312. struct net_device *dev;
  313. int res;
  314. dev = alloc_netdev(sizeof(struct net_bridge), name, NET_NAME_UNKNOWN,
  315. br_dev_setup);
  316. if (!dev)
  317. return -ENOMEM;
  318. dev_net_set(dev, net);
  319. dev->rtnl_link_ops = &br_link_ops;
  320. res = register_netdev(dev);
  321. if (res)
  322. free_netdev(dev);
  323. return res;
  324. }
  325. int br_del_bridge(struct net *net, const char *name)
  326. {
  327. struct net_device *dev;
  328. int ret = 0;
  329. rtnl_lock();
  330. dev = __dev_get_by_name(net, name);
  331. if (dev == NULL)
  332. ret = -ENXIO; /* Could not find device */
  333. else if (!(dev->priv_flags & IFF_EBRIDGE)) {
  334. /* Attempt to delete non bridge device! */
  335. ret = -EPERM;
  336. }
  337. else if (dev->flags & IFF_UP) {
  338. /* Not shutdown yet. */
  339. ret = -EBUSY;
  340. }
  341. else
  342. br_dev_delete(dev, NULL);
  343. rtnl_unlock();
  344. return ret;
  345. }
  346. /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
  347. int br_min_mtu(const struct net_bridge *br)
  348. {
  349. const struct net_bridge_port *p;
  350. int mtu = 0;
  351. ASSERT_RTNL();
  352. if (list_empty(&br->port_list))
  353. mtu = ETH_DATA_LEN;
  354. else {
  355. list_for_each_entry(p, &br->port_list, list) {
  356. if (!mtu || p->dev->mtu < mtu)
  357. mtu = p->dev->mtu;
  358. }
  359. }
  360. return mtu;
  361. }
  362. static void br_set_gso_limits(struct net_bridge *br)
  363. {
  364. unsigned int gso_max_size = GSO_MAX_SIZE;
  365. u16 gso_max_segs = GSO_MAX_SEGS;
  366. const struct net_bridge_port *p;
  367. list_for_each_entry(p, &br->port_list, list) {
  368. gso_max_size = min(gso_max_size, p->dev->gso_max_size);
  369. gso_max_segs = min(gso_max_segs, p->dev->gso_max_segs);
  370. }
  371. br->dev->gso_max_size = gso_max_size;
  372. br->dev->gso_max_segs = gso_max_segs;
  373. }
  374. /*
  375. * Recomputes features using slave's features
  376. */
  377. netdev_features_t br_features_recompute(struct net_bridge *br,
  378. netdev_features_t features)
  379. {
  380. struct net_bridge_port *p;
  381. netdev_features_t mask;
  382. if (list_empty(&br->port_list))
  383. return features;
  384. mask = features;
  385. features &= ~NETIF_F_ONE_FOR_ALL;
  386. list_for_each_entry(p, &br->port_list, list) {
  387. features = netdev_increment_features(features,
  388. p->dev->features, mask);
  389. }
  390. features = netdev_add_tso_features(features, mask);
  391. return features;
  392. }
  393. /* called with RTNL */
  394. int br_add_if(struct net_bridge *br, struct net_device *dev)
  395. {
  396. struct net_bridge_port *p;
  397. int err = 0;
  398. unsigned br_hr, dev_hr;
  399. bool changed_addr;
  400. /* Don't allow bridging non-ethernet like devices, or DSA-enabled
  401. * master network devices since the bridge layer rx_handler prevents
  402. * the DSA fake ethertype handler to be invoked, so we do not strip off
  403. * the DSA switch tag protocol header and the bridge layer just return
  404. * RX_HANDLER_CONSUMED, stopping RX processing for these frames.
  405. */
  406. if ((dev->flags & IFF_LOOPBACK) ||
  407. dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
  408. !is_valid_ether_addr(dev->dev_addr) ||
  409. netdev_uses_dsa(dev))
  410. return -EINVAL;
  411. /* No bridging of bridges */
  412. if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit)
  413. return -ELOOP;
  414. /* Device has master upper dev */
  415. if (netdev_master_upper_dev_get(dev))
  416. return -EBUSY;
  417. /* No bridging devices that dislike that (e.g. wireless) */
  418. if (dev->priv_flags & IFF_DONT_BRIDGE)
  419. return -EOPNOTSUPP;
  420. p = new_nbp(br, dev);
  421. if (IS_ERR(p))
  422. return PTR_ERR(p);
  423. call_netdevice_notifiers(NETDEV_JOIN, dev);
  424. err = dev_set_allmulti(dev, 1);
  425. if (err)
  426. goto put_back;
  427. err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
  428. SYSFS_BRIDGE_PORT_ATTR);
  429. if (err)
  430. goto err1;
  431. err = br_sysfs_addif(p);
  432. if (err)
  433. goto err2;
  434. err = br_netpoll_enable(p);
  435. if (err)
  436. goto err3;
  437. err = netdev_rx_handler_register(dev, br_handle_frame, p);
  438. if (err)
  439. goto err4;
  440. dev->priv_flags |= IFF_BRIDGE_PORT;
  441. err = netdev_master_upper_dev_link(dev, br->dev, NULL, NULL);
  442. if (err)
  443. goto err5;
  444. err = nbp_switchdev_mark_set(p);
  445. if (err)
  446. goto err6;
  447. dev_disable_lro(dev);
  448. list_add_rcu(&p->list, &br->port_list);
  449. nbp_update_port_count(br);
  450. netdev_update_features(br->dev);
  451. br_hr = br->dev->needed_headroom;
  452. dev_hr = netdev_get_fwd_headroom(dev);
  453. if (br_hr < dev_hr)
  454. update_headroom(br, dev_hr);
  455. else
  456. netdev_set_rx_headroom(dev, br_hr);
  457. if (br_fdb_insert(br, p, dev->dev_addr, 0))
  458. netdev_err(dev, "failed insert local address bridge forwarding table\n");
  459. err = nbp_vlan_init(p);
  460. if (err) {
  461. netdev_err(dev, "failed to initialize vlan filtering on this port\n");
  462. goto err7;
  463. }
  464. spin_lock_bh(&br->lock);
  465. changed_addr = br_stp_recalculate_bridge_id(br);
  466. if (netif_running(dev) && netif_oper_up(dev) &&
  467. (br->dev->flags & IFF_UP))
  468. br_stp_enable_port(p);
  469. spin_unlock_bh(&br->lock);
  470. br_ifinfo_notify(RTM_NEWLINK, p);
  471. if (changed_addr)
  472. call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
  473. dev_set_mtu(br->dev, br_min_mtu(br));
  474. br_set_gso_limits(br);
  475. kobject_uevent(&p->kobj, KOBJ_ADD);
  476. return 0;
  477. err7:
  478. list_del_rcu(&p->list);
  479. br_fdb_delete_by_port(br, p, 0, 1);
  480. nbp_update_port_count(br);
  481. err6:
  482. netdev_upper_dev_unlink(dev, br->dev);
  483. err5:
  484. dev->priv_flags &= ~IFF_BRIDGE_PORT;
  485. netdev_rx_handler_unregister(dev);
  486. err4:
  487. br_netpoll_disable(p);
  488. err3:
  489. sysfs_remove_link(br->ifobj, p->dev->name);
  490. err2:
  491. kobject_put(&p->kobj);
  492. p = NULL; /* kobject_put frees */
  493. err1:
  494. dev_set_allmulti(dev, -1);
  495. put_back:
  496. dev_put(dev);
  497. kfree(p);
  498. return err;
  499. }
  500. /* called with RTNL */
  501. int br_del_if(struct net_bridge *br, struct net_device *dev)
  502. {
  503. struct net_bridge_port *p;
  504. bool changed_addr;
  505. p = br_port_get_rtnl(dev);
  506. if (!p || p->br != br)
  507. return -EINVAL;
  508. /* Since more than one interface can be attached to a bridge,
  509. * there still maybe an alternate path for netconsole to use;
  510. * therefore there is no reason for a NETDEV_RELEASE event.
  511. */
  512. del_nbp(p);
  513. dev_set_mtu(br->dev, br_min_mtu(br));
  514. br_set_gso_limits(br);
  515. spin_lock_bh(&br->lock);
  516. changed_addr = br_stp_recalculate_bridge_id(br);
  517. spin_unlock_bh(&br->lock);
  518. if (changed_addr)
  519. call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
  520. netdev_update_features(br->dev);
  521. return 0;
  522. }
  523. void br_port_flags_change(struct net_bridge_port *p, unsigned long mask)
  524. {
  525. struct net_bridge *br = p->br;
  526. if (mask & BR_AUTO_MASK)
  527. nbp_update_port_count(br);
  528. }