]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/net/tcp.h
[INET]: Generalise tcp_bind_hash & tcp_inherit_port
[mirror_ubuntu-bionic-kernel.git] / include / net / tcp.h
CommitLineData
1da177e4
LT
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 *
02c30a84 10 * Authors: Ross Biro
1da177e4
LT
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#define TCP_DEBUG 1
22#define FASTRETRANS_DEBUG 1
23
24/* Cancel timers, when they are not required. */
25#undef TCP_CLEAR_TIMERS
26
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>
77d8bf9c 33#include <net/inet_hashtables.h>
1da177e4 34#include <net/checksum.h>
2e6599cb 35#include <net/request_sock.h>
1da177e4
LT
36#include <net/sock.h>
37#include <net/snmp.h>
38#include <net/ip.h>
39#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
40#include <linux/ipv6.h>
41#endif
42#include <linux/seq_file.h>
43
0f7ff927
ACM
44extern 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)
2d8c4ce5 54#define tcp_bucket_cachep (tcp_hashinfo.bind_bucket_cachep)
1da177e4 55
77d8bf9c 56extern int tcp_port_rover;
1da177e4 57
1da177e4
LT
58#if (BITS_PER_LONG == 64)
59#define TCP_ADDRCMP_ALIGN_BYTES 8
60#else
61#define TCP_ADDRCMP_ALIGN_BYTES 4
62#endif
63
64/* This is a TIME_WAIT bucket. It works around the memory consumption
65 * problems of sockets in such a state on heavily loaded servers, but
66 * without violating the protocol specification.
67 */
68struct tcp_tw_bucket {
69 /*
70 * Now struct sock also uses sock_common, so please just
71 * don't add nothing before this first member (__tw_common) --acme
72 */
73 struct sock_common __tw_common;
74#define tw_family __tw_common.skc_family
75#define tw_state __tw_common.skc_state
76#define tw_reuse __tw_common.skc_reuse
77#define tw_bound_dev_if __tw_common.skc_bound_dev_if
78#define tw_node __tw_common.skc_node
79#define tw_bind_node __tw_common.skc_bind_node
80#define tw_refcnt __tw_common.skc_refcnt
81 volatile unsigned char tw_substate;
82 unsigned char tw_rcv_wscale;
83 __u16 tw_sport;
84 /* Socket demultiplex comparisons on incoming packets. */
85 /* these five are in inet_sock */
86 __u32 tw_daddr
87 __attribute__((aligned(TCP_ADDRCMP_ALIGN_BYTES)));
88 __u32 tw_rcv_saddr;
89 __u16 tw_dport;
90 __u16 tw_num;
91 /* And these are ours. */
92 int tw_hashent;
93 int tw_timeout;
94 __u32 tw_rcv_nxt;
95 __u32 tw_snd_nxt;
96 __u32 tw_rcv_wnd;
97 __u32 tw_ts_recent;
98 long tw_ts_recent_stamp;
99 unsigned long tw_ttd;
0f7ff927 100 struct inet_bind_bucket *tw_tb;
1da177e4
LT
101 struct hlist_node tw_death_node;
102#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
103 struct in6_addr tw_v6_daddr;
104 struct in6_addr tw_v6_rcv_saddr;
105 int tw_v6_ipv6only;
106#endif
107};
108
109static __inline__ void tw_add_node(struct tcp_tw_bucket *tw,
110 struct hlist_head *list)
111{
112 hlist_add_head(&tw->tw_node, list);
113}
114
115static __inline__ void tw_add_bind_node(struct tcp_tw_bucket *tw,
116 struct hlist_head *list)
117{
118 hlist_add_head(&tw->tw_bind_node, list);
119}
120
121static inline int tw_dead_hashed(struct tcp_tw_bucket *tw)
122{
123 return tw->tw_death_node.pprev != NULL;
124}
125
126static __inline__ void tw_dead_node_init(struct tcp_tw_bucket *tw)
127{
128 tw->tw_death_node.pprev = NULL;
129}
130
131static __inline__ void __tw_del_dead_node(struct tcp_tw_bucket *tw)
132{
133 __hlist_del(&tw->tw_death_node);
134 tw_dead_node_init(tw);
135}
136
137static __inline__ int tw_del_dead_node(struct tcp_tw_bucket *tw)
138{
139 if (tw_dead_hashed(tw)) {
140 __tw_del_dead_node(tw);
141 return 1;
142 }
143 return 0;
144}
145
146#define tw_for_each(tw, node, head) \
147 hlist_for_each_entry(tw, node, head, tw_node)
148
149#define tw_for_each_inmate(tw, node, jail) \
150 hlist_for_each_entry(tw, node, jail, tw_death_node)
151
152#define tw_for_each_inmate_safe(tw, node, safe, jail) \
153 hlist_for_each_entry_safe(tw, node, safe, jail, tw_death_node)
154
155#define tcptw_sk(__sk) ((struct tcp_tw_bucket *)(__sk))
156
157static inline u32 tcp_v4_rcv_saddr(const struct sock *sk)
158{
159 return likely(sk->sk_state != TCP_TIME_WAIT) ?
160 inet_sk(sk)->rcv_saddr : tcptw_sk(sk)->tw_rcv_saddr;
161}
162
163#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
164static inline struct in6_addr *__tcp_v6_rcv_saddr(const struct sock *sk)
165{
166 return likely(sk->sk_state != TCP_TIME_WAIT) ?
167 &inet6_sk(sk)->rcv_saddr : &tcptw_sk(sk)->tw_v6_rcv_saddr;
168}
169
170static inline struct in6_addr *tcp_v6_rcv_saddr(const struct sock *sk)
171{
172 return sk->sk_family == AF_INET6 ? __tcp_v6_rcv_saddr(sk) : NULL;
173}
174
175#define tcptw_sk_ipv6only(__sk) (tcptw_sk(__sk)->tw_v6_ipv6only)
176
177static inline int tcp_v6_ipv6only(const struct sock *sk)
178{
179 return likely(sk->sk_state != TCP_TIME_WAIT) ?
180 ipv6_only_sock(sk) : tcptw_sk_ipv6only(sk);
181}
182#else
183# define __tcp_v6_rcv_saddr(__sk) NULL
184# define tcp_v6_rcv_saddr(__sk) NULL
185# define tcptw_sk_ipv6only(__sk) 0
186# define tcp_v6_ipv6only(__sk) 0
187#endif
188
189extern kmem_cache_t *tcp_timewait_cachep;
190
191static inline void tcp_tw_put(struct tcp_tw_bucket *tw)
192{
193 if (atomic_dec_and_test(&tw->tw_refcnt)) {
e6848976 194#ifdef SOCK_REFCNT_DEBUG
1da177e4
LT
195 printk(KERN_DEBUG "tw_bucket %p released\n", tw);
196#endif
197 kmem_cache_free(tcp_timewait_cachep, tw);
198 }
199}
200
201extern atomic_t tcp_orphan_count;
202extern int tcp_tw_count;
203extern void tcp_time_wait(struct sock *sk, int state, int timeo);
204extern void tcp_tw_deschedule(struct tcp_tw_bucket *tw);
205
206
207/* Socket demux engine toys. */
208#ifdef __BIG_ENDIAN
209#define TCP_COMBINED_PORTS(__sport, __dport) \
210 (((__u32)(__sport)<<16) | (__u32)(__dport))
211#else /* __LITTLE_ENDIAN */
212#define TCP_COMBINED_PORTS(__sport, __dport) \
213 (((__u32)(__dport)<<16) | (__u32)(__sport))
214#endif
215
216#if (BITS_PER_LONG == 64)
217#ifdef __BIG_ENDIAN
218#define TCP_V4_ADDR_COOKIE(__name, __saddr, __daddr) \
219 __u64 __name = (((__u64)(__saddr))<<32)|((__u64)(__daddr));
220#else /* __LITTLE_ENDIAN */
221#define TCP_V4_ADDR_COOKIE(__name, __saddr, __daddr) \
222 __u64 __name = (((__u64)(__daddr))<<32)|((__u64)(__saddr));
223#endif /* __BIG_ENDIAN */
224#define TCP_IPV4_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif)\
225 (((*((__u64 *)&(inet_sk(__sk)->daddr)))== (__cookie)) && \
226 ((*((__u32 *)&(inet_sk(__sk)->dport)))== (__ports)) && \
227 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
228#define TCP_IPV4_TW_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif)\
229 (((*((__u64 *)&(tcptw_sk(__sk)->tw_daddr))) == (__cookie)) && \
230 ((*((__u32 *)&(tcptw_sk(__sk)->tw_dport))) == (__ports)) && \
231 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
232#else /* 32-bit arch */
233#define TCP_V4_ADDR_COOKIE(__name, __saddr, __daddr)
234#define TCP_IPV4_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif)\
235 ((inet_sk(__sk)->daddr == (__saddr)) && \
236 (inet_sk(__sk)->rcv_saddr == (__daddr)) && \
237 ((*((__u32 *)&(inet_sk(__sk)->dport)))== (__ports)) && \
238 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
239#define TCP_IPV4_TW_MATCH(__sk, __cookie, __saddr, __daddr, __ports, __dif)\
240 ((tcptw_sk(__sk)->tw_daddr == (__saddr)) && \
241 (tcptw_sk(__sk)->tw_rcv_saddr == (__daddr)) && \
242 ((*((__u32 *)&(tcptw_sk(__sk)->tw_dport))) == (__ports)) && \
243 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
244#endif /* 64-bit arch */
245
246#define TCP_IPV6_MATCH(__sk, __saddr, __daddr, __ports, __dif) \
247 (((*((__u32 *)&(inet_sk(__sk)->dport)))== (__ports)) && \
248 ((__sk)->sk_family == AF_INET6) && \
249 ipv6_addr_equal(&inet6_sk(__sk)->daddr, (__saddr)) && \
250 ipv6_addr_equal(&inet6_sk(__sk)->rcv_saddr, (__daddr)) && \
251 (!((__sk)->sk_bound_dev_if) || ((__sk)->sk_bound_dev_if == (__dif))))
252
1da177e4
LT
253#define MAX_TCP_HEADER (128 + MAX_HEADER)
254
255/*
256 * Never offer a window over 32767 without using window scaling. Some
257 * poor stacks do signed 16bit maths!
258 */
259#define MAX_TCP_WINDOW 32767U
260
261/* Minimal accepted MSS. It is (60+60+8) - (20+20). */
262#define TCP_MIN_MSS 88U
263
264/* Minimal RCV_MSS. */
265#define TCP_MIN_RCVMSS 536U
266
267/* After receiving this amount of duplicate ACKs fast retransmit starts. */
268#define TCP_FASTRETRANS_THRESH 3
269
270/* Maximal reordering. */
271#define TCP_MAX_REORDERING 127
272
273/* Maximal number of ACKs sent quickly to accelerate slow-start. */
274#define TCP_MAX_QUICKACKS 16U
275
276/* urg_data states */
277#define TCP_URG_VALID 0x0100
278#define TCP_URG_NOTYET 0x0200
279#define TCP_URG_READ 0x0400
280
281#define TCP_RETR1 3 /*
282 * This is how many retries it does before it
283 * tries to figure out if the gateway is
284 * down. Minimal RFC value is 3; it corresponds
285 * to ~3sec-8min depending on RTO.
286 */
287
288#define TCP_RETR2 15 /*
289 * This should take at least
290 * 90 minutes to time out.
291 * RFC1122 says that the limit is 100 sec.
292 * 15 is ~13-30min depending on RTO.
293 */
294
295#define TCP_SYN_RETRIES 5 /* number of times to retry active opening a
296 * connection: ~180sec is RFC minumum */
297
298#define TCP_SYNACK_RETRIES 5 /* number of times to retry passive opening a
299 * connection: ~180sec is RFC minumum */
300
301
302#define TCP_ORPHAN_RETRIES 7 /* number of times to retry on an orphaned
303 * socket. 7 is ~50sec-16min.
304 */
305
306
307#define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
308 * state, about 60 seconds */
309#define TCP_FIN_TIMEOUT TCP_TIMEWAIT_LEN
310 /* BSD style FIN_WAIT2 deadlock breaker.
311 * It used to be 3min, new value is 60sec,
312 * to combine FIN-WAIT-2 timeout with
313 * TIME-WAIT timer.
314 */
315
316#define TCP_DELACK_MAX ((unsigned)(HZ/5)) /* maximal time to delay before sending an ACK */
317#if HZ >= 100
318#define TCP_DELACK_MIN ((unsigned)(HZ/25)) /* minimal time to delay before sending an ACK */
319#define TCP_ATO_MIN ((unsigned)(HZ/25))
320#else
321#define TCP_DELACK_MIN 4U
322#define TCP_ATO_MIN 4U
323#endif
324#define TCP_RTO_MAX ((unsigned)(120*HZ))
325#define TCP_RTO_MIN ((unsigned)(HZ/5))
326#define TCP_TIMEOUT_INIT ((unsigned)(3*HZ)) /* RFC 1122 initial RTO value */
327
328#define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
329 * for local resources.
330 */
331
332#define TCP_KEEPALIVE_TIME (120*60*HZ) /* two hours */
333#define TCP_KEEPALIVE_PROBES 9 /* Max of 9 keepalive probes */
334#define TCP_KEEPALIVE_INTVL (75*HZ)
335
336#define MAX_TCP_KEEPIDLE 32767
337#define MAX_TCP_KEEPINTVL 32767
338#define MAX_TCP_KEEPCNT 127
339#define MAX_TCP_SYNCNT 127
340
341#define TCP_SYNQ_INTERVAL (HZ/5) /* Period of SYNACK timer */
342#define TCP_SYNQ_HSIZE 512 /* Size of SYNACK hash table */
343
344#define TCP_PAWS_24DAYS (60 * 60 * 24 * 24)
345#define TCP_PAWS_MSL 60 /* Per-host timestamps are invalidated
346 * after this time. It should be equal
347 * (or greater than) TCP_TIMEWAIT_LEN
348 * to provide reliability equal to one
349 * provided by timewait state.
350 */
351#define TCP_PAWS_WINDOW 1 /* Replay window for per-host
352 * timestamps. It must be less than
353 * minimal timewait lifetime.
354 */
355
356#define TCP_TW_RECYCLE_SLOTS_LOG 5
357#define TCP_TW_RECYCLE_SLOTS (1<<TCP_TW_RECYCLE_SLOTS_LOG)
358
359/* If time > 4sec, it is "slow" path, no recycling is required,
360 so that we select tick to get range about 4 seconds.
361 */
362
363#if HZ <= 16 || HZ > 4096
364# error Unsupported: HZ <= 16 or HZ > 4096
365#elif HZ <= 32
366# define TCP_TW_RECYCLE_TICK (5+2-TCP_TW_RECYCLE_SLOTS_LOG)
367#elif HZ <= 64
368# define TCP_TW_RECYCLE_TICK (6+2-TCP_TW_RECYCLE_SLOTS_LOG)
369#elif HZ <= 128
370# define TCP_TW_RECYCLE_TICK (7+2-TCP_TW_RECYCLE_SLOTS_LOG)
371#elif HZ <= 256
372# define TCP_TW_RECYCLE_TICK (8+2-TCP_TW_RECYCLE_SLOTS_LOG)
373#elif HZ <= 512
374# define TCP_TW_RECYCLE_TICK (9+2-TCP_TW_RECYCLE_SLOTS_LOG)
375#elif HZ <= 1024
376# define TCP_TW_RECYCLE_TICK (10+2-TCP_TW_RECYCLE_SLOTS_LOG)
377#elif HZ <= 2048
378# define TCP_TW_RECYCLE_TICK (11+2-TCP_TW_RECYCLE_SLOTS_LOG)
379#else
380# define TCP_TW_RECYCLE_TICK (12+2-TCP_TW_RECYCLE_SLOTS_LOG)
381#endif
1da177e4
LT
382/*
383 * TCP option
384 */
385
386#define TCPOPT_NOP 1 /* Padding */
387#define TCPOPT_EOL 0 /* End of options */
388#define TCPOPT_MSS 2 /* Segment size negotiating */
389#define TCPOPT_WINDOW 3 /* Window scaling */
390#define TCPOPT_SACK_PERM 4 /* SACK Permitted */
391#define TCPOPT_SACK 5 /* SACK Block */
392#define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
393
394/*
395 * TCP option lengths
396 */
397
398#define TCPOLEN_MSS 4
399#define TCPOLEN_WINDOW 3
400#define TCPOLEN_SACK_PERM 2
401#define TCPOLEN_TIMESTAMP 10
402
403/* But this is what stacks really send out. */
404#define TCPOLEN_TSTAMP_ALIGNED 12
405#define TCPOLEN_WSCALE_ALIGNED 4
406#define TCPOLEN_SACKPERM_ALIGNED 4
407#define TCPOLEN_SACK_BASE 2
408#define TCPOLEN_SACK_BASE_ALIGNED 4
409#define TCPOLEN_SACK_PERBLOCK 8
410
411#define TCP_TIME_RETRANS 1 /* Retransmit timer */
412#define TCP_TIME_DACK 2 /* Delayed ack timer */
413#define TCP_TIME_PROBE0 3 /* Zero window probe timer */
414#define TCP_TIME_KEEPOPEN 4 /* Keepalive timer */
415
416/* Flags in tp->nonagle */
417#define TCP_NAGLE_OFF 1 /* Nagle's algo is disabled */
418#define TCP_NAGLE_CORK 2 /* Socket is corked */
419#define TCP_NAGLE_PUSH 4 /* Cork is overriden for already queued data */
420
421/* sysctl variables for tcp */
1da177e4
LT
422extern int sysctl_tcp_timestamps;
423extern int sysctl_tcp_window_scaling;
424extern int sysctl_tcp_sack;
425extern int sysctl_tcp_fin_timeout;
426extern int sysctl_tcp_tw_recycle;
427extern int sysctl_tcp_keepalive_time;
428extern int sysctl_tcp_keepalive_probes;
429extern int sysctl_tcp_keepalive_intvl;
430extern int sysctl_tcp_syn_retries;
431extern int sysctl_tcp_synack_retries;
432extern int sysctl_tcp_retries1;
433extern int sysctl_tcp_retries2;
434extern int sysctl_tcp_orphan_retries;
435extern int sysctl_tcp_syncookies;
436extern int sysctl_tcp_retrans_collapse;
437extern int sysctl_tcp_stdurg;
438extern int sysctl_tcp_rfc1337;
439extern int sysctl_tcp_abort_on_overflow;
440extern int sysctl_tcp_max_orphans;
441extern int sysctl_tcp_max_tw_buckets;
442extern int sysctl_tcp_fack;
443extern int sysctl_tcp_reordering;
444extern int sysctl_tcp_ecn;
445extern int sysctl_tcp_dsack;
446extern int sysctl_tcp_mem[3];
447extern int sysctl_tcp_wmem[3];
448extern int sysctl_tcp_rmem[3];
449extern int sysctl_tcp_app_win;
450extern int sysctl_tcp_adv_win_scale;
451extern int sysctl_tcp_tw_reuse;
452extern int sysctl_tcp_frto;
453extern int sysctl_tcp_low_latency;
1da177e4 454extern int sysctl_tcp_nometrics_save;
1da177e4
LT
455extern int sysctl_tcp_moderate_rcvbuf;
456extern int sysctl_tcp_tso_win_divisor;
457
458extern atomic_t tcp_memory_allocated;
459extern atomic_t tcp_sockets_allocated;
460extern int tcp_memory_pressure;
461
1da177e4
LT
462#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
463#define TCP_INET_FAMILY(fam) ((fam) == AF_INET)
464#else
465#define TCP_INET_FAMILY(fam) 1
466#endif
467
468/*
469 * Pointers to address related TCP functions
470 * (i.e. things that depend on the address family)
471 */
472
473struct tcp_func {
474 int (*queue_xmit) (struct sk_buff *skb,
475 int ipfragok);
476
477 void (*send_check) (struct sock *sk,
478 struct tcphdr *th,
479 int len,
480 struct sk_buff *skb);
481
482 int (*rebuild_header) (struct sock *sk);
483
484 int (*conn_request) (struct sock *sk,
485 struct sk_buff *skb);
486
487 struct sock * (*syn_recv_sock) (struct sock *sk,
488 struct sk_buff *skb,
60236fdd 489 struct request_sock *req,
1da177e4
LT
490 struct dst_entry *dst);
491
492 int (*remember_stamp) (struct sock *sk);
493
494 __u16 net_header_len;
495
496 int (*setsockopt) (struct sock *sk,
497 int level,
498 int optname,
499 char __user *optval,
500 int optlen);
501
502 int (*getsockopt) (struct sock *sk,
503 int level,
504 int optname,
505 char __user *optval,
506 int __user *optlen);
507
508
509 void (*addr2sockaddr) (struct sock *sk,
510 struct sockaddr *);
511
512 int sockaddr_len;
513};
514
515/*
516 * The next routines deal with comparing 32 bit unsigned ints
517 * and worry about wraparound (automatic with unsigned arithmetic).
518 */
519
520static inline int before(__u32 seq1, __u32 seq2)
521{
522 return (__s32)(seq1-seq2) < 0;
523}
524
525static inline int after(__u32 seq1, __u32 seq2)
526{
527 return (__s32)(seq2-seq1) < 0;
528}
529
530
531/* is s2<=s1<=s3 ? */
532static inline int between(__u32 seq1, __u32 seq2, __u32 seq3)
533{
534 return seq3 - seq2 >= seq1 - seq2;
535}
536
537
538extern struct proto tcp_prot;
539
540DECLARE_SNMP_STAT(struct tcp_mib, tcp_statistics);
541#define TCP_INC_STATS(field) SNMP_INC_STATS(tcp_statistics, field)
542#define TCP_INC_STATS_BH(field) SNMP_INC_STATS_BH(tcp_statistics, field)
543#define TCP_INC_STATS_USER(field) SNMP_INC_STATS_USER(tcp_statistics, field)
544#define TCP_DEC_STATS(field) SNMP_DEC_STATS(tcp_statistics, field)
545#define TCP_ADD_STATS_BH(field, val) SNMP_ADD_STATS_BH(tcp_statistics, field, val)
546#define TCP_ADD_STATS_USER(field, val) SNMP_ADD_STATS_USER(tcp_statistics, field, val)
547
1da177e4
LT
548extern void tcp_v4_err(struct sk_buff *skb, u32);
549
550extern void tcp_shutdown (struct sock *sk, int how);
551
552extern int tcp_v4_rcv(struct sk_buff *skb);
553
554extern int tcp_v4_remember_stamp(struct sock *sk);
555
556extern int tcp_v4_tw_remember_stamp(struct tcp_tw_bucket *tw);
557
558extern int tcp_sendmsg(struct kiocb *iocb, struct sock *sk,
559 struct msghdr *msg, size_t size);
560extern ssize_t tcp_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags);
561
562extern int tcp_ioctl(struct sock *sk,
563 int cmd,
564 unsigned long arg);
565
566extern int tcp_rcv_state_process(struct sock *sk,
567 struct sk_buff *skb,
568 struct tcphdr *th,
569 unsigned len);
570
571extern int tcp_rcv_established(struct sock *sk,
572 struct sk_buff *skb,
573 struct tcphdr *th,
574 unsigned len);
575
576extern void tcp_rcv_space_adjust(struct sock *sk);
577
578enum tcp_ack_state_t
579{
580 TCP_ACK_SCHED = 1,
581 TCP_ACK_TIMER = 2,
582 TCP_ACK_PUSHED= 4
583};
584
585static inline void tcp_schedule_ack(struct tcp_sock *tp)
586{
587 tp->ack.pending |= TCP_ACK_SCHED;
588}
589
590static inline int tcp_ack_scheduled(struct tcp_sock *tp)
591{
592 return tp->ack.pending&TCP_ACK_SCHED;
593}
594
fc6415bc 595static __inline__ void tcp_dec_quickack_mode(struct tcp_sock *tp, unsigned int pkts)
1da177e4 596{
fc6415bc
DM
597 if (tp->ack.quick) {
598 if (pkts >= tp->ack.quick) {
599 tp->ack.quick = 0;
600
601 /* Leaving quickack mode we deflate ATO. */
602 tp->ack.ato = TCP_ATO_MIN;
603 } else
604 tp->ack.quick -= pkts;
1da177e4
LT
605 }
606}
607
608extern void tcp_enter_quickack_mode(struct tcp_sock *tp);
609
610static __inline__ void tcp_delack_init(struct tcp_sock *tp)
611{
612 memset(&tp->ack, 0, sizeof(tp->ack));
613}
614
615static inline void tcp_clear_options(struct tcp_options_received *rx_opt)
616{
617 rx_opt->tstamp_ok = rx_opt->sack_ok = rx_opt->wscale_ok = rx_opt->snd_wscale = 0;
618}
619
620enum tcp_tw_status
621{
622 TCP_TW_SUCCESS = 0,
623 TCP_TW_RST = 1,
624 TCP_TW_ACK = 2,
625 TCP_TW_SYN = 3
626};
627
628
629extern enum tcp_tw_status tcp_timewait_state_process(struct tcp_tw_bucket *tw,
630 struct sk_buff *skb,
631 struct tcphdr *th,
632 unsigned len);
633
634extern struct sock * tcp_check_req(struct sock *sk,struct sk_buff *skb,
60236fdd
ACM
635 struct request_sock *req,
636 struct request_sock **prev);
1da177e4
LT
637extern int tcp_child_process(struct sock *parent,
638 struct sock *child,
639 struct sk_buff *skb);
640extern void tcp_enter_frto(struct sock *sk);
641extern void tcp_enter_loss(struct sock *sk, int how);
642extern void tcp_clear_retrans(struct tcp_sock *tp);
643extern void tcp_update_metrics(struct sock *sk);
644
645extern void tcp_close(struct sock *sk,
646 long timeout);
647extern struct sock * tcp_accept(struct sock *sk, int flags, int *err);
648extern unsigned int tcp_poll(struct file * file, struct socket *sock, struct poll_table_struct *wait);
649
650extern int tcp_getsockopt(struct sock *sk, int level,
651 int optname,
652 char __user *optval,
653 int __user *optlen);
654extern int tcp_setsockopt(struct sock *sk, int level,
655 int optname, char __user *optval,
656 int optlen);
657extern void tcp_set_keepalive(struct sock *sk, int val);
658extern int tcp_recvmsg(struct kiocb *iocb, struct sock *sk,
659 struct msghdr *msg,
660 size_t len, int nonblock,
661 int flags, int *addr_len);
662
663extern int tcp_listen_start(struct sock *sk);
664
665extern void tcp_parse_options(struct sk_buff *skb,
666 struct tcp_options_received *opt_rx,
667 int estab);
668
669/*
670 * TCP v4 functions exported for the inet6 API
671 */
672
1da177e4
LT
673extern void tcp_v4_send_check(struct sock *sk,
674 struct tcphdr *th, int len,
675 struct sk_buff *skb);
676
677extern int tcp_v4_conn_request(struct sock *sk,
678 struct sk_buff *skb);
679
680extern struct sock * tcp_create_openreq_child(struct sock *sk,
60236fdd 681 struct request_sock *req,
1da177e4
LT
682 struct sk_buff *skb);
683
684extern struct sock * tcp_v4_syn_recv_sock(struct sock *sk,
685 struct sk_buff *skb,
60236fdd 686 struct request_sock *req,
1da177e4
LT
687 struct dst_entry *dst);
688
689extern int tcp_v4_do_rcv(struct sock *sk,
690 struct sk_buff *skb);
691
692extern int tcp_v4_connect(struct sock *sk,
693 struct sockaddr *uaddr,
694 int addr_len);
695
696extern int tcp_connect(struct sock *sk);
697
698extern struct sk_buff * tcp_make_synack(struct sock *sk,
699 struct dst_entry *dst,
60236fdd 700 struct request_sock *req);
1da177e4
LT
701
702extern int tcp_disconnect(struct sock *sk, int flags);
703
704extern void tcp_unhash(struct sock *sk);
705
706extern int tcp_v4_hash_connecting(struct sock *sk);
707
708
709/* From syncookies.c */
710extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
711 struct ip_options *opt);
712extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb,
713 __u16 *mss);
714
715/* tcp_output.c */
716
f6302d1d 717extern void __tcp_push_pending_frames(struct sock *sk, struct tcp_sock *tp,
a2e2a59c 718 unsigned int cur_mss, int nonagle);
f6302d1d 719extern int tcp_may_send_now(struct sock *sk, struct tcp_sock *tp);
1da177e4
LT
720extern int tcp_retransmit_skb(struct sock *, struct sk_buff *);
721extern void tcp_xmit_retransmit_queue(struct sock *);
722extern void tcp_simple_retransmit(struct sock *);
723extern int tcp_trim_head(struct sock *, struct sk_buff *, u32);
724
725extern void tcp_send_probe0(struct sock *);
726extern void tcp_send_partial(struct sock *);
727extern int tcp_write_wakeup(struct sock *);
728extern void tcp_send_fin(struct sock *sk);
86a76caf
VF
729extern void tcp_send_active_reset(struct sock *sk,
730 unsigned int __nocast priority);
1da177e4 731extern int tcp_send_synack(struct sock *);
c1b4a7e6 732extern void tcp_push_one(struct sock *, unsigned int mss_now);
1da177e4
LT
733extern void tcp_send_ack(struct sock *sk);
734extern void tcp_send_delayed_ack(struct sock *sk);
735
a762a980
DM
736/* tcp_input.c */
737extern void tcp_cwnd_application_limited(struct sock *sk);
738
1da177e4
LT
739/* tcp_timer.c */
740extern void tcp_init_xmit_timers(struct sock *);
741extern void tcp_clear_xmit_timers(struct sock *);
742
743extern void tcp_delete_keepalive_timer(struct sock *);
744extern void tcp_reset_keepalive_timer(struct sock *, unsigned long);
745extern unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
746extern unsigned int tcp_current_mss(struct sock *sk, int large);
747
748#ifdef TCP_DEBUG
749extern const char tcp_timer_bug_msg[];
750#endif
751
752/* tcp_diag.c */
753extern void tcp_get_info(struct sock *, struct tcp_info *);
754
755/* Read 'sendfile()'-style from a TCP socket */
756typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
757 unsigned int, size_t);
758extern int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
759 sk_read_actor_t recv_actor);
760
761static inline void tcp_clear_xmit_timer(struct sock *sk, int what)
762{
763 struct tcp_sock *tp = tcp_sk(sk);
764
765 switch (what) {
766 case TCP_TIME_RETRANS:
767 case TCP_TIME_PROBE0:
768 tp->pending = 0;
769
770#ifdef TCP_CLEAR_TIMERS
771 sk_stop_timer(sk, &tp->retransmit_timer);
772#endif
773 break;
774 case TCP_TIME_DACK:
775 tp->ack.blocked = 0;
776 tp->ack.pending = 0;
777
778#ifdef TCP_CLEAR_TIMERS
779 sk_stop_timer(sk, &tp->delack_timer);
780#endif
781 break;
782 default:
783#ifdef TCP_DEBUG
784 printk(tcp_timer_bug_msg);
785#endif
786 return;
787 };
788
789}
790
791/*
792 * Reset the retransmission timer
793 */
794static inline void tcp_reset_xmit_timer(struct sock *sk, int what, unsigned long when)
795{
796 struct tcp_sock *tp = tcp_sk(sk);
797
798 if (when > TCP_RTO_MAX) {
799#ifdef TCP_DEBUG
800 printk(KERN_DEBUG "reset_xmit_timer sk=%p %d when=0x%lx, caller=%p\n", sk, what, when, current_text_addr());
801#endif
802 when = TCP_RTO_MAX;
803 }
804
805 switch (what) {
806 case TCP_TIME_RETRANS:
807 case TCP_TIME_PROBE0:
808 tp->pending = what;
809 tp->timeout = jiffies+when;
810 sk_reset_timer(sk, &tp->retransmit_timer, tp->timeout);
811 break;
812
813 case TCP_TIME_DACK:
814 tp->ack.pending |= TCP_ACK_TIMER;
815 tp->ack.timeout = jiffies+when;
816 sk_reset_timer(sk, &tp->delack_timer, tp->ack.timeout);
817 break;
818
819 default:
820#ifdef TCP_DEBUG
821 printk(tcp_timer_bug_msg);
822#endif
823 return;
824 };
825}
826
827/* Initialize RCV_MSS value.
828 * RCV_MSS is an our guess about MSS used by the peer.
829 * We haven't any direct information about the MSS.
830 * It's better to underestimate the RCV_MSS rather than overestimate.
831 * Overestimations make us ACKing less frequently than needed.
832 * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
833 */
834
835static inline void tcp_initialize_rcv_mss(struct sock *sk)
836{
837 struct tcp_sock *tp = tcp_sk(sk);
c1b4a7e6 838 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
1da177e4
LT
839
840 hint = min(hint, tp->rcv_wnd/2);
841 hint = min(hint, TCP_MIN_RCVMSS);
842 hint = max(hint, TCP_MIN_MSS);
843
844 tp->ack.rcv_mss = hint;
845}
846
847static __inline__ void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
848{
849 tp->pred_flags = htonl((tp->tcp_header_len << 26) |
850 ntohl(TCP_FLAG_ACK) |
851 snd_wnd);
852}
853
854static __inline__ void tcp_fast_path_on(struct tcp_sock *tp)
855{
856 __tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
857}
858
859static inline void tcp_fast_path_check(struct sock *sk, struct tcp_sock *tp)
860{
b03efcfb 861 if (skb_queue_empty(&tp->out_of_order_queue) &&
1da177e4
LT
862 tp->rcv_wnd &&
863 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
864 !tp->urg_data)
865 tcp_fast_path_on(tp);
866}
867
868/* Compute the actual receive window we are currently advertising.
869 * Rcv_nxt can be after the window if our peer push more data
870 * than the offered window.
871 */
872static __inline__ u32 tcp_receive_window(const struct tcp_sock *tp)
873{
874 s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
875
876 if (win < 0)
877 win = 0;
878 return (u32) win;
879}
880
881/* Choose a new window, without checks for shrinking, and without
882 * scaling applied to the result. The caller does these things
883 * if necessary. This is a "raw" window selection.
884 */
885extern u32 __tcp_select_window(struct sock *sk);
886
887/* TCP timestamps are only 32-bits, this causes a slight
888 * complication on 64-bit systems since we store a snapshot
889 * of jiffies in the buffer control blocks below. We decidely
890 * only use of the low 32-bits of jiffies and hide the ugly
891 * casts with the following macro.
892 */
893#define tcp_time_stamp ((__u32)(jiffies))
894
895/* This is what the send packet queueing engine uses to pass
896 * TCP per-packet control information to the transmission
897 * code. We also store the host-order sequence numbers in
898 * here too. This is 36 bytes on 32-bit architectures,
899 * 40 bytes on 64-bit machines, if this grows please adjust
900 * skbuff.h:skbuff->cb[xxx] size appropriately.
901 */
902struct tcp_skb_cb {
903 union {
904 struct inet_skb_parm h4;
905#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
906 struct inet6_skb_parm h6;
907#endif
908 } header; /* For incoming frames */
909 __u32 seq; /* Starting sequence number */
910 __u32 end_seq; /* SEQ + FIN + SYN + datalen */
911 __u32 when; /* used to compute rtt's */
912 __u8 flags; /* TCP header flags. */
913
914 /* NOTE: These must match up to the flags byte in a
915 * real TCP header.
916 */
917#define TCPCB_FLAG_FIN 0x01
918#define TCPCB_FLAG_SYN 0x02
919#define TCPCB_FLAG_RST 0x04
920#define TCPCB_FLAG_PSH 0x08
921#define TCPCB_FLAG_ACK 0x10
922#define TCPCB_FLAG_URG 0x20
923#define TCPCB_FLAG_ECE 0x40
924#define TCPCB_FLAG_CWR 0x80
925
926 __u8 sacked; /* State flags for SACK/FACK. */
927#define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block */
928#define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted */
929#define TCPCB_LOST 0x04 /* SKB is lost */
930#define TCPCB_TAGBITS 0x07 /* All tag bits */
931
932#define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */
933#define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
934
935#define TCPCB_URG 0x20 /* Urgent pointer advenced here */
936
937#define TCPCB_AT_TAIL (TCPCB_URG)
938
939 __u16 urg_ptr; /* Valid w/URG flags is set. */
940 __u32 ack_seq; /* Sequence number ACK'd */
941};
942
943#define TCP_SKB_CB(__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0]))
944
945#include <net/tcp_ecn.h>
946
947/* Due to TSO, an SKB can be composed of multiple actual
948 * packets. To keep these tracked properly, we use this.
949 */
950static inline int tcp_skb_pcount(const struct sk_buff *skb)
951{
952 return skb_shinfo(skb)->tso_segs;
953}
954
955/* This is valid iff tcp_skb_pcount() > 1. */
956static inline int tcp_skb_mss(const struct sk_buff *skb)
957{
958 return skb_shinfo(skb)->tso_size;
959}
960
961static inline void tcp_dec_pcount_approx(__u32 *count,
962 const struct sk_buff *skb)
963{
964 if (*count) {
965 *count -= tcp_skb_pcount(skb);
966 if ((int)*count < 0)
967 *count = 0;
968 }
969}
970
971static inline void tcp_packets_out_inc(struct sock *sk,
972 struct tcp_sock *tp,
973 const struct sk_buff *skb)
974{
975 int orig = tp->packets_out;
976
977 tp->packets_out += tcp_skb_pcount(skb);
978 if (!orig)
979 tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
980}
981
982static inline void tcp_packets_out_dec(struct tcp_sock *tp,
983 const struct sk_buff *skb)
984{
985 tp->packets_out -= tcp_skb_pcount(skb);
986}
987
317a76f9
SH
988/* Events passed to congestion control interface */
989enum tcp_ca_event {
990 CA_EVENT_TX_START, /* first transmit when no packets in flight */
991 CA_EVENT_CWND_RESTART, /* congestion window restart */
992 CA_EVENT_COMPLETE_CWR, /* end of congestion recovery */
993 CA_EVENT_FRTO, /* fast recovery timeout */
994 CA_EVENT_LOSS, /* loss timeout */
995 CA_EVENT_FAST_ACK, /* in sequence ack */
996 CA_EVENT_SLOW_ACK, /* other ack */
997};
998
999/*
1000 * Interface for adding new TCP congestion control handlers
1001 */
1002#define TCP_CA_NAME_MAX 16
1003struct tcp_congestion_ops {
1004 struct list_head list;
1005
1006 /* initialize private data (optional) */
1007 void (*init)(struct tcp_sock *tp);
1008 /* cleanup private data (optional) */
1009 void (*release)(struct tcp_sock *tp);
1010
1011 /* return slow start threshold (required) */
1012 u32 (*ssthresh)(struct tcp_sock *tp);
1013 /* lower bound for congestion window (optional) */
1014 u32 (*min_cwnd)(struct tcp_sock *tp);
1015 /* do new cwnd calculation (required) */
1016 void (*cong_avoid)(struct tcp_sock *tp, u32 ack,
1017 u32 rtt, u32 in_flight, int good_ack);
1018 /* round trip time sample per acked packet (optional) */
1019 void (*rtt_sample)(struct tcp_sock *tp, u32 usrtt);
1020 /* call before changing ca_state (optional) */
1021 void (*set_state)(struct tcp_sock *tp, u8 new_state);
1022 /* call when cwnd event occurs (optional) */
1023 void (*cwnd_event)(struct tcp_sock *tp, enum tcp_ca_event ev);
1024 /* new value of cwnd after loss (optional) */
1025 u32 (*undo_cwnd)(struct tcp_sock *tp);
1026 /* hook for packet ack accounting (optional) */
1027 void (*pkts_acked)(struct tcp_sock *tp, u32 num_acked);
1028 /* get info for tcp_diag (optional) */
1029 void (*get_info)(struct tcp_sock *tp, u32 ext, struct sk_buff *skb);
1030
1031 char name[TCP_CA_NAME_MAX];
1032 struct module *owner;
1033};
1034
1035extern int tcp_register_congestion_control(struct tcp_congestion_ops *type);
1036extern void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
1037
1038extern void tcp_init_congestion_control(struct tcp_sock *tp);
1039extern void tcp_cleanup_congestion_control(struct tcp_sock *tp);
1040extern int tcp_set_default_congestion_control(const char *name);
1041extern void tcp_get_default_congestion_control(char *name);
5f8ef48d 1042extern int tcp_set_congestion_control(struct tcp_sock *tp, const char *name);
317a76f9 1043
5f8ef48d 1044extern struct tcp_congestion_ops tcp_init_congestion_ops;
317a76f9
SH
1045extern u32 tcp_reno_ssthresh(struct tcp_sock *tp);
1046extern void tcp_reno_cong_avoid(struct tcp_sock *tp, u32 ack,
1047 u32 rtt, u32 in_flight, int flag);
1048extern u32 tcp_reno_min_cwnd(struct tcp_sock *tp);
a8acfbac 1049extern struct tcp_congestion_ops tcp_reno;
317a76f9
SH
1050
1051static inline void tcp_set_ca_state(struct tcp_sock *tp, u8 ca_state)
1052{
1053 if (tp->ca_ops->set_state)
1054 tp->ca_ops->set_state(tp, ca_state);
1055 tp->ca_state = ca_state;
1056}
1057
1058static inline void tcp_ca_event(struct tcp_sock *tp, enum tcp_ca_event event)
1059{
1060 if (tp->ca_ops->cwnd_event)
1061 tp->ca_ops->cwnd_event(tp, event);
1062}
1063
1da177e4
LT
1064/* This determines how many packets are "in the network" to the best
1065 * of our knowledge. In many cases it is conservative, but where
1066 * detailed information is available from the receiver (via SACK
1067 * blocks etc.) we can make more aggressive calculations.
1068 *
1069 * Use this for decisions involving congestion control, use just
1070 * tp->packets_out to determine if the send queue is empty or not.
1071 *
1072 * Read this equation as:
1073 *
1074 * "Packets sent once on transmission queue" MINUS
1075 * "Packets left network, but not honestly ACKed yet" PLUS
1076 * "Packets fast retransmitted"
1077 */
1078static __inline__ unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
1079{
1080 return (tp->packets_out - tp->left_out + tp->retrans_out);
1081}
1082
1da177e4
LT
1083/* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
1084 * The exception is rate halving phase, when cwnd is decreasing towards
1085 * ssthresh.
1086 */
1087static inline __u32 tcp_current_ssthresh(struct tcp_sock *tp)
1088{
1089 if ((1<<tp->ca_state)&(TCPF_CA_CWR|TCPF_CA_Recovery))
1090 return tp->snd_ssthresh;
1091 else
1092 return max(tp->snd_ssthresh,
1093 ((tp->snd_cwnd >> 1) +
1094 (tp->snd_cwnd >> 2)));
1095}
1096
1097static inline void tcp_sync_left_out(struct tcp_sock *tp)
1098{
1099 if (tp->rx_opt.sack_ok &&
1100 (tp->sacked_out >= tp->packets_out - tp->lost_out))
1101 tp->sacked_out = tp->packets_out - tp->lost_out;
1102 tp->left_out = tp->sacked_out + tp->lost_out;
1103}
1104
d1b04c08 1105/* Set slow start threshold and cwnd not falling to slow start */
1da177e4
LT
1106static inline void __tcp_enter_cwr(struct tcp_sock *tp)
1107{
1108 tp->undo_marker = 0;
317a76f9 1109 tp->snd_ssthresh = tp->ca_ops->ssthresh(tp);
1da177e4
LT
1110 tp->snd_cwnd = min(tp->snd_cwnd,
1111 tcp_packets_in_flight(tp) + 1U);
1112 tp->snd_cwnd_cnt = 0;
1113 tp->high_seq = tp->snd_nxt;
1114 tp->snd_cwnd_stamp = tcp_time_stamp;
1115 TCP_ECN_queue_cwr(tp);
1116}
1117
1118static inline void tcp_enter_cwr(struct tcp_sock *tp)
1119{
1120 tp->prior_ssthresh = 0;
1121 if (tp->ca_state < TCP_CA_CWR) {
1122 __tcp_enter_cwr(tp);
1123 tcp_set_ca_state(tp, TCP_CA_CWR);
1124 }
1125}
1126
1127extern __u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst);
1128
1129/* Slow start with delack produces 3 packets of burst, so that
1130 * it is safe "de facto".
1131 */
1132static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
1133{
1134 return 3;
1135}
1136
1da177e4
LT
1137static __inline__ void tcp_minshall_update(struct tcp_sock *tp, int mss,
1138 const struct sk_buff *skb)
1139{
1140 if (skb->len < mss)
1141 tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
1142}
1143
1da177e4
LT
1144static __inline__ void tcp_check_probe_timer(struct sock *sk, struct tcp_sock *tp)
1145{
1146 if (!tp->packets_out && !tp->pending)
1147 tcp_reset_xmit_timer(sk, TCP_TIME_PROBE0, tp->rto);
1148}
1149
1da177e4
LT
1150static __inline__ void tcp_push_pending_frames(struct sock *sk,
1151 struct tcp_sock *tp)
1152{
1153 __tcp_push_pending_frames(sk, tp, tcp_current_mss(sk, 1), tp->nonagle);
1154}
1155
1da177e4
LT
1156static __inline__ void tcp_init_wl(struct tcp_sock *tp, u32 ack, u32 seq)
1157{
1158 tp->snd_wl1 = seq;
1159}
1160
1161static __inline__ void tcp_update_wl(struct tcp_sock *tp, u32 ack, u32 seq)
1162{
1163 tp->snd_wl1 = seq;
1164}
1165
1166extern void tcp_destroy_sock(struct sock *sk);
1167
1168
1169/*
1170 * Calculate(/check) TCP checksum
1171 */
1172static __inline__ u16 tcp_v4_check(struct tcphdr *th, int len,
1173 unsigned long saddr, unsigned long daddr,
1174 unsigned long base)
1175{
1176 return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
1177}
1178
1179static __inline__ int __tcp_checksum_complete(struct sk_buff *skb)
1180{
1181 return (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
1182}
1183
1184static __inline__ int tcp_checksum_complete(struct sk_buff *skb)
1185{
1186 return skb->ip_summed != CHECKSUM_UNNECESSARY &&
1187 __tcp_checksum_complete(skb);
1188}
1189
1190/* Prequeue for VJ style copy to user, combined with checksumming. */
1191
1192static __inline__ void tcp_prequeue_init(struct tcp_sock *tp)
1193{
1194 tp->ucopy.task = NULL;
1195 tp->ucopy.len = 0;
1196 tp->ucopy.memory = 0;
1197 skb_queue_head_init(&tp->ucopy.prequeue);
1198}
1199
1200/* Packet is added to VJ-style prequeue for processing in process
1201 * context, if a reader task is waiting. Apparently, this exciting
1202 * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93)
1203 * failed somewhere. Latency? Burstiness? Well, at least now we will
1204 * see, why it failed. 8)8) --ANK
1205 *
1206 * NOTE: is this not too big to inline?
1207 */
1208static __inline__ int tcp_prequeue(struct sock *sk, struct sk_buff *skb)
1209{
1210 struct tcp_sock *tp = tcp_sk(sk);
1211
1212 if (!sysctl_tcp_low_latency && tp->ucopy.task) {
1213 __skb_queue_tail(&tp->ucopy.prequeue, skb);
1214 tp->ucopy.memory += skb->truesize;
1215 if (tp->ucopy.memory > sk->sk_rcvbuf) {
1216 struct sk_buff *skb1;
1217
1218 BUG_ON(sock_owned_by_user(sk));
1219
1220 while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) {
1221 sk->sk_backlog_rcv(sk, skb1);
1222 NET_INC_STATS_BH(LINUX_MIB_TCPPREQUEUEDROPPED);
1223 }
1224
1225 tp->ucopy.memory = 0;
1226 } else if (skb_queue_len(&tp->ucopy.prequeue) == 1) {
1227 wake_up_interruptible(sk->sk_sleep);
1228 if (!tcp_ack_scheduled(tp))
1229 tcp_reset_xmit_timer(sk, TCP_TIME_DACK, (3*TCP_RTO_MIN)/4);
1230 }
1231 return 1;
1232 }
1233 return 0;
1234}
1235
1236
1237#undef STATE_TRACE
1238
1239#ifdef STATE_TRACE
1240static const char *statename[]={
1241 "Unused","Established","Syn Sent","Syn Recv",
1242 "Fin Wait 1","Fin Wait 2","Time Wait", "Close",
1243 "Close Wait","Last ACK","Listen","Closing"
1244};
1245#endif
1246
1247static __inline__ void tcp_set_state(struct sock *sk, int state)
1248{
1249 int oldstate = sk->sk_state;
1250
1251 switch (state) {
1252 case TCP_ESTABLISHED:
1253 if (oldstate != TCP_ESTABLISHED)
1254 TCP_INC_STATS(TCP_MIB_CURRESTAB);
1255 break;
1256
1257 case TCP_CLOSE:
1258 if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED)
1259 TCP_INC_STATS(TCP_MIB_ESTABRESETS);
1260
1261 sk->sk_prot->unhash(sk);
a55ebcc4 1262 if (inet_sk(sk)->bind_hash &&
1da177e4 1263 !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
2d8c4ce5 1264 inet_put_port(&tcp_hashinfo, sk);
1da177e4
LT
1265 /* fall through */
1266 default:
1267 if (oldstate==TCP_ESTABLISHED)
1268 TCP_DEC_STATS(TCP_MIB_CURRESTAB);
1269 }
1270
1271 /* Change state AFTER socket is unhashed to avoid closed
1272 * socket sitting in hash tables.
1273 */
1274 sk->sk_state = state;
1275
1276#ifdef STATE_TRACE
1277 SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n",sk, statename[oldstate],statename[state]);
1278#endif
1279}
1280
1281static __inline__ void tcp_done(struct sock *sk)
1282{
1283 tcp_set_state(sk, TCP_CLOSE);
1284 tcp_clear_xmit_timers(sk);
1285
1286 sk->sk_shutdown = SHUTDOWN_MASK;
1287
1288 if (!sock_flag(sk, SOCK_DEAD))
1289 sk->sk_state_change(sk);
1290 else
1291 tcp_destroy_sock(sk);
1292}
1293
1294static __inline__ void tcp_sack_reset(struct tcp_options_received *rx_opt)
1295{
1296 rx_opt->dsack = 0;
1297 rx_opt->eff_sacks = 0;
1298 rx_opt->num_sacks = 0;
1299}
1300
1301static __inline__ void tcp_build_and_update_options(__u32 *ptr, struct tcp_sock *tp, __u32 tstamp)
1302{
1303 if (tp->rx_opt.tstamp_ok) {
1304 *ptr++ = __constant_htonl((TCPOPT_NOP << 24) |
1305 (TCPOPT_NOP << 16) |
1306 (TCPOPT_TIMESTAMP << 8) |
1307 TCPOLEN_TIMESTAMP);
1308 *ptr++ = htonl(tstamp);
1309 *ptr++ = htonl(tp->rx_opt.ts_recent);
1310 }
1311 if (tp->rx_opt.eff_sacks) {
1312 struct tcp_sack_block *sp = tp->rx_opt.dsack ? tp->duplicate_sack : tp->selective_acks;
1313 int this_sack;
1314
1315 *ptr++ = __constant_htonl((TCPOPT_NOP << 24) |
1316 (TCPOPT_NOP << 16) |
1317 (TCPOPT_SACK << 8) |
1318 (TCPOLEN_SACK_BASE +
1319 (tp->rx_opt.eff_sacks * TCPOLEN_SACK_PERBLOCK)));
1320 for(this_sack = 0; this_sack < tp->rx_opt.eff_sacks; this_sack++) {
1321 *ptr++ = htonl(sp[this_sack].start_seq);
1322 *ptr++ = htonl(sp[this_sack].end_seq);
1323 }
1324 if (tp->rx_opt.dsack) {
1325 tp->rx_opt.dsack = 0;
1326 tp->rx_opt.eff_sacks--;
1327 }
1328 }
1329}
1330
1331/* Construct a tcp options header for a SYN or SYN_ACK packet.
1332 * If this is every changed make sure to change the definition of
1333 * MAX_SYN_SIZE to match the new maximum number of options that you
1334 * can generate.
1335 */
1336static inline void tcp_syn_build_options(__u32 *ptr, int mss, int ts, int sack,
1337 int offer_wscale, int wscale, __u32 tstamp, __u32 ts_recent)
1338{
1339 /* We always get an MSS option.
1340 * The option bytes which will be seen in normal data
1341 * packets should timestamps be used, must be in the MSS
1342 * advertised. But we subtract them from tp->mss_cache so
1343 * that calculations in tcp_sendmsg are simpler etc.
1344 * So account for this fact here if necessary. If we
1345 * don't do this correctly, as a receiver we won't
1346 * recognize data packets as being full sized when we
1347 * should, and thus we won't abide by the delayed ACK
1348 * rules correctly.
1349 * SACKs don't matter, we never delay an ACK when we
1350 * have any of those going out.
1351 */
1352 *ptr++ = htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | mss);
1353 if (ts) {
1354 if(sack)
1355 *ptr++ = __constant_htonl((TCPOPT_SACK_PERM << 24) | (TCPOLEN_SACK_PERM << 16) |
1356 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
1357 else
1358 *ptr++ = __constant_htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
1359 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
1360 *ptr++ = htonl(tstamp); /* TSVAL */
1361 *ptr++ = htonl(ts_recent); /* TSECR */
1362 } else if(sack)
1363 *ptr++ = __constant_htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
1364 (TCPOPT_SACK_PERM << 8) | TCPOLEN_SACK_PERM);
1365 if (offer_wscale)
1366 *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_WINDOW << 16) | (TCPOLEN_WINDOW << 8) | (wscale));
1367}
1368
1369/* Determine a window scaling and initial window to offer. */
1370extern void tcp_select_initial_window(int __space, __u32 mss,
1371 __u32 *rcv_wnd, __u32 *window_clamp,
1372 int wscale_ok, __u8 *rcv_wscale);
1373
1374static inline int tcp_win_from_space(int space)
1375{
1376 return sysctl_tcp_adv_win_scale<=0 ?
1377 (space>>(-sysctl_tcp_adv_win_scale)) :
1378 space - (space>>sysctl_tcp_adv_win_scale);
1379}
1380
1381/* Note: caller must be prepared to deal with negative returns */
1382static inline int tcp_space(const struct sock *sk)
1383{
1384 return tcp_win_from_space(sk->sk_rcvbuf -
1385 atomic_read(&sk->sk_rmem_alloc));
1386}
1387
1388static inline int tcp_full_space(const struct sock *sk)
1389{
1390 return tcp_win_from_space(sk->sk_rcvbuf);
1391}
1392
60236fdd 1393static inline void tcp_acceptq_queue(struct sock *sk, struct request_sock *req,
1da177e4
LT
1394 struct sock *child)
1395{
0e87506f 1396 reqsk_queue_add(&tcp_sk(sk)->accept_queue, req, sk, child);
1da177e4
LT
1397}
1398
1da177e4 1399static inline void
60236fdd 1400tcp_synq_removed(struct sock *sk, struct request_sock *req)
1da177e4 1401{
0e87506f 1402 if (reqsk_queue_removed(&tcp_sk(sk)->accept_queue, req) == 0)
1da177e4 1403 tcp_delete_keepalive_timer(sk);
1da177e4
LT
1404}
1405
1406static inline void tcp_synq_added(struct sock *sk)
1407{
0e87506f 1408 if (reqsk_queue_added(&tcp_sk(sk)->accept_queue) == 0)
1da177e4 1409 tcp_reset_keepalive_timer(sk, TCP_TIMEOUT_INIT);
1da177e4
LT
1410}
1411
1412static inline int tcp_synq_len(struct sock *sk)
1413{
0e87506f 1414 return reqsk_queue_len(&tcp_sk(sk)->accept_queue);
1da177e4
LT
1415}
1416
1417static inline int tcp_synq_young(struct sock *sk)
1418{
0e87506f 1419 return reqsk_queue_len_young(&tcp_sk(sk)->accept_queue);
1da177e4
LT
1420}
1421
1422static inline int tcp_synq_is_full(struct sock *sk)
1423{
0e87506f 1424 return reqsk_queue_is_full(&tcp_sk(sk)->accept_queue);
1da177e4
LT
1425}
1426
60236fdd 1427static inline void tcp_synq_unlink(struct tcp_sock *tp, struct request_sock *req,
0e87506f 1428 struct request_sock **prev)
1da177e4 1429{
0e87506f 1430 reqsk_queue_unlink(&tp->accept_queue, req, prev);
1da177e4
LT
1431}
1432
60236fdd
ACM
1433static inline void tcp_synq_drop(struct sock *sk, struct request_sock *req,
1434 struct request_sock **prev)
1da177e4
LT
1435{
1436 tcp_synq_unlink(tcp_sk(sk), req, prev);
1437 tcp_synq_removed(sk, req);
60236fdd 1438 reqsk_free(req);
1da177e4
LT
1439}
1440
60236fdd 1441static __inline__ void tcp_openreq_init(struct request_sock *req,
1da177e4
LT
1442 struct tcp_options_received *rx_opt,
1443 struct sk_buff *skb)
1444{
2e6599cb
ACM
1445 struct inet_request_sock *ireq = inet_rsk(req);
1446
1da177e4 1447 req->rcv_wnd = 0; /* So that tcp_send_synack() knows! */
2e6599cb 1448 tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
1da177e4
LT
1449 req->mss = rx_opt->mss_clamp;
1450 req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
2e6599cb
ACM
1451 ireq->tstamp_ok = rx_opt->tstamp_ok;
1452 ireq->sack_ok = rx_opt->sack_ok;
1453 ireq->snd_wscale = rx_opt->snd_wscale;
1454 ireq->wscale_ok = rx_opt->wscale_ok;
1455 ireq->acked = 0;
1456 ireq->ecn_ok = 0;
1457 ireq->rmt_port = skb->h.th->source;
1da177e4
LT
1458}
1459
1460extern void tcp_enter_memory_pressure(void);
1461
1462extern void tcp_listen_wlock(void);
1463
1464/* - We may sleep inside this lock.
1465 * - If sleeping is not required (or called from BH),
1466 * use plain read_(un)lock(&tcp_lhash_lock).
1467 */
1468
1469static inline void tcp_listen_lock(void)
1470{
1471 /* read_lock synchronizes to candidates to writers */
1472 read_lock(&tcp_lhash_lock);
1473 atomic_inc(&tcp_lhash_users);
1474 read_unlock(&tcp_lhash_lock);
1475}
1476
1477static inline void tcp_listen_unlock(void)
1478{
1479 if (atomic_dec_and_test(&tcp_lhash_users))
1480 wake_up(&tcp_lhash_wait);
1481}
1482
1483static inline int keepalive_intvl_when(const struct tcp_sock *tp)
1484{
1485 return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
1486}
1487
1488static inline int keepalive_time_when(const struct tcp_sock *tp)
1489{
1490 return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
1491}
1492
1493static inline int tcp_fin_time(const struct tcp_sock *tp)
1494{
1495 int fin_timeout = tp->linger2 ? : sysctl_tcp_fin_timeout;
1496
1497 if (fin_timeout < (tp->rto<<2) - (tp->rto>>1))
1498 fin_timeout = (tp->rto<<2) - (tp->rto>>1);
1499
1500 return fin_timeout;
1501}
1502
1503static inline int tcp_paws_check(const struct tcp_options_received *rx_opt, int rst)
1504{
1505 if ((s32)(rx_opt->rcv_tsval - rx_opt->ts_recent) >= 0)
1506 return 0;
1507 if (xtime.tv_sec >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)
1508 return 0;
1509
1510 /* RST segments are not recommended to carry timestamp,
1511 and, if they do, it is recommended to ignore PAWS because
1512 "their cleanup function should take precedence over timestamps."
1513 Certainly, it is mistake. It is necessary to understand the reasons
1514 of this constraint to relax it: if peer reboots, clock may go
1515 out-of-sync and half-open connections will not be reset.
1516 Actually, the problem would be not existing if all
1517 the implementations followed draft about maintaining clock
1518 via reboots. Linux-2.2 DOES NOT!
1519
1520 However, we can relax time bounds for RST segments to MSL.
1521 */
1522 if (rst && xtime.tv_sec >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
1523 return 0;
1524 return 1;
1525}
1526
1da177e4
LT
1527#define TCP_CHECK_TIMER(sk) do { } while (0)
1528
1529static inline int tcp_use_frto(const struct sock *sk)
1530{
1531 const struct tcp_sock *tp = tcp_sk(sk);
1532
1533 /* F-RTO must be activated in sysctl and there must be some
1534 * unsent new data, and the advertised window should allow
1535 * sending it.
1536 */
1537 return (sysctl_tcp_frto && sk->sk_send_head &&
1538 !after(TCP_SKB_CB(sk->sk_send_head)->end_seq,
1539 tp->snd_una + tp->snd_wnd));
1540}
1541
1542static inline void tcp_mib_init(void)
1543{
1544 /* See RFC 2012 */
1545 TCP_ADD_STATS_USER(TCP_MIB_RTOALGORITHM, 1);
1546 TCP_ADD_STATS_USER(TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
1547 TCP_ADD_STATS_USER(TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
1548 TCP_ADD_STATS_USER(TCP_MIB_MAXCONN, -1);
1549}
1550
1551/* /proc */
1552enum tcp_seq_states {
1553 TCP_SEQ_STATE_LISTENING,
1554 TCP_SEQ_STATE_OPENREQ,
1555 TCP_SEQ_STATE_ESTABLISHED,
1556 TCP_SEQ_STATE_TIME_WAIT,
1557};
1558
1559struct tcp_seq_afinfo {
1560 struct module *owner;
1561 char *name;
1562 sa_family_t family;
1563 int (*seq_show) (struct seq_file *m, void *v);
1564 struct file_operations *seq_fops;
1565};
1566
1567struct tcp_iter_state {
1568 sa_family_t family;
1569 enum tcp_seq_states state;
1570 struct sock *syn_wait_sk;
1571 int bucket, sbucket, num, uid;
1572 struct seq_operations seq_ops;
1573};
1574
1575extern int tcp_proc_register(struct tcp_seq_afinfo *afinfo);
1576extern void tcp_proc_unregister(struct tcp_seq_afinfo *afinfo);
1577
1da177e4 1578#endif /* _TCP_H */