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/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 void fdb_rcu_free(struct rcu_head *head)
  64. {
  65. struct net_bridge_fdb_entry *ent
  66. = container_of(head, struct net_bridge_fdb_entry, rcu);
  67. kmem_cache_free(br_fdb_cache, ent);
  68. }
  69. static inline void fdb_delete(struct net_bridge_fdb_entry *f)
  70. {
  71. hlist_del_rcu(&f->hlist);
  72. call_rcu(&f->rcu, fdb_rcu_free);
  73. }
  74. void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr)
  75. {
  76. struct net_bridge *br = p->br;
  77. int i;
  78. spin_lock_bh(&br->hash_lock);
  79. /* Search all chains since old address/hash is unknown */
  80. for (i = 0; i < BR_HASH_SIZE; i++) {
  81. struct hlist_node *h;
  82. hlist_for_each(h, &br->hash[i]) {
  83. struct net_bridge_fdb_entry *f;
  84. f = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  85. if (f->dst == p && f->is_local) {
  86. /* maybe another port has same hw addr? */
  87. struct net_bridge_port *op;
  88. list_for_each_entry(op, &br->port_list, list) {
  89. if (op != p &&
  90. !compare_ether_addr(op->dev->dev_addr,
  91. f->addr.addr)) {
  92. f->dst = op;
  93. goto insert;
  94. }
  95. }
  96. /* delete old one */
  97. fdb_delete(f);
  98. goto insert;
  99. }
  100. }
  101. }
  102. insert:
  103. /* insert new address, may fail if invalid address or dup. */
  104. fdb_insert(br, p, newaddr);
  105. spin_unlock_bh(&br->hash_lock);
  106. }
  107. void br_fdb_cleanup(unsigned long _data)
  108. {
  109. struct net_bridge *br = (struct net_bridge *)_data;
  110. unsigned long delay = hold_time(br);
  111. unsigned long next_timer = jiffies + br->forward_delay;
  112. int i;
  113. spin_lock_bh(&br->hash_lock);
  114. for (i = 0; i < BR_HASH_SIZE; i++) {
  115. struct net_bridge_fdb_entry *f;
  116. struct hlist_node *h, *n;
  117. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  118. unsigned long this_timer;
  119. if (f->is_static)
  120. continue;
  121. this_timer = f->ageing_timer + delay;
  122. if (time_before_eq(this_timer, jiffies))
  123. fdb_delete(f);
  124. else if (time_before(this_timer, next_timer))
  125. next_timer = this_timer;
  126. }
  127. }
  128. spin_unlock_bh(&br->hash_lock);
  129. /* Add HZ/4 to ensure we round the jiffies upwards to be after the next
  130. * timer, otherwise we might round down and will have no-op run. */
  131. mod_timer(&br->gc_timer, round_jiffies(next_timer + HZ/4));
  132. }
  133. /* Completely flush all dynamic entries in forwarding database.*/
  134. void br_fdb_flush(struct net_bridge *br)
  135. {
  136. int i;
  137. spin_lock_bh(&br->hash_lock);
  138. for (i = 0; i < BR_HASH_SIZE; i++) {
  139. struct net_bridge_fdb_entry *f;
  140. struct hlist_node *h, *n;
  141. hlist_for_each_entry_safe(f, h, n, &br->hash[i], hlist) {
  142. if (!f->is_static)
  143. fdb_delete(f);
  144. }
  145. }
  146. spin_unlock_bh(&br->hash_lock);
  147. }
  148. /* Flush all entries refering to a specific port.
  149. * if do_all is set also flush static entries
  150. */
  151. void br_fdb_delete_by_port(struct net_bridge *br,
  152. const struct net_bridge_port *p,
  153. int do_all)
  154. {
  155. int i;
  156. spin_lock_bh(&br->hash_lock);
  157. for (i = 0; i < BR_HASH_SIZE; i++) {
  158. struct hlist_node *h, *g;
  159. hlist_for_each_safe(h, g, &br->hash[i]) {
  160. struct net_bridge_fdb_entry *f
  161. = hlist_entry(h, struct net_bridge_fdb_entry, hlist);
  162. if (f->dst != p)
  163. continue;
  164. if (f->is_static && !do_all)
  165. continue;
  166. /*
  167. * if multiple ports all have the same device address
  168. * then when one port is deleted, assign
  169. * the local entry to other port
  170. */
  171. if (f->is_local) {
  172. struct net_bridge_port *op;
  173. list_for_each_entry(op, &br->port_list, list) {
  174. if (op != p &&
  175. !compare_ether_addr(op->dev->dev_addr,
  176. f->addr.addr)) {
  177. f->dst = op;
  178. goto skip_delete;
  179. }
  180. }
  181. }
  182. fdb_delete(f);
  183. skip_delete: ;
  184. }
  185. }
  186. spin_unlock_bh(&br->hash_lock);
  187. }
  188. /* No locking or refcounting, assumes caller has no preempt (rcu_read_lock) */
  189. struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
  190. const unsigned char *addr)
  191. {
  192. struct hlist_node *h;
  193. struct net_bridge_fdb_entry *fdb;
  194. hlist_for_each_entry_rcu(fdb, h, &br->hash[br_mac_hash(addr)], hlist) {
  195. if (!compare_ether_addr(fdb->addr.addr, addr)) {
  196. if (unlikely(has_expired(br, fdb)))
  197. break;
  198. return fdb;
  199. }
  200. }
  201. return NULL;
  202. }
  203. #if defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)
  204. /* Interface used by ATM LANE hook to test
  205. * if an addr is on some other bridge port */
  206. int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
  207. {
  208. struct net_bridge_fdb_entry *fdb;
  209. int ret;
  210. if (!dev->br_port)
  211. return 0;
  212. rcu_read_lock();
  213. fdb = __br_fdb_get(dev->br_port->br, addr);
  214. ret = fdb && fdb->dst->dev != dev &&
  215. fdb->dst->state == BR_STATE_FORWARDING;
  216. rcu_read_unlock();
  217. return ret;
  218. }
  219. #endif /* CONFIG_ATM_LANE */
  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. hlist_add_head_rcu(&fdb->hlist, head);
  280. fdb->dst = source;
  281. fdb->is_local = is_local;
  282. fdb->is_static = is_local;
  283. fdb->ageing_timer = jiffies;
  284. }
  285. return fdb;
  286. }
  287. static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  288. const unsigned char *addr)
  289. {
  290. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  291. struct net_bridge_fdb_entry *fdb;
  292. if (!is_valid_ether_addr(addr))
  293. return -EINVAL;
  294. fdb = fdb_find(head, addr);
  295. if (fdb) {
  296. /* it is okay to have multiple ports with same
  297. * address, just use the first one.
  298. */
  299. if (fdb->is_local)
  300. return 0;
  301. printk(KERN_WARNING "%s adding interface with same address "
  302. "as a received packet\n",
  303. source->dev->name);
  304. fdb_delete(fdb);
  305. }
  306. if (!fdb_create(head, source, addr, 1))
  307. return -ENOMEM;
  308. return 0;
  309. }
  310. int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  311. const unsigned char *addr)
  312. {
  313. int ret;
  314. spin_lock_bh(&br->hash_lock);
  315. ret = fdb_insert(br, source, addr);
  316. spin_unlock_bh(&br->hash_lock);
  317. return ret;
  318. }
  319. void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
  320. const unsigned char *addr)
  321. {
  322. struct hlist_head *head = &br->hash[br_mac_hash(addr)];
  323. struct net_bridge_fdb_entry *fdb;
  324. /* some users want to always flood. */
  325. if (hold_time(br) == 0)
  326. return;
  327. /* ignore packets unless we are using this port */
  328. if (!(source->state == BR_STATE_LEARNING ||
  329. source->state == BR_STATE_FORWARDING))
  330. return;
  331. fdb = fdb_find(head, addr);
  332. if (likely(fdb)) {
  333. /* attempt to update an entry for a local interface */
  334. if (unlikely(fdb->is_local)) {
  335. if (net_ratelimit())
  336. printk(KERN_WARNING "%s: received packet with "
  337. "own address as source address\n",
  338. source->dev->name);
  339. } else {
  340. /* fastpath: update of existing entry */
  341. fdb->dst = source;
  342. fdb->ageing_timer = jiffies;
  343. }
  344. } else {
  345. spin_lock(&br->hash_lock);
  346. if (!fdb_find(head, addr))
  347. fdb_create(head, source, addr, 0);
  348. /* else we lose race and someone else inserts
  349. * it first, don't bother updating
  350. */
  351. spin_unlock(&br->hash_lock);
  352. }
  353. }