br_fdb.c 25 KB

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