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 <linux/slab.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 void fdb_rcu_free(struct rcu_head *head)
  65. {
  66. struct net_bridge_fdb_entry *ent
  67. = container_of(head, struct net_bridge_fdb_entry, rcu);
  68. kmem_cache_free(br_fdb_cache, ent);
  69. }
  70. static inline void fdb_delete(struct net_bridge_fdb_entry *f)
  71. {
  72. hlist_del_rcu(&f->hlist);
  73. call_rcu(&f->rcu, fdb_rcu_free);
  74. }
  75. void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
  76. {
  77. struct net_bridge *br = p->br;
  78. int i;
  79. spin_lock_bh(&br->hash_lock);
  80. /* Search all chains since old address/hash is unknown */
  81. for (i = 0; i < BR_HASH_SIZE; i++) {
  82. struct hlist_node *h;
  83. hlist_for_each(h, &br->hash[i]) {
  84. struct net_bridge_fdb_entry *f;
  85. f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  86. if (f->dst == p && f->is_local) {
  87. /* maybe another port has same hw addr? */
  88. struct net_bridge_port *op;
  89. list_for_each_entry(op, &br->port_list, list) {
  90. if (op != p &&
  91. !compare_ether_addr(op->dev->dev_addr,
  92. f->addr.addr)) {
  93. f->dst = op;
  94. goto insert;
  95. }
  96. }
  97. /* delete old one */
  98. fdb_delete(f);
  99. goto insert;
  100. }
  101. }
  102. }
  103. insert:
  104. /* insert new address, may fail if invalid address or dup. */
  105. fdb_insert(br, p, newaddr);
  106. spin_unlock_bh(&br->hash_lock);
  107. }
  108. void br_fdb_cleanup(unsigned long _data)
  109. {
  110. struct net_bridge *br = (struct net_bridge *)_data;
  111. unsigned long delay = hold_time(br);
  112. unsigned long next_timer = jiffies + br->forward_delay;
  113. int i;
  114. spin_lock_bh(&br->hash_lock);
  115. for (i = 0; i < BR_HASH_SIZE; i++) {
  116. struct net_bridge_fdb_entry *f;
  117. struct hlist_node *h, *n;
  118. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  119. unsigned long this_timer;
  120. if (f->is_static)
  121. continue;
  122. this_timer = f->ageing_timer + delay;
  123. if (time_before_eq(this_timer, jiffies))
  124. fdb_delete(f);
  125. else if (time_before(this_timer, next_timer))
  126. next_timer = this_timer;
  127. }
  128. }
  129. spin_unlock_bh(&br->hash_lock);
  130. /* Add HZ/4 to ensure we round the jiffies upwards to be after the next
  131. * timer, otherwise we might round down and will have no-op run. */
  132. mod_timer(&br->gc_timer, round_jiffies(next_timer + HZ/4));
  133. }
  134. /* Completely flush all dynamic entries in forwarding database.*/
  135. void br_fdb_flush(struct net_bridge *br)
  136. {
  137. int i;
  138. spin_lock_bh(&br->hash_lock);
  139. for (i = 0; i < BR_HASH_SIZE; i++) {
  140. struct net_bridge_fdb_entry *f;
  141. struct hlist_node *h, *n;
  142. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  143. if (!f->is_static)
  144. fdb_delete(f);
  145. }
  146. }
  147. spin_unlock_bh(&br->hash_lock);
  148. }
  149. /* Flush all entries refering to a specific port.
  150. * if do_all is set also flush static entries
  151. */
  152. void br_fdb_delete_by_port(struct net_bridge *br,
  153. const struct net_bridge_port *p,
  154. int do_all)
  155. {
  156. int i;
  157. spin_lock_bh(&br->hash_lock);
  158. for (i = 0; i < BR_HASH_SIZE; i++) {
  159. struct hlist_node *h, *g;
  160. hlist_for_each_safe(h, g, &br->hash[i]) {
  161. struct net_bridge_fdb_entry *f
  162. = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  163. if (f->dst != p)
  164. continue;
  165. if (f->is_static && !do_all)
  166. continue;
  167. /*
  168. * if multiple ports all have the same device address
  169. * then when one port is deleted, assign
  170. * the local entry to other port
  171. */
  172. if (f->is_local) {
  173. struct net_bridge_port *op;
  174. list_for_each_entry(op, &br->port_list, list) {
  175. if (op != p &&
  176. !compare_ether_addr(op->dev->dev_addr,
  177. f->addr.addr)) {
  178. f->dst = op;
  179. goto skip_delete;
  180. }
  181. }
  182. }
  183. fdb_delete(f);
  184. skip_delete: ;
  185. }
  186. }
  187. spin_unlock_bh(&br->hash_lock);
  188. }
  189. /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
  190. struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
  191. const unsigned char *addr)
  192. {
  193. struct hlist_node *h;
  194. struct net_bridge_fdb_entry *fdb;
  195. hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
  196. if (!compare_ether_addr(fdb->addr.addr, addr)) {
  197. if (unlikely(has_expired(br, fdb)))
  198. break;
  199. return fdb;
  200. }
  201. }
  202. return NULL;
  203. }
  204. #if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)
  205. /* Interface used by ATM LANE hook to test
  206. * if an addr is on some other bridge port */
  207. int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
  208. {
  209. struct net_bridge_fdb_entry *fdb;
  210. int ret;
  211. if (!dev->br_port)
  212. return 0;
  213. rcu_read_lock();
  214. fdb = __br_fdb_get(dev->br_port->br, addr);
  215. ret = fdb && fdb->dst->dev != dev &&
  216. fdb->dst->state == BR_STATE_FORWARDING;
  217. rcu_read_unlock();
  218. return ret;
  219. }
  220. #endif /* CONFIG_ATM_LANE */
  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. 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. }