syncookies.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Syncookies implementation for the Linux kernel
  4. *
  5. * Copyright (C) 1997 Andi Kleen
  6. * Based on ideas by D.J.Bernstein and Eric Schenk.
  7. */
  8. #include <linux/tcp.h>
  9. #include <linux/siphash.h>
  10. #include <linux/kernel.h>
  11. #include <linux/export.h>
  12. #include <net/secure_seq.h>
  13. #include <net/tcp.h>
  14. #include <net/route.h>
  15. static siphash_aligned_key_t syncookie_secret[2];
  16. #define COOKIEBITS 24 /* Upper bits store count */
  17. #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
  18. /* TCP Timestamp: 6 lowest bits of timestamp sent in the cookie SYN-ACK
  19. * stores TCP options:
  20. *
  21. * MSB LSB
  22. * | 31 ... 6 | 5 | 4 | 3 2 1 0 |
  23. * | Timestamp | ECN | SACK | WScale |
  24. *
  25. * When we receive a valid cookie-ACK, we look at the echoed tsval (if
  26. * any) to figure out which TCP options we should use for the rebuilt
  27. * connection.
  28. *
  29. * A WScale setting of '0xf' (which is an invalid scaling value)
  30. * means that original syn did not include the TCP window scaling option.
  31. */
  32. #define TS_OPT_WSCALE_MASK 0xf
  33. #define TS_OPT_SACK BIT(4)
  34. #define TS_OPT_ECN BIT(5)
  35. /* There is no TS_OPT_TIMESTAMP:
  36. * if ACK contains timestamp option, we already know it was
  37. * requested/supported by the syn/synack exchange.
  38. */
  39. #define TSBITS 6
  40. static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
  41. u32 count, int c)
  42. {
  43. net_get_random_once(syncookie_secret, sizeof(syncookie_secret));
  44. return siphash_4u32((__force u32)saddr, (__force u32)daddr,
  45. (__force u32)sport << 16 | (__force u32)dport,
  46. count, &syncookie_secret[c]);
  47. }
  48. /*
  49. * when syncookies are in effect and tcp timestamps are enabled we encode
  50. * tcp options in the lower bits of the timestamp value that will be
  51. * sent in the syn-ack.
  52. * Since subsequent timestamps use the normal tcp_time_stamp value, we
  53. * must make sure that the resulting initial timestamp is <= tcp_time_stamp.
  54. */
  55. u64 cookie_init_timestamp(struct request_sock *req, u64 now)
  56. {
  57. const struct inet_request_sock *ireq = inet_rsk(req);
  58. u64 ts, ts_now = tcp_ns_to_ts(now);
  59. u32 options = 0;
  60. options = ireq->wscale_ok ? ireq->snd_wscale : TS_OPT_WSCALE_MASK;
  61. if (ireq->sack_ok)
  62. options |= TS_OPT_SACK;
  63. if (ireq->ecn_ok)
  64. options |= TS_OPT_ECN;
  65. ts = (ts_now >> TSBITS) << TSBITS;
  66. ts |= options;
  67. if (ts > ts_now)
  68. ts -= (1UL << TSBITS);
  69. return ts * (NSEC_PER_SEC / TCP_TS_HZ);
  70. }
  71. static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
  72. __be16 dport, __u32 sseq, __u32 data)
  73. {
  74. /*
  75. * Compute the secure sequence number.
  76. * The output should be:
  77. * HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24)
  78. * + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24).
  79. * Where sseq is their sequence number and count increases every
  80. * minute by 1.
  81. * As an extra hack, we add a small "data" value that encodes the
  82. * MSS into the second hash value.
  83. */
  84. u32 count = tcp_cookie_time();
  85. return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
  86. sseq + (count << COOKIEBITS) +
  87. ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
  88. & COOKIEMASK));
  89. }
  90. /*
  91. * This retrieves the small "data" value from the syncookie.
  92. * If the syncookie is bad, the data returned will be out of
  93. * range. This must be checked by the caller.
  94. *
  95. * The count value used to generate the cookie must be less than
  96. * MAX_SYNCOOKIE_AGE minutes in the past.
  97. * The return value (__u32)-1 if this test fails.
  98. */
  99. static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
  100. __be16 sport, __be16 dport, __u32 sseq)
  101. {
  102. u32 diff, count = tcp_cookie_time();
  103. /* Strip away the layers from the cookie */
  104. cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
  105. /* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
  106. diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
  107. if (diff >= MAX_SYNCOOKIE_AGE)
  108. return (__u32)-1;
  109. return (cookie -
  110. cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
  111. & COOKIEMASK; /* Leaving the data behind */
  112. }
  113. /*
  114. * MSS Values are chosen based on the 2011 paper
  115. * 'An Analysis of TCP Maximum Segement Sizes' by S. Alcock and R. Nelson.
  116. * Values ..
  117. * .. lower than 536 are rare (< 0.2%)
  118. * .. between 537 and 1299 account for less than < 1.5% of observed values
  119. * .. in the 1300-1349 range account for about 15 to 20% of observed mss values
  120. * .. exceeding 1460 are very rare (< 0.04%)
  121. *
  122. * 1460 is the single most frequently announced mss value (30 to 46% depending
  123. * on monitor location). Table must be sorted.
  124. */
  125. static __u16 const msstab[] = {
  126. 536,
  127. 1300,
  128. 1440, /* 1440, 1452: PPPoE */
  129. 1460,
  130. };
  131. /*
  132. * Generate a syncookie. mssp points to the mss, which is returned
  133. * rounded down to the value encoded in the cookie.
  134. */
  135. u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
  136. u16 *mssp)
  137. {
  138. int mssind;
  139. const __u16 mss = *mssp;
  140. for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
  141. if (mss >= msstab[mssind])
  142. break;
  143. *mssp = msstab[mssind];
  144. return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
  145. th->source, th->dest, ntohl(th->seq),
  146. mssind);
  147. }
  148. EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence);
  149. __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mssp)
  150. {
  151. const struct iphdr *iph = ip_hdr(skb);
  152. const struct tcphdr *th = tcp_hdr(skb);
  153. return __cookie_v4_init_sequence(iph, th, mssp);
  154. }
  155. /*
  156. * Check if a ack sequence number is a valid syncookie.
  157. * Return the decoded mss if it is, or 0 if not.
  158. */
  159. int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
  160. u32 cookie)
  161. {
  162. __u32 seq = ntohl(th->seq) - 1;
  163. __u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
  164. th->source, th->dest, seq);
  165. return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
  166. }
  167. EXPORT_SYMBOL_GPL(__cookie_v4_check);
  168. struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  169. struct request_sock *req,
  170. struct dst_entry *dst, u32 tsoff)
  171. {
  172. struct inet_connection_sock *icsk = inet_csk(sk);
  173. struct sock *child;
  174. bool own_req;
  175. child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
  176. NULL, &own_req);
  177. if (child) {
  178. refcount_set(&req->rsk_refcnt, 1);
  179. tcp_sk(child)->tsoffset = tsoff;
  180. sock_rps_save_rxhash(child, skb);
  181. if (rsk_drop_req(req)) {
  182. reqsk_put(req);
  183. return child;
  184. }
  185. if (inet_csk_reqsk_queue_add(sk, req, child))
  186. return child;
  187. bh_unlock_sock(child);
  188. sock_put(child);
  189. }
  190. __reqsk_free(req);
  191. return NULL;
  192. }
  193. EXPORT_SYMBOL(tcp_get_cookie_sock);
  194. /*
  195. * when syncookies are in effect and tcp timestamps are enabled we stored
  196. * additional tcp options in the timestamp.
  197. * This extracts these options from the timestamp echo.
  198. *
  199. * return false if we decode a tcp option that is disabled
  200. * on the host.
  201. */
  202. bool cookie_timestamp_decode(const struct net *net,
  203. struct tcp_options_received *tcp_opt)
  204. {
  205. /* echoed timestamp, lowest bits contain options */
  206. u32 options = tcp_opt->rcv_tsecr;
  207. if (!tcp_opt->saw_tstamp) {
  208. tcp_clear_options(tcp_opt);
  209. return true;
  210. }
  211. if (!READ_ONCE(net->ipv4.sysctl_tcp_timestamps))
  212. return false;
  213. tcp_opt->sack_ok = (options & TS_OPT_SACK) ? TCP_SACK_SEEN : 0;
  214. if (tcp_opt->sack_ok && !READ_ONCE(net->ipv4.sysctl_tcp_sack))
  215. return false;
  216. if ((options & TS_OPT_WSCALE_MASK) == TS_OPT_WSCALE_MASK)
  217. return true; /* no window scaling */
  218. tcp_opt->wscale_ok = 1;
  219. tcp_opt->snd_wscale = options & TS_OPT_WSCALE_MASK;
  220. return READ_ONCE(net->ipv4.sysctl_tcp_window_scaling) != 0;
  221. }
  222. EXPORT_SYMBOL(cookie_timestamp_decode);
  223. bool cookie_ecn_ok(const struct tcp_options_received *tcp_opt,
  224. const struct net *net, const struct dst_entry *dst)
  225. {
  226. bool ecn_ok = tcp_opt->rcv_tsecr & TS_OPT_ECN;
  227. if (!ecn_ok)
  228. return false;
  229. if (READ_ONCE(net->ipv4.sysctl_tcp_ecn))
  230. return true;
  231. return dst_feature(dst, RTAX_FEATURE_ECN);
  232. }
  233. EXPORT_SYMBOL(cookie_ecn_ok);
  234. struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
  235. const struct tcp_request_sock_ops *af_ops,
  236. struct sock *sk,
  237. struct sk_buff *skb)
  238. {
  239. struct tcp_request_sock *treq;
  240. struct request_sock *req;
  241. if (sk_is_mptcp(sk))
  242. req = mptcp_subflow_reqsk_alloc(ops, sk, false);
  243. else
  244. req = inet_reqsk_alloc(ops, sk, false);
  245. if (!req)
  246. return NULL;
  247. treq = tcp_rsk(req);
  248. /* treq->af_specific might be used to perform TCP_MD5 lookup */
  249. treq->af_specific = af_ops;
  250. treq->syn_tos = TCP_SKB_CB(skb)->ip_dsfield;
  251. #if IS_ENABLED(CONFIG_MPTCP)
  252. treq->is_mptcp = sk_is_mptcp(sk);
  253. if (treq->is_mptcp) {
  254. int err = mptcp_subflow_init_cookie_req(req, sk, skb);
  255. if (err) {
  256. reqsk_free(req);
  257. return NULL;
  258. }
  259. }
  260. #endif
  261. return req;
  262. }
  263. EXPORT_SYMBOL_GPL(cookie_tcp_reqsk_alloc);
  264. /* On input, sk is a listener.
  265. * Output is listener if incoming packet would not create a child
  266. * NULL if memory could not be allocated.
  267. */
  268. struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
  269. {
  270. struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
  271. struct tcp_options_received tcp_opt;
  272. struct inet_request_sock *ireq;
  273. struct tcp_request_sock *treq;
  274. struct tcp_sock *tp = tcp_sk(sk);
  275. const struct tcphdr *th = tcp_hdr(skb);
  276. __u32 cookie = ntohl(th->ack_seq) - 1;
  277. struct sock *ret = sk;
  278. struct request_sock *req;
  279. int full_space, mss;
  280. struct rtable *rt;
  281. __u8 rcv_wscale;
  282. struct flowi4 fl4;
  283. u32 tsoff = 0;
  284. if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies) ||
  285. !th->ack || th->rst)
  286. goto out;
  287. if (tcp_synq_no_recent_overflow(sk))
  288. goto out;
  289. mss = __cookie_v4_check(ip_hdr(skb), th, cookie);
  290. if (mss == 0) {
  291. __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
  292. goto out;
  293. }
  294. __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV);
  295. /* check for timestamp cookie support */
  296. memset(&tcp_opt, 0, sizeof(tcp_opt));
  297. tcp_parse_options(sock_net(sk), skb, &tcp_opt, 0, NULL);
  298. if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) {
  299. tsoff = secure_tcp_ts_off(sock_net(sk),
  300. ip_hdr(skb)->daddr,
  301. ip_hdr(skb)->saddr);
  302. tcp_opt.rcv_tsecr -= tsoff;
  303. }
  304. if (!cookie_timestamp_decode(sock_net(sk), &tcp_opt))
  305. goto out;
  306. ret = NULL;
  307. req = cookie_tcp_reqsk_alloc(&tcp_request_sock_ops,
  308. &tcp_request_sock_ipv4_ops, sk, skb);
  309. if (!req)
  310. goto out;
  311. ireq = inet_rsk(req);
  312. treq = tcp_rsk(req);
  313. treq->rcv_isn = ntohl(th->seq) - 1;
  314. treq->snt_isn = cookie;
  315. treq->ts_off = 0;
  316. treq->txhash = net_tx_rndhash();
  317. req->mss = mss;
  318. ireq->ir_num = ntohs(th->dest);
  319. ireq->ir_rmt_port = th->source;
  320. sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
  321. sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
  322. ireq->ir_mark = inet_request_mark(sk, skb);
  323. ireq->snd_wscale = tcp_opt.snd_wscale;
  324. ireq->sack_ok = tcp_opt.sack_ok;
  325. ireq->wscale_ok = tcp_opt.wscale_ok;
  326. ireq->tstamp_ok = tcp_opt.saw_tstamp;
  327. req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
  328. treq->snt_synack = 0;
  329. treq->tfo_listener = false;
  330. if (IS_ENABLED(CONFIG_SMC))
  331. ireq->smc_ok = 0;
  332. ireq->ir_iif = inet_request_bound_dev_if(sk, skb);
  333. /* We throwed the options of the initial SYN away, so we hope
  334. * the ACK carries the same options again (see RFC1122 4.2.3.8)
  335. */
  336. RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(sock_net(sk), skb));
  337. if (security_inet_conn_request(sk, skb, req)) {
  338. reqsk_free(req);
  339. goto out;
  340. }
  341. req->num_retrans = 0;
  342. /*
  343. * We need to lookup the route here to get at the correct
  344. * window size. We should better make sure that the window size
  345. * hasn't changed since we received the original syn, but I see
  346. * no easy way to do this.
  347. */
  348. flowi4_init_output(&fl4, ireq->ir_iif, ireq->ir_mark,
  349. RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP,
  350. inet_sk_flowi_flags(sk),
  351. opt->srr ? opt->faddr : ireq->ir_rmt_addr,
  352. ireq->ir_loc_addr, th->source, th->dest, sk->sk_uid);
  353. security_req_classify_flow(req, flowi4_to_flowi_common(&fl4));
  354. rt = ip_route_output_key(sock_net(sk), &fl4);
  355. if (IS_ERR(rt)) {
  356. reqsk_free(req);
  357. goto out;
  358. }
  359. /* Try to redo what tcp_v4_send_synack did. */
  360. req->rsk_window_clamp = tp->window_clamp ? :dst_metric(&rt->dst, RTAX_WINDOW);
  361. /* limit the window selection if the user enforce a smaller rx buffer */
  362. full_space = tcp_full_space(sk);
  363. if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
  364. (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
  365. req->rsk_window_clamp = full_space;
  366. tcp_select_initial_window(sk, full_space, req->mss,
  367. &req->rsk_rcv_wnd, &req->rsk_window_clamp,
  368. ireq->wscale_ok, &rcv_wscale,
  369. dst_metric(&rt->dst, RTAX_INITRWND));
  370. ireq->rcv_wscale = rcv_wscale;
  371. ireq->ecn_ok = cookie_ecn_ok(&tcp_opt, sock_net(sk), &rt->dst);
  372. ret = tcp_get_cookie_sock(sk, skb, req, &rt->dst, tsoff);
  373. /* ip_queue_xmit() depends on our flow being setup
  374. * Normal sockets get it right from inet_csk_route_child_sock()
  375. */
  376. if (ret)
  377. inet_sk(ret)->cork.fl.u.ip4 = fl4;
  378. out: return ret;
  379. }