syncookies.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 <net/tcp.h>
  18. #include <net/route.h>
  19. /* Timestamps: lowest bits store TCP options */
  20. #define TSBITS 6
  21. #define TSMASK (((__u32)1 << TSBITS) - 1)
  22. extern int sysctl_tcp_syncookies;
  23. __u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS];
  24. EXPORT_SYMBOL(syncookie_secret);
  25. static __init int init_syncookies(void)
  26. {
  27. get_random_bytes(syncookie_secret, sizeof(syncookie_secret));
  28. return 0;
  29. }
  30. __initcall(init_syncookies);
  31. #define COOKIEBITS 24 /* Upper bits store count */
  32. #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
  33. static DEFINE_PER_CPU(__u32 [16 + 5 + SHA_WORKSPACE_WORDS],
  34. ipv4_cookie_scratch);
  35. static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
  36. u32 count, int c)
  37. {
  38. __u32 *tmp = __get_cpu_var(ipv4_cookie_scratch);
  39. memcpy(tmp + 4, syncookie_secret[c], sizeof(syncookie_secret[c]));
  40. tmp[0] = (__force u32)saddr;
  41. tmp[1] = (__force u32)daddr;
  42. tmp[2] = ((__force u32)sport << 16) + (__force u32)dport;
  43. tmp[3] = count;
  44. sha_transform(tmp + 16, (__u8 *)tmp, tmp + 16 + 5);
  45. return tmp[17];
  46. }
  47. /*
  48. * when syncookies are in effect and tcp timestamps are enabled we encode
  49. * tcp options in the lower bits of the timestamp value that will be
  50. * sent in the syn-ack.
  51. * Since subsequent timestamps use the normal tcp_time_stamp value, we
  52. * must make sure that the resulting initial timestamp is <= tcp_time_stamp.
  53. */
  54. __u32 cookie_init_timestamp(struct request_sock *req)
  55. {
  56. struct inet_request_sock *ireq;
  57. u32 ts, ts_now = tcp_time_stamp;
  58. u32 options = 0;
  59. ireq = inet_rsk(req);
  60. options = ireq->wscale_ok ? ireq->snd_wscale : 0xf;
  61. options |= ireq->sack_ok << 4;
  62. options |= ireq->ecn_ok << 5;
  63. ts = ts_now & ~TSMASK;
  64. ts |= options;
  65. if (ts > ts_now) {
  66. ts >>= TSBITS;
  67. ts--;
  68. ts <<= TSBITS;
  69. ts |= options;
  70. }
  71. return ts;
  72. }
  73. static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
  74. __be16 dport, __u32 sseq, __u32 count,
  75. __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. 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 within
  98. * "maxdiff" if the current (passed-in) "count". The return value
  99. * is (__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. __u32 count, __u32 maxdiff)
  104. {
  105. __u32 diff;
  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 >= maxdiff)
  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 taken from the 2009 paper
  118. * 'Measuring TCP Maximum Segment Size' by S. Alcock and R. Nelson:
  119. * - values 1440 to 1460 accounted for 80% of observed mss values
  120. * - values outside the 536-1460 range are rare (<0.2%).
  121. *
  122. * Table must be sorted.
  123. */
  124. static __u16 const msstab[] = {
  125. 64,
  126. 512,
  127. 536,
  128. 1024,
  129. 1440,
  130. 1460,
  131. 4312,
  132. 8960,
  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(struct sock *sk, struct sk_buff *skb, __u16 *mssp)
  139. {
  140. const struct iphdr *iph = ip_hdr(skb);
  141. const struct tcphdr *th = tcp_hdr(skb);
  142. int mssind;
  143. const __u16 mss = *mssp;
  144. tcp_synq_overflow(sk);
  145. for (mssind = ARRAY_SIZE(msstab) - 1; mssind ; mssind--)
  146. if (mss >= msstab[mssind])
  147. break;
  148. *mssp = msstab[mssind];
  149. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
  150. return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
  151. th->source, th->dest, ntohl(th->seq),
  152. jiffies / (HZ * 60), mssind);
  153. }
  154. /*
  155. * This (misnamed) value is the age of syncookie which is permitted.
  156. * Its ideal value should be dependent on TCP_TIMEOUT_INIT and
  157. * sysctl_tcp_retries1. It's a rather complicated formula (exponential
  158. * backoff) to compute at runtime so it's currently hardcoded here.
  159. */
  160. #define COUNTER_TRIES 4
  161. /*
  162. * Check if a ack sequence number is a valid syncookie.
  163. * Return the decoded mss if it is, or 0 if not.
  164. */
  165. static inline int cookie_check(struct sk_buff *skb, __u32 cookie)
  166. {
  167. const struct iphdr *iph = ip_hdr(skb);
  168. const struct tcphdr *th = tcp_hdr(skb);
  169. __u32 seq = ntohl(th->seq) - 1;
  170. __u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
  171. th->source, th->dest, seq,
  172. jiffies / (HZ * 60),
  173. COUNTER_TRIES);
  174. return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
  175. }
  176. static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  177. struct request_sock *req,
  178. struct dst_entry *dst)
  179. {
  180. struct inet_connection_sock *icsk = inet_csk(sk);
  181. struct sock *child;
  182. child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst);
  183. if (child)
  184. inet_csk_reqsk_queue_add(sk, req, child);
  185. else
  186. reqsk_free(req);
  187. return child;
  188. }
  189. /*
  190. * when syncookies are in effect and tcp timestamps are enabled we stored
  191. * additional tcp options in the timestamp.
  192. * This extracts these options from the timestamp echo.
  193. *
  194. * The lowest 4 bits store snd_wscale.
  195. * next 2 bits indicate SACK and ECN support.
  196. *
  197. * return false if we decode an option that should not be.
  198. */
  199. bool cookie_check_timestamp(struct tcp_options_received *tcp_opt, bool *ecn_ok)
  200. {
  201. /* echoed timestamp, lowest bits contain options */
  202. u32 options = tcp_opt->rcv_tsecr & TSMASK;
  203. if (!tcp_opt->saw_tstamp) {
  204. tcp_clear_options(tcp_opt);
  205. return true;
  206. }
  207. if (!sysctl_tcp_timestamps)
  208. return false;
  209. tcp_opt->sack_ok = (options >> 4) & 0x1;
  210. *ecn_ok = (options >> 5) & 1;
  211. if (*ecn_ok && !sysctl_tcp_ecn)
  212. return false;
  213. if (tcp_opt->sack_ok && !sysctl_tcp_sack)
  214. return false;
  215. if ((options & 0xf) == 0xf)
  216. return true; /* no window scaling */
  217. tcp_opt->wscale_ok = 1;
  218. tcp_opt->snd_wscale = options & 0xf;
  219. return sysctl_tcp_window_scaling != 0;
  220. }
  221. EXPORT_SYMBOL(cookie_check_timestamp);
  222. struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
  223. struct ip_options *opt)
  224. {
  225. struct tcp_options_received tcp_opt;
  226. u8 *hash_location;
  227. struct inet_request_sock *ireq;
  228. struct tcp_request_sock *treq;
  229. struct tcp_sock *tp = tcp_sk(sk);
  230. const struct tcphdr *th = tcp_hdr(skb);
  231. __u32 cookie = ntohl(th->ack_seq) - 1;
  232. struct sock *ret = sk;
  233. struct request_sock *req;
  234. int mss;
  235. struct rtable *rt;
  236. __u8 rcv_wscale;
  237. bool ecn_ok = false;
  238. struct flowi4 fl4;
  239. if (!sysctl_tcp_syncookies || !th->ack || th->rst)
  240. goto out;
  241. if (tcp_synq_no_recent_overflow(sk) ||
  242. (mss = cookie_check(skb, cookie)) == 0) {
  243. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
  244. goto out;
  245. }
  246. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV);
  247. /* check for timestamp cookie support */
  248. memset(&tcp_opt, 0, sizeof(tcp_opt));
  249. tcp_parse_options(skb, &tcp_opt, &hash_location, 0);
  250. if (!cookie_check_timestamp(&tcp_opt, &ecn_ok))
  251. goto out;
  252. ret = NULL;
  253. req = inet_reqsk_alloc(&tcp_request_sock_ops); /* for safety */
  254. if (!req)
  255. goto out;
  256. ireq = inet_rsk(req);
  257. treq = tcp_rsk(req);
  258. treq->rcv_isn = ntohl(th->seq) - 1;
  259. treq->snt_isn = cookie;
  260. req->mss = mss;
  261. ireq->loc_port = th->dest;
  262. ireq->rmt_port = th->source;
  263. ireq->loc_addr = ip_hdr(skb)->daddr;
  264. ireq->rmt_addr = ip_hdr(skb)->saddr;
  265. ireq->ecn_ok = ecn_ok;
  266. ireq->snd_wscale = tcp_opt.snd_wscale;
  267. ireq->sack_ok = tcp_opt.sack_ok;
  268. ireq->wscale_ok = tcp_opt.wscale_ok;
  269. ireq->tstamp_ok = tcp_opt.saw_tstamp;
  270. req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
  271. /* We throwed the options of the initial SYN away, so we hope
  272. * the ACK carries the same options again (see RFC1122 4.2.3.8)
  273. */
  274. if (opt && opt->optlen) {
  275. int opt_size = sizeof(struct ip_options_rcu) + opt->optlen;
  276. ireq->opt = kmalloc(opt_size, GFP_ATOMIC);
  277. if (ireq->opt != NULL && ip_options_echo(&ireq->opt->opt, skb)) {
  278. kfree(ireq->opt);
  279. ireq->opt = NULL;
  280. }
  281. }
  282. if (security_inet_conn_request(sk, skb, req)) {
  283. reqsk_free(req);
  284. goto out;
  285. }
  286. req->expires = 0UL;
  287. req->retrans = 0;
  288. /*
  289. * We need to lookup the route here to get at the correct
  290. * window size. We should better make sure that the window size
  291. * hasn't changed since we received the original syn, but I see
  292. * no easy way to do this.
  293. */
  294. flowi4_init_output(&fl4, 0, sk->sk_mark, RT_CONN_FLAGS(sk),
  295. RT_SCOPE_UNIVERSE, IPPROTO_TCP,
  296. inet_sk_flowi_flags(sk),
  297. (opt && opt->srr) ? opt->faddr : ireq->rmt_addr,
  298. ireq->loc_addr, th->source, th->dest);
  299. security_req_classify_flow(req, flowi4_to_flowi(&fl4));
  300. rt = ip_route_output_key(sock_net(sk), &fl4);
  301. if (IS_ERR(rt)) {
  302. reqsk_free(req);
  303. goto out;
  304. }
  305. /* Try to redo what tcp_v4_send_synack did. */
  306. req->window_clamp = tp->window_clamp ? :dst_metric(&rt->dst, RTAX_WINDOW);
  307. tcp_select_initial_window(tcp_full_space(sk), req->mss,
  308. &req->rcv_wnd, &req->window_clamp,
  309. ireq->wscale_ok, &rcv_wscale,
  310. dst_metric(&rt->dst, RTAX_INITRWND));
  311. ireq->rcv_wscale = rcv_wscale;
  312. ret = get_cookie_sock(sk, skb, req, &rt->dst);
  313. /* ip_queue_xmit() depends on our flow being setup
  314. * Normal sockets get it right from inet_csk_route_child_sock()
  315. */
  316. if (ret)
  317. inet_sk(ret)->cork.fl.u.ip4 = fl4;
  318. out: return ret;
  319. }