br_fdb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. /* some users want to always flood. */
  416. if (hold_time(br) == 0)
  417. return;
  418. /* ignore packets unless we are using this port */
  419. if (!(source->state == BR_STATE_LEARNING ||
  420. source->state == BR_STATE_FORWARDING))
  421. return;
  422. fdb = fdb_find_rcu(head, addr, vid);
  423. if (likely(fdb)) {
  424. /* attempt to update an entry for a local interface */
  425. if (unlikely(fdb->is_local)) {
  426. if (net_ratelimit())
  427. br_warn(br, "received packet on %s with "
  428. "own address as source address\n",
  429. source->dev->name);
  430. } else {
  431. /* fastpath: update of existing entry */
  432. fdb->dst = source;
  433. fdb->updated = jiffies;
  434. if (unlikely(added_by_user))
  435. fdb->added_by_user = 1;
  436. }
  437. } else {
  438. spin_lock(&br->hash_lock);
  439. if (likely(!fdb_find(head, addr, vid))) {
  440. fdb = fdb_create(head, source, addr, vid);
  441. if (fdb) {
  442. if (unlikely(added_by_user))
  443. fdb->added_by_user = 1;
  444. fdb_notify(br, fdb, RTM_NEWNEIGH);
  445. }
  446. }
  447. /* else we lose race and someone else inserts
  448. * it first, don't bother updating
  449. */
  450. spin_unlock(&br->hash_lock);
  451. }
  452. }
  453. static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
  454. {
  455. if (fdb->is_local)
  456. return NUD_PERMANENT;
  457. else if (fdb->is_static)
  458. return NUD_NOARP;
  459. else if (has_expired(fdb->dst->br, fdb))
  460. return NUD_STALE;
  461. else
  462. return NUD_REACHABLE;
  463. }
  464. static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
  465. const struct net_bridge_fdb_entry *fdb,
  466. u32 portid, u32 seq, int type, unsigned int flags)
  467. {
  468. unsigned long now = jiffies;
  469. struct nda_cacheinfo ci;
  470. struct nlmsghdr *nlh;
  471. struct ndmsg *ndm;
  472. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  473. if (nlh == NULL)
  474. return -EMSGSIZE;
  475. ndm = nlmsg_data(nlh);
  476. ndm->ndm_family = AF_BRIDGE;
  477. ndm->ndm_pad1 = 0;
  478. ndm->ndm_pad2 = 0;
  479. ndm->ndm_flags = 0;
  480. ndm->ndm_type = 0;
  481. ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
  482. ndm->ndm_state = fdb_to_nud(fdb);
  483. if (nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr))
  484. goto nla_put_failure;
  485. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  486. ci.ndm_confirmed = 0;
  487. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  488. ci.ndm_refcnt = 0;
  489. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  490. goto nla_put_failure;
  491. if (nla_put(skb, NDA_VLAN, sizeof(u16), &fdb->vlan_id))
  492. goto nla_put_failure;
  493. return nlmsg_end(skb, nlh);
  494. nla_put_failure:
  495. nlmsg_cancel(skb, nlh);
  496. return -EMSGSIZE;
  497. }
  498. static inline size_t fdb_nlmsg_size(void)
  499. {
  500. return NLMSG_ALIGN(sizeof(struct ndmsg))
  501. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  502. + nla_total_size(sizeof(u16)) /* NDA_VLAN */
  503. + nla_total_size(sizeof(struct nda_cacheinfo));
  504. }
  505. static void fdb_notify(struct net_bridge *br,
  506. const struct net_bridge_fdb_entry *fdb, int type)
  507. {
  508. struct net *net = dev_net(br->dev);
  509. struct sk_buff *skb;
  510. int err = -ENOBUFS;
  511. skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
  512. if (skb == NULL)
  513. goto errout;
  514. err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
  515. if (err < 0) {
  516. /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
  517. WARN_ON(err == -EMSGSIZE);
  518. kfree_skb(skb);
  519. goto errout;
  520. }
  521. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  522. return;
  523. errout:
  524. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  525. }
  526. /* Dump information about entries, in response to GETNEIGH */
  527. int br_fdb_dump(struct sk_buff *skb,
  528. struct netlink_callback *cb,
  529. struct net_device *dev,
  530. int idx)
  531. {
  532. struct net_bridge *br = netdev_priv(dev);
  533. int i;
  534. if (!(dev->priv_flags & IFF_EBRIDGE))
  535. goto out;
  536. for (i = 0; i < BR_HASH_SIZE; i++) {
  537. struct net_bridge_fdb_entry *f;
  538. hlist_for_each_entry_rcu(f, &br->hash[i], hlist) {
  539. if (idx < cb->args[0])
  540. goto skip;
  541. if (fdb_fill_info(skb, br, f,
  542. NETLINK_CB(cb->skb).portid,
  543. cb->nlh->nlmsg_seq,
  544. RTM_NEWNEIGH,
  545. NLM_F_MULTI) < 0)
  546. break;
  547. skip:
  548. ++idx;
  549. }
  550. }
  551. out:
  552. return idx;
  553. }
  554. /* Update (create or replace) forwarding database entry */
  555. static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
  556. __u16 state, __u16 flags, __u16 vid)
  557. {
  558. struct net_bridge *br = source->br;
  559. struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
  560. struct net_bridge_fdb_entry *fdb;
  561. bool modified = false;
  562. fdb = fdb_find(head, addr, vid);
  563. if (fdb == NULL) {
  564. if (!(flags & NLM_F_CREATE))
  565. return -ENOENT;
  566. fdb = fdb_create(head, source, addr, vid);
  567. if (!fdb)
  568. return -ENOMEM;
  569. modified = true;
  570. } else {
  571. if (flags & NLM_F_EXCL)
  572. return -EEXIST;
  573. if (fdb->dst != source) {
  574. fdb->dst = source;
  575. modified = true;
  576. }
  577. }
  578. if (fdb_to_nud(fdb) != state) {
  579. if (state & NUD_PERMANENT)
  580. fdb->is_local = fdb->is_static = 1;
  581. else if (state & NUD_NOARP) {
  582. fdb->is_local = 0;
  583. fdb->is_static = 1;
  584. } else
  585. fdb->is_local = fdb->is_static = 0;
  586. modified = true;
  587. }
  588. fdb->added_by_user = 1;
  589. fdb->used = jiffies;
  590. if (modified) {
  591. fdb->updated = jiffies;
  592. fdb_notify(br, fdb, RTM_NEWNEIGH);
  593. }
  594. return 0;
  595. }
  596. static int __br_fdb_add(struct ndmsg *ndm, struct net_bridge_port *p,
  597. const unsigned char *addr, u16 nlh_flags, u16 vid)
  598. {
  599. int err = 0;
  600. if (ndm->ndm_flags & NTF_USE) {
  601. rcu_read_lock();
  602. br_fdb_update(p->br, p, addr, vid, true);
  603. rcu_read_unlock();
  604. } else {
  605. spin_lock_bh(&p->br->hash_lock);
  606. err = fdb_add_entry(p, addr, ndm->ndm_state,
  607. nlh_flags, vid);
  608. spin_unlock_bh(&p->br->hash_lock);
  609. }
  610. return err;
  611. }
  612. /* Add new permanent fdb entry with RTM_NEWNEIGH */
  613. int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  614. struct net_device *dev,
  615. const unsigned char *addr, u16 nlh_flags)
  616. {
  617. struct net_bridge_port *p;
  618. int err = 0;
  619. struct net_port_vlans *pv;
  620. unsigned short vid = VLAN_N_VID;
  621. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
  622. pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
  623. return -EINVAL;
  624. }
  625. if (tb[NDA_VLAN]) {
  626. if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
  627. pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
  628. return -EINVAL;
  629. }
  630. vid = nla_get_u16(tb[NDA_VLAN]);
  631. if (!vid || vid >= VLAN_VID_MASK) {
  632. pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
  633. vid);
  634. return -EINVAL;
  635. }
  636. }
  637. if (is_zero_ether_addr(addr)) {
  638. pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
  639. return -EINVAL;
  640. }
  641. p = br_port_get_rtnl(dev);
  642. if (p == NULL) {
  643. pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
  644. dev->name);
  645. return -EINVAL;
  646. }
  647. pv = nbp_get_vlan_info(p);
  648. if (vid != VLAN_N_VID) {
  649. if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
  650. pr_info("bridge: RTM_NEWNEIGH with unconfigured "
  651. "vlan %d on port %s\n", vid, dev->name);
  652. return -EINVAL;
  653. }
  654. /* VID was specified, so use it. */
  655. err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
  656. } else {
  657. if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
  658. err = __br_fdb_add(ndm, p, addr, nlh_flags, 0);
  659. goto out;
  660. }
  661. /* We have vlans configured on this port and user didn't
  662. * specify a VLAN. To be nice, add/update entry for every
  663. * vlan on this port.
  664. */
  665. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
  666. err = __br_fdb_add(ndm, p, addr, nlh_flags, vid);
  667. if (err)
  668. goto out;
  669. }
  670. }
  671. out:
  672. return err;
  673. }
  674. static int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr, u16 vlan)
  675. {
  676. struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
  677. struct net_bridge_fdb_entry *fdb;
  678. fdb = fdb_find(head, addr, vlan);
  679. if (!fdb)
  680. return -ENOENT;
  681. fdb_delete(br, fdb);
  682. return 0;
  683. }
  684. static int __br_fdb_delete(struct net_bridge_port *p,
  685. const unsigned char *addr, u16 vid)
  686. {
  687. int err;
  688. spin_lock_bh(&p->br->hash_lock);
  689. err = fdb_delete_by_addr(p->br, addr, vid);
  690. spin_unlock_bh(&p->br->hash_lock);
  691. return err;
  692. }
  693. /* Remove neighbor entry with RTM_DELNEIGH */
  694. int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  695. struct net_device *dev,
  696. const unsigned char *addr)
  697. {
  698. struct net_bridge_port *p;
  699. int err;
  700. struct net_port_vlans *pv;
  701. unsigned short vid = VLAN_N_VID;
  702. if (tb[NDA_VLAN]) {
  703. if (nla_len(tb[NDA_VLAN]) != sizeof(unsigned short)) {
  704. pr_info("bridge: RTM_NEWNEIGH with invalid vlan\n");
  705. return -EINVAL;
  706. }
  707. vid = nla_get_u16(tb[NDA_VLAN]);
  708. if (!vid || vid >= VLAN_VID_MASK) {
  709. pr_info("bridge: RTM_NEWNEIGH with invalid vlan id %d\n",
  710. vid);
  711. return -EINVAL;
  712. }
  713. }
  714. p = br_port_get_rtnl(dev);
  715. if (p == NULL) {
  716. pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
  717. dev->name);
  718. return -EINVAL;
  719. }
  720. pv = nbp_get_vlan_info(p);
  721. if (vid != VLAN_N_VID) {
  722. if (!pv || !test_bit(vid, pv->vlan_bitmap)) {
  723. pr_info("bridge: RTM_DELNEIGH with unconfigured "
  724. "vlan %d on port %s\n", vid, dev->name);
  725. return -EINVAL;
  726. }
  727. err = __br_fdb_delete(p, addr, vid);
  728. } else {
  729. if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID)) {
  730. err = __br_fdb_delete(p, addr, 0);
  731. goto out;
  732. }
  733. /* We have vlans configured on this port and user didn't
  734. * specify a VLAN. To be nice, add/update entry for every
  735. * vlan on this port.
  736. */
  737. err = -ENOENT;
  738. for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
  739. err &= __br_fdb_delete(p, addr, vid);
  740. }
  741. }
  742. out:
  743. return err;
  744. }