br_fdb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 "br_private.h"
  26. static struct kmem_cache *br_fdb_cache __read_mostly;
  27. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  28. const unsigned char *addr);
  29. static void fdb_notify(struct net_bridge *br,
  30. const struct net_bridge_fdb_entry *, int);
  31. static u32 fdb_salt __read_mostly;
  32. int __init br_fdb_init(void)
  33. {
  34. br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
  35. sizeof(struct net_bridge_fdb_entry),
  36. 0,
  37. SLAB_HWCACHE_ALIGN, NULL);
  38. if (!br_fdb_cache)
  39. return -ENOMEM;
  40. get_random_bytes(&fdb_salt, sizeof(fdb_salt));
  41. return 0;
  42. }
  43. void br_fdb_fini(void)
  44. {
  45. kmem_cache_destroy(br_fdb_cache);
  46. }
  47. /* if topology_changing then use forward_delay (default 15 sec)
  48. * otherwise keep longer (default 5 minutes)
  49. */
  50. static inline unsigned long hold_time(const struct net_bridge *br)
  51. {
  52. return br->topology_change ? br->forward_delay : br->ageing_time;
  53. }
  54. static inline int has_expired(const struct net_bridge *br,
  55. const struct net_bridge_fdb_entry *fdb)
  56. {
  57. return !fdb->is_static &&
  58. time_before_eq(fdb->updated + hold_time(br), jiffies);
  59. }
  60. static inline int br_mac_hash(const unsigned char *mac)
  61. {
  62. /* use 1 byte of OUI cnd 3 bytes of NIC */
  63. u32 key = get_unaligned((u32 *)(mac + 2));
  64. return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1);
  65. }
  66. static void fdb_rcu_free(struct rcu_head *head)
  67. {
  68. struct net_bridge_fdb_entry *ent
  69. = container_of(head, struct net_bridge_fdb_entry, rcu);
  70. kmem_cache_free(br_fdb_cache, ent);
  71. }
  72. static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
  73. {
  74. hlist_del_rcu(&f->hlist);
  75. fdb_notify(br, f, RTM_DELNEIGH);
  76. call_rcu(&f->rcu, fdb_rcu_free);
  77. }
  78. void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
  79. {
  80. struct net_bridge *br = p->br;
  81. int i;
  82. spin_lock_bh(&br->hash_lock);
  83. /* Search all chains since old address/hash is unknown */
  84. for (i = 0; i < BR_HASH_SIZE; i++) {
  85. struct hlist_node *h;
  86. hlist_for_each(h, &br->hash[i]) {
  87. struct net_bridge_fdb_entry *f;
  88. f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  89. if (f->dst == p && f->is_local) {
  90. /* maybe another port has same hw addr? */
  91. struct net_bridge_port *op;
  92. list_for_each_entry(op, &br->port_list, list) {
  93. if (op != p &&
  94. !compare_ether_addr(op->dev->dev_addr,
  95. f->addr.addr)) {
  96. f->dst = op;
  97. goto insert;
  98. }
  99. }
  100. /* delete old one */
  101. fdb_delete(br, f);
  102. goto insert;
  103. }
  104. }
  105. }
  106. insert:
  107. /* insert new address, may fail if invalid address or dup. */
  108. fdb_insert(br, p, newaddr);
  109. spin_unlock_bh(&br->hash_lock);
  110. }
  111. void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
  112. {
  113. struct net_bridge_fdb_entry *f;
  114. /* If old entry was unassociated with any port, then delete it. */
  115. f = __br_fdb_get(br, br->dev->dev_addr);
  116. if (f && f->is_local && !f->dst)
  117. fdb_delete(br, f);
  118. fdb_insert(br, NULL, newaddr);
  119. }
  120. void br_fdb_cleanup(unsigned long _data)
  121. {
  122. struct net_bridge *br = (struct net_bridge *)_data;
  123. unsigned long delay = hold_time(br);
  124. unsigned long next_timer = jiffies + br->ageing_time;
  125. int i;
  126. spin_lock(&br->hash_lock);
  127. for (i = 0; i < BR_HASH_SIZE; i++) {
  128. struct net_bridge_fdb_entry *f;
  129. struct hlist_node *h, *n;
  130. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  131. unsigned long this_timer;
  132. if (f->is_static)
  133. continue;
  134. this_timer = f->updated + delay;
  135. if (time_before_eq(this_timer, jiffies))
  136. fdb_delete(br, f);
  137. else if (time_before(this_timer, next_timer))
  138. next_timer = this_timer;
  139. }
  140. }
  141. spin_unlock(&br->hash_lock);
  142. mod_timer(&br->gc_timer, round_jiffies_up(next_timer));
  143. }
  144. /* Completely flush all dynamic entries in forwarding database.*/
  145. void br_fdb_flush(struct net_bridge *br)
  146. {
  147. int i;
  148. spin_lock_bh(&br->hash_lock);
  149. for (i = 0; i < BR_HASH_SIZE; i++) {
  150. struct net_bridge_fdb_entry *f;
  151. struct hlist_node *h, *n;
  152. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  153. if (!f->is_static)
  154. fdb_delete(br, f);
  155. }
  156. }
  157. spin_unlock_bh(&br->hash_lock);
  158. }
  159. /* Flush all entries referring to a specific port.
  160. * if do_all is set also flush static entries
  161. */
  162. void br_fdb_delete_by_port(struct net_bridge *br,
  163. const struct net_bridge_port *p,
  164. int do_all)
  165. {
  166. int i;
  167. spin_lock_bh(&br->hash_lock);
  168. for (i = 0; i < BR_HASH_SIZE; i++) {
  169. struct hlist_node *h, *g;
  170. hlist_for_each_safe(h, g, &br->hash[i]) {
  171. struct net_bridge_fdb_entry *f
  172. = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  173. if (f->dst != p)
  174. continue;
  175. if (f->is_static && !do_all)
  176. continue;
  177. /*
  178. * if multiple ports all have the same device address
  179. * then when one port is deleted, assign
  180. * the local entry to other port
  181. */
  182. if (f->is_local) {
  183. struct net_bridge_port *op;
  184. list_for_each_entry(op, &br->port_list, list) {
  185. if (op != p &&
  186. !compare_ether_addr(op->dev->dev_addr,
  187. f->addr.addr)) {
  188. f->dst = op;
  189. goto skip_delete;
  190. }
  191. }
  192. }
  193. fdb_delete(br, f);
  194. skip_delete: ;
  195. }
  196. }
  197. spin_unlock_bh(&br->hash_lock);
  198. }
  199. /* No locking or refcounting, assumes caller has rcu_read_lock */
  200. struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
  201. const unsigned char *addr)
  202. {
  203. struct hlist_node *h;
  204. struct net_bridge_fdb_entry *fdb;
  205. hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
  206. if (!compare_ether_addr(fdb->addr.addr, addr)) {
  207. if (unlikely(has_expired(br, fdb)))
  208. break;
  209. return fdb;
  210. }
  211. }
  212. return NULL;
  213. }
  214. #if IS_ENABLED(CONFIG_ATM_LANE)
  215. /* Interface used by ATM LANE hook to test
  216. * if an addr is on some other bridge port */
  217. int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
  218. {
  219. struct net_bridge_fdb_entry *fdb;
  220. struct net_bridge_port *port;
  221. int ret;
  222. rcu_read_lock();
  223. port = br_port_get_rcu(dev);
  224. if (!port)
  225. ret = 0;
  226. else {
  227. fdb = __br_fdb_get(port->br, addr);
  228. ret = fdb && fdb->dst && fdb->dst->dev != dev &&
  229. fdb->dst->state == BR_STATE_FORWARDING;
  230. }
  231. rcu_read_unlock();
  232. return ret;
  233. }
  234. #endif /* CONFIG_ATM_LANE */
  235. /*
  236. * Fill buffer with forwarding table records in
  237. * the API format.
  238. */
  239. int br_fdb_fillbuf(struct net_bridge *br, void *buf,
  240. unsigned long maxnum, unsigned long skip)
  241. {
  242. struct __fdb_entry *fe = buf;
  243. int i, num = 0;
  244. struct hlist_node *h;
  245. struct net_bridge_fdb_entry *f;
  246. memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
  247. rcu_read_lock();
  248. for (i = 0; i < BR_HASH_SIZE; i++) {
  249. hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
  250. if (num >= maxnum)
  251. goto out;
  252. if (has_expired(br, f))
  253. continue;
  254. /* ignore pseudo entry for local MAC address */
  255. if (!f->dst)
  256. continue;
  257. if (skip) {
  258. --skip;
  259. continue;
  260. }
  261. /* convert from internal format to API */
  262. memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
  263. /* due to ABI compat need to split into hi/lo */
  264. fe->port_no = f->dst->port_no;
  265. fe->port_hi = f->dst->port_no >> 8;
  266. fe->is_local = f->is_local;
  267. if (!f->is_static)
  268. fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->updated);
  269. ++fe;
  270. ++num;
  271. }
  272. }
  273. out:
  274. rcu_read_unlock();
  275. return num;
  276. }
  277. static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
  278. const unsigned char *addr)
  279. {
  280. struct hlist_node *h;
  281. struct net_bridge_fdb_entry *fdb;
  282. hlist_for_each_entry(fdb, h, head, hlist) {
  283. if (!compare_ether_addr(fdb->addr.addr, addr))
  284. return fdb;
  285. }
  286. return NULL;
  287. }
  288. static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
  289. const unsigned char *addr)
  290. {
  291. struct hlist_node *h;
  292. struct net_bridge_fdb_entry *fdb;
  293. hlist_for_each_entry_rcu(fdb, h, head, hlist) {
  294. if (!compare_ether_addr(fdb->addr.addr, addr))
  295. return fdb;
  296. }
  297. return NULL;
  298. }
  299. static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
  300. struct net_bridge_port *source,
  301. const unsigned char *addr)
  302. {
  303. struct net_bridge_fdb_entry *fdb;
  304. fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
  305. if (fdb) {
  306. memcpy(fdb->addr.addr, addr, ETH_ALEN);
  307. fdb->dst = source;
  308. fdb->is_local = 0;
  309. fdb->is_static = 0;
  310. fdb->updated = fdb->used = jiffies;
  311. hlist_add_head_rcu(&fdb->hlist, head);
  312. }
  313. return fdb;
  314. }
  315. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  316. const unsigned char *addr)
  317. {
  318. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  319. struct net_bridge_fdb_entry *fdb;
  320. if (!is_valid_ether_addr(addr))
  321. return -EINVAL;
  322. fdb = fdb_find(head, addr);
  323. if (fdb) {
  324. /* it is okay to have multiple ports with same
  325. * address, just use the first one.
  326. */
  327. if (fdb->is_local)
  328. return 0;
  329. br_warn(br, "adding interface %s with same address "
  330. "as a received packet\n",
  331. source->dev->name);
  332. fdb_delete(br, fdb);
  333. }
  334. fdb = fdb_create(head, source, addr);
  335. if (!fdb)
  336. return -ENOMEM;
  337. fdb->is_local = fdb->is_static = 1;
  338. fdb_notify(br, fdb, RTM_NEWNEIGH);
  339. return 0;
  340. }
  341. /* Add entry for local address of interface */
  342. int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  343. const unsigned char *addr)
  344. {
  345. int ret;
  346. spin_lock_bh(&br->hash_lock);
  347. ret = fdb_insert(br, source, addr);
  348. spin_unlock_bh(&br->hash_lock);
  349. return ret;
  350. }
  351. void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
  352. const unsigned char *addr)
  353. {
  354. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  355. struct net_bridge_fdb_entry *fdb;
  356. /* some users want to always flood. */
  357. if (hold_time(br) == 0)
  358. return;
  359. /* ignore packets unless we are using this port */
  360. if (!(source->state == BR_STATE_LEARNING ||
  361. source->state == BR_STATE_FORWARDING))
  362. return;
  363. fdb = fdb_find_rcu(head, addr);
  364. if (likely(fdb)) {
  365. /* attempt to update an entry for a local interface */
  366. if (unlikely(fdb->is_local)) {
  367. if (net_ratelimit())
  368. br_warn(br, "received packet on %s with "
  369. "own address as source address\n",
  370. source->dev->name);
  371. } else {
  372. /* fastpath: update of existing entry */
  373. fdb->dst = source;
  374. fdb->updated = jiffies;
  375. }
  376. } else {
  377. spin_lock_bh(&br->hash_lock);
  378. if (likely(!fdb_find(head, addr))) {
  379. fdb = fdb_create(head, source, addr);
  380. if (fdb)
  381. fdb_notify(br, fdb, RTM_NEWNEIGH);
  382. }
  383. /* else we lose race and someone else inserts
  384. * it first, don't bother updating
  385. */
  386. spin_unlock_bh(&br->hash_lock);
  387. }
  388. }
  389. static int fdb_to_nud(const struct net_bridge_fdb_entry *fdb)
  390. {
  391. if (fdb->is_local)
  392. return NUD_PERMANENT;
  393. else if (fdb->is_static)
  394. return NUD_NOARP;
  395. else if (has_expired(fdb->dst->br, fdb))
  396. return NUD_STALE;
  397. else
  398. return NUD_REACHABLE;
  399. }
  400. static int fdb_fill_info(struct sk_buff *skb, const struct net_bridge *br,
  401. const struct net_bridge_fdb_entry *fdb,
  402. u32 pid, u32 seq, int type, unsigned int flags)
  403. {
  404. unsigned long now = jiffies;
  405. struct nda_cacheinfo ci;
  406. struct nlmsghdr *nlh;
  407. struct ndmsg *ndm;
  408. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), flags);
  409. if (nlh == NULL)
  410. return -EMSGSIZE;
  411. ndm = nlmsg_data(nlh);
  412. ndm->ndm_family = AF_BRIDGE;
  413. ndm->ndm_pad1 = 0;
  414. ndm->ndm_pad2 = 0;
  415. ndm->ndm_flags = 0;
  416. ndm->ndm_type = 0;
  417. ndm->ndm_ifindex = fdb->dst ? fdb->dst->dev->ifindex : br->dev->ifindex;
  418. ndm->ndm_state = fdb_to_nud(fdb);
  419. NLA_PUT(skb, NDA_LLADDR, ETH_ALEN, &fdb->addr);
  420. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  421. ci.ndm_confirmed = 0;
  422. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  423. ci.ndm_refcnt = 0;
  424. NLA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci);
  425. return nlmsg_end(skb, nlh);
  426. nla_put_failure:
  427. nlmsg_cancel(skb, nlh);
  428. return -EMSGSIZE;
  429. }
  430. static inline size_t fdb_nlmsg_size(void)
  431. {
  432. return NLMSG_ALIGN(sizeof(struct ndmsg))
  433. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  434. + nla_total_size(sizeof(struct nda_cacheinfo));
  435. }
  436. static void fdb_notify(struct net_bridge *br,
  437. const struct net_bridge_fdb_entry *fdb, int type)
  438. {
  439. struct net *net = dev_net(br->dev);
  440. struct sk_buff *skb;
  441. int err = -ENOBUFS;
  442. skb = nlmsg_new(fdb_nlmsg_size(), GFP_ATOMIC);
  443. if (skb == NULL)
  444. goto errout;
  445. err = fdb_fill_info(skb, br, fdb, 0, 0, type, 0);
  446. if (err < 0) {
  447. /* -EMSGSIZE implies BUG in fdb_nlmsg_size() */
  448. WARN_ON(err == -EMSGSIZE);
  449. kfree_skb(skb);
  450. goto errout;
  451. }
  452. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  453. return;
  454. errout:
  455. if (err < 0)
  456. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  457. }
  458. /* Dump information about entries, in response to GETNEIGH */
  459. int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
  460. {
  461. struct net *net = sock_net(skb->sk);
  462. struct net_device *dev;
  463. int idx = 0;
  464. rcu_read_lock();
  465. for_each_netdev_rcu(net, dev) {
  466. struct net_bridge *br = netdev_priv(dev);
  467. int i;
  468. if (!(dev->priv_flags & IFF_EBRIDGE))
  469. continue;
  470. for (i = 0; i < BR_HASH_SIZE; i++) {
  471. struct hlist_node *h;
  472. struct net_bridge_fdb_entry *f;
  473. hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
  474. if (idx < cb->args[0])
  475. goto skip;
  476. if (fdb_fill_info(skb, br, f,
  477. NETLINK_CB(cb->skb).pid,
  478. cb->nlh->nlmsg_seq,
  479. RTM_NEWNEIGH,
  480. NLM_F_MULTI) < 0)
  481. break;
  482. skip:
  483. ++idx;
  484. }
  485. }
  486. }
  487. rcu_read_unlock();
  488. cb->args[0] = idx;
  489. return skb->len;
  490. }
  491. /* Update (create or replace) forwarding database entry */
  492. static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
  493. __u16 state, __u16 flags)
  494. {
  495. struct net_bridge *br = source->br;
  496. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  497. struct net_bridge_fdb_entry *fdb;
  498. fdb = fdb_find(head, addr);
  499. if (fdb == NULL) {
  500. if (!(flags & NLM_F_CREATE))
  501. return -ENOENT;
  502. fdb = fdb_create(head, source, addr);
  503. if (!fdb)
  504. return -ENOMEM;
  505. fdb_notify(br, fdb, RTM_NEWNEIGH);
  506. } else {
  507. if (flags & NLM_F_EXCL)
  508. return -EEXIST;
  509. }
  510. if (fdb_to_nud(fdb) != state) {
  511. if (state & NUD_PERMANENT)
  512. fdb->is_local = fdb->is_static = 1;
  513. else if (state & NUD_NOARP) {
  514. fdb->is_local = 0;
  515. fdb->is_static = 1;
  516. } else
  517. fdb->is_local = fdb->is_static = 0;
  518. fdb->updated = fdb->used = jiffies;
  519. fdb_notify(br, fdb, RTM_NEWNEIGH);
  520. }
  521. return 0;
  522. }
  523. /* Add new permanent fdb entry with RTM_NEWNEIGH */
  524. int br_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  525. {
  526. struct net *net = sock_net(skb->sk);
  527. struct ndmsg *ndm;
  528. struct nlattr *tb[NDA_MAX+1];
  529. struct net_device *dev;
  530. struct net_bridge_port *p;
  531. const __u8 *addr;
  532. int err;
  533. ASSERT_RTNL();
  534. err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
  535. if (err < 0)
  536. return err;
  537. ndm = nlmsg_data(nlh);
  538. if (ndm->ndm_ifindex == 0) {
  539. pr_info("bridge: RTM_NEWNEIGH with invalid ifindex\n");
  540. return -EINVAL;
  541. }
  542. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  543. if (dev == NULL) {
  544. pr_info("bridge: RTM_NEWNEIGH with unknown ifindex\n");
  545. return -ENODEV;
  546. }
  547. if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
  548. pr_info("bridge: RTM_NEWNEIGH with invalid address\n");
  549. return -EINVAL;
  550. }
  551. addr = nla_data(tb[NDA_LLADDR]);
  552. if (!is_valid_ether_addr(addr)) {
  553. pr_info("bridge: RTM_NEWNEIGH with invalid ether address\n");
  554. return -EINVAL;
  555. }
  556. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE))) {
  557. pr_info("bridge: RTM_NEWNEIGH with invalid state %#x\n", ndm->ndm_state);
  558. return -EINVAL;
  559. }
  560. p = br_port_get_rtnl(dev);
  561. if (p == NULL) {
  562. pr_info("bridge: RTM_NEWNEIGH %s not a bridge port\n",
  563. dev->name);
  564. return -EINVAL;
  565. }
  566. if (ndm->ndm_flags & NTF_USE) {
  567. local_bh_disable();
  568. rcu_read_lock();
  569. br_fdb_update(p->br, p, addr);
  570. rcu_read_unlock();
  571. local_bh_enable();
  572. } else {
  573. spin_lock_bh(&p->br->hash_lock);
  574. err = fdb_add_entry(p, addr, ndm->ndm_state, nlh->nlmsg_flags);
  575. spin_unlock_bh(&p->br->hash_lock);
  576. }
  577. return err;
  578. }
  579. static int fdb_delete_by_addr(struct net_bridge_port *p, const u8 *addr)
  580. {
  581. struct net_bridge *br = p->br;
  582. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  583. struct net_bridge_fdb_entry *fdb;
  584. fdb = fdb_find(head, addr);
  585. if (!fdb)
  586. return -ENOENT;
  587. fdb_delete(p->br, fdb);
  588. return 0;
  589. }
  590. /* Remove neighbor entry with RTM_DELNEIGH */
  591. int br_fdb_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  592. {
  593. struct net *net = sock_net(skb->sk);
  594. struct ndmsg *ndm;
  595. struct net_bridge_port *p;
  596. struct nlattr *llattr;
  597. const __u8 *addr;
  598. struct net_device *dev;
  599. int err;
  600. ASSERT_RTNL();
  601. if (nlmsg_len(nlh) < sizeof(*ndm))
  602. return -EINVAL;
  603. ndm = nlmsg_data(nlh);
  604. if (ndm->ndm_ifindex == 0) {
  605. pr_info("bridge: RTM_DELNEIGH with invalid ifindex\n");
  606. return -EINVAL;
  607. }
  608. dev = __dev_get_by_index(net, ndm->ndm_ifindex);
  609. if (dev == NULL) {
  610. pr_info("bridge: RTM_DELNEIGH with unknown ifindex\n");
  611. return -ENODEV;
  612. }
  613. llattr = nlmsg_find_attr(nlh, sizeof(*ndm), NDA_LLADDR);
  614. if (llattr == NULL || nla_len(llattr) != ETH_ALEN) {
  615. pr_info("bridge: RTM_DELNEIGH with invalid address\n");
  616. return -EINVAL;
  617. }
  618. addr = nla_data(llattr);
  619. p = br_port_get_rtnl(dev);
  620. if (p == NULL) {
  621. pr_info("bridge: RTM_DELNEIGH %s not a bridge port\n",
  622. dev->name);
  623. return -EINVAL;
  624. }
  625. spin_lock_bh(&p->br->hash_lock);
  626. err = fdb_delete_by_addr(p, addr);
  627. spin_unlock_bh(&p->br->hash_lock);
  628. return err;
  629. }