br_fdb.c 24 KB

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