|
|
1.1 ! root 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, <[email protected]> ! 11: * Fred N. van Kempen, <[email protected]> ! 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: /* This is for all connections with a full identity, no wildcards. */ ! 25: #define TCP_HTABLE_SIZE 256 ! 26: ! 27: /* This is for listening sockets, thus all sockets which possess wildcards. */ ! 28: #define TCP_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */ ! 29: ! 30: /* This is for all sockets, to keep track of the local port allocations. */ ! 31: #define TCP_BHTABLE_SIZE 64 ! 32: ! 33: /* tcp_ipv4.c: These need to be shared by v4 and v6 because the lookup ! 34: * and hashing code needs to work with different AF's yet ! 35: * the port space is shared. ! 36: */ ! 37: extern struct sock *tcp_established_hash[TCP_HTABLE_SIZE]; ! 38: extern struct sock *tcp_listening_hash[TCP_LHTABLE_SIZE]; ! 39: extern struct sock *tcp_bound_hash[TCP_BHTABLE_SIZE]; ! 40: ! 41: /* These are AF independant. */ ! 42: static __inline__ int tcp_bhashfn(__u16 lport) ! 43: { ! 44: return (lport ^ (lport >> 7)) & (TCP_BHTABLE_SIZE-1); ! 45: } ! 46: ! 47: /* Find the next port that hashes h that is larger than lport. ! 48: * If you change the hash, change this function to match, or you will ! 49: * break TCP port selection. This function must also NOT wrap around ! 50: * when the next number exceeds the largest possible port (2^16-1). ! 51: */ ! 52: static __inline__ int tcp_bhashnext(__u16 lport, __u16 h) ! 53: { ! 54: __u32 s; /* don't change this to a smaller type! */ ! 55: ! 56: s = (lport ^ (h ^ tcp_bhashfn(lport))); ! 57: if (s > lport) ! 58: return s; ! 59: s = lport + TCP_BHTABLE_SIZE; ! 60: return (s ^ (h ^ tcp_bhashfn(s))); ! 61: } ! 62: ! 63: static __inline__ int tcp_sk_bhashfn(struct sock *sk) ! 64: { ! 65: __u16 lport = sk->num; ! 66: return tcp_bhashfn(lport); ! 67: } ! 68: ! 69: /* These can have wildcards, don't try too hard. ! 70: * XXX deal with thousands of IP aliases for listening ports later ! 71: */ ! 72: static __inline__ int tcp_lhashfn(unsigned short num) ! 73: { ! 74: return num & (TCP_LHTABLE_SIZE - 1); ! 75: } ! 76: ! 77: static __inline__ int tcp_sk_listen_hashfn(struct sock *sk) ! 78: { ! 79: return tcp_lhashfn(sk->num); ! 80: } ! 81: ! 82: /* This is IPv4 specific. */ ! 83: static __inline__ int tcp_hashfn(__u32 laddr, __u16 lport, ! 84: __u32 faddr, __u16 fport) ! 85: { ! 86: return ((laddr ^ lport) ^ (faddr ^ fport)) & (TCP_HTABLE_SIZE - 1); ! 87: } ! 88: ! 89: static __inline__ int tcp_sk_hashfn(struct sock *sk) ! 90: { ! 91: __u32 laddr = sk->rcv_saddr; ! 92: __u16 lport = sk->num; ! 93: __u32 faddr = sk->daddr; ! 94: __u16 fport = sk->dummy_th.dest; ! 95: ! 96: return tcp_hashfn(laddr, lport, faddr, fport); ! 97: } ! 98: ! 99: /* Only those holding the sockhash lock call these two things here. ! 100: * Note the slightly gross overloading of sk->prev, AF_UNIX is the ! 101: * only other main benefactor of that member of SK, so who cares. ! 102: */ ! 103: static __inline__ void tcp_sk_bindify(struct sock *sk) ! 104: { ! 105: int hashent = tcp_sk_bhashfn(sk); ! 106: struct sock **htable = &tcp_bound_hash[hashent]; ! 107: ! 108: if((sk->bind_next = *htable) != NULL) ! 109: (*htable)->bind_pprev = &sk->bind_next; ! 110: *htable = sk; ! 111: sk->bind_pprev = htable; ! 112: } ! 113: ! 114: static __inline__ void tcp_sk_unbindify(struct sock *sk) ! 115: { ! 116: if(sk->bind_next) ! 117: sk->bind_next->bind_pprev = sk->bind_pprev; ! 118: *(sk->bind_pprev) = sk->bind_next; ! 119: } ! 120: ! 121: /* ! 122: * 40 is maximal IP options size ! 123: * 4 is TCP option size (MSS) ! 124: */ ! 125: #define MAX_SYN_SIZE (sizeof(struct iphdr) + 40 + sizeof(struct tcphdr) + 4 + MAX_HEADER + 15) ! 126: #define MAX_FIN_SIZE (sizeof(struct iphdr) + 40 + sizeof(struct tcphdr) + MAX_HEADER + 15) ! 127: #define MAX_ACK_SIZE (sizeof(struct iphdr) + 40 + sizeof(struct tcphdr) + MAX_HEADER + 15) ! 128: #define MAX_RESET_SIZE (sizeof(struct iphdr) + 40 + sizeof(struct tcphdr) + MAX_HEADER + 15) ! 129: ! 130: #define MAX_WINDOW 32767 /* Never offer a window over 32767 without using ! 131: window scaling (not yet supported). Some poor ! 132: stacks do signed 16bit maths! */ ! 133: #define MIN_WINDOW 2048 ! 134: #define MAX_ACK_BACKLOG 2 ! 135: #define MAX_DUP_ACKS 3 ! 136: #define MIN_WRITE_SPACE 2048 ! 137: #define TCP_WINDOW_DIFF 2048 ! 138: ! 139: /* urg_data states */ ! 140: #define URG_VALID 0x0100 ! 141: #define URG_NOTYET 0x0200 ! 142: #define URG_READ 0x0400 ! 143: ! 144: #define TCP_RETR1 7 /* ! 145: * This is how many retries it does before it ! 146: * tries to figure out if the gateway is ! 147: * down. ! 148: */ ! 149: ! 150: #define TCP_RETR2 15 /* ! 151: * This should take at least ! 152: * 90 minutes to time out. ! 153: */ ! 154: ! 155: #define TCP_TIMEOUT_LEN (15*60*HZ) /* should be about 15 mins */ ! 156: #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to successfully ! 157: * close the socket, about 60 seconds */ ! 158: #define TCP_FIN_TIMEOUT (3*60*HZ) /* BSD style FIN_WAIT2 deadlock breaker */ ! 159: #define TCP_ACK_TIME (3*HZ) /* time to delay before sending an ACK */ ! 160: #define TCP_DONE_TIME (5*HZ/2)/* maximum time to wait before actually ! 161: * destroying a socket */ ! 162: #define TCP_WRITE_TIME (30*HZ) /* initial time to wait for an ACK, ! 163: * after last transmit */ ! 164: #define TCP_TIMEOUT_INIT (3*HZ) /* RFC 1122 initial timeout value */ ! 165: #define TCP_SYN_RETRIES 5 /* number of times to retry opening a ! 166: * connection (TCP_RETR2-....) */ ! 167: #define TCP_PROBEWAIT_LEN (1*HZ)/* time to wait between probes when ! 168: * I've got something to write and ! 169: * there is no window */ ! 170: ! 171: #define TCP_NO_CHECK 0 /* turn to one if you want the default ! 172: * to be no checksum */ ! 173: ! 174: ! 175: /* ! 176: * TCP option ! 177: */ ! 178: ! 179: #define TCPOPT_NOP 1 /* Padding */ ! 180: #define TCPOPT_EOL 0 /* End of options */ ! 181: #define TCPOPT_MSS 2 /* Segment size negotiating */ ! 182: /* ! 183: * We don't use these yet, but they are for PAWS and big windows ! 184: */ ! 185: #define TCPOPT_WINDOW 3 /* Window scaling */ ! 186: #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */ ! 187: ! 188: ! 189: /* ! 190: * The next routines deal with comparing 32 bit unsigned ints ! 191: * and worry about wraparound (automatic with unsigned arithmetic). ! 192: */ ! 193: ! 194: extern __inline int before(__u32 seq1, __u32 seq2) ! 195: { ! 196: return (__s32)(seq1-seq2) < 0; ! 197: } ! 198: ! 199: extern __inline int after(__u32 seq1, __u32 seq2) ! 200: { ! 201: return (__s32)(seq2-seq1) < 0; ! 202: } ! 203: ! 204: ! 205: /* is s2<=s1<=s3 ? */ ! 206: extern __inline int between(__u32 seq1, __u32 seq2, __u32 seq3) ! 207: { ! 208: return (after(seq1+1, seq2) && before(seq1, seq3+1)); ! 209: } ! 210: ! 211: static __inline__ int min(unsigned int a, unsigned int b) ! 212: { ! 213: if (a > b) ! 214: a = b; ! 215: return a; ! 216: } ! 217: ! 218: static __inline__ int max(unsigned int a, unsigned int b) ! 219: { ! 220: if (a < b) ! 221: a = b; ! 222: return a; ! 223: } ! 224: ! 225: extern struct proto tcp_prot; ! 226: extern struct tcp_mib tcp_statistics; ! 227: ! 228: extern unsigned short tcp_good_socknum(void); ! 229: ! 230: extern void tcp_err(int type, int code, unsigned char *header, __u32 daddr, ! 231: __u32, struct inet_protocol *protocol, int len); ! 232: extern void tcp_shutdown (struct sock *sk, int how); ! 233: extern int tcp_rcv(struct sk_buff *skb, struct device *dev, ! 234: struct options *opt, __u32 daddr, ! 235: unsigned short len, __u32 saddr, int redo, ! 236: struct inet_protocol *protocol); ! 237: ! 238: extern int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg); ! 239: ! 240: extern void tcp_v4_unhash(struct sock *sk); ! 241: ! 242: extern void tcp_read_wakeup(struct sock *); ! 243: extern void tcp_write_xmit(struct sock *); ! 244: extern void tcp_time_wait(struct sock *); ! 245: extern void tcp_retransmit(struct sock *, int); ! 246: extern void tcp_do_retransmit(struct sock *, int); ! 247: extern void tcp_send_check(struct tcphdr *th, unsigned long saddr, ! 248: unsigned long daddr, int len, struct sk_buff *skb); ! 249: ! 250: /* tcp_output.c */ ! 251: ! 252: extern void tcp_send_probe0(struct sock *); ! 253: extern void tcp_send_partial(struct sock *); ! 254: extern void tcp_write_wakeup(struct sock *); ! 255: extern void tcp_send_fin(struct sock *sk); ! 256: extern void tcp_send_synack(struct sock *, struct sock *, struct sk_buff *, int); ! 257: extern void tcp_send_skb(struct sock *, struct sk_buff *); ! 258: extern void tcp_send_ack(struct sock *sk); ! 259: extern void tcp_send_delayed_ack(struct sock *sk, int max_timeout, unsigned long timeout); ! 260: extern void tcp_send_reset(unsigned long saddr, unsigned long daddr, struct tcphdr *th, ! 261: struct proto *prot, struct options *opt, struct device *dev, int tos, int ttl); ! 262: ! 263: extern void tcp_enqueue_partial(struct sk_buff *, struct sock *); ! 264: extern struct sk_buff * tcp_dequeue_partial(struct sock *); ! 265: extern void tcp_shrink_skb(struct sock *,struct sk_buff *,u32); ! 266: ! 267: /* CONFIG_IP_TRANSPARENT_PROXY */ ! 268: extern int tcp_chkaddr(struct sk_buff *); ! 269: ! 270: /* tcp_timer.c */ ! 271: #define tcp_reset_msl_timer(x,y,z) reset_timer(x,y,z) ! 272: extern void tcp_reset_xmit_timer(struct sock *, int, unsigned long); ! 273: extern void tcp_delack_timer(unsigned long); ! 274: extern void tcp_retransmit_timer(unsigned long); ! 275: ! 276: static __inline__ int tcp_old_window(struct sock * sk) ! 277: { ! 278: return sk->window - (sk->acked_seq - sk->lastwin_seq); ! 279: } ! 280: ! 281: extern int tcp_new_window(struct sock *); ! 282: ! 283: /* ! 284: * Return true if we should raise the window when we ! 285: * have cleaned up the receive queue. We don't want to ! 286: * do this normally, only if it makes sense to avoid ! 287: * zero window probes.. ! 288: * ! 289: * We do this only if we can raise the window noticeably. ! 290: */ ! 291: static __inline__ int tcp_raise_window(struct sock * sk) ! 292: { ! 293: int new = tcp_new_window(sk); ! 294: return new && (new >= 2*tcp_old_window(sk)); ! 295: } ! 296: ! 297: static __inline__ unsigned short tcp_select_window(struct sock *sk) ! 298: { ! 299: int window = tcp_new_window(sk); ! 300: int oldwin = tcp_old_window(sk); ! 301: ! 302: /* Don't allow a shrinking window */ ! 303: if (window > oldwin) { ! 304: sk->window = window; ! 305: sk->lastwin_seq = sk->acked_seq; ! 306: oldwin = window; ! 307: } ! 308: return oldwin; ! 309: } ! 310: ! 311: /* ! 312: * List all states of a TCP socket that can be viewed as a "connected" ! 313: * state. This now includes TCP_SYN_RECV, although I am not yet fully ! 314: * convinced that this is the solution for the 'getpeername(2)' ! 315: * problem. Thanks to Stephen A. Wood <[email protected]> -FvK ! 316: */ ! 317: ! 318: extern __inline const int tcp_connected(const int state) ! 319: { ! 320: return(state == TCP_ESTABLISHED || state == TCP_CLOSE_WAIT || ! 321: state == TCP_FIN_WAIT1 || state == TCP_FIN_WAIT2 || ! 322: state == TCP_SYN_RECV); ! 323: } ! 324: ! 325: /* ! 326: * Calculate(/check) TCP checksum ! 327: */ ! 328: static __inline__ u16 tcp_check(struct tcphdr *th, int len, ! 329: unsigned long saddr, unsigned long daddr, unsigned long base) ! 330: { ! 331: return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base); ! 332: } ! 333: ! 334: #undef STATE_TRACE ! 335: ! 336: #ifdef STATE_TRACE ! 337: static char *statename[]={ ! 338: "Unused","Established","Syn Sent","Syn Recv", ! 339: "Fin Wait 1","Fin Wait 2","Time Wait", "Close", ! 340: "Close Wait","Last ACK","Listen","Closing" ! 341: }; ! 342: #endif ! 343: ! 344: static __inline__ void tcp_set_state(struct sock *sk, int state) ! 345: { ! 346: int oldstate = sk->state; ! 347: ! 348: sk->state = state; ! 349: ! 350: #ifdef STATE_TRACE ! 351: if(sk->debug) ! 352: printk("TCP sk=%p, State %s -> %s\n",sk, statename[oldstate],statename[state]); ! 353: #endif ! 354: ! 355: switch (state) { ! 356: case TCP_ESTABLISHED: ! 357: if (oldstate != TCP_ESTABLISHED) { ! 358: tcp_statistics.TcpCurrEstab++; ! 359: } ! 360: break; ! 361: ! 362: case TCP_CLOSE: ! 363: /* Preserve the invariant */ ! 364: tcp_v4_unhash(sk); ! 365: /* Should be about 2 rtt's */ ! 366: reset_timer(sk, TIME_DONE, min(sk->rtt * 2, TCP_DONE_TIME)); ! 367: /* fall through */ ! 368: default: ! 369: if (oldstate==TCP_ESTABLISHED) ! 370: tcp_statistics.TcpCurrEstab--; ! 371: } ! 372: } ! 373: ! 374: #endif /* _TCP_H */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.