br_fdb.c 27 KB

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