]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - include/net/inetpeer.h
Linux 4.14-rc6
[mirror_ubuntu-eoan-kernel.git] / include / net / inetpeer.h
CommitLineData
1da177e4
LT
1/*
2 * INETPEER - A storage for permanent information about peers
3 *
1da177e4
LT
4 * 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>
60659823 14#include <linux/rtnetlink.h>
672f007d 15#include <net/ipv6.h>
60063497 16#include <linux/atomic.h>
1da177e4 17
192132b9
DA
18/* IPv4 address key for cache lookups */
19struct ipv4_addr_key {
20 __be32 addr;
21 int vif;
22};
23
5345c2e1
DA
24#define INETPEER_MAXKEYSZ (sizeof(struct in6_addr) / sizeof(u32))
25
26struct inetpeer_addr {
582a72da 27 union {
192132b9 28 struct ipv4_addr_key a4;
5345c2e1
DA
29 struct in6_addr a6;
30 u32 key[INETPEER_MAXKEYSZ];
582a72da 31 };
7a71ed89 32 __u16 family;
8790ca17 33};
582a72da 34
fd2c3ef7 35struct inet_peer {
b145425f 36 struct rb_node rb_node;
8790ca17 37 struct inetpeer_addr daddr;
2b77bdde
ED
38
39 u32 metrics[RTAX_MAX];
40 u32 rate_tokens; /* rate limiting for ICMP */
41 unsigned long rate_last;
317fe0e6 42 /*
1cc9a98b 43 * Once inet_peer is queued for deletion (refcnt == 0), following field
73f156a6 44 * is not available: rid
60659823 45 * We can share memory with rcu_head to help keep inet_peer small.
317fe0e6
ED
46 */
47 union {
48 struct {
ddd4aa42 49 atomic_t rid; /* Frag reception counter */
317fe0e6
ED
50 };
51 struct rcu_head rcu;
52 };
2b77bdde
ED
53
54 /* following fields might be frequently dirtied */
55 __u32 dtime; /* the time of last use of not referenced entries */
1cc9a98b 56 refcount_t refcnt;
1da177e4
LT
57};
58
c3426b47 59struct inet_peer_base {
b145425f 60 struct rb_root rb_root;
c3426b47
DM
61 seqlock_t lock;
62 int total;
63};
64
1fd51155 65void inet_peer_base_init(struct inet_peer_base *);
c3426b47 66
1fd51155 67void inet_initpeers(void) __init;
1da177e4 68
144001bd
DM
69#define INETPEER_METRICS_NEW (~(u32) 0)
70
3abef286
DA
71static inline void inetpeer_set_addr_v4(struct inetpeer_addr *iaddr, __be32 ip)
72{
192132b9 73 iaddr->a4.addr = ip;
887dc9f2 74 iaddr->a4.vif = 0;
3abef286
DA
75 iaddr->family = AF_INET;
76}
77
78static inline __be32 inetpeer_get_addr_v4(struct inetpeer_addr *iaddr)
79{
192132b9 80 return iaddr->a4.addr;
3abef286
DA
81}
82
83static inline void inetpeer_set_addr_v6(struct inetpeer_addr *iaddr,
84 struct in6_addr *in6)
85{
5345c2e1 86 iaddr->a6 = *in6;
3abef286
DA
87 iaddr->family = AF_INET6;
88}
89
90static inline struct in6_addr *inetpeer_get_addr_v6(struct inetpeer_addr *iaddr)
91{
5345c2e1 92 return &iaddr->a6;
3abef286
DA
93}
94
1da177e4 95/* can be called with or without local BH being disabled */
c0efc887 96struct inet_peer *inet_getpeer(struct inet_peer_base *base,
c8a627ed
G
97 const struct inetpeer_addr *daddr,
98 int create);
b534ecf1 99
c0efc887 100static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
54db0cc2 101 __be32 v4daddr,
192132b9 102 int vif, int create)
b534ecf1 103{
8790ca17 104 struct inetpeer_addr daddr;
b534ecf1 105
192132b9
DA
106 daddr.a4.addr = v4daddr;
107 daddr.a4.vif = vif;
b534ecf1 108 daddr.family = AF_INET;
c0efc887 109 return inet_getpeer(base, &daddr, create);
b534ecf1 110}
1da177e4 111
c0efc887 112static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
54db0cc2
G
113 const struct in6_addr *v6daddr,
114 int create)
672f007d 115{
8790ca17 116 struct inetpeer_addr daddr;
672f007d 117
5345c2e1 118 daddr.a6 = *v6daddr;
672f007d 119 daddr.family = AF_INET6;
c0efc887 120 return inet_getpeer(base, &daddr, create);
672f007d
DM
121}
122
d39d14ff
DA
123static inline int inetpeer_addr_cmp(const struct inetpeer_addr *a,
124 const struct inetpeer_addr *b)
125{
5345c2e1
DA
126 int i, n;
127
128 if (a->family == AF_INET)
129 n = sizeof(a->a4) / sizeof(u32);
130 else
131 n = sizeof(a->a6) / sizeof(u32);
d39d14ff
DA
132
133 for (i = 0; i < n; i++) {
5345c2e1 134 if (a->key[i] == b->key[i])
d39d14ff 135 continue;
5345c2e1 136 if (a->key[i] < b->key[i])
d39d14ff
DA
137 return -1;
138 return 1;
139 }
140
141 return 0;
142}
143
1da177e4 144/* can be called from BH context or outside */
1fd51155
JP
145void inet_putpeer(struct inet_peer *p);
146bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
1da177e4 147
1fd51155 148void inetpeer_invalidate_tree(struct inet_peer_base *);
5faa5df1 149
1da177e4 150#endif /* _NET_INETPEER_H */