syncookies.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #include <linux/tcp.h>
  15. #include <linux/slab.h>
  16. #include <linux/random.h>
  17. #include <linux/cryptohash.h>
  18. #include <linux/kernel.h>
  19. #include <net/tcp.h>
  20. /* Timestamps: lowest 9 bits store TCP options */
  21. #define TSBITS 9
  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, cookie_scratch)[16 + 5 + SHA_WORKSPACE_WORDS];
  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(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 lowest 9 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. if (ireq->wscale_ok) {
  61. options = ireq->snd_wscale;
  62. options |= ireq->rcv_wscale << 4;
  63. }
  64. options |= ireq->sack_ok << 8;
  65. ts = ts_now & ~TSMASK;
  66. ts |= options;
  67. if (ts > ts_now) {
  68. ts >>= TSBITS;
  69. ts--;
  70. ts <<= TSBITS;
  71. ts |= options;
  72. }
  73. return ts;
  74. }
  75. static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
  76. __be16 dport, __u32 sseq, __u32 count,
  77. __u32 data)
  78. {
  79. /*
  80. * Compute the secure sequence number.
  81. * The output should be:
  82. * HASH(sec1,saddr,sport,daddr,dport,sec1) + sseq + (count * 2^24)
  83. * + (HASH(sec2,saddr,sport,daddr,dport,count,sec2) % 2^24).
  84. * Where sseq is their sequence number and count increases every
  85. * minute by 1.
  86. * As an extra hack, we add a small "data" value that encodes the
  87. * MSS into the second hash value.
  88. */
  89. return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
  90. sseq + (count << COOKIEBITS) +
  91. ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
  92. & COOKIEMASK));
  93. }
  94. /*
  95. * This retrieves the small "data" value from the syncookie.
  96. * If the syncookie is bad, the data returned will be out of
  97. * range. This must be checked by the caller.
  98. *
  99. * The count value used to generate the cookie must be within
  100. * "maxdiff" if the current (passed-in) "count". The return value
  101. * is (__u32)-1 if this test fails.
  102. */
  103. static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
  104. __be16 sport, __be16 dport, __u32 sseq,
  105. __u32 count, __u32 maxdiff)
  106. {
  107. __u32 diff;
  108. /* Strip away the layers from the cookie */
  109. cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
  110. /* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
  111. diff = (count - (cookie >> COOKIEBITS)) & ((__u32) - 1 >> COOKIEBITS);
  112. if (diff >= maxdiff)
  113. return (__u32)-1;
  114. return (cookie -
  115. cookie_hash(saddr, daddr, sport, dport, count - diff, 1))
  116. & COOKIEMASK; /* Leaving the data behind */
  117. }
  118. /*
  119. * This table has to be sorted and terminated with (__u16)-1.
  120. * XXX generate a better table.
  121. * Unresolved Issues: HIPPI with a 64k MSS is not well supported.
  122. */
  123. static __u16 const msstab[] = {
  124. 64 - 1,
  125. 256 - 1,
  126. 512 - 1,
  127. 536 - 1,
  128. 1024 - 1,
  129. 1440 - 1,
  130. 1460 - 1,
  131. 4312 - 1,
  132. (__u16)-1
  133. };
  134. /* The number doesn't include the -1 terminator */
  135. #define NUM_MSS (ARRAY_SIZE(msstab) - 1)
  136. /*
  137. * Generate a syncookie. mssp points to the mss, which is returned
  138. * rounded down to the value encoded in the cookie.
  139. */
  140. __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb, __u16 *mssp)
  141. {
  142. struct tcp_sock *tp = tcp_sk(sk);
  143. const struct iphdr *iph = ip_hdr(skb);
  144. const struct tcphdr *th = tcp_hdr(skb);
  145. int mssind;
  146. const __u16 mss = *mssp;
  147. tp->last_synq_overflow = jiffies;
  148. /* XXX sort msstab[] by probability? Binary search? */
  149. for (mssind = 0; mss > msstab[mssind + 1]; mssind++)
  150. ;
  151. *mssp = msstab[mssind] + 1;
  152. NET_INC_STATS_BH(LINUX_MIB_SYNCOOKIESSENT);
  153. return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
  154. th->source, th->dest, ntohl(th->seq),
  155. jiffies / (HZ * 60), mssind);
  156. }
  157. /*
  158. * This (misnamed) value is the age of syncookie which is permitted.
  159. * Its ideal value should be dependent on TCP_TIMEOUT_INIT and
  160. * sysctl_tcp_retries1. It's a rather complicated formula (exponential
  161. * backoff) to compute at runtime so it's currently hardcoded here.
  162. */
  163. #define COUNTER_TRIES 4
  164. /*
  165. * Check if a ack sequence number is a valid syncookie.
  166. * Return the decoded mss if it is, or 0 if not.
  167. */
  168. static inline int cookie_check(struct sk_buff *skb, __u32 cookie)
  169. {
  170. const struct iphdr *iph = ip_hdr(skb);
  171. const struct tcphdr *th = tcp_hdr(skb);
  172. __u32 seq = ntohl(th->seq) - 1;
  173. __u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
  174. th->source, th->dest, seq,
  175. jiffies / (HZ * 60),
  176. COUNTER_TRIES);
  177. return mssind < NUM_MSS ? msstab[mssind] + 1 : 0;
  178. }
  179. static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  180. struct request_sock *req,
  181. struct dst_entry *dst)
  182. {
  183. struct inet_connection_sock *icsk = inet_csk(sk);
  184. struct sock *child;
  185. child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst);
  186. if (child)
  187. inet_csk_reqsk_queue_add(sk, req, child);
  188. else
  189. reqsk_free(req);
  190. return child;
  191. }
  192. /*
  193. * when syncookies are in effect and tcp timestamps are enabled we stored
  194. * additional tcp options in the timestamp.
  195. * This extracts these options from the timestamp echo.
  196. *
  197. * The lowest 4 bits are for snd_wscale
  198. * The next 4 lsb are for rcv_wscale
  199. * The next lsb is for sack_ok
  200. */
  201. void cookie_check_timestamp(struct tcp_options_received *tcp_opt)
  202. {
  203. /* echoed timestamp, 9 lowest bits contain options */
  204. u32 options = tcp_opt->rcv_tsecr & TSMASK;
  205. tcp_opt->snd_wscale = options & 0xf;
  206. options >>= 4;
  207. tcp_opt->rcv_wscale = options & 0xf;
  208. tcp_opt->sack_ok = (options >> 4) & 0x1;
  209. if (tcp_opt->sack_ok)
  210. tcp_sack_reset(tcp_opt);
  211. if (tcp_opt->snd_wscale || tcp_opt->rcv_wscale)
  212. tcp_opt->wscale_ok = 1;
  213. }
  214. EXPORT_SYMBOL(cookie_check_timestamp);
  215. struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
  216. struct ip_options *opt)
  217. {
  218. struct inet_request_sock *ireq;
  219. struct tcp_request_sock *treq;
  220. struct tcp_sock *tp = tcp_sk(sk);
  221. const struct tcphdr *th = tcp_hdr(skb);
  222. __u32 cookie = ntohl(th->ack_seq) - 1;
  223. struct sock *ret = sk;
  224. struct request_sock *req;
  225. int mss;
  226. struct rtable *rt;
  227. __u8 rcv_wscale;
  228. struct tcp_options_received tcp_opt;
  229. if (!sysctl_tcp_syncookies || !th->ack)
  230. goto out;
  231. if (time_after(jiffies, tp->last_synq_overflow + TCP_TIMEOUT_INIT) ||
  232. (mss = cookie_check(skb, cookie)) == 0) {
  233. NET_INC_STATS_BH(LINUX_MIB_SYNCOOKIESFAILED);
  234. goto out;
  235. }
  236. NET_INC_STATS_BH(LINUX_MIB_SYNCOOKIESRECV);
  237. /* check for timestamp cookie support */
  238. memset(&tcp_opt, 0, sizeof(tcp_opt));
  239. tcp_parse_options(skb, &tcp_opt, 0);
  240. if (tcp_opt.saw_tstamp)
  241. cookie_check_timestamp(&tcp_opt);
  242. ret = NULL;
  243. req = reqsk_alloc(&tcp_request_sock_ops); /* for safety */
  244. if (!req)
  245. goto out;
  246. if (security_inet_conn_request(sk, skb, req)) {
  247. reqsk_free(req);
  248. goto out;
  249. }
  250. ireq = inet_rsk(req);
  251. treq = tcp_rsk(req);
  252. treq->rcv_isn = ntohl(th->seq) - 1;
  253. treq->snt_isn = cookie;
  254. req->mss = mss;
  255. ireq->rmt_port = th->source;
  256. ireq->loc_addr = ip_hdr(skb)->daddr;
  257. ireq->rmt_addr = ip_hdr(skb)->saddr;
  258. ireq->opt = NULL;
  259. ireq->snd_wscale = tcp_opt.snd_wscale;
  260. ireq->rcv_wscale = tcp_opt.rcv_wscale;
  261. ireq->sack_ok = tcp_opt.sack_ok;
  262. ireq->wscale_ok = tcp_opt.wscale_ok;
  263. ireq->tstamp_ok = tcp_opt.saw_tstamp;
  264. req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
  265. /* We throwed the options of the initial SYN away, so we hope
  266. * the ACK carries the same options again (see RFC1122 4.2.3.8)
  267. */
  268. if (opt && opt->optlen) {
  269. int opt_size = sizeof(struct ip_options) + opt->optlen;
  270. ireq->opt = kmalloc(opt_size, GFP_ATOMIC);
  271. if (ireq->opt != NULL && ip_options_echo(ireq->opt, skb)) {
  272. kfree(ireq->opt);
  273. ireq->opt = NULL;
  274. }
  275. }
  276. req->expires = 0UL;
  277. req->retrans = 0;
  278. /*
  279. * We need to lookup the route here to get at the correct
  280. * window size. We should better make sure that the window size
  281. * hasn't changed since we received the original syn, but I see
  282. * no easy way to do this.
  283. */
  284. {
  285. struct flowi fl = { .nl_u = { .ip4_u =
  286. { .daddr = ((opt && opt->srr) ?
  287. opt->faddr :
  288. ireq->rmt_addr),
  289. .saddr = ireq->loc_addr,
  290. .tos = RT_CONN_FLAGS(sk) } },
  291. .proto = IPPROTO_TCP,
  292. .uli_u = { .ports =
  293. { .sport = th->dest,
  294. .dport = th->source } } };
  295. security_req_classify_flow(req, &fl);
  296. if (ip_route_output_key(&init_net, &rt, &fl)) {
  297. reqsk_free(req);
  298. goto out;
  299. }
  300. }
  301. /* Try to redo what tcp_v4_send_synack did. */
  302. req->window_clamp = tp->window_clamp ? :dst_metric(&rt->u.dst, RTAX_WINDOW);
  303. tcp_select_initial_window(tcp_full_space(sk), req->mss,
  304. &req->rcv_wnd, &req->window_clamp,
  305. ireq->wscale_ok, &rcv_wscale);
  306. ireq->rcv_wscale = rcv_wscale;
  307. ret = get_cookie_sock(sk, skb, req, &rt->u.dst);
  308. out: return ret;
  309. }