sctp_output.c revision 1.2 1 /* $NetBSD: sctp_output.c,v 1.2 2016/04/03 09:57:40 mlelstv Exp $ */
2 /* $KAME: sctp_output.c,v 1.48 2005/06/16 18:29:24 jinmei Exp $ */
3
4 /*
5 * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sctp_output.c,v 1.2 2016/04/03 09:57:40 mlelstv Exp $");
34
35 #ifdef _KERNEL_OPT
36 #include "opt_ipsec.h"
37 #include "opt_inet.h"
38 #include "opt_sctp.h"
39 #endif /* _KERNEL_OPT */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/proc.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/resourcevar.h>
53 #include <sys/uio.h>
54 #ifdef INET6
55 #include <sys/domain.h>
56 #endif
57
58 #include <machine/limits.h>
59 #include <machine/cpu.h>
60
61 #include <net/if.h>
62 #include <net/if_types.h>
63
64 #include <net/route.h>
65
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/ip.h>
69 #include <netinet/in_pcb.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip_var.h>
72
73 #ifdef INET6
74 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
76 #include <netinet6/scope6_var.h>
77 #include <netinet6/nd6.h>
78
79 #include <netinet6/in6_pcb.h>
80
81 #include <netinet/icmp6.h>
82
83 #endif /* INET6 */
84
85 #include <net/net_osdep.h>
86
87 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
88 #ifndef in6pcb
89 #define in6pcb inpcb
90 #endif
91 #endif
92
93 #include <netinet/sctp_pcb.h>
94
95 #ifdef IPSEC
96 #include <netinet6/ipsec.h>
97 #include <netkey/key.h>
98 #endif /* IPSEC */
99
100 #include <netinet/sctp_var.h>
101 #include <netinet/sctp_header.h>
102 #include <netinet/sctputil.h>
103 #include <netinet/sctp_pcb.h>
104 #include <netinet/sctp_output.h>
105 #include <netinet/sctp_uio.h>
106 #include <netinet/sctputil.h>
107 #include <netinet/sctp_hashdriver.h>
108 #include <netinet/sctp_timer.h>
109 #include <netinet/sctp_asconf.h>
110 #include <netinet/sctp_indata.h>
111
112 #ifdef SCTP_DEBUG
113 extern uint32_t sctp_debug_on;
114 #endif
115
116 extern int sctp_peer_chunk_oh;
117
118 static int
119 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
120 {
121 struct cmsghdr cmh;
122 int tlen, at;
123
124 tlen = control->m_len;
125 at = 0;
126 /*
127 * Independent of how many mbufs, find the c_type inside the control
128 * structure and copy out the data.
129 */
130 while (at < tlen) {
131 if ((tlen-at) < (int)CMSG_ALIGN(sizeof(cmh))) {
132 /* not enough room for one more we are done. */
133 return (0);
134 }
135 m_copydata(control, at, sizeof(cmh), (void *)&cmh);
136 if ((cmh.cmsg_len + at) > tlen) {
137 /*
138 * this is real messed up since there is not enough
139 * data here to cover the cmsg header. We are done.
140 */
141 return (0);
142 }
143 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
144 (c_type == cmh.cmsg_type)) {
145 /* found the one we want, copy it out */
146 at += CMSG_ALIGN(sizeof(struct cmsghdr));
147 if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
148 /*
149 * space of cmsg_len after header not
150 * big enough
151 */
152 return (0);
153 }
154 m_copydata(control, at, cpsize, data);
155 return (1);
156 } else {
157 at += CMSG_ALIGN(cmh.cmsg_len);
158 if (cmh.cmsg_len == 0) {
159 break;
160 }
161 }
162 }
163 /* not found */
164 return (0);
165 }
166
167 static struct mbuf *
168 sctp_add_addr_to_mbuf(struct mbuf *m, struct ifaddr *ifa)
169 {
170 struct sctp_paramhdr *parmh;
171 struct mbuf *mret;
172 int len;
173 if (ifa->ifa_addr->sa_family == AF_INET) {
174 len = sizeof(struct sctp_ipv4addr_param);
175 } else if (ifa->ifa_addr->sa_family == AF_INET6) {
176 len = sizeof(struct sctp_ipv6addr_param);
177 } else {
178 /* unknown type */
179 return (m);
180 }
181
182 if (M_TRAILINGSPACE(m) >= len) {
183 /* easy side we just drop it on the end */
184 parmh = (struct sctp_paramhdr *)(m->m_data + m->m_len);
185 mret = m;
186 } else {
187 /* Need more space */
188 mret = m;
189 while (mret->m_next != NULL) {
190 mret = mret->m_next;
191 }
192 MGET(mret->m_next, M_DONTWAIT, MT_DATA);
193 if (mret->m_next == NULL) {
194 /* We are hosed, can't add more addresses */
195 return (m);
196 }
197 mret = mret->m_next;
198 parmh = mtod(mret, struct sctp_paramhdr *);
199 }
200 /* now add the parameter */
201 if (ifa->ifa_addr->sa_family == AF_INET) {
202 struct sctp_ipv4addr_param *ipv4p;
203 struct sockaddr_in *sin;
204 sin = (struct sockaddr_in *)ifa->ifa_addr;
205 ipv4p = (struct sctp_ipv4addr_param *)parmh;
206 parmh->param_type = htons(SCTP_IPV4_ADDRESS);
207 parmh->param_length = htons(len);
208 ipv4p->addr = sin->sin_addr.s_addr;
209 mret->m_len += len;
210 } else if (ifa->ifa_addr->sa_family == AF_INET6) {
211 struct sctp_ipv6addr_param *ipv6p;
212 struct sockaddr_in6 *sin6;
213 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
214 ipv6p = (struct sctp_ipv6addr_param *)parmh;
215 parmh->param_type = htons(SCTP_IPV6_ADDRESS);
216 parmh->param_length = htons(len);
217 memcpy(ipv6p->addr, &sin6->sin6_addr,
218 sizeof(ipv6p->addr));
219 /* clear embedded scope in the address */
220 in6_clearscope((struct in6_addr *)ipv6p->addr);
221 mret->m_len += len;
222 } else {
223 return (m);
224 }
225 return (mret);
226 }
227
228
229
230 static struct mbuf *
231 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
232 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in)
233 {
234 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
235 struct sctp_state_cookie *stc;
236 struct sctp_paramhdr *ph;
237 uint8_t *signature;
238 int sig_offset;
239 uint16_t cookie_sz;
240
241 mret = NULL;
242
243 MGET(mret, M_DONTWAIT, MT_DATA);
244 if (mret == NULL) {
245 return (NULL);
246 }
247 copy_init = sctp_m_copym(init, init_offset, M_COPYALL, M_DONTWAIT);
248 if (copy_init == NULL) {
249 sctp_m_freem(mret);
250 return (NULL);
251 }
252 copy_initack = sctp_m_copym(initack, initack_offset, M_COPYALL,
253 M_DONTWAIT);
254 if (copy_initack == NULL) {
255 sctp_m_freem(mret);
256 sctp_m_freem(copy_init);
257 return (NULL);
258 }
259 /* easy side we just drop it on the end */
260 ph = mtod(mret, struct sctp_paramhdr *);
261 mret->m_len = sizeof(struct sctp_state_cookie) +
262 sizeof(struct sctp_paramhdr);
263 stc = (struct sctp_state_cookie *)((vaddr_t)ph +
264 sizeof(struct sctp_paramhdr));
265 ph->param_type = htons(SCTP_STATE_COOKIE);
266 ph->param_length = 0; /* fill in at the end */
267 /* Fill in the stc cookie data */
268 *stc = *stc_in;
269
270 /* tack the INIT and then the INIT-ACK onto the chain */
271 cookie_sz = 0;
272 m_at = mret;
273 for (m_at = mret; m_at; m_at = m_at->m_next) {
274 cookie_sz += m_at->m_len;
275 if (m_at->m_next == NULL) {
276 m_at->m_next = copy_init;
277 break;
278 }
279 }
280
281 for (m_at = copy_init; m_at; m_at = m_at->m_next) {
282 cookie_sz += m_at->m_len;
283 if (m_at->m_next == NULL) {
284 m_at->m_next = copy_initack;
285 break;
286 }
287 }
288
289 for (m_at = copy_initack; m_at; m_at = m_at->m_next) {
290 cookie_sz += m_at->m_len;
291 if (m_at->m_next == NULL) {
292 break;
293 }
294 }
295 MGET(sig, M_DONTWAIT, MT_DATA);
296 if (sig == NULL) {
297 /* no space */
298 sctp_m_freem(mret);
299 sctp_m_freem(copy_init);
300 sctp_m_freem(copy_initack);
301 return (NULL);
302 }
303 sig->m_len = 0;
304 m_at->m_next = sig;
305 sig_offset = 0;
306 signature = (uint8_t *)(mtod(sig, vaddr_t) + sig_offset);
307 /* Time to sign the cookie */
308 sctp_hash_digest_m((char *)inp->sctp_ep.secret_key[
309 (int)(inp->sctp_ep.current_secret_number)],
310 SCTP_SECRET_SIZE, mret, sizeof(struct sctp_paramhdr),
311 (uint8_t *)signature);
312 sig->m_len += SCTP_SIGNATURE_SIZE;
313 cookie_sz += SCTP_SIGNATURE_SIZE;
314
315 ph->param_length = htons(cookie_sz);
316 return (mret);
317 }
318
319
320 static struct sockaddr_in *
321 sctp_is_v4_ifa_addr_prefered (struct ifaddr *ifa, uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
322 {
323 struct sockaddr_in *sin;
324 /*
325 * Here we determine if its a prefered address. A
326 * prefered address means it is the same scope or
327 * higher scope then the destination.
328 * L = loopback, P = private, G = global
329 * -----------------------------------------
330 * src | dest | result
331 *-----------------------------------------
332 * L | L | yes
333 *-----------------------------------------
334 * P | L | yes
335 *-----------------------------------------
336 * G | L | yes
337 *-----------------------------------------
338 * L | P | no
339 *-----------------------------------------
340 * P | P | yes
341 *-----------------------------------------
342 * G | P | no
343 *-----------------------------------------
344 * L | G | no
345 *-----------------------------------------
346 * P | G | no
347 *-----------------------------------------
348 * G | G | yes
349 *-----------------------------------------
350 */
351
352 if (ifa->ifa_addr->sa_family != AF_INET) {
353 /* forget non-v4 */
354 return (NULL);
355 }
356 /* Ok the address may be ok */
357 sin = (struct sockaddr_in *)ifa->ifa_addr;
358 if (sin->sin_addr.s_addr == 0) {
359 return (NULL);
360 }
361 *sin_local = *sin_loop = 0;
362 if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
363 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
364 *sin_loop = 1;
365 *sin_local = 1;
366 }
367 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
368 *sin_local = 1;
369 }
370 if (!loopscope && *sin_loop) {
371 /* Its a loopback address and we don't have loop scope */
372 return (NULL);
373 }
374 if (!ipv4_scope && *sin_local) {
375 /* Its a private address, and we don't have private address scope */
376 return (NULL);
377 }
378 if (((ipv4_scope == 0) && (loopscope == 0)) && (*sin_local)) {
379 /* its a global src and a private dest */
380 return (NULL);
381 }
382 /* its a prefered address */
383 return (sin);
384 }
385
386 static struct sockaddr_in *
387 sctp_is_v4_ifa_addr_acceptable (struct ifaddr *ifa, uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
388 {
389 struct sockaddr_in *sin;
390 /*
391 * Here we determine if its a acceptable address. A
392 * acceptable address means it is the same scope or
393 * higher scope but we can allow for NAT which means
394 * its ok to have a global dest and a private src.
395 *
396 * L = loopback, P = private, G = global
397 * -----------------------------------------
398 * src | dest | result
399 *-----------------------------------------
400 * L | L | yes
401 *-----------------------------------------
402 * P | L | yes
403 *-----------------------------------------
404 * G | L | yes
405 *-----------------------------------------
406 * L | P | no
407 *-----------------------------------------
408 * P | P | yes
409 *-----------------------------------------
410 * G | P | yes - probably this won't work.
411 *-----------------------------------------
412 * L | G | no
413 *-----------------------------------------
414 * P | G | yes
415 *-----------------------------------------
416 * G | G | yes
417 *-----------------------------------------
418 */
419
420 if (ifa->ifa_addr->sa_family != AF_INET) {
421 /* forget non-v4 */
422 return (NULL);
423 }
424 /* Ok the address may be ok */
425 sin = (struct sockaddr_in *)ifa->ifa_addr;
426 if (sin->sin_addr.s_addr == 0) {
427 return (NULL);
428 }
429 *sin_local = *sin_loop = 0;
430 if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
431 (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
432 *sin_loop = 1;
433 *sin_local = 1;
434 }
435 if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
436 *sin_local = 1;
437 }
438 if (!loopscope && *sin_loop) {
439 /* Its a loopback address and we don't have loop scope */
440 return (NULL);
441 }
442 /* its an acceptable address */
443 return (sin);
444 }
445
446 /*
447 * This treats the address list on the ep as a restricted list
448 * (negative list). If a the passed address is listed, then
449 * the address is NOT allowed on the association.
450 */
451 int
452 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sockaddr *addr)
453 {
454 struct sctp_laddr *laddr;
455 #ifdef SCTP_DEBUG
456 int cnt=0;
457 #endif
458 if (stcb == NULL) {
459 /* There are no restrictions, no TCB :-) */
460 return (0);
461 }
462 #ifdef SCTP_DEBUG
463 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
464 cnt++;
465 }
466 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
467 printf("There are %d addresses on the restricted list\n", cnt);
468 }
469 cnt = 0;
470 #endif
471 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
472 if (laddr->ifa == NULL) {
473 #ifdef SCTP_DEBUG
474 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
475 printf("Help I have fallen and I can't get up!\n");
476 }
477 #endif
478 continue;
479 }
480 #ifdef SCTP_DEBUG
481 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
482 cnt++;
483 printf("Restricted address[%d]:", cnt);
484 sctp_print_address(laddr->ifa->ifa_addr);
485 }
486 #endif
487 if (sctp_cmpaddr(addr, laddr->ifa->ifa_addr) == 1) {
488 /* Yes it is on the list */
489 return (1);
490 }
491 }
492 return (0);
493 }
494
495 static int
496 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
497 {
498 struct sctp_laddr *laddr;
499
500 if (ifa == NULL)
501 return (0);
502 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
503 if (laddr->ifa == NULL) {
504 #ifdef SCTP_DEBUG
505 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
506 printf("Help I have fallen and I can't get up!\n");
507 }
508 #endif
509 continue;
510 }
511 if (laddr->ifa->ifa_addr == NULL)
512 continue;
513 if (laddr->ifa == ifa)
514 /* same pointer */
515 return (1);
516 if (laddr->ifa->ifa_addr->sa_family != ifa->ifa_addr->sa_family) {
517 /* skip non compatible address comparison */
518 continue;
519 }
520 if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
521 /* Yes it is restricted */
522 return (1);
523 }
524 }
525 return (0);
526 }
527
528
529
530 static struct in_addr
531 sctp_choose_v4_boundspecific_inp(struct sctp_inpcb *inp,
532 struct rtentry *rt,
533 uint8_t ipv4_scope,
534 uint8_t loopscope)
535 {
536 struct in_addr ans;
537 struct sctp_laddr *laddr;
538 struct sockaddr_in *sin;
539 struct ifnet *ifn;
540 struct ifaddr *ifa;
541 uint8_t sin_loop, sin_local;
542
543 /* first question, is the ifn we will emit on
544 * in our list, if so, we want that one.
545 */
546 ifn = rt->rt_ifp;
547 if (ifn) {
548 /* is a prefered one on the interface we route out? */
549 IFADDR_FOREACH(ifa, ifn) {
550 sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
551 if (sin == NULL)
552 continue;
553 if (sctp_is_addr_in_ep(inp, ifa)) {
554 return (sin->sin_addr);
555 }
556 }
557 /* is an acceptable one on the interface we route out? */
558 IFADDR_FOREACH(ifa, ifn) {
559 sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
560 if (sin == NULL)
561 continue;
562 if (sctp_is_addr_in_ep(inp, ifa)) {
563 return (sin->sin_addr);
564 }
565 }
566 }
567 /* ok, what about a prefered address in the inp */
568 for (laddr = LIST_FIRST(&inp->sctp_addr_list);
569 laddr && (laddr != inp->next_addr_touse);
570 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
571 if (laddr->ifa == NULL) {
572 /* address has been removed */
573 continue;
574 }
575 sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
576 if (sin == NULL)
577 continue;
578 return (sin->sin_addr);
579
580 }
581 /* ok, what about an acceptable address in the inp */
582 for (laddr = LIST_FIRST(&inp->sctp_addr_list);
583 laddr && (laddr != inp->next_addr_touse);
584 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
585 if (laddr->ifa == NULL) {
586 /* address has been removed */
587 continue;
588 }
589 sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
590 if (sin == NULL)
591 continue;
592 return (sin->sin_addr);
593
594 }
595
596 /* no address bound can be a source for the destination we are in trouble */
597 #ifdef SCTP_DEBUG
598 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
599 printf("Src address selection for EP, no acceptable src address found for address\n");
600 }
601 #endif
602 memset(&ans, 0, sizeof(ans));
603 return (ans);
604 }
605
606
607
608 static struct in_addr
609 sctp_choose_v4_boundspecific_stcb(struct sctp_inpcb *inp,
610 struct sctp_tcb *stcb,
611 struct sctp_nets *net,
612 struct rtentry *rt,
613 uint8_t ipv4_scope,
614 uint8_t loopscope,
615 int non_asoc_addr_ok)
616 {
617 /*
618 * Here we have two cases, bound all asconf
619 * allowed. bound all asconf not allowed.
620 *
621 */
622 struct sctp_laddr *laddr, *starting_point;
623 struct in_addr ans;
624 struct ifnet *ifn;
625 struct ifaddr *ifa;
626 uint8_t sin_loop, sin_local, start_at_beginning=0;
627 struct sockaddr_in *sin;
628
629 /* first question, is the ifn we will emit on
630 * in our list, if so, we want that one.
631 */
632 ifn = rt->rt_ifp;
633
634 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
635 /*
636 * Here we use the list of addresses on the endpoint. Then
637 * the addresses listed on the "restricted" list is just that,
638 * address that have not been added and can't be used (unless
639 * the non_asoc_addr_ok is set).
640 */
641 #ifdef SCTP_DEBUG
642 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
643 printf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
644 }
645 #endif
646 /* first question, is the ifn we will emit on
647 * in our list, if so, we want that one.
648 */
649 if (ifn) {
650 /* first try for an prefered address on the ep */
651 IFADDR_FOREACH(ifa, ifn) {
652 if (sctp_is_addr_in_ep(inp, ifa)) {
653 sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
654 if (sin == NULL)
655 continue;
656 if ((non_asoc_addr_ok == 0) &&
657 (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
658 /* on the no-no list */
659 continue;
660 }
661 return (sin->sin_addr);
662 }
663 }
664 /* next try for an acceptable address on the ep */
665 IFADDR_FOREACH(ifa, ifn) {
666 if (sctp_is_addr_in_ep(inp, ifa)) {
667 sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
668 if (sin == NULL)
669 continue;
670 if ((non_asoc_addr_ok == 0) &&
671 (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
672 /* on the no-no list */
673 continue;
674 }
675 return (sin->sin_addr);
676 }
677 }
678
679 }
680 /* if we can't find one like that then we must
681 * look at all addresses bound to pick one at
682 * first prefereable then secondly acceptable.
683 */
684 starting_point = stcb->asoc.last_used_address;
685 sctpv4_from_the_top:
686 if (stcb->asoc.last_used_address == NULL) {
687 start_at_beginning=1;
688 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
689 }
690 /* search beginning with the last used address */
691 for (laddr = stcb->asoc.last_used_address; laddr;
692 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
693 if (laddr->ifa == NULL) {
694 /* address has been removed */
695 continue;
696 }
697 sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
698 if (sin == NULL)
699 continue;
700 if ((non_asoc_addr_ok == 0) &&
701 (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
702 /* on the no-no list */
703 continue;
704 }
705 return (sin->sin_addr);
706
707 }
708 if (start_at_beginning == 0) {
709 stcb->asoc.last_used_address = NULL;
710 goto sctpv4_from_the_top;
711 }
712 /* now try for any higher scope than the destination */
713 stcb->asoc.last_used_address = starting_point;
714 start_at_beginning = 0;
715 sctpv4_from_the_top2:
716 if (stcb->asoc.last_used_address == NULL) {
717 start_at_beginning=1;
718 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
719 }
720 /* search beginning with the last used address */
721 for (laddr = stcb->asoc.last_used_address; laddr;
722 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
723 if (laddr->ifa == NULL) {
724 /* address has been removed */
725 continue;
726 }
727 sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
728 if (sin == NULL)
729 continue;
730 if ((non_asoc_addr_ok == 0) &&
731 (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin))) {
732 /* on the no-no list */
733 continue;
734 }
735 return (sin->sin_addr);
736 }
737 if (start_at_beginning == 0) {
738 stcb->asoc.last_used_address = NULL;
739 goto sctpv4_from_the_top2;
740 }
741 } else {
742 /*
743 * Here we have an address list on the association, thats the
744 * only valid source addresses that we can use.
745 */
746 #ifdef SCTP_DEBUG
747 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
748 printf("Have a STCB - no asconf allowed, not bound all have a postive list\n");
749 }
750 #endif
751 /* First look at all addresses for one that is on
752 * the interface we route out
753 */
754 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
755 sctp_nxt_addr) {
756 if (laddr->ifa == NULL) {
757 /* address has been removed */
758 continue;
759 }
760 sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
761 if (sin == NULL)
762 continue;
763 /* first question, is laddr->ifa an address associated with the emit interface */
764 if (ifn) {
765 IFADDR_FOREACH(ifa, ifn) {
766 if (laddr->ifa == ifa) {
767 sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
768 return (sin->sin_addr);
769 }
770 if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
771 sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
772 return (sin->sin_addr);
773 }
774 }
775 }
776 }
777 /* what about an acceptable one on the interface? */
778 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
779 sctp_nxt_addr) {
780 if (laddr->ifa == NULL) {
781 /* address has been removed */
782 continue;
783 }
784 sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
785 if (sin == NULL)
786 continue;
787 /* first question, is laddr->ifa an address associated with the emit interface */
788 if (ifn) {
789 IFADDR_FOREACH(ifa, ifn) {
790 if (laddr->ifa == ifa) {
791 sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
792 return (sin->sin_addr);
793 }
794 if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
795 sin = (struct sockaddr_in *)laddr->ifa->ifa_addr;
796 return (sin->sin_addr);
797 }
798 }
799 }
800 }
801 /* ok, next one that is preferable in general */
802 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
803 sctp_nxt_addr) {
804 if (laddr->ifa == NULL) {
805 /* address has been removed */
806 continue;
807 }
808 sin = sctp_is_v4_ifa_addr_prefered (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
809 if (sin == NULL)
810 continue;
811 return (sin->sin_addr);
812 }
813
814 /* last, what about one that is acceptable */
815 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
816 sctp_nxt_addr) {
817 if (laddr->ifa == NULL) {
818 /* address has been removed */
819 continue;
820 }
821 sin = sctp_is_v4_ifa_addr_acceptable (laddr->ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
822 if (sin == NULL)
823 continue;
824 return (sin->sin_addr);
825 }
826 }
827 memset(&ans, 0, sizeof(ans));
828 return (ans);
829 }
830
831 static struct sockaddr_in *
832 sctp_select_v4_nth_prefered_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
833 uint8_t loopscope, uint8_t ipv4_scope, int cur_addr_num)
834 {
835 struct ifaddr *ifa;
836 struct sockaddr_in *sin;
837 uint8_t sin_loop, sin_local;
838 int num_eligible_addr = 0;
839 IFADDR_FOREACH(ifa, ifn) {
840 sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
841 if (sin == NULL)
842 continue;
843 if (stcb) {
844 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
845 /* It is restricted for some reason.. probably
846 * not yet added.
847 */
848 continue;
849 }
850 }
851 if (cur_addr_num == num_eligible_addr) {
852 return (sin);
853 }
854 }
855 return (NULL);
856 }
857
858
859 static int
860 sctp_count_v4_num_prefered_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok,
861 uint8_t loopscope, uint8_t ipv4_scope, uint8_t *sin_loop, uint8_t *sin_local)
862 {
863 struct ifaddr *ifa;
864 struct sockaddr_in *sin;
865 int num_eligible_addr = 0;
866
867 IFADDR_FOREACH(ifa, ifn) {
868 sin = sctp_is_v4_ifa_addr_prefered (ifa, loopscope, ipv4_scope, sin_loop, sin_local);
869 if (sin == NULL)
870 continue;
871 if (stcb) {
872 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
873 /* It is restricted for some reason.. probably
874 * not yet added.
875 */
876 continue;
877 }
878 }
879 num_eligible_addr++;
880 }
881 return (num_eligible_addr);
882
883 }
884
885 static struct in_addr
886 sctp_choose_v4_boundall(struct sctp_inpcb *inp,
887 struct sctp_tcb *stcb,
888 struct sctp_nets *net,
889 struct rtentry *rt,
890 uint8_t ipv4_scope,
891 uint8_t loopscope,
892 int non_asoc_addr_ok)
893 {
894 int cur_addr_num=0, num_prefered=0;
895 uint8_t sin_loop, sin_local;
896 struct ifnet *ifn;
897 struct sockaddr_in *sin;
898 struct in_addr ans;
899 struct ifaddr *ifa;
900 /*
901 * For v4 we can use (in boundall) any address in the association. If
902 * non_asoc_addr_ok is set we can use any address (at least in theory).
903 * So we look for prefered addresses first. If we find one, we use it.
904 * Otherwise we next try to get an address on the interface, which we
905 * should be able to do (unless non_asoc_addr_ok is false and we are
906 * routed out that way). In these cases where we can't use the address
907 * of the interface we go through all the ifn's looking for an address
908 * we can use and fill that in. Punting means we send back address
909 * 0, which will probably cause problems actually since then IP will
910 * fill in the address of the route ifn, which means we probably already
911 * rejected it.. i.e. here comes an abort :-<.
912 */
913 ifn = rt->rt_ifp;
914 if (net) {
915 cur_addr_num = net->indx_of_eligible_next_to_use;
916 }
917 if (ifn == NULL) {
918 goto bound_all_v4_plan_c;
919 }
920 num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, ipv4_scope, &sin_loop, &sin_local);
921 #ifdef SCTP_DEBUG
922 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
923 printf("Found %d prefered source addresses\n", num_prefered);
924 }
925 #endif
926 if (num_prefered == 0) {
927 /* no eligible addresses, we must use some other
928 * interface address if we can find one.
929 */
930 goto bound_all_v4_plan_b;
931 }
932 /* Ok we have num_eligible_addr set with how many we can use,
933 * this may vary from call to call due to addresses being deprecated etc..
934 */
935 if (cur_addr_num >= num_prefered) {
936 cur_addr_num = 0;
937 }
938 /* select the nth address from the list (where cur_addr_num is the nth) and
939 * 0 is the first one, 1 is the second one etc...
940 */
941 #ifdef SCTP_DEBUG
942 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
943 printf("cur_addr_num:%d\n", cur_addr_num);
944 }
945 #endif
946 sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
947 ipv4_scope, cur_addr_num);
948
949 /* if sin is NULL something changed??, plan_a now */
950 if (sin) {
951 return (sin->sin_addr);
952 }
953
954 /*
955 * plan_b: Look at the interface that we emit on
956 * and see if we can find an acceptable address.
957 */
958 bound_all_v4_plan_b:
959 IFADDR_FOREACH(ifa, ifn) {
960 sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
961 if (sin == NULL)
962 continue;
963 if (stcb) {
964 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
965 /* It is restricted for some reason.. probably
966 * not yet added.
967 */
968 continue;
969 }
970 }
971 return (sin->sin_addr);
972 }
973 /*
974 * plan_c: Look at all interfaces and find a prefered
975 * address. If we reache here we are in trouble I think.
976 */
977 bound_all_v4_plan_c:
978 IFNET_FOREACH(ifn) {
979 if (ifn == inp->next_ifn_touse)
980 break;
981 if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
982 /* wrong base scope */
983 continue;
984 }
985 if (ifn == rt->rt_ifp)
986 /* already looked at this guy */
987 continue;
988 num_prefered = sctp_count_v4_num_prefered_boundall (ifn, stcb, non_asoc_addr_ok,
989 loopscope, ipv4_scope, &sin_loop, &sin_local);
990 #ifdef SCTP_DEBUG
991 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
992 printf("Found ifn:%p %d prefered source addresses\n", ifn, num_prefered);
993 }
994 #endif
995 if (num_prefered == 0) {
996 /*
997 * None on this interface.
998 */
999 continue;
1000 }
1001 /* Ok we have num_eligible_addr set with how many we can use,
1002 * this may vary from call to call due to addresses being deprecated etc..
1003 */
1004 if (cur_addr_num >= num_prefered) {
1005 cur_addr_num = 0;
1006 }
1007 sin = sctp_select_v4_nth_prefered_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
1008 ipv4_scope, cur_addr_num);
1009 if (sin == NULL)
1010 continue;
1011 return (sin->sin_addr);
1012
1013 }
1014
1015 /*
1016 * plan_d: We are in deep trouble. No prefered address on
1017 * any interface. And the emit interface does not
1018 * even have an acceptable address. Take anything
1019 * we can get! If this does not work we are
1020 * probably going to emit a packet that will
1021 * illicit an ABORT, falling through.
1022 */
1023
1024 IFNET_FOREACH(ifn) {
1025 if (ifn == inp->next_ifn_touse)
1026 break;
1027 if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
1028 /* wrong base scope */
1029 continue;
1030 }
1031 if (ifn == rt->rt_ifp)
1032 /* already looked at this guy */
1033 continue;
1034
1035 IFADDR_FOREACH(ifa, ifn) {
1036 sin = sctp_is_v4_ifa_addr_acceptable (ifa, loopscope, ipv4_scope, &sin_loop, &sin_local);
1037 if (sin == NULL)
1038 continue;
1039 if (stcb) {
1040 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin)) {
1041 /* It is restricted for some reason.. probably
1042 * not yet added.
1043 */
1044 continue;
1045 }
1046 }
1047 return (sin->sin_addr);
1048 }
1049 }
1050 /*
1051 * Ok we can find NO address to source from that is
1052 * not on our negative list. It is either the special
1053 * ASCONF case where we are sourceing from a intf that
1054 * has been ifconfig'd to a different address (i.e.
1055 * it holds a ADD/DEL/SET-PRIM and the proper lookup
1056 * address. OR we are hosed, and this baby is going
1057 * to abort the association.
1058 */
1059 if (non_asoc_addr_ok) {
1060 return (((struct sockaddr_in *)(rt->rt_ifa->ifa_addr))->sin_addr);
1061 } else {
1062 memset(&ans, 0, sizeof(ans));
1063 return (ans);
1064 }
1065 }
1066
1067
1068
1069 /* tcb may be NULL */
1070 struct in_addr
1071 sctp_ipv4_source_address_selection(struct sctp_inpcb *inp,
1072 struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
1073 int non_asoc_addr_ok)
1074 {
1075 struct in_addr ans;
1076 const struct sockaddr_in *to;
1077 struct rtentry *rt;
1078 uint8_t ipv4_scope, loopscope;
1079 /*
1080 * Rules:
1081 * - Find the route if needed, cache if I can.
1082 * - Look at interface address in route, Is it
1083 * in the bound list. If so we have the best source.
1084 * - If not we must rotate amongst the addresses.
1085 *
1086 * Cavets and issues
1087 *
1088 * Do we need to pay attention to scope. We can have
1089 * a private address or a global address we are sourcing
1090 * or sending to. So if we draw it out
1091 * source * dest * result
1092 * ------------------------------------------
1093 * a Private * Global * NAT?
1094 * ------------------------------------------
1095 * b Private * Private * No problem
1096 * ------------------------------------------
1097 * c Global * Private * Huh, How will this work?
1098 * ------------------------------------------
1099 * d Global * Global * No Problem
1100 * ------------------------------------------
1101 *
1102 * And then we add to that what happens if there are multiple
1103 * addresses assigned to an interface. Remember the ifa on a
1104 * ifn is a linked list of addresses. So one interface can
1105 * have more than one IPv4 address. What happens if we
1106 * have both a private and a global address? Do we then
1107 * use context of destination to sort out which one is
1108 * best? And what about NAT's sending P->G may get you
1109 * a NAT translation, or should you select the G thats
1110 * on the interface in preference.
1111 *
1112 * Decisions:
1113 *
1114 * - count the number of addresses on the interface.
1115 * - if its one, no problem except case <c>. For <a>
1116 * we will assume a NAT out there.
1117 * - if there are more than one, then we need to worry
1118 * about scope P or G. We should prefer G -> G and
1119 * P -> P if possible. Then as a secondary fall back
1120 * to mixed types G->P being a last ditch one.
1121 * - The above all works for bound all, but bound
1122 * specific we need to use the same concept but instead
1123 * only consider the bound addresses. If the bound set
1124 * is NOT assigned to the interface then we must use
1125 * rotation amongst them.
1126 *
1127 * Notes: For v4, we can always punt and let ip_output
1128 * decide by sending back a source of 0.0.0.0
1129 */
1130
1131 /*
1132 * Need a route to cache.
1133 *
1134 */
1135 rt = rtcache_validate(ro);
1136 if (rt == NULL) {
1137 /* No route to host .. punt */
1138 memset(&ans, 0, sizeof(ans));
1139 return (ans);
1140 } else {
1141 to = satocsin(rtcache_getdst(ro));
1142 }
1143 /* Setup our scopes */
1144 if (stcb) {
1145 ipv4_scope = stcb->asoc.ipv4_local_scope;
1146 loopscope = stcb->asoc.loopback_scope;
1147 } else {
1148 /* Scope based on outbound address */
1149 if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
1150 ipv4_scope = 1;
1151 loopscope = 0;
1152 } else if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
1153 ipv4_scope = 1;
1154 loopscope = 1;
1155 } else {
1156 ipv4_scope = 0;
1157 loopscope = 0;
1158 }
1159 }
1160 #ifdef SCTP_DEBUG
1161 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1162 printf("Scope setup loop:%d ipv4_scope:%d\n",
1163 loopscope, ipv4_scope);
1164 }
1165 #endif
1166 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1167 /*
1168 * When bound to all if the address list is set
1169 * it is a negative list. Addresses being added
1170 * by asconf.
1171 */
1172 return (sctp_choose_v4_boundall(inp, stcb, net, rt,
1173 ipv4_scope, loopscope, non_asoc_addr_ok));
1174 }
1175 /*
1176 * Three possiblities here:
1177 *
1178 * a) stcb is NULL, which means we operate only from
1179 * the list of addresses (ifa's) bound to the assoc and
1180 * we care not about the list.
1181 * b) stcb is NOT-NULL, which means we have an assoc structure and
1182 * auto-asconf is on. This means that the list of addresses is
1183 * a NOT list. We use the list from the inp, but any listed address
1184 * in our list is NOT yet added. However if the non_asoc_addr_ok is
1185 * set we CAN use an address NOT available (i.e. being added). Its
1186 * a negative list.
1187 * c) stcb is NOT-NULL, which means we have an assoc structure and
1188 * auto-asconf is off. This means that the list of addresses is
1189 * the ONLY addresses I can use.. its positive.
1190 *
1191 * Note we collapse b & c into the same function just like in
1192 * the v6 address selection.
1193 */
1194 if (stcb) {
1195 return (sctp_choose_v4_boundspecific_stcb(inp, stcb, net,
1196 rt, ipv4_scope, loopscope, non_asoc_addr_ok));
1197 } else {
1198 return (sctp_choose_v4_boundspecific_inp(inp, rt,
1199 ipv4_scope, loopscope));
1200 }
1201 /* this should not be reached */
1202 memset(&ans, 0, sizeof(ans));
1203 return (ans);
1204 }
1205
1206
1207
1208 static struct sockaddr_in6 *
1209 sctp_is_v6_ifa_addr_acceptable (struct ifaddr *ifa, int loopscope, int loc_scope, int *sin_loop, int *sin_local)
1210 {
1211 struct in6_ifaddr *ifa6;
1212 struct sockaddr_in6 *sin6;
1213
1214 if (ifa->ifa_addr->sa_family != AF_INET6) {
1215 /* forget non-v6 */
1216 return (NULL);
1217 }
1218 ifa6 = (struct in6_ifaddr *)ifa;
1219 /* ok to use deprecated addresses? */
1220 if (!ip6_use_deprecated) {
1221 if (IFA6_IS_DEPRECATED(ifa6)) {
1222 /* can't use this type */
1223 return (NULL);
1224 }
1225 }
1226 /* are we ok, with the current state of this address? */
1227 if (ifa6->ia6_flags &
1228 (IN6_IFF_DETACHED | IN6_IFF_NOTREADY | IN6_IFF_ANYCAST)) {
1229 /* Can't use these types */
1230 return (NULL);
1231 }
1232 /* Ok the address may be ok */
1233 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1234 *sin_local = *sin_loop = 0;
1235 if ((ifa->ifa_ifp->if_type == IFT_LOOP) ||
1236 (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
1237 *sin_loop = 1;
1238 }
1239 if (!loopscope && *sin_loop) {
1240 /* Its a loopback address and we don't have loop scope */
1241 return (NULL);
1242 }
1243 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1244 /* we skip unspecifed addresses */
1245 return (NULL);
1246 }
1247
1248 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1249 *sin_local = 1;
1250 }
1251 if (!loc_scope && *sin_local) {
1252 /* Its a link local address, and we don't have link local scope */
1253 return (NULL);
1254 }
1255 return (sin6);
1256 }
1257
1258
1259 static struct sockaddr_in6 *
1260 sctp_choose_v6_boundspecific_stcb(struct sctp_inpcb *inp,
1261 struct sctp_tcb *stcb,
1262 struct sctp_nets *net,
1263 struct rtentry *rt,
1264 uint8_t loc_scope,
1265 uint8_t loopscope,
1266 int non_asoc_addr_ok)
1267 {
1268 /*
1269 * Each endpoint has a list of local addresses associated
1270 * with it. The address list is either a "negative list" i.e.
1271 * those addresses that are NOT allowed to be used as a source OR
1272 * a "postive list" i.e. those addresses that CAN be used.
1273 *
1274 * Its a negative list if asconf is allowed. What we do
1275 * in this case is use the ep address list BUT we have
1276 * to cross check it against the negative list.
1277 *
1278 * In the case where NO asconf is allowed, we have just
1279 * a straight association level list that we must use to
1280 * find a source address.
1281 */
1282 struct sctp_laddr *laddr, *starting_point;
1283 struct sockaddr_in6 *sin6;
1284 int sin_loop, sin_local;
1285 int start_at_beginning=0;
1286 struct ifnet *ifn;
1287 struct ifaddr *ifa;
1288
1289 ifn = rt->rt_ifp;
1290 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1291 #ifdef SCTP_DEBUG
1292 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1293 printf("Have a STCB - asconf allowed, not bound all have a netgative list\n");
1294 }
1295 #endif
1296 /* first question, is the ifn we will emit on
1297 * in our list, if so, we want that one.
1298 */
1299 if (ifn) {
1300 IFADDR_FOREACH(ifa, ifn) {
1301 if (sctp_is_addr_in_ep(inp, ifa)) {
1302 sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1303 if (sin6 == NULL)
1304 continue;
1305 if ((non_asoc_addr_ok == 0) &&
1306 (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
1307 /* on the no-no list */
1308 continue;
1309 }
1310 return (sin6);
1311 }
1312 }
1313 }
1314 starting_point = stcb->asoc.last_used_address;
1315 /* First try for matching scope */
1316 sctp_from_the_top:
1317 if (stcb->asoc.last_used_address == NULL) {
1318 start_at_beginning=1;
1319 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
1320 }
1321 /* search beginning with the last used address */
1322 for (laddr = stcb->asoc.last_used_address; laddr;
1323 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1324 if (laddr->ifa == NULL) {
1325 /* address has been removed */
1326 continue;
1327 }
1328 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1329 if (sin6 == NULL)
1330 continue;
1331 if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
1332 /* on the no-no list */
1333 continue;
1334 }
1335 /* is it of matching scope ? */
1336 if ((loopscope == 0) &&
1337 (loc_scope == 0) &&
1338 (sin_loop == 0) &&
1339 (sin_local == 0)) {
1340 /* all of global scope we are ok with it */
1341 return (sin6);
1342 }
1343 if (loopscope && sin_loop)
1344 /* both on the loopback, thats ok */
1345 return (sin6);
1346 if (loc_scope && sin_local)
1347 /* both local scope */
1348 return (sin6);
1349
1350 }
1351 if (start_at_beginning == 0) {
1352 stcb->asoc.last_used_address = NULL;
1353 goto sctp_from_the_top;
1354 }
1355 /* now try for any higher scope than the destination */
1356 stcb->asoc.last_used_address = starting_point;
1357 start_at_beginning = 0;
1358 sctp_from_the_top2:
1359 if (stcb->asoc.last_used_address == NULL) {
1360 start_at_beginning=1;
1361 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
1362 }
1363 /* search beginning with the last used address */
1364 for (laddr = stcb->asoc.last_used_address; laddr;
1365 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1366 if (laddr->ifa == NULL) {
1367 /* address has been removed */
1368 continue;
1369 }
1370 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1371 if (sin6 == NULL)
1372 continue;
1373 if ((non_asoc_addr_ok == 0) && (sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6))) {
1374 /* on the no-no list */
1375 continue;
1376 }
1377 return (sin6);
1378 }
1379 if (start_at_beginning == 0) {
1380 stcb->asoc.last_used_address = NULL;
1381 goto sctp_from_the_top2;
1382 }
1383 } else {
1384 #ifdef SCTP_DEBUG
1385 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1386 printf("Have a STCB - no asconf allowed, not bound all have a postive list\n");
1387 }
1388 #endif
1389 /* First try for interface output match */
1390 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1391 sctp_nxt_addr) {
1392 if (laddr->ifa == NULL) {
1393 /* address has been removed */
1394 continue;
1395 }
1396 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1397 if (sin6 == NULL)
1398 continue;
1399 /* first question, is laddr->ifa an address associated with the emit interface */
1400 if (ifn) {
1401 IFADDR_FOREACH(ifa, ifn) {
1402 if (laddr->ifa == ifa) {
1403 sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1404 return (sin6);
1405 }
1406 if (sctp_cmpaddr(ifa->ifa_addr, laddr->ifa->ifa_addr) == 1) {
1407 sin6 = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1408 return (sin6);
1409 }
1410 }
1411 }
1412 }
1413 /* Next try for matching scope */
1414 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1415 sctp_nxt_addr) {
1416 if (laddr->ifa == NULL) {
1417 /* address has been removed */
1418 continue;
1419 }
1420 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1421 if (sin6 == NULL)
1422 continue;
1423
1424 if ((loopscope == 0) &&
1425 (loc_scope == 0) &&
1426 (sin_loop == 0) &&
1427 (sin_local == 0)) {
1428 /* all of global scope we are ok with it */
1429 return (sin6);
1430 }
1431 if (loopscope && sin_loop)
1432 /* both on the loopback, thats ok */
1433 return (sin6);
1434 if (loc_scope && sin_local)
1435 /* both local scope */
1436 return (sin6);
1437 }
1438 /* ok, now try for a higher scope in the source address */
1439 /* First try for matching scope */
1440 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1441 sctp_nxt_addr) {
1442 if (laddr->ifa == NULL) {
1443 /* address has been removed */
1444 continue;
1445 }
1446 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1447 if (sin6 == NULL)
1448 continue;
1449 return (sin6);
1450 }
1451 }
1452 return (NULL);
1453 }
1454
1455 static struct sockaddr_in6 *
1456 sctp_choose_v6_boundspecific_inp(struct sctp_inpcb *inp,
1457 struct rtentry *rt,
1458 uint8_t loc_scope,
1459 uint8_t loopscope)
1460 {
1461 /*
1462 * Here we are bound specific and have only
1463 * an inp. We must find an address that is bound
1464 * that we can give out as a src address. We
1465 * prefer two addresses of same scope if we can
1466 * find them that way.
1467 */
1468 struct sctp_laddr *laddr;
1469 struct sockaddr_in6 *sin6;
1470 struct ifnet *ifn;
1471 struct ifaddr *ifa;
1472 int sin_loop, sin_local;
1473
1474 /* first question, is the ifn we will emit on
1475 * in our list, if so, we want that one.
1476 */
1477
1478 ifn = rt->rt_ifp;
1479 if (ifn) {
1480 IFADDR_FOREACH(ifa, ifn) {
1481 sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1482 if (sin6 == NULL)
1483 continue;
1484 if (sctp_is_addr_in_ep(inp, ifa)) {
1485 return (sin6);
1486 }
1487 }
1488 }
1489 for (laddr = LIST_FIRST(&inp->sctp_addr_list);
1490 laddr && (laddr != inp->next_addr_touse);
1491 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1492 if (laddr->ifa == NULL) {
1493 /* address has been removed */
1494 continue;
1495 }
1496 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1497 if (sin6 == NULL)
1498 continue;
1499
1500 if ((loopscope == 0) &&
1501 (loc_scope == 0) &&
1502 (sin_loop == 0) &&
1503 (sin_local == 0)) {
1504 /* all of global scope we are ok with it */
1505 return (sin6);
1506 }
1507 if (loopscope && sin_loop)
1508 /* both on the loopback, thats ok */
1509 return (sin6);
1510 if (loc_scope && sin_local)
1511 /* both local scope */
1512 return (sin6);
1513
1514 }
1515 /* if we reach here, we could not find two addresses
1516 * of the same scope to give out. Lets look for any higher level
1517 * scope for a source address.
1518 */
1519 for (laddr = LIST_FIRST(&inp->sctp_addr_list);
1520 laddr && (laddr != inp->next_addr_touse);
1521 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
1522 if (laddr->ifa == NULL) {
1523 /* address has been removed */
1524 continue;
1525 }
1526 sin6 = sctp_is_v6_ifa_addr_acceptable (laddr->ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1527 if (sin6 == NULL)
1528 continue;
1529 return (sin6);
1530 }
1531 /* no address bound can be a source for the destination */
1532 #ifdef SCTP_DEBUG
1533 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1534 printf("Src address selection for EP, no acceptable src address found for address\n");
1535 }
1536 #endif
1537 return (NULL);
1538 }
1539
1540
1541 static struct sockaddr_in6 *
1542 sctp_select_v6_nth_addr_from_ifn_boundall (struct ifnet *ifn, struct sctp_tcb *stcb, int non_asoc_addr_ok, uint8_t loopscope,
1543 uint8_t loc_scope, int cur_addr_num, int match_scope)
1544 {
1545 struct ifaddr *ifa;
1546 struct sockaddr_in6 *sin6;
1547 int sin_loop, sin_local;
1548 int num_eligible_addr = 0;
1549
1550 IFADDR_FOREACH(ifa, ifn) {
1551 sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1552 if (sin6 == NULL)
1553 continue;
1554 if (stcb) {
1555 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
1556 /* It is restricted for some reason.. probably
1557 * not yet added.
1558 */
1559 continue;
1560 }
1561 }
1562 if (match_scope) {
1563 /* Here we are asked to match scope if possible */
1564 if (loopscope && sin_loop)
1565 /* src and destination are loopback scope */
1566 return (sin6);
1567 if (loc_scope && sin_local)
1568 /* src and destination are local scope */
1569 return (sin6);
1570 if ((loopscope == 0) &&
1571 (loc_scope == 0) &&
1572 (sin_loop == 0) &&
1573 (sin_local == 0)) {
1574 /* src and destination are global scope */
1575 return (sin6);
1576 }
1577 continue;
1578 }
1579 if (num_eligible_addr == cur_addr_num) {
1580 /* this is it */
1581 return (sin6);
1582 }
1583 num_eligible_addr++;
1584 }
1585 return (NULL);
1586 }
1587
1588
1589 static int
1590 sctp_count_v6_num_eligible_boundall (struct ifnet *ifn, struct sctp_tcb *stcb,
1591 int non_asoc_addr_ok, uint8_t loopscope, uint8_t loc_scope)
1592 {
1593 struct ifaddr *ifa;
1594 struct sockaddr_in6 *sin6;
1595 int num_eligible_addr = 0;
1596 int sin_loop, sin_local;
1597
1598 IFADDR_FOREACH(ifa, ifn) {
1599 sin6 = sctp_is_v6_ifa_addr_acceptable (ifa, loopscope, loc_scope, &sin_loop, &sin_local);
1600 if (sin6 == NULL)
1601 continue;
1602 if (stcb) {
1603 if ((non_asoc_addr_ok == 0) && sctp_is_addr_restricted(stcb, (struct sockaddr *)sin6)) {
1604 /* It is restricted for some reason.. probably
1605 * not yet added.
1606 */
1607 continue;
1608 }
1609 }
1610 num_eligible_addr++;
1611 }
1612 return (num_eligible_addr);
1613 }
1614
1615
1616 static struct sockaddr_in6 *
1617 sctp_choose_v6_boundall(struct sctp_inpcb *inp,
1618 struct sctp_tcb *stcb,
1619 struct sctp_nets *net,
1620 struct rtentry *rt,
1621 uint8_t loc_scope,
1622 uint8_t loopscope,
1623 int non_asoc_addr_ok)
1624 {
1625 /* Ok, we are bound all SO any address
1626 * is ok to use as long as it is NOT in the negative
1627 * list.
1628 */
1629 int num_eligible_addr;
1630 int cur_addr_num=0;
1631 int started_at_beginning=0;
1632 int match_scope_prefered;
1633 /* first question is, how many eligible addresses are
1634 * there for the destination ifn that we are using that
1635 * are within the proper scope?
1636 */
1637 struct ifnet *ifn;
1638 struct sockaddr_in6 *sin6;
1639
1640 ifn = rt->rt_ifp;
1641 if (net) {
1642 cur_addr_num = net->indx_of_eligible_next_to_use;
1643 }
1644 if (cur_addr_num == 0) {
1645 match_scope_prefered = 1;
1646 } else {
1647 match_scope_prefered = 0;
1648 }
1649 num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
1650 #ifdef SCTP_DEBUG
1651 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1652 printf("Found %d eligible source addresses\n", num_eligible_addr);
1653 }
1654 #endif
1655 if (num_eligible_addr == 0) {
1656 /* no eligible addresses, we must use some other
1657 * interface address if we can find one.
1658 */
1659 goto bound_all_v6_plan_b;
1660 }
1661 /* Ok we have num_eligible_addr set with how many we can use,
1662 * this may vary from call to call due to addresses being deprecated etc..
1663 */
1664 if (cur_addr_num >= num_eligible_addr) {
1665 cur_addr_num = 0;
1666 }
1667 /* select the nth address from the list (where cur_addr_num is the nth) and
1668 * 0 is the first one, 1 is the second one etc...
1669 */
1670 #ifdef SCTP_DEBUG
1671 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1672 printf("cur_addr_num:%d match_scope_prefered:%d select it\n",
1673 cur_addr_num, match_scope_prefered);
1674 }
1675 #endif
1676 sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
1677 loc_scope, cur_addr_num, match_scope_prefered);
1678 if (match_scope_prefered && (sin6 == NULL)) {
1679 /* retry without the preference for matching scope */
1680 #ifdef SCTP_DEBUG
1681 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1682 printf("retry with no match_scope_prefered\n");
1683 }
1684 #endif
1685 sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope,
1686 loc_scope, cur_addr_num, 0);
1687 }
1688 if (sin6) {
1689 #ifdef SCTP_DEBUG
1690 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1691 printf("Selected address %d ifn:%p for the route\n", cur_addr_num, ifn);
1692 }
1693 #endif
1694 if (net) {
1695 /* store so we get the next one */
1696 if (cur_addr_num < 255)
1697 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
1698 else
1699 net->indx_of_eligible_next_to_use = 0;
1700 }
1701 return (sin6);
1702 }
1703 num_eligible_addr = 0;
1704 bound_all_v6_plan_b:
1705 /* ok, if we reach here we either fell through
1706 * due to something changing during an interupt (unlikely)
1707 * or we have NO eligible source addresses for the ifn
1708 * of the route (most likely). We must look at all the other
1709 * interfaces EXCEPT rt->rt_ifp and do the same game.
1710 */
1711 #ifdef SCTP_DEBUG
1712 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1713 printf("bound-all Plan B\n");
1714 }
1715 #endif
1716 if (inp->next_ifn_touse == NULL) {
1717 started_at_beginning=1;
1718 inp->next_ifn_touse = IFNET_FIRST();
1719 #ifdef SCTP_DEBUG
1720 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1721 printf("Start at first IFN:%p\n", inp->next_ifn_touse);
1722 }
1723 #endif
1724 } else {
1725 inp->next_ifn_touse = IFNET_NEXT(inp->next_ifn_touse);
1726 #ifdef SCTP_DEBUG
1727 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1728 printf("Resume at IFN:%p\n", inp->next_ifn_touse);
1729 }
1730 #endif
1731 if (inp->next_ifn_touse == NULL) {
1732 #ifdef SCTP_DEBUG
1733 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1734 printf("IFN Resets\n");
1735 }
1736 #endif
1737 started_at_beginning=1;
1738 inp->next_ifn_touse = IFNET_FIRST();
1739 }
1740 }
1741 IFNET_FOREACH(ifn) {
1742 if (loopscope == 0 && ifn->if_type == IFT_LOOP) {
1743 /* wrong base scope */
1744 continue;
1745 }
1746 if (loc_scope && (ifn->if_index != loc_scope)) {
1747 /* by definition the scope (from to->sin6_scopeid)
1748 * must match that of the interface. If not then
1749 * we could pick a wrong scope for the address.
1750 * Ususally we don't hit plan-b since the route
1751 * handles this. However we can hit plan-b when
1752 * we send to local-host so the route is the
1753 * loopback interface, but the destination is a
1754 * link local.
1755 */
1756 continue;
1757 }
1758 if (ifn == rt->rt_ifp) {
1759 /* already looked at this guy */
1760 continue;
1761 }
1762 /* Address rotation will only work when we are not
1763 * rotating sourced interfaces and are using the interface
1764 * of the route. We would need to have a per interface index
1765 * in order to do proper rotation.
1766 */
1767 num_eligible_addr = sctp_count_v6_num_eligible_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope);
1768 #ifdef SCTP_DEBUG
1769 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1770 printf("IFN:%p has %d eligible\n", ifn, num_eligible_addr);
1771 }
1772 #endif
1773 if (num_eligible_addr == 0) {
1774 /* none we can use */
1775 continue;
1776 }
1777 /* Ok we have num_eligible_addr set with how many we can use,
1778 * this may vary from call to call due to addresses being deprecated etc..
1779 */
1780 inp->next_ifn_touse = ifn;
1781
1782 /* select the first one we can find with perference for matching scope.
1783 */
1784 sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 1);
1785 if (sin6 == NULL) {
1786 /* can't find one with matching scope how about a source with higher
1787 * scope
1788 */
1789 sin6 = sctp_select_v6_nth_addr_from_ifn_boundall (ifn, stcb, non_asoc_addr_ok, loopscope, loc_scope, 0, 0);
1790 if (sin6 == NULL)
1791 /* Hmm, can't find one in the interface now */
1792 continue;
1793 }
1794 #ifdef SCTP_DEBUG
1795 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1796 printf("Selected the %d'th address of ifn:%p\n",
1797 cur_addr_num, ifn);
1798 }
1799 #endif
1800 return (sin6);
1801 }
1802 if (started_at_beginning == 0) {
1803 /* we have not been through all of them yet, force
1804 * us to go through them all.
1805 */
1806 #ifdef SCTP_DEBUG
1807 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1808 printf("Force a recycle\n");
1809 }
1810 #endif
1811 inp->next_ifn_touse = NULL;
1812 goto bound_all_v6_plan_b;
1813 }
1814 return (NULL);
1815
1816 }
1817
1818 /* stcb and net may be NULL */
1819 struct in6_addr
1820 sctp_ipv6_source_address_selection(struct sctp_inpcb *inp,
1821 struct sctp_tcb *stcb, struct route *ro, struct sctp_nets *net,
1822 int non_asoc_addr_ok)
1823 {
1824 struct in6_addr ans;
1825 struct sockaddr_in6 *rt_addr;
1826 uint8_t loc_scope, loopscope;
1827 struct sockaddr_in6 to;
1828 struct rtentry *rt;
1829
1830 /*
1831 * This routine is tricky standard v6 src address
1832 * selection cannot take into account what we have
1833 * bound etc, so we can't use it.
1834 *
1835 * Instead here is what we must do:
1836 * 1) Make sure we have a route, if we
1837 * don't have a route we can never reach the peer.
1838 * 2) Once we have a route, determine the scope of the
1839 * route. Link local, loopback or global.
1840 * 3) Next we divide into three types. Either we
1841 * are bound all.. which means we want to use
1842 * one of the addresses of the interface we are
1843 * going out. <or>
1844 * 4a) We have not stcb, which means we are using the
1845 * specific addresses bound on an inp, in this
1846 * case we are similar to the stcb case (4b below)
1847 * accept the list is always a positive list.<or>
1848 * 4b) We are bound specific with a stcb, which means we have a
1849 * list of bound addresses and we must see if the
1850 * ifn of the route is actually one of the bound addresses.
1851 * If not, then we must rotate addresses amongst properly
1852 * scoped bound addresses, if so we use the address
1853 * of the interface.
1854 * 5) Always, no matter which path we take through the above
1855 * we must be sure the source address we use is allowed to
1856 * be used. I.e. IN6_IFF_DETACHED, IN6_IFF_NOTREADY, and IN6_IFF_ANYCAST
1857 * addresses cannot be used.
1858 * 6) Addresses that are deprecated MAY be used
1859 * if (!ip6_use_deprecated) {
1860 * if (IFA6_IS_DEPRECATED(ifa6)) {
1861 * skip the address
1862 * }
1863 * }
1864 */
1865
1866 /*** 1> determine route, if not already done */
1867 rt = rtcache_validate(ro);
1868 if (rt == NULL) {
1869 /*
1870 * Need a route to cache.
1871 */
1872 int scope_save;
1873
1874 memcpy(&to, rtcache_getdst(ro), sizeof(struct sockaddr));
1875 scope_save = to.sin6_scope_id;
1876 to.sin6_scope_id = 0;
1877
1878 rt = rtcache_lookup(ro, (struct sockaddr *)&to);
1879 to.sin6_scope_id = scope_save;
1880 }
1881 if (rt == NULL) {
1882 /*
1883 * no route to host. this packet is going no-where.
1884 * We probably should make sure we arrange to send back
1885 * an error.
1886 */
1887 #ifdef SCTP_DEBUG
1888 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1889 printf("No route to host, this packet cannot be sent!\n");
1890 }
1891 #endif
1892 memset(&ans, 0, sizeof(ans));
1893 return (ans);
1894 }
1895
1896 /*** 2a> determine scope for outbound address/route */
1897 loc_scope = loopscope = 0;
1898 /*
1899 * We base our scope on the outbound packet scope and route,
1900 * NOT the TCB (if there is one). This way in local scope we will only
1901 * use a local scope src address when we send to a local address.
1902 */
1903
1904 if (IN6_IS_ADDR_LOOPBACK(&to.sin6_addr)) {
1905 /* If the route goes to the loopback address OR
1906 * the address is a loopback address, we are loopback
1907 * scope.
1908 */
1909 #ifdef SCTP_DEBUG
1910 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1911 printf("Loopback scope is set\n");
1912 }
1913 #endif
1914 loc_scope = 0;
1915 loopscope = 1;
1916 if (net != NULL) {
1917 /* mark it as local */
1918 net->addr_is_local = 1;
1919 }
1920
1921 } else if (IN6_IS_ADDR_LINKLOCAL(&to.sin6_addr)) {
1922 #ifdef SCTP_DEBUG
1923 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1924 printf("Link local scope is set, id:%d\n", to.sin6_scope_id);
1925 }
1926 #endif
1927 if (to.sin6_scope_id)
1928 loc_scope = to.sin6_scope_id;
1929 else {
1930 loc_scope = 1;
1931 }
1932 loopscope = 0;
1933 } else {
1934 #ifdef SCTP_DEBUG
1935 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1936 printf("Global scope is set\n");
1937 }
1938 #endif
1939 }
1940
1941 /* now, depending on which way we are bound we call the appropriate
1942 * routine to do steps 3-6
1943 */
1944 #ifdef SCTP_DEBUG
1945 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1946 printf("Destination address:");
1947 sctp_print_address((struct sockaddr *)&to);
1948 }
1949 #endif
1950
1951 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1952 #ifdef SCTP_DEBUG
1953 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1954 printf("Calling bound-all src addr selection for v6\n");
1955 }
1956 #endif
1957 rt_addr = sctp_choose_v6_boundall(inp, stcb, net, rt, loc_scope, loopscope, non_asoc_addr_ok);
1958 } else {
1959 #ifdef SCTP_DEBUG
1960 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1961 printf("Calling bound-specific src addr selection for v6\n");
1962 }
1963 #endif
1964 if (stcb)
1965 rt_addr = sctp_choose_v6_boundspecific_stcb(inp, stcb, net, rt, loc_scope, loopscope, non_asoc_addr_ok);
1966 else
1967 /* we can't have a non-asoc address since we have no association */
1968 rt_addr = sctp_choose_v6_boundspecific_inp(inp, rt, loc_scope, loopscope);
1969 }
1970 if (rt_addr == NULL) {
1971 /* no suitable address? */
1972 struct in6_addr in6;
1973 #ifdef SCTP_DEBUG
1974 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1975 printf("V6 packet will reach dead-end no suitable src address\n");
1976 }
1977 #endif
1978 memset(&in6, 0, sizeof(in6));
1979 return (in6);
1980 }
1981 #ifdef SCTP_DEBUG
1982 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
1983 printf("Source address selected is:");
1984 sctp_print_address((struct sockaddr *)rt_addr);
1985 }
1986 #endif
1987 return (rt_addr->sin6_addr);
1988 }
1989
1990 static uint8_t
1991 sctp_get_ect(struct sctp_tcb *stcb,
1992 struct sctp_tmit_chunk *chk)
1993 {
1994 uint8_t this_random;
1995
1996 /* Huh? */
1997 if (sctp_ecn == 0)
1998 return (0);
1999
2000 if (sctp_ecn_nonce == 0)
2001 /* no nonce, always return ECT0 */
2002 return (SCTP_ECT0_BIT);
2003
2004 if (stcb->asoc.peer_supports_ecn_nonce == 0) {
2005 /* Peer does NOT support it, so we send a ECT0 only */
2006 return (SCTP_ECT0_BIT);
2007 }
2008
2009 if (chk == NULL)
2010 return (SCTP_ECT0_BIT);
2011
2012 if (((stcb->asoc.hb_random_idx == 3) &&
2013 (stcb->asoc.hb_ect_randombit > 7)) ||
2014 (stcb->asoc.hb_random_idx > 3)) {
2015 uint32_t rndval;
2016 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
2017 memcpy(stcb->asoc.hb_random_values, &rndval,
2018 sizeof(stcb->asoc.hb_random_values));
2019 this_random = stcb->asoc.hb_random_values[0];
2020 stcb->asoc.hb_random_idx = 0;
2021 stcb->asoc.hb_ect_randombit = 0;
2022 } else {
2023 if (stcb->asoc.hb_ect_randombit > 7) {
2024 stcb->asoc.hb_ect_randombit = 0;
2025 stcb->asoc.hb_random_idx++;
2026 }
2027 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
2028 }
2029 if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
2030 if (chk != NULL)
2031 /* ECN Nonce stuff */
2032 chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
2033 stcb->asoc.hb_ect_randombit++;
2034 return (SCTP_ECT1_BIT);
2035 } else {
2036 stcb->asoc.hb_ect_randombit++;
2037 return (SCTP_ECT0_BIT);
2038 }
2039 }
2040
2041 extern int sctp_no_csum_on_loopback;
2042
2043 static int
2044 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
2045 struct sctp_tcb *stcb, /* may be NULL */
2046 struct sctp_nets *net,
2047 const struct sockaddr *to,
2048 struct mbuf *m,
2049 int nofragment_flag,
2050 int ecn_ok,
2051 struct sctp_tmit_chunk *chk,
2052 int out_of_asoc_ok)
2053 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
2054 {
2055 /*
2056 * Given a mbuf chain (via m_next) that holds a packet header
2057 * WITH a SCTPHDR but no IP header, endpoint inp and sa structure.
2058 * - calculate SCTP checksum and fill in
2059 * - prepend a IP address header
2060 * - if boundall use INADDR_ANY
2061 * - if boundspecific do source address selection
2062 * - set fragmentation option for ipV4
2063 * - On return from IP output, check/adjust mtu size
2064 * - of output interface and smallest_mtu size as well.
2065 */
2066 struct sctphdr *sctphdr;
2067 int o_flgs;
2068 uint32_t csum;
2069 int ret;
2070 unsigned int have_mtu;
2071 struct route *ro;
2072 struct rtentry *rt;
2073
2074 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
2075 sctp_m_freem(m);
2076 return (EFAULT);
2077 }
2078 if ((m->m_flags & M_PKTHDR) == 0) {
2079 #ifdef SCTP_DEBUG
2080 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2081 printf("Software error: sctp_lowlevel_chunk_output() called with non pkthdr!\n");
2082 }
2083 #endif
2084 sctp_m_freem(m);
2085 return (EFAULT);
2086 }
2087 /* Calculate the csum and fill in the length of the packet */
2088 sctphdr = mtod(m, struct sctphdr *);
2089 have_mtu = 0;
2090 if (sctp_no_csum_on_loopback &&
2091 (stcb) &&
2092 (stcb->asoc.loopback_scope)) {
2093 sctphdr->checksum = 0;
2094 m->m_pkthdr.len = sctp_calculate_len(m);
2095 } else {
2096 sctphdr->checksum = 0;
2097 csum = sctp_calculate_sum(m, &m->m_pkthdr.len, 0);
2098 sctphdr->checksum = csum;
2099 }
2100 if (to->sa_family == AF_INET) {
2101 struct ip *ip;
2102 static struct route iproute;
2103 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
2104 if (m == NULL) {
2105 /* failed to prepend data, give up */
2106 return (ENOMEM);
2107 }
2108 ip = mtod(m, struct ip *);
2109 ip->ip_v = IPVERSION;
2110 ip->ip_hl = (sizeof(struct ip) >> 2);
2111 if (nofragment_flag) {
2112 ip->ip_off = htons(IP_DF);
2113 } else
2114 ip->ip_off = 0;
2115
2116 ip->ip_id = htons(ip_newid(NULL));
2117 ip->ip_ttl = inp->inp_ip_ttl;
2118 ip->ip_len = htons(m->m_pkthdr.len);
2119 if (stcb) {
2120 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
2121 /* Enable ECN */
2122 ip->ip_tos = (u_char)((inp->ip_inp.inp.inp_ip.ip_tos & 0x000000fc) |
2123 sctp_get_ect(stcb, chk));
2124 } else {
2125 /* No ECN */
2126 ip->ip_tos = inp->ip_inp.inp.inp_ip.ip_tos;
2127 }
2128 } else {
2129 /* no association at all */
2130 ip->ip_tos = inp->inp_ip_tos;
2131 }
2132 ip->ip_p = IPPROTO_SCTP;
2133 ip->ip_sum = 0;
2134 #ifdef SCTP_DEBUG
2135 printf("chunk_output: net %p\n", net);
2136 #endif
2137 if (net == NULL) {
2138 ro = &iproute;
2139 memset(&iproute, 0, sizeof(iproute));
2140 rtcache_lookup(ro, to);
2141 } else {
2142 ro = (struct route *)&net->ro;
2143 }
2144 /* Now the address selection part */
2145 ip->ip_dst.s_addr = satocsin(to)->sin_addr.s_addr;
2146
2147 /* call the routine to select the src address */
2148 if (net) {
2149 if (net->src_addr_selected == 0) {
2150 /* Cache the source address */
2151 ((struct sockaddr_in *)&net->_s_addr)->sin_addr = sctp_ipv4_source_address_selection(inp,
2152 stcb,
2153 ro, net, out_of_asoc_ok);
2154 if (rtcache_validate(ro)) {
2155 net->src_addr_selected = 1;
2156 }
2157 }
2158 ip->ip_src = ((struct sockaddr_in *)&net->_s_addr)->sin_addr;
2159 } else {
2160 ip->ip_src = sctp_ipv4_source_address_selection(inp,
2161 stcb, ro, net, out_of_asoc_ok);
2162 }
2163 #ifdef SCTP_DEBUG
2164 printf("src addr %x\n", ip->ip_src.s_addr);
2165 #endif
2166 /*
2167 * If source address selection fails and we find no route then
2168 * the ip_ouput should fail as well with a NO_ROUTE_TO_HOST
2169 * type error. We probably should catch that somewhere and
2170 * abort the association right away (assuming this is an INIT
2171 * being sent).
2172 */
2173 rt = rtcache_validate(ro);
2174 if ((rt == NULL)) {
2175 /*
2176 * src addr selection failed to find a route (or valid
2177 * source addr), so we can't get there from here!
2178 */
2179 #ifdef SCTP_DEBUG
2180 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2181 printf("low_level_output: dropped v4 packet- no valid source addr\n");
2182 printf("Destination was %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
2183 }
2184 #endif /* SCTP_DEBUG */
2185 if (net) {
2186 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
2187 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
2188 stcb,
2189 SCTP_FAILED_THRESHOLD,
2190 (void *)net);
2191 net->dest_state &= ~SCTP_ADDR_REACHABLE;
2192 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
2193 if (stcb) {
2194 if (net == stcb->asoc.primary_destination) {
2195 /* need a new primary */
2196 struct sctp_nets *alt;
2197 alt = sctp_find_alternate_net(stcb, net);
2198 if (alt != net) {
2199 if (sctp_set_primary_addr(stcb,
2200 (struct sockaddr *)NULL,
2201 alt) == 0) {
2202 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
2203 net->src_addr_selected = 0;
2204 }
2205 }
2206 }
2207 }
2208 }
2209 sctp_m_freem(m);
2210 return (EHOSTUNREACH);
2211 } else {
2212 have_mtu = rt->rt_ifp->if_mtu;
2213 }
2214
2215 o_flgs = (IP_RAWOUTPUT | (inp->sctp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)));
2216 #ifdef SCTP_DEBUG
2217 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2218 printf("Calling ipv4 output routine from low level src addr:%x\n",
2219 (u_int)(ntohl(ip->ip_src.s_addr)));
2220 printf("Destination is %x\n", (u_int)(ntohl(ip->ip_dst.s_addr)));
2221 printf("RTP route is %p through\n", rt);
2222 printf("length %d\n", ip->ip_len);
2223 }
2224 #endif
2225 if ((have_mtu) && (net) && (have_mtu > net->mtu)) {
2226 rt->rt_ifp->if_mtu = net->mtu;
2227 }
2228 ret = ip_output(m, inp->ip_inp.inp.inp_options,
2229 ro, o_flgs, inp->ip_inp.inp.inp_moptions,
2230 (struct socket *)inp->sctp_socket);
2231 if ((rt) && (have_mtu) && (net) && (have_mtu > net->mtu)) {
2232 rt->rt_ifp->if_mtu = have_mtu;
2233 }
2234 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
2235 #ifdef SCTP_DEBUG
2236 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2237 printf("Ip output returns %d\n", ret);
2238 }
2239 #endif
2240 if (net == NULL) {
2241 } else {
2242 /* PMTU check versus smallest asoc MTU goes here */
2243 if (rt != NULL) {
2244 if (rt->rt_rmx.rmx_mtu &&
2245 (stcb->asoc.smallest_mtu > rt->rt_rmx.rmx_mtu)) {
2246 sctp_mtu_size_reset(inp, &stcb->asoc,
2247 rt->rt_rmx.rmx_mtu);
2248 }
2249 } else {
2250 /* route was freed */
2251 net->src_addr_selected = 0;
2252 }
2253 }
2254 return (ret);
2255 }
2256 #ifdef INET6
2257 else if (to->sa_family == AF_INET6) {
2258 struct ip6_hdr *ip6h;
2259 static struct route ip6route;
2260 struct ifnet *ifp;
2261 u_char flowTop;
2262 uint16_t flowBottom;
2263 u_char tosBottom, tosTop;
2264 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
2265 int prev_scope=0;
2266 u_short prev_port=0;
2267
2268 M_PREPEND(m, sizeof(struct ip6_hdr), M_DONTWAIT);
2269 if (m == NULL) {
2270 /* failed to prepend data, give up */
2271 return (ENOMEM);
2272 }
2273 ip6h = mtod(m, struct ip6_hdr *);
2274
2275 /*
2276 * We assume here that inp_flow is in host byte order within
2277 * the TCB!
2278 */
2279 flowBottom = ((struct in6pcb *)inp)->in6p_flowinfo & 0x0000ffff;
2280 flowTop = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x000f0000) >> 16);
2281
2282 tosTop = (((((struct in6pcb *)inp)->in6p_flowinfo & 0xf0) >> 4) | IPV6_VERSION);
2283
2284 /* protect *sin6 from overwrite */
2285 memcpy(&tmp, to, sizeof(struct sockaddr_in6));
2286 sin6 = &tmp;
2287
2288 /* KAME hack: embed scopeid */
2289 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
2290 if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
2291 #else
2292 /*
2293 * XXX: appropriate scope zone must be provided or otherwise
2294 * ip6_use_defzone must be 1.
2295 */
2296 if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
2297 #endif
2298 return (EINVAL);
2299 if (net == NULL) {
2300 memset(&ip6route, 0, sizeof(ip6route));
2301 ro = (struct route *)&ip6route;
2302 rtcache_lookup(ro, (struct sockaddr *) sin6);
2303 } else {
2304 ro = (struct route *)&net->ro;
2305 }
2306 if (stcb != NULL) {
2307 if ((stcb->asoc.ecn_allowed) && ecn_ok) {
2308 /* Enable ECN */
2309 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
2310 } else {
2311 /* No ECN */
2312 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
2313 }
2314 } else {
2315 /* we could get no asoc if it is a O-O-T-B packet */
2316 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
2317 }
2318 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom|flowTop) << 16) | flowBottom));
2319 ip6h->ip6_nxt = IPPROTO_SCTP;
2320 ip6h->ip6_plen = m->m_pkthdr.len;
2321 ip6h->ip6_dst = sin6->sin6_addr;
2322
2323 /*
2324 * Add SRC address selection here:
2325 * we can only reuse to a limited degree the kame src-addr-sel,
2326 * since we can try their selection but it may not be bound.
2327 */
2328 memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
2329 lsa6_tmp.sin6_family = AF_INET6;
2330 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
2331 lsa6 = &lsa6_tmp;
2332 rt = rtcache_validate(ro);
2333 if (net) {
2334 if (net->src_addr_selected == 0) {
2335 /* Cache the source address */
2336 ((struct sockaddr_in6 *)&net->_s_addr)->sin6_addr = sctp_ipv6_source_address_selection(inp,
2337 stcb, ro, net, out_of_asoc_ok);
2338
2339 if (rt != NULL) {
2340 net->src_addr_selected = 1;
2341 }
2342 }
2343 lsa6->sin6_addr = ((struct sockaddr_in6 *)&net->_s_addr)->sin6_addr;
2344 } else {
2345 lsa6->sin6_addr = sctp_ipv6_source_address_selection(
2346 inp, stcb, ro, net, out_of_asoc_ok);
2347 }
2348 lsa6->sin6_port = inp->sctp_lport;
2349
2350 if ((rt == NULL)) {
2351 /*
2352 * src addr selection failed to find a route (or valid
2353 * source addr), so we can't get there from here!
2354 */
2355 #ifdef SCTP_DEBUG
2356 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2357 printf("low_level_output: dropped v6 pkt- no valid source addr\n");
2358 }
2359 #endif
2360 sctp_m_freem(m);
2361 if (net) {
2362 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb)
2363 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
2364 stcb,
2365 SCTP_FAILED_THRESHOLD,
2366 (void *)net);
2367 net->dest_state &= ~SCTP_ADDR_REACHABLE;
2368 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
2369 if (stcb) {
2370 if (net == stcb->asoc.primary_destination) {
2371 /* need a new primary */
2372 struct sctp_nets *alt;
2373 alt = sctp_find_alternate_net(stcb, net);
2374 if (alt != net) {
2375 if (sctp_set_primary_addr(stcb,
2376 (struct sockaddr *)NULL,
2377 alt) == 0) {
2378 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
2379 net->src_addr_selected = 0;
2380 }
2381 }
2382 }
2383 }
2384 }
2385 return (EHOSTUNREACH);
2386 }
2387
2388 ip6h->ip6_src = lsa6->sin6_addr;
2389
2390 /*
2391 * We set the hop limit now since there is a good chance that
2392 * our ro pointer is now filled
2393 */
2394 ip6h->ip6_hlim = in6_selecthlim((struct in6pcb *)&inp->ip_inp.inp,
2395 (ro ?
2396 (rt ? (rt->rt_ifp) : (NULL)) :
2397 (NULL)));
2398 o_flgs = 0;
2399 ifp = rt->rt_ifp;
2400 #ifdef SCTP_DEBUG
2401 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2402 /* Copy to be sure something bad is not happening */
2403 sin6->sin6_addr = ip6h->ip6_dst;
2404 lsa6->sin6_addr = ip6h->ip6_src;
2405
2406 printf("Calling ipv6 output routine from low level\n");
2407 printf("src: ");
2408 sctp_print_address((struct sockaddr *)lsa6);
2409 printf("dst: ");
2410 sctp_print_address((struct sockaddr *)sin6);
2411 }
2412 #endif /* SCTP_DEBUG */
2413 if (net) {
2414 sin6 = (struct sockaddr_in6 *)&net->ro.ro_sa;
2415 /* preserve the port and scope for link local send */
2416 prev_scope = sin6->sin6_scope_id;
2417 prev_port = sin6->sin6_port;
2418 }
2419 ret = ip6_output(m, ((struct in6pcb *)inp)->in6p_outputopts,
2420 ro,
2421 o_flgs,
2422 ((struct in6pcb *)inp)->in6p_moptions,
2423 (struct socket *)inp->sctp_socket,
2424 &ifp);
2425 if (net) {
2426 /* for link local this must be done */
2427 sin6->sin6_scope_id = prev_scope;
2428 sin6->sin6_port = prev_port;
2429 }
2430 #ifdef SCTP_DEBUG
2431 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
2432 printf("return from send is %d\n", ret);
2433 }
2434 #endif /* SCTP_DEBUG_OUTPUT */
2435 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
2436 if (net) {
2437 /* PMTU check versus smallest asoc MTU goes here */
2438 rt = rtcache_validate(ro);
2439 if (rt == NULL) {
2440 /* Route was freed */
2441 net->src_addr_selected = 0;
2442 }
2443 if (rt != NULL) {
2444 if (rt->rt_rmx.rmx_mtu &&
2445 (stcb->asoc.smallest_mtu > rt->rt_rmx.rmx_mtu)) {
2446 sctp_mtu_size_reset(inp,
2447 &stcb->asoc,
2448 rt->rt_rmx.rmx_mtu);
2449 }
2450 } else if (ifp) {
2451 if (ND_IFINFO(ifp)->linkmtu &&
2452 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
2453 sctp_mtu_size_reset(inp,
2454 &stcb->asoc,
2455 ND_IFINFO(ifp)->linkmtu);
2456 }
2457 }
2458 }
2459 return (ret);
2460 }
2461 #endif
2462 else {
2463 #ifdef SCTP_DEBUG
2464 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
2465 printf("Unknown protocol (TSNH) type %d\n", ((const struct sockaddr *)to)->sa_family);
2466 }
2467 #endif
2468 sctp_m_freem(m);
2469 return (EFAULT);
2470 }
2471 }
2472
2473 static
2474 int sctp_is_address_in_scope(struct ifaddr *ifa,
2475 int ipv4_addr_legal,
2476 int ipv6_addr_legal,
2477 int loopback_scope,
2478 int ipv4_local_scope,
2479 int local_scope,
2480 int site_scope)
2481 {
2482 if ((loopback_scope == 0) &&
2483 (ifa->ifa_ifp) &&
2484 (ifa->ifa_ifp->if_type == IFT_LOOP)) {
2485 /* skip loopback if not in scope *
2486 */
2487 return (0);
2488 }
2489 if ((ifa->ifa_addr->sa_family == AF_INET) && ipv4_addr_legal) {
2490 struct sockaddr_in *sin;
2491 sin = (struct sockaddr_in *)ifa->ifa_addr;
2492 if (sin->sin_addr.s_addr == 0) {
2493 /* not in scope , unspecified */
2494 return (0);
2495 }
2496 if ((ipv4_local_scope == 0) &&
2497 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
2498 /* private address not in scope */
2499 return (0);
2500 }
2501 } else if ((ifa->ifa_addr->sa_family == AF_INET6) && ipv6_addr_legal) {
2502 struct sockaddr_in6 *sin6;
2503 struct in6_ifaddr *ifa6;
2504
2505 ifa6 = (struct in6_ifaddr *)ifa;
2506 /* ok to use deprecated addresses? */
2507 if (!ip6_use_deprecated) {
2508 if (ifa6->ia6_flags &
2509 IN6_IFF_DEPRECATED) {
2510 return (0);
2511 }
2512 }
2513 if (ifa6->ia6_flags &
2514 (IN6_IFF_DETACHED |
2515 IN6_IFF_ANYCAST |
2516 IN6_IFF_NOTREADY)) {
2517 return (0);
2518 }
2519 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
2520 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2521 /* skip unspecifed addresses */
2522 return (0);
2523 }
2524 if (/*(local_scope == 0) && */
2525 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
2526 return (0);
2527 }
2528 if ((site_scope == 0) &&
2529 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
2530 return (0);
2531 }
2532 } else {
2533 return (0);
2534 }
2535 return (1);
2536 }
2537
2538
2539 void
2540 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2541 {
2542 struct mbuf *m, *m_at, *m_last;
2543 struct sctp_nets *net;
2544 struct sctp_init_msg *initm;
2545 struct sctp_supported_addr_param *sup_addr;
2546 struct sctp_ecn_supported_param *ecn;
2547 struct sctp_prsctp_supported_param *prsctp;
2548 struct sctp_ecn_nonce_supported_param *ecn_nonce;
2549 struct sctp_supported_chunk_types_param *pr_supported;
2550 int cnt_inits_to=0;
2551 int padval, ret;
2552
2553 /* INIT's always go to the primary (and usually ONLY address) */
2554 m_last = NULL;
2555 net = stcb->asoc.primary_destination;
2556 if (net == NULL) {
2557 net = TAILQ_FIRST(&stcb->asoc.nets);
2558 if (net == NULL) {
2559 /* TSNH */
2560 return;
2561 }
2562 /* we confirm any address we send an INIT to */
2563 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2564 sctp_set_primary_addr(stcb, NULL, net);
2565 } else {
2566 /* we confirm any address we send an INIT to */
2567 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
2568 }
2569 #ifdef SCTP_DEBUG
2570 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2571 printf("Sending INIT to ");
2572 sctp_print_address (rtcache_getdst(&net->ro));
2573 }
2574 #endif
2575 if (rtcache_getdst(&net->ro)->sa_family == AF_INET6) {
2576 /* special hook, if we are sending to link local
2577 * it will not show up in our private address count.
2578 */
2579 if (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *) rtcache_getdst(&net->ro)->sa_data))
2580 cnt_inits_to = 1;
2581 }
2582 if (callout_pending(&net->rxt_timer.timer)) {
2583 /* This case should not happen */
2584 return;
2585 }
2586 /* start the INIT timer */
2587 if (sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net)) {
2588 /* we are hosed since I can't start the INIT timer? */
2589 return;
2590 }
2591 MGETHDR(m, M_DONTWAIT, MT_HEADER);
2592 if (m == NULL) {
2593 /* No memory, INIT timer will re-attempt. */
2594 return;
2595 }
2596 /* make it into a M_EXT */
2597 MCLGET(m, M_DONTWAIT);
2598 if ((m->m_flags & M_EXT) != M_EXT) {
2599 /* Failed to get cluster buffer */
2600 sctp_m_freem(m);
2601 return;
2602 }
2603 m->m_data += SCTP_MIN_OVERHEAD;
2604 m->m_len = sizeof(struct sctp_init_msg);
2605 /* Now lets put the SCTP header in place */
2606 initm = mtod(m, struct sctp_init_msg *);
2607 initm->sh.src_port = inp->sctp_lport;
2608 initm->sh.dest_port = stcb->rport;
2609 initm->sh.v_tag = 0;
2610 initm->sh.checksum = 0; /* calculate later */
2611 /* now the chunk header */
2612 initm->msg.ch.chunk_type = SCTP_INITIATION;
2613 initm->msg.ch.chunk_flags = 0;
2614 /* fill in later from mbuf we build */
2615 initm->msg.ch.chunk_length = 0;
2616 /* place in my tag */
2617 initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
2618 /* set up some of the credits. */
2619 initm->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.sb_hiwat,
2620 SCTP_MINIMAL_RWND));
2621
2622 initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
2623 initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
2624 initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
2625 /* now the address restriction */
2626 sup_addr = (struct sctp_supported_addr_param *)((vaddr_t)initm +
2627 sizeof(*initm));
2628 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
2629 /* we support 2 types IPv6/IPv4 */
2630 sup_addr->ph.param_length = htons(sizeof(*sup_addr) +
2631 sizeof(uint16_t));
2632 sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
2633 sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
2634 m->m_len += sizeof(*sup_addr) + sizeof(uint16_t);
2635
2636 /* if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
2637 if (inp->sctp_ep.adaption_layer_indicator) {
2638 struct sctp_adaption_layer_indication *ali;
2639 ali = (struct sctp_adaption_layer_indication *)(
2640 (vaddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
2641 ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
2642 ali->ph.param_length = htons(sizeof(*ali));
2643 ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
2644 m->m_len += sizeof(*ali);
2645 ecn = (struct sctp_ecn_supported_param *)((vaddr_t)ali +
2646 sizeof(*ali));
2647 } else {
2648 ecn = (struct sctp_ecn_supported_param *)((vaddr_t)sup_addr +
2649 sizeof(*sup_addr) + sizeof(uint16_t));
2650 }
2651
2652 /* now any cookie time extensions */
2653 if (stcb->asoc.cookie_preserve_req) {
2654 struct sctp_cookie_perserve_param *cookie_preserve;
2655 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
2656 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
2657 cookie_preserve->ph.param_length = htons(
2658 sizeof(*cookie_preserve));
2659 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
2660 m->m_len += sizeof(*cookie_preserve);
2661 ecn = (struct sctp_ecn_supported_param *)(
2662 (vaddr_t)cookie_preserve + sizeof(*cookie_preserve));
2663 stcb->asoc.cookie_preserve_req = 0;
2664 }
2665
2666 /* ECN parameter */
2667 if (sctp_ecn == 1) {
2668 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
2669 ecn->ph.param_length = htons(sizeof(*ecn));
2670 m->m_len += sizeof(*ecn);
2671 prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn +
2672 sizeof(*ecn));
2673 } else {
2674 prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn);
2675 }
2676 /* And now tell the peer we do pr-sctp */
2677 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
2678 prsctp->ph.param_length = htons(sizeof(*prsctp));
2679 m->m_len += sizeof(*prsctp);
2680
2681
2682 /* And now tell the peer we do all the extensions */
2683 pr_supported = (struct sctp_supported_chunk_types_param *)((vaddr_t)prsctp +
2684 sizeof(*prsctp));
2685
2686 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
2687 pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
2688 pr_supported->chunk_types[0] = SCTP_ASCONF;
2689 pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
2690 pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
2691 pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
2692 pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
2693 pr_supported->chunk_types[5] = 0; /* pad */
2694 pr_supported->chunk_types[6] = 0; /* pad */
2695 pr_supported->chunk_types[7] = 0; /* pad */
2696
2697 m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
2698 /* ECN nonce: And now tell the peer we support ECN nonce */
2699
2700 if (sctp_ecn_nonce) {
2701 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((vaddr_t)pr_supported +
2702 sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
2703 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
2704 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
2705 m->m_len += sizeof(*ecn_nonce);
2706 }
2707
2708 m_at = m;
2709 /* now the addresses */
2710 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2711 struct ifnet *ifn;
2712 struct ifaddr *ifa;
2713 int cnt;
2714
2715 cnt = cnt_inits_to;
2716 IFNET_FOREACH(ifn) {
2717 if ((stcb->asoc.loopback_scope == 0) &&
2718 (ifn->if_type == IFT_LOOP)) {
2719 /*
2720 * Skip loopback devices if loopback_scope
2721 * not set
2722 */
2723 continue;
2724 }
2725 IFADDR_FOREACH(ifa, ifn) {
2726 if (sctp_is_address_in_scope(ifa,
2727 stcb->asoc.ipv4_addr_legal,
2728 stcb->asoc.ipv6_addr_legal,
2729 stcb->asoc.loopback_scope,
2730 stcb->asoc.ipv4_local_scope,
2731 stcb->asoc.local_scope,
2732 stcb->asoc.site_scope) == 0) {
2733 continue;
2734 }
2735 cnt++;
2736 }
2737 }
2738 if (cnt > 1) {
2739 IFNET_FOREACH(ifn) {
2740 if ((stcb->asoc.loopback_scope == 0) &&
2741 (ifn->if_type == IFT_LOOP)) {
2742 /*
2743 * Skip loopback devices if loopback_scope
2744 * not set
2745 */
2746 continue;
2747 }
2748 IFADDR_FOREACH(ifa, ifn) {
2749 if (sctp_is_address_in_scope(ifa,
2750 stcb->asoc.ipv4_addr_legal,
2751 stcb->asoc.ipv6_addr_legal,
2752 stcb->asoc.loopback_scope,
2753 stcb->asoc.ipv4_local_scope,
2754 stcb->asoc.local_scope,
2755 stcb->asoc.site_scope) == 0) {
2756 continue;
2757 }
2758 m_at = sctp_add_addr_to_mbuf(m_at, ifa);
2759 }
2760 }
2761 }
2762 } else {
2763 struct sctp_laddr *laddr;
2764 int cnt;
2765 cnt = cnt_inits_to;
2766 /* First, how many ? */
2767 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2768 if (laddr->ifa == NULL) {
2769 continue;
2770 }
2771 if (laddr->ifa->ifa_addr == NULL)
2772 continue;
2773 if (sctp_is_address_in_scope(laddr->ifa,
2774 stcb->asoc.ipv4_addr_legal,
2775 stcb->asoc.ipv6_addr_legal,
2776 stcb->asoc.loopback_scope,
2777 stcb->asoc.ipv4_local_scope,
2778 stcb->asoc.local_scope,
2779 stcb->asoc.site_scope) == 0) {
2780 continue;
2781 }
2782 cnt++;
2783 }
2784 /* To get through a NAT we only list addresses if
2785 * we have more than one. That way if you just
2786 * bind a single address we let the source of the init
2787 * dictate our address.
2788 */
2789 if (cnt > 1) {
2790 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2791 if (laddr->ifa == NULL) {
2792 continue;
2793 }
2794 if (laddr->ifa->ifa_addr == NULL) {
2795 continue;
2796 }
2797
2798 if (sctp_is_address_in_scope(laddr->ifa,
2799 stcb->asoc.ipv4_addr_legal,
2800 stcb->asoc.ipv6_addr_legal,
2801 stcb->asoc.loopback_scope,
2802 stcb->asoc.ipv4_local_scope,
2803 stcb->asoc.local_scope,
2804 stcb->asoc.site_scope) == 0) {
2805 continue;
2806 }
2807 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2808 }
2809 }
2810 }
2811 /* calulate the size and update pkt header and chunk header */
2812 m->m_pkthdr.len = 0;
2813 for (m_at = m; m_at; m_at = m_at->m_next) {
2814 if (m_at->m_next == NULL)
2815 m_last = m_at;
2816 m->m_pkthdr.len += m_at->m_len;
2817 }
2818 initm->msg.ch.chunk_length = htons((m->m_pkthdr.len -
2819 sizeof(struct sctphdr)));
2820 #ifdef SCTP_DEBUG
2821 printf("chunk_length %d\n", ntohs(initm->msg.ch.chunk_length));
2822 #endif
2823 /* We pass 0 here to NOT set IP_DF if its IPv4, we
2824 * ignore the return here since the timer will drive
2825 * a retranmission.
2826 */
2827
2828 /* I don't expect this to execute but we will be safe here */
2829 padval = m->m_pkthdr.len % 4;
2830 if ((padval) && (m_last)) {
2831 /* The compiler worries that m_last may not be
2832 * set even though I think it is impossible :->
2833 * however we add m_last here just in case.
2834 */
2835 ret = sctp_add_pad_tombuf(m_last, (4-padval));
2836 if (ret) {
2837 /* Houston we have a problem, no space */
2838 sctp_m_freem(m);
2839 return;
2840 }
2841 m->m_pkthdr.len += padval;
2842 }
2843 #ifdef SCTP_DEBUG
2844 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2845 printf("Calling lowlevel output stcb:%p net:%p\n",
2846 stcb, net);
2847 }
2848 #endif
2849 ret = sctp_lowlevel_chunk_output(inp, stcb, net,
2850 rtcache_getdst(&net->ro), m, 0, 0, NULL, 0);
2851 #ifdef SCTP_DEBUG
2852 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2853 printf("Low level output returns %d\n", ret);
2854 }
2855 #endif
2856 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
2857 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
2858 }
2859
2860 struct mbuf *
2861 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
2862 int param_offset, int *abort_processing, struct sctp_chunkhdr *cp)
2863 {
2864 /* Given a mbuf containing an INIT or INIT-ACK
2865 * with the param_offset being equal to the
2866 * beginning of the params i.e. (iphlen + sizeof(struct sctp_init_msg)
2867 * parse through the parameters to the end of the mbuf verifying
2868 * that all parameters are known.
2869 *
2870 * For unknown parameters build and return a mbuf with
2871 * UNRECOGNIZED_PARAMETER errors. If the flags indicate
2872 * to stop processing this chunk stop, and set *abort_processing
2873 * to 1.
2874 *
2875 * By having param_offset be pre-set to where parameters begin
2876 * it is hoped that this routine may be reused in the future
2877 * by new features.
2878 */
2879 struct sctp_paramhdr *phdr, params;
2880
2881 struct mbuf *mat, *op_err;
2882 char tempbuf[2048];
2883 int at, limit, pad_needed;
2884 uint16_t ptype, plen;
2885 int err_at;
2886
2887 *abort_processing = 0;
2888 mat = in_initpkt;
2889 err_at = 0;
2890 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
2891 #ifdef SCTP_DEBUG
2892 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2893 printf("Limit is %d bytes\n", limit);
2894 }
2895 #endif
2896 at = param_offset;
2897 op_err = NULL;
2898
2899 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
2900 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
2901 ptype = ntohs(phdr->param_type);
2902 plen = ntohs(phdr->param_length);
2903 limit -= SCTP_SIZE32(plen);
2904 if (plen < sizeof(struct sctp_paramhdr)) {
2905 #ifdef SCTP_DEBUG
2906 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2907 printf("sctp_output.c:Impossible length in parameter < %d\n", plen);
2908 }
2909 #endif
2910 *abort_processing = 1;
2911 break;
2912 }
2913 /* All parameters for all chunks that we
2914 * know/understand are listed here. We process
2915 * them other places and make appropriate
2916 * stop actions per the upper bits. However
2917 * this is the generic routine processor's can
2918 * call to get back an operr.. to either incorporate (init-ack)
2919 * or send.
2920 */
2921 if ((ptype == SCTP_HEARTBEAT_INFO) ||
2922 (ptype == SCTP_IPV4_ADDRESS) ||
2923 (ptype == SCTP_IPV6_ADDRESS) ||
2924 (ptype == SCTP_STATE_COOKIE) ||
2925 (ptype == SCTP_UNRECOG_PARAM) ||
2926 (ptype == SCTP_COOKIE_PRESERVE) ||
2927 (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
2928 (ptype == SCTP_PRSCTP_SUPPORTED) ||
2929 (ptype == SCTP_ADD_IP_ADDRESS) ||
2930 (ptype == SCTP_DEL_IP_ADDRESS) ||
2931 (ptype == SCTP_ECN_CAPABLE) ||
2932 (ptype == SCTP_ULP_ADAPTION) ||
2933 (ptype == SCTP_ERROR_CAUSE_IND) ||
2934 (ptype == SCTP_SET_PRIM_ADDR) ||
2935 (ptype == SCTP_SUCCESS_REPORT) ||
2936 (ptype == SCTP_ULP_ADAPTION) ||
2937 (ptype == SCTP_SUPPORTED_CHUNK_EXT) ||
2938 (ptype == SCTP_ECN_NONCE_SUPPORTED)
2939 ) {
2940 /* no skip it */
2941 at += SCTP_SIZE32(plen);
2942 } else if (ptype == SCTP_HOSTNAME_ADDRESS) {
2943 /* We can NOT handle HOST NAME addresses!! */
2944 #ifdef SCTP_DEBUG
2945 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2946 printf("Can't handle hostname addresses.. abort processing\n");
2947 }
2948 #endif
2949 *abort_processing = 1;
2950 if (op_err == NULL) {
2951 /* Ok need to try to get a mbuf */
2952 MGETHDR(op_err, M_DONTWAIT, MT_DATA);
2953 if (op_err) {
2954 op_err->m_len = 0;
2955 op_err->m_pkthdr.len = 0;
2956 /* pre-reserve space for ip and sctp header and chunk hdr*/
2957 op_err->m_data += sizeof(struct ip6_hdr);
2958 op_err->m_data += sizeof(struct sctphdr);
2959 op_err->m_data += sizeof(struct sctp_chunkhdr);
2960 }
2961 }
2962 if (op_err) {
2963 /* If we have space */
2964 struct sctp_paramhdr s;
2965 if (err_at % 4) {
2966 u_int32_t cpthis=0;
2967 pad_needed = 4 - (err_at % 4);
2968 m_copyback(op_err, err_at, pad_needed, (void *)&cpthis);
2969 err_at += pad_needed;
2970 }
2971 s.param_type = htons(SCTP_CAUSE_UNRESOLV_ADDR);
2972 s.param_length = htons(sizeof(s) + plen);
2973 m_copyback(op_err, err_at, sizeof(s), (void *)&s);
2974 err_at += sizeof(s);
2975 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
2976 if (phdr == NULL) {
2977 sctp_m_freem(op_err);
2978 /* we are out of memory but we
2979 * still need to have a look at what to
2980 * do (the system is in trouble though).
2981 */
2982 return (NULL);
2983 }
2984 m_copyback(op_err, err_at, plen, (void *)phdr);
2985 err_at += plen;
2986 }
2987 return (op_err);
2988 } else {
2989 /* we do not recognize the parameter
2990 * figure out what we do.
2991 */
2992 #ifdef SCTP_DEBUG
2993 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
2994 printf("Got parameter type %x - unknown\n",
2995 (u_int)ptype);
2996 }
2997 #endif
2998 if ((ptype & 0x4000) == 0x4000) {
2999 /* Report bit is set?? */
3000 #ifdef SCTP_DEBUG
3001 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3002 printf("Report bit is set\n");
3003 }
3004 #endif
3005 if (op_err == NULL) {
3006 /* Ok need to try to get an mbuf */
3007 MGETHDR(op_err, M_DONTWAIT, MT_DATA);
3008 if (op_err) {
3009 op_err->m_len = 0;
3010 op_err->m_pkthdr.len = 0;
3011 op_err->m_data += sizeof(struct ip6_hdr);
3012 op_err->m_data += sizeof(struct sctphdr);
3013 op_err->m_data += sizeof(struct sctp_chunkhdr);
3014 }
3015 }
3016 if (op_err) {
3017 /* If we have space */
3018 struct sctp_paramhdr s;
3019 if (err_at % 4) {
3020 u_int32_t cpthis=0;
3021 pad_needed = 4 - (err_at % 4);
3022 m_copyback(op_err, err_at, pad_needed, (void *)&cpthis);
3023 err_at += pad_needed;
3024 }
3025 s.param_type = htons(SCTP_UNRECOG_PARAM);
3026 s.param_length = htons(sizeof(s) + plen);
3027 m_copyback(op_err, err_at, sizeof(s), (void *)&s);
3028 err_at += sizeof(s);
3029 if (plen > sizeof(tempbuf)) {
3030 plen = sizeof(tempbuf);
3031 }
3032 phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, plen);
3033 if (phdr == NULL) {
3034 sctp_m_freem(op_err);
3035 /* we are out of memory but we
3036 * still need to have a look at what to
3037 * do (the system is in trouble though).
3038 */
3039 goto more_processing;
3040 }
3041 m_copyback(op_err, err_at, plen, (void *)phdr);
3042 err_at += plen;
3043 }
3044 }
3045 more_processing:
3046 if ((ptype & 0x8000) == 0x0000) {
3047 #ifdef SCTP_DEBUG
3048 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
3049 printf("Abort bit is now setting1\n");
3050 }
3051 #endif
3052 return (op_err);
3053 } else {
3054 /* skip this chunk and continue processing */
3055 at += SCTP_SIZE32(plen);
3056 }
3057
3058 }
3059 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
3060 }
3061 return (op_err);
3062 }
3063
3064 static int
3065 sctp_are_there_new_addresses(struct sctp_association *asoc,
3066 struct mbuf *in_initpkt, int iphlen, int offset)
3067 {
3068 /*
3069 * Given a INIT packet, look through the packet to verify that
3070 * there are NO new addresses. As we go through the parameters
3071 * add reports of any un-understood parameters that require an
3072 * error. Also we must return (1) to drop the packet if we see
3073 * a un-understood parameter that tells us to drop the chunk.
3074 */
3075 struct sockaddr_in sin4, *sa4;
3076 struct sockaddr_in6 sin6, *sa6;
3077 struct sockaddr *sa_touse;
3078 struct sockaddr *sa;
3079 struct sctp_paramhdr *phdr, params;
3080 struct ip *iph;
3081 struct mbuf *mat;
3082 uint16_t ptype, plen;
3083 uint8_t fnd;
3084 struct sctp_nets *net;
3085
3086 memset(&sin4, 0, sizeof(sin4));
3087 memset(&sin6, 0, sizeof(sin6));
3088 sin4.sin_family = AF_INET;
3089 sin4.sin_len = sizeof(sin4);
3090 sin6.sin6_family = AF_INET6;
3091 sin6.sin6_len = sizeof(sin6);
3092
3093 sa_touse = NULL;
3094 /* First what about the src address of the pkt ? */
3095 iph = mtod(in_initpkt, struct ip *);
3096 if (iph->ip_v == IPVERSION) {
3097 /* source addr is IPv4 */
3098 sin4.sin_addr = iph->ip_src;
3099 sa_touse = (struct sockaddr *)&sin4;
3100 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3101 /* source addr is IPv6 */
3102 struct ip6_hdr *ip6h;
3103 ip6h = mtod(in_initpkt, struct ip6_hdr *);
3104 sin6.sin6_addr = ip6h->ip6_src;
3105 sa_touse = (struct sockaddr *)&sin6;
3106 } else {
3107 return (1);
3108 }
3109
3110 fnd = 0;
3111 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3112 sa = (struct sockaddr *)&net->ro.ro_sa;
3113 if (sa->sa_family == sa_touse->sa_family) {
3114 if (sa->sa_family == AF_INET) {
3115 sa4 = (struct sockaddr_in *)sa;
3116 if (sa4->sin_addr.s_addr ==
3117 sin4.sin_addr.s_addr) {
3118 fnd = 1;
3119 break;
3120 }
3121 } else if (sa->sa_family == AF_INET6) {
3122 sa6 = (struct sockaddr_in6 *)sa;
3123 if (SCTP6_ARE_ADDR_EQUAL(&sa6->sin6_addr,
3124 &sin6.sin6_addr)) {
3125 fnd = 1;
3126 break;
3127 }
3128 }
3129 }
3130 }
3131 if (fnd == 0) {
3132 /* New address added! no need to look futher. */
3133 return (1);
3134 }
3135 /* Ok so far lets munge through the rest of the packet */
3136 mat = in_initpkt;
3137 sa_touse = NULL;
3138 offset += sizeof(struct sctp_init_chunk);
3139 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
3140 while (phdr) {
3141 ptype = ntohs(phdr->param_type);
3142 plen = ntohs(phdr->param_length);
3143 if (ptype == SCTP_IPV4_ADDRESS) {
3144 struct sctp_ipv4addr_param *p4, p4_buf;
3145
3146 phdr = sctp_get_next_param(mat, offset,
3147 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
3148 if (plen != sizeof(struct sctp_ipv4addr_param) ||
3149 phdr == NULL) {
3150 return (1);
3151 }
3152 p4 = (struct sctp_ipv4addr_param *)phdr;
3153 sin4.sin_addr.s_addr = p4->addr;
3154 sa_touse = (struct sockaddr *)&sin4;
3155 } else if (ptype == SCTP_IPV6_ADDRESS) {
3156 struct sctp_ipv6addr_param *p6, p6_buf;
3157
3158 phdr = sctp_get_next_param(mat, offset,
3159 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
3160 if (plen != sizeof(struct sctp_ipv6addr_param) ||
3161 phdr == NULL) {
3162 return (1);
3163 }
3164 p6 = (struct sctp_ipv6addr_param *)phdr;
3165 memcpy((void *)&sin6.sin6_addr, p6->addr,
3166 sizeof(p6->addr));
3167 sa_touse = (struct sockaddr *)&sin4;
3168 }
3169
3170 if (sa_touse) {
3171 /* ok, sa_touse points to one to check */
3172 fnd = 0;
3173 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3174 sa = (struct sockaddr *)&net->ro.ro_sa;
3175 if (sa->sa_family != sa_touse->sa_family) {
3176 continue;
3177 }
3178 if (sa->sa_family == AF_INET) {
3179 sa4 = (struct sockaddr_in *)sa;
3180 if (sa4->sin_addr.s_addr ==
3181 sin4.sin_addr.s_addr) {
3182 fnd = 1;
3183 break;
3184 }
3185 } else if (sa->sa_family == AF_INET6) {
3186 sa6 = (struct sockaddr_in6 *)sa;
3187 if (SCTP6_ARE_ADDR_EQUAL(
3188 &sa6->sin6_addr, &sin6.sin6_addr)) {
3189 fnd = 1;
3190 break;
3191 }
3192 }
3193 }
3194 if (!fnd) {
3195 /* New addr added! no need to look further */
3196 return (1);
3197 }
3198 }
3199 offset += SCTP_SIZE32(plen);
3200 phdr = sctp_get_next_param(mat, offset, ¶ms, sizeof(params));
3201 }
3202 return (0);
3203 }
3204
3205 /*
3206 * Given a MBUF chain that was sent into us containing an
3207 * INIT. Build a INIT-ACK with COOKIE and send back.
3208 * We assume that the in_initpkt has done a pullup to
3209 * include IPv6/4header, SCTP header and initial part of
3210 * INIT message (i.e. the struct sctp_init_msg).
3211 */
3212 void
3213 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
3214 struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
3215 struct sctp_init_chunk *init_chk)
3216 {
3217 struct sctp_association *asoc;
3218 struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *m_last;
3219 struct sctp_init_msg *initackm_out;
3220 struct sctp_ecn_supported_param *ecn;
3221 struct sctp_prsctp_supported_param *prsctp;
3222 struct sctp_ecn_nonce_supported_param *ecn_nonce;
3223 struct sctp_supported_chunk_types_param *pr_supported;
3224 struct sockaddr_storage store;
3225 struct sockaddr_in *sin;
3226 struct sockaddr_in6 *sin6;
3227 struct route *ro;
3228 struct ip *iph;
3229 struct ip6_hdr *ip6;
3230 const struct sockaddr *to;
3231 struct sctp_state_cookie stc;
3232 struct sctp_nets *net=NULL;
3233 int cnt_inits_to=0;
3234 uint16_t his_limit, i_want;
3235 int abort_flag, padval, sz_of;
3236
3237 if (stcb) {
3238 asoc = &stcb->asoc;
3239 } else {
3240 asoc = NULL;
3241 }
3242 m_last = NULL;
3243 if ((asoc != NULL) &&
3244 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
3245 (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
3246 /* new addresses, out of here in non-cookie-wait states */
3247 /*
3248 * Send a ABORT, we don't add the new address error clause though
3249 * we even set the T bit and copy in the 0 tag.. this looks no
3250 * different than if no listner was present.
3251 */
3252 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
3253 return;
3254 }
3255 abort_flag = 0;
3256 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
3257 (offset+sizeof(struct sctp_init_chunk)),
3258 &abort_flag, (struct sctp_chunkhdr *)init_chk);
3259 if (abort_flag) {
3260 sctp_send_abort(init_pkt, iphlen, sh, init_chk->init.initiate_tag, op_err);
3261 return;
3262 }
3263 MGETHDR(m, M_DONTWAIT, MT_HEADER);
3264 if (m == NULL) {
3265 /* No memory, INIT timer will re-attempt. */
3266 if (op_err)
3267 sctp_m_freem(op_err);
3268 return;
3269 }
3270 MCLGET(m, M_DONTWAIT);
3271 if ((m->m_flags & M_EXT) != M_EXT) {
3272 /* Failed to get cluster buffer */
3273 if (op_err)
3274 sctp_m_freem(op_err);
3275 sctp_m_freem(m);
3276 return;
3277 }
3278 m->m_data += SCTP_MIN_OVERHEAD;
3279 m->m_pkthdr.rcvif = 0;
3280 m->m_len = sizeof(struct sctp_init_msg);
3281
3282 /* the time I built cookie */
3283 SCTP_GETTIME_TIMEVAL(&stc.time_entered);
3284
3285 /* populate any tie tags */
3286 if (asoc != NULL) {
3287 /* unlock before tag selections */
3288 SCTP_TCB_UNLOCK(stcb);
3289 if (asoc->my_vtag_nonce == 0)
3290 asoc->my_vtag_nonce = sctp_select_a_tag(inp);
3291 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
3292
3293 if (asoc->peer_vtag_nonce == 0)
3294 asoc->peer_vtag_nonce = sctp_select_a_tag(inp);
3295 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
3296
3297 stc.cookie_life = asoc->cookie_life;
3298 net = asoc->primary_destination;
3299 /* now we must relock */
3300 SCTP_INP_RLOCK(inp);
3301 /* we may be in trouble here if the inp got freed
3302 * most likely this set of tests will protect
3303 * us but there is a chance not.
3304 */
3305 if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE|SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3306 if (op_err)
3307 sctp_m_freem(op_err);
3308 sctp_m_freem(m);
3309 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL);
3310 return;
3311 }
3312 SCTP_TCB_LOCK(stcb);
3313 SCTP_INP_RUNLOCK(stcb->sctp_ep);
3314 } else {
3315 stc.tie_tag_my_vtag = 0;
3316 stc.tie_tag_peer_vtag = 0;
3317 /* life I will award this cookie */
3318 stc.cookie_life = inp->sctp_ep.def_cookie_life;
3319 }
3320
3321 /* copy in the ports for later check */
3322 stc.myport = sh->dest_port;
3323 stc.peerport = sh->src_port;
3324
3325 /*
3326 * If we wanted to honor cookie life extentions, we would add
3327 * to stc.cookie_life. For now we should NOT honor any extension
3328 */
3329 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
3330 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
3331 struct inpcb *in_inp;
3332 /* Its a V6 socket */
3333 in_inp = (struct inpcb *)inp;
3334 stc.ipv6_addr_legal = 1;
3335 /* Now look at the binding flag to see if V4 will be legal */
3336 if (
3337 #if defined(__FreeBSD__) || defined(__APPLE__)
3338 (in_inp->inp_flags & IN6P_IPV6_V6ONLY)
3339 #elif defined(__OpenBSD__)
3340 (0) /* For openbsd we do dual bind only */
3341 #else
3342 (((struct in6pcb *)in_inp)->in6p_flags & IN6P_IPV6_V6ONLY)
3343 #endif
3344 == 0) {
3345 stc.ipv4_addr_legal = 1;
3346 } else {
3347 /* V4 addresses are NOT legal on the association */
3348 stc.ipv4_addr_legal = 0;
3349 }
3350 } else {
3351 /* Its a V4 socket, no - V6 */
3352 stc.ipv4_addr_legal = 1;
3353 stc.ipv6_addr_legal = 0;
3354 }
3355
3356 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
3357 stc.ipv4_scope = 1;
3358 #else
3359 stc.ipv4_scope = 0;
3360 #endif
3361 /* now for scope setup */
3362 memset((void *)&store, 0, sizeof(store));
3363 sin = (struct sockaddr_in *)&store;
3364 sin6 = (struct sockaddr_in6 *)&store;
3365 if (net == NULL) {
3366 to = (struct sockaddr *)&store;
3367 iph = mtod(init_pkt, struct ip *);
3368 if (iph->ip_v == IPVERSION) {
3369 struct in_addr addr;
3370 static struct route iproute;
3371
3372 sin->sin_family = AF_INET;
3373 sin->sin_len = sizeof(struct sockaddr_in);
3374 sin->sin_port = sh->src_port;
3375 sin->sin_addr = iph->ip_src;
3376 /* lookup address */
3377 stc.address[0] = sin->sin_addr.s_addr;
3378 stc.address[1] = 0;
3379 stc.address[2] = 0;
3380 stc.address[3] = 0;
3381 stc.addr_type = SCTP_IPV4_ADDRESS;
3382 /* local from address */
3383 memset(&iproute, 0, sizeof(iproute));
3384 ro = &iproute;
3385
3386 rtcache_lookup(ro, (struct sockaddr *) sin);
3387 addr = sctp_ipv4_source_address_selection(inp, NULL,
3388 ro, NULL, 0);
3389 stc.laddress[0] = addr.s_addr;
3390 stc.laddress[1] = 0;
3391 stc.laddress[2] = 0;
3392 stc.laddress[3] = 0;
3393 stc.laddr_type = SCTP_IPV4_ADDRESS;
3394 /* scope_id is only for v6 */
3395 stc.scope_id = 0;
3396 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
3397 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
3398 stc.ipv4_scope = 1;
3399 }
3400 #else
3401 stc.ipv4_scope = 1;
3402 #endif /* SCTP_DONT_DO_PRIVADDR_SCOPE */
3403 /* Must use the address in this case */
3404 if (sctp_is_address_on_local_host((struct sockaddr *)sin)) {
3405 stc.loopback_scope = 1;
3406 stc.ipv4_scope = 1;
3407 stc.site_scope = 1;
3408 stc.local_scope = 1;
3409 }
3410 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
3411 struct in6_addr addr;
3412 static struct route iproute6;
3413 ip6 = mtod(init_pkt, struct ip6_hdr *);
3414 sin6->sin6_family = AF_INET6;
3415 sin6->sin6_len = sizeof(struct sockaddr_in6);
3416 sin6->sin6_port = sh->src_port;
3417 sin6->sin6_addr = ip6->ip6_src;
3418 /* lookup address */
3419 memcpy(&stc.address, &sin6->sin6_addr,
3420 sizeof(struct in6_addr));
3421 sin6->sin6_scope_id = 0;
3422 stc.addr_type = SCTP_IPV6_ADDRESS;
3423 stc.scope_id = 0;
3424 if (sctp_is_address_on_local_host((struct sockaddr *)sin6)) {
3425 stc.loopback_scope = 1;
3426 stc.local_scope = 1;
3427 stc.site_scope = 1;
3428 stc.ipv4_scope = 1;
3429 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
3430 /*
3431 * If the new destination is a LINK_LOCAL
3432 * we must have common both site and local
3433 * scope. Don't set local scope though since
3434 * we must depend on the source to be added
3435 * implicitly. We cannot assure just because
3436 * we share one link that all links are common.
3437 *
3438 * XXX: never treat link-local case explicitly.
3439 * Use general routines defined in scope6.c.
3440 * (jinmei@kame)
3441 */
3442 stc.local_scope = 0;
3443 stc.site_scope = 1;
3444 stc.ipv4_scope = 1;
3445 /* we start counting for the private
3446 * address stuff at 1. since the link
3447 * local we source from won't show
3448 * up in our scoped count.
3449 */
3450 cnt_inits_to=1;
3451 /* pull out the scope_id from incoming pkt */
3452 #if defined(SCTP_BASE_FREEBSD) || defined(__APPLE__)
3453 (void)in6_recoverscope(sin6, &in6_src,
3454 init_pkt->m_pkthdr.rcvif);
3455 in6_embedscope(&sin6->sin6_addr, sin6, NULL,
3456 NULL);
3457 #else
3458 (void)sa6_recoverscope(sin6);
3459 #endif
3460 stc.scope_id = sin6->sin6_scope_id;
3461
3462 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
3463 /*
3464 * If the new destination is SITE_LOCAL
3465 * then we must have site scope in common.
3466 */
3467 stc.site_scope = 1;
3468 }
3469 /* local from address */
3470 memset(&iproute6, 0, sizeof(iproute6));
3471 ro = (struct route *)&iproute6;
3472 rtcache_lookup(ro, (struct sockaddr *) sin6);
3473 addr = sctp_ipv6_source_address_selection(inp, NULL,
3474 ro, NULL, 0);
3475 memcpy(&stc.laddress, &addr, sizeof(struct in6_addr));
3476 stc.laddr_type = SCTP_IPV6_ADDRESS;
3477 }
3478 } else {
3479 /* set the scope per the existing tcb */
3480 struct sctp_nets *lnet;
3481
3482 stc.loopback_scope = asoc->loopback_scope;
3483 stc.ipv4_scope = asoc->ipv4_local_scope;
3484 stc.site_scope = asoc->site_scope;
3485 stc.local_scope = asoc->local_scope;
3486 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
3487 if (rtcache_getdst(&lnet->ro)->sa_family == AF_INET6) {
3488 if (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *) rtcache_getdst(&lnet->ro)->sa_data)) {
3489 /* if we have a LL address, start counting
3490 * at 1.
3491 */
3492 cnt_inits_to = 1;
3493 }
3494 }
3495 }
3496
3497 /* use the net pointer */
3498 to = rtcache_getdst(&net->ro);
3499 if (to->sa_family == AF_INET) {
3500 memcpy(&stc.address[0], to, sizeof(struct in_addr));
3501 stc.address[1] = 0;
3502 stc.address[2] = 0;
3503 stc.address[3] = 0;
3504 stc.addr_type = SCTP_IPV4_ADDRESS;
3505 if (net->src_addr_selected == 0) {
3506 /* strange case here, the INIT
3507 * should have did the selection.
3508 */
3509 net->_s_addr.sin.sin_addr =
3510 sctp_ipv4_source_address_selection(inp,
3511 stcb, &net->ro, net, 0);
3512 net->src_addr_selected = 1;
3513
3514 }
3515
3516 stc.laddress[0] = net->_s_addr.sin.sin_addr.s_addr;
3517 stc.laddress[1] = 0;
3518 stc.laddress[2] = 0;
3519 stc.laddress[3] = 0;
3520 stc.laddr_type = SCTP_IPV4_ADDRESS;
3521 } else if (to->sa_family == AF_INET6) {
3522 memcpy(&stc.address, &to->sa_data,
3523 sizeof(struct in6_addr));
3524 stc.addr_type = SCTP_IPV6_ADDRESS;
3525 if (net->src_addr_selected == 0) {
3526 /* strange case here, the INIT
3527 * should have did the selection.
3528 */
3529 net->_s_addr.sin6.sin6_addr =
3530 sctp_ipv6_source_address_selection(inp,
3531 stcb, &net->ro, net, 0);
3532 net->src_addr_selected = 1;
3533 }
3534 memcpy(&stc.laddress, &net->_s_addr.sin6.sin6_addr,
3535 sizeof(struct in6_addr));
3536 stc.laddr_type = SCTP_IPV6_ADDRESS;
3537 }
3538 }
3539 /* Now lets put the SCTP header in place */
3540 initackm_out = mtod(m, struct sctp_init_msg *);
3541 initackm_out->sh.src_port = inp->sctp_lport;
3542 initackm_out->sh.dest_port = sh->src_port;
3543 initackm_out->sh.v_tag = init_chk->init.initiate_tag;
3544 /* Save it off for quick ref */
3545 stc.peers_vtag = init_chk->init.initiate_tag;
3546 initackm_out->sh.checksum = 0; /* calculate later */
3547 /* who are we */
3548 strncpy(stc.identification, SCTP_VERSION_STRING,
3549 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
3550 /* now the chunk header */
3551 initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
3552 initackm_out->msg.ch.chunk_flags = 0;
3553 /* fill in later from mbuf we build */
3554 initackm_out->msg.ch.chunk_length = 0;
3555 /* place in my tag */
3556 if ((asoc != NULL) &&
3557 ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
3558 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
3559 /* re-use the v-tags and init-seq here */
3560 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
3561 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
3562 } else {
3563 initackm_out->msg.init.initiate_tag = htonl(sctp_select_a_tag(inp));
3564 /* get a TSN to use too */
3565 initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
3566 }
3567 /* save away my tag to */
3568 stc.my_vtag = initackm_out->msg.init.initiate_tag;
3569
3570 /* set up some of the credits. */
3571 initackm_out->msg.init.a_rwnd = htonl(max(inp->sctp_socket->so_rcv.sb_hiwat, SCTP_MINIMAL_RWND));
3572 /* set what I want */
3573 his_limit = ntohs(init_chk->init.num_inbound_streams);
3574 /* choose what I want */
3575 if (asoc != NULL) {
3576 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
3577 i_want = asoc->streamoutcnt;
3578 } else {
3579 i_want = inp->sctp_ep.pre_open_stream_count;
3580 }
3581 } else {
3582 i_want = inp->sctp_ep.pre_open_stream_count;
3583 }
3584 if (his_limit < i_want) {
3585 /* I Want more :< */
3586 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
3587 } else {
3588 /* I can have what I want :> */
3589 initackm_out->msg.init.num_outbound_streams = htons(i_want);
3590 }
3591 /* tell him his limt. */
3592 initackm_out->msg.init.num_inbound_streams =
3593 htons(inp->sctp_ep.max_open_streams_intome);
3594 /* setup the ECN pointer */
3595
3596 /* if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT) {*/
3597 if (inp->sctp_ep.adaption_layer_indicator) {
3598 struct sctp_adaption_layer_indication *ali;
3599 ali = (struct sctp_adaption_layer_indication *)(
3600 (vaddr_t)initackm_out + sizeof(*initackm_out));
3601 ali->ph.param_type = htons(SCTP_ULP_ADAPTION);
3602 ali->ph.param_length = htons(sizeof(*ali));
3603 ali->indication = ntohl(inp->sctp_ep.adaption_layer_indicator);
3604 m->m_len += sizeof(*ali);
3605 ecn = (struct sctp_ecn_supported_param *)((vaddr_t)ali +
3606 sizeof(*ali));
3607 } else {
3608 ecn = (struct sctp_ecn_supported_param*)(
3609 (vaddr_t)initackm_out + sizeof(*initackm_out));
3610 }
3611
3612 /* ECN parameter */
3613 if (sctp_ecn == 1) {
3614 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
3615 ecn->ph.param_length = htons(sizeof(*ecn));
3616 m->m_len += sizeof(*ecn);
3617
3618 prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn +
3619 sizeof(*ecn));
3620 } else {
3621 prsctp = (struct sctp_prsctp_supported_param *)((vaddr_t)ecn);
3622 }
3623 /* And now tell the peer we do pr-sctp */
3624 prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
3625 prsctp->ph.param_length = htons(sizeof(*prsctp));
3626 m->m_len += sizeof(*prsctp);
3627
3628
3629 /* And now tell the peer we do all the extensions */
3630 pr_supported = (struct sctp_supported_chunk_types_param *)((vaddr_t)prsctp +
3631 sizeof(*prsctp));
3632
3633 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
3634 pr_supported->ph.param_length = htons(sizeof(*pr_supported) + SCTP_EXT_COUNT);
3635 pr_supported->chunk_types[0] = SCTP_ASCONF;
3636 pr_supported->chunk_types[1] = SCTP_ASCONF_ACK;
3637 pr_supported->chunk_types[2] = SCTP_FORWARD_CUM_TSN;
3638 pr_supported->chunk_types[3] = SCTP_PACKET_DROPPED;
3639 pr_supported->chunk_types[4] = SCTP_STREAM_RESET;
3640 pr_supported->chunk_types[5] = 0; /* pad */
3641 pr_supported->chunk_types[6] = 0; /* pad */
3642 pr_supported->chunk_types[7] = 0; /* pad */
3643
3644 m->m_len += (sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
3645 if (sctp_ecn_nonce) {
3646 /* ECN nonce: And now tell the peer we support ECN nonce */
3647 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)((vaddr_t)pr_supported +
3648 sizeof(*pr_supported) + SCTP_EXT_COUNT + SCTP_PAD_EXT_COUNT);
3649 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
3650 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
3651 m->m_len += sizeof(*ecn_nonce);
3652 }
3653
3654 m_at = m;
3655 /* now the addresses */
3656 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3657 struct ifnet *ifn;
3658 struct ifaddr *ifa;
3659 int cnt = cnt_inits_to;
3660
3661 IFNET_FOREACH(ifn) {
3662 if ((stc.loopback_scope == 0) &&
3663 (ifn->if_type == IFT_LOOP)) {
3664 /*
3665 * Skip loopback devices if loopback_scope
3666 * not set
3667 */
3668 continue;
3669 }
3670 IFADDR_FOREACH(ifa, ifn) {
3671 if (sctp_is_address_in_scope(ifa,
3672 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3673 stc.loopback_scope, stc.ipv4_scope,
3674 stc.local_scope, stc.site_scope) == 0) {
3675 continue;
3676 }
3677 cnt++;
3678 }
3679 }
3680 if (cnt > 1) {
3681 IFNET_FOREACH(ifn) {
3682 if ((stc.loopback_scope == 0) &&
3683 (ifn->if_type == IFT_LOOP)) {
3684 /*
3685 * Skip loopback devices if
3686 * loopback_scope not set
3687 */
3688 continue;
3689 }
3690 IFADDR_FOREACH(ifa, ifn) {
3691 if (sctp_is_address_in_scope(ifa,
3692 stc.ipv4_addr_legal,
3693 stc.ipv6_addr_legal,
3694 stc.loopback_scope, stc.ipv4_scope,
3695 stc.local_scope, stc.site_scope) == 0) {
3696 continue;
3697 }
3698 m_at = sctp_add_addr_to_mbuf(m_at, ifa);
3699 }
3700 }
3701 }
3702 } else {
3703 struct sctp_laddr *laddr;
3704 int cnt;
3705 cnt = cnt_inits_to;
3706 /* First, how many ? */
3707 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3708 if (laddr->ifa == NULL) {
3709 continue;
3710 }
3711 if (laddr->ifa->ifa_addr == NULL)
3712 continue;
3713 if (sctp_is_address_in_scope(laddr->ifa,
3714 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3715 stc.loopback_scope, stc.ipv4_scope,
3716 stc.local_scope, stc.site_scope) == 0) {
3717 continue;
3718 }
3719 cnt++;
3720 }
3721 /* If we bind a single address only we won't list
3722 * any. This way you can get through a NAT
3723 */
3724 if (cnt > 1) {
3725 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3726 if (laddr->ifa == NULL) {
3727 #ifdef SCTP_DEBUG
3728 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
3729 printf("Help I have fallen and I can't get up!\n");
3730 }
3731 #endif
3732 continue;
3733 }
3734 if (laddr->ifa->ifa_addr == NULL)
3735 continue;
3736 if (sctp_is_address_in_scope(laddr->ifa,
3737 stc.ipv4_addr_legal, stc.ipv6_addr_legal,
3738 stc.loopback_scope, stc.ipv4_scope,
3739 stc.local_scope, stc.site_scope) == 0) {
3740 continue;
3741 }
3742 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
3743 }
3744 }
3745 }
3746
3747 /* tack on the operational error if present */
3748 if (op_err) {
3749 if (op_err->m_pkthdr.len % 4) {
3750 /* must add a pad to the param */
3751 u_int32_t cpthis=0;
3752 int padlen;
3753 padlen = 4 - (op_err->m_pkthdr.len % 4);
3754 m_copyback(op_err, op_err->m_pkthdr.len, padlen, (void *)&cpthis);
3755 }
3756 while (m_at->m_next != NULL) {
3757 m_at = m_at->m_next;
3758 }
3759 m_at->m_next = op_err;
3760 while (m_at->m_next != NULL) {
3761 m_at = m_at->m_next;
3762 }
3763 }
3764 /* Get total size of init packet */
3765 sz_of = SCTP_SIZE32(ntohs(init_chk->ch.chunk_length));
3766 /* pre-calulate the size and update pkt header and chunk header */
3767 m->m_pkthdr.len = 0;
3768 for (m_tmp = m; m_tmp; m_tmp = m_tmp->m_next) {
3769 m->m_pkthdr.len += m_tmp->m_len;
3770 if (m_tmp->m_next == NULL) {
3771 /* m_tmp should now point to last one */
3772 break;
3773 }
3774 }
3775 /*
3776 * Figure now the size of the cookie. We know the size of the
3777 * INIT-ACK. The Cookie is going to be the size of INIT, INIT-ACK,
3778 * COOKIE-STRUCTURE and SIGNATURE.
3779 */
3780
3781 /*
3782 * take our earlier INIT calc and add in the sz we just calculated
3783 * minus the size of the sctphdr (its not included in chunk size
3784 */
3785
3786 /* add once for the INIT-ACK */
3787 sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3788
3789 /* add a second time for the INIT-ACK in the cookie */
3790 sz_of += (m->m_pkthdr.len - sizeof(struct sctphdr));
3791
3792 /* Now add the cookie header and cookie message struct */
3793 sz_of += sizeof(struct sctp_state_cookie_param);
3794 /* ...and add the size of our signature */
3795 sz_of += SCTP_SIGNATURE_SIZE;
3796 initackm_out->msg.ch.chunk_length = htons(sz_of);
3797
3798 /* Now we must build a cookie */
3799 m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
3800 sizeof(struct sctphdr), &stc);
3801 if (m_cookie == NULL) {
3802 /* memory problem */
3803 sctp_m_freem(m);
3804 return;
3805 }
3806 /* Now append the cookie to the end and update the space/size */
3807 m_tmp->m_next = m_cookie;
3808
3809 /*
3810 * We pass 0 here to NOT set IP_DF if its IPv4, we ignore the
3811 * return here since the timer will drive a retranmission.
3812 */
3813 padval = m->m_pkthdr.len % 4;
3814 if ((padval) && (m_last)) {
3815 /* see my previous comments on m_last */
3816 int ret;
3817 ret = sctp_add_pad_tombuf(m_last, (4-padval));
3818 if (ret) {
3819 /* Houston we have a problem, no space */
3820 sctp_m_freem(m);
3821 return;
3822 }
3823 m->m_pkthdr.len += padval;
3824 }
3825 sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, 0, NULL, 0);
3826 }
3827
3828
3829 static void
3830 sctp_insert_on_wheel(struct sctp_association *asoc,
3831 struct sctp_stream_out *strq)
3832 {
3833 struct sctp_stream_out *stre, *strn;
3834 stre = TAILQ_FIRST(&asoc->out_wheel);
3835 if (stre == NULL) {
3836 /* only one on wheel */
3837 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
3838 return;
3839 }
3840 for (; stre; stre = strn) {
3841 strn = TAILQ_NEXT(stre, next_spoke);
3842 if (stre->stream_no > strq->stream_no) {
3843 TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
3844 return;
3845 } else if (stre->stream_no == strq->stream_no) {
3846 /* huh, should not happen */
3847 return;
3848 } else if (strn == NULL) {
3849 /* next one is null */
3850 TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
3851 next_spoke);
3852 }
3853 }
3854 }
3855
3856 static void
3857 sctp_remove_from_wheel(struct sctp_association *asoc,
3858 struct sctp_stream_out *strq)
3859 {
3860 /* take off and then setup so we know it is not on the wheel */
3861 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
3862 strq->next_spoke.tqe_next = NULL;
3863 strq->next_spoke.tqe_prev = NULL;
3864 }
3865
3866
3867 static void
3868 sctp_prune_prsctp(struct sctp_tcb *stcb,
3869 struct sctp_association *asoc,
3870 struct sctp_sndrcvinfo *srcv,
3871 int dataout
3872 )
3873 {
3874 int freed_spc=0;
3875 struct sctp_tmit_chunk *chk, *nchk;
3876 if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
3877 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
3878 /*
3879 * Look for chunks marked with the PR_SCTP
3880 * flag AND the buffer space flag. If the one
3881 * being sent is equal or greater priority then
3882 * purge the old one and free some space.
3883 */
3884 if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
3885 SCTP_PR_SCTP_BUFFER)) ==
3886 (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
3887 /*
3888 * This one is PR-SCTP AND buffer space
3889 * limited type
3890 */
3891 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
3892 /* Lower numbers equates to
3893 * higher priority so if the
3894 * one we are looking at has a
3895 * larger or equal priority we
3896 * want to drop the data and
3897 * NOT retransmit it.
3898 */
3899 if (chk->data) {
3900 /* We release the
3901 * book_size if the
3902 * mbuf is here
3903 */
3904 int ret_spc;
3905 int cause;
3906 if (chk->sent > SCTP_DATAGRAM_UNSENT)
3907 cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_SENT;
3908 else
3909 cause = SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT;
3910 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
3911 cause,
3912 &asoc->sent_queue);
3913 freed_spc += ret_spc;
3914 if (freed_spc >= dataout) {
3915 return;
3916 }
3917 } /* if chunk was present */
3918 } /* if of sufficent priority */
3919 } /* if chunk has enabled */
3920 } /* tailqforeach */
3921
3922 chk = TAILQ_FIRST(&asoc->send_queue);
3923 while (chk) {
3924 nchk = TAILQ_NEXT(chk, sctp_next);
3925 /* Here we must move to the sent queue and mark */
3926 if ((chk->flags & (SCTP_PR_SCTP_ENABLED |
3927 SCTP_PR_SCTP_BUFFER)) ==
3928 (SCTP_PR_SCTP_ENABLED|SCTP_PR_SCTP_BUFFER)) {
3929 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
3930 if (chk->data) {
3931 /* We release the
3932 * book_size if the
3933 * mbuf is here
3934 */
3935 int ret_spc;
3936 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
3937 SCTP_RESPONSE_TO_USER_REQ|SCTP_NOTIFY_DATAGRAM_UNSENT,
3938 &asoc->send_queue);
3939
3940 freed_spc += ret_spc;
3941 if (freed_spc >= dataout) {
3942 return;
3943 }
3944 } /* end if chk->data */
3945 } /* end if right class */
3946 } /* end if chk pr-sctp */
3947 chk = nchk;
3948 } /* end while (chk) */
3949 } /* if enabled in asoc */
3950 }
3951
3952 static void
3953 sctp_prepare_chunk(struct sctp_tmit_chunk *template,
3954 struct sctp_tcb *stcb,
3955 struct sctp_sndrcvinfo *srcv,
3956 struct sctp_stream_out *strq,
3957 struct sctp_nets *net)
3958 {
3959 memset(template, 0, sizeof(struct sctp_tmit_chunk));
3960 template->sent = SCTP_DATAGRAM_UNSENT;
3961 if ((stcb->asoc.peer_supports_prsctp) &&
3962 (srcv->sinfo_flags & (MSG_PR_SCTP_TTL|MSG_PR_SCTP_BUF)) &&
3963 (srcv->sinfo_timetolive > 0)
3964 ) {
3965 /* If:
3966 * Peer supports PR-SCTP
3967 * The flags is set against this send for PR-SCTP
3968 * And timetolive is a postive value, zero is reserved
3969 * to mean a reliable send for both buffer/time
3970 * related one.
3971 */
3972 if (srcv->sinfo_flags & MSG_PR_SCTP_BUF) {
3973 /*
3974 * Time to live is a priority stored in tv_sec
3975 * when doing the buffer drop thing.
3976 */
3977 template->rec.data.timetodrop.tv_sec = srcv->sinfo_timetolive;
3978 } else {
3979 struct timeval tv;
3980
3981 SCTP_GETTIME_TIMEVAL(&template->rec.data.timetodrop);
3982 tv.tv_sec = srcv->sinfo_timetolive / 1000;
3983 tv.tv_usec = (srcv->sinfo_timetolive * 1000) % 1000000;
3984 #ifndef __FreeBSD__
3985 timeradd(&template->rec.data.timetodrop, &tv,
3986 &template->rec.data.timetodrop);
3987 #else
3988 timevaladd(&template->rec.data.timetodrop, &tv);
3989 #endif
3990 }
3991 }
3992 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
3993 template->rec.data.stream_seq = strq->next_sequence_sent;
3994 } else {
3995 template->rec.data.stream_seq = 0;
3996 }
3997 template->rec.data.TSN_seq = 0; /* not yet assigned */
3998
3999 template->rec.data.stream_number = srcv->sinfo_stream;
4000 template->rec.data.payloadtype = srcv->sinfo_ppid;
4001 template->rec.data.context = srcv->sinfo_context;
4002 template->rec.data.doing_fast_retransmit = 0;
4003 template->rec.data.ect_nonce = 0; /* ECN Nonce */
4004
4005 if (srcv->sinfo_flags & MSG_ADDR_OVER) {
4006 template->whoTo = net;
4007 } else {
4008 if (stcb->asoc.primary_destination)
4009 template->whoTo = stcb->asoc.primary_destination;
4010 else {
4011 /* TSNH */
4012 template->whoTo = net;
4013 }
4014 }
4015 /* the actual chunk flags */
4016 if (srcv->sinfo_flags & MSG_UNORDERED) {
4017 template->rec.data.rcv_flags = SCTP_DATA_UNORDERED;
4018 } else {
4019 template->rec.data.rcv_flags = 0;
4020 }
4021 /* no flags yet, FRAGMENT_OK goes here */
4022 template->flags = 0;
4023 /* PR sctp flags */
4024 if (stcb->asoc.peer_supports_prsctp) {
4025 if (srcv->sinfo_timetolive > 0) {
4026 /*
4027 * We only set the flag if timetolive (or
4028 * priority) was set to a positive number.
4029 * Zero is reserved specifically to be
4030 * EXCLUDED and sent reliable.
4031 */
4032 if (srcv->sinfo_flags & MSG_PR_SCTP_TTL) {
4033 template->flags |= SCTP_PR_SCTP_ENABLED;
4034 }
4035 if (srcv->sinfo_flags & MSG_PR_SCTP_BUF) {
4036 template->flags |= SCTP_PR_SCTP_BUFFER;
4037 }
4038 }
4039 }
4040 template->asoc = &stcb->asoc;
4041 }
4042
4043
4044 int
4045 sctp_get_frag_point(struct sctp_tcb *stcb,
4046 struct sctp_association *asoc)
4047 {
4048 int siz, ovh;
4049
4050 /* For endpoints that have both 6 and 4 addresses
4051 * we must reserver room for the 6 ip header, for
4052 * those that are only dealing with V4 we use
4053 * a larger frag point.
4054 */
4055 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4056 ovh = SCTP_MED_OVERHEAD;
4057 } else {
4058 ovh = SCTP_MED_V4_OVERHEAD;
4059 }
4060
4061 if (stcb->sctp_ep->sctp_frag_point > asoc->smallest_mtu)
4062 siz = asoc->smallest_mtu - ovh;
4063 else
4064 siz = (stcb->sctp_ep->sctp_frag_point - ovh);
4065 /*
4066 if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) { */
4067 /* A data chunk MUST fit in a cluster */
4068 /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk));*/
4069 /* }*/
4070
4071 if (siz % 4) {
4072 /* make it an even word boundary please */
4073 siz -= (siz % 4);
4074 }
4075 return (siz);
4076 }
4077 extern unsigned int sctp_max_chunks_on_queue;
4078
4079 #define SBLOCKWAIT(f) (((f)&MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
4080
4081 static int
4082 sctp_msg_append(struct sctp_tcb *stcb,
4083 struct sctp_nets *net,
4084 struct mbuf *m,
4085 struct sctp_sndrcvinfo *srcv,
4086 int flags)
4087 {
4088 struct socket *so;
4089 struct sctp_association *asoc;
4090 struct sctp_stream_out *strq;
4091 struct sctp_tmit_chunk *chk;
4092 struct sctpchunk_listhead tmp;
4093 struct sctp_tmit_chunk template;
4094 struct mbuf *n, *mnext;
4095 struct mbuf *mm;
4096 unsigned int dataout, siz;
4097 int mbcnt = 0;
4098 int mbcnt_e = 0;
4099 int error = 0;
4100
4101 if ((stcb == NULL) || (net == NULL) || (m == NULL) || (srcv == NULL)) {
4102 /* Software fault, you blew it on the call */
4103 #ifdef SCTP_DEBUG
4104 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4105 printf("software error in sctp_msg_append:1\n");
4106 printf("stcb:%p net:%p m:%p srcv:%p\n",
4107 stcb, net, m, srcv);
4108 }
4109 #endif
4110 if (m)
4111 sctp_m_freem(m);
4112 return (EFAULT);
4113 }
4114 so = stcb->sctp_socket;
4115 asoc = &stcb->asoc;
4116 if (srcv->sinfo_flags & MSG_ABORT) {
4117 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4118 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
4119 /* It has to be up before we abort */
4120 /* how big is the user initiated abort? */
4121 if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
4122 dataout = m->m_pkthdr.len;
4123 } else {
4124 /* we must count */
4125 dataout = 0;
4126 for (n = m; n; n = n->m_next) {
4127 dataout += n->m_len;
4128 }
4129 }
4130 M_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
4131 if (m) {
4132 struct sctp_paramhdr *ph;
4133 m->m_len = sizeof(struct sctp_paramhdr) + dataout;
4134 ph = mtod(m, struct sctp_paramhdr *);
4135 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
4136 ph->param_length = htons(m->m_len);
4137 }
4138 sctp_abort_an_association(stcb->sctp_ep, stcb, SCTP_RESPONSE_TO_USER_REQ, m);
4139 m = NULL;
4140 } else {
4141 /* Only free if we don't send an abort */
4142 ;
4143 }
4144 goto out;
4145 }
4146 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
4147 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
4148 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
4149 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
4150 /* got data while shutting down */
4151 error = ECONNRESET;
4152 goto out;
4153 }
4154
4155 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
4156 /* Invalid stream number */
4157 error = EINVAL;
4158 goto out;
4159 }
4160 if (asoc->strmout == NULL) {
4161 /* huh? software error */
4162 #ifdef SCTP_DEBUG
4163 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4164 printf("software error in sctp_msg_append:2\n");
4165 }
4166 #endif
4167 error = EFAULT;
4168 goto out;
4169 }
4170 strq = &asoc->strmout[srcv->sinfo_stream];
4171 /* how big is it ? */
4172 if ((m->m_flags & M_PKTHDR) && (m->m_pkthdr.len)) {
4173 dataout = m->m_pkthdr.len;
4174 } else {
4175 /* we must count */
4176 dataout = 0;
4177 for (n = m; n; n = n->m_next) {
4178 dataout += n->m_len;
4179 }
4180 }
4181 #ifdef SCTP_DEBUG
4182 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4183 printf("Attempt to send out %d bytes\n",
4184 dataout);
4185 }
4186 #endif
4187
4188 /* lock the socket buf */
4189 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
4190 if (error)
4191 goto out_locked;
4192
4193 if (dataout > so->so_snd.sb_hiwat) {
4194 /* It will NEVER fit */
4195 error = EMSGSIZE;
4196 goto release;
4197 }
4198 if ((srcv->sinfo_flags & MSG_EOF) &&
4199 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
4200 (dataout == 0)
4201 ) {
4202 goto zap_by_it_all;
4203 }
4204 if ((so->so_snd.sb_hiwat <
4205 (dataout + asoc->total_output_queue_size)) ||
4206 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
4207 (asoc->total_output_mbuf_queue_size >
4208 so->so_snd.sb_mbmax)
4209 ) {
4210 /* XXX Buffer space hunt for data to skip */
4211 if (asoc->peer_supports_prsctp) {
4212 sctp_prune_prsctp(stcb, asoc, srcv, dataout);
4213 }
4214 while ((so->so_snd.sb_hiwat <
4215 (dataout + asoc->total_output_queue_size)) ||
4216 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
4217 (asoc->total_output_mbuf_queue_size >
4218 so->so_snd.sb_mbmax)) {
4219 struct sctp_inpcb *inp;
4220 /* Now did we free up enough room? */
4221 if (so->so_state & SS_NBIO) {
4222 /* Non-blocking io in place */
4223 error = EWOULDBLOCK;
4224 goto release;
4225 }
4226 /*
4227 * We store off a pointer to the endpoint.
4228 * Since on return from this we must check to
4229 * see if an so_error is set. If so we may have
4230 * been reset and our stcb destroyed. Returning
4231 * an error will cause the correct error return
4232 * through and fix this all.
4233 */
4234 inp = stcb->sctp_ep;
4235 /*
4236 * Not sure how else to do this since
4237 * the level we suspended at is not
4238 * known deep down where we are. I will
4239 * drop to spl0() so that others can
4240 * get in.
4241 */
4242
4243 inp->sctp_tcb_at_block = (void *)stcb;
4244 inp->error_on_block = 0;
4245 sbunlock(&so->so_snd);
4246 error = sbwait(&so->so_snd);
4247 /*
4248 * XXX: This is ugly but I have
4249 * recreated most of what goes on to
4250 * block in the sb. UGHH
4251 * May want to add the bit about being
4252 * no longer connected.. but this then
4253 * further dooms the UDP model NOT to
4254 * allow this.
4255 */
4256 inp->sctp_tcb_at_block = 0;
4257 if (inp->error_on_block)
4258 error = inp->error_on_block;
4259 if (so->so_error)
4260 error = so->so_error;
4261 if (error) {
4262 goto out_locked;
4263 }
4264 error = sblock(&so->so_snd, M_WAITOK);
4265 if (error)
4266 goto out_locked;
4267 /* Otherwise we cycle back and recheck
4268 * the space
4269 */
4270 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
4271 if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
4272 #else
4273 if (so->so_state & SS_CANTSENDMORE) {
4274 #endif
4275 error = EPIPE;
4276 goto release;
4277 }
4278 if (so->so_error) {
4279 error = so->so_error;
4280 goto release;
4281 }
4282 }
4283 }
4284 /* If we have a packet header fix it if it was broke */
4285 if (m->m_flags & M_PKTHDR) {
4286 m->m_pkthdr.len = dataout;
4287 }
4288 /* use the smallest one, user set value or
4289 * smallest mtu of the asoc
4290 */
4291 siz = sctp_get_frag_point(stcb, asoc);
4292 if ((dataout) && (dataout <= siz)) {
4293 /* Fast path */
4294 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
4295 if (chk == NULL) {
4296 error = ENOMEM;
4297 goto release;
4298 }
4299 sctp_prepare_chunk(chk, stcb, srcv, strq, net);
4300 chk->whoTo->ref_count++;
4301 chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
4302
4303 /* no flags yet, FRAGMENT_OK goes here */
4304 sctppcbinfo.ipi_count_chunk++;
4305 sctppcbinfo.ipi_gencnt_chunk++;
4306 asoc->chunks_on_out_queue++;
4307 chk->data = m;
4308 m = NULL;
4309 /* Total in the MSIZE */
4310 for (mm = chk->data; mm; mm = mm->m_next) {
4311 mbcnt += MSIZE;
4312 if (mm->m_flags & M_EXT) {
4313 mbcnt += chk->data->m_ext.ext_size;
4314 }
4315 }
4316 /* fix up the send_size if it is not present */
4317 chk->send_size = dataout;
4318 chk->book_size = chk->send_size;
4319 chk->mbcnt = mbcnt;
4320 /* ok, we are commited */
4321 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4322 /* bump the ssn if we are unordered. */
4323 strq->next_sequence_sent++;
4324 }
4325 chk->data->m_nextpkt = 0;
4326 asoc->stream_queue_cnt++;
4327 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
4328 /* now check if this stream is on the wheel */
4329 if ((strq->next_spoke.tqe_next == NULL) &&
4330 (strq->next_spoke.tqe_prev == NULL)) {
4331 /* Insert it on the wheel since it is not
4332 * on it currently
4333 */
4334 sctp_insert_on_wheel(asoc, strq);
4335 }
4336 } else if ((dataout) && (dataout > siz)) {
4337 /* Slow path */
4338 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT) &&
4339 (dataout > siz)) {
4340 error = EMSGSIZE;
4341 goto release;
4342 }
4343 /* setup the template */
4344 sctp_prepare_chunk(&template, stcb, srcv, strq, net);
4345
4346 n = m;
4347 while (dataout > siz) {
4348 /*
4349 * We can wait since this is called from the user
4350 * send side
4351 */
4352 n->m_nextpkt = m_split(n, siz, M_WAIT);
4353 if (n->m_nextpkt == NULL) {
4354 error = EFAULT;
4355 goto release;
4356 }
4357 dataout -= siz;
4358 n = n->m_nextpkt;
4359 }
4360 /*
4361 * ok, now we have a chain on m where m->m_nextpkt points to
4362 * the next chunk and m/m->m_next chain is the piece to send.
4363 * We must go through the chains and thread them on to
4364 * sctp_tmit_chunk chains and place them all on the stream
4365 * queue, breaking the m->m_nextpkt pointers as we go.
4366 */
4367 n = m;
4368 TAILQ_INIT(&tmp);
4369 while (n) {
4370 /*
4371 * first go through and allocate a sctp_tmit chunk
4372 * for each chunk piece
4373 */
4374 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
4375 if (chk == NULL) {
4376 /*
4377 * ok we must spin through and dump anything
4378 * we have allocated and then jump to the
4379 * no_membad
4380 */
4381 chk = TAILQ_FIRST(&tmp);
4382 while (chk) {
4383 TAILQ_REMOVE(&tmp, chk, sctp_next);
4384 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4385 sctppcbinfo.ipi_count_chunk--;
4386 asoc->chunks_on_out_queue--;
4387 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4388 panic("Chunk count is negative");
4389 }
4390 sctppcbinfo.ipi_gencnt_chunk++;
4391 chk = TAILQ_FIRST(&tmp);
4392 }
4393 error = ENOMEM;
4394 goto release;
4395 }
4396 sctppcbinfo.ipi_count_chunk++;
4397 asoc->chunks_on_out_queue++;
4398
4399 sctppcbinfo.ipi_gencnt_chunk++;
4400 *chk = template;
4401 chk->whoTo->ref_count++;
4402 chk->data = n;
4403 /* Total in the MSIZE */
4404 mbcnt_e = 0;
4405 for (mm = chk->data; mm; mm = mm->m_next) {
4406 mbcnt_e += MSIZE;
4407 if (mm->m_flags & M_EXT) {
4408 mbcnt_e += chk->data->m_ext.ext_size;
4409 }
4410 }
4411 /* now fix the chk->send_size */
4412 if (chk->data->m_flags & M_PKTHDR) {
4413 chk->send_size = chk->data->m_pkthdr.len;
4414 } else {
4415 struct mbuf *nn;
4416 chk->send_size = 0;
4417 for (nn = chk->data; nn; nn = nn->m_next) {
4418 chk->send_size += nn->m_len;
4419 }
4420 }
4421 chk->book_size = chk->send_size;
4422 chk->mbcnt = mbcnt_e;
4423 mbcnt += mbcnt_e;
4424 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
4425 asoc->sent_queue_cnt_removeable++;
4426 }
4427 n = n->m_nextpkt;
4428 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
4429 }
4430 m = NULL;
4431 /* now that we have enough space for all de-couple the
4432 * chain of mbufs by going through our temp array
4433 * and breaking the pointers.
4434 */
4435 /* ok, we are commited */
4436 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
4437 /* bump the ssn if we are unordered. */
4438 strq->next_sequence_sent++;
4439 }
4440 /* Mark the first/last flags. This will
4441 * result int a 3 for a single item on the list
4442 */
4443 chk = TAILQ_FIRST(&tmp);
4444 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
4445 chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
4446 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
4447 /* now break any chains on the queue and
4448 * move it to the streams actual queue.
4449 */
4450 chk = TAILQ_FIRST(&tmp);
4451 while (chk) {
4452 chk->data->m_nextpkt = 0;
4453 TAILQ_REMOVE(&tmp, chk, sctp_next);
4454 asoc->stream_queue_cnt++;
4455 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
4456 chk = TAILQ_FIRST(&tmp);
4457 }
4458 /* now check if this stream is on the wheel */
4459 if ((strq->next_spoke.tqe_next == NULL) &&
4460 (strq->next_spoke.tqe_prev == NULL)) {
4461 /* Insert it on the wheel since it is not
4462 * on it currently
4463 */
4464 sctp_insert_on_wheel(asoc, strq);
4465 }
4466 }
4467 /* has a SHUTDOWN been (also) requested by the user on this asoc? */
4468 zap_by_it_all:
4469
4470 if ((srcv->sinfo_flags & MSG_EOF) &&
4471 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
4472
4473 int some_on_streamwheel = 0;
4474
4475 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
4476 /* Check to see if some data queued */
4477 struct sctp_stream_out *outs;
4478 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
4479 if (!TAILQ_EMPTY(&outs->outqueue)) {
4480 some_on_streamwheel = 1;
4481 break;
4482 }
4483 }
4484 }
4485
4486 if (TAILQ_EMPTY(&asoc->send_queue) &&
4487 TAILQ_EMPTY(&asoc->sent_queue) &&
4488 (some_on_streamwheel == 0)) {
4489 /* there is nothing queued to send, so I'm done... */
4490 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
4491 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
4492 /* only send SHUTDOWN the first time through */
4493 #ifdef SCTP_DEBUG
4494 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
4495 printf("%s:%d sends a shutdown\n",
4496 __FILE__,
4497 __LINE__
4498 );
4499 }
4500 #endif
4501 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
4502 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
4503 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
4504 asoc->primary_destination);
4505 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
4506 asoc->primary_destination);
4507 }
4508 } else {
4509 /*
4510 * we still got (or just got) data to send, so set
4511 * SHUTDOWN_PENDING
4512 */
4513 /*
4514 * XXX sockets draft says that MSG_EOF should be sent
4515 * with no data. currently, we will allow user data
4516 * to be sent first and move to SHUTDOWN-PENDING
4517 */
4518 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
4519 }
4520 }
4521 #ifdef SCTP_MBCNT_LOGGING
4522 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
4523 asoc->total_output_queue_size,
4524 dataout,
4525 asoc->total_output_mbuf_queue_size,
4526 mbcnt);
4527 #endif
4528 asoc->total_output_queue_size += dataout;
4529 asoc->total_output_mbuf_queue_size += mbcnt;
4530 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4531 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4532 so->so_snd.sb_cc += dataout;
4533 so->so_snd.sb_mbcnt += mbcnt;
4534 }
4535
4536 #ifdef SCTP_DEBUG
4537 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
4538 printf("++total out:%d total_mbuf_out:%d\n",
4539 (int)asoc->total_output_queue_size,
4540 (int)asoc->total_output_mbuf_queue_size);
4541 }
4542 #endif
4543
4544 release:
4545 sbunlock(&so->so_snd);
4546 out_locked:
4547
4548 out:
4549 if (m && m->m_nextpkt) {
4550 n = m;
4551 while (n) {
4552 mnext = n->m_nextpkt;
4553 n->m_nextpkt = NULL;
4554 sctp_m_freem(n);
4555 n = mnext;
4556 }
4557 } else if (m)
4558 sctp_m_freem(m);
4559
4560 return (error);
4561 }
4562
4563 static struct mbuf *
4564 sctp_copy_mbufchain(struct mbuf *clonechain,
4565 struct mbuf *outchain)
4566 {
4567 struct mbuf *appendchain;
4568 #if defined(__FreeBSD__) || defined(__NetBSD__)
4569 /* Supposedly m_copypacket is an optimization, use it if we can */
4570 if (clonechain->m_flags & M_PKTHDR) {
4571 appendchain = m_copypacket(clonechain, M_DONTWAIT);
4572 sctp_pegs[SCTP_CACHED_SRC]++;
4573 } else
4574 appendchain = m_copy(clonechain, 0, M_COPYALL);
4575 #elif defined(__APPLE__)
4576 appendchain = sctp_m_copym(clonechain, 0, M_COPYALL, M_DONTWAIT);
4577 #else
4578 appendchain = m_copy(clonechain, 0, M_COPYALL);
4579 #endif
4580
4581 if (appendchain == NULL) {
4582 /* error */
4583 if (outchain)
4584 sctp_m_freem(outchain);
4585 return (NULL);
4586 }
4587 if (outchain) {
4588 /* tack on to the end */
4589 struct mbuf *m;
4590 m = outchain;
4591 while (m) {
4592 if (m->m_next == NULL) {
4593 m->m_next = appendchain;
4594 break;
4595 }
4596 m = m->m_next;
4597 }
4598 if (outchain->m_flags & M_PKTHDR) {
4599 int append_tot;
4600 struct mbuf *t;
4601 t = appendchain;
4602 append_tot = 0;
4603 while (t) {
4604 append_tot += t->m_len;
4605 t = t->m_next;
4606 }
4607 outchain->m_pkthdr.len += append_tot;
4608 }
4609 return (outchain);
4610 } else {
4611 return (appendchain);
4612 }
4613 }
4614
4615 static void
4616 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr, u_int32_t val)
4617 {
4618 struct sctp_copy_all *ca;
4619 struct mbuf *m;
4620 int turned_on_nonblock=0, ret;
4621
4622 ca = (struct sctp_copy_all *)ptr;
4623 if (ca->m == NULL) {
4624 return;
4625 }
4626 if (ca->inp != inp) {
4627 /* TSNH */
4628 return;
4629 }
4630 m = sctp_copy_mbufchain(ca->m, NULL);
4631 if (m == NULL) {
4632 /* can't copy so we are done */
4633 ca->cnt_failed++;
4634 return;
4635 }
4636 if ((stcb->sctp_socket->so_state & SS_NBIO) == 0) {
4637 /* we have to do this non-blocking */
4638 turned_on_nonblock = 1;
4639 stcb->sctp_socket->so_state |= SS_NBIO;
4640 }
4641 ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m, &ca->sndrcv, 0);
4642 if (turned_on_nonblock) {
4643 /* we turned on non-blocking so turn it off */
4644 stcb->sctp_socket->so_state &= ~SS_NBIO;
4645 }
4646 if (ret) {
4647 ca->cnt_failed++;
4648 } else {
4649 ca->cnt_sent++;
4650 }
4651 }
4652
4653 static void
4654 sctp_sendall_completes(void *ptr, u_int32_t val)
4655 {
4656 struct sctp_copy_all *ca;
4657 ca = (struct sctp_copy_all *)ptr;
4658 /* Do a notify here?
4659 * Kacheong suggests that the notify
4660 * be done at the send time.. so you would
4661 * push up a notification if any send failed.
4662 * Don't know if this is feasable since the
4663 * only failures we have is "memory" related and
4664 * if you cannot get an mbuf to send the data
4665 * you surely can't get an mbuf to send up
4666 * to notify the user you can't send the data :->
4667 */
4668
4669 /* now free everything */
4670 m_freem(ca->m);
4671 free(ca, M_PCB);
4672 }
4673
4674
4675 #define MC_ALIGN(m, len) do { \
4676 (m)->m_data += (MCLBYTES - (len)) & ~(sizeof(long) - 1); \
4677 } while (0)
4678
4679
4680
4681 static struct mbuf *
4682 sctp_copy_out_all(struct uio *uio, int len)
4683 {
4684 struct mbuf *ret, *at;
4685 int left, willcpy, cancpy, error;
4686
4687 MGETHDR(ret, M_WAIT, MT_HEADER);
4688 if (ret == NULL) {
4689 /* TSNH */
4690 return (NULL);
4691 }
4692 left = len;
4693 ret->m_len = 0;
4694 ret->m_pkthdr.len = len;
4695 MCLGET(ret, M_WAIT);
4696 if (ret == NULL) {
4697 return (NULL);
4698 }
4699 if ((ret->m_flags & M_EXT) == 0) {
4700 m_freem (ret);
4701 return (NULL);
4702 }
4703 cancpy = M_TRAILINGSPACE(ret);
4704 willcpy = min(cancpy, left);
4705 at = ret;
4706 while (left > 0) {
4707 /* Align data to the end */
4708 MC_ALIGN(at, willcpy);
4709 error = uiomove(mtod(at, void *), willcpy, uio);
4710 if (error) {
4711 err_out_now:
4712 m_freem(ret);
4713 return (NULL);
4714 }
4715 at->m_len = willcpy;
4716 at->m_nextpkt = at->m_next = 0;
4717 left -= willcpy;
4718 if (left > 0) {
4719 MGET(at->m_next, M_WAIT, MT_DATA);
4720 if (at->m_next == NULL) {
4721 goto err_out_now;
4722 }
4723 at = at->m_next;
4724 at->m_len = 0;
4725 MCLGET(at, M_WAIT);
4726 if (at == NULL) {
4727 goto err_out_now;
4728 }
4729 if ((at->m_flags & M_EXT) == 0) {
4730 goto err_out_now;
4731 }
4732 cancpy = M_TRAILINGSPACE(at);
4733 willcpy = min(cancpy, left);
4734 }
4735 }
4736 return (ret);
4737 }
4738
4739 static int
4740 sctp_sendall (struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m, struct sctp_sndrcvinfo *srcv)
4741 {
4742 int ret;
4743 struct sctp_copy_all *ca;
4744 ca = malloc(sizeof(struct sctp_copy_all), M_PCB, M_WAIT);
4745 if (ca == NULL) {
4746 m_freem(m);
4747 return (ENOMEM);
4748 }
4749 memset (ca, 0, sizeof(struct sctp_copy_all));
4750
4751 ca->inp = inp;
4752 ca->sndrcv = *srcv;
4753 /* take off the sendall flag, it would
4754 * be bad if we failed to do this :-0
4755 */
4756 ca->sndrcv.sinfo_flags &= ~MSG_SENDALL;
4757
4758 /* get length and mbuf chain */
4759 if (uio) {
4760 ca->sndlen = uio->uio_resid;
4761 ca->m = sctp_copy_out_all(uio, ca->sndlen);
4762 if (ca->m == NULL) {
4763 free(ca, M_PCB);
4764 return (ENOMEM);
4765 }
4766 } else {
4767 if ((m->m_flags & M_PKTHDR) == 0) {
4768 ca->sndlen = 0;
4769 while(m) {
4770 ca->sndlen += m->m_len;
4771 m = m->m_next;
4772 }
4773 } else {
4774 ca->sndlen = m->m_pkthdr.len;
4775 }
4776 ca->m = m;
4777 }
4778
4779 ret = sctp_initiate_iterator(sctp_sendall_iterator, SCTP_PCB_ANY_FLAGS, SCTP_ASOC_ANY_STATE,
4780 (void *)ca, 0, sctp_sendall_completes, inp);
4781 if (ret) {
4782 #ifdef SCTP_DEBUG
4783 printf("Failed to initate iterator to takeover associations\n");
4784 #endif
4785 free(ca, M_PCB);
4786 return (EFAULT);
4787
4788 }
4789 return (0);
4790 }
4791
4792
4793 void
4794 sctp_toss_old_cookies(struct sctp_association *asoc)
4795 {
4796 struct sctp_tmit_chunk *chk, *nchk;
4797 chk = TAILQ_FIRST(&asoc->control_send_queue);
4798 while (chk) {
4799 nchk = TAILQ_NEXT(chk, sctp_next);
4800 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
4801 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4802 if (chk->data) {
4803 sctp_m_freem(chk->data);
4804 chk->data = NULL;
4805 }
4806 asoc->ctrl_queue_cnt--;
4807 if (chk->whoTo)
4808 sctp_free_remote_addr(chk->whoTo);
4809 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4810 sctppcbinfo.ipi_count_chunk--;
4811 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4812 panic("Chunk count is negative");
4813 }
4814 sctppcbinfo.ipi_gencnt_chunk++;
4815 }
4816 chk = nchk;
4817 }
4818 }
4819
4820 void
4821 sctp_toss_old_asconf(struct sctp_tcb *stcb)
4822 {
4823 struct sctp_association *asoc;
4824 struct sctp_tmit_chunk *chk, *chk_tmp;
4825
4826 asoc = &stcb->asoc;
4827 for (chk = TAILQ_FIRST(&asoc->control_send_queue); chk != NULL;
4828 chk = chk_tmp) {
4829 /* get next chk */
4830 chk_tmp = TAILQ_NEXT(chk, sctp_next);
4831 /* find SCTP_ASCONF chunk in queue (only one ever in queue) */
4832 if (chk->rec.chunk_id == SCTP_ASCONF) {
4833 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4834 if (chk->data) {
4835 sctp_m_freem(chk->data);
4836 chk->data = NULL;
4837 }
4838 asoc->ctrl_queue_cnt--;
4839 if (chk->whoTo)
4840 sctp_free_remote_addr(chk->whoTo);
4841 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4842 sctppcbinfo.ipi_count_chunk--;
4843 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4844 panic("Chunk count is negative");
4845 }
4846 sctppcbinfo.ipi_gencnt_chunk++;
4847 }
4848 }
4849 }
4850
4851
4852 static void
4853 sctp_clean_up_datalist(struct sctp_tcb *stcb,
4854 struct sctp_association *asoc,
4855 struct sctp_tmit_chunk **data_list,
4856 int bundle_at,
4857 struct sctp_nets *net)
4858 {
4859 int i;
4860 for (i = 0; i < bundle_at; i++) {
4861 /* off of the send queue */
4862 if (i) {
4863 /* Any chunk NOT 0 you zap the time
4864 * chunk 0 gets zapped or set based on
4865 * if a RTO measurment is needed.
4866 */
4867 data_list[i]->do_rtt = 0;
4868 }
4869 /* record time */
4870 data_list[i]->sent_rcv_time = net->last_sent_time;
4871 TAILQ_REMOVE(&asoc->send_queue,
4872 data_list[i],
4873 sctp_next);
4874 /* on to the sent queue */
4875 TAILQ_INSERT_TAIL(&asoc->sent_queue,
4876 data_list[i],
4877 sctp_next);
4878 /* This does not lower until the cum-ack passes it */
4879 asoc->sent_queue_cnt++;
4880 asoc->send_queue_cnt--;
4881 if ((asoc->peers_rwnd <= 0) &&
4882 (asoc->total_flight == 0) &&
4883 (bundle_at == 1)) {
4884 /* Mark the chunk as being a window probe */
4885 #ifdef SCTP_DEBUG
4886 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
4887 printf("WINDOW PROBE SET\n");
4888 }
4889 #endif
4890 sctp_pegs[SCTP_WINDOW_PROBES]++;
4891 data_list[i]->rec.data.state_flags |= SCTP_WINDOW_PROBE;
4892 } else {
4893 data_list[i]->rec.data.state_flags &= ~SCTP_WINDOW_PROBE;
4894 }
4895 #ifdef SCTP_AUDITING_ENABLED
4896 sctp_audit_log(0xC2, 3);
4897 #endif
4898 data_list[i]->sent = SCTP_DATAGRAM_SENT;
4899 data_list[i]->snd_count = 1;
4900 net->flight_size += data_list[i]->book_size;
4901 asoc->total_flight += data_list[i]->book_size;
4902 asoc->total_flight_count++;
4903 #ifdef SCTP_LOG_RWND
4904 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
4905 asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
4906 #endif
4907 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
4908 (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
4909 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
4910 /* SWS sender side engages */
4911 asoc->peers_rwnd = 0;
4912 }
4913 }
4914 }
4915
4916 static void
4917 sctp_clean_up_ctl(struct sctp_association *asoc)
4918 {
4919 struct sctp_tmit_chunk *chk, *nchk;
4920 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
4921 chk; chk = nchk) {
4922 nchk = TAILQ_NEXT(chk, sctp_next);
4923 if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
4924 (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
4925 (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
4926 (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
4927 (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
4928 (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
4929 (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
4930 (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
4931 (chk->rec.chunk_id == SCTP_ECN_CWR) ||
4932 (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
4933 /* Stray chunks must be cleaned up */
4934 clean_up_anyway:
4935 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
4936 if (chk->data) {
4937 sctp_m_freem(chk->data);
4938 chk->data = NULL;
4939 }
4940 asoc->ctrl_queue_cnt--;
4941 sctp_free_remote_addr(chk->whoTo);
4942 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
4943 sctppcbinfo.ipi_count_chunk--;
4944 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
4945 panic("Chunk count is negative");
4946 }
4947 sctppcbinfo.ipi_gencnt_chunk++;
4948 } else if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
4949 struct sctp_stream_reset_req *strreq;
4950 /* special handling, we must look into the param */
4951 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
4952 if (strreq->sr_req.ph.param_type == ntohs(SCTP_STR_RESET_RESPONSE)) {
4953 goto clean_up_anyway;
4954 }
4955 }
4956 }
4957 }
4958
4959 static int
4960 sctp_move_to_outqueue(struct sctp_tcb *stcb,
4961 struct sctp_stream_out *strq)
4962 {
4963 /* Move from the stream to the send_queue keeping track of the total */
4964 struct sctp_association *asoc;
4965 int tot_moved = 0;
4966 int failed = 0;
4967 int padval;
4968 struct sctp_tmit_chunk *chk, *nchk;
4969 struct sctp_data_chunk *dchkh;
4970 struct sctpchunk_listhead tmp;
4971 struct mbuf *orig;
4972
4973 asoc = &stcb->asoc;
4974 TAILQ_INIT(&tmp);
4975 chk = TAILQ_FIRST(&strq->outqueue);
4976 while (chk) {
4977 nchk = TAILQ_NEXT(chk, sctp_next);
4978 /* now put in the chunk header */
4979 orig = chk->data;
4980 M_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
4981 if (chk->data == NULL) {
4982 /* HELP */
4983 failed++;
4984 break;
4985 }
4986 if (orig != chk->data) {
4987 /* A new mbuf was added, account for it */
4988 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
4989 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
4990 stcb->sctp_socket->so_snd.sb_mbcnt += MSIZE;
4991 }
4992 #ifdef SCTP_MBCNT_LOGGING
4993 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
4994 asoc->total_output_queue_size,
4995 0,
4996 asoc->total_output_mbuf_queue_size,
4997 MSIZE);
4998 #endif
4999 stcb->asoc.total_output_mbuf_queue_size += MSIZE;
5000 chk->mbcnt += MSIZE;
5001 }
5002 chk->send_size += sizeof(struct sctp_data_chunk);
5003 /* This should NOT have to do anything, but
5004 * I would rather be cautious
5005 */
5006 if (!failed && ((size_t)chk->data->m_len < sizeof(struct sctp_data_chunk))) {
5007 m_pullup(chk->data, sizeof(struct sctp_data_chunk));
5008 if (chk->data == NULL) {
5009 failed++;
5010 break;
5011 }
5012 }
5013 dchkh = mtod(chk->data, struct sctp_data_chunk *);
5014 dchkh->ch.chunk_length = htons(chk->send_size);
5015 /* Chunks must be padded to even word boundary */
5016 padval = chk->send_size % 4;
5017 if (padval) {
5018 /* For fragmented messages this should not
5019 * run except possibly on the last chunk
5020 */
5021 if (sctp_pad_lastmbuf(chk->data, (4 - padval))) {
5022 /* we are in big big trouble no mbufs :< */
5023 failed++;
5024 break;
5025 }
5026 chk->send_size += (4 - padval);
5027 }
5028 /* pull from stream queue */
5029 TAILQ_REMOVE(&strq->outqueue, chk, sctp_next);
5030 asoc->stream_queue_cnt--;
5031 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
5032 /* add it in to the size of moved chunks */
5033 if (chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5034 /* we pull only one message */
5035 break;
5036 }
5037 chk = nchk;
5038 }
5039 if (failed) {
5040 /* Gak, we just lost the user message */
5041 chk = TAILQ_FIRST(&tmp);
5042 while (chk) {
5043 nchk = TAILQ_NEXT(chk, sctp_next);
5044 TAILQ_REMOVE(&tmp, chk, sctp_next);
5045
5046 sctp_ulp_notify(SCTP_NOTIFY_DG_FAIL, stcb,
5047 (SCTP_NOTIFY_DATAGRAM_UNSENT|SCTP_INTERNAL_ERROR),
5048 chk);
5049
5050 if (chk->data) {
5051 sctp_m_freem(chk->data);
5052 chk->data = NULL;
5053 }
5054 if (chk->whoTo) {
5055 sctp_free_remote_addr(chk->whoTo);
5056 chk->whoTo = NULL;
5057 }
5058 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5059 sctppcbinfo.ipi_count_chunk--;
5060 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5061 panic("Chunk count is negative");
5062 }
5063 sctppcbinfo.ipi_gencnt_chunk++;
5064 chk = nchk;
5065 }
5066 return (0);
5067 }
5068 /* now pull them off of temp wheel */
5069 chk = TAILQ_FIRST(&tmp);
5070 while (chk) {
5071 nchk = TAILQ_NEXT(chk, sctp_next);
5072 /* insert on send_queue */
5073 TAILQ_REMOVE(&tmp, chk, sctp_next);
5074 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
5075 asoc->send_queue_cnt++;
5076 /* assign TSN */
5077 chk->rec.data.TSN_seq = asoc->sending_seq++;
5078
5079 dchkh = mtod(chk->data, struct sctp_data_chunk *);
5080 /* Put the rest of the things in place now. Size
5081 * was done earlier in previous loop prior to
5082 * padding.
5083 */
5084 dchkh->ch.chunk_type = SCTP_DATA;
5085 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
5086 dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
5087 dchkh->dp.stream_id = htons(strq->stream_no);
5088 dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
5089 dchkh->dp.protocol_id = chk->rec.data.payloadtype;
5090 /* total count moved */
5091 tot_moved += chk->send_size;
5092 chk = nchk;
5093 }
5094 return (tot_moved);
5095 }
5096
5097 static void
5098 sctp_fill_outqueue(struct sctp_tcb *stcb,
5099 struct sctp_nets *net)
5100 {
5101 struct sctp_association *asoc;
5102 struct sctp_tmit_chunk *chk;
5103 struct sctp_stream_out *strq, *strqn;
5104 int mtu_fromwheel, goal_mtu;
5105 unsigned int moved, seenend, cnt_mvd=0;
5106
5107 asoc = &stcb->asoc;
5108 /* Attempt to move at least 1 MTU's worth
5109 * onto the wheel for each destination address
5110 */
5111 goal_mtu = net->cwnd - net->flight_size;
5112 if ((unsigned int)goal_mtu < net->mtu) {
5113 goal_mtu = net->mtu;
5114 }
5115 if (sctp_pegs[SCTP_MOVED_MTU] < (unsigned int)goal_mtu) {
5116 sctp_pegs[SCTP_MOVED_MTU] = goal_mtu;
5117 }
5118 seenend = moved = mtu_fromwheel = 0;
5119 if (asoc->last_out_stream == NULL) {
5120 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5121 if (asoc->last_out_stream == NULL) {
5122 /* huh nothing on the wheel, TSNH */
5123 return;
5124 }
5125 goto done_it;
5126 }
5127 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
5128 done_it:
5129 if (strq == NULL) {
5130 asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
5131 }
5132 while (mtu_fromwheel < goal_mtu) {
5133 if (strq == NULL) {
5134 if (seenend == 0) {
5135 seenend = 1;
5136 strq = TAILQ_FIRST(&asoc->out_wheel);
5137 } else if ((moved == 0) && (seenend)) {
5138 /* none left on the wheel */
5139 sctp_pegs[SCTP_MOVED_NLEF]++;
5140 return;
5141 } else if (moved) {
5142 /*
5143 * clear the flags and rotate back through
5144 * again
5145 */
5146 moved = 0;
5147 seenend = 0;
5148 strq = TAILQ_FIRST(&asoc->out_wheel);
5149 }
5150 if (strq == NULL)
5151 break;
5152 continue;
5153 }
5154 strqn = TAILQ_NEXT(strq, next_spoke);
5155 if ((chk = TAILQ_FIRST(&strq->outqueue)) == NULL) {
5156 /* none left on this queue, prune a spoke? */
5157 sctp_remove_from_wheel(asoc, strq);
5158 if (strq == asoc->last_out_stream) {
5159 /* the last one we used went off the wheel */
5160 asoc->last_out_stream = NULL;
5161 }
5162 strq = strqn;
5163 continue;
5164 }
5165 if (chk->whoTo != net) {
5166 /* Skip this stream, first one on stream
5167 * does not head to our current destination.
5168 */
5169 strq = strqn;
5170 continue;
5171 }
5172 mtu_fromwheel += sctp_move_to_outqueue(stcb, strq);
5173 cnt_mvd++;
5174 moved++;
5175 asoc->last_out_stream = strq;
5176 strq = strqn;
5177 }
5178 sctp_pegs[SCTP_MOVED_MAX]++;
5179 #ifdef SCTP_DEBUG
5180 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5181 printf("Ok we moved %d chunks to send queue\n",
5182 moved);
5183 }
5184 #endif
5185 if (sctp_pegs[SCTP_MOVED_QMAX] < cnt_mvd) {
5186 sctp_pegs[SCTP_MOVED_QMAX] = cnt_mvd;
5187 }
5188 }
5189
5190 void
5191 sctp_fix_ecn_echo(struct sctp_association *asoc)
5192 {
5193 struct sctp_tmit_chunk *chk;
5194 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5195 if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
5196 chk->sent = SCTP_DATAGRAM_UNSENT;
5197 }
5198 }
5199 }
5200
5201 static void
5202 sctp_move_to_an_alt(struct sctp_tcb *stcb,
5203 struct sctp_association *asoc,
5204 struct sctp_nets *net)
5205 {
5206 struct sctp_tmit_chunk *chk;
5207 struct sctp_nets *a_net;
5208 a_net = sctp_find_alternate_net(stcb, net);
5209 if ((a_net != net) &&
5210 ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
5211 /*
5212 * We only proceed if a valid alternate is found that is
5213 * not this one and is reachable. Here we must move all
5214 * chunks queued in the send queue off of the destination
5215 * address to our alternate.
5216 */
5217 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5218 if (chk->whoTo == net) {
5219 /* Move the chunk to our alternate */
5220 sctp_free_remote_addr(chk->whoTo);
5221 chk->whoTo = a_net;
5222 a_net->ref_count++;
5223 }
5224 }
5225 }
5226 }
5227
5228 static int sctp_from_user_send=0;
5229
5230 static int
5231 sctp_med_chunk_output(struct sctp_inpcb *inp,
5232 struct sctp_tcb *stcb,
5233 struct sctp_association *asoc,
5234 int *num_out,
5235 int *reason_code,
5236 int control_only, int *cwnd_full, int from_where,
5237 struct timeval *now, int *now_filled)
5238 {
5239 /*
5240 * Ok this is the generic chunk service queue.
5241 * we must do the following:
5242 * - Service the stream queue that is next, moving any message
5243 * (note I must get a complete message i.e. FIRST/MIDDLE and
5244 * LAST to the out queue in one pass) and assigning TSN's
5245 * - Check to see if the cwnd/rwnd allows any output, if so we
5246 * go ahead and fomulate and send the low level chunks. Making
5247 * sure to combine any control in the control chunk queue also.
5248 */
5249 struct sctp_nets *net;
5250 struct mbuf *outchain;
5251 struct sctp_tmit_chunk *chk, *nchk;
5252 struct sctphdr *shdr;
5253 /* temp arrays for unlinking */
5254 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
5255 int no_fragmentflg, error;
5256 int one_chunk, hbflag;
5257 int asconf, cookie, no_out_cnt;
5258 int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind;
5259 unsigned int mtu, r_mtu, omtu;
5260 *num_out = 0;
5261 cwnd_full_ind = 0;
5262 ctl_cnt = no_out_cnt = asconf = cookie = 0;
5263 /*
5264 * First lets prime the pump. For each destination, if there
5265 * is room in the flight size, attempt to pull an MTU's worth
5266 * out of the stream queues into the general send_queue
5267 */
5268 #ifdef SCTP_AUDITING_ENABLED
5269 sctp_audit_log(0xC2, 2);
5270 #endif
5271 #ifdef SCTP_DEBUG
5272 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5273 printf("***********************\n");
5274 }
5275 #endif
5276 hbflag = 0;
5277 if (control_only)
5278 no_data_chunks = 1;
5279 else
5280 no_data_chunks = 0;
5281
5282 /* Nothing to possible to send? */
5283 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
5284 TAILQ_EMPTY(&asoc->send_queue) &&
5285 TAILQ_EMPTY(&asoc->out_wheel)) {
5286 #ifdef SCTP_DEBUG
5287 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5288 printf("All wheels empty\n");
5289 }
5290 #endif
5291 return (0);
5292 }
5293 if (asoc->peers_rwnd <= 0) {
5294 /* No room in peers rwnd */
5295 *cwnd_full = 1;
5296 *reason_code = 1;
5297 if (asoc->total_flight > 0) {
5298 /* we are allowed one chunk in flight */
5299 no_data_chunks = 1;
5300 sctp_pegs[SCTP_RWND_BLOCKED]++;
5301 }
5302 }
5303 #ifdef SCTP_DEBUG
5304 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5305 printf("Ok we have done the fillup no_data_chunk=%d tf=%d prw:%d\n",
5306 (int)no_data_chunks,
5307 (int)asoc->total_flight, (int)asoc->peers_rwnd);
5308 }
5309 #endif
5310 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5311 #ifdef SCTP_DEBUG
5312 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5313 printf("net:%p fs:%d cwnd:%d\n",
5314 net, net->flight_size, net->cwnd);
5315 }
5316 #endif
5317 if (net->flight_size >= net->cwnd) {
5318 /* skip this network, no room */
5319 cwnd_full_ind++;
5320 #ifdef SCTP_DEBUG
5321 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5322 printf("Ok skip fillup->fs:%d > cwnd:%d\n",
5323 net->flight_size,
5324 net->cwnd);
5325 }
5326 #endif
5327 sctp_pegs[SCTP_CWND_NOFILL]++;
5328 continue;
5329 }
5330 /*
5331 * spin through the stream queues moving one message and
5332 * assign TSN's as appropriate.
5333 */
5334 sctp_fill_outqueue(stcb, net);
5335 }
5336 *cwnd_full = cwnd_full_ind;
5337 /* now service each destination and send out what we can for it */
5338 #ifdef SCTP_DEBUG
5339 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5340 int chk_cnt = 0;
5341 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
5342 chk_cnt++;
5343 }
5344 printf("We have %d chunks on the send_queue\n", chk_cnt);
5345 chk_cnt = 0;
5346 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5347 chk_cnt++;
5348 }
5349 printf("We have %d chunks on the sent_queue\n", chk_cnt);
5350 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
5351 chk_cnt++;
5352 }
5353 printf("We have %d chunks on the control_queue\n", chk_cnt);
5354 }
5355 #endif
5356 /* If we have data to send, and DSACK is running, stop it
5357 * and build a SACK to dump on to bundle with output. This
5358 * actually MAY make it so the bundling does not occur if
5359 * the SACK is big but I think this is ok because basic SACK
5360 * space is pre-reserved in our fragmentation size choice.
5361 */
5362 if ((TAILQ_FIRST(&asoc->send_queue) != NULL) &&
5363 (no_data_chunks == 0)) {
5364 /* We will be sending something */
5365 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5366 /* Yep a callout is pending */
5367 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5368 stcb->sctp_ep,
5369 stcb, NULL);
5370 sctp_send_sack(stcb);
5371 }
5372 }
5373 /* Nothing to send? */
5374 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
5375 (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
5376 return (0);
5377 }
5378 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5379 /* how much can we send? */
5380 if (net->ref_count < 2) {
5381 /* Ref-count of 1 so we cannot have data or control
5382 * queued to this address. Skip it.
5383 */
5384 continue;
5385 }
5386 ctl_cnt = bundle_at = 0;
5387 outchain = NULL;
5388 no_fragmentflg = 1;
5389 one_chunk = 0;
5390
5391 if (rtcache_validate(&net->ro)) {
5392 /* if we have a route and an ifp
5393 * check to see if we have room to
5394 * send to this guy
5395 */
5396 struct ifnet *ifp;
5397 ifp = net->ro._ro_rt->rt_ifp;
5398 if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
5399 sctp_pegs[SCTP_IFP_QUEUE_FULL]++;
5400 #ifdef SCTP_LOG_MAXBURST
5401 sctp_log_maxburst(net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
5402 #endif
5403 continue;
5404 }
5405 }
5406 if (((struct sockaddr *)&net->ro.ro_sa)->sa_family == AF_INET) {
5407 mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5408 } else {
5409 mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5410 }
5411 if (mtu > asoc->peers_rwnd) {
5412 if (asoc->total_flight > 0) {
5413 /* We have a packet in flight somewhere */
5414 r_mtu = asoc->peers_rwnd;
5415 } else {
5416 /* We are always allowed to send one MTU out */
5417 one_chunk = 1;
5418 r_mtu = mtu;
5419 }
5420 } else {
5421 r_mtu = mtu;
5422 }
5423 #ifdef SCTP_DEBUG
5424 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5425 printf("Ok r_mtu is %d mtu is %d for this net:%p one_chunk:%d\n",
5426 r_mtu, mtu, net, one_chunk);
5427 }
5428 #endif
5429 /************************/
5430 /* Control transmission */
5431 /************************/
5432 /* Now first lets go through the control queue */
5433 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
5434 chk; chk = nchk) {
5435 nchk = TAILQ_NEXT(chk, sctp_next);
5436 if (chk->whoTo != net) {
5437 /*
5438 * No, not sent to the network we are
5439 * looking at
5440 */
5441 continue;
5442 }
5443 if (chk->data == NULL) {
5444 continue;
5445 }
5446 if ((chk->data->m_flags & M_PKTHDR) == 0) {
5447 /*
5448 * NOTE: the chk queue MUST have the PKTHDR
5449 * flag set on it with a total in the
5450 * m_pkthdr.len field!! else the chunk will
5451 * ALWAYS be skipped
5452 */
5453 continue;
5454 }
5455 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
5456 /*
5457 * It must be unsent. Cookies and ASCONF's
5458 * hang around but there timers will force
5459 * when marked for resend.
5460 */
5461 continue;
5462 }
5463 /* Here we do NOT factor the r_mtu */
5464 if ((chk->data->m_pkthdr.len < (int)mtu) ||
5465 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
5466 /*
5467 * We probably should glom the mbuf chain from
5468 * the chk->data for control but the problem
5469 * is it becomes yet one more level of
5470 * tracking to do if for some reason output
5471 * fails. Then I have got to reconstruct the
5472 * merged control chain.. el yucko.. for now
5473 * we take the easy way and do the copy
5474 */
5475 outchain = sctp_copy_mbufchain(chk->data,
5476 outchain);
5477 if (outchain == NULL) {
5478 return (ENOMEM);
5479 }
5480 /* update our MTU size */
5481 if (mtu > chk->data->m_pkthdr.len)
5482 mtu -= chk->data->m_pkthdr.len;
5483 else
5484 mtu = 0;
5485 /* Do clear IP_DF ? */
5486 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5487 no_fragmentflg = 0;
5488 }
5489 /* Mark things to be removed, if needed */
5490 if ((chk->rec.chunk_id == SCTP_SELECTIVE_ACK) ||
5491 (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST) ||
5492 (chk->rec.chunk_id == SCTP_HEARTBEAT_ACK) ||
5493 (chk->rec.chunk_id == SCTP_SHUTDOWN) ||
5494 (chk->rec.chunk_id == SCTP_SHUTDOWN_ACK) ||
5495 (chk->rec.chunk_id == SCTP_OPERATION_ERROR) ||
5496 (chk->rec.chunk_id == SCTP_COOKIE_ACK) ||
5497 (chk->rec.chunk_id == SCTP_ECN_CWR) ||
5498 (chk->rec.chunk_id == SCTP_PACKET_DROPPED) ||
5499 (chk->rec.chunk_id == SCTP_ASCONF_ACK)) {
5500
5501 if (chk->rec.chunk_id == SCTP_HEARTBEAT_REQUEST)
5502 hbflag = 1;
5503 /* remove these chunks at the end */
5504 if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
5505 /* turn off the timer */
5506 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
5507 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
5508 inp, stcb, net);
5509 }
5510 }
5511 ctl_cnt++;
5512 } else {
5513 /*
5514 * Other chunks, since they have
5515 * timers running (i.e. COOKIE or
5516 * ASCONF) we just "trust" that it
5517 * gets sent or retransmitted.
5518 */
5519 ctl_cnt++;
5520 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
5521 cookie = 1;
5522 no_out_cnt = 1;
5523 } else if (chk->rec.chunk_id == SCTP_ASCONF) {
5524 /*
5525 * set hb flag since we can use
5526 * these for RTO
5527 */
5528 hbflag = 1;
5529 asconf = 1;
5530 }
5531 chk->sent = SCTP_DATAGRAM_SENT;
5532 chk->snd_count++;
5533 }
5534 if (mtu == 0) {
5535 /*
5536 * Ok we are out of room but we can
5537 * output without effecting the flight
5538 * size since this little guy is a
5539 * control only packet.
5540 */
5541 if (asconf) {
5542 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5543 asconf = 0;
5544 }
5545 if (cookie) {
5546 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5547 cookie = 0;
5548 }
5549 if (outchain->m_len == 0) {
5550 /*
5551 * Special case for when you
5552 * get a 0 len mbuf at the
5553 * head due to the lack of a
5554 * MHDR at the beginning.
5555 */
5556 outchain->m_len = sizeof(struct sctphdr);
5557 } else {
5558 M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
5559 if (outchain == NULL) {
5560 /* no memory */
5561 error = ENOBUFS;
5562 goto error_out_again;
5563 }
5564 }
5565 shdr = mtod(outchain, struct sctphdr *);
5566 shdr->src_port = inp->sctp_lport;
5567 shdr->dest_port = stcb->rport;
5568 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
5569 shdr->checksum = 0;
5570
5571 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5572 rtcache_getdst(&net->ro),
5573 outchain,
5574 no_fragmentflg, 0, NULL, asconf))) {
5575 if (error == ENOBUFS) {
5576 asoc->ifp_had_enobuf = 1;
5577 }
5578 sctp_pegs[SCTP_DATA_OUT_ERR]++;
5579 if (from_where == 0) {
5580 sctp_pegs[SCTP_ERROUT_FRM_USR]++;
5581 }
5582 error_out_again:
5583 #ifdef SCTP_DEBUG
5584 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
5585 printf("Gak got ctrl error %d\n", error);
5586 }
5587 #endif
5588 /* error, could not output */
5589 if (hbflag) {
5590 #ifdef SCTP_DEBUG
5591 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5592 printf("Update HB anyway\n");
5593 }
5594 #endif
5595 if (*now_filled == 0) {
5596 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5597 *now_filled = 1;
5598 *now = net->last_sent_time;
5599 } else {
5600 net->last_sent_time = *now;
5601 }
5602 hbflag = 0;
5603 }
5604 if (error == EHOSTUNREACH) {
5605 /*
5606 * Destination went
5607 * unreachable during
5608 * this send
5609 */
5610 #ifdef SCTP_DEBUG
5611 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5612 printf("Moving data to an alterante\n");
5613 }
5614 #endif
5615 sctp_move_to_an_alt(stcb, asoc, net);
5616 }
5617 sctp_clean_up_ctl (asoc);
5618 return (error);
5619 } else
5620 asoc->ifp_had_enobuf = 0;
5621 /* Only HB or ASCONF advances time */
5622 if (hbflag) {
5623 if (*now_filled == 0) {
5624 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5625 *now_filled = 1;
5626 *now = net->last_sent_time;
5627 } else {
5628 net->last_sent_time = *now;
5629 }
5630 hbflag = 0;
5631 }
5632 /*
5633 * increase the number we sent, if a
5634 * cookie is sent we don't tell them
5635 * any was sent out.
5636 */
5637 if (!no_out_cnt)
5638 *num_out += ctl_cnt;
5639 /* recalc a clean slate and setup */
5640 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5641 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
5642 } else {
5643 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
5644 }
5645 no_fragmentflg = 1;
5646 }
5647 }
5648 }
5649 /*********************/
5650 /* Data transmission */
5651 /*********************/
5652 /* now lets add any data within the MTU constraints */
5653 if (((struct sockaddr *)&net->ro.ro_sa)->sa_family == AF_INET) {
5654 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
5655 } else {
5656 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
5657 }
5658
5659 #ifdef SCTP_DEBUG
5660 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5661 printf("Now to data transmission\n");
5662 }
5663 #endif
5664
5665 if (((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) ||
5666 (cookie)) {
5667 for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
5668 if (no_data_chunks) {
5669 /* let only control go out */
5670 #ifdef SCTP_DEBUG
5671 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5672 printf("Either nothing to send or we are full\n");
5673 }
5674 #endif
5675 break;
5676 }
5677 if (net->flight_size >= net->cwnd) {
5678 /* skip this net, no room for data */
5679 #ifdef SCTP_DEBUG
5680 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5681 printf("fs:%d > cwnd:%d\n",
5682 net->flight_size, net->cwnd);
5683 }
5684 #endif
5685 sctp_pegs[SCTP_CWND_BLOCKED]++;
5686 *reason_code = 2;
5687 break;
5688 }
5689 nchk = TAILQ_NEXT(chk, sctp_next);
5690 if (chk->whoTo != net) {
5691 /* No, not sent to this net */
5692 #ifdef SCTP_DEBUG
5693 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5694 printf("chk->whoTo:%p not %p\n",
5695 chk->whoTo, net);
5696
5697 }
5698 #endif
5699 continue;
5700 }
5701 #ifdef SCTP_DEBUG
5702 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5703 printf("Can we pick up a chunk?\n");
5704 }
5705 #endif
5706 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
5707 /* strange, we have a chunk that is to bit
5708 * for its destination and yet no fragment ok flag.
5709 * Something went wrong when the PMTU changed...we did
5710 * not mark this chunk for some reason?? I will
5711 * fix it here by letting IP fragment it for now and
5712 * printing a warning. This really should not happen ...
5713 */
5714 /*#ifdef SCTP_DEBUG*/
5715 printf("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
5716 chk->send_size, mtu);
5717 /*#endif*/
5718 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
5719 }
5720
5721 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
5722 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
5723 /* ok we will add this one */
5724 #ifdef SCTP_DEBUG
5725 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5726 printf("Picking up the chunk\n");
5727 }
5728 #endif
5729 outchain = sctp_copy_mbufchain(chk->data, outchain);
5730 if (outchain == NULL) {
5731 #ifdef SCTP_DEBUG
5732 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5733 printf("Gakk no memory\n");
5734 }
5735 #endif
5736 if (!callout_pending(&net->rxt_timer.timer)) {
5737 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5738 }
5739 return (ENOMEM);
5740 }
5741 /* upate our MTU size */
5742 /* Do clear IP_DF ? */
5743 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
5744 no_fragmentflg = 0;
5745 }
5746 mtu -= chk->send_size;
5747 r_mtu -= chk->send_size;
5748 data_list[bundle_at++] = chk;
5749 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
5750 mtu = 0;
5751 break;
5752 }
5753 if (mtu <= 0) {
5754 mtu = 0;
5755 break;
5756 }
5757 if ((r_mtu <= 0) || one_chunk) {
5758 r_mtu = 0;
5759 break;
5760 }
5761 } else {
5762 /*
5763 * Must be sent in order of the TSN's
5764 * (on a network)
5765 */
5766 #ifdef SCTP_DEBUG
5767 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5768 printf("ok no more chk:%d > mtu:%d || < r_mtu:%d\n",
5769 chk->send_size, mtu, r_mtu);
5770 }
5771 #endif
5772
5773 break;
5774 }
5775 }/* for () */
5776 } /* if asoc.state OPEN */
5777 /* Is there something to send for this destination? */
5778 #ifdef SCTP_DEBUG
5779 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5780 printf("ok now is chain assembled? %p\n",
5781 outchain);
5782 }
5783 #endif
5784
5785 if (outchain) {
5786 /* We may need to start a control timer or two */
5787 if (asconf) {
5788 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
5789 asconf = 0;
5790 }
5791 if (cookie) {
5792 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
5793 cookie = 0;
5794 }
5795 /* must start a send timer if data is being sent */
5796 if (bundle_at && (!callout_pending(&net->rxt_timer.timer))) {
5797 /* no timer running on this destination
5798 * restart it.
5799 */
5800 #ifdef SCTP_DEBUG
5801 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5802 printf("ok lets start a send timer .. we will transmit %p\n",
5803 outchain);
5804 }
5805 #endif
5806 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
5807 }
5808 /* Now send it, if there is anything to send :> */
5809 if ((outchain->m_flags & M_PKTHDR) == 0) {
5810 struct mbuf *t;
5811
5812 MGETHDR(t, M_DONTWAIT, MT_HEADER);
5813 if (t == NULL) {
5814 sctp_m_freem(outchain);
5815 return (ENOMEM);
5816 }
5817 t->m_next = outchain;
5818 t->m_pkthdr.len = 0;
5819 t->m_pkthdr.rcvif = 0;
5820 t->m_len = 0;
5821
5822 outchain = t;
5823 while (t) {
5824 outchain->m_pkthdr.len += t->m_len;
5825 t = t->m_next;
5826 }
5827 }
5828 if (outchain->m_len == 0) {
5829 /* Special case for when you get a 0 len
5830 * mbuf at the head due to the lack
5831 * of a MHDR at the beginning.
5832 */
5833 MH_ALIGN(outchain, sizeof(struct sctphdr));
5834 outchain->m_len = sizeof(struct sctphdr);
5835 } else {
5836 M_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
5837 if (outchain == NULL) {
5838 /* out of mbufs */
5839 error = ENOBUFS;
5840 goto errored_send;
5841 }
5842 }
5843 shdr = mtod(outchain, struct sctphdr *);
5844 shdr->src_port = inp->sctp_lport;
5845 shdr->dest_port = stcb->rport;
5846 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
5847 shdr->checksum = 0;
5848 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5849 rtcache_getdst(&net->ro),
5850 outchain,
5851 no_fragmentflg, bundle_at, data_list[0], asconf))) {
5852 /* error, we could not output */
5853 if (error == ENOBUFS) {
5854 asoc->ifp_had_enobuf = 1;
5855 }
5856 sctp_pegs[SCTP_DATA_OUT_ERR]++;
5857 if (from_where == 0) {
5858 sctp_pegs[SCTP_ERROUT_FRM_USR]++;
5859 }
5860
5861 errored_send:
5862 #ifdef SCTP_DEBUG
5863 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5864 printf("Gak send error %d\n", error);
5865 }
5866 #endif
5867 if (hbflag) {
5868 #ifdef SCTP_DEBUG
5869 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5870 printf("Update HB time anyway\n");
5871 }
5872 #endif
5873 if (*now_filled == 0) {
5874 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5875 *now_filled = 1;
5876 *now = net->last_sent_time;
5877 } else {
5878 net->last_sent_time = *now;
5879 }
5880 hbflag = 0;
5881 }
5882 if (error == EHOSTUNREACH) {
5883 /*
5884 * Destination went unreachable during
5885 * this send
5886 */
5887 #ifdef SCTP_DEBUG
5888 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
5889 printf("Calling the movement routine\n");
5890 }
5891 #endif
5892 sctp_move_to_an_alt(stcb, asoc, net);
5893 }
5894 sctp_clean_up_ctl (asoc);
5895 return (error);
5896 } else {
5897 asoc->ifp_had_enobuf = 0;
5898 }
5899 if (bundle_at || hbflag) {
5900 /* For data/asconf and hb set time */
5901 if (*now_filled == 0) {
5902 SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5903 *now_filled = 1;
5904 *now = net->last_sent_time;
5905 } else {
5906 net->last_sent_time = *now;
5907 }
5908 }
5909
5910 if (!no_out_cnt) {
5911 *num_out += (ctl_cnt + bundle_at);
5912 }
5913 if (bundle_at) {
5914 if (!net->rto_pending) {
5915 /* setup for a RTO measurement */
5916 net->rto_pending = 1;
5917 data_list[0]->do_rtt = 1;
5918 } else {
5919 data_list[0]->do_rtt = 0;
5920 }
5921 sctp_pegs[SCTP_PEG_TSNS_SENT] += bundle_at;
5922 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
5923 }
5924 if (one_chunk) {
5925 break;
5926 }
5927 }
5928 }
5929 /* At the end there should be no NON timed
5930 * chunks hanging on this queue.
5931 */
5932 if ((*num_out == 0) && (*reason_code == 0)) {
5933 *reason_code = 3;
5934 }
5935 sctp_clean_up_ctl (asoc);
5936 return (0);
5937 }
5938
5939 void
5940 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
5941 {
5942 /* Prepend a OPERATIONAL_ERROR chunk header
5943 * and put on the end of the control chunk queue.
5944 */
5945 /* Sender had better have gotten a MGETHDR or else
5946 * the control chunk will be forever skipped
5947 */
5948 struct sctp_chunkhdr *hdr;
5949 struct sctp_tmit_chunk *chk;
5950 struct mbuf *mat;
5951
5952 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
5953 if (chk == NULL) {
5954 /* no memory */
5955 sctp_m_freem(op_err);
5956 return;
5957 }
5958 sctppcbinfo.ipi_count_chunk++;
5959 sctppcbinfo.ipi_gencnt_chunk++;
5960 M_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
5961 if (op_err == NULL) {
5962 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
5963 sctppcbinfo.ipi_count_chunk--;
5964 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
5965 panic("Chunk count is negative");
5966 }
5967 sctppcbinfo.ipi_gencnt_chunk++;
5968 return;
5969 }
5970 chk->send_size = 0;
5971 mat = op_err;
5972 while (mat != NULL) {
5973 chk->send_size += mat->m_len;
5974 mat = mat->m_next;
5975 }
5976 chk->rec.chunk_id = SCTP_OPERATION_ERROR;
5977 chk->sent = SCTP_DATAGRAM_UNSENT;
5978 chk->snd_count = 0;
5979 chk->flags = 0;
5980 chk->asoc = &stcb->asoc;
5981 chk->data = op_err;
5982 chk->whoTo = chk->asoc->primary_destination;
5983 chk->whoTo->ref_count++;
5984 hdr = mtod(op_err, struct sctp_chunkhdr *);
5985 hdr->chunk_type = SCTP_OPERATION_ERROR;
5986 hdr->chunk_flags = 0;
5987 hdr->chunk_length = htons(chk->send_size);
5988 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
5989 chk,
5990 sctp_next);
5991 chk->asoc->ctrl_queue_cnt++;
5992 }
5993
5994 int
5995 sctp_send_cookie_echo(struct mbuf *m,
5996 int offset,
5997 struct sctp_tcb *stcb,
5998 struct sctp_nets *net)
5999 {
6000 /*
6001 * pull out the cookie and put it at the front of the control
6002 * chunk queue.
6003 */
6004 int at;
6005 struct mbuf *cookie, *mat;
6006 struct sctp_paramhdr parm, *phdr;
6007 struct sctp_chunkhdr *hdr;
6008 struct sctp_tmit_chunk *chk;
6009 uint16_t ptype, plen;
6010 /* First find the cookie in the param area */
6011 cookie = NULL;
6012 at = offset + sizeof(struct sctp_init_chunk);
6013
6014 do {
6015 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
6016 if (phdr == NULL) {
6017 return (-3);
6018 }
6019 ptype = ntohs(phdr->param_type);
6020 plen = ntohs(phdr->param_length);
6021 if (ptype == SCTP_STATE_COOKIE) {
6022 int pad;
6023 /* found the cookie */
6024 if ((pad = (plen % 4))) {
6025 plen += 4 - pad;
6026 }
6027 cookie = sctp_m_copym(m, at, plen, M_DONTWAIT);
6028 if (cookie == NULL) {
6029 /* No memory */
6030 return (-2);
6031 }
6032 break;
6033 }
6034 at += SCTP_SIZE32(plen);
6035 } while (phdr);
6036 if (cookie == NULL) {
6037 /* Did not find the cookie */
6038 return (-3);
6039 }
6040 /* ok, we got the cookie lets change it into a cookie echo chunk */
6041
6042 /* first the change from param to cookie */
6043 hdr = mtod(cookie, struct sctp_chunkhdr *);
6044 hdr->chunk_type = SCTP_COOKIE_ECHO;
6045 hdr->chunk_flags = 0;
6046 /* now we MUST have a PKTHDR on it */
6047 if ((cookie->m_flags & M_PKTHDR) != M_PKTHDR) {
6048 /* we hope this happens rarely */
6049 MGETHDR(mat, M_DONTWAIT, MT_HEADER);
6050 if (mat == NULL) {
6051 sctp_m_freem(cookie);
6052 return (-4);
6053 }
6054 mat->m_len = 0;
6055 mat->m_pkthdr.rcvif = 0;
6056 mat->m_next = cookie;
6057 cookie = mat;
6058 }
6059 cookie->m_pkthdr.len = plen;
6060 /* get the chunk stuff now and place it in the FRONT of the queue */
6061 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6062 if (chk == NULL) {
6063 /* no memory */
6064 sctp_m_freem(cookie);
6065 return (-5);
6066 }
6067 sctppcbinfo.ipi_count_chunk++;
6068 sctppcbinfo.ipi_gencnt_chunk++;
6069 chk->send_size = cookie->m_pkthdr.len;
6070 chk->rec.chunk_id = SCTP_COOKIE_ECHO;
6071 chk->sent = SCTP_DATAGRAM_UNSENT;
6072 chk->snd_count = 0;
6073 chk->flags = 0;
6074 chk->asoc = &stcb->asoc;
6075 chk->data = cookie;
6076 chk->whoTo = chk->asoc->primary_destination;
6077 chk->whoTo->ref_count++;
6078 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
6079 chk->asoc->ctrl_queue_cnt++;
6080 return (0);
6081 }
6082
6083 void
6084 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
6085 struct mbuf *m,
6086 int offset,
6087 int chk_length,
6088 struct sctp_nets *net)
6089 {
6090 /* take a HB request and make it into a
6091 * HB ack and send it.
6092 */
6093 struct mbuf *outchain;
6094 struct sctp_chunkhdr *chdr;
6095 struct sctp_tmit_chunk *chk;
6096
6097
6098 if (net == NULL)
6099 /* must have a net pointer */
6100 return;
6101
6102 outchain = sctp_m_copym(m, offset, chk_length, M_DONTWAIT);
6103 if (outchain == NULL) {
6104 /* gak out of memory */
6105 return;
6106 }
6107 chdr = mtod(outchain, struct sctp_chunkhdr *);
6108 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
6109 chdr->chunk_flags = 0;
6110 if ((outchain->m_flags & M_PKTHDR) != M_PKTHDR) {
6111 /* should not happen but we are cautious. */
6112 struct mbuf *tmp;
6113 MGETHDR(tmp, M_DONTWAIT, MT_HEADER);
6114 if (tmp == NULL) {
6115 return;
6116 }
6117 tmp->m_len = 0;
6118 tmp->m_pkthdr.rcvif = 0;
6119 tmp->m_next = outchain;
6120 outchain = tmp;
6121 }
6122 outchain->m_pkthdr.len = chk_length;
6123 if (chk_length % 4) {
6124 /* need pad */
6125 u_int32_t cpthis=0;
6126 int padlen;
6127 padlen = 4 - (outchain->m_pkthdr.len % 4);
6128 m_copyback(outchain, outchain->m_pkthdr.len, padlen, (void *)&cpthis);
6129 }
6130 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6131 if (chk == NULL) {
6132 /* no memory */
6133 sctp_m_freem(outchain);
6134 return ;
6135 }
6136 sctppcbinfo.ipi_count_chunk++;
6137 sctppcbinfo.ipi_gencnt_chunk++;
6138
6139 chk->send_size = chk_length;
6140 chk->rec.chunk_id = SCTP_HEARTBEAT_ACK;
6141 chk->sent = SCTP_DATAGRAM_UNSENT;
6142 chk->snd_count = 0;
6143 chk->flags = 0;
6144 chk->asoc = &stcb->asoc;
6145 chk->data = outchain;
6146 chk->whoTo = net;
6147 chk->whoTo->ref_count++;
6148 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6149 chk->asoc->ctrl_queue_cnt++;
6150 }
6151
6152 int
6153 sctp_send_cookie_ack(struct sctp_tcb *stcb) {
6154 /* formulate and queue a cookie-ack back to sender */
6155 struct mbuf *cookie_ack;
6156 struct sctp_chunkhdr *hdr;
6157 struct sctp_tmit_chunk *chk;
6158
6159 cookie_ack = NULL;
6160 MGETHDR(cookie_ack, M_DONTWAIT, MT_HEADER);
6161 if (cookie_ack == NULL) {
6162 /* no mbuf's */
6163 return (-1);
6164 }
6165 cookie_ack->m_data += SCTP_MIN_OVERHEAD;
6166 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6167 if (chk == NULL) {
6168 /* no memory */
6169 sctp_m_freem(cookie_ack);
6170 return (-1);
6171 }
6172 sctppcbinfo.ipi_count_chunk++;
6173 sctppcbinfo.ipi_gencnt_chunk++;
6174
6175 chk->send_size = sizeof(struct sctp_chunkhdr);
6176 chk->rec.chunk_id = SCTP_COOKIE_ACK;
6177 chk->sent = SCTP_DATAGRAM_UNSENT;
6178 chk->snd_count = 0;
6179 chk->flags = 0;
6180 chk->asoc = &stcb->asoc;
6181 chk->data = cookie_ack;
6182 if (chk->asoc->last_control_chunk_from != NULL) {
6183 chk->whoTo = chk->asoc->last_control_chunk_from;
6184 } else {
6185 chk->whoTo = chk->asoc->primary_destination;
6186 }
6187 chk->whoTo->ref_count++;
6188 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
6189 hdr->chunk_type = SCTP_COOKIE_ACK;
6190 hdr->chunk_flags = 0;
6191 hdr->chunk_length = htons(chk->send_size);
6192 cookie_ack->m_pkthdr.len = cookie_ack->m_len = chk->send_size;
6193 cookie_ack->m_pkthdr.rcvif = 0;
6194 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6195 chk->asoc->ctrl_queue_cnt++;
6196 return (0);
6197 }
6198
6199
6200 int
6201 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
6202 {
6203 /* formulate and queue a SHUTDOWN-ACK back to the sender */
6204 struct mbuf *m_shutdown_ack;
6205 struct sctp_shutdown_ack_chunk *ack_cp;
6206 struct sctp_tmit_chunk *chk;
6207
6208 m_shutdown_ack = NULL;
6209 MGETHDR(m_shutdown_ack, M_DONTWAIT, MT_HEADER);
6210 if (m_shutdown_ack == NULL) {
6211 /* no mbuf's */
6212 return (-1);
6213 }
6214 m_shutdown_ack->m_data += SCTP_MIN_OVERHEAD;
6215 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6216 if (chk == NULL) {
6217 /* no memory */
6218 sctp_m_freem(m_shutdown_ack);
6219 return (-1);
6220 }
6221 sctppcbinfo.ipi_count_chunk++;
6222 sctppcbinfo.ipi_gencnt_chunk++;
6223
6224 chk->send_size = sizeof(struct sctp_chunkhdr);
6225 chk->rec.chunk_id = SCTP_SHUTDOWN_ACK;
6226 chk->sent = SCTP_DATAGRAM_UNSENT;
6227 chk->snd_count = 0;
6228 chk->flags = 0;
6229 chk->asoc = &stcb->asoc;
6230 chk->data = m_shutdown_ack;
6231 chk->whoTo = net;
6232 net->ref_count++;
6233
6234 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
6235 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
6236 ack_cp->ch.chunk_flags = 0;
6237 ack_cp->ch.chunk_length = htons(chk->send_size);
6238 m_shutdown_ack->m_pkthdr.len = m_shutdown_ack->m_len = chk->send_size;
6239 m_shutdown_ack->m_pkthdr.rcvif = 0;
6240 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6241 chk->asoc->ctrl_queue_cnt++;
6242 return (0);
6243 }
6244
6245 int
6246 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
6247 {
6248 /* formulate and queue a SHUTDOWN to the sender */
6249 struct mbuf *m_shutdown;
6250 struct sctp_shutdown_chunk *shutdown_cp;
6251 struct sctp_tmit_chunk *chk;
6252
6253 m_shutdown = NULL;
6254 MGETHDR(m_shutdown, M_DONTWAIT, MT_HEADER);
6255 if (m_shutdown == NULL) {
6256 /* no mbuf's */
6257 return (-1);
6258 }
6259 m_shutdown->m_data += SCTP_MIN_OVERHEAD;
6260 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6261 if (chk == NULL) {
6262 /* no memory */
6263 sctp_m_freem(m_shutdown);
6264 return (-1);
6265 }
6266 sctppcbinfo.ipi_count_chunk++;
6267 sctppcbinfo.ipi_gencnt_chunk++;
6268
6269 chk->send_size = sizeof(struct sctp_shutdown_chunk);
6270 chk->rec.chunk_id = SCTP_SHUTDOWN;
6271 chk->sent = SCTP_DATAGRAM_UNSENT;
6272 chk->snd_count = 0;
6273 chk->flags = 0;
6274 chk->asoc = &stcb->asoc;
6275 chk->data = m_shutdown;
6276 chk->whoTo = net;
6277 net->ref_count++;
6278
6279 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
6280 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
6281 shutdown_cp->ch.chunk_flags = 0;
6282 shutdown_cp->ch.chunk_length = htons(chk->send_size);
6283 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
6284 m_shutdown->m_pkthdr.len = m_shutdown->m_len = chk->send_size;
6285 m_shutdown->m_pkthdr.rcvif = 0;
6286 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6287 chk->asoc->ctrl_queue_cnt++;
6288
6289 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6290 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
6291 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
6292 soisdisconnecting(stcb->sctp_ep->sctp_socket);
6293 }
6294 return (0);
6295 }
6296
6297 int
6298 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net)
6299 {
6300 /*
6301 * formulate and queue an ASCONF to the peer
6302 * ASCONF parameters should be queued on the assoc queue
6303 */
6304 struct sctp_tmit_chunk *chk;
6305 struct mbuf *m_asconf;
6306
6307 /* compose an ASCONF chunk, maximum length is PMTU */
6308 m_asconf = sctp_compose_asconf(stcb);
6309 if (m_asconf == NULL) {
6310 return (-1);
6311 }
6312 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6313 if (chk == NULL) {
6314 /* no memory */
6315 sctp_m_freem(m_asconf);
6316 return (-1);
6317 }
6318 sctppcbinfo.ipi_count_chunk++;
6319 sctppcbinfo.ipi_gencnt_chunk++;
6320
6321 chk->data = m_asconf;
6322 chk->send_size = m_asconf->m_pkthdr.len;
6323 chk->rec.chunk_id = SCTP_ASCONF;
6324 chk->sent = SCTP_DATAGRAM_UNSENT;
6325 chk->snd_count = 0;
6326 chk->flags = 0;
6327 chk->asoc = &stcb->asoc;
6328 chk->whoTo = chk->asoc->primary_destination;
6329 chk->whoTo->ref_count++;
6330 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6331 chk->asoc->ctrl_queue_cnt++;
6332 return (0);
6333 }
6334
6335 int
6336 sctp_send_asconf_ack(struct sctp_tcb *stcb, uint32_t retrans)
6337 {
6338 /*
6339 * formulate and queue a asconf-ack back to sender
6340 * the asconf-ack must be stored in the tcb
6341 */
6342 struct sctp_tmit_chunk *chk;
6343 struct mbuf *m_ack;
6344
6345 /* is there a asconf-ack mbuf chain to send? */
6346 if (stcb->asoc.last_asconf_ack_sent == NULL) {
6347 return (-1);
6348 }
6349
6350 /* copy the asconf_ack */
6351 #if defined(__FreeBSD__) || defined(__NetBSD__)
6352 /* Supposedly the m_copypacket is a optimzation,
6353 * use it if we can.
6354 */
6355 if (stcb->asoc.last_asconf_ack_sent->m_flags & M_PKTHDR) {
6356 m_ack = m_copypacket(stcb->asoc.last_asconf_ack_sent, M_DONTWAIT);
6357 sctp_pegs[SCTP_CACHED_SRC]++;
6358 } else
6359 m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6360 #else
6361 m_ack = m_copy(stcb->asoc.last_asconf_ack_sent, 0, M_COPYALL);
6362 #endif
6363 if (m_ack == NULL) {
6364 /* couldn't copy it */
6365
6366 return (-1);
6367 }
6368 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
6369 if (chk == NULL) {
6370 /* no memory */
6371 if (m_ack)
6372 sctp_m_freem(m_ack);
6373 return (-1);
6374 }
6375 sctppcbinfo.ipi_count_chunk++;
6376 sctppcbinfo.ipi_gencnt_chunk++;
6377
6378 /* figure out where it goes to */
6379 if (retrans) {
6380 /* we're doing a retransmission */
6381 if (stcb->asoc.used_alt_asconfack > 2) {
6382 /* tried alternate nets already, go back */
6383 chk->whoTo = NULL;
6384 } else {
6385 /* need to try and alternate net */
6386 chk->whoTo = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from);
6387 stcb->asoc.used_alt_asconfack++;
6388 }
6389 if (chk->whoTo == NULL) {
6390 /* no alternate */
6391 if (stcb->asoc.last_control_chunk_from == NULL)
6392 chk->whoTo = stcb->asoc.primary_destination;
6393 else
6394 chk->whoTo = stcb->asoc.last_control_chunk_from;
6395 stcb->asoc.used_alt_asconfack = 0;
6396 }
6397 } else {
6398 /* normal case */
6399 if (stcb->asoc.last_control_chunk_from == NULL)
6400 chk->whoTo = stcb->asoc.primary_destination;
6401 else
6402 chk->whoTo = stcb->asoc.last_control_chunk_from;
6403 stcb->asoc.used_alt_asconfack = 0;
6404 }
6405 chk->data = m_ack;
6406 chk->send_size = m_ack->m_pkthdr.len;
6407 chk->rec.chunk_id = SCTP_ASCONF_ACK;
6408 chk->sent = SCTP_DATAGRAM_UNSENT;
6409 chk->snd_count = 0;
6410 chk->flags = 0;
6411 chk->asoc = &stcb->asoc;
6412 chk->whoTo->ref_count++;
6413 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
6414 chk->asoc->ctrl_queue_cnt++;
6415 return (0);
6416 }
6417
6418
6419 static int
6420 sctp_chunk_retransmission(struct sctp_inpcb *inp,
6421 struct sctp_tcb *stcb,
6422 struct sctp_association *asoc,
6423 int *cnt_out, struct timeval *now, int *now_filled)
6424 {
6425 /*
6426 * send out one MTU of retransmission.
6427 * If fast_retransmit is happening we ignore the cwnd.
6428 * Otherwise we obey the cwnd and rwnd.
6429 * For a Cookie or Asconf in the control chunk queue we retransmit
6430 * them by themselves.
6431 *
6432 * For data chunks we will pick out the lowest TSN's in the
6433 * sent_queue marked for resend and bundle them all together
6434 * (up to a MTU of destination). The address to send to should
6435 * have been selected/changed where the retransmission was
6436 * marked (i.e. in FR or t3-timeout routines).
6437 */
6438 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
6439 struct sctp_tmit_chunk *chk, *fwd;
6440 struct mbuf *m;
6441 struct sctphdr *shdr;
6442 int asconf;
6443 struct sctp_nets *net;
6444 int no_fragmentflg, bundle_at, cnt_thru;
6445 unsigned int mtu;
6446 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
6447
6448 tmr_started = ctl_cnt = bundle_at = error = 0;
6449 no_fragmentflg = 1;
6450 asconf = 0;
6451 fwd_tsn = 0;
6452 *cnt_out = 0;
6453 fwd = NULL;
6454 m = NULL;
6455 #ifdef SCTP_AUDITING_ENABLED
6456 sctp_audit_log(0xC3, 1);
6457 #endif
6458 if (TAILQ_EMPTY(&asoc->sent_queue)) {
6459 #ifdef SCTP_DEBUG
6460 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6461 printf("SCTP hits empty queue with cnt set to %d?\n",
6462 asoc->sent_queue_retran_cnt);
6463 }
6464 #endif
6465 asoc->sent_queue_cnt = 0;
6466 asoc->sent_queue_cnt_removeable = 0;
6467 }
6468 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
6469 if (chk->sent != SCTP_DATAGRAM_RESEND) {
6470 /* we only worry about things marked for resend */
6471 continue;
6472 }
6473 if ((chk->rec.chunk_id == SCTP_COOKIE_ECHO) ||
6474 (chk->rec.chunk_id == SCTP_ASCONF) ||
6475 (chk->rec.chunk_id == SCTP_STREAM_RESET) ||
6476 (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN)) {
6477 if (chk->rec.chunk_id == SCTP_STREAM_RESET) {
6478 /* For stream reset we only retran the request
6479 * not the response.
6480 */
6481 struct sctp_stream_reset_req *strreq;
6482 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
6483 if (strreq->sr_req.ph.param_type != ntohs(SCTP_STR_RESET_REQUEST)) {
6484 continue;
6485 }
6486 }
6487 ctl_cnt++;
6488 if (chk->rec.chunk_id == SCTP_ASCONF) {
6489 no_fragmentflg = 1;
6490 asconf = 1;
6491 }
6492 if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
6493 fwd_tsn = 1;
6494 fwd = chk;
6495 }
6496 m = sctp_copy_mbufchain(chk->data, m);
6497 break;
6498 }
6499 }
6500 one_chunk = 0;
6501 cnt_thru = 0;
6502 /* do we have control chunks to retransmit? */
6503 if (m != NULL) {
6504 /* Start a timer no matter if we suceed or fail */
6505 if (chk->rec.chunk_id == SCTP_COOKIE_ECHO) {
6506 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
6507 } else if (chk->rec.chunk_id == SCTP_ASCONF)
6508 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
6509
6510 if (m->m_len == 0) {
6511 /* Special case for when you get a 0 len
6512 * mbuf at the head due to the lack
6513 * of a MHDR at the beginning.
6514 */
6515 m->m_len = sizeof(struct sctphdr);
6516 } else {
6517 M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
6518 if (m == NULL) {
6519 return (ENOBUFS);
6520 }
6521 }
6522 shdr = mtod(m, struct sctphdr *);
6523 shdr->src_port = inp->sctp_lport;
6524 shdr->dest_port = stcb->rport;
6525 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6526 shdr->checksum = 0;
6527 chk->snd_count++; /* update our count */
6528
6529 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
6530 rtcache_getdst(&chk->whoTo->ro), m,
6531 no_fragmentflg, 0, NULL, asconf))) {
6532 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6533 return (error);
6534 }
6535 /*
6536 *We don't want to mark the net->sent time here since this
6537 * we use this for HB and retrans cannot measure RTT
6538 */
6539 /* SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time);*/
6540 *cnt_out += 1;
6541 chk->sent = SCTP_DATAGRAM_SENT;
6542 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6543 if (fwd_tsn == 0) {
6544 return (0);
6545 } else {
6546 /* Clean up the fwd-tsn list */
6547 sctp_clean_up_ctl (asoc);
6548 return (0);
6549 }
6550 }
6551 /* Ok, it is just data retransmission we need to do or
6552 * that and a fwd-tsn with it all.
6553 */
6554 if (TAILQ_EMPTY(&asoc->sent_queue)) {
6555 return (-1);
6556 }
6557 #ifdef SCTP_DEBUG
6558 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6559 printf("Normal chunk retransmission cnt:%d\n",
6560 asoc->sent_queue_retran_cnt);
6561 }
6562 #endif
6563 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
6564 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
6565 /* not yet open, resend the cookie and that is it */
6566 return (1);
6567 }
6568
6569
6570 #ifdef SCTP_AUDITING_ENABLED
6571 sctp_auditing(20, inp, stcb, NULL);
6572 #endif
6573 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6574 if (chk->sent != SCTP_DATAGRAM_RESEND) {
6575 /* No, not sent to this net or not ready for rtx */
6576 continue;
6577
6578 }
6579 /* pick up the net */
6580 net = chk->whoTo;
6581 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6582 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
6583 } else {
6584 mtu = net->mtu- SCTP_MIN_V4_OVERHEAD;
6585 }
6586
6587 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
6588 /* No room in peers rwnd */
6589 uint32_t tsn;
6590 tsn = asoc->last_acked_seq + 1;
6591 if (tsn == chk->rec.data.TSN_seq) {
6592 /* we make a special exception for this case.
6593 * The peer has no rwnd but is missing the
6594 * lowest chunk.. which is probably what is
6595 * holding up the rwnd.
6596 */
6597 goto one_chunk_around;
6598 }
6599 #ifdef SCTP_DEBUG
6600 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6601 printf("blocked-peers_rwnd:%d tf:%d\n",
6602 (int)asoc->peers_rwnd,
6603 (int)asoc->total_flight);
6604 }
6605 #endif
6606 sctp_pegs[SCTP_RWND_BLOCKED]++;
6607 return (1);
6608 }
6609 one_chunk_around:
6610 if (asoc->peers_rwnd < mtu) {
6611 one_chunk = 1;
6612 }
6613 #ifdef SCTP_AUDITING_ENABLED
6614 sctp_audit_log(0xC3, 2);
6615 #endif
6616 bundle_at = 0;
6617 m = NULL;
6618 net->fast_retran_ip = 0;
6619 if (chk->rec.data.doing_fast_retransmit == 0) {
6620 /* if no FR in progress skip destination that
6621 * have flight_size > cwnd.
6622 */
6623 if (net->flight_size >= net->cwnd) {
6624 sctp_pegs[SCTP_CWND_BLOCKED]++;
6625 continue;
6626 }
6627 } else {
6628 /* Mark the destination net to have FR recovery
6629 * limits put on it.
6630 */
6631 net->fast_retran_ip = 1;
6632 }
6633
6634 if ((chk->send_size <= mtu) || (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
6635 /* ok we will add this one */
6636 m = sctp_copy_mbufchain(chk->data, m);
6637 if (m == NULL) {
6638 return (ENOMEM);
6639 }
6640 /* upate our MTU size */
6641 /* Do clear IP_DF ? */
6642 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6643 no_fragmentflg = 0;
6644 }
6645 mtu -= chk->send_size;
6646 data_list[bundle_at++] = chk;
6647 if (one_chunk && (asoc->total_flight <= 0)) {
6648 sctp_pegs[SCTP_WINDOW_PROBES]++;
6649 chk->rec.data.state_flags |= SCTP_WINDOW_PROBE;
6650 }
6651 }
6652 if (one_chunk == 0) {
6653 /* now are there anymore forward from chk to pick up?*/
6654 fwd = TAILQ_NEXT(chk, sctp_next);
6655 while (fwd) {
6656 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
6657 /* Nope, not for retran */
6658 fwd = TAILQ_NEXT(fwd, sctp_next);
6659 continue;
6660 }
6661 if (fwd->whoTo != net) {
6662 /* Nope, not the net in question */
6663 fwd = TAILQ_NEXT(fwd, sctp_next);
6664 continue;
6665 }
6666 if (fwd->send_size <= mtu) {
6667 m = sctp_copy_mbufchain(fwd->data, m);
6668 if (m == NULL) {
6669 return (ENOMEM);
6670 }
6671 /* upate our MTU size */
6672 /* Do clear IP_DF ? */
6673 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
6674 no_fragmentflg = 0;
6675 }
6676 mtu -= fwd->send_size;
6677 data_list[bundle_at++] = fwd;
6678 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
6679 break;
6680 }
6681 fwd = TAILQ_NEXT(fwd, sctp_next);
6682 } else {
6683 /* can't fit so we are done */
6684 break;
6685 }
6686 }
6687 }
6688 /* Is there something to send for this destination? */
6689 if (m) {
6690 /* No matter if we fail/or suceed we should
6691 * start a timer. A failure is like a lost
6692 * IP packet :-)
6693 */
6694 if (!callout_pending(&net->rxt_timer.timer)) {
6695 /* no timer running on this destination
6696 * restart it.
6697 */
6698 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6699 tmr_started = 1;
6700 }
6701 if (m->m_len == 0) {
6702 /* Special case for when you get a 0 len
6703 * mbuf at the head due to the lack
6704 * of a MHDR at the beginning.
6705 */
6706 m->m_len = sizeof(struct sctphdr);
6707 } else {
6708 M_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
6709 if (m == NULL) {
6710 return (ENOBUFS);
6711 }
6712 }
6713 shdr = mtod(m, struct sctphdr *);
6714 shdr->src_port = inp->sctp_lport;
6715 shdr->dest_port = stcb->rport;
6716 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
6717 shdr->checksum = 0;
6718
6719 /* Now lets send it, if there is anything to send :> */
6720 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
6721 rtcache_getdst(&net->ro),
6722 m,
6723 no_fragmentflg, 0, NULL, asconf))) {
6724 /* error, we could not output */
6725 sctp_pegs[SCTP_DATA_OUT_ERR]++;
6726 return (error);
6727 }
6728 /* For HB's */
6729 /*
6730 * We don't want to mark the net->sent time here since
6731 * this we use this for HB and retrans cannot measure
6732 * RTT
6733 */
6734 /* SCTP_GETTIME_TIMEVAL(&net->last_sent_time);*/
6735
6736 /* For auto-close */
6737 cnt_thru++;
6738 if (*now_filled == 0) {
6739 SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
6740 *now = asoc->time_last_sent;
6741 *now_filled = 1;
6742 } else {
6743 asoc->time_last_sent = *now;
6744 }
6745 *cnt_out += bundle_at;
6746 #ifdef SCTP_AUDITING_ENABLED
6747 sctp_audit_log(0xC4, bundle_at);
6748 #endif
6749 for (i = 0; i < bundle_at; i++) {
6750 sctp_pegs[SCTP_RETRANTSN_SENT]++;
6751 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6752 data_list[i]->snd_count++;
6753 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
6754 /* record the time */
6755 data_list[i]->sent_rcv_time = asoc->time_last_sent;
6756 net->flight_size += data_list[i]->book_size;
6757 asoc->total_flight += data_list[i]->book_size;
6758 asoc->total_flight_count++;
6759
6760 #ifdef SCTP_LOG_RWND
6761 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6762 asoc->peers_rwnd , data_list[i]->send_size, sctp_peer_chunk_oh);
6763 #endif
6764 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6765 (u_int32_t)(data_list[i]->send_size + sctp_peer_chunk_oh));
6766 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6767 /* SWS sender side engages */
6768 asoc->peers_rwnd = 0;
6769 }
6770
6771 if ((i == 0) &&
6772 (data_list[i]->rec.data.doing_fast_retransmit)) {
6773 sctp_pegs[SCTP_FAST_RETRAN]++;
6774 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
6775 (tmr_started == 0)) {
6776 /*
6777 * ok we just fast-retrans'd
6778 * the lowest TSN, i.e the
6779 * first on the list. In this
6780 * case we want to give some
6781 * more time to get a SACK
6782 * back without a t3-expiring.
6783 */
6784 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6785 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
6786 }
6787 }
6788 }
6789 #ifdef SCTP_AUDITING_ENABLED
6790 sctp_auditing(21, inp, stcb, NULL);
6791 #endif
6792 } else {
6793 /* None will fit */
6794 return (1);
6795 }
6796 if (asoc->sent_queue_retran_cnt <= 0) {
6797 /* all done we have no more to retran */
6798 asoc->sent_queue_retran_cnt = 0;
6799 break;
6800 }
6801 if (one_chunk) {
6802 /* No more room in rwnd */
6803 return (1);
6804 }
6805 /* stop the for loop here. we sent out a packet */
6806 break;
6807 }
6808 return (0);
6809 }
6810
6811
6812 static int
6813 sctp_timer_validation(struct sctp_inpcb *inp,
6814 struct sctp_tcb *stcb,
6815 struct sctp_association *asoc,
6816 int ret)
6817 {
6818 struct sctp_nets *net;
6819 /* Validate that a timer is running somewhere */
6820 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6821 if (callout_pending(&net->rxt_timer.timer)) {
6822 /* Here is a timer */
6823 return (ret);
6824 }
6825 }
6826 /* Gak, we did not have a timer somewhere */
6827 #ifdef SCTP_DEBUG
6828 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6829 printf("Deadlock avoided starting timer on a dest at retran\n");
6830 }
6831 #endif
6832 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
6833 return (ret);
6834 }
6835
6836 int
6837 sctp_chunk_output(struct sctp_inpcb *inp,
6838 struct sctp_tcb *stcb,
6839 int from_where)
6840 {
6841 /* Ok this is the generic chunk service queue.
6842 * we must do the following:
6843 * - See if there are retransmits pending, if so we
6844 * must do these first and return.
6845 * - Service the stream queue that is next,
6846 * moving any message (note I must get a complete
6847 * message i.e. FIRST/MIDDLE and LAST to the out
6848 * queue in one pass) and assigning TSN's
6849 * - Check to see if the cwnd/rwnd allows any output, if
6850 * so we go ahead and fomulate and send the low level
6851 * chunks. Making sure to combine any control in the
6852 * control chunk queue also.
6853 */
6854 struct sctp_association *asoc;
6855 struct sctp_nets *net;
6856 int error, num_out, tot_out, ret, reason_code, burst_cnt, burst_limit;
6857 struct timeval now;
6858 int now_filled=0;
6859 int cwnd_full=0;
6860 asoc = &stcb->asoc;
6861 tot_out = 0;
6862 num_out = 0;
6863 reason_code = 0;
6864 sctp_pegs[SCTP_CALLS_TO_CO]++;
6865 #ifdef SCTP_DEBUG
6866 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6867 printf("in co - retran count:%d\n", asoc->sent_queue_retran_cnt);
6868 }
6869 #endif
6870 while (asoc->sent_queue_retran_cnt) {
6871 /* Ok, it is retransmission time only, we send out only ONE
6872 * packet with a single call off to the retran code.
6873 */
6874 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled);
6875 if (ret > 0) {
6876 /* Can't send anymore */
6877 #ifdef SCTP_DEBUG
6878 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6879 printf("retransmission ret:%d -- full\n", ret);
6880 }
6881 #endif
6882 /*
6883 * now lets push out control by calling med-level
6884 * output once. this assures that we WILL send HB's
6885 * if queued too.
6886 */
6887 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
6888 &cwnd_full, from_where,
6889 &now, &now_filled);
6890 #ifdef SCTP_DEBUG
6891 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6892 printf("Control send outputs:%d@full\n", num_out);
6893 }
6894 #endif
6895 #ifdef SCTP_AUDITING_ENABLED
6896 sctp_auditing(8, inp, stcb, NULL);
6897 #endif
6898 return (sctp_timer_validation(inp, stcb, asoc, ret));
6899 }
6900 if (ret < 0) {
6901 /*
6902 * The count was off.. retran is not happening so do
6903 * the normal retransmission.
6904 */
6905 #ifdef SCTP_DEBUG
6906 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6907 printf("Done with retrans, none left fill up window\n");
6908 }
6909 #endif
6910 #ifdef SCTP_AUDITING_ENABLED
6911 sctp_auditing(9, inp, stcb, NULL);
6912 #endif
6913 break;
6914 }
6915 if (from_where == 1) {
6916 /* Only one transmission allowed out of a timeout */
6917 #ifdef SCTP_DEBUG
6918 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6919 printf("Only one packet allowed out\n");
6920 }
6921 #endif
6922 #ifdef SCTP_AUDITING_ENABLED
6923 sctp_auditing(10, inp, stcb, NULL);
6924 #endif
6925 /* Push out any control */
6926 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
6927 &now, &now_filled);
6928 return (ret);
6929 }
6930 if ((num_out == 0) && (ret == 0)) {
6931 /* No more retrans to send */
6932 break;
6933 }
6934 }
6935 #ifdef SCTP_AUDITING_ENABLED
6936 sctp_auditing(12, inp, stcb, NULL);
6937 #endif
6938 /* Check for bad destinations, if they exist move chunks around. */
6939 burst_limit = asoc->max_burst;
6940 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6941 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
6942 SCTP_ADDR_NOT_REACHABLE) {
6943 /*
6944 * if possible move things off of this address
6945 * we still may send below due to the dormant state
6946 * but we try to find an alternate address to send
6947 * to and if we have one we move all queued data on
6948 * the out wheel to this alternate address.
6949 */
6950 sctp_move_to_an_alt(stcb, asoc, net);
6951 } else {
6952 /*
6953 if ((asoc->sat_network) || (net->addr_is_local)) {
6954 burst_limit = asoc->max_burst * SCTP_SAT_NETWORK_BURST_INCR;
6955 }
6956 */
6957 #ifdef SCTP_DEBUG
6958 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6959 printf("examined net:%p burst limit:%d\n", net, asoc->max_burst);
6960 }
6961 #endif
6962
6963 #ifdef SCTP_USE_ALLMAN_BURST
6964 if ((net->flight_size+(burst_limit*net->mtu)) < net->cwnd) {
6965 if (net->ssthresh < net->cwnd)
6966 net->ssthresh = net->cwnd;
6967 net->cwnd = (net->flight_size+(burst_limit*net->mtu));
6968 #ifdef SCTP_LOG_MAXBURST
6969 sctp_log_maxburst(net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
6970 #endif
6971 sctp_pegs[SCTP_MAX_BURST_APL]++;
6972 }
6973 net->fast_retran_ip = 0;
6974 #endif
6975 }
6976
6977 }
6978 /* Fill up what we can to the destination */
6979 burst_cnt = 0;
6980 cwnd_full = 0;
6981 do {
6982 #ifdef SCTP_DEBUG
6983 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
6984 printf("Burst count:%d - call m-c-o\n", burst_cnt);
6985 }
6986 #endif
6987 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
6988 &reason_code, 0, &cwnd_full, from_where,
6989 &now, &now_filled);
6990 if (error) {
6991 #ifdef SCTP_DEBUG
6992 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
6993 printf("Error %d was returned from med-c-op\n", error);
6994 }
6995 #endif
6996 #ifdef SCTP_LOG_MAXBURST
6997 sctp_log_maxburst(asoc->primary_destination, error , burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
6998 #endif
6999 break;
7000 }
7001 #ifdef SCTP_DEBUG
7002 if (sctp_debug_on & SCTP_DEBUG_OUTPUT3) {
7003 printf("m-c-o put out %d\n", num_out);
7004 }
7005 #endif
7006 tot_out += num_out;
7007 burst_cnt++;
7008 } while (num_out
7009 #ifndef SCTP_USE_ALLMAN_BURST
7010 && (burst_cnt < burst_limit)
7011 #endif
7012 );
7013 #ifndef SCTP_USE_ALLMAN_BURST
7014 if (burst_cnt >= burst_limit) {
7015 sctp_pegs[SCTP_MAX_BURST_APL]++;
7016 asoc->burst_limit_applied = 1;
7017 #ifdef SCTP_LOG_MAXBURST
7018 sctp_log_maxburst(asoc->primary_destination, 0 , burst_cnt, SCTP_MAX_BURST_APPLIED);
7019 #endif
7020 } else {
7021 asoc->burst_limit_applied = 0;
7022 }
7023 #endif
7024
7025 #ifdef SCTP_DEBUG
7026 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7027 printf("Ok, we have put out %d chunks\n", tot_out);
7028 }
7029 #endif
7030 if (tot_out == 0) {
7031 sctp_pegs[SCTP_CO_NODATASNT]++;
7032 if (asoc->stream_queue_cnt > 0) {
7033 sctp_pegs[SCTP_SOS_NOSNT]++;
7034 } else {
7035 sctp_pegs[SCTP_NOS_NOSNT]++;
7036 }
7037 if (asoc->send_queue_cnt > 0) {
7038 sctp_pegs[SCTP_SOSE_NOSNT]++;
7039 } else {
7040 sctp_pegs[SCTP_NOSE_NOSNT]++;
7041 }
7042 }
7043 /* Now we need to clean up the control chunk chain if
7044 * a ECNE is on it. It must be marked as UNSENT again
7045 * so next call will continue to send it until
7046 * such time that we get a CWR, to remove it.
7047 */
7048 sctp_fix_ecn_echo(asoc);
7049 return (error);
7050 }
7051
7052
7053 int
7054 sctp_output(struct sctp_inpcb *inp, struct mbuf *m,
7055 struct sockaddr *addr, struct mbuf *control, struct lwp *l, int flags)
7056 {
7057 struct sctp_inpcb *t_inp;
7058 struct sctp_tcb *stcb;
7059 struct sctp_nets *net;
7060 struct sctp_association *asoc;
7061 int create_lock_applied = 0;
7062 int queue_only, error = 0;
7063 struct sctp_sndrcvinfo srcv;
7064 int un_sent = 0;
7065 int use_rcvinfo = 0;
7066 t_inp = inp;
7067 /* struct route ro;*/
7068
7069 queue_only = 0;
7070 stcb = NULL;
7071 asoc = NULL;
7072 net = NULL;
7073
7074 #ifdef SCTP_DEBUG
7075 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7076 printf("USR Send BEGINS\n");
7077 }
7078 #endif
7079
7080 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
7081 (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
7082 /* The listner can NOT send */
7083 if (control) {
7084 sctppcbinfo.mbuf_track--;
7085 sctp_m_freem(control);
7086 control = NULL;
7087 }
7088 sctp_m_freem(m);
7089 return (EFAULT);
7090 }
7091 /* Can't allow a V6 address on a non-v6 socket */
7092 if (addr) {
7093 SCTP_ASOC_CREATE_LOCK(inp);
7094 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
7095 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
7096 /* Should I really unlock ? */
7097 SCTP_ASOC_CREATE_UNLOCK(inp);
7098 if (control) {
7099 sctppcbinfo.mbuf_track--;
7100 sctp_m_freem(control);
7101 control = NULL;
7102 }
7103 sctp_m_freem(m);
7104 return (EFAULT);
7105 }
7106 create_lock_applied = 1;
7107 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
7108 (addr->sa_family == AF_INET6)) {
7109 SCTP_ASOC_CREATE_UNLOCK(inp);
7110 if (control) {
7111 sctppcbinfo.mbuf_track--;
7112 sctp_m_freem(control);
7113 control = NULL;
7114 }
7115 sctp_m_freem(m);
7116 return (EINVAL);
7117 }
7118 }
7119 if (control) {
7120 sctppcbinfo.mbuf_track++;
7121 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
7122 sizeof(srcv))) {
7123 if (srcv.sinfo_flags & MSG_SENDALL) {
7124 /* its a sendall */
7125 sctppcbinfo.mbuf_track--;
7126 sctp_m_freem(control);
7127 if (create_lock_applied) {
7128 SCTP_ASOC_CREATE_UNLOCK(inp);
7129 create_lock_applied = 0;
7130 }
7131 return (sctp_sendall(inp, NULL, m, &srcv));
7132 }
7133 if (srcv.sinfo_assoc_id) {
7134 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7135 SCTP_INP_RLOCK(inp);
7136 stcb = LIST_FIRST(&inp->sctp_asoc_list);
7137 if (stcb) {
7138 SCTP_TCB_LOCK(stcb);
7139 }
7140 SCTP_INP_RUNLOCK(inp);
7141
7142 if (stcb == NULL) {
7143 if (create_lock_applied) {
7144 SCTP_ASOC_CREATE_UNLOCK(inp);
7145 create_lock_applied = 0;
7146 }
7147 sctppcbinfo.mbuf_track--;
7148 sctp_m_freem(control);
7149 sctp_m_freem(m);
7150 return (ENOTCONN);
7151 }
7152 net = stcb->asoc.primary_destination;
7153 } else {
7154 stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
7155 }
7156 /*
7157 * Question: Should I error here if the
7158
7159 * assoc_id is no longer valid?
7160 * i.e. I can't find it?
7161 */
7162 if ((stcb) &&
7163 (addr != NULL)) {
7164 /* Must locate the net structure */
7165 if (addr)
7166 net = sctp_findnet(stcb, addr);
7167 }
7168 if (net == NULL)
7169 net = stcb->asoc.primary_destination;
7170 }
7171 use_rcvinfo = 1;
7172 }
7173 }
7174 if (stcb == NULL) {
7175 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
7176 SCTP_INP_RLOCK(inp);
7177 stcb = LIST_FIRST(&inp->sctp_asoc_list);
7178 if (stcb) {
7179 SCTP_TCB_LOCK(stcb);
7180 }
7181 SCTP_INP_RUNLOCK(inp);
7182 if (stcb == NULL) {
7183 if (create_lock_applied) {
7184 SCTP_ASOC_CREATE_UNLOCK(inp);
7185 create_lock_applied = 0;
7186 }
7187 if (control) {
7188 sctppcbinfo.mbuf_track--;
7189 sctp_m_freem(control);
7190 control = NULL;
7191 }
7192 sctp_m_freem(m);
7193 return (ENOTCONN);
7194 }
7195 if (addr == NULL) {
7196 net = stcb->asoc.primary_destination;
7197 } else {
7198 net = sctp_findnet(stcb, addr);
7199 if (net == NULL) {
7200 net = stcb->asoc.primary_destination;
7201 }
7202 }
7203 } else {
7204 if (addr != NULL) {
7205 SCTP_INP_WLOCK(inp);
7206 SCTP_INP_INCR_REF(inp);
7207 SCTP_INP_WUNLOCK(inp);
7208 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
7209 if (stcb == NULL) {
7210 SCTP_INP_WLOCK(inp);
7211 SCTP_INP_DECR_REF(inp);
7212 SCTP_INP_WUNLOCK(inp);
7213 }
7214 }
7215 }
7216 }
7217 if ((stcb == NULL) &&
7218 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
7219 if (control) {
7220 sctppcbinfo.mbuf_track--;
7221 sctp_m_freem(control);
7222 control = NULL;
7223 }
7224 if (create_lock_applied) {
7225 SCTP_ASOC_CREATE_UNLOCK(inp);
7226 create_lock_applied = 0;
7227 }
7228 sctp_m_freem(m);
7229 return (ENOTCONN);
7230 } else if ((stcb == NULL) &&
7231 (addr == NULL)) {
7232 if (control) {
7233 sctppcbinfo.mbuf_track--;
7234 sctp_m_freem(control);
7235 control = NULL;
7236 }
7237 if (create_lock_applied) {
7238 SCTP_ASOC_CREATE_UNLOCK(inp);
7239 create_lock_applied = 0;
7240 }
7241 sctp_m_freem(m);
7242 return (ENOENT);
7243 } else if (stcb == NULL) {
7244 /* UDP mode, we must go ahead and start the INIT process */
7245 if ((use_rcvinfo) && (srcv.sinfo_flags & MSG_ABORT)) {
7246 /* Strange user to do this */
7247 if (control) {
7248 sctppcbinfo.mbuf_track--;
7249 sctp_m_freem(control);
7250 control = NULL;
7251 }
7252 if (create_lock_applied) {
7253 SCTP_ASOC_CREATE_UNLOCK(inp);
7254 create_lock_applied = 0;
7255 }
7256 sctp_m_freem(m);
7257 return (ENOENT);
7258 }
7259 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
7260 if (stcb == NULL) {
7261 if (control) {
7262 sctppcbinfo.mbuf_track--;
7263 sctp_m_freem(control);
7264 control = NULL;
7265 }
7266 if (create_lock_applied) {
7267 SCTP_ASOC_CREATE_UNLOCK(inp);
7268 create_lock_applied = 0;
7269 }
7270 sctp_m_freem(m);
7271 return (error);
7272 }
7273 if (create_lock_applied) {
7274 SCTP_ASOC_CREATE_UNLOCK(inp);
7275 create_lock_applied = 0;
7276 } else {
7277 printf("Huh-1, create lock should have been applied!\n");
7278 }
7279 queue_only = 1;
7280 asoc = &stcb->asoc;
7281 asoc->state = SCTP_STATE_COOKIE_WAIT;
7282 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
7283 if (control) {
7284 /* see if a init structure exists in cmsg headers */
7285 struct sctp_initmsg initm;
7286 int i;
7287 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
7288 sizeof(initm))) {
7289 /* we have an INIT override of the default */
7290 if (initm.sinit_max_attempts)
7291 asoc->max_init_times = initm.sinit_max_attempts;
7292 if (initm.sinit_num_ostreams)
7293 asoc->pre_open_streams = initm.sinit_num_ostreams;
7294 if (initm.sinit_max_instreams)
7295 asoc->max_inbound_streams = initm.sinit_max_instreams;
7296 if (initm.sinit_max_init_timeo)
7297 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
7298 }
7299 if (asoc->streamoutcnt < asoc->pre_open_streams) {
7300 /* Default is NOT correct */
7301 #ifdef SCTP_DEBUG
7302 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7303 printf("Ok, defout:%d pre_open:%d\n",
7304 asoc->streamoutcnt, asoc->pre_open_streams);
7305 }
7306 #endif
7307 free(asoc->strmout, M_PCB);
7308 asoc->strmout = NULL;
7309 asoc->streamoutcnt = asoc->pre_open_streams;
7310 asoc->strmout = malloc(asoc->streamoutcnt *
7311 sizeof(struct sctp_stream_out), M_PCB,
7312 M_WAIT);
7313 for (i = 0; i < asoc->streamoutcnt; i++) {
7314 /*
7315 * inbound side must be set to 0xffff,
7316 * also NOTE when we get the INIT-ACK
7317 * back (for INIT sender) we MUST
7318 * reduce the count (streamoutcnt) but
7319 * first check if we sent to any of the
7320 * upper streams that were dropped (if
7321 * some were). Those that were dropped
7322 * must be notified to the upper layer
7323 * as failed to send.
7324 */
7325 asoc->strmout[i].next_sequence_sent = 0x0;
7326 TAILQ_INIT(&asoc->strmout[i].outqueue);
7327 asoc->strmout[i].stream_no = i;
7328 asoc->strmout[i].next_spoke.tqe_next = 0;
7329 asoc->strmout[i].next_spoke.tqe_prev = 0;
7330 }
7331 }
7332 }
7333 sctp_send_initiate(inp, stcb);
7334 /*
7335 * we may want to dig in after this call and adjust the MTU
7336 * value. It defaulted to 1500 (constant) but the ro structure
7337 * may now have an update and thus we may need to change it
7338 * BEFORE we append the message.
7339 */
7340 net = stcb->asoc.primary_destination;
7341 } else {
7342 if (create_lock_applied) {
7343 SCTP_ASOC_CREATE_UNLOCK(inp);
7344 create_lock_applied = 0;
7345 }
7346 asoc = &stcb->asoc;
7347 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
7348 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
7349 queue_only = 1;
7350 }
7351 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
7352 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7353 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
7354 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
7355 if (control) {
7356 sctppcbinfo.mbuf_track--;
7357 sctp_m_freem(control);
7358 control = NULL;
7359 }
7360 if ((use_rcvinfo) &&
7361 (srcv.sinfo_flags & MSG_ABORT)) {
7362 sctp_msg_append(stcb, net, m, &srcv, flags);
7363 error = 0;
7364 } else {
7365 if (m)
7366 sctp_m_freem(m);
7367 error = ECONNRESET;
7368 }
7369 SCTP_TCB_UNLOCK(stcb);
7370 return (error);
7371 }
7372 }
7373 if (create_lock_applied) {
7374 /* we should never hit here with the create lock applied
7375 *
7376 */
7377 SCTP_ASOC_CREATE_UNLOCK(inp);
7378 create_lock_applied = 0;
7379 }
7380
7381
7382 if (use_rcvinfo == 0) {
7383 srcv = stcb->asoc.def_send;
7384 }
7385 #ifdef SCTP_DEBUG
7386 else {
7387 if (sctp_debug_on & SCTP_DEBUG_OUTPUT5) {
7388 printf("stream:%d\n", srcv.sinfo_stream);
7389 printf("flags:%x\n", (u_int)srcv.sinfo_flags);
7390 printf("ppid:%d\n", srcv.sinfo_ppid);
7391 printf("context:%d\n", srcv.sinfo_context);
7392 }
7393 }
7394 #endif
7395 if (control) {
7396 sctppcbinfo.mbuf_track--;
7397 sctp_m_freem(control);
7398 control = NULL;
7399 }
7400 if (net && ((srcv.sinfo_flags & MSG_ADDR_OVER))) {
7401 /* we take the override or the unconfirmed */
7402 ;
7403 } else {
7404 net = stcb->asoc.primary_destination;
7405 }
7406 if ((error = sctp_msg_append(stcb, net, m, &srcv, flags))) {
7407 SCTP_TCB_UNLOCK(stcb);
7408 return (error);
7409 }
7410 if (net->flight_size > net->cwnd) {
7411 sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
7412 queue_only = 1;
7413 } else if (asoc->ifp_had_enobuf) {
7414 sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
7415 queue_only = 1;
7416 } else {
7417 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
7418 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
7419 SCTP_MED_OVERHEAD);
7420
7421 if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
7422 (stcb->asoc.total_flight > 0) &&
7423 (un_sent < (int)stcb->asoc.smallest_mtu)
7424 ) {
7425
7426 /* Ok, Nagle is set on and we have
7427 * data outstanding. Don't send anything
7428 * and let the SACK drive out the data.
7429 */
7430 sctp_pegs[SCTP_NAGLE_NOQ]++;
7431 queue_only = 1;
7432 } else {
7433 sctp_pegs[SCTP_NAGLE_OFF]++;
7434 }
7435 }
7436 if ((queue_only == 0) && stcb->asoc.peers_rwnd) {
7437 /* we can attempt to send too.*/
7438 #ifdef SCTP_DEBUG
7439 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7440 printf("USR Send calls sctp_chunk_output\n");
7441 }
7442 #endif
7443 #ifdef SCTP_AUDITING_ENABLED
7444 sctp_audit_log(0xC0, 1);
7445 sctp_auditing(6, inp, stcb, net);
7446 #endif
7447 sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
7448 sctp_chunk_output(inp, stcb, 0);
7449 #ifdef SCTP_AUDITING_ENABLED
7450 sctp_audit_log(0xC0, 2);
7451 sctp_auditing(7, inp, stcb, net);
7452 #endif
7453
7454 }
7455 #ifdef SCTP_DEBUG
7456 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
7457 printf("USR Send complete qo:%d prw:%d\n", queue_only, stcb->asoc.peers_rwnd);
7458 }
7459 #endif
7460 SCTP_TCB_UNLOCK(stcb);
7461 return (0);
7462 }
7463
7464 void
7465 send_forward_tsn(struct sctp_tcb *stcb,
7466 struct sctp_association *asoc)
7467 {
7468 struct sctp_tmit_chunk *chk;
7469 struct sctp_forward_tsn_chunk *fwdtsn;
7470
7471 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7472 if (chk->rec.chunk_id == SCTP_FORWARD_CUM_TSN) {
7473 /* mark it to unsent */
7474 chk->sent = SCTP_DATAGRAM_UNSENT;
7475 chk->snd_count = 0;
7476 /* Do we correct its output location? */
7477 if (chk->whoTo != asoc->primary_destination) {
7478 sctp_free_remote_addr(chk->whoTo);
7479 chk->whoTo = asoc->primary_destination;
7480 chk->whoTo->ref_count++;
7481 }
7482 goto sctp_fill_in_rest;
7483 }
7484 }
7485 /* Ok if we reach here we must build one */
7486 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
7487 if (chk == NULL) {
7488 return;
7489 }
7490 sctppcbinfo.ipi_count_chunk++;
7491 sctppcbinfo.ipi_gencnt_chunk++;
7492 chk->rec.chunk_id = SCTP_FORWARD_CUM_TSN;
7493 chk->asoc = asoc;
7494 MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
7495 if (chk->data == NULL) {
7496 chk->whoTo->ref_count--;
7497 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
7498 sctppcbinfo.ipi_count_chunk--;
7499 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7500 panic("Chunk count is negative");
7501 }
7502 sctppcbinfo.ipi_gencnt_chunk++;
7503 return;
7504 }
7505 chk->data->m_data += SCTP_MIN_OVERHEAD;
7506 chk->sent = SCTP_DATAGRAM_UNSENT;
7507 chk->snd_count = 0;
7508 chk->whoTo = asoc->primary_destination;
7509 chk->whoTo->ref_count++;
7510 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
7511 asoc->ctrl_queue_cnt++;
7512 sctp_fill_in_rest:
7513 /* Here we go through and fill out the part that
7514 * deals with stream/seq of the ones we skip.
7515 */
7516 chk->data->m_pkthdr.len = chk->data->m_len = 0;
7517 {
7518 struct sctp_tmit_chunk *at, *tp1, *last;
7519 struct sctp_strseq *strseq;
7520 unsigned int cnt_of_space, i, ovh;
7521 unsigned int space_needed;
7522 unsigned int cnt_of_skipped = 0;
7523 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
7524 if (at->sent != SCTP_FORWARD_TSN_SKIP) {
7525 /* no more to look at */
7526 break;
7527 }
7528 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7529 /* We don't report these */
7530 continue;
7531 }
7532 cnt_of_skipped++;
7533 }
7534 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
7535 (cnt_of_skipped * sizeof(struct sctp_strseq)));
7536 if ((M_TRAILINGSPACE(chk->data) < (int)space_needed) &&
7537 ((chk->data->m_flags & M_EXT) == 0)) {
7538 /* Need a M_EXT, get one and move
7539 * fwdtsn to data area.
7540 */
7541 MCLGET(chk->data, M_DONTWAIT);
7542 }
7543 cnt_of_space = M_TRAILINGSPACE(chk->data);
7544
7545 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7546 ovh = SCTP_MIN_OVERHEAD;
7547 } else {
7548 ovh = SCTP_MIN_V4_OVERHEAD;
7549 }
7550 if (cnt_of_space > (asoc->smallest_mtu-ovh)) {
7551 /* trim to a mtu size */
7552 cnt_of_space = asoc->smallest_mtu - ovh;
7553 }
7554 if (cnt_of_space < space_needed) {
7555 /* ok we must trim down the chunk by lowering
7556 * the advance peer ack point.
7557 */
7558 cnt_of_skipped = (cnt_of_space-
7559 ((sizeof(struct sctp_forward_tsn_chunk))/
7560 sizeof(struct sctp_strseq)));
7561 /* Go through and find the TSN that
7562 * will be the one we report.
7563 */
7564 at = TAILQ_FIRST(&asoc->sent_queue);
7565 for (i = 0; i < cnt_of_skipped; i++) {
7566 tp1 = TAILQ_NEXT(at, sctp_next);
7567 at = tp1;
7568 }
7569 last = at;
7570 /* last now points to last one I can report, update peer ack point */
7571 asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
7572 space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
7573 }
7574 chk->send_size = space_needed;
7575 /* Setup the chunk */
7576 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
7577 fwdtsn->ch.chunk_length = htons(chk->send_size);
7578 fwdtsn->ch.chunk_flags = 0;
7579 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
7580 fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
7581 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
7582 (cnt_of_skipped * sizeof(struct sctp_strseq)));
7583 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
7584 fwdtsn++;
7585 /* Move pointer to after the fwdtsn and transfer to
7586 * the strseq pointer.
7587 */
7588 strseq = (struct sctp_strseq *)fwdtsn;
7589 /*
7590 * Now populate the strseq list. This is done blindly
7591 * without pulling out duplicate stream info. This is
7592 * inefficent but won't harm the process since the peer
7593 * will look at these in sequence and will thus release
7594 * anything. It could mean we exceed the PMTU and chop
7595 * off some that we could have included.. but this is
7596 * unlikely (aka 1432/4 would mean 300+ stream seq's would
7597 * have to be reported in one FWD-TSN. With a bit of work
7598 * we can later FIX this to optimize and pull out duplcates..
7599 * but it does add more overhead. So for now... not!
7600 */
7601 at = TAILQ_FIRST(&asoc->sent_queue);
7602 for (i = 0; i < cnt_of_skipped; i++) {
7603 tp1 = TAILQ_NEXT(at, sctp_next);
7604 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
7605 /* We don't report these */
7606 i--;
7607 at = tp1;
7608 continue;
7609 }
7610 strseq->stream = ntohs(at->rec.data.stream_number);
7611 strseq->sequence = ntohs(at->rec.data.stream_seq);
7612 strseq++;
7613 at = tp1;
7614 }
7615 }
7616 return;
7617
7618 }
7619
7620 void
7621 sctp_send_sack(struct sctp_tcb *stcb)
7622 {
7623 /*
7624 * Queue up a SACK in the control queue. We must first check to
7625 * see if a SACK is somehow on the control queue. If so, we will
7626 * take and and remove the old one.
7627 */
7628 struct sctp_association *asoc;
7629 struct sctp_tmit_chunk *chk, *a_chk;
7630 struct sctp_sack_chunk *sack;
7631 struct sctp_gap_ack_block *gap_descriptor;
7632 uint32_t *dup;
7633 int start;
7634 unsigned int i, maxi, seeing_ones, m_size;
7635 unsigned int num_gap_blocks, space;
7636
7637 start = maxi = 0;
7638 seeing_ones = 1;
7639 a_chk = NULL;
7640 asoc = &stcb->asoc;
7641 if (asoc->last_data_chunk_from == NULL) {
7642 /* Hmm we never received anything */
7643 return;
7644 }
7645 sctp_set_rwnd(stcb, asoc);
7646 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7647 if (chk->rec.chunk_id == SCTP_SELECTIVE_ACK) {
7648 /* Hmm, found a sack already on queue, remove it */
7649 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7650 asoc->ctrl_queue_cnt++;
7651 a_chk = chk;
7652 if (a_chk->data)
7653 sctp_m_freem(a_chk->data);
7654 a_chk->data = NULL;
7655 sctp_free_remote_addr(a_chk->whoTo);
7656 a_chk->whoTo = NULL;
7657 break;
7658 }
7659 }
7660 if (a_chk == NULL) {
7661 a_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
7662 if (a_chk == NULL) {
7663 /* No memory so we drop the idea, and set a timer */
7664 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7665 stcb->sctp_ep, stcb, NULL);
7666 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7667 stcb->sctp_ep, stcb, NULL);
7668 return;
7669 }
7670 sctppcbinfo.ipi_count_chunk++;
7671 sctppcbinfo.ipi_gencnt_chunk++;
7672 a_chk->rec.chunk_id = SCTP_SELECTIVE_ACK;
7673 }
7674 a_chk->asoc = asoc;
7675 a_chk->snd_count = 0;
7676 a_chk->send_size = 0; /* fill in later */
7677 a_chk->sent = SCTP_DATAGRAM_UNSENT;
7678 m_size = (asoc->mapping_array_size << 3);
7679
7680 if ((asoc->numduptsns) ||
7681 (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
7682 ) {
7683 /* Ok, we have some duplicates or the destination for the
7684 * sack is unreachable, lets see if we can select an alternate
7685 * than asoc->last_data_chunk_from
7686 */
7687 if ((!(asoc->last_data_chunk_from->dest_state &
7688 SCTP_ADDR_NOT_REACHABLE)) &&
7689 (asoc->used_alt_onsack > 2)) {
7690 /* We used an alt last time, don't this time */
7691 a_chk->whoTo = NULL;
7692 } else {
7693 asoc->used_alt_onsack++;
7694 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from);
7695 }
7696 if (a_chk->whoTo == NULL) {
7697 /* Nope, no alternate */
7698 a_chk->whoTo = asoc->last_data_chunk_from;
7699 asoc->used_alt_onsack = 0;
7700 }
7701 } else {
7702 /* No duplicates so we use the last
7703 * place we received data from.
7704 */
7705 #ifdef SCTP_DEBUG
7706 if (asoc->last_data_chunk_from == NULL) {
7707 printf("Huh, last_data_chunk_from is null when we want to sack??\n");
7708 }
7709 #endif
7710 asoc->used_alt_onsack = 0;
7711 a_chk->whoTo = asoc->last_data_chunk_from;
7712 }
7713 if (a_chk->whoTo)
7714 a_chk->whoTo->ref_count++;
7715
7716 /* Ok now lets formulate a MBUF with our sack */
7717 MGETHDR(a_chk->data, M_DONTWAIT, MT_DATA);
7718 if ((a_chk->data == NULL) ||
7719 (a_chk->whoTo == NULL)) {
7720 /* rats, no mbuf memory */
7721 if (a_chk->data) {
7722 /* was a problem with the destination */
7723 sctp_m_freem(a_chk->data);
7724 a_chk->data = NULL;
7725 }
7726 a_chk->whoTo->ref_count--;
7727 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
7728 sctppcbinfo.ipi_count_chunk--;
7729 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7730 panic("Chunk count is negative");
7731 }
7732 sctppcbinfo.ipi_gencnt_chunk++;
7733 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7734 stcb->sctp_ep, stcb, NULL);
7735 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7736 stcb->sctp_ep, stcb, NULL);
7737 return;
7738 }
7739 /* First count the number of gap ack blocks we need */
7740 if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
7741 /* We know if there are none above the cum-ack we
7742 * have everything with NO gaps
7743 */
7744 num_gap_blocks = 0;
7745 } else {
7746 /* Ok we must count how many gaps we
7747 * have.
7748 */
7749 num_gap_blocks = 0;
7750 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
7751 maxi = (asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn);
7752 } else {
7753 maxi = (asoc->highest_tsn_inside_map + (MAX_TSN - asoc->mapping_array_base_tsn) + 1);
7754 }
7755 if (maxi > m_size) {
7756 /* impossible but who knows, someone is playing with us :> */
7757 #ifdef SCTP_DEBUG
7758 printf("GAK maxi:%d > m_size:%d came out higher than allowed htsn:%u base:%u cumack:%u\n",
7759 maxi,
7760 m_size,
7761 asoc->highest_tsn_inside_map,
7762 asoc->mapping_array_base_tsn,
7763 asoc->cumulative_tsn
7764 );
7765 #endif
7766 num_gap_blocks = 0;
7767 goto no_gaps_now;
7768 }
7769 if (asoc->cumulative_tsn >= asoc->mapping_array_base_tsn) {
7770 start = (asoc->cumulative_tsn - asoc->mapping_array_base_tsn);
7771 } else {
7772 /* Set it so we start at 0 */
7773 start = -1;
7774 }
7775 /* Ok move start up one to look at the NEXT past the cum-ack */
7776 start++;
7777 for (i = start; i <= maxi; i++) {
7778 if (seeing_ones) {
7779 /* while seeing ones I must
7780 * transition back to 0 before
7781 * finding the next gap and
7782 * counting the segment.
7783 */
7784 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
7785 seeing_ones = 0;
7786 }
7787 } else {
7788 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
7789 seeing_ones = 1;
7790 num_gap_blocks++;
7791 }
7792 }
7793 }
7794 no_gaps_now:
7795 if (num_gap_blocks == 0) {
7796 /*
7797 * Traveled all of the bits and NO one,
7798 * must have reneged
7799 */
7800 if (compare_with_wrap(asoc->cumulative_tsn, asoc->highest_tsn_inside_map, MAX_TSN)) {
7801 asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
7802 #ifdef SCTP_MAP_LOGGING
7803 sctp_log_map(0, 4, asoc->highest_tsn_inside_map, SCTP_MAP_SLIDE_RESULT);
7804 #endif
7805 }
7806 }
7807 }
7808
7809 /* Now calculate the space needed */
7810 space = (sizeof(struct sctp_sack_chunk) +
7811 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7812 (asoc->numduptsns * sizeof(int32_t))
7813 );
7814 if (space > (asoc->smallest_mtu-SCTP_MAX_OVERHEAD)) {
7815 /* Reduce the size of the sack to fit */
7816 int calc, fit;
7817 calc = (asoc->smallest_mtu - SCTP_MAX_OVERHEAD);
7818 calc -= sizeof(struct sctp_gap_ack_block);
7819 fit = calc/sizeof(struct sctp_gap_ack_block);
7820 if (fit > (int)num_gap_blocks) {
7821 /* discard some dups */
7822 asoc->numduptsns = (fit - num_gap_blocks);
7823 } else {
7824 /* discard all dups and some gaps */
7825 num_gap_blocks = fit;
7826 asoc->numduptsns = 0;
7827 }
7828 /* recalc space */
7829 space = (sizeof(struct sctp_sack_chunk) +
7830 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7831 (asoc->numduptsns * sizeof(int32_t))
7832 );
7833
7834 }
7835
7836 if ((space+SCTP_MIN_OVERHEAD) > MHLEN) {
7837 /* We need a cluster */
7838 MCLGET(a_chk->data, M_DONTWAIT);
7839 if ((a_chk->data->m_flags & M_EXT) != M_EXT) {
7840 /* can't get a cluster
7841 * give up and try later.
7842 */
7843 if (a_chk->data)
7844 sctp_m_freem(a_chk->data);
7845 a_chk->data = NULL;
7846 a_chk->whoTo->ref_count--;
7847 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, a_chk);
7848 sctppcbinfo.ipi_count_chunk--;
7849 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
7850 panic("Chunk count is negative");
7851 }
7852 sctppcbinfo.ipi_gencnt_chunk++;
7853 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7854 stcb->sctp_ep, stcb, NULL);
7855 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
7856 stcb->sctp_ep, stcb, NULL);
7857 return;
7858 }
7859 }
7860
7861 /* ok, lets go through and fill it in */
7862 a_chk->data->m_data += SCTP_MIN_OVERHEAD;
7863 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
7864 sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
7865 sack->ch.chunk_flags = asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM;
7866 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
7867 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
7868 asoc->my_last_reported_rwnd = asoc->my_rwnd;
7869 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
7870 sack->sack.num_dup_tsns = htons(asoc->numduptsns);
7871
7872 a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
7873 (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
7874 (asoc->numduptsns * sizeof(int32_t)));
7875 a_chk->data->m_pkthdr.len = a_chk->data->m_len = a_chk->send_size;
7876 sack->ch.chunk_length = htons(a_chk->send_size);
7877
7878 gap_descriptor = (struct sctp_gap_ack_block *)((vaddr_t)sack + sizeof(struct sctp_sack_chunk));
7879 seeing_ones = 0;
7880 for (i = start; i <= maxi; i++) {
7881 if (num_gap_blocks == 0) {
7882 break;
7883 }
7884 if (seeing_ones) {
7885 /* while seeing Ones I must
7886 * transition back to 0 before
7887 * finding the next gap
7888 */
7889 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i) == 0) {
7890 gap_descriptor->end = htons(((uint16_t)(i-start)));
7891 gap_descriptor++;
7892 seeing_ones = 0;
7893 num_gap_blocks--;
7894 }
7895 } else {
7896 if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, i)) {
7897 gap_descriptor->start = htons(((uint16_t)(i+1-start)));
7898 /* advance struct to next pointer */
7899 seeing_ones = 1;
7900 }
7901 }
7902 }
7903 if (num_gap_blocks) {
7904 /* special case where the array is all 1's
7905 * to the end of the array.
7906 */
7907 gap_descriptor->end = htons(((uint16_t)((i-start))));
7908 gap_descriptor++;
7909 }
7910 /* now we must add any dups we are going to report. */
7911 if (asoc->numduptsns) {
7912 dup = (uint32_t *)gap_descriptor;
7913 for (i = 0; i < asoc->numduptsns; i++) {
7914 *dup = htonl(asoc->dup_tsns[i]);
7915 dup++;
7916 }
7917 asoc->numduptsns = 0;
7918 }
7919 /* now that the chunk is prepared queue it to the control
7920 * chunk queue.
7921 */
7922 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
7923 asoc->ctrl_queue_cnt++;
7924 sctp_pegs[SCTP_PEG_SACKS_SENT]++;
7925 return;
7926 }
7927
7928 void
7929 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr)
7930 {
7931 struct mbuf *m_abort;
7932 struct sctp_abort_msg *abort_m;
7933 int sz;
7934 abort_m = NULL;
7935 MGETHDR(m_abort, M_DONTWAIT, MT_HEADER);
7936 if (m_abort == NULL) {
7937 /* no mbuf's */
7938 return;
7939 }
7940 m_abort->m_data += SCTP_MIN_OVERHEAD;
7941 abort_m = mtod(m_abort, struct sctp_abort_msg *);
7942 m_abort->m_len = sizeof(struct sctp_abort_msg);
7943 m_abort->m_next = operr;
7944 sz = 0;
7945 if (operr) {
7946 struct mbuf *n;
7947 n = operr;
7948 while (n) {
7949 sz += n->m_len;
7950 n = n->m_next;
7951 }
7952 }
7953 abort_m->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
7954 abort_m->msg.ch.chunk_flags = 0;
7955 abort_m->msg.ch.chunk_length = htons(sizeof(struct sctp_abort_chunk) +
7956 sz);
7957 abort_m->sh.src_port = stcb->sctp_ep->sctp_lport;
7958 abort_m->sh.dest_port = stcb->rport;
7959 abort_m->sh.v_tag = htonl(stcb->asoc.peer_vtag);
7960 abort_m->sh.checksum = 0;
7961 m_abort->m_pkthdr.len = m_abort->m_len + sz;
7962 m_abort->m_pkthdr.rcvif = 0;
7963 sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
7964 stcb->asoc.primary_destination,
7965 rtcache_getdst(&stcb->asoc.primary_destination->ro),
7966 m_abort, 1, 0, NULL, 0);
7967 }
7968
7969 int
7970 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
7971 struct sctp_nets *net)
7972
7973 {
7974 /* formulate and SEND a SHUTDOWN-COMPLETE */
7975 struct mbuf *m_shutdown_comp;
7976 struct sctp_shutdown_complete_msg *comp_cp;
7977
7978 m_shutdown_comp = NULL;
7979 MGETHDR(m_shutdown_comp, M_DONTWAIT, MT_HEADER);
7980 if (m_shutdown_comp == NULL) {
7981 /* no mbuf's */
7982 return (-1);
7983 }
7984 m_shutdown_comp->m_data += sizeof(struct ip6_hdr);
7985 comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
7986 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
7987 comp_cp->shut_cmp.ch.chunk_flags = 0;
7988 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
7989 comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
7990 comp_cp->sh.dest_port = stcb->rport;
7991 comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
7992 comp_cp->sh.checksum = 0;
7993
7994 m_shutdown_comp->m_pkthdr.len = m_shutdown_comp->m_len = sizeof(struct sctp_shutdown_complete_msg);
7995 m_shutdown_comp->m_pkthdr.rcvif = 0;
7996 sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
7997 rtcache_getdst(&net->ro), m_shutdown_comp,
7998 1, 0, NULL, 0);
7999 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
8000 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
8001 stcb->sctp_ep->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
8002 stcb->sctp_ep->sctp_socket->so_snd.sb_cc = 0;
8003 soisdisconnected(stcb->sctp_ep->sctp_socket);
8004 }
8005 return (0);
8006 }
8007
8008 int
8009 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh)
8010 {
8011 /* formulate and SEND a SHUTDOWN-COMPLETE */
8012 struct mbuf *mout;
8013 struct ip *iph, *iph_out;
8014 struct ip6_hdr *ip6, *ip6_out;
8015 int offset_out;
8016 struct sctp_shutdown_complete_msg *comp_cp;
8017
8018 MGETHDR(mout, M_DONTWAIT, MT_HEADER);
8019 if (mout == NULL) {
8020 /* no mbuf's */
8021 return (-1);
8022 }
8023 iph = mtod(m, struct ip *);
8024 iph_out = NULL;
8025 ip6_out = NULL;
8026 offset_out = 0;
8027 if (iph->ip_v == IPVERSION) {
8028 mout->m_len = sizeof(struct ip) +
8029 sizeof(struct sctp_shutdown_complete_msg);
8030 mout->m_next = NULL;
8031 iph_out = mtod(mout, struct ip *);
8032
8033 /* Fill in the IP header for the ABORT */
8034 iph_out->ip_v = IPVERSION;
8035 iph_out->ip_hl = (sizeof(struct ip)/4);
8036 iph_out->ip_tos = (u_char)0;
8037 iph_out->ip_id = 0;
8038 iph_out->ip_off = 0;
8039 iph_out->ip_ttl = MAXTTL;
8040 iph_out->ip_p = IPPROTO_SCTP;
8041 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
8042 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
8043
8044 /* let IP layer calculate this */
8045 iph_out->ip_sum = 0;
8046 offset_out += sizeof(*iph_out);
8047 comp_cp = (struct sctp_shutdown_complete_msg *)(
8048 (vaddr_t)iph_out + offset_out);
8049 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
8050 ip6 = (struct ip6_hdr *)iph;
8051 mout->m_len = sizeof(struct ip6_hdr) +
8052 sizeof(struct sctp_shutdown_complete_msg);
8053 mout->m_next = NULL;
8054 ip6_out = mtod(mout, struct ip6_hdr *);
8055
8056 /* Fill in the IPv6 header for the ABORT */
8057 ip6_out->ip6_flow = ip6->ip6_flow;
8058 ip6_out->ip6_hlim = ip6_defhlim;
8059 ip6_out->ip6_nxt = IPPROTO_SCTP;
8060 ip6_out->ip6_src = ip6->ip6_dst;
8061 ip6_out->ip6_dst = ip6->ip6_src;
8062 ip6_out->ip6_plen = mout->m_len;
8063 offset_out += sizeof(*ip6_out);
8064 comp_cp = (struct sctp_shutdown_complete_msg *)(
8065 (vaddr_t)ip6_out + offset_out);
8066 } else {
8067 /* Currently not supported. */
8068 return (-1);
8069 }
8070
8071 /* Now copy in and fill in the ABORT tags etc. */
8072 comp_cp->sh.src_port = sh->dest_port;
8073 comp_cp->sh.dest_port = sh->src_port;
8074 comp_cp->sh.checksum = 0;
8075 comp_cp->sh.v_tag = sh->v_tag;
8076 comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
8077 comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
8078 comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
8079
8080 mout->m_pkthdr.len = mout->m_len;
8081 /* add checksum */
8082 if ((sctp_no_csum_on_loopback) &&
8083 (m->m_pkthdr.rcvif) &&
8084 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
8085 comp_cp->sh.checksum = 0;
8086 } else {
8087 comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
8088 }
8089
8090 /* zap the rcvif, it should be null */
8091 mout->m_pkthdr.rcvif = 0;
8092 /* zap the stack pointer to the route */
8093 if (iph_out != NULL) {
8094 struct route ro;
8095
8096 memset(&ro, 0, sizeof ro);
8097 #ifdef SCTP_DEBUG
8098 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8099 printf("sctp_shutdown_complete2 calling ip_output:\n");
8100 sctp_print_address_pkt(iph_out, &comp_cp->sh);
8101 }
8102 #endif
8103 /* set IPv4 length */
8104 iph_out->ip_len = htons(mout->m_pkthdr.len);
8105 /* out it goes */
8106 ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
8107 } else if (ip6_out != NULL) {
8108 struct route ro;
8109
8110 memset(&ro, 0, sizeof(ro));
8111 #ifdef SCTP_DEBUG
8112 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
8113 printf("sctp_shutdown_complete2 calling ip6_output:\n");
8114 sctp_print_address_pkt((struct ip *)ip6_out,
8115 &comp_cp->sh);
8116 }
8117 #endif
8118 ip6_output(mout, NULL, &ro, 0, NULL, NULL, NULL);
8119 }
8120 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
8121 return (0);
8122 }
8123
8124 static struct sctp_nets *
8125 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
8126 {
8127 struct sctp_nets *net, *hnet;
8128 int ms_goneby, highest_ms, state_overide=0;
8129
8130 SCTP_GETTIME_TIMEVAL(now);
8131 highest_ms = 0;
8132 hnet = NULL;
8133 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
8134 if (
8135 ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
8136 (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
8137 ) {
8138 /* Skip this guy from consideration if HB is off AND its confirmed*/
8139 #ifdef SCTP_DEBUG
8140 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8141 printf("Skipping net:%p state:%d nohb/out-of-scope\n",
8142 net, net->dest_state);
8143 }
8144 #endif
8145 continue;
8146 }
8147 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro.ro_sa) == 0) {
8148 /* skip this dest net from consideration */
8149 #ifdef SCTP_DEBUG
8150 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8151 printf("Skipping net:%p reachable NOT\n",
8152 net);
8153 }
8154 #endif
8155 continue;
8156 }
8157 if (net->last_sent_time.tv_sec) {
8158 /* Sent to so we subtract */
8159 ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
8160 } else
8161 /* Never been sent to */
8162 ms_goneby = 0x7fffffff;
8163 #ifdef SCTP_DEBUG
8164 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8165 printf("net:%p ms_goneby:%d\n",
8166 net, ms_goneby);
8167 }
8168 #endif
8169 /* When the address state is unconfirmed but still considered reachable, we
8170 * HB at a higher rate. Once it goes confirmed OR reaches the "unreachable"
8171 * state, thenw we cut it back to HB at a more normal pace.
8172 */
8173 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
8174 state_overide = 1;
8175 } else {
8176 state_overide = 0;
8177 }
8178
8179 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
8180 (ms_goneby > highest_ms)) {
8181 highest_ms = ms_goneby;
8182 hnet = net;
8183 #ifdef SCTP_DEBUG
8184 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8185 printf("net:%p is the new high\n",
8186 net);
8187 }
8188 #endif
8189 }
8190 }
8191 if (hnet &&
8192 ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED|SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
8193 state_overide = 1;
8194 } else {
8195 state_overide = 0;
8196 }
8197
8198 if (highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
8199 /* Found the one with longest delay bounds
8200 * OR it is unconfirmed and still not marked
8201 * unreachable.
8202 */
8203 #ifdef SCTP_DEBUG
8204 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8205 printf("net:%p is the hb winner -",
8206 hnet);
8207 if (hnet)
8208 sctp_print_address((struct sockaddr *)&hnet->ro.ro_sa);
8209 else
8210 printf(" none\n");
8211 }
8212 #endif
8213 /* update the timer now */
8214 hnet->last_sent_time = *now;
8215 return (hnet);
8216 }
8217 /* Nothing to HB */
8218 return (NULL);
8219 }
8220
8221 int
8222 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
8223 {
8224 struct sctp_tmit_chunk *chk;
8225 struct sctp_nets *net;
8226 struct sctp_heartbeat_chunk *hb;
8227 struct timeval now;
8228 struct sockaddr_in *sin;
8229 struct sockaddr_in6 *sin6;
8230
8231 if (user_req == 0) {
8232 net = sctp_select_hb_destination(stcb, &now);
8233 if (net == NULL) {
8234 /* All our busy none to send to, just
8235 * start the timer again.
8236 */
8237 if (stcb->asoc.state == 0) {
8238 return (0);
8239 }
8240 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
8241 stcb->sctp_ep,
8242 stcb,
8243 net);
8244 return (0);
8245 }
8246 #ifndef SCTP_USE_ALLMAN_BURST
8247 else {
8248 /* found one idle.. decay cwnd on this one
8249 * by 1/2 if none outstanding.
8250 */
8251
8252 if (net->flight_size == 0) {
8253 net->cwnd /= 2;
8254 if (net->addr_is_local) {
8255 if (net->cwnd < (net->mtu *4)) {
8256 net->cwnd = net->mtu * 4;
8257 }
8258 } else {
8259 if (net->cwnd < (net->mtu * 2)) {
8260 net->cwnd = net->mtu * 2;
8261 }
8262 }
8263
8264 }
8265
8266 }
8267 #endif
8268 } else {
8269 net = u_net;
8270 if (net == NULL) {
8271 return (0);
8272 }
8273 SCTP_GETTIME_TIMEVAL(&now);
8274 }
8275 sin = (struct sockaddr_in *)&net->ro.ro_sa;
8276 if (sin->sin_family != AF_INET) {
8277 if (sin->sin_family != AF_INET6) {
8278 /* huh */
8279 return (0);
8280 }
8281 }
8282 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8283 if (chk == NULL) {
8284 #ifdef SCTP_DEBUG
8285 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8286 printf("Gak, can't get a chunk for hb\n");
8287 }
8288 #endif
8289 return (0);
8290 }
8291 sctppcbinfo.ipi_gencnt_chunk++;
8292 sctppcbinfo.ipi_count_chunk++;
8293 chk->rec.chunk_id = SCTP_HEARTBEAT_REQUEST;
8294 chk->asoc = &stcb->asoc;
8295 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
8296 MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8297 if (chk->data == NULL) {
8298 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8299 sctppcbinfo.ipi_count_chunk--;
8300 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8301 panic("Chunk count is negative");
8302 }
8303 sctppcbinfo.ipi_gencnt_chunk++;
8304 return (0);
8305 }
8306 chk->data->m_data += SCTP_MIN_OVERHEAD;
8307 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8308 chk->sent = SCTP_DATAGRAM_UNSENT;
8309 chk->snd_count = 0;
8310 chk->whoTo = net;
8311 chk->whoTo->ref_count++;
8312 /* Now we have a mbuf that we can fill in with the details */
8313 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
8314
8315 /* fill out chunk header */
8316 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
8317 hb->ch.chunk_flags = 0;
8318 hb->ch.chunk_length = htons(chk->send_size);
8319 /* Fill out hb parameter */
8320 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
8321 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
8322 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
8323 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
8324 /* Did our user request this one, put it in */
8325 hb->heartbeat.hb_info.user_req = user_req;
8326 hb->heartbeat.hb_info.addr_family = sin->sin_family;
8327 hb->heartbeat.hb_info.addr_len = sin->sin_len;
8328 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
8329 /* we only take from the entropy pool if the address is
8330 * not confirmed.
8331 */
8332 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8333 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
8334 } else {
8335 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
8336 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
8337 }
8338 if (sin->sin_family == AF_INET) {
8339 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
8340 } else if (sin->sin_family == AF_INET6) {
8341 /* We leave the scope the way it is in our lookup table. */
8342 sin6 = (struct sockaddr_in6 *)&net->ro.ro_sa;
8343 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
8344 } else {
8345 /* huh compiler bug */
8346 #ifdef SCTP_DEBUG
8347 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
8348 printf("Compiler bug bleeds a mbuf and a chunk\n");
8349 }
8350 #endif
8351 return (0);
8352 }
8353 /* ok we have a destination that needs a beat */
8354 /* lets do the theshold management Qiaobing style */
8355 if (user_req == 0) {
8356 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
8357 stcb->asoc.max_send_times)) {
8358 /* we have lost the association, in a way this
8359 * is quite bad since we really are one less time
8360 * since we really did not send yet. This is the
8361 * down side to the Q's style as defined in the RFC
8362 * and not my alternate style defined in the RFC.
8363 */
8364 if (chk->data != NULL) {
8365 sctp_m_freem(chk->data);
8366 chk->data = NULL;
8367 }
8368 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8369 sctppcbinfo.ipi_count_chunk--;
8370 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8371 panic("Chunk count is negative");
8372 }
8373 sctppcbinfo.ipi_gencnt_chunk++;
8374 return (-1);
8375 }
8376 }
8377 net->hb_responded = 0;
8378 #ifdef SCTP_DEBUG
8379 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
8380 printf("Inserting chunk for HB\n");
8381 }
8382 #endif
8383 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8384 stcb->asoc.ctrl_queue_cnt++;
8385 sctp_pegs[SCTP_HB_SENT]++;
8386 /*
8387 * Call directly med level routine to put out the chunk. It will
8388 * always tumble out control chunks aka HB but it may even tumble
8389 * out data too.
8390 */
8391 if (user_req == 0) {
8392 /* Ok now lets start the HB timer if it is NOT a user req */
8393 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, stcb->sctp_ep,
8394 stcb, net);
8395 }
8396 return (1);
8397 }
8398
8399 void
8400 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
8401 uint32_t high_tsn)
8402 {
8403 struct sctp_association *asoc;
8404 struct sctp_ecne_chunk *ecne;
8405 struct sctp_tmit_chunk *chk;
8406 asoc = &stcb->asoc;
8407 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8408 if (chk->rec.chunk_id == SCTP_ECN_ECHO) {
8409 /* found a previous ECN_ECHO update it if needed */
8410 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8411 ecne->tsn = htonl(high_tsn);
8412 return;
8413 }
8414 }
8415 /* nope could not find one to update so we must build one */
8416 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8417 if (chk == NULL) {
8418 return;
8419 }
8420 sctp_pegs[SCTP_ECNE_SENT]++;
8421 sctppcbinfo.ipi_count_chunk++;
8422 sctppcbinfo.ipi_gencnt_chunk++;
8423 chk->rec.chunk_id = SCTP_ECN_ECHO;
8424 chk->asoc = &stcb->asoc;
8425 chk->send_size = sizeof(struct sctp_ecne_chunk);
8426 MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8427 if (chk->data == NULL) {
8428 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8429 sctppcbinfo.ipi_count_chunk--;
8430 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8431 panic("Chunk count is negative");
8432 }
8433 sctppcbinfo.ipi_gencnt_chunk++;
8434 return;
8435 }
8436 chk->data->m_data += SCTP_MIN_OVERHEAD;
8437 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8438 chk->sent = SCTP_DATAGRAM_UNSENT;
8439 chk->snd_count = 0;
8440 chk->whoTo = net;
8441 chk->whoTo->ref_count++;
8442 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
8443 ecne->ch.chunk_type = SCTP_ECN_ECHO;
8444 ecne->ch.chunk_flags = 0;
8445 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
8446 ecne->tsn = htonl(high_tsn);
8447 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8448 asoc->ctrl_queue_cnt++;
8449 }
8450
8451 void
8452 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
8453 struct mbuf *m, int iphlen, int bad_crc)
8454 {
8455 struct sctp_association *asoc;
8456 struct sctp_pktdrop_chunk *drp;
8457 struct sctp_tmit_chunk *chk;
8458 uint8_t *datap;
8459 int len;
8460 unsigned int small_one;
8461 struct ip *iph;
8462
8463 long spc;
8464 asoc = &stcb->asoc;
8465 if (asoc->peer_supports_pktdrop == 0) {
8466 /* peer must declare support before I
8467 * send one.
8468 */
8469 return;
8470 }
8471 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8472 if (chk == NULL) {
8473 return;
8474 }
8475 sctppcbinfo.ipi_count_chunk++;
8476 sctppcbinfo.ipi_gencnt_chunk++;
8477
8478 iph = mtod(m, struct ip *);
8479 if (iph == NULL) {
8480 return;
8481 }
8482 if (iph->ip_v == IPVERSION) {
8483 /* IPv4 */
8484 #if defined(__FreeBSD__)
8485 len = chk->send_size = iph->ip_len;
8486 #else
8487 len = chk->send_size = (iph->ip_len - iphlen);
8488 #endif
8489 } else {
8490 struct ip6_hdr *ip6h;
8491 /* IPv6 */
8492 ip6h = mtod(m, struct ip6_hdr *);
8493 len = chk->send_size = htons(ip6h->ip6_plen);
8494 }
8495 if ((len+iphlen) > m->m_pkthdr.len) {
8496 /* huh */
8497 chk->send_size = len = m->m_pkthdr.len - iphlen;
8498 }
8499 chk->asoc = &stcb->asoc;
8500 MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8501 if (chk->data == NULL) {
8502 jump_out:
8503 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8504 sctppcbinfo.ipi_count_chunk--;
8505 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8506 panic("Chunk count is negative");
8507 }
8508 sctppcbinfo.ipi_gencnt_chunk++;
8509 return;
8510 }
8511 if ((chk->send_size+sizeof(struct sctp_pktdrop_chunk)+SCTP_MIN_OVERHEAD) > MHLEN) {
8512 MCLGET(chk->data, M_DONTWAIT);
8513 if ((chk->data->m_flags & M_EXT) == 0) {
8514 /* Give up */
8515 sctp_m_freem(chk->data);
8516 chk->data = NULL;
8517 goto jump_out;
8518 }
8519 }
8520 chk->data->m_data += SCTP_MIN_OVERHEAD;
8521 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
8522 if (drp == NULL) {
8523 sctp_m_freem(chk->data);
8524 chk->data = NULL;
8525 goto jump_out;
8526 }
8527 small_one = asoc->smallest_mtu;
8528 if (small_one > MCLBYTES) {
8529 /* Only one cluster worth of data MAX */
8530 small_one = MCLBYTES;
8531 }
8532 chk->book_size = (chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
8533 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD);
8534 if (chk->book_size > small_one) {
8535 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
8536 drp->trunc_len = htons(chk->send_size);
8537 chk->send_size = small_one - (SCTP_MED_OVERHEAD +
8538 sizeof(struct sctp_pktdrop_chunk) +
8539 sizeof(struct sctphdr));
8540 len = chk->send_size;
8541 } else {
8542 /* no truncation needed */
8543 drp->ch.chunk_flags = 0;
8544 drp->trunc_len = htons(0);
8545 }
8546 if (bad_crc) {
8547 drp->ch.chunk_flags |= SCTP_BADCRC;
8548 }
8549 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
8550 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8551 chk->sent = SCTP_DATAGRAM_UNSENT;
8552 chk->snd_count = 0;
8553 if (net) {
8554 /* we should hit here */
8555 chk->whoTo = net;
8556 } else {
8557 chk->whoTo = asoc->primary_destination;
8558 }
8559 chk->whoTo->ref_count++;
8560 chk->rec.chunk_id = SCTP_PACKET_DROPPED;
8561 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
8562 drp->ch.chunk_length = htons(chk->send_size);
8563 spc = stcb->sctp_socket->so_rcv.sb_hiwat;
8564 if (spc < 0) {
8565 spc = 0;
8566 }
8567 drp->bottle_bw = htonl(spc);
8568 drp->current_onq = htonl(asoc->size_on_delivery_queue +
8569 asoc->size_on_reasm_queue +
8570 asoc->size_on_all_streams +
8571 asoc->my_rwnd_control_len +
8572 stcb->sctp_socket->so_rcv.sb_cc);
8573 drp->reserved = 0;
8574 datap = drp->data;
8575 m_copydata(m, iphlen, len, datap);
8576 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8577 asoc->ctrl_queue_cnt++;
8578 }
8579
8580 void
8581 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
8582 {
8583 struct sctp_association *asoc;
8584 struct sctp_cwr_chunk *cwr;
8585 struct sctp_tmit_chunk *chk;
8586
8587 asoc = &stcb->asoc;
8588 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8589 if (chk->rec.chunk_id == SCTP_ECN_CWR) {
8590 /* found a previous ECN_CWR update it if needed */
8591 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8592 if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
8593 MAX_TSN)) {
8594 cwr->tsn = htonl(high_tsn);
8595 }
8596 return;
8597 }
8598 }
8599 /* nope could not find one to update so we must build one */
8600 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8601 if (chk == NULL) {
8602 return;
8603 }
8604 sctppcbinfo.ipi_count_chunk++;
8605 sctppcbinfo.ipi_gencnt_chunk++;
8606 chk->rec.chunk_id = SCTP_ECN_CWR;
8607 chk->asoc = &stcb->asoc;
8608 chk->send_size = sizeof(struct sctp_cwr_chunk);
8609 MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8610 if (chk->data == NULL) {
8611 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8612 sctppcbinfo.ipi_count_chunk--;
8613 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8614 panic("Chunk count is negative");
8615 }
8616 sctppcbinfo.ipi_gencnt_chunk++;
8617 return;
8618 }
8619 chk->data->m_data += SCTP_MIN_OVERHEAD;
8620 chk->data->m_pkthdr.len = chk->data->m_len = chk->send_size;
8621 chk->sent = SCTP_DATAGRAM_UNSENT;
8622 chk->snd_count = 0;
8623 chk->whoTo = net;
8624 chk->whoTo->ref_count++;
8625 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
8626 cwr->ch.chunk_type = SCTP_ECN_CWR;
8627 cwr->ch.chunk_flags = 0;
8628 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
8629 cwr->tsn = htonl(high_tsn);
8630 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
8631 asoc->ctrl_queue_cnt++;
8632 }
8633 static void
8634 sctp_reset_the_streams(struct sctp_tcb *stcb,
8635 struct sctp_stream_reset_request *req, int number_entries, uint16_t *list)
8636 {
8637 int i;
8638
8639 if (req->reset_flags & SCTP_RESET_ALL) {
8640 for (i=0; i<stcb->asoc.streamoutcnt; i++) {
8641 stcb->asoc.strmout[i].next_sequence_sent = 0;
8642 }
8643 } else if (number_entries) {
8644 for (i=0; i<number_entries; i++) {
8645 if (list[i] >= stcb->asoc.streamoutcnt) {
8646 /* no such stream */
8647 continue;
8648 }
8649 stcb->asoc.strmout[(list[i])].next_sequence_sent = 0;
8650 }
8651 }
8652 sctp_ulp_notify(SCTP_NOTIFY_STR_RESET_SEND, stcb, number_entries, (void *)list);
8653 }
8654
8655 void
8656 sctp_send_str_reset_ack(struct sctp_tcb *stcb,
8657 struct sctp_stream_reset_request *req)
8658 {
8659 struct sctp_association *asoc;
8660 struct sctp_stream_reset_resp *strack;
8661 struct sctp_tmit_chunk *chk;
8662 uint32_t seq;
8663 int number_entries, i;
8664 uint8_t two_way=0, not_peer=0;
8665 uint16_t *list=NULL;
8666
8667 asoc = &stcb->asoc;
8668 if (req->reset_flags & SCTP_RESET_ALL)
8669 number_entries = 0;
8670 else
8671 number_entries = (ntohs(req->ph.param_length) - sizeof(struct sctp_stream_reset_request)) / sizeof(uint16_t);
8672
8673 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8674 if (chk == NULL) {
8675 return;
8676 }
8677 sctppcbinfo.ipi_count_chunk++;
8678 sctppcbinfo.ipi_gencnt_chunk++;
8679 chk->rec.chunk_id = SCTP_STREAM_RESET;
8680 chk->asoc = &stcb->asoc;
8681 chk->send_size = sizeof(struct sctp_stream_reset_resp) + (number_entries * sizeof(uint16_t));
8682 MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8683 if (chk->data == NULL) {
8684 strresp_jump_out:
8685 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8686 sctppcbinfo.ipi_count_chunk--;
8687 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8688 panic("Chunk count is negative");
8689 }
8690 sctppcbinfo.ipi_gencnt_chunk++;
8691 return;
8692 }
8693 chk->data->m_data += SCTP_MIN_OVERHEAD;
8694 chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8695 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8696 MCLGET(chk->data, M_DONTWAIT);
8697 if ((chk->data->m_flags & M_EXT) == 0) {
8698 /* Give up */
8699 sctp_m_freem(chk->data);
8700 chk->data = NULL;
8701 goto strresp_jump_out;
8702 }
8703 chk->data->m_data += SCTP_MIN_OVERHEAD;
8704 }
8705 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8706 /* can't do it, no room */
8707 /* Give up */
8708 sctp_m_freem(chk->data);
8709 chk->data = NULL;
8710 goto strresp_jump_out;
8711
8712 }
8713 chk->sent = SCTP_DATAGRAM_UNSENT;
8714 chk->snd_count = 0;
8715 chk->whoTo = asoc->primary_destination;
8716 chk->whoTo->ref_count++;
8717 strack = mtod(chk->data, struct sctp_stream_reset_resp *);
8718
8719 strack->ch.chunk_type = SCTP_STREAM_RESET;
8720 strack->ch.chunk_flags = 0;
8721 strack->ch.chunk_length = htons(chk->send_size);
8722
8723 memset(strack->sr_resp.reset_pad, 0, sizeof(strack->sr_resp.reset_pad));
8724
8725 strack->sr_resp.ph.param_type = ntohs(SCTP_STR_RESET_RESPONSE);
8726 strack->sr_resp.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
8727
8728
8729
8730 if (chk->send_size % 4) {
8731 /* need a padding for the end */
8732 int pad;
8733 uint8_t *end;
8734 end = (uint8_t *)((vaddr_t)strack + chk->send_size);
8735 pad = chk->send_size % 4;
8736 for (i = 0; i < pad; i++) {
8737 end[i] = 0;
8738 }
8739 chk->send_size += pad;
8740 }
8741
8742 /* actual response */
8743 if (req->reset_flags & SCTP_RESET_YOUR) {
8744 strack->sr_resp.reset_flags = SCTP_RESET_PERFORMED;
8745 } else {
8746 strack->sr_resp.reset_flags = 0;
8747 }
8748
8749 /* copied from reset request */
8750 strack->sr_resp.reset_req_seq_resp = req->reset_req_seq;
8751 seq = ntohl(req->reset_req_seq);
8752
8753 list = req->list_of_streams;
8754 /* copy the un-converted network byte order streams */
8755 for (i=0; i<number_entries; i++) {
8756 strack->sr_resp.list_of_streams[i] = list[i];
8757 }
8758 if (asoc->str_reset_seq_in == seq) {
8759 /* is it the next expected? */
8760 asoc->str_reset_seq_in++;
8761 strack->sr_resp.reset_at_tsn = htonl(asoc->sending_seq);
8762 asoc->str_reset_sending_seq = asoc->sending_seq;
8763 if (number_entries) {
8764 uint16_t temp;
8765 /* convert them to host byte order */
8766 for (i=0 ; i<number_entries; i++) {
8767 temp = ntohs(list[i]);
8768 list[i] = temp;
8769 }
8770 }
8771 if (req->reset_flags & SCTP_RESET_YOUR) {
8772 /* reset my outbound streams */
8773 sctp_reset_the_streams(stcb, req , number_entries, list);
8774 }
8775 if (req->reset_flags & SCTP_RECIPRICAL) {
8776 /* reset peer too */
8777 sctp_send_str_reset_req(stcb, number_entries, list, two_way, not_peer);
8778 }
8779
8780 } else {
8781 /* no its a retran so I must just ack and do nothing */
8782 strack->sr_resp.reset_at_tsn = htonl(asoc->str_reset_sending_seq);
8783 }
8784 strack->sr_resp.cumulative_tsn = htonl(asoc->cumulative_tsn);
8785 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
8786 chk,
8787 sctp_next);
8788 asoc->ctrl_queue_cnt++;
8789 }
8790
8791
8792 void
8793 sctp_send_str_reset_req(struct sctp_tcb *stcb,
8794 int number_entrys, uint16_t *list, uint8_t two_way, uint8_t not_peer)
8795 {
8796 /* Send a stream reset request. The number_entrys may be 0 and list NULL
8797 * if the request is to reset all streams. If two_way is true then we
8798 * not only request a RESET of the received streams but we also
8799 * request the peer to send a reset req to us too.
8800 * Flag combinations in table:
8801 *
8802 * two_way | not_peer | = | Flags
8803 * ------------------------------
8804 * 0 | 0 | = | SCTP_RESET_YOUR (just the peer)
8805 * 1 | 0 | = | SCTP_RESET_YOUR | SCTP_RECIPRICAL (both sides)
8806 * 0 | 1 | = | Not a Valid Request (not anyone)
8807 * 1 | 1 | = | SCTP_RESET_RECIPRICAL (Just local host)
8808 */
8809 struct sctp_association *asoc;
8810 struct sctp_stream_reset_req *strreq;
8811 struct sctp_tmit_chunk *chk;
8812
8813
8814 asoc = &stcb->asoc;
8815 if (asoc->stream_reset_outstanding) {
8816 /* Already one pending, must get ACK back
8817 * to clear the flag.
8818 */
8819 return;
8820 }
8821
8822 if ((two_way == 0) && (not_peer == 1)) {
8823 /* not a valid request */
8824 return;
8825 }
8826
8827 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
8828 if (chk == NULL) {
8829 return;
8830 }
8831 sctppcbinfo.ipi_count_chunk++;
8832 sctppcbinfo.ipi_gencnt_chunk++;
8833 chk->rec.chunk_id = SCTP_STREAM_RESET;
8834 chk->asoc = &stcb->asoc;
8835 chk->send_size = sizeof(struct sctp_stream_reset_req) + (number_entrys * sizeof(uint16_t));
8836 MGETHDR(chk->data, M_DONTWAIT, MT_DATA);
8837 if (chk->data == NULL) {
8838 strreq_jump_out:
8839 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
8840 sctppcbinfo.ipi_count_chunk--;
8841 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
8842 panic("Chunk count is negative");
8843 }
8844 sctppcbinfo.ipi_gencnt_chunk++;
8845 return;
8846 }
8847 chk->data->m_data += SCTP_MIN_OVERHEAD;
8848 chk->data->m_pkthdr.len = chk->data->m_len = SCTP_SIZE32(chk->send_size);
8849 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8850 MCLGET(chk->data, M_DONTWAIT);
8851 if ((chk->data->m_flags & M_EXT) == 0) {
8852 /* Give up */
8853 sctp_m_freem(chk->data);
8854 chk->data = NULL;
8855 goto strreq_jump_out;
8856 }
8857 chk->data->m_data += SCTP_MIN_OVERHEAD;
8858 }
8859 if (M_TRAILINGSPACE(chk->data) < (int)SCTP_SIZE32(chk->send_size)) {
8860 /* can't do it, no room */
8861 /* Give up */
8862 sctp_m_freem(chk->data);
8863 chk->data = NULL;
8864 goto strreq_jump_out;
8865 }
8866 chk->sent = SCTP_DATAGRAM_UNSENT;
8867 chk->snd_count = 0;
8868 chk->whoTo = asoc->primary_destination;
8869 chk->whoTo->ref_count++;
8870
8871 strreq = mtod(chk->data, struct sctp_stream_reset_req *);
8872 strreq->ch.chunk_type = SCTP_STREAM_RESET;
8873 strreq->ch.chunk_flags = 0;
8874 strreq->ch.chunk_length = htons(chk->send_size);
8875
8876 strreq->sr_req.ph.param_type = ntohs(SCTP_STR_RESET_REQUEST);
8877 strreq->sr_req.ph.param_length = htons((chk->send_size - sizeof(struct sctp_chunkhdr)));
8878
8879 if (chk->send_size % 4) {
8880 /* need a padding for the end */
8881 int pad, i;
8882 uint8_t *end;
8883 end = (uint8_t *)((vaddr_t)strreq + chk->send_size);
8884 pad = chk->send_size % 4;
8885 for (i=0; i<pad; i++) {
8886 end[i] = 0;
8887 }
8888 chk->send_size += pad;
8889 }
8890
8891 strreq->sr_req.reset_flags = 0;
8892 if (number_entrys == 0) {
8893 strreq->sr_req.reset_flags |= SCTP_RESET_ALL;
8894 }
8895 if (two_way == 0) {
8896 strreq->sr_req.reset_flags |= SCTP_RESET_YOUR;
8897 } else {
8898 if (not_peer == 0) {
8899 strreq->sr_req.reset_flags |= SCTP_RECIPRICAL | SCTP_RESET_YOUR;
8900 } else {
8901 strreq->sr_req.reset_flags |= SCTP_RECIPRICAL;
8902 }
8903 }
8904 memset(strreq->sr_req.reset_pad, 0, sizeof(strreq->sr_req.reset_pad));
8905 strreq->sr_req.reset_req_seq = htonl(asoc->str_reset_seq_out);
8906 if (number_entrys) {
8907 /* populate the specific entry's */
8908 int i;
8909 for (i=0; i < number_entrys; i++) {
8910 strreq->sr_req.list_of_streams[i] = htons(list[i]);
8911 }
8912 }
8913 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
8914 chk,
8915 sctp_next);
8916 asoc->ctrl_queue_cnt++;
8917 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
8918 asoc->stream_reset_outstanding = 1;
8919 }
8920
8921 void
8922 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
8923 struct mbuf *err_cause)
8924 {
8925 /*
8926 * Formulate the abort message, and send it back down.
8927 */
8928 struct mbuf *mout;
8929 struct sctp_abort_msg *abm;
8930 struct ip *iph, *iph_out;
8931 struct ip6_hdr *ip6, *ip6_out;
8932 int iphlen_out;
8933
8934 /* don't respond to ABORT with ABORT */
8935 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
8936 if (err_cause)
8937 sctp_m_freem(err_cause);
8938 return;
8939 }
8940 MGETHDR(mout, M_DONTWAIT, MT_HEADER);
8941 if (mout == NULL) {
8942 if (err_cause)
8943 sctp_m_freem(err_cause);
8944 return;
8945 }
8946 iph = mtod(m, struct ip *);
8947 iph_out = NULL;
8948 ip6_out = NULL;
8949 if (iph->ip_v == IPVERSION) {
8950 iph_out = mtod(mout, struct ip *);
8951 mout->m_len = sizeof(*iph_out) + sizeof(*abm);
8952 mout->m_next = err_cause;
8953
8954 /* Fill in the IP header for the ABORT */
8955 iph_out->ip_v = IPVERSION;
8956 iph_out->ip_hl = (sizeof(struct ip) / 4);
8957 iph_out->ip_tos = (u_char)0;
8958 iph_out->ip_id = 0;
8959 iph_out->ip_off = 0;
8960 iph_out->ip_ttl = MAXTTL;
8961 iph_out->ip_p = IPPROTO_SCTP;
8962 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
8963 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
8964 /* let IP layer calculate this */
8965 iph_out->ip_sum = 0;
8966
8967 iphlen_out = sizeof(*iph_out);
8968 abm = (struct sctp_abort_msg *)((vaddr_t)iph_out + iphlen_out);
8969 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
8970 ip6 = (struct ip6_hdr *)iph;
8971 ip6_out = mtod(mout, struct ip6_hdr *);
8972 mout->m_len = sizeof(*ip6_out) + sizeof(*abm);
8973 mout->m_next = err_cause;
8974
8975 /* Fill in the IP6 header for the ABORT */
8976 ip6_out->ip6_flow = ip6->ip6_flow;
8977 ip6_out->ip6_hlim = ip6_defhlim;
8978 ip6_out->ip6_nxt = IPPROTO_SCTP;
8979 ip6_out->ip6_src = ip6->ip6_dst;
8980 ip6_out->ip6_dst = ip6->ip6_src;
8981
8982 iphlen_out = sizeof(*ip6_out);
8983 abm = (struct sctp_abort_msg *)((vaddr_t)ip6_out + iphlen_out);
8984 } else {
8985 /* Currently not supported */
8986 return;
8987 }
8988
8989 abm->sh.src_port = sh->dest_port;
8990 abm->sh.dest_port = sh->src_port;
8991 abm->sh.checksum = 0;
8992 if (vtag == 0) {
8993 abm->sh.v_tag = sh->v_tag;
8994 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
8995 } else {
8996 abm->sh.v_tag = htonl(vtag);
8997 abm->msg.ch.chunk_flags = 0;
8998 }
8999 abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
9000
9001 if (err_cause) {
9002 struct mbuf *m_tmp = err_cause;
9003 int err_len = 0;
9004 /* get length of the err_cause chain */
9005 while (m_tmp != NULL) {
9006 err_len += m_tmp->m_len;
9007 m_tmp = m_tmp->m_next;
9008 }
9009 mout->m_pkthdr.len = mout->m_len + err_len;
9010 if (err_len % 4) {
9011 /* need pad at end of chunk */
9012 u_int32_t cpthis=0;
9013 int padlen;
9014 padlen = 4 - (mout->m_pkthdr.len % 4);
9015 m_copyback(mout, mout->m_pkthdr.len, padlen, (void *)&cpthis);
9016 }
9017 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
9018 } else {
9019 mout->m_pkthdr.len = mout->m_len;
9020 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
9021 }
9022
9023 /* add checksum */
9024 if ((sctp_no_csum_on_loopback) &&
9025 (m->m_pkthdr.rcvif) &&
9026 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
9027 abm->sh.checksum = 0;
9028 } else {
9029 abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
9030 }
9031
9032 /* zap the rcvif, it should be null */
9033 mout->m_pkthdr.rcvif = 0;
9034 if (iph_out != NULL) {
9035 struct route ro;
9036
9037 /* zap the stack pointer to the route */
9038 memset(&ro, 0, sizeof ro);
9039 #ifdef SCTP_DEBUG
9040 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9041 printf("sctp_send_abort calling ip_output:\n");
9042 sctp_print_address_pkt(iph_out, &abm->sh);
9043 }
9044 #endif
9045 /* set IPv4 length */
9046 iph_out->ip_len = htons(mout->m_pkthdr.len);
9047 /* out it goes */
9048 (void)ip_output(mout, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
9049 } else if (ip6_out != NULL) {
9050 struct route ro;
9051
9052 /* zap the stack pointer to the route */
9053 memset(&ro, 0, sizeof(ro));
9054 #ifdef SCTP_DEBUG
9055 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9056 printf("sctp_send_abort calling ip6_output:\n");
9057 sctp_print_address_pkt((struct ip *)ip6_out, &abm->sh);
9058 }
9059 #endif
9060 ip6_output(mout, NULL, &ro, 0, NULL, NULL, NULL);
9061 }
9062 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9063 }
9064
9065 void
9066 sctp_send_operr_to(struct mbuf *m, int iphlen,
9067 struct mbuf *scm,
9068 uint32_t vtag)
9069 {
9070 struct sctphdr *ihdr;
9071 struct sctphdr *ohdr;
9072 struct sctp_chunkhdr *ophdr;
9073
9074 struct ip *iph;
9075 #ifdef SCTP_DEBUG
9076 struct sockaddr_in6 lsa6, fsa6;
9077 #endif
9078 uint32_t val;
9079 iph = mtod(m, struct ip *);
9080 ihdr = (struct sctphdr *)((vaddr_t)iph + iphlen);
9081 if (!(scm->m_flags & M_PKTHDR)) {
9082 /* must be a pkthdr */
9083 printf("Huh, not a packet header in send_operr\n");
9084 m_freem(scm);
9085 return;
9086 }
9087 M_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
9088 if (scm == NULL) {
9089 /* can't send because we can't add a mbuf */
9090 return;
9091 }
9092 ohdr = mtod(scm, struct sctphdr *);
9093 ohdr->src_port = ihdr->dest_port;
9094 ohdr->dest_port = ihdr->src_port;
9095 ohdr->v_tag = vtag;
9096 ohdr->checksum = 0;
9097 ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
9098 ophdr->chunk_type = SCTP_OPERATION_ERROR;
9099 ophdr->chunk_flags = 0;
9100 ophdr->chunk_length = htons(scm->m_pkthdr.len - sizeof(struct sctphdr));
9101 if (scm->m_pkthdr.len % 4) {
9102 /* need padding */
9103 u_int32_t cpthis=0;
9104 int padlen;
9105 padlen = 4 - (scm->m_pkthdr.len % 4);
9106 m_copyback(scm, scm->m_pkthdr.len, padlen, (void *)&cpthis);
9107 }
9108 if ((sctp_no_csum_on_loopback) &&
9109 (m->m_pkthdr.rcvif) &&
9110 (m->m_pkthdr.rcvif->if_type == IFT_LOOP)) {
9111 val = 0;
9112 } else {
9113 val = sctp_calculate_sum(scm, NULL, 0);
9114 }
9115 ohdr->checksum = val;
9116 if (iph->ip_v == IPVERSION) {
9117 /* V4 */
9118 struct ip *out;
9119 struct route ro;
9120 M_PREPEND(scm, sizeof(struct ip), M_DONTWAIT);
9121 if (scm == NULL)
9122 return;
9123 memset(&ro, 0, sizeof ro);
9124 out = mtod(scm, struct ip *);
9125 out->ip_v = iph->ip_v;
9126 out->ip_hl = (sizeof(struct ip)/4);
9127 out->ip_tos = iph->ip_tos;
9128 out->ip_id = iph->ip_id;
9129 out->ip_off = 0;
9130 out->ip_ttl = MAXTTL;
9131 out->ip_p = IPPROTO_SCTP;
9132 out->ip_sum = 0;
9133 out->ip_src = iph->ip_dst;
9134 out->ip_dst = iph->ip_src;
9135 out->ip_len = htons(scm->m_pkthdr.len);
9136 ip_output(scm, 0, &ro, IP_RAWOUTPUT, NULL, NULL);
9137 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9138 } else {
9139 /* V6 */
9140 struct route ro;
9141 struct ip6_hdr *out6, *in6;
9142
9143 M_PREPEND(scm, sizeof(struct ip6_hdr), M_DONTWAIT);
9144 if (scm == NULL)
9145 return;
9146 memset(&ro, 0, sizeof ro);
9147 in6 = mtod(m, struct ip6_hdr *);
9148 out6 = mtod(scm, struct ip6_hdr *);
9149 out6->ip6_flow = in6->ip6_flow;
9150 out6->ip6_hlim = ip6_defhlim;
9151 out6->ip6_nxt = IPPROTO_SCTP;
9152 out6->ip6_src = in6->ip6_dst;
9153 out6->ip6_dst = in6->ip6_src;
9154
9155 #ifdef SCTP_DEBUG
9156 memset(&lsa6, 0, sizeof(lsa6));
9157 lsa6.sin6_len = sizeof(lsa6);
9158 lsa6.sin6_family = AF_INET6;
9159 lsa6.sin6_addr = out6->ip6_src;
9160 memset(&fsa6, 0, sizeof(fsa6));
9161 fsa6.sin6_len = sizeof(fsa6);
9162 fsa6.sin6_family = AF_INET6;
9163 fsa6.sin6_addr = out6->ip6_dst;
9164 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9165 printf("sctp_operr_to calling ipv6 output:\n");
9166 printf("src: ");
9167 sctp_print_address((struct sockaddr *)&lsa6);
9168 printf("dst ");
9169 sctp_print_address((struct sockaddr *)&fsa6);
9170 }
9171 #endif /* SCTP_DEBUG */
9172 ip6_output(scm, NULL, &ro, 0, NULL, NULL, NULL);
9173 sctp_pegs[SCTP_DATAGRAMS_SENT]++;
9174 }
9175 }
9176
9177 static int
9178 sctp_copy_one(struct mbuf *m, struct uio *uio, int cpsz, int resv_upfront, int *mbcnt)
9179 {
9180 int left, cancpy, willcpy, error;
9181 left = cpsz;
9182
9183 if (m == NULL) {
9184 /* TSNH */
9185 *mbcnt = 0;
9186 return (ENOMEM);
9187 }
9188 m->m_len = 0;
9189 if ((left+resv_upfront) > (int)MHLEN) {
9190 MCLGET(m, M_WAIT);
9191 if (m == NULL) {
9192 *mbcnt = 0;
9193 return (ENOMEM);
9194 }
9195 if ((m->m_flags & M_EXT) == 0) {
9196 *mbcnt = 0;
9197 return (ENOMEM);
9198 }
9199 *mbcnt += m->m_ext.ext_size;
9200 }
9201 *mbcnt += MSIZE;
9202 cancpy = M_TRAILINGSPACE(m);
9203 willcpy = min(cancpy, left);
9204 if ((willcpy + resv_upfront) > cancpy) {
9205 willcpy -= resv_upfront;
9206 }
9207 while (left > 0) {
9208 /* Align data to the end */
9209 if ((m->m_flags & M_EXT) == 0) {
9210 if (m->m_flags & M_PKTHDR) {
9211 MH_ALIGN(m, willcpy);
9212 } else {
9213 M_ALIGN(m, willcpy);
9214 }
9215 } else {
9216 MC_ALIGN(m, willcpy);
9217 }
9218 error = uiomove(mtod(m, void *), willcpy, uio);
9219 if (error) {
9220 return (error);
9221 }
9222 m->m_len = willcpy;
9223 m->m_nextpkt = 0;
9224 left -= willcpy;
9225 if (left > 0) {
9226 MGET(m->m_next, M_WAIT, MT_DATA);
9227 if (m->m_next == NULL) {
9228 *mbcnt = 0;
9229 return (ENOMEM);
9230 }
9231 m = m->m_next;
9232 m->m_len = 0;
9233 *mbcnt += MSIZE;
9234 if (left > (int)MHLEN) {
9235 MCLGET(m, M_WAIT);
9236 if (m == NULL) {
9237 *mbcnt = 0;
9238 return (ENOMEM);
9239 }
9240 if ((m->m_flags & M_EXT) == 0) {
9241 *mbcnt = 0;
9242 return (ENOMEM);
9243 }
9244 *mbcnt += m->m_ext.ext_size;
9245 }
9246 cancpy = M_TRAILINGSPACE(m);
9247 willcpy = min(cancpy, left);
9248 }
9249 }
9250 return (0);
9251 }
9252
9253 static int
9254 sctp_copy_it_in(struct sctp_inpcb *inp,
9255 struct sctp_tcb *stcb,
9256 struct sctp_association *asoc,
9257 struct sctp_nets *net,
9258 struct sctp_sndrcvinfo *srcv,
9259 struct uio *uio,
9260 int flags)
9261 {
9262 /* This routine must be very careful in
9263 * its work. Protocol processing is
9264 * up and running so care must be taken to
9265 * spl...() when you need to do something
9266 * that may effect the stcb/asoc. The sb is
9267 * locked however. When data is copied the
9268 * protocol processing should be enabled since
9269 * this is a slower operation...
9270 */
9271 struct socket *so;
9272 int error = 0;
9273 int frag_size, mbcnt = 0, mbcnt_e = 0;
9274 unsigned int sndlen;
9275 unsigned int tot_demand;
9276 int tot_out, dataout;
9277 struct sctp_tmit_chunk *chk;
9278 struct mbuf *mm;
9279 struct sctp_stream_out *strq;
9280 uint32_t my_vtag;
9281 int resv_in_first;
9282
9283 so = stcb->sctp_socket;
9284 solock(so);
9285 chk = NULL;
9286 mm = NULL;
9287
9288 sndlen = uio->uio_resid;
9289 /* lock the socket buf */
9290 error = sblock(&so->so_snd, SBLOCKWAIT(flags));
9291 if (error)
9292 goto out_locked;
9293
9294 #ifdef SCTP_DEBUG
9295 printf("sctp_copy_it_in: %d\n", sndlen);
9296 #endif
9297 /* will it ever fit ? */
9298 if (sndlen > so->so_snd.sb_hiwat) {
9299 /* It will NEVER fit */
9300 error = EMSGSIZE;
9301 goto release;
9302 }
9303 /* Do I need to block? */
9304 if ((so->so_snd.sb_hiwat <
9305 (sndlen + asoc->total_output_queue_size)) ||
9306 (asoc->chunks_on_out_queue > sctp_max_chunks_on_queue) ||
9307 (asoc->total_output_mbuf_queue_size >
9308 so->so_snd.sb_mbmax)
9309 ) {
9310 /* prune any prsctp bufs out */
9311 if (asoc->peer_supports_prsctp) {
9312 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9313 }
9314 /*
9315 * We store off a pointer to the endpoint.
9316 * Since on return from this we must check to
9317 * see if an so_error is set. If so we may have
9318 * been reset and our stcb destroyed. Returning
9319 * an error will flow back to the user...
9320 */
9321 while ((so->so_snd.sb_hiwat <
9322 (sndlen + asoc->total_output_queue_size)) ||
9323 (asoc->chunks_on_out_queue >
9324 sctp_max_chunks_on_queue) ||
9325 (asoc->total_output_mbuf_queue_size >
9326 so->so_snd.sb_mbmax)
9327 ) {
9328 if ((so->so_state & SS_NBIO)
9329 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
9330 || (flags & MSG_NBIO)
9331 #endif
9332 ) {
9333 /* Non-blocking io in place */
9334 error = EWOULDBLOCK;
9335 goto release;
9336 }
9337 inp->sctp_tcb_at_block = (void *)stcb;
9338 inp->error_on_block = 0;
9339 #ifdef SCTP_BLK_LOGGING
9340 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
9341 so, asoc);
9342 #endif
9343 sbunlock(&so->so_snd);
9344 SCTP_TCB_UNLOCK(stcb);
9345 error = sbwait(&so->so_snd);
9346 SCTP_INP_RLOCK(inp);
9347 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
9348 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
9349 /* Should I really unlock ? */
9350 SCTP_INP_RUNLOCK(inp);
9351 error = EFAULT;
9352 goto out_locked;
9353 }
9354 SCTP_TCB_LOCK(stcb);
9355 SCTP_INP_RUNLOCK(inp);
9356
9357 inp->sctp_tcb_at_block = 0;
9358 #ifdef SCTP_BLK_LOGGING
9359 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
9360 so, asoc);
9361 #endif
9362 if (inp->error_on_block) {
9363 /*
9364 * if our asoc was killed, the free code
9365 * (in sctp_pcb.c) will save a error in
9366 * here for us
9367 */
9368 error = inp->error_on_block;
9369 goto out_locked;
9370 }
9371 if (error) {
9372 goto out_locked;
9373 }
9374 /* did we encounter a socket error? */
9375 if (so->so_error) {
9376 error = so->so_error;
9377 goto out_locked;
9378 }
9379 error = sblock(&so->so_snd, M_WAITOK);
9380 if (error) {
9381 /* Can't aquire the lock */
9382 goto out_locked;
9383 }
9384 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
9385 if (so->so_rcv.sb_state & SBS_CANTSENDMORE) {
9386 #else
9387 if (so->so_state & SS_CANTSENDMORE) {
9388 #endif
9389 /* The socket is now set not to sendmore.. its gone */
9390 error = EPIPE;
9391 goto release;
9392 }
9393 if (so->so_error) {
9394 error = so->so_error;
9395 goto release;
9396 }
9397 if (asoc->peer_supports_prsctp) {
9398 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
9399 }
9400 }
9401 }
9402 dataout = tot_out = uio->uio_resid;
9403 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9404 resv_in_first = SCTP_MED_OVERHEAD;
9405 } else {
9406 resv_in_first = SCTP_MED_V4_OVERHEAD;
9407 }
9408
9409 /* Are we aborting? */
9410 if (srcv->sinfo_flags & MSG_ABORT) {
9411 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
9412 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
9413 /* It has to be up before we abort */
9414 /* how big is the user initiated abort? */
9415
9416 /* I wonder about doing a MGET without a splnet set.
9417 * it is done that way in the sosend code so I guess
9418 * it is ok :-0
9419 */
9420 MGETHDR(mm, M_WAIT, MT_DATA);
9421 if (mm) {
9422 struct sctp_paramhdr *ph;
9423
9424 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
9425 if (tot_demand > MHLEN) {
9426 if (tot_demand > MCLBYTES) {
9427 /* truncate user data */
9428 tot_demand = MCLBYTES;
9429 tot_out = tot_demand - sizeof(struct sctp_paramhdr);
9430 }
9431 MCLGET(mm, M_WAIT);
9432 if ((mm->m_flags & M_EXT) == 0) {
9433 /* truncate further */
9434 tot_demand = MHLEN;
9435 tot_out = tot_demand - sizeof(struct sctp_paramhdr);
9436 }
9437 }
9438 /* now move forward the data pointer */
9439 ph = mtod(mm, struct sctp_paramhdr *);
9440 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
9441 ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
9442 ph++;
9443 mm->m_pkthdr.len = tot_out + sizeof(struct sctp_paramhdr);
9444 mm->m_len = mm->m_pkthdr.len;
9445 error = uiomove((void *)ph, (int)tot_out, uio);
9446 if (error) {
9447 /*
9448 * Here if we can't get his data we
9449 * still abort we just don't get to
9450 * send the users note :-0
9451 */
9452 sctp_m_freem(mm);
9453 mm = NULL;
9454 }
9455 }
9456 sbunlock(&so->so_snd);
9457 sctp_abort_an_association(stcb->sctp_ep, stcb,
9458 SCTP_RESPONSE_TO_USER_REQ,
9459 mm);
9460 mm = NULL;
9461 goto out_locked;
9462 }
9463 goto release;
9464 }
9465
9466 /* Now can we send this? */
9467 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
9468 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
9469 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
9470 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
9471 /* got data while shutting down */
9472 error = ECONNRESET;
9473 goto release;
9474 }
9475 /* Is the stream no. valid? */
9476 if (srcv->sinfo_stream >= asoc->streamoutcnt) {
9477 /* Invalid stream number */
9478 error = EINVAL;
9479 goto release;
9480 }
9481 if (asoc->strmout == NULL) {
9482 /* huh? software error */
9483 #ifdef SCTP_DEBUG
9484 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
9485 printf("software error in sctp_copy_it_in\n");
9486 }
9487 #endif
9488 error = EFAULT;
9489 goto release;
9490 }
9491 if ((srcv->sinfo_flags & MSG_EOF) &&
9492 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) &&
9493 (tot_out == 0)) {
9494 sounlock(so);
9495 goto zap_by_it_now;
9496 }
9497 if (tot_out == 0) {
9498 /* not allowed */
9499 error = EMSGSIZE;
9500 goto release;
9501 }
9502 /* save off the tag */
9503 my_vtag = asoc->my_vtag;
9504 strq = &asoc->strmout[srcv->sinfo_stream];
9505 /* First lets figure out the "chunking" point */
9506 frag_size = sctp_get_frag_point(stcb, asoc);
9507
9508 /* two choices here, it all fits in one chunk or
9509 * we need multiple chunks.
9510 */
9511 sounlock(so);
9512 if (tot_out <= frag_size) {
9513 /* no need to setup a template */
9514 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9515 if (chk == NULL) {
9516 error = ENOMEM;
9517 goto release;
9518 }
9519 sctppcbinfo.ipi_count_chunk++;
9520 sctppcbinfo.ipi_gencnt_chunk++;
9521 asoc->chunks_on_out_queue++;
9522 MGETHDR(mm, M_WAIT, MT_DATA);
9523 if (mm == NULL) {
9524 error = ENOMEM;
9525 goto clean_up;
9526 }
9527 error = sctp_copy_one(mm, uio, tot_out, resv_in_first, &mbcnt_e);
9528 if (error)
9529 goto clean_up;
9530 sctp_prepare_chunk(chk, stcb, srcv, strq, net);
9531 chk->mbcnt = mbcnt_e;
9532 mbcnt += mbcnt_e;
9533 mbcnt_e = 0;
9534 mm->m_pkthdr.len = tot_out;
9535 chk->data = mm;
9536 mm = NULL;
9537
9538 /* the actual chunk flags */
9539 chk->rec.data.rcv_flags |= SCTP_DATA_NOT_FRAG;
9540 chk->whoTo->ref_count++;
9541
9542 /* fix up the send_size if it is not present */
9543 chk->send_size = tot_out;
9544 chk->book_size = chk->send_size;
9545 /* ok, we are commited */
9546 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
9547 /* bump the ssn if we are unordered. */
9548 strq->next_sequence_sent++;
9549 }
9550 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
9551 asoc->sent_queue_cnt_removeable++;
9552 }
9553 solock(so);
9554 if ((asoc->state == 0) ||
9555 (my_vtag != asoc->my_vtag) ||
9556 (so != inp->sctp_socket) ||
9557 (inp->sctp_socket == 0)) {
9558 /* connection was aborted */
9559 sounlock(so);
9560 error = ECONNRESET;
9561 goto clean_up;
9562 }
9563 asoc->stream_queue_cnt++;
9564 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
9565 /* now check if this stream is on the wheel */
9566 if ((strq->next_spoke.tqe_next == NULL) &&
9567 (strq->next_spoke.tqe_prev == NULL)) {
9568 /* Insert it on the wheel since it is not
9569 * on it currently
9570 */
9571 sctp_insert_on_wheel(asoc, strq);
9572 }
9573 sounlock(so);
9574 clean_up:
9575 if (error) {
9576 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9577 sctppcbinfo.ipi_count_chunk--;
9578 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9579 panic("Chunk count is negative");
9580 }
9581 goto release;
9582 }
9583 } else {
9584 /* we need to setup a template */
9585 struct sctp_tmit_chunk template;
9586 struct sctpchunk_listhead tmp;
9587
9588 /* setup the template */
9589 sctp_prepare_chunk(&template, stcb, srcv, strq, net);
9590
9591 /* Prepare the temp list */
9592 TAILQ_INIT(&tmp);
9593
9594 /* Template is complete, now time for the work */
9595 while (tot_out > 0) {
9596 /* Get a chunk */
9597 chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
9598 if (chk == NULL) {
9599 /*
9600 * ok we must spin through and dump anything
9601 * we have allocated and then jump to the
9602 * no_membad
9603 */
9604 error = ENOMEM;
9605 }
9606 sctppcbinfo.ipi_count_chunk++;
9607 asoc->chunks_on_out_queue++;
9608
9609 sctppcbinfo.ipi_gencnt_chunk++;
9610 *chk = template;
9611 chk->whoTo->ref_count++;
9612 MGETHDR(chk->data, M_WAIT, MT_DATA);
9613 if (chk->data == NULL) {
9614 error = ENOMEM;
9615 goto temp_clean_up;
9616 }
9617 tot_demand = min(tot_out, frag_size);
9618 error = sctp_copy_one(chk->data, uio, tot_demand , resv_in_first, &mbcnt_e);
9619 if (error)
9620 goto temp_clean_up;
9621 /* now fix the chk->send_size */
9622 chk->mbcnt = mbcnt_e;
9623 mbcnt += mbcnt_e;
9624 mbcnt_e = 0;
9625 chk->send_size = tot_demand;
9626 chk->data->m_pkthdr.len = tot_demand;
9627 chk->book_size = chk->send_size;
9628 if (chk->flags & SCTP_PR_SCTP_BUFFER) {
9629 asoc->sent_queue_cnt_removeable++;
9630 }
9631 TAILQ_INSERT_TAIL(&tmp, chk, sctp_next);
9632 tot_out -= tot_demand;
9633 }
9634 /* Now the tmp list holds all chunks and data */
9635 if ((srcv->sinfo_flags & MSG_UNORDERED) == 0) {
9636 /* bump the ssn if we are unordered. */
9637 strq->next_sequence_sent++;
9638 }
9639 /* Mark the first/last flags. This will
9640 * result int a 3 for a single item on the list
9641 */
9642 chk = TAILQ_FIRST(&tmp);
9643 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
9644 chk = TAILQ_LAST(&tmp, sctpchunk_listhead);
9645 chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
9646
9647 /* now move it to the streams actual queue */
9648 /* first stop protocol processing */
9649 mutex_enter(softnet_lock);
9650 if ((asoc->state == 0) ||
9651 (my_vtag != asoc->my_vtag) ||
9652 (so != inp->sctp_socket) ||
9653 (inp->sctp_socket == 0)) {
9654 /* connection was aborted */
9655 mutex_exit(softnet_lock);
9656 error = ECONNRESET;
9657 goto temp_clean_up;
9658 }
9659 chk = TAILQ_FIRST(&tmp);
9660 while (chk) {
9661 chk->data->m_nextpkt = 0;
9662 TAILQ_REMOVE(&tmp, chk, sctp_next);
9663 asoc->stream_queue_cnt++;
9664 TAILQ_INSERT_TAIL(&strq->outqueue, chk, sctp_next);
9665 chk = TAILQ_FIRST(&tmp);
9666 }
9667 /* now check if this stream is on the wheel */
9668 if ((strq->next_spoke.tqe_next == NULL) &&
9669 (strq->next_spoke.tqe_prev == NULL)) {
9670 /* Insert it on the wheel since it is not
9671 * on it currently
9672 */
9673 sctp_insert_on_wheel(asoc, strq);
9674 }
9675 /* Ok now we can allow pping */
9676 mutex_exit(softnet_lock);
9677 temp_clean_up:
9678 if (error) {
9679 chk = TAILQ_FIRST(&tmp);
9680 while (chk) {
9681 if (chk->data) {
9682 sctp_m_freem(chk->data);
9683 chk->data = NULL;
9684 }
9685 TAILQ_REMOVE(&tmp, chk, sctp_next);
9686 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
9687 sctppcbinfo.ipi_count_chunk--;
9688 asoc->chunks_on_out_queue--;
9689 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
9690 panic("Chunk count is negative");
9691 }
9692 sctppcbinfo.ipi_gencnt_chunk++;
9693 chk = TAILQ_FIRST(&tmp);
9694 }
9695 goto release;
9696 }
9697 }
9698 zap_by_it_now:
9699 #ifdef SCTP_MBCNT_LOGGING
9700 sctp_log_mbcnt(SCTP_LOG_MBCNT_INCREASE,
9701 asoc->total_output_queue_size,
9702 dataout,
9703 asoc->total_output_mbuf_queue_size,
9704 mbcnt);
9705 #endif
9706 solock(so);
9707 asoc->total_output_queue_size += dataout;
9708 asoc->total_output_mbuf_queue_size += mbcnt;
9709 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
9710 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
9711 so->so_snd.sb_cc += dataout;
9712 so->so_snd.sb_mbcnt += mbcnt;
9713 }
9714 if ((srcv->sinfo_flags & MSG_EOF) &&
9715 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)
9716 ) {
9717 int some_on_streamwheel = 0;
9718 error = 0;
9719 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
9720 /* Check to see if some data queued */
9721 struct sctp_stream_out *outs;
9722 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
9723 if (!TAILQ_EMPTY(&outs->outqueue)) {
9724 some_on_streamwheel = 1;
9725 break;
9726 }
9727 }
9728 }
9729 if (TAILQ_EMPTY(&asoc->send_queue) &&
9730 TAILQ_EMPTY(&asoc->sent_queue) &&
9731 (some_on_streamwheel == 0)) {
9732 /* there is nothing queued to send, so I'm done... */
9733 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
9734 (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
9735 /* only send SHUTDOWN the first time through */
9736 #ifdef SCTP_DEBUG
9737 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
9738 printf("%s:%d sends a shutdown\n",
9739 __FILE__,
9740 __LINE__
9741 );
9742 }
9743 #endif
9744 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
9745 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
9746 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
9747 asoc->primary_destination);
9748 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
9749 asoc->primary_destination);
9750 }
9751 } else {
9752 /*
9753 * we still got (or just got) data to send, so set
9754 * SHUTDOWN_PENDING
9755 */
9756 /*
9757 * XXX sockets draft says that MSG_EOF should be sent
9758 * with no data. currently, we will allow user data
9759 * to be sent first and move to SHUTDOWN-PENDING
9760 */
9761 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
9762 }
9763 }
9764 #ifdef SCTP_DEBUG
9765 if (sctp_debug_on & SCTP_DEBUG_OUTPUT2) {
9766 printf("++total out:%d total_mbuf_out:%d\n",
9767 (int)asoc->total_output_queue_size,
9768 (int)asoc->total_output_mbuf_queue_size);
9769 }
9770 #endif
9771
9772 release:
9773 sbunlock(&so->so_snd);
9774 out_locked:
9775 sounlock(so);
9776
9777 if (mm)
9778 sctp_m_freem(mm);
9779 return (error);
9780 }
9781
9782
9783 int
9784 sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
9785 struct mbuf *top, struct mbuf *control, int flags, struct lwp *p)
9786 {
9787 int error, use_rcvinfo;
9788 int queue_only = 0, queue_only_for_init=0;
9789 int un_sent = 0;
9790 int now_filled=0;
9791 struct sctp_inpcb *inp;
9792 struct sctp_tcb *stcb=NULL;
9793 struct sctp_sndrcvinfo srcv;
9794 struct timeval now;
9795 struct sctp_nets *net;
9796 struct sctp_association *asoc;
9797 struct sctp_inpcb *t_inp;
9798 int create_lock_applied = 0;
9799
9800 error = use_rcvinfo = 0;
9801 net = NULL;
9802 stcb = NULL;
9803 asoc = NULL;
9804 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
9805
9806 solock(so);
9807 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
9808 (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING)) {
9809 /* The listner can NOT send */
9810 error = EFAULT;
9811 sounlock(so);
9812 goto out;
9813 }
9814 if (addr) {
9815 SCTP_ASOC_CREATE_LOCK(inp);
9816 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
9817 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
9818 /* Should I really unlock ? */
9819 error = EFAULT;
9820 sounlock(so);
9821 goto out;
9822
9823 }
9824 create_lock_applied = 1;
9825 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
9826 (addr->sa_family == AF_INET6)) {
9827 error = EINVAL;
9828 sounlock(so);
9829 goto out;
9830 }
9831 }
9832 /* now we must find the assoc */
9833 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
9834 SCTP_INP_RLOCK(inp);
9835 stcb = LIST_FIRST(&inp->sctp_asoc_list);
9836 if (stcb == NULL) {
9837 SCTP_INP_RUNLOCK(inp);
9838 error = ENOTCONN;
9839 sounlock(so);
9840 goto out;
9841 }
9842 SCTP_TCB_LOCK(stcb);
9843 SCTP_INP_RUNLOCK(inp);
9844 net = stcb->asoc.primary_destination;
9845 }
9846 #ifdef SCTP_DEBUG
9847 printf("sctp_sosend: get control\n");
9848 #endif
9849 /* get control */
9850 if (control) {
9851 /* process cmsg snd/rcv info (maybe a assoc-id) */
9852 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
9853 sizeof(srcv))) {
9854 /* got one */
9855 if (srcv.sinfo_flags & MSG_SENDALL) {
9856 /* its a sendall */
9857 sctppcbinfo.mbuf_track--;
9858 sctp_m_freem(control);
9859
9860 if (create_lock_applied) {
9861 SCTP_ASOC_CREATE_UNLOCK(inp);
9862 create_lock_applied = 0;
9863 }
9864 return (sctp_sendall(inp, uio, top, &srcv));
9865 }
9866 use_rcvinfo = 1;
9867 }
9868 }
9869 #ifdef SCTP_DEBUG
9870 printf("sctp_sosend: doing lookup\n");
9871 #endif
9872 if (stcb == NULL) {
9873 /* Need to do a lookup */
9874 if (use_rcvinfo && srcv.sinfo_assoc_id) {
9875 stcb = sctp_findassociation_ep_asocid(inp, srcv.sinfo_assoc_id);
9876 /*
9877 * Question: Should I error here if the assoc_id is
9878 * no longer valid? i.e. I can't find it?
9879 */
9880 if ((stcb) &&
9881 (addr != NULL)) {
9882 /* Must locate the net structure */
9883 net = sctp_findnet(stcb, addr);
9884 }
9885 }
9886 if (stcb == NULL) {
9887 if (addr != NULL) {
9888 /* Since we did not use findep we must
9889 * increment it, and if we don't find a
9890 * tcb decrement it.
9891 */
9892 SCTP_INP_WLOCK(inp);
9893 SCTP_INP_INCR_REF(inp);
9894 SCTP_INP_WUNLOCK(inp);
9895 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
9896 if (stcb == NULL) {
9897 SCTP_INP_WLOCK(inp);
9898 SCTP_INP_DECR_REF(inp);
9899 SCTP_INP_WUNLOCK(inp);
9900 }
9901 }
9902 }
9903 }
9904 if ((stcb == NULL) &&
9905 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
9906 error = ENOTCONN;
9907 sounlock(so);
9908 goto out;
9909 } else if ((stcb == NULL) && (addr == NULL)) {
9910 error = ENOENT;
9911 sounlock(so);
9912 goto out;
9913 } else if (stcb == NULL) {
9914 /* UDP style, we must go ahead and start the INIT process */
9915 if ((use_rcvinfo) &&
9916 (srcv.sinfo_flags & MSG_ABORT)) {
9917 /* User asks to abort a non-existant asoc */
9918 error = ENOENT;
9919 sounlock(so);
9920 goto out;
9921 }
9922 /* get an asoc/stcb struct */
9923 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
9924 if (stcb == NULL) {
9925 /* Error is setup for us in the call */
9926 sounlock(so);
9927 goto out;
9928 }
9929 if (create_lock_applied) {
9930 SCTP_ASOC_CREATE_UNLOCK(inp);
9931 create_lock_applied = 0;
9932 } else {
9933 printf("Huh-3? create lock should have been on??\n");
9934 }
9935 /* Turn on queue only flag to prevent data from being sent */
9936 queue_only = 1;
9937 asoc = &stcb->asoc;
9938 asoc->state = SCTP_STATE_COOKIE_WAIT;
9939 SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
9940 if (control) {
9941 /* see if a init structure exists in cmsg headers */
9942 struct sctp_initmsg initm;
9943 int i;
9944 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control, sizeof(initm))) {
9945 /* we have an INIT override of the default */
9946 if (initm.sinit_max_attempts)
9947 asoc->max_init_times = initm.sinit_max_attempts;
9948 if (initm.sinit_num_ostreams)
9949 asoc->pre_open_streams = initm.sinit_num_ostreams;
9950 if (initm.sinit_max_instreams)
9951 asoc->max_inbound_streams = initm.sinit_max_instreams;
9952 if (initm.sinit_max_init_timeo)
9953 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
9954 if (asoc->streamoutcnt < asoc->pre_open_streams) {
9955 /* Default is NOT correct */
9956 #ifdef SCTP_DEBUG
9957 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
9958 printf("Ok, defout:%d pre_open:%d\n",
9959 asoc->streamoutcnt, asoc->pre_open_streams);
9960 }
9961 #endif
9962 free(asoc->strmout, M_PCB);
9963 asoc->strmout = NULL;
9964 asoc->streamoutcnt = asoc->pre_open_streams;
9965
9966 /* What happesn if this fails? .. we panic ...*/
9967 asoc->strmout = malloc(
9968 asoc->streamoutcnt *
9969 sizeof(struct sctp_stream_out),
9970 M_PCB, M_WAIT);
9971 for (i = 0; i < asoc->streamoutcnt; i++) {
9972 /*
9973 * inbound side must be set to 0xffff,
9974 * also NOTE when we get the INIT-ACK
9975 * back (for INIT sender) we MUST
9976 * reduce the count (streamoutcnt) but
9977 * first check if we sent to any of the
9978 * upper streams that were dropped (if
9979 * some were). Those that were dropped
9980 * must be notified to the upper layer
9981 * as failed to send.
9982 */
9983 asoc->strmout[i].next_sequence_sent = 0x0;
9984 TAILQ_INIT(&asoc->strmout[i].outqueue);
9985 asoc->strmout[i].stream_no = i;
9986 asoc->strmout[i].next_spoke.tqe_next = 0;
9987 asoc->strmout[i].next_spoke.tqe_prev = 0;
9988 }
9989 }
9990 }
9991
9992 }
9993 /* out with the INIT */
9994 queue_only_for_init = 1;
9995 sctp_send_initiate(inp, stcb);
9996 /*
9997 * we may want to dig in after this call and adjust the MTU
9998 * value. It defaulted to 1500 (constant) but the ro structure
9999 * may now have an update and thus we may need to change it
10000 * BEFORE we append the message.
10001 */
10002 net = stcb->asoc.primary_destination;
10003 asoc = &stcb->asoc;
10004 } else {
10005 asoc = &stcb->asoc;
10006 }
10007 if (create_lock_applied) {
10008 SCTP_ASOC_CREATE_UNLOCK(inp);
10009 create_lock_applied = 0;
10010 }
10011 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
10012 (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
10013 queue_only = 1;
10014 }
10015 if (use_rcvinfo == 0) {
10016 /* Grab the default stuff from the asoc */
10017 srcv = stcb->asoc.def_send;
10018 }
10019 /* we are now done with all control */
10020 if (control) {
10021 sctp_m_freem(control);
10022 control = NULL;
10023 }
10024
10025 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
10026 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
10027 (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
10028 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
10029 if ((use_rcvinfo) &&
10030 (srcv.sinfo_flags & MSG_ABORT)) {
10031 ;
10032 } else {
10033 error = ECONNRESET;
10034 sounlock(so);
10035 goto out;
10036 }
10037 }
10038 /* Ok, we will attempt a msgsnd :> */
10039 #if 0 /* XXX */
10040 if (p)
10041 p->p_stats->p_ru.ru_msgsnd++;
10042 #endif
10043
10044 if (stcb) {
10045 if (net && ((srcv.sinfo_flags & MSG_ADDR_OVER))) {
10046 /* we take the override or the unconfirmed */
10047 ;
10048 } else {
10049 net = stcb->asoc.primary_destination;
10050 }
10051 }
10052
10053 #ifdef SCTP_DEBUG
10054 printf("sctp_sosend: before copying in %p\n", top);
10055 #endif
10056 if (top == NULL) {
10057 /* Must copy it all in from user land. The
10058 * socket buf is locked but we don't suspend
10059 * protocol processing until we are ready to
10060 * send/queue it.
10061 */
10062 sounlock(so);
10063 #ifdef SCTP_DEBUG
10064 printf("sctp_sosend: before cii\n");
10065 #endif
10066 error = sctp_copy_it_in(inp, stcb, asoc, net, &srcv, uio, flags);
10067 #ifdef SCTP_DEBUG
10068 printf("sctp_sosend: after cii\n");
10069 #endif
10070 if (error)
10071 goto out;
10072 } else {
10073 /* Here we must either pull in the user data to chunk
10074 * buffers, or use top to do a msg_append.
10075 */
10076 error = sctp_msg_append(stcb, net, top, &srcv, flags);
10077 sounlock(so);
10078 if (error)
10079 goto out;
10080 /* zap the top since it is now being used */
10081 top = 0;
10082 }
10083 #ifdef SCTP_DEBUG
10084 printf("sctp_sosend: after copying in\n");
10085 #endif
10086 if (net->flight_size > net->cwnd) {
10087 sctp_pegs[SCTP_SENDTO_FULL_CWND]++;
10088 queue_only = 1;
10089
10090 } else if (asoc->ifp_had_enobuf) {
10091 sctp_pegs[SCTP_QUEONLY_BURSTLMT]++;
10092 queue_only = 1;
10093 } else {
10094 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10095 ((stcb->asoc.chunks_on_out_queue - stcb->asoc.total_flight_count) * sizeof(struct sctp_data_chunk)) +
10096 SCTP_MED_OVERHEAD);
10097
10098 if (((inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY) == 0) &&
10099 (stcb->asoc.total_flight > 0) &&
10100 (un_sent < (int)stcb->asoc.smallest_mtu)) {
10101
10102 /* Ok, Nagle is set on and we have data outstanding. Don't
10103 * send anything and let SACKs drive out the data unless we
10104 * have a "full" segment to send.
10105 */
10106 sctp_pegs[SCTP_NAGLE_NOQ]++;
10107 queue_only = 1;
10108 } else {
10109 sctp_pegs[SCTP_NAGLE_OFF]++;
10110 }
10111 }
10112 if (queue_only_for_init) {
10113 /* It is possible to have a turn around of the
10114 * INIT/INIT-ACK/COOKIE before I have a chance to
10115 * copy in the data. In such a case I DO want to
10116 * send it out by reversing the queue only flag.
10117 */
10118 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) ||
10119 (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_ECHOED)) {
10120 /* yep, reverse it */
10121 queue_only = 0;
10122 }
10123 }
10124
10125 #ifdef SCTP_DEBUG
10126 printf("sctp_sosend: before sending chunk\n");
10127 #endif
10128 if ((queue_only == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
10129 /* we can attempt to send too.*/
10130 #ifdef SCTP_DEBUG
10131 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10132 printf("USR Send calls sctp_chunk_output\n");
10133 }
10134 #endif
10135 solock(so);
10136 sctp_pegs[SCTP_OUTPUT_FRM_SND]++;
10137 sctp_chunk_output(inp, stcb, 0);
10138 sounlock(so);
10139 } else if ((queue_only == 0) &&
10140 (stcb->asoc.peers_rwnd == 0) &&
10141 (stcb->asoc.total_flight == 0)) {
10142 /* We get to have a probe outstanding */
10143 solock(so);
10144 sctp_from_user_send = 1;
10145 sctp_chunk_output(inp, stcb, 0);
10146 sctp_from_user_send = 0;
10147 sounlock(so);
10148
10149 } else if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
10150 int num_out, reason, cwnd_full;
10151 /* Here we do control only */
10152 solock(so);
10153 sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
10154 &reason, 1, &cwnd_full, 1, &now, &now_filled);
10155 sounlock(so);
10156 }
10157 #ifdef SCTP_DEBUG
10158 if (sctp_debug_on & SCTP_DEBUG_OUTPUT1) {
10159 printf("USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d \n",
10160 queue_only, stcb->asoc.peers_rwnd, un_sent,
10161 stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
10162 stcb->asoc.total_output_queue_size);
10163 }
10164 #endif
10165 out:
10166 if (create_lock_applied) {
10167 SCTP_ASOC_CREATE_UNLOCK(inp);
10168 create_lock_applied = 0;
10169 }
10170 if (stcb) {
10171 SCTP_TCB_UNLOCK(stcb);
10172 }
10173 if (top)
10174 sctp_m_freem(top);
10175 if (control)
10176 sctp_m_freem(control);
10177 return (error);
10178 }
10179