br_fdb.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Forwarding database
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.org>
  7. *
  8. * $Id: br_fdb.c,v 1.6 2002/01/17 00:57:07 davem Exp $
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/rculist.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/times.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/jhash.h>
  23. #include <linux/random.h>
  24. #include <asm/atomic.h>
  25. #include <asm/unaligned.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 u32 fdb_salt __read_mostly;
  31. int __init br_fdb_init(void)
  32. {
  33. br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
  34. sizeof(struct net_bridge_fdb_entry),
  35. 0,
  36. SLAB_HWCACHE_ALIGN, NULL);
  37. if (!br_fdb_cache)
  38. return -ENOMEM;
  39. get_random_bytes(&fdb_salt, sizeof(fdb_salt));
  40. return 0;
  41. }
  42. void br_fdb_fini(void)
  43. {
  44. kmem_cache_destroy(br_fdb_cache);
  45. }
  46. /* if topology_changing then use forward_delay (default 15 sec)
  47. * otherwise keep longer (default 5 minutes)
  48. */
  49. static inline unsigned long hold_time(const struct net_bridge *br)
  50. {
  51. return br->topology_change ? br->forward_delay : br->ageing_time;
  52. }
  53. static inline int has_expired(const struct net_bridge *br,
  54. const struct net_bridge_fdb_entry *fdb)
  55. {
  56. return !fdb->is_static
  57. && time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
  58. }
  59. static inline int br_mac_hash(const unsigned char *mac)
  60. {
  61. /* use 1 byte of OUI cnd 3 bytes of NIC */
  62. u32 key = get_unaligned((u32 *)(mac + 2));
  63. return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1);
  64. }
  65. static inline void fdb_delete(struct net_bridge_fdb_entry *f)
  66. {
  67. hlist_del_rcu(&f->hlist);
  68. br_fdb_put(f);
  69. }
  70. void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
  71. {
  72. struct net_bridge *br = p->br;
  73. int i;
  74. spin_lock_bh(&br->hash_lock);
  75. /* Search all chains since old address/hash is unknown */
  76. for (i = 0; i < BR_HASH_SIZE; i++) {
  77. struct hlist_node *h;
  78. hlist_for_each(h, &br->hash[i]) {
  79. struct net_bridge_fdb_entry *f;
  80. f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  81. if (f->dst == p && f->is_local) {
  82. /* maybe another port has same hw addr? */
  83. struct net_bridge_port *op;
  84. list_for_each_entry(op, &br->port_list, list) {
  85. if (op != p &&
  86. !compare_ether_addr(op->dev->dev_addr,
  87. f->addr.addr)) {
  88. f->dst = op;
  89. goto insert;
  90. }
  91. }
  92. /* delete old one */
  93. fdb_delete(f);
  94. goto insert;
  95. }
  96. }
  97. }
  98. insert:
  99. /* insert new address, may fail if invalid address or dup. */
  100. fdb_insert(br, p, newaddr);
  101. spin_unlock_bh(&br->hash_lock);
  102. }
  103. void br_fdb_cleanup(unsigned long _data)
  104. {
  105. struct net_bridge *br = (struct net_bridge *)_data;
  106. unsigned long delay = hold_time(br);
  107. unsigned long next_timer = jiffies + br->forward_delay;
  108. int i;
  109. spin_lock_bh(&br->hash_lock);
  110. for (i = 0; i < BR_HASH_SIZE; i++) {
  111. struct net_bridge_fdb_entry *f;
  112. struct hlist_node *h, *n;
  113. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  114. unsigned long this_timer;
  115. if (f->is_static)
  116. continue;
  117. this_timer = f->ageing_timer + delay;
  118. if (time_before_eq(this_timer, jiffies))
  119. fdb_delete(f);
  120. else if (time_before(this_timer, next_timer))
  121. next_timer = this_timer;
  122. }
  123. }
  124. spin_unlock_bh(&br->hash_lock);
  125. /* Add HZ/4 to ensure we round the jiffies upwards to be after the next
  126. * timer, otherwise we might round down and will have no-op run. */
  127. mod_timer(&br->gc_timer, round_jiffies(next_timer + HZ/4));
  128. }
  129. /* Completely flush all dynamic entries in forwarding database.*/
  130. void br_fdb_flush(struct net_bridge *br)
  131. {
  132. int i;
  133. spin_lock_bh(&br->hash_lock);
  134. for (i = 0; i < BR_HASH_SIZE; i++) {
  135. struct net_bridge_fdb_entry *f;
  136. struct hlist_node *h, *n;
  137. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  138. if (!f->is_static)
  139. fdb_delete(f);
  140. }
  141. }
  142. spin_unlock_bh(&br->hash_lock);
  143. }
  144. /* Flush all entries refering to a specific port.
  145. * if do_all is set also flush static entries
  146. */
  147. void br_fdb_delete_by_port(struct net_bridge *br,
  148. const struct net_bridge_port *p,
  149. int do_all)
  150. {
  151. int i;
  152. spin_lock_bh(&br->hash_lock);
  153. for (i = 0; i < BR_HASH_SIZE; i++) {
  154. struct hlist_node *h, *g;
  155. hlist_for_each_safe(h, g, &br->hash[i]) {
  156. struct net_bridge_fdb_entry *f
  157. = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  158. if (f->dst != p)
  159. continue;
  160. if (f->is_static && !do_all)
  161. continue;
  162. /*
  163. * if multiple ports all have the same device address
  164. * then when one port is deleted, assign
  165. * the local entry to other port
  166. */
  167. if (f->is_local) {
  168. struct net_bridge_port *op;
  169. list_for_each_entry(op, &br->port_list, list) {
  170. if (op != p &&
  171. !compare_ether_addr(op->dev->dev_addr,
  172. f->addr.addr)) {
  173. f->dst = op;
  174. goto skip_delete;
  175. }
  176. }
  177. }
  178. fdb_delete(f);
  179. skip_delete: ;
  180. }
  181. }
  182. spin_unlock_bh(&br->hash_lock);
  183. }
  184. /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
  185. struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
  186. const unsigned char *addr)
  187. {
  188. struct hlist_node *h;
  189. struct net_bridge_fdb_entry *fdb;
  190. hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
  191. if (!compare_ether_addr(fdb->addr.addr, addr)) {
  192. if (unlikely(has_expired(br, fdb)))
  193. break;
  194. return fdb;
  195. }
  196. }
  197. return NULL;
  198. }
  199. /* Interface used by ATM hook that keeps a ref count */
  200. struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br,
  201. unsigned char *addr)
  202. {
  203. struct net_bridge_fdb_entry *fdb;
  204. rcu_read_lock();
  205. fdb = __br_fdb_get(br, addr);
  206. if (fdb && !atomic_inc_not_zero(&fdb->use_count))
  207. fdb = NULL;
  208. rcu_read_unlock();
  209. return fdb;
  210. }
  211. static void fdb_rcu_free(struct rcu_head *head)
  212. {
  213. struct net_bridge_fdb_entry *ent
  214. = container_of(head, struct net_bridge_fdb_entry, rcu);
  215. kmem_cache_free(br_fdb_cache, ent);
  216. }
  217. /* Set entry up for deletion with RCU */
  218. void br_fdb_put(struct net_bridge_fdb_entry *ent)
  219. {
  220. if (atomic_dec_and_test(&ent->use_count))
  221. call_rcu(&ent->rcu, fdb_rcu_free);
  222. }
  223. /*
  224. * Fill buffer with forwarding table records in
  225. * the API format.
  226. */
  227. int br_fdb_fillbuf(struct net_bridge *br, void *buf,
  228. unsigned long maxnum, unsigned long skip)
  229. {
  230. struct __fdb_entry *fe = buf;
  231. int i, num = 0;
  232. struct hlist_node *h;
  233. struct net_bridge_fdb_entry *f;
  234. memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
  235. rcu_read_lock();
  236. for (i = 0; i < BR_HASH_SIZE; i++) {
  237. hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
  238. if (num >= maxnum)
  239. goto out;
  240. if (has_expired(br, f))
  241. continue;
  242. if (skip) {
  243. --skip;
  244. continue;
  245. }
  246. /* convert from internal format to API */
  247. memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
  248. /* due to ABI compat need to split into hi/lo */
  249. fe->port_no = f->dst->port_no;
  250. fe->port_hi = f->dst->port_no >> 8;
  251. fe->is_local = f->is_local;
  252. if (!f->is_static)
  253. fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
  254. ++fe;
  255. ++num;
  256. }
  257. }
  258. out:
  259. rcu_read_unlock();
  260. return num;
  261. }
  262. static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
  263. const unsigned char *addr)
  264. {
  265. struct hlist_node *h;
  266. struct net_bridge_fdb_entry *fdb;
  267. hlist_for_each_entry_rcu(fdb, h, head, hlist) {
  268. if (!compare_ether_addr(fdb->addr.addr, addr))
  269. return fdb;
  270. }
  271. return NULL;
  272. }
  273. static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
  274. struct net_bridge_port *source,
  275. const unsigned char *addr,
  276. int is_local)
  277. {
  278. struct net_bridge_fdb_entry *fdb;
  279. fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
  280. if (fdb) {
  281. memcpy(fdb->addr.addr, addr, ETH_ALEN);
  282. atomic_set(&fdb->use_count, 1);
  283. hlist_add_head_rcu(&fdb->hlist, head);
  284. fdb->dst = source;
  285. fdb->is_local = is_local;
  286. fdb->is_static = is_local;
  287. fdb->ageing_timer = jiffies;
  288. }
  289. return fdb;
  290. }
  291. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  292. const unsigned char *addr)
  293. {
  294. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  295. struct net_bridge_fdb_entry *fdb;
  296. if (!is_valid_ether_addr(addr))
  297. return -EINVAL;
  298. fdb = fdb_find(head, addr);
  299. if (fdb) {
  300. /* it is okay to have multiple ports with same
  301. * address, just use the first one.
  302. */
  303. if (fdb->is_local)
  304. return 0;
  305. printk(KERN_WARNING "%s adding interface with same address "
  306. "as a received packet\n",
  307. source->dev->name);
  308. fdb_delete(fdb);
  309. }
  310. if (!fdb_create(head, source, addr, 1))
  311. return -ENOMEM;
  312. return 0;
  313. }
  314. int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  315. const unsigned char *addr)
  316. {
  317. int ret;
  318. spin_lock_bh(&br->hash_lock);
  319. ret = fdb_insert(br, source, addr);
  320. spin_unlock_bh(&br->hash_lock);
  321. return ret;
  322. }
  323. void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
  324. const unsigned char *addr)
  325. {
  326. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  327. struct net_bridge_fdb_entry *fdb;
  328. /* some users want to always flood. */
  329. if (hold_time(br) == 0)
  330. return;
  331. /* ignore packets unless we are using this port */
  332. if (!(source->state == BR_STATE_LEARNING ||
  333. source->state == BR_STATE_FORWARDING))
  334. return;
  335. fdb = fdb_find(head, addr);
  336. if (likely(fdb)) {
  337. /* attempt to update an entry for a local interface */
  338. if (unlikely(fdb->is_local)) {
  339. if (net_ratelimit())
  340. printk(KERN_WARNING "%s: received packet with "
  341. " own address as source address\n",
  342. source->dev->name);
  343. } else {
  344. /* fastpath: update of existing entry */
  345. fdb->dst = source;
  346. fdb->ageing_timer = jiffies;
  347. }
  348. } else {
  349. spin_lock(&br->hash_lock);
  350. if (!fdb_find(head, addr))
  351. fdb_create(head, source, addr, 0);
  352. /* else we lose race and someone else inserts
  353. * it first, don't bother updating
  354. */
  355. spin_unlock(&br->hash_lock);
  356. }
  357. }