br_fdb.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/spinlock.h>
  18. #include <linux/times.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/jhash.h>
  22. #include <linux/random.h>
  23. #include <asm/atomic.h>
  24. #include <asm/unaligned.h>
  25. #include "br_private.h"
  26. static struct kmem_cache *br_fdb_cache __read_mostly;
  27. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  28. const unsigned char *addr);
  29. static u32 fdb_salt __read_mostly;
  30. void __init br_fdb_init(void)
  31. {
  32. br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
  33. sizeof(struct net_bridge_fdb_entry),
  34. 0,
  35. SLAB_HWCACHE_ALIGN, NULL, NULL);
  36. get_random_bytes(&fdb_salt, sizeof(fdb_salt));
  37. }
  38. void __exit br_fdb_fini(void)
  39. {
  40. kmem_cache_destroy(br_fdb_cache);
  41. }
  42. /* if topology_changing then use forward_delay (default 15 sec)
  43. * otherwise keep longer (default 5 minutes)
  44. */
  45. static inline unsigned long hold_time(const struct net_bridge *br)
  46. {
  47. return br->topology_change ? br->forward_delay : br->ageing_time;
  48. }
  49. static inline int has_expired(const struct net_bridge *br,
  50. const struct net_bridge_fdb_entry *fdb)
  51. {
  52. return !fdb->is_static
  53. && time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
  54. }
  55. static inline int br_mac_hash(const unsigned char *mac)
  56. {
  57. /* use 1 byte of OUI cnd 3 bytes of NIC */
  58. u32 key = get_unaligned((u32 *)(mac + 2));
  59. return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1);
  60. }
  61. static inline void fdb_delete(struct net_bridge_fdb_entry *f)
  62. {
  63. hlist_del_rcu(&f->hlist);
  64. br_fdb_put(f);
  65. }
  66. void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
  67. {
  68. struct net_bridge *br = p->br;
  69. int i;
  70. spin_lock_bh(&br->hash_lock);
  71. /* Search all chains since old address/hash is unknown */
  72. for (i = 0; i < BR_HASH_SIZE; i++) {
  73. struct hlist_node *h;
  74. hlist_for_each(h, &br->hash[i]) {
  75. struct net_bridge_fdb_entry *f;
  76. f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  77. if (f->dst == p && f->is_local) {
  78. /* maybe another port has same hw addr? */
  79. struct net_bridge_port *op;
  80. list_for_each_entry(op, &br->port_list, list) {
  81. if (op != p &&
  82. !compare_ether_addr(op->dev->dev_addr,
  83. f->addr.addr)) {
  84. f->dst = op;
  85. goto insert;
  86. }
  87. }
  88. /* delete old one */
  89. fdb_delete(f);
  90. goto insert;
  91. }
  92. }
  93. }
  94. insert:
  95. /* insert new address, may fail if invalid address or dup. */
  96. fdb_insert(br, p, newaddr);
  97. spin_unlock_bh(&br->hash_lock);
  98. }
  99. void br_fdb_cleanup(unsigned long _data)
  100. {
  101. struct net_bridge *br = (struct net_bridge *)_data;
  102. unsigned long delay = hold_time(br);
  103. int i;
  104. spin_lock_bh(&br->hash_lock);
  105. for (i = 0; i < BR_HASH_SIZE; i++) {
  106. struct net_bridge_fdb_entry *f;
  107. struct hlist_node *h, *n;
  108. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  109. if (!f->is_static &&
  110. time_before_eq(f->ageing_timer + delay, jiffies))
  111. fdb_delete(f);
  112. }
  113. }
  114. spin_unlock_bh(&br->hash_lock);
  115. mod_timer(&br->gc_timer, jiffies + HZ/10);
  116. }
  117. void br_fdb_delete_by_port(struct net_bridge *br,
  118. const struct net_bridge_port *p,
  119. int do_all)
  120. {
  121. int i;
  122. spin_lock_bh(&br->hash_lock);
  123. for (i = 0; i < BR_HASH_SIZE; i++) {
  124. struct hlist_node *h, *g;
  125. hlist_for_each_safe(h, g, &br->hash[i]) {
  126. struct net_bridge_fdb_entry *f
  127. = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  128. if (f->dst != p)
  129. continue;
  130. if (f->is_static && !do_all)
  131. continue;
  132. /*
  133. * if multiple ports all have the same device address
  134. * then when one port is deleted, assign
  135. * the local entry to other port
  136. */
  137. if (f->is_local) {
  138. struct net_bridge_port *op;
  139. list_for_each_entry(op, &br->port_list, list) {
  140. if (op != p &&
  141. !compare_ether_addr(op->dev->dev_addr,
  142. f->addr.addr)) {
  143. f->dst = op;
  144. goto skip_delete;
  145. }
  146. }
  147. }
  148. fdb_delete(f);
  149. skip_delete: ;
  150. }
  151. }
  152. spin_unlock_bh(&br->hash_lock);
  153. }
  154. /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
  155. struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
  156. const unsigned char *addr)
  157. {
  158. struct hlist_node *h;
  159. struct net_bridge_fdb_entry *fdb;
  160. hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
  161. if (!compare_ether_addr(fdb->addr.addr, addr)) {
  162. if (unlikely(has_expired(br, fdb)))
  163. break;
  164. return fdb;
  165. }
  166. }
  167. return NULL;
  168. }
  169. /* Interface used by ATM hook that keeps a ref count */
  170. struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br,
  171. unsigned char *addr)
  172. {
  173. struct net_bridge_fdb_entry *fdb;
  174. rcu_read_lock();
  175. fdb = __br_fdb_get(br, addr);
  176. if (fdb && !atomic_inc_not_zero(&fdb->use_count))
  177. fdb = NULL;
  178. rcu_read_unlock();
  179. return fdb;
  180. }
  181. static void fdb_rcu_free(struct rcu_head *head)
  182. {
  183. struct net_bridge_fdb_entry *ent
  184. = container_of(head, struct net_bridge_fdb_entry, rcu);
  185. kmem_cache_free(br_fdb_cache, ent);
  186. }
  187. /* Set entry up for deletion with RCU */
  188. void br_fdb_put(struct net_bridge_fdb_entry *ent)
  189. {
  190. if (atomic_dec_and_test(&ent->use_count))
  191. call_rcu(&ent->rcu, fdb_rcu_free);
  192. }
  193. /*
  194. * Fill buffer with forwarding table records in
  195. * the API format.
  196. */
  197. int br_fdb_fillbuf(struct net_bridge *br, void *buf,
  198. unsigned long maxnum, unsigned long skip)
  199. {
  200. struct __fdb_entry *fe = buf;
  201. int i, num = 0;
  202. struct hlist_node *h;
  203. struct net_bridge_fdb_entry *f;
  204. memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
  205. rcu_read_lock();
  206. for (i = 0; i < BR_HASH_SIZE; i++) {
  207. hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
  208. if (num >= maxnum)
  209. goto out;
  210. if (has_expired(br, f))
  211. continue;
  212. if (skip) {
  213. --skip;
  214. continue;
  215. }
  216. /* convert from internal format to API */
  217. memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
  218. fe->port_no = f->dst->port_no;
  219. fe->is_local = f->is_local;
  220. if (!f->is_static)
  221. fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
  222. ++fe;
  223. ++num;
  224. }
  225. }
  226. out:
  227. rcu_read_unlock();
  228. return num;
  229. }
  230. static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
  231. const unsigned char *addr)
  232. {
  233. struct hlist_node *h;
  234. struct net_bridge_fdb_entry *fdb;
  235. hlist_for_each_entry_rcu(fdb, h, head, hlist) {
  236. if (!compare_ether_addr(fdb->addr.addr, addr))
  237. return fdb;
  238. }
  239. return NULL;
  240. }
  241. static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
  242. struct net_bridge_port *source,
  243. const unsigned char *addr,
  244. int is_local)
  245. {
  246. struct net_bridge_fdb_entry *fdb;
  247. fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
  248. if (fdb) {
  249. memcpy(fdb->addr.addr, addr, ETH_ALEN);
  250. atomic_set(&fdb->use_count, 1);
  251. hlist_add_head_rcu(&fdb->hlist, head);
  252. fdb->dst = source;
  253. fdb->is_local = is_local;
  254. fdb->is_static = is_local;
  255. fdb->ageing_timer = jiffies;
  256. }
  257. return fdb;
  258. }
  259. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  260. const unsigned char *addr)
  261. {
  262. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  263. struct net_bridge_fdb_entry *fdb;
  264. if (!is_valid_ether_addr(addr))
  265. return -EINVAL;
  266. fdb = fdb_find(head, addr);
  267. if (fdb) {
  268. /* it is okay to have multiple ports with same
  269. * address, just use the first one.
  270. */
  271. if (fdb->is_local)
  272. return 0;
  273. printk(KERN_WARNING "%s adding interface with same address "
  274. "as a received packet\n",
  275. source->dev->name);
  276. fdb_delete(fdb);
  277. }
  278. if (!fdb_create(head, source, addr, 1))
  279. return -ENOMEM;
  280. return 0;
  281. }
  282. int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  283. const unsigned char *addr)
  284. {
  285. int ret;
  286. spin_lock_bh(&br->hash_lock);
  287. ret = fdb_insert(br, source, addr);
  288. spin_unlock_bh(&br->hash_lock);
  289. return ret;
  290. }
  291. void br_fdb_update(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. /* some users want to always flood. */
  297. if (hold_time(br) == 0)
  298. return;
  299. fdb = fdb_find(head, addr);
  300. if (likely(fdb)) {
  301. /* attempt to update an entry for a local interface */
  302. if (unlikely(fdb->is_local)) {
  303. if (net_ratelimit())
  304. printk(KERN_WARNING "%s: received packet with "
  305. " own address as source address\n",
  306. source->dev->name);
  307. } else {
  308. /* fastpath: update of existing entry */
  309. fdb->dst = source;
  310. fdb->ageing_timer = jiffies;
  311. }
  312. } else {
  313. spin_lock(&br->hash_lock);
  314. if (!fdb_find(head, addr))
  315. fdb_create(head, source, addr, 0);
  316. /* else we lose race and someone else inserts
  317. * it first, don't bother updating
  318. */
  319. spin_unlock(&br->hash_lock);
  320. }
  321. }