blob: 74ff688568a0c6559946a9ae763d5c9822f1d112 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INETPEER - A storage for permanent information about peers
4 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
David S. Miller60659822011-01-26 20:55:53 -080015#include <linux/rtnetlink.h>
David S. Miller672f0072010-11-30 12:20:00 -080016#include <net/ipv6.h>
Arun Sharma600634972011-07-26 16:09:06 -070017#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
David Ahern192132b2015-08-27 16:07:03 -070019/* IPv4 address key for cache lookups */
20struct ipv4_addr_key {
21 __be32 addr;
22 int vif;
23};
24
David Ahern5345c2e2015-08-27 16:07:02 -070025#define INETPEER_MAXKEYSZ (sizeof(struct in6_addr) / sizeof(u32))
David S. Miller7a71ed82011-02-09 14:30:26 -080026
27struct inetpeer_addr {
David Ahern5345c2e2015-08-27 16:07:02 -070028 union {
David Ahern192132b2015-08-27 16:07:03 -070029 struct ipv4_addr_key a4;
David Ahern5345c2e2015-08-27 16:07:02 -070030 struct in6_addr a6;
31 u32 key[INETPEER_MAXKEYSZ];
32 };
David S. Miller7a71ed82011-02-09 14:30:26 -080033 __u16 family;
David S. Miller8790ca12010-12-01 17:28:18 -080034};
David S. Miller582a72d2010-11-30 11:53:55 -080035
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000036struct inet_peer {
Eric Dumazetb1454252017-07-17 02:56:10 -070037 struct rb_node rb_node;
David S. Miller8790ca12010-12-01 17:28:18 -080038 struct inetpeer_addr daddr;
Eric Dumazet2b77bdd2011-06-08 23:31:27 -070039
40 u32 metrics[RTAX_MAX];
41 u32 rate_tokens; /* rate limiting for ICMP */
Lorenzo Bianconic09551c62019-02-06 19:18:04 +010042 u32 n_redirects;
Eric Dumazet2b77bdd2011-06-08 23:31:27 -070043 unsigned long rate_last;
Eric Dumazet317fe0e2010-06-16 04:52:13 +000044 /*
Reshetova, Elena1cc9a982017-06-30 13:07:54 +030045 * Once inet_peer is queued for deletion (refcnt == 0), following field
Eric Dumazet73f156a2014-06-02 05:26:03 -070046 * 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;
54 };
Eric Dumazet2b77bdd2011-06-08 23:31:27 -070055
56 /* following fields might be frequently dirtied */
57 __u32 dtime; /* the time of last use of not referenced entries */
Reshetova, Elena1cc9a982017-06-30 13:07:54 +030058 refcount_t refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
David S. Millerc3426b42012-06-09 16:27:05 -070061struct inet_peer_base {
Eric Dumazetb1454252017-07-17 02:56:10 -070062 struct rb_root rb_root;
David S. Millerc3426b42012-06-09 16:27:05 -070063 seqlock_t lock;
64 int total;
65};
66
Joe Perches1fd51152013-09-21 10:22:41 -070067void inet_peer_base_init(struct inet_peer_base *);
David S. Millerc3426b42012-06-09 16:27:05 -070068
Joe Perches1fd51152013-09-21 10:22:41 -070069void inet_initpeers(void) __init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
David S. Miller144001b2011-01-27 13:52:16 -080071#define INETPEER_METRICS_NEW (~(u32) 0)
72
David Ahern3abef282015-08-27 16:07:00 -070073static inline void inetpeer_set_addr_v4(struct inetpeer_addr *iaddr, __be32 ip)
74{
David Ahern192132b2015-08-27 16:07:03 -070075 iaddr->a4.addr = ip;
Eric Dumazet887dc9f2015-12-15 20:56:44 -080076 iaddr->a4.vif = 0;
David Ahern3abef282015-08-27 16:07:00 -070077 iaddr->family = AF_INET;
78}
79
80static inline __be32 inetpeer_get_addr_v4(struct inetpeer_addr *iaddr)
81{
David Ahern192132b2015-08-27 16:07:03 -070082 return iaddr->a4.addr;
David Ahern3abef282015-08-27 16:07:00 -070083}
84
85static inline void inetpeer_set_addr_v6(struct inetpeer_addr *iaddr,
86 struct in6_addr *in6)
87{
David Ahern5345c2e2015-08-27 16:07:02 -070088 iaddr->a6 = *in6;
David Ahern3abef282015-08-27 16:07:00 -070089 iaddr->family = AF_INET6;
90}
91
92static inline struct in6_addr *inetpeer_get_addr_v6(struct inetpeer_addr *iaddr)
93{
David Ahern5345c2e2015-08-27 16:07:02 -070094 return &iaddr->a6;
David Ahern3abef282015-08-27 16:07:00 -070095}
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,
David Ahern192132b2015-08-27 16:07:03 -0700104 int vif, 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 Ahern192132b2015-08-27 16:07:03 -0700108 daddr.a4.addr = v4daddr;
109 daddr.a4.vif = vif;
David S. Millerb534ecf2010-11-30 11:54:19 -0800110 daddr.family = AF_INET;
David S. Millerc0efc882012-06-09 19:12:36 -0700111 return inet_getpeer(base, &daddr, create);
David S. Millerb534ecf2010-11-30 11:54:19 -0800112}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
David S. Millerc0efc882012-06-09 19:12:36 -0700114static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
Gao feng54db0cc2012-06-08 01:21:40 +0000115 const struct in6_addr *v6daddr,
116 int create)
David S. Miller672f0072010-11-30 12:20:00 -0800117{
David S. Miller8790ca12010-12-01 17:28:18 -0800118 struct inetpeer_addr daddr;
David S. Miller672f0072010-11-30 12:20:00 -0800119
David Ahern5345c2e2015-08-27 16:07:02 -0700120 daddr.a6 = *v6daddr;
David S. Miller672f0072010-11-30 12:20:00 -0800121 daddr.family = AF_INET6;
David S. Millerc0efc882012-06-09 19:12:36 -0700122 return inet_getpeer(base, &daddr, create);
David S. Miller672f0072010-11-30 12:20:00 -0800123}
124
David Ahernd39d14f2015-08-27 16:07:01 -0700125static inline int inetpeer_addr_cmp(const struct inetpeer_addr *a,
126 const struct inetpeer_addr *b)
127{
David Ahern5345c2e2015-08-27 16:07:02 -0700128 int i, n;
129
130 if (a->family == AF_INET)
131 n = sizeof(a->a4) / sizeof(u32);
132 else
133 n = sizeof(a->a6) / sizeof(u32);
David Ahernd39d14f2015-08-27 16:07:01 -0700134
135 for (i = 0; i < n; i++) {
David Ahern5345c2e2015-08-27 16:07:02 -0700136 if (a->key[i] == b->key[i])
David Ahernd39d14f2015-08-27 16:07:01 -0700137 continue;
David Ahern5345c2e2015-08-27 16:07:02 -0700138 if (a->key[i] < b->key[i])
David Ahernd39d14f2015-08-27 16:07:01 -0700139 return -1;
140 return 1;
141 }
142
143 return 0;
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* can be called from BH context or outside */
Joe Perches1fd51152013-09-21 10:22:41 -0700147void inet_putpeer(struct inet_peer *p);
148bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Joe Perches1fd51152013-09-21 10:22:41 -0700150void inetpeer_invalidate_tree(struct inet_peer_base *);
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#endif /* _NET_INETPEER_H */