blob: 567017b5fc9e5976bb1c291edecb610961fba2d7 [file] [log] [blame]
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001/*
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 a generic INET TIMEWAIT sock
7 *
8 * From code originally in net/tcp.h
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15#ifndef _INET_TIMEWAIT_SOCK_
16#define _INET_TIMEWAIT_SOCK_
17
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070018
Vegard Nossum9e337b02008-10-18 17:37:51 +020019#include <linux/kmemcheck.h>
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070020#include <linux/list.h>
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -070021#include <linux/timer.h>
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070022#include <linux/types.h>
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -070023#include <linux/workqueue.h>
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070024
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020025#include <net/inet_sock.h>
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070026#include <net/sock.h>
27#include <net/tcp_states.h>
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -080028#include <net/timewait_sock.h>
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070029
Arun Sharma600634972011-07-26 16:09:06 -070030#include <linux/atomic.h>
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070031
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -070032struct inet_hashinfo;
33
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -070034struct inet_timewait_death_row {
Eric Dumazet789f5582015-04-12 18:51:09 -070035 atomic_t tw_count;
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -070036
Eric Dumazet789f5582015-04-12 18:51:09 -070037 struct inet_hashinfo *hashinfo ____cacheline_aligned_in_smp;
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -070038 int sysctl_tw_recycle;
39 int sysctl_max_tw_buckets;
40};
41
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070042struct inet_bind_bucket;
43
44/*
45 * This is a TIME_WAIT sock. It works around the memory consumption
46 * problems of sockets in such a state on heavily loaded servers, but
47 * without violating the protocol specification.
48 */
49struct inet_timewait_sock {
50 /*
51 * Now struct sock also uses sock_common, so please just
52 * don't add nothing before this first member (__tw_common) --acme
53 */
54 struct sock_common __tw_common;
55#define tw_family __tw_common.skc_family
56#define tw_state __tw_common.skc_state
57#define tw_reuse __tw_common.skc_reuse
Eric Dumazete68fb9622018-04-07 13:42:43 -070058#define tw_reuseport __tw_common.skc_reuseport
Eric Dumazet9fe516b2014-06-27 08:36:16 -070059#define tw_ipv6only __tw_common.skc_ipv6only
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070060#define tw_bound_dev_if __tw_common.skc_bound_dev_if
Eric Dumazet3ab5aee2008-11-16 19:40:17 -080061#define tw_node __tw_common.skc_nulls_node
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070062#define tw_bind_node __tw_common.skc_bind_node
63#define tw_refcnt __tw_common.skc_refcnt
Eric Dumazet81c3d542005-10-03 14:13:38 -070064#define tw_hash __tw_common.skc_hash
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070065#define tw_prot __tw_common.skc_prot
Eric W. Biederman07feaeb2007-09-12 11:58:02 +020066#define tw_net __tw_common.skc_net
Eric Dumazet68835ab2010-11-30 19:04:07 +000067#define tw_daddr __tw_common.skc_daddr
Eric Dumazetefe42082013-10-03 15:42:29 -070068#define tw_v6_daddr __tw_common.skc_v6_daddr
Eric Dumazet68835ab2010-11-30 19:04:07 +000069#define tw_rcv_saddr __tw_common.skc_rcv_saddr
Eric Dumazetefe42082013-10-03 15:42:29 -070070#define tw_v6_rcv_saddr __tw_common.skc_v6_rcv_saddr
Eric Dumazetce43b032012-11-30 09:49:27 +000071#define tw_dport __tw_common.skc_dport
72#define tw_num __tw_common.skc_num
Eric Dumazet33cf7c92015-03-11 18:53:14 -070073#define tw_cookie __tw_common.skc_cookie
Eric Dumazet8e5eb542015-10-08 19:33:22 -070074#define tw_dr __tw_common.skc_tw_dr
Eric Dumazetce43b032012-11-30 09:49:27 +000075
Arnaldo Carvalho de Meload8bb782008-02-03 04:08:26 -080076 int tw_timeout;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070077 volatile unsigned char tw_substate;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070078 unsigned char tw_rcv_wscale;
Eric Dumazet68835ab2010-11-30 19:04:07 +000079
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070080 /* Socket demultiplex comparisons on incoming packets. */
Eric Dumazet68835ab2010-11-30 19:04:07 +000081 /* these three are in inet_sock */
Al Viro23f33c22006-09-27 18:43:50 -070082 __be16 tw_sport;
Vegard Nossum9e337b02008-10-18 17:37:51 +020083 kmemcheck_bitfield_begin(flags);
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070084 /* And these are ours. */
Eric Dumazet789f5582015-04-12 18:51:09 -070085 unsigned int tw_kill : 1,
Eric Dumazetabf90cc2009-10-18 22:48:51 +000086 tw_transparent : 1,
Florent Fourcot1d13a962014-01-16 17:21:22 +010087 tw_flowlabel : 20,
88 tw_pad : 2, /* 2 bits hole */
89 tw_tos : 8;
Vegard Nossum9e337b02008-10-18 17:37:51 +020090 kmemcheck_bitfield_end(flags);
Eric Dumazet789f5582015-04-12 18:51:09 -070091 struct timer_list tw_timer;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070092 struct inet_bind_bucket *tw_tb;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070093};
Eric Dumazetb903d322011-10-27 00:44:35 -040094#define tw_tclass tw_tos
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070095
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070096static inline struct inet_timewait_sock *inet_twsk(const struct sock *sk)
97{
98 return (struct inet_timewait_sock *)sk;
99}
100
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700101void inet_twsk_free(struct inet_timewait_sock *tw);
Joe Perches1fd51152013-09-21 10:22:41 -0700102void inet_twsk_put(struct inet_timewait_sock *tw);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700103
Eric Dumazetfc01538f2015-07-08 14:28:29 -0700104void inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
105 struct inet_hashinfo *hashinfo);
Eric Dumazet3cdaeda2009-12-04 03:47:42 +0000106
Joe Perches1fd51152013-09-21 10:22:41 -0700107struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
Eric Dumazet789f5582015-04-12 18:51:09 -0700108 struct inet_timewait_death_row *dr,
Joe Perches1fd51152013-09-21 10:22:41 -0700109 const int state);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700110
Joe Perches1fd51152013-09-21 10:22:41 -0700111void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
112 struct inet_hashinfo *hashinfo);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700113
Eric Dumazeted2e9232015-09-19 09:08:34 -0700114void __inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo,
115 bool rearm);
116
Raanan Avargil8695a142015-10-01 04:48:53 -0700117static inline void inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo)
Eric Dumazeted2e9232015-09-19 09:08:34 -0700118{
119 __inet_twsk_schedule(tw, timeo, false);
120}
121
Raanan Avargil8695a142015-10-01 04:48:53 -0700122static inline void inet_twsk_reschedule(struct inet_timewait_sock *tw, int timeo)
Eric Dumazeted2e9232015-09-19 09:08:34 -0700123{
124 __inet_twsk_schedule(tw, timeo, true);
125}
126
Eric Dumazetdbe7faa2015-07-08 14:28:30 -0700127void inet_twsk_deschedule_put(struct inet_timewait_sock *tw);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900128
Joe Perches1fd51152013-09-21 10:22:41 -0700129void inet_twsk_purge(struct inet_hashinfo *hashinfo,
130 struct inet_timewait_death_row *twdr, int family);
Daniel Lezcanod3154922008-09-08 13:17:27 -0700131
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900132static inline
133struct net *twsk_net(const struct inet_timewait_sock *twsk)
134{
Eric Dumazetf943cbe2011-12-14 04:58:33 +0000135 return read_pnet(&twsk->tw_net);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900136}
137
138static inline
Denis V. Lunevf5aa23f2008-03-26 00:48:17 -0700139void twsk_net_set(struct inet_timewait_sock *twsk, struct net *net)
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900140{
Eric Dumazetf943cbe2011-12-14 04:58:33 +0000141 write_pnet(&twsk->tw_net, net);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900142}
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700143#endif /* _INET_TIMEWAIT_SOCK_ */