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_raise_window
  8. tcp_select_window
  9. tcp_connected
  10. tcp_check
  11. 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 #define MAX_SYN_SIZE    44 + MAX_HEADER + 15
  25 #define MAX_FIN_SIZE    40 + MAX_HEADER + 15
  26 #define MAX_ACK_SIZE    40 + MAX_HEADER + 15
  27 #define MAX_RESET_SIZE  40 + MAX_HEADER + 15
  28 #define MAX_WINDOW      32767           /* Never offer a window over 32767 without using
  29                                            window scaling (not yet supported). Some poor
  30                                            stacks do signed 16bit maths! */
  31 #define MIN_WINDOW      2048
  32 #define MAX_ACK_BACKLOG 2
  33 #define MIN_WRITE_SPACE 2048
  34 #define TCP_WINDOW_DIFF 2048
  35 
  36 /* urg_data states */
  37 #define URG_VALID       0x0100
  38 #define URG_NOTYET      0x0200
  39 #define URG_READ        0x0400
  40 
  41 #define TCP_RETR1       7       /*
  42                                  * This is how many retries it does before it
  43                                  * tries to figure out if the gateway is
  44                                  * down.
  45                                  */
  46 
  47 #define TCP_RETR2       15      /*
  48                                  * This should take at least
  49                                  * 90 minutes to time out.
  50                                  */
  51 
  52 #define TCP_TIMEOUT_LEN (15*60*HZ) /* should be about 15 mins           */
  53 #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to successfully 
  54                                   * close the socket, about 60 seconds  */
  55 #define TCP_FIN_TIMEOUT (3*60*HZ) /* BSD style FIN_WAIT2 deadlock breaker */                              
  56 #define TCP_ACK_TIME    (3*HZ)  /* time to delay before sending an ACK  */
  57 #define TCP_DONE_TIME   (5*HZ/2)/* maximum time to wait before actually
  58                                  * destroying a socket                  */
  59 #define TCP_WRITE_TIME  (30*HZ) /* initial time to wait for an ACK,
  60                                  * after last transmit                  */
  61 #define TCP_TIMEOUT_INIT (3*HZ) /* RFC 1122 initial timeout value       */
  62 #define TCP_SYN_RETRIES  10     /* number of times to retry opening a
  63                                  * connection   (TCP_RETR2-....)        */
  64 #define TCP_PROBEWAIT_LEN (1*HZ)/* time to wait between probes when
  65                                  * I've got something to write and
  66                                  * there is no window                   */
  67 
  68 #define TCP_NO_CHECK    0       /* turn to one if you want the default
  69                                  * to be no checksum                    */
  70 
  71 
  72 /*
  73  *      TCP option
  74  */
  75  
  76 #define TCPOPT_NOP              1       /* Padding */
  77 #define TCPOPT_EOL              0       /* End of options */
  78 #define TCPOPT_MSS              2       /* Segment size negotiating */
  79 /*
  80  *      We don't use these yet, but they are for PAWS and big windows
  81  */
  82 #define TCPOPT_WINDOW           3       /* Window scaling */
  83 #define TCPOPT_TIMESTAMP        8       /* Better RTT estimations/PAWS */
  84 
  85 
  86 /*
  87  * The next routines deal with comparing 32 bit unsigned ints
  88  * and worry about wraparound (automatic with unsigned arithmetic).
  89  */
  90 
  91 extern __inline int before(__u32 seq1, __u32 seq2)
     /* [previous][next][first][last][top][bottom][index][help] */
  92 {
  93         return (__s32)(seq1-seq2) < 0;
  94 }
  95 
  96 extern __inline int after(__u32 seq1, __u32 seq2)
     /* [previous][next][first][last][top][bottom][index][help] */
  97 {
  98         return (__s32)(seq2-seq1) < 0;
  99 }
 100 
 101 
 102 /* is s2<=s1<=s3 ? */
 103 extern __inline int between(__u32 seq1, __u32 seq2, __u32 seq3)
     /* [previous][next][first][last][top][bottom][index][help] */
 104 {
 105         return (after(seq1+1, seq2) && before(seq1, seq3+1));
 106 }
 107 
 108 static __inline__ int min(unsigned int a, unsigned int b)
     /* [previous][next][first][last][top][bottom][index][help] */
 109 {
 110         if (a > b)
 111                 a = b;
 112         return a;
 113 }
 114 
 115 static __inline__ int max(unsigned int a, unsigned int b)
     /* [previous][next][first][last][top][bottom][index][help] */
 116 {
 117         if (a < b)
 118                 a = b;
 119         return a;
 120 }
 121 
 122 extern struct proto tcp_prot;
 123 extern struct tcp_mib tcp_statistics;
 124 extern struct wait_queue *master_select_wakeup;
 125 
 126 extern void     tcp_err(int type, int code, unsigned char *header, __u32 daddr,
 127                         __u32, struct inet_protocol *protocol);
 128 extern void     tcp_shutdown (struct sock *sk, int how);
 129 extern int      tcp_rcv(struct sk_buff *skb, struct device *dev,
 130                         struct options *opt, __u32 daddr,
 131                         unsigned short len, __u32 saddr, int redo,
 132                         struct inet_protocol *protocol);
 133 
 134 extern int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
 135 
 136 extern void tcp_read_wakeup(struct sock *);
 137 extern void tcp_write_xmit(struct sock *);
 138 extern void tcp_time_wait(struct sock *);
 139 extern void tcp_retransmit(struct sock *, int);
 140 extern void tcp_do_retransmit(struct sock *, int);
 141 extern void tcp_send_check(struct tcphdr *th, unsigned long saddr, 
 142                 unsigned long daddr, int len, struct sk_buff *skb);
 143 
 144 /* tcp_output.c */
 145 
 146 extern void tcp_send_probe0(struct sock *);
 147 extern void tcp_send_partial(struct sock *);
 148 extern void tcp_write_wakeup(struct sock *);
 149 extern void tcp_send_fin(struct sock *sk);
 150 extern void tcp_send_synack(struct sock *, struct sock *, struct sk_buff *);
 151 extern void tcp_send_skb(struct sock *, struct sk_buff *);
 152 extern void tcp_send_ack(u32, u32, struct sock *sk, struct tcphdr *th, u32);
 153 extern void tcp_send_reset(unsigned long saddr, unsigned long daddr, struct tcphdr *th,
 154           struct proto *prot, struct options *opt, struct device *dev, int tos, int ttl);
 155 
 156 extern void tcp_enqueue_partial(struct sk_buff *, struct sock *);
 157 extern struct sk_buff * tcp_dequeue_partial(struct sock *);
 158 
 159 /* tcp_input.c */
 160 extern void tcp_cache_zap(void);
 161 
 162 /* tcp_timer.c */
 163 #define     tcp_reset_msl_timer(x,y,z)  reset_timer(x,y,z)
 164 extern void tcp_reset_xmit_timer(struct sock *, int, unsigned long);
 165 extern void tcp_retransmit_timer(unsigned long);
 166 
 167 /*
 168  *      Default sequence number picking algorithm.
 169  *      As close as possible to RFC 793, which
 170  *      suggests using a 250kHz clock.
 171  *      Further reading shows this assumes 2MB/s networks.
 172  *      For 10MB/s ethernet, a 1MHz clock is appropriate.
 173  *      That's funny, Linux has one built in!  Use it!
 174  */
 175 
 176 static inline u32 tcp_init_seq(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 177 {
 178         struct timeval tv;
 179         do_gettimeofday(&tv);
 180         return tv.tv_usec+tv.tv_sec*1000000;
 181 }
 182 
 183 /*
 184  *      This function returns the amount that we can raise the
 185  *      usable window based on the following constraints
 186  *  
 187  *      1. The window can never be shrunk once it is offered (RFC 793)
 188  *      2. We limit memory per socket
 189  *      3. No reason to raise the window if the other side has
 190  *         lots of room to play with.
 191  */
 192 
 193 static __inline__ unsigned short tcp_raise_window(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 194 {
 195         long free_space;
 196         long window;
 197 
 198 
 199         /* 
 200          * compute the actual window i.e. 
 201          * old_window - received_bytes_on_that_win.
 202          *
 203          * Don't raise the window if we have lots left:
 204          * that only results in unnecessary packets.
 205          */
 206         window = sk->window - (sk->acked_seq - sk->lastwin_seq);        
 207         if (window >= MAX_WINDOW/2)
 208                 return 0;
 209 
 210         free_space = sock_rspace(sk);
 211         if (free_space > 1024)
 212                 free_space &= ~0x3FF; /* make free space a multiple of 1024 */
 213 
 214         if(sk->window_clamp)
 215                 free_space = min(sk->window_clamp, free_space);
 216 
 217         if (sk->mss == 0)
 218                 sk->mss = sk->mtu;
 219  
 220         if ( window < 0 ) {     
 221                 window = 0;
 222                 printk(KERN_DEBUG "TRW: win < 0 w=%d 1=%u 2=%u\n", 
 223                        sk->window, sk->acked_seq, sk->lastwin_seq);
 224         }
 225         
 226         if ( (free_space - window) >= min(sk->mss, MAX_WINDOW/2) )
 227                 return ((free_space - window) / sk->mss) * sk->mss;
 228 
 229         return 0;
 230 }
 231 
 232 static __inline__ unsigned short tcp_select_window(struct sock *sk)
     /* [previous][next][first][last][top][bottom][index][help] */
 233 {
 234         long free_space = sock_rspace(sk);
 235         long window;
 236 
 237         if (free_space > 1024)
 238                 free_space &= ~0x3FF;   /* make free space a multiple of 1024 */
 239 
 240         if (sk->window_clamp)
 241                 free_space = min(sk->window_clamp, free_space);
 242         
 243         /*
 244          * compute the actual window i.e.
 245          * old_window - received_bytes_on_that_win
 246          */
 247 
 248         if (sk->mss == 0)
 249                 sk->mss = sk->mtu;
 250 
 251         window = sk->window - (sk->acked_seq - sk->lastwin_seq);
 252 
 253         if ( window < 0 ) {
 254                 window = 0;
 255                 printk(KERN_DEBUG "TSW: win < 0 w=%d 1=%u 2=%u\n",
 256                         sk->window, sk->acked_seq, sk->lastwin_seq);
 257         }
 258 
 259         /*
 260          * RFC 1122:
 261          * "the suggested [SWS] avoidance algoritm for the receiver is to keep
 262          *  RECV.NEXT + RCV.WIN fixed until:
 263          *  RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
 264          *
 265          * i.e. don't raise the right edge of the window until you can't raise
 266          * it MSS bytes
 267          */
 268 
 269         if ( (free_space - window) >= min(sk->mss, MAX_WINDOW/2) )
 270                 window += ((free_space - window) / sk->mss) * sk->mss;
 271 
 272         sk->window = window;
 273         sk->lastwin_seq = sk->acked_seq;
 274 
 275         return sk->window;
 276 }
 277 
 278 /*
 279  * List all states of a TCP socket that can be viewed as a "connected"
 280  * state.  This now includes TCP_SYN_RECV, although I am not yet fully
 281  * convinced that this is the solution for the 'getpeername(2)'
 282  * problem. Thanks to Stephen A. Wood <saw@cebaf.gov>  -FvK
 283  */
 284 
 285 extern __inline const int tcp_connected(const int state)
     /* [previous][next][first][last][top][bottom][index][help] */
 286 {
 287   return(state == TCP_ESTABLISHED || state == TCP_CLOSE_WAIT ||
 288          state == TCP_FIN_WAIT1   || state == TCP_FIN_WAIT2 ||
 289          state == TCP_SYN_RECV);
 290 }
 291 
 292 /*
 293  * Calculate(/check) TCP checksum
 294  */
 295 static __inline__ u16 tcp_check(struct tcphdr *th, int len,
     /* [previous][next][first][last][top][bottom][index][help] */
 296         unsigned long saddr, unsigned long daddr, unsigned long base)
 297 {
 298         return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
 299 }
 300 
 301 #undef STATE_TRACE
 302 
 303 #ifdef STATE_TRACE
 304 static char *statename[]={
 305         "Unused","Established","Syn Sent","Syn Recv",
 306         "Fin Wait 1","Fin Wait 2","Time Wait", "Close",
 307         "Close Wait","Last ACK","Listen","Closing"
 308 };
 309 #endif
 310 
 311 static __inline__ void tcp_set_state(struct sock *sk, int state)
     /* [previous][next][first][last][top][bottom][index][help] */
 312 {
 313         int oldstate = sk->state;
 314 
 315         sk->state = state;
 316 
 317 #ifdef STATE_TRACE
 318         if(sk->debug)
 319                 printk("TCP sk=%p, State %s -> %s\n",sk, statename[oldstate],statename[state]);
 320 #endif  
 321 
 322         switch (state) {
 323         case TCP_ESTABLISHED:
 324                 if (oldstate != TCP_ESTABLISHED) {
 325                         tcp_statistics.TcpCurrEstab++;
 326                         /* This is a hack but it doesn't occur often and it's going to
 327                            be a real        to fix nicely */
 328                         if (oldstate == TCP_SYN_RECV)
 329                                 wake_up_interruptible(&master_select_wakeup);
 330                 }
 331                 break;
 332 
 333         case TCP_CLOSE:
 334                 tcp_cache_zap();
 335                 /* Should be about 2 rtt's */
 336                 reset_timer(sk, TIME_DONE, min(sk->rtt * 2, TCP_DONE_TIME));
 337                 /* fall through */
 338         default:
 339                 if (oldstate==TCP_ESTABLISHED)
 340                         tcp_statistics.TcpCurrEstab--;
 341         }
 342 }
 343 
 344 #endif  /* _TCP_H */

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