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.
6 * Definitions for the TCP module.
8 * Version: @(#)tcp.h 1.0.5 05/23/93
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
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.
22 #define FASTRETRANS_DEBUG 1
24 /* Cancel timers, when they are not required. */
25 #undef TCP_CLEAR_TIMERS
27 #include <linux/config.h>
28 #include <linux/list.h>
29 #include <linux/tcp.h>
30 #include <linux/slab.h>
31 #include <linux/cache.h>
32 #include <linux/percpu.h>
33 #include <net/inet_hashtables.h>
34 #include <net/checksum.h>
35 #include <net/request_sock.h>
39 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
40 #include <linux/ipv6.h>
42 #include <linux/seq_file.h>
44 extern struct inet_hashinfo tcp_hashinfo
;
45 #define tcp_ehash (tcp_hashinfo.ehash)
46 #define tcp_bhash (tcp_hashinfo.bhash)
47 #define tcp_ehash_size (tcp_hashinfo.ehash_size)
48 #define tcp_bhash_size (tcp_hashinfo.bhash_size)
49 #define tcp_listening_hash (tcp_hashinfo.listening_hash)
50 #define tcp_lhash_lock (tcp_hashinfo.lhash_lock)
51 #define tcp_lhash_users (tcp_hashinfo.lhash_users)
52 #define tcp_lhash_wait (tcp_hashinfo.lhash_wait)
53 #define tcp_portalloc_lock (tcp_hashinfo.portalloc_lock)
55 extern kmem_cache_t
*tcp_bucket_cachep
;
57 extern int tcp_port_rover
;
59 extern void tcp_bind_hash(struct sock
*sk
, struct inet_bind_bucket
*tb
,
62 #if (BITS_PER_LONG == 64)
63 #define TCP_ADDRCMP_ALIGN_BYTES 8
65 #define TCP_ADDRCMP_ALIGN_BYTES 4
68 /* This is a TIME_WAIT bucket. It works around the memory consumption
69 * problems of sockets in such a state on heavily loaded servers, but
70 * without violating the protocol specification.
72 struct tcp_tw_bucket
{
74 * Now struct sock also uses sock_common, so please just
75 * don't add nothing before this first member (__tw_common) --acme
77 struct sock_common __tw_common
;
78 #define tw_family __tw_common.skc_family
79 #define tw_state __tw_common.skc_state
80 #define tw_reuse __tw_common.skc_reuse
81 #define tw_bound_dev_if __tw_common.skc_bound_dev_if
82 #define tw_node __tw_common.skc_node
83 #define tw_bind_node __tw_common.skc_bind_node
84 #define tw_refcnt __tw_common.skc_refcnt
85 volatile unsigned char tw_substate
;
86 unsigned char tw_rcv_wscale
;
88 /* Socket demultiplex comparisons on incoming packets. */
89 /* these five are in inet_sock */
91 __attribute__((aligned(TCP_ADDRCMP_ALIGN_BYTES
)));
95 /* And these are ours. */
102 long tw_ts_recent_stamp
;
103 unsigned long tw_ttd
;
104 struct inet_bind_bucket
*tw_tb
;
105 struct hlist_node tw_death_node
;
106 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
107 struct in6_addr tw_v6_daddr
;
108 struct in6_addr tw_v6_rcv_saddr
;
113 static __inline__
void tw_add_node(struct tcp_tw_bucket
*tw
,
114 struct hlist_head
*list
)
116 hlist_add_head(&tw
->tw_node
, list
);
119 static __inline__
void tw_add_bind_node(struct tcp_tw_bucket
*tw
,
120 struct hlist_head
*list
)
122 hlist_add_head(&tw
->tw_bind_node
, list
);
125 static inline int tw_dead_hashed(struct tcp_tw_bucket
*tw
)
127 return tw
->tw_death_node
.pprev
!= NULL
;
130 static __inline__
void tw_dead_node_init(struct tcp_tw_bucket
*tw
)
132 tw
->tw_death_node
.pprev
= NULL
;
135 static __inline__
void __tw_del_dead_node(struct tcp_tw_bucket
*tw
)
137 __hlist_del(&tw
->tw_death_node
);
138 tw_dead_node_init(tw
);
141 static __inline__
int tw_del_dead_node(struct tcp_tw_bucket
*tw
)
143 if (tw_dead_hashed(tw
)) {
144 __tw_del_dead_node(tw
);
150 #define tw_for_each(tw, node, head) \
151 hlist_for_each_entry(tw, node, head, tw_node)
153 #define tw_for_each_inmate(tw, node, jail) \
154 hlist_for_each_entry(tw, node, jail, tw_death_node)
156 #define tw_for_each_inmate_safe(tw, node, safe, jail) \
157 hlist_for_each_entry_safe(tw, node, safe, jail, tw_death_node)
159 #define tcptw_sk(__sk) ((struct tcp_tw_bucket *)(__sk))
161 static inline u32
tcp_v4_rcv_saddr(const struct sock
*sk
)
163 return likely(sk
->sk_state
!= TCP_TIME_WAIT
) ?
164 inet_sk(sk
)->rcv_saddr
: tcptw_sk(sk
)->tw_rcv_saddr
;
167 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
168 static inline struct in6_addr
*__tcp_v6_rcv_saddr(const struct sock
*sk
)
170 return likely(sk
->sk_state
!= TCP_TIME_WAIT
) ?
171 &inet6_sk(sk
)->rcv_saddr
: &tcptw_sk(sk
)->tw_v6_rcv_saddr
;
174 static inline struct in6_addr
*tcp_v6_rcv_saddr(const struct sock
*sk
)
176 return sk
->sk_family
== AF_INET6
? __tcp_v6_rcv_saddr(sk
) : NULL
;
179 #define tcptw_sk_ipv6only(__sk) (tcptw_sk(__sk)->tw_v6_ipv6only)
181 static inline int tcp_v6_ipv6only(const struct sock
*sk
)
183 return likely(sk
->sk_state
!= TCP_TIME_WAIT
) ?
184 ipv6_only_sock(sk
) : tcptw_sk_ipv6only(sk
);
187 # define __tcp_v6_rcv_saddr(__sk) NULL
188 # define tcp_v6_rcv_saddr(__sk) NULL
189 # define tcptw_sk_ipv6only(__sk) 0
190 # define tcp_v6_ipv6only(__sk) 0
193 extern kmem_cache_t
*tcp_timewait_cachep
;
195 static inline void tcp_tw_put(struct tcp_tw_bucket
*tw
)
197 if (atomic_dec_and_test(&tw
->tw_refcnt
)) {
198 #ifdef SOCK_REFCNT_DEBUG
199 printk(KERN_DEBUG
"tw_bucket %p released\n", tw
);
201 kmem_cache_free(tcp_timewait_cachep
, tw
);
205 extern atomic_t tcp_orphan_count
;
206 extern int tcp_tw_count
;
207 extern void tcp_time_wait(struct sock
*sk
, int state
, int timeo
);
208 extern void tcp_tw_deschedule(struct tcp_tw_bucket
*tw
);
211 /* Socket demux engine toys. */
213 #define TCP_COMBINED_PORTS(__sport, __dport) \
214 (((__u32)(__sport)<<16) | (__u32)(__dport))
215 #else /* __LITTLE_ENDIAN */
216 #define TCP_COMBINED_PORTS(__sport, __dport) \
217 (((__u32)(__dport)<<16) | (__u32)(__sport))
220 #if (BITS_PER_LONG == 64)
222 #define TCP_V4_ADDR_COOKIE(__name, __saddr, __daddr) \
223 __u64 __name = (((__u64)(__saddr))<<32)|((__u64)(__daddr));
224 #else /* __LITTLE_ENDIAN */
225 #define TCP_V4_ADDR_COOKIE(__name, __saddr, __daddr) \
226 __u64 __name = (((__u64)(__daddr))<<32)|((__u64)(__saddr));
227 #endif /* __BIG_ENDIAN */
228 #define TCP_IPV4_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif)\
229 (((*((__u64 *)&(inet_sk(__sk)->daddr)))== (__cookie)) && \
230 ((*((__u32 *)&(inet_sk(__sk)->dport)))== (__ports)) && \
231 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
232 #define TCP_IPV4_TW_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif)\
233 (((*((__u64 *)&(tcptw_sk(__sk)->tw_daddr))) == (__cookie)) && \
234 ((*((__u32 *)&(tcptw_sk(__sk)->tw_dport))) == (__ports)) && \
235 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
236 #else /* 32-bit arch */
237 #define TCP_V4_ADDR_COOKIE(__name, __saddr, __daddr)
238 #define TCP_IPV4_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif)\
239 ((inet_sk(__sk)->daddr == (__saddr)) && \
240 (inet_sk(__sk)->rcv_saddr == (__daddr)) && \
241 ((*((__u32 *)&(inet_sk(__sk)->dport)))== (__ports)) && \
242 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
243 #define TCP_IPV4_TW_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif)\
244 ((tcptw_sk(__sk)->tw_daddr == (__saddr)) && \
245 (tcptw_sk(__sk)->tw_rcv_saddr == (__daddr)) && \
246 ((*((__u32 *)&(tcptw_sk(__sk)->tw_dport))) == (__ports)) && \
247 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
248 #endif /* 64-bit arch */
250 #define TCP_IPV6_MATCH(__sk, __saddr, __daddr, __ports, __dif) \
251 (((*((__u32 *)&(inet_sk(__sk)->dport)))== (__ports)) && \
252 ((__sk)->sk_family == AF_INET6) && \
253 ipv6_addr_equal(&inet6_sk(__sk)->daddr, (__saddr)) && \
254 ipv6_addr_equal(&inet6_sk(__sk)->rcv_saddr, (__daddr)) && \
255 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
257 #define MAX_TCP_HEADER (128 + MAX_HEADER)
260 * Never offer a window over 32767 without using window scaling. Some
261 * poor stacks do signed 16bit maths!
263 #define MAX_TCP_WINDOW 32767U
265 /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
266 #define TCP_MIN_MSS 88U
268 /* Minimal RCV_MSS. */
269 #define TCP_MIN_RCVMSS 536U
271 /* After receiving this amount of duplicate ACKs fast retransmit starts. */
272 #define TCP_FASTRETRANS_THRESH 3
274 /* Maximal reordering. */
275 #define TCP_MAX_REORDERING 127
277 /* Maximal number of ACKs sent quickly to accelerate slow-start. */
278 #define TCP_MAX_QUICKACKS 16U
280 /* urg_data states */
281 #define TCP_URG_VALID 0x0100
282 #define TCP_URG_NOTYET 0x0200
283 #define TCP_URG_READ 0x0400
285 #define TCP_RETR1 3 /*
286 * This is how many retries it does before it
287 * tries to figure out if the gateway is
288 * down. Minimal RFC value is 3; it corresponds
289 * to ~3sec-8min depending on RTO.
292 #define TCP_RETR2 15 /*
293 * This should take at least
294 * 90 minutes to time out.
295 * RFC1122 says that the limit is 100 sec.
296 * 15 is ~13-30min depending on RTO.
299 #define TCP_SYN_RETRIES 5 /* number of times to retry active opening a
300 * connection: ~180sec is RFC minumum */
302 #define TCP_SYNACK_RETRIES 5 /* number of times to retry passive opening a
303 * connection: ~180sec is RFC minumum */
306 #define TCP_ORPHAN_RETRIES 7 /* number of times to retry on an orphaned
307 * socket. 7 is ~50sec-16min.
311 #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
312 * state, about 60 seconds */
313 #define TCP_FIN_TIMEOUT TCP_TIMEWAIT_LEN
314 /* BSD style FIN_WAIT2 deadlock breaker.
315 * It used to be 3min, new value is 60sec,
316 * to combine FIN-WAIT-2 timeout with
320 #define TCP_DELACK_MAX ((unsigned)(HZ/5)) /* maximal time to delay before sending an ACK */
322 #define TCP_DELACK_MIN ((unsigned)(HZ/25)) /* minimal time to delay before sending an ACK */
323 #define TCP_ATO_MIN ((unsigned)(HZ/25))
325 #define TCP_DELACK_MIN 4U
326 #define TCP_ATO_MIN 4U
328 #define TCP_RTO_MAX ((unsigned)(120*HZ))
329 #define TCP_RTO_MIN ((unsigned)(HZ/5))
330 #define TCP_TIMEOUT_INIT ((unsigned)(3*HZ)) /* RFC 1122 initial RTO value */
332 #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
333 * for local resources.
336 #define TCP_KEEPALIVE_TIME (120*60*HZ) /* two hours */
337 #define TCP_KEEPALIVE_PROBES 9 /* Max of 9 keepalive probes */
338 #define TCP_KEEPALIVE_INTVL (75*HZ)
340 #define MAX_TCP_KEEPIDLE 32767
341 #define MAX_TCP_KEEPINTVL 32767
342 #define MAX_TCP_KEEPCNT 127
343 #define MAX_TCP_SYNCNT 127
345 #define TCP_SYNQ_INTERVAL (HZ/5) /* Period of SYNACK timer */
346 #define TCP_SYNQ_HSIZE 512 /* Size of SYNACK hash table */
348 #define TCP_PAWS_24DAYS (60 * 60 * 24 * 24)
349 #define TCP_PAWS_MSL 60 /* Per-host timestamps are invalidated
350 * after this time. It should be equal
351 * (or greater than) TCP_TIMEWAIT_LEN
352 * to provide reliability equal to one
353 * provided by timewait state.
355 #define TCP_PAWS_WINDOW 1 /* Replay window for per-host
356 * timestamps. It must be less than
357 * minimal timewait lifetime.
360 #define TCP_TW_RECYCLE_SLOTS_LOG 5
361 #define TCP_TW_RECYCLE_SLOTS (1<<TCP_TW_RECYCLE_SLOTS_LOG)
363 /* If time > 4sec, it is "slow" path, no recycling is required,
364 so that we select tick to get range about 4 seconds.
367 #if HZ <= 16 || HZ > 4096
368 # error Unsupported: HZ <= 16 or HZ > 4096
370 # define TCP_TW_RECYCLE_TICK (5+2-TCP_TW_RECYCLE_SLOTS_LOG)
372 # define TCP_TW_RECYCLE_TICK (6+2-TCP_TW_RECYCLE_SLOTS_LOG)
374 # define TCP_TW_RECYCLE_TICK (7+2-TCP_TW_RECYCLE_SLOTS_LOG)
376 # define TCP_TW_RECYCLE_TICK (8+2-TCP_TW_RECYCLE_SLOTS_LOG)
378 # define TCP_TW_RECYCLE_TICK (9+2-TCP_TW_RECYCLE_SLOTS_LOG)
380 # define TCP_TW_RECYCLE_TICK (10+2-TCP_TW_RECYCLE_SLOTS_LOG)
382 # define TCP_TW_RECYCLE_TICK (11+2-TCP_TW_RECYCLE_SLOTS_LOG)
384 # define TCP_TW_RECYCLE_TICK (12+2-TCP_TW_RECYCLE_SLOTS_LOG)
390 #define TCPOPT_NOP 1 /* Padding */
391 #define TCPOPT_EOL 0 /* End of options */
392 #define TCPOPT_MSS 2 /* Segment size negotiating */
393 #define TCPOPT_WINDOW 3 /* Window scaling */
394 #define TCPOPT_SACK_PERM 4 /* SACK Permitted */
395 #define TCPOPT_SACK 5 /* SACK Block */
396 #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
402 #define TCPOLEN_MSS 4
403 #define TCPOLEN_WINDOW 3
404 #define TCPOLEN_SACK_PERM 2
405 #define TCPOLEN_TIMESTAMP 10
407 /* But this is what stacks really send out. */
408 #define TCPOLEN_TSTAMP_ALIGNED 12
409 #define TCPOLEN_WSCALE_ALIGNED 4
410 #define TCPOLEN_SACKPERM_ALIGNED 4
411 #define TCPOLEN_SACK_BASE 2
412 #define TCPOLEN_SACK_BASE_ALIGNED 4
413 #define TCPOLEN_SACK_PERBLOCK 8
415 #define TCP_TIME_RETRANS 1 /* Retransmit timer */
416 #define TCP_TIME_DACK 2 /* Delayed ack timer */
417 #define TCP_TIME_PROBE0 3 /* Zero window probe timer */
418 #define TCP_TIME_KEEPOPEN 4 /* Keepalive timer */
420 /* Flags in tp->nonagle */
421 #define TCP_NAGLE_OFF 1 /* Nagle's algo is disabled */
422 #define TCP_NAGLE_CORK 2 /* Socket is corked */
423 #define TCP_NAGLE_PUSH 4 /* Cork is overriden for already queued data */
425 /* sysctl variables for tcp */
426 extern int sysctl_tcp_timestamps
;
427 extern int sysctl_tcp_window_scaling
;
428 extern int sysctl_tcp_sack
;
429 extern int sysctl_tcp_fin_timeout
;
430 extern int sysctl_tcp_tw_recycle
;
431 extern int sysctl_tcp_keepalive_time
;
432 extern int sysctl_tcp_keepalive_probes
;
433 extern int sysctl_tcp_keepalive_intvl
;
434 extern int sysctl_tcp_syn_retries
;
435 extern int sysctl_tcp_synack_retries
;
436 extern int sysctl_tcp_retries1
;
437 extern int sysctl_tcp_retries2
;
438 extern int sysctl_tcp_orphan_retries
;
439 extern int sysctl_tcp_syncookies
;
440 extern int sysctl_tcp_retrans_collapse
;
441 extern int sysctl_tcp_stdurg
;
442 extern int sysctl_tcp_rfc1337
;
443 extern int sysctl_tcp_abort_on_overflow
;
444 extern int sysctl_tcp_max_orphans
;
445 extern int sysctl_tcp_max_tw_buckets
;
446 extern int sysctl_tcp_fack
;
447 extern int sysctl_tcp_reordering
;
448 extern int sysctl_tcp_ecn
;
449 extern int sysctl_tcp_dsack
;
450 extern int sysctl_tcp_mem
[3];
451 extern int sysctl_tcp_wmem
[3];
452 extern int sysctl_tcp_rmem
[3];
453 extern int sysctl_tcp_app_win
;
454 extern int sysctl_tcp_adv_win_scale
;
455 extern int sysctl_tcp_tw_reuse
;
456 extern int sysctl_tcp_frto
;
457 extern int sysctl_tcp_low_latency
;
458 extern int sysctl_tcp_nometrics_save
;
459 extern int sysctl_tcp_moderate_rcvbuf
;
460 extern int sysctl_tcp_tso_win_divisor
;
462 extern atomic_t tcp_memory_allocated
;
463 extern atomic_t tcp_sockets_allocated
;
464 extern int tcp_memory_pressure
;
466 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
467 #define TCP_INET_FAMILY(fam) ((fam) == AF_INET)
469 #define TCP_INET_FAMILY(fam) 1
473 * Pointers to address related TCP functions
474 * (i.e. things that depend on the address family)
478 int (*queue_xmit
) (struct sk_buff
*skb
,
481 void (*send_check
) (struct sock
*sk
,
484 struct sk_buff
*skb
);
486 int (*rebuild_header
) (struct sock
*sk
);
488 int (*conn_request
) (struct sock
*sk
,
489 struct sk_buff
*skb
);
491 struct sock
* (*syn_recv_sock
) (struct sock
*sk
,
493 struct request_sock
*req
,
494 struct dst_entry
*dst
);
496 int (*remember_stamp
) (struct sock
*sk
);
498 __u16 net_header_len
;
500 int (*setsockopt
) (struct sock
*sk
,
506 int (*getsockopt
) (struct sock
*sk
,
513 void (*addr2sockaddr
) (struct sock
*sk
,
520 * The next routines deal with comparing 32 bit unsigned ints
521 * and worry about wraparound (automatic with unsigned arithmetic).
524 static inline int before(__u32 seq1
, __u32 seq2
)
526 return (__s32
)(seq1
-seq2
) < 0;
529 static inline int after(__u32 seq1
, __u32 seq2
)
531 return (__s32
)(seq2
-seq1
) < 0;
535 /* is s2<=s1<=s3 ? */
536 static inline int between(__u32 seq1
, __u32 seq2
, __u32 seq3
)
538 return seq3
- seq2
>= seq1
- seq2
;
542 extern struct proto tcp_prot
;
544 DECLARE_SNMP_STAT(struct tcp_mib
, tcp_statistics
);
545 #define TCP_INC_STATS(field) SNMP_INC_STATS(tcp_statistics, field)
546 #define TCP_INC_STATS_BH(field) SNMP_INC_STATS_BH(tcp_statistics, field)
547 #define TCP_INC_STATS_USER(field) SNMP_INC_STATS_USER(tcp_statistics, field)
548 #define TCP_DEC_STATS(field) SNMP_DEC_STATS(tcp_statistics, field)
549 #define TCP_ADD_STATS_BH(field, val) SNMP_ADD_STATS_BH(tcp_statistics, field, val)
550 #define TCP_ADD_STATS_USER(field, val) SNMP_ADD_STATS_USER(tcp_statistics, field, val)
552 extern void tcp_put_port(struct sock
*sk
);
553 extern void tcp_inherit_port(struct sock
*sk
, struct sock
*child
);
555 extern void tcp_v4_err(struct sk_buff
*skb
, u32
);
557 extern void tcp_shutdown (struct sock
*sk
, int how
);
559 extern int tcp_v4_rcv(struct sk_buff
*skb
);
561 extern int tcp_v4_remember_stamp(struct sock
*sk
);
563 extern int tcp_v4_tw_remember_stamp(struct tcp_tw_bucket
*tw
);
565 extern int tcp_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
566 struct msghdr
*msg
, size_t size
);
567 extern ssize_t
tcp_sendpage(struct socket
*sock
, struct page
*page
, int offset
, size_t size
, int flags
);
569 extern int tcp_ioctl(struct sock
*sk
,
573 extern int tcp_rcv_state_process(struct sock
*sk
,
578 extern int tcp_rcv_established(struct sock
*sk
,
583 extern void tcp_rcv_space_adjust(struct sock
*sk
);
592 static inline void tcp_schedule_ack(struct tcp_sock
*tp
)
594 tp
->ack
.pending
|= TCP_ACK_SCHED
;
597 static inline int tcp_ack_scheduled(struct tcp_sock
*tp
)
599 return tp
->ack
.pending
&TCP_ACK_SCHED
;
602 static __inline__
void tcp_dec_quickack_mode(struct tcp_sock
*tp
, unsigned int pkts
)
605 if (pkts
>= tp
->ack
.quick
) {
608 /* Leaving quickack mode we deflate ATO. */
609 tp
->ack
.ato
= TCP_ATO_MIN
;
611 tp
->ack
.quick
-= pkts
;
615 extern void tcp_enter_quickack_mode(struct tcp_sock
*tp
);
617 static __inline__
void tcp_delack_init(struct tcp_sock
*tp
)
619 memset(&tp
->ack
, 0, sizeof(tp
->ack
));
622 static inline void tcp_clear_options(struct tcp_options_received
*rx_opt
)
624 rx_opt
->tstamp_ok
= rx_opt
->sack_ok
= rx_opt
->wscale_ok
= rx_opt
->snd_wscale
= 0;
636 extern enum tcp_tw_status
tcp_timewait_state_process(struct tcp_tw_bucket
*tw
,
641 extern struct sock
* tcp_check_req(struct sock
*sk
,struct sk_buff
*skb
,
642 struct request_sock
*req
,
643 struct request_sock
**prev
);
644 extern int tcp_child_process(struct sock
*parent
,
646 struct sk_buff
*skb
);
647 extern void tcp_enter_frto(struct sock
*sk
);
648 extern void tcp_enter_loss(struct sock
*sk
, int how
);
649 extern void tcp_clear_retrans(struct tcp_sock
*tp
);
650 extern void tcp_update_metrics(struct sock
*sk
);
652 extern void tcp_close(struct sock
*sk
,
654 extern struct sock
* tcp_accept(struct sock
*sk
, int flags
, int *err
);
655 extern unsigned int tcp_poll(struct file
* file
, struct socket
*sock
, struct poll_table_struct
*wait
);
657 extern int tcp_getsockopt(struct sock
*sk
, int level
,
661 extern int tcp_setsockopt(struct sock
*sk
, int level
,
662 int optname
, char __user
*optval
,
664 extern void tcp_set_keepalive(struct sock
*sk
, int val
);
665 extern int tcp_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
667 size_t len
, int nonblock
,
668 int flags
, int *addr_len
);
670 extern int tcp_listen_start(struct sock
*sk
);
672 extern void tcp_parse_options(struct sk_buff
*skb
,
673 struct tcp_options_received
*opt_rx
,
677 * TCP v4 functions exported for the inet6 API
680 extern void tcp_v4_send_check(struct sock
*sk
,
681 struct tcphdr
*th
, int len
,
682 struct sk_buff
*skb
);
684 extern int tcp_v4_conn_request(struct sock
*sk
,
685 struct sk_buff
*skb
);
687 extern struct sock
* tcp_create_openreq_child(struct sock
*sk
,
688 struct request_sock
*req
,
689 struct sk_buff
*skb
);
691 extern struct sock
* tcp_v4_syn_recv_sock(struct sock
*sk
,
693 struct request_sock
*req
,
694 struct dst_entry
*dst
);
696 extern int tcp_v4_do_rcv(struct sock
*sk
,
697 struct sk_buff
*skb
);
699 extern int tcp_v4_connect(struct sock
*sk
,
700 struct sockaddr
*uaddr
,
703 extern int tcp_connect(struct sock
*sk
);
705 extern struct sk_buff
* tcp_make_synack(struct sock
*sk
,
706 struct dst_entry
*dst
,
707 struct request_sock
*req
);
709 extern int tcp_disconnect(struct sock
*sk
, int flags
);
711 extern void tcp_unhash(struct sock
*sk
);
713 extern int tcp_v4_hash_connecting(struct sock
*sk
);
716 /* From syncookies.c */
717 extern struct sock
*cookie_v4_check(struct sock
*sk
, struct sk_buff
*skb
,
718 struct ip_options
*opt
);
719 extern __u32
cookie_v4_init_sequence(struct sock
*sk
, struct sk_buff
*skb
,
724 extern void __tcp_push_pending_frames(struct sock
*sk
, struct tcp_sock
*tp
,
725 unsigned int cur_mss
, int nonagle
);
726 extern int tcp_may_send_now(struct sock
*sk
, struct tcp_sock
*tp
);
727 extern int tcp_retransmit_skb(struct sock
*, struct sk_buff
*);
728 extern void tcp_xmit_retransmit_queue(struct sock
*);
729 extern void tcp_simple_retransmit(struct sock
*);
730 extern int tcp_trim_head(struct sock
*, struct sk_buff
*, u32
);
732 extern void tcp_send_probe0(struct sock
*);
733 extern void tcp_send_partial(struct sock
*);
734 extern int tcp_write_wakeup(struct sock
*);
735 extern void tcp_send_fin(struct sock
*sk
);
736 extern void tcp_send_active_reset(struct sock
*sk
,
737 unsigned int __nocast priority
);
738 extern int tcp_send_synack(struct sock
*);
739 extern void tcp_push_one(struct sock
*, unsigned int mss_now
);
740 extern void tcp_send_ack(struct sock
*sk
);
741 extern void tcp_send_delayed_ack(struct sock
*sk
);
744 extern void tcp_cwnd_application_limited(struct sock
*sk
);
747 extern void tcp_init_xmit_timers(struct sock
*);
748 extern void tcp_clear_xmit_timers(struct sock
*);
750 extern void tcp_delete_keepalive_timer(struct sock
*);
751 extern void tcp_reset_keepalive_timer(struct sock
*, unsigned long);
752 extern unsigned int tcp_sync_mss(struct sock
*sk
, u32 pmtu
);
753 extern unsigned int tcp_current_mss(struct sock
*sk
, int large
);
756 extern const char tcp_timer_bug_msg
[];
760 extern void tcp_get_info(struct sock
*, struct tcp_info
*);
762 /* Read 'sendfile()'-style from a TCP socket */
763 typedef int (*sk_read_actor_t
)(read_descriptor_t
*, struct sk_buff
*,
764 unsigned int, size_t);
765 extern int tcp_read_sock(struct sock
*sk
, read_descriptor_t
*desc
,
766 sk_read_actor_t recv_actor
);
768 static inline void tcp_clear_xmit_timer(struct sock
*sk
, int what
)
770 struct tcp_sock
*tp
= tcp_sk(sk
);
773 case TCP_TIME_RETRANS
:
774 case TCP_TIME_PROBE0
:
777 #ifdef TCP_CLEAR_TIMERS
778 sk_stop_timer(sk
, &tp
->retransmit_timer
);
785 #ifdef TCP_CLEAR_TIMERS
786 sk_stop_timer(sk
, &tp
->delack_timer
);
791 printk(tcp_timer_bug_msg
);
799 * Reset the retransmission timer
801 static inline void tcp_reset_xmit_timer(struct sock
*sk
, int what
, unsigned long when
)
803 struct tcp_sock
*tp
= tcp_sk(sk
);
805 if (when
> TCP_RTO_MAX
) {
807 printk(KERN_DEBUG
"reset_xmit_timer sk=%p %d when=0x%lx, caller=%p\n", sk
, what
, when
, current_text_addr());
813 case TCP_TIME_RETRANS
:
814 case TCP_TIME_PROBE0
:
816 tp
->timeout
= jiffies
+when
;
817 sk_reset_timer(sk
, &tp
->retransmit_timer
, tp
->timeout
);
821 tp
->ack
.pending
|= TCP_ACK_TIMER
;
822 tp
->ack
.timeout
= jiffies
+when
;
823 sk_reset_timer(sk
, &tp
->delack_timer
, tp
->ack
.timeout
);
828 printk(tcp_timer_bug_msg
);
834 /* Initialize RCV_MSS value.
835 * RCV_MSS is an our guess about MSS used by the peer.
836 * We haven't any direct information about the MSS.
837 * It's better to underestimate the RCV_MSS rather than overestimate.
838 * Overestimations make us ACKing less frequently than needed.
839 * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
842 static inline void tcp_initialize_rcv_mss(struct sock
*sk
)
844 struct tcp_sock
*tp
= tcp_sk(sk
);
845 unsigned int hint
= min_t(unsigned int, tp
->advmss
, tp
->mss_cache
);
847 hint
= min(hint
, tp
->rcv_wnd
/2);
848 hint
= min(hint
, TCP_MIN_RCVMSS
);
849 hint
= max(hint
, TCP_MIN_MSS
);
851 tp
->ack
.rcv_mss
= hint
;
854 static __inline__
void __tcp_fast_path_on(struct tcp_sock
*tp
, u32 snd_wnd
)
856 tp
->pred_flags
= htonl((tp
->tcp_header_len
<< 26) |
857 ntohl(TCP_FLAG_ACK
) |
861 static __inline__
void tcp_fast_path_on(struct tcp_sock
*tp
)
863 __tcp_fast_path_on(tp
, tp
->snd_wnd
>> tp
->rx_opt
.snd_wscale
);
866 static inline void tcp_fast_path_check(struct sock
*sk
, struct tcp_sock
*tp
)
868 if (skb_queue_empty(&tp
->out_of_order_queue
) &&
870 atomic_read(&sk
->sk_rmem_alloc
) < sk
->sk_rcvbuf
&&
872 tcp_fast_path_on(tp
);
875 /* Compute the actual receive window we are currently advertising.
876 * Rcv_nxt can be after the window if our peer push more data
877 * than the offered window.
879 static __inline__ u32
tcp_receive_window(const struct tcp_sock
*tp
)
881 s32 win
= tp
->rcv_wup
+ tp
->rcv_wnd
- tp
->rcv_nxt
;
888 /* Choose a new window, without checks for shrinking, and without
889 * scaling applied to the result. The caller does these things
890 * if necessary. This is a "raw" window selection.
892 extern u32
__tcp_select_window(struct sock
*sk
);
894 /* TCP timestamps are only 32-bits, this causes a slight
895 * complication on 64-bit systems since we store a snapshot
896 * of jiffies in the buffer control blocks below. We decidely
897 * only use of the low 32-bits of jiffies and hide the ugly
898 * casts with the following macro.
900 #define tcp_time_stamp ((__u32)(jiffies))
902 /* This is what the send packet queueing engine uses to pass
903 * TCP per-packet control information to the transmission
904 * code. We also store the host-order sequence numbers in
905 * here too. This is 36 bytes on 32-bit architectures,
906 * 40 bytes on 64-bit machines, if this grows please adjust
907 * skbuff.h:skbuff->cb[xxx] size appropriately.
911 struct inet_skb_parm h4
;
912 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
913 struct inet6_skb_parm h6
;
915 } header
; /* For incoming frames */
916 __u32 seq
; /* Starting sequence number */
917 __u32 end_seq
; /* SEQ + FIN + SYN + datalen */
918 __u32 when
; /* used to compute rtt's */
919 __u8 flags
; /* TCP header flags. */
921 /* NOTE: These must match up to the flags byte in a
924 #define TCPCB_FLAG_FIN 0x01
925 #define TCPCB_FLAG_SYN 0x02
926 #define TCPCB_FLAG_RST 0x04
927 #define TCPCB_FLAG_PSH 0x08
928 #define TCPCB_FLAG_ACK 0x10
929 #define TCPCB_FLAG_URG 0x20
930 #define TCPCB_FLAG_ECE 0x40
931 #define TCPCB_FLAG_CWR 0x80
933 __u8 sacked
; /* State flags for SACK/FACK. */
934 #define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block */
935 #define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted */
936 #define TCPCB_LOST 0x04 /* SKB is lost */
937 #define TCPCB_TAGBITS 0x07 /* All tag bits */
939 #define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */
940 #define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
942 #define TCPCB_URG 0x20 /* Urgent pointer advenced here */
944 #define TCPCB_AT_TAIL (TCPCB_URG)
946 __u16 urg_ptr
; /* Valid w/URG flags is set. */
947 __u32 ack_seq
; /* Sequence number ACK'd */
950 #define TCP_SKB_CB(__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0]))
952 #include <net/tcp_ecn.h>
954 /* Due to TSO, an SKB can be composed of multiple actual
955 * packets. To keep these tracked properly, we use this.
957 static inline int tcp_skb_pcount(const struct sk_buff
*skb
)
959 return skb_shinfo(skb
)->tso_segs
;
962 /* This is valid iff tcp_skb_pcount() > 1. */
963 static inline int tcp_skb_mss(const struct sk_buff
*skb
)
965 return skb_shinfo(skb
)->tso_size
;
968 static inline void tcp_dec_pcount_approx(__u32
*count
,
969 const struct sk_buff
*skb
)
972 *count
-= tcp_skb_pcount(skb
);
978 static inline void tcp_packets_out_inc(struct sock
*sk
,
980 const struct sk_buff
*skb
)
982 int orig
= tp
->packets_out
;
984 tp
->packets_out
+= tcp_skb_pcount(skb
);
986 tcp_reset_xmit_timer(sk
, TCP_TIME_RETRANS
, tp
->rto
);
989 static inline void tcp_packets_out_dec(struct tcp_sock
*tp
,
990 const struct sk_buff
*skb
)
992 tp
->packets_out
-= tcp_skb_pcount(skb
);
995 /* Events passed to congestion control interface */
997 CA_EVENT_TX_START
, /* first transmit when no packets in flight */
998 CA_EVENT_CWND_RESTART
, /* congestion window restart */
999 CA_EVENT_COMPLETE_CWR
, /* end of congestion recovery */
1000 CA_EVENT_FRTO
, /* fast recovery timeout */
1001 CA_EVENT_LOSS
, /* loss timeout */
1002 CA_EVENT_FAST_ACK
, /* in sequence ack */
1003 CA_EVENT_SLOW_ACK
, /* other ack */
1007 * Interface for adding new TCP congestion control handlers
1009 #define TCP_CA_NAME_MAX 16
1010 struct tcp_congestion_ops
{
1011 struct list_head list
;
1013 /* initialize private data (optional) */
1014 void (*init
)(struct tcp_sock
*tp
);
1015 /* cleanup private data (optional) */
1016 void (*release
)(struct tcp_sock
*tp
);
1018 /* return slow start threshold (required) */
1019 u32 (*ssthresh
)(struct tcp_sock
*tp
);
1020 /* lower bound for congestion window (optional) */
1021 u32 (*min_cwnd
)(struct tcp_sock
*tp
);
1022 /* do new cwnd calculation (required) */
1023 void (*cong_avoid
)(struct tcp_sock
*tp
, u32 ack
,
1024 u32 rtt
, u32 in_flight
, int good_ack
);
1025 /* round trip time sample per acked packet (optional) */
1026 void (*rtt_sample
)(struct tcp_sock
*tp
, u32 usrtt
);
1027 /* call before changing ca_state (optional) */
1028 void (*set_state
)(struct tcp_sock
*tp
, u8 new_state
);
1029 /* call when cwnd event occurs (optional) */
1030 void (*cwnd_event
)(struct tcp_sock
*tp
, enum tcp_ca_event ev
);
1031 /* new value of cwnd after loss (optional) */
1032 u32 (*undo_cwnd
)(struct tcp_sock
*tp
);
1033 /* hook for packet ack accounting (optional) */
1034 void (*pkts_acked
)(struct tcp_sock
*tp
, u32 num_acked
);
1035 /* get info for tcp_diag (optional) */
1036 void (*get_info
)(struct tcp_sock
*tp
, u32 ext
, struct sk_buff
*skb
);
1038 char name
[TCP_CA_NAME_MAX
];
1039 struct module
*owner
;
1042 extern int tcp_register_congestion_control(struct tcp_congestion_ops
*type
);
1043 extern void tcp_unregister_congestion_control(struct tcp_congestion_ops
*type
);
1045 extern void tcp_init_congestion_control(struct tcp_sock
*tp
);
1046 extern void tcp_cleanup_congestion_control(struct tcp_sock
*tp
);
1047 extern int tcp_set_default_congestion_control(const char *name
);
1048 extern void tcp_get_default_congestion_control(char *name
);
1049 extern int tcp_set_congestion_control(struct tcp_sock
*tp
, const char *name
);
1051 extern struct tcp_congestion_ops tcp_init_congestion_ops
;
1052 extern u32
tcp_reno_ssthresh(struct tcp_sock
*tp
);
1053 extern void tcp_reno_cong_avoid(struct tcp_sock
*tp
, u32 ack
,
1054 u32 rtt
, u32 in_flight
, int flag
);
1055 extern u32
tcp_reno_min_cwnd(struct tcp_sock
*tp
);
1056 extern struct tcp_congestion_ops tcp_reno
;
1058 static inline void tcp_set_ca_state(struct tcp_sock
*tp
, u8 ca_state
)
1060 if (tp
->ca_ops
->set_state
)
1061 tp
->ca_ops
->set_state(tp
, ca_state
);
1062 tp
->ca_state
= ca_state
;
1065 static inline void tcp_ca_event(struct tcp_sock
*tp
, enum tcp_ca_event event
)
1067 if (tp
->ca_ops
->cwnd_event
)
1068 tp
->ca_ops
->cwnd_event(tp
, event
);
1071 /* This determines how many packets are "in the network" to the best
1072 * of our knowledge. In many cases it is conservative, but where
1073 * detailed information is available from the receiver (via SACK
1074 * blocks etc.) we can make more aggressive calculations.
1076 * Use this for decisions involving congestion control, use just
1077 * tp->packets_out to determine if the send queue is empty or not.
1079 * Read this equation as:
1081 * "Packets sent once on transmission queue" MINUS
1082 * "Packets left network, but not honestly ACKed yet" PLUS
1083 * "Packets fast retransmitted"
1085 static __inline__
unsigned int tcp_packets_in_flight(const struct tcp_sock
*tp
)
1087 return (tp
->packets_out
- tp
->left_out
+ tp
->retrans_out
);
1090 /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
1091 * The exception is rate halving phase, when cwnd is decreasing towards
1094 static inline __u32
tcp_current_ssthresh(struct tcp_sock
*tp
)
1096 if ((1<<tp
->ca_state
)&(TCPF_CA_CWR
|TCPF_CA_Recovery
))
1097 return tp
->snd_ssthresh
;
1099 return max(tp
->snd_ssthresh
,
1100 ((tp
->snd_cwnd
>> 1) +
1101 (tp
->snd_cwnd
>> 2)));
1104 static inline void tcp_sync_left_out(struct tcp_sock
*tp
)
1106 if (tp
->rx_opt
.sack_ok
&&
1107 (tp
->sacked_out
>= tp
->packets_out
- tp
->lost_out
))
1108 tp
->sacked_out
= tp
->packets_out
- tp
->lost_out
;
1109 tp
->left_out
= tp
->sacked_out
+ tp
->lost_out
;
1112 /* Set slow start threshold and cwnd not falling to slow start */
1113 static inline void __tcp_enter_cwr(struct tcp_sock
*tp
)
1115 tp
->undo_marker
= 0;
1116 tp
->snd_ssthresh
= tp
->ca_ops
->ssthresh(tp
);
1117 tp
->snd_cwnd
= min(tp
->snd_cwnd
,
1118 tcp_packets_in_flight(tp
) + 1U);
1119 tp
->snd_cwnd_cnt
= 0;
1120 tp
->high_seq
= tp
->snd_nxt
;
1121 tp
->snd_cwnd_stamp
= tcp_time_stamp
;
1122 TCP_ECN_queue_cwr(tp
);
1125 static inline void tcp_enter_cwr(struct tcp_sock
*tp
)
1127 tp
->prior_ssthresh
= 0;
1128 if (tp
->ca_state
< TCP_CA_CWR
) {
1129 __tcp_enter_cwr(tp
);
1130 tcp_set_ca_state(tp
, TCP_CA_CWR
);
1134 extern __u32
tcp_init_cwnd(struct tcp_sock
*tp
, struct dst_entry
*dst
);
1136 /* Slow start with delack produces 3 packets of burst, so that
1137 * it is safe "de facto".
1139 static __inline__ __u32
tcp_max_burst(const struct tcp_sock
*tp
)
1144 static __inline__
void tcp_minshall_update(struct tcp_sock
*tp
, int mss
,
1145 const struct sk_buff
*skb
)
1148 tp
->snd_sml
= TCP_SKB_CB(skb
)->end_seq
;
1151 static __inline__
void tcp_check_probe_timer(struct sock
*sk
, struct tcp_sock
*tp
)
1153 if (!tp
->packets_out
&& !tp
->pending
)
1154 tcp_reset_xmit_timer(sk
, TCP_TIME_PROBE0
, tp
->rto
);
1157 static __inline__
void tcp_push_pending_frames(struct sock
*sk
,
1158 struct tcp_sock
*tp
)
1160 __tcp_push_pending_frames(sk
, tp
, tcp_current_mss(sk
, 1), tp
->nonagle
);
1163 static __inline__
void tcp_init_wl(struct tcp_sock
*tp
, u32 ack
, u32 seq
)
1168 static __inline__
void tcp_update_wl(struct tcp_sock
*tp
, u32 ack
, u32 seq
)
1173 extern void tcp_destroy_sock(struct sock
*sk
);
1177 * Calculate(/check) TCP checksum
1179 static __inline__ u16
tcp_v4_check(struct tcphdr
*th
, int len
,
1180 unsigned long saddr
, unsigned long daddr
,
1183 return csum_tcpudp_magic(saddr
,daddr
,len
,IPPROTO_TCP
,base
);
1186 static __inline__
int __tcp_checksum_complete(struct sk_buff
*skb
)
1188 return (unsigned short)csum_fold(skb_checksum(skb
, 0, skb
->len
, skb
->csum
));
1191 static __inline__
int tcp_checksum_complete(struct sk_buff
*skb
)
1193 return skb
->ip_summed
!= CHECKSUM_UNNECESSARY
&&
1194 __tcp_checksum_complete(skb
);
1197 /* Prequeue for VJ style copy to user, combined with checksumming. */
1199 static __inline__
void tcp_prequeue_init(struct tcp_sock
*tp
)
1201 tp
->ucopy
.task
= NULL
;
1203 tp
->ucopy
.memory
= 0;
1204 skb_queue_head_init(&tp
->ucopy
.prequeue
);
1207 /* Packet is added to VJ-style prequeue for processing in process
1208 * context, if a reader task is waiting. Apparently, this exciting
1209 * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93)
1210 * failed somewhere. Latency? Burstiness? Well, at least now we will
1211 * see, why it failed. 8)8) --ANK
1213 * NOTE: is this not too big to inline?
1215 static __inline__
int tcp_prequeue(struct sock
*sk
, struct sk_buff
*skb
)
1217 struct tcp_sock
*tp
= tcp_sk(sk
);
1219 if (!sysctl_tcp_low_latency
&& tp
->ucopy
.task
) {
1220 __skb_queue_tail(&tp
->ucopy
.prequeue
, skb
);
1221 tp
->ucopy
.memory
+= skb
->truesize
;
1222 if (tp
->ucopy
.memory
> sk
->sk_rcvbuf
) {
1223 struct sk_buff
*skb1
;
1225 BUG_ON(sock_owned_by_user(sk
));
1227 while ((skb1
= __skb_dequeue(&tp
->ucopy
.prequeue
)) != NULL
) {
1228 sk
->sk_backlog_rcv(sk
, skb1
);
1229 NET_INC_STATS_BH(LINUX_MIB_TCPPREQUEUEDROPPED
);
1232 tp
->ucopy
.memory
= 0;
1233 } else if (skb_queue_len(&tp
->ucopy
.prequeue
) == 1) {
1234 wake_up_interruptible(sk
->sk_sleep
);
1235 if (!tcp_ack_scheduled(tp
))
1236 tcp_reset_xmit_timer(sk
, TCP_TIME_DACK
, (3*TCP_RTO_MIN
)/4);
1247 static const char *statename
[]={
1248 "Unused","Established","Syn Sent","Syn Recv",
1249 "Fin Wait 1","Fin Wait 2","Time Wait", "Close",
1250 "Close Wait","Last ACK","Listen","Closing"
1254 static __inline__
void tcp_set_state(struct sock
*sk
, int state
)
1256 int oldstate
= sk
->sk_state
;
1259 case TCP_ESTABLISHED
:
1260 if (oldstate
!= TCP_ESTABLISHED
)
1261 TCP_INC_STATS(TCP_MIB_CURRESTAB
);
1265 if (oldstate
== TCP_CLOSE_WAIT
|| oldstate
== TCP_ESTABLISHED
)
1266 TCP_INC_STATS(TCP_MIB_ESTABRESETS
);
1268 sk
->sk_prot
->unhash(sk
);
1269 if (tcp_sk(sk
)->bind_hash
&&
1270 !(sk
->sk_userlocks
& SOCK_BINDPORT_LOCK
))
1274 if (oldstate
==TCP_ESTABLISHED
)
1275 TCP_DEC_STATS(TCP_MIB_CURRESTAB
);
1278 /* Change state AFTER socket is unhashed to avoid closed
1279 * socket sitting in hash tables.
1281 sk
->sk_state
= state
;
1284 SOCK_DEBUG(sk
, "TCP sk=%p, State %s -> %s\n",sk
, statename
[oldstate
],statename
[state
]);
1288 static __inline__
void tcp_done(struct sock
*sk
)
1290 tcp_set_state(sk
, TCP_CLOSE
);
1291 tcp_clear_xmit_timers(sk
);
1293 sk
->sk_shutdown
= SHUTDOWN_MASK
;
1295 if (!sock_flag(sk
, SOCK_DEAD
))
1296 sk
->sk_state_change(sk
);
1298 tcp_destroy_sock(sk
);
1301 static __inline__
void tcp_sack_reset(struct tcp_options_received
*rx_opt
)
1304 rx_opt
->eff_sacks
= 0;
1305 rx_opt
->num_sacks
= 0;
1308 static __inline__
void tcp_build_and_update_options(__u32
*ptr
, struct tcp_sock
*tp
, __u32 tstamp
)
1310 if (tp
->rx_opt
.tstamp_ok
) {
1311 *ptr
++ = __constant_htonl((TCPOPT_NOP
<< 24) |
1312 (TCPOPT_NOP
<< 16) |
1313 (TCPOPT_TIMESTAMP
<< 8) |
1315 *ptr
++ = htonl(tstamp
);
1316 *ptr
++ = htonl(tp
->rx_opt
.ts_recent
);
1318 if (tp
->rx_opt
.eff_sacks
) {
1319 struct tcp_sack_block
*sp
= tp
->rx_opt
.dsack
? tp
->duplicate_sack
: tp
->selective_acks
;
1322 *ptr
++ = __constant_htonl((TCPOPT_NOP
<< 24) |
1323 (TCPOPT_NOP
<< 16) |
1324 (TCPOPT_SACK
<< 8) |
1325 (TCPOLEN_SACK_BASE
+
1326 (tp
->rx_opt
.eff_sacks
* TCPOLEN_SACK_PERBLOCK
)));
1327 for(this_sack
= 0; this_sack
< tp
->rx_opt
.eff_sacks
; this_sack
++) {
1328 *ptr
++ = htonl(sp
[this_sack
].start_seq
);
1329 *ptr
++ = htonl(sp
[this_sack
].end_seq
);
1331 if (tp
->rx_opt
.dsack
) {
1332 tp
->rx_opt
.dsack
= 0;
1333 tp
->rx_opt
.eff_sacks
--;
1338 /* Construct a tcp options header for a SYN or SYN_ACK packet.
1339 * If this is every changed make sure to change the definition of
1340 * MAX_SYN_SIZE to match the new maximum number of options that you
1343 static inline void tcp_syn_build_options(__u32
*ptr
, int mss
, int ts
, int sack
,
1344 int offer_wscale
, int wscale
, __u32 tstamp
, __u32 ts_recent
)
1346 /* We always get an MSS option.
1347 * The option bytes which will be seen in normal data
1348 * packets should timestamps be used, must be in the MSS
1349 * advertised. But we subtract them from tp->mss_cache so
1350 * that calculations in tcp_sendmsg are simpler etc.
1351 * So account for this fact here if necessary. If we
1352 * don't do this correctly, as a receiver we won't
1353 * recognize data packets as being full sized when we
1354 * should, and thus we won't abide by the delayed ACK
1356 * SACKs don't matter, we never delay an ACK when we
1357 * have any of those going out.
1359 *ptr
++ = htonl((TCPOPT_MSS
<< 24) | (TCPOLEN_MSS
<< 16) | mss
);
1362 *ptr
++ = __constant_htonl((TCPOPT_SACK_PERM
<< 24) | (TCPOLEN_SACK_PERM
<< 16) |
1363 (TCPOPT_TIMESTAMP
<< 8) | TCPOLEN_TIMESTAMP
);
1365 *ptr
++ = __constant_htonl((TCPOPT_NOP
<< 24) | (TCPOPT_NOP
<< 16) |
1366 (TCPOPT_TIMESTAMP
<< 8) | TCPOLEN_TIMESTAMP
);
1367 *ptr
++ = htonl(tstamp
); /* TSVAL */
1368 *ptr
++ = htonl(ts_recent
); /* TSECR */
1370 *ptr
++ = __constant_htonl((TCPOPT_NOP
<< 24) | (TCPOPT_NOP
<< 16) |
1371 (TCPOPT_SACK_PERM
<< 8) | TCPOLEN_SACK_PERM
);
1373 *ptr
++ = htonl((TCPOPT_NOP
<< 24) | (TCPOPT_WINDOW
<< 16) | (TCPOLEN_WINDOW
<< 8) | (wscale
));
1376 /* Determine a window scaling and initial window to offer. */
1377 extern void tcp_select_initial_window(int __space
, __u32 mss
,
1378 __u32
*rcv_wnd
, __u32
*window_clamp
,
1379 int wscale_ok
, __u8
*rcv_wscale
);
1381 static inline int tcp_win_from_space(int space
)
1383 return sysctl_tcp_adv_win_scale
<=0 ?
1384 (space
>>(-sysctl_tcp_adv_win_scale
)) :
1385 space
- (space
>>sysctl_tcp_adv_win_scale
);
1388 /* Note: caller must be prepared to deal with negative returns */
1389 static inline int tcp_space(const struct sock
*sk
)
1391 return tcp_win_from_space(sk
->sk_rcvbuf
-
1392 atomic_read(&sk
->sk_rmem_alloc
));
1395 static inline int tcp_full_space(const struct sock
*sk
)
1397 return tcp_win_from_space(sk
->sk_rcvbuf
);
1400 static inline void tcp_acceptq_queue(struct sock
*sk
, struct request_sock
*req
,
1403 reqsk_queue_add(&tcp_sk(sk
)->accept_queue
, req
, sk
, child
);
1407 tcp_synq_removed(struct sock
*sk
, struct request_sock
*req
)
1409 if (reqsk_queue_removed(&tcp_sk(sk
)->accept_queue
, req
) == 0)
1410 tcp_delete_keepalive_timer(sk
);
1413 static inline void tcp_synq_added(struct sock
*sk
)
1415 if (reqsk_queue_added(&tcp_sk(sk
)->accept_queue
) == 0)
1416 tcp_reset_keepalive_timer(sk
, TCP_TIMEOUT_INIT
);
1419 static inline int tcp_synq_len(struct sock
*sk
)
1421 return reqsk_queue_len(&tcp_sk(sk
)->accept_queue
);
1424 static inline int tcp_synq_young(struct sock
*sk
)
1426 return reqsk_queue_len_young(&tcp_sk(sk
)->accept_queue
);
1429 static inline int tcp_synq_is_full(struct sock
*sk
)
1431 return reqsk_queue_is_full(&tcp_sk(sk
)->accept_queue
);
1434 static inline void tcp_synq_unlink(struct tcp_sock
*tp
, struct request_sock
*req
,
1435 struct request_sock
**prev
)
1437 reqsk_queue_unlink(&tp
->accept_queue
, req
, prev
);
1440 static inline void tcp_synq_drop(struct sock
*sk
, struct request_sock
*req
,
1441 struct request_sock
**prev
)
1443 tcp_synq_unlink(tcp_sk(sk
), req
, prev
);
1444 tcp_synq_removed(sk
, req
);
1448 static __inline__
void tcp_openreq_init(struct request_sock
*req
,
1449 struct tcp_options_received
*rx_opt
,
1450 struct sk_buff
*skb
)
1452 struct inet_request_sock
*ireq
= inet_rsk(req
);
1454 req
->rcv_wnd
= 0; /* So that tcp_send_synack() knows! */
1455 tcp_rsk(req
)->rcv_isn
= TCP_SKB_CB(skb
)->seq
;
1456 req
->mss
= rx_opt
->mss_clamp
;
1457 req
->ts_recent
= rx_opt
->saw_tstamp
? rx_opt
->rcv_tsval
: 0;
1458 ireq
->tstamp_ok
= rx_opt
->tstamp_ok
;
1459 ireq
->sack_ok
= rx_opt
->sack_ok
;
1460 ireq
->snd_wscale
= rx_opt
->snd_wscale
;
1461 ireq
->wscale_ok
= rx_opt
->wscale_ok
;
1464 ireq
->rmt_port
= skb
->h
.th
->source
;
1467 extern void tcp_enter_memory_pressure(void);
1469 extern void tcp_listen_wlock(void);
1471 /* - We may sleep inside this lock.
1472 * - If sleeping is not required (or called from BH),
1473 * use plain read_(un)lock(&tcp_lhash_lock).
1476 static inline void tcp_listen_lock(void)
1478 /* read_lock synchronizes to candidates to writers */
1479 read_lock(&tcp_lhash_lock
);
1480 atomic_inc(&tcp_lhash_users
);
1481 read_unlock(&tcp_lhash_lock
);
1484 static inline void tcp_listen_unlock(void)
1486 if (atomic_dec_and_test(&tcp_lhash_users
))
1487 wake_up(&tcp_lhash_wait
);
1490 static inline int keepalive_intvl_when(const struct tcp_sock
*tp
)
1492 return tp
->keepalive_intvl
? : sysctl_tcp_keepalive_intvl
;
1495 static inline int keepalive_time_when(const struct tcp_sock
*tp
)
1497 return tp
->keepalive_time
? : sysctl_tcp_keepalive_time
;
1500 static inline int tcp_fin_time(const struct tcp_sock
*tp
)
1502 int fin_timeout
= tp
->linger2
? : sysctl_tcp_fin_timeout
;
1504 if (fin_timeout
< (tp
->rto
<<2) - (tp
->rto
>>1))
1505 fin_timeout
= (tp
->rto
<<2) - (tp
->rto
>>1);
1510 static inline int tcp_paws_check(const struct tcp_options_received
*rx_opt
, int rst
)
1512 if ((s32
)(rx_opt
->rcv_tsval
- rx_opt
->ts_recent
) >= 0)
1514 if (xtime
.tv_sec
>= rx_opt
->ts_recent_stamp
+ TCP_PAWS_24DAYS
)
1517 /* RST segments are not recommended to carry timestamp,
1518 and, if they do, it is recommended to ignore PAWS because
1519 "their cleanup function should take precedence over timestamps."
1520 Certainly, it is mistake. It is necessary to understand the reasons
1521 of this constraint to relax it: if peer reboots, clock may go
1522 out-of-sync and half-open connections will not be reset.
1523 Actually, the problem would be not existing if all
1524 the implementations followed draft about maintaining clock
1525 via reboots. Linux-2.2 DOES NOT!
1527 However, we can relax time bounds for RST segments to MSL.
1529 if (rst
&& xtime
.tv_sec
>= rx_opt
->ts_recent_stamp
+ TCP_PAWS_MSL
)
1534 #define TCP_CHECK_TIMER(sk) do { } while (0)
1536 static inline int tcp_use_frto(const struct sock
*sk
)
1538 const struct tcp_sock
*tp
= tcp_sk(sk
);
1540 /* F-RTO must be activated in sysctl and there must be some
1541 * unsent new data, and the advertised window should allow
1544 return (sysctl_tcp_frto
&& sk
->sk_send_head
&&
1545 !after(TCP_SKB_CB(sk
->sk_send_head
)->end_seq
,
1546 tp
->snd_una
+ tp
->snd_wnd
));
1549 static inline void tcp_mib_init(void)
1552 TCP_ADD_STATS_USER(TCP_MIB_RTOALGORITHM
, 1);
1553 TCP_ADD_STATS_USER(TCP_MIB_RTOMIN
, TCP_RTO_MIN
*1000/HZ
);
1554 TCP_ADD_STATS_USER(TCP_MIB_RTOMAX
, TCP_RTO_MAX
*1000/HZ
);
1555 TCP_ADD_STATS_USER(TCP_MIB_MAXCONN
, -1);
1559 enum tcp_seq_states
{
1560 TCP_SEQ_STATE_LISTENING
,
1561 TCP_SEQ_STATE_OPENREQ
,
1562 TCP_SEQ_STATE_ESTABLISHED
,
1563 TCP_SEQ_STATE_TIME_WAIT
,
1566 struct tcp_seq_afinfo
{
1567 struct module
*owner
;
1570 int (*seq_show
) (struct seq_file
*m
, void *v
);
1571 struct file_operations
*seq_fops
;
1574 struct tcp_iter_state
{
1576 enum tcp_seq_states state
;
1577 struct sock
*syn_wait_sk
;
1578 int bucket
, sbucket
, num
, uid
;
1579 struct seq_operations seq_ops
;
1582 extern int tcp_proc_register(struct tcp_seq_afinfo
*afinfo
);
1583 extern void tcp_proc_unregister(struct tcp_seq_afinfo
*afinfo
);