xref: /netbsd-src/sys/netinet/sctp_usrreq.c (revision c9496f6b604074a9451a67df576a5b423068e71e)
1 /*	$KAME: sctp_usrreq.c,v 1.50 2005/06/16 20:45:29 jinmei Exp $	*/
2 /*	$NetBSD: sctp_usrreq.c,v 1.9 2017/12/10 11:52:14 rjs Exp $	*/
3 
4 /*
5  * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
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  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Cisco Systems, Inc.
19  * 4. Neither the name of the project nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: sctp_usrreq.c,v 1.9 2017/12/10 11:52:14 rjs Exp $");
37 
38 #ifdef _KERNEL_OPT
39 #include "opt_inet.h"
40 #include "opt_sctp.h"
41 #endif /* _KERNEL_OPT */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/domain.h>
49 #include <sys/proc.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/syslog.h>
55 #include <net/if.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip6.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip_var.h>
65 #include <netinet6/ip6_var.h>
66 #include <netinet6/in6_var.h>
67 #include <netinet6/scope6_var.h>
68 
69 #include <netinet/ip_icmp.h>
70 #include <netinet/icmp_var.h>
71 #include <netinet/sctp_pcb.h>
72 #include <netinet/sctp_header.h>
73 #include <netinet/sctp_var.h>
74 #include <netinet/sctp_output.h>
75 #include <netinet/sctp_uio.h>
76 #include <netinet/sctp_asconf.h>
77 #include <netinet/sctputil.h>
78 #include <netinet/sctp_indata.h>
79 #include <netinet/sctp_asconf.h>
80 #ifdef IPSEC
81 #include <netipsec/ipsec.h>
82 #include <netipsec/key.h>
83 #endif /* IPSEC */
84 
85 #include <net/net_osdep.h>
86 
87 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
88 #ifndef in6pcb
89 #define in6pcb		inpcb
90 #endif
91 #ifndef sotoin6pcb
92 #define sotoin6pcb      sotoinpcb
93 #endif
94 #endif
95 
96 #ifdef SCTP_DEBUG
97 extern u_int32_t sctp_debug_on;
98 #endif /* SCTP_DEBUG */
99 
100 /*
101  * sysctl tunable variables
102  */
103 int sctp_auto_asconf = SCTP_DEFAULT_AUTO_ASCONF;
104 int sctp_max_burst_default = SCTP_DEF_MAX_BURST;
105 int sctp_peer_chunk_oh = sizeof(struct mbuf);
106 int sctp_strict_init = 1;
107 int sctp_no_csum_on_loopback = 1;
108 unsigned int sctp_max_chunks_on_queue = SCTP_ASOC_MAX_CHUNKS_ON_QUEUE;
109 int sctp_sendspace = (128 * 1024);
110 int sctp_recvspace = 128 * (1024 +
111 #ifdef INET6
112 				sizeof(struct sockaddr_in6)
113 #else
114 				sizeof(struct sockaddr_in)
115 #endif
116 	);
117 int sctp_strict_sacks = 0;
118 int sctp_ecn = 1;
119 int sctp_ecn_nonce = 0;
120 
121 unsigned int sctp_delayed_sack_time_default = SCTP_RECV_MSEC;
122 unsigned int sctp_heartbeat_interval_default = SCTP_HB_DEFAULT_MSEC;
123 unsigned int sctp_pmtu_raise_time_default = SCTP_DEF_PMTU_RAISE_SEC;
124 unsigned int sctp_shutdown_guard_time_default = SCTP_DEF_MAX_SHUTDOWN_SEC;
125 unsigned int sctp_secret_lifetime_default = SCTP_DEFAULT_SECRET_LIFE_SEC;
126 unsigned int sctp_rto_max_default = SCTP_RTO_UPPER_BOUND;
127 unsigned int sctp_rto_min_default = SCTP_RTO_LOWER_BOUND;
128 unsigned int sctp_rto_initial_default = SCTP_RTO_INITIAL;
129 unsigned int sctp_init_rto_max_default = SCTP_RTO_UPPER_BOUND;
130 unsigned int sctp_valid_cookie_life_default = SCTP_DEFAULT_COOKIE_LIFE;
131 unsigned int sctp_init_rtx_max_default = SCTP_DEF_MAX_INIT;
132 unsigned int sctp_assoc_rtx_max_default = SCTP_DEF_MAX_SEND;
133 unsigned int sctp_path_rtx_max_default = SCTP_DEF_MAX_SEND/2;
134 unsigned int sctp_nr_outgoing_streams_default = SCTP_OSTREAM_INITIAL;
135 
136 void
137 sctp_init(void)
138 {
139 	/* Init the SCTP pcb in sctp_pcb.c */
140 	u_long sb_max_adj;
141 
142 	sctp_pcb_init();
143 
144 	if (nmbclusters > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
145 		sctp_max_chunks_on_queue = nmbclusters;
146 	/*
147 	 * Allow a user to take no more than 1/2 the number of clusters
148 	 * or the SB_MAX whichever is smaller for the send window.
149 	 */
150 	sb_max_adj = (u_long)((u_quad_t)(SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
151 	sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
152 			     ((nmbclusters/2) * SCTP_DEFAULT_MAXSEGMENT));
153 	/*
154 	 * Now for the recv window, should we take the same amount?
155 	 * or should I do 1/2 the SB_MAX instead in the SB_MAX min above.
156 	 * For now I will just copy.
157 	 */
158 	sctp_recvspace = sctp_sendspace;
159 }
160 
161 #ifdef INET6
162 void
163 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
164 {
165 	memset(ip6, 0, sizeof(*ip6));
166 
167 	ip6->ip6_vfc = IPV6_VERSION;
168 	ip6->ip6_plen = ip->ip_len;
169 	ip6->ip6_nxt = ip->ip_p;
170 	ip6->ip6_hlim = ip->ip_ttl;
171 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
172 		IPV6_ADDR_INT32_SMP;
173 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
174 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
175 }
176 #endif /* INET6 */
177 
178 static void
179 sctp_split_chunks(struct sctp_association *asoc,
180 		  struct sctp_stream_out *strm,
181 		  struct sctp_tmit_chunk *chk)
182 {
183 	struct sctp_tmit_chunk *new_chk;
184 
185 	/* First we need a chunk */
186 	new_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
187 	if (new_chk == NULL) {
188 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
189 		return;
190 	}
191 	sctppcbinfo.ipi_count_chunk++;
192 	sctppcbinfo.ipi_gencnt_chunk++;
193 	/* Copy it all */
194 	*new_chk = *chk;
195 	/*  split the data */
196 	new_chk->data = m_split(chk->data, (chk->send_size>>1), M_DONTWAIT);
197 	if (new_chk->data == NULL) {
198 		/* Can't split */
199 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
200 		SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, new_chk);
201 		sctppcbinfo.ipi_count_chunk--;
202 		if ((int)sctppcbinfo.ipi_count_chunk < 0) {
203 			panic("Chunk count is negative");
204 		}
205 		sctppcbinfo.ipi_gencnt_chunk++;
206 		return;
207 
208 	}
209 	/* Data is now split adjust sizes */
210 	chk->send_size >>= 1;
211 	new_chk->send_size >>= 1;
212 
213 	chk->book_size >>= 1;
214 	new_chk->book_size >>= 1;
215 
216 	/* now adjust the marks */
217 	chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
218 	chk->rec.data.rcv_flags &= ~SCTP_DATA_LAST_FRAG;
219 
220 	new_chk->rec.data.rcv_flags &= ~SCTP_DATA_FIRST_FRAG;
221 	new_chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
222 
223 	/* Increase ref count if dest is set */
224 	if (chk->whoTo) {
225 		new_chk->whoTo->ref_count++;
226 	}
227 	/* now drop it on the end of the list*/
228 	asoc->stream_queue_cnt++;
229 	TAILQ_INSERT_AFTER(&strm->outqueue, chk, new_chk, sctp_next);
230 }
231 
232 static void
233 sctp_notify_mbuf(struct sctp_inpcb *inp,
234 		 struct sctp_tcb *stcb,
235 		 struct sctp_nets *net,
236 		 struct ip *ip,
237 		 struct sctphdr *sh)
238 
239 {
240 	struct icmp *icmph;
241 	int totsz;
242 	uint16_t nxtsz;
243 
244 	/* protection */
245 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
246 	    (ip == NULL) || (sh == NULL)) {
247 		if (stcb != NULL) {
248 			SCTP_TCB_UNLOCK(stcb);
249 		}
250 		return;
251 	}
252 	/* First job is to verify the vtag matches what I would send */
253 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
254 		SCTP_TCB_UNLOCK(stcb);
255 		return;
256 	}
257 	icmph = (struct icmp *)((vaddr_t)ip - (sizeof(struct icmp) -
258 					       sizeof(struct ip)));
259 	if (icmph->icmp_type != ICMP_UNREACH) {
260 		/* We only care about unreachable */
261 		SCTP_TCB_UNLOCK(stcb);
262 		return;
263 	}
264 	if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
265 		/* not a unreachable message due to frag. */
266 		SCTP_TCB_UNLOCK(stcb);
267 		return;
268 	}
269 	totsz = ip->ip_len;
270 	nxtsz = ntohs(icmph->icmp_seq);
271 	if (nxtsz == 0) {
272 		/*
273 		 * old type router that does not tell us what the next size
274 		 * mtu is. Rats we will have to guess (in a educated fashion
275 		 * of course)
276 		 */
277 		nxtsz = find_next_best_mtu(totsz);
278 	}
279 
280 	/* Stop any PMTU timer */
281 	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
282 
283 	/* Adjust destination size limit */
284 	if (net->mtu > nxtsz) {
285 		net->mtu = nxtsz;
286 	}
287 	/* now what about the ep? */
288 	if (stcb->asoc.smallest_mtu > nxtsz) {
289 		struct sctp_tmit_chunk *chk, *nchk;
290 		struct sctp_stream_out *strm;
291 		/* Adjust that too */
292 		stcb->asoc.smallest_mtu = nxtsz;
293 		/* now off to subtract IP_DF flag if needed */
294 
295 		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
296 			if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
297 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
298 			}
299 		}
300 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
301 			if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
302 				/*
303 				 * For this guy we also mark for immediate
304 				 * resend since we sent to big of chunk
305 				 */
306 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
307 				if (chk->sent != SCTP_DATAGRAM_RESEND) {
308 					stcb->asoc.sent_queue_retran_cnt++;
309 				}
310 				chk->sent = SCTP_DATAGRAM_RESEND;
311 				chk->rec.data.doing_fast_retransmit = 0;
312 
313 				/* Clear any time so NO RTT is being done */
314 				chk->do_rtt = 0;
315 				sctp_total_flight_decrease(stcb, chk);
316 				if (net->flight_size >= chk->book_size) {
317 					net->flight_size -= chk->book_size;
318 				} else {
319 					net->flight_size = 0;
320 				}
321 			}
322 		}
323 		TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
324 			chk = TAILQ_FIRST(&strm->outqueue);
325 			while (chk) {
326 				nchk = TAILQ_NEXT(chk, sctp_next);
327 				if ((chk->send_size+SCTP_MED_OVERHEAD) > nxtsz) {
328 					sctp_split_chunks(&stcb->asoc, strm, chk);
329 				}
330 				chk = nchk;
331 			}
332 		}
333 	}
334 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
335 	SCTP_TCB_UNLOCK(stcb);
336 }
337 
338 
339 void
340 sctp_notify(struct sctp_inpcb *inp,
341 	    int errno,
342 	    struct sctphdr *sh,
343 	    struct sockaddr *to,
344 	    struct sctp_tcb *stcb,
345 	    struct sctp_nets *net)
346 {
347 	/* protection */
348 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
349 	    (sh == NULL) || (to == NULL)) {
350 #ifdef SCTP_DEBUG
351 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
352 			printf("sctp-notify, bad call\n");
353 		}
354 #endif /* SCTP_DEBUG */
355 		return;
356 	}
357 	/* First job is to verify the vtag matches what I would send */
358 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
359 		return;
360 	}
361 
362 /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
363 
364 	if ((errno == EHOSTUNREACH) ||  /* Host is not reachable */
365 	    (errno == EHOSTDOWN) ||	/* Host is down */
366 	    (errno == ECONNREFUSED) ||	/* Host refused the connection, (not an abort?) */
367 	    (errno == ENOPROTOOPT)	/* SCTP is not present on host */
368 		) {
369 		/*
370 		 * Hmm reachablity problems we must examine closely.
371 		 * If its not reachable, we may have lost a network.
372 		 * Or if there is NO protocol at the other end named SCTP.
373 		 * well we consider it a OOTB abort.
374 		 */
375 		if ((errno == EHOSTUNREACH) || (errno == EHOSTDOWN)) {
376 			if (net->dest_state & SCTP_ADDR_REACHABLE) {
377 				/* Ok that destination is NOT reachable */
378 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
379 				net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
380 				net->error_count = net->failure_threshold + 1;
381 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
382 						stcb, SCTP_FAILED_THRESHOLD,
383 						(void *)net);
384 			}
385 			if (stcb) {
386 				SCTP_TCB_UNLOCK(stcb);
387 			}
388 		} else {
389 			/*
390 			 * Here the peer is either playing tricks on us,
391 			 * including an address that belongs to someone who
392 			 * does not support SCTP OR was a userland
393 			 * implementation that shutdown and now is dead. In
394 			 * either case treat it like a OOTB abort with no TCB
395 			 */
396 			sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
397 			sctp_free_assoc(inp, stcb);
398 			/* no need to unlock here, since the TCB is gone */
399 		}
400 	} else {
401 		/* Send all others to the app */
402 		if (inp->sctp_socket) {
403 			inp->sctp_socket->so_error = errno;
404 			sctp_sowwakeup(inp, inp->sctp_socket);
405 		}
406 	        if (stcb) {
407 			SCTP_TCB_UNLOCK(stcb);
408 		}
409 	}
410 }
411 
412 void *
413 sctp_ctlinput(int cmd, const struct sockaddr *sa, void *vip)
414 {
415 	struct ip *ip = vip;
416 	struct sctphdr *sh;
417 	int s;
418 
419 	if (sa->sa_family != AF_INET ||
420 	    ((const struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
421 		return (NULL);
422 	}
423 
424 	if (PRC_IS_REDIRECT(cmd)) {
425 		ip = 0;
426 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
427 		return (NULL);
428 	}
429 	if (ip) {
430 		struct sctp_inpcb *inp;
431 		struct sctp_tcb *stcb;
432 		struct sctp_nets *net;
433 		struct sockaddr_in to, from;
434 
435 		sh = (struct sctphdr *)((vaddr_t)ip + (ip->ip_hl << 2));
436 		memset(&to, 0, sizeof(to));
437 		memset(&from, 0, sizeof(from));
438 		from.sin_family = to.sin_family = AF_INET;
439 		from.sin_len = to.sin_len = sizeof(to);
440 		from.sin_port = sh->src_port;
441 		from.sin_addr = ip->ip_src;
442 		to.sin_port = sh->dest_port;
443 		to.sin_addr = ip->ip_dst;
444 
445 		/*
446 		 * 'to' holds the dest of the packet that failed to be sent.
447 		 * 'from' holds our local endpoint address.
448 		 * Thus we reverse the to and the from in the lookup.
449 		 */
450 		s = splsoftnet();
451 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
452 						    (struct sockaddr *)&to,
453 						    &inp, &net, 1);
454 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
455 			if (cmd != PRC_MSGSIZE) {
456 				int cm;
457 				if (cmd == PRC_HOSTDEAD) {
458 					cm = EHOSTUNREACH;
459 				} else {
460 					cm = inetctlerrmap[cmd];
461 				}
462 				sctp_notify(inp, cm, sh,
463 					    (struct sockaddr *)&to, stcb,
464 					    net);
465 			} else {
466 				/* handle possible ICMP size messages */
467 				sctp_notify_mbuf(inp, stcb, net, ip, sh);
468 			}
469 		} else {
470 #if defined(__FreeBSD__) && __FreeBSD_version < 500000
471                         /* XXX must be fixed for 5.x and higher, leave for 4.x */
472 			if (PRC_IS_REDIRECT(cmd) && inp) {
473 				in_rtchange((struct inpcb *)inp,
474 					    inetctlerrmap[cmd]);
475 			}
476 #endif
477 			if ((stcb == NULL) && (inp != NULL)) {
478 				/* reduce ref-count */
479 				SCTP_INP_WLOCK(inp);
480 				SCTP_INP_DECR_REF(inp);
481 				SCTP_INP_WUNLOCK(inp);
482 			}
483 
484 		}
485 		splx(s);
486 	}
487 	return (NULL);
488 }
489 
490 static int
491 sctp_abort(struct socket *so)
492 {
493 	struct sctp_inpcb *inp;
494 
495 	inp = (struct sctp_inpcb *)so->so_pcb;
496 	if (inp == 0)
497 		return EINVAL;	/* ??? possible? panic instead? */
498 
499 	sctp_inpcb_free(inp, 1);
500 	return 0;
501 }
502 
503 static int
504 sctp_attach(struct socket *so, int proto)
505 {
506 	struct sctp_inpcb *inp;
507 #ifdef IPSEC
508 	struct inpcb *ip_inp;
509 #endif
510 	int error;
511 
512 	sosetlock(so);
513 	inp = (struct sctp_inpcb *)so->so_pcb;
514 	if (inp != 0) {
515 		return EINVAL;
516 	}
517 	error = soreserve(so, sctp_sendspace, sctp_recvspace);
518 	if (error) {
519 		return error;
520 	}
521 	error = sctp_inpcb_alloc(so);
522 	if (error) {
523 		return error;
524 	}
525 	inp = (struct sctp_inpcb *)so->so_pcb;
526 	SCTP_INP_WLOCK(inp);
527 
528 	inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6;	/* I'm not v6! */
529 #ifdef IPSEC
530 	ip_inp = &inp->ip_inp.inp;
531 	ip_inp->inp_af = proto;
532 #endif
533 	inp->inp_vflag |= INP_IPV4;
534 	inp->inp_ip_ttl = ip_defttl;
535 
536 #ifdef IPSEC
537 	error = ipsec_init_pcbpolicy(so, &ip_inp->inp_sp);
538 	if (error != 0) {
539 		sctp_inpcb_free(inp, 1);
540 		return error;
541 	}
542 #endif /*IPSEC*/
543 	SCTP_INP_WUNLOCK(inp);
544 	so->so_send = sctp_sosend;
545 	return 0;
546 }
547 
548 static int
549 sctp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
550 {
551 	struct sctp_inpcb *inp;
552 	int error;
553 
554 	KASSERT(solocked(so));
555 
556 #ifdef INET6
557 	if (nam && nam->sa_family != AF_INET)
558 		/* must be a v4 address! */
559 		return EINVAL;
560 #endif /* INET6 */
561 
562 	inp = (struct sctp_inpcb *)so->so_pcb;
563 	if (inp == 0)
564 		return EINVAL;
565 
566 	error = sctp_inpcb_bind(so, nam, l);
567 	return error;
568 }
569 
570 
571 static int
572 sctp_detach(struct socket *so)
573 {
574 	struct sctp_inpcb *inp;
575 
576 	inp = (struct sctp_inpcb *)so->so_pcb;
577 	if (inp == 0)
578 		return EINVAL;
579 
580 	if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
581 	    (so->so_rcv.sb_cc > 0)) {
582 		sctp_inpcb_free(inp, 1);
583 	} else {
584 		sctp_inpcb_free(inp, 0);
585 	}
586 	return 0;
587 }
588 
589 static int
590 sctp_recvoob(struct socket *so, struct mbuf *m, int flags)
591 {
592 	KASSERT(solocked(so));
593 
594 	return EOPNOTSUPP;
595 }
596 
597 int
598 sctp_send(struct socket *so, struct mbuf *m, struct sockaddr *addr,
599 	  struct mbuf *control, struct lwp *l)
600 {
601 	struct sctp_inpcb *inp;
602 	int error;
603 	inp = (struct sctp_inpcb *)so->so_pcb;
604 	if (inp == 0) {
605 		if (control) {
606 			sctp_m_freem(control);
607 			control = NULL;
608 		}
609 		sctp_m_freem(m);
610 		return EINVAL;
611 	}
612 	/* Got to have an to address if we are NOT a connected socket */
613 	if ((addr == NULL) &&
614 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
615 	     (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
616 		) {
617 		goto connected_type;
618 	} else if (addr == NULL) {
619 		error = EDESTADDRREQ;
620 		sctp_m_freem(m);
621 		if (control) {
622 			sctp_m_freem(control);
623 			control = NULL;
624 		}
625 		return (error);
626 	}
627 #ifdef INET6
628 	if (addr->sa_family != AF_INET) {
629 		/* must be a v4 address! */
630 		sctp_m_freem(m);
631 		if (control) {
632 			sctp_m_freem(control);
633 			control = NULL;
634 		}
635 		error = EDESTADDRREQ;
636 		return EINVAL;
637 	}
638 #endif /* INET6 */
639  connected_type:
640 	/* now what about control */
641 	if (control) {
642 		if (inp->control) {
643 			printf("huh? control set?\n");
644 			sctp_m_freem(inp->control);
645 			inp->control = NULL;
646 		}
647 		inp->control = control;
648 	}
649 	/* add it in possibly */
650 	if ((inp->pkt) && (inp->pkt->m_flags & M_PKTHDR)) {
651 		struct mbuf *x;
652 		int c_len;
653 
654 		c_len = 0;
655 		/* How big is it */
656 		for (x=m;x;x = x->m_next) {
657 			c_len += x->m_len;
658 		}
659 		inp->pkt->m_pkthdr.len += c_len;
660 	}
661 	/* Place the data */
662 	if (inp->pkt) {
663 		inp->pkt_last->m_next = m;
664 		inp->pkt_last = m;
665 	} else {
666 		inp->pkt_last = inp->pkt = m;
667 	}
668 	if ((so->so_state & SS_MORETOCOME) == 0) {
669 		/*
670 		 * note with the current version this code will only be used
671 		 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
672 		 * re-defining sosend to use the sctp_sosend. One can
673 		 * optionally switch back to this code (by changing back the
674 		 * definitions) but this is not advisable.
675 	     */
676 		int ret;
677 		ret = sctp_output(inp, inp->pkt, addr, inp->control, l, 0);
678 		inp->pkt = NULL;
679 		inp->control = NULL;
680 		return (ret);
681 	} else {
682 		return (0);
683 	}
684 }
685 
686 static int
687 sctp_disconnect(struct socket *so)
688 {
689 	struct sctp_inpcb *inp;
690 	int s;
691 
692 	inp = (struct sctp_inpcb *)so->so_pcb;
693 	if (inp == NULL) {
694 		return (ENOTCONN);
695 	}
696 	s = splsoftnet();
697 	SCTP_INP_RLOCK(inp);
698 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
699 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
700 			/* No connection */
701 			SCTP_INP_RUNLOCK(inp);
702 			splx(s);
703 			return (0);
704 		} else {
705 			int some_on_streamwheel = 0;
706 			struct sctp_association *asoc;
707 			struct sctp_tcb *stcb;
708 
709 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
710 			if (stcb == NULL) {
711 				SCTP_INP_RUNLOCK(inp);
712 				splx(s);
713 				return (EINVAL);
714 			}
715 			asoc = &stcb->asoc;
716 			SCTP_TCB_LOCK(stcb);
717 			if (((so->so_options & SO_LINGER) &&
718 			     (so->so_linger == 0)) ||
719 			    (so->so_rcv.sb_cc > 0)) {
720 				if (SCTP_GET_STATE(asoc) !=
721 				    SCTP_STATE_COOKIE_WAIT) {
722 					/* Left with Data unread */
723 					struct mbuf *err;
724 					err = NULL;
725 					MGET(err, M_DONTWAIT, MT_DATA);
726 					if (err) {
727 						/* Fill in the user initiated abort */
728 						struct sctp_paramhdr *ph;
729 						ph = mtod(err, struct sctp_paramhdr *);
730 						err->m_len = sizeof(struct sctp_paramhdr);
731 						ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
732 						ph->param_length = htons(err->m_len);
733 					}
734 					sctp_send_abort_tcb(stcb, err);
735 				}
736 				SCTP_INP_RUNLOCK(inp);
737 				sctp_free_assoc(inp, stcb);
738 				/* No unlock tcb assoc is gone */
739 				splx(s);
740 				return (0);
741 			}
742 			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
743 				/* Check to see if some data queued */
744 				struct sctp_stream_out *outs;
745 				TAILQ_FOREACH(outs, &asoc->out_wheel,
746 					      next_spoke) {
747 					if (!TAILQ_EMPTY(&outs->outqueue)) {
748 						some_on_streamwheel = 1;
749 						break;
750 					}
751 				}
752 			}
753 
754 			if (TAILQ_EMPTY(&asoc->send_queue) &&
755 			    TAILQ_EMPTY(&asoc->sent_queue) &&
756 			    (some_on_streamwheel == 0)) {
757 				/* there is nothing queued to send, so done */
758 				if ((SCTP_GET_STATE(asoc) !=
759 				     SCTP_STATE_SHUTDOWN_SENT) &&
760 				    (SCTP_GET_STATE(asoc) !=
761 				     SCTP_STATE_SHUTDOWN_ACK_SENT)) {
762 					/* only send SHUTDOWN 1st time thru */
763 #ifdef SCTP_DEBUG
764 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
765 						printf("%s:%d sends a shutdown\n",
766 						       __FILE__,
767 						       __LINE__
768 							);
769 					}
770 #endif
771 					sctp_send_shutdown(stcb,
772 							   stcb->asoc.primary_destination);
773 					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
774 					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
775 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
776 							 stcb->sctp_ep, stcb,
777 							 asoc->primary_destination);
778 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
779 							 stcb->sctp_ep, stcb,
780 							 asoc->primary_destination);
781 				}
782 			} else {
783 				/*
784 				 * we still got (or just got) data to send,
785 				 * so set SHUTDOWN_PENDING
786 				 */
787 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
788 			}
789 			SCTP_TCB_UNLOCK(stcb);
790 			SCTP_INP_RUNLOCK(inp);
791 			splx(s);
792 			return (0);
793 		}
794 		/* not reached */
795 	} else {
796 		/* UDP model does not support this */
797 		SCTP_INP_RUNLOCK(inp);
798 		splx(s);
799 		return EOPNOTSUPP;
800 	}
801 }
802 
803 int
804 sctp_shutdown(struct socket *so)
805 {
806 	struct sctp_inpcb *inp;
807 
808 	inp = (struct sctp_inpcb *)so->so_pcb;
809 	if (inp == 0) {
810 		return EINVAL;
811 	}
812 	SCTP_INP_RLOCK(inp);
813 	/* For UDP model this is a invalid call */
814 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
815 		/* Restore the flags that the soshutdown took away. */
816 		so->so_state &= ~SS_CANTRCVMORE;
817 		/* This proc will wakeup for read and do nothing (I hope) */
818 		SCTP_INP_RUNLOCK(inp);
819 		return (EOPNOTSUPP);
820 	}
821 	/*
822 	 * Ok if we reach here its the TCP model and it is either a SHUT_WR
823 	 * or SHUT_RDWR. This means we put the shutdown flag against it.
824 	 */
825 	{
826 		int some_on_streamwheel = 0;
827 		struct sctp_tcb *stcb;
828 		struct sctp_association *asoc;
829 		socantsendmore(so);
830 
831 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
832 		if (stcb == NULL) {
833 			/*
834 			 * Ok we hit the case that the shutdown call was made
835 			 * after an abort or something. Nothing to do now.
836 			 */
837 			return (0);
838 		}
839 		SCTP_TCB_LOCK(stcb);
840 		asoc = &stcb->asoc;
841 
842 		if (!TAILQ_EMPTY(&asoc->out_wheel)) {
843 			/* Check to see if some data queued */
844 			struct sctp_stream_out *outs;
845 			TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
846 				if (!TAILQ_EMPTY(&outs->outqueue)) {
847 					some_on_streamwheel = 1;
848 					break;
849 				}
850 			}
851 		}
852 		if (TAILQ_EMPTY(&asoc->send_queue) &&
853 		    TAILQ_EMPTY(&asoc->sent_queue) &&
854 		    (some_on_streamwheel == 0)) {
855 			/* there is nothing queued to send, so I'm done... */
856 			if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
857 				/* only send SHUTDOWN the first time through */
858 #ifdef SCTP_DEBUG
859 				if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
860 					printf("%s:%d sends a shutdown\n",
861 					       __FILE__,
862 					       __LINE__
863 						);
864 				}
865 #endif
866 				sctp_send_shutdown(stcb,
867 						   stcb->asoc.primary_destination);
868 				sctp_chunk_output(stcb->sctp_ep, stcb, 1);
869 				asoc->state = SCTP_STATE_SHUTDOWN_SENT;
870 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
871 						 stcb->sctp_ep, stcb,
872 						 asoc->primary_destination);
873 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
874 						 stcb->sctp_ep, stcb,
875 						 asoc->primary_destination);
876 			}
877 		} else {
878 			/*
879 			 * we still got (or just got) data to send, so
880 			 * set SHUTDOWN_PENDING
881 			 */
882 			asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
883 		}
884 		SCTP_TCB_UNLOCK(stcb);
885 	}
886 	SCTP_INP_RUNLOCK(inp);
887 	return 0;
888 }
889 
890 /*
891  * copies a "user" presentable address and removes embedded scope, etc.
892  * returns 0 on success, 1 on error
893  */
894 static uint32_t
895 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
896 {
897 	struct sockaddr_in6 lsa6;
898 
899 	sctp_recover_scope((struct sockaddr_in6 *)sa, &lsa6);
900 	memcpy(ss, sa, sa->sa_len);
901 	return (0);
902 }
903 
904 
905 static int
906 sctp_fill_up_addresses(struct sctp_inpcb *inp,
907 		       struct sctp_tcb *stcb,
908 		       int limit,
909 		       struct sockaddr_storage *sas)
910 {
911 	struct ifnet *ifn;
912 	struct ifaddr *ifa;
913 	int loopback_scope, ipv4_local_scope, local_scope, site_scope, actual;
914 	int ipv4_addr_legal, ipv6_addr_legal;
915 	actual = 0;
916 	if (limit <= 0)
917 		return (actual);
918 
919 	if (stcb) {
920 		/* Turn on all the appropriate scope */
921 		loopback_scope = stcb->asoc.loopback_scope;
922 		ipv4_local_scope = stcb->asoc.ipv4_local_scope;
923 		local_scope = stcb->asoc.local_scope;
924 		site_scope = stcb->asoc.site_scope;
925 	} else {
926 		/* Turn on ALL scope, since we look at the EP */
927 		loopback_scope = ipv4_local_scope = local_scope =
928 			site_scope = 1;
929 	}
930 	ipv4_addr_legal = ipv6_addr_legal = 0;
931 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
932 		ipv6_addr_legal = 1;
933 		if (
934 #if defined(__OpenBSD__)
935 		(0) /* we always do dual bind */
936 #elif defined (__NetBSD__)
937 		(((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
938 #else
939 		(((struct in6pcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
940 #endif
941 		== 0) {
942 			ipv4_addr_legal = 1;
943 		}
944 	} else {
945 		ipv4_addr_legal = 1;
946 	}
947 
948 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
949 		int s = pserialize_read_enter();
950 		IFNET_READER_FOREACH(ifn) {
951 			if ((loopback_scope == 0) &&
952 			    (ifn->if_type == IFT_LOOP)) {
953 				/* Skip loopback if loopback_scope not set */
954 				continue;
955 			}
956 			IFADDR_READER_FOREACH(ifa, ifn) {
957 				if (stcb) {
958 				/*
959 				 * For the BOUND-ALL case, the list
960 				 * associated with a TCB is Always
961 				 * considered a reverse list.. i.e.
962 				 * it lists addresses that are NOT
963 				 * part of the association. If this
964 				 * is one of those we must skip it.
965 				 */
966 					if (sctp_is_addr_restricted(stcb,
967 								    ifa->ifa_addr)) {
968 						continue;
969 					}
970 				}
971 				if ((ifa->ifa_addr->sa_family == AF_INET) &&
972 				    (ipv4_addr_legal)) {
973 					struct sockaddr_in *sin;
974 					sin = (struct sockaddr_in *)ifa->ifa_addr;
975 					if (sin->sin_addr.s_addr == 0) {
976 						/* we skip unspecifed addresses */
977 						continue;
978 					}
979 					if ((ipv4_local_scope == 0) &&
980 					    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
981 						continue;
982 					}
983 					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
984 						in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
985 						((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
986 						sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(struct sockaddr_in6));
987 						actual += sizeof(struct sockaddr_in6);
988 					} else {
989 						memcpy(sas, sin, sizeof(*sin));
990 						((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
991 						sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(*sin));
992 						actual += sizeof(*sin);
993 					}
994 					if (actual >= limit) {
995 						pserialize_read_exit(s);
996 						return (actual);
997 					}
998 				} else if ((ifa->ifa_addr->sa_family == AF_INET6) &&
999 					   (ipv6_addr_legal)) {
1000 					struct sockaddr_in6 *sin6;
1001 					sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1002 					if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1003 						/*
1004 						 * we skip unspecified
1005 						 * addresses
1006 						 */
1007 						continue;
1008 					}
1009 					if ((site_scope == 0) &&
1010 					    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1011 						continue;
1012 					}
1013 					memcpy(sas, sin6, sizeof(*sin6));
1014 					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1015 					sas = (struct sockaddr_storage *)((vaddr_t)sas + sizeof(*sin6));
1016 					actual += sizeof(*sin6);
1017 					if (actual >= limit) {
1018 						pserialize_read_exit(s);
1019 						return (actual);
1020 					}
1021 				}
1022 			}
1023 		}
1024 		pserialize_read_exit(s);
1025 	} else {
1026 		struct sctp_laddr *laddr;
1027 		/*
1028 		 * If we have a TCB and we do NOT support ASCONF (it's
1029 		 * turned off or otherwise) then the list is always the
1030 		 * true list of addresses (the else case below).  Otherwise
1031 		 * the list on the association is a list of addresses that
1032 		 * are NOT part of the association.
1033 		 */
1034 		if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1035 			/* The list is a NEGATIVE list */
1036 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1037 				if (stcb) {
1038 					if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr)) {
1039 						continue;
1040 					}
1041 				}
1042 				if (sctp_fill_user_address(sas, laddr->ifa->ifa_addr))
1043 					continue;
1044 
1045 				((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1046 				sas = (struct sockaddr_storage *)((vaddr_t)sas +
1047 								  laddr->ifa->ifa_addr->sa_len);
1048 				actual += laddr->ifa->ifa_addr->sa_len;
1049 				if (actual >= limit) {
1050 					return (actual);
1051 				}
1052 			}
1053 		} else {
1054 			/* The list is a positive list if present */
1055 			if (stcb) {
1056 				/* Must use the specific association list */
1057 				LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1058 					     sctp_nxt_addr) {
1059 					if (sctp_fill_user_address(sas,
1060 								   laddr->ifa->ifa_addr))
1061 						continue;
1062 					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1063 					sas = (struct sockaddr_storage *)((vaddr_t)sas +
1064 									  laddr->ifa->ifa_addr->sa_len);
1065 					actual += laddr->ifa->ifa_addr->sa_len;
1066 					if (actual >= limit) {
1067 						return (actual);
1068 					}
1069 				}
1070 			} else {
1071 				/* No endpoint so use the endpoints individual list */
1072 				LIST_FOREACH(laddr, &inp->sctp_addr_list,
1073 					     sctp_nxt_addr) {
1074 					if (sctp_fill_user_address(sas,
1075 								   laddr->ifa->ifa_addr))
1076 						continue;
1077 					((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1078 					sas = (struct sockaddr_storage *)((vaddr_t)sas +
1079 									  laddr->ifa->ifa_addr->sa_len);
1080 					actual += laddr->ifa->ifa_addr->sa_len;
1081 					if (actual >= limit) {
1082 						return (actual);
1083 					}
1084 				}
1085 			}
1086 		}
1087 	}
1088 	return (actual);
1089 }
1090 
1091 static int
1092 sctp_count_max_addresses(struct sctp_inpcb *inp)
1093 {
1094 	int cnt = 0;
1095 	/*
1096 	 * In both sub-set bound an bound_all cases we return the MAXIMUM
1097 	 * number of addresses that you COULD get. In reality the sub-set
1098 	 * bound may have an exclusion list for a given TCB OR in the
1099 	 * bound-all case a TCB may NOT include the loopback or other
1100 	 * addresses as well.
1101 	 */
1102 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1103 		struct ifnet *ifn;
1104 		struct ifaddr *ifa;
1105 		int s;
1106 
1107 		s = pserialize_read_enter();
1108 		IFNET_READER_FOREACH(ifn) {
1109 			IFADDR_READER_FOREACH(ifa, ifn) {
1110 				/* Count them if they are the right type */
1111 				if (ifa->ifa_addr->sa_family == AF_INET) {
1112 					if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1113 						cnt += sizeof(struct sockaddr_in6);
1114 					else
1115 						cnt += sizeof(struct sockaddr_in);
1116 
1117 				} else if (ifa->ifa_addr->sa_family == AF_INET6)
1118 					cnt += sizeof(struct sockaddr_in6);
1119 			}
1120 		}
1121 		pserialize_read_exit(s);
1122 	} else {
1123 		struct sctp_laddr *laddr;
1124 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1125 			if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
1126 				if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1127 					cnt += sizeof(struct sockaddr_in6);
1128 				else
1129 					cnt += sizeof(struct sockaddr_in);
1130 
1131 			} else if (laddr->ifa->ifa_addr->sa_family == AF_INET6)
1132 				cnt += sizeof(struct sockaddr_in6);
1133 		}
1134 	}
1135 	return (cnt);
1136 }
1137 
1138 static int
1139 sctp_do_connect_x(struct socket *so, struct sctp_inpcb *inp, struct mbuf *m,
1140 		  struct lwp *l, int delay)
1141 {
1142         int error = 0;
1143 	struct sctp_tcb *stcb = NULL;
1144 	struct sockaddr *sa;
1145 	int num_v6=0, num_v4=0, *totaddrp, totaddr, i, incr, at;
1146 #ifdef SCTP_DEBUG
1147 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1148 		printf("Connectx called\n");
1149 	}
1150 #endif /* SCTP_DEBUG */
1151 
1152 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1153 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1154 		/* We are already connected AND the TCP model */
1155 		return (EADDRINUSE);
1156 	}
1157 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1158 		SCTP_INP_RLOCK(inp);
1159 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1160 		SCTP_INP_RUNLOCK(inp);
1161 	}
1162 	if (stcb) {
1163 		return (EALREADY);
1164 
1165 	}
1166 	SCTP_ASOC_CREATE_LOCK(inp);
1167 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1168 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1169 		SCTP_ASOC_CREATE_UNLOCK(inp);
1170 		return (EFAULT);
1171 	}
1172 
1173 	totaddrp = mtod(m, int *);
1174 	totaddr = *totaddrp;
1175 	sa = (struct sockaddr *)(totaddrp + 1);
1176 	at = incr = 0;
1177 	/* account and validate addresses */
1178 	SCTP_INP_WLOCK(inp);
1179 	SCTP_INP_INCR_REF(inp);
1180 	SCTP_INP_WUNLOCK(inp);
1181 	for (i = 0; i < totaddr; i++) {
1182 		if (sa->sa_family == AF_INET) {
1183 			num_v4++;
1184 			incr = sizeof(struct sockaddr_in);
1185 		} else if (sa->sa_family == AF_INET6) {
1186 			struct sockaddr_in6 *sin6;
1187 			sin6 = (struct sockaddr_in6 *)sa;
1188 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1189 				/* Must be non-mapped for connectx */
1190 				SCTP_ASOC_CREATE_UNLOCK(inp);
1191 				return EINVAL;
1192 			}
1193 			num_v6++;
1194 			incr = sizeof(struct sockaddr_in6);
1195 		} else {
1196 			totaddr = i;
1197 			break;
1198 		}
1199 		stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
1200 		if (stcb != NULL) {
1201 			/* Already have or am bring up an association */
1202 			SCTP_ASOC_CREATE_UNLOCK(inp);
1203 			SCTP_TCB_UNLOCK(stcb);
1204 			return (EALREADY);
1205 		}
1206 		if ((at + incr) > m->m_len) {
1207 			totaddr = i;
1208 			break;
1209 		}
1210 		sa = (struct sockaddr *)((vaddr_t)sa + incr);
1211 	}
1212 	sa = (struct sockaddr *)(totaddrp + 1);
1213 	SCTP_INP_WLOCK(inp);
1214 	SCTP_INP_DECR_REF(inp);
1215 	SCTP_INP_WUNLOCK(inp);
1216 #ifdef INET6
1217 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1218 	    (num_v6 > 0)) {
1219 		SCTP_INP_WUNLOCK(inp);
1220 		SCTP_ASOC_CREATE_UNLOCK(inp);
1221 		return (EINVAL);
1222 	}
1223 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1224 	    (num_v4 > 0)) {
1225 		struct in6pcb *inp6;
1226 		inp6 = (struct in6pcb *)inp;
1227 		if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
1228 			/*
1229 			 * if IPV6_V6ONLY flag, ignore connections
1230 			 * destined to a v4 addr or v4-mapped addr
1231 			 */
1232 			SCTP_INP_WUNLOCK(inp);
1233 			SCTP_ASOC_CREATE_UNLOCK(inp);
1234 			return EINVAL;
1235 		}
1236 	}
1237 #endif /* INET6 */
1238 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1239 	    SCTP_PCB_FLAGS_UNBOUND) {
1240 		/* Bind a ephemeral port */
1241 		SCTP_INP_WUNLOCK(inp);
1242 		error = sctp_inpcb_bind(so, NULL, l);
1243 		if (error) {
1244 			SCTP_ASOC_CREATE_UNLOCK(inp);
1245 			return (error);
1246 		}
1247 	} else {
1248 		SCTP_INP_WUNLOCK(inp);
1249 	}
1250         /* We are GOOD to go */
1251 	stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0);
1252 	if (stcb == NULL) {
1253 		/* Gak! no memory */
1254 		SCTP_ASOC_CREATE_UNLOCK(inp);
1255 		return (error);
1256 	}
1257 	/* move to second address */
1258 	if (sa->sa_family == AF_INET)
1259 		sa = (struct sockaddr *)((vaddr_t)sa + sizeof(struct sockaddr_in));
1260 	else
1261 		sa = (struct sockaddr *)((vaddr_t)sa + sizeof(struct sockaddr_in6));
1262 
1263 	for (i = 1; i < totaddr; i++) {
1264 		if (sa->sa_family == AF_INET) {
1265 			incr = sizeof(struct sockaddr_in);
1266 			if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1267 				/* assoc gone no un-lock */
1268 				sctp_free_assoc(inp, stcb);
1269 				SCTP_ASOC_CREATE_UNLOCK(inp);
1270 				return (ENOBUFS);
1271 			}
1272 
1273 		} else if (sa->sa_family == AF_INET6) {
1274 			incr = sizeof(struct sockaddr_in6);
1275 			if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1276 				/* assoc gone no un-lock */
1277 				sctp_free_assoc(inp, stcb);
1278 				SCTP_ASOC_CREATE_UNLOCK(inp);
1279 				return (ENOBUFS);
1280 			}
1281 		}
1282 		sa = (struct sockaddr *)((vaddr_t)sa + incr);
1283 	}
1284 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1285 	if (delay) {
1286 		/* doing delayed connection */
1287 		stcb->asoc.delayed_connection = 1;
1288 		sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1289 	} else {
1290 		SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1291 		sctp_send_initiate(inp, stcb);
1292 	}
1293 	SCTP_TCB_UNLOCK(stcb);
1294 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1295 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1296 		/* Set the connected flag so we can queue data */
1297 		soisconnecting(so);
1298 	}
1299 	SCTP_ASOC_CREATE_UNLOCK(inp);
1300 	return error;
1301 }
1302 
1303 
1304 static int
1305 sctp_optsget(struct socket *so, struct sockopt *sopt)
1306 {
1307 	struct sctp_inpcb *inp;
1308 	int error, optval=0;
1309 	int *ovp;
1310 	struct sctp_tcb *stcb = NULL;
1311 
1312         inp = (struct sctp_inpcb *)so->so_pcb;
1313 	if (inp == 0)
1314 		return EINVAL;
1315 	error = 0;
1316 
1317 #ifdef SCTP_DEBUG
1318 	if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1319 		printf("optsget opt:%x sz:%zu\n", sopt->sopt_name,
1320 		       sopt->sopt_size);
1321 	}
1322 #endif /* SCTP_DEBUG */
1323 
1324 	switch (sopt->sopt_name) {
1325 	case SCTP_NODELAY:
1326 	case SCTP_AUTOCLOSE:
1327 	case SCTP_AUTO_ASCONF:
1328 	case SCTP_DISABLE_FRAGMENTS:
1329 	case SCTP_I_WANT_MAPPED_V4_ADDR:
1330 #ifdef SCTP_DEBUG
1331 		if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1332 			printf("other stuff\n");
1333 		}
1334 #endif /* SCTP_DEBUG */
1335 		SCTP_INP_RLOCK(inp);
1336 		switch (sopt->sopt_name) {
1337 		case SCTP_DISABLE_FRAGMENTS:
1338 			optval = inp->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT;
1339 			break;
1340 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1341 			optval = inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
1342 			break;
1343 		case SCTP_AUTO_ASCONF:
1344 			optval = inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF;
1345 			break;
1346 		case SCTP_NODELAY:
1347 			optval = inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY;
1348 			break;
1349 		case SCTP_AUTOCLOSE:
1350 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE) ==
1351 			    SCTP_PCB_FLAGS_AUTOCLOSE)
1352 				optval = inp->sctp_ep.auto_close_time;
1353 			else
1354 				optval = 0;
1355 			break;
1356 
1357 		default:
1358 			error = ENOPROTOOPT;
1359 		} /* end switch (sopt->sopt_name) */
1360 		if (sopt->sopt_name != SCTP_AUTOCLOSE) {
1361 			/* make it an "on/off" value */
1362 			optval = (optval != 0);
1363 		}
1364 		if (sopt->sopt_size < sizeof(int)) {
1365 			error = EINVAL;
1366 		}
1367 		SCTP_INP_RUNLOCK(inp);
1368 		if (error == 0) {
1369 			/* return the option value */
1370 			ovp = sopt->sopt_data;
1371 			*ovp = optval;
1372 			sopt->sopt_size = sizeof(optval);
1373 		}
1374 		break;
1375 	case SCTP_GET_ASOC_ID_LIST:
1376 	{
1377 		struct sctp_assoc_ids *ids;
1378 		int cnt, at;
1379 		u_int16_t orig;
1380 
1381 		if (sopt->sopt_size < sizeof(struct sctp_assoc_ids)) {
1382 			error = EINVAL;
1383 			break;
1384 		}
1385 		ids = sopt->sopt_data;
1386 		cnt = 0;
1387 		SCTP_INP_RLOCK(inp);
1388 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1389 		if (stcb == NULL) {
1390 		none_out_now:
1391 			ids->asls_numb_present = 0;
1392 			ids->asls_more_to_get = 0;
1393 			SCTP_INP_RUNLOCK(inp);
1394 			break;
1395 		}
1396 		orig = ids->asls_assoc_start;
1397 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
1398 		while( orig ) {
1399 			stcb = LIST_NEXT(stcb , sctp_tcblist);
1400 			orig--;
1401 			cnt--;
1402 		}
1403 		if ( stcb == NULL)
1404 			goto none_out_now;
1405 
1406 		at = 0;
1407 		ids->asls_numb_present = 0;
1408 		ids->asls_more_to_get = 1;
1409 		while(at < MAX_ASOC_IDS_RET) {
1410 			ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1411 			at++;
1412 			ids->asls_numb_present++;
1413 			stcb = LIST_NEXT(stcb , sctp_tcblist);
1414 			if (stcb == NULL) {
1415 				ids->asls_more_to_get = 0;
1416 				break;
1417 			}
1418 		}
1419 		SCTP_INP_RUNLOCK(inp);
1420 	}
1421 	break;
1422 	case SCTP_GET_NONCE_VALUES:
1423 	{
1424 		struct sctp_get_nonce_values *gnv;
1425 		if (sopt->sopt_size < sizeof(struct sctp_get_nonce_values)) {
1426 			error = EINVAL;
1427 			break;
1428 		}
1429 		gnv = sopt->sopt_data;
1430 		stcb = sctp_findassociation_ep_asocid(inp, gnv->gn_assoc_id);
1431 		if (stcb == NULL) {
1432 			error = ENOTCONN;
1433 		} else {
1434 			gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1435 			gnv->gn_local_tag = stcb->asoc.my_vtag;
1436 			SCTP_TCB_UNLOCK(stcb);
1437 		}
1438 
1439 	}
1440 	break;
1441 	case SCTP_PEER_PUBLIC_KEY:
1442 	case SCTP_MY_PUBLIC_KEY:
1443 	case SCTP_SET_AUTH_CHUNKS:
1444 	case SCTP_SET_AUTH_SECRET:
1445 		/* not supported yet and until we refine the draft */
1446 		error = EOPNOTSUPP;
1447 		break;
1448 
1449 	case SCTP_DELAYED_ACK_TIME:
1450 	{
1451 		int32_t *tm;
1452 		if (sopt->sopt_size < sizeof(int32_t)) {
1453 			error = EINVAL;
1454 			break;
1455 		}
1456 		tm = sopt->sopt_data;
1457 
1458 		*tm = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1459 	}
1460 	break;
1461 
1462 	case SCTP_GET_SNDBUF_USE:
1463 		if (sopt->sopt_size < sizeof(struct sctp_sockstat)) {
1464 			error = EINVAL;
1465 		} else {
1466 			struct sctp_sockstat *ss;
1467 			struct sctp_association *asoc;
1468 			ss = sopt->sopt_data;
1469    		        stcb = sctp_findassociation_ep_asocid(inp, ss->ss_assoc_id);
1470 			if (stcb == NULL) {
1471 				error = ENOTCONN;
1472 			} else {
1473 				asoc = &stcb->asoc;
1474 				ss->ss_total_sndbuf = (u_int32_t)asoc->total_output_queue_size;
1475 				ss->ss_total_mbuf_sndbuf = (u_int32_t)asoc->total_output_mbuf_queue_size;
1476 				ss->ss_total_recv_buf = (u_int32_t)(asoc->size_on_delivery_queue +
1477 								    asoc->size_on_reasm_queue +
1478 								    asoc->size_on_all_streams);
1479 				SCTP_TCB_UNLOCK(stcb);
1480 				error = 0;
1481 				sopt->sopt_size = sizeof(struct sctp_sockstat);
1482 			}
1483 		}
1484 		break;
1485 	case SCTP_MAXBURST:
1486 	{
1487 		u_int8_t *burst;
1488 		burst = sopt->sopt_data;
1489 		SCTP_INP_RLOCK(inp);
1490 		*burst = inp->sctp_ep.max_burst;
1491 		SCTP_INP_RUNLOCK(inp);
1492 		sopt->sopt_size = sizeof(u_int8_t);
1493 	}
1494 	break;
1495 	case SCTP_MAXSEG:
1496 	{
1497 		u_int32_t *segsize;
1498 		sctp_assoc_t *assoc_id;
1499 		int ovh;
1500 
1501 		if (sopt->sopt_size < sizeof(u_int32_t)) {
1502 			error = EINVAL;
1503 			break;
1504 		}
1505 		if (sopt->sopt_size < sizeof(sctp_assoc_t)) {
1506 			error = EINVAL;
1507 			break;
1508 		}
1509 		assoc_id = sopt->sopt_data;
1510 		segsize = sopt->sopt_data;
1511 		sopt->sopt_size = sizeof(u_int32_t);
1512 
1513 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1514 		     (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) ||
1515 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1516 			SCTP_INP_RLOCK(inp);
1517 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1518 			if (stcb) {
1519 				SCTP_TCB_LOCK(stcb);
1520 				SCTP_INP_RUNLOCK(inp);
1521 				*segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1522 				SCTP_TCB_UNLOCK(stcb);
1523 			} else {
1524 				SCTP_INP_RUNLOCK(inp);
1525 				goto skipit;
1526 			}
1527 		} else {
1528 			stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
1529 			if (stcb) {
1530 				*segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1531 				SCTP_TCB_UNLOCK(stcb);
1532 				break;
1533 			}
1534 		skipit:
1535 			/* default is to get the max, if I
1536 			 * can't calculate from an existing association.
1537 			 */
1538 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1539 				ovh = SCTP_MED_OVERHEAD;
1540 			} else {
1541 				ovh = SCTP_MED_V4_OVERHEAD;
1542 			}
1543 			*segsize = inp->sctp_frag_point - ovh;
1544 		}
1545 	}
1546 	break;
1547 
1548 	case SCTP_SET_DEBUG_LEVEL:
1549 #ifdef SCTP_DEBUG
1550 	{
1551 		u_int32_t *level;
1552 		if (sopt->sopt_size < sizeof(u_int32_t)) {
1553 			error = EINVAL;
1554 			break;
1555 		}
1556 		level = sopt->sopt_data;
1557 		error = 0;
1558 		*level = sctp_debug_on;
1559 		sopt->sopt_size = sizeof(u_int32_t);
1560 		printf("Returning DEBUG LEVEL %x is set\n",
1561 		       (u_int)sctp_debug_on);
1562 	}
1563 #else /* SCTP_DEBUG */
1564 	error = EOPNOTSUPP;
1565 #endif
1566 	break;
1567 	case SCTP_GET_STAT_LOG:
1568 #ifdef SCTP_STAT_LOGGING
1569 		error = sctp_fill_stat_log(m);
1570 #else /* SCTP_DEBUG */
1571 		error = EOPNOTSUPP;
1572 #endif
1573 		break;
1574 	case SCTP_GET_PEGS:
1575 	{
1576 		u_int32_t *pt;
1577 		if (sopt->sopt_size < sizeof(sctp_pegs)) {
1578 			error = EINVAL;
1579 			break;
1580 		}
1581 		pt = sopt->sopt_data;
1582 		memcpy(pt, sctp_pegs, sizeof(sctp_pegs));
1583 		sopt->sopt_size = sizeof(sctp_pegs);
1584 	}
1585 	break;
1586 	case SCTP_EVENTS:
1587 	{
1588 		struct sctp_event_subscribe *events;
1589 #ifdef SCTP_DEBUG
1590 		if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1591 			printf("get events\n");
1592 		}
1593 #endif /* SCTP_DEBUG */
1594 		if (sopt->sopt_size < sizeof(struct sctp_event_subscribe)) {
1595 #ifdef SCTP_DEBUG
1596 			if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1597 				printf("sopt->sopt_size is %d not %d\n",
1598 				       (int)sopt->sopt_size,
1599 				       (int)sizeof(struct sctp_event_subscribe));
1600 			}
1601 #endif /* SCTP_DEBUG */
1602 			error = EINVAL;
1603 			break;
1604 		}
1605 		events = sopt->sopt_data;
1606 		memset(events, 0, sopt->sopt_size);
1607 		SCTP_INP_RLOCK(inp);
1608 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT)
1609 			events->sctp_data_io_event = 1;
1610 
1611 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVASSOCEVNT)
1612 			events->sctp_association_event = 1;
1613 
1614 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPADDREVNT)
1615 			events->sctp_address_event = 1;
1616 
1617 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSENDFAILEVNT)
1618 			events->sctp_send_failure_event = 1;
1619 
1620 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPEERERR)
1621 			events->sctp_peer_error_event = 1;
1622 
1623 		if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)
1624 			events->sctp_shutdown_event = 1;
1625 
1626 		if (inp->sctp_flags & SCTP_PCB_FLAGS_PDAPIEVNT)
1627 			events->sctp_partial_delivery_event = 1;
1628 
1629 		if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT)
1630 			events->sctp_adaption_layer_event = 1;
1631 
1632 		if (inp->sctp_flags & SCTP_PCB_FLAGS_STREAM_RESETEVNT)
1633 			events->sctp_stream_reset_events = 1;
1634 		SCTP_INP_RUNLOCK(inp);
1635 		sopt->sopt_size = sizeof(struct sctp_event_subscribe);
1636 
1637 	}
1638 	break;
1639 
1640 	case SCTP_ADAPTION_LAYER:
1641 		if (sopt->sopt_size < sizeof(int)) {
1642 			error = EINVAL;
1643 			break;
1644 		}
1645 #ifdef SCTP_DEBUG
1646 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1647 			printf("getadaption ind\n");
1648 		}
1649 #endif /* SCTP_DEBUG */
1650 		SCTP_INP_RLOCK(inp);
1651 		ovp = sopt->sopt_data;
1652 		*ovp = inp->sctp_ep.adaption_layer_indicator;
1653 		SCTP_INP_RUNLOCK(inp);
1654 		sopt->sopt_size = sizeof(int);
1655 		break;
1656 	case SCTP_SET_INITIAL_DBG_SEQ:
1657 		if (sopt->sopt_size < sizeof(int)) {
1658 			error = EINVAL;
1659 			break;
1660 		}
1661 #ifdef SCTP_DEBUG
1662 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1663 			printf("get initial dbg seq\n");
1664 		}
1665 #endif /* SCTP_DEBUG */
1666 		SCTP_INP_RLOCK(inp);
1667 		ovp = sopt->sopt_data;
1668 		*ovp = inp->sctp_ep.initial_sequence_debug;
1669 		SCTP_INP_RUNLOCK(inp);
1670 		sopt->sopt_size = sizeof(int);
1671 		break;
1672 	case SCTP_GET_LOCAL_ADDR_SIZE:
1673 		if (sopt->sopt_size < sizeof(int)) {
1674 			error = EINVAL;
1675 			break;
1676 		}
1677 #ifdef SCTP_DEBUG
1678 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1679 			printf("get local sizes\n");
1680 		}
1681 #endif /* SCTP_DEBUG */
1682 		SCTP_INP_RLOCK(inp);
1683 		ovp = sopt->sopt_data;
1684 		*ovp = sctp_count_max_addresses(inp);
1685 		SCTP_INP_RUNLOCK(inp);
1686 		sopt->sopt_size = sizeof(int);
1687 		break;
1688 	case SCTP_GET_REMOTE_ADDR_SIZE:
1689 	{
1690 		sctp_assoc_t *assoc_id;
1691 		u_int32_t *val, sz;
1692 		struct sctp_nets *net;
1693 #ifdef SCTP_DEBUG
1694 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1695 			printf("get remote size\n");
1696 		}
1697 #endif /* SCTP_DEBUG */
1698 		if (sopt->sopt_size < sizeof(sctp_assoc_t)) {
1699 #ifdef SCTP_DEBUG
1700 			printf("sopt->sopt_size:%zu not %zu\n",
1701 			       sopt->sopt_size, sizeof(sctp_assoc_t));
1702 #endif /* SCTP_DEBUG */
1703 			error = EINVAL;
1704 			break;
1705 		}
1706 		stcb = NULL;
1707 		val = sopt->sopt_data;
1708 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1709 			SCTP_INP_RLOCK(inp);
1710 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1711 			if (stcb) {
1712 				SCTP_TCB_LOCK(stcb);
1713 			}
1714 			SCTP_INP_RUNLOCK(inp);
1715 		}
1716 		if (stcb == NULL) {
1717 			assoc_id = sopt->sopt_data;
1718 			stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
1719 		}
1720 
1721 		if (stcb == NULL) {
1722 			error = EINVAL;
1723 			break;
1724 		}
1725 		*val = 0;
1726 		sz = 0;
1727 		/* Count the sizes */
1728 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1729 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1730 			    (rtcache_getdst(&net->ro)->sa_family == AF_INET6)) {
1731 				sz += sizeof(struct sockaddr_in6);
1732 			} else if (rtcache_getdst(&net->ro)->sa_family == AF_INET) {
1733 				sz += sizeof(struct sockaddr_in);
1734 			} else {
1735 				/* huh */
1736 				break;
1737 			}
1738 		}
1739 		SCTP_TCB_UNLOCK(stcb);
1740 		*val = sz;
1741 		sopt->sopt_size = sizeof(u_int32_t);
1742 	}
1743 	break;
1744 	case SCTP_GET_PEER_ADDRESSES:
1745 		/*
1746 		 * Get the address information, an array
1747 		 * is passed in to fill up we pack it.
1748 		 */
1749 	{
1750 		int cpsz, left;
1751 		struct sockaddr_storage *sas;
1752 		struct sctp_nets *net;
1753 		struct sctp_getaddresses *saddr;
1754 #ifdef SCTP_DEBUG
1755 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1756 			printf("get peer addresses\n");
1757 		}
1758 #endif /* SCTP_DEBUG */
1759 		if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
1760 			error = EINVAL;
1761 			break;
1762 		}
1763 		left = sopt->sopt_size - sizeof(struct sctp_getaddresses);
1764 		saddr = sopt->sopt_data;
1765 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1766 			SCTP_INP_RLOCK(inp);
1767 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1768 			if (stcb) {
1769 				SCTP_TCB_LOCK(stcb);
1770 			}
1771 			SCTP_INP_RUNLOCK(inp);
1772 		} else
1773 			stcb = sctp_findassociation_ep_asocid(inp,
1774 							      saddr->sget_assoc_id);
1775 		if (stcb == NULL) {
1776 			error = ENOENT;
1777 			break;
1778 		}
1779 		sopt->sopt_size = sizeof(struct sctp_getaddresses);
1780 		sas = (struct sockaddr_storage *)&saddr->addr[0];
1781 
1782 		TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1783 			sa_family_t family;
1784 
1785 			family = rtcache_getdst(&net->ro)->sa_family;
1786 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
1787 			    (family == AF_INET6)) {
1788 				cpsz = sizeof(struct sockaddr_in6);
1789 			} else if (family == AF_INET) {
1790 				cpsz = sizeof(struct sockaddr_in);
1791 			} else {
1792 				/* huh */
1793 				break;
1794 			}
1795 			if (left < cpsz) {
1796 				/* not enough room. */
1797 #ifdef SCTP_DEBUG
1798 				if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1799 					printf("Out of room\n");
1800 				}
1801 #endif /* SCTP_DEBUG */
1802 				break;
1803 			}
1804 			if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
1805 			    (family == AF_INET)) {
1806 				/* Must map the address */
1807 				in6_sin_2_v4mapsin6((const struct sockaddr_in *) rtcache_getdst(&net->ro),
1808 						    (struct sockaddr_in6 *)sas);
1809 			} else {
1810 				memcpy(sas, rtcache_getdst(&net->ro), cpsz);
1811 			}
1812 			((struct sockaddr_in *)sas)->sin_port = stcb->rport;
1813 
1814 			sas = (struct sockaddr_storage *)((vaddr_t)sas + cpsz);
1815 			left -= cpsz;
1816 			sopt->sopt_size += cpsz;
1817 #ifdef SCTP_DEBUG
1818 			if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1819 				printf("left now:%d mlen:%zu\n",
1820 				       left, sopt->sopt_size);
1821 			}
1822 #endif /* SCTP_DEBUG */
1823 		}
1824 		SCTP_TCB_UNLOCK(stcb);
1825 	}
1826 #ifdef SCTP_DEBUG
1827 	if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1828 		printf("All done\n");
1829 	}
1830 #endif /* SCTP_DEBUG */
1831 	break;
1832 	case SCTP_GET_LOCAL_ADDRESSES:
1833 	{
1834 		int limit, actual;
1835 		struct sockaddr_storage *sas;
1836 		struct sctp_getaddresses *saddr;
1837 #ifdef SCTP_DEBUG
1838 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1839 			printf("get local addresses\n");
1840 		}
1841 #endif /* SCTP_DEBUG */
1842 		if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
1843 			error = EINVAL;
1844 			break;
1845 		}
1846 		saddr = sopt->sopt_data;
1847 
1848 		if (saddr->sget_assoc_id) {
1849 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1850 				SCTP_INP_RLOCK(inp);
1851 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
1852 				if (stcb) {
1853 					SCTP_TCB_LOCK(stcb);
1854 				}
1855 				SCTP_INP_RUNLOCK(inp);
1856 			} else
1857 				stcb = sctp_findassociation_ep_asocid(inp, saddr->sget_assoc_id);
1858 
1859 		} else {
1860 			stcb = NULL;
1861 		}
1862 		/*
1863 		 * assure that the TCP model does not need a assoc id
1864 		 * once connected.
1865 		 */
1866 		if ( (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
1867 		     (stcb == NULL) ) {
1868 			SCTP_INP_RLOCK(inp);
1869 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1870 			if (stcb) {
1871 				SCTP_TCB_LOCK(stcb);
1872 			}
1873 			SCTP_INP_RUNLOCK(inp);
1874 		}
1875 		sas = (struct sockaddr_storage *)&saddr->addr[0];
1876 		limit = sopt->sopt_size - sizeof(sctp_assoc_t);
1877 		actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
1878 		SCTP_TCB_UNLOCK(stcb);
1879 		sopt->sopt_size = sizeof(struct sockaddr_storage) + actual;
1880 	}
1881 	break;
1882 	case SCTP_PEER_ADDR_PARAMS:
1883 	{
1884 		struct sctp_paddrparams *paddrp;
1885 		struct sctp_nets *net;
1886 
1887 #ifdef SCTP_DEBUG
1888 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1889 			printf("Getting peer_addr_params\n");
1890 		}
1891 #endif /* SCTP_DEBUG */
1892 		if (sopt->sopt_size < sizeof(struct sctp_paddrparams)) {
1893 #ifdef SCTP_DEBUG
1894 			if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1895 				printf("Hmm m->m_len:%zu is to small\n",
1896 				       sopt->sopt_size);
1897 			}
1898 #endif /* SCTP_DEBUG */
1899 			error = EINVAL;
1900 			break;
1901 		}
1902 		paddrp = sopt->sopt_data;
1903 
1904 		net = NULL;
1905 		if (paddrp->spp_assoc_id) {
1906 #ifdef SCTP_DEBUG
1907 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1908 				printf("In spp_assoc_id find type\n");
1909 			}
1910 #endif /* SCTP_DEBUG */
1911 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1912 				SCTP_INP_RLOCK(inp);
1913 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
1914 				if (stcb) {
1915 					SCTP_TCB_LOCK(stcb);
1916 					net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1917 				}
1918 				SCTP_INP_RLOCK(inp);
1919 			} else {
1920 				stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
1921 			}
1922 			if (stcb == NULL) {
1923 				error = ENOENT;
1924 				break;
1925 			}
1926 		}
1927 		if (	(stcb == NULL) &&
1928 			((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
1929 			 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
1930 			/* Lookup via address */
1931 #ifdef SCTP_DEBUG
1932 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1933 				printf("Ok we need to lookup a param\n");
1934 			}
1935 #endif /* SCTP_DEBUG */
1936 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1937 				SCTP_INP_RLOCK(inp);
1938 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
1939 				if (stcb) {
1940 					SCTP_TCB_LOCK(stcb);
1941 					net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
1942 				}
1943 				SCTP_INP_RUNLOCK(inp);
1944 			} else {
1945 				SCTP_INP_WLOCK(inp);
1946 				SCTP_INP_INCR_REF(inp);
1947 				SCTP_INP_WUNLOCK(inp);
1948 				stcb = sctp_findassociation_ep_addr(&inp,
1949 								    (struct sockaddr *)&paddrp->spp_address,
1950 								    &net, NULL, NULL);
1951 				if (stcb == NULL) {
1952 					SCTP_INP_WLOCK(inp);
1953 					SCTP_INP_DECR_REF(inp);
1954 					SCTP_INP_WUNLOCK(inp);
1955 				}
1956 			}
1957 
1958 			if (stcb == NULL) {
1959 				error = ENOENT;
1960 				break;
1961 			}
1962 		} else {
1963 			/* Effects the Endpoint */
1964 #ifdef SCTP_DEBUG
1965 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1966 				printf("User wants EP level info\n");
1967 			}
1968 #endif /* SCTP_DEBUG */
1969 			stcb = NULL;
1970 		}
1971 		if (stcb) {
1972 			/* Applys to the specific association */
1973 #ifdef SCTP_DEBUG
1974 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1975 				printf("In TCB side\n");
1976 			}
1977 #endif /* SCTP_DEBUG */
1978 			if (net) {
1979 				paddrp->spp_pathmaxrxt = net->failure_threshold;
1980 			} else {
1981 				/* No destination so return default value */
1982 				paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
1983 			}
1984 			paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
1985 			paddrp->spp_assoc_id = sctp_get_associd(stcb);
1986 			SCTP_TCB_UNLOCK(stcb);
1987 		} else {
1988 			/* Use endpoint defaults */
1989 			SCTP_INP_RLOCK(inp);
1990 #ifdef SCTP_DEBUG
1991 			if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1992 				printf("In EP levle info\n");
1993 			}
1994 #endif /* SCTP_DEBUG */
1995 			paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
1996 			paddrp->spp_hbinterval = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT];
1997 			paddrp->spp_assoc_id = (sctp_assoc_t)0;
1998 			SCTP_INP_RUNLOCK(inp);
1999 		}
2000 		sopt->sopt_size = sizeof(struct sctp_paddrparams);
2001 	}
2002 	break;
2003 	case SCTP_GET_PEER_ADDR_INFO:
2004 	{
2005 		struct sctp_paddrinfo *paddri;
2006 		struct sctp_nets *net;
2007 #ifdef SCTP_DEBUG
2008 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2009 			printf("GetPEER ADDR_INFO\n");
2010 		}
2011 #endif /* SCTP_DEBUG */
2012 		if (sopt->sopt_size < sizeof(struct sctp_paddrinfo)) {
2013 			error = EINVAL;
2014 			break;
2015 		}
2016 		paddri = sopt->sopt_data;
2017 		net = NULL;
2018 		if ((((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET) ||
2019 		    (((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET6)) {
2020 			/* Lookup via address */
2021 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2022 				SCTP_INP_RLOCK(inp);
2023 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
2024 				if (stcb) {
2025 					SCTP_TCB_LOCK(stcb);
2026 					net = sctp_findnet(stcb,
2027 							    (struct sockaddr *)&paddri->spinfo_address);
2028 				}
2029 				SCTP_INP_RUNLOCK(inp);
2030 			} else {
2031 				SCTP_INP_WLOCK(inp);
2032 				SCTP_INP_INCR_REF(inp);
2033 				SCTP_INP_WUNLOCK(inp);
2034 				stcb = sctp_findassociation_ep_addr(&inp,
2035 				    (struct sockaddr *)&paddri->spinfo_address,
2036 				    &net, NULL, NULL);
2037 				if (stcb == NULL) {
2038 					SCTP_INP_WLOCK(inp);
2039 					SCTP_INP_DECR_REF(inp);
2040 					SCTP_INP_WUNLOCK(inp);
2041 				}
2042 			}
2043 
2044 		} else {
2045 			stcb = NULL;
2046 		}
2047 		if ((stcb == NULL) || (net == NULL)) {
2048 			error = ENOENT;
2049 			break;
2050 		}
2051 		sopt->sopt_size = sizeof(struct sctp_paddrinfo);
2052 		paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK|SCTP_ADDR_NOHB);
2053 		paddri->spinfo_cwnd = net->cwnd;
2054 		paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2055 		paddri->spinfo_rto = net->RTO;
2056 		paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2057 		SCTP_TCB_UNLOCK(stcb);
2058 	}
2059 	break;
2060 	case SCTP_PCB_STATUS:
2061 	{
2062 		struct sctp_pcbinfo *spcb;
2063 #ifdef SCTP_DEBUG
2064 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2065 			printf("PCB status\n");
2066 		}
2067 #endif /* SCTP_DEBUG */
2068 		if (sopt->sopt_size < sizeof(struct sctp_pcbinfo)) {
2069 			error = EINVAL;
2070 			break;
2071 		}
2072 		spcb = sopt->sopt_data;
2073 		sctp_fill_pcbinfo(spcb);
2074 		sopt->sopt_size = sizeof(struct sctp_pcbinfo);
2075 	}
2076 	break;
2077 	case SCTP_STATUS:
2078 	{
2079 		struct sctp_nets *net;
2080 		struct sctp_status *sstat;
2081 #ifdef SCTP_DEBUG
2082 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2083 			printf("SCTP status\n");
2084 		}
2085 #endif /* SCTP_DEBUG */
2086 
2087 		if (sopt->sopt_size < sizeof(struct sctp_status)) {
2088 			error = EINVAL;
2089 			break;
2090 		}
2091 		sstat = sopt->sopt_data;
2092 
2093 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2094 			SCTP_INP_RLOCK(inp);
2095 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
2096 			if (stcb) {
2097 				SCTP_TCB_LOCK(stcb);
2098 			}
2099 			SCTP_INP_RUNLOCK(inp);
2100 		} else
2101 			stcb = sctp_findassociation_ep_asocid(inp, sstat->sstat_assoc_id);
2102 
2103 		if (stcb == NULL) {
2104 			error = EINVAL;
2105 			break;
2106 		}
2107 		/*
2108 		 * I think passing the state is fine since
2109 		 * sctp_constants.h will be available to the user
2110 		 * land.
2111 		 */
2112 		sstat->sstat_state = stcb->asoc.state;
2113 		sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2114 		sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2115 		/*
2116 		 * We can't include chunks that have been passed
2117 		 * to the socket layer. Only things in queue.
2118 		 */
2119 		sstat->sstat_penddata = (stcb->asoc.cnt_on_delivery_queue +
2120 					 stcb->asoc.cnt_on_reasm_queue +
2121 					 stcb->asoc.cnt_on_all_streams);
2122 
2123 
2124 		sstat->sstat_instrms = stcb->asoc.streamincnt;
2125 		sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2126 		sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2127 		memcpy(&sstat->sstat_primary.spinfo_address,
2128 		       rtcache_getdst(&stcb->asoc.primary_destination->ro),
2129 		       (rtcache_getdst(&stcb->asoc.primary_destination->ro))->sa_len);
2130 		net = stcb->asoc.primary_destination;
2131 		((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2132 		/*
2133 		 * Again the user can get info from sctp_constants.h
2134 		 * for what the state of the network is.
2135 		 */
2136 		sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2137 		sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2138 		sstat->sstat_primary.spinfo_srtt = net->lastsa;
2139 		sstat->sstat_primary.spinfo_rto = net->RTO;
2140 		sstat->sstat_primary.spinfo_mtu = net->mtu;
2141 		sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2142 		SCTP_TCB_UNLOCK(stcb);
2143 		sopt->sopt_size = sizeof(*sstat);
2144 	}
2145 	break;
2146 	case SCTP_RTOINFO:
2147 	{
2148 		struct sctp_rtoinfo *srto;
2149 #ifdef SCTP_DEBUG
2150 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2151 			printf("RTO Info\n");
2152 		}
2153 #endif /* SCTP_DEBUG */
2154 		if (sopt->sopt_size < sizeof(struct sctp_rtoinfo)) {
2155 			error = EINVAL;
2156 			break;
2157 		}
2158 		srto = sopt->sopt_data;
2159 		if (srto->srto_assoc_id == 0) {
2160 			/* Endpoint only please */
2161 			SCTP_INP_RLOCK(inp);
2162 			srto->srto_initial = inp->sctp_ep.initial_rto;
2163 			srto->srto_max = inp->sctp_ep.sctp_maxrto;
2164 			srto->srto_min = inp->sctp_ep.sctp_minrto;
2165 			SCTP_INP_RUNLOCK(inp);
2166 			break;
2167 		}
2168 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2169 			SCTP_INP_RLOCK(inp);
2170 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
2171 			if (stcb) {
2172 				SCTP_TCB_LOCK(stcb);
2173 			}
2174 			SCTP_INP_RUNLOCK(inp);
2175 		} else
2176 			stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
2177 
2178 		if (stcb == NULL) {
2179 			error = EINVAL;
2180 			break;
2181 		}
2182 		srto->srto_initial = stcb->asoc.initial_rto;
2183 		srto->srto_max = stcb->asoc.maxrto;
2184 		srto->srto_min = stcb->asoc.minrto;
2185 		SCTP_TCB_UNLOCK(stcb);
2186 		sopt->sopt_size = sizeof(*srto);
2187 	}
2188 	break;
2189 	case SCTP_ASSOCINFO:
2190 	{
2191 		struct sctp_assocparams *sasoc;
2192 #ifdef SCTP_DEBUG
2193 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2194 			printf("Associnfo\n");
2195 		}
2196 #endif /* SCTP_DEBUG */
2197 		if (sopt->sopt_size < sizeof(struct sctp_assocparams)) {
2198 			error = EINVAL;
2199 			break;
2200 		}
2201 		sasoc = sopt->sopt_data;
2202 		stcb = NULL;
2203 
2204 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2205 			SCTP_INP_RLOCK(inp);
2206 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
2207 			if (stcb) {
2208 				SCTP_TCB_LOCK(stcb);
2209 			}
2210 			SCTP_INP_RUNLOCK(inp);
2211 		}
2212 		if ((sasoc->sasoc_assoc_id) && (stcb == NULL)) {
2213 			stcb = sctp_findassociation_ep_asocid(inp,
2214 							     sasoc->sasoc_assoc_id);
2215 			if (stcb == NULL) {
2216 				error = ENOENT;
2217 				break;
2218 			}
2219 		} else {
2220 			stcb = NULL;
2221 		}
2222 
2223 		if (stcb) {
2224 			sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2225 			sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2226 			sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2227 			sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2228 			sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2229 			SCTP_TCB_UNLOCK(stcb);
2230 		} else {
2231 			SCTP_INP_RLOCK(inp);
2232 			sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2233 			sasoc->sasoc_number_peer_destinations = 0;
2234 			sasoc->sasoc_peer_rwnd = 0;
2235 			sasoc->sasoc_local_rwnd = sbspace(&inp->sctp_socket->so_rcv);
2236 			sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2237 			SCTP_INP_RUNLOCK(inp);
2238 		}
2239 		sopt->sopt_size = sizeof(*sasoc);
2240 	}
2241 	break;
2242 	case SCTP_DEFAULT_SEND_PARAM:
2243 	{
2244 		struct sctp_sndrcvinfo *s_info;
2245 
2246 		if (sopt->sopt_size != sizeof(struct sctp_sndrcvinfo)) {
2247 			error = EINVAL;
2248 			break;
2249 		}
2250 		s_info = sopt->sopt_data;
2251 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2252 			SCTP_INP_RLOCK(inp);
2253 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
2254 			if (stcb) {
2255 				SCTP_TCB_LOCK(stcb);
2256 			}
2257 			SCTP_INP_RUNLOCK(inp);
2258 		} else
2259 			stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
2260 
2261 		if (stcb == NULL) {
2262 			error = ENOENT;
2263 			break;
2264 		}
2265 		/* Copy it out */
2266 		*s_info = stcb->asoc.def_send;
2267 		SCTP_TCB_UNLOCK(stcb);
2268 		sopt->sopt_size = sizeof(*s_info);
2269 	}
2270 	case SCTP_INITMSG:
2271 	{
2272 		struct sctp_initmsg *sinit;
2273 #ifdef SCTP_DEBUG
2274 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2275 			printf("initmsg\n");
2276 		}
2277 #endif /* SCTP_DEBUG */
2278 		if (sopt->sopt_size < sizeof(struct sctp_initmsg)) {
2279 			error = EINVAL;
2280 			break;
2281 		}
2282 		sinit = sopt->sopt_data;
2283 		SCTP_INP_RLOCK(inp);
2284 		sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2285 		sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2286 		sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2287 		sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2288 		SCTP_INP_RUNLOCK(inp);
2289 		sopt->sopt_size = sizeof(*sinit);
2290 	}
2291 	break;
2292 	case SCTP_PRIMARY_ADDR:
2293 		/* we allow a "get" operation on this */
2294 	{
2295 		struct sctp_setprim *ssp;
2296 
2297 #ifdef SCTP_DEBUG
2298 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2299 			printf("setprimary\n");
2300 		}
2301 #endif /* SCTP_DEBUG */
2302 		if (sopt->sopt_size < sizeof(struct sctp_setprim)) {
2303 			error = EINVAL;
2304 			break;
2305 		}
2306 		ssp = sopt->sopt_data;
2307 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2308 			SCTP_INP_RLOCK(inp);
2309 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
2310 			if (stcb) {
2311 				SCTP_TCB_LOCK(stcb);
2312 			}
2313 			SCTP_INP_RUNLOCK(inp);
2314 		} else {
2315 			stcb = sctp_findassociation_ep_asocid(inp, ssp->ssp_assoc_id);
2316 			if (stcb == NULL) {
2317 				/* one last shot, try it by the address in */
2318 				struct sctp_nets *net;
2319 
2320 				SCTP_INP_WLOCK(inp);
2321 				SCTP_INP_INCR_REF(inp);
2322 				SCTP_INP_WUNLOCK(inp);
2323 				stcb = sctp_findassociation_ep_addr(&inp,
2324 							    (struct sockaddr *)&ssp->ssp_addr,
2325 							    &net, NULL, NULL);
2326 				if (stcb == NULL) {
2327 					SCTP_INP_WLOCK(inp);
2328 					SCTP_INP_DECR_REF(inp);
2329 					SCTP_INP_WUNLOCK(inp);
2330 				}
2331 			}
2332 			if (stcb == NULL) {
2333 				error = EINVAL;
2334 				break;
2335 			}
2336 		}
2337 		/* simply copy out the sockaddr_storage... */
2338 		memcpy(&ssp->ssp_addr,
2339 		       rtcache_getdst(&stcb->asoc.primary_destination->ro),
2340 		       (rtcache_getdst(&stcb->asoc.primary_destination->ro))->sa_len);
2341 		SCTP_TCB_UNLOCK(stcb);
2342 		sopt->sopt_size = sizeof(*ssp);
2343 	}
2344 	break;
2345 	default:
2346 		error = ENOPROTOOPT;
2347 		sopt->sopt_size = 0;
2348 		break;
2349 	} /* end switch (sopt->sopt_name) */
2350         return (error);
2351 }
2352 
2353 static int
2354 sctp_optsset(struct socket *so, struct sockopt *sopt)
2355 {
2356 	int error, *mopt, set_opt;
2357 	struct sctp_tcb *stcb = NULL;
2358         struct sctp_inpcb *inp;
2359 
2360 	if (sopt->sopt_data == NULL) {
2361 #ifdef SCTP_DEBUG
2362 		if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2363 			printf("optsset:MP is NULL EINVAL\n");
2364 		}
2365 #endif /* SCTP_DEBUG */
2366 		return (EINVAL);
2367 	}
2368 	inp = (struct sctp_inpcb *)so->so_pcb;
2369 	if (inp == 0)
2370 		return EINVAL;
2371 
2372 	error = 0;
2373 	switch (sopt->sopt_name) {
2374 	case SCTP_NODELAY:
2375 	case SCTP_AUTOCLOSE:
2376 	case SCTP_AUTO_ASCONF:
2377 	case SCTP_DISABLE_FRAGMENTS:
2378 	case SCTP_I_WANT_MAPPED_V4_ADDR:
2379 		/* copy in the option value */
2380 		if (sopt->sopt_size < sizeof(int)) {
2381 			error = EINVAL;
2382 			break;
2383 		}
2384 		mopt = sopt->sopt_data;
2385 		set_opt = 0;
2386 		if (error)
2387 			break;
2388 		switch (sopt->sopt_name) {
2389 		case SCTP_DISABLE_FRAGMENTS:
2390 			set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2391 			break;
2392 		case SCTP_AUTO_ASCONF:
2393 			set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2394 			break;
2395 
2396 		case SCTP_I_WANT_MAPPED_V4_ADDR:
2397 			if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2398 				set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2399 			} else {
2400 				return (EINVAL);
2401 			}
2402 			break;
2403 		case SCTP_NODELAY:
2404 			set_opt = SCTP_PCB_FLAGS_NODELAY;
2405 			break;
2406 		case SCTP_AUTOCLOSE:
2407 			set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2408 			/*
2409 			 * The value is in ticks.
2410 			 * Note this does not effect old associations, only
2411 			 * new ones.
2412 			 */
2413 			inp->sctp_ep.auto_close_time = (*mopt * hz);
2414 			break;
2415 		}
2416 		SCTP_INP_WLOCK(inp);
2417 		if (*mopt != 0) {
2418 			inp->sctp_flags |= set_opt;
2419 		} else {
2420 			inp->sctp_flags &= ~set_opt;
2421 		}
2422 		SCTP_INP_WUNLOCK(inp);
2423 		break;
2424 	case SCTP_MY_PUBLIC_KEY:    /* set my public key */
2425 	case SCTP_SET_AUTH_CHUNKS:  /* set the authenticated chunks required */
2426 	case SCTP_SET_AUTH_SECRET:  /* set the actual secret for the endpoint */
2427 		/* not supported yet and until we refine the draft */
2428 		error = EOPNOTSUPP;
2429 		break;
2430 
2431 	case SCTP_CLR_STAT_LOG:
2432 #ifdef SCTP_STAT_LOGGING
2433 		sctp_clr_stat_log();
2434 #else
2435 		error = EOPNOTSUPP;
2436 #endif
2437 		break;
2438 	case SCTP_DELAYED_ACK_TIME:
2439 	{
2440 		int32_t *tm;
2441 		if (sopt->sopt_size < sizeof(int32_t)) {
2442 			error = EINVAL;
2443 			break;
2444 		}
2445 		tm = sopt->sopt_data;
2446 
2447 		if ((*tm < 10) || (*tm > 500)) {
2448 			/* can't be smaller than 10ms */
2449 			/* MUST NOT be larger than 500ms */
2450 			error = EINVAL;
2451 			break;
2452 		}
2453 		inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(*tm);
2454 	}
2455 		break;
2456 	case SCTP_RESET_STREAMS:
2457 	{
2458 		struct sctp_stream_reset *strrst;
2459 		uint8_t two_way, not_peer;
2460 
2461 		if (sopt->sopt_size < sizeof(struct sctp_stream_reset)) {
2462 			error = EINVAL;
2463 			break;
2464 		}
2465 		strrst = sopt->sopt_data;
2466 
2467 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2468 			SCTP_INP_RLOCK(inp);
2469 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
2470 			if (stcb) {
2471 				SCTP_TCB_LOCK(stcb);
2472 			}
2473 			SCTP_INP_RUNLOCK(inp);
2474 		} else
2475 			stcb = sctp_findassociation_ep_asocid(inp, strrst->strrst_assoc_id);
2476 		if (stcb == NULL) {
2477 			error = ENOENT;
2478 			break;
2479 		}
2480 		if (stcb->asoc.peer_supports_strreset == 0) {
2481 			/* Peer does not support it,
2482 			 * we return protocol not supported since
2483 			 * this is true for this feature and this
2484 			 * peer, not the socket request in general.
2485 			 */
2486 			error = EPROTONOSUPPORT;
2487 			SCTP_TCB_UNLOCK(stcb);
2488 			break;
2489 		}
2490 
2491 /* Having re-thought this code I added as I write the I-D there
2492  * is NO need for it. The peer, if we are requesting a stream-reset
2493  * will send a request to us but will itself do what we do, take
2494  * and copy off the "reset information" we send and queue TSN's
2495  * larger than the send-next in our response message. Thus they
2496  * will handle it.
2497  */
2498 /*		if (stcb->asoc.sending_seq != (stcb->asoc.last_acked_seq + 1)) {*/
2499 		/* Must have all sending data ack'd before we
2500 		 * start this procedure. This is a bit restrictive
2501 		 * and we SHOULD work on changing this so ONLY the
2502 		 * streams being RESET get held up. So, a reset-all
2503 		 * would require this.. but a reset specific just
2504 		 * needs to be sure that the ones being reset have
2505 		 * nothing on the send_queue. For now we will
2506 		 * skip this more detailed method and do a course
2507 		 * way.. i.e. nothing pending ... for future FIX ME!
2508 		 */
2509 /*			error = EBUSY;*/
2510 /*			break;*/
2511 /*		}*/
2512 
2513 		if (stcb->asoc.stream_reset_outstanding) {
2514 			error = EALREADY;
2515 			SCTP_TCB_UNLOCK(stcb);
2516 			break;
2517 		}
2518 		if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2519 			two_way = 0;
2520 			not_peer = 0;
2521 		} else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2522 			two_way = 1;
2523 			not_peer = 1;
2524 		} else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2525 			two_way = 1;
2526 			not_peer = 0;
2527 		} else {
2528 			error = EINVAL;
2529 			SCTP_TCB_UNLOCK(stcb);
2530 			break;
2531 		}
2532 		sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2533 					strrst->strrst_list, two_way, not_peer);
2534 		sctp_chunk_output(inp, stcb, 12);
2535 		SCTP_TCB_UNLOCK(stcb);
2536 
2537 	}
2538 	break;
2539 	case SCTP_RESET_PEGS:
2540 		memset(sctp_pegs, 0, sizeof(sctp_pegs));
2541 		error = 0;
2542 		break;
2543 	case SCTP_CONNECT_X:
2544 		if (sopt->sopt_size < (sizeof(int) + sizeof(struct sockaddr_in))) {
2545 			error = EINVAL;
2546 			break;
2547 		}
2548 		error = sctp_do_connect_x(so, inp, sopt->sopt_data, curlwp, 0);
2549 		break;
2550 
2551 	case SCTP_CONNECT_X_DELAYED:
2552 		if (sopt->sopt_size < (sizeof(int) + sizeof(struct sockaddr_in))) {
2553 			error = EINVAL;
2554 			break;
2555 		}
2556 		error = sctp_do_connect_x(so, inp, sopt->sopt_data, curlwp, 1);
2557 		break;
2558 
2559 	case SCTP_CONNECT_X_COMPLETE:
2560 	{
2561 		struct sockaddr *sa;
2562 		struct sctp_nets *net;
2563 		if (sopt->sopt_size < sizeof(struct sockaddr_in)) {
2564 			error = EINVAL;
2565 			break;
2566 		}
2567 		sa = sopt->sopt_data;
2568 		/* find tcb */
2569 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2570 			SCTP_INP_RLOCK(inp);
2571 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
2572 			if (stcb) {
2573 				SCTP_TCB_LOCK(stcb);
2574 				net = sctp_findnet(stcb, sa);
2575 			}
2576 			SCTP_INP_RUNLOCK(inp);
2577 		} else {
2578 			SCTP_INP_WLOCK(inp);
2579 			SCTP_INP_INCR_REF(inp);
2580 			SCTP_INP_WUNLOCK(inp);
2581 			stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2582 			if (stcb == NULL) {
2583 				SCTP_INP_WLOCK(inp);
2584 				SCTP_INP_DECR_REF(inp);
2585 				SCTP_INP_WUNLOCK(inp);
2586 			}
2587 		}
2588 
2589 		if (stcb == NULL) {
2590 			error = ENOENT;
2591 			break;
2592 		}
2593 		if (stcb->asoc.delayed_connection == 1) {
2594 			stcb->asoc.delayed_connection = 0;
2595 			SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2596 			sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
2597 			sctp_send_initiate(inp, stcb);
2598 		} else {
2599 			/* already expired or did not use delayed connectx */
2600 			error = EALREADY;
2601 		}
2602 		SCTP_TCB_UNLOCK(stcb);
2603 	}
2604 	break;
2605 	case SCTP_MAXBURST:
2606 	{
2607 		u_int8_t *burst;
2608 		SCTP_INP_WLOCK(inp);
2609 		burst = sopt->sopt_data;
2610 		if (*burst) {
2611 			inp->sctp_ep.max_burst = *burst;
2612 		}
2613 		SCTP_INP_WUNLOCK(inp);
2614 	}
2615 	break;
2616 	case SCTP_MAXSEG:
2617 	{
2618 		u_int32_t *segsize;
2619 		int ovh;
2620 		if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2621 			ovh = SCTP_MED_OVERHEAD;
2622 		} else {
2623 			ovh = SCTP_MED_V4_OVERHEAD;
2624 		}
2625 		segsize = sopt->sopt_data;
2626 		if (*segsize < 1) {
2627 			error = EINVAL;
2628 			break;
2629 		}
2630 		SCTP_INP_WLOCK(inp);
2631 		inp->sctp_frag_point = (*segsize+ovh);
2632 		if (inp->sctp_frag_point < MHLEN) {
2633 			inp->sctp_frag_point = MHLEN;
2634 		}
2635 		SCTP_INP_WUNLOCK(inp);
2636 	}
2637 	break;
2638 	case SCTP_SET_DEBUG_LEVEL:
2639 #ifdef SCTP_DEBUG
2640 	{
2641 		u_int32_t *level;
2642 		if (sopt->sopt_size < sizeof(u_int32_t)) {
2643 			error = EINVAL;
2644 			break;
2645 		}
2646 		level = sopt->sopt_data;
2647 		error = 0;
2648 		sctp_debug_on = (*level & (SCTP_DEBUG_ALL |
2649 					   SCTP_DEBUG_NOISY));
2650 		printf("SETTING DEBUG LEVEL to %x\n",
2651 		       (u_int)sctp_debug_on);
2652 
2653 	}
2654 #else
2655 	error = EOPNOTSUPP;
2656 #endif /* SCTP_DEBUG */
2657 	break;
2658 	case SCTP_EVENTS:
2659 	{
2660 		struct sctp_event_subscribe *events;
2661 		if (sopt->sopt_size < sizeof(struct sctp_event_subscribe)) {
2662 			error = EINVAL;
2663 			break;
2664 		}
2665 		SCTP_INP_WLOCK(inp);
2666 		events = sopt->sopt_data;
2667 		if (events->sctp_data_io_event) {
2668 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVDATAIOEVNT;
2669 		} else {
2670 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVDATAIOEVNT;
2671 		}
2672 
2673 		if (events->sctp_association_event) {
2674 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVASSOCEVNT;
2675 		} else {
2676 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVASSOCEVNT;
2677 		}
2678 
2679 		if (events->sctp_address_event) {
2680 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPADDREVNT;
2681 		} else {
2682 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPADDREVNT;
2683 		}
2684 
2685 		if (events->sctp_send_failure_event) {
2686 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
2687 		} else {
2688 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
2689 		}
2690 
2691 		if (events->sctp_peer_error_event) {
2692 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPEERERR;
2693 		} else {
2694 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPEERERR;
2695 		}
2696 
2697 		if (events->sctp_shutdown_event) {
2698 			inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
2699 		} else {
2700 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
2701 		}
2702 
2703 		if (events->sctp_partial_delivery_event) {
2704 			inp->sctp_flags |= SCTP_PCB_FLAGS_PDAPIEVNT;
2705 		} else {
2706 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_PDAPIEVNT;
2707 		}
2708 
2709 		if (events->sctp_adaption_layer_event) {
2710 			inp->sctp_flags |= SCTP_PCB_FLAGS_ADAPTIONEVNT;
2711 		} else {
2712 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_ADAPTIONEVNT;
2713 		}
2714 
2715 		if (events->sctp_stream_reset_events) {
2716 			inp->sctp_flags |= SCTP_PCB_FLAGS_STREAM_RESETEVNT;
2717 		} else {
2718 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_STREAM_RESETEVNT;
2719 		}
2720 		SCTP_INP_WUNLOCK(inp);
2721 	}
2722 	break;
2723 
2724 	case SCTP_ADAPTION_LAYER:
2725 	{
2726 		struct sctp_setadaption *adap_bits;
2727 		if (sopt->sopt_size < sizeof(struct sctp_setadaption)) {
2728 			error = EINVAL;
2729 			break;
2730 		}
2731 		SCTP_INP_WLOCK(inp);
2732 		adap_bits = sopt->sopt_data;
2733 		inp->sctp_ep.adaption_layer_indicator = adap_bits->ssb_adaption_ind;
2734 		SCTP_INP_WUNLOCK(inp);
2735 	}
2736 	break;
2737 	case SCTP_SET_INITIAL_DBG_SEQ:
2738 	{
2739 		u_int32_t *vvv;
2740 		if (sopt->sopt_size < sizeof(u_int32_t)) {
2741 			error = EINVAL;
2742 			break;
2743 		}
2744 		SCTP_INP_WLOCK(inp);
2745 		vvv = sopt->sopt_data;
2746 		inp->sctp_ep.initial_sequence_debug = *vvv;
2747 		SCTP_INP_WUNLOCK(inp);
2748 	}
2749 	break;
2750 	case SCTP_DEFAULT_SEND_PARAM:
2751 	{
2752 		struct sctp_sndrcvinfo *s_info;
2753 
2754 		if (sopt->sopt_size != sizeof(struct sctp_sndrcvinfo)) {
2755 			error = EINVAL;
2756 			break;
2757 		}
2758 		s_info = sopt->sopt_data;
2759 
2760 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2761 			SCTP_INP_RLOCK(inp);
2762 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
2763 			if (stcb) {
2764 				SCTP_TCB_LOCK(stcb);
2765 			}
2766 			SCTP_INP_RUNLOCK(inp);
2767 		} else
2768 			stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
2769 
2770 		if (stcb == NULL) {
2771 			error = ENOENT;
2772 			break;
2773 		}
2774 		/* Validate things */
2775 		if (s_info->sinfo_stream > stcb->asoc.streamoutcnt) {
2776 			SCTP_TCB_UNLOCK(stcb);
2777 			error = EINVAL;
2778 			break;
2779 		}
2780 		/* Mask off the flags that are allowed */
2781 		s_info->sinfo_flags = (s_info->sinfo_flags &
2782 				       (SCTP_UNORDERED | SCTP_ADDR_OVER |
2783 					SCTP_PR_SCTP_TTL | SCTP_PR_SCTP_BUF));
2784 		/* Copy it in */
2785 		stcb->asoc.def_send = *s_info;
2786 		SCTP_TCB_UNLOCK(stcb);
2787 	}
2788 	break;
2789 	case SCTP_PEER_ADDR_PARAMS:
2790 	{
2791 		struct sctp_paddrparams *paddrp;
2792 		struct sctp_nets *net;
2793 		if (sopt->sopt_size < sizeof(struct sctp_paddrparams)) {
2794 			error = EINVAL;
2795 			break;
2796 		}
2797 		paddrp = sopt->sopt_data;
2798 		net = NULL;
2799 		if (paddrp->spp_assoc_id) {
2800 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2801 				SCTP_INP_RLOCK(inp);
2802 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
2803 				if (stcb) {
2804 					SCTP_TCB_LOCK(stcb);
2805 					net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2806 				}
2807 				SCTP_INP_RUNLOCK(inp);
2808 			} else
2809 				stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
2810 			if (stcb == NULL) {
2811 				error = ENOENT;
2812 				break;
2813 			}
2814 
2815 		}
2816 		if ((stcb == NULL) &&
2817 		    ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
2818 		     (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
2819 			/* Lookup via address */
2820 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2821 				SCTP_INP_RLOCK(inp);
2822 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
2823 				if (stcb) {
2824 					SCTP_TCB_LOCK(stcb);
2825 					net = sctp_findnet(stcb,
2826 							   (struct sockaddr *)&paddrp->spp_address);
2827 				}
2828 				SCTP_INP_RUNLOCK(inp);
2829 			} else {
2830 				SCTP_INP_WLOCK(inp);
2831 				SCTP_INP_INCR_REF(inp);
2832 				SCTP_INP_WUNLOCK(inp);
2833 				stcb = sctp_findassociation_ep_addr(&inp,
2834 								    (struct sockaddr *)&paddrp->spp_address,
2835 								    &net, NULL, NULL);
2836 				if (stcb == NULL) {
2837 					SCTP_INP_WLOCK(inp);
2838 					SCTP_INP_DECR_REF(inp);
2839 					SCTP_INP_WUNLOCK(inp);
2840 				}
2841 			}
2842 		} else {
2843 			/* Effects the Endpoint */
2844 			stcb = NULL;
2845 		}
2846 		if (stcb) {
2847 			/* Applies to the specific association */
2848 			if (paddrp->spp_pathmaxrxt) {
2849 				if (net) {
2850 					if (paddrp->spp_pathmaxrxt)
2851 						net->failure_threshold = paddrp->spp_pathmaxrxt;
2852 				} else {
2853 					if (paddrp->spp_pathmaxrxt)
2854 						stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
2855 				}
2856 			}
2857 			if ((paddrp->spp_hbinterval != 0) && (paddrp->spp_hbinterval != 0xffffffff)) {
2858 				/* Just a set */
2859 				int old;
2860 				if (net) {
2861 					net->dest_state &= ~SCTP_ADDR_NOHB;
2862 				} else {
2863 					old = stcb->asoc.heart_beat_delay;
2864 					stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
2865 					if (old == 0) {
2866 						/* Turn back on the timer */
2867 						sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
2868 					}
2869 				}
2870 			} else if (paddrp->spp_hbinterval == 0xffffffff) {
2871 				/* on demand HB */
2872 				sctp_send_hb(stcb, 1, net);
2873 			} else {
2874 				if (net == NULL) {
2875 					/* off on association */
2876 					if (stcb->asoc.heart_beat_delay) {
2877 						int cnt_of_unconf = 0;
2878 						struct sctp_nets *lnet;
2879 						TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
2880 							if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
2881 								cnt_of_unconf++;
2882 							}
2883 						}
2884 						/* stop the timer ONLY if we have no unconfirmed addresses
2885 						 */
2886 						if (cnt_of_unconf == 0)
2887 							sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
2888 					}
2889 					stcb->asoc.heart_beat_delay = 0;
2890 				} else {
2891 					net->dest_state |= SCTP_ADDR_NOHB;
2892 				}
2893 			}
2894 			SCTP_TCB_UNLOCK(stcb);
2895 		} else {
2896 			/* Use endpoint defaults */
2897 			SCTP_INP_WLOCK(inp);
2898 			if (paddrp->spp_pathmaxrxt)
2899 				inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
2900 			if (paddrp->spp_hbinterval != SCTP_ISSUE_HB)
2901 				inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = paddrp->spp_hbinterval;
2902 			SCTP_INP_WUNLOCK(inp);
2903 		}
2904 	}
2905 	break;
2906 	case SCTP_RTOINFO:
2907 	{
2908 		struct sctp_rtoinfo *srto;
2909 		if (sopt->sopt_size < sizeof(struct sctp_rtoinfo)) {
2910 			error = EINVAL;
2911 			break;
2912 		}
2913 		srto = sopt->sopt_data;
2914 		if (srto->srto_assoc_id == 0) {
2915 			SCTP_INP_WLOCK(inp);
2916 			/* If we have a null asoc, its default for the endpoint */
2917 			if (srto->srto_initial > 10)
2918 				inp->sctp_ep.initial_rto = srto->srto_initial;
2919 			if (srto->srto_max > 10)
2920 				inp->sctp_ep.sctp_maxrto = srto->srto_max;
2921 			if (srto->srto_min > 10)
2922 				inp->sctp_ep.sctp_minrto = srto->srto_min;
2923 			SCTP_INP_WUNLOCK(inp);
2924 			break;
2925 		}
2926 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2927 			SCTP_INP_RLOCK(inp);
2928 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
2929 			if (stcb) {
2930 				SCTP_TCB_LOCK(stcb);
2931 			}
2932 			SCTP_INP_RUNLOCK(inp);
2933 		} else
2934 			stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
2935 		if (stcb == NULL) {
2936 			error = EINVAL;
2937 			break;
2938 		}
2939 		/* Set in ms we hope :-) */
2940 		if (srto->srto_initial > 10)
2941 			stcb->asoc.initial_rto = srto->srto_initial;
2942 		if (srto->srto_max > 10)
2943 			stcb->asoc.maxrto = srto->srto_max;
2944 		if (srto->srto_min > 10)
2945 			stcb->asoc.minrto = srto->srto_min;
2946 		SCTP_TCB_UNLOCK(stcb);
2947 	}
2948 	break;
2949 	case SCTP_ASSOCINFO:
2950 	{
2951 		struct sctp_assocparams *sasoc;
2952 
2953 		if (sopt->sopt_size < sizeof(struct sctp_assocparams)) {
2954 			error = EINVAL;
2955 			break;
2956 		}
2957 		sasoc = sopt->sopt_data;
2958 		if (sasoc->sasoc_assoc_id) {
2959 			if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2960 				SCTP_INP_RLOCK(inp);
2961 				stcb = LIST_FIRST(&inp->sctp_asoc_list);
2962 				if (stcb) {
2963 					SCTP_TCB_LOCK(stcb);
2964 				}
2965 				SCTP_INP_RUNLOCK(inp);
2966 			} else
2967 				stcb = sctp_findassociation_ep_asocid(inp,
2968 								      sasoc->sasoc_assoc_id);
2969 			if (stcb == NULL) {
2970 				error = ENOENT;
2971 				break;
2972 			}
2973 
2974 		} else {
2975 			stcb = NULL;
2976 		}
2977 		if (stcb) {
2978 			if (sasoc->sasoc_asocmaxrxt)
2979 				stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
2980 			sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2981 			sasoc->sasoc_peer_rwnd = 0;
2982 			sasoc->sasoc_local_rwnd = 0;
2983 			if (stcb->asoc.cookie_life)
2984 				stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
2985 			SCTP_TCB_UNLOCK(stcb);
2986 		} else {
2987 			SCTP_INP_WLOCK(inp);
2988                         if (sasoc->sasoc_asocmaxrxt)
2989 				inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
2990 			sasoc->sasoc_number_peer_destinations = 0;
2991 			sasoc->sasoc_peer_rwnd = 0;
2992 			sasoc->sasoc_local_rwnd = 0;
2993 			if (sasoc->sasoc_cookie_life)
2994 				inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
2995 			SCTP_INP_WUNLOCK(inp);
2996 		}
2997 	}
2998 	break;
2999 	case SCTP_INITMSG:
3000 	{
3001                 struct sctp_initmsg *sinit;
3002 
3003 		if (sopt->sopt_size < sizeof(struct sctp_initmsg)) {
3004 			error = EINVAL;
3005 			break;
3006 		}
3007 		sinit = sopt->sopt_data;
3008 		SCTP_INP_WLOCK(inp);
3009 		if (sinit->sinit_num_ostreams)
3010 			inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3011 
3012 		if (sinit->sinit_max_instreams)
3013 			inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3014 
3015 		if (sinit->sinit_max_attempts)
3016 			inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3017 
3018 		if (sinit->sinit_max_init_timeo > 10)
3019 			/* We must be at least a 100ms (we set in ticks) */
3020 			inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3021 		SCTP_INP_WUNLOCK(inp);
3022 	}
3023 	break;
3024 	case SCTP_PRIMARY_ADDR:
3025 	{
3026 		struct sctp_setprim *spa;
3027 		struct sctp_nets *net, *lnet;
3028 		if (sopt->sopt_size < sizeof(struct sctp_setprim)) {
3029 			error = EINVAL;
3030 			break;
3031 		}
3032 		spa = sopt->sopt_data;
3033 
3034 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3035  			SCTP_INP_RLOCK(inp);
3036 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
3037 			if (stcb) {
3038 				SCTP_TCB_LOCK(stcb);
3039 			} else {
3040 				error = EINVAL;
3041 				break;
3042 			}
3043  			SCTP_INP_RUNLOCK(inp);
3044 		} else
3045 			stcb = sctp_findassociation_ep_asocid(inp, spa->ssp_assoc_id);
3046 		if (stcb == NULL) {
3047 			/* One last shot */
3048 			SCTP_INP_WLOCK(inp);
3049 			SCTP_INP_INCR_REF(inp);
3050 			SCTP_INP_WUNLOCK(inp);
3051 			stcb = sctp_findassociation_ep_addr(&inp,
3052 							    (struct sockaddr *)&spa->ssp_addr,
3053 							    &net, NULL, NULL);
3054 			if (stcb == NULL) {
3055 				SCTP_INP_WLOCK(inp);
3056 				SCTP_INP_DECR_REF(inp);
3057 				SCTP_INP_WUNLOCK(inp);
3058 				error = EINVAL;
3059 				break;
3060 			}
3061 		} else {
3062 			/* find the net, associd or connected lookup type */
3063 			net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3064 			if (net == NULL) {
3065 				SCTP_TCB_UNLOCK(stcb);
3066 				error = EINVAL;
3067 				break;
3068 			}
3069                 }
3070                 if ((net != stcb->asoc.primary_destination) &&
3071   		    (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3072 			/* Ok we need to set it */
3073 			lnet = stcb->asoc.primary_destination;
3074                         lnet->next_tsn_at_change = net->next_tsn_at_change = stcb->asoc.sending_seq;
3075 		        if (sctp_set_primary_addr(stcb,
3076 						  (struct sockaddr *)NULL,
3077 						  net) == 0) {
3078 			        if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3079    				        net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3080                                 }
3081                                 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3082                         }
3083 		}
3084 		SCTP_TCB_UNLOCK(stcb);
3085         }
3086 	break;
3087 
3088 	case SCTP_SET_PEER_PRIMARY_ADDR:
3089 	{
3090 		struct sctp_setpeerprim *sspp;
3091 		if (sopt->sopt_size < sizeof(struct sctp_setpeerprim)) {
3092 			error = EINVAL;
3093 			break;
3094 		}
3095 		sspp = sopt->sopt_data;
3096 
3097 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3098 			SCTP_INP_RLOCK(inp);
3099 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
3100 			if (stcb) {
3101 				SCTP_TCB_UNLOCK(stcb);
3102 			}
3103 			SCTP_INP_RUNLOCK(inp);
3104 		} else
3105 			stcb = sctp_findassociation_ep_asocid(inp, sspp->sspp_assoc_id);
3106 		if (stcb == NULL) {
3107 			error = EINVAL;
3108 			break;
3109 		}
3110 		if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3111 			error = EINVAL;
3112 		}
3113 		SCTP_TCB_UNLOCK(stcb);
3114 	}
3115 	break;
3116 	case SCTP_BINDX_ADD_ADDR:
3117 	{
3118 		struct sctp_getaddresses *addrs;
3119 		struct sockaddr *addr_touse;
3120 		struct sockaddr_in sin;
3121 		/* see if we're bound all already! */
3122 		if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3123 			error = EINVAL;
3124 			break;
3125 		}
3126 		if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
3127 			error = EINVAL;
3128 			break;
3129 		}
3130 		addrs = sopt->sopt_data;
3131 		addr_touse = addrs->addr;
3132 		if (addrs->addr->sa_family == AF_INET6) {
3133 			struct sockaddr_in6 *sin6;
3134 			sin6 = (struct sockaddr_in6 *)addr_touse;
3135 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3136 				in6_sin6_2_sin(&sin, sin6);
3137 				addr_touse = (struct sockaddr *)&sin;
3138 			}
3139 		}
3140 		if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3141 			error = sctp_inpcb_bind(so, addr_touse, curlwp);
3142 			break;
3143 		}
3144 		/* No locks required here since bind and mgmt_ep_sa all
3145 		 * do their own locking. If we do something for the FIX:
3146 		 * below we may need to lock in that case.
3147 		 */
3148 		if (addrs->sget_assoc_id == 0) {
3149 			/* add the address */
3150 			struct sctp_inpcb  *lep;
3151 			((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3152 			lep = sctp_pcb_findep(addr_touse, 1, 0);
3153 			if (lep != NULL) {
3154 				/* We must decrement the refcount
3155 				 * since we have the ep already and
3156 				 * are binding. No remove going on
3157 				 * here.
3158 				 */
3159 				SCTP_INP_WLOCK(inp);
3160 				SCTP_INP_DECR_REF(inp);
3161 				SCTP_INP_WUNLOCK(inp);
3162 			}
3163 			if (lep == inp) {
3164 				/* already bound to it.. ok */
3165 				break;
3166 			} else if (lep == NULL) {
3167 				((struct sockaddr_in *)addr_touse)->sin_port = 0;
3168 				error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3169 							     SCTP_ADD_IP_ADDRESS);
3170 			} else {
3171 				error = EADDRNOTAVAIL;
3172 			}
3173 			if (error)
3174 				break;
3175 
3176 		} else {
3177 			/* FIX: decide whether we allow assoc based bindx */
3178 		}
3179 	}
3180 	break;
3181 	case SCTP_BINDX_REM_ADDR:
3182 	{
3183 		struct sctp_getaddresses *addrs;
3184 		struct sockaddr *addr_touse;
3185 		struct sockaddr_in sin;
3186 		/* see if we're bound all already! */
3187 		if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3188 			error = EINVAL;
3189 			break;
3190 		}
3191 		if (sopt->sopt_size < sizeof(struct sctp_getaddresses)) {
3192 			error = EINVAL;
3193 			break;
3194 		}
3195 		addrs = sopt->sopt_data;
3196 		addr_touse = addrs->addr;
3197 		if (addrs->addr->sa_family == AF_INET6) {
3198 			struct sockaddr_in6 *sin6;
3199 			sin6 = (struct sockaddr_in6 *)addr_touse;
3200 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3201 				in6_sin6_2_sin(&sin, sin6);
3202 				addr_touse = (struct sockaddr *)&sin;
3203 			}
3204 		}
3205                 /* No lock required mgmt_ep_sa does its own locking. If
3206 		 * the FIX: below is ever changed we may need to
3207 		 * lock before calling association level binding.
3208 		 */
3209 		if (addrs->sget_assoc_id == 0) {
3210 			/* delete the address */
3211 			sctp_addr_mgmt_ep_sa(inp, addr_touse,
3212 					     SCTP_DEL_IP_ADDRESS);
3213 		} else {
3214 			/* FIX: decide whether we allow assoc based bindx */
3215 		}
3216 	}
3217 	break;
3218 	default:
3219 		error = ENOPROTOOPT;
3220 		break;
3221 	} /* end switch (opt) */
3222 	return (error);
3223 }
3224 
3225 int
3226 sctp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
3227 {
3228 	int s, error = 0;
3229 	struct inpcb *inp;
3230 #ifdef INET6
3231 	struct in6pcb *in6p;
3232 #endif
3233 	int family;	/* family of the socket */
3234 
3235 	family = so->so_proto->pr_domain->dom_family;
3236 
3237 	s = splsoftnet();
3238 	switch (family) {
3239 	case PF_INET:
3240 		inp = sotoinpcb(so);
3241 #ifdef INET6
3242 		in6p = NULL;
3243 #endif
3244 		break;
3245 #ifdef INET6
3246 	case PF_INET6:
3247 		inp = NULL;
3248 		in6p = sotoin6pcb(so);
3249 		break;
3250 #endif
3251 	default:
3252 		splx(s);
3253 		return EAFNOSUPPORT;
3254 	}
3255 #ifndef INET6
3256 	if (inp == NULL)
3257 #else
3258 	if (inp == NULL && in6p == NULL)
3259 #endif
3260 	{
3261 		splx(s);
3262 		return (ECONNRESET);
3263 	}
3264 	if (sopt->sopt_level != IPPROTO_SCTP) {
3265 		switch (family) {
3266 		case PF_INET:
3267 			error = ip_ctloutput(op, so, sopt);
3268 			break;
3269 #ifdef INET6
3270 		case PF_INET6:
3271 			error = ip6_ctloutput(op, so, sopt);
3272 			break;
3273 #endif
3274 		}
3275 		splx(s);
3276 		return (error);
3277 	}
3278 	/* Ok if we reach here it is a SCTP option we hope */
3279 	if (op == PRCO_SETOPT) {
3280 		error = sctp_optsset(so, sopt);
3281 	} else if (op ==  PRCO_GETOPT) {
3282 		error = sctp_optsget(so, sopt);
3283 	} else {
3284 		error = EINVAL;
3285 	}
3286 	splx(s);
3287 	return (error);
3288 }
3289 
3290 static int
3291 sctp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
3292 {
3293 	int error = 0;
3294 	struct sctp_inpcb *inp;
3295 	struct sctp_tcb *stcb;
3296 
3297 	KASSERT(solocked(so));
3298 #ifdef SCTP_DEBUG
3299 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3300 		printf("Connect called in SCTP to ");
3301 		sctp_print_address(nam);
3302 		printf("Port %d\n", ntohs(((struct sockaddr_in *)nam)->sin_port));
3303 	}
3304 #endif /* SCTP_DEBUG */
3305 	inp = (struct sctp_inpcb *)so->so_pcb;
3306 	if (inp == 0) {
3307 		/* I made the same as TCP since we are not setup? */
3308 		return (ECONNRESET);
3309 	}
3310 	SCTP_ASOC_CREATE_LOCK(inp);
3311 #ifdef SCTP_DEBUG
3312 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3313 		printf("After ASOC lock\n");
3314 	}
3315 #endif /* SCTP_DEBUG */
3316 	SCTP_INP_WLOCK(inp);
3317 #ifdef SCTP_DEBUG
3318 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3319 		printf("After INP_WLOCK lock\n");
3320 	}
3321 #endif /* SCTP_DEBUG */
3322 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3323 	    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3324 		/* Should I really unlock ? */
3325 		SCTP_INP_WUNLOCK(inp);
3326 		SCTP_ASOC_CREATE_UNLOCK(inp);
3327 		return (EFAULT);
3328 	}
3329 #ifdef INET6
3330 	if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3331 	    (nam->sa_family == AF_INET6)) {
3332 		SCTP_INP_WUNLOCK(inp);
3333 		SCTP_ASOC_CREATE_UNLOCK(inp);
3334 		return (EINVAL);
3335 	}
3336 #endif /* INET6 */
3337 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3338 	    SCTP_PCB_FLAGS_UNBOUND) {
3339 		/* Bind a ephemeral port */
3340 		SCTP_INP_WUNLOCK(inp);
3341 		error = sctp_inpcb_bind(so, NULL, l);
3342 		if (error) {
3343 			SCTP_ASOC_CREATE_UNLOCK(inp);
3344 			return (error);
3345 		}
3346 		SCTP_INP_WLOCK(inp);
3347 	}
3348 #ifdef SCTP_DEBUG
3349 	if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3350 		printf("After bind\n");
3351 	}
3352 #endif /* SCTP_DEBUG */
3353 	/* Now do we connect? */
3354 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3355 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3356 		/* We are already connected AND the TCP model */
3357 		SCTP_INP_WUNLOCK(inp);
3358 		SCTP_ASOC_CREATE_UNLOCK(inp);
3359 		return (EADDRINUSE);
3360 	}
3361 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3362 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
3363 		if (stcb) {
3364 			SCTP_TCB_UNLOCK(stcb);
3365 		}
3366 		SCTP_INP_WUNLOCK(inp);
3367 	} else {
3368 		SCTP_INP_INCR_REF(inp);
3369 		SCTP_INP_WUNLOCK(inp);
3370 		stcb = sctp_findassociation_ep_addr(&inp, nam, NULL, NULL, NULL);
3371 		if (stcb == NULL) {
3372 			SCTP_INP_WLOCK(inp);
3373 			SCTP_INP_DECR_REF(inp);
3374 			SCTP_INP_WUNLOCK(inp);
3375 		}
3376 	}
3377 	if (stcb != NULL) {
3378 		/* Already have or am bring up an association */
3379 		SCTP_ASOC_CREATE_UNLOCK(inp);
3380 		SCTP_TCB_UNLOCK(stcb);
3381 		return (EALREADY);
3382 	}
3383 	/* We are GOOD to go */
3384 	stcb = sctp_aloc_assoc(inp, nam, 1, &error, 0);
3385 	if (stcb == NULL) {
3386 		/* Gak! no memory */
3387 		return (error);
3388 	}
3389 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3390 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3391 		/* Set the connected flag so we can queue data */
3392 		soisconnecting(so);
3393 	}
3394 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3395 	SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3396 	sctp_send_initiate(inp, stcb);
3397 	SCTP_ASOC_CREATE_UNLOCK(inp);
3398 	SCTP_TCB_UNLOCK(stcb);
3399 	return error;
3400 }
3401 
3402 static int
3403 sctp_connect2(struct socket *so, struct socket *so2)
3404 {
3405 	KASSERT(solocked(so));
3406 
3407 	return EOPNOTSUPP;
3408 }
3409 
3410 int
3411 sctp_rcvd(struct socket *so, int flags, struct lwp *l)
3412 {
3413 	struct sctp_socket_q_list *sq=NULL;
3414 	/*
3415 	 * The user has received some data, we may be able to stuff more
3416 	 * up the socket. And we need to possibly update the rwnd.
3417 	 */
3418 	struct sctp_inpcb *inp;
3419 	struct sctp_tcb *stcb=NULL;
3420 
3421 	inp = (struct sctp_inpcb *)so->so_pcb;
3422 #ifdef SCTP_DEBUG
3423 	if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3424 		printf("Read for so:%p inp:%p Flags:%x\n",
3425 		       so, inp, flags);
3426 #endif
3427 
3428 	if (inp == 0) {
3429 		/* I made the same as TCP since we are not setup? */
3430 #ifdef SCTP_DEBUG
3431 		if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3432 			printf("Nope, connection reset\n");
3433 #endif
3434 		return (ECONNRESET);
3435 	}
3436 	/*
3437 	 * Grab the first one on the list. It will re-insert itself if
3438 	 * it runs out of room
3439 	 */
3440 	SCTP_INP_WLOCK(inp);
3441 	if ((flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3442 	    && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3443 		/* Ok the other part of our grubby tracking
3444 		 * stuff for our horrible layer violation that
3445 		 * the tsvwg thinks is ok for sctp_peeloff.. gak!
3446 		 * We must update the next vtag pending on the
3447 		 * socket buffer (if any).
3448 		 */
3449 		inp->sctp_vtag_first = sctp_get_first_vtag_from_sb(so);
3450 		sq = TAILQ_FIRST(&inp->sctp_queue_list);
3451 		if (sq) {
3452 			stcb = sq->tcb;
3453 		} else {
3454 			stcb = NULL;
3455 		}
3456 	} else {
3457 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
3458 	}
3459 	if (stcb) {
3460 		SCTP_TCB_LOCK(stcb);
3461 	}
3462 	if (stcb) {
3463 		long incr;
3464 		/* all code in normal stcb path assumes
3465 		 * that you have a tcb_lock only. Thus
3466 		 * we must release the inp write lock.
3467 		 */
3468 		if (flags & MSG_EOR) {
3469 			if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3470 			   && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3471 				stcb = sctp_remove_from_socket_q(inp);
3472 			}
3473 #ifdef SCTP_DEBUG
3474 			if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3475 				printf("remove from socket queue for inp:%p tcbret:%p\n",
3476 				       inp, stcb);
3477 #endif
3478 
3479  			stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
3480  									  sizeof(struct mbuf));
3481 			if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) {
3482  				stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
3483  										  CMSG_LEN(sizeof(struct sctp_sndrcvinfo)));
3484 			}
3485 		}
3486 		if ((TAILQ_EMPTY(&stcb->asoc.delivery_queue) == 0) ||
3487 		    (TAILQ_EMPTY(&stcb->asoc.reasmqueue) == 0)) {
3488 			/* Deliver if there is something to be delivered */
3489 			sctp_service_queues(stcb, &stcb->asoc, 1);
3490 		}
3491 		sctp_set_rwnd(stcb, &stcb->asoc);
3492 		/* if we increase by 1 or more MTU's (smallest MTUs of all
3493 		 * nets) we send a window update sack
3494 		 */
3495 		incr = stcb->asoc.my_rwnd - stcb->asoc.my_last_reported_rwnd;
3496 		if (incr < 0) {
3497 			incr = 0;
3498 		}
3499 		if (((uint32_t)incr >= (stcb->asoc.smallest_mtu * SCTP_SEG_TO_RWND_UPD)) ||
3500 		    ((((uint32_t)incr)*SCTP_SCALE_OF_RWND_TO_UPD) >= so->so_rcv.sb_hiwat)) {
3501 			if (callout_pending(&stcb->asoc.dack_timer.timer)) {
3502 				/* If the timer is up, stop it */
3503 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
3504 						stcb->sctp_ep, stcb, NULL);
3505 			}
3506 			/* Send the sack, with the new rwnd */
3507 			sctp_send_sack(stcb);
3508 			/* Now do the output */
3509 			sctp_chunk_output(inp, stcb, 10);
3510 		}
3511 	} else {
3512 		if ((( sq ) && (flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0))
3513 		    && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3514 			stcb = sctp_remove_from_socket_q(inp);
3515 		}
3516 	}
3517 	if ((so->so_rcv.sb_mb == NULL) &&
3518 	    (TAILQ_EMPTY(&inp->sctp_queue_list) == 0)) {
3519 		int sq_cnt=0;
3520 #ifdef SCTP_DEBUG
3521 		if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3522 			printf("Something off, inp:%p so->so_rcv->sb_mb is empty and sockq is not.. cleaning\n",
3523 			       inp);
3524 #endif
3525 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3526 		   && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3527 			int done_yet;
3528 			done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
3529 			while (!done_yet) {
3530 				sq_cnt++;
3531 				(void)sctp_remove_from_socket_q(inp);
3532 				done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
3533 			}
3534 		}
3535 #ifdef SCTP_DEBUG
3536 		if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3537 			printf("Cleaned up %d sockq's\n", sq_cnt);
3538 #endif
3539 	}
3540 	if (stcb) {
3541 		SCTP_TCB_UNLOCK(stcb);
3542 	}
3543 	SCTP_INP_WUNLOCK(inp);
3544 	return (0);
3545 }
3546 
3547 int
3548 sctp_listen(struct socket *so, struct lwp *l)
3549 {
3550 	/*
3551 	 * Note this module depends on the protocol processing being
3552 	 * called AFTER any socket level flags and backlog are applied
3553 	 * to the socket. The traditional way that the socket flags are
3554 	 * applied is AFTER protocol processing. We have made a change
3555 	 * to the sys/kern/uipc_socket.c module to reverse this but this
3556 	 * MUST be in place if the socket API for SCTP is to work properly.
3557 	 */
3558 	int error = 0;
3559 	struct sctp_inpcb *inp;
3560 
3561 	inp = (struct sctp_inpcb *)so->so_pcb;
3562 	if (inp == 0) {
3563 		/* I made the same as TCP since we are not setup? */
3564 		return (ECONNRESET);
3565 	}
3566 	SCTP_INP_RLOCK(inp);
3567 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3568 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3569 		/* We are already connected AND the TCP model */
3570 		SCTP_INP_RUNLOCK(inp);
3571 		return (EADDRINUSE);
3572 	}
3573 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3574 		/* We must do a bind. */
3575 		SCTP_INP_RUNLOCK(inp);
3576 		if ((error = sctp_inpcb_bind(so, NULL, l))) {
3577 			/* bind error, probably perm */
3578 			return (error);
3579 		}
3580 	} else {
3581 		SCTP_INP_RUNLOCK(inp);
3582 	}
3583 	SCTP_INP_WLOCK(inp);
3584 	if (inp->sctp_socket->so_qlimit) {
3585 		if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3586 			/*
3587 			 * For the UDP model we must TURN OFF the ACCEPT
3588 			 * flags since we do NOT allow the accept() call.
3589 			 * The TCP model (when present) will do accept which
3590 			 * then prohibits connect().
3591 			 */
3592 			inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
3593 		}
3594 		inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
3595 	} else {
3596 		if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
3597 			/*
3598 			 * Turning off the listen flags if the backlog is
3599 			 * set to 0 (i.e. qlimit is 0).
3600 			 */
3601 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING;
3602 		}
3603 		inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
3604 	}
3605 	SCTP_INP_WUNLOCK(inp);
3606 	return (error);
3607 }
3608 
3609 int
3610 sctp_accept(struct socket *so, struct sockaddr *nam)
3611 {
3612 	struct sctp_tcb *stcb;
3613 	const struct sockaddr *prim;
3614 	struct sctp_inpcb *inp;
3615 	int error;
3616 
3617 	if (nam == NULL) {
3618 		return EINVAL;
3619 	}
3620 	inp = (struct sctp_inpcb *)so->so_pcb;
3621 
3622 	if (inp == 0) {
3623 		return ECONNRESET;
3624 	}
3625 	SCTP_INP_RLOCK(inp);
3626 	if (so->so_state & SS_ISDISCONNECTED) {
3627 		SCTP_INP_RUNLOCK(inp);
3628 		return ECONNABORTED;
3629 	}
3630 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
3631 	if (stcb == NULL) {
3632 		SCTP_INP_RUNLOCK(inp);
3633 		return ECONNRESET;
3634 	}
3635 	SCTP_TCB_LOCK(stcb);
3636 	SCTP_INP_RUNLOCK(inp);
3637 	prim = (const struct sockaddr *)rtcache_getdst(&stcb->asoc.primary_destination->ro);
3638 	if (prim->sa_family == AF_INET) {
3639 		struct sockaddr_in *sin;
3640 
3641 		sin = (struct sockaddr_in *)nam;
3642 		memset((void *)sin, 0, sizeof (*sin));
3643 
3644 		sin->sin_family = AF_INET;
3645 		sin->sin_len = sizeof(*sin);
3646 		sin->sin_port = ((const struct sockaddr_in *)prim)->sin_port;
3647 		sin->sin_addr = ((const struct sockaddr_in *)prim)->sin_addr;
3648 	} else {
3649 		struct sockaddr_in6 *sin6;
3650 
3651 		sin6 = (struct sockaddr_in6 *)nam;
3652 		memset((void *)sin6, 0, sizeof (*sin6));
3653 		sin6->sin6_family = AF_INET6;
3654 		sin6->sin6_len = sizeof(*sin6);
3655 		sin6->sin6_port = ((const struct sockaddr_in6 *)prim)->sin6_port;
3656 
3657 		sin6->sin6_addr = ((const struct sockaddr_in6 *)prim)->sin6_addr;
3658 		if ((error = sa6_recoverscope(sin6)) != 0)
3659 			return error;
3660 
3661 	}
3662 	/* Wake any delayed sleep action */
3663 	SCTP_TCB_UNLOCK(stcb);
3664 	SCTP_INP_WLOCK(inp);
3665 	if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
3666 		inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
3667 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
3668 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3669 			if (sowritable(inp->sctp_socket))
3670 				sowwakeup(inp->sctp_socket);
3671 		}
3672 		if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
3673 			inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3674 			if (soreadable(inp->sctp_socket))
3675 				sorwakeup(inp->sctp_socket);
3676 		}
3677 
3678 	}
3679 	SCTP_INP_WUNLOCK(inp);
3680 	return 0;
3681 }
3682 
3683 static int
3684 sctp_stat(struct socket *so, struct stat *ub)
3685 {
3686 	return 0;
3687 }
3688 
3689 int
3690 sctp_sockaddr(struct socket *so, struct sockaddr *nam)
3691 {
3692 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
3693 	struct sctp_inpcb *inp;
3694 
3695 	memset(sin, 0, sizeof(*sin));
3696 	sin->sin_family = AF_INET;
3697 	sin->sin_len = sizeof(*sin);
3698 	inp = (struct sctp_inpcb *)so->so_pcb;
3699 	if (!inp) {
3700 		return ECONNRESET;
3701 	}
3702 	SCTP_INP_RLOCK(inp);
3703 	sin->sin_port = inp->sctp_lport;
3704 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3705 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3706 			struct sctp_tcb *stcb;
3707 			const struct sockaddr_in *sin_a;
3708 			struct sctp_nets *net;
3709 			int fnd;
3710 
3711 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
3712 			if (stcb == NULL) {
3713 				goto notConn;
3714 			}
3715 			fnd = 0;
3716 			sin_a = NULL;
3717 			SCTP_TCB_LOCK(stcb);
3718 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3719 				sin_a = (const struct sockaddr_in *)rtcache_getdst(&net->ro);
3720 				if (sin_a->sin_family == AF_INET) {
3721 					fnd = 1;
3722 					break;
3723 				}
3724 			}
3725 			if ((!fnd) || (sin_a == NULL)) {
3726 				/* punt */
3727 				SCTP_TCB_UNLOCK(stcb);
3728 				goto notConn;
3729 			}
3730 			sin->sin_addr = sctp_ipv4_source_address_selection(inp,
3731 			    stcb, (struct route *)&net->ro, net, 0);
3732 			SCTP_TCB_UNLOCK(stcb);
3733 		} else {
3734 			/* For the bound all case you get back 0 */
3735 		notConn:
3736 			sin->sin_addr.s_addr = 0;
3737 		}
3738 
3739 	} else {
3740 		/* Take the first IPv4 address in the list */
3741 		struct sctp_laddr *laddr;
3742 		int fnd = 0;
3743 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3744 			if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
3745 				struct sockaddr_in *sin_a;
3746 				sin_a = (struct sockaddr_in *)laddr->ifa->ifa_addr;
3747 				sin->sin_addr = sin_a->sin_addr;
3748 				fnd = 1;
3749 				break;
3750 			}
3751 		}
3752 		if (!fnd) {
3753 			SCTP_INP_RUNLOCK(inp);
3754 			return ENOENT;
3755 		}
3756 	}
3757 	SCTP_INP_RUNLOCK(inp);
3758 	return (0);
3759 }
3760 
3761 int
3762 sctp_peeraddr(struct socket *so, struct sockaddr *nam)
3763 {
3764 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
3765 	int fnd;
3766 	const struct sockaddr_in *sin_a;
3767 	struct sctp_inpcb *inp;
3768 	struct sctp_tcb *stcb;
3769 	struct sctp_nets *net;
3770 
3771 	/* Do the malloc first in case it blocks. */
3772 	inp = (struct sctp_inpcb *)so->so_pcb;
3773 	if ((inp == NULL) ||
3774 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3775 		/* UDP type and listeners will drop out here */
3776 		return (ENOTCONN);
3777 	}
3778 
3779 	memset(sin, 0, sizeof(*sin));
3780 	sin->sin_family = AF_INET;
3781 	sin->sin_len = sizeof(*sin);
3782 
3783 	/* We must recapture incase we blocked */
3784 	inp = (struct sctp_inpcb *)so->so_pcb;
3785 	if (!inp) {
3786 		return ECONNRESET;
3787 	}
3788 	SCTP_INP_RLOCK(inp);
3789 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
3790 	if (stcb) {
3791 		SCTP_TCB_LOCK(stcb);
3792 	}
3793 	SCTP_INP_RUNLOCK(inp);
3794 	if (stcb == NULL) {
3795 		return ECONNRESET;
3796 	}
3797 	fnd = 0;
3798 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3799 		sin_a = (const struct sockaddr_in *)rtcache_getdst(&net->ro);
3800 		if (sin_a->sin_family == AF_INET) {
3801 			fnd = 1;
3802 			sin->sin_port = stcb->rport;
3803 			sin->sin_addr = sin_a->sin_addr;
3804 			break;
3805 		}
3806 	}
3807 	SCTP_TCB_UNLOCK(stcb);
3808 	if (!fnd) {
3809 		/* No IPv4 address */
3810 		return ENOENT;
3811 	}
3812 	return (0);
3813 }
3814 
3815 static int
3816 sctp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
3817 {
3818 	KASSERT(solocked(so));
3819 
3820 	if (m)
3821 		m_freem(m);
3822 	if (control)
3823 		m_freem(control);
3824 
3825 	return EOPNOTSUPP;
3826 }
3827 
3828 static int
3829 sctp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
3830 {
3831 	int error = 0;
3832 	int family;
3833 
3834 	family = so->so_proto->pr_domain->dom_family;
3835 	switch (family) {
3836 #ifdef INET
3837 	case PF_INET:
3838 		error = in_control(so, cmd, nam, ifp);
3839 		break;
3840 #endif
3841 #ifdef INET6
3842 	case PF_INET6:
3843 		error = in6_control(so, cmd, nam, ifp);
3844 		break;
3845 #endif
3846 	default:
3847 		error =  EAFNOSUPPORT;
3848 	}
3849 	return (error);
3850 }
3851 
3852 static int
3853 sctp_purgeif(struct socket *so, struct ifnet *ifp)
3854 {
3855 	struct ifaddr *ifa;
3856 	IFADDR_READER_FOREACH(ifa, ifp) {
3857 		if (ifa->ifa_addr->sa_family == PF_INET) {
3858 			sctp_delete_ip_address(ifa);
3859 		}
3860 	}
3861 
3862 	mutex_enter(softnet_lock);
3863 	in_purgeif(ifp);
3864 	mutex_exit(softnet_lock);
3865 
3866 	return 0;
3867 }
3868 
3869 /*
3870  * Sysctl for sctp variables.
3871  */
3872 SYSCTL_SETUP(sysctl_net_inet_sctp_setup, "sysctl net.inet.sctp subtree setup")
3873 {
3874 
3875 	sysctl_createv(clog, 0, NULL, NULL,
3876 		       CTLFLAG_PERMANENT,
3877 	               CTLTYPE_NODE, "net", NULL,
3878                        NULL, 0, NULL, 0,
3879                        CTL_NET, CTL_EOL);
3880         sysctl_createv(clog, 0, NULL, NULL,
3881                        CTLFLAG_PERMANENT,
3882                        CTLTYPE_NODE, "inet", NULL,
3883                        NULL, 0, NULL, 0,
3884                        CTL_NET, PF_INET, CTL_EOL);
3885         sysctl_createv(clog, 0, NULL, NULL,
3886                        CTLFLAG_PERMANENT,
3887                        CTLTYPE_NODE, "sctp",
3888                        SYSCTL_DESCR("sctp related settings"),
3889                        NULL, 0, NULL, 0,
3890                        CTL_NET, PF_INET, IPPROTO_SCTP, CTL_EOL);
3891 
3892        sysctl_createv(clog, 0, NULL, NULL,
3893                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3894                        CTLTYPE_INT, "maxdgram",
3895                        SYSCTL_DESCR("Maximum outgoing SCTP buffer size"),
3896                        NULL, 0, &sctp_sendspace, 0,
3897                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXDGRAM,
3898                        CTL_EOL);
3899 
3900        sysctl_createv(clog, 0, NULL, NULL,
3901                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3902                        CTLTYPE_INT, "recvspace",
3903                        SYSCTL_DESCR("Maximum incoming SCTP buffer size"),
3904                        NULL, 0, &sctp_recvspace, 0,
3905                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_RECVSPACE,
3906                        CTL_EOL);
3907 
3908        sysctl_createv(clog, 0, NULL, NULL,
3909                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3910                        CTLTYPE_INT, "autoasconf",
3911                        SYSCTL_DESCR("Enable SCTP Auto-ASCONF"),
3912                        NULL, 0, &sctp_auto_asconf, 0,
3913                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_AUTOASCONF,
3914                        CTL_EOL);
3915 
3916        sysctl_createv(clog, 0, NULL, NULL,
3917                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3918                        CTLTYPE_INT, "ecn_enable",
3919                        SYSCTL_DESCR("Enable SCTP ECN"),
3920                        NULL, 0, &sctp_ecn, 0,
3921                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_ENABLE,
3922                        CTL_EOL);
3923 
3924        sysctl_createv(clog, 0, NULL, NULL,
3925                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3926                        CTLTYPE_INT, "ecn_nonce",
3927                        SYSCTL_DESCR("Enable SCTP ECN Nonce"),
3928                        NULL, 0, &sctp_ecn_nonce, 0,
3929                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_NONCE,
3930                        CTL_EOL);
3931 
3932        sysctl_createv(clog, 0, NULL, NULL,
3933                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3934                        CTLTYPE_INT, "strict_sack",
3935                        SYSCTL_DESCR("Enable SCTP Strict SACK checking"),
3936                        NULL, 0, &sctp_strict_sacks, 0,
3937                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_SACK,
3938                        CTL_EOL);
3939 
3940        sysctl_createv(clog, 0, NULL, NULL,
3941                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3942                        CTLTYPE_INT, "loopback_nocsum",
3943                        SYSCTL_DESCR("Enable NO Csum on packets sent on loopback"),
3944                        NULL, 0, &sctp_no_csum_on_loopback, 0,
3945                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_NOCSUM_LO,
3946                        CTL_EOL);
3947 
3948        sysctl_createv(clog, 0, NULL, NULL,
3949                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3950                        CTLTYPE_INT, "strict_init",
3951                        SYSCTL_DESCR("Enable strict INIT/INIT-ACK singleton enforcement"),
3952                        NULL, 0, &sctp_strict_init, 0,
3953                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_INIT,
3954                        CTL_EOL);
3955 
3956        sysctl_createv(clog, 0, NULL, NULL,
3957                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3958                        CTLTYPE_INT, "peer_chkoh",
3959                        SYSCTL_DESCR("Amount to debit peers rwnd per chunk sent"),
3960                        NULL, 0, &sctp_peer_chunk_oh, 0,
3961                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_PEER_CHK_OH,
3962                        CTL_EOL);
3963 
3964        sysctl_createv(clog, 0, NULL, NULL,
3965                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3966                        CTLTYPE_INT, "maxburst",
3967                        SYSCTL_DESCR("Default max burst for sctp endpoints"),
3968                        NULL, 0, &sctp_max_burst_default, 0,
3969                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXBURST,
3970                        CTL_EOL);
3971 
3972        sysctl_createv(clog, 0, NULL, NULL,
3973                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3974                        CTLTYPE_INT, "maxchunks",
3975                        SYSCTL_DESCR("Default max chunks on queue per asoc"),
3976                        NULL, 0, &sctp_max_chunks_on_queue, 0,
3977                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXCHUNKONQ,
3978                        CTL_EOL);
3979 #ifdef SCTP_DEBUG
3980        sysctl_createv(clog, 0, NULL, NULL,
3981                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
3982                        CTLTYPE_INT, "debug",
3983                        SYSCTL_DESCR("Configure debug output"),
3984                        NULL, 0, &sctp_debug_on, 0,
3985                        CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_DEBUG,
3986                        CTL_EOL);
3987 #endif
3988 }
3989 
3990 PR_WRAP_USRREQS(sctp)
3991 #define	sctp_attach	sctp_attach_wrapper
3992 #define	sctp_detach	sctp_detach_wrapper
3993 #define sctp_accept	sctp_accept_wrapper
3994 #define sctp_bind	sctp_bind_wrapper
3995 #define sctp_listen	sctp_listen_wrapper
3996 #define sctp_connect	sctp_connect_wrapper
3997 #define sctp_connect2	sctp_connect2_wrapper
3998 #define sctp_disconnect	sctp_disconnect_wrapper
3999 #define sctp_shutdown	sctp_shutdown_wrapper
4000 #define sctp_abort	sctp_abort_wrapper
4001 #define	sctp_ioctl	sctp_ioctl_wrapper
4002 #define	sctp_stat	sctp_stat_wrapper
4003 #define sctp_peeraddr	sctp_peeraddr_wrapper
4004 #define sctp_sockaddr	sctp_sockaddr_wrapper
4005 #define sctp_rcvd	sctp_rcvd_wrapper
4006 #define sctp_recvoob	sctp_recvoob_wrapper
4007 #define sctp_send	sctp_send_wrapper
4008 #define sctp_sendoob	sctp_sendoob_wrapper
4009 #define sctp_purgeif	sctp_purgeif_wrapper
4010 
4011 const struct pr_usrreqs sctp_usrreqs = {
4012 	.pr_attach	= sctp_attach,
4013 	.pr_detach	= sctp_detach,
4014 	.pr_accept	= sctp_accept,
4015 	.pr_bind	= sctp_bind,
4016 	.pr_listen	= sctp_listen,
4017 	.pr_connect	= sctp_connect,
4018 	.pr_connect2	= sctp_connect2,
4019 	.pr_disconnect	= sctp_disconnect,
4020 	.pr_shutdown	= sctp_shutdown,
4021 	.pr_abort	= sctp_abort,
4022 	.pr_ioctl	= sctp_ioctl,
4023 	.pr_stat	= sctp_stat,
4024 	.pr_peeraddr	= sctp_peeraddr,
4025 	.pr_sockaddr	= sctp_sockaddr,
4026 	.pr_rcvd	= sctp_rcvd,
4027 	.pr_recvoob	= sctp_recvoob,
4028 	.pr_send	= sctp_send,
4029 	.pr_sendoob	= sctp_sendoob,
4030 	.pr_purgeif	= sctp_purgeif,
4031 };
4032