br_fdb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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);
  36. if (!br_fdb_cache)
  37. return -ENOMEM;
  38. get_random_bytes(&fdb_salt, sizeof(fdb_salt));
  39. return 0;
  40. }
  41. void 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. unsigned long next_timer = jiffies + br->forward_delay;
  107. int i;
  108. spin_lock_bh(&br->hash_lock);
  109. for (i = 0; i < BR_HASH_SIZE; i++) {
  110. struct net_bridge_fdb_entry *f;
  111. struct hlist_node *h, *n;
  112. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  113. unsigned long this_timer;
  114. if (f->is_static)
  115. continue;
  116. this_timer = f->ageing_timer + delay;
  117. if (time_before_eq(this_timer, jiffies))
  118. fdb_delete(f);
  119. else if (this_timer < next_timer)
  120. next_timer = this_timer;
  121. }
  122. }
  123. spin_unlock_bh(&br->hash_lock);
  124. /* Add HZ/4 to ensure we round the jiffies upwards to be after the next
  125. * timer, otherwise we might round down and will have no-op run. */
  126. mod_timer(&br->gc_timer, round_jiffies(next_timer + HZ/4));
  127. }
  128. /* Completely flush all dynamic entries in forwarding database.*/
  129. void br_fdb_flush(struct net_bridge *br)
  130. {
  131. int i;
  132. spin_lock_bh(&br->hash_lock);
  133. for (i = 0; i < BR_HASH_SIZE; i++) {
  134. struct net_bridge_fdb_entry *f;
  135. struct hlist_node *h, *n;
  136. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  137. if (!f->is_static)
  138. fdb_delete(f);
  139. }
  140. }
  141. spin_unlock_bh(&br->hash_lock);
  142. }
  143. /* Flush all entries refering to a specific port.
  144. * if do_all is set also flush static entries
  145. */
  146. void br_fdb_delete_by_port(struct net_bridge *br,
  147. const struct net_bridge_port *p,
  148. int do_all)
  149. {
  150. int i;
  151. spin_lock_bh(&br->hash_lock);
  152. for (i = 0; i < BR_HASH_SIZE; i++) {
  153. struct hlist_node *h, *g;
  154. hlist_for_each_safe(h, g, &br->hash[i]) {
  155. struct net_bridge_fdb_entry *f
  156. = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  157. if (f->dst != p)
  158. continue;
  159. if (f->is_static && !do_all)
  160. continue;
  161. /*
  162. * if multiple ports all have the same device address
  163. * then when one port is deleted, assign
  164. * the local entry to other port
  165. */
  166. if (f->is_local) {
  167. struct net_bridge_port *op;
  168. list_for_each_entry(op, &br->port_list, list) {
  169. if (op != p &&
  170. !compare_ether_addr(op->dev->dev_addr,
  171. f->addr.addr)) {
  172. f->dst = op;
  173. goto skip_delete;
  174. }
  175. }
  176. }
  177. fdb_delete(f);
  178. skip_delete: ;
  179. }
  180. }
  181. spin_unlock_bh(&br->hash_lock);
  182. }
  183. /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
  184. struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
  185. const unsigned char *addr)
  186. {
  187. struct hlist_node *h;
  188. struct net_bridge_fdb_entry *fdb;
  189. hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
  190. if (!compare_ether_addr(fdb->addr.addr, addr)) {
  191. if (unlikely(has_expired(br, fdb)))
  192. break;
  193. return fdb;
  194. }
  195. }
  196. return NULL;
  197. }
  198. /* Interface used by ATM hook that keeps a ref count */
  199. struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br,
  200. unsigned char *addr)
  201. {
  202. struct net_bridge_fdb_entry *fdb;
  203. rcu_read_lock();
  204. fdb = __br_fdb_get(br, addr);
  205. if (fdb && !atomic_inc_not_zero(&fdb->use_count))
  206. fdb = NULL;
  207. rcu_read_unlock();
  208. return fdb;
  209. }
  210. static void fdb_rcu_free(struct rcu_head *head)
  211. {
  212. struct net_bridge_fdb_entry *ent
  213. = container_of(head, struct net_bridge_fdb_entry, rcu);
  214. kmem_cache_free(br_fdb_cache, ent);
  215. }
  216. /* Set entry up for deletion with RCU */
  217. void br_fdb_put(struct net_bridge_fdb_entry *ent)
  218. {
  219. if (atomic_dec_and_test(&ent->use_count))
  220. call_rcu(&ent->rcu, fdb_rcu_free);
  221. }
  222. /*
  223. * Fill buffer with forwarding table records in
  224. * the API format.
  225. */
  226. int br_fdb_fillbuf(struct net_bridge *br, void *buf,
  227. unsigned long maxnum, unsigned long skip)
  228. {
  229. struct __fdb_entry *fe = buf;
  230. int i, num = 0;
  231. struct hlist_node *h;
  232. struct net_bridge_fdb_entry *f;
  233. memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
  234. rcu_read_lock();
  235. for (i = 0; i < BR_HASH_SIZE; i++) {
  236. hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
  237. if (num >= maxnum)
  238. goto out;
  239. if (has_expired(br, f))
  240. continue;
  241. if (skip) {
  242. --skip;
  243. continue;
  244. }
  245. /* convert from internal format to API */
  246. memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
  247. fe->port_no = f->dst->port_no;
  248. fe->is_local = f->is_local;
  249. if (!f->is_static)
  250. fe->ageing_timer_value = jiffies_to_clock_t(jiffies - f->ageing_timer);
  251. ++fe;
  252. ++num;
  253. }
  254. }
  255. out:
  256. rcu_read_unlock();
  257. return num;
  258. }
  259. static inline struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
  260. const unsigned char *addr)
  261. {
  262. struct hlist_node *h;
  263. struct net_bridge_fdb_entry *fdb;
  264. hlist_for_each_entry_rcu(fdb, h, head, hlist) {
  265. if (!compare_ether_addr(fdb->addr.addr, addr))
  266. return fdb;
  267. }
  268. return NULL;
  269. }
  270. static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
  271. struct net_bridge_port *source,
  272. const unsigned char *addr,
  273. int is_local)
  274. {
  275. struct net_bridge_fdb_entry *fdb;
  276. fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
  277. if (fdb) {
  278. memcpy(fdb->addr.addr, addr, ETH_ALEN);
  279. atomic_set(&fdb->use_count, 1);
  280. hlist_add_head_rcu(&fdb->hlist, head);
  281. fdb->dst = source;
  282. fdb->is_local = is_local;
  283. fdb->is_static = is_local;
  284. fdb->ageing_timer = jiffies;
  285. }
  286. return fdb;
  287. }
  288. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  289. const unsigned char *addr)
  290. {
  291. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  292. struct net_bridge_fdb_entry *fdb;
  293. if (!is_valid_ether_addr(addr))
  294. return -EINVAL;
  295. fdb = fdb_find(head, addr);
  296. if (fdb) {
  297. /* it is okay to have multiple ports with same
  298. * address, just use the first one.
  299. */
  300. if (fdb->is_local)
  301. return 0;
  302. printk(KERN_WARNING "%s adding interface with same address "
  303. "as a received packet\n",
  304. source->dev->name);
  305. fdb_delete(fdb);
  306. }
  307. if (!fdb_create(head, source, addr, 1))
  308. return -ENOMEM;
  309. return 0;
  310. }
  311. int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  312. const unsigned char *addr)
  313. {
  314. int ret;
  315. spin_lock_bh(&br->hash_lock);
  316. ret = fdb_insert(br, source, addr);
  317. spin_unlock_bh(&br->hash_lock);
  318. return ret;
  319. }
  320. void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
  321. const unsigned char *addr)
  322. {
  323. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  324. struct net_bridge_fdb_entry *fdb;
  325. /* some users want to always flood. */
  326. if (hold_time(br) == 0)
  327. return;
  328. /* ignore packets unless we are using this port */
  329. if (!(source->state == BR_STATE_LEARNING ||
  330. source->state == BR_STATE_FORWARDING))
  331. return;
  332. fdb = fdb_find(head, addr);
  333. if (likely(fdb)) {
  334. /* attempt to update an entry for a local interface */
  335. if (unlikely(fdb->is_local)) {
  336. if (net_ratelimit())
  337. printk(KERN_WARNING "%s: received packet with "
  338. " own address as source address\n",
  339. source->dev->name);
  340. } else {
  341. /* fastpath: update of existing entry */
  342. fdb->dst = source;
  343. fdb->ageing_timer = jiffies;
  344. }
  345. } else {
  346. spin_lock(&br->hash_lock);
  347. if (!fdb_find(head, addr))
  348. fdb_create(head, source, addr, 0);
  349. /* else we lose race and someone else inserts
  350. * it first, don't bother updating
  351. */
  352. spin_unlock(&br->hash_lock);
  353. }
  354. }