syncookies.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Syncookies implementation for the Linux kernel
  3. *
  4. * Copyright (C) 1997 Andi Kleen
  5. * Based on ideas by D.J.Bernstein and Eric Schenk.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/tcp.h>
  13. #include <linux/slab.h>
  14. #include <linux/random.h>
  15. #include <linux/cryptohash.h>
  16. #include <linux/kernel.h>
  17. #include <linux/export.h>
  18. #include <net/tcp.h>
  19. #include <net/route.h>
  20. /* Timestamps: lowest bits store TCP options */
  21. #define TSBITS 6
  22. #define TSMASK (((__u32)1 << TSBITS) - 1)
  23. extern int sysctl_tcp_syncookies;
  24. __u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS];
  25. EXPORT_SYMBOL(syncookie_secret);
  26. static __init int init_syncookies(void)
  27. {
  28. get_random_bytes(syncookie_secret, sizeof(syncookie_secret));
  29. return 0;
  30. }
  31. __initcall(init_syncookies);
  32. #define COOKIEBITS 24 /* Upper bits store count */
  33. #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
  34. static DEFINE_PER_CPU(__u32 [16 + 5 + SHA_WORKSPACE_WORDS],
  35. ipv4_cookie_scratch);
  36. static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
  37. u32 count, int c)
  38. {
  39. __u32 *tmp = __get_cpu_var(ipv4_cookie_scratch);
  40. memcpy(tmp + 4, syncookie_secret[c], sizeof(syncookie_secret[c]));
  41. tmp[0] = (__force u32)saddr;
  42. tmp[1] = (__force u32)daddr;
  43. tmp[2] = ((__force u32)sport << 16) + (__force u32)dport;
  44. tmp[3] = count;
  45. sha_transform(tmp + 16, (__u8 *)tmp, tmp + 16 + 5);
  46. return tmp[17];
  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. __u32 cookie_init_timestamp(struct request_sock *req)
  56. {
  57. struct inet_request_sock *ireq;
  58. u32 ts, ts_now = tcp_time_stamp;
  59. u32 options = 0;
  60. ireq = inet_rsk(req);
  61. options = ireq->wscale_ok ? ireq->snd_wscale : 0xf;
  62. options |= ireq->sack_ok << 4;
  63. options |= ireq->ecn_ok << 5;
  64. ts = ts_now & ~TSMASK;
  65. ts |= options;
  66. if (ts > ts_now) {
  67. ts >>= TSBITS;
  68. ts--;
  69. ts <<= TSBITS;
  70. ts |= options;
  71. }
  72. return ts;
  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(struct sock *sk, struct sk_buff *skb, __u16 *mssp)
  153. {
  154. const struct iphdr *iph = ip_hdr(skb);
  155. const struct tcphdr *th = tcp_hdr(skb);
  156. tcp_synq_overflow(sk);
  157. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
  158. return __cookie_v4_init_sequence(iph, th, mssp);
  159. }
  160. /*
  161. * Check if a ack sequence number is a valid syncookie.
  162. * Return the decoded mss if it is, or 0 if not.
  163. */
  164. int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
  165. u32 cookie)
  166. {
  167. __u32 seq = ntohl(th->seq) - 1;
  168. __u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
  169. th->source, th->dest, seq);
  170. return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
  171. }
  172. EXPORT_SYMBOL_GPL(__cookie_v4_check);
  173. static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  174. struct request_sock *req,
  175. struct dst_entry *dst)
  176. {
  177. struct inet_connection_sock *icsk = inet_csk(sk);
  178. struct sock *child;
  179. child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst);
  180. if (child)
  181. inet_csk_reqsk_queue_add(sk, req, child);
  182. else
  183. reqsk_free(req);
  184. return child;
  185. }
  186. /*
  187. * when syncookies are in effect and tcp timestamps are enabled we stored
  188. * additional tcp options in the timestamp.
  189. * This extracts these options from the timestamp echo.
  190. *
  191. * The lowest 4 bits store snd_wscale.
  192. * next 2 bits indicate SACK and ECN support.
  193. *
  194. * return false if we decode an option that should not be.
  195. */
  196. bool cookie_check_timestamp(struct tcp_options_received *tcp_opt,
  197. struct net *net, bool *ecn_ok)
  198. {
  199. /* echoed timestamp, lowest bits contain options */
  200. u32 options = tcp_opt->rcv_tsecr & TSMASK;
  201. if (!tcp_opt->saw_tstamp) {
  202. tcp_clear_options(tcp_opt);
  203. return true;
  204. }
  205. if (!sysctl_tcp_timestamps)
  206. return false;
  207. tcp_opt->sack_ok = (options & (1 << 4)) ? TCP_SACK_SEEN : 0;
  208. *ecn_ok = (options >> 5) & 1;
  209. if (*ecn_ok && !net->ipv4.sysctl_tcp_ecn)
  210. return false;
  211. if (tcp_opt->sack_ok && !sysctl_tcp_sack)
  212. return false;
  213. if ((options & 0xf) == 0xf)
  214. return true; /* no window scaling */
  215. tcp_opt->wscale_ok = 1;
  216. tcp_opt->snd_wscale = options & 0xf;
  217. return sysctl_tcp_window_scaling != 0;
  218. }
  219. EXPORT_SYMBOL(cookie_check_timestamp);
  220. struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
  221. struct ip_options *opt)
  222. {
  223. struct tcp_options_received tcp_opt;
  224. struct inet_request_sock *ireq;
  225. struct tcp_request_sock *treq;
  226. struct tcp_sock *tp = tcp_sk(sk);
  227. const struct tcphdr *th = tcp_hdr(skb);
  228. __u32 cookie = ntohl(th->ack_seq) - 1;
  229. struct sock *ret = sk;
  230. struct request_sock *req;
  231. int mss;
  232. struct rtable *rt;
  233. __u8 rcv_wscale;
  234. bool ecn_ok = false;
  235. struct flowi4 fl4;
  236. if (!sysctl_tcp_syncookies || !th->ack || th->rst)
  237. goto out;
  238. if (tcp_synq_no_recent_overflow(sk) ||
  239. (mss = __cookie_v4_check(ip_hdr(skb), th, cookie)) == 0) {
  240. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
  241. goto out;
  242. }
  243. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV);
  244. /* check for timestamp cookie support */
  245. memset(&tcp_opt, 0, sizeof(tcp_opt));
  246. tcp_parse_options(skb, &tcp_opt, 0, NULL);
  247. if (!cookie_check_timestamp(&tcp_opt, sock_net(sk), &ecn_ok))
  248. goto out;
  249. ret = NULL;
  250. req = inet_reqsk_alloc(&tcp_request_sock_ops); /* for safety */
  251. if (!req)
  252. goto out;
  253. ireq = inet_rsk(req);
  254. treq = tcp_rsk(req);
  255. treq->rcv_isn = ntohl(th->seq) - 1;
  256. treq->snt_isn = cookie;
  257. req->mss = mss;
  258. ireq->ir_num = ntohs(th->dest);
  259. ireq->ir_rmt_port = th->source;
  260. ireq->ir_loc_addr = ip_hdr(skb)->daddr;
  261. ireq->ir_rmt_addr = ip_hdr(skb)->saddr;
  262. ireq->ecn_ok = ecn_ok;
  263. ireq->snd_wscale = tcp_opt.snd_wscale;
  264. ireq->sack_ok = tcp_opt.sack_ok;
  265. ireq->wscale_ok = tcp_opt.wscale_ok;
  266. ireq->tstamp_ok = tcp_opt.saw_tstamp;
  267. req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
  268. treq->snt_synack = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsecr : 0;
  269. treq->listener = NULL;
  270. /* We throwed the options of the initial SYN away, so we hope
  271. * the ACK carries the same options again (see RFC1122 4.2.3.8)
  272. */
  273. if (opt && opt->optlen) {
  274. int opt_size = sizeof(struct ip_options_rcu) + opt->optlen;
  275. ireq->opt = kmalloc(opt_size, GFP_ATOMIC);
  276. if (ireq->opt != NULL && ip_options_echo(&ireq->opt->opt, skb)) {
  277. kfree(ireq->opt);
  278. ireq->opt = NULL;
  279. }
  280. }
  281. if (security_inet_conn_request(sk, skb, req)) {
  282. reqsk_free(req);
  283. goto out;
  284. }
  285. req->expires = 0UL;
  286. req->num_retrans = 0;
  287. /*
  288. * We need to lookup the route here to get at the correct
  289. * window size. We should better make sure that the window size
  290. * hasn't changed since we received the original syn, but I see
  291. * no easy way to do this.
  292. */
  293. flowi4_init_output(&fl4, sk->sk_bound_dev_if, sk->sk_mark,
  294. RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP,
  295. inet_sk_flowi_flags(sk),
  296. (opt && opt->srr) ? opt->faddr : ireq->ir_rmt_addr,
  297. ireq->ir_loc_addr, th->source, th->dest);
  298. security_req_classify_flow(req, flowi4_to_flowi(&fl4));
  299. rt = ip_route_output_key(sock_net(sk), &fl4);
  300. if (IS_ERR(rt)) {
  301. reqsk_free(req);
  302. goto out;
  303. }
  304. /* Try to redo what tcp_v4_send_synack did. */
  305. req->window_clamp = tp->window_clamp ? :dst_metric(&rt->dst, RTAX_WINDOW);
  306. tcp_select_initial_window(tcp_full_space(sk), req->mss,
  307. &req->rcv_wnd, &req->window_clamp,
  308. ireq->wscale_ok, &rcv_wscale,
  309. dst_metric(&rt->dst, RTAX_INITRWND));
  310. ireq->rcv_wscale = rcv_wscale;
  311. ret = get_cookie_sock(sk, skb, req, &rt->dst);
  312. /* ip_queue_xmit() depends on our flow being setup
  313. * Normal sockets get it right from inet_csk_route_child_sock()
  314. */
  315. if (ret)
  316. inet_sk(ret)->cork.fl.u.ip4 = fl4;
  317. out: return ret;
  318. }