blob: c9b3eb70f340d48ffe60105622bee367c7ea848f [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 Dumazet9fe516b2014-06-27 08:36:16 -070058#define tw_ipv6only __tw_common.skc_ipv6only
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070059#define tw_bound_dev_if __tw_common.skc_bound_dev_if
Eric Dumazet3ab5aee2008-11-16 19:40:17 -080060#define tw_node __tw_common.skc_nulls_node
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070061#define tw_bind_node __tw_common.skc_bind_node
62#define tw_refcnt __tw_common.skc_refcnt
Eric Dumazet81c3d542005-10-03 14:13:38 -070063#define tw_hash __tw_common.skc_hash
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070064#define tw_prot __tw_common.skc_prot
Eric W. Biederman07feaeb2007-09-12 11:58:02 +020065#define tw_net __tw_common.skc_net
Eric Dumazet68835ab2010-11-30 19:04:07 +000066#define tw_daddr __tw_common.skc_daddr
Eric Dumazetefe42082013-10-03 15:42:29 -070067#define tw_v6_daddr __tw_common.skc_v6_daddr
Eric Dumazet68835ab2010-11-30 19:04:07 +000068#define tw_rcv_saddr __tw_common.skc_rcv_saddr
Eric Dumazetefe42082013-10-03 15:42:29 -070069#define tw_v6_rcv_saddr __tw_common.skc_v6_rcv_saddr
Eric Dumazetce43b032012-11-30 09:49:27 +000070#define tw_dport __tw_common.skc_dport
71#define tw_num __tw_common.skc_num
Eric Dumazet33cf7c92015-03-11 18:53:14 -070072#define tw_cookie __tw_common.skc_cookie
Eric Dumazet8e5eb542015-10-08 19:33:22 -070073#define tw_dr __tw_common.skc_tw_dr
Eric Dumazetce43b032012-11-30 09:49:27 +000074
Arnaldo Carvalho de Meload8bb782008-02-03 04:08:26 -080075 int tw_timeout;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070076 volatile unsigned char tw_substate;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070077 unsigned char tw_rcv_wscale;
Eric Dumazet68835ab2010-11-30 19:04:07 +000078
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070079 /* Socket demultiplex comparisons on incoming packets. */
Eric Dumazet68835ab2010-11-30 19:04:07 +000080 /* these three are in inet_sock */
Al Viro23f33c22006-09-27 18:43:50 -070081 __be16 tw_sport;
Vegard Nossum9e337b02008-10-18 17:37:51 +020082 kmemcheck_bitfield_begin(flags);
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070083 /* And these are ours. */
Eric Dumazet789f5582015-04-12 18:51:09 -070084 unsigned int tw_kill : 1,
Eric Dumazetabf90cc2009-10-18 22:48:51 +000085 tw_transparent : 1,
Florent Fourcot1d13a962014-01-16 17:21:22 +010086 tw_flowlabel : 20,
87 tw_pad : 2, /* 2 bits hole */
88 tw_tos : 8;
Vegard Nossum9e337b02008-10-18 17:37:51 +020089 kmemcheck_bitfield_end(flags);
Eric Dumazet789f5582015-04-12 18:51:09 -070090 struct timer_list tw_timer;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070091 struct inet_bind_bucket *tw_tb;
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070092};
Eric Dumazetb903d322011-10-27 00:44:35 -040093#define tw_tclass tw_tos
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070094
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070095static inline struct inet_timewait_sock *inet_twsk(const struct sock *sk)
96{
97 return (struct inet_timewait_sock *)sk;
98}
99
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700100void inet_twsk_free(struct inet_timewait_sock *tw);
Joe Perches1fd51152013-09-21 10:22:41 -0700101void inet_twsk_put(struct inet_timewait_sock *tw);
Arnaldo Carvalho de Meloe48c4142005-08-09 20:09:46 -0700102
Eric Dumazetfc01538f2015-07-08 14:28:29 -0700103void inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
104 struct inet_hashinfo *hashinfo);
Eric Dumazet3cdaeda2009-12-04 03:47:42 +0000105
Joe Perches1fd51152013-09-21 10:22:41 -0700106struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
Eric Dumazet789f5582015-04-12 18:51:09 -0700107 struct inet_timewait_death_row *dr,
Joe Perches1fd51152013-09-21 10:22:41 -0700108 const int state);
Arnaldo Carvalho de Meloc6762702005-08-09 20:09:59 -0700109
Joe Perches1fd51152013-09-21 10:22:41 -0700110void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
111 struct inet_hashinfo *hashinfo);
Arnaldo Carvalho de Melo696ab2d2005-08-09 20:45:03 -0700112
Eric Dumazeted2e9232015-09-19 09:08:34 -0700113void __inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo,
114 bool rearm);
115
Raanan Avargil8695a142015-10-01 04:48:53 -0700116static inline void inet_twsk_schedule(struct inet_timewait_sock *tw, int timeo)
Eric Dumazeted2e9232015-09-19 09:08:34 -0700117{
118 __inet_twsk_schedule(tw, timeo, false);
119}
120
Raanan Avargil8695a142015-10-01 04:48:53 -0700121static inline void inet_twsk_reschedule(struct inet_timewait_sock *tw, int timeo)
Eric Dumazeted2e9232015-09-19 09:08:34 -0700122{
123 __inet_twsk_schedule(tw, timeo, true);
124}
125
Eric Dumazetdbe7faa2015-07-08 14:28:30 -0700126void inet_twsk_deschedule_put(struct inet_timewait_sock *tw);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900127
Joe Perches1fd51152013-09-21 10:22:41 -0700128void inet_twsk_purge(struct inet_hashinfo *hashinfo,
129 struct inet_timewait_death_row *twdr, int family);
Daniel Lezcanod3154922008-09-08 13:17:27 -0700130
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900131static inline
132struct net *twsk_net(const struct inet_timewait_sock *twsk)
133{
Eric Dumazetf943cbe2011-12-14 04:58:33 +0000134 return read_pnet(&twsk->tw_net);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900135}
136
137static inline
Denis V. Lunevf5aa23f2008-03-26 00:48:17 -0700138void twsk_net_set(struct inet_timewait_sock *twsk, struct net *net)
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900139{
Eric Dumazetf943cbe2011-12-14 04:58:33 +0000140 write_pnet(&twsk->tw_net, net);
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900141}
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700142#endif /* _INET_TIMEWAIT_SOCK_ */