br_fdb.c 9.7 KB

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