]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - include/net/tcp.h
[INET]: Move the TCP hashtable functions/structs to inet_hashtables.[ch]
[mirror_ubuntu-jammy-kernel.git] / include / net / tcp.h
1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Definitions for the TCP module.
7 *
8 * Version: @(#)tcp.h 1.0.5 05/23/93
9 *
10 * Authors: Ross Biro
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>
33 #include <net/inet_hashtables.h>
34 #include <net/checksum.h>
35 #include <net/request_sock.h>
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
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)
54
55 extern kmem_cache_t *tcp_bucket_cachep;
56
57 extern int tcp_port_rover;
58
59 extern void tcp_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
60 unsigned short snum);
61
62 #if (BITS_PER_LONG == 64)
63 #define TCP_ADDRCMP_ALIGN_BYTES 8
64 #else
65 #define TCP_ADDRCMP_ALIGN_BYTES 4
66 #endif
67
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.
71 */
72 struct tcp_tw_bucket {
73 /*
74 * Now struct sock also uses sock_common, so please just
75 * don't add nothing before this first member (__tw_common) --acme
76 */
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;
87 __u16 tw_sport;
88 /* Socket demultiplex comparisons on incoming packets. */
89 /* these five are in inet_sock */
90 __u32 tw_daddr
91 __attribute__((aligned(TCP_ADDRCMP_ALIGN_BYTES)));
92 __u32 tw_rcv_saddr;
93 __u16 tw_dport;
94 __u16 tw_num;
95 /* And these are ours. */
96 int tw_hashent;
97 int tw_timeout;
98 __u32 tw_rcv_nxt;
99 __u32 tw_snd_nxt;
100 __u32 tw_rcv_wnd;
101 __u32 tw_ts_recent;
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;
109 int tw_v6_ipv6only;
110 #endif
111 };
112
113 static __inline__ void tw_add_node(struct tcp_tw_bucket *tw,
114 struct hlist_head *list)
115 {
116 hlist_add_head(&tw->tw_node, list);
117 }
118
119 static __inline__ void tw_add_bind_node(struct tcp_tw_bucket *tw,
120 struct hlist_head *list)
121 {
122 hlist_add_head(&tw->tw_bind_node, list);
123 }
124
125 static inline int tw_dead_hashed(struct tcp_tw_bucket *tw)
126 {
127 return tw->tw_death_node.pprev != NULL;
128 }
129
130 static __inline__ void tw_dead_node_init(struct tcp_tw_bucket *tw)
131 {
132 tw->tw_death_node.pprev = NULL;
133 }
134
135 static __inline__ void __tw_del_dead_node(struct tcp_tw_bucket *tw)
136 {
137 __hlist_del(&tw->tw_death_node);
138 tw_dead_node_init(tw);
139 }
140
141 static __inline__ int tw_del_dead_node(struct tcp_tw_bucket *tw)
142 {
143 if (tw_dead_hashed(tw)) {
144 __tw_del_dead_node(tw);
145 return 1;
146 }
147 return 0;
148 }
149
150 #define tw_for_each(tw, node, head) \
151 hlist_for_each_entry(tw, node, head, tw_node)
152
153 #define tw_for_each_inmate(tw, node, jail) \
154 hlist_for_each_entry(tw, node, jail, tw_death_node)
155
156 #define tw_for_each_inmate_safe(tw, node, safe, jail) \
157 hlist_for_each_entry_safe(tw, node, safe, jail, tw_death_node)
158
159 #define tcptw_sk(__sk) ((struct tcp_tw_bucket *)(__sk))
160
161 static inline u32 tcp_v4_rcv_saddr(const struct sock *sk)
162 {
163 return likely(sk->sk_state != TCP_TIME_WAIT) ?
164 inet_sk(sk)->rcv_saddr : tcptw_sk(sk)->tw_rcv_saddr;
165 }
166
167 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
168 static inline struct in6_addr *__tcp_v6_rcv_saddr(const struct sock *sk)
169 {
170 return likely(sk->sk_state != TCP_TIME_WAIT) ?
171 &inet6_sk(sk)->rcv_saddr : &tcptw_sk(sk)->tw_v6_rcv_saddr;
172 }
173
174 static inline struct in6_addr *tcp_v6_rcv_saddr(const struct sock *sk)
175 {
176 return sk->sk_family == AF_INET6 ? __tcp_v6_rcv_saddr(sk) : NULL;
177 }
178
179 #define tcptw_sk_ipv6only(__sk) (tcptw_sk(__sk)->tw_v6_ipv6only)
180
181 static inline int tcp_v6_ipv6only(const struct sock *sk)
182 {
183 return likely(sk->sk_state != TCP_TIME_WAIT) ?
184 ipv6_only_sock(sk) : tcptw_sk_ipv6only(sk);
185 }
186 #else
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
191 #endif
192
193 extern kmem_cache_t *tcp_timewait_cachep;
194
195 static inline void tcp_tw_put(struct tcp_tw_bucket *tw)
196 {
197 if (atomic_dec_and_test(&tw->tw_refcnt)) {
198 #ifdef SOCK_REFCNT_DEBUG
199 printk(KERN_DEBUG "tw_bucket %p released\n", tw);
200 #endif
201 kmem_cache_free(tcp_timewait_cachep, tw);
202 }
203 }
204
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);
209
210
211 /* Socket demux engine toys. */
212 #ifdef __BIG_ENDIAN
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))
218 #endif
219
220 #if (BITS_PER_LONG == 64)
221 #ifdef __BIG_ENDIAN
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 */
249
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))))
256
257 #define MAX_TCP_HEADER (128 + MAX_HEADER)
258
259 /*
260 * Never offer a window over 32767 without using window scaling. Some
261 * poor stacks do signed 16bit maths!
262 */
263 #define MAX_TCP_WINDOW 32767U
264
265 /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
266 #define TCP_MIN_MSS 88U
267
268 /* Minimal RCV_MSS. */
269 #define TCP_MIN_RCVMSS 536U
270
271 /* After receiving this amount of duplicate ACKs fast retransmit starts. */
272 #define TCP_FASTRETRANS_THRESH 3
273
274 /* Maximal reordering. */
275 #define TCP_MAX_REORDERING 127
276
277 /* Maximal number of ACKs sent quickly to accelerate slow-start. */
278 #define TCP_MAX_QUICKACKS 16U
279
280 /* urg_data states */
281 #define TCP_URG_VALID 0x0100
282 #define TCP_URG_NOTYET 0x0200
283 #define TCP_URG_READ 0x0400
284
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.
290 */
291
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.
297 */
298
299 #define TCP_SYN_RETRIES 5 /* number of times to retry active opening a
300 * connection: ~180sec is RFC minumum */
301
302 #define TCP_SYNACK_RETRIES 5 /* number of times to retry passive opening a
303 * connection: ~180sec is RFC minumum */
304
305
306 #define TCP_ORPHAN_RETRIES 7 /* number of times to retry on an orphaned
307 * socket. 7 is ~50sec-16min.
308 */
309
310
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
317 * TIME-WAIT timer.
318 */
319
320 #define TCP_DELACK_MAX ((unsigned)(HZ/5)) /* maximal time to delay before sending an ACK */
321 #if HZ >= 100
322 #define TCP_DELACK_MIN ((unsigned)(HZ/25)) /* minimal time to delay before sending an ACK */
323 #define TCP_ATO_MIN ((unsigned)(HZ/25))
324 #else
325 #define TCP_DELACK_MIN 4U
326 #define TCP_ATO_MIN 4U
327 #endif
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 */
331
332 #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
333 * for local resources.
334 */
335
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)
339
340 #define MAX_TCP_KEEPIDLE 32767
341 #define MAX_TCP_KEEPINTVL 32767
342 #define MAX_TCP_KEEPCNT 127
343 #define MAX_TCP_SYNCNT 127
344
345 #define TCP_SYNQ_INTERVAL (HZ/5) /* Period of SYNACK timer */
346 #define TCP_SYNQ_HSIZE 512 /* Size of SYNACK hash table */
347
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.
354 */
355 #define TCP_PAWS_WINDOW 1 /* Replay window for per-host
356 * timestamps. It must be less than
357 * minimal timewait lifetime.
358 */
359
360 #define TCP_TW_RECYCLE_SLOTS_LOG 5
361 #define TCP_TW_RECYCLE_SLOTS (1<<TCP_TW_RECYCLE_SLOTS_LOG)
362
363 /* If time > 4sec, it is "slow" path, no recycling is required,
364 so that we select tick to get range about 4 seconds.
365 */
366
367 #if HZ <= 16 || HZ > 4096
368 # error Unsupported: HZ <= 16 or HZ > 4096
369 #elif HZ <= 32
370 # define TCP_TW_RECYCLE_TICK (5+2-TCP_TW_RECYCLE_SLOTS_LOG)
371 #elif HZ <= 64
372 # define TCP_TW_RECYCLE_TICK (6+2-TCP_TW_RECYCLE_SLOTS_LOG)
373 #elif HZ <= 128
374 # define TCP_TW_RECYCLE_TICK (7+2-TCP_TW_RECYCLE_SLOTS_LOG)
375 #elif HZ <= 256
376 # define TCP_TW_RECYCLE_TICK (8+2-TCP_TW_RECYCLE_SLOTS_LOG)
377 #elif HZ <= 512
378 # define TCP_TW_RECYCLE_TICK (9+2-TCP_TW_RECYCLE_SLOTS_LOG)
379 #elif HZ <= 1024
380 # define TCP_TW_RECYCLE_TICK (10+2-TCP_TW_RECYCLE_SLOTS_LOG)
381 #elif HZ <= 2048
382 # define TCP_TW_RECYCLE_TICK (11+2-TCP_TW_RECYCLE_SLOTS_LOG)
383 #else
384 # define TCP_TW_RECYCLE_TICK (12+2-TCP_TW_RECYCLE_SLOTS_LOG)
385 #endif
386 /*
387 * TCP option
388 */
389
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 */
397
398 /*
399 * TCP option lengths
400 */
401
402 #define TCPOLEN_MSS 4
403 #define TCPOLEN_WINDOW 3
404 #define TCPOLEN_SACK_PERM 2
405 #define TCPOLEN_TIMESTAMP 10
406
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
414
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 */
419
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 */
424
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;
461
462 extern atomic_t tcp_memory_allocated;
463 extern atomic_t tcp_sockets_allocated;
464 extern int tcp_memory_pressure;
465
466 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
467 #define TCP_INET_FAMILY(fam) ((fam) == AF_INET)
468 #else
469 #define TCP_INET_FAMILY(fam) 1
470 #endif
471
472 /*
473 * Pointers to address related TCP functions
474 * (i.e. things that depend on the address family)
475 */
476
477 struct tcp_func {
478 int (*queue_xmit) (struct sk_buff *skb,
479 int ipfragok);
480
481 void (*send_check) (struct sock *sk,
482 struct tcphdr *th,
483 int len,
484 struct sk_buff *skb);
485
486 int (*rebuild_header) (struct sock *sk);
487
488 int (*conn_request) (struct sock *sk,
489 struct sk_buff *skb);
490
491 struct sock * (*syn_recv_sock) (struct sock *sk,
492 struct sk_buff *skb,
493 struct request_sock *req,
494 struct dst_entry *dst);
495
496 int (*remember_stamp) (struct sock *sk);
497
498 __u16 net_header_len;
499
500 int (*setsockopt) (struct sock *sk,
501 int level,
502 int optname,
503 char __user *optval,
504 int optlen);
505
506 int (*getsockopt) (struct sock *sk,
507 int level,
508 int optname,
509 char __user *optval,
510 int __user *optlen);
511
512
513 void (*addr2sockaddr) (struct sock *sk,
514 struct sockaddr *);
515
516 int sockaddr_len;
517 };
518
519 /*
520 * The next routines deal with comparing 32 bit unsigned ints
521 * and worry about wraparound (automatic with unsigned arithmetic).
522 */
523
524 static inline int before(__u32 seq1, __u32 seq2)
525 {
526 return (__s32)(seq1-seq2) < 0;
527 }
528
529 static inline int after(__u32 seq1, __u32 seq2)
530 {
531 return (__s32)(seq2-seq1) < 0;
532 }
533
534
535 /* is s2<=s1<=s3 ? */
536 static inline int between(__u32 seq1, __u32 seq2, __u32 seq3)
537 {
538 return seq3 - seq2 >= seq1 - seq2;
539 }
540
541
542 extern struct proto tcp_prot;
543
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)
551
552 extern void tcp_put_port(struct sock *sk);
553 extern void tcp_inherit_port(struct sock *sk, struct sock *child);
554
555 extern void tcp_v4_err(struct sk_buff *skb, u32);
556
557 extern void tcp_shutdown (struct sock *sk, int how);
558
559 extern int tcp_v4_rcv(struct sk_buff *skb);
560
561 extern int tcp_v4_remember_stamp(struct sock *sk);
562
563 extern int tcp_v4_tw_remember_stamp(struct tcp_tw_bucket *tw);
564
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);
568
569 extern int tcp_ioctl(struct sock *sk,
570 int cmd,
571 unsigned long arg);
572
573 extern int tcp_rcv_state_process(struct sock *sk,
574 struct sk_buff *skb,
575 struct tcphdr *th,
576 unsigned len);
577
578 extern int tcp_rcv_established(struct sock *sk,
579 struct sk_buff *skb,
580 struct tcphdr *th,
581 unsigned len);
582
583 extern void tcp_rcv_space_adjust(struct sock *sk);
584
585 enum tcp_ack_state_t
586 {
587 TCP_ACK_SCHED = 1,
588 TCP_ACK_TIMER = 2,
589 TCP_ACK_PUSHED= 4
590 };
591
592 static inline void tcp_schedule_ack(struct tcp_sock *tp)
593 {
594 tp->ack.pending |= TCP_ACK_SCHED;
595 }
596
597 static inline int tcp_ack_scheduled(struct tcp_sock *tp)
598 {
599 return tp->ack.pending&TCP_ACK_SCHED;
600 }
601
602 static __inline__ void tcp_dec_quickack_mode(struct tcp_sock *tp, unsigned int pkts)
603 {
604 if (tp->ack.quick) {
605 if (pkts >= tp->ack.quick) {
606 tp->ack.quick = 0;
607
608 /* Leaving quickack mode we deflate ATO. */
609 tp->ack.ato = TCP_ATO_MIN;
610 } else
611 tp->ack.quick -= pkts;
612 }
613 }
614
615 extern void tcp_enter_quickack_mode(struct tcp_sock *tp);
616
617 static __inline__ void tcp_delack_init(struct tcp_sock *tp)
618 {
619 memset(&tp->ack, 0, sizeof(tp->ack));
620 }
621
622 static inline void tcp_clear_options(struct tcp_options_received *rx_opt)
623 {
624 rx_opt->tstamp_ok = rx_opt->sack_ok = rx_opt->wscale_ok = rx_opt->snd_wscale = 0;
625 }
626
627 enum tcp_tw_status
628 {
629 TCP_TW_SUCCESS = 0,
630 TCP_TW_RST = 1,
631 TCP_TW_ACK = 2,
632 TCP_TW_SYN = 3
633 };
634
635
636 extern enum tcp_tw_status tcp_timewait_state_process(struct tcp_tw_bucket *tw,
637 struct sk_buff *skb,
638 struct tcphdr *th,
639 unsigned len);
640
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,
645 struct sock *child,
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);
651
652 extern void tcp_close(struct sock *sk,
653 long timeout);
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);
656
657 extern int tcp_getsockopt(struct sock *sk, int level,
658 int optname,
659 char __user *optval,
660 int __user *optlen);
661 extern int tcp_setsockopt(struct sock *sk, int level,
662 int optname, char __user *optval,
663 int optlen);
664 extern void tcp_set_keepalive(struct sock *sk, int val);
665 extern int tcp_recvmsg(struct kiocb *iocb, struct sock *sk,
666 struct msghdr *msg,
667 size_t len, int nonblock,
668 int flags, int *addr_len);
669
670 extern int tcp_listen_start(struct sock *sk);
671
672 extern void tcp_parse_options(struct sk_buff *skb,
673 struct tcp_options_received *opt_rx,
674 int estab);
675
676 /*
677 * TCP v4 functions exported for the inet6 API
678 */
679
680 extern void tcp_v4_send_check(struct sock *sk,
681 struct tcphdr *th, int len,
682 struct sk_buff *skb);
683
684 extern int tcp_v4_conn_request(struct sock *sk,
685 struct sk_buff *skb);
686
687 extern struct sock * tcp_create_openreq_child(struct sock *sk,
688 struct request_sock *req,
689 struct sk_buff *skb);
690
691 extern struct sock * tcp_v4_syn_recv_sock(struct sock *sk,
692 struct sk_buff *skb,
693 struct request_sock *req,
694 struct dst_entry *dst);
695
696 extern int tcp_v4_do_rcv(struct sock *sk,
697 struct sk_buff *skb);
698
699 extern int tcp_v4_connect(struct sock *sk,
700 struct sockaddr *uaddr,
701 int addr_len);
702
703 extern int tcp_connect(struct sock *sk);
704
705 extern struct sk_buff * tcp_make_synack(struct sock *sk,
706 struct dst_entry *dst,
707 struct request_sock *req);
708
709 extern int tcp_disconnect(struct sock *sk, int flags);
710
711 extern void tcp_unhash(struct sock *sk);
712
713 extern int tcp_v4_hash_connecting(struct sock *sk);
714
715
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,
720 __u16 *mss);
721
722 /* tcp_output.c */
723
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);
731
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);
742
743 /* tcp_input.c */
744 extern void tcp_cwnd_application_limited(struct sock *sk);
745
746 /* tcp_timer.c */
747 extern void tcp_init_xmit_timers(struct sock *);
748 extern void tcp_clear_xmit_timers(struct sock *);
749
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);
754
755 #ifdef TCP_DEBUG
756 extern const char tcp_timer_bug_msg[];
757 #endif
758
759 /* tcp_diag.c */
760 extern void tcp_get_info(struct sock *, struct tcp_info *);
761
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);
767
768 static inline void tcp_clear_xmit_timer(struct sock *sk, int what)
769 {
770 struct tcp_sock *tp = tcp_sk(sk);
771
772 switch (what) {
773 case TCP_TIME_RETRANS:
774 case TCP_TIME_PROBE0:
775 tp->pending = 0;
776
777 #ifdef TCP_CLEAR_TIMERS
778 sk_stop_timer(sk, &tp->retransmit_timer);
779 #endif
780 break;
781 case TCP_TIME_DACK:
782 tp->ack.blocked = 0;
783 tp->ack.pending = 0;
784
785 #ifdef TCP_CLEAR_TIMERS
786 sk_stop_timer(sk, &tp->delack_timer);
787 #endif
788 break;
789 default:
790 #ifdef TCP_DEBUG
791 printk(tcp_timer_bug_msg);
792 #endif
793 return;
794 };
795
796 }
797
798 /*
799 * Reset the retransmission timer
800 */
801 static inline void tcp_reset_xmit_timer(struct sock *sk, int what, unsigned long when)
802 {
803 struct tcp_sock *tp = tcp_sk(sk);
804
805 if (when > TCP_RTO_MAX) {
806 #ifdef TCP_DEBUG
807 printk(KERN_DEBUG "reset_xmit_timer sk=%p %d when=0x%lx, caller=%p\n", sk, what, when, current_text_addr());
808 #endif
809 when = TCP_RTO_MAX;
810 }
811
812 switch (what) {
813 case TCP_TIME_RETRANS:
814 case TCP_TIME_PROBE0:
815 tp->pending = what;
816 tp->timeout = jiffies+when;
817 sk_reset_timer(sk, &tp->retransmit_timer, tp->timeout);
818 break;
819
820 case TCP_TIME_DACK:
821 tp->ack.pending |= TCP_ACK_TIMER;
822 tp->ack.timeout = jiffies+when;
823 sk_reset_timer(sk, &tp->delack_timer, tp->ack.timeout);
824 break;
825
826 default:
827 #ifdef TCP_DEBUG
828 printk(tcp_timer_bug_msg);
829 #endif
830 return;
831 };
832 }
833
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().
840 */
841
842 static inline void tcp_initialize_rcv_mss(struct sock *sk)
843 {
844 struct tcp_sock *tp = tcp_sk(sk);
845 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
846
847 hint = min(hint, tp->rcv_wnd/2);
848 hint = min(hint, TCP_MIN_RCVMSS);
849 hint = max(hint, TCP_MIN_MSS);
850
851 tp->ack.rcv_mss = hint;
852 }
853
854 static __inline__ void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
855 {
856 tp->pred_flags = htonl((tp->tcp_header_len << 26) |
857 ntohl(TCP_FLAG_ACK) |
858 snd_wnd);
859 }
860
861 static __inline__ void tcp_fast_path_on(struct tcp_sock *tp)
862 {
863 __tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
864 }
865
866 static inline void tcp_fast_path_check(struct sock *sk, struct tcp_sock *tp)
867 {
868 if (skb_queue_empty(&tp->out_of_order_queue) &&
869 tp->rcv_wnd &&
870 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
871 !tp->urg_data)
872 tcp_fast_path_on(tp);
873 }
874
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.
878 */
879 static __inline__ u32 tcp_receive_window(const struct tcp_sock *tp)
880 {
881 s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
882
883 if (win < 0)
884 win = 0;
885 return (u32) win;
886 }
887
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.
891 */
892 extern u32 __tcp_select_window(struct sock *sk);
893
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.
899 */
900 #define tcp_time_stamp ((__u32)(jiffies))
901
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.
908 */
909 struct tcp_skb_cb {
910 union {
911 struct inet_skb_parm h4;
912 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
913 struct inet6_skb_parm h6;
914 #endif
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. */
920
921 /* NOTE: These must match up to the flags byte in a
922 * real TCP header.
923 */
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
932
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 */
938
939 #define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */
940 #define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
941
942 #define TCPCB_URG 0x20 /* Urgent pointer advenced here */
943
944 #define TCPCB_AT_TAIL (TCPCB_URG)
945
946 __u16 urg_ptr; /* Valid w/URG flags is set. */
947 __u32 ack_seq; /* Sequence number ACK'd */
948 };
949
950 #define TCP_SKB_CB(__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0]))
951
952 #include <net/tcp_ecn.h>
953
954 /* Due to TSO, an SKB can be composed of multiple actual
955 * packets. To keep these tracked properly, we use this.
956 */
957 static inline int tcp_skb_pcount(const struct sk_buff *skb)
958 {
959 return skb_shinfo(skb)->tso_segs;
960 }
961
962 /* This is valid iff tcp_skb_pcount() > 1. */
963 static inline int tcp_skb_mss(const struct sk_buff *skb)
964 {
965 return skb_shinfo(skb)->tso_size;
966 }
967
968 static inline void tcp_dec_pcount_approx(__u32 *count,
969 const struct sk_buff *skb)
970 {
971 if (*count) {
972 *count -= tcp_skb_pcount(skb);
973 if ((int)*count < 0)
974 *count = 0;
975 }
976 }
977
978 static inline void tcp_packets_out_inc(struct sock *sk,
979 struct tcp_sock *tp,
980 const struct sk_buff *skb)
981 {
982 int orig = tp->packets_out;
983
984 tp->packets_out += tcp_skb_pcount(skb);
985 if (!orig)
986 tcp_reset_xmit_timer(sk, TCP_TIME_RETRANS, tp->rto);
987 }
988
989 static inline void tcp_packets_out_dec(struct tcp_sock *tp,
990 const struct sk_buff *skb)
991 {
992 tp->packets_out -= tcp_skb_pcount(skb);
993 }
994
995 /* Events passed to congestion control interface */
996 enum tcp_ca_event {
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 */
1004 };
1005
1006 /*
1007 * Interface for adding new TCP congestion control handlers
1008 */
1009 #define TCP_CA_NAME_MAX 16
1010 struct tcp_congestion_ops {
1011 struct list_head list;
1012
1013 /* initialize private data (optional) */
1014 void (*init)(struct tcp_sock *tp);
1015 /* cleanup private data (optional) */
1016 void (*release)(struct tcp_sock *tp);
1017
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);
1037
1038 char name[TCP_CA_NAME_MAX];
1039 struct module *owner;
1040 };
1041
1042 extern int tcp_register_congestion_control(struct tcp_congestion_ops *type);
1043 extern void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
1044
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);
1050
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;
1057
1058 static inline void tcp_set_ca_state(struct tcp_sock *tp, u8 ca_state)
1059 {
1060 if (tp->ca_ops->set_state)
1061 tp->ca_ops->set_state(tp, ca_state);
1062 tp->ca_state = ca_state;
1063 }
1064
1065 static inline void tcp_ca_event(struct tcp_sock *tp, enum tcp_ca_event event)
1066 {
1067 if (tp->ca_ops->cwnd_event)
1068 tp->ca_ops->cwnd_event(tp, event);
1069 }
1070
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.
1075 *
1076 * Use this for decisions involving congestion control, use just
1077 * tp->packets_out to determine if the send queue is empty or not.
1078 *
1079 * Read this equation as:
1080 *
1081 * "Packets sent once on transmission queue" MINUS
1082 * "Packets left network, but not honestly ACKed yet" PLUS
1083 * "Packets fast retransmitted"
1084 */
1085 static __inline__ unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
1086 {
1087 return (tp->packets_out - tp->left_out + tp->retrans_out);
1088 }
1089
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
1092 * ssthresh.
1093 */
1094 static inline __u32 tcp_current_ssthresh(struct tcp_sock *tp)
1095 {
1096 if ((1<<tp->ca_state)&(TCPF_CA_CWR|TCPF_CA_Recovery))
1097 return tp->snd_ssthresh;
1098 else
1099 return max(tp->snd_ssthresh,
1100 ((tp->snd_cwnd >> 1) +
1101 (tp->snd_cwnd >> 2)));
1102 }
1103
1104 static inline void tcp_sync_left_out(struct tcp_sock *tp)
1105 {
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;
1110 }
1111
1112 /* Set slow start threshold and cwnd not falling to slow start */
1113 static inline void __tcp_enter_cwr(struct tcp_sock *tp)
1114 {
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);
1123 }
1124
1125 static inline void tcp_enter_cwr(struct tcp_sock *tp)
1126 {
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);
1131 }
1132 }
1133
1134 extern __u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst);
1135
1136 /* Slow start with delack produces 3 packets of burst, so that
1137 * it is safe "de facto".
1138 */
1139 static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
1140 {
1141 return 3;
1142 }
1143
1144 static __inline__ void tcp_minshall_update(struct tcp_sock *tp, int mss,
1145 const struct sk_buff *skb)
1146 {
1147 if (skb->len < mss)
1148 tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
1149 }
1150
1151 static __inline__ void tcp_check_probe_timer(struct sock *sk, struct tcp_sock *tp)
1152 {
1153 if (!tp->packets_out && !tp->pending)
1154 tcp_reset_xmit_timer(sk, TCP_TIME_PROBE0, tp->rto);
1155 }
1156
1157 static __inline__ void tcp_push_pending_frames(struct sock *sk,
1158 struct tcp_sock *tp)
1159 {
1160 __tcp_push_pending_frames(sk, tp, tcp_current_mss(sk, 1), tp->nonagle);
1161 }
1162
1163 static __inline__ void tcp_init_wl(struct tcp_sock *tp, u32 ack, u32 seq)
1164 {
1165 tp->snd_wl1 = seq;
1166 }
1167
1168 static __inline__ void tcp_update_wl(struct tcp_sock *tp, u32 ack, u32 seq)
1169 {
1170 tp->snd_wl1 = seq;
1171 }
1172
1173 extern void tcp_destroy_sock(struct sock *sk);
1174
1175
1176 /*
1177 * Calculate(/check) TCP checksum
1178 */
1179 static __inline__ u16 tcp_v4_check(struct tcphdr *th, int len,
1180 unsigned long saddr, unsigned long daddr,
1181 unsigned long base)
1182 {
1183 return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
1184 }
1185
1186 static __inline__ int __tcp_checksum_complete(struct sk_buff *skb)
1187 {
1188 return (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
1189 }
1190
1191 static __inline__ int tcp_checksum_complete(struct sk_buff *skb)
1192 {
1193 return skb->ip_summed != CHECKSUM_UNNECESSARY &&
1194 __tcp_checksum_complete(skb);
1195 }
1196
1197 /* Prequeue for VJ style copy to user, combined with checksumming. */
1198
1199 static __inline__ void tcp_prequeue_init(struct tcp_sock *tp)
1200 {
1201 tp->ucopy.task = NULL;
1202 tp->ucopy.len = 0;
1203 tp->ucopy.memory = 0;
1204 skb_queue_head_init(&tp->ucopy.prequeue);
1205 }
1206
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
1212 *
1213 * NOTE: is this not too big to inline?
1214 */
1215 static __inline__ int tcp_prequeue(struct sock *sk, struct sk_buff *skb)
1216 {
1217 struct tcp_sock *tp = tcp_sk(sk);
1218
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;
1224
1225 BUG_ON(sock_owned_by_user(sk));
1226
1227 while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) {
1228 sk->sk_backlog_rcv(sk, skb1);
1229 NET_INC_STATS_BH(LINUX_MIB_TCPPREQUEUEDROPPED);
1230 }
1231
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);
1237 }
1238 return 1;
1239 }
1240 return 0;
1241 }
1242
1243
1244 #undef STATE_TRACE
1245
1246 #ifdef STATE_TRACE
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"
1251 };
1252 #endif
1253
1254 static __inline__ void tcp_set_state(struct sock *sk, int state)
1255 {
1256 int oldstate = sk->sk_state;
1257
1258 switch (state) {
1259 case TCP_ESTABLISHED:
1260 if (oldstate != TCP_ESTABLISHED)
1261 TCP_INC_STATS(TCP_MIB_CURRESTAB);
1262 break;
1263
1264 case TCP_CLOSE:
1265 if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED)
1266 TCP_INC_STATS(TCP_MIB_ESTABRESETS);
1267
1268 sk->sk_prot->unhash(sk);
1269 if (tcp_sk(sk)->bind_hash &&
1270 !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
1271 tcp_put_port(sk);
1272 /* fall through */
1273 default:
1274 if (oldstate==TCP_ESTABLISHED)
1275 TCP_DEC_STATS(TCP_MIB_CURRESTAB);
1276 }
1277
1278 /* Change state AFTER socket is unhashed to avoid closed
1279 * socket sitting in hash tables.
1280 */
1281 sk->sk_state = state;
1282
1283 #ifdef STATE_TRACE
1284 SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n",sk, statename[oldstate],statename[state]);
1285 #endif
1286 }
1287
1288 static __inline__ void tcp_done(struct sock *sk)
1289 {
1290 tcp_set_state(sk, TCP_CLOSE);
1291 tcp_clear_xmit_timers(sk);
1292
1293 sk->sk_shutdown = SHUTDOWN_MASK;
1294
1295 if (!sock_flag(sk, SOCK_DEAD))
1296 sk->sk_state_change(sk);
1297 else
1298 tcp_destroy_sock(sk);
1299 }
1300
1301 static __inline__ void tcp_sack_reset(struct tcp_options_received *rx_opt)
1302 {
1303 rx_opt->dsack = 0;
1304 rx_opt->eff_sacks = 0;
1305 rx_opt->num_sacks = 0;
1306 }
1307
1308 static __inline__ void tcp_build_and_update_options(__u32 *ptr, struct tcp_sock *tp, __u32 tstamp)
1309 {
1310 if (tp->rx_opt.tstamp_ok) {
1311 *ptr++ = __constant_htonl((TCPOPT_NOP << 24) |
1312 (TCPOPT_NOP << 16) |
1313 (TCPOPT_TIMESTAMP << 8) |
1314 TCPOLEN_TIMESTAMP);
1315 *ptr++ = htonl(tstamp);
1316 *ptr++ = htonl(tp->rx_opt.ts_recent);
1317 }
1318 if (tp->rx_opt.eff_sacks) {
1319 struct tcp_sack_block *sp = tp->rx_opt.dsack ? tp->duplicate_sack : tp->selective_acks;
1320 int this_sack;
1321
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);
1330 }
1331 if (tp->rx_opt.dsack) {
1332 tp->rx_opt.dsack = 0;
1333 tp->rx_opt.eff_sacks--;
1334 }
1335 }
1336 }
1337
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
1341 * can generate.
1342 */
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)
1345 {
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
1355 * rules correctly.
1356 * SACKs don't matter, we never delay an ACK when we
1357 * have any of those going out.
1358 */
1359 *ptr++ = htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | mss);
1360 if (ts) {
1361 if(sack)
1362 *ptr++ = __constant_htonl((TCPOPT_SACK_PERM << 24) | (TCPOLEN_SACK_PERM << 16) |
1363 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
1364 else
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 */
1369 } else if(sack)
1370 *ptr++ = __constant_htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
1371 (TCPOPT_SACK_PERM << 8) | TCPOLEN_SACK_PERM);
1372 if (offer_wscale)
1373 *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_WINDOW << 16) | (TCPOLEN_WINDOW << 8) | (wscale));
1374 }
1375
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);
1380
1381 static inline int tcp_win_from_space(int space)
1382 {
1383 return sysctl_tcp_adv_win_scale<=0 ?
1384 (space>>(-sysctl_tcp_adv_win_scale)) :
1385 space - (space>>sysctl_tcp_adv_win_scale);
1386 }
1387
1388 /* Note: caller must be prepared to deal with negative returns */
1389 static inline int tcp_space(const struct sock *sk)
1390 {
1391 return tcp_win_from_space(sk->sk_rcvbuf -
1392 atomic_read(&sk->sk_rmem_alloc));
1393 }
1394
1395 static inline int tcp_full_space(const struct sock *sk)
1396 {
1397 return tcp_win_from_space(sk->sk_rcvbuf);
1398 }
1399
1400 static inline void tcp_acceptq_queue(struct sock *sk, struct request_sock *req,
1401 struct sock *child)
1402 {
1403 reqsk_queue_add(&tcp_sk(sk)->accept_queue, req, sk, child);
1404 }
1405
1406 static inline void
1407 tcp_synq_removed(struct sock *sk, struct request_sock *req)
1408 {
1409 if (reqsk_queue_removed(&tcp_sk(sk)->accept_queue, req) == 0)
1410 tcp_delete_keepalive_timer(sk);
1411 }
1412
1413 static inline void tcp_synq_added(struct sock *sk)
1414 {
1415 if (reqsk_queue_added(&tcp_sk(sk)->accept_queue) == 0)
1416 tcp_reset_keepalive_timer(sk, TCP_TIMEOUT_INIT);
1417 }
1418
1419 static inline int tcp_synq_len(struct sock *sk)
1420 {
1421 return reqsk_queue_len(&tcp_sk(sk)->accept_queue);
1422 }
1423
1424 static inline int tcp_synq_young(struct sock *sk)
1425 {
1426 return reqsk_queue_len_young(&tcp_sk(sk)->accept_queue);
1427 }
1428
1429 static inline int tcp_synq_is_full(struct sock *sk)
1430 {
1431 return reqsk_queue_is_full(&tcp_sk(sk)->accept_queue);
1432 }
1433
1434 static inline void tcp_synq_unlink(struct tcp_sock *tp, struct request_sock *req,
1435 struct request_sock **prev)
1436 {
1437 reqsk_queue_unlink(&tp->accept_queue, req, prev);
1438 }
1439
1440 static inline void tcp_synq_drop(struct sock *sk, struct request_sock *req,
1441 struct request_sock **prev)
1442 {
1443 tcp_synq_unlink(tcp_sk(sk), req, prev);
1444 tcp_synq_removed(sk, req);
1445 reqsk_free(req);
1446 }
1447
1448 static __inline__ void tcp_openreq_init(struct request_sock *req,
1449 struct tcp_options_received *rx_opt,
1450 struct sk_buff *skb)
1451 {
1452 struct inet_request_sock *ireq = inet_rsk(req);
1453
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;
1462 ireq->acked = 0;
1463 ireq->ecn_ok = 0;
1464 ireq->rmt_port = skb->h.th->source;
1465 }
1466
1467 extern void tcp_enter_memory_pressure(void);
1468
1469 extern void tcp_listen_wlock(void);
1470
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).
1474 */
1475
1476 static inline void tcp_listen_lock(void)
1477 {
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);
1482 }
1483
1484 static inline void tcp_listen_unlock(void)
1485 {
1486 if (atomic_dec_and_test(&tcp_lhash_users))
1487 wake_up(&tcp_lhash_wait);
1488 }
1489
1490 static inline int keepalive_intvl_when(const struct tcp_sock *tp)
1491 {
1492 return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
1493 }
1494
1495 static inline int keepalive_time_when(const struct tcp_sock *tp)
1496 {
1497 return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
1498 }
1499
1500 static inline int tcp_fin_time(const struct tcp_sock *tp)
1501 {
1502 int fin_timeout = tp->linger2 ? : sysctl_tcp_fin_timeout;
1503
1504 if (fin_timeout < (tp->rto<<2) - (tp->rto>>1))
1505 fin_timeout = (tp->rto<<2) - (tp->rto>>1);
1506
1507 return fin_timeout;
1508 }
1509
1510 static inline int tcp_paws_check(const struct tcp_options_received *rx_opt, int rst)
1511 {
1512 if ((s32)(rx_opt->rcv_tsval - rx_opt->ts_recent) >= 0)
1513 return 0;
1514 if (xtime.tv_sec >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)
1515 return 0;
1516
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!
1526
1527 However, we can relax time bounds for RST segments to MSL.
1528 */
1529 if (rst && xtime.tv_sec >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
1530 return 0;
1531 return 1;
1532 }
1533
1534 #define TCP_CHECK_TIMER(sk) do { } while (0)
1535
1536 static inline int tcp_use_frto(const struct sock *sk)
1537 {
1538 const struct tcp_sock *tp = tcp_sk(sk);
1539
1540 /* F-RTO must be activated in sysctl and there must be some
1541 * unsent new data, and the advertised window should allow
1542 * sending it.
1543 */
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));
1547 }
1548
1549 static inline void tcp_mib_init(void)
1550 {
1551 /* See RFC 2012 */
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);
1556 }
1557
1558 /* /proc */
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,
1564 };
1565
1566 struct tcp_seq_afinfo {
1567 struct module *owner;
1568 char *name;
1569 sa_family_t family;
1570 int (*seq_show) (struct seq_file *m, void *v);
1571 struct file_operations *seq_fops;
1572 };
1573
1574 struct tcp_iter_state {
1575 sa_family_t family;
1576 enum tcp_seq_states state;
1577 struct sock *syn_wait_sk;
1578 int bucket, sbucket, num, uid;
1579 struct seq_operations seq_ops;
1580 };
1581
1582 extern int tcp_proc_register(struct tcp_seq_afinfo *afinfo);
1583 extern void tcp_proc_unregister(struct tcp_seq_afinfo *afinfo);
1584
1585 #endif /* _TCP_H */