syncookies.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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(false, 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. if (tcp_rsk(req)->req_usec_ts)
  70. return ts * NSEC_PER_USEC;
  71. return ts * NSEC_PER_MSEC;
  72. }
  73. static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
  74. __be16 dport, __u32 sseq, __u32 data)
  75. {
  76. /*
  77. * Compute the secure sequence number.
  78. * The output should be:
  79. * HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24)
  80. * + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24).
  81. * Where sseq is their sequence number and count increases every
  82. * minute by 1.
  83. * As an extra hack, we add a small "data" value that encodes the
  84. * MSS into the second hash value.
  85. */
  86. u32 count = tcp_cookie_time();
  87. return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
  88. sseq + (count << COOKIEBITS) +
  89. ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
  90. & COOKIEMASK));
  91. }
  92. /*
  93. * This retrieves the small "data" value from the syncookie.
  94. * If the syncookie is bad, the data returned will be out of
  95. * range. This must be checked by the caller.
  96. *
  97. * The count value used to generate the cookie must be less than
  98. * MAX_SYNCOOKIE_AGE minutes in the past.
  99. * The return value (__u32)-1 if this test fails.
  100. */
  101. static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
  102. __be16 sport, __be16 dport, __u32 sseq)
  103. {
  104. u32 diff, count = tcp_cookie_time();
  105. /* Strip away the layers from the cookie */
  106. cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
  107. /* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
  108. diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
  109. if (diff >= MAX_SYNCOOKIE_AGE)
  110. return (__u32)-1;
  111. return (cookie -
  112. cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
  113. & COOKIEMASK; /* Leaving the data behind */
  114. }
  115. /*
  116. * MSS Values are chosen based on the 2011 paper
  117. * 'An Analysis of TCP Maximum Segement Sizes' by S. Alcock and R. Nelson.
  118. * Values ..
  119. * .. lower than 536 are rare (< 0.2%)
  120. * .. between 537 and 1299 account for less than < 1.5% of observed values
  121. * .. in the 1300-1349 range account for about 15 to 20% of observed mss values
  122. * .. exceeding 1460 are very rare (< 0.04%)
  123. *
  124. * 1460 is the single most frequently announced mss value (30 to 46% depending
  125. * on monitor location). Table must be sorted.
  126. */
  127. static __u16 const msstab[] = {
  128. 536,
  129. 1300,
  130. 1440, /* 1440, 1452: PPPoE */
  131. 1460,
  132. };
  133. /*
  134. * Generate a syncookie. mssp points to the mss, which is returned
  135. * rounded down to the value encoded in the cookie.
  136. */
  137. u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
  138. u16 *mssp)
  139. {
  140. int mssind;
  141. const __u16 mss = *mssp;
  142. for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
  143. if (mss >= msstab[mssind])
  144. break;
  145. *mssp = msstab[mssind];
  146. return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
  147. th->source, th->dest, ntohl(th->seq),
  148. mssind);
  149. }
  150. EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence);
  151. __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mssp)
  152. {
  153. const struct iphdr *iph = ip_hdr(skb);
  154. const struct tcphdr *th = tcp_hdr(skb);
  155. return __cookie_v4_init_sequence(iph, th, mssp);
  156. }
  157. /*
  158. * Check if a ack sequence number is a valid syncookie.
  159. * Return the decoded mss if it is, or 0 if not.
  160. */
  161. int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th)
  162. {
  163. __u32 cookie = ntohl(th->ack_seq) - 1;
  164. __u32 seq = ntohl(th->seq) - 1;
  165. __u32 mssind;
  166. mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
  167. th->source, th->dest, seq);
  168. return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
  169. }
  170. EXPORT_SYMBOL_GPL(__cookie_v4_check);
  171. struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  172. struct request_sock *req,
  173. struct dst_entry *dst)
  174. {
  175. struct inet_connection_sock *icsk = inet_csk(sk);
  176. struct sock *child;
  177. bool own_req;
  178. child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst,
  179. NULL, &own_req);
  180. if (child) {
  181. refcount_set(&req->rsk_refcnt, 1);
  182. sock_rps_save_rxhash(child, skb);
  183. if (rsk_drop_req(req)) {
  184. reqsk_put(req);
  185. return child;
  186. }
  187. if (inet_csk_reqsk_queue_add(sk, req, child))
  188. return child;
  189. bh_unlock_sock(child);
  190. sock_put(child);
  191. }
  192. __reqsk_free(req);
  193. return NULL;
  194. }
  195. EXPORT_SYMBOL(tcp_get_cookie_sock);
  196. /*
  197. * when syncookies are in effect and tcp timestamps are enabled we stored
  198. * additional tcp options in the timestamp.
  199. * This extracts these options from the timestamp echo.
  200. *
  201. * return false if we decode a tcp option that is disabled
  202. * on the host.
  203. */
  204. bool cookie_timestamp_decode(const struct net *net,
  205. struct tcp_options_received *tcp_opt)
  206. {
  207. /* echoed timestamp, lowest bits contain options */
  208. u32 options = tcp_opt->rcv_tsecr;
  209. if (!tcp_opt->saw_tstamp) {
  210. tcp_clear_options(tcp_opt);
  211. return true;
  212. }
  213. if (!READ_ONCE(net->ipv4.sysctl_tcp_timestamps))
  214. return false;
  215. tcp_opt->sack_ok = (options & TS_OPT_SACK) ? TCP_SACK_SEEN : 0;
  216. if (tcp_opt->sack_ok && !READ_ONCE(net->ipv4.sysctl_tcp_sack))
  217. return false;
  218. if ((options & TS_OPT_WSCALE_MASK) == TS_OPT_WSCALE_MASK)
  219. return true; /* no window scaling */
  220. tcp_opt->wscale_ok = 1;
  221. tcp_opt->snd_wscale = options & TS_OPT_WSCALE_MASK;
  222. return READ_ONCE(net->ipv4.sysctl_tcp_window_scaling) != 0;
  223. }
  224. EXPORT_SYMBOL(cookie_timestamp_decode);
  225. static int cookie_tcp_reqsk_init(struct sock *sk, struct sk_buff *skb,
  226. struct request_sock *req)
  227. {
  228. struct inet_request_sock *ireq = inet_rsk(req);
  229. struct tcp_request_sock *treq = tcp_rsk(req);
  230. const struct tcphdr *th = tcp_hdr(skb);
  231. req->num_retrans = 0;
  232. ireq->ir_num = ntohs(th->dest);
  233. ireq->ir_rmt_port = th->source;
  234. ireq->ir_iif = inet_request_bound_dev_if(sk, skb);
  235. ireq->ir_mark = inet_request_mark(sk, skb);
  236. if (IS_ENABLED(CONFIG_SMC))
  237. ireq->smc_ok = 0;
  238. treq->snt_synack = 0;
  239. treq->tfo_listener = false;
  240. treq->txhash = net_tx_rndhash();
  241. treq->rcv_isn = ntohl(th->seq) - 1;
  242. treq->snt_isn = ntohl(th->ack_seq) - 1;
  243. treq->syn_tos = TCP_SKB_CB(skb)->ip_dsfield;
  244. treq->req_usec_ts = false;
  245. #if IS_ENABLED(CONFIG_MPTCP)
  246. treq->is_mptcp = sk_is_mptcp(sk);
  247. if (treq->is_mptcp)
  248. return mptcp_subflow_init_cookie_req(req, sk, skb);
  249. #endif
  250. return 0;
  251. }
  252. #if IS_ENABLED(CONFIG_BPF)
  253. struct request_sock *cookie_bpf_check(struct sock *sk, struct sk_buff *skb)
  254. {
  255. struct request_sock *req = inet_reqsk(skb->sk);
  256. skb->sk = NULL;
  257. skb->destructor = NULL;
  258. if (cookie_tcp_reqsk_init(sk, skb, req)) {
  259. reqsk_free(req);
  260. req = NULL;
  261. }
  262. return req;
  263. }
  264. EXPORT_SYMBOL_GPL(cookie_bpf_check);
  265. #endif
  266. struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
  267. struct sock *sk, struct sk_buff *skb,
  268. struct tcp_options_received *tcp_opt,
  269. int mss, u32 tsoff)
  270. {
  271. struct inet_request_sock *ireq;
  272. struct tcp_request_sock *treq;
  273. struct request_sock *req;
  274. if (sk_is_mptcp(sk))
  275. req = mptcp_subflow_reqsk_alloc(ops, sk, false);
  276. else
  277. req = inet_reqsk_alloc(ops, sk, false);
  278. if (!req)
  279. return NULL;
  280. if (cookie_tcp_reqsk_init(sk, skb, req)) {
  281. reqsk_free(req);
  282. return NULL;
  283. }
  284. ireq = inet_rsk(req);
  285. treq = tcp_rsk(req);
  286. req->mss = mss;
  287. req->ts_recent = tcp_opt->saw_tstamp ? tcp_opt->rcv_tsval : 0;
  288. ireq->snd_wscale = tcp_opt->snd_wscale;
  289. ireq->tstamp_ok = tcp_opt->saw_tstamp;
  290. ireq->sack_ok = tcp_opt->sack_ok;
  291. ireq->wscale_ok = tcp_opt->wscale_ok;
  292. ireq->ecn_ok = !!(tcp_opt->rcv_tsecr & TS_OPT_ECN);
  293. treq->ts_off = tsoff;
  294. return req;
  295. }
  296. EXPORT_SYMBOL_GPL(cookie_tcp_reqsk_alloc);
  297. static struct request_sock *cookie_tcp_check(struct net *net, struct sock *sk,
  298. struct sk_buff *skb)
  299. {
  300. struct tcp_options_received tcp_opt;
  301. u32 tsoff = 0;
  302. int mss;
  303. if (tcp_synq_no_recent_overflow(sk))
  304. goto out;
  305. mss = __cookie_v4_check(ip_hdr(skb), tcp_hdr(skb));
  306. if (!mss) {
  307. __NET_INC_STATS(net, LINUX_MIB_SYNCOOKIESFAILED);
  308. goto out;
  309. }
  310. __NET_INC_STATS(net, LINUX_MIB_SYNCOOKIESRECV);
  311. /* check for timestamp cookie support */
  312. memset(&tcp_opt, 0, sizeof(tcp_opt));
  313. tcp_parse_options(net, skb, &tcp_opt, 0, NULL);
  314. if (tcp_opt.saw_tstamp && tcp_opt.rcv_tsecr) {
  315. tsoff = secure_tcp_ts_off(net,
  316. ip_hdr(skb)->daddr,
  317. ip_hdr(skb)->saddr);
  318. tcp_opt.rcv_tsecr -= tsoff;
  319. }
  320. if (!cookie_timestamp_decode(net, &tcp_opt))
  321. goto out;
  322. return cookie_tcp_reqsk_alloc(&tcp_request_sock_ops, sk, skb,
  323. &tcp_opt, mss, tsoff);
  324. out:
  325. return ERR_PTR(-EINVAL);
  326. }
  327. /* On input, sk is a listener.
  328. * Output is listener if incoming packet would not create a child
  329. * NULL if memory could not be allocated.
  330. */
  331. struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
  332. {
  333. struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
  334. const struct tcphdr *th = tcp_hdr(skb);
  335. struct tcp_sock *tp = tcp_sk(sk);
  336. struct inet_request_sock *ireq;
  337. struct net *net = sock_net(sk);
  338. struct request_sock *req;
  339. struct sock *ret = sk;
  340. struct flowi4 fl4;
  341. struct rtable *rt;
  342. __u8 rcv_wscale;
  343. int full_space;
  344. SKB_DR(reason);
  345. if (!READ_ONCE(net->ipv4.sysctl_tcp_syncookies) ||
  346. !th->ack || th->rst)
  347. goto out;
  348. if (cookie_bpf_ok(skb)) {
  349. req = cookie_bpf_check(sk, skb);
  350. } else {
  351. req = cookie_tcp_check(net, sk, skb);
  352. if (IS_ERR(req))
  353. goto out;
  354. }
  355. if (!req) {
  356. SKB_DR_SET(reason, NO_SOCKET);
  357. goto out_drop;
  358. }
  359. ireq = inet_rsk(req);
  360. sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
  361. sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
  362. /* We throwed the options of the initial SYN away, so we hope
  363. * the ACK carries the same options again (see RFC1122 4.2.3.8)
  364. */
  365. RCU_INIT_POINTER(ireq->ireq_opt, tcp_v4_save_options(net, skb));
  366. if (security_inet_conn_request(sk, skb, req)) {
  367. SKB_DR_SET(reason, SECURITY_HOOK);
  368. goto out_free;
  369. }
  370. tcp_ao_syncookie(sk, skb, req, AF_INET);
  371. /*
  372. * We need to lookup the route here to get at the correct
  373. * window size. We should better make sure that the window size
  374. * hasn't changed since we received the original syn, but I see
  375. * no easy way to do this.
  376. */
  377. flowi4_init_output(&fl4, ireq->ir_iif, ireq->ir_mark,
  378. ip_sock_rt_tos(sk), ip_sock_rt_scope(sk),
  379. IPPROTO_TCP, inet_sk_flowi_flags(sk),
  380. opt->srr ? opt->faddr : ireq->ir_rmt_addr,
  381. ireq->ir_loc_addr, th->source, th->dest, sk->sk_uid);
  382. security_req_classify_flow(req, flowi4_to_flowi_common(&fl4));
  383. rt = ip_route_output_key(net, &fl4);
  384. if (IS_ERR(rt)) {
  385. SKB_DR_SET(reason, IP_OUTNOROUTES);
  386. goto out_free;
  387. }
  388. /* Try to redo what tcp_v4_send_synack did. */
  389. req->rsk_window_clamp = READ_ONCE(tp->window_clamp) ? :
  390. dst_metric(&rt->dst, RTAX_WINDOW);
  391. /* limit the window selection if the user enforce a smaller rx buffer */
  392. full_space = tcp_full_space(sk);
  393. if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
  394. (req->rsk_window_clamp > full_space || req->rsk_window_clamp == 0))
  395. req->rsk_window_clamp = full_space;
  396. tcp_select_initial_window(sk, full_space, req->mss,
  397. &req->rsk_rcv_wnd, &req->rsk_window_clamp,
  398. ireq->wscale_ok, &rcv_wscale,
  399. dst_metric(&rt->dst, RTAX_INITRWND));
  400. /* req->syncookie is set true only if ACK is validated
  401. * by BPF kfunc, then, rcv_wscale is already configured.
  402. */
  403. if (!req->syncookie)
  404. ireq->rcv_wscale = rcv_wscale;
  405. ireq->ecn_ok &= cookie_ecn_ok(net, &rt->dst);
  406. ret = tcp_get_cookie_sock(sk, skb, req, &rt->dst);
  407. /* ip_queue_xmit() depends on our flow being setup
  408. * Normal sockets get it right from inet_csk_route_child_sock()
  409. */
  410. if (!ret) {
  411. SKB_DR_SET(reason, NO_SOCKET);
  412. goto out_drop;
  413. }
  414. inet_sk(ret)->cork.fl.u.ip4 = fl4;
  415. out:
  416. return ret;
  417. out_free:
  418. reqsk_free(req);
  419. out_drop:
  420. sk_skb_reason_drop(sk, skb, reason);
  421. return NULL;
  422. }