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