syncookies.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.17 2001/10/26 14:55:41 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 <net/tcp.h>
  20. extern int sysctl_tcp_syncookies;
  21. /* 
  22.  * This table has to be sorted and terminated with (__u16)-1.
  23.  * XXX generate a better table.
  24.  * Unresolved Issues: HIPPI with a 64k MSS is not well supported.
  25.  */
  26. static __u16 const msstab[] = {
  27. 64 - 1,
  28. 256 - 1,
  29. 512 - 1,
  30. 536 - 1,
  31. 1024 - 1,
  32. 1440 - 1,
  33. 1460 - 1,
  34. 4312 - 1,
  35. (__u16)-1
  36. };
  37. /* The number doesn't include the -1 terminator */
  38. #define NUM_MSS (sizeof(msstab)/sizeof(msstab[0]) - 1)
  39. /*
  40.  * Generate a syncookie.  mssp points to the mss, which is returned
  41.  * rounded down to the value encoded in the cookie.
  42.  */
  43. __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb, __u16 *mssp)
  44. {
  45. int mssind;
  46. const __u16 mss = *mssp;
  47. sk->tp_pinfo.af_tcp.last_synq_overflow = jiffies;
  48. /* XXX sort msstab[] by probability?  Binary search? */
  49. for (mssind = 0; mss > msstab[mssind + 1]; mssind++)
  50. ;
  51. *mssp = msstab[mssind] + 1;
  52. NET_INC_STATS_BH(SyncookiesSent);
  53. return secure_tcp_syn_cookie(skb->nh.iph->saddr, skb->nh.iph->daddr,
  54.      skb->h.th->source, skb->h.th->dest,
  55.      ntohl(skb->h.th->seq),
  56.      jiffies / (HZ * 60), mssind);
  57. }
  58. /* 
  59.  * This (misnamed) value is the age of syncookie which is permitted.
  60.  * Its ideal value should be dependent on TCP_TIMEOUT_INIT and
  61.  * sysctl_tcp_retries1. It's a rather complicated formula (exponential
  62.  * backoff) to compute at runtime so it's currently hardcoded here.
  63.  */
  64. #define COUNTER_TRIES 4
  65. /*  
  66.  * Check if a ack sequence number is a valid syncookie. 
  67.  * Return the decoded mss if it is, or 0 if not.
  68.  */
  69. static inline int cookie_check(struct sk_buff *skb, __u32 cookie)
  70. {
  71. __u32 seq; 
  72. __u32 mssind;
  73. seq = ntohl(skb->h.th->seq)-1; 
  74. mssind = check_tcp_syn_cookie(cookie,
  75.       skb->nh.iph->saddr, skb->nh.iph->daddr,
  76.       skb->h.th->source, skb->h.th->dest,
  77.       seq, jiffies / (HZ * 60), COUNTER_TRIES);
  78. return mssind < NUM_MSS ? msstab[mssind] + 1 : 0;
  79. }
  80. extern struct or_calltable or_ipv4;
  81. static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
  82.    struct open_request *req,
  83.    struct dst_entry *dst)
  84. {
  85. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  86. struct sock *child;
  87. child = tp->af_specific->syn_recv_sock(sk, skb, req, dst);
  88. if (child)
  89. tcp_acceptq_queue(sk, req, child);
  90. else
  91. tcp_openreq_free(req);
  92. return child;
  93. }
  94. struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
  95.      struct ip_options *opt)
  96. {
  97. __u32 cookie = ntohl(skb->h.th->ack_seq) - 1; 
  98. struct sock *ret = sk;
  99. struct open_request *req; 
  100. int mss; 
  101. struct rtable *rt; 
  102. __u8 rcv_wscale;
  103. if (!sysctl_tcp_syncookies || !skb->h.th->ack)
  104. goto out;
  105.    if (time_after(jiffies, sk->tp_pinfo.af_tcp.last_synq_overflow + TCP_TIMEOUT_INIT) ||
  106.     (mss = cookie_check(skb, cookie)) == 0) {
  107.   NET_INC_STATS_BH(SyncookiesFailed);
  108. goto out;
  109. }
  110. NET_INC_STATS_BH(SyncookiesRecv);
  111. req = tcp_openreq_alloc();
  112. ret = NULL;
  113. if (!req)
  114. goto out;
  115. req->rcv_isn = htonl(skb->h.th->seq) - 1;
  116. req->snt_isn = cookie; 
  117. req->mss = mss;
  118.   req->rmt_port = skb->h.th->source;
  119. req->af.v4_req.loc_addr = skb->nh.iph->daddr;
  120. req->af.v4_req.rmt_addr = skb->nh.iph->saddr;
  121. req->class = &or_ipv4; /* for savety */
  122. req->af.v4_req.opt = NULL;
  123. /* We throwed the options of the initial SYN away, so we hope
  124.  * the ACK carries the same options again (see RFC1122 4.2.3.8)
  125.  */
  126. if (opt && opt->optlen) {
  127. int opt_size = sizeof(struct ip_options) + opt->optlen;
  128. req->af.v4_req.opt = kmalloc(opt_size, GFP_ATOMIC);
  129. if (req->af.v4_req.opt) {
  130. if (ip_options_echo(req->af.v4_req.opt, skb)) {
  131. kfree(req->af.v4_req.opt);
  132. req->af.v4_req.opt = NULL;
  133. }
  134. }
  135. }
  136. req->snd_wscale = req->rcv_wscale = req->tstamp_ok = 0;
  137. req->wscale_ok = req->sack_ok = 0; 
  138. req->expires = 0UL; 
  139. req->retrans = 0; 
  140. /*
  141.  * We need to lookup the route here to get at the correct
  142.  * window size. We should better make sure that the window size
  143.  * hasn't changed since we received the original syn, but I see
  144.  * no easy way to do this. 
  145.  */
  146. if (ip_route_output(&rt,
  147.     opt && 
  148.     opt->srr ? opt->faddr : req->af.v4_req.rmt_addr,
  149.     req->af.v4_req.loc_addr,
  150.     RT_CONN_FLAGS(sk),
  151.     0)) { 
  152. tcp_openreq_free(req);
  153. goto out; 
  154. }
  155. /* Try to redo what tcp_v4_send_synack did. */
  156. req->window_clamp = rt->u.dst.window;  
  157. tcp_select_initial_window(tcp_full_space(sk), req->mss,
  158.   &req->rcv_wnd, &req->window_clamp, 
  159.   0, &rcv_wscale);
  160. /* BTW win scale with syncookies is 0 by definition */
  161. req->rcv_wscale   = rcv_wscale; 
  162. ret = get_cookie_sock(sk, skb, req, &rt->u.dst);
  163. out: return ret;
  164. }