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