0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/tcp.h>
0010 #include <linux/siphash.h>
0011 #include <linux/kernel.h>
0012 #include <linux/export.h>
0013 #include <net/secure_seq.h>
0014 #include <net/tcp.h>
0015 #include <net/route.h>
0016
0017 static siphash_aligned_key_t syncookie_secret[2];
0018
0019 #define COOKIEBITS 24
0020 #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #define TS_OPT_WSCALE_MASK 0xf
0037 #define TS_OPT_SACK BIT(4)
0038 #define TS_OPT_ECN BIT(5)
0039
0040
0041
0042
0043 #define TSBITS 6
0044 #define TSMASK (((__u32)1 << TSBITS) - 1)
0045
0046 static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
0047 u32 count, int c)
0048 {
0049 net_get_random_once(syncookie_secret, sizeof(syncookie_secret));
0050 return siphash_4u32((__force u32)saddr, (__force u32)daddr,
0051 (__force u32)sport << 16 | (__force u32)dport,
0052 count, &syncookie_secret[c]);
0053 }
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 u64 cookie_init_timestamp(struct request_sock *req, u64 now)
0064 {
0065 struct inet_request_sock *ireq;
0066 u32 ts, ts_now = tcp_ns_to_ts(now);
0067 u32 options = 0;
0068
0069 ireq = inet_rsk(req);
0070
0071 options = ireq->wscale_ok ? ireq->snd_wscale : TS_OPT_WSCALE_MASK;
0072 if (ireq->sack_ok)
0073 options |= TS_OPT_SACK;
0074 if (ireq->ecn_ok)
0075 options |= TS_OPT_ECN;
0076
0077 ts = ts_now & ~TSMASK;
0078 ts |= options;
0079 if (ts > ts_now) {
0080 ts >>= TSBITS;
0081 ts--;
0082 ts <<= TSBITS;
0083 ts |= options;
0084 }
0085 return (u64)ts * (NSEC_PER_SEC / TCP_TS_HZ);
0086 }
0087
0088
0089 static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
0090 __be16 dport, __u32 sseq, __u32 data)
0091 {
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 u32 count = tcp_cookie_time();
0103 return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
0104 sseq + (count << COOKIEBITS) +
0105 ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
0106 & COOKIEMASK));
0107 }
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
0119 __be16 sport, __be16 dport, __u32 sseq)
0120 {
0121 u32 diff, count = tcp_cookie_time();
0122
0123
0124 cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
0125
0126
0127 diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
0128 if (diff >= MAX_SYNCOOKIE_AGE)
0129 return (__u32)-1;
0130
0131 return (cookie -
0132 cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
0133 & COOKIEMASK;
0134 }
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 static __u16 const msstab[] = {
0149 536,
0150 1300,
0151 1440,
0152 1460,
0153 };
0154
0155
0156
0157
0158
0159 u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
0160 u16 *mssp)
0161 {
0162 int mssind;
0163 const __u16 mss = *mssp;
0164
0165 for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
0166 if (mss >= msstab[mssind])
0167 break;
0168 *mssp = msstab[mssind];
0169
0170 return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
0171 th->source, th->dest, ntohl(th->seq),
0172 mssind);
0173 }
0174 EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence);
0175
0176 __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mssp)
0177 {
0178 const struct iphdr *iph = ip_hdr(skb);
0179 const struct tcphdr *th = tcp_hdr(skb);
0180
0181 return __cookie_v4_init_sequence(iph, th, mssp);
0182 }
0183
0184
0185
0186
0187
0188 int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
0189 u32 cookie)
0190 {
0191 __u32 seq = ntohl(th->seq) - 1;
0192 __u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
0193 th->source, th->dest, seq);
0194
0195 return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
0196 }
0197 EXPORT_SYMBOL_GPL(__cookie_v4_check);
0198
0199 struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
0200 struct request_sock *req,
0201 struct dst_entry *dst, u32 tsoff)
0202 {
0203 struct inet_connection_sock *icsk = inet_csk(sk);
0204 struct sock *child;
0205 bool own_req;
0206
0207 child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
0208 NULL, &own_req);
0209 if (child) {
0210 refcount_set(&req->rsk_refcnt, 1);
0211 tcp_sk(child)->tsoffset = tsoff;
0212 sock_rps_save_rxhash(child, skb);
0213
0214 if (rsk_drop_req(req)) {
0215 reqsk_put(req);
0216 return child;
0217 }
0218
0219 if (inet_csk_reqsk_queue_add(sk, req, child))
0220 return child;
0221
0222 bh_unlock_sock(child);
0223 sock_put(child);
0224 }
0225 __reqsk_free(req);
0226
0227 return NULL;
0228 }
0229 EXPORT_SYMBOL(tcp_get_cookie_sock);
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239 bool cookie_timestamp_decode(const struct net *net,
0240 struct tcp_options_received *tcp_opt)
0241 {
0242
0243 u32 options = tcp_opt->rcv_tsecr;
0244
0245 if (!tcp_opt->saw_tstamp) {
0246 tcp_clear_options(tcp_opt);
0247 return true;
0248 }
0249
0250 if (!READ_ONCE(net->ipv4.sysctl_tcp_timestamps))
0251 return false;
0252
0253 tcp_opt->sack_ok = (options & TS_OPT_SACK) ? TCP_SACK_SEEN : 0;
0254
0255 if (tcp_opt->sack_ok && !READ_ONCE(net->ipv4.sysctl_tcp_sack))
0256 return false;
0257
0258 if ((options & TS_OPT_WSCALE_MASK) == TS_OPT_WSCALE_MASK)
0259 return true;
0260
0261 tcp_opt->wscale_ok = 1;
0262 tcp_opt->snd_wscale = options & TS_OPT_WSCALE_MASK;
0263
0264 return READ_ONCE(net->ipv4.sysctl_tcp_window_scaling) != 0;
0265 }
0266 EXPORT_SYMBOL(cookie_timestamp_decode);
0267
0268 bool cookie_ecn_ok(const struct tcp_options_received *tcp_opt,
0269 const struct net *net, const struct dst_entry *dst)
0270 {
0271 bool ecn_ok = tcp_opt->rcv_tsecr & TS_OPT_ECN;
0272
0273 if (!ecn_ok)
0274 return false;
0275
0276 if (READ_ONCE(net->ipv4.sysctl_tcp_ecn))
0277 return true;
0278
0279 return dst_feature(dst, RTAX_FEATURE_ECN);
0280 }
0281 EXPORT_SYMBOL(cookie_ecn_ok);
0282
0283 struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
0284 const struct tcp_request_sock_ops *af_ops,
0285 struct sock *sk,
0286 struct sk_buff *skb)
0287 {
0288 struct tcp_request_sock *treq;
0289 struct request_sock *req;
0290
0291 #ifdef CONFIG_MPTCP
0292 if (sk_is_mptcp(sk))
0293 ops = &mptcp_subflow_request_sock_ops;
0294 #endif
0295
0296 req = inet_reqsk_alloc(ops, sk, false);
0297 if (!req)
0298 return NULL;
0299
0300 treq = tcp_rsk(req);
0301
0302
0303 treq->af_specific = af_ops;
0304
0305 treq->syn_tos = TCP_SKB_CB(skb)->ip_dsfield;
0306 #if IS_ENABLED(CONFIG_MPTCP)
0307 treq->is_mptcp = sk_is_mptcp(sk);
0308 if (treq->is_mptcp) {
0309 int err = mptcp_subflow_init_cookie_req(req, sk, skb);
0310
0311 if (err) {
0312 reqsk_free(req);
0313 return NULL;
0314 }
0315 }
0316 #endif
0317
0318 return req;
0319 }
0320 EXPORT_SYMBOL_GPL(cookie_tcp_reqsk_alloc);
0321
0322
0323
0324
0325
0326 struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
0327 {
0328 struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
0329 struct tcp_options_received tcp_opt;
0330 struct inet_request_sock *ireq;
0331 struct tcp_request_sock *treq;
0332 struct tcp_sock *tp = tcp_sk(sk);
0333 const struct tcphdr *th = tcp_hdr(skb);
0334 __u32 cookie = ntohl(th->ack_seq) - 1;
0335 struct sock *ret = sk;
0336 struct request_sock *req;
0337 int full_space, mss;
0338 struct rtable *rt;
0339 __u8 rcv_wscale;
0340 struct flowi4 fl4;
0341 u32 tsoff = 0;
0342
0343 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies) ||
0344 !th->ack || th->rst)
0345 goto out;
0346
0347 if (tcp_synq_no_recent_overflow(sk))
0348 goto out;
0349
0350 mss = __cookie_v4_check(ip_hdr(skb), th, cookie);
0351 if (mss == 0) {
0352 __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
0353 goto out;
0354 }
0355
0356 __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV);
0357
0358
0359 memset(&tcp_opt, 0, sizeof(tcp_opt));
0360 tcp_parse_options(sock_net(sk), skb, &tcp_opt, 0, NULL);
0361
0362 if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) {
0363 tsoff = secure_tcp_ts_off(sock_net(sk),
0364 ip_hdr(skb)->daddr,
0365 ip_hdr(skb)->saddr);
0366 tcp_opt.rcv_tsecr -= tsoff;
0367 }
0368
0369 if (!cookie_timestamp_decode(sock_net(sk), &tcp_opt))
0370 goto out;
0371
0372 ret = NULL;
0373 req = cookie_tcp_reqsk_alloc(&tcp_request_sock_ops,
0374 &tcp_request_sock_ipv4_ops, sk, skb);
0375 if (!req)
0376 goto out;
0377
0378 ireq = inet_rsk(req);
0379 treq = tcp_rsk(req);
0380 treq->rcv_isn = ntohl(th->seq) - 1;
0381 treq->snt_isn = cookie;
0382 treq->ts_off = 0;
0383 treq->txhash = net_tx_rndhash();
0384 req->mss = mss;
0385 ireq->ir_num = ntohs(th->dest);
0386 ireq->ir_rmt_port = th->source;
0387 sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
0388 sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
0389 ireq->ir_mark = inet_request_mark(sk, skb);
0390 ireq->snd_wscale = tcp_opt.snd_wscale;
0391 ireq->sack_ok = tcp_opt.sack_ok;
0392 ireq->wscale_ok = tcp_opt.wscale_ok;
0393 ireq->tstamp_ok = tcp_opt.saw_tstamp;
0394 req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
0395 treq->snt_synack = 0;
0396 treq->tfo_listener = false;
0397
0398 if (IS_ENABLED(CONFIG_SMC))
0399 ireq->smc_ok = 0;
0400
0401 ireq->ir_iif = inet_request_bound_dev_if(sk, skb);
0402
0403
0404
0405
0406 RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(sock_net(sk), skb));
0407
0408 if (security_inet_conn_request(sk, skb, req)) {
0409 reqsk_free(req);
0410 goto out;
0411 }
0412
0413 req->num_retrans = 0;
0414
0415
0416
0417
0418
0419
0420
0421 flowi4_init_output(&fl4, ireq->ir_iif, ireq->ir_mark,
0422 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP,
0423 inet_sk_flowi_flags(sk),
0424 opt->srr ? opt->faddr : ireq->ir_rmt_addr,
0425 ireq->ir_loc_addr, th->source, th->dest, sk->sk_uid);
0426 security_req_classify_flow(req, flowi4_to_flowi_common(&fl4));
0427 rt = ip_route_output_key(sock_net(sk), &fl4);
0428 if (IS_ERR(rt)) {
0429 reqsk_free(req);
0430 goto out;
0431 }
0432
0433
0434 req->rsk_window_clamp = tp->window_clamp ? :dst_metric(&rt->dst, RTAX_WINDOW);
0435
0436 full_space = tcp_full_space(sk);
0437 if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
0438 (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
0439 req->rsk_window_clamp = full_space;
0440
0441 tcp_select_initial_window(sk, full_space, req->mss,
0442 &req->rsk_rcv_wnd, &req->rsk_window_clamp,
0443 ireq->wscale_ok, &rcv_wscale,
0444 dst_metric(&rt->dst, RTAX_INITRWND));
0445
0446 ireq->rcv_wscale = rcv_wscale;
0447 ireq->ecn_ok = cookie_ecn_ok(&tcp_opt, sock_net(sk), &rt->dst);
0448
0449 ret = tcp_get_cookie_sock(sk, skb, req, &rt->dst, tsoff);
0450
0451
0452
0453 if (ret)
0454 inet_sk(ret)->cork.fl.u.ip4 = fl4;
0455 out: return ret;
0456 }