syncookies.c 8.0 KB

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