br_fdb.c 21 KB

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