br_fdb.c 23 KB

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