0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/tcp.h>
0013 #include <linux/random.h>
0014 #include <linux/siphash.h>
0015 #include <linux/kernel.h>
0016 #include <net/secure_seq.h>
0017 #include <net/ipv6.h>
0018 #include <net/tcp.h>
0019
0020 #define COOKIEBITS 24
0021 #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
0022
0023 static siphash_aligned_key_t syncookie6_secret[2];
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 static __u16 const msstab[] = {
0034 1280 - 60,
0035 1480 - 60,
0036 1500 - 60,
0037 9000 - 60,
0038 };
0039
0040 static u32 cookie_hash(const struct in6_addr *saddr,
0041 const struct in6_addr *daddr,
0042 __be16 sport, __be16 dport, u32 count, int c)
0043 {
0044 const struct {
0045 struct in6_addr saddr;
0046 struct in6_addr daddr;
0047 u32 count;
0048 __be16 sport;
0049 __be16 dport;
0050 } __aligned(SIPHASH_ALIGNMENT) combined = {
0051 .saddr = *saddr,
0052 .daddr = *daddr,
0053 .count = count,
0054 .sport = sport,
0055 .dport = dport
0056 };
0057
0058 net_get_random_once(syncookie6_secret, sizeof(syncookie6_secret));
0059 return siphash(&combined, offsetofend(typeof(combined), dport),
0060 &syncookie6_secret[c]);
0061 }
0062
0063 static __u32 secure_tcp_syn_cookie(const struct in6_addr *saddr,
0064 const struct in6_addr *daddr,
0065 __be16 sport, __be16 dport, __u32 sseq,
0066 __u32 data)
0067 {
0068 u32 count = tcp_cookie_time();
0069 return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
0070 sseq + (count << COOKIEBITS) +
0071 ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
0072 & COOKIEMASK));
0073 }
0074
0075 static __u32 check_tcp_syn_cookie(__u32 cookie, const struct in6_addr *saddr,
0076 const struct in6_addr *daddr, __be16 sport,
0077 __be16 dport, __u32 sseq)
0078 {
0079 __u32 diff, count = tcp_cookie_time();
0080
0081 cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
0082
0083 diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
0084 if (diff >= MAX_SYNCOOKIE_AGE)
0085 return (__u32)-1;
0086
0087 return (cookie -
0088 cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
0089 & COOKIEMASK;
0090 }
0091
0092 u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
0093 const struct tcphdr *th, __u16 *mssp)
0094 {
0095 int mssind;
0096 const __u16 mss = *mssp;
0097
0098 for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
0099 if (mss >= msstab[mssind])
0100 break;
0101
0102 *mssp = msstab[mssind];
0103
0104 return secure_tcp_syn_cookie(&iph->saddr, &iph->daddr, th->source,
0105 th->dest, ntohl(th->seq), mssind);
0106 }
0107 EXPORT_SYMBOL_GPL(__cookie_v6_init_sequence);
0108
0109 __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mssp)
0110 {
0111 const struct ipv6hdr *iph = ipv6_hdr(skb);
0112 const struct tcphdr *th = tcp_hdr(skb);
0113
0114 return __cookie_v6_init_sequence(iph, th, mssp);
0115 }
0116
0117 int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
0118 __u32 cookie)
0119 {
0120 __u32 seq = ntohl(th->seq) - 1;
0121 __u32 mssind = check_tcp_syn_cookie(cookie, &iph->saddr, &iph->daddr,
0122 th->source, th->dest, seq);
0123
0124 return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
0125 }
0126 EXPORT_SYMBOL_GPL(__cookie_v6_check);
0127
0128 struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
0129 {
0130 struct tcp_options_received tcp_opt;
0131 struct inet_request_sock *ireq;
0132 struct tcp_request_sock *treq;
0133 struct ipv6_pinfo *np = inet6_sk(sk);
0134 struct tcp_sock *tp = tcp_sk(sk);
0135 const struct tcphdr *th = tcp_hdr(skb);
0136 __u32 cookie = ntohl(th->ack_seq) - 1;
0137 struct sock *ret = sk;
0138 struct request_sock *req;
0139 int full_space, mss;
0140 struct dst_entry *dst;
0141 __u8 rcv_wscale;
0142 u32 tsoff = 0;
0143
0144 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies) ||
0145 !th->ack || th->rst)
0146 goto out;
0147
0148 if (tcp_synq_no_recent_overflow(sk))
0149 goto out;
0150
0151 mss = __cookie_v6_check(ipv6_hdr(skb), th, cookie);
0152 if (mss == 0) {
0153 __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
0154 goto out;
0155 }
0156
0157 __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV);
0158
0159
0160 memset(&tcp_opt, 0, sizeof(tcp_opt));
0161 tcp_parse_options(sock_net(sk), skb, &tcp_opt, 0, NULL);
0162
0163 if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) {
0164 tsoff = secure_tcpv6_ts_off(sock_net(sk),
0165 ipv6_hdr(skb)->daddr.s6_addr32,
0166 ipv6_hdr(skb)->saddr.s6_addr32);
0167 tcp_opt.rcv_tsecr -= tsoff;
0168 }
0169
0170 if (!cookie_timestamp_decode(sock_net(sk), &tcp_opt))
0171 goto out;
0172
0173 ret = NULL;
0174 req = cookie_tcp_reqsk_alloc(&tcp6_request_sock_ops,
0175 &tcp_request_sock_ipv6_ops, sk, skb);
0176 if (!req)
0177 goto out;
0178
0179 ireq = inet_rsk(req);
0180 treq = tcp_rsk(req);
0181 treq->tfo_listener = false;
0182
0183 if (security_inet_conn_request(sk, skb, req))
0184 goto out_free;
0185
0186 req->mss = mss;
0187 ireq->ir_rmt_port = th->source;
0188 ireq->ir_num = ntohs(th->dest);
0189 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
0190 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
0191 if (ipv6_opt_accepted(sk, skb, &TCP_SKB_CB(skb)->header.h6) ||
0192 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
0193 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
0194 refcount_inc(&skb->users);
0195 ireq->pktopts = skb;
0196 }
0197
0198 ireq->ir_iif = inet_request_bound_dev_if(sk, skb);
0199
0200 if (!sk->sk_bound_dev_if &&
0201 ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
0202 ireq->ir_iif = tcp_v6_iif(skb);
0203
0204 ireq->ir_mark = inet_request_mark(sk, skb);
0205
0206 req->num_retrans = 0;
0207 ireq->snd_wscale = tcp_opt.snd_wscale;
0208 ireq->sack_ok = tcp_opt.sack_ok;
0209 ireq->wscale_ok = tcp_opt.wscale_ok;
0210 ireq->tstamp_ok = tcp_opt.saw_tstamp;
0211 req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
0212 treq->snt_synack = 0;
0213 treq->rcv_isn = ntohl(th->seq) - 1;
0214 treq->snt_isn = cookie;
0215 treq->ts_off = 0;
0216 treq->txhash = net_tx_rndhash();
0217 if (IS_ENABLED(CONFIG_SMC))
0218 ireq->smc_ok = 0;
0219
0220
0221
0222
0223
0224
0225 {
0226 struct in6_addr *final_p, final;
0227 struct flowi6 fl6;
0228 memset(&fl6, 0, sizeof(fl6));
0229 fl6.flowi6_proto = IPPROTO_TCP;
0230 fl6.daddr = ireq->ir_v6_rmt_addr;
0231 final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), &final);
0232 fl6.saddr = ireq->ir_v6_loc_addr;
0233 fl6.flowi6_oif = ireq->ir_iif;
0234 fl6.flowi6_mark = ireq->ir_mark;
0235 fl6.fl6_dport = ireq->ir_rmt_port;
0236 fl6.fl6_sport = inet_sk(sk)->inet_sport;
0237 fl6.flowi6_uid = sk->sk_uid;
0238 security_req_classify_flow(req, flowi6_to_flowi_common(&fl6));
0239
0240 dst = ip6_dst_lookup_flow(sock_net(sk), sk, &fl6, final_p);
0241 if (IS_ERR(dst))
0242 goto out_free;
0243 }
0244
0245 req->rsk_window_clamp = tp->window_clamp ? :dst_metric(dst, RTAX_WINDOW);
0246
0247 full_space = tcp_full_space(sk);
0248 if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
0249 (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
0250 req->rsk_window_clamp = full_space;
0251
0252 tcp_select_initial_window(sk, full_space, req->mss,
0253 &req->rsk_rcv_wnd, &req->rsk_window_clamp,
0254 ireq->wscale_ok, &rcv_wscale,
0255 dst_metric(dst, RTAX_INITRWND));
0256
0257 ireq->rcv_wscale = rcv_wscale;
0258 ireq->ecn_ok = cookie_ecn_ok(&tcp_opt, sock_net(sk), dst);
0259
0260 ret = tcp_get_cookie_sock(sk, skb, req, dst, tsoff);
0261 out:
0262 return ret;
0263 out_free:
0264 reqsk_free(req);
0265 return NULL;
0266 }