br_fdb.c 19 KB

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