syncookies.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  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
  10. */
  11. #include <linux/tcp.h>
  12. #include <linux/random.h>
  13. #include <linux/siphash.h>
  14. #include <linux/kernel.h>
  15. #include <net/secure_seq.h>
  16. #include <net/ipv6.h>
  17. #include <net/tcp.h>
  18. #include <net/tcp_ecn.h>
  19. #define COOKIEBITS 24 /* Upper bits store count */
  20. #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
  21. static siphash_aligned_key_t syncookie6_secret[2];
  22. /* RFC 2460, Section 8.3:
  23. * [ipv6 tcp] MSS must be computed as the maximum packet size minus 60 [..]
  24. *
  25. * Due to IPV6_MIN_MTU=1280 the lowest possible MSS is 1220, which allows
  26. * using higher values than ipv4 tcp syncookies.
  27. * The other values are chosen based on ethernet (1500 and 9k MTU), plus
  28. * one that accounts for common encap (PPPoe) overhead. Table must be sorted.
  29. */
  30. static __u16 const msstab[] = {
  31. 1280 - 60, /* IPV6_MIN_MTU - 60 */
  32. 1480 - 60,
  33. 1500 - 60,
  34. 9000 - 60,
  35. };
  36. static u32 cookie_hash(const struct in6_addr *saddr,
  37. const struct in6_addr *daddr,
  38. __be16 sport, __be16 dport, u32 count, int c)
  39. {
  40. const struct {
  41. struct in6_addr saddr;
  42. struct in6_addr daddr;
  43. u32 count;
  44. __be16 sport;
  45. __be16 dport;
  46. } __aligned(SIPHASH_ALIGNMENT) combined = {
  47. .saddr = *saddr,
  48. .daddr = *daddr,
  49. .count = count,
  50. .sport = sport,
  51. .dport = dport
  52. };
  53. net_get_random_once(syncookie6_secret, sizeof(syncookie6_secret));
  54. return siphash(&combined, offsetofend(typeof(combined), dport),
  55. &syncookie6_secret[c]);
  56. }
  57. static __u32 secure_tcp_syn_cookie(const struct in6_addr *saddr,
  58. const struct in6_addr *daddr,
  59. __be16 sport, __be16 dport, __u32 sseq,
  60. __u32 data)
  61. {
  62. u32 count = tcp_cookie_time();
  63. return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
  64. sseq + (count << COOKIEBITS) +
  65. ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
  66. & COOKIEMASK));
  67. }
  68. static __u32 check_tcp_syn_cookie(__u32 cookie, const struct in6_addr *saddr,
  69. const struct in6_addr *daddr, __be16 sport,
  70. __be16 dport, __u32 sseq)
  71. {
  72. __u32 diff, count = tcp_cookie_time();
  73. cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
  74. diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
  75. if (diff >= MAX_SYNCOOKIE_AGE)
  76. return (__u32)-1;
  77. return (cookie -
  78. cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
  79. & COOKIEMASK;
  80. }
  81. u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
  82. const struct tcphdr *th, __u16 *mssp)
  83. {
  84. int mssind;
  85. const __u16 mss = *mssp;
  86. for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
  87. if (mss >= msstab[mssind])
  88. break;
  89. *mssp = msstab[mssind];
  90. return secure_tcp_syn_cookie(&iph->saddr, &iph->daddr, th->source,
  91. th->dest, ntohl(th->seq), mssind);
  92. }
  93. EXPORT_SYMBOL_GPL(__cookie_v6_init_sequence);
  94. __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mssp)
  95. {
  96. const struct ipv6hdr *iph = ipv6_hdr(skb);
  97. const struct tcphdr *th = tcp_hdr(skb);
  98. return __cookie_v6_init_sequence(iph, th, mssp);
  99. }
  100. int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th)
  101. {
  102. __u32 cookie = ntohl(th->ack_seq) - 1;
  103. __u32 seq = ntohl(th->seq) - 1;
  104. __u32 mssind;
  105. mssind = check_tcp_syn_cookie(cookie, &iph->saddr, &iph->daddr,
  106. th->source, th->dest, seq);
  107. return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
  108. }
  109. EXPORT_SYMBOL_GPL(__cookie_v6_check);
  110. static struct request_sock *cookie_tcp_check(struct net *net, struct sock *sk,
  111. struct sk_buff *skb)
  112. {
  113. struct tcp_options_received tcp_opt;
  114. u32 tsoff = 0;
  115. int mss;
  116. if (tcp_synq_no_recent_overflow(sk))
  117. goto out;
  118. mss = __cookie_v6_check(ipv6_hdr(skb), tcp_hdr(skb));
  119. if (!mss) {
  120. __NET_INC_STATS(net, LINUX_MIB_SYNCOOKIESFAILED);
  121. goto out;
  122. }
  123. __NET_INC_STATS(net, LINUX_MIB_SYNCOOKIESRECV);
  124. /* check for timestamp cookie support */
  125. memset(&tcp_opt, 0, sizeof(tcp_opt));
  126. tcp_parse_options(net, skb, &tcp_opt, 0, NULL);
  127. if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) {
  128. union tcp_seq_and_ts_off st;
  129. st = secure_tcpv6_seq_and_ts_off(net,
  130. ipv6_hdr(skb)->daddr.s6_addr32,
  131. ipv6_hdr(skb)->saddr.s6_addr32,
  132. tcp_hdr(skb)->dest,
  133. tcp_hdr(skb)->source);
  134. tsoff = st.ts_off;
  135. tcp_opt.rcv_tsecr -= tsoff;
  136. }
  137. if (!cookie_timestamp_decode(net, &tcp_opt))
  138. goto out;
  139. return cookie_tcp_reqsk_alloc(&tcp6_request_sock_ops, sk, skb,
  140. &tcp_opt, mss, tsoff);
  141. out:
  142. return ERR_PTR(-EINVAL);
  143. }
  144. struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
  145. {
  146. const struct tcphdr *th = tcp_hdr(skb);
  147. struct ipv6_pinfo *np = inet6_sk(sk);
  148. struct tcp_sock *tp = tcp_sk(sk);
  149. struct inet_request_sock *ireq;
  150. struct net *net = sock_net(sk);
  151. struct request_sock *req;
  152. struct dst_entry *dst;
  153. struct sock *ret = sk;
  154. __u8 rcv_wscale;
  155. int full_space;
  156. SKB_DR(reason);
  157. if (!READ_ONCE(net->ipv4.sysctl_tcp_syncookies) ||
  158. !th->ack || th->rst)
  159. goto out;
  160. if (cookie_bpf_ok(skb)) {
  161. req = cookie_bpf_check(sk, skb);
  162. } else {
  163. req = cookie_tcp_check(net, sk, skb);
  164. if (IS_ERR(req))
  165. goto out;
  166. }
  167. if (!req) {
  168. SKB_DR_SET(reason, NO_SOCKET);
  169. goto out_drop;
  170. }
  171. ireq = inet_rsk(req);
  172. ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
  173. ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
  174. if (security_inet_conn_request(sk, skb, req)) {
  175. SKB_DR_SET(reason, SECURITY_HOOK);
  176. goto out_free;
  177. }
  178. if (ipv6_opt_accepted(sk, skb, &TCP_SKB_CB(skb)->header.h6) ||
  179. np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
  180. np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
  181. refcount_inc(&skb->users);
  182. ireq->pktopts = skb;
  183. }
  184. /* So that link locals have meaning */
  185. if (!sk->sk_bound_dev_if &&
  186. ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
  187. ireq->ir_iif = tcp_v6_iif(skb);
  188. tcp_ao_syncookie(sk, skb, req, AF_INET6);
  189. /*
  190. * We need to lookup the dst_entry to get the correct window size.
  191. * This is taken from tcp_v6_syn_recv_sock. Somebody please enlighten
  192. * me if there is a preferred way.
  193. */
  194. {
  195. struct in6_addr *final_p, final;
  196. struct flowi6 fl6;
  197. memset(&fl6, 0, sizeof(fl6));
  198. fl6.flowi6_proto = IPPROTO_TCP;
  199. fl6.daddr = ireq->ir_v6_rmt_addr;
  200. final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), &final);
  201. fl6.saddr = ireq->ir_v6_loc_addr;
  202. fl6.flowi6_oif = ireq->ir_iif;
  203. fl6.flowi6_mark = ireq->ir_mark;
  204. fl6.fl6_dport = ireq->ir_rmt_port;
  205. fl6.fl6_sport = inet_sk(sk)->inet_sport;
  206. fl6.flowi6_uid = sk_uid(sk);
  207. security_req_classify_flow(req, flowi6_to_flowi_common(&fl6));
  208. dst = ip6_dst_lookup_flow(net, sk, &fl6, final_p);
  209. if (IS_ERR(dst)) {
  210. SKB_DR_SET(reason, IP_OUTNOROUTES);
  211. goto out_free;
  212. }
  213. }
  214. req->rsk_window_clamp = READ_ONCE(tp->window_clamp) ? :dst_metric(dst, RTAX_WINDOW);
  215. /* limit the window selection if the user enforce a smaller rx buffer */
  216. full_space = tcp_full_space(sk);
  217. if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
  218. (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
  219. req->rsk_window_clamp = full_space;
  220. tcp_select_initial_window(sk, full_space, req->mss,
  221. &req->rsk_rcv_wnd, &req->rsk_window_clamp,
  222. ireq->wscale_ok, &rcv_wscale,
  223. dst_metric(dst, RTAX_INITRWND));
  224. /* req->syncookie is set true only if ACK is validated
  225. * by BPF kfunc, then, rcv_wscale is already configured.
  226. */
  227. if (!req->syncookie)
  228. ireq->rcv_wscale = rcv_wscale;
  229. ireq->ecn_ok &= cookie_ecn_ok(net, dst);
  230. tcp_rsk(req)->accecn_ok = ireq->ecn_ok && cookie_accecn_ok(th);
  231. ret = tcp_get_cookie_sock(sk, skb, req, dst);
  232. if (!ret) {
  233. SKB_DR_SET(reason, NO_SOCKET);
  234. goto out_drop;
  235. }
  236. out:
  237. return ret;
  238. out_free:
  239. reqsk_free(req);
  240. out_drop:
  241. sk_skb_reason_drop(sk, skb, reason);
  242. return NULL;
  243. }