br_fdb.c 20 KB

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