]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - net/ipv6/syncookies.c
Merge tag 'net-next-6.10' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev...
[thirdparty/kernel/linux.git] / net / ipv6 / syncookies.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
c6aefafb
GG
2/*
3 * IPv6 Syncookies implementation for the Linux kernel
4 *
5 * Authors:
6 * Glenn Griffin <ggriffin.kernel@gmail.com>
7 *
8 * Based on IPv4 implementation by Andi Kleen
9 * linux/net/ipv4/syncookies.c
c6aefafb
GG
10 */
11
12#include <linux/tcp.h>
13#include <linux/random.h>
fe62d05b 14#include <linux/siphash.h>
c6aefafb 15#include <linux/kernel.h>
84b114b9 16#include <net/secure_seq.h>
c6aefafb
GG
17#include <net/ipv6.h>
18#include <net/tcp.h>
19
c6aefafb
GG
20#define COOKIEBITS 24 /* Upper bits store count */
21#define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
22
49ecc2e9 23static siphash_aligned_key_t syncookie6_secret[2];
b23a002f 24
08629354
FW
25/* RFC 2460, Section 8.3:
26 * [ipv6 tcp] MSS must be computed as the maximum packet size minus 60 [..]
27 *
28 * Due to IPV6_MIN_MTU=1280 the lowest possible MSS is 1220, which allows
29 * using higher values than ipv4 tcp syncookies.
30 * The other values are chosen based on ethernet (1500 and 9k MTU), plus
31 * one that accounts for common encap (PPPoe) overhead. Table must be sorted.
32 */
c6aefafb 33static __u16 const msstab[] = {
08629354 34 1280 - 60, /* IPV6_MIN_MTU - 60 */
5918e2fb
FW
35 1480 - 60,
36 1500 - 60,
5918e2fb 37 9000 - 60,
c6aefafb 38};
c6aefafb 39
fe62d05b
JD
40static u32 cookie_hash(const struct in6_addr *saddr,
41 const struct in6_addr *daddr,
c6aefafb
GG
42 __be16 sport, __be16 dport, u32 count, int c)
43{
fe62d05b
JD
44 const struct {
45 struct in6_addr saddr;
46 struct in6_addr daddr;
47 u32 count;
48 __be16 sport;
49 __be16 dport;
50 } __aligned(SIPHASH_ALIGNMENT) combined = {
51 .saddr = *saddr,
52 .daddr = *daddr,
53 .count = count,
54 .sport = sport,
55 .dport = dport
56 };
b23a002f
HFS
57
58 net_get_random_once(syncookie6_secret, sizeof(syncookie6_secret));
fe62d05b
JD
59 return siphash(&combined, offsetofend(typeof(combined), dport),
60 &syncookie6_secret[c]);
c6aefafb
GG
61}
62
b71d1d42
ED
63static __u32 secure_tcp_syn_cookie(const struct in6_addr *saddr,
64 const struct in6_addr *daddr,
c6aefafb 65 __be16 sport, __be16 dport, __u32 sseq,
8c27bd75 66 __u32 data)
c6aefafb 67{
8c27bd75 68 u32 count = tcp_cookie_time();
c6aefafb
GG
69 return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
70 sseq + (count << COOKIEBITS) +
71 ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
72 & COOKIEMASK));
73}
74
b71d1d42
ED
75static __u32 check_tcp_syn_cookie(__u32 cookie, const struct in6_addr *saddr,
76 const struct in6_addr *daddr, __be16 sport,
8c27bd75 77 __be16 dport, __u32 sseq)
c6aefafb 78{
8c27bd75 79 __u32 diff, count = tcp_cookie_time();
c6aefafb
GG
80
81 cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
82
83 diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
8c27bd75 84 if (diff >= MAX_SYNCOOKIE_AGE)
c6aefafb
GG
85 return (__u32)-1;
86
87 return (cookie -
88 cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
89 & COOKIEMASK;
90}
91
81eb6a14
PM
92u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
93 const struct tcphdr *th, __u16 *mssp)
c6aefafb 94{
c6aefafb
GG
95 int mssind;
96 const __u16 mss = *mssp;
97
5918e2fb
FW
98 for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
99 if (mss >= msstab[mssind])
100 break;
101
102 *mssp = msstab[mssind];
c6aefafb 103
c6aefafb 104 return secure_tcp_syn_cookie(&iph->saddr, &iph->daddr, th->source,
8c27bd75 105 th->dest, ntohl(th->seq), mssind);
c6aefafb 106}
81eb6a14 107EXPORT_SYMBOL_GPL(__cookie_v6_init_sequence);
c6aefafb 108
3f684b4b 109__u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mssp)
c6aefafb 110{
b71d1d42 111 const struct ipv6hdr *iph = ipv6_hdr(skb);
c6aefafb 112 const struct tcphdr *th = tcp_hdr(skb);
81eb6a14 113
81eb6a14
PM
114 return __cookie_v6_init_sequence(iph, th, mssp);
115}
116
7577bc82 117int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th)
81eb6a14 118{
7577bc82 119 __u32 cookie = ntohl(th->ack_seq) - 1;
c6aefafb 120 __u32 seq = ntohl(th->seq) - 1;
7577bc82
KI
121 __u32 mssind;
122
123 mssind = check_tcp_syn_cookie(cookie, &iph->saddr, &iph->daddr,
124 th->source, th->dest, seq);
c6aefafb 125
5918e2fb 126 return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
c6aefafb 127}
81eb6a14 128EXPORT_SYMBOL_GPL(__cookie_v6_check);
c6aefafb 129
8e7bab6b
KI
130static struct request_sock *cookie_tcp_check(struct net *net, struct sock *sk,
131 struct sk_buff *skb)
c6aefafb 132{
4957faad 133 struct tcp_options_received tcp_opt;
84b114b9 134 u32 tsoff = 0;
8e7bab6b 135 int mss;
c6aefafb 136
646697b9
FW
137 if (tcp_synq_no_recent_overflow(sk))
138 goto out;
139
8e7bab6b
KI
140 mss = __cookie_v6_check(ipv6_hdr(skb), tcp_hdr(skb));
141 if (!mss) {
45c28509 142 __NET_INC_STATS(net, LINUX_MIB_SYNCOOKIESFAILED);
c6aefafb
GG
143 goto out;
144 }
145
45c28509 146 __NET_INC_STATS(net, LINUX_MIB_SYNCOOKIESRECV);
c6aefafb 147
bb5b7c11
DM
148 /* check for timestamp cookie support */
149 memset(&tcp_opt, 0, sizeof(tcp_opt));
45c28509 150 tcp_parse_options(net, skb, &tcp_opt, 0, NULL);
bb5b7c11 151
84b114b9 152 if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) {
45c28509 153 tsoff = secure_tcpv6_ts_off(net,
5d2ed052 154 ipv6_hdr(skb)->daddr.s6_addr32,
84b114b9
ED
155 ipv6_hdr(skb)->saddr.s6_addr32);
156 tcp_opt.rcv_tsecr -= tsoff;
157 }
158
45c28509 159 if (!cookie_timestamp_decode(net, &tcp_opt))
8c763681 160 goto out;
bb5b7c11 161
8e7bab6b
KI
162 return cookie_tcp_reqsk_alloc(&tcp6_request_sock_ops, sk, skb,
163 &tcp_opt, mss, tsoff);
164out:
165 return ERR_PTR(-EINVAL);
166}
167
168struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
169{
170 const struct tcphdr *th = tcp_hdr(skb);
171 struct ipv6_pinfo *np = inet6_sk(sk);
172 struct tcp_sock *tp = tcp_sk(sk);
173 struct inet_request_sock *ireq;
174 struct net *net = sock_net(sk);
175 struct request_sock *req;
176 struct dst_entry *dst;
177 struct sock *ret = sk;
178 __u8 rcv_wscale;
179 int full_space;
ed43e76c 180 SKB_DR(reason);
8e7bab6b
KI
181
182 if (!READ_ONCE(net->ipv4.sysctl_tcp_syncookies) ||
183 !th->ack || th->rst)
184 goto out;
185
695751e3
KI
186 if (cookie_bpf_ok(skb)) {
187 req = cookie_bpf_check(sk, skb);
188 } else {
189 req = cookie_tcp_check(net, sk, skb);
190 if (IS_ERR(req))
191 goto out;
192 }
253541a3
JX
193 if (!req) {
194 SKB_DR_SET(reason, NO_SOCKET);
50468cdd 195 goto out_drop;
253541a3 196 }
c6aefafb
GG
197
198 ireq = inet_rsk(req);
c6aefafb 199
634fb979
ED
200 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
201 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
23be1e0e 202
253541a3
JX
203 if (security_inet_conn_request(sk, skb, req)) {
204 SKB_DR_SET(reason, SECURITY_HOOK);
23be1e0e 205 goto out_free;
253541a3 206 }
23be1e0e 207
a224772d 208 if (ipv6_opt_accepted(sk, skb, &TCP_SKB_CB(skb)->header.h6) ||
c6aefafb
GG
209 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
210 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
63354797 211 refcount_inc(&skb->users);
634fb979 212 ireq->pktopts = skb;
c6aefafb
GG
213 }
214
c6aefafb
GG
215 /* So that link locals have meaning */
216 if (!sk->sk_bound_dev_if &&
634fb979 217 ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
870c3151 218 ireq->ir_iif = tcp_v6_iif(skb);
c6aefafb 219
7b0f570f 220 tcp_ao_syncookie(sk, skb, req, AF_INET6);
06b22ef2 221
c6aefafb
GG
222 /*
223 * We need to lookup the dst_entry to get the correct window size.
224 * This is taken from tcp_v6_syn_recv_sock. Somebody please enlighten
225 * me if there is a preferred way.
226 */
227 {
20c59de2 228 struct in6_addr *final_p, final;
4c9483b2
DM
229 struct flowi6 fl6;
230 memset(&fl6, 0, sizeof(fl6));
231 fl6.flowi6_proto = IPPROTO_TCP;
634fb979 232 fl6.daddr = ireq->ir_v6_rmt_addr;
45f6fad8 233 final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), &final);
634fb979 234 fl6.saddr = ireq->ir_v6_loc_addr;
6dd9a14e 235 fl6.flowi6_oif = ireq->ir_iif;
84f39b08 236 fl6.flowi6_mark = ireq->ir_mark;
634fb979 237 fl6.fl6_dport = ireq->ir_rmt_port;
1958b856 238 fl6.fl6_sport = inet_sk(sk)->inet_sport;
e2d118a1 239 fl6.flowi6_uid = sk->sk_uid;
3df98d79 240 security_req_classify_flow(req, flowi6_to_flowi_common(&fl6));
4c9483b2 241
45c28509 242 dst = ip6_dst_lookup_flow(net, sk, &fl6, final_p);
253541a3
JX
243 if (IS_ERR(dst)) {
244 SKB_DR_SET(reason, IP_OUTNOROUTES);
1730554f 245 goto out_free;
253541a3 246 }
c6aefafb
GG
247 }
248
f410cbea 249 req->rsk_window_clamp = READ_ONCE(tp->window_clamp) ? :dst_metric(dst, RTAX_WINDOW);
909172a1
MW
250 /* limit the window selection if the user enforce a smaller rx buffer */
251 full_space = tcp_full_space(sk);
252 if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
253 (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
254 req->rsk_window_clamp = full_space;
255
256 tcp_select_initial_window(sk, full_space, req->mss,
ed53d0ab 257 &req->rsk_rcv_wnd, &req->rsk_window_clamp,
31d12926 258 ireq->wscale_ok, &rcv_wscale,
259 dst_metric(dst, RTAX_INITRWND));
c6aefafb 260
956c0d61
KI
261 /* req->syncookie is set true only if ACK is validated
262 * by BPF kfunc, then, rcv_wscale is already configured.
263 */
695751e3
KI
264 if (!req->syncookie)
265 ireq->rcv_wscale = rcv_wscale;
8e7bab6b 266 ireq->ecn_ok &= cookie_ecn_ok(net, dst);
c6aefafb 267
efce3d1f 268 ret = tcp_get_cookie_sock(sk, skb, req, dst);
253541a3
JX
269 if (!ret) {
270 SKB_DR_SET(reason, NO_SOCKET);
ed43e76c 271 goto out_drop;
253541a3 272 }
1730554f
FW
273out:
274 return ret;
275out_free:
276 reqsk_free(req);
50468cdd 277out_drop:
ed43e76c 278 kfree_skb_reason(skb, reason);
1730554f 279 return NULL;
c6aefafb 280}