blob: d5332ddcea3f3b58fb7f38254d6c8a7e84de21eb [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
David S. Miller97bab732012-06-09 22:36:36 -070068#define INETPEER_BASE_BIT 0x1UL
69
70static inline struct inet_peer *inetpeer_ptr(unsigned long val)
71{
72 BUG_ON(val & INETPEER_BASE_BIT);
73 return (struct inet_peer *) val;
74}
75
76static inline struct inet_peer_base *inetpeer_base_ptr(unsigned long val)
77{
78 if (!(val & INETPEER_BASE_BIT))
79 return NULL;
80 val &= ~INETPEER_BASE_BIT;
81 return (struct inet_peer_base *) val;
82}
83
84static inline bool inetpeer_ptr_is_peer(unsigned long val)
85{
86 return !(val & INETPEER_BASE_BIT);
87}
88
89static inline void __inetpeer_ptr_set_peer(unsigned long *val, struct inet_peer *peer)
90{
91 /* This implicitly clears INETPEER_BASE_BIT */
92 *val = (unsigned long) peer;
93}
94
95static inline bool inetpeer_ptr_set_peer(unsigned long *ptr, struct inet_peer *peer)
96{
97 unsigned long val = (unsigned long) peer;
98 unsigned long orig = *ptr;
99
David S. Miller7b34ca22012-06-11 04:13:57 -0700100 if (!(orig & INETPEER_BASE_BIT) ||
David S. Miller97bab732012-06-09 22:36:36 -0700101 cmpxchg(ptr, orig, val) != orig)
102 return false;
103 return true;
104}
105
106static inline void inetpeer_init_ptr(unsigned long *ptr, struct inet_peer_base *base)
107{
108 *ptr = (unsigned long) base | INETPEER_BASE_BIT;
109}
110
111static inline void inetpeer_transfer_peer(unsigned long *to, unsigned long *from)
112{
113 unsigned long val = *from;
114
115 *to = val;
116 if (inetpeer_ptr_is_peer(val)) {
117 struct inet_peer *peer = inetpeer_ptr(val);
118 atomic_inc(&peer->refcnt);
119 }
120}
121
Joe Perches1fd51152013-09-21 10:22:41 -0700122void inet_peer_base_init(struct inet_peer_base *);
David S. Millerc3426b42012-06-09 16:27:05 -0700123
Joe Perches1fd51152013-09-21 10:22:41 -0700124void inet_initpeers(void) __init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
David S. Miller144001b2011-01-27 13:52:16 -0800126#define INETPEER_METRICS_NEW (~(u32) 0)
127
128static inline bool inet_metrics_new(const struct inet_peer *p)
129{
130 return p->metrics[RTAX_LOCK-1] == INETPEER_METRICS_NEW;
131}
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/* can be called with or without local BH being disabled */
David S. Millerc0efc882012-06-09 19:12:36 -0700134struct inet_peer *inet_getpeer(struct inet_peer_base *base,
Gao fengc8a627e2012-06-08 01:20:41 +0000135 const struct inetpeer_addr *daddr,
136 int create);
David S. Millerb534ecf2010-11-30 11:54:19 -0800137
David S. Millerc0efc882012-06-09 19:12:36 -0700138static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
Gao feng54db0cc2012-06-08 01:21:40 +0000139 __be32 v4daddr,
140 int create)
David S. Millerb534ecf2010-11-30 11:54:19 -0800141{
David S. Miller8790ca12010-12-01 17:28:18 -0800142 struct inetpeer_addr daddr;
David S. Millerb534ecf2010-11-30 11:54:19 -0800143
David S. Miller7a71ed82011-02-09 14:30:26 -0800144 daddr.addr.a4 = v4daddr;
David S. Millerb534ecf2010-11-30 11:54:19 -0800145 daddr.family = AF_INET;
David S. Millerc0efc882012-06-09 19:12:36 -0700146 return inet_getpeer(base, &daddr, create);
David S. Millerb534ecf2010-11-30 11:54:19 -0800147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
David S. Millerc0efc882012-06-09 19:12:36 -0700149static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
Gao feng54db0cc2012-06-08 01:21:40 +0000150 const struct in6_addr *v6daddr,
151 int create)
David S. Miller672f0072010-11-30 12:20:00 -0800152{
David S. Miller8790ca12010-12-01 17:28:18 -0800153 struct inetpeer_addr daddr;
David S. Miller672f0072010-11-30 12:20:00 -0800154
Jiri Benc8f55db42015-03-29 16:59:23 +0200155 daddr.addr.in6 = *v6daddr;
David S. Miller672f0072010-11-30 12:20:00 -0800156 daddr.family = AF_INET6;
David S. Millerc0efc882012-06-09 19:12:36 -0700157 return inet_getpeer(base, &daddr, create);
David S. Miller672f0072010-11-30 12:20:00 -0800158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* can be called from BH context or outside */
Joe Perches1fd51152013-09-21 10:22:41 -0700161void inet_putpeer(struct inet_peer *p);
162bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Joe Perches1fd51152013-09-21 10:22:41 -0700164void inetpeer_invalidate_tree(struct inet_peer_base *);
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000165
Eric Dumazet317fe0e2010-06-16 04:52:13 +0000166/*
Eric Dumazet73f156a2014-06-02 05:26:03 -0700167 * temporary check to make sure we dont access rid, tcp_ts,
Eric Dumazet317fe0e2010-06-16 04:52:13 +0000168 * tcp_ts_stamp if no refcount is taken on inet_peer
169 */
170static inline void inet_peer_refcheck(const struct inet_peer *p)
171{
172 WARN_ON_ONCE(atomic_read(&p->refcnt) <= 0);
173}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#endif /* _NET_INETPEER_H */