br_fdb.c 9.4 KB

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