blob: 4a6009d4486b9cfc914d583c5065a3be3d23fa7e [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 Ahern192132b2015-08-27 16:07:03 -070018/* IPv4 address key for cache lookups */
19struct ipv4_addr_key {
20 __be32 addr;
21 int vif;
22};
23
David Ahern5345c2e2015-08-27 16:07:02 -070024#define INETPEER_MAXKEYSZ (sizeof(struct in6_addr) / sizeof(u32))
David S. Miller7a71ed82011-02-09 14:30:26 -080025
26struct inetpeer_addr {
David Ahern5345c2e2015-08-27 16:07:02 -070027 union {
David Ahern192132b2015-08-27 16:07:03 -070028 struct ipv4_addr_key a4;
David Ahern5345c2e2015-08-27 16:07:02 -070029 struct in6_addr a6;
30 u32 key[INETPEER_MAXKEYSZ];
31 };
David S. Miller7a71ed82011-02-09 14:30:26 -080032 __u16 family;
David S. Miller8790ca12010-12-01 17:28:18 -080033};
David S. Miller582a72d2010-11-30 11:53:55 -080034
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000035struct inet_peer {
Eric Dumazet78d79422006-10-20 00:28:35 -070036 /* group together avl_left,avl_right,v4daddr to speedup lookups */
Eric Dumazetb914c4e2010-10-25 23:55:38 +000037 struct inet_peer __rcu *avl_left, *avl_right;
David S. Miller8790ca12010-12-01 17:28:18 -080038 struct inetpeer_addr daddr;
Eric Dumazet2c1409a2009-11-12 09:33:09 +000039 __u32 avl_height;
Eric Dumazet2b77bdd2011-06-08 23:31:27 -070040
41 u32 metrics[RTAX_MAX];
42 u32 rate_tokens; /* rate limiting for ICMP */
43 unsigned long rate_last;
Eric Dumazet55432d22012-06-05 03:00:18 +000044 union {
45 struct list_head gc_list;
46 struct rcu_head gc_rcu;
47 };
Eric Dumazet317fe0e2010-06-16 04:52:13 +000048 /*
Eric Dumazet73f156a2014-06-02 05:26:03 -070049 * Once inet_peer is queued for deletion (refcnt == -1), following field
50 * is not available: rid
David S. Miller60659822011-01-26 20:55:53 -080051 * We can share memory with rcu_head to help keep inet_peer small.
Eric Dumazet317fe0e2010-06-16 04:52:13 +000052 */
53 union {
54 struct {
David S. Millerddd4aa42011-02-09 15:36:47 -080055 atomic_t rid; /* Frag reception counter */
Eric Dumazet317fe0e2010-06-16 04:52:13 +000056 };
57 struct rcu_head rcu;
Eric Dumazet4b9d9be2011-06-08 13:35:34 +000058 struct inet_peer *gc_next;
Eric Dumazet317fe0e2010-06-16 04:52:13 +000059 };
Eric Dumazet2b77bdd2011-06-08 23:31:27 -070060
61 /* following fields might be frequently dirtied */
62 __u32 dtime; /* the time of last use of not referenced entries */
63 atomic_t refcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
David S. Millerc3426b42012-06-09 16:27:05 -070066struct inet_peer_base {
67 struct inet_peer __rcu *root;
68 seqlock_t lock;
69 int total;
70};
71
Joe Perches1fd51152013-09-21 10:22:41 -070072void inet_peer_base_init(struct inet_peer_base *);
David S. Millerc3426b42012-06-09 16:27:05 -070073
Joe Perches1fd51152013-09-21 10:22:41 -070074void inet_initpeers(void) __init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
David S. Miller144001b2011-01-27 13:52:16 -080076#define INETPEER_METRICS_NEW (~(u32) 0)
77
David Ahern3abef282015-08-27 16:07:00 -070078static inline void inetpeer_set_addr_v4(struct inetpeer_addr *iaddr, __be32 ip)
79{
David Ahern192132b2015-08-27 16:07:03 -070080 iaddr->a4.addr = ip;
David Ahern3abef282015-08-27 16:07:00 -070081 iaddr->family = AF_INET;
82}
83
84static inline __be32 inetpeer_get_addr_v4(struct inetpeer_addr *iaddr)
85{
David Ahern192132b2015-08-27 16:07:03 -070086 return iaddr->a4.addr;
David Ahern3abef282015-08-27 16:07:00 -070087}
88
89static inline void inetpeer_set_addr_v6(struct inetpeer_addr *iaddr,
90 struct in6_addr *in6)
91{
David Ahern5345c2e2015-08-27 16:07:02 -070092 iaddr->a6 = *in6;
David Ahern3abef282015-08-27 16:07:00 -070093 iaddr->family = AF_INET6;
94}
95
96static inline struct in6_addr *inetpeer_get_addr_v6(struct inetpeer_addr *iaddr)
97{
David Ahern5345c2e2015-08-27 16:07:02 -070098 return &iaddr->a6;
David Ahern3abef282015-08-27 16:07:00 -070099}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* can be called with or without local BH being disabled */
David S. Millerc0efc882012-06-09 19:12:36 -0700102struct inet_peer *inet_getpeer(struct inet_peer_base *base,
Gao fengc8a627e2012-06-08 01:20:41 +0000103 const struct inetpeer_addr *daddr,
104 int create);
David S. Millerb534ecf2010-11-30 11:54:19 -0800105
David S. Millerc0efc882012-06-09 19:12:36 -0700106static inline struct inet_peer *inet_getpeer_v4(struct inet_peer_base *base,
Gao feng54db0cc2012-06-08 01:21:40 +0000107 __be32 v4daddr,
David Ahern192132b2015-08-27 16:07:03 -0700108 int vif, int create)
David S. Millerb534ecf2010-11-30 11:54:19 -0800109{
David S. Miller8790ca12010-12-01 17:28:18 -0800110 struct inetpeer_addr daddr;
David S. Millerb534ecf2010-11-30 11:54:19 -0800111
David Ahern192132b2015-08-27 16:07:03 -0700112 daddr.a4.addr = v4daddr;
113 daddr.a4.vif = vif;
David S. Millerb534ecf2010-11-30 11:54:19 -0800114 daddr.family = AF_INET;
David S. Millerc0efc882012-06-09 19:12:36 -0700115 return inet_getpeer(base, &daddr, create);
David S. Millerb534ecf2010-11-30 11:54:19 -0800116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
David S. Millerc0efc882012-06-09 19:12:36 -0700118static inline struct inet_peer *inet_getpeer_v6(struct inet_peer_base *base,
Gao feng54db0cc2012-06-08 01:21:40 +0000119 const struct in6_addr *v6daddr,
120 int create)
David S. Miller672f0072010-11-30 12:20:00 -0800121{
David S. Miller8790ca12010-12-01 17:28:18 -0800122 struct inetpeer_addr daddr;
David S. Miller672f0072010-11-30 12:20:00 -0800123
David Ahern5345c2e2015-08-27 16:07:02 -0700124 daddr.a6 = *v6daddr;
David S. Miller672f0072010-11-30 12:20:00 -0800125 daddr.family = AF_INET6;
David S. Millerc0efc882012-06-09 19:12:36 -0700126 return inet_getpeer(base, &daddr, create);
David S. Miller672f0072010-11-30 12:20:00 -0800127}
128
David Ahernd39d14f2015-08-27 16:07:01 -0700129static inline int inetpeer_addr_cmp(const struct inetpeer_addr *a,
130 const struct inetpeer_addr *b)
131{
David Ahern5345c2e2015-08-27 16:07:02 -0700132 int i, n;
133
134 if (a->family == AF_INET)
135 n = sizeof(a->a4) / sizeof(u32);
136 else
137 n = sizeof(a->a6) / sizeof(u32);
David Ahernd39d14f2015-08-27 16:07:01 -0700138
139 for (i = 0; i < n; i++) {
David Ahern5345c2e2015-08-27 16:07:02 -0700140 if (a->key[i] == b->key[i])
David Ahernd39d14f2015-08-27 16:07:01 -0700141 continue;
David Ahern5345c2e2015-08-27 16:07:02 -0700142 if (a->key[i] < b->key[i])
David Ahernd39d14f2015-08-27 16:07:01 -0700143 return -1;
144 return 1;
145 }
146
147 return 0;
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* can be called from BH context or outside */
Joe Perches1fd51152013-09-21 10:22:41 -0700151void inet_putpeer(struct inet_peer *p);
152bool inet_peer_xrlim_allow(struct inet_peer *peer, int timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Joe Perches1fd51152013-09-21 10:22:41 -0700154void inetpeer_invalidate_tree(struct inet_peer_base *);
Steffen Klassert5faa5df2012-03-06 21:20:26 +0000155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#endif /* _NET_INETPEER_H */