1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * NET Generic infrastructure for INET connection oriented protocols.
4 *
5 * Definitions for inet_connection_sock
6 *
7 * Authors: Many people, see the TCP sources
8 *
9 * From code originally in TCP
10 */
11 #ifndef _INET_CONNECTION_SOCK_H
12 #define _INET_CONNECTION_SOCK_H
13
14 #include <linux/compiler.h>
15 #include <linux/string.h>
16 #include <linux/timer.h>
17 #include <linux/poll.h>
18 #include <linux/kernel.h>
19 #include <linux/sockptr.h>
20
21 #include <net/inet_sock.h>
22 #include <net/request_sock.h>
23 #if defined(CONFIG_TCP_NATA_URC) || defined(CONFIG_TCP_NATA_STL)
24 #include <net/nata.h>
25 #endif
26
27 /* Cancel timers, when they are not required. */
28 #undef INET_CSK_CLEAR_TIMERS
29
30 struct inet_bind_bucket;
31 struct tcp_congestion_ops;
32
33 /*
34 * Pointers to address related TCP functions
35 * (i.e. things that depend on the address family)
36 */
37 struct inet_connection_sock_af_ops {
38 int (*queue_xmit)(struct sock *sk, struct sk_buff *skb, struct flowi *fl);
39 void (*send_check)(struct sock *sk, struct sk_buff *skb);
40 int (*rebuild_header)(struct sock *sk);
41 void (*sk_rx_dst_set)(struct sock *sk, const struct sk_buff *skb);
42 int (*conn_request)(struct sock *sk, struct sk_buff *skb);
43 struct sock *(*syn_recv_sock)(const struct sock *sk, struct sk_buff *skb,
44 struct request_sock *req,
45 struct dst_entry *dst,
46 struct request_sock *req_unhash,
47 bool *own_req);
48 u16 net_header_len;
49 u16 net_frag_header_len;
50 u16 sockaddr_len;
51 int (*setsockopt)(struct sock *sk, int level, int optname,
52 sockptr_t optval, unsigned int optlen);
53 int (*getsockopt)(struct sock *sk, int level, int optname,
54 char __user *optval, int __user *optlen);
55 void (*addr2sockaddr)(struct sock *sk, struct sockaddr *);
56 void (*mtu_reduced)(struct sock *sk);
57 };
58
59 /** inet_connection_sock - INET connection oriented sock
60 *
61 * @icsk_accept_queue: FIFO of established children
62 * @icsk_bind_hash: Bind node
63 * @icsk_timeout: Timeout
64 * @icsk_retransmit_timer: Resend (no ack)
65 * @icsk_rto: Retransmit timeout
66 * @icsk_pmtu_cookie Last pmtu seen by socket
67 * @icsk_ca_ops Pluggable congestion control hook
68 * @icsk_af_ops Operations which are AF_INET{4,6} specific
69 * @icsk_ulp_ops Pluggable ULP control hook
70 * @icsk_ulp_data ULP private data
71 * @icsk_clean_acked Clean acked data hook
72 * @icsk_listen_portaddr_node hash to the portaddr listener hashtable
73 * @icsk_ca_state: Congestion control state
74 * @icsk_retransmits: Number of unrecovered [RTO] timeouts
75 * @icsk_pending: Scheduled timer event
76 * @icsk_backoff: Backoff
77 * @icsk_syn_retries: Number of allowed SYN (or equivalent) retries
78 * @icsk_probes_out: unanswered 0 window probes
79 * @icsk_ext_hdr_len: Network protocol overhead (IP/IPv6 options)
80 * @icsk_ack: Delayed ACK control data
81 * @icsk_mtup; MTU probing control data
82 * @icsk_probes_tstamp: Probe timestamp (cleared by non-zero window ack)
83 * @icsk_user_timeout: TCP_USER_TIMEOUT value
84 */
85 struct inet_connection_sock {
86 /* inet_sock has to be the first member! */
87 struct inet_sock icsk_inet;
88 struct request_sock_queue icsk_accept_queue;
89 struct inet_bind_bucket *icsk_bind_hash;
90 unsigned long icsk_timeout;
91 struct timer_list icsk_retransmit_timer;
92 struct timer_list icsk_delack_timer;
93 __u32 icsk_rto;
94 __u32 icsk_rto_min;
95 __u32 icsk_delack_max;
96 __u32 icsk_pmtu_cookie;
97 const struct tcp_congestion_ops *icsk_ca_ops;
98 const struct inet_connection_sock_af_ops *icsk_af_ops;
99 const struct tcp_ulp_ops *icsk_ulp_ops;
100 void __rcu *icsk_ulp_data;
101 void (*icsk_clean_acked)(struct sock *sk, u32 acked_seq);
102 struct hlist_node icsk_listen_portaddr_node;
103 unsigned int (*icsk_sync_mss)(struct sock *sk, u32 pmtu);
104 __u8 icsk_ca_state:5,
105 icsk_ca_initialized:1,
106 icsk_ca_setsockopt:1,
107 icsk_ca_dst_locked:1;
108 __u8 icsk_retransmits;
109 __u8 icsk_pending;
110 __u8 icsk_backoff;
111 __u8 icsk_syn_retries;
112 __u8 icsk_probes_out;
113 __u16 icsk_ext_hdr_len;
114 #if defined(CONFIG_TCP_NATA_URC) || defined(CONFIG_TCP_NATA_STL)
115 __u8 nata_retries_enabled:1,
116 nata_reserved:7;
117 __u8 nata_data_retries;
118 __u8 nata_retries_type;
119 __u32 nata_syn_rto;
120 __u32 nata_data_rto;
121 #endif
122 struct {
123 __u8 pending; /* ACK is pending */
124 __u8 quick; /* Scheduled number of quick acks */
125 __u8 pingpong; /* The session is interactive */
126 __u8 retry; /* Number of attempts */
127 __u32 ato; /* Predicted tick of soft clock */
128 unsigned long timeout; /* Currently scheduled timeout */
129 __u32 lrcvtime; /* timestamp of last received data packet */
130 __u16 last_seg_size; /* Size of last incoming segment */
131 __u16 rcv_mss; /* MSS used for delayed ACK decisions */
132 } icsk_ack;
133 struct {
134 int enabled;
135
136 /* Range of MTUs to search */
137 int search_high;
138 int search_low;
139
140 /* Information on the current probe. */
141 int probe_size;
142
143 u32 probe_timestamp;
144 } icsk_mtup;
145 u32 icsk_probes_tstamp;
146 u32 icsk_user_timeout;
147
148 u64 icsk_ca_priv[104 / sizeof(u64)];
149 #define ICSK_CA_PRIV_SIZE (13 * sizeof(u64))
150 };
151
152 #define ICSK_TIME_RETRANS 1 /* Retransmit timer */
153 #define ICSK_TIME_DACK 2 /* Delayed ack timer */
154 #define ICSK_TIME_PROBE0 3 /* Zero window probe timer */
155 #define ICSK_TIME_EARLY_RETRANS 4 /* Early retransmit timer */
156 #define ICSK_TIME_LOSS_PROBE 5 /* Tail loss probe timer */
157 #define ICSK_TIME_REO_TIMEOUT 6 /* Reordering timer */
158
inet_csk(const struct sock * sk)159 static inline struct inet_connection_sock *inet_csk(const struct sock *sk)
160 {
161 return (struct inet_connection_sock *)sk;
162 }
163
inet_csk_ca(const struct sock * sk)164 static inline void *inet_csk_ca(const struct sock *sk)
165 {
166 return (void *)inet_csk(sk)->icsk_ca_priv;
167 }
168
169 struct sock *inet_csk_clone_lock(const struct sock *sk,
170 const struct request_sock *req,
171 const gfp_t priority);
172
173 enum inet_csk_ack_state_t {
174 ICSK_ACK_SCHED = 1,
175 ICSK_ACK_TIMER = 2,
176 ICSK_ACK_PUSHED = 4,
177 ICSK_ACK_PUSHED2 = 8,
178 ICSK_ACK_NOW = 16 /* Send the next ACK immediately (once) */
179 };
180
181 void inet_csk_init_xmit_timers(struct sock *sk,
182 void (*retransmit_handler)(struct timer_list *),
183 void (*delack_handler)(struct timer_list *),
184 void (*keepalive_handler)(struct timer_list *));
185 void inet_csk_clear_xmit_timers(struct sock *sk);
186 void inet_csk_clear_xmit_timers_sync(struct sock *sk);
187
inet_csk_schedule_ack(struct sock * sk)188 static inline void inet_csk_schedule_ack(struct sock *sk)
189 {
190 inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_SCHED;
191 }
192
inet_csk_ack_scheduled(const struct sock * sk)193 static inline int inet_csk_ack_scheduled(const struct sock *sk)
194 {
195 return inet_csk(sk)->icsk_ack.pending & ICSK_ACK_SCHED;
196 }
197
inet_csk_delack_init(struct sock * sk)198 static inline void inet_csk_delack_init(struct sock *sk)
199 {
200 memset(&inet_csk(sk)->icsk_ack, 0, sizeof(inet_csk(sk)->icsk_ack));
201 }
202
203 void inet_csk_delete_keepalive_timer(struct sock *sk);
204 void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long timeout);
205
inet_csk_clear_xmit_timer(struct sock * sk,const int what)206 static inline void inet_csk_clear_xmit_timer(struct sock *sk, const int what)
207 {
208 struct inet_connection_sock *icsk = inet_csk(sk);
209
210 if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0) {
211 icsk->icsk_pending = 0;
212 #ifdef INET_CSK_CLEAR_TIMERS
213 sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
214 #endif
215 } else if (what == ICSK_TIME_DACK) {
216 icsk->icsk_ack.pending = 0;
217 icsk->icsk_ack.retry = 0;
218 #ifdef INET_CSK_CLEAR_TIMERS
219 sk_stop_timer(sk, &icsk->icsk_delack_timer);
220 #endif
221 } else {
222 pr_debug("inet_csk BUG: unknown timer value\n");
223 }
224 }
225
226 #if defined(CONFIG_TCP_NATA_URC) || defined(CONFIG_TCP_NATA_STL)
get_nata_rto(struct sock * sk,struct inet_connection_sock * icsk,unsigned long when,const int what)227 static inline unsigned long get_nata_rto(struct sock *sk,
228 struct inet_connection_sock *icsk,
229 unsigned long when, const int what)
230 {
231 unsigned long when_nata;
232 unsigned long shift;
233
234 if (!icsk->nata_retries_enabled)
235 return when;
236
237 switch (what) {
238 case ICSK_TIME_RETRANS:
239 case ICSK_TIME_EARLY_RETRANS:
240 case ICSK_TIME_LOSS_PROBE:
241 case ICSK_TIME_REO_TIMEOUT:
242 break;
243 default:
244 return when;
245 }
246
247 if (icsk->nata_retries_type == NATA_STL)
248 return sk->sk_state == TCP_SYN_SENT ?
249 icsk->nata_syn_rto : icsk->nata_data_rto;
250
251 when_nata = icsk->nata_data_rto;
252 if (icsk->icsk_retransmits > icsk->nata_data_retries) {
253 shift = icsk->icsk_retransmits - icsk->nata_data_retries;
254 if (shift > MAX_SHIFT) {
255 when_nata = NATA_RTO_MAX;
256 } else {
257 when_nata <<= shift;
258 }
259 }
260 return min(when, when_nata);
261 }
262 #endif
263
264 /*
265 * Reset the retransmission timer
266 */
inet_csk_reset_xmit_timer(struct sock * sk,const int what,unsigned long when,const unsigned long max_when)267 static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int what,
268 unsigned long when,
269 const unsigned long max_when)
270 {
271 struct inet_connection_sock *icsk = inet_csk(sk);
272
273 #if defined(CONFIG_TCP_NATA_URC) || defined(CONFIG_TCP_NATA_STL)
274 when = get_nata_rto(sk, icsk, when, what);
275 #endif
276
277 if (when > max_when) {
278 pr_debug("reset_xmit_timer: sk=%p %d when=0x%lx, caller=%p\n",
279 sk, what, when, (void *)_THIS_IP_);
280 when = max_when;
281 }
282
283 if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0 ||
284 what == ICSK_TIME_EARLY_RETRANS || what == ICSK_TIME_LOSS_PROBE ||
285 what == ICSK_TIME_REO_TIMEOUT) {
286 icsk->icsk_pending = what;
287 icsk->icsk_timeout = jiffies + when;
288 sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
289 } else if (what == ICSK_TIME_DACK) {
290 icsk->icsk_ack.pending |= ICSK_ACK_TIMER;
291 icsk->icsk_ack.timeout = jiffies + when;
292 sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
293 } else {
294 pr_debug("inet_csk BUG: unknown timer value\n");
295 }
296 }
297
298 static inline unsigned long
inet_csk_rto_backoff(const struct inet_connection_sock * icsk,unsigned long max_when)299 inet_csk_rto_backoff(const struct inet_connection_sock *icsk,
300 unsigned long max_when)
301 {
302 u64 when = (u64)icsk->icsk_rto << icsk->icsk_backoff;
303
304 return (unsigned long)min_t(u64, when, max_when);
305 }
306
307 struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern);
308
309 int inet_csk_get_port(struct sock *sk, unsigned short snum);
310
311 struct dst_entry *inet_csk_route_req(const struct sock *sk, struct flowi4 *fl4,
312 const struct request_sock *req);
313 struct dst_entry *inet_csk_route_child_sock(const struct sock *sk,
314 struct sock *newsk,
315 const struct request_sock *req);
316
317 struct sock *inet_csk_reqsk_queue_add(struct sock *sk,
318 struct request_sock *req,
319 struct sock *child);
320 void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
321 unsigned long timeout);
322 struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,
323 struct request_sock *req,
324 bool own_req);
325
inet_csk_reqsk_queue_added(struct sock * sk)326 static inline void inet_csk_reqsk_queue_added(struct sock *sk)
327 {
328 reqsk_queue_added(&inet_csk(sk)->icsk_accept_queue);
329 }
330
inet_csk_reqsk_queue_len(const struct sock * sk)331 static inline int inet_csk_reqsk_queue_len(const struct sock *sk)
332 {
333 return reqsk_queue_len(&inet_csk(sk)->icsk_accept_queue);
334 }
335
inet_csk_reqsk_queue_is_full(const struct sock * sk)336 static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk)
337 {
338 return inet_csk_reqsk_queue_len(sk) >= sk->sk_max_ack_backlog;
339 }
340
341 bool inet_csk_reqsk_queue_drop(struct sock *sk, struct request_sock *req);
342 void inet_csk_reqsk_queue_drop_and_put(struct sock *sk, struct request_sock *req);
343
inet_csk_prepare_for_destroy_sock(struct sock * sk)344 static inline void inet_csk_prepare_for_destroy_sock(struct sock *sk)
345 {
346 /* The below has to be done to allow calling inet_csk_destroy_sock */
347 sock_set_flag(sk, SOCK_DEAD);
348 this_cpu_inc(*sk->sk_prot->orphan_count);
349 }
350
351 void inet_csk_destroy_sock(struct sock *sk);
352 void inet_csk_prepare_forced_close(struct sock *sk);
353
354 /*
355 * LISTEN is a special case for poll..
356 */
inet_csk_listen_poll(const struct sock * sk)357 static inline __poll_t inet_csk_listen_poll(const struct sock *sk)
358 {
359 return !reqsk_queue_empty(&inet_csk(sk)->icsk_accept_queue) ?
360 (EPOLLIN | EPOLLRDNORM) : 0;
361 }
362
363 int inet_csk_listen_start(struct sock *sk, int backlog);
364 void inet_csk_listen_stop(struct sock *sk);
365
366 void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr);
367
368 /* update the fast reuse flag when adding a socket */
369 void inet_csk_update_fastreuse(struct inet_bind_bucket *tb,
370 struct sock *sk);
371
372 struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu);
373
374 #define TCP_PINGPONG_THRESH 1
375
inet_csk_enter_pingpong_mode(struct sock * sk)376 static inline void inet_csk_enter_pingpong_mode(struct sock *sk)
377 {
378 inet_csk(sk)->icsk_ack.pingpong = TCP_PINGPONG_THRESH;
379 }
380
inet_csk_exit_pingpong_mode(struct sock * sk)381 static inline void inet_csk_exit_pingpong_mode(struct sock *sk)
382 {
383 inet_csk(sk)->icsk_ack.pingpong = 0;
384 }
385
inet_csk_in_pingpong_mode(struct sock * sk)386 static inline bool inet_csk_in_pingpong_mode(struct sock *sk)
387 {
388 return inet_csk(sk)->icsk_ack.pingpong >= TCP_PINGPONG_THRESH;
389 }
390
inet_csk_has_ulp(struct sock * sk)391 static inline bool inet_csk_has_ulp(struct sock *sk)
392 {
393 return inet_sk(sk)->is_icsk && !!inet_csk(sk)->icsk_ulp_ops;
394 }
395
inet_init_csk_locks(struct sock * sk)396 static inline void inet_init_csk_locks(struct sock *sk)
397 {
398 struct inet_connection_sock *icsk = inet_csk(sk);
399
400 spin_lock_init(&icsk->icsk_accept_queue.rskq_lock);
401 spin_lock_init(&icsk->icsk_accept_queue.fastopenq.lock);
402 }
403
404 #endif /* _INET_CONNECTION_SOCK_H */
405