br_fdb.c 19 KB

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