blob: 9d9b3446731dde34e0d5ed2721be32a67f894a6e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INETPEER - A storage for permanent information about peers
3 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Authors: Andrey V. Savochkin <saw@msu.ru>
5 */
6
7#ifndef _NET_INETPEER_H
8#define _NET_INETPEER_H
9
10#include <linux/types.h>
11#include <linux/init.h>
12#include <linux/jiffies.h>
13#include <linux/spinlock.h>
David S. Miller60659822011-01-26 20:55:53 -080014#include <linux/rtnetlink.h>
David S. Miller672f0072010-11-30 12:20:00 -080015#include <net/ipv6.h>
Arun Sharma600634972011-07-26 16:09:06 -070016#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
David S. Miller7a71ed82011-02-09 14:30:26 -080018struct inetpeer_addr_base {
David S. Miller582a72d2010-11-30 11:53:55 -080019 union {
David S. Miller7a71ed82011-02-09 14:30:26 -080020 __be32 a4;
21 __be32 a6[4];
Jiri Benc8f55db42015-03-29 16:59:23 +020022 struct in6_addr in6;
David S. Miller582a72d2010-11-30 11:53:55 -080023 };
David S. Miller7a71ed82011-02-09 14:30:26 -080024};
25
26struct inetpeer_addr {
27 struct inetpeer_addr_base addr;
28 __u16 family;
David S. Miller8790ca12010-12-01 17:28:18 -080029};
David S. Miller582a72d2010-11-30 11:53:55 -080030
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000031struct inet_peer {
Eric Dumazet78d79422006-10-20 00:28:35 -070032 /* group together avl_left,avl_right,v4daddr to speedup lookups */
Eric Dumazetb914c4e2010-10-25 23:55:38 +000033 struct inet_peer __rcu *avl_left, *avl_right;
David S. Miller8790ca12010-12-01 17:28:18 -080034 struct inetpeer_addr daddr;
Eric Dumazet2c1409a2009-11-12 09:33:09 +000035 __u32 avl_height;
Eric Dumazet2b77bdd2011-06-08 23:31:27 -070036
37 u32 metrics[RTAX_MAX];
38 u32 rate_tokens; /* rate limiting for ICMP */
39 unsigned long rate_last;
Eric Dumazet55432d22012-06-05 03:00:18 +000040 union {
41 struct list_head gc_list;
42 struct rcu_head gc_rcu;
43 };
Eric Dumazet317fe0e2010-06-16 04:52:13 +000044 /*
Eric Dumazet73f156a2014-06-02 05:26:03 -070045 * Once inet_peer is queued for deletion (refcnt == -1), following field
46 * is not available: rid
David S. Miller60659822011-01-26 20:55:53 -080047 * We can share memory with rcu_head to help keep inet_peer small.
Eric Dumazet317fe0e2010-06-16 04:52:13 +000048 */
49 union {
50 struct {
David S. Millerddd4aa42011-02-09 15:36:47 -080051 atomic_t rid; /* Frag reception counter */
Eric Dumazet317fe0e2010-06-16 04:52:13 +000052 };
53 struct rcu_head rcu;
Eric Dumazet4b9d9be2011-06-08 13:35:34 +000054 struct inet_peer *gc_next;
Eric Dumazet317fe0e2010-06-16 04:52:13 +000055 };
Eric Dumazet2b77bdd2011-06-08 23:31:27 -070056
57 /* following fields might be frequently dirtied */
58 __u32 dtime; /* the time of last use of not referenced entries */
59 atomic_t refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
David S. Millerc3426b42012-06-09 16:27:05 -070062struct inet_peer_base {
63 struct inet_peer __rcu *root;
64 seqlock_t lock;
65 int total;
66};
67
Joe Perches1fd51152013-09-21 10:22:41 -070068void inet_peer_base_init(struct inet_peer_base *);
David S. Millerc3426b42012-06-09 16:27:05 -070069
Joe Perches1fd51152013-09-21 10:22:41 -070070void inet_initpeers(void) __init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
David S. Miller144001b2011-01-27 13:52:16 -080072#define INETPEER_METRICS_NEW (~(u32) 0)
73
David Ahern3abef282015-08-27 16:07:00 -070074static inline void inetpeer_set_addr_v4(struct inetpeer_addr *iaddr, __be32 ip)
75{
76 iaddr->addr.a4 = ip;
77 iaddr->family = AF_INET;
78}
79
80static inline __be32 inetpeer_get_addr_v4(struct inetpeer_addr *iaddr)
81{
82 return iaddr->addr.a4;
83}
84
85static inline void inetpeer_set_addr_v6(struct inetpeer_addr *iaddr,
86 struct in6_addr *in6)
87{
88 iaddr->addr.in6 = *in6;
89 iaddr->family = AF_INET6;
90}
91
92static inline struct in6_addr *inetpeer_get_addr_v6(struct inetpeer_addr *iaddr)
93{
94 return &iaddr->addr.in6;
95}
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* can be called with or without local BH being disabled */
David S. Millerc0efc882012-06-09 19:12:36 -070098struct inet_peer *inet_getpeer(struct inet_peer_base *base,
Gao fengc8a627e2012-06-08 01:20:41 +000099 const struct inetpeer_addr *daddr,
100 int create);
David S. Millerb534ecf2010-11-30 11:54:19 -0800101
David S. Millerc0efc882012-06-09 19:12:36 -0700102static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
Gao feng54db0cc2012-06-08 01:21:40 +0000103 __be32 v4daddr,
104 int create)
David S. Millerb534ecf2010-11-30 11:54:19 -0800105{
David S. Miller8790ca12010-12-01 17:28:18 -0800106 struct inetpeer_addr daddr;
David S. Millerb534ecf2010-11-30 11:54:19 -0800107
David S. Miller7a71ed82011-02-09 14:30:26 -0800108 daddr.addr.a4 = v4daddr;
David S. Millerb534ecf2010-11-30 11:54:19 -0800109 daddr.family = AF_INET;
David S. Millerc0efc882012-06-09 19:12:36 -0700110 return inet_getpeer(base, &daddr, create);
David S. Millerb534ecf2010-11-30 11:54:19 -0800111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
David S. Millerc0efc882012-06-09 19:12:36 -0700113static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
Gao feng54db0cc2012-06-08 01:21:40 +0000114 const struct in6_addr *v6daddr,
115 int create)
David S. Miller672f0072010-11-30 12:20:00 -0800116{
David S. Miller8790ca12010-12-01 17:28:18 -0800117 struct inetpeer_addr daddr;
David S. Miller672f0072010-11-30 12:20:00 -0800118
Jiri Benc8f55db42015-03-29 16:59:23 +0200119 daddr.addr.in6 = *v6daddr;
David S. Miller672f0072010-11-30 12:20:00 -0800120 daddr.family = AF_INET6;
David S. Millerc0efc882012-06-09 19:12:36 -0700121 return inet_getpeer(base, &daddr, create);
David S. Miller672f0072010-11-30 12:20:00 -0800122}
123
David Ahernd39d14f2015-08-27 16:07:01 -0700124static inline int inetpeer_addr_cmp(const struct inetpeer_addr *a,
125 const struct inetpeer_addr *b)
126{
127 int i, n = (a->family == AF_INET ? 1 : 4);
128
129 for (i = 0; i < n; i++) {
130 if (a->addr.a6[i] == b->addr.a6[i])
131 continue;
132 if ((__force u32)a->addr.a6[i] < (__force u32)b->addr.a6[i])
133 return -1;
134 return 1;
135 }
136
137 return 0;
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/* can be called from BH context or outside */
Joe Perches1fd51152013-09-21 10:22:41 -0700141void inet_putpeer(struct inet_peer *p);
142bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Joe Perches1fd51152013-09-21 10:22:41 -0700144void inetpeer_invalidate_tree(struct inet_peer_base *);
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#endif /* _NET_INETPEER_H */