br_fdb.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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/spinlock.h>
  16. #include <linux/times.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/jhash.h>
  20. #include <linux/random.h>
  21. #include <asm/atomic.h>
  22. #include <asm/unaligned.h>
  23. #include "br_private.h"
  24. static struct kmem_cache *br_fdb_cache __read_mostly;
  25. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  26. const unsigned char *addr);
  27. static u32 fdb_salt __read_mostly;
  28. int __init br_fdb_init(void)
  29. {
  30. br_fdb_cache = kmem_cache_create("bridge_fdb_cache",
  31. sizeof(struct net_bridge_fdb_entry),
  32. 0,
  33. SLAB_HWCACHE_ALIGN, NULL);
  34. if (!br_fdb_cache)
  35. return -ENOMEM;
  36. get_random_bytes(&fdb_salt, sizeof(fdb_salt));
  37. return 0;
  38. }
  39. void br_fdb_fini(void)
  40. {
  41. kmem_cache_destroy(br_fdb_cache);
  42. }
  43. /* if topology_changing then use forward_delay (default 15 sec)
  44. * otherwise keep longer (default 5 minutes)
  45. */
  46. static inline unsigned long hold_time(const struct net_bridge *br)
  47. {
  48. return br->topology_change ? br->forward_delay : br->ageing_time;
  49. }
  50. static inline int has_expired(const struct net_bridge *br,
  51. const struct net_bridge_fdb_entry *fdb)
  52. {
  53. return !fdb->is_static
  54. && time_before_eq(fdb->ageing_timer + hold_time(br), jiffies);
  55. }
  56. static inline int br_mac_hash(const unsigned char *mac)
  57. {
  58. /* use 1 byte of OUI cnd 3 bytes of NIC */
  59. u32 key = get_unaligned((u32 *)(mac + 2));
  60. return jhash_1word(key, fdb_salt) & (BR_HASH_SIZE - 1);
  61. }
  62. static inline void fdb_delete(struct net_bridge_fdb_entry *f)
  63. {
  64. hlist_del_rcu(&f->hlist);
  65. br_fdb_put(f);
  66. }
  67. void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
  68. {
  69. struct net_bridge *br = p->br;
  70. int i;
  71. spin_lock_bh(&br->hash_lock);
  72. /* Search all chains since old address/hash is unknown */
  73. for (i = 0; i < BR_HASH_SIZE; i++) {
  74. struct hlist_node *h;
  75. hlist_for_each(h, &br->hash[i]) {
  76. struct net_bridge_fdb_entry *f;
  77. f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  78. if (f->dst == p && f->is_local) {
  79. /* maybe another port has same hw addr? */
  80. struct net_bridge_port *op;
  81. list_for_each_entry(op, &br->port_list, list) {
  82. if (op != p &&
  83. !compare_ether_addr(op->dev->dev_addr,
  84. f->addr.addr)) {
  85. f->dst = op;
  86. goto insert;
  87. }
  88. }
  89. /* delete old one */
  90. fdb_delete(f);
  91. goto insert;
  92. }
  93. }
  94. }
  95. insert:
  96. /* insert new address, may fail if invalid address or dup. */
  97. fdb_insert(br, p, newaddr);
  98. spin_unlock_bh(&br->hash_lock);
  99. }
  100. void br_fdb_cleanup(unsigned long _data)
  101. {
  102. struct net_bridge *br = (struct net_bridge *)_data;
  103. unsigned long delay = hold_time(br);
  104. unsigned long next_timer = jiffies + br->forward_delay;
  105. int i;
  106. spin_lock_bh(&br->hash_lock);
  107. for (i = 0; i < BR_HASH_SIZE; i++) {
  108. struct net_bridge_fdb_entry *f;
  109. struct hlist_node *h, *n;
  110. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  111. unsigned long this_timer;
  112. if (f->is_static)
  113. continue;
  114. this_timer = f->ageing_timer + delay;
  115. if (time_before_eq(this_timer, jiffies))
  116. fdb_delete(f);
  117. else if (time_before(this_timer, next_timer))
  118. next_timer = this_timer;
  119. }
  120. }
  121. spin_unlock_bh(&br->hash_lock);
  122. /* Add HZ/4 to ensure we round the jiffies upwards to be after the next
  123. * timer, otherwise we might round down and will have no-op run. */
  124. mod_timer(&br->gc_timer, round_jiffies(next_timer + HZ/4));
  125. }
  126. /* Completely flush all dynamic entries in forwarding database.*/
  127. void br_fdb_flush(struct net_bridge *br)
  128. {
  129. int i;
  130. spin_lock_bh(&br->hash_lock);
  131. for (i = 0; i < BR_HASH_SIZE; i++) {
  132. struct net_bridge_fdb_entry *f;
  133. struct hlist_node *h, *n;
  134. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  135. if (!f->is_static)
  136. fdb_delete(f);
  137. }
  138. }
  139. spin_unlock_bh(&br->hash_lock);
  140. }
  141. /* Flush all entries refering to a specific port.
  142. * if do_all is set also flush static entries
  143. */
  144. void br_fdb_delete_by_port(struct net_bridge *br,
  145. const struct net_bridge_port *p,
  146. int do_all)
  147. {
  148. int i;
  149. spin_lock_bh(&br->hash_lock);
  150. for (i = 0; i < BR_HASH_SIZE; i++) {
  151. struct hlist_node *h, *g;
  152. hlist_for_each_safe(h, g, &br->hash[i]) {
  153. struct net_bridge_fdb_entry *f
  154. = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  155. if (f->dst != p)
  156. continue;
  157. if (f->is_static && !do_all)
  158. continue;
  159. /*
  160. * if multiple ports all have the same device address
  161. * then when one port is deleted, assign
  162. * the local entry to other port
  163. */
  164. if (f->is_local) {
  165. struct net_bridge_port *op;
  166. list_for_each_entry(op, &br->port_list, list) {
  167. if (op != p &&
  168. !compare_ether_addr(op->dev->dev_addr,
  169. f->addr.addr)) {
  170. f->dst = op;
  171. goto skip_delete;
  172. }
  173. }
  174. }
  175. fdb_delete(f);
  176. skip_delete: ;
  177. }
  178. }
  179. spin_unlock_bh(&br->hash_lock);
  180. }
  181. /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
  182. struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
  183. const unsigned char *addr)
  184. {
  185. struct hlist_node *h;
  186. struct net_bridge_fdb_entry *fdb;
  187. hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
  188. if (!compare_ether_addr(fdb->addr.addr, addr)) {
  189. if (unlikely(has_expired(br, fdb)))
  190. break;
  191. return fdb;
  192. }
  193. }
  194. return NULL;
  195. }
  196. /* Interface used by ATM hook that keeps a ref count */
  197. struct net_bridge_fdb_entry *br_fdb_get(struct net_bridge *br,
  198. unsigned char *addr)
  199. {
  200. struct net_bridge_fdb_entry *fdb;
  201. rcu_read_lock();
  202. fdb = __br_fdb_get(br, addr);
  203. if (fdb && !atomic_inc_not_zero(&fdb->use_count))
  204. fdb = NULL;
  205. rcu_read_unlock();
  206. return fdb;
  207. }
  208. static void fdb_rcu_free(struct rcu_head *head)
  209. {
  210. struct net_bridge_fdb_entry *ent
  211. = container_of(head, struct net_bridge_fdb_entry, rcu);
  212. kmem_cache_free(br_fdb_cache, ent);
  213. }
  214. /* Set entry up for deletion with RCU */
  215. void br_fdb_put(struct net_bridge_fdb_entry *ent)
  216. {
  217. if (atomic_dec_and_test(&ent->use_count))
  218. call_rcu(&ent->rcu, fdb_rcu_free);
  219. }
  220. /*
  221. * Fill buffer with forwarding table records in
  222. * the API format.
  223. */
  224. int br_fdb_fillbuf(struct net_bridge *br, void *buf,
  225. unsigned long maxnum, unsigned long skip)
  226. {
  227. struct __fdb_entry *fe = buf;
  228. int i, num = 0;
  229. struct hlist_node *h;
  230. struct net_bridge_fdb_entry *f;
  231. memset(buf, 0, maxnum*sizeof(struct __fdb_entry));
  232. rcu_read_lock();
  233. for (i = 0; i < BR_HASH_SIZE; i++) {
  234. hlist_for_each_entry_rcu(f, h, &br->hash[i], hlist) {
  235. if (num >= maxnum)
  236. goto out;
  237. if (has_expired(br, f))
  238. continue;
  239. if (skip) {
  240. --skip;
  241. continue;
  242. }
  243. /* convert from internal format to API */
  244. memcpy(fe->mac_addr, f->addr.addr, ETH_ALEN);
  245. /* due to ABI compat need to split into hi/lo */
  246. fe->port_no = f->dst->port_no;
  247. fe->port_hi = f->dst->port_no >> 8;
  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. }