syncookies.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. * $Id: syncookies.c,v 1.18 2002/02/01 22:01:04 davem Exp $
  13. *
  14. * Missing: IPv6 support.
  15. */
  16. #include <linux/tcp.h>
  17. #include <linux/slab.h>
  18. #include <linux/random.h>
  19. #include <linux/cryptohash.h>
  20. #include <linux/kernel.h>
  21. #include <net/tcp.h>
  22. extern int sysctl_tcp_syncookies;
  23. static __u32 syncookie_secret[2][16-3+SHA_DIGEST_WORDS];
  24. static __init int init_syncookies(void)
  25. {
  26. get_random_bytes(syncookie_secret, sizeof(syncookie_secret));
  27. return 0;
  28. }
  29. module_init(init_syncookies);
  30. #define COOKIEBITS 24 /* Upper bits store count */
  31. #define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
  32. static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
  33. u32 count, int c)
  34. {
  35. __u32 tmp[16 + 5 + SHA_WORKSPACE_WORDS];
  36. memcpy(tmp + 3, syncookie_secret[c], sizeof(syncookie_secret[c]));
  37. tmp[0] = (__force u32)saddr;
  38. tmp[1] = (__force u32)daddr;
  39. tmp[2] = ((__force u32)sport << 16) + (__force u32)dport;
  40. tmp[3] = count;
  41. sha_transform(tmp + 16, (__u8 *)tmp, tmp + 16 + 5);
  42. return tmp[17];
  43. }
  44. static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
  45. __be16 dport, __u32 sseq, __u32 count,
  46. __u32 data)
  47. {
  48. /*
  49. * Compute the secure sequence number.
  50. * The output should be:
  51. * HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24)
  52. * + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24).
  53. * Where sseq is their sequence number and count increases every
  54. * minute by 1.
  55. * As an extra hack, we add a small "data" value that encodes the
  56. * MSS into the second hash value.
  57. */
  58. return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
  59. sseq + (count << COOKIEBITS) +
  60. ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
  61. & COOKIEMASK));
  62. }
  63. /*
  64. * This retrieves the small "data" value from the syncookie.
  65. * If the syncookie is bad, the data returned will be out of
  66. * range. This must be checked by the caller.
  67. *
  68. * The count value used to generate the cookie must be within
  69. * "maxdiff" if the current (passed-in) "count". The return value
  70. * is (__u32)-1 if this test fails.
  71. */
  72. static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
  73. __be16 sport, __be16 dport, __u32 sseq,
  74. __u32 count, __u32 maxdiff)
  75. {
  76. __u32 diff;
  77. /* Strip away the layers from the cookie */
  78. cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
  79. /* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
  80. diff = (count - (cookie >> COOKIEBITS)) & ((__u32) - 1 >> COOKIEBITS);
  81. if (diff >= maxdiff)
  82. return (__u32)-1;
  83. return (cookie -
  84. cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
  85. & COOKIEMASK; /* Leaving the data behind */
  86. }
  87. /*
  88. * This table has to be sorted and terminated with (__u16)-1.
  89. * XXX generate a better table.
  90. * Unresolved Issues: HIPPI with a 64k MSS is not well supported.
  91. */
  92. static __u16 const msstab[] = {
  93. 64 - 1,
  94. 256 - 1,
  95. 512 - 1,
  96. 536 - 1,
  97. 1024 - 1,
  98. 1440 - 1,
  99. 1460 - 1,
  100. 4312 - 1,
  101. (__u16)-1
  102. };
  103. /* The number doesn't include the -1 terminator */
  104. #define NUM_MSS (ARRAY_SIZE(msstab) - 1)
  105. /*
  106. * Generate a syncookie. mssp points to the mss, which is returned
  107. * rounded down to the value encoded in the cookie.
  108. */
  109. __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb, __u16 *mssp)
  110. {
  111. struct tcp_sock *tp = tcp_sk(sk);
  112. const struct iphdr *iph = ip_hdr(skb);
  113. const struct tcphdr *th = tcp_hdr(skb);
  114. int mssind;
  115. const __u16 mss = *mssp;
  116. tp->last_synq_overflow = jiffies;
  117. /* XXX sort msstab[] by probability? Binary search? */
  118. for (mssind = 0; mss > msstab[mssind + 1]; mssind++)
  119. ;
  120. *mssp = msstab[mssind] + 1;
  121. NET_INC_STATS_BH(LINUX_MIB_SYNCOOKIESSENT);
  122. return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
  123. th->source, th->dest, ntohl(th->seq),
  124. jiffies / (HZ * 60), mssind);
  125. }
  126. /*
  127. * This (misnamed) value is the age of syncookie which is permitted.
  128. * Its ideal value should be dependent on TCP_TIMEOUT_INIT and
  129. * sysctl_tcp_retries1. It's a rather complicated formula (exponential
  130. * backoff) to compute at runtime so it's currently hardcoded here.
  131. */
  132. #define COUNTER_TRIES 4
  133. /*
  134. * Check if a ack sequence number is a valid syncookie.
  135. * Return the decoded mss if it is, or 0 if not.
  136. */
  137. static inline int cookie_check(struct sk_buff *skb, __u32 cookie)
  138. {
  139. const struct iphdr *iph = ip_hdr(skb);
  140. const struct tcphdr *th = tcp_hdr(skb);
  141. __u32 seq = ntohl(th->seq) - 1;
  142. __u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
  143. th->source, th->dest, seq,
  144. jiffies / (HZ * 60),
  145. COUNTER_TRIES);
  146. return mssind < NUM_MSS ? msstab[mssind] + 1 : 0;
  147. }
  148. static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  149. struct request_sock *req,
  150. struct dst_entry *dst)
  151. {
  152. struct inet_connection_sock *icsk = inet_csk(sk);
  153. struct sock *child;
  154. child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst);
  155. if (child)
  156. inet_csk_reqsk_queue_add(sk, req, child);
  157. else
  158. reqsk_free(req);
  159. return child;
  160. }
  161. struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
  162. struct ip_options *opt)
  163. {
  164. struct inet_request_sock *ireq;
  165. struct tcp_request_sock *treq;
  166. struct tcp_sock *tp = tcp_sk(sk);
  167. const struct tcphdr *th = tcp_hdr(skb);
  168. __u32 cookie = ntohl(th->ack_seq) - 1;
  169. struct sock *ret = sk;
  170. struct request_sock *req;
  171. int mss;
  172. struct rtable *rt;
  173. __u8 rcv_wscale;
  174. if (!sysctl_tcp_syncookies || !th->ack)
  175. goto out;
  176. if (time_after(jiffies, tp->last_synq_overflow + TCP_TIMEOUT_INIT) ||
  177. (mss = cookie_check(skb, cookie)) == 0) {
  178. NET_INC_STATS_BH(LINUX_MIB_SYNCOOKIESFAILED);
  179. goto out;
  180. }
  181. NET_INC_STATS_BH(LINUX_MIB_SYNCOOKIESRECV);
  182. ret = NULL;
  183. req = reqsk_alloc(&tcp_request_sock_ops); /* for safety */
  184. if (!req)
  185. goto out;
  186. if (security_inet_conn_request(sk, skb, req)) {
  187. reqsk_free(req);
  188. goto out;
  189. }
  190. ireq = inet_rsk(req);
  191. treq = tcp_rsk(req);
  192. treq->rcv_isn = ntohl(th->seq) - 1;
  193. treq->snt_isn = cookie;
  194. req->mss = mss;
  195. ireq->rmt_port = th->source;
  196. ireq->loc_addr = ip_hdr(skb)->daddr;
  197. ireq->rmt_addr = ip_hdr(skb)->saddr;
  198. ireq->opt = NULL;
  199. /* We throwed the options of the initial SYN away, so we hope
  200. * the ACK carries the same options again (see RFC1122 4.2.3.8)
  201. */
  202. if (opt && opt->optlen) {
  203. int opt_size = sizeof(struct ip_options) + opt->optlen;
  204. ireq->opt = kmalloc(opt_size, GFP_ATOMIC);
  205. if (ireq->opt != NULL && ip_options_echo(ireq->opt, skb)) {
  206. kfree(ireq->opt);
  207. ireq->opt = NULL;
  208. }
  209. }
  210. ireq->snd_wscale = ireq->rcv_wscale = ireq->tstamp_ok = 0;
  211. ireq->wscale_ok = ireq->sack_ok = 0;
  212. req->expires = 0UL;
  213. req->retrans = 0;
  214. /*
  215. * We need to lookup the route here to get at the correct
  216. * window size. We should better make sure that the window size
  217. * hasn't changed since we received the original syn, but I see
  218. * no easy way to do this.
  219. */
  220. {
  221. struct flowi fl = { .nl_u = { .ip4_u =
  222. { .daddr = ((opt && opt->srr) ?
  223. opt->faddr :
  224. ireq->rmt_addr),
  225. .saddr = ireq->loc_addr,
  226. .tos = RT_CONN_FLAGS(sk) } },
  227. .proto = IPPROTO_TCP,
  228. .uli_u = { .ports =
  229. { .sport = th->dest,
  230. .dport = th->source } } };
  231. security_req_classify_flow(req, &fl);
  232. if (ip_route_output_key(&rt, &fl)) {
  233. reqsk_free(req);
  234. goto out;
  235. }
  236. }
  237. /* Try to redo what tcp_v4_send_synack did. */
  238. req->window_clamp = dst_metric(&rt->u.dst, RTAX_WINDOW);
  239. tcp_select_initial_window(tcp_full_space(sk), req->mss,
  240. &req->rcv_wnd, &req->window_clamp,
  241. 0, &rcv_wscale);
  242. /* BTW win scale with syncookies is 0 by definition */
  243. ireq->rcv_wscale = rcv_wscale;
  244. ret = get_cookie_sock(sk, skb, req, &rt->u.dst);
  245. out: return ret;
  246. }