syncookies.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. /* Timestamps: lowest 9 bits store TCP options */
  19. #define TSBITS 9
  20. #define TSMASK (((__u32)1 << TSBITS) - 1)
  21. extern int sysctl_tcp_syncookies;
  22. __u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS];
  23. EXPORT_SYMBOL(syncookie_secret);
  24. static __init int init_syncookies(void)
  25. {
  26. get_random_bytes(syncookie_secret, sizeof(syncookie_secret));
  27. return 0;
  28. }
  29. __initcall(init_syncookies);
  30. #define COOKIEBITS 24 /* Upper bits store count */
  31. #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
  32. static DEFINE_PER_CPU(__u32, cookie_scratch)[16 + 5 + SHA_WORKSPACE_WORDS];
  33. static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
  34. u32 count, int c)
  35. {
  36. __u32 *tmp = __get_cpu_var(cookie_scratch);
  37. memcpy(tmp + 4, syncookie_secret[c], sizeof(syncookie_secret[c]));
  38. tmp[0] = (__force u32)saddr;
  39. tmp[1] = (__force u32)daddr;
  40. tmp[2] = ((__force u32)sport << 16) + (__force u32)dport;
  41. tmp[3] = count;
  42. sha_transform(tmp + 16, (__u8 *)tmp, tmp + 16 + 5);
  43. return tmp[17];
  44. }
  45. /*
  46. * when syncookies are in effect and tcp timestamps are enabled we encode
  47. * tcp options in the lowest 9 bits of the timestamp value that will be
  48. * sent in the syn-ack.
  49. * Since subsequent timestamps use the normal tcp_time_stamp value, we
  50. * must make sure that the resulting initial timestamp is <= tcp_time_stamp.
  51. */
  52. __u32 cookie_init_timestamp(struct request_sock *req)
  53. {
  54. struct inet_request_sock *ireq;
  55. u32 ts, ts_now = tcp_time_stamp;
  56. u32 options = 0;
  57. ireq = inet_rsk(req);
  58. if (ireq->wscale_ok) {
  59. options = ireq->snd_wscale;
  60. options |= ireq->rcv_wscale << 4;
  61. }
  62. options |= ireq->sack_ok << 8;
  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. * This table has to be sorted and terminated with (__u16)-1.
  118. * XXX generate a better table.
  119. * Unresolved Issues: HIPPI with a 64k MSS is not well supported.
  120. */
  121. static __u16 const msstab[] = {
  122. 64 - 1,
  123. 256 - 1,
  124. 512 - 1,
  125. 536 - 1,
  126. 1024 - 1,
  127. 1440 - 1,
  128. 1460 - 1,
  129. 4312 - 1,
  130. (__u16)-1
  131. };
  132. /* The number doesn't include the -1 terminator */
  133. #define NUM_MSS (ARRAY_SIZE(msstab) - 1)
  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. struct tcp_sock *tp = tcp_sk(sk);
  141. const struct iphdr *iph = ip_hdr(skb);
  142. const struct tcphdr *th = tcp_hdr(skb);
  143. int mssind;
  144. const __u16 mss = *mssp;
  145. tp->last_synq_overflow = jiffies;
  146. /* XXX sort msstab[] by probability? Binary search? */
  147. for (mssind = 0; mss > msstab[mssind + 1]; mssind++)
  148. ;
  149. *mssp = msstab[mssind] + 1;
  150. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
  151. return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
  152. th->source, th->dest, ntohl(th->seq),
  153. jiffies / (HZ * 60), mssind);
  154. }
  155. /*
  156. * This (misnamed) value is the age of syncookie which is permitted.
  157. * Its ideal value should be dependent on TCP_TIMEOUT_INIT and
  158. * sysctl_tcp_retries1. It's a rather complicated formula (exponential
  159. * backoff) to compute at runtime so it's currently hardcoded here.
  160. */
  161. #define COUNTER_TRIES 4
  162. /*
  163. * Check if a ack sequence number is a valid syncookie.
  164. * Return the decoded mss if it is, or 0 if not.
  165. */
  166. static inline int cookie_check(struct sk_buff *skb, __u32 cookie)
  167. {
  168. const struct iphdr *iph = ip_hdr(skb);
  169. const struct tcphdr *th = tcp_hdr(skb);
  170. __u32 seq = ntohl(th->seq) - 1;
  171. __u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
  172. th->source, th->dest, seq,
  173. jiffies / (HZ * 60),
  174. COUNTER_TRIES);
  175. return mssind < NUM_MSS ? msstab[mssind] + 1 : 0;
  176. }
  177. static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  178. struct request_sock *req,
  179. struct dst_entry *dst)
  180. {
  181. struct inet_connection_sock *icsk = inet_csk(sk);
  182. struct sock *child;
  183. child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst);
  184. if (child)
  185. inet_csk_reqsk_queue_add(sk, req, child);
  186. else
  187. reqsk_free(req);
  188. return child;
  189. }
  190. /*
  191. * when syncookies are in effect and tcp timestamps are enabled we stored
  192. * additional tcp options in the timestamp.
  193. * This extracts these options from the timestamp echo.
  194. *
  195. * The lowest 4 bits are for snd_wscale
  196. * The next 4 lsb are for rcv_wscale
  197. * The next lsb is for sack_ok
  198. */
  199. void cookie_check_timestamp(struct tcp_options_received *tcp_opt)
  200. {
  201. /* echoed timestamp, 9 lowest bits contain options */
  202. u32 options = tcp_opt->rcv_tsecr & TSMASK;
  203. tcp_opt->snd_wscale = options & 0xf;
  204. options >>= 4;
  205. tcp_opt->rcv_wscale = options & 0xf;
  206. tcp_opt->sack_ok = (options >> 4) & 0x1;
  207. if (tcp_opt->sack_ok)
  208. tcp_sack_reset(tcp_opt);
  209. if (tcp_opt->snd_wscale || tcp_opt->rcv_wscale)
  210. tcp_opt->wscale_ok = 1;
  211. }
  212. EXPORT_SYMBOL(cookie_check_timestamp);
  213. struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
  214. struct ip_options *opt)
  215. {
  216. struct inet_request_sock *ireq;
  217. struct tcp_request_sock *treq;
  218. struct tcp_sock *tp = tcp_sk(sk);
  219. const struct tcphdr *th = tcp_hdr(skb);
  220. __u32 cookie = ntohl(th->ack_seq) - 1;
  221. struct sock *ret = sk;
  222. struct request_sock *req;
  223. int mss;
  224. struct rtable *rt;
  225. __u8 rcv_wscale;
  226. struct tcp_options_received tcp_opt;
  227. if (!sysctl_tcp_syncookies || !th->ack)
  228. goto out;
  229. if (time_after(jiffies, tp->last_synq_overflow + TCP_TIMEOUT_INIT) ||
  230. (mss = cookie_check(skb, cookie)) == 0) {
  231. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESFAILED);
  232. goto out;
  233. }
  234. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESRECV);
  235. /* check for timestamp cookie support */
  236. memset(&tcp_opt, 0, sizeof(tcp_opt));
  237. tcp_parse_options(skb, &tcp_opt, 0);
  238. if (tcp_opt.saw_tstamp)
  239. cookie_check_timestamp(&tcp_opt);
  240. ret = NULL;
  241. req = inet_reqsk_alloc(&tcp_request_sock_ops); /* for safety */
  242. if (!req)
  243. goto out;
  244. if (security_inet_conn_request(sk, skb, req)) {
  245. reqsk_free(req);
  246. goto out;
  247. }
  248. ireq = inet_rsk(req);
  249. treq = tcp_rsk(req);
  250. treq->rcv_isn = ntohl(th->seq) - 1;
  251. treq->snt_isn = cookie;
  252. req->mss = mss;
  253. ireq->rmt_port = th->source;
  254. ireq->loc_addr = ip_hdr(skb)->daddr;
  255. ireq->rmt_addr = ip_hdr(skb)->saddr;
  256. ireq->ecn_ok = 0;
  257. ireq->snd_wscale = tcp_opt.snd_wscale;
  258. ireq->rcv_wscale = tcp_opt.rcv_wscale;
  259. ireq->sack_ok = tcp_opt.sack_ok;
  260. ireq->wscale_ok = tcp_opt.wscale_ok;
  261. ireq->tstamp_ok = tcp_opt.saw_tstamp;
  262. req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
  263. /* We throwed the options of the initial SYN away, so we hope
  264. * the ACK carries the same options again (see RFC1122 4.2.3.8)
  265. */
  266. if (opt && opt->optlen) {
  267. int opt_size = sizeof(struct ip_options) + opt->optlen;
  268. ireq->opt = kmalloc(opt_size, GFP_ATOMIC);
  269. if (ireq->opt != NULL && ip_options_echo(ireq->opt, skb)) {
  270. kfree(ireq->opt);
  271. ireq->opt = NULL;
  272. }
  273. }
  274. req->expires = 0UL;
  275. req->retrans = 0;
  276. /*
  277. * We need to lookup the route here to get at the correct
  278. * window size. We should better make sure that the window size
  279. * hasn't changed since we received the original syn, but I see
  280. * no easy way to do this.
  281. */
  282. {
  283. struct flowi fl = { .nl_u = { .ip4_u =
  284. { .daddr = ((opt && opt->srr) ?
  285. opt->faddr :
  286. ireq->rmt_addr),
  287. .saddr = ireq->loc_addr,
  288. .tos = RT_CONN_FLAGS(sk) } },
  289. .proto = IPPROTO_TCP,
  290. .uli_u = { .ports =
  291. { .sport = th->dest,
  292. .dport = th->source } } };
  293. security_req_classify_flow(req, &fl);
  294. if (ip_route_output_key(&init_net, &rt, &fl)) {
  295. reqsk_free(req);
  296. goto out;
  297. }
  298. }
  299. /* Try to redo what tcp_v4_send_synack did. */
  300. req->window_clamp = tp->window_clamp ? :dst_metric(&rt->u.dst, RTAX_WINDOW);
  301. tcp_select_initial_window(tcp_full_space(sk), req->mss,
  302. &req->rcv_wnd, &req->window_clamp,
  303. ireq->wscale_ok, &rcv_wscale);
  304. ireq->rcv_wscale = rcv_wscale;
  305. ret = get_cookie_sock(sk, skb, req, &rt->u.dst);
  306. out: return ret;
  307. }