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