br_fdb.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /*
  2. * Forwarding database
  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/init.h>
  15. #include <linux/rculist.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/times.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/jhash.h>
  21. #include <linux/random.h>
  22. #include <linux/slab.h>
  23. #include <linux/atomic.h>
  24. #include <asm/unaligned.h>
  25. #include <linux/if_vlan.h>
  26. #include <net/switchdev.h>
  27. #include <trace/events/bridge.h>
  28. #include "br_private.h"
  29. static const struct rhashtable_params br_fdb_rht_params = {
  30. .head_offset = offsetof(struct net_bridge_fdb_entry, rhnode),
  31. .key_offset = offsetof(struct net_bridge_fdb_entry, key),
  32. .key_len = sizeof(struct net_bridge_fdb_key),
  33. .automatic_shrinking = true,
  34. .locks_mul = 1,
  35. };
  36. static struct kmem_cache *br_fdb_cache __read_mostly;
  37. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  38. const unsigned char *addr, u16 vid);
  39. static void fdb_notify(struct net_bridge *br,
  40. const struct net_bridge_fdb_entry *, int, bool);
  41. int __init br_fdb_init(void)
  42. {
  43. br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
  44. sizeof(struct net_bridge_fdb_entry),
  45. 0,
  46. SLAB_HWCACHE_ALIGN, NULL);
  47. if (!br_fdb_cache)
  48. return -ENOMEM;
  49. return 0;
  50. }
  51. void br_fdb_fini(void)
  52. {
  53. kmem_cache_destroy(br_fdb_cache);
  54. }
  55. int br_fdb_hash_init(struct net_bridge *br)
  56. {
  57. return rhashtable_init(&br->fdb_hash_tbl, &br_fdb_rht_params);
  58. }
  59. void br_fdb_hash_fini(struct net_bridge *br)
  60. {
  61. rhashtable_destroy(&br->fdb_hash_tbl);
  62. }
  63. /* if topology_changing then use forward_delay (default 15 sec)
  64. * otherwise keep longer (default 5 minutes)
  65. */
  66. static inline unsigned long hold_time(const struct net_bridge *br)
  67. {
  68. return br->topology_change ? br->forward_delay : br->ageing_time;
  69. }
  70. static inline int has_expired(const struct net_bridge *br,
  71. const struct net_bridge_fdb_entry *fdb)
  72. {
  73. return !fdb->is_static && !fdb->added_by_external_learn &&
  74. time_before_eq(fdb->updated + hold_time(br), jiffies);
  75. }
  76. static void fdb_rcu_free(struct rcu_head *head)
  77. {
  78. struct net_bridge_fdb_entry *ent
  79. = container_of(head, struct net_bridge_fdb_entry, rcu);
  80. kmem_cache_free(br_fdb_cache, ent);
  81. }
  82. static struct net_bridge_fdb_entry *fdb_find_rcu(struct rhashtable *tbl,
  83. const unsigned char *addr,
  84. __u16 vid)
  85. {
  86. struct net_bridge_fdb_key key;
  87. WARN_ON_ONCE(!rcu_read_lock_held());
  88. key.vlan_id = vid;
  89. memcpy(key.addr.addr, addr, sizeof(key.addr.addr));
  90. return rhashtable_lookup(tbl, &key, br_fdb_rht_params);
  91. }
  92. /* requires bridge hash_lock */
  93. static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
  94. const unsigned char *addr,
  95. __u16 vid)
  96. {
  97. struct net_bridge_fdb_entry *fdb;
  98. lockdep_assert_held_once(&br->hash_lock);
  99. rcu_read_lock();
  100. fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
  101. rcu_read_unlock();
  102. return fdb;
  103. }
  104. struct net_device *br_fdb_find_port(const struct net_device *br_dev,
  105. const unsigned char *addr,
  106. __u16 vid)
  107. {
  108. struct net_bridge_fdb_entry *f;
  109. struct net_device *dev = NULL;
  110. struct net_bridge *br;
  111. ASSERT_RTNL();
  112. if (!netif_is_bridge_master(br_dev))
  113. return NULL;
  114. br = netdev_priv(br_dev);
  115. rcu_read_lock();
  116. f = br_fdb_find_rcu(br, addr, vid);
  117. if (f && f->dst)
  118. dev = f->dst->dev;
  119. rcu_read_unlock();
  120. return dev;
  121. }
  122. EXPORT_SYMBOL_GPL(br_fdb_find_port);
  123. struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
  124. const unsigned char *addr,
  125. __u16 vid)
  126. {
  127. return fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
  128. }
  129. /* When a static FDB entry is added, the mac address from the entry is
  130. * added to the bridge private HW address list and all required ports
  131. * are then updated with the new information.
  132. * Called under RTNL.
  133. */
  134. static void fdb_add_hw_addr(struct net_bridge *br, const unsigned char *addr)
  135. {
  136. int err;
  137. struct net_bridge_port *p;
  138. ASSERT_RTNL();
  139. list_for_each_entry(p, &br->port_list, list) {
  140. if (!br_promisc_port(p)) {
  141. err = dev_uc_add(p->dev, addr);
  142. if (err)
  143. goto undo;
  144. }
  145. }
  146. return;
  147. undo:
  148. list_for_each_entry_continue_reverse(p, &br->port_list, list) {
  149. if (!br_promisc_port(p))
  150. dev_uc_del(p->dev, addr);
  151. }
  152. }
  153. /* When a static FDB entry is deleted, the HW address from that entry is
  154. * also removed from the bridge private HW address list and updates all
  155. * the ports with needed information.
  156. * Called under RTNL.
  157. */
  158. static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
  159. {
  160. struct net_bridge_port *p;
  161. ASSERT_RTNL();
  162. list_for_each_entry(p, &br->port_list, list) {
  163. if (!br_promisc_port(p))
  164. dev_uc_del(p->dev, addr);
  165. }
  166. }
  167. static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
  168. bool swdev_notify)
  169. {
  170. trace_fdb_delete(br, f);
  171. if (f->is_static)
  172. fdb_del_hw_addr(br, f->key.addr.addr);
  173. hlist_del_init_rcu(&f->fdb_node);
  174. rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
  175. br_fdb_rht_params);
  176. fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
  177. call_rcu(&f->rcu, fdb_rcu_free);
  178. }
  179. /* Delete a local entry if no other port had the same address. */
  180. static void fdb_delete_local(struct net_bridge *br,
  181. const struct net_bridge_port *p,
  182. struct net_bridge_fdb_entry *f)
  183. {
  184. const unsigned char *addr = f->key.addr.addr;
  185. struct net_bridge_vlan_group *vg;
  186. const struct net_bridge_vlan *v;
  187. struct net_bridge_port *op;
  188. u16 vid = f->key.vlan_id;
  189. /* Maybe another port has same hw addr? */
  190. list_for_each_entry(op, &br->port_list, list) {
  191. vg = nbp_vlan_group(op);
  192. if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
  193. (!vid || br_vlan_find(vg, vid))) {
  194. f->dst = op;
  195. f->added_by_user = 0;
  196. return;
  197. }
  198. }
  199. vg = br_vlan_group(br);
  200. v = br_vlan_find(vg, vid);
  201. /* Maybe bridge device has same hw addr? */
  202. if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
  203. (!vid || (v && br_vlan_should_use(v)))) {
  204. f->dst = NULL;
  205. f->added_by_user = 0;
  206. return;
  207. }
  208. fdb_delete(br, f, true);
  209. }
  210. void br_fdb_find_delete_local(struct net_bridge *br,
  211. const struct net_bridge_port *p,
  212. const unsigned char *addr, u16 vid)
  213. {
  214. struct net_bridge_fdb_entry *f;
  215. spin_lock_bh(&br->hash_lock);
  216. f = br_fdb_find(br, addr, vid);
  217. if (f && f->is_local && !f->added_by_user && f->dst == p)
  218. fdb_delete_local(br, p, f);
  219. spin_unlock_bh(&br->hash_lock);
  220. }
  221. void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
  222. {
  223. struct net_bridge_vlan_group *vg;
  224. struct net_bridge_fdb_entry *f;
  225. struct net_bridge *br = p->br;
  226. struct net_bridge_vlan *v;
  227. spin_lock_bh(&br->hash_lock);
  228. vg = nbp_vlan_group(p);
  229. hlist_for_each_entry(f, &br->fdb_list, fdb_node) {
  230. if (f->dst == p && f->is_local && !f->added_by_user) {
  231. /* delete old one */
  232. fdb_delete_local(br, p, f);
  233. /* if this port has no vlan information
  234. * configured, we can safely be done at
  235. * this point.
  236. */
  237. if (!vg || !vg->num_vlans)
  238. goto insert;
  239. }
  240. }
  241. insert:
  242. /* insert new address, may fail if invalid address or dup. */
  243. fdb_insert(br, p, newaddr, 0);
  244. if (!vg || !vg->num_vlans)
  245. goto done;
  246. /* Now add entries for every VLAN configured on the port.
  247. * This function runs under RTNL so the bitmap will not change
  248. * from under us.
  249. */
  250. list_for_each_entry(v, &vg->vlan_list, vlist)
  251. fdb_insert(br, p, newaddr, v->vid);
  252. done:
  253. spin_unlock_bh(&br->hash_lock);
  254. }
  255. void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
  256. {
  257. struct net_bridge_vlan_group *vg;
  258. struct net_bridge_fdb_entry *f;
  259. struct net_bridge_vlan *v;
  260. spin_lock_bh(&br->hash_lock);
  261. /* If old entry was unassociated with any port, then delete it. */
  262. f = br_fdb_find(br, br->dev->dev_addr, 0);
  263. if (f && f->is_local && !f->dst && !f->added_by_user)
  264. fdb_delete_local(br, NULL, f);
  265. fdb_insert(br, NULL, newaddr, 0);
  266. vg = br_vlan_group(br);
  267. if (!vg || !vg->num_vlans)
  268. goto out;
  269. /* Now remove and add entries for every VLAN configured on the
  270. * bridge. This function runs under RTNL so the bitmap will not
  271. * change from under us.
  272. */
  273. list_for_each_entry(v, &vg->vlan_list, vlist) {
  274. if (!br_vlan_should_use(v))
  275. continue;
  276. f = br_fdb_find(br, br->dev->dev_addr, v->vid);
  277. if (f && f->is_local && !f->dst && !f->added_by_user)
  278. fdb_delete_local(br, NULL, f);
  279. fdb_insert(br, NULL, newaddr, v->vid);
  280. }
  281. out:
  282. spin_unlock_bh(&br->hash_lock);
  283. }
  284. void br_fdb_cleanup(struct work_struct *work)
  285. {
  286. struct net_bridge *br = container_of(work, struct net_bridge,
  287. gc_work.work);
  288. struct net_bridge_fdb_entry *f = NULL;
  289. unsigned long delay = hold_time(br);
  290. unsigned long work_delay = delay;
  291. unsigned long now = jiffies;
  292. /* this part is tricky, in order to avoid blocking learning and
  293. * consequently forwarding, we rely on rcu to delete objects with
  294. * delayed freeing allowing us to continue traversing
  295. */
  296. rcu_read_lock();
  297. hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
  298. unsigned long this_timer;
  299. if (f->is_static || f->added_by_external_learn)
  300. continue;
  301. this_timer = f->updated + delay;
  302. if (time_after(this_timer, now)) {
  303. work_delay = min(work_delay, this_timer - now);
  304. } else {
  305. spin_lock_bh(&br->hash_lock);
  306. if (!hlist_unhashed(&f->fdb_node))
  307. fdb_delete(br, f, true);
  308. spin_unlock_bh(&br->hash_lock);
  309. }
  310. }
  311. rcu_read_unlock();
  312. /* Cleanup minimum 10 milliseconds apart */
  313. work_delay = max_t(unsigned long, work_delay, msecs_to_jiffies(10));
  314. mod_delayed_work(system_long_wq, &br->gc_work, work_delay);
  315. }
  316. /* Completely flush all dynamic entries in forwarding database.*/
  317. void br_fdb_flush(struct net_bridge *br)
  318. {
  319. struct net_bridge_fdb_entry *f;
  320. struct hlist_node *tmp;
  321. spin_lock_bh(&br->hash_lock);
  322. hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
  323. if (!f->is_static)
  324. fdb_delete(br, f, true);
  325. }
  326. spin_unlock_bh(&br->hash_lock);
  327. }
  328. /* Flush all entries referring to a specific port.
  329. * if do_all is set also flush static entries
  330. * if vid is set delete all entries that match the vlan_id
  331. */
  332. void br_fdb_delete_by_port(struct net_bridge *br,
  333. const struct net_bridge_port *p,
  334. u16 vid,
  335. int do_all)
  336. {
  337. struct net_bridge_fdb_entry *f;
  338. struct hlist_node *tmp;
  339. spin_lock_bh(&br->hash_lock);
  340. hlist_for_each_entry_safe(f, tmp, &br->fdb_list, fdb_node) {
  341. if (f->dst != p)
  342. continue;
  343. if (!do_all)
  344. if (f->is_static || (vid && f->key.vlan_id != vid))
  345. continue;
  346. if (f->is_local)
  347. fdb_delete_local(br, p, f);
  348. else
  349. fdb_delete(br, f, true);
  350. }
  351. spin_unlock_bh(&br->hash_lock);
  352. }
  353. #if IS_ENABLED(CONFIG_ATM_LANE)
  354. /* Interface used by ATM LANE hook to test
  355. * if an addr is on some other bridge port */
  356. int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
  357. {
  358. struct net_bridge_fdb_entry *fdb;
  359. struct net_bridge_port *port;
  360. int ret;
  361. rcu_read_lock();
  362. port = br_port_get_rcu(dev);
  363. if (!port)
  364. ret = 0;
  365. else {
  366. fdb = br_fdb_find_rcu(port->br, addr, 0);
  367. ret = fdb && fdb->dst && fdb->dst->dev != dev &&
  368. fdb->dst->state == BR_STATE_FORWARDING;
  369. }
  370. rcu_read_unlock();
  371. return ret;
  372. }
  373. #endif /* CONFIG_ATM_LANE */
  374. /*
  375. * Fill buffer with forwarding table records in
  376. * the API format.
  377. */
  378. int br_fdb_fillbuf(struct net_bridge *br, void *buf,
  379. unsigned long maxnum, unsigned long skip)
  380. {
  381. struct net_bridge_fdb_entry *f;
  382. struct __fdb_entry *fe = buf;
  383. int num = 0;
  384. memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
  385. rcu_read_lock();
  386. hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
  387. if (num >= maxnum)
  388. break;
  389. if (has_expired(br, f))
  390. continue;
  391. /* ignore pseudo entry for local MAC address */
  392. if (!f->dst)
  393. continue;
  394. if (skip) {
  395. --skip;
  396. continue;
  397. }
  398. /* convert from internal format to API */
  399. memcpy(fe->mac_addr, f->key.addr.addr, ETH_ALEN);
  400. /* due to ABI compat need to split into hi/lo */
  401. fe->port_no = f->dst->port_no;
  402. fe->port_hi = f->dst->port_no >> 8;
  403. fe->is_local = f->is_local;
  404. if (!f->is_static)
  405. fe->ageing_timer_value = jiffies_delta_to_clock_t(jiffies - f->updated);
  406. ++fe;
  407. ++num;
  408. }
  409. rcu_read_unlock();
  410. return num;
  411. }
  412. static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
  413. struct net_bridge_port *source,
  414. const unsigned char *addr,
  415. __u16 vid,
  416. unsigned char is_local,
  417. unsigned char is_static)
  418. {
  419. struct net_bridge_fdb_entry *fdb;
  420. fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
  421. if (fdb) {
  422. memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
  423. fdb->dst = source;
  424. fdb->key.vlan_id = vid;
  425. fdb->is_local = is_local;
  426. fdb->is_static = is_static;
  427. fdb->added_by_user = 0;
  428. fdb->added_by_external_learn = 0;
  429. fdb->offloaded = 0;
  430. fdb->updated = fdb->used = jiffies;
  431. if (rhashtable_lookup_insert_fast(&br->fdb_hash_tbl,
  432. &fdb->rhnode,
  433. br_fdb_rht_params)) {
  434. kmem_cache_free(br_fdb_cache, fdb);
  435. fdb = NULL;
  436. } else {
  437. hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
  438. }
  439. }
  440. return fdb;
  441. }
  442. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  443. const unsigned char *addr, u16 vid)
  444. {
  445. struct net_bridge_fdb_entry *fdb;
  446. if (!is_valid_ether_addr(addr))
  447. return -EINVAL;
  448. fdb = br_fdb_find(br, addr, vid);
  449. if (fdb) {
  450. /* it is okay to have multiple ports with same
  451. * address, just use the first one.
  452. */
  453. if (fdb->is_local)
  454. return 0;
  455. br_warn(br, "adding interface %s with same address as a received packet (addr:%pM, vlan:%u)\n",
  456. source ? source->dev->name : br->dev->name, addr, vid);
  457. fdb_delete(br, fdb, true);
  458. }
  459. fdb = fdb_create(br, source, addr, vid, 1, 1);
  460. if (!fdb)
  461. return -ENOMEM;
  462. fdb_add_hw_addr(br, addr);
  463. fdb_notify(br, fdb, RTM_NEWNEIGH, true);
  464. return 0;
  465. }
  466. /* Add entry for local address of interface */
  467. int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  468. const unsigned char *addr, u16 vid)
  469. {
  470. int ret;
  471. spin_lock_bh(&br->hash_lock);
  472. ret = fdb_insert(br, source, addr, vid);
  473. spin_unlock_bh(&br->hash_lock);
  474. return ret;
  475. }
  476. void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
  477. const unsigned char *addr, u16 vid, bool added_by_user)
  478. {
  479. struct net_bridge_fdb_entry *fdb;
  480. bool fdb_modified = false;
  481. /* some users want to always flood. */
  482. if (hold_time(br) == 0)
  483. return;
  484. /* ignore packets unless we are using this port */
  485. if (!(source->state == BR_STATE_LEARNING ||
  486. source->state == BR_STATE_FORWARDING))
  487. return;
  488. fdb = fdb_find_rcu(&br->fdb_hash_tbl, addr, vid);
  489. if (likely(fdb)) {
  490. /* attempt to update an entry for a local interface */
  491. if (unlikely(fdb->is_local)) {
  492. if (net_ratelimit())
  493. br_warn(br, "received packet on %s with own address as source address (addr:%pM, vlan:%u)\n",
  494. source->dev->name, addr, vid);
  495. } else {
  496. unsigned long now = jiffies;
  497. /* fastpath: update of existing entry */
  498. if (unlikely(source != fdb->dst)) {
  499. fdb->dst = source;
  500. fdb_modified = true;
  501. /* Take over HW learned entry */
  502. if (unlikely(fdb->added_by_external_learn))
  503. fdb->added_by_external_learn = 0;
  504. }
  505. if (now != fdb->updated)
  506. fdb->updated = now;
  507. if (unlikely(added_by_user))
  508. fdb->added_by_user = 1;
  509. if (unlikely(fdb_modified)) {
  510. trace_br_fdb_update(br, source, addr, vid, added_by_user);
  511. fdb_notify(br, fdb, RTM_NEWNEIGH, true);
  512. }
  513. }
  514. } else {
  515. spin_lock(&br->hash_lock);
  516. fdb = fdb_create(br, source, addr, vid, 0, 0);
  517. if (fdb) {
  518. if (unlikely(added_by_user))
  519. fdb->added_by_user = 1;
  520. trace_br_fdb_update(br, source, addr, vid,
  521. added_by_user);
  522. fdb_notify(br, fdb, RTM_NEWNEIGH, true);
  523. }
  524. /* else we lose race and someone else inserts
  525. * it first, don't bother updating
  526. */
  527. spin_unlock(&br->hash_lock);
  528. }
  529. }
  530. static int fdb_to_nud(const struct net_bridge *br,
  531. const struct net_bridge_fdb_entry *fdb)
  532. {
  533. if (fdb->is_local)
  534. return NUD_PERMANENT;
  535. else if (fdb->is_static)
  536. return NUD_NOARP;
  537. else if (has_expired(br, fdb))
  538. return NUD_STALE;
  539. else
  540. return NUD_REACHABLE;
  541. }
  542. static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
  543. const struct net_bridge_fdb_entry *fdb,
  544. u32 portid, u32 seq, int type, unsigned int flags)
  545. {
  546. unsigned long now = jiffies;
  547. struct nda_cacheinfo ci;
  548. struct nlmsghdr *nlh;
  549. struct ndmsg *ndm;
  550. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  551. if (nlh == NULL)
  552. return -EMSGSIZE;
  553. ndm = nlmsg_data(nlh);
  554. ndm->ndm_family = AF_BRIDGE;
  555. ndm->ndm_pad1 = 0;
  556. ndm->ndm_pad2 = 0;
  557. ndm->ndm_flags = 0;
  558. ndm->ndm_type = 0;
  559. ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
  560. ndm->ndm_state = fdb_to_nud(br, fdb);
  561. if (fdb->offloaded)
  562. ndm->ndm_flags |= NTF_OFFLOADED;
  563. if (fdb->added_by_external_learn)
  564. ndm->ndm_flags |= NTF_EXT_LEARNED;
  565. if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->key.addr))
  566. goto nla_put_failure;
  567. if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
  568. goto nla_put_failure;
  569. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  570. ci.ndm_confirmed = 0;
  571. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  572. ci.ndm_refcnt = 0;
  573. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  574. goto nla_put_failure;
  575. if (fdb->key.vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16),
  576. &fdb->key.vlan_id))
  577. goto nla_put_failure;
  578. nlmsg_end(skb, nlh);
  579. return 0;
  580. nla_put_failure:
  581. nlmsg_cancel(skb, nlh);
  582. return -EMSGSIZE;
  583. }
  584. static inline size_t fdb_nlmsg_size(void)
  585. {
  586. return NLMSG_ALIGN(sizeof(struct ndmsg))
  587. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  588. + nla_total_size(sizeof(u32)) /* NDA_MASTER */
  589. + nla_total_size(sizeof(u16)) /* NDA_VLAN */
  590. + nla_total_size(sizeof(struct nda_cacheinfo));
  591. }
  592. static void fdb_notify(struct net_bridge *br,
  593. const struct net_bridge_fdb_entry *fdb, int type,
  594. bool swdev_notify)
  595. {
  596. struct net *net = dev_net(br->dev);
  597. struct sk_buff *skb;
  598. int err = -ENOBUFS;
  599. if (swdev_notify)
  600. br_switchdev_fdb_notify(fdb, type);
  601. skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
  602. if (skb == NULL)
  603. goto errout;
  604. err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
  605. if (err < 0) {
  606. /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
  607. WARN_ON(err == -EMSGSIZE);
  608. kfree_skb(skb);
  609. goto errout;
  610. }
  611. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  612. return;
  613. errout:
  614. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  615. }
  616. /* Dump information about entries, in response to GETNEIGH */
  617. int br_fdb_dump(struct sk_buff *skb,
  618. struct netlink_callback *cb,
  619. struct net_device *dev,
  620. struct net_device *filter_dev,
  621. int *idx)
  622. {
  623. struct net_bridge *br = netdev_priv(dev);
  624. struct net_bridge_fdb_entry *f;
  625. int err = 0;
  626. if (!(dev->priv_flags & IFF_EBRIDGE))
  627. return err;
  628. if (!filter_dev) {
  629. err = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
  630. if (err < 0)
  631. return err;
  632. }
  633. rcu_read_lock();
  634. hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
  635. if (*idx < cb->args[2])
  636. goto skip;
  637. if (filter_dev && (!f->dst || f->dst->dev != filter_dev)) {
  638. if (filter_dev != dev)
  639. goto skip;
  640. /* !f->dst is a special case for bridge
  641. * It means the MAC belongs to the bridge
  642. * Therefore need a little more filtering
  643. * we only want to dump the !f->dst case
  644. */
  645. if (f->dst)
  646. goto skip;
  647. }
  648. if (!filter_dev && f->dst)
  649. goto skip;
  650. err = fdb_fill_info(skb, br, f,
  651. NETLINK_CB(cb->skb).portid,
  652. cb->nlh->nlmsg_seq,
  653. RTM_NEWNEIGH,
  654. NLM_F_MULTI);
  655. if (err < 0)
  656. break;
  657. skip:
  658. *idx += 1;
  659. }
  660. rcu_read_unlock();
  661. return err;
  662. }
  663. /* Update (create or replace) forwarding database entry */
  664. static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
  665. const __u8 *addr, __u16 state, __u16 flags, __u16 vid)
  666. {
  667. struct net_bridge_fdb_entry *fdb;
  668. bool modified = false;
  669. /* If the port cannot learn allow only local and static entries */
  670. if (source && !(state & NUD_PERMANENT) && !(state & NUD_NOARP) &&
  671. !(source->state == BR_STATE_LEARNING ||
  672. source->state == BR_STATE_FORWARDING))
  673. return -EPERM;
  674. if (!source && !(state & NUD_PERMANENT)) {
  675. pr_info("bridge: RTM_NEWNEIGH %s without NUD_PERMANENT\n",
  676. br->dev->name);
  677. return -EINVAL;
  678. }
  679. fdb = br_fdb_find(br, addr, vid);
  680. if (fdb == NULL) {
  681. if (!(flags & NLM_F_CREATE))
  682. return -ENOENT;
  683. fdb = fdb_create(br, source, addr, vid, 0, 0);
  684. if (!fdb)
  685. return -ENOMEM;
  686. modified = true;
  687. } else {
  688. if (flags & NLM_F_EXCL)
  689. return -EEXIST;
  690. if (fdb->dst != source) {
  691. fdb->dst = source;
  692. modified = true;
  693. }
  694. }
  695. if (fdb_to_nud(br, fdb) != state) {
  696. if (state & NUD_PERMANENT) {
  697. fdb->is_local = 1;
  698. if (!fdb->is_static) {
  699. fdb->is_static = 1;
  700. fdb_add_hw_addr(br, addr);
  701. }
  702. } else if (state & NUD_NOARP) {
  703. fdb->is_local = 0;
  704. if (!fdb->is_static) {
  705. fdb->is_static = 1;
  706. fdb_add_hw_addr(br, addr);
  707. }
  708. } else {
  709. fdb->is_local = 0;
  710. if (fdb->is_static) {
  711. fdb->is_static = 0;
  712. fdb_del_hw_addr(br, addr);
  713. }
  714. }
  715. modified = true;
  716. }
  717. fdb->added_by_user = 1;
  718. fdb->used = jiffies;
  719. if (modified) {
  720. fdb->updated = jiffies;
  721. fdb_notify(br, fdb, RTM_NEWNEIGH, true);
  722. }
  723. return 0;
  724. }
  725. static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge *br,
  726. struct net_bridge_port *p, const unsigned char *addr,
  727. u16 nlh_flags, u16 vid)
  728. {
  729. int err = 0;
  730. if (ndm->ndm_flags & NTF_USE) {
  731. if (!p) {
  732. pr_info("bridge: RTM_NEWNEIGH %s with NTF_USE is not supported\n",
  733. br->dev->name);
  734. return -EINVAL;
  735. }
  736. local_bh_disable();
  737. rcu_read_lock();
  738. br_fdb_update(br, p, addr, vid, true);
  739. rcu_read_unlock();
  740. local_bh_enable();
  741. } else if (ndm->ndm_flags & NTF_EXT_LEARNED) {
  742. err = br_fdb_external_learn_add(br, p, addr, vid, true);
  743. } else {
  744. spin_lock_bh(&br->hash_lock);
  745. err = fdb_add_entry(br, p, addr, ndm->ndm_state,
  746. nlh_flags, vid);
  747. spin_unlock_bh(&br->hash_lock);
  748. }
  749. return err;
  750. }
  751. /* Add new permanent fdb entry with RTM_NEWNEIGH */
  752. int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  753. struct net_device *dev,
  754. const unsigned char *addr, u16 vid, u16 nlh_flags)
  755. {
  756. struct net_bridge_vlan_group *vg;
  757. struct net_bridge_port *p = NULL;
  758. struct net_bridge_vlan *v;
  759. struct net_bridge *br = NULL;
  760. int err = 0;
  761. trace_br_fdb_add(ndm, dev, addr, vid, nlh_flags);
  762. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
  763. pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
  764. return -EINVAL;
  765. }
  766. if (is_zero_ether_addr(addr)) {
  767. pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
  768. return -EINVAL;
  769. }
  770. if (dev->priv_flags & IFF_EBRIDGE) {
  771. br = netdev_priv(dev);
  772. vg = br_vlan_group(br);
  773. } else {
  774. p = br_port_get_rtnl(dev);
  775. if (!p) {
  776. pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
  777. dev->name);
  778. return -EINVAL;
  779. }
  780. br = p->br;
  781. vg = nbp_vlan_group(p);
  782. }
  783. if (vid) {
  784. v = br_vlan_find(vg, vid);
  785. if (!v || !br_vlan_should_use(v)) {
  786. pr_info("bridge: RTM_NEWNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
  787. return -EINVAL;
  788. }
  789. /* VID was specified, so use it. */
  790. err = __br_fdb_add(ndm, br, p, addr, nlh_flags, vid);
  791. } else {
  792. err = __br_fdb_add(ndm, br, p, addr, nlh_flags, 0);
  793. if (err || !vg || !vg->num_vlans)
  794. goto out;
  795. /* We have vlans configured on this port and user didn't
  796. * specify a VLAN. To be nice, add/update entry for every
  797. * vlan on this port.
  798. */
  799. list_for_each_entry(v, &vg->vlan_list, vlist) {
  800. if (!br_vlan_should_use(v))
  801. continue;
  802. err = __br_fdb_add(ndm, br, p, addr, nlh_flags, v->vid);
  803. if (err)
  804. goto out;
  805. }
  806. }
  807. out:
  808. return err;
  809. }
  810. static int fdb_delete_by_addr_and_port(struct net_bridge *br,
  811. const struct net_bridge_port *p,
  812. const u8 *addr, u16 vlan)
  813. {
  814. struct net_bridge_fdb_entry *fdb;
  815. fdb = br_fdb_find(br, addr, vlan);
  816. if (!fdb || fdb->dst != p)
  817. return -ENOENT;
  818. fdb_delete(br, fdb, true);
  819. return 0;
  820. }
  821. static int __br_fdb_delete(struct net_bridge *br,
  822. const struct net_bridge_port *p,
  823. const unsigned char *addr, u16 vid)
  824. {
  825. int err;
  826. spin_lock_bh(&br->hash_lock);
  827. err = fdb_delete_by_addr_and_port(br, p, addr, vid);
  828. spin_unlock_bh(&br->hash_lock);
  829. return err;
  830. }
  831. /* Remove neighbor entry with RTM_DELNEIGH */
  832. int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  833. struct net_device *dev,
  834. const unsigned char *addr, u16 vid)
  835. {
  836. struct net_bridge_vlan_group *vg;
  837. struct net_bridge_port *p = NULL;
  838. struct net_bridge_vlan *v;
  839. struct net_bridge *br;
  840. int err;
  841. if (dev->priv_flags & IFF_EBRIDGE) {
  842. br = netdev_priv(dev);
  843. vg = br_vlan_group(br);
  844. } else {
  845. p = br_port_get_rtnl(dev);
  846. if (!p) {
  847. pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
  848. dev->name);
  849. return -EINVAL;
  850. }
  851. vg = nbp_vlan_group(p);
  852. br = p->br;
  853. }
  854. if (vid) {
  855. v = br_vlan_find(vg, vid);
  856. if (!v) {
  857. pr_info("bridge: RTM_DELNEIGH with unconfigured vlan %d on %s\n", vid, dev->name);
  858. return -EINVAL;
  859. }
  860. err = __br_fdb_delete(br, p, addr, vid);
  861. } else {
  862. err = -ENOENT;
  863. err &= __br_fdb_delete(br, p, addr, 0);
  864. if (!vg || !vg->num_vlans)
  865. return err;
  866. list_for_each_entry(v, &vg->vlan_list, vlist) {
  867. if (!br_vlan_should_use(v))
  868. continue;
  869. err &= __br_fdb_delete(br, p, addr, v->vid);
  870. }
  871. }
  872. return err;
  873. }
  874. int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
  875. {
  876. struct net_bridge_fdb_entry *f, *tmp;
  877. int err = 0;
  878. ASSERT_RTNL();
  879. /* the key here is that static entries change only under rtnl */
  880. rcu_read_lock();
  881. hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
  882. /* We only care for static entries */
  883. if (!f->is_static)
  884. continue;
  885. err = dev_uc_add(p->dev, f->key.addr.addr);
  886. if (err)
  887. goto rollback;
  888. }
  889. done:
  890. rcu_read_unlock();
  891. return err;
  892. rollback:
  893. hlist_for_each_entry_rcu(tmp, &br->fdb_list, fdb_node) {
  894. /* We only care for static entries */
  895. if (!tmp->is_static)
  896. continue;
  897. if (tmp == f)
  898. break;
  899. dev_uc_del(p->dev, tmp->key.addr.addr);
  900. }
  901. goto done;
  902. }
  903. void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
  904. {
  905. struct net_bridge_fdb_entry *f;
  906. ASSERT_RTNL();
  907. rcu_read_lock();
  908. hlist_for_each_entry_rcu(f, &br->fdb_list, fdb_node) {
  909. /* We only care for static entries */
  910. if (!f->is_static)
  911. continue;
  912. dev_uc_del(p->dev, f->key.addr.addr);
  913. }
  914. rcu_read_unlock();
  915. }
  916. int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
  917. const unsigned char *addr, u16 vid,
  918. bool swdev_notify)
  919. {
  920. struct net_bridge_fdb_entry *fdb;
  921. bool modified = false;
  922. int err = 0;
  923. trace_br_fdb_external_learn_add(br, p, addr, vid);
  924. spin_lock_bh(&br->hash_lock);
  925. fdb = br_fdb_find(br, addr, vid);
  926. if (!fdb) {
  927. fdb = fdb_create(br, p, addr, vid, 0, 0);
  928. if (!fdb) {
  929. err = -ENOMEM;
  930. goto err_unlock;
  931. }
  932. if (swdev_notify)
  933. fdb->added_by_user = 1;
  934. fdb->added_by_external_learn = 1;
  935. fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
  936. } else {
  937. fdb->updated = jiffies;
  938. if (fdb->dst != p) {
  939. fdb->dst = p;
  940. modified = true;
  941. }
  942. if (fdb->added_by_external_learn) {
  943. /* Refresh entry */
  944. fdb->used = jiffies;
  945. } else if (!fdb->added_by_user) {
  946. /* Take over SW learned entry */
  947. fdb->added_by_external_learn = 1;
  948. modified = true;
  949. }
  950. if (swdev_notify)
  951. fdb->added_by_user = 1;
  952. if (modified)
  953. fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
  954. }
  955. err_unlock:
  956. spin_unlock_bh(&br->hash_lock);
  957. return err;
  958. }
  959. int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
  960. const unsigned char *addr, u16 vid,
  961. bool swdev_notify)
  962. {
  963. struct net_bridge_fdb_entry *fdb;
  964. int err = 0;
  965. spin_lock_bh(&br->hash_lock);
  966. fdb = br_fdb_find(br, addr, vid);
  967. if (fdb && fdb->added_by_external_learn)
  968. fdb_delete(br, fdb, swdev_notify);
  969. else
  970. err = -ENOENT;
  971. spin_unlock_bh(&br->hash_lock);
  972. return err;
  973. }
  974. void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
  975. const unsigned char *addr, u16 vid)
  976. {
  977. struct net_bridge_fdb_entry *fdb;
  978. spin_lock_bh(&br->hash_lock);
  979. fdb = br_fdb_find(br, addr, vid);
  980. if (fdb)
  981. fdb->offloaded = 1;
  982. spin_unlock_bh(&br->hash_lock);
  983. }