syncookies.c 14 KB

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