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