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 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 18 19#include <linux/kmemcheck.h> 20#include <linux/list.h> 21#include <linux/module.h> 22#include <linux/timer.h> 23#include <linux/types.h> 24#include <linux/workqueue.h> 25 26#include <net/inet_sock.h> 27#include <net/sock.h> 28#include <net/tcp_states.h> 29#include <net/timewait_sock.h> 30 31#include <asm/atomic.h> 32 33struct inet_hashinfo; 34 35#define INET_TWDR_RECYCLE_SLOTS_LOG 5 36#define INET_TWDR_RECYCLE_SLOTS (1 << INET_TWDR_RECYCLE_SLOTS_LOG) 37 38/* 39 * If time > 4sec, it is "slow" path, no recycling is required, 40 * so that we select tick to get range about 4 seconds. 41 */ 42#if (SHIFT_HZ >= 5) 43#define INET_TWDR_RECYCLE_TICK (SHIFT_HZ + 2 - INET_TWDR_RECYCLE_SLOTS_LOG) 44#else 45#define INET_TWDR_RECYCLE_TICK (1) 46#endif 47 48/* TIME_WAIT reaping mechanism. */ 49#define INET_TWDR_TWKILL_SLOTS 8 /* Please keep this a power of 2. */ 50 51#define INET_TWDR_TWKILL_QUOTA 100 52 53struct inet_timewait_death_row { 54 /* Short-time timewait calendar */ 55 int twcal_hand; 56 unsigned long twcal_jiffie; 57 struct timer_list twcal_timer; 58 struct hlist_head twcal_row[INET_TWDR_RECYCLE_SLOTS]; 59 60 spinlock_t death_lock; 61 int tw_count; 62 int period; 63 u32 thread_slots; 64 struct work_struct twkill_work; 65 struct timer_list tw_timer; 66 int slot; 67 struct hlist_head cells[INET_TWDR_TWKILL_SLOTS]; 68 struct inet_hashinfo *hashinfo; 69 int sysctl_tw_recycle; 70 int sysctl_max_tw_buckets; 71}; 72 73extern void inet_twdr_hangman(unsigned long data); 74extern void inet_twdr_twkill_work(struct work_struct *work); 75extern void inet_twdr_twcal_tick(unsigned long data); 76 77#if (BITS_PER_LONG == 64) 78#define INET_TIMEWAIT_ADDRCMP_ALIGN_BYTES 8 79#else 80#define INET_TIMEWAIT_ADDRCMP_ALIGN_BYTES 4 81#endif 82 83struct inet_bind_bucket; 84 85/* 86 * This is a TIME_WAIT sock. It works around the memory consumption 87 * problems of sockets in such a state on heavily loaded servers, but 88 * without violating the protocol specification. 89 */ 90struct inet_timewait_sock { 91 /* 92 * Now struct sock also uses sock_common, so please just 93 * don't add nothing before this first member (__tw_common) --acme 94 */ 95 struct sock_common __tw_common; 96#define tw_family __tw_common.skc_family 97#define tw_state __tw_common.skc_state 98#define tw_reuse __tw_common.skc_reuse 99#define tw_bound_dev_if __tw_common.skc_bound_dev_if 100#define tw_node __tw_common.skc_nulls_node 101#define tw_bind_node __tw_common.skc_bind_node 102#define tw_refcnt __tw_common.skc_refcnt 103#define tw_hash __tw_common.skc_hash 104#define tw_prot __tw_common.skc_prot 105#define tw_net __tw_common.skc_net 106 int tw_timeout; 107 volatile unsigned char tw_substate; 108 /* 3 bits hole, try to pack */ 109 unsigned char tw_rcv_wscale; 110 /* Socket demultiplex comparisons on incoming packets. */ 111 /* these five are in inet_sock */ 112 __be16 tw_sport; 113 __be32 tw_daddr __attribute__((aligned(INET_TIMEWAIT_ADDRCMP_ALIGN_BYTES))); 114 __be32 tw_rcv_saddr; 115 __be16 tw_dport; 116 __u16 tw_num; 117 kmemcheck_bitfield_begin(flags); 118 /* And these are ours. */ 119 unsigned int tw_ipv6only : 1, 120 tw_transparent : 1, 121 tw_pad : 14, /* 14 bits hole */ 122 tw_ipv6_offset : 16; 123 kmemcheck_bitfield_end(flags); 124 unsigned long tw_ttd; 125 struct inet_bind_bucket *tw_tb; 126 struct hlist_node tw_death_node; 127}; 128 129static inline void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw, 130 struct hlist_nulls_head *list) 131{ 132 hlist_nulls_add_head_rcu(&tw->tw_node, list); 133} 134 135static inline void inet_twsk_add_bind_node(struct inet_timewait_sock *tw, 136 struct hlist_head *list) 137{ 138 hlist_add_head(&tw->tw_bind_node, list); 139} 140 141static inline int inet_twsk_dead_hashed(const struct inet_timewait_sock *tw) 142{ 143 return !hlist_unhashed(&tw->tw_death_node); 144} 145 146static inline void inet_twsk_dead_node_init(struct inet_timewait_sock *tw) 147{ 148 tw->tw_death_node.pprev = NULL; 149} 150 151static inline void __inet_twsk_del_dead_node(struct inet_timewait_sock *tw) 152{ 153 __hlist_del(&tw->tw_death_node); 154 inet_twsk_dead_node_init(tw); 155} 156 157static inline int inet_twsk_del_dead_node(struct inet_timewait_sock *tw) 158{ 159 if (inet_twsk_dead_hashed(tw)) { 160 __inet_twsk_del_dead_node(tw); 161 return 1; 162 } 163 return 0; 164} 165 166#define inet_twsk_for_each(tw, node, head) \ 167 hlist_nulls_for_each_entry(tw, node, head, tw_node) 168 169#define inet_twsk_for_each_inmate(tw, node, jail) \ 170 hlist_for_each_entry(tw, node, jail, tw_death_node) 171 172#define inet_twsk_for_each_inmate_safe(tw, node, safe, jail) \ 173 hlist_for_each_entry_safe(tw, node, safe, jail, tw_death_node) 174 175static inline struct inet_timewait_sock *inet_twsk(const struct sock *sk) 176{ 177 return (struct inet_timewait_sock *)sk; 178} 179 180static inline __be32 inet_rcv_saddr(const struct sock *sk) 181{ 182 return likely(sk->sk_state != TCP_TIME_WAIT) ? 183 inet_sk(sk)->inet_rcv_saddr : inet_twsk(sk)->tw_rcv_saddr; 184} 185 186extern void inet_twsk_put(struct inet_timewait_sock *tw); 187 188extern int inet_twsk_unhash(struct inet_timewait_sock *tw); 189 190extern int inet_twsk_bind_unhash(struct inet_timewait_sock *tw, 191 struct inet_hashinfo *hashinfo); 192 193extern struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, 194 const int state); 195 196extern void __inet_twsk_hashdance(struct inet_timewait_sock *tw, 197 struct sock *sk, 198 struct inet_hashinfo *hashinfo); 199 200extern void inet_twsk_schedule(struct inet_timewait_sock *tw, 201 struct inet_timewait_death_row *twdr, 202 const int timeo, const int timewait_len); 203extern void inet_twsk_deschedule(struct inet_timewait_sock *tw, 204 struct inet_timewait_death_row *twdr); 205 206extern void inet_twsk_purge(struct inet_hashinfo *hashinfo, 207 struct inet_timewait_death_row *twdr, int family); 208 209static inline 210struct net *twsk_net(const struct inet_timewait_sock *twsk) 211{ 212#ifdef CONFIG_NET_NS 213 return rcu_dereference_raw(twsk->tw_net); /* protected by locking, */ 214 /* reference counting, */ 215 /* initialization, or RCU. */ 216#else 217 return &init_net; 218#endif 219} 220 221static inline 222void twsk_net_set(struct inet_timewait_sock *twsk, struct net *net) 223{ 224#ifdef CONFIG_NET_NS 225 rcu_assign_pointer(twsk->tw_net, net); 226#endif 227} 228#endif /* _INET_TIMEWAIT_SOCK_ */ 229