br_fdb.c 20 KB

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