root/include/net/tcp.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. before
  2. after
  3. between
  4. min
  5. max
  6. tcp_init_seq
  7. tcp_old_window
  8. tcp_new_window
  9. tcp_raise_window
  10. tcp_select_window
  11. tcp_connected
  12. tcp_check
  13. tcp_set_state

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              Definitions for the TCP module.
   7  *
   8  * Version:     @(#)tcp.h       1.0.5   05/23/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *
  13  *              This program is free software; you can redistribute it and/or
  14  *              modify it under the terms of the GNU General Public License
  15  *              as published by the Free Software Foundation; either version
  16  *              2 of the License, or (at your option) any later version.
  17  */
  18 #ifndef _TCP_H
  19 #define _TCP_H
  20 
  21 #include <linux/tcp.h>
  22 #include <net/checksum.h>
  23 
  24 /*
  25  * 40 is maximal IP options size
  26  * 4  is TCP option size (MSS)
  27  */
  28 #define MAX_SYN_SIZE    (sizeof(struct iphdr) + 40 + sizeof(struct tcphdr) + 4 + MAX_HEADER + 15)
  29 #define MAX_FIN_SIZE    (sizeof(struct iphdr) + 40 + sizeof(struct tcphdr) + MAX_HEADER + 15)
  30 #define MAX_ACK_SIZE    (sizeof(struct iphdr) + 40 + sizeof(struct tcphdr) + MAX_HEADER + 15)
  31 #define MAX_RESET_SIZE  (sizeof(struct iphdr) + 40 + sizeof(struct tcphdr) + MAX_HEADER + 15)
  32 
  33 #define MAX_WINDOW      32767           /* Never offer a window over 32767 without using
  34                                            window scaling (not yet supported). Some poor
  35                                            stacks do signed 16bit maths! */
  36 #define MIN_WINDOW      2048
  37 #define MAX_ACK_BACKLOG 2
  38 #define MAX_DUP_ACKS    2
  39 #define MIN_WRITE_SPACE 2048
  40 #define TCP_WINDOW_DIFF 2048
  41 
  42 /* urg_data states */
  43 #define URG_VALID       0x0100
  44 #define URG_NOTYET      0x0200
  45 #define URG_READ        0x0400
  46 
  47 #define TCP_RETR1       7       /*
  48                                  * This is how many retries it does before it
  49                                  * tries to figure out if the gateway is
  50                                  * down.
  51                                  */
  52 
  53 #define TCP_RETR2       15      /*
  54                                  * This should take at least
  55                                  * 90 minutes to time out.
  56                                  */
  57 
  58 #define TCP_TIMEOUT_LEN (15*60*HZ) /* should be about 15 mins           */
  59 #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to successfully 
  60                                   * close the socket, about 60 seconds  */
  61 #define TCP_FIN_TIMEOUT (3*60*HZ) /* BSD style FIN_WAIT2 deadlock breaker */                              
  62 #define TCP_ACK_TIME    (3*HZ)  /* time to delay before sending an ACK  */
  63 #define TCP_DONE_TIME   (5*HZ/2)/* maximum time to wait before actually
  64                                  * destroying a socket                  */
  65 #define TCP_WRITE_TIME  (30*HZ) /* initial time to wait for an ACK,
  66                                  * after last transmit                  */
  67 #define TCP_TIMEOUT_INIT (3*HZ) /* RFC 1122 initial timeout value       */
  68 #define TCP_SYN_RETRIES  10     /* number of times to retry opening a
  69                                  * connection   (TCP_RETR2-....)        */
  70 #define TCP_PROBEWAIT_LEN (1*HZ)/* time to wait between probes when
  71                                  * I've got something to write and
  72                                  * there is no window                   */
  73 
  74 #define TCP_NO_CHECK    0       /* turn to one if you want the default
  75                                  * to be no checksum                    */
  76 
  77 
  78 /*
  79  *      TCP option
  80  */
  81  
  82 #define TCPOPT_NOP              1       /* Padding */
  83 #define TCPOPT_EOL              0       /* End of options */
  84 #define TCPOPT_MSS              2       /* Segment size negotiating */
  85 /*
  86  *      We don't use these yet, but they are for PAWS and big windows
  87  */
  88 #define TCPOPT_WINDOW           3       /* Window scaling */
  89 #define TCPOPT_TIMESTAMP        8       /* Better RTT estimations/PAWS */
  90 
  91 
  92 /*
  93  * The next routines deal with comparing 32 bit unsigned ints
  94  * and worry about wraparound (automatic with unsigned arithmetic).
  95  */
  96 
  97 extern __inline int before(__u32 seq1, __u32 seq2)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99         return (__s32)(seq1-seq2) < 0;
 100 }
 101 
 102 extern __inline int after(__u32 seq1, __u32 seq2)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104         return (__s32)(seq2-seq1) < 0;
 105 }
 106 
 107 
 108 /* is s2<=s1<=s3 ? */
 109 extern __inline int between(__u32 seq1, __u32 seq2, __u32 seq3)
     /* [previous][next][first][last][top][bottom][index][help] */
 110 {
 111         return (after(seq1+1, seq2) && before(seq1, seq3+1));
 112 }
 113 
 114 static __inline__ int min(unsigned int a, unsigned int b)
     /* [previous][next][first][last][top][bottom][index][help] */
 115 {
 116         if (a > b)
 117                 a = b;
 118         return a;
 119 }
 120 
 121 static __inline__ int max(unsigned int a, unsigned int b)
     /* [previous][next][first][last][top][bottom][index][help] */
 122 {
 123         if (a < b)
 124                 a = b;
 125         return a;
 126 }
 127 
 128 extern struct proto tcp_prot;
 129 extern struct tcp_mib tcp_statistics;
 130 extern struct wait_queue *master_select_wakeup;
 131 
 132 extern void     tcp_err(int type, int code, unsigned char *header, __u32 daddr,
 133                         __u32, struct inet_protocol *protocol);
 134 extern void     tcp_shutdown (struct sock *sk, int how);
 135 extern int      tcp_rcv(struct sk_buff *skb, struct device *dev,
 136                         struct options *opt, __u32 daddr,
 137                         unsigned short len, __u32 saddr, int redo,
 138                         struct inet_protocol *protocol);
 139 
 140 extern int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
 141 
 142 extern void tcp_read_wakeup(struct sock *);
 143 extern void tcp_write_xmit(struct sock *);
 144 extern void tcp_time_wait(struct sock *);
 145 extern void tcp_retransmit(struct sock *, int);
 146 extern void tcp_do_retransmit(struct sock *, int);
 147 extern void tcp_send_check(struct tcphdr *th, unsigned long saddr, 
 148                 unsigned long daddr, int len, struct sk_buff *skb);
 149 
 150 /* tcp_output.c */
 151 
 152 extern void tcp_send_probe0(struct sock *);
 153 extern void tcp_send_partial(struct sock *);
 154 extern void tcp_write_wakeup(struct sock *);
 155 extern void tcp_send_fin(struct sock *sk);
 156 extern void tcp_send_synack(struct sock *, struct sock *, struct sk_buff *);
 157 extern void tcp_send_skb(struct sock *, struct sk_buff *);
 158 extern void tcp_send_ack(struct sock *sk);
 159 extern void tcp_send_delayed_ack(struct sock *sk, int timeout);
 160 extern void tcp_send_reset(unsigned long saddr, unsigned long daddr, struct tcphdr *th,
 161           struct proto *prot, struct options *opt, struct device *dev, int tos, int ttl);
 162 
 163 extern void tcp_enqueue_partial(struct sk_buff *, struct sock *);
 164 extern struct sk_buff * tcp_dequeue_partial(struct sock *);
 165 
 166 /* tcp_input.c */
 167 extern void tcp_cache_zap(void);
 168 
 169 /* tcp_timer.c */
 170 #define     tcp_reset_msl_timer(x,y,z)  reset_timer(x,y,z)
 171 extern void tcp_reset_xmit_timer(struct sock *, int, unsigned long);
 172 extern void tcp_delack_timer(unsigned long);
 173 extern void tcp_retransmit_timer(unsigned long);
 174 
 175 /*
 176  *      Default sequence number picking algorithm.
 177  *      As close as possible to RFC 793, which
 178  *      suggests using a 250kHz clock.
 179  *      Further reading shows this assumes 2MB/s networks.
 180  *      For 10MB/s ethernet, a 1MHz clock is appropriate.
 181  *      That's funny, Linux has one built in!  Use it!
 182  */
 183 
 184 static inline u32 tcp_init_seq(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 185 {
 186         struct timeval tv;
 187         do_gettimeofday(&tv);
 188         return tv.tv_usec+tv.tv_sec*1000000;
 189 }
 190 
 191 static __inline__ int tcp_old_window(struct sock * sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 192 {
 193         return sk->window - (sk->acked_seq - sk->lastwin_seq);
 194 }
 195 
 196 static __inline__ int tcp_new_window(struct sock * sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 197 {
 198         int window = sock_rspace(sk);
 199 
 200         if (window > 1024)
 201                 window &= ~0x3FF;       /* make free space a multiple of 1024 */
 202 
 203         if (sk->window_clamp && sk->window_clamp < window)
 204                 window = sk->window_clamp;
 205 
 206         /*
 207          * RFC 1122 says:
 208          *
 209          * "the suggested [SWS] avoidance algorithm for the receiver is to keep
 210          *  RECV.NEXT + RCV.WIN fixed until:
 211          *  RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
 212          *
 213          * Experiments against BSD and Solaris machines show that following
 214          * these rules results in the BSD and Solaris machines making very
 215          * bad guesses about how much data they can have in flight.
 216          *
 217          * Instead we follow the BSD lead and offer a window that gives
 218          * the size of the current free space, truncated to a multiple
 219          * of 1024 bytes. If the window is smaller than
 220          *      min(sk->mss, MAX_WINDOW/2)
 221          * then we advertise the window as having size 0, unless this
 222          * would shrink the window we offered last time.
 223          * This results in as much as double the throughput as the original
 224          * implementation.
 225          */
 226 
 227         if (sk->mss == 0)
 228                 sk->mss = sk->mtu;
 229 
 230         /* BSD style SWS avoidance
 231          * Note that RFC1122 only says we must do silly window avoidance,
 232          * it does not require that we use the suggested algorithm.
 233          */
 234 
 235         if (window < min(sk->mss, MAX_WINDOW/2))
 236                 window = 0;
 237 
 238         return window;
 239 }
 240 
 241 /*
 242  * Return true if we should raise the window when we
 243  * have cleaned up the receive queue. We don't want to
 244  * do this normally, only if it makes sense to avoid
 245  * zero window probes..
 246  *
 247  * We do this only if we can raise the window noticeably.
 248  */
 249 static __inline__ int tcp_raise_window(struct sock * sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 250 {
 251         return tcp_new_window(sk) >= 2*tcp_old_window(sk);
 252 }
 253 
 254 static __inline__ unsigned short tcp_select_window(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 255 {
 256         int window = tcp_new_window(sk);
 257 
 258         /* Don't allow a shrinking window */
 259         if (window > tcp_old_window(sk)) {
 260                 sk->window = window;
 261                 sk->lastwin_seq = sk->acked_seq;
 262         }
 263         return sk->window;
 264 }
 265 
 266 /*
 267  * List all states of a TCP socket that can be viewed as a "connected"
 268  * state.  This now includes TCP_SYN_RECV, although I am not yet fully
 269  * convinced that this is the solution for the 'getpeername(2)'
 270  * problem. Thanks to Stephen A. Wood <saw@cebaf.gov>  -FvK
 271  */
 272 
 273 extern __inline const int tcp_connected(const int state)
     /* [previous][next][first][last][top][bottom][index][help] */
 274 {
 275   return(state == TCP_ESTABLISHED || state == TCP_CLOSE_WAIT ||
 276          state == TCP_FIN_WAIT1   || state == TCP_FIN_WAIT2 ||
 277          state == TCP_SYN_RECV);
 278 }
 279 
 280 /*
 281  * Calculate(/check) TCP checksum
 282  */
 283 static __inline__ u16 tcp_check(struct tcphdr *th, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 284         unsigned long saddr, unsigned long daddr, unsigned long base)
 285 {
 286         return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
 287 }
 288 
 289 #undef STATE_TRACE
 290 
 291 #ifdef STATE_TRACE
 292 static char *statename[]={
 293         "Unused","Established","Syn Sent","Syn Recv",
 294         "Fin Wait 1","Fin Wait 2","Time Wait", "Close",
 295         "Close Wait","Last ACK","Listen","Closing"
 296 };
 297 #endif
 298 
 299 static __inline__ void tcp_set_state(struct sock *sk, int state)
     /* [previous][next][first][last][top][bottom][index][help] */
 300 {
 301         int oldstate = sk->state;
 302 
 303         sk->state = state;
 304 
 305 #ifdef STATE_TRACE
 306         if(sk->debug)
 307                 printk("TCP sk=%p, State %s -> %s\n",sk, statename[oldstate],statename[state]);
 308 #endif  
 309 
 310         switch (state) {
 311         case TCP_ESTABLISHED:
 312                 if (oldstate != TCP_ESTABLISHED) {
 313                         tcp_statistics.TcpCurrEstab++;
 314                         /* This is a hack but it doesn't occur often and it's going to
 315                            be a real        to fix nicely */
 316                         if (oldstate == TCP_SYN_RECV)
 317                                 wake_up_interruptible(&master_select_wakeup);
 318                 }
 319                 break;
 320 
 321         case TCP_CLOSE:
 322                 tcp_cache_zap();
 323                 /* Should be about 2 rtt's */
 324                 reset_timer(sk, TIME_DONE, min(sk->rtt * 2, TCP_DONE_TIME));
 325                 /* fall through */
 326         default:
 327                 if (oldstate==TCP_ESTABLISHED)
 328                         tcp_statistics.TcpCurrEstab--;
 329         }
 330 }
 331 
 332 #endif  /* _TCP_H */

/* [previous][next][first][last][top][bottom][index][help] */