1/*
2 *  net/dccp/minisocks.c
3 *
4 *  An implementation of the DCCP protocol
5 *  Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 *
7 *	This program is free software; you can redistribute it and/or
8 *	modify it under the terms of the GNU General Public License
9 *	as published by the Free Software Foundation; either version
10 *	2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/dccp.h>
14#include <linux/kernel.h>
15#include <linux/skbuff.h>
16#include <linux/timer.h>
17
18#include <net/sock.h>
19#include <net/xfrm.h>
20#include <net/inet_timewait_sock.h>
21
22#include "ackvec.h"
23#include "ccid.h"
24#include "dccp.h"
25#include "feat.h"
26
27struct inet_timewait_death_row dccp_death_row = {
28	.sysctl_max_tw_buckets = NR_FILE * 2,
29	.period		= DCCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
30	.death_lock	= __SPIN_LOCK_UNLOCKED(dccp_death_row.death_lock),
31	.hashinfo	= &dccp_hashinfo,
32	.tw_timer	= TIMER_INITIALIZER(inet_twdr_hangman, 0,
33					    (unsigned long)&dccp_death_row),
34	.twkill_work	= __WORK_INITIALIZER(dccp_death_row.twkill_work,
35					     inet_twdr_twkill_work),
36/* Short-time timewait calendar */
37
38	.twcal_hand	= -1,
39	.twcal_timer	= TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
40					    (unsigned long)&dccp_death_row),
41};
42
43EXPORT_SYMBOL_GPL(dccp_death_row);
44
45void dccp_time_wait(struct sock *sk, int state, int timeo)
46{
47	struct inet_timewait_sock *tw = NULL;
48
49	if (dccp_death_row.tw_count < dccp_death_row.sysctl_max_tw_buckets)
50		tw = inet_twsk_alloc(sk, state);
51
52	if (tw != NULL) {
53		const struct inet_connection_sock *icsk = inet_csk(sk);
54		const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
55#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
56		if (tw->tw_family == PF_INET6) {
57			const struct ipv6_pinfo *np = inet6_sk(sk);
58			struct inet6_timewait_sock *tw6;
59
60			tw->tw_ipv6_offset = inet6_tw_offset(sk->sk_prot);
61			tw6 = inet6_twsk((struct sock *)tw);
62			ipv6_addr_copy(&tw6->tw_v6_daddr, &np->daddr);
63			ipv6_addr_copy(&tw6->tw_v6_rcv_saddr, &np->rcv_saddr);
64			tw->tw_ipv6only = np->ipv6only;
65		}
66#endif
67		/* Linkage updates. */
68		__inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
69
70		/* Get the TIME_WAIT timeout firing. */
71		if (timeo < rto)
72			timeo = rto;
73
74		tw->tw_timeout = DCCP_TIMEWAIT_LEN;
75		if (state == DCCP_TIME_WAIT)
76			timeo = DCCP_TIMEWAIT_LEN;
77
78		inet_twsk_schedule(tw, &dccp_death_row, timeo,
79				   DCCP_TIMEWAIT_LEN);
80		inet_twsk_put(tw);
81	} else {
82		/* Sorry, if we're out of memory, just CLOSE this
83		 * socket up.  We've got bigger problems than
84		 * non-graceful socket closings.
85		 */
86		DCCP_WARN("time wait bucket table overflow\n");
87	}
88
89	dccp_done(sk);
90}
91
92struct sock *dccp_create_openreq_child(struct sock *sk,
93				       const struct request_sock *req,
94				       const struct sk_buff *skb)
95{
96	/*
97	 * Step 3: Process LISTEN state
98	 *
99	 *   (* Generate a new socket and switch to that socket *)
100	 *   Set S := new socket for this port pair
101	 */
102	struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
103
104	if (newsk != NULL) {
105		const struct dccp_request_sock *dreq = dccp_rsk(req);
106		struct inet_connection_sock *newicsk = inet_csk(newsk);
107		struct dccp_sock *newdp = dccp_sk(newsk);
108		struct dccp_minisock *newdmsk = dccp_msk(newsk);
109
110		newdp->dccps_role	   = DCCP_ROLE_SERVER;
111		newdp->dccps_hc_rx_ackvec  = NULL;
112		newdp->dccps_service_list  = NULL;
113		newdp->dccps_service	   = dreq->dreq_service;
114		newicsk->icsk_rto	   = DCCP_TIMEOUT_INIT;
115		do_gettimeofday(&newdp->dccps_epoch);
116
117		if (dccp_feat_clone(sk, newsk))
118			goto out_free;
119
120		if (newdmsk->dccpms_send_ack_vector) {
121			newdp->dccps_hc_rx_ackvec =
122						dccp_ackvec_alloc(GFP_ATOMIC);
123			if (unlikely(newdp->dccps_hc_rx_ackvec == NULL))
124				goto out_free;
125		}
126
127		newdp->dccps_hc_rx_ccid =
128			    ccid_hc_rx_new(newdmsk->dccpms_rx_ccid,
129					   newsk, GFP_ATOMIC);
130		newdp->dccps_hc_tx_ccid =
131			    ccid_hc_tx_new(newdmsk->dccpms_tx_ccid,
132					   newsk, GFP_ATOMIC);
133		if (unlikely(newdp->dccps_hc_rx_ccid == NULL ||
134			     newdp->dccps_hc_tx_ccid == NULL)) {
135			dccp_ackvec_free(newdp->dccps_hc_rx_ackvec);
136			ccid_hc_rx_delete(newdp->dccps_hc_rx_ccid, newsk);
137			ccid_hc_tx_delete(newdp->dccps_hc_tx_ccid, newsk);
138out_free:
139			/* It is still raw copy of parent, so invalidate
140			 * destructor and make plain sk_free() */
141			newsk->sk_destruct = NULL;
142			sk_free(newsk);
143			return NULL;
144		}
145
146		/*
147		 * Step 3: Process LISTEN state
148		 *
149		 *    Choose S.ISS (initial seqno) or set from Init Cookies
150		 *    Initialize S.GAR := S.ISS
151		 *    Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
152		 */
153
154		/* See dccp_v4_conn_request */
155		newdmsk->dccpms_sequence_window = req->rcv_wnd;
156
157		newdp->dccps_gar = newdp->dccps_isr = dreq->dreq_isr;
158		dccp_update_gsr(newsk, dreq->dreq_isr);
159
160		newdp->dccps_iss = dreq->dreq_iss;
161		dccp_update_gss(newsk, dreq->dreq_iss);
162
163		/*
164		 * SWL and AWL are initially adjusted so that they are not less than
165		 * the initial Sequence Numbers received and sent, respectively:
166		 *	SWL := max(GSR + 1 - floor(W/4), ISR),
167		 *	AWL := max(GSS - W' + 1, ISS).
168		 * These adjustments MUST be applied only at the beginning of the
169		 * connection.
170		 */
171		dccp_set_seqno(&newdp->dccps_swl,
172			       max48(newdp->dccps_swl, newdp->dccps_isr));
173		dccp_set_seqno(&newdp->dccps_awl,
174			       max48(newdp->dccps_awl, newdp->dccps_iss));
175
176		dccp_init_xmit_timers(newsk);
177
178		DCCP_INC_STATS_BH(DCCP_MIB_PASSIVEOPENS);
179	}
180	return newsk;
181}
182
183EXPORT_SYMBOL_GPL(dccp_create_openreq_child);
184
185/*
186 * Process an incoming packet for RESPOND sockets represented
187 * as an request_sock.
188 */
189struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
190			    struct request_sock *req,
191			    struct request_sock **prev)
192{
193	struct sock *child = NULL;
194
195	/* Check for retransmitted REQUEST */
196	if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
197		struct dccp_request_sock *dreq = dccp_rsk(req);
198
199		if (after48(DCCP_SKB_CB(skb)->dccpd_seq, dreq->dreq_isr)) {
200			dccp_pr_debug("Retransmitted REQUEST\n");
201			dreq->dreq_isr = DCCP_SKB_CB(skb)->dccpd_seq;
202			/*
203			 * Send another RESPONSE packet
204			 * To protect against Request floods, increment retrans
205			 * counter (backoff, monitored by dccp_response_timer).
206			 */
207			req->retrans++;
208			req->rsk_ops->rtx_syn_ack(sk, req, NULL);
209		}
210		/* Network Duplicate, discard packet */
211		return NULL;
212	}
213
214	DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
215
216	if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
217	    dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
218		goto drop;
219
220	/* Invalid ACK */
221	if (DCCP_SKB_CB(skb)->dccpd_ack_seq != dccp_rsk(req)->dreq_iss) {
222		dccp_pr_debug("Invalid ACK number: ack_seq=%llu, "
223			      "dreq_iss=%llu\n",
224			      (unsigned long long)
225			      DCCP_SKB_CB(skb)->dccpd_ack_seq,
226			      (unsigned long long)
227			      dccp_rsk(req)->dreq_iss);
228		goto drop;
229	}
230
231	child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
232	if (child == NULL)
233		goto listen_overflow;
234
235
236	inet_csk_reqsk_queue_unlink(sk, req, prev);
237	inet_csk_reqsk_queue_removed(sk, req);
238	inet_csk_reqsk_queue_add(sk, req, child);
239out:
240	return child;
241listen_overflow:
242	dccp_pr_debug("listen_overflow!\n");
243	DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
244drop:
245	if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
246		req->rsk_ops->send_reset(sk, skb);
247
248	inet_csk_reqsk_queue_drop(sk, req, prev);
249	goto out;
250}
251
252EXPORT_SYMBOL_GPL(dccp_check_req);
253
254/*
255 *  Queue segment on the new socket if the new socket is active,
256 *  otherwise we just shortcircuit this and continue with
257 *  the new socket.
258 */
259int dccp_child_process(struct sock *parent, struct sock *child,
260		       struct sk_buff *skb)
261{
262	int ret = 0;
263	const int state = child->sk_state;
264
265	if (!sock_owned_by_user(child)) {
266		ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
267					     skb->len);
268
269		/* Wakeup parent, send SIGIO */
270		if (state == DCCP_RESPOND && child->sk_state != state)
271			parent->sk_data_ready(parent, 0);
272	} else {
273		/* Alas, it is possible again, because we do lookup
274		 * in main socket hash table and lock on listening
275		 * socket does not protect us more.
276		 */
277		sk_add_backlog(child, skb);
278	}
279
280	bh_unlock_sock(child);
281	sock_put(child);
282	return ret;
283}
284
285EXPORT_SYMBOL_GPL(dccp_child_process);
286
287void dccp_reqsk_send_ack(struct sk_buff *skb, struct request_sock *rsk)
288{
289	DCCP_BUG("DCCP-ACK packets are never sent in LISTEN/RESPOND state");
290}
291
292EXPORT_SYMBOL_GPL(dccp_reqsk_send_ack);
293
294void dccp_reqsk_init(struct request_sock *req, struct sk_buff *skb)
295{
296	inet_rsk(req)->rmt_port = dccp_hdr(skb)->dccph_sport;
297	inet_rsk(req)->acked	= 0;
298	req->rcv_wnd		= sysctl_dccp_feat_sequence_window;
299}
300
301EXPORT_SYMBOL_GPL(dccp_reqsk_init);
302