1 /* SPDX-License-Identifier: GPL-2.0 */
3 * INETPEER - A storage for permanent information about peers
5 * Authors: Andrey V. Savochkin <saw@msu.ru>
8 #ifndef _NET_INETPEER_H
9 #define _NET_INETPEER_H
11 #include <linux/types.h>
12 #include <linux/init.h>
13 #include <linux/jiffies.h>
14 #include <linux/spinlock.h>
15 #include <linux/rtnetlink.h>
17 #include <linux/atomic.h>
19 /* IPv4 address key for cache lookups */
20 struct ipv4_addr_key
{
25 #define INETPEER_MAXKEYSZ (sizeof(struct in6_addr) / sizeof(u32))
27 struct inetpeer_addr
{
29 struct ipv4_addr_key a4
;
31 u32 key
[INETPEER_MAXKEYSZ
];
37 struct rb_node rb_node
;
38 struct inetpeer_addr daddr
;
40 u32 metrics
[RTAX_MAX
];
41 u32 rate_tokens
; /* rate limiting for ICMP */
43 unsigned long rate_last
;
45 * Once inet_peer is queued for deletion (refcnt == 0), following field
46 * is not available: rid
47 * We can share memory with rcu_head to help keep inet_peer small.
51 atomic_t rid
; /* Frag reception counter */
56 /* following fields might be frequently dirtied */
57 __u32 dtime
; /* the time of last use of not referenced entries */
61 struct inet_peer_base
{
62 struct rb_root rb_root
;
67 void inet_peer_base_init(struct inet_peer_base
*);
69 void inet_initpeers(void) __init
;
71 #define INETPEER_METRICS_NEW (~(u32) 0)
73 static inline void inetpeer_set_addr_v4(struct inetpeer_addr
*iaddr
, __be32 ip
)
77 iaddr
->family
= AF_INET
;
80 static inline __be32
inetpeer_get_addr_v4(struct inetpeer_addr
*iaddr
)
82 return iaddr
->a4
.addr
;
85 static inline void inetpeer_set_addr_v6(struct inetpeer_addr
*iaddr
,
89 iaddr
->family
= AF_INET6
;
92 static inline struct in6_addr
*inetpeer_get_addr_v6(struct inetpeer_addr
*iaddr
)
97 /* can be called with or without local BH being disabled */
98 struct inet_peer
*inet_getpeer(struct inet_peer_base
*base
,
99 const struct inetpeer_addr
*daddr
,
102 static inline struct inet_peer
*inet_getpeer_v4(struct inet_peer_base
*base
,
106 struct inetpeer_addr daddr
;
108 daddr
.a4
.addr
= v4daddr
;
110 daddr
.family
= AF_INET
;
111 return inet_getpeer(base
, &daddr
, create
);
114 static inline struct inet_peer
*inet_getpeer_v6(struct inet_peer_base
*base
,
115 const struct in6_addr
*v6daddr
,
118 struct inetpeer_addr daddr
;
121 daddr
.family
= AF_INET6
;
122 return inet_getpeer(base
, &daddr
, create
);
125 static inline int inetpeer_addr_cmp(const struct inetpeer_addr
*a
,
126 const struct inetpeer_addr
*b
)
130 if (a
->family
== AF_INET
)
131 n
= sizeof(a
->a4
) / sizeof(u32
);
133 n
= sizeof(a
->a6
) / sizeof(u32
);
135 for (i
= 0; i
< n
; i
++) {
136 if (a
->key
[i
] == b
->key
[i
])
138 if (a
->key
[i
] < b
->key
[i
])
146 /* can be called from BH context or outside */
147 void inet_putpeer(struct inet_peer
*p
);
148 bool inet_peer_xrlim_allow(struct inet_peer
*peer
, int timeout
);
150 void inetpeer_invalidate_tree(struct inet_peer_base
*);
152 #endif /* _NET_INETPEER_H */