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