xref: /openbmc/linux/include/net/request_sock.h (revision 4dfc2817)
12e6599cbSArnaldo Carvalho de Melo /*
22e6599cbSArnaldo Carvalho de Melo  * NET		Generic infrastructure for Network protocols.
32e6599cbSArnaldo Carvalho de Melo  *
42e6599cbSArnaldo Carvalho de Melo  *		Definitions for request_sock
52e6599cbSArnaldo Carvalho de Melo  *
62e6599cbSArnaldo Carvalho de Melo  * Authors:	Arnaldo Carvalho de Melo <acme@conectiva.com.br>
72e6599cbSArnaldo Carvalho de Melo  *
82e6599cbSArnaldo Carvalho de Melo  * 		From code originally in include/net/tcp.h
92e6599cbSArnaldo Carvalho de Melo  *
102e6599cbSArnaldo Carvalho de Melo  *		This program is free software; you can redistribute it and/or
112e6599cbSArnaldo Carvalho de Melo  *		modify it under the terms of the GNU General Public License
122e6599cbSArnaldo Carvalho de Melo  *		as published by the Free Software Foundation; either version
132e6599cbSArnaldo Carvalho de Melo  *		2 of the License, or (at your option) any later version.
142e6599cbSArnaldo Carvalho de Melo  */
152e6599cbSArnaldo Carvalho de Melo #ifndef _REQUEST_SOCK_H
162e6599cbSArnaldo Carvalho de Melo #define _REQUEST_SOCK_H
172e6599cbSArnaldo Carvalho de Melo 
182e6599cbSArnaldo Carvalho de Melo #include <linux/slab.h>
190e87506fSArnaldo Carvalho de Melo #include <linux/spinlock.h>
202e6599cbSArnaldo Carvalho de Melo #include <linux/types.h>
210e87506fSArnaldo Carvalho de Melo 
222e6599cbSArnaldo Carvalho de Melo #include <net/sock.h>
232e6599cbSArnaldo Carvalho de Melo 
2460236fddSArnaldo Carvalho de Melo struct request_sock;
252e6599cbSArnaldo Carvalho de Melo struct sk_buff;
262e6599cbSArnaldo Carvalho de Melo struct dst_entry;
272e6599cbSArnaldo Carvalho de Melo struct proto;
282e6599cbSArnaldo Carvalho de Melo 
2960236fddSArnaldo Carvalho de Melo struct request_sock_ops {
302e6599cbSArnaldo Carvalho de Melo 	int		family;
312e6599cbSArnaldo Carvalho de Melo 	int		obj_size;
32e18b890bSChristoph Lameter 	struct kmem_cache	*slab;
332e6599cbSArnaldo Carvalho de Melo 	int		(*rtx_syn_ack)(struct sock *sk,
34fd80eb94SDenis V. Lunev 				       struct request_sock *req);
352e6599cbSArnaldo Carvalho de Melo 	void		(*send_ack)(struct sk_buff *skb,
3660236fddSArnaldo Carvalho de Melo 				    struct request_sock *req);
37cfb6eeb4SYOSHIFUJI Hideaki 	void		(*send_reset)(struct sock *sk,
38cfb6eeb4SYOSHIFUJI Hideaki 				      struct sk_buff *skb);
3960236fddSArnaldo Carvalho de Melo 	void		(*destructor)(struct request_sock *req);
402e6599cbSArnaldo Carvalho de Melo };
412e6599cbSArnaldo Carvalho de Melo 
4260236fddSArnaldo Carvalho de Melo /* struct request_sock - mini sock to represent a connection request
432e6599cbSArnaldo Carvalho de Melo  */
4460236fddSArnaldo Carvalho de Melo struct request_sock {
4560236fddSArnaldo Carvalho de Melo 	struct request_sock		*dl_next; /* Must be first member! */
462e6599cbSArnaldo Carvalho de Melo 	u16				mss;
472e6599cbSArnaldo Carvalho de Melo 	u8				retrans;
484dfc2817SFlorian Westphal 	u8				cookie_ts; /* syncookie: encode tcpopts in timestamp */
492e6599cbSArnaldo Carvalho de Melo 	/* The following two fields can be easily recomputed I think -AK */
502e6599cbSArnaldo Carvalho de Melo 	u32				window_clamp; /* window clamp at creation time */
512e6599cbSArnaldo Carvalho de Melo 	u32				rcv_wnd;	  /* rcv_wnd offered first time */
522e6599cbSArnaldo Carvalho de Melo 	u32				ts_recent;
532e6599cbSArnaldo Carvalho de Melo 	unsigned long			expires;
5472a3effaSEric Dumazet 	const struct request_sock_ops	*rsk_ops;
552e6599cbSArnaldo Carvalho de Melo 	struct sock			*sk;
564237c75cSVenkat Yekkirala 	u32				secid;
576b877699SVenkat Yekkirala 	u32				peer_secid;
582e6599cbSArnaldo Carvalho de Melo };
592e6599cbSArnaldo Carvalho de Melo 
6072a3effaSEric Dumazet static inline struct request_sock *reqsk_alloc(const struct request_sock_ops *ops)
612e6599cbSArnaldo Carvalho de Melo {
6254e6ecb2SChristoph Lameter 	struct request_sock *req = kmem_cache_alloc(ops->slab, GFP_ATOMIC);
632e6599cbSArnaldo Carvalho de Melo 
642e6599cbSArnaldo Carvalho de Melo 	if (req != NULL)
6560236fddSArnaldo Carvalho de Melo 		req->rsk_ops = ops;
662e6599cbSArnaldo Carvalho de Melo 
672e6599cbSArnaldo Carvalho de Melo 	return req;
682e6599cbSArnaldo Carvalho de Melo }
692e6599cbSArnaldo Carvalho de Melo 
7060236fddSArnaldo Carvalho de Melo static inline void __reqsk_free(struct request_sock *req)
712e6599cbSArnaldo Carvalho de Melo {
7260236fddSArnaldo Carvalho de Melo 	kmem_cache_free(req->rsk_ops->slab, req);
732e6599cbSArnaldo Carvalho de Melo }
742e6599cbSArnaldo Carvalho de Melo 
7560236fddSArnaldo Carvalho de Melo static inline void reqsk_free(struct request_sock *req)
762e6599cbSArnaldo Carvalho de Melo {
7760236fddSArnaldo Carvalho de Melo 	req->rsk_ops->destructor(req);
7860236fddSArnaldo Carvalho de Melo 	__reqsk_free(req);
792e6599cbSArnaldo Carvalho de Melo }
802e6599cbSArnaldo Carvalho de Melo 
810e87506fSArnaldo Carvalho de Melo extern int sysctl_max_syn_backlog;
820e87506fSArnaldo Carvalho de Melo 
832ad69c55SArnaldo Carvalho de Melo /** struct listen_sock - listen state
840e87506fSArnaldo Carvalho de Melo  *
850e87506fSArnaldo Carvalho de Melo  * @max_qlen_log - log_2 of maximal queued SYNs/REQUESTs
860e87506fSArnaldo Carvalho de Melo  */
872ad69c55SArnaldo Carvalho de Melo struct listen_sock {
880e87506fSArnaldo Carvalho de Melo 	u8			max_qlen_log;
890e87506fSArnaldo Carvalho de Melo 	/* 3 bytes hole, try to use */
900e87506fSArnaldo Carvalho de Melo 	int			qlen;
910e87506fSArnaldo Carvalho de Melo 	int			qlen_young;
920e87506fSArnaldo Carvalho de Melo 	int			clock_hand;
930e87506fSArnaldo Carvalho de Melo 	u32			hash_rnd;
9483e3609eSArnaldo Carvalho de Melo 	u32			nr_table_entries;
950e87506fSArnaldo Carvalho de Melo 	struct request_sock	*syn_table[0];
960e87506fSArnaldo Carvalho de Melo };
970e87506fSArnaldo Carvalho de Melo 
980e87506fSArnaldo Carvalho de Melo /** struct request_sock_queue - queue of request_socks
990e87506fSArnaldo Carvalho de Melo  *
1000e87506fSArnaldo Carvalho de Melo  * @rskq_accept_head - FIFO head of established children
1010e87506fSArnaldo Carvalho de Melo  * @rskq_accept_tail - FIFO tail of established children
102295f7324SArnaldo Carvalho de Melo  * @rskq_defer_accept - User waits for some data after accept()
1030e87506fSArnaldo Carvalho de Melo  * @syn_wait_lock - serializer
1040e87506fSArnaldo Carvalho de Melo  *
1050e87506fSArnaldo Carvalho de Melo  * %syn_wait_lock is necessary only to avoid proc interface having to grab the main
1060e87506fSArnaldo Carvalho de Melo  * lock sock while browsing the listening hash (otherwise it's deadlock prone).
1070e87506fSArnaldo Carvalho de Melo  *
1080e87506fSArnaldo Carvalho de Melo  * This lock is acquired in read mode only from listening_get_next() seq_file
1090e87506fSArnaldo Carvalho de Melo  * op and it's acquired in write mode _only_ from code that is actively
1100e87506fSArnaldo Carvalho de Melo  * changing rskq_accept_head. All readers that are holding the master sock lock
1110e87506fSArnaldo Carvalho de Melo  * don't need to grab this lock in read mode too as rskq_accept_head. writes
1120e87506fSArnaldo Carvalho de Melo  * are always protected from the main sock lock.
1130e87506fSArnaldo Carvalho de Melo  */
1140e87506fSArnaldo Carvalho de Melo struct request_sock_queue {
1150e87506fSArnaldo Carvalho de Melo 	struct request_sock	*rskq_accept_head;
1160e87506fSArnaldo Carvalho de Melo 	struct request_sock	*rskq_accept_tail;
1170e87506fSArnaldo Carvalho de Melo 	rwlock_t		syn_wait_lock;
118ec3c0982SPatrick McManus 	u16			rskq_defer_accept;
119ec3c0982SPatrick McManus 	/* 2 bytes hole, try to pack */
1202ad69c55SArnaldo Carvalho de Melo 	struct listen_sock	*listen_opt;
1210e87506fSArnaldo Carvalho de Melo };
1220e87506fSArnaldo Carvalho de Melo 
1230e87506fSArnaldo Carvalho de Melo extern int reqsk_queue_alloc(struct request_sock_queue *queue,
12472a3effaSEric Dumazet 			     unsigned int nr_table_entries);
1250e87506fSArnaldo Carvalho de Melo 
126dab6ba36SPavel Emelyanov extern void __reqsk_queue_destroy(struct request_sock_queue *queue);
12783e3609eSArnaldo Carvalho de Melo extern void reqsk_queue_destroy(struct request_sock_queue *queue);
12883e3609eSArnaldo Carvalho de Melo 
1290e87506fSArnaldo Carvalho de Melo static inline struct request_sock *
1300e87506fSArnaldo Carvalho de Melo 	reqsk_queue_yank_acceptq(struct request_sock_queue *queue)
1310e87506fSArnaldo Carvalho de Melo {
1320e87506fSArnaldo Carvalho de Melo 	struct request_sock *req = queue->rskq_accept_head;
1330e87506fSArnaldo Carvalho de Melo 
1343eb4801dSNorbert Kiesel 	queue->rskq_accept_head = NULL;
1350e87506fSArnaldo Carvalho de Melo 	return req;
1360e87506fSArnaldo Carvalho de Melo }
1370e87506fSArnaldo Carvalho de Melo 
1380e87506fSArnaldo Carvalho de Melo static inline int reqsk_queue_empty(struct request_sock_queue *queue)
1390e87506fSArnaldo Carvalho de Melo {
1400e87506fSArnaldo Carvalho de Melo 	return queue->rskq_accept_head == NULL;
1410e87506fSArnaldo Carvalho de Melo }
1420e87506fSArnaldo Carvalho de Melo 
1430e87506fSArnaldo Carvalho de Melo static inline void reqsk_queue_unlink(struct request_sock_queue *queue,
1440e87506fSArnaldo Carvalho de Melo 				      struct request_sock *req,
1450e87506fSArnaldo Carvalho de Melo 				      struct request_sock **prev_req)
1460e87506fSArnaldo Carvalho de Melo {
1470e87506fSArnaldo Carvalho de Melo 	write_lock(&queue->syn_wait_lock);
1480e87506fSArnaldo Carvalho de Melo 	*prev_req = req->dl_next;
1490e87506fSArnaldo Carvalho de Melo 	write_unlock(&queue->syn_wait_lock);
1500e87506fSArnaldo Carvalho de Melo }
1510e87506fSArnaldo Carvalho de Melo 
1520e87506fSArnaldo Carvalho de Melo static inline void reqsk_queue_add(struct request_sock_queue *queue,
1530e87506fSArnaldo Carvalho de Melo 				   struct request_sock *req,
1540e87506fSArnaldo Carvalho de Melo 				   struct sock *parent,
1550e87506fSArnaldo Carvalho de Melo 				   struct sock *child)
1560e87506fSArnaldo Carvalho de Melo {
1570e87506fSArnaldo Carvalho de Melo 	req->sk = child;
1580e87506fSArnaldo Carvalho de Melo 	sk_acceptq_added(parent);
1590e87506fSArnaldo Carvalho de Melo 
1600e87506fSArnaldo Carvalho de Melo 	if (queue->rskq_accept_head == NULL)
1610e87506fSArnaldo Carvalho de Melo 		queue->rskq_accept_head = req;
1620e87506fSArnaldo Carvalho de Melo 	else
1630e87506fSArnaldo Carvalho de Melo 		queue->rskq_accept_tail->dl_next = req;
1640e87506fSArnaldo Carvalho de Melo 
1650e87506fSArnaldo Carvalho de Melo 	queue->rskq_accept_tail = req;
1660e87506fSArnaldo Carvalho de Melo 	req->dl_next = NULL;
1670e87506fSArnaldo Carvalho de Melo }
1680e87506fSArnaldo Carvalho de Melo 
1690e87506fSArnaldo Carvalho de Melo static inline struct request_sock *reqsk_queue_remove(struct request_sock_queue *queue)
1700e87506fSArnaldo Carvalho de Melo {
1710e87506fSArnaldo Carvalho de Melo 	struct request_sock *req = queue->rskq_accept_head;
1720e87506fSArnaldo Carvalho de Melo 
1730e87506fSArnaldo Carvalho de Melo 	BUG_TRAP(req != NULL);
1740e87506fSArnaldo Carvalho de Melo 
1750e87506fSArnaldo Carvalho de Melo 	queue->rskq_accept_head = req->dl_next;
1760e87506fSArnaldo Carvalho de Melo 	if (queue->rskq_accept_head == NULL)
1770e87506fSArnaldo Carvalho de Melo 		queue->rskq_accept_tail = NULL;
1780e87506fSArnaldo Carvalho de Melo 
1790e87506fSArnaldo Carvalho de Melo 	return req;
1800e87506fSArnaldo Carvalho de Melo }
1810e87506fSArnaldo Carvalho de Melo 
1820e87506fSArnaldo Carvalho de Melo static inline struct sock *reqsk_queue_get_child(struct request_sock_queue *queue,
1830e87506fSArnaldo Carvalho de Melo 						 struct sock *parent)
1840e87506fSArnaldo Carvalho de Melo {
1850e87506fSArnaldo Carvalho de Melo 	struct request_sock *req = reqsk_queue_remove(queue);
1860e87506fSArnaldo Carvalho de Melo 	struct sock *child = req->sk;
1870e87506fSArnaldo Carvalho de Melo 
1880e87506fSArnaldo Carvalho de Melo 	BUG_TRAP(child != NULL);
1890e87506fSArnaldo Carvalho de Melo 
1900e87506fSArnaldo Carvalho de Melo 	sk_acceptq_removed(parent);
1910e87506fSArnaldo Carvalho de Melo 	__reqsk_free(req);
1920e87506fSArnaldo Carvalho de Melo 	return child;
1930e87506fSArnaldo Carvalho de Melo }
1940e87506fSArnaldo Carvalho de Melo 
1950e87506fSArnaldo Carvalho de Melo static inline int reqsk_queue_removed(struct request_sock_queue *queue,
1960e87506fSArnaldo Carvalho de Melo 				      struct request_sock *req)
1970e87506fSArnaldo Carvalho de Melo {
1982ad69c55SArnaldo Carvalho de Melo 	struct listen_sock *lopt = queue->listen_opt;
1990e87506fSArnaldo Carvalho de Melo 
2000e87506fSArnaldo Carvalho de Melo 	if (req->retrans == 0)
2010e87506fSArnaldo Carvalho de Melo 		--lopt->qlen_young;
2020e87506fSArnaldo Carvalho de Melo 
2030e87506fSArnaldo Carvalho de Melo 	return --lopt->qlen;
2040e87506fSArnaldo Carvalho de Melo }
2050e87506fSArnaldo Carvalho de Melo 
2060e87506fSArnaldo Carvalho de Melo static inline int reqsk_queue_added(struct request_sock_queue *queue)
2070e87506fSArnaldo Carvalho de Melo {
2082ad69c55SArnaldo Carvalho de Melo 	struct listen_sock *lopt = queue->listen_opt;
2090e87506fSArnaldo Carvalho de Melo 	const int prev_qlen = lopt->qlen;
2100e87506fSArnaldo Carvalho de Melo 
2110e87506fSArnaldo Carvalho de Melo 	lopt->qlen_young++;
2120e87506fSArnaldo Carvalho de Melo 	lopt->qlen++;
2130e87506fSArnaldo Carvalho de Melo 	return prev_qlen;
2140e87506fSArnaldo Carvalho de Melo }
2150e87506fSArnaldo Carvalho de Melo 
216463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len(const struct request_sock_queue *queue)
2170e87506fSArnaldo Carvalho de Melo {
2180e87506fSArnaldo Carvalho de Melo 	return queue->listen_opt != NULL ? queue->listen_opt->qlen : 0;
2190e87506fSArnaldo Carvalho de Melo }
2200e87506fSArnaldo Carvalho de Melo 
221463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_len_young(const struct request_sock_queue *queue)
2220e87506fSArnaldo Carvalho de Melo {
2230e87506fSArnaldo Carvalho de Melo 	return queue->listen_opt->qlen_young;
2240e87506fSArnaldo Carvalho de Melo }
2250e87506fSArnaldo Carvalho de Melo 
226463c84b9SArnaldo Carvalho de Melo static inline int reqsk_queue_is_full(const struct request_sock_queue *queue)
2270e87506fSArnaldo Carvalho de Melo {
2280e87506fSArnaldo Carvalho de Melo 	return queue->listen_opt->qlen >> queue->listen_opt->max_qlen_log;
2290e87506fSArnaldo Carvalho de Melo }
2300e87506fSArnaldo Carvalho de Melo 
2310e87506fSArnaldo Carvalho de Melo static inline void reqsk_queue_hash_req(struct request_sock_queue *queue,
2320e87506fSArnaldo Carvalho de Melo 					u32 hash, struct request_sock *req,
2338129765aSArnaldo Carvalho de Melo 					unsigned long timeout)
2340e87506fSArnaldo Carvalho de Melo {
2352ad69c55SArnaldo Carvalho de Melo 	struct listen_sock *lopt = queue->listen_opt;
2360e87506fSArnaldo Carvalho de Melo 
2370e87506fSArnaldo Carvalho de Melo 	req->expires = jiffies + timeout;
2380e87506fSArnaldo Carvalho de Melo 	req->retrans = 0;
2390e87506fSArnaldo Carvalho de Melo 	req->sk = NULL;
2400e87506fSArnaldo Carvalho de Melo 	req->dl_next = lopt->syn_table[hash];
2410e87506fSArnaldo Carvalho de Melo 
2420e87506fSArnaldo Carvalho de Melo 	write_lock(&queue->syn_wait_lock);
2430e87506fSArnaldo Carvalho de Melo 	lopt->syn_table[hash] = req;
2440e87506fSArnaldo Carvalho de Melo 	write_unlock(&queue->syn_wait_lock);
2450e87506fSArnaldo Carvalho de Melo }
2460e87506fSArnaldo Carvalho de Melo 
2472e6599cbSArnaldo Carvalho de Melo #endif /* _REQUEST_SOCK_H */
248