xref: /netbsd-src/sys/netinet/dccp_usrreq.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$KAME: dccp_usrreq.c,v 1.67 2005/11/03 16:05:04 nishida Exp $	*/
2 /*	$NetBSD: dccp_usrreq.c,v 1.19 2018/05/18 18:58:51 maxv Exp $ */
3 
4 /*
5  * Copyright (c) 2003 Joacim H�ggmark, Magnus Erixzon, Nils-Erik Mattsson
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * Id: dccp_usrreq.c,v 1.47 2003/07/31 11:23:08 joahag-9 Exp
32  */
33 
34 /*
35  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
67  */
68 
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: dccp_usrreq.c,v 1.19 2018/05/18 18:58:51 maxv Exp $");
71 
72 #ifdef _KERNEL_OPT
73 #include "opt_inet.h"
74 #include "opt_dccp.h"
75 #endif
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/domain.h>
80 #include <sys/kernel.h>
81 #include <sys/pool.h>
82 #include <sys/lock.h>
83 #include <sys/malloc.h>
84 #include <sys/mbuf.h>
85 #include <sys/proc.h>
86 #include <sys/protosw.h>
87 #include <sys/signalvar.h>
88 #include <sys/socket.h>
89 #include <sys/socketvar.h>
90 #include <sys/mutex.h>
91 #include <sys/sysctl.h>
92 #include <sys/syslog.h>
93 #include <sys/queue.h>
94 
95 #include <net/if.h>
96 
97 #include <netinet/in.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/ip.h>
100 #include <netinet/in_pcb.h>
101 #include <netinet/in_var.h>
102 #ifdef INET6
103 #include <netinet/ip6.h>
104 #endif
105 #include <netinet/ip_icmp.h>
106 #include <netinet/icmp_var.h>
107 #include <netinet/ip_var.h>
108 #ifdef INET6
109 #include <netinet6/in6_pcb.h>
110 #include <netinet6/ip6_var.h>
111 #include <netinet6/dccp6_var.h>
112 #endif
113 #include <netinet/dccp.h>
114 #include <netinet/dccp_var.h>
115 #include <netinet/dccp_cc_sw.h>
116 
117 #define DEFAULT_CCID 2
118 
119 #define INP_INFO_LOCK_INIT(x,y)
120 #define INP_INFO_WLOCK(x)
121 #define INP_INFO_WUNLOCK(x)
122 #define INP_INFO_RLOCK(x)
123 #define INP_INFO_RUNLOCK(x)
124 #define INP_LOCK(x)
125 #define IN6P_LOCK(x)
126 #define INP_UNLOCK(x)
127 #define IN6P_UNLOCK(x)
128 
129 /* Congestion control switch table */
130 extern struct dccp_cc_sw cc_sw[];
131 
132 int	dccp_log_in_vain = 1;
133 int	dccp_do_feature_nego = 1;
134 
135 struct	inpcbhead dccpb;		/* from dccp_var.h */
136 #ifdef __FreeBSD__
137 struct	inpcbinfo dccpbinfo;
138 #else
139 struct	inpcbtable dccpbtable;
140 #endif
141 
142 #ifndef DCCPBHASHSIZE
143 #define DCCPBHASHSIZE 16
144 #endif
145 
146 struct	pool dccpcb_pool;
147 
148 u_long	dccp_sendspace = 32768;
149 u_long	dccp_recvspace = 65536;
150 
151 struct	dccpstat dccpstat;	/* from dccp_var.h */
152 
153 static struct dccpcb * dccp_close(struct dccpcb *);
154 static int dccp_disconnect2(struct dccpcb *);
155 int dccp_get_option(char *, int, int, char *,int);
156 void dccp_parse_options(struct dccpcb *, char *, int);
157 int dccp_remove_feature(struct dccpcb *, u_int8_t, u_int8_t);
158 int dccp_add_feature_option(struct dccpcb *, u_int8_t, u_int8_t, char *, u_int8_t);
159 void dccp_feature_neg(struct dccpcb *, u_int8_t, u_int8_t, u_int8_t, char *);
160 void dccp_close_t(void *);
161 void dccp_timewait_t(void *);
162 
163 /* Ack Vector functions */
164 #define DCCP_VECTORSIZE 512 /* initial ack and cwnd-vector size. Multiple of 8 ! */
165 void dccp_use_ackvector(struct dccpcb *);
166 void dccp_update_ackvector(struct dccpcb *, u_int64_t);
167 void dccp_increment_ackvector(struct dccpcb *, u_int64_t);
168 u_int16_t dccp_generate_ackvector(struct dccpcb *, u_char *);
169 u_char dccp_ackvector_state(struct dccpcb *, u_int64_t);
170 
171 /*
172  * DCCP initialization
173  */
174 void
175 dccp_init(void)
176 {
177 	pool_init(&dccpcb_pool, sizeof(struct dccpcb), 0, 0, 0, "dccpcbpl",
178 		  NULL, IPL_SOFTNET);
179 
180 	in_pcbinit(&dccpbtable, DCCPBHASHSIZE, DCCPBHASHSIZE);
181 }
182 
183 void
184 dccp_input(struct mbuf *m, ...)
185 {
186 	int iphlen;
187 	struct ip *ip = NULL;
188 	struct dccphdr *dh;
189 	struct dccplhdr *dlh;
190 	struct inpcb *inp = NULL, *oinp = NULL;
191 	struct in6pcb *in6p = NULL, *oin6p = NULL;
192 	struct dccpcb *dp;
193 	struct ipovly *ipov = NULL;
194 	struct dccp_requesthdr *drqh;
195 	struct dccp_ackhdr *dah = NULL;
196 	struct dccp_acklhdr *dalh = NULL;
197 	struct dccp_resethdr *drth;
198 	struct socket *so;
199 	u_char *optp = NULL;
200 	struct mbuf *opts = 0;
201 	int len, data_off, extrah_len, optlen;
202 	/*struct ip save_ip;*/
203 	char options[DCCP_MAX_OPTIONS];
204 	char test[2];
205 	u_int32_t cslen;
206 	dccp_seq seqnr, low_seqnr, high_seqnr;
207 	int isipv6 = 0;
208 	int is_shortseq; /* Is this shortseq packet? */
209 #ifdef INET6
210 	struct ip6_hdr *ip6 = NULL;
211 #endif
212 
213 	int off;
214 	va_list ap;
215 
216 	va_start(ap, m);
217 	iphlen = off = va_arg(ap, int);
218 	va_end(ap);
219 
220 	DCCP_DEBUG((LOG_INFO, "Got DCCP packet!\n"));
221 
222 	dccpstat.dccps_ipackets++;
223 	dccpstat.dccps_ibytes += m->m_pkthdr.len;
224 
225 #ifdef INET6
226 	isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0;
227 #endif
228 
229 #ifdef INET6
230 	if (isipv6) {
231 		DCCP_DEBUG((LOG_INFO, "Got DCCP ipv6 packet, iphlen = %u!\n", iphlen));
232 		ip6 = mtod(m, struct ip6_hdr *);
233 		M_REGION_GET(dh, struct dccphdr *, m, iphlen, sizeof(*dh));
234 		if (dh == NULL) {
235 			dccpstat.dccps_badlen++;
236 			return;
237 		}
238 	} else
239 #endif
240 	{
241 		/*
242 		 * Strip IP options, if any; should skip this,
243 		 * make available to user, and use on returned packets,
244 		 * but we don't yet have a way to check the checksum
245 		 * with options still present.
246 		 */
247 		if (iphlen > sizeof (struct ip)) {
248 			DCCP_DEBUG((LOG_INFO, "Need to strip options\n"));
249 #if 0				/* XXX */
250 			ip_stripoptions(m, (struct mbuf *)0);
251 #endif
252 			iphlen = sizeof(struct ip);
253 		}
254 
255 		/*
256 		 * Get IP and DCCP header together in first mbuf.
257 		 */
258 		ip = mtod(m, struct ip *);
259 		M_REGION_GET(dh, struct dccphdr *, m, iphlen, sizeof(*dh));
260 		if (dh == NULL) {
261 			dccpstat.dccps_badlen++;
262 			return;
263 		}
264 	}
265 	dlh = (struct dccplhdr*)dh;
266 	is_shortseq = !dh->dh_x;
267 
268 	if (!is_shortseq) {
269 		DCCP_DEBUG((LOG_INFO,
270 		"Header info: cslen = %u, off = %u, type = %u, reserved = %u, seq = %u.%lu\n",
271 			 dlh->dh_cscov, dlh->dh_off, dlh->dh_type, dlh->dh_res, ntohs(dlh->dh_seq),
272 			 (unsigned long)ntohl(dlh->dh_seq2)));
273 	} else {
274 		DCCP_DEBUG((LOG_INFO,
275 		"Header info(short): cslen = %u, off = %u, type = %u, reserved = %u, seq = %u\n",
276 			dh->dh_cscov, dh->dh_off, dh->dh_type, dh->dh_res, ntohl(dh->dh_seq)));
277 	}
278 
279 	/*
280 	 * Make mbuf data length reflect DCCP length.
281 	 * If not enough data to reflect DCCP length, drop.
282 	 */
283 
284 #ifdef INET6
285 	if (isipv6)
286 		len = m->m_pkthdr.len - off;
287 	else
288 #endif
289 	{
290 		len = ntohs(ip->ip_len);
291 		len -= ip->ip_hl << 2;
292 	}
293 
294 	if (len < sizeof(struct dccphdr)) {
295 		DCCP_DEBUG((LOG_INFO, "Dropping DCCP packet!\n"));
296 		dccpstat.dccps_badlen++;
297 		goto badunlocked;
298 	}
299 	/*
300 	 * Save a copy of the IP header in case we want restore it
301 	 * for sending a DCCP reset packet in response.
302 	 */
303 	if (!isipv6) {
304 		/*save_ip = *ip;*/
305 		ipov = (struct ipovly *)ip;
306 	}
307 
308 	if (dh->dh_cscov == 0) {
309 		cslen = len;
310 	} else {
311 		cslen = dh->dh_off * 4 + (dh->dh_cscov - 1) * 4;
312 		if (cslen > len)
313 			cslen = len;
314 	}
315 
316 	/*
317 	 * Checksum extended DCCP header and data.
318 	 */
319 
320 #ifdef INET6
321 	if (isipv6) {
322 		if (in6_cksum(m, IPPROTO_DCCP, off, cslen) != 0) {
323 			dccpstat.dccps_badsum++;
324 			goto badunlocked;
325 		}
326 	} else
327 #endif
328 	{
329 		memset(ipov->ih_x1, 0, sizeof(ipov->ih_x1));
330 		ip->ip_len = htons(m->m_pkthdr.len);
331 		dh->dh_sum = in4_cksum(m, IPPROTO_DCCP, off, cslen);
332 
333 		if (dh->dh_sum) {
334 			dccpstat.dccps_badsum++;
335 			goto badunlocked;
336 		}
337 	}
338 
339 	INP_INFO_WLOCK(&dccpbinfo);
340 
341 	/*
342 	 * Locate pcb for datagram.
343 	 */
344 #ifdef INET6
345 	if (isipv6) {
346 		in6p = in6_pcblookup_connect(&dccpbtable, &ip6->ip6_src,
347 		    dh->dh_sport, &ip6->ip6_dst, dh->dh_dport, 0, 0);
348 		if (in6p == 0) {
349 			/* XXX stats increment? */
350 			in6p = in6_pcblookup_bind(&dccpbtable, &ip6->ip6_dst,
351 			    dh->dh_dport, 0);
352 		}
353 	} else
354 #endif
355 	{
356 		inp = in_pcblookup_connect(&dccpbtable, ip->ip_src,
357 		    dh->dh_sport, ip->ip_dst, dh->dh_dport, 0);
358 		if (inp == NULL) {
359 			/* XXX stats increment? */
360 			inp = in_pcblookup_bind(&dccpbtable, ip->ip_dst,
361 			    dh->dh_dport);
362 		}
363 	}
364 	if (isipv6) {
365 		DCCP_DEBUG((LOG_INFO, "in6p=%p\n", in6p));
366 	} else {
367 		DCCP_DEBUG((LOG_INFO, "inp=%p\n", inp));
368 	}
369 
370 	if (isipv6 ? in6p == NULL : inp == NULL) {
371 		if (dccp_log_in_vain) {
372 #ifdef INET6
373 			char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
374 #else
375 			char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
376 #endif
377 
378 #ifdef INET6
379 			if (isipv6) {
380 				char ip6buf[INET6_ADDRSTRLEN];
381 				strlcpy(dbuf, "[", sizeof dbuf);
382 				strlcat(dbuf, IN6_PRINT(ip6buf, &ip6->ip6_dst), sizeof dbuf);
383 				strlcat(dbuf, "]", sizeof dbuf);
384 				strlcpy(sbuf, "[", sizeof sbuf);
385 				strlcat(sbuf, IN6_PRINT(ip6buf, &ip6->ip6_src), sizeof sbuf);
386 				strlcat(sbuf, "]", sizeof sbuf);
387 			} else
388 #endif
389 			{
390 				strlcpy(dbuf, inet_ntoa(ip->ip_dst), sizeof dbuf);
391 				strlcpy(sbuf, inet_ntoa(ip->ip_src), sizeof sbuf);
392 			}
393 			log(LOG_INFO,
394 			    "Connection attempt to DCCP %s:%d from %s:%d\n",
395 			    dbuf, ntohs(dh->dh_dport), sbuf,
396 			    ntohs(dh->dh_sport));
397 		}
398 		dccpstat.dccps_noport++;
399 
400 		/*
401 		 * We should send DCCP reset here but we can't call dccp_output
402 		 * since we have no dccpcb. A icmp unreachable works great but
403 		 * the specs says DCCP reset :(
404 		 *
405 		 * if (!isipv6) {
406 		 *	*ip = save_ip;
407 		 *	ip->ip_len += iphlen;
408 		 *	icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
409 		 * }
410 		 */
411 
412 		INP_INFO_WUNLOCK(&dccpbinfo);
413 		goto badunlocked;
414 	}
415 	INP_LOCK(inp);
416 
417 #ifdef INET6
418 	if (isipv6)
419 		dp = in6todccpcb(in6p);
420 	else
421 #endif
422 		dp = intodccpcb(inp);
423 
424 	if (dp == 0) {
425 		INP_UNLOCK(inp);
426 		INP_INFO_WUNLOCK(&dccpbinfo);
427 		goto badunlocked;
428 	}
429 
430 	if (dp->state == DCCPS_CLOSED) {
431 		DCCP_DEBUG((LOG_INFO, "We are in closed state, dropping packet and sending reset!\n"));
432 		if (dh->dh_type != DCCP_TYPE_RESET)
433 			dccp_output(dp, DCCP_TYPE_RESET + 2);
434 		INP_UNLOCK(inp);
435 		INP_INFO_WUNLOCK(&dccpbinfo);
436 		goto badunlocked;
437 	}
438 
439 #if defined(INET6)
440 	if (isipv6)
441 		so = in6p->in6p_socket;
442 	else
443 #endif
444 		so = inp->inp_socket;
445 
446 	if (so->so_options & SO_ACCEPTCONN) {
447 		DCCP_DEBUG((LOG_INFO, "so->options & SO_ACCEPTCONN! dp->state = %i\n", dp->state));
448 		so = sonewconn(so, SS_ISCONNECTED);
449 		if (so == 0) {
450 			DCCP_DEBUG((LOG_INFO, "Error, sonewconn failed!\n"));
451 			INP_UNLOCK(inp);
452 			INP_INFO_WUNLOCK(&dccpbinfo);
453 			goto badunlocked;
454 		}
455 
456 		/* INP_LOCK(inp); XXX */
457 
458 #if defined(INET6)
459 		if (isipv6)
460 			oin6p = in6p;
461 		else
462 #endif
463 			oinp = inp;
464 
465 #ifdef INET6
466 		if (isipv6) {
467 			in6p = sotoin6pcb(so);
468 			in6p->in6p_laddr = ip6->ip6_dst;
469 			in6p->in6p_faddr = ip6->ip6_src;
470 			in6p->in6p_lport = dh->dh_dport;
471 			in6p->in6p_fport = dh->dh_sport;
472 			in6_pcbstate(in6p, IN6P_CONNECTED);
473 		} else
474 #endif
475 		{
476 			inp = sotoinpcb(so);
477 			inp->inp_laddr = ip->ip_dst;
478 			inp->inp_faddr = ip->ip_src;
479 			inp->inp_lport = dh->dh_dport;
480 			inp->inp_fport = dh->dh_sport;
481 		}
482 
483 		if (!isipv6)
484 			in_pcbstate(inp, INP_BOUND);
485 
486 #if defined(INET6)
487 		if (isipv6)
488 			dp = (struct dccpcb *)in6p->in6p_ppcb;
489 		else
490 #endif
491 			dp = (struct dccpcb *)inp->inp_ppcb;
492 
493 		dp->state = DCCPS_LISTEN;
494 		dp->who = DCCP_SERVER;
495 #if defined(INET6)
496 		if (isipv6) {
497 			dp->cslen = ((struct dccpcb *)oin6p->in6p_ppcb)->cslen;
498 			dp->avgpsize = ((struct dccpcb *)oin6p->in6p_ppcb)->avgpsize;
499 			dp->scode = ((struct dccpcb *)oin6p->in6p_ppcb)->scode;
500 		} else
501 #endif
502 		{
503 			dp->cslen = ((struct dccpcb *)oinp->inp_ppcb)->cslen;
504 			dp->avgpsize = ((struct dccpcb *)oinp->inp_ppcb)->avgpsize;
505 			dp->scode = ((struct dccpcb *)oinp->inp_ppcb)->scode;
506 		}
507 		dp->seq_snd = (((u_int64_t)random() << 32) | random()) % 281474976710656LL;
508 		dp->ref_seq.hi = dp->seq_snd >> 24;
509 		dp->ref_seq.lo = (u_int64_t)(dp->seq_snd & 0xffffff);
510 		INP_UNLOCK(oinp);
511 		DCCP_DEBUG((LOG_INFO, "New dp = %u, dp->state = %u!\n", (int)dp, dp->state));
512 	}
513 
514 	INP_INFO_WUNLOCK(&dccpbinfo);
515 
516 	/*
517 	 * Check if sequence number is inside the loss window
518 	 */
519 	if (!is_shortseq) {
520 		DHDR_TO_DSEQ(seqnr, dlh);
521 	} else {
522 		/* shortseq */
523 		seqnr = CONVERT_TO_LONGSEQ((ntohl(dh->dh_seq) >> 8), dp->ref_pseq);
524 		DCCP_DEBUG((LOG_INFO, "short seq conversion %x,  %u %u\n",
525 			ntohl(dh->dh_seq) >> 8, dp->ref_pseq.hi, dp->ref_pseq.lo));
526 	}
527 
528 	DCCP_DEBUG((LOG_INFO, "Received DCCP packet with sequence number = %llu , gsn_rcv %llu\n", seqnr, dp->gsn_rcv));
529 
530 	/* store ccval */
531 	dp->ccval = dh->dh_ccval;
532 
533 	if (dp->gsn_rcv == 281474976710656LL) dp->gsn_rcv = seqnr;
534 	if (dp->gsn_rcv > (dp->loss_window / 4))
535 		low_seqnr = (dp->gsn_rcv - (dp->loss_window / 4)) % 281474976710656LL;
536 	else
537 		low_seqnr = 0ll;
538 	high_seqnr = (dp->gsn_rcv + (dp->loss_window / 4 * 3)) % 281474976710656LL;
539 
540 	if (! (DCCP_SEQ_GT(seqnr, low_seqnr) && DCCP_SEQ_LT(seqnr, high_seqnr))) {
541 		dccpstat.dccps_badseq++;
542 		DCCP_DEBUG((LOG_INFO, "Received DCCP packet with bad sequence number = %llu (low_seqnr = %llu, high_seqnr = %llu)\n", seqnr, low_seqnr, high_seqnr));
543 		INP_UNLOCK(inp);
544 		goto badunlocked;
545 	}
546 
547 	/* dp->gsn_rcv should always be the highest received valid sequence number */
548 	if (DCCP_SEQ_GT(seqnr, dp->gsn_rcv))
549 		dp->gsn_rcv = seqnr;
550 
551 	/* Just ignore DCCP-Move for now */
552 	if (dlh->dh_type == DCCP_TYPE_DATA) {
553 		extrah_len = 0;
554 		if (!is_shortseq)
555 			optp = (u_char *)(dlh + 1);
556 		else
557 			optp = (u_char *)(dh + 1);
558 	} else if (dh->dh_type == DCCP_TYPE_REQUEST) {
559 		drqh = (struct dccp_requesthdr *)(dlh + 1);
560 		if (drqh->drqh_scode != dp->scode){
561 			DCCP_DEBUG((LOG_INFO, "service code in request packet doesn't match! %x %x\n", drqh->drqh_scode, dp->scode));
562 			INP_UNLOCK(inp);
563 			dp->state = DCCPS_SERVER_CLOSE; /* So disconnect2 doesn't send CLOSEREQ */
564 			dccp_disconnect2(dp);
565 			dccp_output(dp, DCCP_TYPE_RESET + 2);
566 			dccp_close(dp);
567 			goto badunlocked;
568 		}
569 		optp = (u_char *)(drqh + 1);
570 		extrah_len = 4;
571 
572 		/* store reference peer sequence number */
573 		dp->ref_pseq.hi = seqnr >> 24;
574 		dp->ref_pseq.lo = (u_int64_t)(seqnr & 0xffffff);
575 
576 	} else if (dh->dh_type == DCCP_TYPE_RESET) {
577 		extrah_len = 8 ;
578 		drth = (struct dccp_resethdr *)(dlh + 1);
579 		optp = (u_char *)(drth + 1);
580 	} else {
581 		if (!is_shortseq){
582 			extrah_len = 8;
583 			dalh = (struct dccp_acklhdr *)(dlh + 1);
584 			if (dh->dh_type == DCCP_TYPE_RESPONSE) {
585 				extrah_len += 4;
586 				drqh = (struct dccp_requesthdr *)(dalh + 1);
587 				if (drqh->drqh_scode != dp->scode){
588 					DCCP_DEBUG((LOG_INFO, "service code in response packet doesn't match! %x %x\n", drqh->drqh_scode, dp->scode));
589 					INP_UNLOCK(inp);
590 					dp->state = DCCPS_CLIENT_CLOSE; /* So disconnect2 doesn't send CLOSEREQ */
591 					dccp_disconnect2(dp);
592 					dccp_output(dp, DCCP_TYPE_RESET + 2);
593 					dccp_close(dp);
594 					goto badunlocked;
595 				}
596 				optp = (u_char *)(drqh + 1);
597 
598 				/* store reference peer sequence number */
599 				dp->ref_pseq.hi = seqnr >> 24;
600 				dp->ref_pseq.lo = (u_int64_t)(seqnr & 0xffffff);
601 			} else
602 				optp = (u_char *)(dalh + 1);
603 		} else {
604 			extrah_len = 4;
605 			dah = (struct dccp_ackhdr *)(dh + 1);
606 			optp = (u_char *)(dah + 1);
607 		}
608 
609 	}
610 
611 	data_off = (dh->dh_off << 2);
612 
613 	dp->seq_rcv = seqnr;
614 	dp->ack_rcv = 0; /* Clear it for now */
615 	dp->type_rcv = dh->dh_type;
616 	dp->len_rcv = m->m_len - data_off - iphlen; /* Correct length ? */
617 
618 	if (!is_shortseq)
619 		optlen = data_off - (sizeof(struct dccplhdr) + extrah_len);
620 	else
621 		optlen = data_off - (sizeof(struct dccphdr) + extrah_len);
622 
623 	if (optlen < 0) {
624 		DCCP_DEBUG((LOG_INFO, "Data offset is smaller then it could be, optlen = %i data_off = %i, m_len = %i, iphlen = %i extrah_len = %i !\n", optlen, data_off, m->m_len, iphlen, extrah_len));
625 		INP_UNLOCK(inp);
626 		goto badunlocked;
627 	}
628 
629 	if (optlen > 0) {
630 		if (optlen > DCCP_MAX_OPTIONS) {
631 			DCCP_DEBUG((LOG_INFO, "Error, more options (%i) then DCCP_MAX_OPTIONS options!\n", optlen));
632 			INP_UNLOCK(inp);
633 			goto badunlocked;
634 		}
635 
636 		DCCP_DEBUG((LOG_INFO, "Parsing DCCP options, optlen = %i\n", optlen));
637 		memcpy(options, optp, optlen);
638 		dccp_parse_options(dp, options, optlen);
639 	}
640 
641 	DCCP_DEBUG((LOG_INFO, "BEFORE state check, Got a %u packet while in %u state, who = %u!\n", dh->dh_type, dp->state, dp->who));
642 
643 	if (dp->state == DCCPS_LISTEN) {
644 		switch (dh->dh_type) {
645 
646 		case DCCP_TYPE_REQUEST:
647 			DCCP_DEBUG((LOG_INFO, "Got DCCP REQUEST\n"));
648 			dp->state = DCCPS_REQUEST;
649 			if (dp->cc_in_use[1] < 0) {
650 				test[0] = DEFAULT_CCID;
651 				test[1] = 3;
652 				dccp_add_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC, test, 2);
653 			}
654 			if (len > data_off) {
655 				DCCP_DEBUG((LOG_INFO, "XXX: len=%d, data_off=%d\n", len, data_off));
656 				dccp_add_option(dp, DCCP_OPT_DATA_DISCARD, test, 0);
657 			}
658 			callout_reset(&dp->connect_timer, DCCP_CONNECT_TIMER,
659 			    dccp_connect_t, dp);
660 			dccp_output(dp, 0);
661 			break;
662 
663 
664 		/* These are ok if the sender has a valid init Cookie */
665 		case DCCP_TYPE_ACK:
666 		case DCCP_TYPE_DATAACK:
667 		case DCCP_TYPE_DATA:
668 			DCCP_DEBUG((LOG_INFO, "Got DCCP ACK/DATAACK/DATA, should check init cookie...\n"));
669 			dccp_output(dp, DCCP_TYPE_RESET + 2);
670 			break;
671 
672 		case DCCP_TYPE_RESET:
673 			DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n"));
674 			dp->state = DCCPS_TIME_WAIT;
675 			dp = dccp_close(dp);
676 			return;
677 
678 		default:
679 			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in listen stage!\n", dh->dh_type));
680 			/* Force send reset. */
681 			dccp_output(dp, DCCP_TYPE_RESET + 2);
682 		}
683 	} else if (dp->state == DCCPS_REQUEST) {
684 		switch (dh->dh_type) {
685 		case DCCP_TYPE_RESPONSE:
686 			DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash);
687 			dp->ack_snd = dp->seq_rcv;
688 			DCCP_DEBUG((LOG_INFO, "Got DCCP REPSONSE %x %llx\n", dp, dp->ack_snd));
689 
690 			callout_stop(&dp->retrans_timer);
691 			callout_stop(&dp->connect_timer);
692 
693 			/* First check if we have negotiated a cc */
694 			if (dp->cc_in_use[0] > 0 && dp->cc_in_use[1] > 0) {
695 				DCCP_DEBUG((LOG_INFO, "Setting DCCPS_ESTAB & soisconnected\n"));
696 				dp->state = DCCPS_ESTAB;
697 				dccpstat.dccps_connects++;
698 #if defined(INET6)
699 				if (isipv6)
700 					soisconnected(in6p->in6p_socket);
701 				else
702 #endif
703 					soisconnected(inp->inp_socket);
704 			} else {
705 				dp->state = DCCPS_RESPOND;
706 				DCCP_DEBUG((LOG_INFO, "CC negotiation is not finished, cc_in_use[0] = %u, cc_in_use[1] = %u\n",dp->cc_in_use[0], dp->cc_in_use[1]));
707 
708 			}
709 			dccp_output(dp, 0);
710 			break;
711 
712 		case DCCP_TYPE_RESET:
713 			DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n"));
714 			dp->state = DCCPS_TIME_WAIT;
715 			dp = dccp_close(dp);
716 			return;
717 
718 		default:
719 			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in REQUEST stage!\n", dh->dh_type));
720 			/* Force send reset. */
721 			dccp_output(dp, DCCP_TYPE_RESET + 2);
722 			if (dh->dh_type == DCCP_TYPE_CLOSE) {
723 				dp = dccp_close(dp);
724 				return;
725 			} else {
726 				callout_stop(&dp->retrans_timer);
727 				dp->state = DCCPS_TIME_WAIT;
728 			}
729 		}
730 	} else if (dp->state == DCCPS_RESPOND) {
731 		switch (dh->dh_type) {
732 
733 		case DCCP_TYPE_REQUEST:
734 			break;
735 		case DCCP_TYPE_ACK:
736 		case DCCP_TYPE_DATAACK:
737 			DCCP_DEBUG((LOG_INFO, "Got DCCP ACK/DATAACK\n"));
738 
739 			callout_stop(&dp->connect_timer);
740 
741 			if (!is_shortseq) {
742 				DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash);
743 			} else {
744 				/* shortseq XXX */
745 				dp->ack_rcv = CONVERT_TO_LONGSEQ((ntohl(dah->dash.dah_ack) >> 8), dp->ref_seq);
746 			}
747 
748 			if (dp->cc_in_use[0] > 0 && dp->cc_in_use[1] > 0) {
749 				DCCP_DEBUG((LOG_INFO, "Setting DCCPS_ESTAB & soisconnected\n"));
750 				dp->state = DCCPS_ESTAB;
751 				dccpstat.dccps_connects++;
752 #if defined(INET6)
753 				if (isipv6)
754 					soisconnected(in6p->in6p_socket);
755 				else
756 #endif
757 					soisconnected(inp->inp_socket);
758 			} else {
759 				DCCP_DEBUG((LOG_INFO, "CC negotiation is not finished, cc_in_use[0] = %u, cc_in_use[1] = %u\n",dp->cc_in_use[0], dp->cc_in_use[1]));
760 				/* Force an output!!! */
761 				dp->ack_snd = dp->seq_rcv;
762 				dccp_output(dp, 0);
763 			}
764 
765 			if (dh->dh_type == DCCP_TYPE_DATAACK && dp->cc_in_use[1] > 0) {
766 				if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv;
767 				DCCP_DEBUG((LOG_INFO, "Calling *cc_sw[%u].cc_recv_packet_recv!\n", dp->cc_in_use[1]));
768 				(*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen);
769 			}
770 			break;
771 
772 		case DCCP_TYPE_CLOSE:
773 			dccp_output(dp, DCCP_TYPE_CLOSE + 1);
774 			dp = dccp_close(dp);
775 			goto badunlocked;
776 
777 		case DCCP_TYPE_RESET:
778 			dp->state = DCCPS_TIME_WAIT;
779 			callout_stop(&dp->retrans_timer);
780 			break;
781 
782 		default:
783 			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in response stage!\n", dh->dh_type));
784 			/* Force send reset. */
785 			dccp_output(dp, DCCP_TYPE_RESET + 2);
786 		}
787 	} else if (dp->state == DCCPS_ESTAB) {
788 		switch (dh->dh_type) {
789 
790 		case DCCP_TYPE_DATA:
791 			DCCP_DEBUG((LOG_INFO, "Got DCCP DATA, state = %i, cc_in_use[1] = %u\n", dp->state, dp->cc_in_use[1]));
792 
793 			if (dp->cc_in_use[1] > 0) {
794 				if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv;
795 				DCCP_DEBUG((LOG_INFO, "Calling data *cc_sw[%u].cc_recv_packet_recv! %llx %llx dp=%x\n", dp->cc_in_use[1], dp->ack_snd, dp->seq_rcv, dp));
796 				(*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen);
797 			}
798 			break;
799 
800 		case DCCP_TYPE_ACK:
801 			DCCP_DEBUG((LOG_INFO, "Got DCCP ACK\n"));
802 			if (!is_shortseq) {
803 				DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash);
804 			} else {
805 				/* shortseq */
806 				dp->ack_rcv = CONVERT_TO_LONGSEQ((ntohl(dah->dash.dah_ack) >> 8), dp->ref_seq);
807 			}
808 
809 			if (dp->cc_in_use[1] > 0) {
810 				/* This is called so Acks on Acks can be handled */
811 				if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv;
812 				DCCP_DEBUG((LOG_INFO, "Calling ACK *cc_sw[%u].cc_recv_packet_recv! %llx %llx\n", dp->cc_in_use[1], dp->ack_snd, dp->seq_rcv));
813 				(*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen);
814 			}
815 			break;
816 
817 		case DCCP_TYPE_DATAACK:
818 			DCCP_DEBUG((LOG_INFO, "Got DCCP DATAACK\n"));
819 
820 			if (!is_shortseq) {
821 				DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash);
822 			} else {
823 				/* shortseq */
824 				dp->ack_rcv = CONVERT_TO_LONGSEQ((ntohl(dah->dash.dah_ack) >> 8), dp->ref_seq);
825 			}
826 
827 			if (dp->cc_in_use[1] > 0) {
828 				if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv;
829 				DCCP_DEBUG((LOG_INFO, "Calling *cc_sw[%u].cc_recv_packet_recv! %llx %llx\n", dp->cc_in_use[1], dp->ack_snd, dp->seq_rcv));
830 				(*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen);
831 			}
832 			break;
833 
834 		case DCCP_TYPE_CLOSEREQ:
835 			DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSEREQ, state = estab\n"));
836 			if (dp->who == DCCP_CLIENT) {
837 				dccp_disconnect2(dp);
838 			} else {
839 				dccp_output(dp, DCCP_TYPE_RESET + 2);
840 			}
841 			break;
842 
843 		case DCCP_TYPE_CLOSE:
844 			DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSE, state = estab\n"));
845 			dp->state = DCCPS_SERVER_CLOSE; /* So disconnect2 doesn't send CLOSEREQ */
846 			dccp_disconnect2(dp);
847 			dccp_output(dp, DCCP_TYPE_RESET + 2);
848 			dccp_close(dp);
849 			goto badunlocked;
850 			break;
851 
852 		case DCCP_TYPE_RESET:
853 			DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n"));
854 			dp->state = DCCPS_TIME_WAIT;
855 			callout_stop(&dp->retrans_timer);
856 			callout_reset(&dp->timewait_timer, DCCP_TIMEWAIT_TIMER,
857 			    dccp_timewait_t, dp);
858 			break;
859 
860 		case DCCP_TYPE_MOVE:
861 			DCCP_DEBUG((LOG_INFO, "Got DCCP MOVE\n"));
862 			break;
863 
864 		default:
865 			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in established stage!\n", dh->dh_type));
866 		}
867 
868 	} else if (dp->state == DCCPS_SERVER_CLOSE) {
869 		/* Server */
870 		switch (dh->dh_type) {
871 		case DCCP_TYPE_CLOSE:
872 			DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSE (State DCCPS_SERVER_CLOSE)\n"));
873 			callout_stop(&dp->retrans_timer);
874 			dccp_output(dp, DCCP_TYPE_RESET + 2);
875 			dp = dccp_close(dp);
876 			goto badunlocked;
877 
878 		case DCCP_TYPE_RESET:
879 			DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n"));
880 			callout_stop(&dp->retrans_timer);
881 			dccp_output(dp, DCCP_TYPE_RESET + 2);
882 			dp->state = DCCPS_TIME_WAIT;
883 			break;
884 		default:
885 			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in server_close stage!\n", dh->dh_type));
886 		}
887 
888 	} else if (dp->state == DCCPS_CLIENT_CLOSE) {
889 		/* Client */
890 		switch (dh->dh_type) {
891 		case DCCP_TYPE_CLOSE:
892 			/* Ignore */
893 			break;
894 		case DCCP_TYPE_CLOSEREQ:
895 			DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSEREQ, state = DCCPS_CLIENT_CLOSE\n"));
896 			/* Just resend close */
897 			dccp_output(dp, 0);
898 			break;
899 		case DCCP_TYPE_RESET:
900 			DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n"));
901 			callout_stop(&dp->retrans_timer);
902 			dp->state = DCCPS_TIME_WAIT;
903 			callout_reset(&dp->timewait_timer, DCCP_TIMEWAIT_TIMER,
904 			    dccp_timewait_t, dp);
905 			break;
906 		default:
907 			DCCP_DEBUG((LOG_INFO, "Got a %u packet while in client_close stage!\n", dh->dh_type));
908 
909 		}
910 	} else {
911 		DCCP_DEBUG((LOG_INFO, "Got a %u packet while in %u state!\n", dh->dh_type, dp->state));
912 		if (dh->dh_type != DCCP_TYPE_RESET) {
913 			/* Force send reset. */
914 			DCCP_DEBUG((LOG_INFO, "Force sending a request!\n"));
915 			dccp_output(dp, DCCP_TYPE_RESET + 2);
916 		}
917 	}
918 
919 	if (dh->dh_type == DCCP_TYPE_DATA ||
920 	    dh->dh_type == DCCP_TYPE_ACK  ||
921 	    dh->dh_type == DCCP_TYPE_DATAACK) {
922 		if (dp->cc_in_use[0] > 0) {
923 			(*cc_sw[dp->cc_in_use[0]].cc_send_packet_recv)(dp->cc_state[0],options, optlen);
924 		}
925 
926 	}
927 
928 	if (dh->dh_type == DCCP_TYPE_DATA || dh->dh_type == DCCP_TYPE_DATAACK) {
929 		if (so->so_state & SS_CANTRCVMORE)
930 		{
931 			DCCP_DEBUG((LOG_INFO, "state & SS_CANTRCVMORE...!\n"));
932 			m_freem(m);
933 			if (opts)
934 				m_freem(opts);
935 		} else {
936 			m_adj(m, (iphlen + data_off));
937 			DCCP_DEBUG((LOG_INFO, "Calling sbappend!\n"));
938 			sbappend(&so->so_rcv, m);
939 		}
940 		DCCP_DEBUG((LOG_INFO, "Calling sorwakeup...!\n"));
941 		sorwakeup(so);
942 	} else {
943 		m_freem(m);
944 		if (opts)
945 			m_freem(opts);
946 	}
947 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
948 	if (dp)
949 		INP_UNLOCK(inp);
950 #endif
951 
952 	return;
953 
954 badunlocked:
955 	m_freem(m);
956 	if (opts)
957 		m_freem(opts);
958 	return;
959 }
960 
961 /*
962  * Notify a dccp user of an asynchronous error;
963  * just wake up so that he can collect error status.
964  */
965 void
966 dccp_notify(struct inpcb *inp, int errno)
967 {
968 	inp->inp_socket->so_error = errno;
969 	sorwakeup(inp->inp_socket);
970 	sowwakeup(inp->inp_socket);
971 	return;
972 }
973 
974 /*
975  * Called when we get ICMP errors (destination unrechable,
976  * parameter problem, source quench, time exceeded and redirects)
977  */
978 void *
979 dccp_ctlinput(int cmd, const struct sockaddr *sa, void *vip)
980 {
981 	struct ip *ip = vip;
982 	struct dccphdr *dh;
983 	void (*notify)(struct inpcb *, int) = dccp_notify;
984 	struct in_addr faddr;
985 	struct inpcb *inp = NULL;
986 
987 	faddr = ((const struct sockaddr_in *)sa)->sin_addr;
988 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
989 		return NULL;
990 
991 	if (PRC_IS_REDIRECT(cmd)) {
992 		ip = 0;
993 		notify = in_rtchange;
994 	} else if (cmd == PRC_HOSTDEAD)
995 		ip = 0;
996 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
997 		return NULL;
998 	if (ip) {
999 		/*s = splsoftnet();*/
1000 		dh = (struct dccphdr *)((vaddr_t)ip + (ip->ip_hl << 2));
1001 		INP_INFO_RLOCK(&dccpbinfo);
1002 		in_pcbnotify(&dccpbtable, faddr, dh->dh_dport,
1003 		    ip->ip_src, dh->dh_sport, inetctlerrmap[cmd], notify);
1004 		if (inp != NULL) {
1005 			INP_LOCK(inp);
1006 			if (inp->inp_socket != NULL) {
1007 				(*notify)(inp, inetctlerrmap[cmd]);
1008 			}
1009 			INP_UNLOCK(inp);
1010 		}
1011 		INP_INFO_RUNLOCK(&dccpbinfo);
1012 		/*splx(s);*/
1013 	} else
1014 		in_pcbnotifyall(&dccpbtable, faddr, inetctlerrmap[cmd], notify);
1015 
1016 	return NULL;
1017 }
1018 
1019 static int
1020 dccp_optsset(struct dccpcb *dp, struct sockopt *sopt)
1021 {
1022 	int optval;
1023 	int error = 0;
1024 
1025 	switch (sopt->sopt_name) {
1026 	case DCCP_CCID:
1027 		error = sockopt_getint(sopt, &optval);
1028 		/* Add check that optval is a CCID we support!!! */
1029 		if (optval == 2 || optval == 3 || optval == 0) {
1030 			dp->pref_cc = optval;
1031 		} else {
1032 			error = EINVAL;
1033 		}
1034 		break;
1035 	case DCCP_CSLEN:
1036 		error = sockopt_getint(sopt, &optval);
1037 		if (optval > 15 || optval < 0) {
1038 			error = EINVAL;
1039 		} else {
1040 			dp->cslen = optval;
1041 		}
1042 		break;
1043 	case DCCP_MAXSEG:
1044 		error = sockopt_getint(sopt, &optval);
1045 		if (optval > 0 && optval <= dp->d_maxseg) {
1046 			dp->d_maxseg = optval;
1047 		} else {
1048 			error = EINVAL;
1049 		}
1050 		break;
1051 	case DCCP_SERVICE:
1052 		error = sockopt_getint(sopt, &optval);
1053 		dp->scode = optval;
1054 		break;
1055 
1056 	default:
1057 		error = ENOPROTOOPT;
1058 	}
1059 
1060 	return error;
1061 }
1062 
1063 static int
1064 dccp_optsget(struct dccpcb *dp, struct sockopt *sopt)
1065 {
1066 	int optval = 0;
1067 	int error = 0;
1068 
1069 	switch (sopt->sopt_name) {
1070 	case DCCP_CCID:
1071 		optval = dp->pref_cc;
1072 		error = sockopt_set(sopt, &optval, sizeof(optval));
1073 		break;
1074 	case DCCP_CSLEN:
1075 		optval = dp->cslen;
1076 		error = sockopt_set(sopt, &optval, sizeof(optval));
1077 		break;
1078 	case DCCP_MAXSEG:
1079 		optval = dp->d_maxseg;
1080 		error = sockopt_set(sopt, &optval, sizeof(optval));
1081 		break;
1082 	case DCCP_SERVICE:
1083 		optval = dp->scode;
1084 		error = sockopt_set(sopt, &optval, sizeof(optval));
1085 		break;
1086 	default:
1087 		error = ENOPROTOOPT;
1088 	}
1089 
1090 	return error;
1091 }
1092 
1093 /*
1094  * Called by getsockopt and setsockopt.
1095  *
1096  */
1097 int
1098 dccp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1099 {
1100 	int s, error = 0;
1101 	struct inpcb	*inp;
1102 #if defined(INET6)
1103 	struct in6pcb *in6p;
1104 #endif
1105 	struct dccpcb	*dp;
1106 	int family;	/* family of the socket */
1107 
1108 	family = so->so_proto->pr_domain->dom_family;
1109 	error = 0;
1110 
1111 	s = splsoftnet();
1112 	INP_INFO_RLOCK(&dccpbinfo);
1113 	switch (family) {
1114 	case PF_INET:
1115 		inp = sotoinpcb(so);
1116 #if defined(INET6)
1117 		in6p = NULL;
1118 #endif
1119 		break;
1120 #if defined(INET6)
1121 	case PF_INET6:
1122 		inp = NULL;
1123 		in6p = sotoin6pcb(so);
1124 		break;
1125 #endif
1126 	default:
1127 		INP_INFO_RUNLOCK(&dccpbinfo);
1128 		splx(s);
1129 		return EAFNOSUPPORT;
1130 	}
1131 #if defined(INET6)
1132 	if (inp == NULL && in6p == NULL)
1133 #else
1134 	if (inp == NULL)
1135 #endif
1136 	{
1137 		INP_INFO_RUNLOCK(&dccpbinfo);
1138 		splx(s);
1139 		return (ECONNRESET);
1140 	}
1141 /*
1142 	if (inp)
1143 		INP_LOCK(inp);
1144 	else
1145 		IN6P_LOCK(in6p);
1146 	INP_INFO_RUNLOCK(&dccpbinfo);
1147 */
1148 	if (sopt->sopt_level != IPPROTO_DCCP) {
1149 		switch (family) {
1150 		case PF_INET:
1151 			error = ip_ctloutput(op, so, sopt);
1152 			break;
1153 #if defined(INET6)
1154 		case PF_INET6:
1155 			error = ip6_ctloutput(op, so, sopt);
1156 			break;
1157 #endif
1158 		}
1159 		splx(s);
1160 		return (error);
1161 	}
1162 
1163 	if (inp)
1164 		dp = intodccpcb(inp);
1165 #if defined(INET6)
1166 	else if (in6p)
1167 		dp = in6todccpcb(in6p);
1168 #endif
1169 	else
1170 		dp = NULL;
1171 
1172 	if (op == PRCO_SETOPT) {
1173 		error = dccp_optsset(dp, sopt);
1174 	} else if (op ==  PRCO_GETOPT) {
1175 		error = dccp_optsget(dp, sopt);
1176 	} else {
1177 		error = EINVAL;
1178 	}
1179 /*
1180 	if (inp)
1181 		INP_UNLOCK(inp);
1182 	else
1183 		IN6P_UNLOCK(in6p);
1184 */
1185 	splx(s);
1186 	return error;
1187 }
1188 
1189 int
1190 dccp_output(struct dccpcb *dp, u_int8_t extra)
1191 {
1192 	struct inpcb *inp;
1193 	struct in6pcb *in6p = NULL;
1194 	struct socket *so;
1195 	struct mbuf *m;
1196 
1197 	struct ip *ip = NULL;
1198 	struct dccphdr *dh;
1199 	struct dccplhdr *dlh;
1200 	struct dccp_requesthdr *drqh;
1201 	struct dccp_ackhdr *dah;
1202 	struct dccp_acklhdr *dalh;
1203 	struct dccp_resethdr *drth;
1204 	u_char *optp = NULL;
1205 	int error = 0;
1206 	int off, sendalot, t, i;
1207 	u_int32_t hdrlen, optlen, extrah_len, cslen;
1208 	u_int8_t type;
1209 	char options[DCCP_MAX_OPTIONS *2];
1210 	long len, pktlen;
1211 	int isipv6 = 0;
1212 	int use_shortseq = 0;
1213 #ifdef INET6
1214 	struct ip6_hdr *ip6 = NULL;
1215 #endif
1216 
1217 	DCCP_DEBUG((LOG_INFO, "dccp_output start!\n"));
1218 
1219 	isipv6 = (dp->inp_vflag & INP_IPV6) != 0;
1220 
1221 	DCCP_DEBUG((LOG_INFO, "Going to send a DCCP packet!\n"));
1222 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
1223 	KASSERT(mutex_assert(&dp->d_inpcb->inp_mtx, MA_OWNED));
1224 #endif
1225 
1226 #if defined(INET6)
1227 	if (isipv6) {
1228 		inp = 0;
1229 		in6p = dp->d_in6pcb;
1230 		so = in6p->in6p_socket;
1231 	} else
1232 #endif
1233 	{
1234 		inp = dp->d_inpcb;
1235 		so = inp->inp_socket;
1236 	}
1237 
1238 	if (dp->state != DCCPS_ESTAB && extra == 1) {
1239 		/* Only let cc decide when to resend if we are in establised state */
1240 		return 0;
1241 	}
1242 
1243 	if (so->so_snd.sb_cc){
1244 		pktlen = dp->pktlen[dp->pktlenidx];
1245 	} else
1246 		pktlen = 0;
1247 
1248 	/* Check with CC if we can send... */
1249 	if (pktlen && dp->cc_in_use[0] > 0 && dp->state == DCCPS_ESTAB) {
1250 		if (!(*cc_sw[dp->cc_in_use[0]].cc_send_packet)(dp->cc_state[0], pktlen)) {
1251 			DCCP_DEBUG((LOG_INFO, "Not allowed to send right now\n"));
1252 			return 0;
1253 		}
1254 	}
1255 
1256 	if (pktlen) {
1257 		dp->pktcnt --;
1258 		dp->pktlenidx = (dp->pktlenidx +1) % DCCP_MAX_PKTS;
1259 	}
1260 
1261 again:
1262 	sendalot = 0;
1263 
1264 	/*
1265 	 * off not needed for dccp because we do not need to wait for ACK
1266 	 * before removing the packet
1267 	 */
1268 	off = 0;
1269 	optlen = 0;
1270 
1271 	if (pktlen > dp->d_maxseg) {
1272 		/* this should not happen */
1273 		DCCP_DEBUG((LOG_INFO, "packet will be fragmented! maxseg %d\n", dp->d_maxseg));
1274 		len = dp->d_maxseg;
1275 		pktlen -= len;
1276 		sendalot = 1;
1277 	} else
1278 		len = pktlen;
1279 
1280 	if (extra == DCCP_TYPE_RESET + 2) {
1281 		DCCP_DEBUG((LOG_INFO, "Force sending of DCCP TYPE_RESET! seq=%llu\n", dp->seq_snd));
1282 		type = DCCP_TYPE_RESET;
1283 		extrah_len = 12;
1284 	} else if (dp->state <= DCCPS_REQUEST && dp->who == DCCP_CLIENT) {
1285 		DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_REQUEST!\n"));
1286 		type = DCCP_TYPE_REQUEST;
1287 		dp->state = DCCPS_REQUEST;
1288 		extrah_len = 4;
1289 	} else if (dp->state == DCCPS_REQUEST && dp->who == DCCP_SERVER) {
1290 		DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_RESPONSE!\n"));
1291 		type = DCCP_TYPE_RESPONSE;
1292 		dp->state = DCCPS_RESPOND;
1293 		extrah_len = 12;
1294 	} else if (dp->state == DCCPS_RESPOND) {
1295 		DCCP_DEBUG((LOG_INFO, "Still in feature neg, sending DCCP TYPE_ACK!\n"));
1296 		type = DCCP_TYPE_ACK;
1297 		if (!dp->shortseq)
1298 			extrah_len = 8;
1299 		else {
1300 			extrah_len = 4;
1301 			use_shortseq = 1;
1302 		}
1303 	} else if (dp->state == DCCPS_ESTAB) {
1304 		if (dp->ack_snd && len) {
1305 			DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_DATAACK!\n"));
1306 			type = DCCP_TYPE_DATAACK;
1307 			/*(u_int32_t *)&extrah = dp->seq_rcv; */
1308 			if (!dp->shortseq)
1309 				extrah_len = 8;
1310 			else {
1311 				extrah_len = 4;
1312 				use_shortseq = 1;
1313 			}
1314 		} else if (dp->ack_snd) {
1315 			DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_ACK!\n"));
1316 			type = DCCP_TYPE_ACK;
1317 			if (!dp->shortseq)
1318 				extrah_len = 8;
1319 			else {
1320 				extrah_len = 4;
1321 				use_shortseq = 1;
1322 			}
1323 		} else if (len) {
1324 			DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_DATA!\n"));
1325 			type = DCCP_TYPE_DATA;
1326 			extrah_len = 0;
1327 		} else {
1328 			DCCP_DEBUG((LOG_INFO, "No ack or data to send!\n"));
1329 			return 0;
1330 		}
1331 	} else if (dp->state == DCCPS_CLIENT_CLOSE) {
1332 		DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_CLOSE!\n"));
1333 		type = DCCP_TYPE_CLOSE;
1334 		extrah_len = 8;
1335 	} else if (dp->state == DCCPS_SERVER_CLOSE) {
1336 		DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_CLOSEREQ!\n"));
1337 		type = DCCP_TYPE_CLOSEREQ;
1338 		extrah_len = 8;
1339 	} else {
1340 		DCCP_DEBUG((LOG_INFO, "Hey, we should never get here, state = %u\n", dp->state));
1341 		return 1;
1342 	}
1343 
1344 	/* Adding options. */
1345 	if (dp->optlen) {
1346 		DCCP_DEBUG((LOG_INFO, "Copying options from dp->options! %u\n", dp->optlen));
1347 		memcpy(options, dp->options, dp->optlen);
1348 		optlen = dp->optlen;
1349 		dp->optlen = 0;
1350 	}
1351 
1352 	if (dp->featlen && (optlen + dp->featlen < DCCP_MAX_OPTIONS)) {
1353 		DCCP_DEBUG((LOG_INFO, "Copying options from dp->features! %u\n", dp->featlen));
1354 		memcpy(options + optlen, dp->features, dp->featlen);
1355 		optlen += dp->featlen;
1356 	}
1357 
1358 	t = optlen % 4;
1359 
1360 	if (t) {
1361 		t = 4 - t;
1362 		for (i = 0 ; i<t; i++) {
1363 			options[optlen] = 0;
1364 			optlen++;
1365 		}
1366 	}
1367 
1368 #ifdef INET6
1369 	if (isipv6) {
1370 		DCCP_DEBUG((LOG_INFO, "Sending ipv6 packet...\n"));
1371 		if (!use_shortseq)
1372 			hdrlen = sizeof(struct ip6_hdr) + sizeof(struct dccplhdr) +
1373 			    extrah_len + optlen;
1374 		else
1375 			hdrlen = sizeof(struct ip6_hdr) + sizeof(struct dccphdr) +
1376 			    extrah_len + optlen;
1377 	} else
1378 #endif
1379 	{
1380 		if (!use_shortseq)
1381 			hdrlen = sizeof(struct ip) + sizeof(struct dccplhdr) +
1382 		   		extrah_len + optlen;
1383 		else
1384 			hdrlen = sizeof(struct ip) + sizeof(struct dccphdr) +
1385 		   		extrah_len + optlen;
1386 	}
1387 	DCCP_DEBUG((LOG_INFO, "Pkt headerlen %u\n", hdrlen));
1388 
1389 	if (len > (dp->d_maxseg - extrah_len - optlen)) {
1390 		len = dp->d_maxseg - extrah_len - optlen;
1391 		sendalot = 1;
1392 	}
1393 
1394 	MGETHDR(m, M_DONTWAIT, MT_HEADER);
1395 	if (m == NULL) {
1396 		error = ENOBUFS;
1397 		goto release;
1398 	}
1399 	if (MHLEN < hdrlen + max_linkhdr) {
1400 		MCLGET(m, M_DONTWAIT);
1401 		if ((m->m_flags & M_EXT) == 0) {
1402 			error = ENOBUFS;
1403 			goto release;
1404 		}
1405 	}
1406 
1407 	m->m_data += max_linkhdr;
1408 	m->m_len = hdrlen;
1409 
1410 	if (len) { /* We have data to send */
1411 		if (len <= M_TRAILINGSPACE(m) - hdrlen) {
1412 			m_copydata(so->so_snd.sb_mb, off, (int) len,
1413 			mtod(m, char *) + hdrlen);
1414 			m->m_len += len;
1415 		} else {
1416 			m->m_next = m_copym(so->so_snd.sb_mb, off,
1417 			    (int)len, M_DONTWAIT);
1418 			if (m->m_next == 0) {
1419 				error = ENOBUFS;
1420 				goto release;
1421 			}
1422 		}
1423 	} else {
1424 		dp->ndp++;
1425 	}
1426 
1427 	m_reset_rcvif(m);
1428 
1429 	if (!isipv6 && (len + hdrlen) > IP_MAXPACKET) {
1430 		error = EMSGSIZE;
1431 		goto release;
1432 	}
1433 
1434 	/*
1435 	 * Fill in mbuf with extended DCCP header
1436 	 * and addresses and length put into network format.
1437 	 */
1438 #ifdef INET6
1439 	if (isipv6) {
1440 		ip6 = mtod(m, struct ip6_hdr *);
1441 		dh = (struct dccphdr *)(ip6 + 1);
1442 		ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
1443 			(in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
1444 		ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
1445 			 (IPV6_VERSION & IPV6_VERSION_MASK);
1446 		ip6->ip6_nxt = IPPROTO_DCCP;
1447 		ip6->ip6_src = in6p->in6p_laddr;
1448 		ip6->ip6_dst = in6p->in6p_faddr;
1449 	} else
1450 #endif
1451 	{
1452 		ip = mtod(m, struct ip *);
1453 		dh = (struct dccphdr *)(ip + 1);
1454 		memset(ip, 0, sizeof(struct ip));
1455 		ip->ip_p = IPPROTO_DCCP;
1456 		ip->ip_src = inp->inp_laddr;
1457 		ip->ip_dst = inp->inp_faddr;
1458 	}
1459 	dlh = (struct dccplhdr *)dh;
1460 
1461 	if (inp) {
1462 		dh->dh_sport = inp->inp_lport;
1463 		dh->dh_dport = inp->inp_fport;
1464 	}
1465 #ifdef INET6
1466 	else if (in6p) {
1467 		dh->dh_sport = in6p->in6p_lport;
1468 		dh->dh_dport = in6p->in6p_fport;
1469 	}
1470 #endif
1471 	dh->dh_cscov = dp->cslen;
1472 	dh->dh_ccval = dp->ccval;
1473 	dh->dh_type = type;
1474 	dh->dh_res = 0; /* Reserved field should be zero */
1475 	if (!use_shortseq) {
1476 		dlh->dh_res2 = 0; /* Reserved field should be zero */
1477 		dh->dh_off = 4 + (extrah_len / 4) + (optlen / 4);
1478 	} else
1479 		dh->dh_off = 3 + (extrah_len / 4) + (optlen / 4);
1480 
1481 	dp->seq_snd = (dp->seq_snd +1) % 281474976710656LL;
1482 	if (!use_shortseq) {
1483 		DSEQ_TO_DHDR(dlh, dp->seq_snd);
1484 		dlh->dh_x = 1;
1485 	} else {
1486 		/* short sequene number */
1487 		dh->dh_seq = htonl(dp->seq_snd) >> 8;
1488 		dh->dh_x = 0;
1489 	}
1490 
1491 	if (!use_shortseq) {
1492 		DCCP_DEBUG((LOG_INFO, "Sending with seq %x.%x, (dp->seq_snd = %llu)\n\n", dlh->dh_seq, dlh->dh_seq2, dp->seq_snd));
1493 	} else {
1494 		DCCP_DEBUG((LOG_INFO, "Sending with seq %x, (dp->seq_snd = %llu)\n\n", dh->dh_seq, dp->seq_snd));
1495 	}
1496 
1497 	if (dh->dh_type == DCCP_TYPE_REQUEST) {
1498 		drqh = (struct dccp_requesthdr *)(dlh + 1);
1499 		drqh->drqh_scode = dp->scode;
1500 		optp = (u_char *)(drqh + 1);
1501 	} else if (dh->dh_type == DCCP_TYPE_RESET) {
1502 		drth = (struct dccp_resethdr *)(dlh + 1);
1503 		drth->drth_dash.dah_res = 0;
1504 		DSEQ_TO_DAHDR(drth->drth_dash, dp->seq_rcv);
1505 		if (dp->state == DCCPS_SERVER_CLOSE)
1506 			drth->drth_reason = 1;
1507 		else
1508 			drth->drth_reason = 2;
1509 		drth->drth_data1 = 0;
1510 		drth->drth_data2 = 0;
1511 		drth->drth_data3 = 0;
1512 		optp = (u_char *)(drth + 1);
1513 	} else if (extrah_len) {
1514 		if (!use_shortseq){
1515 			dalh = (struct dccp_acklhdr *)(dlh + 1);
1516 			dalh->dash.dah_res = 0; /* Reserved field should be zero */
1517 
1518 			if (dp->state == DCCPS_ESTAB) {
1519 				DSEQ_TO_DAHDR(dalh->dash, dp->ack_snd);
1520 				dp->ack_snd = 0;
1521 			} else {
1522 				DSEQ_TO_DAHDR(dalh->dash, dp->seq_rcv);
1523 			}
1524 
1525 			if (dh->dh_type == DCCP_TYPE_RESPONSE) {
1526 				DCCP_DEBUG((LOG_INFO, "Sending dccp type response\n"));
1527 				drqh = (struct dccp_requesthdr *)(dalh + 1);
1528 				drqh->drqh_scode = dp->scode;
1529 				optp = (u_char *)(drqh + 1);
1530 			} else
1531 				optp = (u_char *)(dalh + 1);
1532 		} else {
1533 			/* XXX shortseq */
1534 			dah = (struct dccp_ackhdr *)(dh + 1);
1535 			dah->dash.dah_res = 0; /* Reserved field should be zero */
1536 			dah->dash.dah_ack = htonl(dp->seq_rcv) >> 8;
1537 			optp = (u_char *)(dah + 1);
1538 		}
1539 
1540 	} else {
1541 		optp = (u_char *)(dlh + 1);
1542 	}
1543 
1544 	if (optlen)
1545 		memcpy(optp, options, optlen);
1546 
1547 	m->m_pkthdr.len = hdrlen + len;
1548 
1549 	if (dh->dh_cscov == 0) {
1550 #ifdef INET6
1551 		if (isipv6)
1552 			cslen = (hdrlen - sizeof(struct ip6_hdr)) + len;
1553 		else
1554 			cslen = (hdrlen - sizeof(struct ip)) + len;
1555 #else
1556 		cslen = (hdrlen - sizeof(struct ip)) + len;
1557 #endif
1558 	} else {
1559 		cslen = dh->dh_off * 4 + (dh->dh_cscov - 1) * 4;
1560 #ifdef INET6
1561 		if (isipv6) {
1562 			if (cslen > (hdrlen - sizeof(struct ip6_hdr)) + len)
1563 				cslen = (hdrlen - sizeof(struct ip6_hdr)) + len;
1564 		} else {
1565 			if (cslen > (hdrlen - sizeof(struct ip)) + len)
1566 				cslen = (hdrlen - sizeof(struct ip)) + len;
1567 		}
1568 #else
1569 		if (cslen > (hdrlen - sizeof(struct ip)) + len)
1570 			cslen = (hdrlen - sizeof(struct ip)) + len;
1571 #endif
1572 	}
1573 
1574 	/*
1575 	 * Set up checksum
1576 	 */
1577 	m->m_pkthdr.csum_flags = 0;
1578 
1579 	dh->dh_sum = 0;
1580 #ifdef INET6
1581 	if (isipv6) {
1582 		dh->dh_sum = in6_cksum(m, IPPROTO_DCCP, sizeof(struct ip6_hdr),
1583 		    cslen);
1584 	} else
1585 #endif
1586 	{
1587 		ip->ip_len = htons(hdrlen + len);
1588 		ip->ip_ttl = dp->inp_ip_ttl;	/* XXX */
1589 		ip->ip_tos = dp->inp_ip_tos;	/* XXX */
1590 
1591 		dh->dh_sum = in4_cksum(m, IPPROTO_DCCP, sizeof(struct ip),
1592 		    cslen);
1593 #ifndef __OpenBSD__
1594 		m->m_pkthdr.csum_data = offsetof(struct dccphdr, dh_sum);
1595 #endif
1596 	}
1597 
1598 	dccpstat.dccps_opackets++;
1599 	dccpstat.dccps_obytes += m->m_pkthdr.len;
1600 
1601 #ifdef INET6
1602 	if (isipv6) {
1603 		DCCP_DEBUG((LOG_INFO, "Calling ip_output6, mbuf->m_len = %u, mbuf->m_pkthdr.len = %u\n", m->m_len, m->m_pkthdr.len));
1604 
1605 		error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
1606 		    (in6p->in6p_socket->so_options & SO_DONTROUTE), NULL, NULL,
1607 		    NULL);
1608 	} else
1609 #endif
1610 	{
1611 		DCCP_DEBUG((LOG_INFO, "Calling ip_output, mbuf->m_len = %u, mbuf->m_pkthdr.len = %u\n", m->m_len, m->m_pkthdr.len));
1612 		error = ip_output(m, inp->inp_options, &inp->inp_route,
1613 		    (inp->inp_socket->so_options & SO_DONTROUTE), 0,
1614 				  inp);
1615 	}
1616 
1617 	if (error) {
1618 		DCCP_DEBUG((LOG_INFO, "IP output failed! %d\n", error));
1619 		return (error);
1620 	}
1621 
1622 #if defined(INET6)
1623 	if (isipv6) {
1624 		sbdrop(&in6p->in6p_socket->so_snd, len);
1625 		sowwakeup(in6p->in6p_socket);
1626 	} else
1627 #endif
1628 	{
1629 		sbdrop(&inp->inp_socket->so_snd, len);
1630 		sowwakeup(inp->inp_socket);
1631 	}
1632 
1633 	if (dp->cc_in_use[0] > 0  && dp->state == DCCPS_ESTAB) {
1634 		DCCP_DEBUG((LOG_INFO, "Calling *cc_sw[%u].cc_send_packet_sent!\n", dp->cc_in_use[0]));
1635 		if (sendalot) {
1636 			(*cc_sw[dp->cc_in_use[0]].cc_send_packet_sent)(dp->cc_state[0], 1,len);
1637 			goto again;
1638 		} else {
1639 			(*cc_sw[dp->cc_in_use[0]].cc_send_packet_sent)(dp->cc_state[0], 0,len);
1640 		}
1641 	} else {
1642 		if (sendalot)
1643 			goto again;
1644 	}
1645 
1646 	DCCP_DEBUG((LOG_INFO, "dccp_output finished\n"));
1647 
1648 	return (0);
1649 
1650 release:
1651 	m_freem(m);
1652 	return (error);
1653 }
1654 
1655 int
1656 dccp_abort(struct socket *so)
1657 {
1658 	struct inpcb *inp = 0;
1659 	struct in6pcb *in6p;
1660 	struct dccpcb *dp;
1661 
1662 	DCCP_DEBUG((LOG_INFO, "Entering dccp_abort!\n"));
1663 	INP_INFO_WLOCK(&dccpbinfo);
1664 	if (so->so_proto->pr_domain->dom_family == PF_INET6) {
1665 		in6p = sotoin6pcb(so);
1666 		if (in6p == 0) {
1667 			return EINVAL;
1668 		}
1669 		inp = 0;
1670 		dp = (struct dccpcb *)in6p->in6p_ppcb;
1671 	} else {
1672 		inp = sotoinpcb(so);
1673 		if (inp == 0) {
1674 			INP_INFO_WUNLOCK(&dccpbinfo);
1675 			return EINVAL;
1676 		}
1677 		dp = (struct dccpcb *)inp->inp_ppcb;
1678 	}
1679 
1680 	dccp_disconnect2(dp);
1681 
1682 	INP_INFO_WUNLOCK(&dccpbinfo);
1683 	return 0;
1684 }
1685 
1686 static struct dccpcb *
1687 dccp_close(struct dccpcb *dp)
1688 {
1689 	struct socket *so;
1690 	struct inpcb *inp = dp->d_inpcb;
1691 	struct in6pcb *in6p = dp->d_in6pcb;
1692 	so = dptosocket(dp);
1693 
1694 	DCCP_DEBUG((LOG_INFO, "Entering dccp_close!\n"));
1695 
1696 	/* Stop all timers */
1697 	callout_stop(&dp->connect_timer);
1698 	callout_stop(&dp->retrans_timer);
1699 	callout_stop(&dp->close_timer);
1700 	callout_stop(&dp->timewait_timer);
1701 
1702 	if (dp->cc_in_use[0] > 0)
1703 		(*cc_sw[dp->cc_in_use[0]].cc_send_free)(dp->cc_state[0]);
1704 	if (dp->cc_in_use[1] > 0)
1705 		(*cc_sw[dp->cc_in_use[1]].cc_recv_free)(dp->cc_state[1]);
1706 
1707 	pool_put(&dccpcb_pool, dp);
1708 	if (inp) {
1709 		inp->inp_ppcb = NULL;
1710 		soisdisconnected(so);
1711 		in_pcbdetach(inp);
1712 	}
1713 #if defined(INET6)
1714 	else if (in6p) {
1715 		in6p->in6p_ppcb = 0;
1716 		soisdisconnected(so);
1717 		in6_pcbdetach(in6p);
1718 	}
1719 #endif
1720 	return ((struct dccpcb *)0);
1721 }
1722 
1723 /*
1724  * Runs when a new socket is created with the
1725  * socket system call or sonewconn.
1726  */
1727 int
1728 dccp_attach(struct socket *so, int proto)
1729 {
1730 	struct inpcb *inp = 0;
1731 	struct in6pcb *in6p = 0;
1732 	struct dccpcb *dp;
1733 	int s, family, error = 0;
1734 
1735 	DCCP_DEBUG((LOG_INFO, "Entering dccp_attach(proto=%d)!\n", proto));
1736 	INP_INFO_WLOCK(&dccpbinfo);
1737 	s = splsoftnet();
1738 	sosetlock(so);
1739 
1740 	family = so->so_proto->pr_domain->dom_family;
1741 	switch (family) {
1742 	case PF_INET:
1743 		inp = sotoinpcb(so);
1744 		if (inp != 0) {
1745 			error = EINVAL;
1746 			goto out;
1747 		}
1748 		error = soreserve(so, dccp_sendspace, dccp_recvspace);
1749 		if (error)
1750 			goto out;
1751 		error = in_pcballoc(so, &dccpbtable);
1752 		if (error)
1753 			goto out;
1754 		inp = sotoinpcb(so);
1755 		break;
1756 #if defined(INET6)
1757 	case PF_INET6:
1758 		in6p = sotoin6pcb(so);
1759 		if (in6p != 0) {
1760 			error = EINVAL;
1761 			goto out;
1762 		}
1763 		error = soreserve(so, dccp_sendspace, dccp_recvspace);
1764 		if (error)
1765 			goto out;
1766 		error = in6_pcballoc(so, &dccpbtable);
1767 		if (error)
1768 			goto out;
1769 		in6p = sotoin6pcb(so);
1770 		break;
1771 #endif
1772 	default:
1773 		error = EAFNOSUPPORT;
1774 		goto out;
1775 	}
1776 
1777 	if (inp)
1778 		dp = dccp_newdccpcb(PF_INET, (void *)inp);
1779 	else if (in6p)
1780 		dp = dccp_newdccpcb(PF_INET6, (void *)in6p);
1781 	else
1782 		dp = NULL;
1783 
1784 	if (dp == 0) {
1785 		int nofd = so->so_state & SS_NOFDREF;
1786 		so->so_state &= ~SS_NOFDREF;
1787 #if defined(INET6)
1788 		if (proto == PF_INET6) {
1789 			in6_pcbdetach(in6p);
1790 		} else
1791 #endif
1792 			in_pcbdetach(inp);
1793 		so->so_state |= nofd;
1794 		error = ENOBUFS;
1795 		goto out;
1796 	}
1797 
1798 #ifdef INET6
1799 	if (proto == PF_INET6) {
1800 		DCCP_DEBUG((LOG_INFO, "We are a ipv6 socket!!!\n"));
1801 		dp->inp_vflag |= INP_IPV6;
1802 	} else
1803 #endif
1804 		dp->inp_vflag |= INP_IPV4;
1805 	dp->inp_ip_ttl = ip_defttl;
1806 
1807 	dp->state = DCCPS_CLOSED;
1808 out:
1809 	splx(s);
1810 	INP_INFO_WUNLOCK(&dccpbinfo);
1811 	return error;
1812 }
1813 
1814 static int
1815 dccp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
1816 {
1817 	struct inpcb *inp;
1818 	int error;
1819 	int s;
1820 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1821 
1822 	DCCP_DEBUG((LOG_INFO, "Entering dccp_bind!\n"));
1823 	INP_INFO_WLOCK(&dccpbinfo);
1824 	inp = sotoinpcb(so);
1825 	if (inp == 0) {
1826 		INP_INFO_WUNLOCK(&dccpbinfo);
1827 		return EINVAL;
1828 	}
1829 
1830 	/* Do not bind to multicast addresses! */
1831 	if (sin->sin_family == AF_INET &&
1832 	    IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
1833 		INP_INFO_WUNLOCK(&dccpbinfo);
1834 		return EAFNOSUPPORT;
1835 	}
1836 	INP_LOCK(inp);
1837 	s = splsoftnet();
1838 	error = in_pcbbind(inp, sin, l);
1839 	splx(s);
1840 	INP_UNLOCK(inp);
1841 	INP_INFO_WUNLOCK(&dccpbinfo);
1842 	return error;
1843 }
1844 
1845 /*
1846  * Initiates a connection to a server
1847  * Called by the connect system call.
1848  */
1849 static int
1850 dccp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
1851 {
1852 	struct inpcb *inp;
1853 	struct dccpcb *dp;
1854 	int error;
1855 	struct sockaddr_in *sin;
1856 	char test[2];
1857 
1858 	DCCP_DEBUG((LOG_INFO, "Entering dccp_connect!\n"));
1859 
1860 	INP_INFO_WLOCK(&dccpbinfo);
1861 	inp = sotoinpcb(so);
1862 	if (inp == 0) {
1863 		INP_INFO_WUNLOCK(&dccpbinfo);
1864 		return EINVAL;
1865 	}
1866 	INP_LOCK(inp);
1867 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1868 		INP_UNLOCK(inp);
1869 		INP_INFO_WUNLOCK(&dccpbinfo);
1870 		return EISCONN;
1871 	}
1872 
1873 	dp = (struct dccpcb *)inp->inp_ppcb;
1874 
1875 	if (dp->state == DCCPS_ESTAB) {
1876 		DCCP_DEBUG((LOG_INFO, "Why are we in connect when we already have a established connection?\n"));
1877 	}
1878 
1879 	dp->who = DCCP_CLIENT;
1880 	dp->seq_snd = (((u_int64_t)random() << 32) | random()) % 281474976710656LL;
1881 	dp->ref_seq.hi = dp->seq_snd >> 24;
1882 	dp->ref_seq.lo = (u_int64_t)(dp->seq_snd & 0xffffff);
1883 	DCCP_DEBUG((LOG_INFO, "dccp_connect seq_snd %llu\n", dp->seq_snd));
1884 
1885 	dccpstat.dccps_connattempt++;
1886 
1887 	sin = (struct sockaddr_in *)nam;
1888 	if (sin->sin_family == AF_INET
1889 	    && IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
1890 		error = EAFNOSUPPORT;
1891 		goto bad;
1892 	}
1893 
1894 	error = dccp_doconnect(so, nam, l, 0);
1895 
1896 	if (error != 0)
1897 		goto bad;
1898 
1899 	callout_reset(&dp->retrans_timer, dp->retrans, dccp_retrans_t, dp);
1900 	callout_reset(&dp->connect_timer, DCCP_CONNECT_TIMER, dccp_connect_t, dp);
1901 
1902 	if (dccp_do_feature_nego){
1903 		test[0] = dp->pref_cc;
1904 		dccp_add_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC, test, 1);
1905 	}
1906 
1907 	error = dccp_output(dp, 0);
1908 
1909 bad:
1910 	INP_UNLOCK(inp);
1911 	INP_INFO_WUNLOCK(&dccpbinfo);
1912 	return error;
1913 }
1914 
1915 static int
1916 dccp_connect2(struct socket *so, struct socket *so2)
1917 {
1918 	KASSERT(solocked(so));
1919 
1920 	return EOPNOTSUPP;
1921 }
1922 
1923 /*
1924  *
1925  *
1926  */
1927 int
1928 dccp_doconnect(struct socket *so, struct sockaddr *nam,
1929     struct lwp *l, int isipv6)
1930 {
1931 	struct inpcb *inp;
1932 #ifdef INET6
1933 	struct in6pcb *in6p;
1934 #endif
1935 	int error = 0;
1936 
1937 	DCCP_DEBUG((LOG_INFO, "Entering dccp_doconnect!\n"));
1938 
1939 #if defined(INET6)
1940 	if (isipv6) {
1941 		in6p = sotoin6pcb(so);
1942 		inp = 0;
1943 	} else
1944 #endif
1945 	{
1946 		inp = sotoinpcb(so);
1947 		in6p = 0;
1948 	}
1949 
1950 #if !defined(__NetBSD__) || !defined(INET6)
1951 	if (inp->inp_lport == 0) {
1952 #else
1953 	if (isipv6 ? in6p->in6p_lport == 0 : inp->inp_lport == 0) {
1954 #endif
1955 #ifdef INET6
1956 		if (isipv6) {
1957 			DCCP_DEBUG((LOG_INFO, "Running in6_pcbbind!\n"));
1958 			error = in6_pcbbind(in6p, NULL, l);
1959 		} else
1960 #endif /* INET6 */
1961 		{
1962 			error = in_pcbbind(inp, NULL, l);
1963 		}
1964 		if (error) {
1965 			DCCP_DEBUG((LOG_INFO, "in_pcbbind=%d\n",error));
1966 			return error;
1967 		}
1968 	}
1969 
1970 #ifdef INET6
1971 	if (isipv6) {
1972 		error = in6_pcbconnect(in6p, (struct sockaddr_in6 *)nam, l);
1973 		DCCP_DEBUG((LOG_INFO, "in6_pcbconnect=%d\n",error));
1974 	} else
1975 #endif
1976 		error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l);
1977 	if (error) {
1978 		DCCP_DEBUG((LOG_INFO, "in_pcbconnect=%d\n",error));
1979 		return error;
1980 	}
1981 
1982 	soisconnecting(so);
1983 	return error;
1984 }
1985 
1986 /*
1987  * Detaches the DCCP protocol from the socket.
1988  *
1989  */
1990 int
1991 dccp_detach(struct socket *so)
1992 {
1993 	struct inpcb *inp;
1994 	struct in6pcb *in6p;
1995 	struct dccpcb *dp;
1996 
1997 	DCCP_DEBUG((LOG_INFO, "Entering dccp_detach!\n"));
1998 #ifdef INET6
1999 	if (so->so_proto->pr_domain->dom_family == AF_INET6) {
2000 		in6p = sotoin6pcb(so);
2001 		if (in6p == 0) {
2002 			return EINVAL;
2003 		}
2004 		dp = (struct dccpcb *)in6p->in6p_ppcb;
2005 	} else
2006 #endif
2007 	{
2008 		inp = sotoinpcb(so);
2009 		if (inp == 0) {
2010 			return EINVAL;
2011 		}
2012 		dp = (struct dccpcb *)inp->inp_ppcb;
2013 	}
2014 	if (! dccp_disconnect2(dp)) {
2015 		INP_UNLOCK(inp);
2016 	}
2017 	INP_INFO_WUNLOCK(&dccpbinfo);
2018 	return 0;
2019 }
2020 
2021 /*
2022  *
2023  *
2024  */
2025 int
2026 dccp_disconnect(struct socket *so)
2027 {
2028 	struct inpcb *inp;
2029 	struct in6pcb *in6p;
2030 	struct dccpcb *dp;
2031 
2032 	DCCP_DEBUG((LOG_INFO, "Entering dccp_disconnect!\n"));
2033 	INP_INFO_WLOCK(&dccpbinfo);
2034 #ifndef __NetBSD__
2035 	inp = sotoinpcb(so);
2036 	if (inp == 0) {
2037 		INP_INFO_WUNLOCK(&dccpbinfo);
2038 		return EINVAL;
2039 	}
2040 	INP_LOCK(inp);
2041 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
2042 		INP_INFO_WUNLOCK(&dccpbinfo);
2043 		INP_UNLOCK(inp);
2044 		return ENOTCONN;
2045 	}
2046 
2047 	dp = (struct dccpcb *)inp->inp_ppcb;
2048 #else /* NetBSD */
2049 #ifdef INET6
2050 	if (so->so_proto->pr_domain->dom_family == AF_INET6) {
2051 		in6p = sotoin6pcb(so);
2052 		if (in6p == 0) {
2053 			INP_INFO_WUNLOCK(&dccpbinfo);
2054 			return EINVAL;
2055 		}
2056 		dp = (struct dccpcb *)in6p->in6p_ppcb;
2057 	} else
2058 #endif
2059 	{
2060 		inp = sotoinpcb(so);
2061 		if (inp == 0) {
2062 			return EINVAL;
2063 		}
2064 		dp = (struct dccpcb *)inp->inp_ppcb;
2065 	}
2066 #endif
2067 	if (!dccp_disconnect2(dp)) {
2068 		INP_UNLOCK(inp);
2069 	}
2070 	INP_INFO_WUNLOCK(&dccpbinfo);
2071 	return 0;
2072 }
2073 
2074 /*
2075  * If we have don't have a established connection
2076  * we can call dccp_close, otherwise we can just
2077  * set SS_ISDISCONNECTED and flush the receive queue.
2078  */
2079 static int
2080 dccp_disconnect2(struct dccpcb *dp)
2081 {
2082 	struct socket *so = dptosocket(dp);
2083 
2084 	DCCP_DEBUG((LOG_INFO, "Entering dccp_disconnect2!\n"));
2085 
2086 	if (dp->state < DCCPS_ESTAB) {
2087 		dccp_close(dp);
2088 		return 1;
2089 	} else {
2090 		soisdisconnecting(so);
2091 		sbflush(&so->so_rcv);
2092 		if (dp->state == DCCPS_ESTAB) {
2093 			dp->retrans = 100;
2094 			callout_reset(&dp->retrans_timer, dp->retrans,
2095 			    dccp_retrans_t, dp);
2096 			callout_reset(&dp->close_timer, DCCP_CLOSE_TIMER,
2097 			    dccp_close_t, dp);
2098 			if (dp->who == DCCP_CLIENT) {
2099 				dp->state = DCCPS_CLIENT_CLOSE;
2100 			} else {
2101 				dp->state = DCCPS_SERVER_CLOSE;
2102 			}
2103 			dccp_output(dp, 0);
2104 		}
2105 	}
2106 	return 0;
2107 }
2108 
2109 int
2110 dccp_send(struct socket *so, struct mbuf *m, struct sockaddr *addr,
2111     struct mbuf *control, struct lwp *l)
2112 {
2113 	struct inpcb	*inp;
2114 	struct dccpcb	*dp;
2115 	int		error = 0;
2116 	int		isipv6 = 0;
2117 
2118 	DCCP_DEBUG((LOG_INFO, "Entering dccp_send!\n"));
2119 	KASSERT(solocked(so));
2120 	KASSERT(m != NULL);
2121 
2122 	if (control && control->m_len) {
2123 		m_freem(control);
2124 		m_freem(m);
2125 		return EINVAL;
2126 	}
2127 
2128 #ifdef INET6
2129 	isipv6 = addr && addr->sa_family == AF_INET6;
2130 #endif
2131 
2132 #if defined(INET6)
2133 	if (so->so_proto->pr_domain->dom_family == AF_INET6) {
2134 		struct in6pcb	*in6p;
2135 		in6p = sotoin6pcb(so);
2136 		if (in6p == 0) {
2137 			error = EINVAL;
2138 			goto release;
2139 		}
2140 		dp = (struct dccpcb *)in6p->in6p_ppcb;
2141 	} else
2142 #endif
2143 	{
2144 		INP_INFO_WLOCK(&dccpbinfo);
2145 		inp = sotoinpcb(so);
2146 		if (inp == 0) {
2147 			error = EINVAL;
2148 			goto release;
2149 		}
2150 		INP_LOCK(inp);
2151 		dp = (struct dccpcb *)inp->inp_ppcb;
2152 	}
2153 	if (dp->state != DCCPS_ESTAB) {
2154 		DCCP_DEBUG((LOG_INFO, "We have no established connection!\n"));
2155 	}
2156 
2157 	if (control != NULL) {
2158 		DCCP_DEBUG((LOG_INFO, "We got a control message!\n"));
2159 		/* Are we going to use control messages??? */
2160 		if (control->m_len) {
2161 			m_freem(control);
2162 		}
2163 	}
2164 
2165 	if (sbspace(&so->so_snd) < -512) {
2166 		INP_UNLOCK(inp);
2167 		error = ENOBUFS;
2168 		goto release;
2169 	}
2170 
2171 	if (m->m_pkthdr.len > dp->d_maxseg) {
2172 		/* XXX we should calculate packet size more carefully */
2173 		INP_UNLOCK(inp);
2174 		error = EINVAL;
2175 		goto release;
2176 	}
2177 
2178 	if (dp->pktcnt >= DCCP_MAX_PKTS) {
2179 		INP_UNLOCK(inp);
2180 		error = ENOBUFS;
2181 		goto release;
2182 	}
2183 
2184 	sbappend(&so->so_snd, m);
2185 	dp->pktlen[(dp->pktlenidx + dp->pktcnt) % DCCP_MAX_PKTS] = m->m_pkthdr.len;
2186 	dp->pktcnt ++;
2187 
2188 	if (addr && dp->state == DCCPS_CLOSED) {
2189 		error = dccp_doconnect(so, addr, l, isipv6);
2190 		if (error)
2191 			goto out;
2192 	}
2193 
2194 	error = dccp_output(dp, 0);
2195 
2196 out:
2197 	INP_UNLOCK(inp);
2198 	INP_INFO_WUNLOCK(&dccpbinfo);
2199 	return error;
2200 
2201 release:
2202 	INP_INFO_WUNLOCK(&dccpbinfo);
2203 	m_freem(m);
2204 	return (error);
2205 }
2206 
2207 /*
2208  * Sets socket to SS_CANTSENDMORE
2209  */
2210 int
2211 dccp_shutdown(struct socket *so)
2212 {
2213 	struct inpcb *inp;
2214 
2215 	DCCP_DEBUG((LOG_INFO, "Entering dccp_shutdown!\n"));
2216 	INP_INFO_RLOCK(&dccpbinfo);
2217 	inp = sotoinpcb(so);
2218 	if (inp == 0) {
2219 		INP_INFO_RUNLOCK(&dccpbinfo);
2220 		return EINVAL;
2221 	}
2222 	INP_LOCK(inp);
2223 	INP_INFO_RUNLOCK(&dccpbinfo);
2224 	socantsendmore(so);
2225 	INP_UNLOCK(inp);
2226 	return 0;
2227 }
2228 
2229 static int
2230 dccp_listen(struct socket *so, struct lwp *td)
2231 {
2232 	struct inpcb *inp;
2233 	struct dccpcb *dp;
2234 	int error = 0;
2235 
2236 	DCCP_DEBUG((LOG_INFO, "Entering dccp_listen!\n"));
2237 
2238 	INP_INFO_RLOCK(&dccpbinfo);
2239 	inp = sotoinpcb(so);
2240 	if (inp == 0) {
2241 		INP_INFO_RUNLOCK(&dccpbinfo);
2242 		return EINVAL;
2243 	}
2244 	INP_LOCK(inp);
2245 	INP_INFO_RUNLOCK(&dccpbinfo);
2246 	dp = (struct dccpcb *)inp->inp_ppcb;
2247 	if (inp->inp_lport == 0)
2248 		error = in_pcbbind(inp, NULL, td);
2249 	if (error == 0) {
2250 		dp->state = DCCPS_LISTEN;
2251 		dp->who = DCCP_LISTENER;
2252 	}
2253 	INP_UNLOCK(inp);
2254 	return error;
2255 }
2256 
2257 /*
2258  * Accepts a connection (accept system call)
2259  */
2260 static int
2261 dccp_accept(struct socket *so, struct sockaddr *nam)
2262 {
2263 	struct inpcb *inp = NULL;
2264 	int error = 0;
2265 
2266 	DCCP_DEBUG((LOG_INFO, "Entering dccp_accept!\n"));
2267 
2268 	if (nam == NULL) {
2269 		return EINVAL;
2270 	}
2271 	if (so->so_state & SS_ISDISCONNECTED) {
2272 		DCCP_DEBUG((LOG_INFO, "so_state && SS_ISDISCONNECTED!, so->state = %i\n", so->so_state));
2273 		return ECONNABORTED;
2274 	}
2275 
2276 	INP_INFO_RLOCK(&dccpbinfo);
2277 	inp = sotoinpcb(so);
2278 	if (inp == 0) {
2279 		INP_INFO_RUNLOCK(&dccpbinfo);
2280 		return EINVAL;
2281 	}
2282 	INP_LOCK(inp);
2283 	INP_INFO_RUNLOCK(&dccpbinfo);
2284 	in_setpeeraddr(inp, (struct sockaddr_in *)nam);
2285 
2286 	return error;
2287 }
2288 
2289 /*
2290  * Initializes a new DCCP control block
2291  * (in_pcballoc in attach has already allocated memory for it)
2292  */
2293 struct dccpcb *
2294 dccp_newdccpcb(int family, void *aux)
2295 {
2296 	struct inpcb *inp;
2297 	struct in6pcb *in6p;
2298 	struct dccpcb	*dp;
2299 
2300 	DCCP_DEBUG((LOG_INFO, "Creating a new dccpcb!\n"));
2301 
2302 	dp = pool_get(&dccpcb_pool, PR_NOWAIT);
2303 	if (dp == NULL)
2304 		return NULL;
2305 	memset((char *) dp, 0, sizeof(struct dccpcb));
2306 
2307 	callout_init(&dp->connect_timer, 0);
2308 	callout_init(&dp->retrans_timer, 0);
2309 	callout_init(&dp->close_timer, 0);
2310 	callout_init(&dp->timewait_timer, 0);
2311 
2312 	dp->ndp = 0;
2313 	dp->loss_window = 1000;
2314 	dp->cslen = 0;
2315 	dp->pref_cc = DEFAULT_CCID;
2316 	dp->who = DCCP_UNDEF;
2317 	dp->seq_snd = 0;
2318 	dp->seq_rcv = 0;
2319 	dp->shortseq = 0;
2320 	dp->gsn_rcv = 281474976710656LL;
2321 	dp->optlen = 0;
2322 	if (dccp_do_feature_nego){
2323 		dp->cc_in_use[0] = -1;
2324 		dp->cc_in_use[1] = -1;
2325 	} else {
2326 		/* for compatibility with linux */
2327 		dp->cc_in_use[0] = 4;
2328 		dp->cc_in_use[1] = 4;
2329 	}
2330 	dp->av_size = 0; /* no ack vector initially */
2331 	dp->remote_ackvector = 0; /* no ack vector on remote side initially */
2332 	dp->retrans = 200;
2333 	dp->avgpsize = 0;
2334 	dp->d_maxseg = 1400;
2335 	dp->ref_pseq.hi = 0;
2336 	dp->ref_pseq.lo = 0;
2337 	dp->pktlenidx = 0;
2338 	dp->pktcnt = 0;
2339 
2340 	switch (family) {
2341 	case PF_INET:
2342 		inp = (struct inpcb *)aux;
2343 		dp->d_inpcb = inp;
2344 		inp->inp_ip.ip_ttl = ip_defttl;
2345 		inp->inp_ppcb = dp;
2346 		break;
2347 	case PF_INET6:
2348 		in6p = (struct in6pcb *)aux;
2349 		dp->d_in6pcb = in6p;
2350 		in6p->in6p_ip6.ip6_hlim = in6_selecthlim_rt(in6p);
2351 		in6p->in6p_ppcb = dp;
2352 		break;
2353 	}
2354 
2355 	if (!dccp_do_feature_nego){
2356 		dp->cc_state[0] = (*cc_sw[4].cc_send_init)(dp);
2357 		dp->cc_state[1] = (*cc_sw[4].cc_recv_init)(dp);
2358 	}
2359 
2360 	return dp;
2361 }
2362 
2363 int
2364 dccp_add_option(struct dccpcb *dp, u_int8_t opt, char *val, u_int8_t val_len)
2365 {
2366 	return dccp_add_feature_option(dp, opt, 0, val, val_len);
2367 }
2368 
2369 int
2370 dccp_add_feature_option(struct dccpcb *dp, u_int8_t opt, u_int8_t feature, char *val, u_int8_t val_len)
2371 {
2372 	int i;
2373 	DCCP_DEBUG((LOG_INFO, "Entering dccp_add_feature_option, opt = %u, val_len = %u optlen %u\n", opt, val_len, dp->optlen));
2374 
2375 	if (DCCP_MAX_OPTIONS > (dp->optlen + val_len + 2)) {
2376 		dp->options[dp->optlen] = opt;
2377 		if (opt < 32) {
2378 			dp->optlen++;
2379 		} else {
2380 			if (opt == DCCP_OPT_CONFIRM_L && val_len) {
2381 				dp->options[dp->optlen + 1] = val_len + 3;
2382 				dp->options[dp->optlen +2] = feature;
2383 				dp->optlen += 3;
2384 			} else {
2385 				dp->options[dp->optlen + 1] = val_len + 2;
2386 				dp->optlen += 2;
2387 			}
2388 
2389 			for (i = 0; i<val_len; i++) {
2390 				dp->options[dp->optlen] = val[i];
2391 				dp->optlen++;
2392 			}
2393 		}
2394 	} else {
2395 		DCCP_DEBUG((LOG_INFO, "No room for more options, optlen = %u\n", dp->optlen));
2396 		return -1;
2397 	}
2398 
2399 	return 0;
2400 }
2401 
2402 /*
2403  * Searches "options" for given option type. if found, the data is copied to buffer
2404  * and returns the data length.
2405  * Returns 0 if option type not found
2406  */
2407 int
2408 dccp_get_option(char *options, int optlen, int type, char *buffer, int buflen)
2409 {
2410 	int i, j, size;
2411 	u_int8_t t;
2412 
2413 	for (i=0; i < optlen;) {
2414 		t = options[i++];
2415 		if (t >= 32) {
2416 			size = options[i++] - 2;
2417 			if (t == type) {
2418 				if (size > buflen)
2419 					return 0;
2420 				for (j = 0; j < size; j++)
2421 					buffer[j] = options[i++];
2422 				return size;
2423 			}
2424 			i += size;
2425 		}
2426 	}
2427 	/* If we get here the options was not found */
2428 	DCCP_DEBUG((LOG_INFO, "dccp_get_option option(%d) not found\n", type));
2429 	return 0;
2430 }
2431 
2432 void
2433 dccp_parse_options(struct dccpcb *dp, char *options, int optlen)
2434 {
2435 	u_int8_t opt, size, i, j;
2436 	char val[8];
2437 
2438 	for (i = 0; i < optlen; i++) {
2439 		opt = options[i];
2440 
2441 		DCCP_DEBUG((LOG_INFO, "Parsing opt: 0x%02x\n", opt));
2442 
2443 		if (opt < 32) {
2444 			switch (opt) {
2445 			    case DCCP_OPT_PADDING:
2446 				DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_PADDING!\n"));
2447 				break;
2448 			    case DCCP_OPT_DATA_DISCARD:
2449 				DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_DATA_DISCARD!\n"));
2450 				break;
2451 			    case DCCP_OPT_SLOW_RECV:
2452 				DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_SLOW_RECV!\n"));
2453 				break;
2454 			    case DCCP_OPT_BUF_CLOSED:
2455 				DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_BUF_CLOSED!\n"));
2456 				break;
2457 			    default:
2458 				DCCP_DEBUG((LOG_INFO, "Got an unknown option, option = %u!\n", opt));
2459 			}
2460 		} else if (opt > 32 && opt < 36) {
2461 			size = options[i+ 1];
2462 			if (size < 3 || size > 10) {
2463 				DCCP_DEBUG((LOG_INFO, "Error, option size = %u\n", size));
2464 				return;
2465 			}
2466 			/* Feature negotiations are options 33 to 35 */
2467 			DCCP_DEBUG((LOG_INFO, "Got option %u, size = %u, feature = %u\n", opt, size, options[i+2]));
2468 			memcpy(val, options + i + 3, size -3);
2469 			DCCP_DEBUG((LOG_INFO, "Calling dccp_feature neg(%u, %u, options[%u + 1], %u)!\n", (u_int)dp, opt, i+ 1, (size - 3)));
2470 			dccp_feature_neg(dp, opt, options[i+2], (size -3) , val);
2471 			i += size - 1;
2472 
2473 		} else if (opt < 128) {
2474 			size = options[i+ 1];
2475 			if (size < 3 || size > 10) {
2476 				DCCP_DEBUG((LOG_INFO, "Error, option size = %u\n", size));
2477 				return;
2478 			}
2479 
2480 			switch (opt) {
2481 			    case DCCP_OPT_RECV_BUF_DROPS:
2482 					DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_RECV_BUF_DROPS, size = %u!\n", size));
2483 					for (j=2; j < size; j++) {
2484 						DCCP_DEBUG((LOG_INFO, "val[%u] = %u ", j-1, options[i+j]));
2485 					}
2486 					DCCP_DEBUG((LOG_INFO, "\n"));
2487 				break;
2488 
2489 			    case DCCP_OPT_TIMESTAMP:
2490 					DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_TIMESTAMP, size = %u\n", size));
2491 
2492 					/* Adding TimestampEcho to next outgoing */
2493 					memcpy(val, options + i + 2, 4);
2494 					memset(val + 4, 0, 4);
2495 					dccp_add_option(dp, DCCP_OPT_TIMESTAMP_ECHO, val, 8);
2496 				break;
2497 
2498 			    case DCCP_OPT_TIMESTAMP_ECHO:
2499 					DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_TIMESTAMP_ECHO, size = %u\n",size));
2500 					for (j=2; j < size; j++) {
2501 						DCCP_DEBUG((LOG_INFO, "val[%u] = %u ", j-1, options[i+j]));
2502 					}
2503 					DCCP_DEBUG((LOG_INFO, "\n"));
2504 
2505 					/*
2506 					memcpy(&(dp->timestamp_echo), options + i + 2, 4);
2507 					memcpy(&(dp->timestamp_elapsed), options + i + 6, 4);
2508 					ACK_DEBUG((LOG_INFO, "DATA; echo = %u , elapsed = %u\n",
2509 					    dp->timestamp_echo, dp->timestamp_elapsed));
2510 					*/
2511 
2512 				break;
2513 
2514 			case DCCP_OPT_ACK_VECTOR0:
2515 			case DCCP_OPT_ACK_VECTOR1:
2516 			case DCCP_OPT_ELAPSEDTIME:
2517 				/* Dont do nothing here. Let the CC deal with it */
2518 				break;
2519 
2520 			default:
2521 				DCCP_DEBUG((LOG_INFO, "Got an unknown option, option = %u, size = %u!\n", opt, size));
2522 				break;
2523 
2524 			}
2525 			i += size - 1;
2526 
2527 		} else {
2528 			DCCP_DEBUG((LOG_INFO, "Got a CCID option (%d), do nothing!\n", opt));
2529 			size = options[i+ 1];
2530 			if (size < 3 || size > 10) {
2531 				DCCP_DEBUG((LOG_INFO, "Error, option size = %u\n", size));
2532 				return;
2533 			}
2534 			i += size - 1;
2535 		}
2536 	}
2537 
2538 }
2539 
2540 int
2541 dccp_add_feature(struct dccpcb *dp, u_int8_t opt, u_int8_t feature, char *val, u_int8_t val_len)
2542 {
2543 	int i;
2544 	DCCP_DEBUG((LOG_INFO, "Entering dccp_add_feature, opt = %u, feature = %u, val_len = %u\n", opt, feature, val_len));
2545 
2546 	if (DCCP_MAX_OPTIONS > (dp->featlen + val_len + 3)) {
2547 		dp->features[dp->featlen] = opt;
2548 		dp->features[dp->featlen + 1] = val_len + 3;
2549 		dp->features[dp->featlen +2] = feature;
2550 		dp->featlen += 3;
2551 		for (i = 0; i<val_len; i++) {
2552 			dp->features[dp->featlen] = val[i];
2553 			dp->featlen++;
2554 		}
2555 	} else {
2556 		DCCP_DEBUG((LOG_INFO, "No room for more features, featlen = %u\n", dp->featlen));
2557 		return -1;
2558 	}
2559 
2560 	return 0;
2561 }
2562 
2563 int
2564 dccp_remove_feature(struct dccpcb *dp, u_int8_t opt, u_int8_t feature)
2565 {
2566 	int i = 0, j = 0, k;
2567 	u_int8_t t_opt, t_feature, len;
2568 	DCCP_DEBUG((LOG_INFO, "Entering dccp_remove_feature, featlen = %u, opt = %u, feature = %u\n", dp->featlen, opt, feature));
2569 
2570 	while (i < dp->featlen) {
2571 		t_opt = dp->features[i];
2572 		len = dp->features[i+ 1];
2573 
2574 		if (i + len > dp->featlen) {
2575 			DCCP_DEBUG((LOG_INFO, "Error, len = %u and i(%u) + len > dp->featlen (%u)\n", len, i, dp->featlen));
2576 			return 1;
2577 		}
2578 		t_feature = dp->features[i+2];
2579 
2580 		if (t_opt == opt && t_feature == feature) {
2581 			i += len;
2582 		} else {
2583 			if (i != j) {
2584 				for (k = 0; k < len; k++) {
2585 					dp->features[j+k] = dp->features[i+k];
2586 				}
2587 			}
2588 			i += len;
2589 			j += len;
2590 		}
2591 	}
2592 	dp->featlen = j;
2593 	DCCP_DEBUG((LOG_INFO, "Exiting dccp_remove_feature, featlen = %u\n", dp->featlen));
2594 	return 0;
2595 }
2596 
2597 void
2598 dccp_feature_neg(struct dccpcb *dp, u_int8_t opt, u_int8_t feature, u_int8_t val_len, char *val)
2599 {
2600 	DCCP_DEBUG((LOG_INFO, "Running dccp_feature_neg, opt = %u, feature = %u len = %u ", opt, feature, val_len));
2601 
2602 	switch (feature) {
2603 		case DCCP_FEATURE_CC:
2604 			DCCP_DEBUG((LOG_INFO, "Got CCID negotiation, opt = %u, val[0] = %u\n", opt, val[0]));
2605 			if (opt == DCCP_OPT_CHANGE_R) {
2606 				if (val[0] == 2 || val[0] == 3 || val[0] == 0) {
2607 					/* try to use preferable CCID */
2608 					int i;
2609 					for (i = 1; i < val_len; i ++) if (val[i] == dp->pref_cc) val[0] = dp->pref_cc;
2610 					DCCP_DEBUG((LOG_INFO, "Sending DCCP_OPT_CONFIRM_L on CCID %u\n", val[0]));
2611 					dccp_remove_feature(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_CC);
2612 					dccp_add_feature_option(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_CC , val, 1);
2613 					if (dp->cc_in_use[0] < 1) {
2614 						dp->cc_state[0] = (*cc_sw[val[0] + 1].cc_send_init)(dp);
2615 						dp->cc_in_use[0] = val[0] + 1;
2616 					} else {
2617 						DCCP_DEBUG((LOG_INFO, "We already have negotiated a CC!!!\n"));
2618 					}
2619 				}
2620 			} else if (opt == DCCP_OPT_CONFIRM_L) {
2621 				DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_CONFIRM_L on CCID %u\n", val[0]));
2622 				dccp_remove_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC);
2623 				if (dp->cc_in_use[1] < 1) {
2624 					dp->cc_state[1] = (*cc_sw[val[0] + 1].cc_recv_init)(dp);
2625 					dp->cc_in_use[1] = val[0] + 1;
2626 					DCCP_DEBUG((LOG_INFO, "confirmed cc_in_use[1] = %d\n", dp->cc_in_use[1]));
2627 				} else {
2628 					DCCP_DEBUG((LOG_INFO, "We already have negotiated a CC!!! (confirm) %d\n", dp->cc_in_use[1]));
2629 				}
2630 			}
2631 
2632 		break;
2633 
2634 		case DCCP_FEATURE_ACKVECTOR:
2635 			ACK_DEBUG((LOG_INFO, "Got _Use Ack Vector_\n"));
2636 			if (opt == DCCP_OPT_CHANGE_R) {
2637 				if (val[0] == 1) {
2638 					dccp_use_ackvector(dp);
2639 					dccp_remove_feature(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_ACKVECTOR);
2640 					dccp_add_feature_option(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_ACKVECTOR , val, 1);
2641 				} else {
2642 					ACK_DEBUG((LOG_INFO, "ERROR. Strange val %u\n", val[0]));
2643 				}
2644 			} else if (opt == DCCP_OPT_CONFIRM_L) {
2645 					dccp_remove_feature(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_ACKVECTOR);
2646 			if (val[0] == 1) {
2647 					dp->remote_ackvector = 1;
2648 					ACK_DEBUG((LOG_INFO,"Remote side confirmed AckVector usage\n"));
2649 				} else {
2650 					ACK_DEBUG((LOG_INFO, "ERROR. Strange val %u\n", val[0]));
2651 				}
2652 			}
2653 			break;
2654 
2655 		case DCCP_FEATURE_ACKRATIO:
2656 			if (opt == DCCP_OPT_CHANGE_R) {
2657 				memcpy(&(dp->ack_ratio), val, 1);
2658 				ACK_DEBUG((LOG_INFO, "Feature: Change Ack Ratio to %u\n", dp->ack_ratio));
2659 			}
2660 			break;
2661 
2662 		case DCCP_FEATURE_ECN:
2663 		case DCCP_FEATURE_MOBILITY:
2664 		default:
2665 			/* we should send back empty CONFIRM_L for unknown feature unless it's not mandatory */
2666 			dccp_add_option(dp, DCCP_OPT_CONFIRM_L, NULL, 0);
2667 		break;
2668 
2669 	}
2670 }
2671 
2672 #ifdef __FreeBSD__
2673 static int
2674 dccp_pcblist(SYSCTL_HANDLER_ARGS)
2675 {
2676 
2677 	int error, i, n, s;
2678 	struct inpcb *inp, **inp_list;
2679 	inp_gen_t gencnt;
2680 	struct xinpgen xig;
2681 
2682 	/*
2683 	 * The process of preparing the TCB list is too time-consuming and
2684 	 * resource-intensive to repeat twice on every request.
2685 	 */
2686 	if (req->oldptr == 0) {
2687 		n = dccpbinfo.ipi_count;
2688 		req->oldidx = 2 * (sizeof xig)
2689 			+ (n + n/8) * sizeof(struct xdccpcb);
2690 		return 0;
2691 	}
2692 
2693 
2694 	if (req->newptr != 0)
2695 		return EPERM;
2696 
2697 
2698 	/*
2699 	 * OK, now we're committed to doing something.
2700 	 */
2701 	s = splnet();
2702 	gencnt = dccpbinfo.ipi_gencnt;
2703 	n = dccpbinfo.ipi_count;
2704 	splx(s);
2705 
2706 #if __FreeBSD_version >= 500000
2707 	sysctl_wire_old_buffer(req, 2 * (sizeof xig)
2708 		+ n * sizeof(struct xdccpcb));
2709 #endif
2710 
2711 	xig.xig_len = sizeof xig;
2712 	xig.xig_count = n;
2713 	xig.xig_gen = gencnt;
2714 	xig.xig_sogen = so_gencnt;
2715 	error = SYSCTL_OUT(req, &xig, sizeof xig);
2716 	if (error)
2717 		return error;
2718 
2719 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
2720 	if (inp_list == 0)
2721 		return ENOMEM;
2722 
2723 	s = splsoftnet();
2724 	INP_INFO_RLOCK(&dccpbinfo);
2725 
2726 	for (inp = LIST_FIRST(dccpbinfo.listhead), i = 0; inp && i < n;
2727 	     inp = LIST_NEXT(inp, inp_list)) {
2728 		INP_LOCK(inp);
2729 		if (inp->inp_gencnt <= gencnt &&
2730 #if __FreeBSD_version >= 500000
2731 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
2732 #else
2733 		    !prison_xinpcb(req->p, inp))
2734 #endif
2735 			inp_list[i++] = inp;
2736 		INP_UNLOCK(inp);
2737 	}
2738 	INP_INFO_RUNLOCK(&dccpbinfo);
2739 	splx(s);
2740 	n = i;
2741 
2742 	error = 0;
2743 	for (i = 0; i < n; i++) {
2744 		inp = inp_list[i];
2745 		INP_LOCK(inp);
2746 
2747 		if (inp->inp_gencnt <= gencnt) {
2748 			struct xdccpcb xd;
2749 			vaddr_t inp_ppcb;
2750 			xd.xd_len = sizeof xd;
2751 			/* XXX should avoid extra copy */
2752 			memcpy(&xd.xd_inp, inp, sizeof *inp);
2753 			inp_ppcb = inp->inp_ppcb;
2754 			if (inp_ppcb != NULL)
2755 				memcpy(&xd.xd_dp, inp_ppcb, sizeof xd.xd_dp);
2756 			else
2757 				memset((char *) &xd.xd_dp, 0, sizeof xd.xd_dp);
2758 			if (inp->inp_socket)
2759 				 sotoxsocket(inp->inp_socket, &xd.xd_socket);
2760 			error = SYSCTL_OUT(req, &xd, sizeof xd);
2761 		}
2762 		INP_UNLOCK(inp);
2763 	}
2764 	if (!error) {
2765 		/*
2766 		 * Give the user an updated idea of our state.
2767 		 * If the generation differs from what we told
2768 		 * her before, she knows that something happened
2769 		 * while we were processing this request, and it
2770 		 * might be necessary to retry.
2771 		 */
2772 		s = splnet();
2773 		INP_INFO_RLOCK(&dccpbinfo);
2774 		xig.xig_gen = dccpbinfo.ipi_gencnt;
2775 		xig.xig_sogen = so_gencnt;
2776 		xig.xig_count = dccpbinfo.ipi_count;
2777 
2778 
2779 		INP_INFO_RUNLOCK(&dccpbinfo);
2780 		splx(s);
2781 		error = SYSCTL_OUT(req, &xig, sizeof xig);
2782 	}
2783 	free(inp_list, M_TEMP);
2784 	return error;
2785 }
2786 #endif
2787 
2788 #ifdef __FreeBSD__
2789 SYSCTL_PROC(_net_inet_dccp, DCCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
2790     dccp_pcblist, "S,xdccpcb", "List of active DCCP sockets");
2791 #endif
2792 
2793 void
2794 dccp_timewait_t(void *dcb)
2795 {
2796 	struct dccpcb *dp = dcb;
2797 
2798 	DCCP_DEBUG((LOG_INFO, "Entering dccp_timewait_t!\n"));
2799 	mutex_enter(softnet_lock);
2800 	INP_INFO_WLOCK(&dccpbinfo);
2801 	INP_LOCK(dp->d_inpcb);
2802 	dccp_close(dp);
2803 	INP_INFO_WUNLOCK(&dccpbinfo);
2804 	mutex_exit(softnet_lock);
2805 }
2806 
2807 void
2808 dccp_connect_t(void *dcb)
2809 {
2810 	struct dccpcb *dp = dcb;
2811 
2812 	DCCP_DEBUG((LOG_INFO, "Entering dccp_connect_t!\n"));
2813 	mutex_enter(softnet_lock);
2814 	INP_INFO_WLOCK(&dccpbinfo);
2815 	INP_LOCK(dp->d_inpcb);
2816 	dccp_close(dp);
2817 	INP_INFO_WUNLOCK(&dccpbinfo);
2818 	mutex_exit(softnet_lock);
2819 }
2820 
2821 void
2822 dccp_close_t(void *dcb)
2823 {
2824 	struct dccpcb *dp = dcb;
2825 
2826 	DCCP_DEBUG((LOG_INFO, "Entering dccp_close_t!\n"));
2827 	mutex_enter(softnet_lock);
2828 	INP_INFO_WLOCK(&dccpbinfo);
2829 	dp->state = DCCPS_TIME_WAIT; /* HMM */
2830 	if (dp->who == DCCP_SERVER) {
2831 		INP_LOCK(dp->d_inpcb);
2832 		KERNEL_LOCK(1, NULL);
2833 		dccp_output(dp, DCCP_TYPE_RESET + 2);
2834 		KERNEL_UNLOCK_ONE(NULL);
2835 		dccp_close(dp);
2836 	} else {
2837 		INP_LOCK(dp->d_inpcb);
2838 		dccp_output(dp, DCCP_TYPE_RESET + 2);
2839 		/*dp->state = DCCPS_TIME_WAIT; */
2840 		callout_reset(&dp->timewait_timer, DCCP_TIMEWAIT_TIMER,
2841 		    dccp_timewait_t, dp);
2842 		INP_UNLOCK(dp->d_inpcb);
2843 	}
2844 	INP_INFO_WUNLOCK(&dccpbinfo);
2845 	mutex_exit(softnet_lock);
2846 }
2847 
2848 void
2849 dccp_retrans_t(void *dcb)
2850 {
2851 	struct dccpcb *dp = dcb;
2852 	/*struct inpcb *inp;*/
2853 
2854 	DCCP_DEBUG((LOG_INFO, "Entering dccp_retrans_t!\n"));
2855 	mutex_enter(softnet_lock);
2856 	INP_INFO_RLOCK(&dccpbinfo);
2857 	/*inp = dp->d_inpcb;*/
2858 	INP_LOCK(inp);
2859 	INP_INFO_RUNLOCK(&dccpbinfo);
2860 	callout_stop(&dp->retrans_timer);
2861 	KERNEL_LOCK(1, NULL);
2862 	dccp_output(dp, 0);
2863 	KERNEL_UNLOCK_ONE(NULL);
2864 	dp->retrans = dp->retrans * 2;
2865 	callout_reset(&dp->retrans_timer, dp->retrans, dccp_retrans_t, dp);
2866 	INP_UNLOCK(inp);
2867 	mutex_exit(softnet_lock);
2868 }
2869 
2870 static int
2871 dccp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
2872 {
2873 	int error = 0;
2874 	int family;
2875 
2876 	family = so->so_proto->pr_domain->dom_family;
2877 	switch (family) {
2878 	case PF_INET:
2879 		error = in_control(so, cmd, nam, ifp);
2880 		break;
2881 #ifdef INET6
2882 	case PF_INET6:
2883 		error = in6_control(so, cmd, nam, ifp);
2884 		break;
2885 #endif
2886 	default:
2887 		error =	 EAFNOSUPPORT;
2888 	}
2889 	return (error);
2890 }
2891 
2892 static int
2893 dccp_stat(struct socket *so, struct stat *ub)
2894 {
2895 	return 0;
2896 }
2897 
2898 static int
2899 dccp_peeraddr(struct socket *so, struct sockaddr *nam)
2900 {
2901 
2902 	KASSERT(solocked(so));
2903 	KASSERT(sotoinpcb(so) != NULL);
2904 	KASSERT(nam != NULL);
2905 
2906 	in_setpeeraddr(sotoinpcb(so), (struct sockaddr_in *)nam);
2907 	return 0;
2908 }
2909 
2910 static int
2911 dccp_sockaddr(struct socket *so, struct sockaddr *nam)
2912 {
2913 
2914 	KASSERT(solocked(so));
2915 	KASSERT(sotoinpcb(so) != NULL);
2916 	KASSERT(nam != NULL);
2917 
2918 	in_setsockaddr(sotoinpcb(so), (struct sockaddr_in *)nam);
2919 	return 0;
2920 }
2921 
2922 static int
2923 dccp_rcvd(struct socket *so, int flags, struct lwp *l)
2924 {
2925 	KASSERT(solocked(so));
2926 
2927 	return EOPNOTSUPP;
2928 }
2929 
2930 static int
2931 dccp_recvoob(struct socket *so, struct mbuf *m, int flags)
2932 {
2933 	KASSERT(solocked(so));
2934 
2935 	return EOPNOTSUPP;
2936 }
2937 
2938 static int
2939 dccp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
2940 {
2941 	KASSERT(solocked(so));
2942 
2943 	m_freem(m);
2944 	m_freem(control);
2945 
2946 	return EOPNOTSUPP;
2947 }
2948 
2949 static int
2950 dccp_purgeif(struct socket *so, struct ifnet *ifp)
2951 {
2952 	int s;
2953 
2954 	s = splsoftnet();
2955 	mutex_enter(softnet_lock);
2956 	in_pcbpurgeif0(&dccpbtable, ifp);
2957 	in_purgeif(ifp);
2958 	in_pcbpurgeif(&dccpbtable, ifp);
2959 	mutex_exit(softnet_lock);
2960 	splx(s);
2961 
2962 	return 0;
2963 }
2964 
2965 /****** Ack Vector functions *********/
2966 
2967 /**
2968  * Initialize and allocate mem for Ack Vector
2969  **/
2970 void
2971 dccp_use_ackvector(struct dccpcb *dp)
2972 {
2973 	DCCP_DEBUG((LOG_INFO,"Initializing AckVector\n"));
2974 	if (dp->ackvector != 0) {
2975 		DCCP_DEBUG((LOG_INFO, "It was already initialized!!!\n"));
2976 		return;
2977 	}
2978 	dp->av_size = DCCP_VECTORSIZE;
2979 	/* need 2 bits per entry */
2980 	dp->ackvector = malloc(dp->av_size/4, M_PCB, M_NOWAIT | M_ZERO);
2981 	if (dp->ackvector == 0) {
2982 		DCCP_DEBUG((LOG_INFO, "Unable to allocate memory for ackvector\n"));
2983 		/* What to do now? */
2984 		dp->av_size = 0;
2985 		return;
2986 	}
2987 	memset(dp->ackvector, 0xff, dp->av_size/4);
2988 	dp->av_hs = dp->av_ts = 0;
2989 	dp->av_hp = dp->ackvector;
2990 }
2991 
2992 /**
2993  * Set 'seqnr' as the new head in ackvector
2994  **/
2995 void
2996 dccp_update_ackvector(struct dccpcb *dp, u_int64_t seqnr)
2997 {
2998 	int64_t gap;
2999 	u_char *t;
3000 
3001 	/* Ignore wrapping for now */
3002 
3003 	ACK_DEBUG((LOG_INFO,"New head in ackvector: %u\n", seqnr));
3004 
3005 	if (dp->av_size == 0) {
3006 		ACK_DEBUG((LOG_INFO, "Update: AckVector NOT YET INITIALIZED!!!\n"));
3007 		dccp_use_ackvector(dp);
3008 	}
3009 
3010 	if (seqnr > dp->av_hs) {
3011 		gap = seqnr - dp->av_hs;
3012 	} else {
3013 		/* We received obsolete information */
3014 		return;
3015 	}
3016 
3017 	t = dp->av_hp + (gap/4);
3018 	if (t >= (dp->ackvector + (dp->av_size/4)))
3019 		t -= (dp->av_size / 4); /* ackvector wrapped */
3020 	dp->av_hp = t;
3021 	dp->av_hs = seqnr;
3022 }
3023 
3024 /**
3025  * We've received a packet. store in local av so it's included in
3026  * next Ack Vector sent
3027  **/
3028 void
3029 dccp_increment_ackvector(struct dccpcb *dp, u_int64_t seqnr)
3030 {
3031 	u_int64_t offset, dc;
3032 	int64_t gap;
3033 	u_char *t, *n;
3034 
3035 	DCCP_DEBUG((LOG_INFO, "Entering dccp_increment_ackvecktor %d\n", dp->av_size));
3036 	if (dp->av_size == 0) {
3037 		DCCP_DEBUG((LOG_INFO, "Increment: AckVector NOT YET INITIALIZED!!!\n"));
3038 		dccp_use_ackvector(dp);
3039 	}
3040 
3041 	if (dp->av_hs == dp->av_ts) {
3042 		/* Empty ack vector */
3043 		dp->av_hs = dp->av_ts = seqnr;
3044 	}
3045 
3046 	/* Check for wrapping */
3047 	if (seqnr >= dp->av_hs) {
3048 		/* Not wrapped */
3049 		gap = seqnr - dp->av_hs;
3050 	} else {
3051 		/* Wrapped */
3052 		gap = seqnr + 0x1000000000000LL - dp->av_hs; /* seqnr = 48 bits */
3053 	}
3054 	DCCP_DEBUG((LOG_INFO, "dccp_increment_ackvecktor gap=%llu av_size %d\n", gap, dp->av_size));
3055 
3056 	if (gap >= dp->av_size) {
3057 		/* gap is bigger than ackvector size? baaad */
3058 		/* maybe we should increase the ackvector here */
3059 		DCCP_DEBUG((LOG_INFO, "increment_ackvector error. gap: %llu, av_size: %d, seqnr: %d\n",
3060 			    gap, dp->av_size, seqnr));
3061 		return;
3062 	}
3063 
3064 	offset = gap % 4; /* hi or low 2 bits to mark */
3065 	t = dp->av_hp + (gap/4);
3066 	if (t >= (dp->ackvector + (dp->av_size/4)))
3067 		t -= (dp->av_size / 4); /* ackvector wrapped */
3068 
3069 	*t = *t & (~(0x03 << (offset *2))); /* turn off bits, 00 is rcvd, 11 is missing */
3070 
3071 	dp->av_ts = seqnr + 1;
3072 	if (dp->av_ts == 0x1000000000000LL)
3073 		dp->av_ts = 0;
3074 
3075 	if (gap > (dp->av_size - 128)) {
3076 		n = malloc(dp->av_size/2, M_PCB, M_NOWAIT | M_ZERO); /* old size * 2 */
3077 		memset (n + dp->av_size / 4, 0xff, dp->av_size / 4); /* new half all missing */
3078 		dc = (dp->ackvector + (dp->av_size/4)) - dp->av_hp;
3079 		memcpy (n, dp->av_hp, dc); /* tail to end */
3080 		memcpy (n+dc, dp->ackvector, dp->av_hp - dp->ackvector); /* start to tail */
3081 		dp->av_size = dp->av_size * 2; /* counted in items, so it';s a doubling */
3082 		free (dp->ackvector, M_PCB);
3083 		dp->av_hp = dp->ackvector = n;
3084 	}
3085 }
3086 
3087 /**
3088  * Generates the ack vector to send in outgoing packet.
3089  * These are backwards (first packet in ack vector is packet indicated by Ack Number,
3090  * subsequent are older packets).
3091  **/
3092 
3093 u_int16_t
3094 dccp_generate_ackvector(struct dccpcb *dp, u_char *buf)
3095 {
3096 	int64_t j;
3097 	u_int64_t i;
3098 	u_int16_t cnt, oldlen, bufsize;
3099 	u_char oldstate, st;
3100 
3101 	bufsize = 16;
3102 	cnt = 0;
3103 
3104 	oldstate = 0x04; /* bad value */
3105 	oldlen = 0;
3106 
3107 	if (dp->av_size == 0) {
3108 		ACK_DEBUG((LOG_INFO, "Generate: AckVector NOT YET INITIALIZED!!!\n"));
3109 		return 0;
3110 	}
3111 
3112 	if (dp->seq_rcv > dp->av_ts) {
3113 		/* AckNum is beyond our av-list , so we'll start with some
3114 		 * 0x3 (Packet not yet received) */
3115 		j = dp->seq_rcv - dp->av_ts -1;
3116 		do {
3117 			/* state | length */
3118 			oldstate = 0x03;
3119 			if (j > 63)
3120 				oldlen = 63;
3121 			else
3122 				oldlen = j;
3123 
3124 			buf[cnt] = (0x03 << 6) | oldlen;
3125 			cnt++;
3126 			if (cnt == bufsize) {
3127 				/* I've skipped the realloc bshit */
3128 				/* PANIC */
3129 			}
3130 			j-=63;
3131 		} while (j > 0);
3132 	}
3133 
3134 	/* Ok now we're at dp->av_ts (unless AckNum is lower) */
3135 	i = (dp->seq_rcv < dp->av_ts) ? dp->seq_rcv : dp->av_ts;
3136 	st = dccp_ackvector_state(dp, i);
3137 
3138 	if (st == oldstate) {
3139 		cnt--;
3140 		oldlen++;
3141 	} else {
3142 		oldlen = 0;
3143 		oldstate = st;
3144 	}
3145 
3146 	if (dp->av_ts > dp->av_hs) {
3147 		do {
3148 			i--;
3149 			st = dccp_ackvector_state(dp, i);
3150 			if (st == oldstate && oldlen < 64) {
3151 				oldlen++;
3152 			} else {
3153 				buf[cnt] = (oldstate << 6) | (oldlen & 0x3f);
3154 				cnt++;
3155 				oldlen = 0;
3156 				oldstate = st;
3157 				if (cnt == bufsize) {
3158 					/* PANIC */
3159 				}
3160 			}
3161 
3162 		} while (i > dp->av_hs);
3163 	} else {
3164 		/* It's wrapped */
3165 		do {
3166 			i--;
3167 			st = dccp_ackvector_state(dp, i);
3168 			if (st == oldstate && oldlen < 64) {
3169 				oldlen++;
3170 			} else {
3171 				buf[cnt] = (oldstate << 6) | (oldlen & 0x3f);
3172 				cnt++;
3173 				oldlen = 0;
3174 				oldstate = st;
3175 				if (cnt == bufsize) {
3176 					/* PANIC */
3177 				}
3178 			}
3179 
3180 		} while (i > 0);
3181 		i = 0x1000000;
3182 		do {
3183 			i--;
3184 			st = dccp_ackvector_state(dp, i);
3185 			if (st == oldstate && oldlen < 64) {
3186 				oldlen++;
3187 			} else {
3188 				buf[cnt] = (oldstate << 6) | (oldlen & 0x3f);
3189 				cnt++;
3190 				oldlen = 0;
3191 				oldstate = st;
3192 				if (cnt == bufsize) {
3193 					/* PANIC */
3194 				}
3195 			}
3196 		} while (i > dp->av_hs);
3197 	}
3198 
3199 	/* add the last one */
3200 	buf[cnt] = (oldstate << 6) | (oldlen & 0x3f);
3201 	cnt++;
3202 
3203 	return cnt;
3204 }
3205 
3206 u_char
3207 dccp_ackvector_state(struct dccpcb *dp, u_int64_t seqnr)
3208 {
3209 	u_int64_t gap, offset;
3210 	u_char *t;
3211 
3212 	/* Check for wrapping */
3213 	if (seqnr >= dp->av_hs) {
3214 		/* Not wrapped */
3215 		gap = seqnr - dp->av_hs;
3216 	} else {
3217 		/* Wrapped */
3218 		gap = seqnr + 0x1000000000000LL - dp->av_hs; /* seq nr = 48 bits */
3219 	}
3220 
3221 	if (gap >= dp->av_size) {
3222 		/* gap is bigger than ackvector size? baaad */
3223 		return 0x03;
3224 	}
3225 
3226 	offset = gap % 4 *2;
3227 	t = dp->av_hp + (gap/4);
3228 	if (t >= (dp->ackvector + (dp->av_size/4)))
3229 		t -= (dp->av_size / 4); /* wrapped */
3230 
3231 	return ((*t & (0x03 << offset)) >> offset);
3232 }
3233 
3234 /****** End of Ack Vector functions *********/
3235 
3236 /* No cc functions */
3237 void *
3238 dccp_nocc_init(struct dccpcb *pcb)
3239 {
3240   return (void*) 1;
3241 }
3242 
3243 void
3244 dccp_nocc_free(void *ccb)
3245 {
3246 }
3247 
3248 int
3249 dccp_nocc_send_packet(void *ccb, long size)
3250 {
3251   return 1;
3252 }
3253 
3254 void
3255 dccp_nocc_send_packet_sent(void *ccb, int moreToSend, long size)
3256 {
3257 }
3258 
3259 void
3260 dccp_nocc_packet_recv(void *ccb, char* options ,int optlen)
3261 {
3262 }
3263 
3264 void
3265 dccp_log(int level, const char *format, ...)
3266 {
3267 	va_list ap;
3268 
3269 	va_start(ap, format);
3270 	vprintf(format,  ap);
3271 	va_end(ap);
3272 	return;
3273 }
3274 
3275 /*
3276  * Sysctl for dccp variables.
3277  */
3278 SYSCTL_SETUP(sysctl_net_inet_dccp_setup, "sysctl net.inet.dccp subtree setup")
3279 {
3280 
3281 	sysctl_createv(clog, 0, NULL, NULL,
3282 		CTLFLAG_PERMANENT,
3283 		CTLTYPE_NODE, "net", NULL,
3284 		NULL, 0, NULL, 0,
3285 		CTL_NET, CTL_EOL);
3286 
3287 	sysctl_createv(clog, 0, NULL, NULL,
3288 		CTLFLAG_PERMANENT,
3289 		CTLTYPE_NODE, "inet", NULL,
3290 		NULL, 0, NULL, 0,
3291 		CTL_NET, PF_INET, CTL_EOL);
3292 
3293 	sysctl_createv(clog, 0, NULL, NULL,
3294 		CTLFLAG_PERMANENT,
3295 		CTLTYPE_NODE, "dccp",
3296 		SYSCTL_DESCR("DCCPv4 related settings"),
3297 		NULL, 0, NULL, 0,
3298 		CTL_NET, PF_INET, IPPROTO_DCCP, CTL_EOL);
3299 
3300 	sysctl_createv(clog, 0, NULL, NULL,
3301 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3302 		CTLTYPE_INT, "dccp_log_in_vain",
3303 		SYSCTL_DESCR("log all connection attempt"),
3304 		NULL, 0, &dccp_log_in_vain, 0,
3305 		CTL_NET, PF_INET, IPPROTO_DCCP, DCCPCTL_LOGINVAIN,
3306 		CTL_EOL);
3307 
3308 	sysctl_createv(clog, 0, NULL, NULL,
3309 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3310 		CTLTYPE_INT, "do_feature_nego",
3311 		SYSCTL_DESCR("enable feature negotiation"),
3312 		NULL, 0, &dccp_do_feature_nego, 0,
3313 		CTL_NET, PF_INET, IPPROTO_DCCP, DCCPCTL_DOFEATURENEGO,
3314 		CTL_EOL);
3315 }
3316 
3317 PR_WRAP_USRREQS(dccp)
3318 #define	dccp_attach	dccp_attach_wrapper
3319 #define	dccp_detach	dccp_detach_wrapper
3320 #define dccp_accept	dccp_accept_wrapper
3321 #define	dccp_bind	dccp_bind_wrapper
3322 #define	dccp_listen	dccp_listen_wrapper
3323 #define	dccp_connect	dccp_connect_wrapper
3324 #define	dccp_connect2	dccp_connect2_wrapper
3325 #define	dccp_disconnect	dccp_disconnect_wrapper
3326 #define	dccp_shutdown	dccp_shutdown_wrapper
3327 #define	dccp_abort	dccp_abort_wrapper
3328 #define	dccp_ioctl	dccp_ioctl_wrapper
3329 #define	dccp_stat	dccp_stat_wrapper
3330 #define	dccp_peeraddr	dccp_peeraddr_wrapper
3331 #define	dccp_sockaddr	dccp_sockaddr_wrapper
3332 #define	dccp_rcvd	dccp_rcvd_wrapper
3333 #define	dccp_recvoob	dccp_recvoob_wrapper
3334 #define	dccp_send	dccp_send_wrapper
3335 #define	dccp_sendoob	dccp_sendoob_wrapper
3336 #define	dccp_purgeif	dccp_purgeif_wrapper
3337 
3338 const struct pr_usrreqs dccp_usrreqs = {
3339 	.pr_attach	= dccp_attach,
3340 	.pr_detach	= dccp_detach,
3341 	.pr_accept	= dccp_accept,
3342 	.pr_bind	= dccp_bind,
3343 	.pr_listen	= dccp_listen,
3344 	.pr_connect	= dccp_connect,
3345 	.pr_connect2	= dccp_connect2,
3346 	.pr_disconnect	= dccp_disconnect,
3347 	.pr_shutdown	= dccp_shutdown,
3348 	.pr_abort	= dccp_abort,
3349 	.pr_ioctl	= dccp_ioctl,
3350 	.pr_stat	= dccp_stat,
3351 	.pr_peeraddr	= dccp_peeraddr,
3352 	.pr_sockaddr	= dccp_sockaddr,
3353 	.pr_rcvd	= dccp_rcvd,
3354 	.pr_recvoob	= dccp_recvoob,
3355 	.pr_send	= dccp_send,
3356 	.pr_sendoob	= dccp_sendoob,
3357 	.pr_purgeif	= dccp_purgeif,
3358 };
3359