br_fdb.c 17 KB

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