br_fdb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. local_bh_disable();
  683. rcu_read_lock();
  684. br_fdb_update(p->br, p, addr, vid, true);
  685. rcu_read_unlock();
  686. local_bh_enable();
  687. } else {
  688. spin_lock_bh(&p->br->hash_lock);
  689. err = fdb_add_entry(p, addr, ndm->ndm_state,
  690. nlh_flags, vid);
  691. spin_unlock_bh(&p->br->hash_lock);
  692. }
  693. return err;
  694. }
  695. /* Add new permanent fdb entry with RTM_NEWNEIGH */
  696. int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  697. struct net_device *dev,
  698. const unsigned char *addr, u16 vid, u16 nlh_flags)
  699. {
  700. struct net_bridge_port *p;
  701. int err = 0;
  702. struct net_port_vlans *pv;
  703. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
  704. pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
  705. return -EINVAL;
  706. }
  707. if (is_zero_ether_addr(addr)) {
  708. pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
  709. return -EINVAL;
  710. }
  711. p = br_port_get_rtnl(dev);
  712. if (p == NULL) {
  713. pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
  714. dev->name);
  715. return -EINVAL;
  716. }
  717. pv = nbp_get_vlan_info(p);
  718. if (vid) {
  719. if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
  720. pr_info("bridge: RTM_NEWNEIGH with unconfigured "
  721. "vlan %d on port %s\n", vid, dev->name);
  722. return -EINVAL;
  723. }
  724. /* VID was specified, so use it. */
  725. err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
  726. } else {
  727. err = __br_fdb_add(ndm, p, addr, nlh_flags, 0);
  728. if (err || !pv)
  729. goto out;
  730. /* We have vlans configured on this port and user didn't
  731. * specify a VLAN. To be nice, add/update entry for every
  732. * vlan on this port.
  733. */
  734. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
  735. err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
  736. if (err)
  737. goto out;
  738. }
  739. }
  740. out:
  741. return err;
  742. }
  743. static int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr, u16 vlan)
  744. {
  745. struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
  746. struct net_bridge_fdb_entry *fdb;
  747. fdb = fdb_find(head, addr, vlan);
  748. if (!fdb)
  749. return -ENOENT;
  750. fdb_delete(br, fdb);
  751. return 0;
  752. }
  753. static int __br_fdb_delete(struct net_bridge_port *p,
  754. const unsigned char *addr, u16 vid)
  755. {
  756. int err;
  757. spin_lock_bh(&p->br->hash_lock);
  758. err = fdb_delete_by_addr(p->br, addr, vid);
  759. spin_unlock_bh(&p->br->hash_lock);
  760. return err;
  761. }
  762. /* Remove neighbor entry with RTM_DELNEIGH */
  763. int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  764. struct net_device *dev,
  765. const unsigned char *addr, u16 vid)
  766. {
  767. struct net_bridge_port *p;
  768. int err;
  769. struct net_port_vlans *pv;
  770. p = br_port_get_rtnl(dev);
  771. if (p == NULL) {
  772. pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
  773. dev->name);
  774. return -EINVAL;
  775. }
  776. pv = nbp_get_vlan_info(p);
  777. if (vid) {
  778. if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
  779. pr_info("bridge: RTM_DELNEIGH with unconfigured "
  780. "vlan %d on port %s\n", vid, dev->name);
  781. return -EINVAL;
  782. }
  783. err = __br_fdb_delete(p, addr, vid);
  784. } else {
  785. err = -ENOENT;
  786. err &= __br_fdb_delete(p, addr, 0);
  787. if (!pv)
  788. goto out;
  789. /* We have vlans configured on this port and user didn't
  790. * specify a VLAN. To be nice, add/update entry for every
  791. * vlan on this port.
  792. */
  793. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
  794. err &= __br_fdb_delete(p, addr, vid);
  795. }
  796. }
  797. out:
  798. return err;
  799. }
  800. int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
  801. {
  802. struct net_bridge_fdb_entry *fdb, *tmp;
  803. int i;
  804. int err;
  805. ASSERT_RTNL();
  806. for (i = 0; i < BR_HASH_SIZE; i++) {
  807. hlist_for_each_entry(fdb, &br->hash[i], hlist) {
  808. /* We only care for static entries */
  809. if (!fdb->is_static)
  810. continue;
  811. err = dev_uc_add(p->dev, fdb->addr.addr);
  812. if (err)
  813. goto rollback;
  814. }
  815. }
  816. return 0;
  817. rollback:
  818. for (i = 0; i < BR_HASH_SIZE; i++) {
  819. hlist_for_each_entry(tmp, &br->hash[i], hlist) {
  820. /* If we reached the fdb that failed, we can stop */
  821. if (tmp == fdb)
  822. break;
  823. /* We only care for static entries */
  824. if (!tmp->is_static)
  825. continue;
  826. dev_uc_del(p->dev, tmp->addr.addr);
  827. }
  828. }
  829. return err;
  830. }
  831. void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
  832. {
  833. struct net_bridge_fdb_entry *fdb;
  834. int i;
  835. ASSERT_RTNL();
  836. for (i = 0; i < BR_HASH_SIZE; i++) {
  837. hlist_for_each_entry_rcu(fdb, &br->hash[i], hlist) {
  838. /* We only care for static entries */
  839. if (!fdb->is_static)
  840. continue;
  841. dev_uc_del(p->dev, fdb->addr.addr);
  842. }
  843. }
  844. }
  845. int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
  846. const unsigned char *addr, u16 vid)
  847. {
  848. struct hlist_head *head;
  849. struct net_bridge_fdb_entry *fdb;
  850. int err = 0;
  851. ASSERT_RTNL();
  852. spin_lock_bh(&br->hash_lock);
  853. head = &br->hash[br_mac_hash(addr, vid)];
  854. fdb = fdb_find(head, addr, vid);
  855. if (!fdb) {
  856. fdb = fdb_create(head, p, addr, vid);
  857. if (!fdb) {
  858. err = -ENOMEM;
  859. goto err_unlock;
  860. }
  861. fdb->added_by_external_learn = 1;
  862. fdb_notify(br, fdb, RTM_NEWNEIGH);
  863. } else if (fdb->added_by_external_learn) {
  864. /* Refresh entry */
  865. fdb->updated = fdb->used = jiffies;
  866. } else if (!fdb->added_by_user) {
  867. /* Take over SW learned entry */
  868. fdb->added_by_external_learn = 1;
  869. fdb->updated = jiffies;
  870. fdb_notify(br, fdb, RTM_NEWNEIGH);
  871. }
  872. err_unlock:
  873. spin_unlock_bh(&br->hash_lock);
  874. return err;
  875. }
  876. int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
  877. const unsigned char *addr, u16 vid)
  878. {
  879. struct hlist_head *head;
  880. struct net_bridge_fdb_entry *fdb;
  881. int err = 0;
  882. ASSERT_RTNL();
  883. spin_lock_bh(&br->hash_lock);
  884. head = &br->hash[br_mac_hash(addr, vid)];
  885. fdb = fdb_find(head, addr, vid);
  886. if (fdb && fdb->added_by_external_learn)
  887. fdb_delete(br, fdb);
  888. else
  889. err = -ENOENT;
  890. spin_unlock_bh(&br->hash_lock);
  891. return err;
  892. }