br_fdb.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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(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(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(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->updated = fdb->used = jiffies;
  410. hlist_add_head_rcu(&fdb->hlist, head);
  411. }
  412. return fdb;
  413. }
  414. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  415. const unsigned char *addr, u16 vid)
  416. {
  417. struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
  418. struct net_bridge_fdb_entry *fdb;
  419. if (!is_valid_ether_addr(addr))
  420. return -EINVAL;
  421. fdb = fdb_find(head, addr, vid);
  422. if (fdb) {
  423. /* it is okay to have multiple ports with same
  424. * address, just use the first one.
  425. */
  426. if (fdb->is_local)
  427. return 0;
  428. br_warn(br, "adding interface %s with same address "
  429. "as a received packet\n",
  430. source ? source->dev->name : br->dev->name);
  431. fdb_delete(br, fdb);
  432. }
  433. fdb = fdb_create(head, source, addr, vid);
  434. if (!fdb)
  435. return -ENOMEM;
  436. fdb->is_local = fdb->is_static = 1;
  437. fdb_add_hw(br, addr);
  438. fdb_notify(br, fdb, RTM_NEWNEIGH);
  439. return 0;
  440. }
  441. /* Add entry for local address of interface */
  442. int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  443. const unsigned char *addr, u16 vid)
  444. {
  445. int ret;
  446. spin_lock_bh(&br->hash_lock);
  447. ret = fdb_insert(br, source, addr, vid);
  448. spin_unlock_bh(&br->hash_lock);
  449. return ret;
  450. }
  451. void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
  452. const unsigned char *addr, u16 vid, bool added_by_user)
  453. {
  454. struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
  455. struct net_bridge_fdb_entry *fdb;
  456. bool fdb_modified = false;
  457. /* some users want to always flood. */
  458. if (hold_time(br) == 0)
  459. return;
  460. /* ignore packets unless we are using this port */
  461. if (!(source->state == BR_STATE_LEARNING ||
  462. source->state == BR_STATE_FORWARDING))
  463. return;
  464. fdb = fdb_find_rcu(head, addr, vid);
  465. if (likely(fdb)) {
  466. /* attempt to update an entry for a local interface */
  467. if (unlikely(fdb->is_local)) {
  468. if (net_ratelimit())
  469. br_warn(br, "received packet on %s with "
  470. "own address as source address\n",
  471. source->dev->name);
  472. } else {
  473. /* fastpath: update of existing entry */
  474. if (unlikely(source != fdb->dst)) {
  475. fdb->dst = source;
  476. fdb_modified = true;
  477. }
  478. fdb->updated = jiffies;
  479. if (unlikely(added_by_user))
  480. fdb->added_by_user = 1;
  481. if (unlikely(fdb_modified))
  482. fdb_notify(br, fdb, RTM_NEWNEIGH);
  483. }
  484. } else {
  485. spin_lock(&br->hash_lock);
  486. if (likely(!fdb_find(head, addr, vid))) {
  487. fdb = fdb_create(head, source, addr, vid);
  488. if (fdb) {
  489. if (unlikely(added_by_user))
  490. fdb->added_by_user = 1;
  491. fdb_notify(br, fdb, RTM_NEWNEIGH);
  492. }
  493. }
  494. /* else we lose race and someone else inserts
  495. * it first, don't bother updating
  496. */
  497. spin_unlock(&br->hash_lock);
  498. }
  499. }
  500. static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
  501. {
  502. if (fdb->is_local)
  503. return NUD_PERMANENT;
  504. else if (fdb->is_static)
  505. return NUD_NOARP;
  506. else if (has_expired(fdb->dst->br, fdb))
  507. return NUD_STALE;
  508. else
  509. return NUD_REACHABLE;
  510. }
  511. static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
  512. const struct net_bridge_fdb_entry *fdb,
  513. u32 portid, u32 seq, int type, unsigned int flags)
  514. {
  515. unsigned long now = jiffies;
  516. struct nda_cacheinfo ci;
  517. struct nlmsghdr *nlh;
  518. struct ndmsg *ndm;
  519. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  520. if (nlh == NULL)
  521. return -EMSGSIZE;
  522. ndm = nlmsg_data(nlh);
  523. ndm->ndm_family = AF_BRIDGE;
  524. ndm->ndm_pad1 = 0;
  525. ndm->ndm_pad2 = 0;
  526. ndm->ndm_flags = 0;
  527. ndm->ndm_type = 0;
  528. ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
  529. ndm->ndm_state = fdb_to_nud(fdb);
  530. if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
  531. goto nla_put_failure;
  532. if (nla_put_u32(skb, NDA_MASTER, br->dev->ifindex))
  533. goto nla_put_failure;
  534. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  535. ci.ndm_confirmed = 0;
  536. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  537. ci.ndm_refcnt = 0;
  538. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  539. goto nla_put_failure;
  540. if (fdb->vlan_id && nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
  541. goto nla_put_failure;
  542. return nlmsg_end(skb, nlh);
  543. nla_put_failure:
  544. nlmsg_cancel(skb, nlh);
  545. return -EMSGSIZE;
  546. }
  547. static inline size_t fdb_nlmsg_size(void)
  548. {
  549. return NLMSG_ALIGN(sizeof(struct ndmsg))
  550. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  551. + nla_total_size(sizeof(u32)) /* NDA_MASTER */
  552. + nla_total_size(sizeof(u16)) /* NDA_VLAN */
  553. + nla_total_size(sizeof(struct nda_cacheinfo));
  554. }
  555. static void fdb_notify(struct net_bridge *br,
  556. const struct net_bridge_fdb_entry *fdb, int type)
  557. {
  558. struct net *net = dev_net(br->dev);
  559. struct sk_buff *skb;
  560. int err = -ENOBUFS;
  561. skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
  562. if (skb == NULL)
  563. goto errout;
  564. err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
  565. if (err < 0) {
  566. /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
  567. WARN_ON(err == -EMSGSIZE);
  568. kfree_skb(skb);
  569. goto errout;
  570. }
  571. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  572. return;
  573. errout:
  574. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  575. }
  576. /* Dump information about entries, in response to GETNEIGH */
  577. int br_fdb_dump(struct sk_buff *skb,
  578. struct netlink_callback *cb,
  579. struct net_device *dev,
  580. struct net_device *filter_dev,
  581. int idx)
  582. {
  583. struct net_bridge *br = netdev_priv(dev);
  584. int i;
  585. if (!(dev->priv_flags & IFF_EBRIDGE))
  586. goto out;
  587. for (i = 0; i < BR_HASH_SIZE; i++) {
  588. struct net_bridge_fdb_entry *f;
  589. hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
  590. if (idx < cb->args[0])
  591. goto skip;
  592. if (filter_dev &&
  593. (!f->dst || f->dst->dev != filter_dev)) {
  594. if (filter_dev != dev)
  595. goto skip;
  596. /* !f->dst is a speacial case for bridge
  597. * It means the MAC belongs to the bridge
  598. * Therefore need a little more filtering
  599. * we only want to dump the !f->dst case
  600. */
  601. if (f->dst)
  602. goto skip;
  603. }
  604. if (fdb_fill_info(skb, br, f,
  605. NETLINK_CB(cb->skb).portid,
  606. cb->nlh->nlmsg_seq,
  607. RTM_NEWNEIGH,
  608. NLM_F_MULTI) < 0)
  609. break;
  610. skip:
  611. ++idx;
  612. }
  613. }
  614. out:
  615. return idx;
  616. }
  617. /* Update (create or replace) forwarding database entry */
  618. static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
  619. __u16 state, __u16 flags, __u16 vid)
  620. {
  621. struct net_bridge *br = source->br;
  622. struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
  623. struct net_bridge_fdb_entry *fdb;
  624. bool modified = false;
  625. fdb = fdb_find(head, addr, vid);
  626. if (fdb == NULL) {
  627. if (!(flags & NLM_F_CREATE))
  628. return -ENOENT;
  629. fdb = fdb_create(head, source, addr, vid);
  630. if (!fdb)
  631. return -ENOMEM;
  632. modified = true;
  633. } else {
  634. if (flags & NLM_F_EXCL)
  635. return -EEXIST;
  636. if (fdb->dst != source) {
  637. fdb->dst = source;
  638. modified = true;
  639. }
  640. }
  641. if (fdb_to_nud(fdb) != state) {
  642. if (state & NUD_PERMANENT) {
  643. fdb->is_local = 1;
  644. if (!fdb->is_static) {
  645. fdb->is_static = 1;
  646. fdb_add_hw(br, addr);
  647. }
  648. } else if (state & NUD_NOARP) {
  649. fdb->is_local = 0;
  650. if (!fdb->is_static) {
  651. fdb->is_static = 1;
  652. fdb_add_hw(br, addr);
  653. }
  654. } else {
  655. fdb->is_local = 0;
  656. if (fdb->is_static) {
  657. fdb->is_static = 0;
  658. fdb_del_hw(br, addr);
  659. }
  660. }
  661. modified = true;
  662. }
  663. fdb->added_by_user = 1;
  664. fdb->used = jiffies;
  665. if (modified) {
  666. fdb->updated = jiffies;
  667. fdb_notify(br, fdb, RTM_NEWNEIGH);
  668. }
  669. return 0;
  670. }
  671. static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge_port *p,
  672. const unsigned char *addr, u16 nlh_flags, u16 vid)
  673. {
  674. int err = 0;
  675. if (ndm->ndm_flags & NTF_USE) {
  676. rcu_read_lock();
  677. br_fdb_update(p->br, p, addr, vid, true);
  678. rcu_read_unlock();
  679. } else {
  680. spin_lock_bh(&p->br->hash_lock);
  681. err = fdb_add_entry(p, addr, ndm->ndm_state,
  682. nlh_flags, vid);
  683. spin_unlock_bh(&p->br->hash_lock);
  684. }
  685. return err;
  686. }
  687. /* Add new permanent fdb entry with RTM_NEWNEIGH */
  688. int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  689. struct net_device *dev,
  690. const unsigned char *addr, u16 nlh_flags)
  691. {
  692. struct net_bridge_port *p;
  693. int err = 0;
  694. struct net_port_vlans *pv;
  695. unsigned short vid = VLAN_N_VID;
  696. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
  697. pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
  698. return -EINVAL;
  699. }
  700. if (tb[NDA_VLAN]) {
  701. if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
  702. pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
  703. return -EINVAL;
  704. }
  705. vid = nla_get_u16(tb[NDA_VLAN]);
  706. if (!vid || vid >= VLAN_VID_MASK) {
  707. pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
  708. vid);
  709. return -EINVAL;
  710. }
  711. }
  712. if (is_zero_ether_addr(addr)) {
  713. pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
  714. return -EINVAL;
  715. }
  716. p = br_port_get_rtnl(dev);
  717. if (p == NULL) {
  718. pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
  719. dev->name);
  720. return -EINVAL;
  721. }
  722. pv = nbp_get_vlan_info(p);
  723. if (vid != VLAN_N_VID) {
  724. if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
  725. pr_info("bridge: RTM_NEWNEIGH with unconfigured "
  726. "vlan %d on port %s\n", vid, dev->name);
  727. return -EINVAL;
  728. }
  729. /* VID was specified, so use it. */
  730. err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
  731. } else {
  732. if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
  733. err = __br_fdb_add(ndm, p, addr, nlh_flags, 0);
  734. goto out;
  735. }
  736. /* We have vlans configured on this port and user didn't
  737. * specify a VLAN. To be nice, add/update entry for every
  738. * vlan on this port.
  739. */
  740. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
  741. err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
  742. if (err)
  743. goto out;
  744. }
  745. }
  746. out:
  747. return err;
  748. }
  749. static int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr, u16 vlan)
  750. {
  751. struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
  752. struct net_bridge_fdb_entry *fdb;
  753. fdb = fdb_find(head, addr, vlan);
  754. if (!fdb)
  755. return -ENOENT;
  756. fdb_delete(br, fdb);
  757. return 0;
  758. }
  759. static int __br_fdb_delete(struct net_bridge_port *p,
  760. const unsigned char *addr, u16 vid)
  761. {
  762. int err;
  763. spin_lock_bh(&p->br->hash_lock);
  764. err = fdb_delete_by_addr(p->br, addr, vid);
  765. spin_unlock_bh(&p->br->hash_lock);
  766. return err;
  767. }
  768. /* Remove neighbor entry with RTM_DELNEIGH */
  769. int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  770. struct net_device *dev,
  771. const unsigned char *addr)
  772. {
  773. struct net_bridge_port *p;
  774. int err;
  775. struct net_port_vlans *pv;
  776. unsigned short vid = VLAN_N_VID;
  777. if (tb[NDA_VLAN]) {
  778. if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
  779. pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
  780. return -EINVAL;
  781. }
  782. vid = nla_get_u16(tb[NDA_VLAN]);
  783. if (!vid || vid >= VLAN_VID_MASK) {
  784. pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
  785. vid);
  786. return -EINVAL;
  787. }
  788. }
  789. p = br_port_get_rtnl(dev);
  790. if (p == NULL) {
  791. pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
  792. dev->name);
  793. return -EINVAL;
  794. }
  795. pv = nbp_get_vlan_info(p);
  796. if (vid != VLAN_N_VID) {
  797. if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
  798. pr_info("bridge: RTM_DELNEIGH with unconfigured "
  799. "vlan %d on port %s\n", vid, dev->name);
  800. return -EINVAL;
  801. }
  802. err = __br_fdb_delete(p, addr, vid);
  803. } else {
  804. if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
  805. err = __br_fdb_delete(p, addr, 0);
  806. goto out;
  807. }
  808. /* We have vlans configured on this port and user didn't
  809. * specify a VLAN. To be nice, add/update entry for every
  810. * vlan on this port.
  811. */
  812. err = -ENOENT;
  813. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
  814. err &= __br_fdb_delete(p, addr, vid);
  815. }
  816. }
  817. out:
  818. return err;
  819. }
  820. int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
  821. {
  822. struct net_bridge_fdb_entry *fdb, *tmp;
  823. int i;
  824. int err;
  825. ASSERT_RTNL();
  826. for (i = 0; i < BR_HASH_SIZE; i++) {
  827. hlist_for_each_entry(fdb, &br->hash[i], hlist) {
  828. /* We only care for static entries */
  829. if (!fdb->is_static)
  830. continue;
  831. err = dev_uc_add(p->dev, fdb->addr.addr);
  832. if (err)
  833. goto rollback;
  834. }
  835. }
  836. return 0;
  837. rollback:
  838. for (i = 0; i < BR_HASH_SIZE; i++) {
  839. hlist_for_each_entry(tmp, &br->hash[i], hlist) {
  840. /* If we reached the fdb that failed, we can stop */
  841. if (tmp == fdb)
  842. break;
  843. /* We only care for static entries */
  844. if (!tmp->is_static)
  845. continue;
  846. dev_uc_del(p->dev, tmp->addr.addr);
  847. }
  848. }
  849. return err;
  850. }
  851. void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
  852. {
  853. struct net_bridge_fdb_entry *fdb;
  854. int i;
  855. ASSERT_RTNL();
  856. for (i = 0; i < BR_HASH_SIZE; i++) {
  857. hlist_for_each_entry_rcu(fdb, &br->hash[i], hlist) {
  858. /* We only care for static entries */
  859. if (!fdb->is_static)
  860. continue;
  861. dev_uc_del(p->dev, fdb->addr.addr);
  862. }
  863. }
  864. }