br_fdb.c 9.9 KB

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