xref: /freebsd/sys/netinet/sctp_output.c (revision 5a0bba9007c527b18db7f9b64f06b486cda4fe9d)
1 /*-
2  * Copyright (c) 2001-2008, by 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 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_crc32.h>
53 #include <netinet/udp.h>
54 #include <machine/in_cksum.h>
55 
56 
57 
58 #define SCTP_MAX_GAPS_INARRAY 4
59 struct sack_track {
60 	uint8_t right_edge;	/* mergable on the right edge */
61 	uint8_t left_edge;	/* mergable on the left edge */
62 	uint8_t num_entries;
63 	uint8_t spare;
64 	struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
65 };
66 
67 struct sack_track sack_array[256] = {
68 	{0, 0, 0, 0,		/* 0x00 */
69 		{{0, 0},
70 		{0, 0},
71 		{0, 0},
72 		{0, 0}
73 		}
74 	},
75 	{1, 0, 1, 0,		/* 0x01 */
76 		{{0, 0},
77 		{0, 0},
78 		{0, 0},
79 		{0, 0}
80 		}
81 	},
82 	{0, 0, 1, 0,		/* 0x02 */
83 		{{1, 1},
84 		{0, 0},
85 		{0, 0},
86 		{0, 0}
87 		}
88 	},
89 	{1, 0, 1, 0,		/* 0x03 */
90 		{{0, 1},
91 		{0, 0},
92 		{0, 0},
93 		{0, 0}
94 		}
95 	},
96 	{0, 0, 1, 0,		/* 0x04 */
97 		{{2, 2},
98 		{0, 0},
99 		{0, 0},
100 		{0, 0}
101 		}
102 	},
103 	{1, 0, 2, 0,		/* 0x05 */
104 		{{0, 0},
105 		{2, 2},
106 		{0, 0},
107 		{0, 0}
108 		}
109 	},
110 	{0, 0, 1, 0,		/* 0x06 */
111 		{{1, 2},
112 		{0, 0},
113 		{0, 0},
114 		{0, 0}
115 		}
116 	},
117 	{1, 0, 1, 0,		/* 0x07 */
118 		{{0, 2},
119 		{0, 0},
120 		{0, 0},
121 		{0, 0}
122 		}
123 	},
124 	{0, 0, 1, 0,		/* 0x08 */
125 		{{3, 3},
126 		{0, 0},
127 		{0, 0},
128 		{0, 0}
129 		}
130 	},
131 	{1, 0, 2, 0,		/* 0x09 */
132 		{{0, 0},
133 		{3, 3},
134 		{0, 0},
135 		{0, 0}
136 		}
137 	},
138 	{0, 0, 2, 0,		/* 0x0a */
139 		{{1, 1},
140 		{3, 3},
141 		{0, 0},
142 		{0, 0}
143 		}
144 	},
145 	{1, 0, 2, 0,		/* 0x0b */
146 		{{0, 1},
147 		{3, 3},
148 		{0, 0},
149 		{0, 0}
150 		}
151 	},
152 	{0, 0, 1, 0,		/* 0x0c */
153 		{{2, 3},
154 		{0, 0},
155 		{0, 0},
156 		{0, 0}
157 		}
158 	},
159 	{1, 0, 2, 0,		/* 0x0d */
160 		{{0, 0},
161 		{2, 3},
162 		{0, 0},
163 		{0, 0}
164 		}
165 	},
166 	{0, 0, 1, 0,		/* 0x0e */
167 		{{1, 3},
168 		{0, 0},
169 		{0, 0},
170 		{0, 0}
171 		}
172 	},
173 	{1, 0, 1, 0,		/* 0x0f */
174 		{{0, 3},
175 		{0, 0},
176 		{0, 0},
177 		{0, 0}
178 		}
179 	},
180 	{0, 0, 1, 0,		/* 0x10 */
181 		{{4, 4},
182 		{0, 0},
183 		{0, 0},
184 		{0, 0}
185 		}
186 	},
187 	{1, 0, 2, 0,		/* 0x11 */
188 		{{0, 0},
189 		{4, 4},
190 		{0, 0},
191 		{0, 0}
192 		}
193 	},
194 	{0, 0, 2, 0,		/* 0x12 */
195 		{{1, 1},
196 		{4, 4},
197 		{0, 0},
198 		{0, 0}
199 		}
200 	},
201 	{1, 0, 2, 0,		/* 0x13 */
202 		{{0, 1},
203 		{4, 4},
204 		{0, 0},
205 		{0, 0}
206 		}
207 	},
208 	{0, 0, 2, 0,		/* 0x14 */
209 		{{2, 2},
210 		{4, 4},
211 		{0, 0},
212 		{0, 0}
213 		}
214 	},
215 	{1, 0, 3, 0,		/* 0x15 */
216 		{{0, 0},
217 		{2, 2},
218 		{4, 4},
219 		{0, 0}
220 		}
221 	},
222 	{0, 0, 2, 0,		/* 0x16 */
223 		{{1, 2},
224 		{4, 4},
225 		{0, 0},
226 		{0, 0}
227 		}
228 	},
229 	{1, 0, 2, 0,		/* 0x17 */
230 		{{0, 2},
231 		{4, 4},
232 		{0, 0},
233 		{0, 0}
234 		}
235 	},
236 	{0, 0, 1, 0,		/* 0x18 */
237 		{{3, 4},
238 		{0, 0},
239 		{0, 0},
240 		{0, 0}
241 		}
242 	},
243 	{1, 0, 2, 0,		/* 0x19 */
244 		{{0, 0},
245 		{3, 4},
246 		{0, 0},
247 		{0, 0}
248 		}
249 	},
250 	{0, 0, 2, 0,		/* 0x1a */
251 		{{1, 1},
252 		{3, 4},
253 		{0, 0},
254 		{0, 0}
255 		}
256 	},
257 	{1, 0, 2, 0,		/* 0x1b */
258 		{{0, 1},
259 		{3, 4},
260 		{0, 0},
261 		{0, 0}
262 		}
263 	},
264 	{0, 0, 1, 0,		/* 0x1c */
265 		{{2, 4},
266 		{0, 0},
267 		{0, 0},
268 		{0, 0}
269 		}
270 	},
271 	{1, 0, 2, 0,		/* 0x1d */
272 		{{0, 0},
273 		{2, 4},
274 		{0, 0},
275 		{0, 0}
276 		}
277 	},
278 	{0, 0, 1, 0,		/* 0x1e */
279 		{{1, 4},
280 		{0, 0},
281 		{0, 0},
282 		{0, 0}
283 		}
284 	},
285 	{1, 0, 1, 0,		/* 0x1f */
286 		{{0, 4},
287 		{0, 0},
288 		{0, 0},
289 		{0, 0}
290 		}
291 	},
292 	{0, 0, 1, 0,		/* 0x20 */
293 		{{5, 5},
294 		{0, 0},
295 		{0, 0},
296 		{0, 0}
297 		}
298 	},
299 	{1, 0, 2, 0,		/* 0x21 */
300 		{{0, 0},
301 		{5, 5},
302 		{0, 0},
303 		{0, 0}
304 		}
305 	},
306 	{0, 0, 2, 0,		/* 0x22 */
307 		{{1, 1},
308 		{5, 5},
309 		{0, 0},
310 		{0, 0}
311 		}
312 	},
313 	{1, 0, 2, 0,		/* 0x23 */
314 		{{0, 1},
315 		{5, 5},
316 		{0, 0},
317 		{0, 0}
318 		}
319 	},
320 	{0, 0, 2, 0,		/* 0x24 */
321 		{{2, 2},
322 		{5, 5},
323 		{0, 0},
324 		{0, 0}
325 		}
326 	},
327 	{1, 0, 3, 0,		/* 0x25 */
328 		{{0, 0},
329 		{2, 2},
330 		{5, 5},
331 		{0, 0}
332 		}
333 	},
334 	{0, 0, 2, 0,		/* 0x26 */
335 		{{1, 2},
336 		{5, 5},
337 		{0, 0},
338 		{0, 0}
339 		}
340 	},
341 	{1, 0, 2, 0,		/* 0x27 */
342 		{{0, 2},
343 		{5, 5},
344 		{0, 0},
345 		{0, 0}
346 		}
347 	},
348 	{0, 0, 2, 0,		/* 0x28 */
349 		{{3, 3},
350 		{5, 5},
351 		{0, 0},
352 		{0, 0}
353 		}
354 	},
355 	{1, 0, 3, 0,		/* 0x29 */
356 		{{0, 0},
357 		{3, 3},
358 		{5, 5},
359 		{0, 0}
360 		}
361 	},
362 	{0, 0, 3, 0,		/* 0x2a */
363 		{{1, 1},
364 		{3, 3},
365 		{5, 5},
366 		{0, 0}
367 		}
368 	},
369 	{1, 0, 3, 0,		/* 0x2b */
370 		{{0, 1},
371 		{3, 3},
372 		{5, 5},
373 		{0, 0}
374 		}
375 	},
376 	{0, 0, 2, 0,		/* 0x2c */
377 		{{2, 3},
378 		{5, 5},
379 		{0, 0},
380 		{0, 0}
381 		}
382 	},
383 	{1, 0, 3, 0,		/* 0x2d */
384 		{{0, 0},
385 		{2, 3},
386 		{5, 5},
387 		{0, 0}
388 		}
389 	},
390 	{0, 0, 2, 0,		/* 0x2e */
391 		{{1, 3},
392 		{5, 5},
393 		{0, 0},
394 		{0, 0}
395 		}
396 	},
397 	{1, 0, 2, 0,		/* 0x2f */
398 		{{0, 3},
399 		{5, 5},
400 		{0, 0},
401 		{0, 0}
402 		}
403 	},
404 	{0, 0, 1, 0,		/* 0x30 */
405 		{{4, 5},
406 		{0, 0},
407 		{0, 0},
408 		{0, 0}
409 		}
410 	},
411 	{1, 0, 2, 0,		/* 0x31 */
412 		{{0, 0},
413 		{4, 5},
414 		{0, 0},
415 		{0, 0}
416 		}
417 	},
418 	{0, 0, 2, 0,		/* 0x32 */
419 		{{1, 1},
420 		{4, 5},
421 		{0, 0},
422 		{0, 0}
423 		}
424 	},
425 	{1, 0, 2, 0,		/* 0x33 */
426 		{{0, 1},
427 		{4, 5},
428 		{0, 0},
429 		{0, 0}
430 		}
431 	},
432 	{0, 0, 2, 0,		/* 0x34 */
433 		{{2, 2},
434 		{4, 5},
435 		{0, 0},
436 		{0, 0}
437 		}
438 	},
439 	{1, 0, 3, 0,		/* 0x35 */
440 		{{0, 0},
441 		{2, 2},
442 		{4, 5},
443 		{0, 0}
444 		}
445 	},
446 	{0, 0, 2, 0,		/* 0x36 */
447 		{{1, 2},
448 		{4, 5},
449 		{0, 0},
450 		{0, 0}
451 		}
452 	},
453 	{1, 0, 2, 0,		/* 0x37 */
454 		{{0, 2},
455 		{4, 5},
456 		{0, 0},
457 		{0, 0}
458 		}
459 	},
460 	{0, 0, 1, 0,		/* 0x38 */
461 		{{3, 5},
462 		{0, 0},
463 		{0, 0},
464 		{0, 0}
465 		}
466 	},
467 	{1, 0, 2, 0,		/* 0x39 */
468 		{{0, 0},
469 		{3, 5},
470 		{0, 0},
471 		{0, 0}
472 		}
473 	},
474 	{0, 0, 2, 0,		/* 0x3a */
475 		{{1, 1},
476 		{3, 5},
477 		{0, 0},
478 		{0, 0}
479 		}
480 	},
481 	{1, 0, 2, 0,		/* 0x3b */
482 		{{0, 1},
483 		{3, 5},
484 		{0, 0},
485 		{0, 0}
486 		}
487 	},
488 	{0, 0, 1, 0,		/* 0x3c */
489 		{{2, 5},
490 		{0, 0},
491 		{0, 0},
492 		{0, 0}
493 		}
494 	},
495 	{1, 0, 2, 0,		/* 0x3d */
496 		{{0, 0},
497 		{2, 5},
498 		{0, 0},
499 		{0, 0}
500 		}
501 	},
502 	{0, 0, 1, 0,		/* 0x3e */
503 		{{1, 5},
504 		{0, 0},
505 		{0, 0},
506 		{0, 0}
507 		}
508 	},
509 	{1, 0, 1, 0,		/* 0x3f */
510 		{{0, 5},
511 		{0, 0},
512 		{0, 0},
513 		{0, 0}
514 		}
515 	},
516 	{0, 0, 1, 0,		/* 0x40 */
517 		{{6, 6},
518 		{0, 0},
519 		{0, 0},
520 		{0, 0}
521 		}
522 	},
523 	{1, 0, 2, 0,		/* 0x41 */
524 		{{0, 0},
525 		{6, 6},
526 		{0, 0},
527 		{0, 0}
528 		}
529 	},
530 	{0, 0, 2, 0,		/* 0x42 */
531 		{{1, 1},
532 		{6, 6},
533 		{0, 0},
534 		{0, 0}
535 		}
536 	},
537 	{1, 0, 2, 0,		/* 0x43 */
538 		{{0, 1},
539 		{6, 6},
540 		{0, 0},
541 		{0, 0}
542 		}
543 	},
544 	{0, 0, 2, 0,		/* 0x44 */
545 		{{2, 2},
546 		{6, 6},
547 		{0, 0},
548 		{0, 0}
549 		}
550 	},
551 	{1, 0, 3, 0,		/* 0x45 */
552 		{{0, 0},
553 		{2, 2},
554 		{6, 6},
555 		{0, 0}
556 		}
557 	},
558 	{0, 0, 2, 0,		/* 0x46 */
559 		{{1, 2},
560 		{6, 6},
561 		{0, 0},
562 		{0, 0}
563 		}
564 	},
565 	{1, 0, 2, 0,		/* 0x47 */
566 		{{0, 2},
567 		{6, 6},
568 		{0, 0},
569 		{0, 0}
570 		}
571 	},
572 	{0, 0, 2, 0,		/* 0x48 */
573 		{{3, 3},
574 		{6, 6},
575 		{0, 0},
576 		{0, 0}
577 		}
578 	},
579 	{1, 0, 3, 0,		/* 0x49 */
580 		{{0, 0},
581 		{3, 3},
582 		{6, 6},
583 		{0, 0}
584 		}
585 	},
586 	{0, 0, 3, 0,		/* 0x4a */
587 		{{1, 1},
588 		{3, 3},
589 		{6, 6},
590 		{0, 0}
591 		}
592 	},
593 	{1, 0, 3, 0,		/* 0x4b */
594 		{{0, 1},
595 		{3, 3},
596 		{6, 6},
597 		{0, 0}
598 		}
599 	},
600 	{0, 0, 2, 0,		/* 0x4c */
601 		{{2, 3},
602 		{6, 6},
603 		{0, 0},
604 		{0, 0}
605 		}
606 	},
607 	{1, 0, 3, 0,		/* 0x4d */
608 		{{0, 0},
609 		{2, 3},
610 		{6, 6},
611 		{0, 0}
612 		}
613 	},
614 	{0, 0, 2, 0,		/* 0x4e */
615 		{{1, 3},
616 		{6, 6},
617 		{0, 0},
618 		{0, 0}
619 		}
620 	},
621 	{1, 0, 2, 0,		/* 0x4f */
622 		{{0, 3},
623 		{6, 6},
624 		{0, 0},
625 		{0, 0}
626 		}
627 	},
628 	{0, 0, 2, 0,		/* 0x50 */
629 		{{4, 4},
630 		{6, 6},
631 		{0, 0},
632 		{0, 0}
633 		}
634 	},
635 	{1, 0, 3, 0,		/* 0x51 */
636 		{{0, 0},
637 		{4, 4},
638 		{6, 6},
639 		{0, 0}
640 		}
641 	},
642 	{0, 0, 3, 0,		/* 0x52 */
643 		{{1, 1},
644 		{4, 4},
645 		{6, 6},
646 		{0, 0}
647 		}
648 	},
649 	{1, 0, 3, 0,		/* 0x53 */
650 		{{0, 1},
651 		{4, 4},
652 		{6, 6},
653 		{0, 0}
654 		}
655 	},
656 	{0, 0, 3, 0,		/* 0x54 */
657 		{{2, 2},
658 		{4, 4},
659 		{6, 6},
660 		{0, 0}
661 		}
662 	},
663 	{1, 0, 4, 0,		/* 0x55 */
664 		{{0, 0},
665 		{2, 2},
666 		{4, 4},
667 		{6, 6}
668 		}
669 	},
670 	{0, 0, 3, 0,		/* 0x56 */
671 		{{1, 2},
672 		{4, 4},
673 		{6, 6},
674 		{0, 0}
675 		}
676 	},
677 	{1, 0, 3, 0,		/* 0x57 */
678 		{{0, 2},
679 		{4, 4},
680 		{6, 6},
681 		{0, 0}
682 		}
683 	},
684 	{0, 0, 2, 0,		/* 0x58 */
685 		{{3, 4},
686 		{6, 6},
687 		{0, 0},
688 		{0, 0}
689 		}
690 	},
691 	{1, 0, 3, 0,		/* 0x59 */
692 		{{0, 0},
693 		{3, 4},
694 		{6, 6},
695 		{0, 0}
696 		}
697 	},
698 	{0, 0, 3, 0,		/* 0x5a */
699 		{{1, 1},
700 		{3, 4},
701 		{6, 6},
702 		{0, 0}
703 		}
704 	},
705 	{1, 0, 3, 0,		/* 0x5b */
706 		{{0, 1},
707 		{3, 4},
708 		{6, 6},
709 		{0, 0}
710 		}
711 	},
712 	{0, 0, 2, 0,		/* 0x5c */
713 		{{2, 4},
714 		{6, 6},
715 		{0, 0},
716 		{0, 0}
717 		}
718 	},
719 	{1, 0, 3, 0,		/* 0x5d */
720 		{{0, 0},
721 		{2, 4},
722 		{6, 6},
723 		{0, 0}
724 		}
725 	},
726 	{0, 0, 2, 0,		/* 0x5e */
727 		{{1, 4},
728 		{6, 6},
729 		{0, 0},
730 		{0, 0}
731 		}
732 	},
733 	{1, 0, 2, 0,		/* 0x5f */
734 		{{0, 4},
735 		{6, 6},
736 		{0, 0},
737 		{0, 0}
738 		}
739 	},
740 	{0, 0, 1, 0,		/* 0x60 */
741 		{{5, 6},
742 		{0, 0},
743 		{0, 0},
744 		{0, 0}
745 		}
746 	},
747 	{1, 0, 2, 0,		/* 0x61 */
748 		{{0, 0},
749 		{5, 6},
750 		{0, 0},
751 		{0, 0}
752 		}
753 	},
754 	{0, 0, 2, 0,		/* 0x62 */
755 		{{1, 1},
756 		{5, 6},
757 		{0, 0},
758 		{0, 0}
759 		}
760 	},
761 	{1, 0, 2, 0,		/* 0x63 */
762 		{{0, 1},
763 		{5, 6},
764 		{0, 0},
765 		{0, 0}
766 		}
767 	},
768 	{0, 0, 2, 0,		/* 0x64 */
769 		{{2, 2},
770 		{5, 6},
771 		{0, 0},
772 		{0, 0}
773 		}
774 	},
775 	{1, 0, 3, 0,		/* 0x65 */
776 		{{0, 0},
777 		{2, 2},
778 		{5, 6},
779 		{0, 0}
780 		}
781 	},
782 	{0, 0, 2, 0,		/* 0x66 */
783 		{{1, 2},
784 		{5, 6},
785 		{0, 0},
786 		{0, 0}
787 		}
788 	},
789 	{1, 0, 2, 0,		/* 0x67 */
790 		{{0, 2},
791 		{5, 6},
792 		{0, 0},
793 		{0, 0}
794 		}
795 	},
796 	{0, 0, 2, 0,		/* 0x68 */
797 		{{3, 3},
798 		{5, 6},
799 		{0, 0},
800 		{0, 0}
801 		}
802 	},
803 	{1, 0, 3, 0,		/* 0x69 */
804 		{{0, 0},
805 		{3, 3},
806 		{5, 6},
807 		{0, 0}
808 		}
809 	},
810 	{0, 0, 3, 0,		/* 0x6a */
811 		{{1, 1},
812 		{3, 3},
813 		{5, 6},
814 		{0, 0}
815 		}
816 	},
817 	{1, 0, 3, 0,		/* 0x6b */
818 		{{0, 1},
819 		{3, 3},
820 		{5, 6},
821 		{0, 0}
822 		}
823 	},
824 	{0, 0, 2, 0,		/* 0x6c */
825 		{{2, 3},
826 		{5, 6},
827 		{0, 0},
828 		{0, 0}
829 		}
830 	},
831 	{1, 0, 3, 0,		/* 0x6d */
832 		{{0, 0},
833 		{2, 3},
834 		{5, 6},
835 		{0, 0}
836 		}
837 	},
838 	{0, 0, 2, 0,		/* 0x6e */
839 		{{1, 3},
840 		{5, 6},
841 		{0, 0},
842 		{0, 0}
843 		}
844 	},
845 	{1, 0, 2, 0,		/* 0x6f */
846 		{{0, 3},
847 		{5, 6},
848 		{0, 0},
849 		{0, 0}
850 		}
851 	},
852 	{0, 0, 1, 0,		/* 0x70 */
853 		{{4, 6},
854 		{0, 0},
855 		{0, 0},
856 		{0, 0}
857 		}
858 	},
859 	{1, 0, 2, 0,		/* 0x71 */
860 		{{0, 0},
861 		{4, 6},
862 		{0, 0},
863 		{0, 0}
864 		}
865 	},
866 	{0, 0, 2, 0,		/* 0x72 */
867 		{{1, 1},
868 		{4, 6},
869 		{0, 0},
870 		{0, 0}
871 		}
872 	},
873 	{1, 0, 2, 0,		/* 0x73 */
874 		{{0, 1},
875 		{4, 6},
876 		{0, 0},
877 		{0, 0}
878 		}
879 	},
880 	{0, 0, 2, 0,		/* 0x74 */
881 		{{2, 2},
882 		{4, 6},
883 		{0, 0},
884 		{0, 0}
885 		}
886 	},
887 	{1, 0, 3, 0,		/* 0x75 */
888 		{{0, 0},
889 		{2, 2},
890 		{4, 6},
891 		{0, 0}
892 		}
893 	},
894 	{0, 0, 2, 0,		/* 0x76 */
895 		{{1, 2},
896 		{4, 6},
897 		{0, 0},
898 		{0, 0}
899 		}
900 	},
901 	{1, 0, 2, 0,		/* 0x77 */
902 		{{0, 2},
903 		{4, 6},
904 		{0, 0},
905 		{0, 0}
906 		}
907 	},
908 	{0, 0, 1, 0,		/* 0x78 */
909 		{{3, 6},
910 		{0, 0},
911 		{0, 0},
912 		{0, 0}
913 		}
914 	},
915 	{1, 0, 2, 0,		/* 0x79 */
916 		{{0, 0},
917 		{3, 6},
918 		{0, 0},
919 		{0, 0}
920 		}
921 	},
922 	{0, 0, 2, 0,		/* 0x7a */
923 		{{1, 1},
924 		{3, 6},
925 		{0, 0},
926 		{0, 0}
927 		}
928 	},
929 	{1, 0, 2, 0,		/* 0x7b */
930 		{{0, 1},
931 		{3, 6},
932 		{0, 0},
933 		{0, 0}
934 		}
935 	},
936 	{0, 0, 1, 0,		/* 0x7c */
937 		{{2, 6},
938 		{0, 0},
939 		{0, 0},
940 		{0, 0}
941 		}
942 	},
943 	{1, 0, 2, 0,		/* 0x7d */
944 		{{0, 0},
945 		{2, 6},
946 		{0, 0},
947 		{0, 0}
948 		}
949 	},
950 	{0, 0, 1, 0,		/* 0x7e */
951 		{{1, 6},
952 		{0, 0},
953 		{0, 0},
954 		{0, 0}
955 		}
956 	},
957 	{1, 0, 1, 0,		/* 0x7f */
958 		{{0, 6},
959 		{0, 0},
960 		{0, 0},
961 		{0, 0}
962 		}
963 	},
964 	{0, 1, 1, 0,		/* 0x80 */
965 		{{7, 7},
966 		{0, 0},
967 		{0, 0},
968 		{0, 0}
969 		}
970 	},
971 	{1, 1, 2, 0,		/* 0x81 */
972 		{{0, 0},
973 		{7, 7},
974 		{0, 0},
975 		{0, 0}
976 		}
977 	},
978 	{0, 1, 2, 0,		/* 0x82 */
979 		{{1, 1},
980 		{7, 7},
981 		{0, 0},
982 		{0, 0}
983 		}
984 	},
985 	{1, 1, 2, 0,		/* 0x83 */
986 		{{0, 1},
987 		{7, 7},
988 		{0, 0},
989 		{0, 0}
990 		}
991 	},
992 	{0, 1, 2, 0,		/* 0x84 */
993 		{{2, 2},
994 		{7, 7},
995 		{0, 0},
996 		{0, 0}
997 		}
998 	},
999 	{1, 1, 3, 0,		/* 0x85 */
1000 		{{0, 0},
1001 		{2, 2},
1002 		{7, 7},
1003 		{0, 0}
1004 		}
1005 	},
1006 	{0, 1, 2, 0,		/* 0x86 */
1007 		{{1, 2},
1008 		{7, 7},
1009 		{0, 0},
1010 		{0, 0}
1011 		}
1012 	},
1013 	{1, 1, 2, 0,		/* 0x87 */
1014 		{{0, 2},
1015 		{7, 7},
1016 		{0, 0},
1017 		{0, 0}
1018 		}
1019 	},
1020 	{0, 1, 2, 0,		/* 0x88 */
1021 		{{3, 3},
1022 		{7, 7},
1023 		{0, 0},
1024 		{0, 0}
1025 		}
1026 	},
1027 	{1, 1, 3, 0,		/* 0x89 */
1028 		{{0, 0},
1029 		{3, 3},
1030 		{7, 7},
1031 		{0, 0}
1032 		}
1033 	},
1034 	{0, 1, 3, 0,		/* 0x8a */
1035 		{{1, 1},
1036 		{3, 3},
1037 		{7, 7},
1038 		{0, 0}
1039 		}
1040 	},
1041 	{1, 1, 3, 0,		/* 0x8b */
1042 		{{0, 1},
1043 		{3, 3},
1044 		{7, 7},
1045 		{0, 0}
1046 		}
1047 	},
1048 	{0, 1, 2, 0,		/* 0x8c */
1049 		{{2, 3},
1050 		{7, 7},
1051 		{0, 0},
1052 		{0, 0}
1053 		}
1054 	},
1055 	{1, 1, 3, 0,		/* 0x8d */
1056 		{{0, 0},
1057 		{2, 3},
1058 		{7, 7},
1059 		{0, 0}
1060 		}
1061 	},
1062 	{0, 1, 2, 0,		/* 0x8e */
1063 		{{1, 3},
1064 		{7, 7},
1065 		{0, 0},
1066 		{0, 0}
1067 		}
1068 	},
1069 	{1, 1, 2, 0,		/* 0x8f */
1070 		{{0, 3},
1071 		{7, 7},
1072 		{0, 0},
1073 		{0, 0}
1074 		}
1075 	},
1076 	{0, 1, 2, 0,		/* 0x90 */
1077 		{{4, 4},
1078 		{7, 7},
1079 		{0, 0},
1080 		{0, 0}
1081 		}
1082 	},
1083 	{1, 1, 3, 0,		/* 0x91 */
1084 		{{0, 0},
1085 		{4, 4},
1086 		{7, 7},
1087 		{0, 0}
1088 		}
1089 	},
1090 	{0, 1, 3, 0,		/* 0x92 */
1091 		{{1, 1},
1092 		{4, 4},
1093 		{7, 7},
1094 		{0, 0}
1095 		}
1096 	},
1097 	{1, 1, 3, 0,		/* 0x93 */
1098 		{{0, 1},
1099 		{4, 4},
1100 		{7, 7},
1101 		{0, 0}
1102 		}
1103 	},
1104 	{0, 1, 3, 0,		/* 0x94 */
1105 		{{2, 2},
1106 		{4, 4},
1107 		{7, 7},
1108 		{0, 0}
1109 		}
1110 	},
1111 	{1, 1, 4, 0,		/* 0x95 */
1112 		{{0, 0},
1113 		{2, 2},
1114 		{4, 4},
1115 		{7, 7}
1116 		}
1117 	},
1118 	{0, 1, 3, 0,		/* 0x96 */
1119 		{{1, 2},
1120 		{4, 4},
1121 		{7, 7},
1122 		{0, 0}
1123 		}
1124 	},
1125 	{1, 1, 3, 0,		/* 0x97 */
1126 		{{0, 2},
1127 		{4, 4},
1128 		{7, 7},
1129 		{0, 0}
1130 		}
1131 	},
1132 	{0, 1, 2, 0,		/* 0x98 */
1133 		{{3, 4},
1134 		{7, 7},
1135 		{0, 0},
1136 		{0, 0}
1137 		}
1138 	},
1139 	{1, 1, 3, 0,		/* 0x99 */
1140 		{{0, 0},
1141 		{3, 4},
1142 		{7, 7},
1143 		{0, 0}
1144 		}
1145 	},
1146 	{0, 1, 3, 0,		/* 0x9a */
1147 		{{1, 1},
1148 		{3, 4},
1149 		{7, 7},
1150 		{0, 0}
1151 		}
1152 	},
1153 	{1, 1, 3, 0,		/* 0x9b */
1154 		{{0, 1},
1155 		{3, 4},
1156 		{7, 7},
1157 		{0, 0}
1158 		}
1159 	},
1160 	{0, 1, 2, 0,		/* 0x9c */
1161 		{{2, 4},
1162 		{7, 7},
1163 		{0, 0},
1164 		{0, 0}
1165 		}
1166 	},
1167 	{1, 1, 3, 0,		/* 0x9d */
1168 		{{0, 0},
1169 		{2, 4},
1170 		{7, 7},
1171 		{0, 0}
1172 		}
1173 	},
1174 	{0, 1, 2, 0,		/* 0x9e */
1175 		{{1, 4},
1176 		{7, 7},
1177 		{0, 0},
1178 		{0, 0}
1179 		}
1180 	},
1181 	{1, 1, 2, 0,		/* 0x9f */
1182 		{{0, 4},
1183 		{7, 7},
1184 		{0, 0},
1185 		{0, 0}
1186 		}
1187 	},
1188 	{0, 1, 2, 0,		/* 0xa0 */
1189 		{{5, 5},
1190 		{7, 7},
1191 		{0, 0},
1192 		{0, 0}
1193 		}
1194 	},
1195 	{1, 1, 3, 0,		/* 0xa1 */
1196 		{{0, 0},
1197 		{5, 5},
1198 		{7, 7},
1199 		{0, 0}
1200 		}
1201 	},
1202 	{0, 1, 3, 0,		/* 0xa2 */
1203 		{{1, 1},
1204 		{5, 5},
1205 		{7, 7},
1206 		{0, 0}
1207 		}
1208 	},
1209 	{1, 1, 3, 0,		/* 0xa3 */
1210 		{{0, 1},
1211 		{5, 5},
1212 		{7, 7},
1213 		{0, 0}
1214 		}
1215 	},
1216 	{0, 1, 3, 0,		/* 0xa4 */
1217 		{{2, 2},
1218 		{5, 5},
1219 		{7, 7},
1220 		{0, 0}
1221 		}
1222 	},
1223 	{1, 1, 4, 0,		/* 0xa5 */
1224 		{{0, 0},
1225 		{2, 2},
1226 		{5, 5},
1227 		{7, 7}
1228 		}
1229 	},
1230 	{0, 1, 3, 0,		/* 0xa6 */
1231 		{{1, 2},
1232 		{5, 5},
1233 		{7, 7},
1234 		{0, 0}
1235 		}
1236 	},
1237 	{1, 1, 3, 0,		/* 0xa7 */
1238 		{{0, 2},
1239 		{5, 5},
1240 		{7, 7},
1241 		{0, 0}
1242 		}
1243 	},
1244 	{0, 1, 3, 0,		/* 0xa8 */
1245 		{{3, 3},
1246 		{5, 5},
1247 		{7, 7},
1248 		{0, 0}
1249 		}
1250 	},
1251 	{1, 1, 4, 0,		/* 0xa9 */
1252 		{{0, 0},
1253 		{3, 3},
1254 		{5, 5},
1255 		{7, 7}
1256 		}
1257 	},
1258 	{0, 1, 4, 0,		/* 0xaa */
1259 		{{1, 1},
1260 		{3, 3},
1261 		{5, 5},
1262 		{7, 7}
1263 		}
1264 	},
1265 	{1, 1, 4, 0,		/* 0xab */
1266 		{{0, 1},
1267 		{3, 3},
1268 		{5, 5},
1269 		{7, 7}
1270 		}
1271 	},
1272 	{0, 1, 3, 0,		/* 0xac */
1273 		{{2, 3},
1274 		{5, 5},
1275 		{7, 7},
1276 		{0, 0}
1277 		}
1278 	},
1279 	{1, 1, 4, 0,		/* 0xad */
1280 		{{0, 0},
1281 		{2, 3},
1282 		{5, 5},
1283 		{7, 7}
1284 		}
1285 	},
1286 	{0, 1, 3, 0,		/* 0xae */
1287 		{{1, 3},
1288 		{5, 5},
1289 		{7, 7},
1290 		{0, 0}
1291 		}
1292 	},
1293 	{1, 1, 3, 0,		/* 0xaf */
1294 		{{0, 3},
1295 		{5, 5},
1296 		{7, 7},
1297 		{0, 0}
1298 		}
1299 	},
1300 	{0, 1, 2, 0,		/* 0xb0 */
1301 		{{4, 5},
1302 		{7, 7},
1303 		{0, 0},
1304 		{0, 0}
1305 		}
1306 	},
1307 	{1, 1, 3, 0,		/* 0xb1 */
1308 		{{0, 0},
1309 		{4, 5},
1310 		{7, 7},
1311 		{0, 0}
1312 		}
1313 	},
1314 	{0, 1, 3, 0,		/* 0xb2 */
1315 		{{1, 1},
1316 		{4, 5},
1317 		{7, 7},
1318 		{0, 0}
1319 		}
1320 	},
1321 	{1, 1, 3, 0,		/* 0xb3 */
1322 		{{0, 1},
1323 		{4, 5},
1324 		{7, 7},
1325 		{0, 0}
1326 		}
1327 	},
1328 	{0, 1, 3, 0,		/* 0xb4 */
1329 		{{2, 2},
1330 		{4, 5},
1331 		{7, 7},
1332 		{0, 0}
1333 		}
1334 	},
1335 	{1, 1, 4, 0,		/* 0xb5 */
1336 		{{0, 0},
1337 		{2, 2},
1338 		{4, 5},
1339 		{7, 7}
1340 		}
1341 	},
1342 	{0, 1, 3, 0,		/* 0xb6 */
1343 		{{1, 2},
1344 		{4, 5},
1345 		{7, 7},
1346 		{0, 0}
1347 		}
1348 	},
1349 	{1, 1, 3, 0,		/* 0xb7 */
1350 		{{0, 2},
1351 		{4, 5},
1352 		{7, 7},
1353 		{0, 0}
1354 		}
1355 	},
1356 	{0, 1, 2, 0,		/* 0xb8 */
1357 		{{3, 5},
1358 		{7, 7},
1359 		{0, 0},
1360 		{0, 0}
1361 		}
1362 	},
1363 	{1, 1, 3, 0,		/* 0xb9 */
1364 		{{0, 0},
1365 		{3, 5},
1366 		{7, 7},
1367 		{0, 0}
1368 		}
1369 	},
1370 	{0, 1, 3, 0,		/* 0xba */
1371 		{{1, 1},
1372 		{3, 5},
1373 		{7, 7},
1374 		{0, 0}
1375 		}
1376 	},
1377 	{1, 1, 3, 0,		/* 0xbb */
1378 		{{0, 1},
1379 		{3, 5},
1380 		{7, 7},
1381 		{0, 0}
1382 		}
1383 	},
1384 	{0, 1, 2, 0,		/* 0xbc */
1385 		{{2, 5},
1386 		{7, 7},
1387 		{0, 0},
1388 		{0, 0}
1389 		}
1390 	},
1391 	{1, 1, 3, 0,		/* 0xbd */
1392 		{{0, 0},
1393 		{2, 5},
1394 		{7, 7},
1395 		{0, 0}
1396 		}
1397 	},
1398 	{0, 1, 2, 0,		/* 0xbe */
1399 		{{1, 5},
1400 		{7, 7},
1401 		{0, 0},
1402 		{0, 0}
1403 		}
1404 	},
1405 	{1, 1, 2, 0,		/* 0xbf */
1406 		{{0, 5},
1407 		{7, 7},
1408 		{0, 0},
1409 		{0, 0}
1410 		}
1411 	},
1412 	{0, 1, 1, 0,		/* 0xc0 */
1413 		{{6, 7},
1414 		{0, 0},
1415 		{0, 0},
1416 		{0, 0}
1417 		}
1418 	},
1419 	{1, 1, 2, 0,		/* 0xc1 */
1420 		{{0, 0},
1421 		{6, 7},
1422 		{0, 0},
1423 		{0, 0}
1424 		}
1425 	},
1426 	{0, 1, 2, 0,		/* 0xc2 */
1427 		{{1, 1},
1428 		{6, 7},
1429 		{0, 0},
1430 		{0, 0}
1431 		}
1432 	},
1433 	{1, 1, 2, 0,		/* 0xc3 */
1434 		{{0, 1},
1435 		{6, 7},
1436 		{0, 0},
1437 		{0, 0}
1438 		}
1439 	},
1440 	{0, 1, 2, 0,		/* 0xc4 */
1441 		{{2, 2},
1442 		{6, 7},
1443 		{0, 0},
1444 		{0, 0}
1445 		}
1446 	},
1447 	{1, 1, 3, 0,		/* 0xc5 */
1448 		{{0, 0},
1449 		{2, 2},
1450 		{6, 7},
1451 		{0, 0}
1452 		}
1453 	},
1454 	{0, 1, 2, 0,		/* 0xc6 */
1455 		{{1, 2},
1456 		{6, 7},
1457 		{0, 0},
1458 		{0, 0}
1459 		}
1460 	},
1461 	{1, 1, 2, 0,		/* 0xc7 */
1462 		{{0, 2},
1463 		{6, 7},
1464 		{0, 0},
1465 		{0, 0}
1466 		}
1467 	},
1468 	{0, 1, 2, 0,		/* 0xc8 */
1469 		{{3, 3},
1470 		{6, 7},
1471 		{0, 0},
1472 		{0, 0}
1473 		}
1474 	},
1475 	{1, 1, 3, 0,		/* 0xc9 */
1476 		{{0, 0},
1477 		{3, 3},
1478 		{6, 7},
1479 		{0, 0}
1480 		}
1481 	},
1482 	{0, 1, 3, 0,		/* 0xca */
1483 		{{1, 1},
1484 		{3, 3},
1485 		{6, 7},
1486 		{0, 0}
1487 		}
1488 	},
1489 	{1, 1, 3, 0,		/* 0xcb */
1490 		{{0, 1},
1491 		{3, 3},
1492 		{6, 7},
1493 		{0, 0}
1494 		}
1495 	},
1496 	{0, 1, 2, 0,		/* 0xcc */
1497 		{{2, 3},
1498 		{6, 7},
1499 		{0, 0},
1500 		{0, 0}
1501 		}
1502 	},
1503 	{1, 1, 3, 0,		/* 0xcd */
1504 		{{0, 0},
1505 		{2, 3},
1506 		{6, 7},
1507 		{0, 0}
1508 		}
1509 	},
1510 	{0, 1, 2, 0,		/* 0xce */
1511 		{{1, 3},
1512 		{6, 7},
1513 		{0, 0},
1514 		{0, 0}
1515 		}
1516 	},
1517 	{1, 1, 2, 0,		/* 0xcf */
1518 		{{0, 3},
1519 		{6, 7},
1520 		{0, 0},
1521 		{0, 0}
1522 		}
1523 	},
1524 	{0, 1, 2, 0,		/* 0xd0 */
1525 		{{4, 4},
1526 		{6, 7},
1527 		{0, 0},
1528 		{0, 0}
1529 		}
1530 	},
1531 	{1, 1, 3, 0,		/* 0xd1 */
1532 		{{0, 0},
1533 		{4, 4},
1534 		{6, 7},
1535 		{0, 0}
1536 		}
1537 	},
1538 	{0, 1, 3, 0,		/* 0xd2 */
1539 		{{1, 1},
1540 		{4, 4},
1541 		{6, 7},
1542 		{0, 0}
1543 		}
1544 	},
1545 	{1, 1, 3, 0,		/* 0xd3 */
1546 		{{0, 1},
1547 		{4, 4},
1548 		{6, 7},
1549 		{0, 0}
1550 		}
1551 	},
1552 	{0, 1, 3, 0,		/* 0xd4 */
1553 		{{2, 2},
1554 		{4, 4},
1555 		{6, 7},
1556 		{0, 0}
1557 		}
1558 	},
1559 	{1, 1, 4, 0,		/* 0xd5 */
1560 		{{0, 0},
1561 		{2, 2},
1562 		{4, 4},
1563 		{6, 7}
1564 		}
1565 	},
1566 	{0, 1, 3, 0,		/* 0xd6 */
1567 		{{1, 2},
1568 		{4, 4},
1569 		{6, 7},
1570 		{0, 0}
1571 		}
1572 	},
1573 	{1, 1, 3, 0,		/* 0xd7 */
1574 		{{0, 2},
1575 		{4, 4},
1576 		{6, 7},
1577 		{0, 0}
1578 		}
1579 	},
1580 	{0, 1, 2, 0,		/* 0xd8 */
1581 		{{3, 4},
1582 		{6, 7},
1583 		{0, 0},
1584 		{0, 0}
1585 		}
1586 	},
1587 	{1, 1, 3, 0,		/* 0xd9 */
1588 		{{0, 0},
1589 		{3, 4},
1590 		{6, 7},
1591 		{0, 0}
1592 		}
1593 	},
1594 	{0, 1, 3, 0,		/* 0xda */
1595 		{{1, 1},
1596 		{3, 4},
1597 		{6, 7},
1598 		{0, 0}
1599 		}
1600 	},
1601 	{1, 1, 3, 0,		/* 0xdb */
1602 		{{0, 1},
1603 		{3, 4},
1604 		{6, 7},
1605 		{0, 0}
1606 		}
1607 	},
1608 	{0, 1, 2, 0,		/* 0xdc */
1609 		{{2, 4},
1610 		{6, 7},
1611 		{0, 0},
1612 		{0, 0}
1613 		}
1614 	},
1615 	{1, 1, 3, 0,		/* 0xdd */
1616 		{{0, 0},
1617 		{2, 4},
1618 		{6, 7},
1619 		{0, 0}
1620 		}
1621 	},
1622 	{0, 1, 2, 0,		/* 0xde */
1623 		{{1, 4},
1624 		{6, 7},
1625 		{0, 0},
1626 		{0, 0}
1627 		}
1628 	},
1629 	{1, 1, 2, 0,		/* 0xdf */
1630 		{{0, 4},
1631 		{6, 7},
1632 		{0, 0},
1633 		{0, 0}
1634 		}
1635 	},
1636 	{0, 1, 1, 0,		/* 0xe0 */
1637 		{{5, 7},
1638 		{0, 0},
1639 		{0, 0},
1640 		{0, 0}
1641 		}
1642 	},
1643 	{1, 1, 2, 0,		/* 0xe1 */
1644 		{{0, 0},
1645 		{5, 7},
1646 		{0, 0},
1647 		{0, 0}
1648 		}
1649 	},
1650 	{0, 1, 2, 0,		/* 0xe2 */
1651 		{{1, 1},
1652 		{5, 7},
1653 		{0, 0},
1654 		{0, 0}
1655 		}
1656 	},
1657 	{1, 1, 2, 0,		/* 0xe3 */
1658 		{{0, 1},
1659 		{5, 7},
1660 		{0, 0},
1661 		{0, 0}
1662 		}
1663 	},
1664 	{0, 1, 2, 0,		/* 0xe4 */
1665 		{{2, 2},
1666 		{5, 7},
1667 		{0, 0},
1668 		{0, 0}
1669 		}
1670 	},
1671 	{1, 1, 3, 0,		/* 0xe5 */
1672 		{{0, 0},
1673 		{2, 2},
1674 		{5, 7},
1675 		{0, 0}
1676 		}
1677 	},
1678 	{0, 1, 2, 0,		/* 0xe6 */
1679 		{{1, 2},
1680 		{5, 7},
1681 		{0, 0},
1682 		{0, 0}
1683 		}
1684 	},
1685 	{1, 1, 2, 0,		/* 0xe7 */
1686 		{{0, 2},
1687 		{5, 7},
1688 		{0, 0},
1689 		{0, 0}
1690 		}
1691 	},
1692 	{0, 1, 2, 0,		/* 0xe8 */
1693 		{{3, 3},
1694 		{5, 7},
1695 		{0, 0},
1696 		{0, 0}
1697 		}
1698 	},
1699 	{1, 1, 3, 0,		/* 0xe9 */
1700 		{{0, 0},
1701 		{3, 3},
1702 		{5, 7},
1703 		{0, 0}
1704 		}
1705 	},
1706 	{0, 1, 3, 0,		/* 0xea */
1707 		{{1, 1},
1708 		{3, 3},
1709 		{5, 7},
1710 		{0, 0}
1711 		}
1712 	},
1713 	{1, 1, 3, 0,		/* 0xeb */
1714 		{{0, 1},
1715 		{3, 3},
1716 		{5, 7},
1717 		{0, 0}
1718 		}
1719 	},
1720 	{0, 1, 2, 0,		/* 0xec */
1721 		{{2, 3},
1722 		{5, 7},
1723 		{0, 0},
1724 		{0, 0}
1725 		}
1726 	},
1727 	{1, 1, 3, 0,		/* 0xed */
1728 		{{0, 0},
1729 		{2, 3},
1730 		{5, 7},
1731 		{0, 0}
1732 		}
1733 	},
1734 	{0, 1, 2, 0,		/* 0xee */
1735 		{{1, 3},
1736 		{5, 7},
1737 		{0, 0},
1738 		{0, 0}
1739 		}
1740 	},
1741 	{1, 1, 2, 0,		/* 0xef */
1742 		{{0, 3},
1743 		{5, 7},
1744 		{0, 0},
1745 		{0, 0}
1746 		}
1747 	},
1748 	{0, 1, 1, 0,		/* 0xf0 */
1749 		{{4, 7},
1750 		{0, 0},
1751 		{0, 0},
1752 		{0, 0}
1753 		}
1754 	},
1755 	{1, 1, 2, 0,		/* 0xf1 */
1756 		{{0, 0},
1757 		{4, 7},
1758 		{0, 0},
1759 		{0, 0}
1760 		}
1761 	},
1762 	{0, 1, 2, 0,		/* 0xf2 */
1763 		{{1, 1},
1764 		{4, 7},
1765 		{0, 0},
1766 		{0, 0}
1767 		}
1768 	},
1769 	{1, 1, 2, 0,		/* 0xf3 */
1770 		{{0, 1},
1771 		{4, 7},
1772 		{0, 0},
1773 		{0, 0}
1774 		}
1775 	},
1776 	{0, 1, 2, 0,		/* 0xf4 */
1777 		{{2, 2},
1778 		{4, 7},
1779 		{0, 0},
1780 		{0, 0}
1781 		}
1782 	},
1783 	{1, 1, 3, 0,		/* 0xf5 */
1784 		{{0, 0},
1785 		{2, 2},
1786 		{4, 7},
1787 		{0, 0}
1788 		}
1789 	},
1790 	{0, 1, 2, 0,		/* 0xf6 */
1791 		{{1, 2},
1792 		{4, 7},
1793 		{0, 0},
1794 		{0, 0}
1795 		}
1796 	},
1797 	{1, 1, 2, 0,		/* 0xf7 */
1798 		{{0, 2},
1799 		{4, 7},
1800 		{0, 0},
1801 		{0, 0}
1802 		}
1803 	},
1804 	{0, 1, 1, 0,		/* 0xf8 */
1805 		{{3, 7},
1806 		{0, 0},
1807 		{0, 0},
1808 		{0, 0}
1809 		}
1810 	},
1811 	{1, 1, 2, 0,		/* 0xf9 */
1812 		{{0, 0},
1813 		{3, 7},
1814 		{0, 0},
1815 		{0, 0}
1816 		}
1817 	},
1818 	{0, 1, 2, 0,		/* 0xfa */
1819 		{{1, 1},
1820 		{3, 7},
1821 		{0, 0},
1822 		{0, 0}
1823 		}
1824 	},
1825 	{1, 1, 2, 0,		/* 0xfb */
1826 		{{0, 1},
1827 		{3, 7},
1828 		{0, 0},
1829 		{0, 0}
1830 		}
1831 	},
1832 	{0, 1, 1, 0,		/* 0xfc */
1833 		{{2, 7},
1834 		{0, 0},
1835 		{0, 0},
1836 		{0, 0}
1837 		}
1838 	},
1839 	{1, 1, 2, 0,		/* 0xfd */
1840 		{{0, 0},
1841 		{2, 7},
1842 		{0, 0},
1843 		{0, 0}
1844 		}
1845 	},
1846 	{0, 1, 1, 0,		/* 0xfe */
1847 		{{1, 7},
1848 		{0, 0},
1849 		{0, 0},
1850 		{0, 0}
1851 		}
1852 	},
1853 	{1, 1, 1, 0,		/* 0xff */
1854 		{{0, 7},
1855 		{0, 0},
1856 		{0, 0},
1857 		{0, 0}
1858 		}
1859 	}
1860 };
1861 
1862 
1863 int
1864 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1865     int ipv4_addr_legal,
1866     int ipv6_addr_legal,
1867     int loopback_scope,
1868     int ipv4_local_scope,
1869     int local_scope,
1870     int site_scope,
1871     int do_update)
1872 {
1873 	if ((loopback_scope == 0) &&
1874 	    (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1875 		/*
1876 		 * skip loopback if not in scope *
1877 		 */
1878 		return (0);
1879 	}
1880 	switch (ifa->address.sa.sa_family) {
1881 	case AF_INET:
1882 		if (ipv4_addr_legal) {
1883 			struct sockaddr_in *sin;
1884 
1885 			sin = (struct sockaddr_in *)&ifa->address.sin;
1886 			if (sin->sin_addr.s_addr == 0) {
1887 				/* not in scope , unspecified */
1888 				return (0);
1889 			}
1890 			if ((ipv4_local_scope == 0) &&
1891 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1892 				/* private address not in scope */
1893 				return (0);
1894 			}
1895 		} else {
1896 			return (0);
1897 		}
1898 		break;
1899 #ifdef INET6
1900 	case AF_INET6:
1901 		if (ipv6_addr_legal) {
1902 			struct sockaddr_in6 *sin6;
1903 
1904 			/*
1905 			 * Must update the flags,  bummer, which means any
1906 			 * IFA locks must now be applied HERE <->
1907 			 */
1908 			if (do_update) {
1909 				sctp_gather_internal_ifa_flags(ifa);
1910 			}
1911 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1912 				return (0);
1913 			}
1914 			/* ok to use deprecated addresses? */
1915 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1916 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1917 				/* skip unspecifed addresses */
1918 				return (0);
1919 			}
1920 			if (	/* (local_scope == 0) && */
1921 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1922 				return (0);
1923 			}
1924 			if ((site_scope == 0) &&
1925 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1926 				return (0);
1927 			}
1928 		} else {
1929 			return (0);
1930 		}
1931 		break;
1932 #endif
1933 	default:
1934 		return (0);
1935 	}
1936 	return (1);
1937 }
1938 
1939 static struct mbuf *
1940 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1941 {
1942 	struct sctp_paramhdr *parmh;
1943 	struct mbuf *mret;
1944 	int len;
1945 
1946 	if (ifa->address.sa.sa_family == AF_INET) {
1947 		len = sizeof(struct sctp_ipv4addr_param);
1948 	} else if (ifa->address.sa.sa_family == AF_INET6) {
1949 		len = sizeof(struct sctp_ipv6addr_param);
1950 	} else {
1951 		/* unknown type */
1952 		return (m);
1953 	}
1954 	if (M_TRAILINGSPACE(m) >= len) {
1955 		/* easy side we just drop it on the end */
1956 		parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1957 		mret = m;
1958 	} else {
1959 		/* Need more space */
1960 		mret = m;
1961 		while (SCTP_BUF_NEXT(mret) != NULL) {
1962 			mret = SCTP_BUF_NEXT(mret);
1963 		}
1964 		SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1965 		if (SCTP_BUF_NEXT(mret) == NULL) {
1966 			/* We are hosed, can't add more addresses */
1967 			return (m);
1968 		}
1969 		mret = SCTP_BUF_NEXT(mret);
1970 		parmh = mtod(mret, struct sctp_paramhdr *);
1971 	}
1972 	/* now add the parameter */
1973 	switch (ifa->address.sa.sa_family) {
1974 	case AF_INET:
1975 		{
1976 			struct sctp_ipv4addr_param *ipv4p;
1977 			struct sockaddr_in *sin;
1978 
1979 			sin = (struct sockaddr_in *)&ifa->address.sin;
1980 			ipv4p = (struct sctp_ipv4addr_param *)parmh;
1981 			parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1982 			parmh->param_length = htons(len);
1983 			ipv4p->addr = sin->sin_addr.s_addr;
1984 			SCTP_BUF_LEN(mret) += len;
1985 			break;
1986 		}
1987 #ifdef INET6
1988 	case AF_INET6:
1989 		{
1990 			struct sctp_ipv6addr_param *ipv6p;
1991 			struct sockaddr_in6 *sin6;
1992 
1993 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1994 			ipv6p = (struct sctp_ipv6addr_param *)parmh;
1995 			parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1996 			parmh->param_length = htons(len);
1997 			memcpy(ipv6p->addr, &sin6->sin6_addr,
1998 			    sizeof(ipv6p->addr));
1999 			/* clear embedded scope in the address */
2000 			in6_clearscope((struct in6_addr *)ipv6p->addr);
2001 			SCTP_BUF_LEN(mret) += len;
2002 			break;
2003 		}
2004 #endif
2005 	default:
2006 		return (m);
2007 	}
2008 	return (mret);
2009 }
2010 
2011 
2012 struct mbuf *
2013 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2014     struct mbuf *m_at, int cnt_inits_to)
2015 {
2016 	struct sctp_vrf *vrf = NULL;
2017 	int cnt, limit_out = 0, total_count;
2018 	uint32_t vrf_id;
2019 
2020 	vrf_id = inp->def_vrf_id;
2021 	SCTP_IPI_ADDR_RLOCK();
2022 	vrf = sctp_find_vrf(vrf_id);
2023 	if (vrf == NULL) {
2024 		SCTP_IPI_ADDR_RUNLOCK();
2025 		return (m_at);
2026 	}
2027 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2028 		struct sctp_ifa *sctp_ifap;
2029 		struct sctp_ifn *sctp_ifnp;
2030 
2031 		cnt = cnt_inits_to;
2032 		if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2033 			limit_out = 1;
2034 			cnt = SCTP_ADDRESS_LIMIT;
2035 			goto skip_count;
2036 		}
2037 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2038 			if ((scope->loopback_scope == 0) &&
2039 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2040 				/*
2041 				 * Skip loopback devices if loopback_scope
2042 				 * not set
2043 				 */
2044 				continue;
2045 			}
2046 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2047 				if (sctp_is_address_in_scope(sctp_ifap,
2048 				    scope->ipv4_addr_legal,
2049 				    scope->ipv6_addr_legal,
2050 				    scope->loopback_scope,
2051 				    scope->ipv4_local_scope,
2052 				    scope->local_scope,
2053 				    scope->site_scope, 1) == 0) {
2054 					continue;
2055 				}
2056 				cnt++;
2057 				if (cnt > SCTP_ADDRESS_LIMIT) {
2058 					break;
2059 				}
2060 			}
2061 			if (cnt > SCTP_ADDRESS_LIMIT) {
2062 				break;
2063 			}
2064 		}
2065 skip_count:
2066 		if (cnt > 1) {
2067 			total_count = 0;
2068 			LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2069 				cnt = 0;
2070 				if ((scope->loopback_scope == 0) &&
2071 				    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2072 					/*
2073 					 * Skip loopback devices if
2074 					 * loopback_scope not set
2075 					 */
2076 					continue;
2077 				}
2078 				LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2079 					if (sctp_is_address_in_scope(sctp_ifap,
2080 					    scope->ipv4_addr_legal,
2081 					    scope->ipv6_addr_legal,
2082 					    scope->loopback_scope,
2083 					    scope->ipv4_local_scope,
2084 					    scope->local_scope,
2085 					    scope->site_scope, 0) == 0) {
2086 						continue;
2087 					}
2088 					m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2089 					if (limit_out) {
2090 						cnt++;
2091 						total_count++;
2092 						if (cnt >= 2) {
2093 							/*
2094 							 * two from each
2095 							 * address
2096 							 */
2097 							break;
2098 						}
2099 						if (total_count > SCTP_ADDRESS_LIMIT) {
2100 							/* No more addresses */
2101 							break;
2102 						}
2103 					}
2104 				}
2105 			}
2106 		}
2107 	} else {
2108 		struct sctp_laddr *laddr;
2109 
2110 		cnt = cnt_inits_to;
2111 		/* First, how many ? */
2112 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2113 			if (laddr->ifa == NULL) {
2114 				continue;
2115 			}
2116 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2117 				/*
2118 				 * Address being deleted by the system, dont
2119 				 * list.
2120 				 */
2121 				continue;
2122 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2123 				/*
2124 				 * Address being deleted on this ep don't
2125 				 * list.
2126 				 */
2127 				continue;
2128 			}
2129 			if (sctp_is_address_in_scope(laddr->ifa,
2130 			    scope->ipv4_addr_legal,
2131 			    scope->ipv6_addr_legal,
2132 			    scope->loopback_scope,
2133 			    scope->ipv4_local_scope,
2134 			    scope->local_scope,
2135 			    scope->site_scope, 1) == 0) {
2136 				continue;
2137 			}
2138 			cnt++;
2139 		}
2140 		if (cnt > SCTP_ADDRESS_LIMIT) {
2141 			limit_out = 1;
2142 		}
2143 		/*
2144 		 * To get through a NAT we only list addresses if we have
2145 		 * more than one. That way if you just bind a single address
2146 		 * we let the source of the init dictate our address.
2147 		 */
2148 		if (cnt > 1) {
2149 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2150 				cnt = 0;
2151 				if (laddr->ifa == NULL) {
2152 					continue;
2153 				}
2154 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2155 					continue;
2156 
2157 				if (sctp_is_address_in_scope(laddr->ifa,
2158 				    scope->ipv4_addr_legal,
2159 				    scope->ipv6_addr_legal,
2160 				    scope->loopback_scope,
2161 				    scope->ipv4_local_scope,
2162 				    scope->local_scope,
2163 				    scope->site_scope, 0) == 0) {
2164 					continue;
2165 				}
2166 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2167 				cnt++;
2168 				if (cnt >= SCTP_ADDRESS_LIMIT) {
2169 					break;
2170 				}
2171 			}
2172 		}
2173 	}
2174 	SCTP_IPI_ADDR_RUNLOCK();
2175 	return (m_at);
2176 }
2177 
2178 static struct sctp_ifa *
2179 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2180     uint8_t dest_is_loop,
2181     uint8_t dest_is_priv,
2182     sa_family_t fam)
2183 {
2184 	uint8_t dest_is_global = 0;
2185 
2186 	/* dest_is_priv is true if destination is a private address */
2187 	/* dest_is_loop is true if destination is a loopback addresses */
2188 
2189 	/**
2190 	 * Here we determine if its a preferred address. A preferred address
2191 	 * means it is the same scope or higher scope then the destination.
2192 	 * L = loopback, P = private, G = global
2193 	 * -----------------------------------------
2194          *    src    |  dest | result
2195          *  ----------------------------------------
2196          *     L     |    L  |    yes
2197          *  -----------------------------------------
2198          *     P     |    L  |    yes-v4 no-v6
2199          *  -----------------------------------------
2200          *     G     |    L  |    yes-v4 no-v6
2201          *  -----------------------------------------
2202          *     L     |    P  |    no
2203          *  -----------------------------------------
2204          *     P     |    P  |    yes
2205          *  -----------------------------------------
2206          *     G     |    P  |    no
2207          *   -----------------------------------------
2208          *     L     |    G  |    no
2209          *   -----------------------------------------
2210          *     P     |    G  |    no
2211          *    -----------------------------------------
2212          *     G     |    G  |    yes
2213          *    -----------------------------------------
2214 	 */
2215 
2216 	if (ifa->address.sa.sa_family != fam) {
2217 		/* forget mis-matched family */
2218 		return (NULL);
2219 	}
2220 	if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2221 		dest_is_global = 1;
2222 	}
2223 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2224 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2225 	/* Ok the address may be ok */
2226 	if (fam == AF_INET6) {
2227 		/* ok to use deprecated addresses? no lets not! */
2228 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2229 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2230 			return (NULL);
2231 		}
2232 		if (ifa->src_is_priv && !ifa->src_is_loop) {
2233 			if (dest_is_loop) {
2234 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2235 				return (NULL);
2236 			}
2237 		}
2238 		if (ifa->src_is_glob) {
2239 			if (dest_is_loop) {
2240 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2241 				return (NULL);
2242 			}
2243 		}
2244 	}
2245 	/*
2246 	 * Now that we know what is what, implement or table this could in
2247 	 * theory be done slicker (it used to be), but this is
2248 	 * straightforward and easier to validate :-)
2249 	 */
2250 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2251 	    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2252 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2253 	    dest_is_loop, dest_is_priv, dest_is_global);
2254 
2255 	if ((ifa->src_is_loop) && (dest_is_priv)) {
2256 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2257 		return (NULL);
2258 	}
2259 	if ((ifa->src_is_glob) && (dest_is_priv)) {
2260 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2261 		return (NULL);
2262 	}
2263 	if ((ifa->src_is_loop) && (dest_is_global)) {
2264 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2265 		return (NULL);
2266 	}
2267 	if ((ifa->src_is_priv) && (dest_is_global)) {
2268 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2269 		return (NULL);
2270 	}
2271 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2272 	/* its a preferred address */
2273 	return (ifa);
2274 }
2275 
2276 static struct sctp_ifa *
2277 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2278     uint8_t dest_is_loop,
2279     uint8_t dest_is_priv,
2280     sa_family_t fam)
2281 {
2282 	uint8_t dest_is_global = 0;
2283 
2284 	/*
2285 	 * Here we determine if its a acceptable address. A acceptable
2286 	 * address means it is the same scope or higher scope but we can
2287 	 * allow for NAT which means its ok to have a global dest and a
2288 	 * private src.
2289 	 *
2290 	 * L = loopback, P = private, G = global
2291 	 * ----------------------------------------- src    |  dest | result
2292 	 * ----------------------------------------- L     |   L   |    yes
2293 	 * ----------------------------------------- P     |   L   |
2294 	 * yes-v4 no-v6 ----------------------------------------- G     |
2295 	 * L   |    yes ----------------------------------------- L     |
2296 	 * P   |    no ----------------------------------------- P     |   P
2297 	 * |    yes ----------------------------------------- G     |   P
2298 	 * |    yes - May not work -----------------------------------------
2299 	 * L     |   G   |    no ----------------------------------------- P
2300 	 * |   G   |    yes - May not work
2301 	 * ----------------------------------------- G     |   G   |    yes
2302 	 * -----------------------------------------
2303 	 */
2304 
2305 	if (ifa->address.sa.sa_family != fam) {
2306 		/* forget non matching family */
2307 		return (NULL);
2308 	}
2309 	/* Ok the address may be ok */
2310 	if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2311 		dest_is_global = 1;
2312 	}
2313 	if (fam == AF_INET6) {
2314 		/* ok to use deprecated addresses? */
2315 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2316 			return (NULL);
2317 		}
2318 		if (ifa->src_is_priv) {
2319 			/* Special case, linklocal to loop */
2320 			if (dest_is_loop)
2321 				return (NULL);
2322 		}
2323 	}
2324 	/*
2325 	 * Now that we know what is what, implement our table. This could in
2326 	 * theory be done slicker (it used to be), but this is
2327 	 * straightforward and easier to validate :-)
2328 	 */
2329 	if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2330 		return (NULL);
2331 	}
2332 	if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2333 		return (NULL);
2334 	}
2335 	/* its an acceptable address */
2336 	return (ifa);
2337 }
2338 
2339 int
2340 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2341 {
2342 	struct sctp_laddr *laddr;
2343 
2344 	if (stcb == NULL) {
2345 		/* There are no restrictions, no TCB :-) */
2346 		return (0);
2347 	}
2348 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2349 		if (laddr->ifa == NULL) {
2350 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2351 			    __FUNCTION__);
2352 			continue;
2353 		}
2354 		if (laddr->ifa == ifa) {
2355 			/* Yes it is on the list */
2356 			return (1);
2357 		}
2358 	}
2359 	return (0);
2360 }
2361 
2362 
2363 int
2364 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2365 {
2366 	struct sctp_laddr *laddr;
2367 
2368 	if (ifa == NULL)
2369 		return (0);
2370 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2371 		if (laddr->ifa == NULL) {
2372 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2373 			    __FUNCTION__);
2374 			continue;
2375 		}
2376 		if ((laddr->ifa == ifa) && laddr->action == 0)
2377 			/* same pointer */
2378 			return (1);
2379 	}
2380 	return (0);
2381 }
2382 
2383 
2384 
2385 static struct sctp_ifa *
2386 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2387     sctp_route_t * ro,
2388     uint32_t vrf_id,
2389     int non_asoc_addr_ok,
2390     uint8_t dest_is_priv,
2391     uint8_t dest_is_loop,
2392     sa_family_t fam)
2393 {
2394 	struct sctp_laddr *laddr, *starting_point;
2395 	void *ifn;
2396 	int resettotop = 0;
2397 	struct sctp_ifn *sctp_ifn;
2398 	struct sctp_ifa *sctp_ifa, *sifa;
2399 	struct sctp_vrf *vrf;
2400 	uint32_t ifn_index;
2401 
2402 	vrf = sctp_find_vrf(vrf_id);
2403 	if (vrf == NULL)
2404 		return (NULL);
2405 
2406 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2407 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2408 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2409 	/*
2410 	 * first question, is the ifn we will emit on in our list, if so, we
2411 	 * want such an address. Note that we first looked for a preferred
2412 	 * address.
2413 	 */
2414 	if (sctp_ifn) {
2415 		/* is a preferred one on the interface we route out? */
2416 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2417 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2418 			    (non_asoc_addr_ok == 0))
2419 				continue;
2420 			sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2421 			    dest_is_loop,
2422 			    dest_is_priv, fam);
2423 			if (sifa == NULL)
2424 				continue;
2425 			if (sctp_is_addr_in_ep(inp, sifa)) {
2426 				atomic_add_int(&sifa->refcount, 1);
2427 				return (sifa);
2428 			}
2429 		}
2430 	}
2431 	/*
2432 	 * ok, now we now need to find one on the list of the addresses. We
2433 	 * can't get one on the emitting interface so let's find first a
2434 	 * preferred one. If not that an acceptable one otherwise... we
2435 	 * return NULL.
2436 	 */
2437 	starting_point = inp->next_addr_touse;
2438 once_again:
2439 	if (inp->next_addr_touse == NULL) {
2440 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2441 		resettotop = 1;
2442 	}
2443 	for (laddr = inp->next_addr_touse; laddr;
2444 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2445 		if (laddr->ifa == NULL) {
2446 			/* address has been removed */
2447 			continue;
2448 		}
2449 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2450 			/* address is being deleted */
2451 			continue;
2452 		}
2453 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2454 		    dest_is_priv, fam);
2455 		if (sifa == NULL)
2456 			continue;
2457 		atomic_add_int(&sifa->refcount, 1);
2458 		return (sifa);
2459 	}
2460 	if (resettotop == 0) {
2461 		inp->next_addr_touse = NULL;
2462 		goto once_again;
2463 	}
2464 	inp->next_addr_touse = starting_point;
2465 	resettotop = 0;
2466 once_again_too:
2467 	if (inp->next_addr_touse == NULL) {
2468 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2469 		resettotop = 1;
2470 	}
2471 	/* ok, what about an acceptable address in the inp */
2472 	for (laddr = inp->next_addr_touse; laddr;
2473 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2474 		if (laddr->ifa == NULL) {
2475 			/* address has been removed */
2476 			continue;
2477 		}
2478 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2479 			/* address is being deleted */
2480 			continue;
2481 		}
2482 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2483 		    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(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 				    (non_asoc_addr_ok &&
2550 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2551 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2552 					/* on the no-no list */
2553 					continue;
2554 				}
2555 				atomic_add_int(&sifa->refcount, 1);
2556 				return (sifa);
2557 			}
2558 		}
2559 		/* next try for an acceptable address on the ep */
2560 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2561 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2562 				continue;
2563 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2564 				sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2565 				if (sifa == NULL)
2566 					continue;
2567 				if (((non_asoc_addr_ok == 0) &&
2568 				    (sctp_is_addr_restricted(stcb, sifa))) ||
2569 				    (non_asoc_addr_ok &&
2570 				    (sctp_is_addr_restricted(stcb, sifa)) &&
2571 				    (!sctp_is_addr_pending(stcb, sifa)))) {
2572 					/* on the no-no list */
2573 					continue;
2574 				}
2575 				atomic_add_int(&sifa->refcount, 1);
2576 				return (sifa);
2577 			}
2578 		}
2579 
2580 	}
2581 	/*
2582 	 * if we can't find one like that then we must look at all addresses
2583 	 * bound to pick one at first preferable then secondly acceptable.
2584 	 */
2585 	starting_point = stcb->asoc.last_used_address;
2586 sctp_from_the_top:
2587 	if (stcb->asoc.last_used_address == NULL) {
2588 		start_at_beginning = 1;
2589 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2590 	}
2591 	/* search beginning with the last used address */
2592 	for (laddr = stcb->asoc.last_used_address; laddr;
2593 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2594 		if (laddr->ifa == NULL) {
2595 			/* address has been removed */
2596 			continue;
2597 		}
2598 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2599 			/* address is being deleted */
2600 			continue;
2601 		}
2602 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2603 		if (sifa == NULL)
2604 			continue;
2605 		if (((non_asoc_addr_ok == 0) &&
2606 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2607 		    (non_asoc_addr_ok &&
2608 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2609 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2610 			/* on the no-no list */
2611 			continue;
2612 		}
2613 		stcb->asoc.last_used_address = laddr;
2614 		atomic_add_int(&sifa->refcount, 1);
2615 		return (sifa);
2616 	}
2617 	if (start_at_beginning == 0) {
2618 		stcb->asoc.last_used_address = NULL;
2619 		goto sctp_from_the_top;
2620 	}
2621 	/* now try for any higher scope than the destination */
2622 	stcb->asoc.last_used_address = starting_point;
2623 	start_at_beginning = 0;
2624 sctp_from_the_top2:
2625 	if (stcb->asoc.last_used_address == NULL) {
2626 		start_at_beginning = 1;
2627 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2628 	}
2629 	/* search beginning with the last used address */
2630 	for (laddr = stcb->asoc.last_used_address; laddr;
2631 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2632 		if (laddr->ifa == NULL) {
2633 			/* address has been removed */
2634 			continue;
2635 		}
2636 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2637 			/* address is being deleted */
2638 			continue;
2639 		}
2640 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2641 		    dest_is_priv, fam);
2642 		if (sifa == NULL)
2643 			continue;
2644 		if (((non_asoc_addr_ok == 0) &&
2645 		    (sctp_is_addr_restricted(stcb, sifa))) ||
2646 		    (non_asoc_addr_ok &&
2647 		    (sctp_is_addr_restricted(stcb, sifa)) &&
2648 		    (!sctp_is_addr_pending(stcb, sifa)))) {
2649 			/* on the no-no list */
2650 			continue;
2651 		}
2652 		stcb->asoc.last_used_address = laddr;
2653 		atomic_add_int(&sifa->refcount, 1);
2654 		return (sifa);
2655 	}
2656 	if (start_at_beginning == 0) {
2657 		stcb->asoc.last_used_address = NULL;
2658 		goto sctp_from_the_top2;
2659 	}
2660 	return (NULL);
2661 }
2662 
2663 static struct sctp_ifa *
2664 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2665     struct sctp_tcb *stcb,
2666     int non_asoc_addr_ok,
2667     uint8_t dest_is_loop,
2668     uint8_t dest_is_priv,
2669     int addr_wanted,
2670     sa_family_t fam,
2671     sctp_route_t * ro
2672 )
2673 {
2674 	struct sctp_ifa *ifa, *sifa;
2675 	int num_eligible_addr = 0;
2676 
2677 #ifdef INET6
2678 	struct sockaddr_in6 sin6, lsa6;
2679 
2680 	if (fam == AF_INET6) {
2681 		memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2682 		(void)sa6_recoverscope(&sin6);
2683 	}
2684 #endif				/* INET6 */
2685 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2686 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2687 		    (non_asoc_addr_ok == 0))
2688 			continue;
2689 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2690 		    dest_is_priv, fam);
2691 		if (sifa == NULL)
2692 			continue;
2693 #ifdef INET6
2694 		if (fam == AF_INET6 &&
2695 		    dest_is_loop &&
2696 		    sifa->src_is_loop && sifa->src_is_priv) {
2697 			/*
2698 			 * don't allow fe80::1 to be a src on loop ::1, we
2699 			 * don't list it to the peer so we will get an
2700 			 * abort.
2701 			 */
2702 			continue;
2703 		}
2704 		if (fam == AF_INET6 &&
2705 		    IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2706 		    IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2707 			/*
2708 			 * link-local <-> link-local must belong to the same
2709 			 * scope.
2710 			 */
2711 			memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2712 			(void)sa6_recoverscope(&lsa6);
2713 			if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2714 				continue;
2715 			}
2716 		}
2717 #endif				/* INET6 */
2718 
2719 		/*
2720 		 * Check if the IPv6 address matches to next-hop. In the
2721 		 * mobile case, old IPv6 address may be not deleted from the
2722 		 * interface. Then, the interface has previous and new
2723 		 * addresses.  We should use one corresponding to the
2724 		 * next-hop.  (by micchie)
2725 		 */
2726 #ifdef INET6
2727 		if (stcb && fam == AF_INET6 &&
2728 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2729 			if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2730 			    == 0) {
2731 				continue;
2732 			}
2733 		}
2734 #endif
2735 		/* Avoid topologically incorrect IPv4 address */
2736 		if (stcb && fam == AF_INET &&
2737 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2738 			if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2739 				continue;
2740 			}
2741 		}
2742 		if (stcb) {
2743 			if (sctp_is_address_in_scope(ifa,
2744 			    stcb->asoc.ipv4_addr_legal,
2745 			    stcb->asoc.ipv6_addr_legal,
2746 			    stcb->asoc.loopback_scope,
2747 			    stcb->asoc.ipv4_local_scope,
2748 			    stcb->asoc.local_scope,
2749 			    stcb->asoc.site_scope, 0) == 0) {
2750 				continue;
2751 			}
2752 			if (((non_asoc_addr_ok == 0) &&
2753 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2754 			    (non_asoc_addr_ok &&
2755 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2756 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2757 				/*
2758 				 * It is restricted for some reason..
2759 				 * probably not yet added.
2760 				 */
2761 				continue;
2762 			}
2763 		}
2764 		if (num_eligible_addr >= addr_wanted) {
2765 			return (sifa);
2766 		}
2767 		num_eligible_addr++;
2768 	}
2769 	return (NULL);
2770 }
2771 
2772 
2773 static int
2774 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2775     struct sctp_tcb *stcb,
2776     int non_asoc_addr_ok,
2777     uint8_t dest_is_loop,
2778     uint8_t dest_is_priv,
2779     sa_family_t fam)
2780 {
2781 	struct sctp_ifa *ifa, *sifa;
2782 	int num_eligible_addr = 0;
2783 
2784 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2785 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2786 		    (non_asoc_addr_ok == 0)) {
2787 			continue;
2788 		}
2789 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2790 		    dest_is_priv, fam);
2791 		if (sifa == NULL) {
2792 			continue;
2793 		}
2794 		if (stcb) {
2795 			if (sctp_is_address_in_scope(ifa,
2796 			    stcb->asoc.ipv4_addr_legal,
2797 			    stcb->asoc.ipv6_addr_legal,
2798 			    stcb->asoc.loopback_scope,
2799 			    stcb->asoc.ipv4_local_scope,
2800 			    stcb->asoc.local_scope,
2801 			    stcb->asoc.site_scope, 0) == 0) {
2802 				continue;
2803 			}
2804 			if (((non_asoc_addr_ok == 0) &&
2805 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2806 			    (non_asoc_addr_ok &&
2807 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2808 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2809 				/*
2810 				 * It is restricted for some reason..
2811 				 * probably not yet added.
2812 				 */
2813 				continue;
2814 			}
2815 		}
2816 		num_eligible_addr++;
2817 	}
2818 	return (num_eligible_addr);
2819 }
2820 
2821 static struct sctp_ifa *
2822 sctp_choose_boundall(struct sctp_inpcb *inp,
2823     struct sctp_tcb *stcb,
2824     struct sctp_nets *net,
2825     sctp_route_t * ro,
2826     uint32_t vrf_id,
2827     uint8_t dest_is_priv,
2828     uint8_t dest_is_loop,
2829     int non_asoc_addr_ok,
2830     sa_family_t fam)
2831 {
2832 	int cur_addr_num = 0, num_preferred = 0;
2833 	void *ifn;
2834 	struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2835 	struct sctp_ifa *sctp_ifa, *sifa;
2836 	uint32_t ifn_index;
2837 	struct sctp_vrf *vrf;
2838 
2839 	/*-
2840 	 * For boundall we can use any address in the association.
2841 	 * If non_asoc_addr_ok is set we can use any address (at least in
2842 	 * theory). So we look for preferred addresses first. If we find one,
2843 	 * we use it. Otherwise we next try to get an address on the
2844 	 * interface, which we should be able to do (unless non_asoc_addr_ok
2845 	 * is false and we are routed out that way). In these cases where we
2846 	 * can't use the address of the interface we go through all the
2847 	 * ifn's looking for an address we can use and fill that in. Punting
2848 	 * means we send back address 0, which will probably cause problems
2849 	 * actually since then IP will fill in the address of the route ifn,
2850 	 * which means we probably already rejected it.. i.e. here comes an
2851 	 * abort :-<.
2852 	 */
2853 	vrf = sctp_find_vrf(vrf_id);
2854 	if (vrf == NULL)
2855 		return (NULL);
2856 
2857 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2858 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2859 	emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2860 	if (sctp_ifn == NULL) {
2861 		/* ?? We don't have this guy ?? */
2862 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2863 		goto bound_all_plan_b;
2864 	}
2865 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2866 	    ifn_index, sctp_ifn->ifn_name);
2867 
2868 	if (net) {
2869 		cur_addr_num = net->indx_of_eligible_next_to_use;
2870 	}
2871 	num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2872 	    stcb,
2873 	    non_asoc_addr_ok,
2874 	    dest_is_loop,
2875 	    dest_is_priv, fam);
2876 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2877 	    num_preferred, sctp_ifn->ifn_name);
2878 	if (num_preferred == 0) {
2879 		/*
2880 		 * no eligible addresses, we must use some other interface
2881 		 * address if we can find one.
2882 		 */
2883 		goto bound_all_plan_b;
2884 	}
2885 	/*
2886 	 * Ok we have num_eligible_addr set with how many we can use, this
2887 	 * may vary from call to call due to addresses being deprecated
2888 	 * etc..
2889 	 */
2890 	if (cur_addr_num >= num_preferred) {
2891 		cur_addr_num = 0;
2892 	}
2893 	/*
2894 	 * select the nth address from the list (where cur_addr_num is the
2895 	 * nth) and 0 is the first one, 1 is the second one etc...
2896 	 */
2897 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2898 
2899 	sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2900 	    dest_is_priv, cur_addr_num, fam, ro);
2901 
2902 	/* if sctp_ifa is NULL something changed??, fall to plan b. */
2903 	if (sctp_ifa) {
2904 		atomic_add_int(&sctp_ifa->refcount, 1);
2905 		if (net) {
2906 			/* save off where the next one we will want */
2907 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2908 		}
2909 		return (sctp_ifa);
2910 	}
2911 	/*
2912 	 * plan_b: Look at all interfaces and find a preferred address. If
2913 	 * no preferred fall through to plan_c.
2914 	 */
2915 bound_all_plan_b:
2916 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2917 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2918 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2919 		    sctp_ifn->ifn_name);
2920 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2921 			/* wrong base scope */
2922 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2923 			continue;
2924 		}
2925 		if ((sctp_ifn == looked_at) && looked_at) {
2926 			/* already looked at this guy */
2927 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2928 			continue;
2929 		}
2930 		num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2931 		    dest_is_loop, dest_is_priv, fam);
2932 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2933 		    "Found ifn:%p %d preferred source addresses\n",
2934 		    ifn, num_preferred);
2935 		if (num_preferred == 0) {
2936 			/* None on this interface. */
2937 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2938 			continue;
2939 		}
2940 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
2941 		    "num preferred:%d on interface:%p cur_addr_num:%d\n",
2942 		    num_preferred, sctp_ifn, cur_addr_num);
2943 
2944 		/*
2945 		 * Ok we have num_eligible_addr set with how many we can
2946 		 * use, this may vary from call to call due to addresses
2947 		 * being deprecated etc..
2948 		 */
2949 		if (cur_addr_num >= num_preferred) {
2950 			cur_addr_num = 0;
2951 		}
2952 		sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2953 		    dest_is_priv, cur_addr_num, fam, ro);
2954 		if (sifa == NULL)
2955 			continue;
2956 		if (net) {
2957 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2958 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2959 			    cur_addr_num);
2960 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2961 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2962 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2963 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2964 		}
2965 		atomic_add_int(&sifa->refcount, 1);
2966 		return (sifa);
2967 
2968 	}
2969 
2970 	/* plan_c: do we have an acceptable address on the emit interface */
2971 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2972 	if (emit_ifn == NULL) {
2973 		goto plan_d;
2974 	}
2975 	LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2976 		if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2977 		    (non_asoc_addr_ok == 0))
2978 			continue;
2979 		sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2980 		    dest_is_priv, fam);
2981 		if (sifa == NULL)
2982 			continue;
2983 		if (stcb) {
2984 			if (sctp_is_address_in_scope(sifa,
2985 			    stcb->asoc.ipv4_addr_legal,
2986 			    stcb->asoc.ipv6_addr_legal,
2987 			    stcb->asoc.loopback_scope,
2988 			    stcb->asoc.ipv4_local_scope,
2989 			    stcb->asoc.local_scope,
2990 			    stcb->asoc.site_scope, 0) == 0) {
2991 				continue;
2992 			}
2993 			if (((non_asoc_addr_ok == 0) &&
2994 			    (sctp_is_addr_restricted(stcb, sifa))) ||
2995 			    (non_asoc_addr_ok &&
2996 			    (sctp_is_addr_restricted(stcb, sifa)) &&
2997 			    (!sctp_is_addr_pending(stcb, sifa)))) {
2998 				/*
2999 				 * It is restricted for some reason..
3000 				 * probably not yet added.
3001 				 */
3002 				continue;
3003 			}
3004 		}
3005 		atomic_add_int(&sifa->refcount, 1);
3006 		return (sifa);
3007 	}
3008 plan_d:
3009 	/*
3010 	 * plan_d: We are in trouble. No preferred address on the emit
3011 	 * interface. And not even a preferred address on all interfaces. Go
3012 	 * out and see if we can find an acceptable address somewhere
3013 	 * amongst all interfaces.
3014 	 */
3015 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
3016 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3017 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3018 			/* wrong base scope */
3019 			continue;
3020 		}
3021 		if ((sctp_ifn == looked_at) && looked_at)
3022 			/* already looked at this guy */
3023 			continue;
3024 
3025 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3026 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3027 			    (non_asoc_addr_ok == 0))
3028 				continue;
3029 			sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3030 			    dest_is_loop,
3031 			    dest_is_priv, fam);
3032 			if (sifa == NULL)
3033 				continue;
3034 			if (stcb) {
3035 				if (sctp_is_address_in_scope(sifa,
3036 				    stcb->asoc.ipv4_addr_legal,
3037 				    stcb->asoc.ipv6_addr_legal,
3038 				    stcb->asoc.loopback_scope,
3039 				    stcb->asoc.ipv4_local_scope,
3040 				    stcb->asoc.local_scope,
3041 				    stcb->asoc.site_scope, 0) == 0) {
3042 					continue;
3043 				}
3044 				if (((non_asoc_addr_ok == 0) &&
3045 				    (sctp_is_addr_restricted(stcb, sifa))) ||
3046 				    (non_asoc_addr_ok &&
3047 				    (sctp_is_addr_restricted(stcb, sifa)) &&
3048 				    (!sctp_is_addr_pending(stcb, sifa)))) {
3049 					/*
3050 					 * It is restricted for some
3051 					 * reason.. probably not yet added.
3052 					 */
3053 					continue;
3054 				}
3055 			}
3056 			atomic_add_int(&sifa->refcount, 1);
3057 			return (sifa);
3058 		}
3059 	}
3060 	/*
3061 	 * Ok we can find NO address to source from that is not on our
3062 	 * restricted list and non_asoc_address is NOT ok, or it is on our
3063 	 * restricted list. We can't source to it :-(
3064 	 */
3065 	return (NULL);
3066 }
3067 
3068 
3069 
3070 /* tcb may be NULL */
3071 struct sctp_ifa *
3072 sctp_source_address_selection(struct sctp_inpcb *inp,
3073     struct sctp_tcb *stcb,
3074     sctp_route_t * ro,
3075     struct sctp_nets *net,
3076     int non_asoc_addr_ok, uint32_t vrf_id)
3077 {
3078 	struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3079 
3080 #ifdef INET6
3081 	struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3082 
3083 #endif
3084 	struct sctp_ifa *answer;
3085 	uint8_t dest_is_priv, dest_is_loop;
3086 	sa_family_t fam;
3087 
3088 	/*-
3089 	 * Rules: - Find the route if needed, cache if I can. - Look at
3090 	 * interface address in route, Is it in the bound list. If so we
3091 	 * have the best source. - If not we must rotate amongst the
3092 	 * addresses.
3093 	 *
3094 	 * Cavets and issues
3095 	 *
3096 	 * Do we need to pay attention to scope. We can have a private address
3097 	 * or a global address we are sourcing or sending to. So if we draw
3098 	 * it out
3099 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3100 	 * For V4
3101 	 * ------------------------------------------
3102 	 *      source     *      dest  *  result
3103 	 * -----------------------------------------
3104 	 * <a>  Private    *    Global  *  NAT
3105 	 * -----------------------------------------
3106 	 * <b>  Private    *    Private *  No problem
3107 	 * -----------------------------------------
3108 	 * <c>  Global     *    Private *  Huh, How will this work?
3109 	 * -----------------------------------------
3110 	 * <d>  Global     *    Global  *  No Problem
3111 	 *------------------------------------------
3112 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3113 	 * For V6
3114 	 *------------------------------------------
3115 	 *      source     *      dest  *  result
3116 	 * -----------------------------------------
3117 	 * <a>  Linklocal  *    Global  *
3118 	 * -----------------------------------------
3119 	 * <b>  Linklocal  * Linklocal  *  No problem
3120 	 * -----------------------------------------
3121 	 * <c>  Global     * Linklocal  *  Huh, How will this work?
3122 	 * -----------------------------------------
3123 	 * <d>  Global     *    Global  *  No Problem
3124 	 *------------------------------------------
3125 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3126 	 *
3127 	 * And then we add to that what happens if there are multiple addresses
3128 	 * assigned to an interface. Remember the ifa on a ifn is a linked
3129 	 * list of addresses. So one interface can have more than one IP
3130 	 * address. What happens if we have both a private and a global
3131 	 * address? Do we then use context of destination to sort out which
3132 	 * one is best? And what about NAT's sending P->G may get you a NAT
3133 	 * translation, or should you select the G thats on the interface in
3134 	 * preference.
3135 	 *
3136 	 * Decisions:
3137 	 *
3138 	 * - count the number of addresses on the interface.
3139 	 * - if it is one, no problem except case <c>.
3140 	 *   For <a> we will assume a NAT out there.
3141 	 * - if there are more than one, then we need to worry about scope P
3142 	 *   or G. We should prefer G -> G and P -> P if possible.
3143 	 *   Then as a secondary fall back to mixed types G->P being a last
3144 	 *   ditch one.
3145 	 * - The above all works for bound all, but bound specific we need to
3146 	 *   use the same concept but instead only consider the bound
3147 	 *   addresses. If the bound set is NOT assigned to the interface then
3148 	 *   we must use rotation amongst the bound addresses..
3149 	 */
3150 	if (ro->ro_rt == NULL) {
3151 		/*
3152 		 * Need a route to cache.
3153 		 */
3154 		SCTP_RTALLOC(ro, vrf_id);
3155 	}
3156 	if (ro->ro_rt == NULL) {
3157 		return (NULL);
3158 	}
3159 	fam = to->sin_family;
3160 	dest_is_priv = dest_is_loop = 0;
3161 	/* Setup our scopes for the destination */
3162 	switch (fam) {
3163 	case AF_INET:
3164 		/* Scope based on outbound address */
3165 		if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3166 			dest_is_loop = 1;
3167 			if (net != NULL) {
3168 				/* mark it as local */
3169 				net->addr_is_local = 1;
3170 			}
3171 		} else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3172 			dest_is_priv = 1;
3173 		}
3174 		break;
3175 #ifdef INET6
3176 	case AF_INET6:
3177 		/* Scope based on outbound address */
3178 		if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3179 		    SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3180 			/*
3181 			 * If the address is a loopback address, which
3182 			 * consists of "::1" OR "fe80::1%lo0", we are
3183 			 * loopback scope. But we don't use dest_is_priv
3184 			 * (link local addresses).
3185 			 */
3186 			dest_is_loop = 1;
3187 			if (net != NULL) {
3188 				/* mark it as local */
3189 				net->addr_is_local = 1;
3190 			}
3191 		} else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3192 			dest_is_priv = 1;
3193 		}
3194 		break;
3195 #endif
3196 	}
3197 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3198 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3199 	SCTP_IPI_ADDR_RLOCK();
3200 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3201 		/*
3202 		 * Bound all case
3203 		 */
3204 		answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3205 		    dest_is_priv, dest_is_loop,
3206 		    non_asoc_addr_ok, fam);
3207 		SCTP_IPI_ADDR_RUNLOCK();
3208 		return (answer);
3209 	}
3210 	/*
3211 	 * Subset bound case
3212 	 */
3213 	if (stcb) {
3214 		answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3215 		    vrf_id, dest_is_priv,
3216 		    dest_is_loop,
3217 		    non_asoc_addr_ok, fam);
3218 	} else {
3219 		answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3220 		    non_asoc_addr_ok,
3221 		    dest_is_priv,
3222 		    dest_is_loop, fam);
3223 	}
3224 	SCTP_IPI_ADDR_RUNLOCK();
3225 	return (answer);
3226 }
3227 
3228 static int
3229 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3230 {
3231 	struct cmsghdr cmh;
3232 	int tlen, at;
3233 
3234 	tlen = SCTP_BUF_LEN(control);
3235 	at = 0;
3236 	/*
3237 	 * Independent of how many mbufs, find the c_type inside the control
3238 	 * structure and copy out the data.
3239 	 */
3240 	while (at < tlen) {
3241 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3242 			/* not enough room for one more we are done. */
3243 			return (0);
3244 		}
3245 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3246 		if (((int)cmh.cmsg_len + at) > tlen) {
3247 			/*
3248 			 * this is real messed up since there is not enough
3249 			 * data here to cover the cmsg header. We are done.
3250 			 */
3251 			return (0);
3252 		}
3253 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3254 		    (c_type == cmh.cmsg_type)) {
3255 			/* found the one we want, copy it out */
3256 			at += CMSG_ALIGN(sizeof(struct cmsghdr));
3257 			if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3258 				/*
3259 				 * space of cmsg_len after header not big
3260 				 * enough
3261 				 */
3262 				return (0);
3263 			}
3264 			m_copydata(control, at, cpsize, data);
3265 			return (1);
3266 		} else {
3267 			at += CMSG_ALIGN(cmh.cmsg_len);
3268 			if (cmh.cmsg_len == 0) {
3269 				break;
3270 			}
3271 		}
3272 	}
3273 	/* not found */
3274 	return (0);
3275 }
3276 
3277 static struct mbuf *
3278 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3279     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3280 {
3281 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3282 	struct sctp_state_cookie *stc;
3283 	struct sctp_paramhdr *ph;
3284 	uint8_t *foo;
3285 	int sig_offset;
3286 	uint16_t cookie_sz;
3287 
3288 	mret = NULL;
3289 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3290 	    sizeof(struct sctp_paramhdr)), 0,
3291 	    M_DONTWAIT, 1, MT_DATA);
3292 	if (mret == NULL) {
3293 		return (NULL);
3294 	}
3295 	copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3296 	if (copy_init == NULL) {
3297 		sctp_m_freem(mret);
3298 		return (NULL);
3299 	}
3300 #ifdef SCTP_MBUF_LOGGING
3301 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3302 		struct mbuf *mat;
3303 
3304 		mat = copy_init;
3305 		while (mat) {
3306 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3307 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3308 			}
3309 			mat = SCTP_BUF_NEXT(mat);
3310 		}
3311 	}
3312 #endif
3313 	copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3314 	    M_DONTWAIT);
3315 	if (copy_initack == NULL) {
3316 		sctp_m_freem(mret);
3317 		sctp_m_freem(copy_init);
3318 		return (NULL);
3319 	}
3320 #ifdef SCTP_MBUF_LOGGING
3321 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3322 		struct mbuf *mat;
3323 
3324 		mat = copy_initack;
3325 		while (mat) {
3326 			if (SCTP_BUF_IS_EXTENDED(mat)) {
3327 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3328 			}
3329 			mat = SCTP_BUF_NEXT(mat);
3330 		}
3331 	}
3332 #endif
3333 	/* easy side we just drop it on the end */
3334 	ph = mtod(mret, struct sctp_paramhdr *);
3335 	SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3336 	    sizeof(struct sctp_paramhdr);
3337 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
3338 	    sizeof(struct sctp_paramhdr));
3339 	ph->param_type = htons(SCTP_STATE_COOKIE);
3340 	ph->param_length = 0;	/* fill in at the end */
3341 	/* Fill in the stc cookie data */
3342 	memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3343 
3344 	/* tack the INIT and then the INIT-ACK onto the chain */
3345 	cookie_sz = 0;
3346 	m_at = mret;
3347 	for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3348 		cookie_sz += SCTP_BUF_LEN(m_at);
3349 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3350 			SCTP_BUF_NEXT(m_at) = copy_init;
3351 			break;
3352 		}
3353 	}
3354 
3355 	for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3356 		cookie_sz += SCTP_BUF_LEN(m_at);
3357 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3358 			SCTP_BUF_NEXT(m_at) = copy_initack;
3359 			break;
3360 		}
3361 	}
3362 
3363 	for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3364 		cookie_sz += SCTP_BUF_LEN(m_at);
3365 		if (SCTP_BUF_NEXT(m_at) == NULL) {
3366 			break;
3367 		}
3368 	}
3369 	sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3370 	if (sig == NULL) {
3371 		/* no space, so free the entire chain */
3372 		sctp_m_freem(mret);
3373 		return (NULL);
3374 	}
3375 	SCTP_BUF_LEN(sig) = 0;
3376 	SCTP_BUF_NEXT(m_at) = sig;
3377 	sig_offset = 0;
3378 	foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3379 	memset(foo, 0, SCTP_SIGNATURE_SIZE);
3380 	*signature = foo;
3381 	SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3382 	cookie_sz += SCTP_SIGNATURE_SIZE;
3383 	ph->param_length = htons(cookie_sz);
3384 	return (mret);
3385 }
3386 
3387 
3388 static uint8_t
3389 sctp_get_ect(struct sctp_tcb *stcb,
3390     struct sctp_tmit_chunk *chk)
3391 {
3392 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3393 		return (0);
3394 
3395 	return (SCTP_ECT0_BIT);
3396 }
3397 
3398 static int
3399 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3400     struct sctp_tcb *stcb,	/* may be NULL */
3401     struct sctp_nets *net,
3402     struct sockaddr *to,
3403     struct mbuf *m,
3404     uint32_t auth_offset,
3405     struct sctp_auth_chunk *auth,
3406     uint16_t auth_keyid,
3407     int nofragment_flag,
3408     int ecn_ok,
3409     struct sctp_tmit_chunk *chk,
3410     int out_of_asoc_ok,
3411     uint16_t src_port,
3412     uint16_t dest_port,
3413     uint32_t v_tag,
3414     uint16_t port,
3415     int so_locked,
3416 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3417     SCTP_UNUSED
3418 #endif
3419     union sctp_sockstore *over_addr
3420 )
3421 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3422 {
3423 	/*
3424 	 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3425 	 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3426 	 * structure: - fill in the HMAC digest of any AUTH chunk in the
3427 	 * packet. - calculate and fill in the SCTP checksum. - prepend an
3428 	 * IP address header. - if boundall use INADDR_ANY. - if
3429 	 * boundspecific do source address selection. - set fragmentation
3430 	 * option for ipV4. - On return from IP output, check/adjust mtu
3431 	 * size of output interface and smallest_mtu size as well.
3432 	 */
3433 	/* Will need ifdefs around this */
3434 	struct mbuf *o_pak;
3435 	struct mbuf *newm;
3436 	struct sctphdr *sctphdr;
3437 	int packet_length;
3438 	int ret;
3439 	uint32_t vrf_id;
3440 	sctp_route_t *ro = NULL;
3441 	struct udphdr *udp = NULL;
3442 
3443 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3444 	struct socket *so = NULL;
3445 
3446 #endif
3447 
3448 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3449 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3450 		sctp_m_freem(m);
3451 		return (EFAULT);
3452 	}
3453 	if (stcb) {
3454 		vrf_id = stcb->asoc.vrf_id;
3455 	} else {
3456 		vrf_id = inp->def_vrf_id;
3457 	}
3458 
3459 	/* fill in the HMAC digest for any AUTH chunk in the packet */
3460 	if ((auth != NULL) && (stcb != NULL)) {
3461 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3462 	}
3463 	if (to->sa_family == AF_INET) {
3464 		struct ip *ip = NULL;
3465 		sctp_route_t iproute;
3466 		uint8_t tos_value;
3467 		int len;
3468 
3469 		len = sizeof(struct ip) + sizeof(struct sctphdr);
3470 		if (port) {
3471 			len += sizeof(struct udphdr);
3472 		}
3473 		newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3474 		if (newm == NULL) {
3475 			sctp_m_freem(m);
3476 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3477 			return (ENOMEM);
3478 		}
3479 		SCTP_ALIGN_TO_END(newm, len);
3480 		SCTP_BUF_LEN(newm) = len;
3481 		SCTP_BUF_NEXT(newm) = m;
3482 		m = newm;
3483 		packet_length = sctp_calculate_len(m);
3484 		ip = mtod(m, struct ip *);
3485 		ip->ip_v = IPVERSION;
3486 		ip->ip_hl = (sizeof(struct ip) >> 2);
3487 		if (net) {
3488 			tos_value = net->tos_flowlabel & 0x000000ff;
3489 		} else {
3490 			tos_value = inp->ip_inp.inp.inp_ip_tos;
3491 		}
3492 		if ((nofragment_flag) && (port == 0)) {
3493 			ip->ip_off = IP_DF;
3494 		} else
3495 			ip->ip_off = 0;
3496 
3497 		/* FreeBSD has a function for ip_id's */
3498 		ip->ip_id = ip_newid();
3499 
3500 		ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3501 		ip->ip_len = packet_length;
3502 		if (stcb) {
3503 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3504 				/* Enable ECN */
3505 				ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3506 			} else {
3507 				/* No ECN */
3508 				ip->ip_tos = (u_char)(tos_value & 0xfc);
3509 			}
3510 		} else {
3511 			/* no association at all */
3512 			ip->ip_tos = (tos_value & 0xfc);
3513 		}
3514 		if (port) {
3515 			ip->ip_p = IPPROTO_UDP;
3516 		} else {
3517 			ip->ip_p = IPPROTO_SCTP;
3518 		}
3519 		ip->ip_sum = 0;
3520 		if (net == NULL) {
3521 			ro = &iproute;
3522 			memset(&iproute, 0, sizeof(iproute));
3523 			memcpy(&ro->ro_dst, to, to->sa_len);
3524 		} else {
3525 			ro = (sctp_route_t *) & net->ro;
3526 		}
3527 		/* Now the address selection part */
3528 		ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3529 
3530 		/* call the routine to select the src address */
3531 		if (net && out_of_asoc_ok == 0) {
3532 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3533 				sctp_free_ifa(net->ro._s_addr);
3534 				net->ro._s_addr = NULL;
3535 				net->src_addr_selected = 0;
3536 				if (ro->ro_rt) {
3537 					RTFREE(ro->ro_rt);
3538 					ro->ro_rt = NULL;
3539 				}
3540 			}
3541 			if (net->src_addr_selected == 0) {
3542 				/* Cache the source address */
3543 				net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3544 				    ro, net, 0,
3545 				    vrf_id);
3546 				net->src_addr_selected = 1;
3547 			}
3548 			if (net->ro._s_addr == NULL) {
3549 				/* No route to host */
3550 				net->src_addr_selected = 0;
3551 				goto no_route;
3552 			}
3553 			ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3554 		} else {
3555 			if (over_addr == NULL) {
3556 				struct sctp_ifa *_lsrc;
3557 
3558 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
3559 				    net,
3560 				    out_of_asoc_ok,
3561 				    vrf_id);
3562 				if (_lsrc == NULL) {
3563 					goto no_route;
3564 				}
3565 				ip->ip_src = _lsrc->address.sin.sin_addr;
3566 				sctp_free_ifa(_lsrc);
3567 			} else {
3568 				ip->ip_src = over_addr->sin.sin_addr;
3569 				SCTP_RTALLOC(ro, vrf_id);
3570 			}
3571 		}
3572 		if (port) {
3573 			udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
3574 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3575 			udp->uh_dport = port;
3576 			udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3577 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3578 			sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3579 		} else {
3580 			sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
3581 		}
3582 
3583 		sctphdr->src_port = src_port;
3584 		sctphdr->dest_port = dest_port;
3585 		sctphdr->v_tag = v_tag;
3586 		sctphdr->checksum = 0;
3587 
3588 		/*
3589 		 * If source address selection fails and we find no route
3590 		 * then the ip_output should fail as well with a
3591 		 * NO_ROUTE_TO_HOST type error. We probably should catch
3592 		 * that somewhere and abort the association right away
3593 		 * (assuming this is an INIT being sent).
3594 		 */
3595 		if ((ro->ro_rt == NULL)) {
3596 			/*
3597 			 * src addr selection failed to find a route (or
3598 			 * valid source addr), so we can't get there from
3599 			 * here (yet)!
3600 			 */
3601 	no_route:
3602 			SCTPDBG(SCTP_DEBUG_OUTPUT1,
3603 			    "%s: dropped packet - no valid source addr\n",
3604 			    __FUNCTION__);
3605 			if (net) {
3606 				SCTPDBG(SCTP_DEBUG_OUTPUT1,
3607 				    "Destination was ");
3608 				SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3609 				    &net->ro._l_addr.sa);
3610 				if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3611 					if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3612 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3613 						sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3614 						    stcb,
3615 						    SCTP_FAILED_THRESHOLD,
3616 						    (void *)net,
3617 						    so_locked);
3618 						net->dest_state &= ~SCTP_ADDR_REACHABLE;
3619 						net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3620 						/*
3621 						 * JRS 5/14/07 - If a
3622 						 * destination is
3623 						 * unreachable, the PF bit
3624 						 * is turned off.  This
3625 						 * allows an unambiguous use
3626 						 * of the PF bit for
3627 						 * destinations that are
3628 						 * reachable but potentially
3629 						 * failed. If the
3630 						 * destination is set to the
3631 						 * unreachable state, also
3632 						 * set the destination to
3633 						 * the PF state.
3634 						 */
3635 						/*
3636 						 * Add debug message here if
3637 						 * destination is not in PF
3638 						 * state.
3639 						 */
3640 						/*
3641 						 * Stop any running T3
3642 						 * timers here?
3643 						 */
3644 						if ((stcb->asoc.sctp_cmt_on_off > 0) &&
3645 						    (stcb->asoc.sctp_cmt_pf > 0)) {
3646 							net->dest_state &= ~SCTP_ADDR_PF;
3647 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3648 							    net);
3649 						}
3650 					}
3651 				}
3652 				if (stcb) {
3653 					if (net == stcb->asoc.primary_destination) {
3654 						/* need a new primary */
3655 						struct sctp_nets *alt;
3656 
3657 						alt = sctp_find_alternate_net(stcb, net, 0);
3658 						if (alt != net) {
3659 							if (sctp_set_primary_addr(stcb,
3660 							    (struct sockaddr *)NULL,
3661 							    alt) == 0) {
3662 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3663 								if (net->ro._s_addr) {
3664 									sctp_free_ifa(net->ro._s_addr);
3665 									net->ro._s_addr = NULL;
3666 								}
3667 								net->src_addr_selected = 0;
3668 							}
3669 						}
3670 					}
3671 				}
3672 			}
3673 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3674 			sctp_m_freem(m);
3675 			return (EHOSTUNREACH);
3676 		}
3677 		if (ro != &iproute) {
3678 			memcpy(&iproute, ro, sizeof(*ro));
3679 		}
3680 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3681 		    (uint32_t) (ntohl(ip->ip_src.s_addr)));
3682 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3683 		    (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3684 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3685 		    ro->ro_rt);
3686 
3687 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3688 			/* failed to prepend data, give up */
3689 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3690 			sctp_m_freem(m);
3691 			return (ENOMEM);
3692 		}
3693 #ifdef  SCTP_PACKET_LOGGING
3694 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3695 			sctp_packet_log(m, packet_length);
3696 #endif
3697 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3698 		if (port) {
3699 #if defined(SCTP_WITH_NO_CSUM)
3700 			SCTP_STAT_INCR(sctps_sendnocrc);
3701 #else
3702 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3703 			    (stcb) &&
3704 			    (stcb->asoc.loopback_scope))) {
3705 				sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
3706 				SCTP_STAT_INCR(sctps_sendswcrc);
3707 			} else {
3708 				SCTP_STAT_INCR(sctps_sendnocrc);
3709 			}
3710 #endif
3711 			SCTP_ENABLE_UDP_CSUM(o_pak);
3712 		} else {
3713 #if defined(SCTP_WITH_NO_CSUM)
3714 			SCTP_STAT_INCR(sctps_sendnocrc);
3715 #else
3716 			m->m_pkthdr.csum_flags = CSUM_SCTP;
3717 			m->m_pkthdr.csum_data = 0;
3718 			SCTP_STAT_INCR(sctps_sendhwcrc);
3719 #endif
3720 		}
3721 		/* send it out.  table id is taken from stcb */
3722 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3723 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3724 			so = SCTP_INP_SO(inp);
3725 			SCTP_SOCKET_UNLOCK(so, 0);
3726 		}
3727 #endif
3728 		SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3729 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3730 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3731 			atomic_add_int(&stcb->asoc.refcnt, 1);
3732 			SCTP_TCB_UNLOCK(stcb);
3733 			SCTP_SOCKET_LOCK(so, 0);
3734 			SCTP_TCB_LOCK(stcb);
3735 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
3736 		}
3737 #endif
3738 		SCTP_STAT_INCR(sctps_sendpackets);
3739 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3740 		if (ret)
3741 			SCTP_STAT_INCR(sctps_senderrors);
3742 
3743 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3744 		if (net == NULL) {
3745 			/* free tempy routes */
3746 			if (ro->ro_rt) {
3747 				RTFREE(ro->ro_rt);
3748 				ro->ro_rt = NULL;
3749 			}
3750 		} else {
3751 			/* PMTU check versus smallest asoc MTU goes here */
3752 			if ((ro->ro_rt != NULL) &&
3753 			    (net->ro._s_addr)) {
3754 				uint32_t mtu;
3755 
3756 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3757 				if (net->port) {
3758 					mtu -= sizeof(struct udphdr);
3759 				}
3760 				if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
3761 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3762 					net->mtu = mtu;
3763 				}
3764 			} else if (ro->ro_rt == NULL) {
3765 				/* route was freed */
3766 				if (net->ro._s_addr &&
3767 				    net->src_addr_selected) {
3768 					sctp_free_ifa(net->ro._s_addr);
3769 					net->ro._s_addr = NULL;
3770 				}
3771 				net->src_addr_selected = 0;
3772 			}
3773 		}
3774 		return (ret);
3775 	}
3776 #ifdef INET6
3777 	else if (to->sa_family == AF_INET6) {
3778 		uint32_t flowlabel;
3779 		struct ip6_hdr *ip6h;
3780 		struct route_in6 ip6route;
3781 		struct ifnet *ifp;
3782 		u_char flowTop;
3783 		uint16_t flowBottom;
3784 		u_char tosBottom, tosTop;
3785 		struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3786 		int prev_scope = 0;
3787 		struct sockaddr_in6 lsa6_storage;
3788 		int error;
3789 		u_short prev_port = 0;
3790 		int len;
3791 
3792 		if (net != NULL) {
3793 			flowlabel = net->tos_flowlabel;
3794 		} else {
3795 			flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3796 		}
3797 
3798 		len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
3799 		if (port) {
3800 			len += sizeof(struct udphdr);
3801 		}
3802 		newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3803 		if (newm == NULL) {
3804 			sctp_m_freem(m);
3805 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3806 			return (ENOMEM);
3807 		}
3808 		SCTP_ALIGN_TO_END(newm, len);
3809 		SCTP_BUF_LEN(newm) = len;
3810 		SCTP_BUF_NEXT(newm) = m;
3811 		m = newm;
3812 		packet_length = sctp_calculate_len(m);
3813 
3814 		ip6h = mtod(m, struct ip6_hdr *);
3815 		/*
3816 		 * We assume here that inp_flow is in host byte order within
3817 		 * the TCB!
3818 		 */
3819 		flowBottom = flowlabel & 0x0000ffff;
3820 		flowTop = ((flowlabel & 0x000f0000) >> 16);
3821 		tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3822 		/* protect *sin6 from overwrite */
3823 		sin6 = (struct sockaddr_in6 *)to;
3824 		tmp = *sin6;
3825 		sin6 = &tmp;
3826 
3827 		/* KAME hack: embed scopeid */
3828 		if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3829 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3830 			return (EINVAL);
3831 		}
3832 		if (net == NULL) {
3833 			memset(&ip6route, 0, sizeof(ip6route));
3834 			ro = (sctp_route_t *) & ip6route;
3835 			memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3836 		} else {
3837 			ro = (sctp_route_t *) & net->ro;
3838 		}
3839 		if (stcb != NULL) {
3840 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3841 				/* Enable ECN */
3842 				tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3843 			} else {
3844 				/* No ECN */
3845 				tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3846 			}
3847 		} else {
3848 			/* we could get no asoc if it is a O-O-T-B packet */
3849 			tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3850 		}
3851 		ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3852 		if (port) {
3853 			ip6h->ip6_nxt = IPPROTO_UDP;
3854 		} else {
3855 			ip6h->ip6_nxt = IPPROTO_SCTP;
3856 		}
3857 		ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3858 		ip6h->ip6_dst = sin6->sin6_addr;
3859 
3860 		/*
3861 		 * Add SRC address selection here: we can only reuse to a
3862 		 * limited degree the kame src-addr-sel, since we can try
3863 		 * their selection but it may not be bound.
3864 		 */
3865 		bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3866 		lsa6_tmp.sin6_family = AF_INET6;
3867 		lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3868 		lsa6 = &lsa6_tmp;
3869 		if (net && out_of_asoc_ok == 0) {
3870 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3871 				sctp_free_ifa(net->ro._s_addr);
3872 				net->ro._s_addr = NULL;
3873 				net->src_addr_selected = 0;
3874 				if (ro->ro_rt) {
3875 					RTFREE(ro->ro_rt);
3876 					ro->ro_rt = NULL;
3877 				}
3878 			}
3879 			if (net->src_addr_selected == 0) {
3880 				sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3881 				/* KAME hack: embed scopeid */
3882 				if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3883 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3884 					return (EINVAL);
3885 				}
3886 				/* Cache the source address */
3887 				net->ro._s_addr = sctp_source_address_selection(inp,
3888 				    stcb,
3889 				    ro,
3890 				    net,
3891 				    0,
3892 				    vrf_id);
3893 				(void)sa6_recoverscope(sin6);
3894 				net->src_addr_selected = 1;
3895 			}
3896 			if (net->ro._s_addr == NULL) {
3897 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3898 				net->src_addr_selected = 0;
3899 				goto no_route;
3900 			}
3901 			lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3902 		} else {
3903 			sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3904 			/* KAME hack: embed scopeid */
3905 			if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3906 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3907 				return (EINVAL);
3908 			}
3909 			if (over_addr == NULL) {
3910 				struct sctp_ifa *_lsrc;
3911 
3912 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
3913 				    net,
3914 				    out_of_asoc_ok,
3915 				    vrf_id);
3916 				if (_lsrc == NULL) {
3917 					goto no_route;
3918 				}
3919 				lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3920 				sctp_free_ifa(_lsrc);
3921 			} else {
3922 				lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3923 				SCTP_RTALLOC(ro, vrf_id);
3924 			}
3925 			(void)sa6_recoverscope(sin6);
3926 		}
3927 		lsa6->sin6_port = inp->sctp_lport;
3928 
3929 		if (ro->ro_rt == NULL) {
3930 			/*
3931 			 * src addr selection failed to find a route (or
3932 			 * valid source addr), so we can't get there from
3933 			 * here!
3934 			 */
3935 			goto no_route;
3936 		}
3937 		/*
3938 		 * XXX: sa6 may not have a valid sin6_scope_id in the
3939 		 * non-SCOPEDROUTING case.
3940 		 */
3941 		bzero(&lsa6_storage, sizeof(lsa6_storage));
3942 		lsa6_storage.sin6_family = AF_INET6;
3943 		lsa6_storage.sin6_len = sizeof(lsa6_storage);
3944 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
3945 		if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3946 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3947 			sctp_m_freem(m);
3948 			return (error);
3949 		}
3950 		/* XXX */
3951 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
3952 		lsa6_storage.sin6_port = inp->sctp_lport;
3953 		lsa6 = &lsa6_storage;
3954 		ip6h->ip6_src = lsa6->sin6_addr;
3955 
3956 		if (port) {
3957 			udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3958 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3959 			udp->uh_dport = port;
3960 			udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
3961 			udp->uh_sum = 0;
3962 			sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3963 		} else {
3964 			sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
3965 		}
3966 
3967 		sctphdr->src_port = src_port;
3968 		sctphdr->dest_port = dest_port;
3969 		sctphdr->v_tag = v_tag;
3970 		sctphdr->checksum = 0;
3971 
3972 		/*
3973 		 * We set the hop limit now since there is a good chance
3974 		 * that our ro pointer is now filled
3975 		 */
3976 		ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
3977 		ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3978 
3979 #ifdef SCTP_DEBUG
3980 		/* Copy to be sure something bad is not happening */
3981 		sin6->sin6_addr = ip6h->ip6_dst;
3982 		lsa6->sin6_addr = ip6h->ip6_src;
3983 #endif
3984 
3985 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
3986 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
3987 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
3988 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
3989 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
3990 		if (net) {
3991 			sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3992 			/* preserve the port and scope for link local send */
3993 			prev_scope = sin6->sin6_scope_id;
3994 			prev_port = sin6->sin6_port;
3995 		}
3996 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3997 			/* failed to prepend data, give up */
3998 			sctp_m_freem(m);
3999 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4000 			return (ENOMEM);
4001 		}
4002 #ifdef  SCTP_PACKET_LOGGING
4003 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4004 			sctp_packet_log(m, packet_length);
4005 #endif
4006 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4007 		if (port) {
4008 #if defined(SCTP_WITH_NO_CSUM)
4009 			SCTP_STAT_INCR(sctps_sendnocrc);
4010 #else
4011 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4012 			    (stcb) &&
4013 			    (stcb->asoc.loopback_scope))) {
4014 				sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4015 				SCTP_STAT_INCR(sctps_sendswcrc);
4016 			} else {
4017 				SCTP_STAT_INCR(sctps_sendnocrc);
4018 			}
4019 #endif
4020 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4021 				udp->uh_sum = 0xffff;
4022 			}
4023 		} else {
4024 #if defined(SCTP_WITH_NO_CSUM)
4025 			SCTP_STAT_INCR(sctps_sendnocrc);
4026 #else
4027 			m->m_pkthdr.csum_flags = CSUM_SCTP;
4028 			m->m_pkthdr.csum_data = 0;
4029 			SCTP_STAT_INCR(sctps_sendhwcrc);
4030 #endif
4031 		}
4032 		/* send it out. table id is taken from stcb */
4033 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4034 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4035 			so = SCTP_INP_SO(inp);
4036 			SCTP_SOCKET_UNLOCK(so, 0);
4037 		}
4038 #endif
4039 		SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4040 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4041 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4042 			atomic_add_int(&stcb->asoc.refcnt, 1);
4043 			SCTP_TCB_UNLOCK(stcb);
4044 			SCTP_SOCKET_LOCK(so, 0);
4045 			SCTP_TCB_LOCK(stcb);
4046 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
4047 		}
4048 #endif
4049 		if (net) {
4050 			/* for link local this must be done */
4051 			sin6->sin6_scope_id = prev_scope;
4052 			sin6->sin6_port = prev_port;
4053 		}
4054 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4055 		SCTP_STAT_INCR(sctps_sendpackets);
4056 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4057 		if (ret) {
4058 			SCTP_STAT_INCR(sctps_senderrors);
4059 		}
4060 		if (net == NULL) {
4061 			/* Now if we had a temp route free it */
4062 			if (ro->ro_rt) {
4063 				RTFREE(ro->ro_rt);
4064 			}
4065 		} else {
4066 			/* PMTU check versus smallest asoc MTU goes here */
4067 			if (ro->ro_rt == NULL) {
4068 				/* Route was freed */
4069 				if (net->ro._s_addr &&
4070 				    net->src_addr_selected) {
4071 					sctp_free_ifa(net->ro._s_addr);
4072 					net->ro._s_addr = NULL;
4073 				}
4074 				net->src_addr_selected = 0;
4075 			}
4076 			if ((ro->ro_rt != NULL) &&
4077 			    (net->ro._s_addr)) {
4078 				uint32_t mtu;
4079 
4080 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4081 				if (mtu &&
4082 				    (stcb->asoc.smallest_mtu > mtu)) {
4083 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4084 					net->mtu = mtu;
4085 					if (net->port) {
4086 						net->mtu -= sizeof(struct udphdr);
4087 					}
4088 				}
4089 			} else if (ifp) {
4090 				if (ND_IFINFO(ifp)->linkmtu &&
4091 				    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4092 					sctp_mtu_size_reset(inp,
4093 					    &stcb->asoc,
4094 					    ND_IFINFO(ifp)->linkmtu);
4095 				}
4096 			}
4097 		}
4098 		return (ret);
4099 	}
4100 #endif
4101 	else {
4102 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4103 		    ((struct sockaddr *)to)->sa_family);
4104 		sctp_m_freem(m);
4105 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4106 		return (EFAULT);
4107 	}
4108 }
4109 
4110 
4111 void
4112 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4113 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4114     SCTP_UNUSED
4115 #endif
4116 )
4117 {
4118 	struct mbuf *m, *m_at, *mp_last;
4119 	struct sctp_nets *net;
4120 	struct sctp_init_chunk *init;
4121 	struct sctp_supported_addr_param *sup_addr;
4122 	struct sctp_adaptation_layer_indication *ali;
4123 	struct sctp_ecn_supported_param *ecn;
4124 	struct sctp_prsctp_supported_param *prsctp;
4125 	struct sctp_supported_chunk_types_param *pr_supported;
4126 	int cnt_inits_to = 0;
4127 	int padval, ret;
4128 	int num_ext;
4129 	int p_len;
4130 
4131 	/* INIT's always go to the primary (and usually ONLY address) */
4132 	mp_last = NULL;
4133 	net = stcb->asoc.primary_destination;
4134 	if (net == NULL) {
4135 		net = TAILQ_FIRST(&stcb->asoc.nets);
4136 		if (net == NULL) {
4137 			/* TSNH */
4138 			return;
4139 		}
4140 		/* we confirm any address we send an INIT to */
4141 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4142 		(void)sctp_set_primary_addr(stcb, NULL, net);
4143 	} else {
4144 		/* we confirm any address we send an INIT to */
4145 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4146 	}
4147 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4148 #ifdef INET6
4149 	if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4150 		/*
4151 		 * special hook, if we are sending to link local it will not
4152 		 * show up in our private address count.
4153 		 */
4154 		struct sockaddr_in6 *sin6l;
4155 
4156 		sin6l = &net->ro._l_addr.sin6;
4157 		if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4158 			cnt_inits_to = 1;
4159 	}
4160 #endif
4161 	if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4162 		/* This case should not happen */
4163 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4164 		return;
4165 	}
4166 	/* start the INIT timer */
4167 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4168 
4169 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4170 	if (m == NULL) {
4171 		/* No memory, INIT timer will re-attempt. */
4172 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4173 		return;
4174 	}
4175 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4176 	/*
4177 	 * assume peer supports asconf in order to be able to queue local
4178 	 * address changes while an INIT is in flight and before the assoc
4179 	 * is established.
4180 	 */
4181 	stcb->asoc.peer_supports_asconf = 1;
4182 	/* Now lets put the SCTP header in place */
4183 	init = mtod(m, struct sctp_init_chunk *);
4184 	/* now the chunk header */
4185 	init->ch.chunk_type = SCTP_INITIATION;
4186 	init->ch.chunk_flags = 0;
4187 	/* fill in later from mbuf we build */
4188 	init->ch.chunk_length = 0;
4189 	/* place in my tag */
4190 	init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4191 	/* set up some of the credits. */
4192 	init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4193 	    SCTP_MINIMAL_RWND));
4194 
4195 	init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4196 	init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4197 	init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4198 	/* now the address restriction */
4199 	sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4200 	    sizeof(*init));
4201 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4202 #ifdef INET6
4203 	/* we support 2 types: IPv6/IPv4 */
4204 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4205 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4206 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4207 #else
4208 	/* we support 1 type: IPv4 */
4209 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4210 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4211 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
4212 #endif
4213 	SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4214 	/* adaptation layer indication parameter */
4215 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4216 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4217 	ali->ph.param_length = htons(sizeof(*ali));
4218 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4219 	SCTP_BUF_LEN(m) += sizeof(*ali);
4220 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4221 
4222 	if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4223 		/* Add NAT friendly parameter */
4224 		struct sctp_paramhdr *ph;
4225 
4226 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4227 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4228 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
4229 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4230 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4231 	}
4232 	/* now any cookie time extensions */
4233 	if (stcb->asoc.cookie_preserve_req) {
4234 		struct sctp_cookie_perserve_param *cookie_preserve;
4235 
4236 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4237 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4238 		cookie_preserve->ph.param_length = htons(
4239 		    sizeof(*cookie_preserve));
4240 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4241 		SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4242 		ecn = (struct sctp_ecn_supported_param *)(
4243 		    (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4244 		stcb->asoc.cookie_preserve_req = 0;
4245 	}
4246 	/* ECN parameter */
4247 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4248 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4249 		ecn->ph.param_length = htons(sizeof(*ecn));
4250 		SCTP_BUF_LEN(m) += sizeof(*ecn);
4251 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4252 		    sizeof(*ecn));
4253 	} else {
4254 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4255 	}
4256 	/* And now tell the peer we do pr-sctp */
4257 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4258 	prsctp->ph.param_length = htons(sizeof(*prsctp));
4259 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
4260 
4261 	/* And now tell the peer we do all the extensions */
4262 	pr_supported = (struct sctp_supported_chunk_types_param *)
4263 	    ((caddr_t)prsctp + sizeof(*prsctp));
4264 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4265 	num_ext = 0;
4266 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4267 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4268 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4269 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4270 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4271 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4272 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4273 	}
4274 	if (stcb->asoc.sctp_nr_sack_on_off == 1) {
4275 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4276 	}
4277 	p_len = sizeof(*pr_supported) + num_ext;
4278 	pr_supported->ph.param_length = htons(p_len);
4279 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4280 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4281 
4282 
4283 	/* add authentication parameters */
4284 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4285 		struct sctp_auth_random *randp;
4286 		struct sctp_auth_hmac_algo *hmacs;
4287 		struct sctp_auth_chunk_list *chunks;
4288 
4289 		/* attach RANDOM parameter, if available */
4290 		if (stcb->asoc.authinfo.random != NULL) {
4291 			randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4292 			p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4293 			/* random key already contains the header */
4294 			bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4295 			/* zero out any padding required */
4296 			bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4297 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4298 		}
4299 		/* add HMAC_ALGO parameter */
4300 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4301 		p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4302 		    (uint8_t *) hmacs->hmac_ids);
4303 		if (p_len > 0) {
4304 			p_len += sizeof(*hmacs);
4305 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4306 			hmacs->ph.param_length = htons(p_len);
4307 			/* zero out any padding required */
4308 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4309 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4310 		}
4311 		/* add CHUNKS parameter */
4312 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4313 		p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4314 		    chunks->chunk_types);
4315 		if (p_len > 0) {
4316 			p_len += sizeof(*chunks);
4317 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4318 			chunks->ph.param_length = htons(p_len);
4319 			/* zero out any padding required */
4320 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4321 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4322 		}
4323 	}
4324 	m_at = m;
4325 	/* now the addresses */
4326 	{
4327 		struct sctp_scoping scp;
4328 
4329 		/*
4330 		 * To optimize this we could put the scoping stuff into a
4331 		 * structure and remove the individual uint8's from the
4332 		 * assoc structure. Then we could just sifa in the address
4333 		 * within the stcb.. but for now this is a quick hack to get
4334 		 * the address stuff teased apart.
4335 		 */
4336 		scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4337 		scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4338 		scp.loopback_scope = stcb->asoc.loopback_scope;
4339 		scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4340 		scp.local_scope = stcb->asoc.local_scope;
4341 		scp.site_scope = stcb->asoc.site_scope;
4342 
4343 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4344 	}
4345 
4346 	/* calulate the size and update pkt header and chunk header */
4347 	p_len = 0;
4348 	for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4349 		if (SCTP_BUF_NEXT(m_at) == NULL)
4350 			mp_last = m_at;
4351 		p_len += SCTP_BUF_LEN(m_at);
4352 	}
4353 	init->ch.chunk_length = htons(p_len);
4354 	/*
4355 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4356 	 * here since the timer will drive a retranmission.
4357 	 */
4358 
4359 	/* I don't expect this to execute but we will be safe here */
4360 	padval = p_len % 4;
4361 	if ((padval) && (mp_last)) {
4362 		/*
4363 		 * The compiler worries that mp_last may not be set even
4364 		 * though I think it is impossible :-> however we add
4365 		 * mp_last here just in case.
4366 		 */
4367 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4368 		if (ret) {
4369 			/* Houston we have a problem, no space */
4370 			sctp_m_freem(m);
4371 			return;
4372 		}
4373 		p_len += padval;
4374 	}
4375 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4376 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4377 	    (struct sockaddr *)&net->ro._l_addr,
4378 	    m, 0, NULL, 0, 0, 0, NULL, 0,
4379 	    inp->sctp_lport, stcb->rport, htonl(0),
4380 	    net->port, so_locked, NULL);
4381 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4382 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4383 	(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4384 }
4385 
4386 struct mbuf *
4387 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4388     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4389 {
4390 	/*
4391 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4392 	 * being equal to the beginning of the params i.e. (iphlen +
4393 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
4394 	 * end of the mbuf verifying that all parameters are known.
4395 	 *
4396 	 * For unknown parameters build and return a mbuf with
4397 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4398 	 * processing this chunk stop, and set *abort_processing to 1.
4399 	 *
4400 	 * By having param_offset be pre-set to where parameters begin it is
4401 	 * hoped that this routine may be reused in the future by new
4402 	 * features.
4403 	 */
4404 	struct sctp_paramhdr *phdr, params;
4405 
4406 	struct mbuf *mat, *op_err;
4407 	char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4408 	int at, limit, pad_needed;
4409 	uint16_t ptype, plen, padded_size;
4410 	int err_at;
4411 
4412 	*abort_processing = 0;
4413 	mat = in_initpkt;
4414 	err_at = 0;
4415 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4416 	at = param_offset;
4417 	op_err = NULL;
4418 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4419 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4420 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4421 		ptype = ntohs(phdr->param_type);
4422 		plen = ntohs(phdr->param_length);
4423 		if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4424 			/* wacked parameter */
4425 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4426 			goto invalid_size;
4427 		}
4428 		limit -= SCTP_SIZE32(plen);
4429 		/*-
4430 		 * All parameters for all chunks that we know/understand are
4431 		 * listed here. We process them other places and make
4432 		 * appropriate stop actions per the upper bits. However this
4433 		 * is the generic routine processor's can call to get back
4434 		 * an operr.. to either incorporate (init-ack) or send.
4435 		 */
4436 		padded_size = SCTP_SIZE32(plen);
4437 		switch (ptype) {
4438 			/* Param's with variable size */
4439 		case SCTP_HEARTBEAT_INFO:
4440 		case SCTP_STATE_COOKIE:
4441 		case SCTP_UNRECOG_PARAM:
4442 		case SCTP_ERROR_CAUSE_IND:
4443 			/* ok skip fwd */
4444 			at += padded_size;
4445 			break;
4446 			/* Param's with variable size within a range */
4447 		case SCTP_CHUNK_LIST:
4448 		case SCTP_SUPPORTED_CHUNK_EXT:
4449 			if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4450 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4451 				goto invalid_size;
4452 			}
4453 			at += padded_size;
4454 			break;
4455 		case SCTP_SUPPORTED_ADDRTYPE:
4456 			if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4457 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4458 				goto invalid_size;
4459 			}
4460 			at += padded_size;
4461 			break;
4462 		case SCTP_RANDOM:
4463 			if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4464 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4465 				goto invalid_size;
4466 			}
4467 			at += padded_size;
4468 			break;
4469 		case SCTP_SET_PRIM_ADDR:
4470 		case SCTP_DEL_IP_ADDRESS:
4471 		case SCTP_ADD_IP_ADDRESS:
4472 			if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4473 			    (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4474 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4475 				goto invalid_size;
4476 			}
4477 			at += padded_size;
4478 			break;
4479 			/* Param's with a fixed size */
4480 		case SCTP_IPV4_ADDRESS:
4481 			if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4482 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4483 				goto invalid_size;
4484 			}
4485 			at += padded_size;
4486 			break;
4487 		case SCTP_IPV6_ADDRESS:
4488 			if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4489 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4490 				goto invalid_size;
4491 			}
4492 			at += padded_size;
4493 			break;
4494 		case SCTP_COOKIE_PRESERVE:
4495 			if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4496 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4497 				goto invalid_size;
4498 			}
4499 			at += padded_size;
4500 			break;
4501 		case SCTP_HAS_NAT_SUPPORT:
4502 			*nat_friendly = 1;
4503 			/* fall through */
4504 		case SCTP_PRSCTP_SUPPORTED:
4505 
4506 			if (padded_size != sizeof(struct sctp_paramhdr)) {
4507 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
4508 				goto invalid_size;
4509 			}
4510 			at += padded_size;
4511 			break;
4512 		case SCTP_ECN_CAPABLE:
4513 			if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4514 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4515 				goto invalid_size;
4516 			}
4517 			at += padded_size;
4518 			break;
4519 		case SCTP_ULP_ADAPTATION:
4520 			if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4521 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4522 				goto invalid_size;
4523 			}
4524 			at += padded_size;
4525 			break;
4526 		case SCTP_SUCCESS_REPORT:
4527 			if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4528 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4529 				goto invalid_size;
4530 			}
4531 			at += padded_size;
4532 			break;
4533 		case SCTP_HOSTNAME_ADDRESS:
4534 			{
4535 				/* We can NOT handle HOST NAME addresses!! */
4536 				int l_len;
4537 
4538 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4539 				*abort_processing = 1;
4540 				if (op_err == NULL) {
4541 					/* Ok need to try to get a mbuf */
4542 #ifdef INET6
4543 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4544 #else
4545 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4546 #endif
4547 					l_len += plen;
4548 					l_len += sizeof(struct sctp_paramhdr);
4549 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4550 					if (op_err) {
4551 						SCTP_BUF_LEN(op_err) = 0;
4552 						/*
4553 						 * pre-reserve space for ip
4554 						 * and sctp header  and
4555 						 * chunk hdr
4556 						 */
4557 #ifdef INET6
4558 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4559 #else
4560 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4561 #endif
4562 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4563 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4564 					}
4565 				}
4566 				if (op_err) {
4567 					/* If we have space */
4568 					struct sctp_paramhdr s;
4569 
4570 					if (err_at % 4) {
4571 						uint32_t cpthis = 0;
4572 
4573 						pad_needed = 4 - (err_at % 4);
4574 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4575 						err_at += pad_needed;
4576 					}
4577 					s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4578 					s.param_length = htons(sizeof(s) + plen);
4579 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4580 					err_at += sizeof(s);
4581 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4582 					if (phdr == NULL) {
4583 						sctp_m_freem(op_err);
4584 						/*
4585 						 * we are out of memory but
4586 						 * we still need to have a
4587 						 * look at what to do (the
4588 						 * system is in trouble
4589 						 * though).
4590 						 */
4591 						return (NULL);
4592 					}
4593 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4594 					err_at += plen;
4595 				}
4596 				return (op_err);
4597 				break;
4598 			}
4599 		default:
4600 			/*
4601 			 * we do not recognize the parameter figure out what
4602 			 * we do.
4603 			 */
4604 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4605 			if ((ptype & 0x4000) == 0x4000) {
4606 				/* Report bit is set?? */
4607 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4608 				if (op_err == NULL) {
4609 					int l_len;
4610 
4611 					/* Ok need to try to get an mbuf */
4612 #ifdef INET6
4613 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4614 #else
4615 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4616 #endif
4617 					l_len += plen;
4618 					l_len += sizeof(struct sctp_paramhdr);
4619 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4620 					if (op_err) {
4621 						SCTP_BUF_LEN(op_err) = 0;
4622 #ifdef INET6
4623 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4624 #else
4625 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4626 #endif
4627 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4628 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4629 					}
4630 				}
4631 				if (op_err) {
4632 					/* If we have space */
4633 					struct sctp_paramhdr s;
4634 
4635 					if (err_at % 4) {
4636 						uint32_t cpthis = 0;
4637 
4638 						pad_needed = 4 - (err_at % 4);
4639 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4640 						err_at += pad_needed;
4641 					}
4642 					s.param_type = htons(SCTP_UNRECOG_PARAM);
4643 					s.param_length = htons(sizeof(s) + plen);
4644 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4645 					err_at += sizeof(s);
4646 					if (plen > sizeof(tempbuf)) {
4647 						plen = sizeof(tempbuf);
4648 					}
4649 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4650 					if (phdr == NULL) {
4651 						sctp_m_freem(op_err);
4652 						/*
4653 						 * we are out of memory but
4654 						 * we still need to have a
4655 						 * look at what to do (the
4656 						 * system is in trouble
4657 						 * though).
4658 						 */
4659 						op_err = NULL;
4660 						goto more_processing;
4661 					}
4662 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4663 					err_at += plen;
4664 				}
4665 			}
4666 	more_processing:
4667 			if ((ptype & 0x8000) == 0x0000) {
4668 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4669 				return (op_err);
4670 			} else {
4671 				/* skip this chunk and continue processing */
4672 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4673 				at += SCTP_SIZE32(plen);
4674 			}
4675 			break;
4676 
4677 		}
4678 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4679 	}
4680 	return (op_err);
4681 invalid_size:
4682 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4683 	*abort_processing = 1;
4684 	if ((op_err == NULL) && phdr) {
4685 		int l_len;
4686 
4687 #ifdef INET6
4688 		l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4689 #else
4690 		l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4691 #endif
4692 		l_len += (2 * sizeof(struct sctp_paramhdr));
4693 		op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4694 		if (op_err) {
4695 			SCTP_BUF_LEN(op_err) = 0;
4696 #ifdef INET6
4697 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4698 #else
4699 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4700 #endif
4701 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4702 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4703 		}
4704 	}
4705 	if ((op_err) && phdr) {
4706 		struct sctp_paramhdr s;
4707 
4708 		if (err_at % 4) {
4709 			uint32_t cpthis = 0;
4710 
4711 			pad_needed = 4 - (err_at % 4);
4712 			m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4713 			err_at += pad_needed;
4714 		}
4715 		s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4716 		s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4717 		m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4718 		err_at += sizeof(s);
4719 		/* Only copy back the p-hdr that caused the issue */
4720 		m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4721 	}
4722 	return (op_err);
4723 }
4724 
4725 static int
4726 sctp_are_there_new_addresses(struct sctp_association *asoc,
4727     struct mbuf *in_initpkt, int iphlen, int offset)
4728 {
4729 	/*
4730 	 * Given a INIT packet, look through the packet to verify that there
4731 	 * are NO new addresses. As we go through the parameters add reports
4732 	 * of any un-understood parameters that require an error.  Also we
4733 	 * must return (1) to drop the packet if we see a un-understood
4734 	 * parameter that tells us to drop the chunk.
4735 	 */
4736 	struct sockaddr_in sin4, *sa4;
4737 
4738 #ifdef INET6
4739 	struct sockaddr_in6 sin6, *sa6;
4740 
4741 #endif
4742 	struct sockaddr *sa_touse;
4743 	struct sockaddr *sa;
4744 	struct sctp_paramhdr *phdr, params;
4745 	struct ip *iph;
4746 
4747 #ifdef INET6
4748 	struct ip6_hdr *ip6h;
4749 
4750 #endif
4751 	struct mbuf *mat;
4752 	uint16_t ptype, plen;
4753 	int err_at;
4754 	uint8_t fnd;
4755 	struct sctp_nets *net;
4756 
4757 	memset(&sin4, 0, sizeof(sin4));
4758 #ifdef INET6
4759 	memset(&sin6, 0, sizeof(sin6));
4760 #endif
4761 	sin4.sin_family = AF_INET;
4762 	sin4.sin_len = sizeof(sin4);
4763 #ifdef INET6
4764 	sin6.sin6_family = AF_INET6;
4765 	sin6.sin6_len = sizeof(sin6);
4766 #endif
4767 	sa_touse = NULL;
4768 	/* First what about the src address of the pkt ? */
4769 	iph = mtod(in_initpkt, struct ip *);
4770 	switch (iph->ip_v) {
4771 	case IPVERSION:
4772 		/* source addr is IPv4 */
4773 		sin4.sin_addr = iph->ip_src;
4774 		sa_touse = (struct sockaddr *)&sin4;
4775 		break;
4776 #ifdef INET6
4777 	case IPV6_VERSION >> 4:
4778 		/* source addr is IPv6 */
4779 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
4780 		sin6.sin6_addr = ip6h->ip6_src;
4781 		sa_touse = (struct sockaddr *)&sin6;
4782 		break;
4783 #endif
4784 	default:
4785 		return (1);
4786 	}
4787 
4788 	fnd = 0;
4789 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4790 		sa = (struct sockaddr *)&net->ro._l_addr;
4791 		if (sa->sa_family == sa_touse->sa_family) {
4792 			if (sa->sa_family == AF_INET) {
4793 				sa4 = (struct sockaddr_in *)sa;
4794 				if (sa4->sin_addr.s_addr ==
4795 				    sin4.sin_addr.s_addr) {
4796 					fnd = 1;
4797 					break;
4798 				}
4799 			}
4800 #ifdef INET6
4801 			if (sa->sa_family == AF_INET6) {
4802 				sa6 = (struct sockaddr_in6 *)sa;
4803 				if (SCTP6_ARE_ADDR_EQUAL(sa6,
4804 				    &sin6)) {
4805 					fnd = 1;
4806 					break;
4807 				}
4808 			}
4809 #endif
4810 		}
4811 	}
4812 	if (fnd == 0) {
4813 		/* New address added! no need to look futher. */
4814 		return (1);
4815 	}
4816 	/* Ok so far lets munge through the rest of the packet */
4817 	mat = in_initpkt;
4818 	err_at = 0;
4819 	sa_touse = NULL;
4820 	offset += sizeof(struct sctp_init_chunk);
4821 	phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4822 	while (phdr) {
4823 		ptype = ntohs(phdr->param_type);
4824 		plen = ntohs(phdr->param_length);
4825 		if (ptype == SCTP_IPV4_ADDRESS) {
4826 			struct sctp_ipv4addr_param *p4, p4_buf;
4827 
4828 			phdr = sctp_get_next_param(mat, offset,
4829 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4830 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
4831 			    phdr == NULL) {
4832 				return (1);
4833 			}
4834 			p4 = (struct sctp_ipv4addr_param *)phdr;
4835 			sin4.sin_addr.s_addr = p4->addr;
4836 			sa_touse = (struct sockaddr *)&sin4;
4837 		} else if (ptype == SCTP_IPV6_ADDRESS) {
4838 			struct sctp_ipv6addr_param *p6, p6_buf;
4839 
4840 			phdr = sctp_get_next_param(mat, offset,
4841 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4842 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
4843 			    phdr == NULL) {
4844 				return (1);
4845 			}
4846 			p6 = (struct sctp_ipv6addr_param *)phdr;
4847 #ifdef INET6
4848 			memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4849 			    sizeof(p6->addr));
4850 #endif
4851 			sa_touse = (struct sockaddr *)&sin4;
4852 		}
4853 		if (sa_touse) {
4854 			/* ok, sa_touse points to one to check */
4855 			fnd = 0;
4856 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4857 				sa = (struct sockaddr *)&net->ro._l_addr;
4858 				if (sa->sa_family != sa_touse->sa_family) {
4859 					continue;
4860 				}
4861 				if (sa->sa_family == AF_INET) {
4862 					sa4 = (struct sockaddr_in *)sa;
4863 					if (sa4->sin_addr.s_addr ==
4864 					    sin4.sin_addr.s_addr) {
4865 						fnd = 1;
4866 						break;
4867 					}
4868 				}
4869 #ifdef INET6
4870 				if (sa->sa_family == AF_INET6) {
4871 					sa6 = (struct sockaddr_in6 *)sa;
4872 					if (SCTP6_ARE_ADDR_EQUAL(
4873 					    sa6, &sin6)) {
4874 						fnd = 1;
4875 						break;
4876 					}
4877 				}
4878 #endif
4879 			}
4880 			if (!fnd) {
4881 				/* New addr added! no need to look further */
4882 				return (1);
4883 			}
4884 		}
4885 		offset += SCTP_SIZE32(plen);
4886 		phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4887 	}
4888 	return (0);
4889 }
4890 
4891 /*
4892  * Given a MBUF chain that was sent into us containing an INIT. Build a
4893  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4894  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4895  * message (i.e. the struct sctp_init_msg).
4896  */
4897 void
4898 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4899     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4900     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4901 {
4902 	struct sctp_association *asoc;
4903 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4904 	struct sctp_init_ack_chunk *initack;
4905 	struct sctp_adaptation_layer_indication *ali;
4906 	struct sctp_ecn_supported_param *ecn;
4907 	struct sctp_prsctp_supported_param *prsctp;
4908 	struct sctp_supported_chunk_types_param *pr_supported;
4909 	union sctp_sockstore store, store1, *over_addr;
4910 	struct sockaddr_in *sin, *to_sin;
4911 
4912 #ifdef INET6
4913 	struct sockaddr_in6 *sin6, *to_sin6;
4914 
4915 #endif
4916 	struct ip *iph;
4917 
4918 #ifdef INET6
4919 	struct ip6_hdr *ip6;
4920 
4921 #endif
4922 	struct sockaddr *to;
4923 	struct sctp_state_cookie stc;
4924 	struct sctp_nets *net = NULL;
4925 	uint8_t *signature = NULL;
4926 	int cnt_inits_to = 0;
4927 	uint16_t his_limit, i_want;
4928 	int abort_flag, padval;
4929 	int num_ext;
4930 	int p_len;
4931 	int nat_friendly = 0;
4932 	struct socket *so;
4933 
4934 	if (stcb)
4935 		asoc = &stcb->asoc;
4936 	else
4937 		asoc = NULL;
4938 	mp_last = NULL;
4939 	if ((asoc != NULL) &&
4940 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
4941 	    (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
4942 		/* new addresses, out of here in non-cookie-wait states */
4943 		/*
4944 		 * Send a ABORT, we don't add the new address error clause
4945 		 * though we even set the T bit and copy in the 0 tag.. this
4946 		 * looks no different than if no listener was present.
4947 		 */
4948 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
4949 		return;
4950 	}
4951 	abort_flag = 0;
4952 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
4953 	    (offset + sizeof(struct sctp_init_chunk)),
4954 	    &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
4955 	if (abort_flag) {
4956 do_a_abort:
4957 		sctp_send_abort(init_pkt, iphlen, sh,
4958 		    init_chk->init.initiate_tag, op_err, vrf_id, port);
4959 		return;
4960 	}
4961 	m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
4962 	if (m == NULL) {
4963 		/* No memory, INIT timer will re-attempt. */
4964 		if (op_err)
4965 			sctp_m_freem(op_err);
4966 		return;
4967 	}
4968 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4969 
4970 	/* the time I built cookie */
4971 	(void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
4972 
4973 	/* populate any tie tags */
4974 	if (asoc != NULL) {
4975 		/* unlock before tag selections */
4976 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
4977 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
4978 		stc.cookie_life = asoc->cookie_life;
4979 		net = asoc->primary_destination;
4980 	} else {
4981 		stc.tie_tag_my_vtag = 0;
4982 		stc.tie_tag_peer_vtag = 0;
4983 		/* life I will award this cookie */
4984 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
4985 	}
4986 
4987 	/* copy in the ports for later check */
4988 	stc.myport = sh->dest_port;
4989 	stc.peerport = sh->src_port;
4990 
4991 	/*
4992 	 * If we wanted to honor cookie life extentions, we would add to
4993 	 * stc.cookie_life. For now we should NOT honor any extension
4994 	 */
4995 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
4996 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
4997 		struct inpcb *in_inp;
4998 
4999 		/* Its a V6 socket */
5000 		in_inp = (struct inpcb *)inp;
5001 		stc.ipv6_addr_legal = 1;
5002 		/* Now look at the binding flag to see if V4 will be legal */
5003 		if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5004 			stc.ipv4_addr_legal = 1;
5005 		} else {
5006 			/* V4 addresses are NOT legal on the association */
5007 			stc.ipv4_addr_legal = 0;
5008 		}
5009 	} else {
5010 		/* Its a V4 socket, no - V6 */
5011 		stc.ipv4_addr_legal = 1;
5012 		stc.ipv6_addr_legal = 0;
5013 	}
5014 
5015 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5016 	stc.ipv4_scope = 1;
5017 #else
5018 	stc.ipv4_scope = 0;
5019 #endif
5020 	/* now for scope setup */
5021 	memset((caddr_t)&store, 0, sizeof(store));
5022 	memset((caddr_t)&store1, 0, sizeof(store1));
5023 	sin = &store.sin;
5024 	to_sin = &store1.sin;
5025 #ifdef INET6
5026 	sin6 = &store.sin6;
5027 	to_sin6 = &store1.sin6;
5028 #endif
5029 	iph = mtod(init_pkt, struct ip *);
5030 	/* establish the to_addr's */
5031 	switch (iph->ip_v) {
5032 	case IPVERSION:
5033 		to_sin->sin_port = sh->dest_port;
5034 		to_sin->sin_family = AF_INET;
5035 		to_sin->sin_len = sizeof(struct sockaddr_in);
5036 		to_sin->sin_addr = iph->ip_dst;
5037 		break;
5038 #ifdef INET6
5039 	case IPV6_VERSION >> 4:
5040 		ip6 = mtod(init_pkt, struct ip6_hdr *);
5041 		to_sin6->sin6_addr = ip6->ip6_dst;
5042 		to_sin6->sin6_scope_id = 0;
5043 		to_sin6->sin6_port = sh->dest_port;
5044 		to_sin6->sin6_family = AF_INET6;
5045 		to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5046 		break;
5047 #endif
5048 	default:
5049 		goto do_a_abort;
5050 		break;
5051 	};
5052 
5053 	if (net == NULL) {
5054 		to = (struct sockaddr *)&store;
5055 		switch (iph->ip_v) {
5056 		case IPVERSION:
5057 			{
5058 				sin->sin_family = AF_INET;
5059 				sin->sin_len = sizeof(struct sockaddr_in);
5060 				sin->sin_port = sh->src_port;
5061 				sin->sin_addr = iph->ip_src;
5062 				/* lookup address */
5063 				stc.address[0] = sin->sin_addr.s_addr;
5064 				stc.address[1] = 0;
5065 				stc.address[2] = 0;
5066 				stc.address[3] = 0;
5067 				stc.addr_type = SCTP_IPV4_ADDRESS;
5068 				/* local from address */
5069 				stc.laddress[0] = to_sin->sin_addr.s_addr;
5070 				stc.laddress[1] = 0;
5071 				stc.laddress[2] = 0;
5072 				stc.laddress[3] = 0;
5073 				stc.laddr_type = SCTP_IPV4_ADDRESS;
5074 				/* scope_id is only for v6 */
5075 				stc.scope_id = 0;
5076 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5077 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5078 					stc.ipv4_scope = 1;
5079 				}
5080 #else
5081 				stc.ipv4_scope = 1;
5082 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
5083 				/* Must use the address in this case */
5084 				if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5085 					stc.loopback_scope = 1;
5086 					stc.ipv4_scope = 1;
5087 					stc.site_scope = 1;
5088 					stc.local_scope = 0;
5089 				}
5090 				break;
5091 			}
5092 #ifdef INET6
5093 		case IPV6_VERSION >> 4:
5094 			{
5095 				ip6 = mtod(init_pkt, struct ip6_hdr *);
5096 				sin6->sin6_family = AF_INET6;
5097 				sin6->sin6_len = sizeof(struct sockaddr_in6);
5098 				sin6->sin6_port = sh->src_port;
5099 				sin6->sin6_addr = ip6->ip6_src;
5100 				/* lookup address */
5101 				memcpy(&stc.address, &sin6->sin6_addr,
5102 				    sizeof(struct in6_addr));
5103 				sin6->sin6_scope_id = 0;
5104 				stc.addr_type = SCTP_IPV6_ADDRESS;
5105 				stc.scope_id = 0;
5106 				if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5107 					/*
5108 					 * FIX ME: does this have scope from
5109 					 * rcvif?
5110 					 */
5111 					(void)sa6_recoverscope(sin6);
5112 					stc.scope_id = sin6->sin6_scope_id;
5113 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5114 					stc.loopback_scope = 1;
5115 					stc.local_scope = 0;
5116 					stc.site_scope = 1;
5117 					stc.ipv4_scope = 1;
5118 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5119 					/*
5120 					 * If the new destination is a
5121 					 * LINK_LOCAL we must have common
5122 					 * both site and local scope. Don't
5123 					 * set local scope though since we
5124 					 * must depend on the source to be
5125 					 * added implicitly. We cannot
5126 					 * assure just because we share one
5127 					 * link that all links are common.
5128 					 */
5129 					stc.local_scope = 0;
5130 					stc.site_scope = 1;
5131 					stc.ipv4_scope = 1;
5132 					/*
5133 					 * we start counting for the private
5134 					 * address stuff at 1. since the
5135 					 * link local we source from won't
5136 					 * show up in our scoped count.
5137 					 */
5138 					cnt_inits_to = 1;
5139 					/*
5140 					 * pull out the scope_id from
5141 					 * incoming pkt
5142 					 */
5143 					/*
5144 					 * FIX ME: does this have scope from
5145 					 * rcvif?
5146 					 */
5147 					(void)sa6_recoverscope(sin6);
5148 					stc.scope_id = sin6->sin6_scope_id;
5149 					sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5150 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5151 					/*
5152 					 * If the new destination is
5153 					 * SITE_LOCAL then we must have site
5154 					 * scope in common.
5155 					 */
5156 					stc.site_scope = 1;
5157 				}
5158 				memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5159 				stc.laddr_type = SCTP_IPV6_ADDRESS;
5160 				break;
5161 			}
5162 #endif
5163 		default:
5164 			/* TSNH */
5165 			goto do_a_abort;
5166 			break;
5167 		}
5168 	} else {
5169 		/* set the scope per the existing tcb */
5170 
5171 #ifdef INET6
5172 		struct sctp_nets *lnet;
5173 
5174 #endif
5175 
5176 		stc.loopback_scope = asoc->loopback_scope;
5177 		stc.ipv4_scope = asoc->ipv4_local_scope;
5178 		stc.site_scope = asoc->site_scope;
5179 		stc.local_scope = asoc->local_scope;
5180 #ifdef INET6
5181 		/* Why do we not consider IPv4 LL addresses? */
5182 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5183 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5184 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5185 					/*
5186 					 * if we have a LL address, start
5187 					 * counting at 1.
5188 					 */
5189 					cnt_inits_to = 1;
5190 				}
5191 			}
5192 		}
5193 #endif
5194 		/* use the net pointer */
5195 		to = (struct sockaddr *)&net->ro._l_addr;
5196 		switch (to->sa_family) {
5197 		case AF_INET:
5198 			sin = (struct sockaddr_in *)to;
5199 			stc.address[0] = sin->sin_addr.s_addr;
5200 			stc.address[1] = 0;
5201 			stc.address[2] = 0;
5202 			stc.address[3] = 0;
5203 			stc.addr_type = SCTP_IPV4_ADDRESS;
5204 			if (net->src_addr_selected == 0) {
5205 				/*
5206 				 * strange case here, the INIT should have
5207 				 * did the selection.
5208 				 */
5209 				net->ro._s_addr = sctp_source_address_selection(inp,
5210 				    stcb, (sctp_route_t *) & net->ro,
5211 				    net, 0, vrf_id);
5212 				if (net->ro._s_addr == NULL)
5213 					return;
5214 
5215 				net->src_addr_selected = 1;
5216 
5217 			}
5218 			stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5219 			stc.laddress[1] = 0;
5220 			stc.laddress[2] = 0;
5221 			stc.laddress[3] = 0;
5222 			stc.laddr_type = SCTP_IPV4_ADDRESS;
5223 			break;
5224 #ifdef INET6
5225 		case AF_INET6:
5226 			sin6 = (struct sockaddr_in6 *)to;
5227 			memcpy(&stc.address, &sin6->sin6_addr,
5228 			    sizeof(struct in6_addr));
5229 			stc.addr_type = SCTP_IPV6_ADDRESS;
5230 			if (net->src_addr_selected == 0) {
5231 				/*
5232 				 * strange case here, the INIT should have
5233 				 * did the selection.
5234 				 */
5235 				net->ro._s_addr = sctp_source_address_selection(inp,
5236 				    stcb, (sctp_route_t *) & net->ro,
5237 				    net, 0, vrf_id);
5238 				if (net->ro._s_addr == NULL)
5239 					return;
5240 
5241 				net->src_addr_selected = 1;
5242 			}
5243 			memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5244 			    sizeof(struct in6_addr));
5245 			stc.laddr_type = SCTP_IPV6_ADDRESS;
5246 			break;
5247 #endif
5248 		}
5249 	}
5250 	/* Now lets put the SCTP header in place */
5251 	initack = mtod(m, struct sctp_init_ack_chunk *);
5252 	/* Save it off for quick ref */
5253 	stc.peers_vtag = init_chk->init.initiate_tag;
5254 	/* who are we */
5255 	memcpy(stc.identification, SCTP_VERSION_STRING,
5256 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5257 	/* now the chunk header */
5258 	initack->ch.chunk_type = SCTP_INITIATION_ACK;
5259 	initack->ch.chunk_flags = 0;
5260 	/* fill in later from mbuf we build */
5261 	initack->ch.chunk_length = 0;
5262 	/* place in my tag */
5263 	if ((asoc != NULL) &&
5264 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5265 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5266 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5267 		/* re-use the v-tags and init-seq here */
5268 		initack->init.initiate_tag = htonl(asoc->my_vtag);
5269 		initack->init.initial_tsn = htonl(asoc->init_seq_number);
5270 	} else {
5271 		uint32_t vtag, itsn;
5272 
5273 		if (hold_inp_lock) {
5274 			SCTP_INP_INCR_REF(inp);
5275 			SCTP_INP_RUNLOCK(inp);
5276 		}
5277 		if (asoc) {
5278 			atomic_add_int(&asoc->refcnt, 1);
5279 			SCTP_TCB_UNLOCK(stcb);
5280 	new_tag:
5281 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5282 			if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5283 				/*
5284 				 * Got a duplicate vtag on some guy behind a
5285 				 * nat make sure we don't use it.
5286 				 */
5287 				goto new_tag;
5288 			}
5289 			initack->init.initiate_tag = htonl(vtag);
5290 			/* get a TSN to use too */
5291 			itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5292 			initack->init.initial_tsn = htonl(itsn);
5293 			SCTP_TCB_LOCK(stcb);
5294 			atomic_add_int(&asoc->refcnt, -1);
5295 		} else {
5296 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5297 			initack->init.initiate_tag = htonl(vtag);
5298 			/* get a TSN to use too */
5299 			initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5300 		}
5301 		if (hold_inp_lock) {
5302 			SCTP_INP_RLOCK(inp);
5303 			SCTP_INP_DECR_REF(inp);
5304 		}
5305 	}
5306 	/* save away my tag to */
5307 	stc.my_vtag = initack->init.initiate_tag;
5308 
5309 	/* set up some of the credits. */
5310 	so = inp->sctp_socket;
5311 	if (so == NULL) {
5312 		/* memory problem */
5313 		sctp_m_freem(m);
5314 		return;
5315 	} else {
5316 		initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5317 	}
5318 	/* set what I want */
5319 	his_limit = ntohs(init_chk->init.num_inbound_streams);
5320 	/* choose what I want */
5321 	if (asoc != NULL) {
5322 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5323 			i_want = asoc->streamoutcnt;
5324 		} else {
5325 			i_want = inp->sctp_ep.pre_open_stream_count;
5326 		}
5327 	} else {
5328 		i_want = inp->sctp_ep.pre_open_stream_count;
5329 	}
5330 	if (his_limit < i_want) {
5331 		/* I Want more :< */
5332 		initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5333 	} else {
5334 		/* I can have what I want :> */
5335 		initack->init.num_outbound_streams = htons(i_want);
5336 	}
5337 	/* tell him his limt. */
5338 	initack->init.num_inbound_streams =
5339 	    htons(inp->sctp_ep.max_open_streams_intome);
5340 
5341 	/* adaptation layer indication parameter */
5342 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5343 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5344 	ali->ph.param_length = htons(sizeof(*ali));
5345 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5346 	SCTP_BUF_LEN(m) += sizeof(*ali);
5347 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5348 
5349 	/* ECN parameter */
5350 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5351 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5352 		ecn->ph.param_length = htons(sizeof(*ecn));
5353 		SCTP_BUF_LEN(m) += sizeof(*ecn);
5354 
5355 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5356 		    sizeof(*ecn));
5357 	} else {
5358 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5359 	}
5360 	/* And now tell the peer we do  pr-sctp */
5361 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5362 	prsctp->ph.param_length = htons(sizeof(*prsctp));
5363 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
5364 	if (nat_friendly) {
5365 		/* Add NAT friendly parameter */
5366 		struct sctp_paramhdr *ph;
5367 
5368 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5369 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5370 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
5371 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5372 	}
5373 	/* And now tell the peer we do all the extensions */
5374 	pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5375 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5376 	num_ext = 0;
5377 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5378 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5379 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5380 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5381 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5382 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5383 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5384 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5385 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5386 	p_len = sizeof(*pr_supported) + num_ext;
5387 	pr_supported->ph.param_length = htons(p_len);
5388 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5389 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5390 
5391 	/* add authentication parameters */
5392 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5393 		struct sctp_auth_random *randp;
5394 		struct sctp_auth_hmac_algo *hmacs;
5395 		struct sctp_auth_chunk_list *chunks;
5396 		uint16_t random_len;
5397 
5398 		/* generate and add RANDOM parameter */
5399 		random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5400 		randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5401 		randp->ph.param_type = htons(SCTP_RANDOM);
5402 		p_len = sizeof(*randp) + random_len;
5403 		randp->ph.param_length = htons(p_len);
5404 		SCTP_READ_RANDOM(randp->random_data, random_len);
5405 		/* zero out any padding required */
5406 		bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5407 		SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5408 
5409 		/* add HMAC_ALGO parameter */
5410 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5411 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5412 		    (uint8_t *) hmacs->hmac_ids);
5413 		if (p_len > 0) {
5414 			p_len += sizeof(*hmacs);
5415 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5416 			hmacs->ph.param_length = htons(p_len);
5417 			/* zero out any padding required */
5418 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5419 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5420 		}
5421 		/* add CHUNKS parameter */
5422 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5423 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5424 		    chunks->chunk_types);
5425 		if (p_len > 0) {
5426 			p_len += sizeof(*chunks);
5427 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5428 			chunks->ph.param_length = htons(p_len);
5429 			/* zero out any padding required */
5430 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5431 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5432 		}
5433 	}
5434 	m_at = m;
5435 	/* now the addresses */
5436 	{
5437 		struct sctp_scoping scp;
5438 
5439 		/*
5440 		 * To optimize this we could put the scoping stuff into a
5441 		 * structure and remove the individual uint8's from the stc
5442 		 * structure. Then we could just sifa in the address within
5443 		 * the stc.. but for now this is a quick hack to get the
5444 		 * address stuff teased apart.
5445 		 */
5446 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5447 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5448 		scp.loopback_scope = stc.loopback_scope;
5449 		scp.ipv4_local_scope = stc.ipv4_scope;
5450 		scp.local_scope = stc.local_scope;
5451 		scp.site_scope = stc.site_scope;
5452 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5453 	}
5454 
5455 	/* tack on the operational error if present */
5456 	if (op_err) {
5457 		struct mbuf *ol;
5458 		int llen;
5459 
5460 		llen = 0;
5461 		ol = op_err;
5462 		while (ol) {
5463 			llen += SCTP_BUF_LEN(ol);
5464 			ol = SCTP_BUF_NEXT(ol);
5465 		}
5466 		if (llen % 4) {
5467 			/* must add a pad to the param */
5468 			uint32_t cpthis = 0;
5469 			int padlen;
5470 
5471 			padlen = 4 - (llen % 4);
5472 			m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5473 		}
5474 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5475 			m_at = SCTP_BUF_NEXT(m_at);
5476 		}
5477 		SCTP_BUF_NEXT(m_at) = op_err;
5478 		while (SCTP_BUF_NEXT(m_at) != NULL) {
5479 			m_at = SCTP_BUF_NEXT(m_at);
5480 		}
5481 	}
5482 	/* pre-calulate the size and update pkt header and chunk header */
5483 	p_len = 0;
5484 	for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5485 		p_len += SCTP_BUF_LEN(m_tmp);
5486 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5487 			/* m_tmp should now point to last one */
5488 			break;
5489 		}
5490 	}
5491 
5492 	/* Now we must build a cookie */
5493 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5494 	if (m_cookie == NULL) {
5495 		/* memory problem */
5496 		sctp_m_freem(m);
5497 		return;
5498 	}
5499 	/* Now append the cookie to the end and update the space/size */
5500 	SCTP_BUF_NEXT(m_tmp) = m_cookie;
5501 
5502 	for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5503 		p_len += SCTP_BUF_LEN(m_tmp);
5504 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5505 			/* m_tmp should now point to last one */
5506 			mp_last = m_tmp;
5507 			break;
5508 		}
5509 	}
5510 	/*
5511 	 * Place in the size, but we don't include the last pad (if any) in
5512 	 * the INIT-ACK.
5513 	 */
5514 	initack->ch.chunk_length = htons(p_len);
5515 
5516 	/*
5517 	 * Time to sign the cookie, we don't sign over the cookie signature
5518 	 * though thus we set trailer.
5519 	 */
5520 	(void)sctp_hmac_m(SCTP_HMAC,
5521 	    (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5522 	    SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5523 	    (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5524 	/*
5525 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5526 	 * here since the timer will drive a retranmission.
5527 	 */
5528 	padval = p_len % 4;
5529 	if ((padval) && (mp_last)) {
5530 		/* see my previous comments on mp_last */
5531 		int ret;
5532 
5533 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5534 		if (ret) {
5535 			/* Houston we have a problem, no space */
5536 			sctp_m_freem(m);
5537 			return;
5538 		}
5539 		p_len += padval;
5540 	}
5541 	if (stc.loopback_scope) {
5542 		over_addr = &store1;
5543 	} else {
5544 		over_addr = NULL;
5545 	}
5546 
5547 	(void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5548 	    0, NULL, 0,
5549 	    inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
5550 	    port, SCTP_SO_NOT_LOCKED, over_addr);
5551 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5552 }
5553 
5554 
5555 static void
5556 sctp_prune_prsctp(struct sctp_tcb *stcb,
5557     struct sctp_association *asoc,
5558     struct sctp_sndrcvinfo *srcv,
5559     int dataout)
5560 {
5561 	int freed_spc = 0;
5562 	struct sctp_tmit_chunk *chk, *nchk;
5563 
5564 	SCTP_TCB_LOCK_ASSERT(stcb);
5565 	if ((asoc->peer_supports_prsctp) &&
5566 	    (asoc->sent_queue_cnt_removeable > 0)) {
5567 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5568 			/*
5569 			 * Look for chunks marked with the PR_SCTP flag AND
5570 			 * the buffer space flag. If the one being sent is
5571 			 * equal or greater priority then purge the old one
5572 			 * and free some space.
5573 			 */
5574 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5575 				/*
5576 				 * This one is PR-SCTP AND buffer space
5577 				 * limited type
5578 				 */
5579 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5580 					/*
5581 					 * Lower numbers equates to higher
5582 					 * priority so if the one we are
5583 					 * looking at has a larger or equal
5584 					 * priority we want to drop the data
5585 					 * and NOT retransmit it.
5586 					 */
5587 					if (chk->data) {
5588 						/*
5589 						 * We release the book_size
5590 						 * if the mbuf is here
5591 						 */
5592 						int ret_spc;
5593 						int cause;
5594 
5595 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
5596 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5597 						else
5598 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5599 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5600 						    cause,
5601 						    SCTP_SO_LOCKED);
5602 						freed_spc += ret_spc;
5603 						if (freed_spc >= dataout) {
5604 							return;
5605 						}
5606 					}	/* if chunk was present */
5607 				}	/* if of sufficent priority */
5608 			}	/* if chunk has enabled */
5609 		}		/* tailqforeach */
5610 
5611 		TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
5612 			/* Here we must move to the sent queue and mark */
5613 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5614 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5615 					if (chk->data) {
5616 						/*
5617 						 * We release the book_size
5618 						 * if the mbuf is here
5619 						 */
5620 						int ret_spc;
5621 
5622 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5623 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5624 						    SCTP_SO_LOCKED);
5625 
5626 						freed_spc += ret_spc;
5627 						if (freed_spc >= dataout) {
5628 							return;
5629 						}
5630 					}	/* end if chk->data */
5631 				}	/* end if right class */
5632 			}	/* end if chk pr-sctp */
5633 		}		/* tailqforeachsafe (chk) */
5634 	}			/* if enabled in asoc */
5635 }
5636 
5637 int
5638 sctp_get_frag_point(struct sctp_tcb *stcb,
5639     struct sctp_association *asoc)
5640 {
5641 	int siz, ovh;
5642 
5643 	/*
5644 	 * For endpoints that have both v6 and v4 addresses we must reserve
5645 	 * room for the ipv6 header, for those that are only dealing with V4
5646 	 * we use a larger frag point.
5647 	 */
5648 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5649 		ovh = SCTP_MED_OVERHEAD;
5650 	} else {
5651 		ovh = SCTP_MED_V4_OVERHEAD;
5652 	}
5653 
5654 	if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5655 		siz = asoc->smallest_mtu - ovh;
5656 	else
5657 		siz = (stcb->asoc.sctp_frag_point - ovh);
5658 	/*
5659 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5660 	 */
5661 	/* A data chunk MUST fit in a cluster */
5662 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5663 	/* } */
5664 
5665 	/* adjust for an AUTH chunk if DATA requires auth */
5666 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5667 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5668 
5669 	if (siz % 4) {
5670 		/* make it an even word boundary please */
5671 		siz -= (siz % 4);
5672 	}
5673 	return (siz);
5674 }
5675 
5676 static void
5677 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
5678 {
5679 	sp->pr_sctp_on = 0;
5680 	/*
5681 	 * We assume that the user wants PR_SCTP_TTL if the user provides a
5682 	 * positive lifetime but does not specify any PR_SCTP policy. This
5683 	 * is a BAD assumption and causes problems at least with the
5684 	 * U-Vancovers MPI folks. I will change this to be no policy means
5685 	 * NO PR-SCTP.
5686 	 */
5687 	if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5688 		sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5689 		sp->pr_sctp_on = 1;
5690 	} else {
5691 		return;
5692 	}
5693 	switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5694 	case CHUNK_FLAGS_PR_SCTP_BUF:
5695 		/*
5696 		 * Time to live is a priority stored in tv_sec when doing
5697 		 * the buffer drop thing.
5698 		 */
5699 		sp->ts.tv_sec = sp->timetolive;
5700 		sp->ts.tv_usec = 0;
5701 		break;
5702 	case CHUNK_FLAGS_PR_SCTP_TTL:
5703 		{
5704 			struct timeval tv;
5705 
5706 			(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5707 			tv.tv_sec = sp->timetolive / 1000;
5708 			tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5709 			/*
5710 			 * TODO sctp_constants.h needs alternative time
5711 			 * macros when _KERNEL is undefined.
5712 			 */
5713 			timevaladd(&sp->ts, &tv);
5714 		}
5715 		break;
5716 	case CHUNK_FLAGS_PR_SCTP_RTX:
5717 		/*
5718 		 * Time to live is a the number or retransmissions stored in
5719 		 * tv_sec.
5720 		 */
5721 		sp->ts.tv_sec = sp->timetolive;
5722 		sp->ts.tv_usec = 0;
5723 		break;
5724 	default:
5725 		SCTPDBG(SCTP_DEBUG_USRREQ1,
5726 		    "Unknown PR_SCTP policy %u.\n",
5727 		    PR_SCTP_POLICY(sp->sinfo_flags));
5728 		break;
5729 	}
5730 }
5731 
5732 static int
5733 sctp_msg_append(struct sctp_tcb *stcb,
5734     struct sctp_nets *net,
5735     struct mbuf *m,
5736     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5737 {
5738 	int error = 0, holds_lock;
5739 	struct mbuf *at;
5740 	struct sctp_stream_queue_pending *sp = NULL;
5741 	struct sctp_stream_out *strm;
5742 
5743 	/*
5744 	 * Given an mbuf chain, put it into the association send queue and
5745 	 * place it on the wheel
5746 	 */
5747 	holds_lock = hold_stcb_lock;
5748 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5749 		/* Invalid stream number */
5750 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5751 		error = EINVAL;
5752 		goto out_now;
5753 	}
5754 	if ((stcb->asoc.stream_locked) &&
5755 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5756 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5757 		error = EINVAL;
5758 		goto out_now;
5759 	}
5760 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5761 	/* Now can we send this? */
5762 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5763 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5764 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5765 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5766 		/* got data while shutting down */
5767 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5768 		error = ECONNRESET;
5769 		goto out_now;
5770 	}
5771 	sctp_alloc_a_strmoq(stcb, sp);
5772 	if (sp == NULL) {
5773 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5774 		error = ENOMEM;
5775 		goto out_now;
5776 	}
5777 	sp->sinfo_flags = srcv->sinfo_flags;
5778 	sp->timetolive = srcv->sinfo_timetolive;
5779 	sp->ppid = srcv->sinfo_ppid;
5780 	sp->context = srcv->sinfo_context;
5781 	sp->strseq = 0;
5782 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5783 		sp->net = net;
5784 		atomic_add_int(&sp->net->ref_count, 1);
5785 	} else {
5786 		sp->net = NULL;
5787 	}
5788 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5789 	sp->stream = srcv->sinfo_stream;
5790 	sp->msg_is_complete = 1;
5791 	sp->sender_all_done = 1;
5792 	sp->some_taken = 0;
5793 	sp->data = m;
5794 	sp->tail_mbuf = NULL;
5795 	sp->length = 0;
5796 	at = m;
5797 	sctp_set_prsctp_policy(sp);
5798 	/*
5799 	 * We could in theory (for sendall) sifa the length in, but we would
5800 	 * still have to hunt through the chain since we need to setup the
5801 	 * tail_mbuf
5802 	 */
5803 	while (at) {
5804 		if (SCTP_BUF_NEXT(at) == NULL)
5805 			sp->tail_mbuf = at;
5806 		sp->length += SCTP_BUF_LEN(at);
5807 		at = SCTP_BUF_NEXT(at);
5808 	}
5809 	SCTP_TCB_SEND_LOCK(stcb);
5810 	sctp_snd_sb_alloc(stcb, sp->length);
5811 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5812 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5813 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5814 		sp->strseq = strm->next_sequence_sent;
5815 		strm->next_sequence_sent++;
5816 	}
5817 	stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1);
5818 	m = NULL;
5819 	SCTP_TCB_SEND_UNLOCK(stcb);
5820 out_now:
5821 	if (m) {
5822 		sctp_m_freem(m);
5823 	}
5824 	return (error);
5825 }
5826 
5827 
5828 static struct mbuf *
5829 sctp_copy_mbufchain(struct mbuf *clonechain,
5830     struct mbuf *outchain,
5831     struct mbuf **endofchain,
5832     int can_take_mbuf,
5833     int sizeofcpy,
5834     uint8_t copy_by_ref)
5835 {
5836 	struct mbuf *m;
5837 	struct mbuf *appendchain;
5838 	caddr_t cp;
5839 	int len;
5840 
5841 	if (endofchain == NULL) {
5842 		/* error */
5843 error_out:
5844 		if (outchain)
5845 			sctp_m_freem(outchain);
5846 		return (NULL);
5847 	}
5848 	if (can_take_mbuf) {
5849 		appendchain = clonechain;
5850 	} else {
5851 		if (!copy_by_ref &&
5852 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5853 		    ) {
5854 			/* Its not in a cluster */
5855 			if (*endofchain == NULL) {
5856 				/* lets get a mbuf cluster */
5857 				if (outchain == NULL) {
5858 					/* This is the general case */
5859 			new_mbuf:
5860 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5861 					if (outchain == NULL) {
5862 						goto error_out;
5863 					}
5864 					SCTP_BUF_LEN(outchain) = 0;
5865 					*endofchain = outchain;
5866 					/* get the prepend space */
5867 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5868 				} else {
5869 					/*
5870 					 * We really should not get a NULL
5871 					 * in endofchain
5872 					 */
5873 					/* find end */
5874 					m = outchain;
5875 					while (m) {
5876 						if (SCTP_BUF_NEXT(m) == NULL) {
5877 							*endofchain = m;
5878 							break;
5879 						}
5880 						m = SCTP_BUF_NEXT(m);
5881 					}
5882 					/* sanity */
5883 					if (*endofchain == NULL) {
5884 						/*
5885 						 * huh, TSNH XXX maybe we
5886 						 * should panic
5887 						 */
5888 						sctp_m_freem(outchain);
5889 						goto new_mbuf;
5890 					}
5891 				}
5892 				/* get the new end of length */
5893 				len = M_TRAILINGSPACE(*endofchain);
5894 			} else {
5895 				/* how much is left at the end? */
5896 				len = M_TRAILINGSPACE(*endofchain);
5897 			}
5898 			/* Find the end of the data, for appending */
5899 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
5900 
5901 			/* Now lets copy it out */
5902 			if (len >= sizeofcpy) {
5903 				/* It all fits, copy it in */
5904 				m_copydata(clonechain, 0, sizeofcpy, cp);
5905 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5906 			} else {
5907 				/* fill up the end of the chain */
5908 				if (len > 0) {
5909 					m_copydata(clonechain, 0, len, cp);
5910 					SCTP_BUF_LEN((*endofchain)) += len;
5911 					/* now we need another one */
5912 					sizeofcpy -= len;
5913 				}
5914 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5915 				if (m == NULL) {
5916 					/* We failed */
5917 					goto error_out;
5918 				}
5919 				SCTP_BUF_NEXT((*endofchain)) = m;
5920 				*endofchain = m;
5921 				cp = mtod((*endofchain), caddr_t);
5922 				m_copydata(clonechain, len, sizeofcpy, cp);
5923 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
5924 			}
5925 			return (outchain);
5926 		} else {
5927 			/* copy the old fashion way */
5928 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
5929 #ifdef SCTP_MBUF_LOGGING
5930 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5931 				struct mbuf *mat;
5932 
5933 				mat = appendchain;
5934 				while (mat) {
5935 					if (SCTP_BUF_IS_EXTENDED(mat)) {
5936 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5937 					}
5938 					mat = SCTP_BUF_NEXT(mat);
5939 				}
5940 			}
5941 #endif
5942 		}
5943 	}
5944 	if (appendchain == NULL) {
5945 		/* error */
5946 		if (outchain)
5947 			sctp_m_freem(outchain);
5948 		return (NULL);
5949 	}
5950 	if (outchain) {
5951 		/* tack on to the end */
5952 		if (*endofchain != NULL) {
5953 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
5954 		} else {
5955 			m = outchain;
5956 			while (m) {
5957 				if (SCTP_BUF_NEXT(m) == NULL) {
5958 					SCTP_BUF_NEXT(m) = appendchain;
5959 					break;
5960 				}
5961 				m = SCTP_BUF_NEXT(m);
5962 			}
5963 		}
5964 		/*
5965 		 * save off the end and update the end-chain postion
5966 		 */
5967 		m = appendchain;
5968 		while (m) {
5969 			if (SCTP_BUF_NEXT(m) == NULL) {
5970 				*endofchain = m;
5971 				break;
5972 			}
5973 			m = SCTP_BUF_NEXT(m);
5974 		}
5975 		return (outchain);
5976 	} else {
5977 		/* save off the end and update the end-chain postion */
5978 		m = appendchain;
5979 		while (m) {
5980 			if (SCTP_BUF_NEXT(m) == NULL) {
5981 				*endofchain = m;
5982 				break;
5983 			}
5984 			m = SCTP_BUF_NEXT(m);
5985 		}
5986 		return (appendchain);
5987 	}
5988 }
5989 
5990 int
5991 sctp_med_chunk_output(struct sctp_inpcb *inp,
5992     struct sctp_tcb *stcb,
5993     struct sctp_association *asoc,
5994     int *num_out,
5995     int *reason_code,
5996     int control_only, int from_where,
5997     struct timeval *now, int *now_filled, int frag_point, int so_locked
5998 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5999     SCTP_UNUSED
6000 #endif
6001 );
6002 
6003 static void
6004 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6005     uint32_t val)
6006 {
6007 	struct sctp_copy_all *ca;
6008 	struct mbuf *m;
6009 	int ret = 0;
6010 	int added_control = 0;
6011 	int un_sent, do_chunk_output = 1;
6012 	struct sctp_association *asoc;
6013 
6014 	ca = (struct sctp_copy_all *)ptr;
6015 	if (ca->m == NULL) {
6016 		return;
6017 	}
6018 	if (ca->inp != inp) {
6019 		/* TSNH */
6020 		return;
6021 	}
6022 	if ((ca->m) && ca->sndlen) {
6023 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6024 		if (m == NULL) {
6025 			/* can't copy so we are done */
6026 			ca->cnt_failed++;
6027 			return;
6028 		}
6029 #ifdef SCTP_MBUF_LOGGING
6030 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6031 			struct mbuf *mat;
6032 
6033 			mat = m;
6034 			while (mat) {
6035 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6036 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6037 				}
6038 				mat = SCTP_BUF_NEXT(mat);
6039 			}
6040 		}
6041 #endif
6042 	} else {
6043 		m = NULL;
6044 	}
6045 	SCTP_TCB_LOCK_ASSERT(stcb);
6046 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6047 		/* Abort this assoc with m as the user defined reason */
6048 		if (m) {
6049 			struct sctp_paramhdr *ph;
6050 
6051 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6052 			if (m) {
6053 				ph = mtod(m, struct sctp_paramhdr *);
6054 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6055 				ph->param_length = htons(ca->sndlen);
6056 			}
6057 			/*
6058 			 * We add one here to keep the assoc from
6059 			 * dis-appearing on us.
6060 			 */
6061 			atomic_add_int(&stcb->asoc.refcnt, 1);
6062 			sctp_abort_an_association(inp, stcb,
6063 			    SCTP_RESPONSE_TO_USER_REQ,
6064 			    m, SCTP_SO_NOT_LOCKED);
6065 			/*
6066 			 * sctp_abort_an_association calls sctp_free_asoc()
6067 			 * free association will NOT free it since we
6068 			 * incremented the refcnt .. we do this to prevent
6069 			 * it being freed and things getting tricky since we
6070 			 * could end up (from free_asoc) calling inpcb_free
6071 			 * which would get a recursive lock call to the
6072 			 * iterator lock.. But as a consequence of that the
6073 			 * stcb will return to us un-locked.. since
6074 			 * free_asoc returns with either no TCB or the TCB
6075 			 * unlocked, we must relock.. to unlock in the
6076 			 * iterator timer :-0
6077 			 */
6078 			SCTP_TCB_LOCK(stcb);
6079 			atomic_add_int(&stcb->asoc.refcnt, -1);
6080 			goto no_chunk_output;
6081 		}
6082 	} else {
6083 		if (m) {
6084 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6085 			    &ca->sndrcv, 1);
6086 		}
6087 		asoc = &stcb->asoc;
6088 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6089 			/* shutdown this assoc */
6090 			int cnt;
6091 
6092 			cnt = sctp_is_there_unsent_data(stcb);
6093 
6094 			if (TAILQ_EMPTY(&asoc->send_queue) &&
6095 			    TAILQ_EMPTY(&asoc->sent_queue) &&
6096 			    (cnt == 0)) {
6097 				if (asoc->locked_on_sending) {
6098 					goto abort_anyway;
6099 				}
6100 				/*
6101 				 * there is nothing queued to send, so I'm
6102 				 * done...
6103 				 */
6104 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6105 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6106 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6107 					/*
6108 					 * only send SHUTDOWN the first time
6109 					 * through
6110 					 */
6111 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6112 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6113 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6114 					}
6115 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6116 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6117 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6118 					    asoc->primary_destination);
6119 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6120 					    asoc->primary_destination);
6121 					added_control = 1;
6122 					do_chunk_output = 0;
6123 				}
6124 			} else {
6125 				/*
6126 				 * we still got (or just got) data to send,
6127 				 * so set SHUTDOWN_PENDING
6128 				 */
6129 				/*
6130 				 * XXX sockets draft says that SCTP_EOF
6131 				 * should be sent with no data.  currently,
6132 				 * we will allow user data to be sent first
6133 				 * and move to SHUTDOWN-PENDING
6134 				 */
6135 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6136 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6137 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6138 					if (asoc->locked_on_sending) {
6139 						/*
6140 						 * Locked to send out the
6141 						 * data
6142 						 */
6143 						struct sctp_stream_queue_pending *sp;
6144 
6145 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6146 						if (sp) {
6147 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
6148 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6149 						}
6150 					}
6151 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6152 					if (TAILQ_EMPTY(&asoc->send_queue) &&
6153 					    TAILQ_EMPTY(&asoc->sent_queue) &&
6154 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6155 				abort_anyway:
6156 						atomic_add_int(&stcb->asoc.refcnt, 1);
6157 						sctp_abort_an_association(stcb->sctp_ep, stcb,
6158 						    SCTP_RESPONSE_TO_USER_REQ,
6159 						    NULL, SCTP_SO_NOT_LOCKED);
6160 						atomic_add_int(&stcb->asoc.refcnt, -1);
6161 						goto no_chunk_output;
6162 					}
6163 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6164 					    asoc->primary_destination);
6165 				}
6166 			}
6167 
6168 		}
6169 	}
6170 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6171 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6172 
6173 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6174 	    (stcb->asoc.total_flight > 0) &&
6175 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6176 	    ) {
6177 		do_chunk_output = 0;
6178 	}
6179 	if (do_chunk_output)
6180 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6181 	else if (added_control) {
6182 		int num_out = 0, reason = 0, now_filled = 0;
6183 		struct timeval now;
6184 		int frag_point;
6185 
6186 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6187 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6188 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6189 	}
6190 no_chunk_output:
6191 	if (ret) {
6192 		ca->cnt_failed++;
6193 	} else {
6194 		ca->cnt_sent++;
6195 	}
6196 }
6197 
6198 static void
6199 sctp_sendall_completes(void *ptr, uint32_t val)
6200 {
6201 	struct sctp_copy_all *ca;
6202 
6203 	ca = (struct sctp_copy_all *)ptr;
6204 	/*
6205 	 * Do a notify here? Kacheong suggests that the notify be done at
6206 	 * the send time.. so you would push up a notification if any send
6207 	 * failed. Don't know if this is feasable since the only failures we
6208 	 * have is "memory" related and if you cannot get an mbuf to send
6209 	 * the data you surely can't get an mbuf to send up to notify the
6210 	 * user you can't send the data :->
6211 	 */
6212 
6213 	/* now free everything */
6214 	sctp_m_freem(ca->m);
6215 	SCTP_FREE(ca, SCTP_M_COPYAL);
6216 }
6217 
6218 
6219 #define	MC_ALIGN(m, len) do {						\
6220 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
6221 } while (0)
6222 
6223 
6224 
6225 static struct mbuf *
6226 sctp_copy_out_all(struct uio *uio, int len)
6227 {
6228 	struct mbuf *ret, *at;
6229 	int left, willcpy, cancpy, error;
6230 
6231 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6232 	if (ret == NULL) {
6233 		/* TSNH */
6234 		return (NULL);
6235 	}
6236 	left = len;
6237 	SCTP_BUF_LEN(ret) = 0;
6238 	/* save space for the data chunk header */
6239 	cancpy = M_TRAILINGSPACE(ret);
6240 	willcpy = min(cancpy, left);
6241 	at = ret;
6242 	while (left > 0) {
6243 		/* Align data to the end */
6244 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
6245 		if (error) {
6246 	err_out_now:
6247 			sctp_m_freem(at);
6248 			return (NULL);
6249 		}
6250 		SCTP_BUF_LEN(at) = willcpy;
6251 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6252 		left -= willcpy;
6253 		if (left > 0) {
6254 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6255 			if (SCTP_BUF_NEXT(at) == NULL) {
6256 				goto err_out_now;
6257 			}
6258 			at = SCTP_BUF_NEXT(at);
6259 			SCTP_BUF_LEN(at) = 0;
6260 			cancpy = M_TRAILINGSPACE(at);
6261 			willcpy = min(cancpy, left);
6262 		}
6263 	}
6264 	return (ret);
6265 }
6266 
6267 static int
6268 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6269     struct sctp_sndrcvinfo *srcv)
6270 {
6271 	int ret;
6272 	struct sctp_copy_all *ca;
6273 
6274 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6275 	    SCTP_M_COPYAL);
6276 	if (ca == NULL) {
6277 		sctp_m_freem(m);
6278 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6279 		return (ENOMEM);
6280 	}
6281 	memset(ca, 0, sizeof(struct sctp_copy_all));
6282 
6283 	ca->inp = inp;
6284 	memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6285 	/*
6286 	 * take off the sendall flag, it would be bad if we failed to do
6287 	 * this :-0
6288 	 */
6289 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6290 	/* get length and mbuf chain */
6291 	if (uio) {
6292 		ca->sndlen = uio->uio_resid;
6293 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
6294 		if (ca->m == NULL) {
6295 			SCTP_FREE(ca, SCTP_M_COPYAL);
6296 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6297 			return (ENOMEM);
6298 		}
6299 	} else {
6300 		/* Gather the length of the send */
6301 		struct mbuf *mat;
6302 
6303 		mat = m;
6304 		ca->sndlen = 0;
6305 		while (m) {
6306 			ca->sndlen += SCTP_BUF_LEN(m);
6307 			m = SCTP_BUF_NEXT(m);
6308 		}
6309 		ca->m = mat;
6310 	}
6311 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6312 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6313 	    SCTP_ASOC_ANY_STATE,
6314 	    (void *)ca, 0,
6315 	    sctp_sendall_completes, inp, 1);
6316 	if (ret) {
6317 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6318 		SCTP_FREE(ca, SCTP_M_COPYAL);
6319 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6320 		return (EFAULT);
6321 	}
6322 	return (0);
6323 }
6324 
6325 
6326 void
6327 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6328 {
6329 	struct sctp_tmit_chunk *chk, *nchk;
6330 
6331 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6332 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6333 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6334 			if (chk->data) {
6335 				sctp_m_freem(chk->data);
6336 				chk->data = NULL;
6337 			}
6338 			asoc->ctrl_queue_cnt--;
6339 			sctp_free_a_chunk(stcb, chk);
6340 		}
6341 	}
6342 }
6343 
6344 void
6345 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6346 {
6347 	struct sctp_association *asoc;
6348 	struct sctp_tmit_chunk *chk, *nchk;
6349 	struct sctp_asconf_chunk *acp;
6350 
6351 	asoc = &stcb->asoc;
6352 	TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6353 		/* find SCTP_ASCONF chunk in queue */
6354 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6355 			if (chk->data) {
6356 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
6357 				if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6358 					/* Not Acked yet */
6359 					break;
6360 				}
6361 			}
6362 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6363 			if (chk->data) {
6364 				sctp_m_freem(chk->data);
6365 				chk->data = NULL;
6366 			}
6367 			asoc->ctrl_queue_cnt--;
6368 			sctp_free_a_chunk(stcb, chk);
6369 		}
6370 	}
6371 }
6372 
6373 
6374 static void
6375 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6376     struct sctp_association *asoc,
6377     struct sctp_tmit_chunk **data_list,
6378     int bundle_at,
6379     struct sctp_nets *net)
6380 {
6381 	int i;
6382 	struct sctp_tmit_chunk *tp1;
6383 
6384 	for (i = 0; i < bundle_at; i++) {
6385 		/* off of the send queue */
6386 		TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
6387 		asoc->send_queue_cnt--;
6388 		if (i > 0) {
6389 			/*
6390 			 * Any chunk NOT 0 you zap the time chunk 0 gets
6391 			 * zapped or set based on if a RTO measurment is
6392 			 * needed.
6393 			 */
6394 			data_list[i]->do_rtt = 0;
6395 		}
6396 		/* record time */
6397 		data_list[i]->sent_rcv_time = net->last_sent_time;
6398 		data_list[i]->rec.data.cwnd_at_send = net->cwnd;
6399 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6400 		if (data_list[i]->whoTo == NULL) {
6401 			data_list[i]->whoTo = net;
6402 			atomic_add_int(&net->ref_count, 1);
6403 		}
6404 		/* on to the sent queue */
6405 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6406 		if ((tp1) && SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6407 			struct sctp_tmit_chunk *tpp;
6408 
6409 			/* need to move back */
6410 	back_up_more:
6411 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6412 			if (tpp == NULL) {
6413 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6414 				goto all_done;
6415 			}
6416 			tp1 = tpp;
6417 			if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6418 				goto back_up_more;
6419 			}
6420 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6421 		} else {
6422 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
6423 			    data_list[i],
6424 			    sctp_next);
6425 		}
6426 all_done:
6427 		/* This does not lower until the cum-ack passes it */
6428 		asoc->sent_queue_cnt++;
6429 		if ((asoc->peers_rwnd <= 0) &&
6430 		    (asoc->total_flight == 0) &&
6431 		    (bundle_at == 1)) {
6432 			/* Mark the chunk as being a window probe */
6433 			SCTP_STAT_INCR(sctps_windowprobed);
6434 		}
6435 #ifdef SCTP_AUDITING_ENABLED
6436 		sctp_audit_log(0xC2, 3);
6437 #endif
6438 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
6439 		data_list[i]->snd_count = 1;
6440 		data_list[i]->rec.data.chunk_was_revoked = 0;
6441 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6442 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6443 			    data_list[i]->whoTo->flight_size,
6444 			    data_list[i]->book_size,
6445 			    (uintptr_t) data_list[i]->whoTo,
6446 			    data_list[i]->rec.data.TSN_seq);
6447 		}
6448 		sctp_flight_size_increase(data_list[i]);
6449 		sctp_total_flight_increase(stcb, data_list[i]);
6450 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6451 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6452 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6453 		}
6454 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6455 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6456 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6457 			/* SWS sender side engages */
6458 			asoc->peers_rwnd = 0;
6459 		}
6460 	}
6461 }
6462 
6463 static void
6464 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6465 {
6466 	struct sctp_tmit_chunk *chk, *nchk;
6467 
6468 	TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6469 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6470 		    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
6471 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6472 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6473 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6474 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6475 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6476 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6477 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6478 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6479 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6480 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6481 			/* Stray chunks must be cleaned up */
6482 	clean_up_anyway:
6483 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6484 			if (chk->data) {
6485 				sctp_m_freem(chk->data);
6486 				chk->data = NULL;
6487 			}
6488 			asoc->ctrl_queue_cnt--;
6489 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
6490 				asoc->fwd_tsn_cnt--;
6491 			sctp_free_a_chunk(stcb, chk);
6492 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6493 			/* special handling, we must look into the param */
6494 			if (chk != asoc->str_reset) {
6495 				goto clean_up_anyway;
6496 			}
6497 		}
6498 	}
6499 }
6500 
6501 
6502 static int
6503 sctp_can_we_split_this(struct sctp_tcb *stcb,
6504     uint32_t length,
6505     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6506 {
6507 	/*
6508 	 * Make a decision on if I should split a msg into multiple parts.
6509 	 * This is only asked of incomplete messages.
6510 	 */
6511 	if (eeor_on) {
6512 		/*
6513 		 * If we are doing EEOR we need to always send it if its the
6514 		 * entire thing, since it might be all the guy is putting in
6515 		 * the hopper.
6516 		 */
6517 		if (goal_mtu >= length) {
6518 			/*-
6519 			 * If we have data outstanding,
6520 			 * we get another chance when the sack
6521 			 * arrives to transmit - wait for more data
6522 			 */
6523 			if (stcb->asoc.total_flight == 0) {
6524 				/*
6525 				 * If nothing is in flight, we zero the
6526 				 * packet counter.
6527 				 */
6528 				return (length);
6529 			}
6530 			return (0);
6531 
6532 		} else {
6533 			/* You can fill the rest */
6534 			return (goal_mtu);
6535 		}
6536 	}
6537 	/*-
6538 	 * For those strange folk that make the send buffer
6539 	 * smaller than our fragmentation point, we can't
6540 	 * get a full msg in so we have to allow splitting.
6541 	 */
6542 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6543 		return (length);
6544 	}
6545 	if ((length <= goal_mtu) ||
6546 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6547 		/* Sub-optimial residual don't split in non-eeor mode. */
6548 		return (0);
6549 	}
6550 	/*
6551 	 * If we reach here length is larger than the goal_mtu. Do we wish
6552 	 * to split it for the sake of packet putting together?
6553 	 */
6554 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6555 		/* Its ok to split it */
6556 		return (min(goal_mtu, frag_point));
6557 	}
6558 	/* Nope, can't split */
6559 	return (0);
6560 
6561 }
6562 
6563 static uint32_t
6564 sctp_move_to_outqueue(struct sctp_tcb *stcb,
6565     struct sctp_stream_out *strq,
6566     uint32_t goal_mtu,
6567     uint32_t frag_point,
6568     int *locked,
6569     int *giveup,
6570     int eeor_mode,
6571     int *bail)
6572 {
6573 	/* Move from the stream to the send_queue keeping track of the total */
6574 	struct sctp_association *asoc;
6575 	struct sctp_stream_queue_pending *sp;
6576 	struct sctp_tmit_chunk *chk;
6577 	struct sctp_data_chunk *dchkh;
6578 	uint32_t to_move, length;
6579 	uint8_t rcv_flags = 0;
6580 	uint8_t some_taken;
6581 	uint8_t send_lock_up = 0;
6582 
6583 	SCTP_TCB_LOCK_ASSERT(stcb);
6584 	asoc = &stcb->asoc;
6585 one_more_time:
6586 	/* sa_ignore FREED_MEMORY */
6587 	sp = TAILQ_FIRST(&strq->outqueue);
6588 	if (sp == NULL) {
6589 		*locked = 0;
6590 		if (send_lock_up == 0) {
6591 			SCTP_TCB_SEND_LOCK(stcb);
6592 			send_lock_up = 1;
6593 		}
6594 		sp = TAILQ_FIRST(&strq->outqueue);
6595 		if (sp) {
6596 			goto one_more_time;
6597 		}
6598 		if (strq->last_msg_incomplete) {
6599 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6600 			    strq->stream_no,
6601 			    strq->last_msg_incomplete);
6602 			strq->last_msg_incomplete = 0;
6603 		}
6604 		to_move = 0;
6605 		if (send_lock_up) {
6606 			SCTP_TCB_SEND_UNLOCK(stcb);
6607 			send_lock_up = 0;
6608 		}
6609 		goto out_of;
6610 	}
6611 	if ((sp->msg_is_complete) && (sp->length == 0)) {
6612 		if (sp->sender_all_done) {
6613 			/*
6614 			 * We are doing differed cleanup. Last time through
6615 			 * when we took all the data the sender_all_done was
6616 			 * not set.
6617 			 */
6618 			if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
6619 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6620 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6621 				    sp->sender_all_done,
6622 				    sp->length,
6623 				    sp->msg_is_complete,
6624 				    sp->put_last_out,
6625 				    send_lock_up);
6626 			}
6627 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6628 				SCTP_TCB_SEND_LOCK(stcb);
6629 				send_lock_up = 1;
6630 			}
6631 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6632 			TAILQ_REMOVE(&strq->outqueue, sp, next);
6633 			stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
6634 			if (sp->net) {
6635 				sctp_free_remote_addr(sp->net);
6636 				sp->net = NULL;
6637 			}
6638 			if (sp->data) {
6639 				sctp_m_freem(sp->data);
6640 				sp->data = NULL;
6641 			}
6642 			sctp_free_a_strmoq(stcb, sp);
6643 			/* we can't be locked to it */
6644 			*locked = 0;
6645 			stcb->asoc.locked_on_sending = NULL;
6646 			if (send_lock_up) {
6647 				SCTP_TCB_SEND_UNLOCK(stcb);
6648 				send_lock_up = 0;
6649 			}
6650 			/* back to get the next msg */
6651 			goto one_more_time;
6652 		} else {
6653 			/*
6654 			 * sender just finished this but still holds a
6655 			 * reference
6656 			 */
6657 			*locked = 1;
6658 			*giveup = 1;
6659 			to_move = 0;
6660 			goto out_of;
6661 		}
6662 	} else {
6663 		/* is there some to get */
6664 		if (sp->length == 0) {
6665 			/* no */
6666 			*locked = 1;
6667 			*giveup = 1;
6668 			to_move = 0;
6669 			goto out_of;
6670 		} else if (sp->discard_rest) {
6671 			if (send_lock_up == 0) {
6672 				SCTP_TCB_SEND_LOCK(stcb);
6673 				send_lock_up = 1;
6674 			}
6675 			/* Whack down the size */
6676 			atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
6677 			if ((stcb->sctp_socket != NULL) && \
6678 			    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6679 			    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6680 				atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
6681 			}
6682 			if (sp->data) {
6683 				sctp_m_freem(sp->data);
6684 				sp->data = NULL;
6685 				sp->tail_mbuf = NULL;
6686 			}
6687 			sp->length = 0;
6688 			sp->some_taken = 1;
6689 			*locked = 1;
6690 			*giveup = 1;
6691 			to_move = 0;
6692 			goto out_of;
6693 		}
6694 	}
6695 	some_taken = sp->some_taken;
6696 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6697 		sp->msg_is_complete = 1;
6698 	}
6699 re_look:
6700 	length = sp->length;
6701 	if (sp->msg_is_complete) {
6702 		/* The message is complete */
6703 		to_move = min(length, frag_point);
6704 		if (to_move == length) {
6705 			/* All of it fits in the MTU */
6706 			if (sp->some_taken) {
6707 				rcv_flags |= SCTP_DATA_LAST_FRAG;
6708 				sp->put_last_out = 1;
6709 			} else {
6710 				rcv_flags |= SCTP_DATA_NOT_FRAG;
6711 				sp->put_last_out = 1;
6712 			}
6713 		} else {
6714 			/* Not all of it fits, we fragment */
6715 			if (sp->some_taken == 0) {
6716 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6717 			}
6718 			sp->some_taken = 1;
6719 		}
6720 	} else {
6721 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
6722 		if (to_move) {
6723 			/*-
6724 			 * We use a snapshot of length in case it
6725 			 * is expanding during the compare.
6726 			 */
6727 			uint32_t llen;
6728 
6729 			llen = length;
6730 			if (to_move >= llen) {
6731 				to_move = llen;
6732 				if (send_lock_up == 0) {
6733 					/*-
6734 					 * We are taking all of an incomplete msg
6735 					 * thus we need a send lock.
6736 					 */
6737 					SCTP_TCB_SEND_LOCK(stcb);
6738 					send_lock_up = 1;
6739 					if (sp->msg_is_complete) {
6740 						/*
6741 						 * the sender finished the
6742 						 * msg
6743 						 */
6744 						goto re_look;
6745 					}
6746 				}
6747 			}
6748 			if (sp->some_taken == 0) {
6749 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
6750 				sp->some_taken = 1;
6751 			}
6752 		} else {
6753 			/* Nothing to take. */
6754 			if (sp->some_taken) {
6755 				*locked = 1;
6756 			}
6757 			*giveup = 1;
6758 			to_move = 0;
6759 			goto out_of;
6760 		}
6761 	}
6762 
6763 	/* If we reach here, we can copy out a chunk */
6764 	sctp_alloc_a_chunk(stcb, chk);
6765 	if (chk == NULL) {
6766 		/* No chunk memory */
6767 		*giveup = 1;
6768 		to_move = 0;
6769 		goto out_of;
6770 	}
6771 	/*
6772 	 * Setup for unordered if needed by looking at the user sent info
6773 	 * flags.
6774 	 */
6775 	if (sp->sinfo_flags & SCTP_UNORDERED) {
6776 		rcv_flags |= SCTP_DATA_UNORDERED;
6777 	}
6778 	if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
6779 	    ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
6780 		rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6781 	}
6782 	/* clear out the chunk before setting up */
6783 	memset(chk, 0, sizeof(*chk));
6784 	chk->rec.data.rcv_flags = rcv_flags;
6785 
6786 	if (to_move >= length) {
6787 		/* we think we can steal the whole thing */
6788 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6789 			SCTP_TCB_SEND_LOCK(stcb);
6790 			send_lock_up = 1;
6791 		}
6792 		if (to_move < sp->length) {
6793 			/* bail, it changed */
6794 			goto dont_do_it;
6795 		}
6796 		chk->data = sp->data;
6797 		chk->last_mbuf = sp->tail_mbuf;
6798 		/* register the stealing */
6799 		sp->data = sp->tail_mbuf = NULL;
6800 	} else {
6801 		struct mbuf *m;
6802 
6803 dont_do_it:
6804 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6805 		chk->last_mbuf = NULL;
6806 		if (chk->data == NULL) {
6807 			sp->some_taken = some_taken;
6808 			sctp_free_a_chunk(stcb, chk);
6809 			*bail = 1;
6810 			to_move = 0;
6811 			goto out_of;
6812 		}
6813 #ifdef SCTP_MBUF_LOGGING
6814 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6815 			struct mbuf *mat;
6816 
6817 			mat = chk->data;
6818 			while (mat) {
6819 				if (SCTP_BUF_IS_EXTENDED(mat)) {
6820 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6821 				}
6822 				mat = SCTP_BUF_NEXT(mat);
6823 			}
6824 		}
6825 #endif
6826 		/* Pull off the data */
6827 		m_adj(sp->data, to_move);
6828 		/* Now lets work our way down and compact it */
6829 		m = sp->data;
6830 		while (m && (SCTP_BUF_LEN(m) == 0)) {
6831 			sp->data = SCTP_BUF_NEXT(m);
6832 			SCTP_BUF_NEXT(m) = NULL;
6833 			if (sp->tail_mbuf == m) {
6834 				/*-
6835 				 * Freeing tail? TSNH since
6836 				 * we supposedly were taking less
6837 				 * than the sp->length.
6838 				 */
6839 #ifdef INVARIANTS
6840 				panic("Huh, freing tail? - TSNH");
6841 #else
6842 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6843 				sp->tail_mbuf = sp->data = NULL;
6844 				sp->length = 0;
6845 #endif
6846 
6847 			}
6848 			sctp_m_free(m);
6849 			m = sp->data;
6850 		}
6851 	}
6852 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6853 		chk->copy_by_ref = 1;
6854 	} else {
6855 		chk->copy_by_ref = 0;
6856 	}
6857 	/*
6858 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
6859 	 * its only one mbuf.
6860 	 */
6861 	if (chk->last_mbuf == NULL) {
6862 		chk->last_mbuf = chk->data;
6863 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6864 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6865 		}
6866 	}
6867 	if (to_move > length) {
6868 		/*- This should not happen either
6869 		 * since we always lower to_move to the size
6870 		 * of sp->length if its larger.
6871 		 */
6872 #ifdef INVARIANTS
6873 		panic("Huh, how can to_move be larger?");
6874 #else
6875 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
6876 		sp->length = 0;
6877 #endif
6878 	} else {
6879 		atomic_subtract_int(&sp->length, to_move);
6880 	}
6881 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
6882 		/* Not enough room for a chunk header, get some */
6883 		struct mbuf *m;
6884 
6885 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
6886 		if (m == NULL) {
6887 			/*
6888 			 * we're in trouble here. _PREPEND below will free
6889 			 * all the data if there is no leading space, so we
6890 			 * must put the data back and restore.
6891 			 */
6892 			if (send_lock_up == 0) {
6893 				SCTP_TCB_SEND_LOCK(stcb);
6894 				send_lock_up = 1;
6895 			}
6896 			if (chk->data == NULL) {
6897 				/* unsteal the data */
6898 				sp->data = chk->data;
6899 				sp->tail_mbuf = chk->last_mbuf;
6900 			} else {
6901 				struct mbuf *m_tmp;
6902 
6903 				/* reassemble the data */
6904 				m_tmp = sp->data;
6905 				sp->data = chk->data;
6906 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
6907 			}
6908 			sp->some_taken = some_taken;
6909 			atomic_add_int(&sp->length, to_move);
6910 			chk->data = NULL;
6911 			*bail = 1;
6912 			sctp_free_a_chunk(stcb, chk);
6913 			to_move = 0;
6914 			goto out_of;
6915 		} else {
6916 			SCTP_BUF_LEN(m) = 0;
6917 			SCTP_BUF_NEXT(m) = chk->data;
6918 			chk->data = m;
6919 			M_ALIGN(chk->data, 4);
6920 		}
6921 	}
6922 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
6923 	if (chk->data == NULL) {
6924 		/* HELP, TSNH since we assured it would not above? */
6925 #ifdef INVARIANTS
6926 		panic("prepend failes HELP?");
6927 #else
6928 		SCTP_PRINTF("prepend fails HELP?\n");
6929 		sctp_free_a_chunk(stcb, chk);
6930 #endif
6931 		*bail = 1;
6932 		to_move = 0;
6933 		goto out_of;
6934 	}
6935 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
6936 	chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
6937 	chk->book_size_scale = 0;
6938 	chk->sent = SCTP_DATAGRAM_UNSENT;
6939 
6940 	chk->flags = 0;
6941 	chk->asoc = &stcb->asoc;
6942 	chk->pad_inplace = 0;
6943 	chk->no_fr_allowed = 0;
6944 	chk->rec.data.stream_seq = sp->strseq;
6945 	chk->rec.data.stream_number = sp->stream;
6946 	chk->rec.data.payloadtype = sp->ppid;
6947 	chk->rec.data.context = sp->context;
6948 	chk->rec.data.doing_fast_retransmit = 0;
6949 
6950 	chk->rec.data.timetodrop = sp->ts;
6951 	chk->flags = sp->act_flags;
6952 
6953 	if (sp->net) {
6954 		chk->whoTo = sp->net;
6955 		atomic_add_int(&chk->whoTo->ref_count, 1);
6956 	} else
6957 		chk->whoTo = NULL;
6958 
6959 	if (sp->holds_key_ref) {
6960 		chk->auth_keyid = sp->auth_keyid;
6961 		sctp_auth_key_acquire(stcb, chk->auth_keyid);
6962 		chk->holds_key_ref = 1;
6963 	}
6964 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
6965 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
6966 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
6967 		    (uintptr_t) stcb, sp->length,
6968 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
6969 		    chk->rec.data.TSN_seq);
6970 	}
6971 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
6972 	/*
6973 	 * Put the rest of the things in place now. Size was done earlier in
6974 	 * previous loop prior to padding.
6975 	 */
6976 
6977 #ifdef SCTP_ASOCLOG_OF_TSNS
6978 	SCTP_TCB_LOCK_ASSERT(stcb);
6979 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
6980 		asoc->tsn_out_at = 0;
6981 		asoc->tsn_out_wrapped = 1;
6982 	}
6983 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
6984 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
6985 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
6986 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
6987 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
6988 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
6989 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
6990 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
6991 	asoc->tsn_out_at++;
6992 #endif
6993 
6994 	dchkh->ch.chunk_type = SCTP_DATA;
6995 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
6996 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
6997 	dchkh->dp.stream_id = htons(strq->stream_no);
6998 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
6999 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7000 	dchkh->ch.chunk_length = htons(chk->send_size);
7001 	/* Now advance the chk->send_size by the actual pad needed. */
7002 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7003 		/* need a pad */
7004 		struct mbuf *lm;
7005 		int pads;
7006 
7007 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7008 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7009 			chk->pad_inplace = 1;
7010 		}
7011 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7012 			/* pad added an mbuf */
7013 			chk->last_mbuf = lm;
7014 		}
7015 		chk->send_size += pads;
7016 	}
7017 	/* We only re-set the policy if it is on */
7018 	if (sp->pr_sctp_on) {
7019 		sctp_set_prsctp_policy(sp);
7020 		asoc->pr_sctp_cnt++;
7021 		chk->pr_sctp_on = 1;
7022 	} else {
7023 		chk->pr_sctp_on = 0;
7024 	}
7025 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7026 		/* All done pull and kill the message */
7027 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7028 		if (sp->put_last_out == 0) {
7029 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7030 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7031 			    sp->sender_all_done,
7032 			    sp->length,
7033 			    sp->msg_is_complete,
7034 			    sp->put_last_out,
7035 			    send_lock_up);
7036 		}
7037 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7038 			SCTP_TCB_SEND_LOCK(stcb);
7039 			send_lock_up = 1;
7040 		}
7041 		TAILQ_REMOVE(&strq->outqueue, sp, next);
7042 		stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
7043 		if (sp->net) {
7044 			sctp_free_remote_addr(sp->net);
7045 			sp->net = NULL;
7046 		}
7047 		if (sp->data) {
7048 			sctp_m_freem(sp->data);
7049 			sp->data = NULL;
7050 		}
7051 		sctp_free_a_strmoq(stcb, sp);
7052 
7053 		/* we can't be locked to it */
7054 		*locked = 0;
7055 		stcb->asoc.locked_on_sending = NULL;
7056 	} else {
7057 		/* more to go, we are locked */
7058 		*locked = 1;
7059 	}
7060 	asoc->chunks_on_out_queue++;
7061 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7062 	asoc->send_queue_cnt++;
7063 out_of:
7064 	if (send_lock_up) {
7065 		SCTP_TCB_SEND_UNLOCK(stcb);
7066 		send_lock_up = 0;
7067 	}
7068 	return (to_move);
7069 }
7070 
7071 
7072 static void
7073 sctp_fill_outqueue(struct sctp_tcb *stcb,
7074     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7075 {
7076 	struct sctp_association *asoc;
7077 	struct sctp_stream_out *strq, *strqn;
7078 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7079 	int locked, giveup;
7080 
7081 	SCTP_TCB_LOCK_ASSERT(stcb);
7082 	asoc = &stcb->asoc;
7083 #ifdef INET6
7084 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7085 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7086 	} else {
7087 		/* ?? not sure what else to do */
7088 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7089 	}
7090 #else
7091 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7092 #endif
7093 	/* Need an allowance for the data chunk header too */
7094 	goal_mtu -= sizeof(struct sctp_data_chunk);
7095 
7096 	/* must make even word boundary */
7097 	goal_mtu &= 0xfffffffc;
7098 	if (asoc->locked_on_sending) {
7099 		/* We are stuck on one stream until the message completes. */
7100 		strq = asoc->locked_on_sending;
7101 		locked = 1;
7102 	} else {
7103 		strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7104 		locked = 0;
7105 	}
7106 	strqn = strq;
7107 	while ((goal_mtu > 0) && strq) {
7108 		giveup = 0;
7109 		bail = 0;
7110 		moved_how_much = sctp_move_to_outqueue(stcb, strq, goal_mtu, frag_point, &locked,
7111 		    &giveup, eeor_mode, &bail);
7112 		if (moved_how_much)
7113 			stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved_how_much);
7114 
7115 		if (locked) {
7116 			asoc->locked_on_sending = strq;
7117 			if ((moved_how_much == 0) || (giveup) || bail)
7118 				/* no more to move for now */
7119 				break;
7120 		} else {
7121 			asoc->locked_on_sending = NULL;
7122 			if ((giveup) || bail) {
7123 				break;
7124 			}
7125 			strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7126 			if (strq == NULL) {
7127 				break;
7128 			}
7129 		}
7130 		total_moved += moved_how_much;
7131 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7132 		goal_mtu &= 0xfffffffc;
7133 	}
7134 	if (bail)
7135 		*quit_now = 1;
7136 
7137 	stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7138 
7139 	if (total_moved == 0) {
7140 		if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7141 		    (net == stcb->asoc.primary_destination)) {
7142 			/* ran dry for primary network net */
7143 			SCTP_STAT_INCR(sctps_primary_randry);
7144 		} else if (stcb->asoc.sctp_cmt_on_off > 0) {
7145 			/* ran dry with CMT on */
7146 			SCTP_STAT_INCR(sctps_cmt_randry);
7147 		}
7148 	}
7149 }
7150 
7151 void
7152 sctp_fix_ecn_echo(struct sctp_association *asoc)
7153 {
7154 	struct sctp_tmit_chunk *chk;
7155 
7156 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7157 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7158 			chk->sent = SCTP_DATAGRAM_UNSENT;
7159 		}
7160 	}
7161 }
7162 
7163 void
7164 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7165 {
7166 	struct sctp_association *asoc;
7167 	struct sctp_tmit_chunk *chk;
7168 	struct sctp_stream_queue_pending *sp;
7169 	unsigned int i;
7170 
7171 	if (net == NULL) {
7172 		return;
7173 	}
7174 	asoc = &stcb->asoc;
7175 	for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7176 		TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7177 			if (sp->net == net) {
7178 				sctp_free_remote_addr(sp->net);
7179 				sp->net = NULL;
7180 			}
7181 		}
7182 	}
7183 	TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7184 		if (chk->whoTo == net) {
7185 			sctp_free_remote_addr(chk->whoTo);
7186 			chk->whoTo = NULL;
7187 		}
7188 	}
7189 }
7190 
7191 int
7192 sctp_med_chunk_output(struct sctp_inpcb *inp,
7193     struct sctp_tcb *stcb,
7194     struct sctp_association *asoc,
7195     int *num_out,
7196     int *reason_code,
7197     int control_only, int from_where,
7198     struct timeval *now, int *now_filled, int frag_point, int so_locked
7199 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7200     SCTP_UNUSED
7201 #endif
7202 )
7203 {
7204 	/*
7205 	 * Ok this is the generic chunk service queue. we must do the
7206 	 * following: - Service the stream queue that is next, moving any
7207 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7208 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
7209 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
7210 	 * fomulate and send the low level chunks. Making sure to combine
7211 	 * any control in the control chunk queue also.
7212 	 */
7213 	struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7214 	struct mbuf *outchain, *endoutchain;
7215 	struct sctp_tmit_chunk *chk, *nchk;
7216 
7217 	/* temp arrays for unlinking */
7218 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7219 	int no_fragmentflg, error;
7220 	unsigned int max_rwnd_per_dest, max_send_per_dest;
7221 	int one_chunk, hbflag, skip_data_for_this_net;
7222 	int asconf, cookie, no_out_cnt;
7223 	int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7224 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7225 	int tsns_sent = 0;
7226 	uint32_t auth_offset = 0;
7227 	struct sctp_auth_chunk *auth = NULL;
7228 	uint16_t auth_keyid;
7229 	int override_ok = 1;
7230 	int data_auth_reqd = 0;
7231 
7232 	/*
7233 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7234 	 * destination.
7235 	 */
7236 	int pf_hbflag = 0;
7237 	int quit_now = 0;
7238 
7239 	*num_out = 0;
7240 	auth_keyid = stcb->asoc.authinfo.active_keyid;
7241 
7242 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7243 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7244 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7245 		eeor_mode = 1;
7246 	} else {
7247 		eeor_mode = 0;
7248 	}
7249 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
7250 	/*
7251 	 * First lets prime the pump. For each destination, if there is room
7252 	 * in the flight size, attempt to pull an MTU's worth out of the
7253 	 * stream queues into the general send_queue
7254 	 */
7255 #ifdef SCTP_AUDITING_ENABLED
7256 	sctp_audit_log(0xC2, 2);
7257 #endif
7258 	SCTP_TCB_LOCK_ASSERT(stcb);
7259 	hbflag = 0;
7260 	if ((control_only) || (asoc->stream_reset_outstanding))
7261 		no_data_chunks = 1;
7262 	else
7263 		no_data_chunks = 0;
7264 
7265 	/* Nothing to possible to send? */
7266 	if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7267 	    (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7268 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7269 	    TAILQ_EMPTY(&asoc->send_queue) &&
7270 	    stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
7271 nothing_to_send:
7272 		*reason_code = 9;
7273 		return (0);
7274 	}
7275 	if (asoc->peers_rwnd == 0) {
7276 		/* No room in peers rwnd */
7277 		*reason_code = 1;
7278 		if (asoc->total_flight > 0) {
7279 			/* we are allowed one chunk in flight */
7280 			no_data_chunks = 1;
7281 		}
7282 	}
7283 	if (stcb->asoc.ecn_echo_cnt_onq) {
7284 		/* Record where a sack goes, if any */
7285 		if (no_data_chunks &&
7286 		    (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7287 			/* Nothing but ECNe to send - we don't do that */
7288 			goto nothing_to_send;
7289 		}
7290 		TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7291 			if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7292 			    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7293 				sack_goes_to = chk->whoTo;
7294 				break;
7295 			}
7296 		}
7297 	}
7298 	max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7299 	if (stcb->sctp_socket)
7300 		max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7301 	else
7302 		max_send_per_dest = 0;
7303 	if ((no_data_chunks == 0) &&
7304 	    (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7305 		TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7306 			/*
7307 			 * This for loop we are in takes in each net, if
7308 			 * its's got space in cwnd and has data sent to it
7309 			 * (when CMT is off) then it calls
7310 			 * sctp_fill_outqueue for the net. This gets data on
7311 			 * the send queue for that network.
7312 			 *
7313 			 * In sctp_fill_outqueue TSN's are assigned and data is
7314 			 * copied out of the stream buffers. Note mostly
7315 			 * copy by reference (we hope).
7316 			 */
7317 			net->window_probe = 0;
7318 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ||
7319 			    (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7320 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7321 					sctp_log_cwnd(stcb, net, 1,
7322 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7323 				}
7324 				continue;
7325 			}
7326 			if ((asoc->sctp_cmt_on_off == 0) &&
7327 			    (asoc->primary_destination != net) &&
7328 			    (net->ref_count < 2)) {
7329 				/* nothing can be in queue for this guy */
7330 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7331 					sctp_log_cwnd(stcb, net, 2,
7332 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7333 				}
7334 				continue;
7335 			}
7336 			if (net->flight_size >= net->cwnd) {
7337 				/* skip this network, no room - can't fill */
7338 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7339 					sctp_log_cwnd(stcb, net, 3,
7340 					    SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7341 				}
7342 				continue;
7343 			}
7344 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7345 				sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7346 			}
7347 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7348 			if (quit_now) {
7349 				/* memory alloc failure */
7350 				no_data_chunks = 1;
7351 				break;
7352 			}
7353 		}
7354 	}
7355 	/* now service each destination and send out what we can for it */
7356 	/* Nothing to send? */
7357 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7358 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7359 	    TAILQ_EMPTY(&asoc->send_queue)) {
7360 		*reason_code = 8;
7361 		return (0);
7362 	}
7363 	if (asoc->sctp_cmt_on_off > 0) {
7364 		/* get the last start point */
7365 		start_at = asoc->last_net_cmt_send_started;
7366 		if (start_at == NULL) {
7367 			/* null so to beginning */
7368 			start_at = TAILQ_FIRST(&asoc->nets);
7369 		} else {
7370 			start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7371 			if (start_at == NULL) {
7372 				start_at = TAILQ_FIRST(&asoc->nets);
7373 			}
7374 		}
7375 		asoc->last_net_cmt_send_started = start_at;
7376 	} else {
7377 		start_at = TAILQ_FIRST(&asoc->nets);
7378 	}
7379 	old_start_at = NULL;
7380 again_one_more_time:
7381 	for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7382 		/* how much can we send? */
7383 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7384 		if (old_start_at && (old_start_at == net)) {
7385 			/* through list ocmpletely. */
7386 			break;
7387 		}
7388 		tsns_sent = 0xa;
7389 		if ((asoc->sctp_cmt_on_off == 0) &&
7390 		    (asoc->primary_destination != net) &&
7391 		    (net->ref_count < 2)) {
7392 			/*
7393 			 * Ref-count of 1 so we cannot have data or control
7394 			 * queued to this address. Skip it (non-CMT).
7395 			 */
7396 			continue;
7397 		}
7398 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7399 		    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7400 		    (net->flight_size >= net->cwnd)) {
7401 			/*
7402 			 * Nothing on control or asconf and flight is full,
7403 			 * we can skip even in the CMT case.
7404 			 */
7405 			continue;
7406 		}
7407 		ctl_cnt = bundle_at = 0;
7408 		endoutchain = outchain = NULL;
7409 		no_fragmentflg = 1;
7410 		one_chunk = 0;
7411 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7412 			skip_data_for_this_net = 1;
7413 		} else {
7414 			skip_data_for_this_net = 0;
7415 		}
7416 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7417 			/*
7418 			 * if we have a route and an ifp check to see if we
7419 			 * have room to send to this guy
7420 			 */
7421 			struct ifnet *ifp;
7422 
7423 			ifp = net->ro.ro_rt->rt_ifp;
7424 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7425 				SCTP_STAT_INCR(sctps_ifnomemqueued);
7426 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7427 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7428 				}
7429 				continue;
7430 			}
7431 		}
7432 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7433 		case AF_INET:
7434 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7435 			break;
7436 #ifdef INET6
7437 		case AF_INET6:
7438 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7439 			break;
7440 #endif
7441 		default:
7442 			/* TSNH */
7443 			mtu = net->mtu;
7444 			break;
7445 		}
7446 		mx_mtu = mtu;
7447 		to_out = 0;
7448 		if (mtu > asoc->peers_rwnd) {
7449 			if (asoc->total_flight > 0) {
7450 				/* We have a packet in flight somewhere */
7451 				r_mtu = asoc->peers_rwnd;
7452 			} else {
7453 				/* We are always allowed to send one MTU out */
7454 				one_chunk = 1;
7455 				r_mtu = mtu;
7456 			}
7457 		} else {
7458 			r_mtu = mtu;
7459 		}
7460 		/************************/
7461 		/* ASCONF transmission */
7462 		/************************/
7463 		/* Now first lets go through the asconf queue */
7464 		TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
7465 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7466 				continue;
7467 			}
7468 			if (chk->whoTo != net) {
7469 				/*
7470 				 * No, not sent to the network we are
7471 				 * looking at
7472 				 */
7473 				break;
7474 			}
7475 			if (chk->data == NULL) {
7476 				break;
7477 			}
7478 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7479 			    chk->sent != SCTP_DATAGRAM_RESEND) {
7480 				break;
7481 			}
7482 			/*
7483 			 * if no AUTH is yet included and this chunk
7484 			 * requires it, make sure to account for it.  We
7485 			 * don't apply the size until the AUTH chunk is
7486 			 * actually added below in case there is no room for
7487 			 * this chunk. NOTE: we overload the use of "omtu"
7488 			 * here
7489 			 */
7490 			if ((auth == NULL) &&
7491 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7492 			    stcb->asoc.peer_auth_chunks)) {
7493 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7494 			} else
7495 				omtu = 0;
7496 			/* Here we do NOT factor the r_mtu */
7497 			if ((chk->send_size < (int)(mtu - omtu)) ||
7498 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7499 				/*
7500 				 * We probably should glom the mbuf chain
7501 				 * from the chk->data for control but the
7502 				 * problem is it becomes yet one more level
7503 				 * of tracking to do if for some reason
7504 				 * output fails. Then I have got to
7505 				 * reconstruct the merged control chain.. el
7506 				 * yucko.. for now we take the easy way and
7507 				 * do the copy
7508 				 */
7509 				/*
7510 				 * Add an AUTH chunk, if chunk requires it
7511 				 * save the offset into the chain for AUTH
7512 				 */
7513 				if ((auth == NULL) &&
7514 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7515 				    stcb->asoc.peer_auth_chunks))) {
7516 					outchain = sctp_add_auth_chunk(outchain,
7517 					    &endoutchain,
7518 					    &auth,
7519 					    &auth_offset,
7520 					    stcb,
7521 					    chk->rec.chunk_id.id);
7522 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7523 				}
7524 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7525 				    (int)chk->rec.chunk_id.can_take_data,
7526 				    chk->send_size, chk->copy_by_ref);
7527 				if (outchain == NULL) {
7528 					*reason_code = 8;
7529 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7530 					return (ENOMEM);
7531 				}
7532 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7533 				/* update our MTU size */
7534 				if (mtu > (chk->send_size + omtu))
7535 					mtu -= (chk->send_size + omtu);
7536 				else
7537 					mtu = 0;
7538 				to_out += (chk->send_size + omtu);
7539 				/* Do clear IP_DF ? */
7540 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7541 					no_fragmentflg = 0;
7542 				}
7543 				if (chk->rec.chunk_id.can_take_data)
7544 					chk->data = NULL;
7545 				/*
7546 				 * set hb flag since we can use these for
7547 				 * RTO
7548 				 */
7549 				hbflag = 1;
7550 				asconf = 1;
7551 				/*
7552 				 * should sysctl this: don't bundle data
7553 				 * with ASCONF since it requires AUTH
7554 				 */
7555 				no_data_chunks = 1;
7556 				chk->sent = SCTP_DATAGRAM_SENT;
7557 				chk->snd_count++;
7558 				if (mtu == 0) {
7559 					/*
7560 					 * Ok we are out of room but we can
7561 					 * output without effecting the
7562 					 * flight size since this little guy
7563 					 * is a control only packet.
7564 					 */
7565 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7566 					/*
7567 					 * do NOT clear the asconf flag as
7568 					 * it is used to do appropriate
7569 					 * source address selection.
7570 					 */
7571 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7572 					    (struct sockaddr *)&net->ro._l_addr,
7573 					    outchain, auth_offset, auth,
7574 					    stcb->asoc.authinfo.active_keyid,
7575 					    no_fragmentflg, 0, NULL, asconf,
7576 					    inp->sctp_lport, stcb->rport,
7577 					    htonl(stcb->asoc.peer_vtag),
7578 					    net->port, so_locked, NULL))) {
7579 						if (error == ENOBUFS) {
7580 							asoc->ifp_had_enobuf = 1;
7581 							SCTP_STAT_INCR(sctps_lowlevelerr);
7582 						}
7583 						if (from_where == 0) {
7584 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7585 						}
7586 						if (*now_filled == 0) {
7587 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7588 							*now_filled = 1;
7589 							*now = net->last_sent_time;
7590 						} else {
7591 							net->last_sent_time = *now;
7592 						}
7593 						hbflag = 0;
7594 						/* error, could not output */
7595 						if (error == EHOSTUNREACH) {
7596 							/*
7597 							 * Destination went
7598 							 * unreachable
7599 							 * during this send
7600 							 */
7601 							sctp_move_chunks_from_net(stcb, net);
7602 						}
7603 						*reason_code = 7;
7604 						continue;
7605 					} else
7606 						asoc->ifp_had_enobuf = 0;
7607 					if (*now_filled == 0) {
7608 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7609 						*now_filled = 1;
7610 						*now = net->last_sent_time;
7611 					} else {
7612 						net->last_sent_time = *now;
7613 					}
7614 					hbflag = 0;
7615 					/*
7616 					 * increase the number we sent, if a
7617 					 * cookie is sent we don't tell them
7618 					 * any was sent out.
7619 					 */
7620 					outchain = endoutchain = NULL;
7621 					auth = NULL;
7622 					auth_offset = 0;
7623 					if (!no_out_cnt)
7624 						*num_out += ctl_cnt;
7625 					/* recalc a clean slate and setup */
7626 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7627 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7628 					} else {
7629 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7630 					}
7631 					to_out = 0;
7632 					no_fragmentflg = 1;
7633 				}
7634 			}
7635 		}
7636 		/************************/
7637 		/* Control transmission */
7638 		/************************/
7639 		/* Now first lets go through the control queue */
7640 		TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
7641 			if ((sack_goes_to) &&
7642 			    (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
7643 			    (chk->whoTo != sack_goes_to)) {
7644 				/*
7645 				 * if we have a sack in queue, and we are
7646 				 * looking at an ecn echo that is NOT queued
7647 				 * to where the sack is going..
7648 				 */
7649 				if (chk->whoTo == net) {
7650 					/*
7651 					 * Don't transmit it to where its
7652 					 * going (current net)
7653 					 */
7654 					continue;
7655 				} else if (sack_goes_to == net) {
7656 					/*
7657 					 * But do transmit it to this
7658 					 * address
7659 					 */
7660 					goto skip_net_check;
7661 				}
7662 			}
7663 			if (chk->whoTo != net) {
7664 				/*
7665 				 * No, not sent to the network we are
7666 				 * looking at
7667 				 */
7668 				continue;
7669 			}
7670 	skip_net_check:
7671 			if (chk->data == NULL) {
7672 				continue;
7673 			}
7674 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7675 				/*
7676 				 * It must be unsent. Cookies and ASCONF's
7677 				 * hang around but there timers will force
7678 				 * when marked for resend.
7679 				 */
7680 				continue;
7681 			}
7682 			/*
7683 			 * if no AUTH is yet included and this chunk
7684 			 * requires it, make sure to account for it.  We
7685 			 * don't apply the size until the AUTH chunk is
7686 			 * actually added below in case there is no room for
7687 			 * this chunk. NOTE: we overload the use of "omtu"
7688 			 * here
7689 			 */
7690 			if ((auth == NULL) &&
7691 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7692 			    stcb->asoc.peer_auth_chunks)) {
7693 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7694 			} else
7695 				omtu = 0;
7696 			/* Here we do NOT factor the r_mtu */
7697 			if ((chk->send_size <= (int)(mtu - omtu)) ||
7698 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7699 				/*
7700 				 * We probably should glom the mbuf chain
7701 				 * from the chk->data for control but the
7702 				 * problem is it becomes yet one more level
7703 				 * of tracking to do if for some reason
7704 				 * output fails. Then I have got to
7705 				 * reconstruct the merged control chain.. el
7706 				 * yucko.. for now we take the easy way and
7707 				 * do the copy
7708 				 */
7709 				/*
7710 				 * Add an AUTH chunk, if chunk requires it
7711 				 * save the offset into the chain for AUTH
7712 				 */
7713 				if ((auth == NULL) &&
7714 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7715 				    stcb->asoc.peer_auth_chunks))) {
7716 					outchain = sctp_add_auth_chunk(outchain,
7717 					    &endoutchain,
7718 					    &auth,
7719 					    &auth_offset,
7720 					    stcb,
7721 					    chk->rec.chunk_id.id);
7722 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7723 				}
7724 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7725 				    (int)chk->rec.chunk_id.can_take_data,
7726 				    chk->send_size, chk->copy_by_ref);
7727 				if (outchain == NULL) {
7728 					*reason_code = 8;
7729 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7730 					return (ENOMEM);
7731 				}
7732 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7733 				/* update our MTU size */
7734 				if (mtu > (chk->send_size + omtu))
7735 					mtu -= (chk->send_size + omtu);
7736 				else
7737 					mtu = 0;
7738 				to_out += (chk->send_size + omtu);
7739 				/* Do clear IP_DF ? */
7740 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7741 					no_fragmentflg = 0;
7742 				}
7743 				if (chk->rec.chunk_id.can_take_data)
7744 					chk->data = NULL;
7745 				/* Mark things to be removed, if needed */
7746 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7747 				    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
7748 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7749 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7750 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7751 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7752 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7753 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7754 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7755 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7756 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7757 
7758 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7759 						hbflag = 1;
7760 						/*
7761 						 * JRS 5/14/07 - Set the
7762 						 * flag to say a heartbeat
7763 						 * is being sent.
7764 						 */
7765 						pf_hbflag = 1;
7766 					}
7767 					/* remove these chunks at the end */
7768 					if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7769 					    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7770 						/* turn off the timer */
7771 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7772 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7773 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7774 						}
7775 					}
7776 					ctl_cnt++;
7777 				} else {
7778 					/*
7779 					 * Other chunks, since they have
7780 					 * timers running (i.e. COOKIE) we
7781 					 * just "trust" that it gets sent or
7782 					 * retransmitted.
7783 					 */
7784 					ctl_cnt++;
7785 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7786 						cookie = 1;
7787 						no_out_cnt = 1;
7788 					} else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7789 						/*
7790 						 * Increment ecne send count
7791 						 * here this means we may be
7792 						 * over-zealous in our
7793 						 * counting if the send
7794 						 * fails, but its the best
7795 						 * place to do it (we used
7796 						 * to do it in the queue of
7797 						 * the chunk, but that did
7798 						 * not tell how many times
7799 						 * it was sent.
7800 						 */
7801 						SCTP_STAT_INCR(sctps_sendecne);
7802 					}
7803 					chk->sent = SCTP_DATAGRAM_SENT;
7804 					chk->snd_count++;
7805 				}
7806 				if (mtu == 0) {
7807 					/*
7808 					 * Ok we are out of room but we can
7809 					 * output without effecting the
7810 					 * flight size since this little guy
7811 					 * is a control only packet.
7812 					 */
7813 					if (asconf) {
7814 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7815 						/*
7816 						 * do NOT clear the asconf
7817 						 * flag as it is used to do
7818 						 * appropriate source
7819 						 * address selection.
7820 						 */
7821 					}
7822 					if (cookie) {
7823 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7824 						cookie = 0;
7825 					}
7826 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7827 					    (struct sockaddr *)&net->ro._l_addr,
7828 					    outchain,
7829 					    auth_offset, auth,
7830 					    stcb->asoc.authinfo.active_keyid,
7831 					    no_fragmentflg, 0, NULL, asconf,
7832 					    inp->sctp_lport, stcb->rport,
7833 					    htonl(stcb->asoc.peer_vtag),
7834 					    net->port, so_locked, NULL))) {
7835 						if (error == ENOBUFS) {
7836 							asoc->ifp_had_enobuf = 1;
7837 							SCTP_STAT_INCR(sctps_lowlevelerr);
7838 						}
7839 						if (from_where == 0) {
7840 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
7841 						}
7842 						/* error, could not output */
7843 						if (hbflag) {
7844 							if (*now_filled == 0) {
7845 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7846 								*now_filled = 1;
7847 								*now = net->last_sent_time;
7848 							} else {
7849 								net->last_sent_time = *now;
7850 							}
7851 							hbflag = 0;
7852 						}
7853 						if (error == EHOSTUNREACH) {
7854 							/*
7855 							 * Destination went
7856 							 * unreachable
7857 							 * during this send
7858 							 */
7859 							sctp_move_chunks_from_net(stcb, net);
7860 						}
7861 						*reason_code = 7;
7862 						continue;
7863 					} else
7864 						asoc->ifp_had_enobuf = 0;
7865 					/* Only HB or ASCONF advances time */
7866 					if (hbflag) {
7867 						if (*now_filled == 0) {
7868 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7869 							*now_filled = 1;
7870 							*now = net->last_sent_time;
7871 						} else {
7872 							net->last_sent_time = *now;
7873 						}
7874 						hbflag = 0;
7875 					}
7876 					/*
7877 					 * increase the number we sent, if a
7878 					 * cookie is sent we don't tell them
7879 					 * any was sent out.
7880 					 */
7881 					outchain = endoutchain = NULL;
7882 					auth = NULL;
7883 					auth_offset = 0;
7884 					if (!no_out_cnt)
7885 						*num_out += ctl_cnt;
7886 					/* recalc a clean slate and setup */
7887 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7888 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7889 					} else {
7890 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7891 					}
7892 					to_out = 0;
7893 					no_fragmentflg = 1;
7894 				}
7895 			}
7896 		}
7897 		/* JRI: if dest is in PF state, do not send data to it */
7898 		if ((asoc->sctp_cmt_on_off > 0) &&
7899 		    (asoc->sctp_cmt_pf > 0) &&
7900 		    (net->dest_state & SCTP_ADDR_PF)) {
7901 			goto no_data_fill;
7902 		}
7903 		if (net->flight_size >= net->cwnd) {
7904 			goto no_data_fill;
7905 		}
7906 		if ((asoc->sctp_cmt_on_off > 0) &&
7907 		    (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
7908 		    (net->flight_size > max_rwnd_per_dest)) {
7909 			goto no_data_fill;
7910 		}
7911 		/*
7912 		 * We need a specific accounting for the usage of the send
7913 		 * buffer. We also need to check the number of messages per
7914 		 * net. For now, this is better than nothing and it disabled
7915 		 * by default...
7916 		 */
7917 		if ((asoc->sctp_cmt_on_off > 0) &&
7918 		    (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
7919 		    (max_send_per_dest > 0) &&
7920 		    (net->flight_size > max_send_per_dest)) {
7921 			goto no_data_fill;
7922 		}
7923 		/*********************/
7924 		/* Data transmission */
7925 		/*********************/
7926 		/*
7927 		 * if AUTH for DATA is required and no AUTH has been added
7928 		 * yet, account for this in the mtu now... if no data can be
7929 		 * bundled, this adjustment won't matter anyways since the
7930 		 * packet will be going out...
7931 		 */
7932 		data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
7933 		    stcb->asoc.peer_auth_chunks);
7934 		if (data_auth_reqd && (auth == NULL)) {
7935 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7936 		}
7937 		/* now lets add any data within the MTU constraints */
7938 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7939 		case AF_INET:
7940 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
7941 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7942 			else
7943 				omtu = 0;
7944 			break;
7945 #ifdef INET6
7946 		case AF_INET6:
7947 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
7948 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7949 			else
7950 				omtu = 0;
7951 			break;
7952 #endif
7953 		default:
7954 			/* TSNH */
7955 			omtu = 0;
7956 			break;
7957 		}
7958 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
7959 		    (skip_data_for_this_net == 0)) ||
7960 		    (cookie)) {
7961 			TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
7962 				if (no_data_chunks) {
7963 					/* let only control go out */
7964 					*reason_code = 1;
7965 					break;
7966 				}
7967 				if (net->flight_size >= net->cwnd) {
7968 					/* skip this net, no room for data */
7969 					*reason_code = 2;
7970 					break;
7971 				}
7972 				if ((chk->whoTo != NULL) &&
7973 				    (chk->whoTo != net)) {
7974 					/* Don't send the chunk on this net */
7975 					continue;
7976 				}
7977 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
7978 					/*-
7979 					 * strange, we have a chunk that is
7980 					 * to big for its destination and
7981 					 * yet no fragment ok flag.
7982 					 * Something went wrong when the
7983 					 * PMTU changed...we did not mark
7984 					 * this chunk for some reason?? I
7985 					 * will fix it here by letting IP
7986 					 * fragment it for now and printing
7987 					 * a warning. This really should not
7988 					 * happen ...
7989 					 */
7990 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
7991 					    chk->send_size, mtu);
7992 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
7993 				}
7994 				if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
7995 				    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
7996 					struct sctp_data_chunk *dchkh;
7997 
7998 					dchkh = mtod(chk->data, struct sctp_data_chunk *);
7999 					dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8000 				}
8001 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8002 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8003 					/* ok we will add this one */
8004 
8005 					/*
8006 					 * Add an AUTH chunk, if chunk
8007 					 * requires it, save the offset into
8008 					 * the chain for AUTH
8009 					 */
8010 					if (data_auth_reqd) {
8011 						if (auth == NULL) {
8012 							outchain = sctp_add_auth_chunk(outchain,
8013 							    &endoutchain,
8014 							    &auth,
8015 							    &auth_offset,
8016 							    stcb,
8017 							    SCTP_DATA);
8018 							auth_keyid = chk->auth_keyid;
8019 							override_ok = 0;
8020 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8021 						} else if (override_ok) {
8022 							/*
8023 							 * use this data's
8024 							 * keyid
8025 							 */
8026 							auth_keyid = chk->auth_keyid;
8027 							override_ok = 0;
8028 						} else if (auth_keyid != chk->auth_keyid) {
8029 							/*
8030 							 * different keyid,
8031 							 * so done bundling
8032 							 */
8033 							break;
8034 						}
8035 					}
8036 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8037 					    chk->send_size, chk->copy_by_ref);
8038 					if (outchain == NULL) {
8039 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8040 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8041 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8042 						}
8043 						*reason_code = 3;
8044 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8045 						return (ENOMEM);
8046 					}
8047 					/* upate our MTU size */
8048 					/* Do clear IP_DF ? */
8049 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8050 						no_fragmentflg = 0;
8051 					}
8052 					/* unsigned subtraction of mtu */
8053 					if (mtu > chk->send_size)
8054 						mtu -= chk->send_size;
8055 					else
8056 						mtu = 0;
8057 					/* unsigned subtraction of r_mtu */
8058 					if (r_mtu > chk->send_size)
8059 						r_mtu -= chk->send_size;
8060 					else
8061 						r_mtu = 0;
8062 
8063 					to_out += chk->send_size;
8064 					if ((to_out > mx_mtu) && no_fragmentflg) {
8065 #ifdef INVARIANTS
8066 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8067 #else
8068 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8069 						    mx_mtu, to_out);
8070 #endif
8071 					}
8072 					chk->window_probe = 0;
8073 					data_list[bundle_at++] = chk;
8074 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8075 						mtu = 0;
8076 						break;
8077 					}
8078 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8079 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8080 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8081 						} else {
8082 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8083 						}
8084 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8085 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8086 							/*
8087 							 * Count number of
8088 							 * user msg's that
8089 							 * were fragmented
8090 							 * we do this by
8091 							 * counting when we
8092 							 * see a LAST
8093 							 * fragment only.
8094 							 */
8095 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8096 					}
8097 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8098 						if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8099 							data_list[0]->window_probe = 1;
8100 							net->window_probe = 1;
8101 						}
8102 						break;
8103 					}
8104 				} else {
8105 					/*
8106 					 * Must be sent in order of the
8107 					 * TSN's (on a network)
8108 					 */
8109 					break;
8110 				}
8111 			}	/* for (chunk gather loop for this net) */
8112 		}		/* if asoc.state OPEN */
8113 no_data_fill:
8114 		/* Is there something to send for this destination? */
8115 		if (outchain) {
8116 			/* We may need to start a control timer or two */
8117 			if (asconf) {
8118 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8119 				    stcb, net);
8120 				/*
8121 				 * do NOT clear the asconf flag as it is
8122 				 * used to do appropriate source address
8123 				 * selection.
8124 				 */
8125 			}
8126 			if (cookie) {
8127 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8128 				cookie = 0;
8129 			}
8130 			/* must start a send timer if data is being sent */
8131 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8132 				/*
8133 				 * no timer running on this destination
8134 				 * restart it.
8135 				 */
8136 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8137 			} else if ((asoc->sctp_cmt_on_off > 0) &&
8138 				    (asoc->sctp_cmt_pf > 0) &&
8139 				    pf_hbflag &&
8140 				    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8141 			    (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8142 				/*
8143 				 * JRS 5/14/07 - If a HB has been sent to a
8144 				 * PF destination and no T3 timer is
8145 				 * currently running, start the T3 timer to
8146 				 * track the HBs that were sent.
8147 				 */
8148 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8149 			}
8150 			/* Now send it, if there is anything to send :> */
8151 			if ((error = sctp_lowlevel_chunk_output(inp,
8152 			    stcb,
8153 			    net,
8154 			    (struct sockaddr *)&net->ro._l_addr,
8155 			    outchain,
8156 			    auth_offset,
8157 			    auth,
8158 			    auth_keyid,
8159 			    no_fragmentflg,
8160 			    bundle_at,
8161 			    data_list[0],
8162 			    asconf,
8163 			    inp->sctp_lport, stcb->rport,
8164 			    htonl(stcb->asoc.peer_vtag),
8165 			    net->port, so_locked, NULL))) {
8166 				/* error, we could not output */
8167 				if (error == ENOBUFS) {
8168 					SCTP_STAT_INCR(sctps_lowlevelerr);
8169 					asoc->ifp_had_enobuf = 1;
8170 				}
8171 				if (from_where == 0) {
8172 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
8173 				}
8174 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8175 				if (hbflag) {
8176 					if (*now_filled == 0) {
8177 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8178 						*now_filled = 1;
8179 						*now = net->last_sent_time;
8180 					} else {
8181 						net->last_sent_time = *now;
8182 					}
8183 					hbflag = 0;
8184 				}
8185 				if (error == EHOSTUNREACH) {
8186 					/*
8187 					 * Destination went unreachable
8188 					 * during this send
8189 					 */
8190 					sctp_move_chunks_from_net(stcb, net);
8191 				}
8192 				*reason_code = 6;
8193 				/*-
8194 				 * I add this line to be paranoid. As far as
8195 				 * I can tell the continue, takes us back to
8196 				 * the top of the for, but just to make sure
8197 				 * I will reset these again here.
8198 				 */
8199 				ctl_cnt = bundle_at = 0;
8200 				continue;	/* This takes us back to the
8201 						 * for() for the nets. */
8202 			} else {
8203 				asoc->ifp_had_enobuf = 0;
8204 			}
8205 			outchain = endoutchain = NULL;
8206 			auth = NULL;
8207 			auth_offset = 0;
8208 			if (bundle_at || hbflag) {
8209 				/* For data/asconf and hb set time */
8210 				if (*now_filled == 0) {
8211 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8212 					*now_filled = 1;
8213 					*now = net->last_sent_time;
8214 				} else {
8215 					net->last_sent_time = *now;
8216 				}
8217 			}
8218 			if (!no_out_cnt) {
8219 				*num_out += (ctl_cnt + bundle_at);
8220 			}
8221 			if (bundle_at) {
8222 				/* setup for a RTO measurement */
8223 				tsns_sent = data_list[0]->rec.data.TSN_seq;
8224 				/* fill time if not already filled */
8225 				if (*now_filled == 0) {
8226 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8227 					*now_filled = 1;
8228 					*now = asoc->time_last_sent;
8229 				} else {
8230 					asoc->time_last_sent = *now;
8231 				}
8232 				data_list[0]->do_rtt = 1;
8233 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8234 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8235 				if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8236 					if (net->flight_size < net->cwnd) {
8237 						/* start or restart it */
8238 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8239 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8240 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8241 						}
8242 						SCTP_STAT_INCR(sctps_earlyfrstrout);
8243 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8244 					} else {
8245 						/* stop it if its running */
8246 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8247 							SCTP_STAT_INCR(sctps_earlyfrstpout);
8248 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8249 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8250 						}
8251 					}
8252 				}
8253 			}
8254 			if (one_chunk) {
8255 				break;
8256 			}
8257 		}
8258 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8259 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8260 		}
8261 	}
8262 	if (old_start_at == NULL) {
8263 		old_start_at = start_at;
8264 		start_at = TAILQ_FIRST(&asoc->nets);
8265 		if (old_start_at)
8266 			goto again_one_more_time;
8267 	}
8268 	/*
8269 	 * At the end there should be no NON timed chunks hanging on this
8270 	 * queue.
8271 	 */
8272 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8273 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8274 	}
8275 	if ((*num_out == 0) && (*reason_code == 0)) {
8276 		*reason_code = 4;
8277 	} else {
8278 		*reason_code = 5;
8279 	}
8280 	sctp_clean_up_ctl(stcb, asoc);
8281 	return (0);
8282 }
8283 
8284 void
8285 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8286 {
8287 	/*-
8288 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8289 	 * the control chunk queue.
8290 	 */
8291 	struct sctp_chunkhdr *hdr;
8292 	struct sctp_tmit_chunk *chk;
8293 	struct mbuf *mat;
8294 
8295 	SCTP_TCB_LOCK_ASSERT(stcb);
8296 	sctp_alloc_a_chunk(stcb, chk);
8297 	if (chk == NULL) {
8298 		/* no memory */
8299 		sctp_m_freem(op_err);
8300 		return;
8301 	}
8302 	chk->copy_by_ref = 0;
8303 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8304 	if (op_err == NULL) {
8305 		sctp_free_a_chunk(stcb, chk);
8306 		return;
8307 	}
8308 	chk->send_size = 0;
8309 	mat = op_err;
8310 	while (mat != NULL) {
8311 		chk->send_size += SCTP_BUF_LEN(mat);
8312 		mat = SCTP_BUF_NEXT(mat);
8313 	}
8314 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8315 	chk->rec.chunk_id.can_take_data = 1;
8316 	chk->sent = SCTP_DATAGRAM_UNSENT;
8317 	chk->snd_count = 0;
8318 	chk->flags = 0;
8319 	chk->asoc = &stcb->asoc;
8320 	chk->data = op_err;
8321 	chk->whoTo = chk->asoc->primary_destination;
8322 	atomic_add_int(&chk->whoTo->ref_count, 1);
8323 	hdr = mtod(op_err, struct sctp_chunkhdr *);
8324 	hdr->chunk_type = SCTP_OPERATION_ERROR;
8325 	hdr->chunk_flags = 0;
8326 	hdr->chunk_length = htons(chk->send_size);
8327 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8328 	    chk,
8329 	    sctp_next);
8330 	chk->asoc->ctrl_queue_cnt++;
8331 }
8332 
8333 int
8334 sctp_send_cookie_echo(struct mbuf *m,
8335     int offset,
8336     struct sctp_tcb *stcb,
8337     struct sctp_nets *net)
8338 {
8339 	/*-
8340 	 * pull out the cookie and put it at the front of the control chunk
8341 	 * queue.
8342 	 */
8343 	int at;
8344 	struct mbuf *cookie;
8345 	struct sctp_paramhdr parm, *phdr;
8346 	struct sctp_chunkhdr *hdr;
8347 	struct sctp_tmit_chunk *chk;
8348 	uint16_t ptype, plen;
8349 
8350 	/* First find the cookie in the param area */
8351 	cookie = NULL;
8352 	at = offset + sizeof(struct sctp_init_chunk);
8353 
8354 	SCTP_TCB_LOCK_ASSERT(stcb);
8355 	do {
8356 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8357 		if (phdr == NULL) {
8358 			return (-3);
8359 		}
8360 		ptype = ntohs(phdr->param_type);
8361 		plen = ntohs(phdr->param_length);
8362 		if (ptype == SCTP_STATE_COOKIE) {
8363 			int pad;
8364 
8365 			/* found the cookie */
8366 			if ((pad = (plen % 4))) {
8367 				plen += 4 - pad;
8368 			}
8369 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8370 			if (cookie == NULL) {
8371 				/* No memory */
8372 				return (-2);
8373 			}
8374 #ifdef SCTP_MBUF_LOGGING
8375 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8376 				struct mbuf *mat;
8377 
8378 				mat = cookie;
8379 				while (mat) {
8380 					if (SCTP_BUF_IS_EXTENDED(mat)) {
8381 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8382 					}
8383 					mat = SCTP_BUF_NEXT(mat);
8384 				}
8385 			}
8386 #endif
8387 			break;
8388 		}
8389 		at += SCTP_SIZE32(plen);
8390 	} while (phdr);
8391 	if (cookie == NULL) {
8392 		/* Did not find the cookie */
8393 		return (-3);
8394 	}
8395 	/* ok, we got the cookie lets change it into a cookie echo chunk */
8396 
8397 	/* first the change from param to cookie */
8398 	hdr = mtod(cookie, struct sctp_chunkhdr *);
8399 	hdr->chunk_type = SCTP_COOKIE_ECHO;
8400 	hdr->chunk_flags = 0;
8401 	/* get the chunk stuff now and place it in the FRONT of the queue */
8402 	sctp_alloc_a_chunk(stcb, chk);
8403 	if (chk == NULL) {
8404 		/* no memory */
8405 		sctp_m_freem(cookie);
8406 		return (-5);
8407 	}
8408 	chk->copy_by_ref = 0;
8409 	chk->send_size = plen;
8410 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8411 	chk->rec.chunk_id.can_take_data = 0;
8412 	chk->sent = SCTP_DATAGRAM_UNSENT;
8413 	chk->snd_count = 0;
8414 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8415 	chk->asoc = &stcb->asoc;
8416 	chk->data = cookie;
8417 	chk->whoTo = chk->asoc->primary_destination;
8418 	atomic_add_int(&chk->whoTo->ref_count, 1);
8419 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8420 	chk->asoc->ctrl_queue_cnt++;
8421 	return (0);
8422 }
8423 
8424 void
8425 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8426     struct mbuf *m,
8427     int offset,
8428     int chk_length,
8429     struct sctp_nets *net)
8430 {
8431 	/*
8432 	 * take a HB request and make it into a HB ack and send it.
8433 	 */
8434 	struct mbuf *outchain;
8435 	struct sctp_chunkhdr *chdr;
8436 	struct sctp_tmit_chunk *chk;
8437 
8438 
8439 	if (net == NULL)
8440 		/* must have a net pointer */
8441 		return;
8442 
8443 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8444 	if (outchain == NULL) {
8445 		/* gak out of memory */
8446 		return;
8447 	}
8448 #ifdef SCTP_MBUF_LOGGING
8449 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8450 		struct mbuf *mat;
8451 
8452 		mat = outchain;
8453 		while (mat) {
8454 			if (SCTP_BUF_IS_EXTENDED(mat)) {
8455 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8456 			}
8457 			mat = SCTP_BUF_NEXT(mat);
8458 		}
8459 	}
8460 #endif
8461 	chdr = mtod(outchain, struct sctp_chunkhdr *);
8462 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8463 	chdr->chunk_flags = 0;
8464 	if (chk_length % 4) {
8465 		/* need pad */
8466 		uint32_t cpthis = 0;
8467 		int padlen;
8468 
8469 		padlen = 4 - (chk_length % 4);
8470 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8471 	}
8472 	sctp_alloc_a_chunk(stcb, chk);
8473 	if (chk == NULL) {
8474 		/* no memory */
8475 		sctp_m_freem(outchain);
8476 		return;
8477 	}
8478 	chk->copy_by_ref = 0;
8479 	chk->send_size = chk_length;
8480 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8481 	chk->rec.chunk_id.can_take_data = 1;
8482 	chk->sent = SCTP_DATAGRAM_UNSENT;
8483 	chk->snd_count = 0;
8484 	chk->flags = 0;
8485 	chk->asoc = &stcb->asoc;
8486 	chk->data = outchain;
8487 	chk->whoTo = net;
8488 	atomic_add_int(&chk->whoTo->ref_count, 1);
8489 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8490 	chk->asoc->ctrl_queue_cnt++;
8491 }
8492 
8493 void
8494 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8495 {
8496 	/* formulate and queue a cookie-ack back to sender */
8497 	struct mbuf *cookie_ack;
8498 	struct sctp_chunkhdr *hdr;
8499 	struct sctp_tmit_chunk *chk;
8500 
8501 	cookie_ack = NULL;
8502 	SCTP_TCB_LOCK_ASSERT(stcb);
8503 
8504 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8505 	if (cookie_ack == NULL) {
8506 		/* no mbuf's */
8507 		return;
8508 	}
8509 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8510 	sctp_alloc_a_chunk(stcb, chk);
8511 	if (chk == NULL) {
8512 		/* no memory */
8513 		sctp_m_freem(cookie_ack);
8514 		return;
8515 	}
8516 	chk->copy_by_ref = 0;
8517 	chk->send_size = sizeof(struct sctp_chunkhdr);
8518 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8519 	chk->rec.chunk_id.can_take_data = 1;
8520 	chk->sent = SCTP_DATAGRAM_UNSENT;
8521 	chk->snd_count = 0;
8522 	chk->flags = 0;
8523 	chk->asoc = &stcb->asoc;
8524 	chk->data = cookie_ack;
8525 	if (chk->asoc->last_control_chunk_from != NULL) {
8526 		chk->whoTo = chk->asoc->last_control_chunk_from;
8527 	} else {
8528 		chk->whoTo = chk->asoc->primary_destination;
8529 	}
8530 	atomic_add_int(&chk->whoTo->ref_count, 1);
8531 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8532 	hdr->chunk_type = SCTP_COOKIE_ACK;
8533 	hdr->chunk_flags = 0;
8534 	hdr->chunk_length = htons(chk->send_size);
8535 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8536 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8537 	chk->asoc->ctrl_queue_cnt++;
8538 	return;
8539 }
8540 
8541 
8542 void
8543 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8544 {
8545 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
8546 	struct mbuf *m_shutdown_ack;
8547 	struct sctp_shutdown_ack_chunk *ack_cp;
8548 	struct sctp_tmit_chunk *chk;
8549 
8550 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8551 	if (m_shutdown_ack == NULL) {
8552 		/* no mbuf's */
8553 		return;
8554 	}
8555 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8556 	sctp_alloc_a_chunk(stcb, chk);
8557 	if (chk == NULL) {
8558 		/* no memory */
8559 		sctp_m_freem(m_shutdown_ack);
8560 		return;
8561 	}
8562 	chk->copy_by_ref = 0;
8563 	chk->send_size = sizeof(struct sctp_chunkhdr);
8564 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8565 	chk->rec.chunk_id.can_take_data = 1;
8566 	chk->sent = SCTP_DATAGRAM_UNSENT;
8567 	chk->snd_count = 0;
8568 	chk->flags = 0;
8569 	chk->asoc = &stcb->asoc;
8570 	chk->data = m_shutdown_ack;
8571 	chk->whoTo = net;
8572 	atomic_add_int(&net->ref_count, 1);
8573 
8574 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8575 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8576 	ack_cp->ch.chunk_flags = 0;
8577 	ack_cp->ch.chunk_length = htons(chk->send_size);
8578 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8579 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8580 	chk->asoc->ctrl_queue_cnt++;
8581 	return;
8582 }
8583 
8584 void
8585 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8586 {
8587 	/* formulate and queue a SHUTDOWN to the sender */
8588 	struct mbuf *m_shutdown;
8589 	struct sctp_shutdown_chunk *shutdown_cp;
8590 	struct sctp_tmit_chunk *chk;
8591 
8592 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8593 	if (m_shutdown == NULL) {
8594 		/* no mbuf's */
8595 		return;
8596 	}
8597 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8598 	sctp_alloc_a_chunk(stcb, chk);
8599 	if (chk == NULL) {
8600 		/* no memory */
8601 		sctp_m_freem(m_shutdown);
8602 		return;
8603 	}
8604 	chk->copy_by_ref = 0;
8605 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
8606 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8607 	chk->rec.chunk_id.can_take_data = 1;
8608 	chk->sent = SCTP_DATAGRAM_UNSENT;
8609 	chk->snd_count = 0;
8610 	chk->flags = 0;
8611 	chk->asoc = &stcb->asoc;
8612 	chk->data = m_shutdown;
8613 	chk->whoTo = net;
8614 	atomic_add_int(&net->ref_count, 1);
8615 
8616 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8617 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8618 	shutdown_cp->ch.chunk_flags = 0;
8619 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
8620 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8621 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8622 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8623 	chk->asoc->ctrl_queue_cnt++;
8624 	return;
8625 }
8626 
8627 void
8628 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8629 {
8630 	/*
8631 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
8632 	 * should be queued on the assoc queue.
8633 	 */
8634 	struct sctp_tmit_chunk *chk;
8635 	struct mbuf *m_asconf;
8636 	int len;
8637 
8638 	SCTP_TCB_LOCK_ASSERT(stcb);
8639 
8640 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8641 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8642 		/* can't send a new one if there is one in flight already */
8643 		return;
8644 	}
8645 	/* compose an ASCONF chunk, maximum length is PMTU */
8646 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8647 	if (m_asconf == NULL) {
8648 		return;
8649 	}
8650 	sctp_alloc_a_chunk(stcb, chk);
8651 	if (chk == NULL) {
8652 		/* no memory */
8653 		sctp_m_freem(m_asconf);
8654 		return;
8655 	}
8656 	chk->copy_by_ref = 0;
8657 	chk->data = m_asconf;
8658 	chk->send_size = len;
8659 	chk->rec.chunk_id.id = SCTP_ASCONF;
8660 	chk->rec.chunk_id.can_take_data = 0;
8661 	chk->sent = SCTP_DATAGRAM_UNSENT;
8662 	chk->snd_count = 0;
8663 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8664 	chk->asoc = &stcb->asoc;
8665 	chk->whoTo = net;
8666 	atomic_add_int(&chk->whoTo->ref_count, 1);
8667 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8668 	chk->asoc->ctrl_queue_cnt++;
8669 	return;
8670 }
8671 
8672 void
8673 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8674 {
8675 	/*
8676 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
8677 	 * must be stored in the tcb.
8678 	 */
8679 	struct sctp_tmit_chunk *chk;
8680 	struct sctp_asconf_ack *ack, *latest_ack;
8681 	struct mbuf *m_ack, *m;
8682 	struct sctp_nets *net = NULL;
8683 
8684 	SCTP_TCB_LOCK_ASSERT(stcb);
8685 	/* Get the latest ASCONF-ACK */
8686 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8687 	if (latest_ack == NULL) {
8688 		return;
8689 	}
8690 	if (latest_ack->last_sent_to != NULL &&
8691 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8692 		/* we're doing a retransmission */
8693 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8694 		if (net == NULL) {
8695 			/* no alternate */
8696 			if (stcb->asoc.last_control_chunk_from == NULL)
8697 				net = stcb->asoc.primary_destination;
8698 			else
8699 				net = stcb->asoc.last_control_chunk_from;
8700 		}
8701 	} else {
8702 		/* normal case */
8703 		if (stcb->asoc.last_control_chunk_from == NULL)
8704 			net = stcb->asoc.primary_destination;
8705 		else
8706 			net = stcb->asoc.last_control_chunk_from;
8707 	}
8708 	latest_ack->last_sent_to = net;
8709 
8710 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8711 		if (ack->data == NULL) {
8712 			continue;
8713 		}
8714 		/* copy the asconf_ack */
8715 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8716 		if (m_ack == NULL) {
8717 			/* couldn't copy it */
8718 			return;
8719 		}
8720 #ifdef SCTP_MBUF_LOGGING
8721 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8722 			struct mbuf *mat;
8723 
8724 			mat = m_ack;
8725 			while (mat) {
8726 				if (SCTP_BUF_IS_EXTENDED(mat)) {
8727 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8728 				}
8729 				mat = SCTP_BUF_NEXT(mat);
8730 			}
8731 		}
8732 #endif
8733 
8734 		sctp_alloc_a_chunk(stcb, chk);
8735 		if (chk == NULL) {
8736 			/* no memory */
8737 			if (m_ack)
8738 				sctp_m_freem(m_ack);
8739 			return;
8740 		}
8741 		chk->copy_by_ref = 0;
8742 
8743 		chk->whoTo = net;
8744 		chk->data = m_ack;
8745 		chk->send_size = 0;
8746 		/* Get size */
8747 		m = m_ack;
8748 		chk->send_size = ack->len;
8749 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8750 		chk->rec.chunk_id.can_take_data = 1;
8751 		chk->sent = SCTP_DATAGRAM_UNSENT;
8752 		chk->snd_count = 0;
8753 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
8754 		chk->asoc = &stcb->asoc;
8755 		atomic_add_int(&chk->whoTo->ref_count, 1);
8756 
8757 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8758 		chk->asoc->ctrl_queue_cnt++;
8759 	}
8760 	return;
8761 }
8762 
8763 
8764 static int
8765 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8766     struct sctp_tcb *stcb,
8767     struct sctp_association *asoc,
8768     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8769 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8770     SCTP_UNUSED
8771 #endif
8772 )
8773 {
8774 	/*-
8775 	 * send out one MTU of retransmission. If fast_retransmit is
8776 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
8777 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
8778 	 * retransmit them by themselves.
8779 	 *
8780 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
8781 	 * marked for resend and bundle them all together (up to a MTU of
8782 	 * destination). The address to send to should have been
8783 	 * selected/changed where the retransmission was marked (i.e. in FR
8784 	 * or t3-timeout routines).
8785 	 */
8786 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8787 	struct sctp_tmit_chunk *chk, *fwd;
8788 	struct mbuf *m, *endofchain;
8789 	struct sctp_nets *net = NULL;
8790 	uint32_t tsns_sent = 0;
8791 	int no_fragmentflg, bundle_at, cnt_thru;
8792 	unsigned int mtu;
8793 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8794 	struct sctp_auth_chunk *auth = NULL;
8795 	uint32_t auth_offset = 0;
8796 	uint16_t auth_keyid;
8797 	int override_ok = 1;
8798 	int data_auth_reqd = 0;
8799 	uint32_t dmtu = 0;
8800 
8801 	SCTP_TCB_LOCK_ASSERT(stcb);
8802 	tmr_started = ctl_cnt = bundle_at = error = 0;
8803 	no_fragmentflg = 1;
8804 	fwd_tsn = 0;
8805 	*cnt_out = 0;
8806 	fwd = NULL;
8807 	endofchain = m = NULL;
8808 	auth_keyid = stcb->asoc.authinfo.active_keyid;
8809 #ifdef SCTP_AUDITING_ENABLED
8810 	sctp_audit_log(0xC3, 1);
8811 #endif
8812 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8813 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
8814 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8815 		    asoc->sent_queue_retran_cnt);
8816 		asoc->sent_queue_cnt = 0;
8817 		asoc->sent_queue_cnt_removeable = 0;
8818 		/* send back 0/0 so we enter normal transmission */
8819 		*cnt_out = 0;
8820 		return (0);
8821 	}
8822 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8823 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8824 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8825 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8826 			if (chk->sent != SCTP_DATAGRAM_RESEND) {
8827 				continue;
8828 			}
8829 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8830 				if (chk != asoc->str_reset) {
8831 					/*
8832 					 * not eligible for retran if its
8833 					 * not ours
8834 					 */
8835 					continue;
8836 				}
8837 			}
8838 			ctl_cnt++;
8839 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8840 				fwd_tsn = 1;
8841 				fwd = chk;
8842 			}
8843 			/*
8844 			 * Add an AUTH chunk, if chunk requires it save the
8845 			 * offset into the chain for AUTH
8846 			 */
8847 			if ((auth == NULL) &&
8848 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8849 			    stcb->asoc.peer_auth_chunks))) {
8850 				m = sctp_add_auth_chunk(m, &endofchain,
8851 				    &auth, &auth_offset,
8852 				    stcb,
8853 				    chk->rec.chunk_id.id);
8854 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8855 			}
8856 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8857 			break;
8858 		}
8859 	}
8860 	one_chunk = 0;
8861 	cnt_thru = 0;
8862 	/* do we have control chunks to retransmit? */
8863 	if (m != NULL) {
8864 		/* Start a timer no matter if we suceed or fail */
8865 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8866 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
8867 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
8868 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
8869 		chk->snd_count++;	/* update our count */
8870 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
8871 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
8872 		    auth_offset, auth, stcb->asoc.authinfo.active_keyid,
8873 		    no_fragmentflg, 0, NULL, 0,
8874 		    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
8875 		    chk->whoTo->port, so_locked, NULL))) {
8876 			SCTP_STAT_INCR(sctps_lowlevelerr);
8877 			return (error);
8878 		}
8879 		m = endofchain = NULL;
8880 		auth = NULL;
8881 		auth_offset = 0;
8882 		/*
8883 		 * We don't want to mark the net->sent time here since this
8884 		 * we use this for HB and retrans cannot measure RTT
8885 		 */
8886 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
8887 		*cnt_out += 1;
8888 		chk->sent = SCTP_DATAGRAM_SENT;
8889 		sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
8890 		if (fwd_tsn == 0) {
8891 			return (0);
8892 		} else {
8893 			/* Clean up the fwd-tsn list */
8894 			sctp_clean_up_ctl(stcb, asoc);
8895 			return (0);
8896 		}
8897 	}
8898 	/*
8899 	 * Ok, it is just data retransmission we need to do or that and a
8900 	 * fwd-tsn with it all.
8901 	 */
8902 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
8903 		return (SCTP_RETRAN_DONE);
8904 	}
8905 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
8906 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
8907 		/* not yet open, resend the cookie and that is it */
8908 		return (1);
8909 	}
8910 #ifdef SCTP_AUDITING_ENABLED
8911 	sctp_auditing(20, inp, stcb, NULL);
8912 #endif
8913 	data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
8914 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
8915 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
8916 			/* No, not sent to this net or not ready for rtx */
8917 			continue;
8918 		}
8919 		if (chk->data == NULL) {
8920 			printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
8921 			    chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
8922 			continue;
8923 		}
8924 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
8925 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
8926 			/* Gak, we have exceeded max unlucky retran, abort! */
8927 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
8928 			    chk->snd_count,
8929 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
8930 			atomic_add_int(&stcb->asoc.refcnt, 1);
8931 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
8932 			SCTP_TCB_LOCK(stcb);
8933 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
8934 			return (SCTP_RETRAN_EXIT);
8935 		}
8936 		/* pick up the net */
8937 		net = chk->whoTo;
8938 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8939 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8940 		} else {
8941 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8942 		}
8943 
8944 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
8945 			/* No room in peers rwnd */
8946 			uint32_t tsn;
8947 
8948 			tsn = asoc->last_acked_seq + 1;
8949 			if (tsn == chk->rec.data.TSN_seq) {
8950 				/*
8951 				 * we make a special exception for this
8952 				 * case. The peer has no rwnd but is missing
8953 				 * the lowest chunk.. which is probably what
8954 				 * is holding up the rwnd.
8955 				 */
8956 				goto one_chunk_around;
8957 			}
8958 			return (1);
8959 		}
8960 one_chunk_around:
8961 		if (asoc->peers_rwnd < mtu) {
8962 			one_chunk = 1;
8963 			if ((asoc->peers_rwnd == 0) &&
8964 			    (asoc->total_flight == 0)) {
8965 				chk->window_probe = 1;
8966 				chk->whoTo->window_probe = 1;
8967 			}
8968 		}
8969 #ifdef SCTP_AUDITING_ENABLED
8970 		sctp_audit_log(0xC3, 2);
8971 #endif
8972 		bundle_at = 0;
8973 		m = NULL;
8974 		net->fast_retran_ip = 0;
8975 		if (chk->rec.data.doing_fast_retransmit == 0) {
8976 			/*
8977 			 * if no FR in progress skip destination that have
8978 			 * flight_size > cwnd.
8979 			 */
8980 			if (net->flight_size >= net->cwnd) {
8981 				continue;
8982 			}
8983 		} else {
8984 			/*
8985 			 * Mark the destination net to have FR recovery
8986 			 * limits put on it.
8987 			 */
8988 			*fr_done = 1;
8989 			net->fast_retran_ip = 1;
8990 		}
8991 
8992 		/*
8993 		 * if no AUTH is yet included and this chunk requires it,
8994 		 * make sure to account for it.  We don't apply the size
8995 		 * until the AUTH chunk is actually added below in case
8996 		 * there is no room for this chunk.
8997 		 */
8998 		if (data_auth_reqd && (auth == NULL)) {
8999 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9000 		} else
9001 			dmtu = 0;
9002 
9003 		if ((chk->send_size <= (mtu - dmtu)) ||
9004 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9005 			/* ok we will add this one */
9006 			if (data_auth_reqd) {
9007 				if (auth == NULL) {
9008 					m = sctp_add_auth_chunk(m,
9009 					    &endofchain,
9010 					    &auth,
9011 					    &auth_offset,
9012 					    stcb,
9013 					    SCTP_DATA);
9014 					auth_keyid = chk->auth_keyid;
9015 					override_ok = 0;
9016 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9017 				} else if (override_ok) {
9018 					auth_keyid = chk->auth_keyid;
9019 					override_ok = 0;
9020 				} else if (chk->auth_keyid != auth_keyid) {
9021 					/* different keyid, so done bundling */
9022 					break;
9023 				}
9024 			}
9025 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9026 			if (m == NULL) {
9027 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9028 				return (ENOMEM);
9029 			}
9030 			/* Do clear IP_DF ? */
9031 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9032 				no_fragmentflg = 0;
9033 			}
9034 			/* upate our MTU size */
9035 			if (mtu > (chk->send_size + dmtu))
9036 				mtu -= (chk->send_size + dmtu);
9037 			else
9038 				mtu = 0;
9039 			data_list[bundle_at++] = chk;
9040 			if (one_chunk && (asoc->total_flight <= 0)) {
9041 				SCTP_STAT_INCR(sctps_windowprobed);
9042 			}
9043 		}
9044 		if (one_chunk == 0) {
9045 			/*
9046 			 * now are there anymore forward from chk to pick
9047 			 * up?
9048 			 */
9049 			fwd = TAILQ_NEXT(chk, sctp_next);
9050 			while (fwd) {
9051 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9052 					/* Nope, not for retran */
9053 					fwd = TAILQ_NEXT(fwd, sctp_next);
9054 					continue;
9055 				}
9056 				if (fwd->whoTo != net) {
9057 					/* Nope, not the net in question */
9058 					fwd = TAILQ_NEXT(fwd, sctp_next);
9059 					continue;
9060 				}
9061 				if (data_auth_reqd && (auth == NULL)) {
9062 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9063 				} else
9064 					dmtu = 0;
9065 				if (fwd->send_size <= (mtu - dmtu)) {
9066 					if (data_auth_reqd) {
9067 						if (auth == NULL) {
9068 							m = sctp_add_auth_chunk(m,
9069 							    &endofchain,
9070 							    &auth,
9071 							    &auth_offset,
9072 							    stcb,
9073 							    SCTP_DATA);
9074 							auth_keyid = fwd->auth_keyid;
9075 							override_ok = 0;
9076 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9077 						} else if (override_ok) {
9078 							auth_keyid = fwd->auth_keyid;
9079 							override_ok = 0;
9080 						} else if (fwd->auth_keyid != auth_keyid) {
9081 							/*
9082 							 * different keyid,
9083 							 * so done bundling
9084 							 */
9085 							break;
9086 						}
9087 					}
9088 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9089 					if (m == NULL) {
9090 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9091 						return (ENOMEM);
9092 					}
9093 					/* Do clear IP_DF ? */
9094 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9095 						no_fragmentflg = 0;
9096 					}
9097 					/* upate our MTU size */
9098 					if (mtu > (fwd->send_size + dmtu))
9099 						mtu -= (fwd->send_size + dmtu);
9100 					else
9101 						mtu = 0;
9102 					data_list[bundle_at++] = fwd;
9103 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9104 						break;
9105 					}
9106 					fwd = TAILQ_NEXT(fwd, sctp_next);
9107 				} else {
9108 					/* can't fit so we are done */
9109 					break;
9110 				}
9111 			}
9112 		}
9113 		/* Is there something to send for this destination? */
9114 		if (m) {
9115 			/*
9116 			 * No matter if we fail/or suceed we should start a
9117 			 * timer. A failure is like a lost IP packet :-)
9118 			 */
9119 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9120 				/*
9121 				 * no timer running on this destination
9122 				 * restart it.
9123 				 */
9124 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9125 				tmr_started = 1;
9126 			}
9127 			/* Now lets send it, if there is anything to send :> */
9128 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9129 			    (struct sockaddr *)&net->ro._l_addr, m,
9130 			    auth_offset, auth, auth_keyid,
9131 			    no_fragmentflg, 0, NULL, 0,
9132 			    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9133 			    net->port, so_locked, NULL))) {
9134 				/* error, we could not output */
9135 				SCTP_STAT_INCR(sctps_lowlevelerr);
9136 				return (error);
9137 			}
9138 			m = endofchain = NULL;
9139 			auth = NULL;
9140 			auth_offset = 0;
9141 			/* For HB's */
9142 			/*
9143 			 * We don't want to mark the net->sent time here
9144 			 * since this we use this for HB and retrans cannot
9145 			 * measure RTT
9146 			 */
9147 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9148 
9149 			/* For auto-close */
9150 			cnt_thru++;
9151 			if (*now_filled == 0) {
9152 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9153 				*now = asoc->time_last_sent;
9154 				*now_filled = 1;
9155 			} else {
9156 				asoc->time_last_sent = *now;
9157 			}
9158 			*cnt_out += bundle_at;
9159 #ifdef SCTP_AUDITING_ENABLED
9160 			sctp_audit_log(0xC4, bundle_at);
9161 #endif
9162 			if (bundle_at) {
9163 				tsns_sent = data_list[0]->rec.data.TSN_seq;
9164 			}
9165 			for (i = 0; i < bundle_at; i++) {
9166 				SCTP_STAT_INCR(sctps_sendretransdata);
9167 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
9168 				/*
9169 				 * When we have a revoked data, and we
9170 				 * retransmit it, then we clear the revoked
9171 				 * flag since this flag dictates if we
9172 				 * subtracted from the fs
9173 				 */
9174 				if (data_list[i]->rec.data.chunk_was_revoked) {
9175 					/* Deflate the cwnd */
9176 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9177 					data_list[i]->rec.data.chunk_was_revoked = 0;
9178 				}
9179 				data_list[i]->snd_count++;
9180 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9181 				/* record the time */
9182 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
9183 				if (data_list[i]->book_size_scale) {
9184 					/*
9185 					 * need to double the book size on
9186 					 * this one
9187 					 */
9188 					data_list[i]->book_size_scale = 0;
9189 					/*
9190 					 * Since we double the booksize, we
9191 					 * must also double the output queue
9192 					 * size, since this get shrunk when
9193 					 * we free by this amount.
9194 					 */
9195 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9196 					data_list[i]->book_size *= 2;
9197 
9198 
9199 				} else {
9200 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9201 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9202 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9203 					}
9204 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9205 					    (uint32_t) (data_list[i]->send_size +
9206 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9207 				}
9208 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9209 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9210 					    data_list[i]->whoTo->flight_size,
9211 					    data_list[i]->book_size,
9212 					    (uintptr_t) data_list[i]->whoTo,
9213 					    data_list[i]->rec.data.TSN_seq);
9214 				}
9215 				sctp_flight_size_increase(data_list[i]);
9216 				sctp_total_flight_increase(stcb, data_list[i]);
9217 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9218 					/* SWS sender side engages */
9219 					asoc->peers_rwnd = 0;
9220 				}
9221 				if ((i == 0) &&
9222 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
9223 					SCTP_STAT_INCR(sctps_sendfastretrans);
9224 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9225 					    (tmr_started == 0)) {
9226 						/*-
9227 						 * ok we just fast-retrans'd
9228 						 * the lowest TSN, i.e the
9229 						 * first on the list. In
9230 						 * this case we want to give
9231 						 * some more time to get a
9232 						 * SACK back without a
9233 						 * t3-expiring.
9234 						 */
9235 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9236 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9237 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9238 					}
9239 				}
9240 			}
9241 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9242 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9243 			}
9244 #ifdef SCTP_AUDITING_ENABLED
9245 			sctp_auditing(21, inp, stcb, NULL);
9246 #endif
9247 		} else {
9248 			/* None will fit */
9249 			return (1);
9250 		}
9251 		if (asoc->sent_queue_retran_cnt <= 0) {
9252 			/* all done we have no more to retran */
9253 			asoc->sent_queue_retran_cnt = 0;
9254 			break;
9255 		}
9256 		if (one_chunk) {
9257 			/* No more room in rwnd */
9258 			return (1);
9259 		}
9260 		/* stop the for loop here. we sent out a packet */
9261 		break;
9262 	}
9263 	return (0);
9264 }
9265 
9266 
9267 static int
9268 sctp_timer_validation(struct sctp_inpcb *inp,
9269     struct sctp_tcb *stcb,
9270     struct sctp_association *asoc,
9271     int ret)
9272 {
9273 	struct sctp_nets *net;
9274 
9275 	/* Validate that a timer is running somewhere */
9276 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9277 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9278 			/* Here is a timer */
9279 			return (ret);
9280 		}
9281 	}
9282 	SCTP_TCB_LOCK_ASSERT(stcb);
9283 	/* Gak, we did not have a timer somewhere */
9284 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9285 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9286 	return (ret);
9287 }
9288 
9289 void
9290 sctp_chunk_output(struct sctp_inpcb *inp,
9291     struct sctp_tcb *stcb,
9292     int from_where,
9293     int so_locked
9294 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9295     SCTP_UNUSED
9296 #endif
9297 )
9298 {
9299 	/*-
9300 	 * Ok this is the generic chunk service queue. we must do the
9301 	 * following:
9302 	 * - See if there are retransmits pending, if so we must
9303 	 *   do these first.
9304 	 * - Service the stream queue that is next, moving any
9305 	 *   message (note I must get a complete message i.e.
9306 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9307 	 *   TSN's
9308 	 * - Check to see if the cwnd/rwnd allows any output, if so we
9309 	 *   go ahead and fomulate and send the low level chunks. Making sure
9310 	 *   to combine any control in the control chunk queue also.
9311 	 */
9312 	struct sctp_association *asoc;
9313 	struct sctp_nets *net;
9314 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9315 	    burst_cnt = 0;
9316 	struct timeval now;
9317 	int now_filled = 0;
9318 	int nagle_on = 0;
9319 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9320 	int un_sent = 0;
9321 	int fr_done, tot_frs = 0;
9322 
9323 	asoc = &stcb->asoc;
9324 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9325 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9326 			nagle_on = 0;
9327 		} else {
9328 			nagle_on = 1;
9329 		}
9330 	}
9331 	SCTP_TCB_LOCK_ASSERT(stcb);
9332 
9333 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9334 
9335 	if ((un_sent <= 0) &&
9336 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9337 	    (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9338 	    (asoc->sent_queue_retran_cnt == 0)) {
9339 		/* Nothing to do unless there is something to be sent left */
9340 		return;
9341 	}
9342 	/*
9343 	 * Do we have something to send, data or control AND a sack timer
9344 	 * running, if so piggy-back the sack.
9345 	 */
9346 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9347 		sctp_send_sack(stcb);
9348 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9349 	}
9350 	while (asoc->sent_queue_retran_cnt) {
9351 		/*-
9352 		 * Ok, it is retransmission time only, we send out only ONE
9353 		 * packet with a single call off to the retran code.
9354 		 */
9355 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9356 			/*-
9357 			 * Special hook for handling cookiess discarded
9358 			 * by peer that carried data. Send cookie-ack only
9359 			 * and then the next call with get the retran's.
9360 			 */
9361 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9362 			    from_where,
9363 			    &now, &now_filled, frag_point, so_locked);
9364 			return;
9365 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9366 			/* if its not from a HB then do it */
9367 			fr_done = 0;
9368 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9369 			if (fr_done) {
9370 				tot_frs++;
9371 			}
9372 		} else {
9373 			/*
9374 			 * its from any other place, we don't allow retran
9375 			 * output (only control)
9376 			 */
9377 			ret = 1;
9378 		}
9379 		if (ret > 0) {
9380 			/* Can't send anymore */
9381 			/*-
9382 			 * now lets push out control by calling med-level
9383 			 * output once. this assures that we WILL send HB's
9384 			 * if queued too.
9385 			 */
9386 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9387 			    from_where,
9388 			    &now, &now_filled, frag_point, so_locked);
9389 #ifdef SCTP_AUDITING_ENABLED
9390 			sctp_auditing(8, inp, stcb, NULL);
9391 #endif
9392 			(void)sctp_timer_validation(inp, stcb, asoc, ret);
9393 			return;
9394 		}
9395 		if (ret < 0) {
9396 			/*-
9397 			 * The count was off.. retran is not happening so do
9398 			 * the normal retransmission.
9399 			 */
9400 #ifdef SCTP_AUDITING_ENABLED
9401 			sctp_auditing(9, inp, stcb, NULL);
9402 #endif
9403 			if (ret == SCTP_RETRAN_EXIT) {
9404 				return;
9405 			}
9406 			break;
9407 		}
9408 		if (from_where == SCTP_OUTPUT_FROM_T3) {
9409 			/* Only one transmission allowed out of a timeout */
9410 #ifdef SCTP_AUDITING_ENABLED
9411 			sctp_auditing(10, inp, stcb, NULL);
9412 #endif
9413 			/* Push out any control */
9414 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9415 			    &now, &now_filled, frag_point, so_locked);
9416 			return;
9417 		}
9418 		if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
9419 			/* Hit FR burst limit */
9420 			return;
9421 		}
9422 		if ((num_out == 0) && (ret == 0)) {
9423 			/* No more retrans to send */
9424 			break;
9425 		}
9426 	}
9427 #ifdef SCTP_AUDITING_ENABLED
9428 	sctp_auditing(12, inp, stcb, NULL);
9429 #endif
9430 	/* Check for bad destinations, if they exist move chunks around. */
9431 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9432 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9433 		    SCTP_ADDR_NOT_REACHABLE) {
9434 			/*-
9435 			 * if possible move things off of this address we
9436 			 * still may send below due to the dormant state but
9437 			 * we try to find an alternate address to send to
9438 			 * and if we have one we move all queued data on the
9439 			 * out wheel to this alternate address.
9440 			 */
9441 			if (net->ref_count > 1)
9442 				sctp_move_chunks_from_net(stcb, net);
9443 		} else if ((asoc->sctp_cmt_on_off > 0) &&
9444 			    (asoc->sctp_cmt_pf > 0) &&
9445 		    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9446 			/*
9447 			 * JRS 5/14/07 - If CMT PF is on and the current
9448 			 * destination is in PF state, move all queued data
9449 			 * to an alternate desination.
9450 			 */
9451 			if (net->ref_count > 1)
9452 				sctp_move_chunks_from_net(stcb, net);
9453 		} else {
9454 			/*-
9455 			 * if ((asoc->sat_network) || (net->addr_is_local))
9456 			 * { burst_limit = asoc->max_burst *
9457 			 * SCTP_SAT_NETWORK_BURST_INCR; }
9458 			 */
9459 			if (asoc->max_burst > 0) {
9460 				if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9461 					if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
9462 						/*
9463 						 * JRS - Use the congestion
9464 						 * control given in the
9465 						 * congestion control module
9466 						 */
9467 						asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
9468 						if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9469 							sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
9470 						}
9471 						SCTP_STAT_INCR(sctps_maxburstqueued);
9472 					}
9473 					net->fast_retran_ip = 0;
9474 				} else {
9475 					if (net->flight_size == 0) {
9476 						/*
9477 						 * Should be decaying the
9478 						 * cwnd here
9479 						 */
9480 						;
9481 					}
9482 				}
9483 			}
9484 		}
9485 
9486 	}
9487 	burst_cnt = 0;
9488 	do {
9489 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9490 		    &reason_code, 0, from_where,
9491 		    &now, &now_filled, frag_point, so_locked);
9492 		if (error) {
9493 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9494 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9495 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9496 			}
9497 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9498 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9499 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9500 			}
9501 			break;
9502 		}
9503 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9504 
9505 		tot_out += num_out;
9506 		burst_cnt++;
9507 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9508 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9509 			if (num_out == 0) {
9510 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9511 			}
9512 		}
9513 		if (nagle_on) {
9514 			/*-
9515 			 * When nagle is on, we look at how much is un_sent, then
9516 			 * if its smaller than an MTU and we have data in
9517 			 * flight we stop.
9518 			 */
9519 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9520 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9521 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9522 			    (stcb->asoc.total_flight > 0)) {
9523 				break;
9524 			}
9525 		}
9526 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9527 		    TAILQ_EMPTY(&asoc->send_queue) &&
9528 		    stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
9529 			/* Nothing left to send */
9530 			break;
9531 		}
9532 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9533 			/* Nothing left to send */
9534 			break;
9535 		}
9536 	} while (num_out &&
9537 	    ((asoc->max_burst == 0) ||
9538 	    SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9539 	    (burst_cnt < asoc->max_burst)));
9540 
9541 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9542 		if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
9543 			SCTP_STAT_INCR(sctps_maxburstqueued);
9544 			asoc->burst_limit_applied = 1;
9545 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9546 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9547 			}
9548 		} else {
9549 			asoc->burst_limit_applied = 0;
9550 		}
9551 	}
9552 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9553 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9554 	}
9555 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9556 	    tot_out);
9557 
9558 	/*-
9559 	 * Now we need to clean up the control chunk chain if a ECNE is on
9560 	 * it. It must be marked as UNSENT again so next call will continue
9561 	 * to send it until such time that we get a CWR, to remove it.
9562 	 */
9563 	if (stcb->asoc.ecn_echo_cnt_onq)
9564 		sctp_fix_ecn_echo(asoc);
9565 	return;
9566 }
9567 
9568 
9569 int
9570 sctp_output(inp, m, addr, control, p, flags)
9571 	struct sctp_inpcb *inp;
9572 	struct mbuf *m;
9573 	struct sockaddr *addr;
9574 	struct mbuf *control;
9575 	struct thread *p;
9576 	int flags;
9577 {
9578 	if (inp == NULL) {
9579 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9580 		return (EINVAL);
9581 	}
9582 	if (inp->sctp_socket == NULL) {
9583 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9584 		return (EINVAL);
9585 	}
9586 	return (sctp_sosend(inp->sctp_socket,
9587 	    addr,
9588 	    (struct uio *)NULL,
9589 	    m,
9590 	    control,
9591 	    flags, p
9592 	    ));
9593 }
9594 
9595 void
9596 send_forward_tsn(struct sctp_tcb *stcb,
9597     struct sctp_association *asoc)
9598 {
9599 	struct sctp_tmit_chunk *chk;
9600 	struct sctp_forward_tsn_chunk *fwdtsn;
9601 	uint32_t advance_peer_ack_point;
9602 
9603 	SCTP_TCB_LOCK_ASSERT(stcb);
9604 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9605 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9606 			/* mark it to unsent */
9607 			chk->sent = SCTP_DATAGRAM_UNSENT;
9608 			chk->snd_count = 0;
9609 			/* Do we correct its output location? */
9610 			if (chk->whoTo != asoc->primary_destination) {
9611 				sctp_free_remote_addr(chk->whoTo);
9612 				chk->whoTo = asoc->primary_destination;
9613 				atomic_add_int(&chk->whoTo->ref_count, 1);
9614 			}
9615 			goto sctp_fill_in_rest;
9616 		}
9617 	}
9618 	/* Ok if we reach here we must build one */
9619 	sctp_alloc_a_chunk(stcb, chk);
9620 	if (chk == NULL) {
9621 		return;
9622 	}
9623 	asoc->fwd_tsn_cnt++;
9624 	chk->copy_by_ref = 0;
9625 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9626 	chk->rec.chunk_id.can_take_data = 0;
9627 	chk->asoc = asoc;
9628 	chk->whoTo = NULL;
9629 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9630 	if (chk->data == NULL) {
9631 		sctp_free_a_chunk(stcb, chk);
9632 		return;
9633 	}
9634 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9635 	chk->sent = SCTP_DATAGRAM_UNSENT;
9636 	chk->snd_count = 0;
9637 	chk->whoTo = asoc->primary_destination;
9638 	atomic_add_int(&chk->whoTo->ref_count, 1);
9639 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9640 	asoc->ctrl_queue_cnt++;
9641 sctp_fill_in_rest:
9642 	/*-
9643 	 * Here we go through and fill out the part that deals with
9644 	 * stream/seq of the ones we skip.
9645 	 */
9646 	SCTP_BUF_LEN(chk->data) = 0;
9647 	{
9648 		struct sctp_tmit_chunk *at, *tp1, *last;
9649 		struct sctp_strseq *strseq;
9650 		unsigned int cnt_of_space, i, ovh;
9651 		unsigned int space_needed;
9652 		unsigned int cnt_of_skipped = 0;
9653 
9654 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9655 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9656 				/* no more to look at */
9657 				break;
9658 			}
9659 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9660 				/* We don't report these */
9661 				continue;
9662 			}
9663 			cnt_of_skipped++;
9664 		}
9665 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9666 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
9667 
9668 		cnt_of_space = M_TRAILINGSPACE(chk->data);
9669 
9670 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9671 			ovh = SCTP_MIN_OVERHEAD;
9672 		} else {
9673 			ovh = SCTP_MIN_V4_OVERHEAD;
9674 		}
9675 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9676 			/* trim to a mtu size */
9677 			cnt_of_space = asoc->smallest_mtu - ovh;
9678 		}
9679 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9680 			sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9681 			    0xff, 0, cnt_of_skipped,
9682 			    asoc->advanced_peer_ack_point);
9683 
9684 		}
9685 		advance_peer_ack_point = asoc->advanced_peer_ack_point;
9686 		if (cnt_of_space < space_needed) {
9687 			/*-
9688 			 * ok we must trim down the chunk by lowering the
9689 			 * advance peer ack point.
9690 			 */
9691 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9692 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9693 				    0xff, 0xff, cnt_of_space,
9694 				    space_needed);
9695 			}
9696 			cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
9697 			cnt_of_skipped /= sizeof(struct sctp_strseq);
9698 			/*-
9699 			 * Go through and find the TSN that will be the one
9700 			 * we report.
9701 			 */
9702 			at = TAILQ_FIRST(&asoc->sent_queue);
9703 			for (i = 0; i < cnt_of_skipped; i++) {
9704 				tp1 = TAILQ_NEXT(at, sctp_next);
9705 				if (tp1 == NULL) {
9706 					break;
9707 				}
9708 				at = tp1;
9709 			}
9710 			if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9711 				sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9712 				    0xff, cnt_of_skipped, at->rec.data.TSN_seq,
9713 				    asoc->advanced_peer_ack_point);
9714 			}
9715 			last = at;
9716 			/*-
9717 			 * last now points to last one I can report, update
9718 			 * peer ack point
9719 			 */
9720 			if (last)
9721 				advance_peer_ack_point = last->rec.data.TSN_seq;
9722 			space_needed = sizeof(struct sctp_forward_tsn_chunk) +
9723 			    cnt_of_skipped * sizeof(struct sctp_strseq);
9724 		}
9725 		chk->send_size = space_needed;
9726 		/* Setup the chunk */
9727 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9728 		fwdtsn->ch.chunk_length = htons(chk->send_size);
9729 		fwdtsn->ch.chunk_flags = 0;
9730 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9731 		fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
9732 		SCTP_BUF_LEN(chk->data) = chk->send_size;
9733 		fwdtsn++;
9734 		/*-
9735 		 * Move pointer to after the fwdtsn and transfer to the
9736 		 * strseq pointer.
9737 		 */
9738 		strseq = (struct sctp_strseq *)fwdtsn;
9739 		/*-
9740 		 * Now populate the strseq list. This is done blindly
9741 		 * without pulling out duplicate stream info. This is
9742 		 * inefficent but won't harm the process since the peer will
9743 		 * look at these in sequence and will thus release anything.
9744 		 * It could mean we exceed the PMTU and chop off some that
9745 		 * we could have included.. but this is unlikely (aka 1432/4
9746 		 * would mean 300+ stream seq's would have to be reported in
9747 		 * one FWD-TSN. With a bit of work we can later FIX this to
9748 		 * optimize and pull out duplcates.. but it does add more
9749 		 * overhead. So for now... not!
9750 		 */
9751 		at = TAILQ_FIRST(&asoc->sent_queue);
9752 		for (i = 0; i < cnt_of_skipped; i++) {
9753 			tp1 = TAILQ_NEXT(at, sctp_next);
9754 			if (tp1 == NULL)
9755 				break;
9756 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9757 				/* We don't report these */
9758 				i--;
9759 				at = tp1;
9760 				continue;
9761 			}
9762 			if (at->rec.data.TSN_seq == advance_peer_ack_point) {
9763 				at->rec.data.fwd_tsn_cnt = 0;
9764 			}
9765 			strseq->stream = ntohs(at->rec.data.stream_number);
9766 			strseq->sequence = ntohs(at->rec.data.stream_seq);
9767 			strseq++;
9768 			at = tp1;
9769 		}
9770 	}
9771 	return;
9772 
9773 }
9774 
9775 void
9776 sctp_send_sack(struct sctp_tcb *stcb)
9777 {
9778 	/*-
9779 	 * Queue up a SACK or NR-SACK in the control queue.
9780 	 * We must first check to see if a SACK or NR-SACK is
9781 	 * somehow on the control queue.
9782 	 * If so, we will take and and remove the old one.
9783 	 */
9784 	struct sctp_association *asoc;
9785 	struct sctp_tmit_chunk *chk, *a_chk;
9786 	struct sctp_sack_chunk *sack;
9787 	struct sctp_nr_sack_chunk *nr_sack;
9788 	struct sctp_gap_ack_block *gap_descriptor;
9789 	struct sack_track *selector;
9790 	int mergeable = 0;
9791 	int offset;
9792 	caddr_t limit;
9793 	uint32_t *dup;
9794 	int limit_reached = 0;
9795 	unsigned int i, siz, j;
9796 	unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
9797 	int num_dups = 0;
9798 	int space_req;
9799 	uint32_t highest_tsn;
9800 	uint8_t flags;
9801 	uint8_t type;
9802 	uint8_t tsn_map;
9803 
9804 	if ((stcb->asoc.sctp_nr_sack_on_off == 1) &&
9805 	    (stcb->asoc.peer_supports_nr_sack == 1)) {
9806 		type = SCTP_NR_SELECTIVE_ACK;
9807 	} else {
9808 		type = SCTP_SELECTIVE_ACK;
9809 	}
9810 	a_chk = NULL;
9811 	asoc = &stcb->asoc;
9812 	SCTP_TCB_LOCK_ASSERT(stcb);
9813 	if (asoc->last_data_chunk_from == NULL) {
9814 		/* Hmm we never received anything */
9815 		return;
9816 	}
9817 	sctp_slide_mapping_arrays(stcb);
9818 	sctp_set_rwnd(stcb, asoc);
9819 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9820 		if (chk->rec.chunk_id.id == type) {
9821 			/* Hmm, found a sack already on queue, remove it */
9822 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9823 			asoc->ctrl_queue_cnt--;
9824 			a_chk = chk;
9825 			if (a_chk->data) {
9826 				sctp_m_freem(a_chk->data);
9827 				a_chk->data = NULL;
9828 			}
9829 			sctp_free_remote_addr(a_chk->whoTo);
9830 			a_chk->whoTo = NULL;
9831 			break;
9832 		}
9833 	}
9834 	if (a_chk == NULL) {
9835 		sctp_alloc_a_chunk(stcb, a_chk);
9836 		if (a_chk == NULL) {
9837 			/* No memory so we drop the idea, and set a timer */
9838 			if (stcb->asoc.delayed_ack) {
9839 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9840 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9841 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9842 				    stcb->sctp_ep, stcb, NULL);
9843 			} else {
9844 				stcb->asoc.send_sack = 1;
9845 			}
9846 			return;
9847 		}
9848 		a_chk->copy_by_ref = 0;
9849 		a_chk->rec.chunk_id.id = type;
9850 		a_chk->rec.chunk_id.can_take_data = 1;
9851 	}
9852 	/* Clear our pkt counts */
9853 	asoc->data_pkts_seen = 0;
9854 
9855 	a_chk->asoc = asoc;
9856 	a_chk->snd_count = 0;
9857 	a_chk->send_size = 0;	/* fill in later */
9858 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
9859 	a_chk->whoTo = NULL;
9860 
9861 	if ((asoc->numduptsns) ||
9862 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)) {
9863 		/*-
9864 		 * Ok, we have some duplicates or the destination for the
9865 		 * sack is unreachable, lets see if we can select an
9866 		 * alternate than asoc->last_data_chunk_from
9867 		 */
9868 		if ((!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)) &&
9869 		    (asoc->used_alt_onsack > asoc->numnets)) {
9870 			/* We used an alt last time, don't this time */
9871 			a_chk->whoTo = NULL;
9872 		} else {
9873 			asoc->used_alt_onsack++;
9874 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9875 		}
9876 		if (a_chk->whoTo == NULL) {
9877 			/* Nope, no alternate */
9878 			a_chk->whoTo = asoc->last_data_chunk_from;
9879 			asoc->used_alt_onsack = 0;
9880 		}
9881 	} else {
9882 		/*
9883 		 * No duplicates so we use the last place we received data
9884 		 * from.
9885 		 */
9886 		asoc->used_alt_onsack = 0;
9887 		a_chk->whoTo = asoc->last_data_chunk_from;
9888 	}
9889 	if (a_chk->whoTo) {
9890 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
9891 	}
9892 	if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
9893 		highest_tsn = asoc->highest_tsn_inside_map;
9894 	} else {
9895 		highest_tsn = asoc->highest_tsn_inside_nr_map;
9896 	}
9897 	if (highest_tsn == asoc->cumulative_tsn) {
9898 		/* no gaps */
9899 		if (type == SCTP_SELECTIVE_ACK) {
9900 			space_req = sizeof(struct sctp_sack_chunk);
9901 		} else {
9902 			space_req = sizeof(struct sctp_nr_sack_chunk);
9903 		}
9904 	} else {
9905 		/* gaps get a cluster */
9906 		space_req = MCLBYTES;
9907 	}
9908 	/* Ok now lets formulate a MBUF with our sack */
9909 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
9910 	if ((a_chk->data == NULL) ||
9911 	    (a_chk->whoTo == NULL)) {
9912 		/* rats, no mbuf memory */
9913 		if (a_chk->data) {
9914 			/* was a problem with the destination */
9915 			sctp_m_freem(a_chk->data);
9916 			a_chk->data = NULL;
9917 		}
9918 		sctp_free_a_chunk(stcb, a_chk);
9919 		/* sa_ignore NO_NULL_CHK */
9920 		if (stcb->asoc.delayed_ack) {
9921 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9922 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
9923 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9924 			    stcb->sctp_ep, stcb, NULL);
9925 		} else {
9926 			stcb->asoc.send_sack = 1;
9927 		}
9928 		return;
9929 	}
9930 	/* ok, lets go through and fill it in */
9931 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
9932 	space = M_TRAILINGSPACE(a_chk->data);
9933 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
9934 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
9935 	}
9936 	limit = mtod(a_chk->data, caddr_t);
9937 	limit += space;
9938 
9939 	flags = 0;
9940 
9941 	if ((asoc->sctp_cmt_on_off > 0) &&
9942 	    SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
9943 		/*-
9944 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
9945 		 * received, then set high bit to 1, else 0. Reset
9946 		 * pkts_rcvd.
9947 		 */
9948 		flags |= (asoc->cmt_dac_pkts_rcvd << 6);
9949 		asoc->cmt_dac_pkts_rcvd = 0;
9950 	}
9951 #ifdef SCTP_ASOCLOG_OF_TSNS
9952 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
9953 	stcb->asoc.cumack_log_atsnt++;
9954 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
9955 		stcb->asoc.cumack_log_atsnt = 0;
9956 	}
9957 #endif
9958 	/* reset the readers interpretation */
9959 	stcb->freed_by_sorcv_sincelast = 0;
9960 
9961 	if (type == SCTP_SELECTIVE_ACK) {
9962 		sack = mtod(a_chk->data, struct sctp_sack_chunk *);
9963 		nr_sack = NULL;
9964 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
9965 		if (highest_tsn > asoc->mapping_array_base_tsn) {
9966 			siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
9967 		} else {
9968 			siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
9969 		}
9970 	} else {
9971 		sack = NULL;
9972 		nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
9973 		gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
9974 		if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
9975 			siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
9976 		} else {
9977 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
9978 		}
9979 	}
9980 
9981 	if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
9982 		offset = 1;
9983 	} else {
9984 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
9985 	}
9986 	if (((type == SCTP_SELECTIVE_ACK) &&
9987 	    SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
9988 	    ((type == SCTP_NR_SELECTIVE_ACK) &&
9989 	    SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
9990 		/* we have a gap .. maybe */
9991 		for (i = 0; i < siz; i++) {
9992 			tsn_map = asoc->mapping_array[i];
9993 			if (type == SCTP_SELECTIVE_ACK) {
9994 				tsn_map |= asoc->nr_mapping_array[i];
9995 			}
9996 			if (i == 0) {
9997 				/*
9998 				 * Clear all bits corresponding to TSNs
9999 				 * smaller or equal to the cumulative TSN.
10000 				 */
10001 				tsn_map &= (~0 << (1 - offset));
10002 			}
10003 			selector = &sack_array[tsn_map];
10004 			if (mergeable && selector->right_edge) {
10005 				/*
10006 				 * Backup, left and right edges were ok to
10007 				 * merge.
10008 				 */
10009 				num_gap_blocks--;
10010 				gap_descriptor--;
10011 			}
10012 			if (selector->num_entries == 0)
10013 				mergeable = 0;
10014 			else {
10015 				for (j = 0; j < selector->num_entries; j++) {
10016 					if (mergeable && selector->right_edge) {
10017 						/*
10018 						 * do a merge by NOT setting
10019 						 * the left side
10020 						 */
10021 						mergeable = 0;
10022 					} else {
10023 						/*
10024 						 * no merge, set the left
10025 						 * side
10026 						 */
10027 						mergeable = 0;
10028 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
10029 					}
10030 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
10031 					num_gap_blocks++;
10032 					gap_descriptor++;
10033 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10034 						/* no more room */
10035 						limit_reached = 1;
10036 						break;
10037 					}
10038 				}
10039 				if (selector->left_edge) {
10040 					mergeable = 1;
10041 				}
10042 			}
10043 			if (limit_reached) {
10044 				/* Reached the limit stop */
10045 				break;
10046 			}
10047 			offset += 8;
10048 		}
10049 	}
10050 	if ((type == SCTP_NR_SELECTIVE_ACK) &&
10051 	    (limit_reached == 0)) {
10052 
10053 		mergeable = 0;
10054 
10055 		if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10056 			siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10057 		} else {
10058 			siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10059 		}
10060 
10061 		if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10062 			offset = 1;
10063 		} else {
10064 			offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10065 		}
10066 		if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10067 			/* we have a gap .. maybe */
10068 			for (i = 0; i < siz; i++) {
10069 				tsn_map = asoc->nr_mapping_array[i];
10070 				if (i == 0) {
10071 					/*
10072 					 * Clear all bits corresponding to
10073 					 * TSNs smaller or equal to the
10074 					 * cumulative TSN.
10075 					 */
10076 					tsn_map &= (~0 << (1 - offset));
10077 				}
10078 				selector = &sack_array[tsn_map];
10079 				if (mergeable && selector->right_edge) {
10080 					/*
10081 					 * Backup, left and right edges were
10082 					 * ok to merge.
10083 					 */
10084 					num_nr_gap_blocks--;
10085 					gap_descriptor--;
10086 				}
10087 				if (selector->num_entries == 0)
10088 					mergeable = 0;
10089 				else {
10090 					for (j = 0; j < selector->num_entries; j++) {
10091 						if (mergeable && selector->right_edge) {
10092 							/*
10093 							 * do a merge by NOT
10094 							 * setting the left
10095 							 * side
10096 							 */
10097 							mergeable = 0;
10098 						} else {
10099 							/*
10100 							 * no merge, set the
10101 							 * left side
10102 							 */
10103 							mergeable = 0;
10104 							gap_descriptor->start = htons((selector->gaps[j].start + offset));
10105 						}
10106 						gap_descriptor->end = htons((selector->gaps[j].end + offset));
10107 						num_nr_gap_blocks++;
10108 						gap_descriptor++;
10109 						if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10110 							/* no more room */
10111 							limit_reached = 1;
10112 							break;
10113 						}
10114 					}
10115 					if (selector->left_edge) {
10116 						mergeable = 1;
10117 					}
10118 				}
10119 				if (limit_reached) {
10120 					/* Reached the limit stop */
10121 					break;
10122 				}
10123 				offset += 8;
10124 			}
10125 		}
10126 	}
10127 	/* now we must add any dups we are going to report. */
10128 	if ((limit_reached == 0) && (asoc->numduptsns)) {
10129 		dup = (uint32_t *) gap_descriptor;
10130 		for (i = 0; i < asoc->numduptsns; i++) {
10131 			*dup = htonl(asoc->dup_tsns[i]);
10132 			dup++;
10133 			num_dups++;
10134 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10135 				/* no more room */
10136 				break;
10137 			}
10138 		}
10139 		asoc->numduptsns = 0;
10140 	}
10141 	/*
10142 	 * now that the chunk is prepared queue it to the control chunk
10143 	 * queue.
10144 	 */
10145 	if (type == SCTP_SELECTIVE_ACK) {
10146 		a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10147 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10148 		    num_dups * sizeof(int32_t);
10149 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10150 		sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10151 		sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10152 		sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10153 		sack->sack.num_dup_tsns = htons(num_dups);
10154 		sack->ch.chunk_type = type;
10155 		sack->ch.chunk_flags = flags;
10156 		sack->ch.chunk_length = htons(a_chk->send_size);
10157 	} else {
10158 		a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10159 		    (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10160 		    num_dups * sizeof(int32_t);
10161 		SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10162 		nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10163 		nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10164 		nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10165 		nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10166 		nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10167 		nr_sack->nr_sack.reserved = 0;
10168 		nr_sack->ch.chunk_type = type;
10169 		nr_sack->ch.chunk_flags = flags;
10170 		nr_sack->ch.chunk_length = htons(a_chk->send_size);
10171 	}
10172 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10173 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
10174 	asoc->ctrl_queue_cnt++;
10175 	asoc->send_sack = 0;
10176 	SCTP_STAT_INCR(sctps_sendsacks);
10177 	return;
10178 }
10179 
10180 void
10181 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10182 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10183     SCTP_UNUSED
10184 #endif
10185 )
10186 {
10187 	struct mbuf *m_abort;
10188 	struct mbuf *m_out = NULL, *m_end = NULL;
10189 	struct sctp_abort_chunk *abort = NULL;
10190 	int sz;
10191 	uint32_t auth_offset = 0;
10192 	struct sctp_auth_chunk *auth = NULL;
10193 
10194 	/*-
10195 	 * Add an AUTH chunk, if chunk requires it and save the offset into
10196 	 * the chain for AUTH
10197 	 */
10198 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10199 	    stcb->asoc.peer_auth_chunks)) {
10200 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10201 		    stcb, SCTP_ABORT_ASSOCIATION);
10202 		SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10203 	}
10204 	SCTP_TCB_LOCK_ASSERT(stcb);
10205 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10206 	if (m_abort == NULL) {
10207 		/* no mbuf's */
10208 		if (m_out)
10209 			sctp_m_freem(m_out);
10210 		return;
10211 	}
10212 	/* link in any error */
10213 	SCTP_BUF_NEXT(m_abort) = operr;
10214 	sz = 0;
10215 	if (operr) {
10216 		struct mbuf *n;
10217 
10218 		n = operr;
10219 		while (n) {
10220 			sz += SCTP_BUF_LEN(n);
10221 			n = SCTP_BUF_NEXT(n);
10222 		}
10223 	}
10224 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10225 	if (m_out == NULL) {
10226 		/* NO Auth chunk prepended, so reserve space in front */
10227 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10228 		m_out = m_abort;
10229 	} else {
10230 		/* Put AUTH chunk at the front of the chain */
10231 		SCTP_BUF_NEXT(m_end) = m_abort;
10232 	}
10233 
10234 	/* fill in the ABORT chunk */
10235 	abort = mtod(m_abort, struct sctp_abort_chunk *);
10236 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10237 	abort->ch.chunk_flags = 0;
10238 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10239 
10240 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10241 	    stcb->asoc.primary_destination,
10242 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10243 	    m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10244 	    stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10245 	    stcb->asoc.primary_destination->port, so_locked, NULL);
10246 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10247 }
10248 
10249 void
10250 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10251     struct sctp_nets *net,
10252     int reflect_vtag)
10253 {
10254 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10255 	struct mbuf *m_shutdown_comp;
10256 	struct sctp_shutdown_complete_chunk *shutdown_complete;
10257 	uint32_t vtag;
10258 	uint8_t flags;
10259 
10260 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10261 	if (m_shutdown_comp == NULL) {
10262 		/* no mbuf's */
10263 		return;
10264 	}
10265 	if (reflect_vtag) {
10266 		flags = SCTP_HAD_NO_TCB;
10267 		vtag = stcb->asoc.my_vtag;
10268 	} else {
10269 		flags = 0;
10270 		vtag = stcb->asoc.peer_vtag;
10271 	}
10272 	shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10273 	shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10274 	shutdown_complete->ch.chunk_flags = flags;
10275 	shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10276 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10277 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10278 	    (struct sockaddr *)&net->ro._l_addr,
10279 	    m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10280 	    stcb->sctp_ep->sctp_lport, stcb->rport,
10281 	    htonl(vtag),
10282 	    net->port, SCTP_SO_NOT_LOCKED, NULL);
10283 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10284 	return;
10285 }
10286 
10287 void
10288 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10289     uint32_t vrf_id, uint16_t port)
10290 {
10291 	/* formulate and SEND a SHUTDOWN-COMPLETE */
10292 	struct mbuf *o_pak;
10293 	struct mbuf *mout;
10294 	struct ip *iph, *iph_out;
10295 	struct udphdr *udp = NULL;
10296 
10297 #ifdef INET6
10298 	struct ip6_hdr *ip6, *ip6_out;
10299 
10300 #endif
10301 	int offset_out, len, mlen;
10302 	struct sctp_shutdown_complete_msg *comp_cp;
10303 
10304 	iph = mtod(m, struct ip *);
10305 	switch (iph->ip_v) {
10306 	case IPVERSION:
10307 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10308 		break;
10309 #ifdef INET6
10310 	case IPV6_VERSION >> 4:
10311 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10312 		break;
10313 #endif
10314 	default:
10315 		return;
10316 	}
10317 	if (port) {
10318 		len += sizeof(struct udphdr);
10319 	}
10320 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10321 	if (mout == NULL) {
10322 		return;
10323 	}
10324 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
10325 	SCTP_BUF_LEN(mout) = len;
10326 	SCTP_BUF_NEXT(mout) = NULL;
10327 	iph_out = NULL;
10328 #ifdef INET6
10329 	ip6_out = NULL;
10330 #endif
10331 	offset_out = 0;
10332 
10333 	switch (iph->ip_v) {
10334 	case IPVERSION:
10335 		iph_out = mtod(mout, struct ip *);
10336 
10337 		/* Fill in the IP header for the ABORT */
10338 		iph_out->ip_v = IPVERSION;
10339 		iph_out->ip_hl = (sizeof(struct ip) / 4);
10340 		iph_out->ip_tos = (u_char)0;
10341 		iph_out->ip_id = 0;
10342 		iph_out->ip_off = 0;
10343 		iph_out->ip_ttl = MAXTTL;
10344 		if (port) {
10345 			iph_out->ip_p = IPPROTO_UDP;
10346 		} else {
10347 			iph_out->ip_p = IPPROTO_SCTP;
10348 		}
10349 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10350 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10351 
10352 		/* let IP layer calculate this */
10353 		iph_out->ip_sum = 0;
10354 		offset_out += sizeof(*iph_out);
10355 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10356 		    (caddr_t)iph_out + offset_out);
10357 		break;
10358 #ifdef INET6
10359 	case IPV6_VERSION >> 4:
10360 		ip6 = (struct ip6_hdr *)iph;
10361 		ip6_out = mtod(mout, struct ip6_hdr *);
10362 
10363 		/* Fill in the IPv6 header for the ABORT */
10364 		ip6_out->ip6_flow = ip6->ip6_flow;
10365 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10366 		if (port) {
10367 			ip6_out->ip6_nxt = IPPROTO_UDP;
10368 		} else {
10369 			ip6_out->ip6_nxt = IPPROTO_SCTP;
10370 		}
10371 		ip6_out->ip6_src = ip6->ip6_dst;
10372 		ip6_out->ip6_dst = ip6->ip6_src;
10373 		/*
10374 		 * ?? The old code had both the iph len + payload, I think
10375 		 * this is wrong and would never have worked
10376 		 */
10377 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10378 		offset_out += sizeof(*ip6_out);
10379 		comp_cp = (struct sctp_shutdown_complete_msg *)(
10380 		    (caddr_t)ip6_out + offset_out);
10381 		break;
10382 #endif				/* INET6 */
10383 	default:
10384 		/* Currently not supported. */
10385 		sctp_m_freem(mout);
10386 		return;
10387 	}
10388 	if (port) {
10389 		udp = (struct udphdr *)comp_cp;
10390 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10391 		udp->uh_dport = port;
10392 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10393 		if (iph_out)
10394 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10395 		offset_out += sizeof(struct udphdr);
10396 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10397 	}
10398 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10399 		/* no mbuf's */
10400 		sctp_m_freem(mout);
10401 		return;
10402 	}
10403 	/* Now copy in and fill in the ABORT tags etc. */
10404 	comp_cp->sh.src_port = sh->dest_port;
10405 	comp_cp->sh.dest_port = sh->src_port;
10406 	comp_cp->sh.checksum = 0;
10407 	comp_cp->sh.v_tag = sh->v_tag;
10408 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10409 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10410 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10411 
10412 	if (iph_out != NULL) {
10413 		sctp_route_t ro;
10414 		int ret;
10415 
10416 		mlen = SCTP_BUF_LEN(mout);
10417 		bzero(&ro, sizeof ro);
10418 		/* set IPv4 length */
10419 		iph_out->ip_len = mlen;
10420 #ifdef  SCTP_PACKET_LOGGING
10421 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10422 			sctp_packet_log(mout, mlen);
10423 #endif
10424 		if (port) {
10425 #if defined(SCTP_WITH_NO_CSUM)
10426 			SCTP_STAT_INCR(sctps_sendnocrc);
10427 #else
10428 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10429 			SCTP_STAT_INCR(sctps_sendswcrc);
10430 #endif
10431 			SCTP_ENABLE_UDP_CSUM(mout);
10432 		} else {
10433 #if defined(SCTP_WITH_NO_CSUM)
10434 			SCTP_STAT_INCR(sctps_sendnocrc);
10435 #else
10436 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
10437 			mout->m_pkthdr.csum_data = 0;
10438 			SCTP_STAT_INCR(sctps_sendhwcrc);
10439 #endif
10440 		}
10441 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10442 		/* out it goes */
10443 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
10444 
10445 		/* Free the route if we got one back */
10446 		if (ro.ro_rt)
10447 			RTFREE(ro.ro_rt);
10448 	}
10449 #ifdef INET6
10450 	if (ip6_out != NULL) {
10451 		struct route_in6 ro;
10452 		int ret;
10453 		struct ifnet *ifp = NULL;
10454 
10455 		bzero(&ro, sizeof(ro));
10456 		mlen = SCTP_BUF_LEN(mout);
10457 #ifdef  SCTP_PACKET_LOGGING
10458 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10459 			sctp_packet_log(mout, mlen);
10460 #endif
10461 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10462 		if (port) {
10463 #if defined(SCTP_WITH_NO_CSUM)
10464 			SCTP_STAT_INCR(sctps_sendnocrc);
10465 #else
10466 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
10467 			SCTP_STAT_INCR(sctps_sendswcrc);
10468 #endif
10469 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
10470 				udp->uh_sum = 0xffff;
10471 			}
10472 		} else {
10473 #if defined(SCTP_WITH_NO_CSUM)
10474 			SCTP_STAT_INCR(sctps_sendnocrc);
10475 #else
10476 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
10477 			mout->m_pkthdr.csum_data = 0;
10478 			SCTP_STAT_INCR(sctps_sendhwcrc);
10479 #endif
10480 		}
10481 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
10482 
10483 		/* Free the route if we got one back */
10484 		if (ro.ro_rt)
10485 			RTFREE(ro.ro_rt);
10486 	}
10487 #endif
10488 	SCTP_STAT_INCR(sctps_sendpackets);
10489 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10490 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10491 	return;
10492 
10493 }
10494 
10495 static struct sctp_nets *
10496 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10497 {
10498 	struct sctp_nets *net, *hnet;
10499 	int ms_goneby, highest_ms, state_overide = 0;
10500 
10501 	(void)SCTP_GETTIME_TIMEVAL(now);
10502 	highest_ms = 0;
10503 	hnet = NULL;
10504 	SCTP_TCB_LOCK_ASSERT(stcb);
10505 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10506 		if (
10507 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10508 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10509 		    ) {
10510 			/*
10511 			 * Skip this guy from consideration if HB is off AND
10512 			 * its confirmed
10513 			 */
10514 			continue;
10515 		}
10516 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10517 			/* skip this dest net from consideration */
10518 			continue;
10519 		}
10520 		if (net->last_sent_time.tv_sec) {
10521 			/* Sent to so we subtract */
10522 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10523 		} else
10524 			/* Never been sent to */
10525 			ms_goneby = 0x7fffffff;
10526 		/*-
10527 		 * When the address state is unconfirmed but still
10528 		 * considered reachable, we HB at a higher rate. Once it
10529 		 * goes confirmed OR reaches the "unreachable" state, thenw
10530 		 * we cut it back to HB at a more normal pace.
10531 		 */
10532 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10533 			state_overide = 1;
10534 		} else {
10535 			state_overide = 0;
10536 		}
10537 
10538 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10539 		    (ms_goneby > highest_ms)) {
10540 			highest_ms = ms_goneby;
10541 			hnet = net;
10542 		}
10543 	}
10544 	if (hnet &&
10545 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10546 		state_overide = 1;
10547 	} else {
10548 		state_overide = 0;
10549 	}
10550 
10551 	if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10552 		/*-
10553 		 * Found the one with longest delay bounds OR it is
10554 		 * unconfirmed and still not marked unreachable.
10555 		 */
10556 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10557 #ifdef SCTP_DEBUG
10558 		if (hnet) {
10559 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10560 			    (struct sockaddr *)&hnet->ro._l_addr);
10561 		} else {
10562 			SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10563 		}
10564 #endif
10565 		/* update the timer now */
10566 		hnet->last_sent_time = *now;
10567 		return (hnet);
10568 	}
10569 	/* Nothing to HB */
10570 	return (NULL);
10571 }
10572 
10573 int
10574 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10575 {
10576 	struct sctp_tmit_chunk *chk;
10577 	struct sctp_nets *net;
10578 	struct sctp_heartbeat_chunk *hb;
10579 	struct timeval now;
10580 	struct sockaddr_in *sin;
10581 	struct sockaddr_in6 *sin6;
10582 
10583 	SCTP_TCB_LOCK_ASSERT(stcb);
10584 	if (user_req == 0) {
10585 		net = sctp_select_hb_destination(stcb, &now);
10586 		if (net == NULL) {
10587 			/*-
10588 			 * All our busy none to send to, just start the
10589 			 * timer again.
10590 			 */
10591 			if (stcb->asoc.state == 0) {
10592 				return (0);
10593 			}
10594 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10595 			    stcb->sctp_ep,
10596 			    stcb,
10597 			    net);
10598 			return (0);
10599 		}
10600 	} else {
10601 		net = u_net;
10602 		if (net == NULL) {
10603 			return (0);
10604 		}
10605 		(void)SCTP_GETTIME_TIMEVAL(&now);
10606 	}
10607 	sin = (struct sockaddr_in *)&net->ro._l_addr;
10608 	if (sin->sin_family != AF_INET) {
10609 		if (sin->sin_family != AF_INET6) {
10610 			/* huh */
10611 			return (0);
10612 		}
10613 	}
10614 	sctp_alloc_a_chunk(stcb, chk);
10615 	if (chk == NULL) {
10616 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10617 		return (0);
10618 	}
10619 	chk->copy_by_ref = 0;
10620 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10621 	chk->rec.chunk_id.can_take_data = 1;
10622 	chk->asoc = &stcb->asoc;
10623 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10624 
10625 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10626 	if (chk->data == NULL) {
10627 		sctp_free_a_chunk(stcb, chk);
10628 		return (0);
10629 	}
10630 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10631 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10632 	chk->sent = SCTP_DATAGRAM_UNSENT;
10633 	chk->snd_count = 0;
10634 	chk->whoTo = net;
10635 	atomic_add_int(&chk->whoTo->ref_count, 1);
10636 	/* Now we have a mbuf that we can fill in with the details */
10637 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10638 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10639 	/* fill out chunk header */
10640 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10641 	hb->ch.chunk_flags = 0;
10642 	hb->ch.chunk_length = htons(chk->send_size);
10643 	/* Fill out hb parameter */
10644 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10645 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10646 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10647 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10648 	/* Did our user request this one, put it in */
10649 	hb->heartbeat.hb_info.user_req = user_req;
10650 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
10651 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
10652 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10653 		/*
10654 		 * we only take from the entropy pool if the address is not
10655 		 * confirmed.
10656 		 */
10657 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10658 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10659 	} else {
10660 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10661 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10662 	}
10663 	if (sin->sin_family == AF_INET) {
10664 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10665 	} else if (sin->sin_family == AF_INET6) {
10666 		/* We leave the scope the way it is in our lookup table. */
10667 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10668 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10669 	} else {
10670 		/* huh compiler bug */
10671 		return (0);
10672 	}
10673 
10674 	/*
10675 	 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10676 	 * PF-heartbeats.  Because of this, threshold management is done by
10677 	 * the t3 timer handler, and does not need to be done upon the send
10678 	 * of a PF-heartbeat. If CMT PF is on and the destination to which a
10679 	 * heartbeat is being sent is in PF state, do NOT do threshold
10680 	 * management.
10681 	 */
10682 	if ((stcb->asoc.sctp_cmt_pf == 0) ||
10683 	    ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10684 		/* ok we have a destination that needs a beat */
10685 		/* lets do the theshold management Qiaobing style */
10686 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10687 		    stcb->asoc.max_send_times)) {
10688 			/*-
10689 			 * we have lost the association, in a way this is
10690 			 * quite bad since we really are one less time since
10691 			 * we really did not send yet. This is the down side
10692 			 * to the Q's style as defined in the RFC and not my
10693 			 * alternate style defined in the RFC.
10694 			 */
10695 			if (chk->data != NULL) {
10696 				sctp_m_freem(chk->data);
10697 				chk->data = NULL;
10698 			}
10699 			/*
10700 			 * Here we do NOT use the macro since the
10701 			 * association is now gone.
10702 			 */
10703 			if (chk->whoTo) {
10704 				sctp_free_remote_addr(chk->whoTo);
10705 				chk->whoTo = NULL;
10706 			}
10707 			sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10708 			return (-1);
10709 		}
10710 	}
10711 	net->hb_responded = 0;
10712 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10713 	stcb->asoc.ctrl_queue_cnt++;
10714 	SCTP_STAT_INCR(sctps_sendheartbeat);
10715 	/*-
10716 	 * Call directly med level routine to put out the chunk. It will
10717 	 * always tumble out control chunks aka HB but it may even tumble
10718 	 * out data too.
10719 	 */
10720 	return (1);
10721 }
10722 
10723 void
10724 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10725     uint32_t high_tsn)
10726 {
10727 	struct sctp_association *asoc;
10728 	struct sctp_ecne_chunk *ecne;
10729 	struct sctp_tmit_chunk *chk;
10730 
10731 	asoc = &stcb->asoc;
10732 	SCTP_TCB_LOCK_ASSERT(stcb);
10733 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10734 		if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
10735 			/* found a previous ECN_ECHO update it if needed */
10736 			uint32_t cnt, ctsn;
10737 
10738 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10739 			ctsn = ntohl(ecne->tsn);
10740 			if (SCTP_TSN_GT(high_tsn, ctsn)) {
10741 				ecne->tsn = htonl(high_tsn);
10742 				SCTP_STAT_INCR(sctps_queue_upd_ecne);
10743 			}
10744 			cnt = ntohl(ecne->num_pkts_since_cwr);
10745 			cnt++;
10746 			ecne->num_pkts_since_cwr = htonl(cnt);
10747 			return;
10748 		}
10749 	}
10750 	/* nope could not find one to update so we must build one */
10751 	sctp_alloc_a_chunk(stcb, chk);
10752 	if (chk == NULL) {
10753 		return;
10754 	}
10755 	chk->copy_by_ref = 0;
10756 	SCTP_STAT_INCR(sctps_queue_upd_ecne);
10757 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10758 	chk->rec.chunk_id.can_take_data = 0;
10759 	chk->asoc = &stcb->asoc;
10760 	chk->send_size = sizeof(struct sctp_ecne_chunk);
10761 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10762 	if (chk->data == NULL) {
10763 		sctp_free_a_chunk(stcb, chk);
10764 		return;
10765 	}
10766 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10767 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10768 	chk->sent = SCTP_DATAGRAM_UNSENT;
10769 	chk->snd_count = 0;
10770 	chk->whoTo = net;
10771 	atomic_add_int(&chk->whoTo->ref_count, 1);
10772 	stcb->asoc.ecn_echo_cnt_onq++;
10773 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10774 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
10775 	ecne->ch.chunk_flags = 0;
10776 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10777 	ecne->tsn = htonl(high_tsn);
10778 	ecne->num_pkts_since_cwr = htonl(1);
10779 	TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
10780 	asoc->ctrl_queue_cnt++;
10781 }
10782 
10783 void
10784 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10785     struct mbuf *m, int iphlen, int bad_crc)
10786 {
10787 	struct sctp_association *asoc;
10788 	struct sctp_pktdrop_chunk *drp;
10789 	struct sctp_tmit_chunk *chk;
10790 	uint8_t *datap;
10791 	int len;
10792 	int was_trunc = 0;
10793 	struct ip *iph;
10794 
10795 #ifdef INET6
10796 	struct ip6_hdr *ip6h;
10797 
10798 #endif
10799 	int fullsz = 0, extra = 0;
10800 	long spc;
10801 	int offset;
10802 	struct sctp_chunkhdr *ch, chunk_buf;
10803 	unsigned int chk_length;
10804 
10805 	if (!stcb) {
10806 		return;
10807 	}
10808 	asoc = &stcb->asoc;
10809 	SCTP_TCB_LOCK_ASSERT(stcb);
10810 	if (asoc->peer_supports_pktdrop == 0) {
10811 		/*-
10812 		 * peer must declare support before I send one.
10813 		 */
10814 		return;
10815 	}
10816 	if (stcb->sctp_socket == NULL) {
10817 		return;
10818 	}
10819 	sctp_alloc_a_chunk(stcb, chk);
10820 	if (chk == NULL) {
10821 		return;
10822 	}
10823 	chk->copy_by_ref = 0;
10824 	iph = mtod(m, struct ip *);
10825 	if (iph == NULL) {
10826 		sctp_free_a_chunk(stcb, chk);
10827 		return;
10828 	}
10829 	switch (iph->ip_v) {
10830 	case IPVERSION:
10831 		/* IPv4 */
10832 		len = chk->send_size = iph->ip_len;
10833 		break;
10834 #ifdef INET6
10835 	case IPV6_VERSION >> 4:
10836 		/* IPv6 */
10837 		ip6h = mtod(m, struct ip6_hdr *);
10838 		len = chk->send_size = htons(ip6h->ip6_plen);
10839 		break;
10840 #endif
10841 	default:
10842 		return;
10843 	}
10844 	/* Validate that we do not have an ABORT in here. */
10845 	offset = iphlen + sizeof(struct sctphdr);
10846 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10847 	    sizeof(*ch), (uint8_t *) & chunk_buf);
10848 	while (ch != NULL) {
10849 		chk_length = ntohs(ch->chunk_length);
10850 		if (chk_length < sizeof(*ch)) {
10851 			/* break to abort land */
10852 			break;
10853 		}
10854 		switch (ch->chunk_type) {
10855 		case SCTP_PACKET_DROPPED:
10856 		case SCTP_ABORT_ASSOCIATION:
10857 		case SCTP_INITIATION_ACK:
10858 			/**
10859 			 * We don't respond with an PKT-DROP to an ABORT
10860 			 * or PKT-DROP. We also do not respond to an
10861 			 * INIT-ACK, because we can't know if the initiation
10862 			 * tag is correct or not.
10863 			 */
10864 			sctp_free_a_chunk(stcb, chk);
10865 			return;
10866 		default:
10867 			break;
10868 		}
10869 		offset += SCTP_SIZE32(chk_length);
10870 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10871 		    sizeof(*ch), (uint8_t *) & chunk_buf);
10872 	}
10873 
10874 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
10875 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
10876 		/*
10877 		 * only send 1 mtu worth, trim off the excess on the end.
10878 		 */
10879 		fullsz = len - extra;
10880 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
10881 		was_trunc = 1;
10882 	}
10883 	chk->asoc = &stcb->asoc;
10884 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10885 	if (chk->data == NULL) {
10886 jump_out:
10887 		sctp_free_a_chunk(stcb, chk);
10888 		return;
10889 	}
10890 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10891 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
10892 	if (drp == NULL) {
10893 		sctp_m_freem(chk->data);
10894 		chk->data = NULL;
10895 		goto jump_out;
10896 	}
10897 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
10898 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
10899 	chk->book_size_scale = 0;
10900 	if (was_trunc) {
10901 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
10902 		drp->trunc_len = htons(fullsz);
10903 		/*
10904 		 * Len is already adjusted to size minus overhead above take
10905 		 * out the pkt_drop chunk itself from it.
10906 		 */
10907 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
10908 		len = chk->send_size;
10909 	} else {
10910 		/* no truncation needed */
10911 		drp->ch.chunk_flags = 0;
10912 		drp->trunc_len = htons(0);
10913 	}
10914 	if (bad_crc) {
10915 		drp->ch.chunk_flags |= SCTP_BADCRC;
10916 	}
10917 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
10918 	SCTP_BUF_LEN(chk->data) = chk->send_size;
10919 	chk->sent = SCTP_DATAGRAM_UNSENT;
10920 	chk->snd_count = 0;
10921 	if (net) {
10922 		/* we should hit here */
10923 		chk->whoTo = net;
10924 	} else {
10925 		chk->whoTo = asoc->primary_destination;
10926 	}
10927 	atomic_add_int(&chk->whoTo->ref_count, 1);
10928 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
10929 	chk->rec.chunk_id.can_take_data = 1;
10930 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
10931 	drp->ch.chunk_length = htons(chk->send_size);
10932 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
10933 	if (spc < 0) {
10934 		spc = 0;
10935 	}
10936 	drp->bottle_bw = htonl(spc);
10937 	if (asoc->my_rwnd) {
10938 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
10939 		    asoc->size_on_all_streams +
10940 		    asoc->my_rwnd_control_len +
10941 		    stcb->sctp_socket->so_rcv.sb_cc);
10942 	} else {
10943 		/*-
10944 		 * If my rwnd is 0, possibly from mbuf depletion as well as
10945 		 * space used, tell the peer there is NO space aka onq == bw
10946 		 */
10947 		drp->current_onq = htonl(spc);
10948 	}
10949 	drp->reserved = 0;
10950 	datap = drp->data;
10951 	m_copydata(m, iphlen, len, (caddr_t)datap);
10952 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10953 	asoc->ctrl_queue_cnt++;
10954 }
10955 
10956 void
10957 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
10958 {
10959 	struct sctp_association *asoc;
10960 	struct sctp_cwr_chunk *cwr;
10961 	struct sctp_tmit_chunk *chk;
10962 
10963 	asoc = &stcb->asoc;
10964 	SCTP_TCB_LOCK_ASSERT(stcb);
10965 
10966 
10967 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10968 		if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
10969 			/*
10970 			 * found a previous CWR queued to same destination
10971 			 * update it if needed
10972 			 */
10973 			uint32_t ctsn;
10974 
10975 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
10976 			ctsn = ntohl(cwr->tsn);
10977 			if (SCTP_TSN_GT(high_tsn, ctsn)) {
10978 				cwr->tsn = htonl(high_tsn);
10979 			}
10980 			if (override & SCTP_CWR_REDUCE_OVERRIDE) {
10981 				/* Make sure override is carried */
10982 				cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
10983 			}
10984 			return;
10985 		}
10986 	}
10987 	sctp_alloc_a_chunk(stcb, chk);
10988 	if (chk == NULL) {
10989 		return;
10990 	}
10991 	chk->copy_by_ref = 0;
10992 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
10993 	chk->rec.chunk_id.can_take_data = 1;
10994 	chk->asoc = &stcb->asoc;
10995 	chk->send_size = sizeof(struct sctp_cwr_chunk);
10996 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10997 	if (chk->data == NULL) {
10998 		sctp_free_a_chunk(stcb, chk);
10999 		return;
11000 	}
11001 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11002 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11003 	chk->sent = SCTP_DATAGRAM_UNSENT;
11004 	chk->snd_count = 0;
11005 	chk->whoTo = net;
11006 	atomic_add_int(&chk->whoTo->ref_count, 1);
11007 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11008 	cwr->ch.chunk_type = SCTP_ECN_CWR;
11009 	cwr->ch.chunk_flags = override;
11010 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11011 	cwr->tsn = htonl(high_tsn);
11012 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11013 	asoc->ctrl_queue_cnt++;
11014 }
11015 
11016 void
11017 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11018     int number_entries, uint16_t * list,
11019     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11020 {
11021 	int len, old_len, i;
11022 	struct sctp_stream_reset_out_request *req_out;
11023 	struct sctp_chunkhdr *ch;
11024 
11025 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11026 
11027 
11028 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11029 
11030 	/* get to new offset for the param. */
11031 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11032 	/* now how long will this param be? */
11033 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11034 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11035 	req_out->ph.param_length = htons(len);
11036 	req_out->request_seq = htonl(seq);
11037 	req_out->response_seq = htonl(resp_seq);
11038 	req_out->send_reset_at_tsn = htonl(last_sent);
11039 	if (number_entries) {
11040 		for (i = 0; i < number_entries; i++) {
11041 			req_out->list_of_streams[i] = htons(list[i]);
11042 		}
11043 	}
11044 	if (SCTP_SIZE32(len) > len) {
11045 		/*-
11046 		 * Need to worry about the pad we may end up adding to the
11047 		 * end. This is easy since the struct is either aligned to 4
11048 		 * bytes or 2 bytes off.
11049 		 */
11050 		req_out->list_of_streams[number_entries] = 0;
11051 	}
11052 	/* now fix the chunk length */
11053 	ch->chunk_length = htons(len + old_len);
11054 	chk->book_size = len + old_len;
11055 	chk->book_size_scale = 0;
11056 	chk->send_size = SCTP_SIZE32(chk->book_size);
11057 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11058 	return;
11059 }
11060 
11061 
11062 void
11063 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11064     int number_entries, uint16_t * list,
11065     uint32_t seq)
11066 {
11067 	int len, old_len, i;
11068 	struct sctp_stream_reset_in_request *req_in;
11069 	struct sctp_chunkhdr *ch;
11070 
11071 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11072 
11073 
11074 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11075 
11076 	/* get to new offset for the param. */
11077 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11078 	/* now how long will this param be? */
11079 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11080 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11081 	req_in->ph.param_length = htons(len);
11082 	req_in->request_seq = htonl(seq);
11083 	if (number_entries) {
11084 		for (i = 0; i < number_entries; i++) {
11085 			req_in->list_of_streams[i] = htons(list[i]);
11086 		}
11087 	}
11088 	if (SCTP_SIZE32(len) > len) {
11089 		/*-
11090 		 * Need to worry about the pad we may end up adding to the
11091 		 * end. This is easy since the struct is either aligned to 4
11092 		 * bytes or 2 bytes off.
11093 		 */
11094 		req_in->list_of_streams[number_entries] = 0;
11095 	}
11096 	/* now fix the chunk length */
11097 	ch->chunk_length = htons(len + old_len);
11098 	chk->book_size = len + old_len;
11099 	chk->book_size_scale = 0;
11100 	chk->send_size = SCTP_SIZE32(chk->book_size);
11101 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11102 	return;
11103 }
11104 
11105 
11106 void
11107 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11108     uint32_t seq)
11109 {
11110 	int len, old_len;
11111 	struct sctp_stream_reset_tsn_request *req_tsn;
11112 	struct sctp_chunkhdr *ch;
11113 
11114 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11115 
11116 
11117 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11118 
11119 	/* get to new offset for the param. */
11120 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11121 	/* now how long will this param be? */
11122 	len = sizeof(struct sctp_stream_reset_tsn_request);
11123 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11124 	req_tsn->ph.param_length = htons(len);
11125 	req_tsn->request_seq = htonl(seq);
11126 
11127 	/* now fix the chunk length */
11128 	ch->chunk_length = htons(len + old_len);
11129 	chk->send_size = len + old_len;
11130 	chk->book_size = SCTP_SIZE32(chk->send_size);
11131 	chk->book_size_scale = 0;
11132 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11133 	return;
11134 }
11135 
11136 void
11137 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11138     uint32_t resp_seq, uint32_t result)
11139 {
11140 	int len, old_len;
11141 	struct sctp_stream_reset_response *resp;
11142 	struct sctp_chunkhdr *ch;
11143 
11144 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11145 
11146 
11147 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11148 
11149 	/* get to new offset for the param. */
11150 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11151 	/* now how long will this param be? */
11152 	len = sizeof(struct sctp_stream_reset_response);
11153 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11154 	resp->ph.param_length = htons(len);
11155 	resp->response_seq = htonl(resp_seq);
11156 	resp->result = ntohl(result);
11157 
11158 	/* now fix the chunk length */
11159 	ch->chunk_length = htons(len + old_len);
11160 	chk->book_size = len + old_len;
11161 	chk->book_size_scale = 0;
11162 	chk->send_size = SCTP_SIZE32(chk->book_size);
11163 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11164 	return;
11165 
11166 }
11167 
11168 
11169 void
11170 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11171     uint32_t resp_seq, uint32_t result,
11172     uint32_t send_una, uint32_t recv_next)
11173 {
11174 	int len, old_len;
11175 	struct sctp_stream_reset_response_tsn *resp;
11176 	struct sctp_chunkhdr *ch;
11177 
11178 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11179 
11180 
11181 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11182 
11183 	/* get to new offset for the param. */
11184 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11185 	/* now how long will this param be? */
11186 	len = sizeof(struct sctp_stream_reset_response_tsn);
11187 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11188 	resp->ph.param_length = htons(len);
11189 	resp->response_seq = htonl(resp_seq);
11190 	resp->result = htonl(result);
11191 	resp->senders_next_tsn = htonl(send_una);
11192 	resp->receivers_next_tsn = htonl(recv_next);
11193 
11194 	/* now fix the chunk length */
11195 	ch->chunk_length = htons(len + old_len);
11196 	chk->book_size = len + old_len;
11197 	chk->send_size = SCTP_SIZE32(chk->book_size);
11198 	chk->book_size_scale = 0;
11199 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11200 	return;
11201 }
11202 
11203 static void
11204 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11205     uint32_t seq,
11206     uint16_t adding)
11207 {
11208 	int len, old_len;
11209 	struct sctp_chunkhdr *ch;
11210 	struct sctp_stream_reset_add_strm *addstr;
11211 
11212 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11213 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11214 
11215 	/* get to new offset for the param. */
11216 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11217 	/* now how long will this param be? */
11218 	len = sizeof(struct sctp_stream_reset_add_strm);
11219 
11220 	/* Fill it out. */
11221 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11222 	addstr->ph.param_length = htons(len);
11223 	addstr->request_seq = htonl(seq);
11224 	addstr->number_of_streams = htons(adding);
11225 	addstr->reserved = 0;
11226 
11227 	/* now fix the chunk length */
11228 	ch->chunk_length = htons(len + old_len);
11229 	chk->send_size = len + old_len;
11230 	chk->book_size = SCTP_SIZE32(chk->send_size);
11231 	chk->book_size_scale = 0;
11232 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11233 	return;
11234 }
11235 
11236 int
11237 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11238     int number_entries, uint16_t * list,
11239     uint8_t send_out_req,
11240     uint32_t resp_seq,
11241     uint8_t send_in_req,
11242     uint8_t send_tsn_req,
11243     uint8_t add_stream,
11244     uint16_t adding
11245 )
11246 {
11247 
11248 	struct sctp_association *asoc;
11249 	struct sctp_tmit_chunk *chk;
11250 	struct sctp_chunkhdr *ch;
11251 	uint32_t seq;
11252 
11253 	asoc = &stcb->asoc;
11254 	if (asoc->stream_reset_outstanding) {
11255 		/*-
11256 		 * Already one pending, must get ACK back to clear the flag.
11257 		 */
11258 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11259 		return (EBUSY);
11260 	}
11261 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11262 	    (add_stream == 0)) {
11263 		/* nothing to do */
11264 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11265 		return (EINVAL);
11266 	}
11267 	if (send_tsn_req && (send_out_req || send_in_req)) {
11268 		/* error, can't do that */
11269 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11270 		return (EINVAL);
11271 	}
11272 	sctp_alloc_a_chunk(stcb, chk);
11273 	if (chk == NULL) {
11274 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11275 		return (ENOMEM);
11276 	}
11277 	chk->copy_by_ref = 0;
11278 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11279 	chk->rec.chunk_id.can_take_data = 0;
11280 	chk->asoc = &stcb->asoc;
11281 	chk->book_size = sizeof(struct sctp_chunkhdr);
11282 	chk->send_size = SCTP_SIZE32(chk->book_size);
11283 	chk->book_size_scale = 0;
11284 
11285 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11286 	if (chk->data == NULL) {
11287 		sctp_free_a_chunk(stcb, chk);
11288 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11289 		return (ENOMEM);
11290 	}
11291 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11292 
11293 	/* setup chunk parameters */
11294 	chk->sent = SCTP_DATAGRAM_UNSENT;
11295 	chk->snd_count = 0;
11296 	chk->whoTo = asoc->primary_destination;
11297 	atomic_add_int(&chk->whoTo->ref_count, 1);
11298 
11299 	ch = mtod(chk->data, struct sctp_chunkhdr *);
11300 	ch->chunk_type = SCTP_STREAM_RESET;
11301 	ch->chunk_flags = 0;
11302 	ch->chunk_length = htons(chk->book_size);
11303 	SCTP_BUF_LEN(chk->data) = chk->send_size;
11304 
11305 	seq = stcb->asoc.str_reset_seq_out;
11306 	if (send_out_req) {
11307 		sctp_add_stream_reset_out(chk, number_entries, list,
11308 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
11309 		asoc->stream_reset_out_is_outstanding = 1;
11310 		seq++;
11311 		asoc->stream_reset_outstanding++;
11312 	}
11313 	if (add_stream) {
11314 		sctp_add_a_stream(chk, seq, adding);
11315 		seq++;
11316 		asoc->stream_reset_outstanding++;
11317 	}
11318 	if (send_in_req) {
11319 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
11320 		asoc->stream_reset_outstanding++;
11321 	}
11322 	if (send_tsn_req) {
11323 		sctp_add_stream_reset_tsn(chk, seq);
11324 		asoc->stream_reset_outstanding++;
11325 	}
11326 	asoc->str_reset = chk;
11327 
11328 	/* insert the chunk for sending */
11329 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11330 	    chk,
11331 	    sctp_next);
11332 	asoc->ctrl_queue_cnt++;
11333 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11334 	return (0);
11335 }
11336 
11337 void
11338 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11339     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11340 {
11341 	/*-
11342 	 * Formulate the abort message, and send it back down.
11343 	 */
11344 	struct mbuf *o_pak;
11345 	struct mbuf *mout;
11346 	struct sctp_abort_msg *abm;
11347 	struct ip *iph, *iph_out;
11348 	struct udphdr *udp;
11349 
11350 #ifdef INET6
11351 	struct ip6_hdr *ip6, *ip6_out;
11352 
11353 #endif
11354 	int iphlen_out, len;
11355 
11356 	/* don't respond to ABORT with ABORT */
11357 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11358 		if (err_cause)
11359 			sctp_m_freem(err_cause);
11360 		return;
11361 	}
11362 	iph = mtod(m, struct ip *);
11363 	switch (iph->ip_v) {
11364 	case IPVERSION:
11365 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11366 		break;
11367 #ifdef INET6
11368 	case IPV6_VERSION >> 4:
11369 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11370 		break;
11371 #endif
11372 	default:
11373 		if (err_cause) {
11374 			sctp_m_freem(err_cause);
11375 		}
11376 		return;
11377 	}
11378 	if (port) {
11379 		len += sizeof(struct udphdr);
11380 	}
11381 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11382 	if (mout == NULL) {
11383 		if (err_cause) {
11384 			sctp_m_freem(err_cause);
11385 		}
11386 		return;
11387 	}
11388 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
11389 	SCTP_BUF_LEN(mout) = len;
11390 	SCTP_BUF_NEXT(mout) = err_cause;
11391 	iph_out = NULL;
11392 #ifdef INET6
11393 	ip6_out = NULL;
11394 #endif
11395 	switch (iph->ip_v) {
11396 	case IPVERSION:
11397 		iph_out = mtod(mout, struct ip *);
11398 
11399 		/* Fill in the IP header for the ABORT */
11400 		iph_out->ip_v = IPVERSION;
11401 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11402 		iph_out->ip_tos = (u_char)0;
11403 		iph_out->ip_id = 0;
11404 		iph_out->ip_off = 0;
11405 		iph_out->ip_ttl = MAXTTL;
11406 		if (port) {
11407 			iph_out->ip_p = IPPROTO_UDP;
11408 		} else {
11409 			iph_out->ip_p = IPPROTO_SCTP;
11410 		}
11411 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11412 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11413 		/* let IP layer calculate this */
11414 		iph_out->ip_sum = 0;
11415 
11416 		iphlen_out = sizeof(*iph_out);
11417 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11418 		break;
11419 #ifdef INET6
11420 	case IPV6_VERSION >> 4:
11421 		ip6 = (struct ip6_hdr *)iph;
11422 		ip6_out = mtod(mout, struct ip6_hdr *);
11423 
11424 		/* Fill in the IP6 header for the ABORT */
11425 		ip6_out->ip6_flow = ip6->ip6_flow;
11426 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11427 		if (port) {
11428 			ip6_out->ip6_nxt = IPPROTO_UDP;
11429 		} else {
11430 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11431 		}
11432 		ip6_out->ip6_src = ip6->ip6_dst;
11433 		ip6_out->ip6_dst = ip6->ip6_src;
11434 
11435 		iphlen_out = sizeof(*ip6_out);
11436 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11437 		break;
11438 #endif				/* INET6 */
11439 	default:
11440 		/* Currently not supported */
11441 		sctp_m_freem(mout);
11442 		return;
11443 	}
11444 
11445 	udp = (struct udphdr *)abm;
11446 	if (port) {
11447 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11448 		udp->uh_dport = port;
11449 		/* set udp->uh_ulen later */
11450 		udp->uh_sum = 0;
11451 		iphlen_out += sizeof(struct udphdr);
11452 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11453 	}
11454 	abm->sh.src_port = sh->dest_port;
11455 	abm->sh.dest_port = sh->src_port;
11456 	abm->sh.checksum = 0;
11457 	if (vtag == 0) {
11458 		abm->sh.v_tag = sh->v_tag;
11459 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11460 	} else {
11461 		abm->sh.v_tag = htonl(vtag);
11462 		abm->msg.ch.chunk_flags = 0;
11463 	}
11464 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11465 
11466 	if (err_cause) {
11467 		struct mbuf *m_tmp = err_cause;
11468 		int err_len = 0;
11469 
11470 		/* get length of the err_cause chain */
11471 		while (m_tmp != NULL) {
11472 			err_len += SCTP_BUF_LEN(m_tmp);
11473 			m_tmp = SCTP_BUF_NEXT(m_tmp);
11474 		}
11475 		len = SCTP_BUF_LEN(mout) + err_len;
11476 		if (err_len % 4) {
11477 			/* need pad at end of chunk */
11478 			uint32_t cpthis = 0;
11479 			int padlen;
11480 
11481 			padlen = 4 - (len % 4);
11482 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11483 			len += padlen;
11484 		}
11485 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11486 	} else {
11487 		len = SCTP_BUF_LEN(mout);
11488 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11489 	}
11490 
11491 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11492 		/* no mbuf's */
11493 		sctp_m_freem(mout);
11494 		return;
11495 	}
11496 	if (iph_out != NULL) {
11497 		sctp_route_t ro;
11498 		int ret;
11499 
11500 		/* zap the stack pointer to the route */
11501 		bzero(&ro, sizeof ro);
11502 		if (port) {
11503 			udp->uh_ulen = htons(len - sizeof(struct ip));
11504 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11505 		}
11506 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11507 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11508 		/* set IPv4 length */
11509 		iph_out->ip_len = len;
11510 		/* out it goes */
11511 #ifdef  SCTP_PACKET_LOGGING
11512 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11513 			sctp_packet_log(mout, len);
11514 #endif
11515 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11516 		if (port) {
11517 #if defined(SCTP_WITH_NO_CSUM)
11518 			SCTP_STAT_INCR(sctps_sendnocrc);
11519 #else
11520 			abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11521 			SCTP_STAT_INCR(sctps_sendswcrc);
11522 #endif
11523 			SCTP_ENABLE_UDP_CSUM(o_pak);
11524 		} else {
11525 #if defined(SCTP_WITH_NO_CSUM)
11526 			SCTP_STAT_INCR(sctps_sendnocrc);
11527 #else
11528 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11529 			mout->m_pkthdr.csum_data = 0;
11530 			SCTP_STAT_INCR(sctps_sendhwcrc);
11531 #endif
11532 		}
11533 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
11534 
11535 		/* Free the route if we got one back */
11536 		if (ro.ro_rt)
11537 			RTFREE(ro.ro_rt);
11538 	}
11539 #ifdef INET6
11540 	if (ip6_out != NULL) {
11541 		struct route_in6 ro;
11542 		int ret;
11543 		struct ifnet *ifp = NULL;
11544 
11545 		/* zap the stack pointer to the route */
11546 		bzero(&ro, sizeof(ro));
11547 		if (port) {
11548 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11549 		}
11550 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11551 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11552 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
11553 #ifdef  SCTP_PACKET_LOGGING
11554 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11555 			sctp_packet_log(mout, len);
11556 #endif
11557 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11558 		if (port) {
11559 #if defined(SCTP_WITH_NO_CSUM)
11560 			SCTP_STAT_INCR(sctps_sendnocrc);
11561 #else
11562 			abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11563 			SCTP_STAT_INCR(sctps_sendswcrc);
11564 #endif
11565 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11566 				udp->uh_sum = 0xffff;
11567 			}
11568 		} else {
11569 #if defined(SCTP_WITH_NO_CSUM)
11570 			SCTP_STAT_INCR(sctps_sendnocrc);
11571 #else
11572 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11573 			mout->m_pkthdr.csum_data = 0;
11574 			SCTP_STAT_INCR(sctps_sendhwcrc);
11575 #endif
11576 		}
11577 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
11578 
11579 		/* Free the route if we got one back */
11580 		if (ro.ro_rt)
11581 			RTFREE(ro.ro_rt);
11582 	}
11583 #endif
11584 	SCTP_STAT_INCR(sctps_sendpackets);
11585 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11586 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11587 }
11588 
11589 void
11590 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11591     uint32_t vrf_id, uint16_t port)
11592 {
11593 	struct mbuf *o_pak;
11594 	struct sctphdr *sh, *sh_out;
11595 	struct sctp_chunkhdr *ch;
11596 	struct ip *iph, *iph_out;
11597 	struct udphdr *udp = NULL;
11598 	struct mbuf *mout;
11599 
11600 #ifdef INET6
11601 	struct ip6_hdr *ip6, *ip6_out;
11602 
11603 #endif
11604 	int iphlen_out, len;
11605 
11606 	iph = mtod(m, struct ip *);
11607 	sh = (struct sctphdr *)((caddr_t)iph + iphlen);
11608 	switch (iph->ip_v) {
11609 	case IPVERSION:
11610 		len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11611 		break;
11612 #ifdef INET6
11613 	case IPV6_VERSION >> 4:
11614 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11615 		break;
11616 #endif
11617 	default:
11618 		if (scm) {
11619 			sctp_m_freem(scm);
11620 		}
11621 		return;
11622 	}
11623 	if (port) {
11624 		len += sizeof(struct udphdr);
11625 	}
11626 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11627 	if (mout == NULL) {
11628 		if (scm) {
11629 			sctp_m_freem(scm);
11630 		}
11631 		return;
11632 	}
11633 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
11634 	SCTP_BUF_LEN(mout) = len;
11635 	SCTP_BUF_NEXT(mout) = scm;
11636 	iph_out = NULL;
11637 #ifdef INET6
11638 	ip6_out = NULL;
11639 #endif
11640 	switch (iph->ip_v) {
11641 	case IPVERSION:
11642 		iph_out = mtod(mout, struct ip *);
11643 
11644 		/* Fill in the IP header for the ABORT */
11645 		iph_out->ip_v = IPVERSION;
11646 		iph_out->ip_hl = (sizeof(struct ip) / 4);
11647 		iph_out->ip_tos = (u_char)0;
11648 		iph_out->ip_id = 0;
11649 		iph_out->ip_off = 0;
11650 		iph_out->ip_ttl = MAXTTL;
11651 		if (port) {
11652 			iph_out->ip_p = IPPROTO_UDP;
11653 		} else {
11654 			iph_out->ip_p = IPPROTO_SCTP;
11655 		}
11656 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11657 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11658 		/* let IP layer calculate this */
11659 		iph_out->ip_sum = 0;
11660 
11661 		iphlen_out = sizeof(struct ip);
11662 		sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
11663 		break;
11664 #ifdef INET6
11665 	case IPV6_VERSION >> 4:
11666 		ip6 = (struct ip6_hdr *)iph;
11667 		ip6_out = mtod(mout, struct ip6_hdr *);
11668 
11669 		/* Fill in the IP6 header for the ABORT */
11670 		ip6_out->ip6_flow = ip6->ip6_flow;
11671 		ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11672 		if (port) {
11673 			ip6_out->ip6_nxt = IPPROTO_UDP;
11674 		} else {
11675 			ip6_out->ip6_nxt = IPPROTO_SCTP;
11676 		}
11677 		ip6_out->ip6_src = ip6->ip6_dst;
11678 		ip6_out->ip6_dst = ip6->ip6_src;
11679 
11680 		iphlen_out = sizeof(struct ip6_hdr);
11681 		sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
11682 		break;
11683 #endif				/* INET6 */
11684 	default:
11685 		/* Currently not supported */
11686 		sctp_m_freem(mout);
11687 		return;
11688 	}
11689 
11690 	udp = (struct udphdr *)sh_out;
11691 	if (port) {
11692 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11693 		udp->uh_dport = port;
11694 		/* set udp->uh_ulen later */
11695 		udp->uh_sum = 0;
11696 		iphlen_out += sizeof(struct udphdr);
11697 		sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
11698 	}
11699 	sh_out->src_port = sh->dest_port;
11700 	sh_out->dest_port = sh->src_port;
11701 	sh_out->v_tag = vtag;
11702 	sh_out->checksum = 0;
11703 
11704 	ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
11705 	ch->chunk_type = SCTP_OPERATION_ERROR;
11706 	ch->chunk_flags = 0;
11707 
11708 	if (scm) {
11709 		struct mbuf *m_tmp = scm;
11710 		int cause_len = 0;
11711 
11712 		/* get length of the err_cause chain */
11713 		while (m_tmp != NULL) {
11714 			cause_len += SCTP_BUF_LEN(m_tmp);
11715 			m_tmp = SCTP_BUF_NEXT(m_tmp);
11716 		}
11717 		len = SCTP_BUF_LEN(mout) + cause_len;
11718 		if (cause_len % 4) {
11719 			/* need pad at end of chunk */
11720 			uint32_t cpthis = 0;
11721 			int padlen;
11722 
11723 			padlen = 4 - (len % 4);
11724 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11725 			len += padlen;
11726 		}
11727 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
11728 	} else {
11729 		len = SCTP_BUF_LEN(mout);
11730 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
11731 	}
11732 
11733 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11734 		/* no mbuf's */
11735 		sctp_m_freem(mout);
11736 		return;
11737 	}
11738 	if (iph_out != NULL) {
11739 		sctp_route_t ro;
11740 		int ret;
11741 
11742 		/* zap the stack pointer to the route */
11743 		bzero(&ro, sizeof ro);
11744 		if (port) {
11745 			udp->uh_ulen = htons(len - sizeof(struct ip));
11746 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11747 		}
11748 		/* set IPv4 length */
11749 		iph_out->ip_len = len;
11750 		/* out it goes */
11751 #ifdef  SCTP_PACKET_LOGGING
11752 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11753 			sctp_packet_log(mout, len);
11754 #endif
11755 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11756 		if (port) {
11757 #if defined(SCTP_WITH_NO_CSUM)
11758 			SCTP_STAT_INCR(sctps_sendnocrc);
11759 #else
11760 			sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
11761 			SCTP_STAT_INCR(sctps_sendswcrc);
11762 #endif
11763 			SCTP_ENABLE_UDP_CSUM(o_pak);
11764 		} else {
11765 #if defined(SCTP_WITH_NO_CSUM)
11766 			SCTP_STAT_INCR(sctps_sendnocrc);
11767 #else
11768 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11769 			mout->m_pkthdr.csum_data = 0;
11770 			SCTP_STAT_INCR(sctps_sendhwcrc);
11771 #endif
11772 		}
11773 		SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
11774 
11775 		/* Free the route if we got one back */
11776 		if (ro.ro_rt)
11777 			RTFREE(ro.ro_rt);
11778 	}
11779 #ifdef INET6
11780 	if (ip6_out != NULL) {
11781 		struct route_in6 ro;
11782 		int ret;
11783 		struct ifnet *ifp = NULL;
11784 
11785 		/* zap the stack pointer to the route */
11786 		bzero(&ro, sizeof(ro));
11787 		if (port) {
11788 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11789 		}
11790 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
11791 #ifdef  SCTP_PACKET_LOGGING
11792 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11793 			sctp_packet_log(mout, len);
11794 #endif
11795 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
11796 		if (port) {
11797 #if defined(SCTP_WITH_NO_CSUM)
11798 			SCTP_STAT_INCR(sctps_sendnocrc);
11799 #else
11800 			sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11801 			SCTP_STAT_INCR(sctps_sendswcrc);
11802 #endif
11803 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11804 				udp->uh_sum = 0xffff;
11805 			}
11806 		} else {
11807 #if defined(SCTP_WITH_NO_CSUM)
11808 			SCTP_STAT_INCR(sctps_sendnocrc);
11809 #else
11810 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
11811 			mout->m_pkthdr.csum_data = 0;
11812 			SCTP_STAT_INCR(sctps_sendhwcrc);
11813 #endif
11814 		}
11815 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
11816 
11817 		/* Free the route if we got one back */
11818 		if (ro.ro_rt)
11819 			RTFREE(ro.ro_rt);
11820 	}
11821 #endif
11822 	SCTP_STAT_INCR(sctps_sendpackets);
11823 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11824 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11825 }
11826 
11827 static struct mbuf *
11828 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11829     struct uio *uio,
11830     struct sctp_sndrcvinfo *srcv,
11831     int max_send_len,
11832     int user_marks_eor,
11833     int *error,
11834     uint32_t * sndout,
11835     struct mbuf **new_tail)
11836 {
11837 	struct mbuf *m;
11838 
11839 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11840 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11841 	if (m == NULL) {
11842 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11843 		*error = ENOMEM;
11844 	} else {
11845 		*sndout = m_length(m, NULL);
11846 		*new_tail = m_last(m);
11847 	}
11848 	return (m);
11849 }
11850 
11851 static int
11852 sctp_copy_one(struct sctp_stream_queue_pending *sp,
11853     struct uio *uio,
11854     int resv_upfront)
11855 {
11856 	int left;
11857 
11858 	left = sp->length;
11859 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
11860 	    resv_upfront, 0);
11861 	if (sp->data == NULL) {
11862 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11863 		return (ENOMEM);
11864 	}
11865 	sp->tail_mbuf = m_last(sp->data);
11866 	return (0);
11867 }
11868 
11869 
11870 
11871 static struct sctp_stream_queue_pending *
11872 sctp_copy_it_in(struct sctp_tcb *stcb,
11873     struct sctp_association *asoc,
11874     struct sctp_sndrcvinfo *srcv,
11875     struct uio *uio,
11876     struct sctp_nets *net,
11877     int max_send_len,
11878     int user_marks_eor,
11879     int *error,
11880     int non_blocking)
11881 {
11882 	/*-
11883 	 * This routine must be very careful in its work. Protocol
11884 	 * processing is up and running so care must be taken to spl...()
11885 	 * when you need to do something that may effect the stcb/asoc. The
11886 	 * sb is locked however. When data is copied the protocol processing
11887 	 * should be enabled since this is a slower operation...
11888 	 */
11889 	struct sctp_stream_queue_pending *sp = NULL;
11890 	int resv_in_first;
11891 
11892 	*error = 0;
11893 	/* Now can we send this? */
11894 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
11895 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
11896 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
11897 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
11898 		/* got data while shutting down */
11899 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
11900 		*error = ECONNRESET;
11901 		goto out_now;
11902 	}
11903 	sctp_alloc_a_strmoq(stcb, sp);
11904 	if (sp == NULL) {
11905 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11906 		*error = ENOMEM;
11907 		goto out_now;
11908 	}
11909 	sp->act_flags = 0;
11910 	sp->sender_all_done = 0;
11911 	sp->sinfo_flags = srcv->sinfo_flags;
11912 	sp->timetolive = srcv->sinfo_timetolive;
11913 	sp->ppid = srcv->sinfo_ppid;
11914 	sp->context = srcv->sinfo_context;
11915 	sp->strseq = 0;
11916 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
11917 
11918 	sp->stream = srcv->sinfo_stream;
11919 	sp->length = min(uio->uio_resid, max_send_len);
11920 	if ((sp->length == (uint32_t) uio->uio_resid) &&
11921 	    ((user_marks_eor == 0) ||
11922 	    (srcv->sinfo_flags & SCTP_EOF) ||
11923 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
11924 		sp->msg_is_complete = 1;
11925 	} else {
11926 		sp->msg_is_complete = 0;
11927 	}
11928 	sp->sender_all_done = 0;
11929 	sp->some_taken = 0;
11930 	sp->put_last_out = 0;
11931 	resv_in_first = sizeof(struct sctp_data_chunk);
11932 	sp->data = sp->tail_mbuf = NULL;
11933 	if (sp->length == 0) {
11934 		*error = 0;
11935 		goto skip_copy;
11936 	}
11937 	sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
11938 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
11939 		sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
11940 		sp->holds_key_ref = 1;
11941 	}
11942 	*error = sctp_copy_one(sp, uio, resv_in_first);
11943 skip_copy:
11944 	if (*error) {
11945 		sctp_free_a_strmoq(stcb, sp);
11946 		sp = NULL;
11947 	} else {
11948 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
11949 			sp->net = net;
11950 			atomic_add_int(&sp->net->ref_count, 1);
11951 		} else {
11952 			sp->net = NULL;
11953 		}
11954 		sctp_set_prsctp_policy(sp);
11955 	}
11956 out_now:
11957 	return (sp);
11958 }
11959 
11960 
11961 int
11962 sctp_sosend(struct socket *so,
11963     struct sockaddr *addr,
11964     struct uio *uio,
11965     struct mbuf *top,
11966     struct mbuf *control,
11967     int flags,
11968     struct thread *p
11969 )
11970 {
11971 	int error, use_rcvinfo = 0;
11972 	struct sctp_sndrcvinfo srcv;
11973 	struct sockaddr *addr_to_use;
11974 
11975 #if defined(INET) && defined(INET6)
11976 	struct sockaddr_in sin;
11977 
11978 #endif
11979 
11980 	if (control) {
11981 		/* process cmsg snd/rcv info (maybe a assoc-id) */
11982 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
11983 		    sizeof(srcv))) {
11984 			/* got one */
11985 			use_rcvinfo = 1;
11986 		}
11987 	}
11988 	addr_to_use = addr;
11989 #if defined(INET) && defined(INET6)
11990 	if ((addr) && (addr->sa_family == AF_INET6)) {
11991 		struct sockaddr_in6 *sin6;
11992 
11993 		sin6 = (struct sockaddr_in6 *)addr;
11994 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
11995 			in6_sin6_2_sin(&sin, sin6);
11996 			addr_to_use = (struct sockaddr *)&sin;
11997 		}
11998 	}
11999 #endif
12000 	error = sctp_lower_sosend(so, addr_to_use, uio, top,
12001 	    control,
12002 	    flags,
12003 	    use_rcvinfo ? &srcv : NULL
12004 	    ,p
12005 	    );
12006 	return (error);
12007 }
12008 
12009 
12010 int
12011 sctp_lower_sosend(struct socket *so,
12012     struct sockaddr *addr,
12013     struct uio *uio,
12014     struct mbuf *i_pak,
12015     struct mbuf *control,
12016     int flags,
12017     struct sctp_sndrcvinfo *srcv
12018     ,
12019     struct thread *p
12020 )
12021 {
12022 	unsigned int sndlen = 0, max_len;
12023 	int error, len;
12024 	struct mbuf *top = NULL;
12025 	int queue_only = 0, queue_only_for_init = 0;
12026 	int free_cnt_applied = 0;
12027 	int un_sent;
12028 	int now_filled = 0;
12029 	unsigned int inqueue_bytes = 0;
12030 	struct sctp_block_entry be;
12031 	struct sctp_inpcb *inp;
12032 	struct sctp_tcb *stcb = NULL;
12033 	struct timeval now;
12034 	struct sctp_nets *net;
12035 	struct sctp_association *asoc;
12036 	struct sctp_inpcb *t_inp;
12037 	int user_marks_eor;
12038 	int create_lock_applied = 0;
12039 	int nagle_applies = 0;
12040 	int some_on_control = 0;
12041 	int got_all_of_the_send = 0;
12042 	int hold_tcblock = 0;
12043 	int non_blocking = 0;
12044 	uint32_t local_add_more, local_soresv = 0;
12045 	uint16_t port;
12046 	uint16_t sinfo_flags;
12047 	sctp_assoc_t sinfo_assoc_id;
12048 
12049 	error = 0;
12050 	net = NULL;
12051 	stcb = NULL;
12052 	asoc = NULL;
12053 
12054 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12055 	if (inp == NULL) {
12056 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12057 		error = EINVAL;
12058 		if (i_pak) {
12059 			SCTP_RELEASE_PKT(i_pak);
12060 		}
12061 		return (error);
12062 	}
12063 	if ((uio == NULL) && (i_pak == NULL)) {
12064 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12065 		return (EINVAL);
12066 	}
12067 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12068 	atomic_add_int(&inp->total_sends, 1);
12069 	if (uio) {
12070 		if (uio->uio_resid < 0) {
12071 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12072 			return (EINVAL);
12073 		}
12074 		sndlen = uio->uio_resid;
12075 	} else {
12076 		top = SCTP_HEADER_TO_CHAIN(i_pak);
12077 		sndlen = SCTP_HEADER_LEN(i_pak);
12078 	}
12079 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12080 	    addr,
12081 	    sndlen);
12082 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12083 	    (inp->sctp_socket->so_qlimit)) {
12084 		/* The listener can NOT send */
12085 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12086 		error = ENOTCONN;
12087 		goto out_unlocked;
12088 	}
12089 	/**
12090 	 * Pre-screen address, if one is given the sin-len
12091 	 * must be set correctly!
12092 	 */
12093 	if (addr) {
12094 		union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12095 
12096 		switch (raddr->sa.sa_family) {
12097 #if defined(INET)
12098 		case AF_INET:
12099 			if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12100 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12101 				error = EINVAL;
12102 				goto out_unlocked;
12103 			}
12104 			port = raddr->sin.sin_port;
12105 			break;
12106 #endif
12107 #if defined(INET6)
12108 		case AF_INET6:
12109 			if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12110 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12111 				error = EINVAL;
12112 				goto out_unlocked;
12113 			}
12114 			port = raddr->sin6.sin6_port;
12115 			break;
12116 #endif
12117 		default:
12118 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT);
12119 			error = EAFNOSUPPORT;
12120 			goto out_unlocked;
12121 		}
12122 	} else
12123 		port = 0;
12124 
12125 	if (srcv) {
12126 		sinfo_flags = srcv->sinfo_flags;
12127 		sinfo_assoc_id = srcv->sinfo_assoc_id;
12128 		if (INVALID_SINFO_FLAG(sinfo_flags) ||
12129 		    PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12130 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12131 			error = EINVAL;
12132 			goto out_unlocked;
12133 		}
12134 		if (srcv->sinfo_flags)
12135 			SCTP_STAT_INCR(sctps_sends_with_flags);
12136 	} else {
12137 		sinfo_flags = inp->def_send.sinfo_flags;
12138 		sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12139 	}
12140 	if (sinfo_flags & SCTP_SENDALL) {
12141 		/* its a sendall */
12142 		error = sctp_sendall(inp, uio, top, srcv);
12143 		top = NULL;
12144 		goto out_unlocked;
12145 	}
12146 	if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12147 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12148 		error = EINVAL;
12149 		goto out_unlocked;
12150 	}
12151 	/* now we must find the assoc */
12152 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12153 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12154 		SCTP_INP_RLOCK(inp);
12155 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
12156 		if (stcb == NULL) {
12157 			SCTP_INP_RUNLOCK(inp);
12158 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12159 			error = ENOTCONN;
12160 			goto out_unlocked;
12161 		}
12162 		SCTP_TCB_LOCK(stcb);
12163 		hold_tcblock = 1;
12164 		SCTP_INP_RUNLOCK(inp);
12165 	} else if (sinfo_assoc_id) {
12166 		stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 0);
12167 	} else if (addr) {
12168 		/*-
12169 		 * Since we did not use findep we must
12170 		 * increment it, and if we don't find a tcb
12171 		 * decrement it.
12172 		 */
12173 		SCTP_INP_WLOCK(inp);
12174 		SCTP_INP_INCR_REF(inp);
12175 		SCTP_INP_WUNLOCK(inp);
12176 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12177 		if (stcb == NULL) {
12178 			SCTP_INP_WLOCK(inp);
12179 			SCTP_INP_DECR_REF(inp);
12180 			SCTP_INP_WUNLOCK(inp);
12181 		} else {
12182 			hold_tcblock = 1;
12183 		}
12184 	}
12185 	if ((stcb == NULL) && (addr)) {
12186 		/* Possible implicit send? */
12187 		SCTP_ASOC_CREATE_LOCK(inp);
12188 		create_lock_applied = 1;
12189 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12190 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12191 			/* Should I really unlock ? */
12192 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12193 			error = EINVAL;
12194 			goto out_unlocked;
12195 
12196 		}
12197 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12198 		    (addr->sa_family == AF_INET6)) {
12199 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12200 			error = EINVAL;
12201 			goto out_unlocked;
12202 		}
12203 		SCTP_INP_WLOCK(inp);
12204 		SCTP_INP_INCR_REF(inp);
12205 		SCTP_INP_WUNLOCK(inp);
12206 		/* With the lock applied look again */
12207 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12208 		if (stcb == NULL) {
12209 			SCTP_INP_WLOCK(inp);
12210 			SCTP_INP_DECR_REF(inp);
12211 			SCTP_INP_WUNLOCK(inp);
12212 		} else {
12213 			hold_tcblock = 1;
12214 		}
12215 		if (t_inp != inp) {
12216 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12217 			error = ENOTCONN;
12218 			goto out_unlocked;
12219 		}
12220 	}
12221 	if (stcb == NULL) {
12222 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
12223 		    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12224 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12225 			error = ENOTCONN;
12226 			goto out_unlocked;
12227 		}
12228 		if (addr == NULL) {
12229 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12230 			error = ENOENT;
12231 			goto out_unlocked;
12232 		} else {
12233 			/*
12234 			 * UDP style, we must go ahead and start the INIT
12235 			 * process
12236 			 */
12237 			uint32_t vrf_id;
12238 
12239 			if ((sinfo_flags & SCTP_ABORT) ||
12240 			    ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12241 				/*-
12242 				 * User asks to abort a non-existant assoc,
12243 				 * or EOF a non-existant assoc with no data
12244 				 */
12245 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12246 				error = ENOENT;
12247 				goto out_unlocked;
12248 			}
12249 			/* get an asoc/stcb struct */
12250 			vrf_id = inp->def_vrf_id;
12251 #ifdef INVARIANTS
12252 			if (create_lock_applied == 0) {
12253 				panic("Error, should hold create lock and I don't?");
12254 			}
12255 #endif
12256 			stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12257 			    p
12258 			    );
12259 			if (stcb == NULL) {
12260 				/* Error is setup for us in the call */
12261 				goto out_unlocked;
12262 			}
12263 			if (create_lock_applied) {
12264 				SCTP_ASOC_CREATE_UNLOCK(inp);
12265 				create_lock_applied = 0;
12266 			} else {
12267 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12268 			}
12269 			/*
12270 			 * Turn on queue only flag to prevent data from
12271 			 * being sent
12272 			 */
12273 			queue_only = 1;
12274 			asoc = &stcb->asoc;
12275 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12276 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12277 
12278 			/* initialize authentication params for the assoc */
12279 			sctp_initialize_auth_params(inp, stcb);
12280 
12281 			if (control) {
12282 				/*
12283 				 * see if a init structure exists in cmsg
12284 				 * headers
12285 				 */
12286 				struct sctp_initmsg initm;
12287 				int i;
12288 
12289 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12290 				    sizeof(initm))) {
12291 					/*
12292 					 * we have an INIT override of the
12293 					 * default
12294 					 */
12295 					if (initm.sinit_max_attempts)
12296 						asoc->max_init_times = initm.sinit_max_attempts;
12297 					if (initm.sinit_num_ostreams)
12298 						asoc->pre_open_streams = initm.sinit_num_ostreams;
12299 					if (initm.sinit_max_instreams)
12300 						asoc->max_inbound_streams = initm.sinit_max_instreams;
12301 					if (initm.sinit_max_init_timeo)
12302 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12303 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
12304 						struct sctp_stream_out *tmp_str;
12305 						int had_lock = 0;
12306 
12307 						/* Default is NOT correct */
12308 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12309 						    asoc->streamoutcnt, asoc->pre_open_streams);
12310 						/*
12311 						 * What happens if this
12312 						 * fails? we panic ...
12313 						 */
12314 
12315 						if (hold_tcblock) {
12316 							had_lock = 1;
12317 							SCTP_TCB_UNLOCK(stcb);
12318 						}
12319 						SCTP_MALLOC(tmp_str,
12320 						    struct sctp_stream_out *,
12321 						    (asoc->pre_open_streams *
12322 						    sizeof(struct sctp_stream_out)),
12323 						    SCTP_M_STRMO);
12324 						if (had_lock) {
12325 							SCTP_TCB_LOCK(stcb);
12326 						}
12327 						if (tmp_str != NULL) {
12328 							SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12329 							asoc->strmout = tmp_str;
12330 							asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
12331 						} else {
12332 							asoc->pre_open_streams = asoc->streamoutcnt;
12333 						}
12334 						for (i = 0; i < asoc->streamoutcnt; i++) {
12335 							/*-
12336 							 * inbound side must be set
12337 							 * to 0xffff, also NOTE when
12338 							 * we get the INIT-ACK back
12339 							 * (for INIT sender) we MUST
12340 							 * reduce the count
12341 							 * (streamoutcnt) but first
12342 							 * check if we sent to any
12343 							 * of the upper streams that
12344 							 * were dropped (if some
12345 							 * were). Those that were
12346 							 * dropped must be notified
12347 							 * to the upper layer as
12348 							 * failed to send.
12349 							 */
12350 							asoc->strmout[i].next_sequence_sent = 0x0;
12351 							TAILQ_INIT(&asoc->strmout[i].outqueue);
12352 							asoc->strmout[i].stream_no = i;
12353 							asoc->strmout[i].last_msg_incomplete = 0;
12354 							asoc->ss_functions.sctp_ss_init_stream(&asoc->strmout[i]);
12355 						}
12356 					}
12357 				}
12358 			}
12359 			hold_tcblock = 1;
12360 			/* out with the INIT */
12361 			queue_only_for_init = 1;
12362 			/*-
12363 			 * we may want to dig in after this call and adjust the MTU
12364 			 * value. It defaulted to 1500 (constant) but the ro
12365 			 * structure may now have an update and thus we may need to
12366 			 * change it BEFORE we append the message.
12367 			 */
12368 		}
12369 	} else
12370 		asoc = &stcb->asoc;
12371 	if (srcv == NULL)
12372 		srcv = (struct sctp_sndrcvinfo *)&asoc->def_send;
12373 	if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
12374 		if (addr)
12375 			net = sctp_findnet(stcb, addr);
12376 		else
12377 			net = NULL;
12378 		if ((net == NULL) ||
12379 		    ((port != 0) && (port != stcb->rport))) {
12380 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12381 			error = EINVAL;
12382 			goto out_unlocked;
12383 		}
12384 	} else {
12385 		net = stcb->asoc.primary_destination;
12386 	}
12387 	atomic_add_int(&stcb->total_sends, 1);
12388 	/* Keep the stcb from being freed under our feet */
12389 	atomic_add_int(&asoc->refcnt, 1);
12390 	free_cnt_applied = 1;
12391 
12392 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12393 		if (sndlen > asoc->smallest_mtu) {
12394 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12395 			error = EMSGSIZE;
12396 			goto out_unlocked;
12397 		}
12398 	}
12399 	if ((SCTP_SO_IS_NBIO(so)
12400 	    || (flags & MSG_NBIO)
12401 	    )) {
12402 		non_blocking = 1;
12403 	}
12404 	/* would we block? */
12405 	if (non_blocking) {
12406 		if (hold_tcblock == 0) {
12407 			SCTP_TCB_LOCK(stcb);
12408 			hold_tcblock = 1;
12409 		}
12410 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12411 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12412 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12413 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12414 			if (sndlen > SCTP_SB_LIMIT_SND(so))
12415 				error = EMSGSIZE;
12416 			else
12417 				error = EWOULDBLOCK;
12418 			goto out_unlocked;
12419 		}
12420 		stcb->asoc.sb_send_resv += sndlen;
12421 		SCTP_TCB_UNLOCK(stcb);
12422 		hold_tcblock = 0;
12423 	} else {
12424 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12425 	}
12426 	local_soresv = sndlen;
12427 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12428 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12429 		error = ECONNRESET;
12430 		goto out_unlocked;
12431 	}
12432 	if (create_lock_applied) {
12433 		SCTP_ASOC_CREATE_UNLOCK(inp);
12434 		create_lock_applied = 0;
12435 	}
12436 	if (asoc->stream_reset_outstanding) {
12437 		/*
12438 		 * Can't queue any data while stream reset is underway.
12439 		 */
12440 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12441 		error = EAGAIN;
12442 		goto out_unlocked;
12443 	}
12444 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12445 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12446 		queue_only = 1;
12447 	}
12448 	/* we are now done with all control */
12449 	if (control) {
12450 		sctp_m_freem(control);
12451 		control = NULL;
12452 	}
12453 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12454 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12455 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12456 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12457 		if (srcv->sinfo_flags & SCTP_ABORT) {
12458 			;
12459 		} else {
12460 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12461 			error = ECONNRESET;
12462 			goto out_unlocked;
12463 		}
12464 	}
12465 	/* Ok, we will attempt a msgsnd :> */
12466 	if (p) {
12467 		p->td_ru.ru_msgsnd++;
12468 	}
12469 	/* Are we aborting? */
12470 	if (srcv->sinfo_flags & SCTP_ABORT) {
12471 		struct mbuf *mm;
12472 		int tot_demand, tot_out = 0, max_out;
12473 
12474 		SCTP_STAT_INCR(sctps_sends_with_abort);
12475 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12476 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12477 			/* It has to be up before we abort */
12478 			/* how big is the user initiated abort? */
12479 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12480 			error = EINVAL;
12481 			goto out;
12482 		}
12483 		if (hold_tcblock) {
12484 			SCTP_TCB_UNLOCK(stcb);
12485 			hold_tcblock = 0;
12486 		}
12487 		if (top) {
12488 			struct mbuf *cntm = NULL;
12489 
12490 			mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12491 			if (sndlen != 0) {
12492 				cntm = top;
12493 				while (cntm) {
12494 					tot_out += SCTP_BUF_LEN(cntm);
12495 					cntm = SCTP_BUF_NEXT(cntm);
12496 				}
12497 			}
12498 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12499 		} else {
12500 			/* Must fit in a MTU */
12501 			tot_out = sndlen;
12502 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12503 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12504 				/* To big */
12505 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12506 				error = EMSGSIZE;
12507 				goto out;
12508 			}
12509 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12510 		}
12511 		if (mm == NULL) {
12512 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12513 			error = ENOMEM;
12514 			goto out;
12515 		}
12516 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12517 		max_out -= sizeof(struct sctp_abort_msg);
12518 		if (tot_out > max_out) {
12519 			tot_out = max_out;
12520 		}
12521 		if (mm) {
12522 			struct sctp_paramhdr *ph;
12523 
12524 			/* now move forward the data pointer */
12525 			ph = mtod(mm, struct sctp_paramhdr *);
12526 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12527 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12528 			ph++;
12529 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12530 			if (top == NULL) {
12531 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
12532 				if (error) {
12533 					/*-
12534 					 * Here if we can't get his data we
12535 					 * still abort we just don't get to
12536 					 * send the users note :-0
12537 					 */
12538 					sctp_m_freem(mm);
12539 					mm = NULL;
12540 				}
12541 			} else {
12542 				if (sndlen != 0) {
12543 					SCTP_BUF_NEXT(mm) = top;
12544 				}
12545 			}
12546 		}
12547 		if (hold_tcblock == 0) {
12548 			SCTP_TCB_LOCK(stcb);
12549 			hold_tcblock = 1;
12550 		}
12551 		atomic_add_int(&stcb->asoc.refcnt, -1);
12552 		free_cnt_applied = 0;
12553 		/* release this lock, otherwise we hang on ourselves */
12554 		sctp_abort_an_association(stcb->sctp_ep, stcb,
12555 		    SCTP_RESPONSE_TO_USER_REQ,
12556 		    mm, SCTP_SO_LOCKED);
12557 		/* now relock the stcb so everything is sane */
12558 		hold_tcblock = 0;
12559 		stcb = NULL;
12560 		/*
12561 		 * In this case top is already chained to mm avoid double
12562 		 * free, since we free it below if top != NULL and driver
12563 		 * would free it after sending the packet out
12564 		 */
12565 		if (sndlen != 0) {
12566 			top = NULL;
12567 		}
12568 		goto out_unlocked;
12569 	}
12570 	/* Calculate the maximum we can send */
12571 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12572 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12573 		if (non_blocking) {
12574 			/* we already checked for non-blocking above. */
12575 			max_len = sndlen;
12576 		} else {
12577 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12578 		}
12579 	} else {
12580 		max_len = 0;
12581 	}
12582 	if (hold_tcblock) {
12583 		SCTP_TCB_UNLOCK(stcb);
12584 		hold_tcblock = 0;
12585 	}
12586 	/* Is the stream no. valid? */
12587 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12588 		/* Invalid stream number */
12589 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12590 		error = EINVAL;
12591 		goto out_unlocked;
12592 	}
12593 	if (asoc->strmout == NULL) {
12594 		/* huh? software error */
12595 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12596 		error = EFAULT;
12597 		goto out_unlocked;
12598 	}
12599 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
12600 	if ((user_marks_eor == 0) &&
12601 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12602 		/* It will NEVER fit */
12603 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12604 		error = EMSGSIZE;
12605 		goto out_unlocked;
12606 	}
12607 	if ((uio == NULL) && user_marks_eor) {
12608 		/*-
12609 		 * We do not support eeor mode for
12610 		 * sending with mbuf chains (like sendfile).
12611 		 */
12612 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12613 		error = EINVAL;
12614 		goto out_unlocked;
12615 	}
12616 	if (user_marks_eor) {
12617 		local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12618 	} else {
12619 		/*-
12620 		 * For non-eeor the whole message must fit in
12621 		 * the socket send buffer.
12622 		 */
12623 		local_add_more = sndlen;
12624 	}
12625 	len = 0;
12626 	if (non_blocking) {
12627 		goto skip_preblock;
12628 	}
12629 	if (((max_len <= local_add_more) &&
12630 	    (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12631 	    (max_len == 0) ||
12632 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12633 		/* No room right now ! */
12634 		SOCKBUF_LOCK(&so->so_snd);
12635 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12636 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12637 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12638 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
12639 			    (unsigned int)SCTP_SB_LIMIT_SND(so),
12640 			    inqueue_bytes,
12641 			    local_add_more,
12642 			    stcb->asoc.stream_queue_cnt,
12643 			    stcb->asoc.chunks_on_out_queue,
12644 			    SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12645 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12646 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
12647 			}
12648 			be.error = 0;
12649 			stcb->block_entry = &be;
12650 			error = sbwait(&so->so_snd);
12651 			stcb->block_entry = NULL;
12652 			if (error || so->so_error || be.error) {
12653 				if (error == 0) {
12654 					if (so->so_error)
12655 						error = so->so_error;
12656 					if (be.error) {
12657 						error = be.error;
12658 					}
12659 				}
12660 				SOCKBUF_UNLOCK(&so->so_snd);
12661 				goto out_unlocked;
12662 			}
12663 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12664 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12665 				    so, asoc, stcb->asoc.total_output_queue_size);
12666 			}
12667 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12668 				goto out_unlocked;
12669 			}
12670 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12671 		}
12672 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12673 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12674 		} else {
12675 			max_len = 0;
12676 		}
12677 		SOCKBUF_UNLOCK(&so->so_snd);
12678 	}
12679 skip_preblock:
12680 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12681 		goto out_unlocked;
12682 	}
12683 	/*
12684 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12685 	 * case NOTE: uio will be null when top/mbuf is passed
12686 	 */
12687 	if (sndlen == 0) {
12688 		if (srcv->sinfo_flags & SCTP_EOF) {
12689 			got_all_of_the_send = 1;
12690 			goto dataless_eof;
12691 		} else {
12692 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12693 			error = EINVAL;
12694 			goto out;
12695 		}
12696 	}
12697 	if (top == NULL) {
12698 		struct sctp_stream_queue_pending *sp;
12699 		struct sctp_stream_out *strm;
12700 		uint32_t sndout;
12701 
12702 		SCTP_TCB_SEND_LOCK(stcb);
12703 		if ((asoc->stream_locked) &&
12704 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
12705 			SCTP_TCB_SEND_UNLOCK(stcb);
12706 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12707 			error = EINVAL;
12708 			goto out;
12709 		}
12710 		SCTP_TCB_SEND_UNLOCK(stcb);
12711 
12712 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12713 		if (strm->last_msg_incomplete == 0) {
12714 	do_a_copy_in:
12715 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12716 			if ((sp == NULL) || (error)) {
12717 				goto out;
12718 			}
12719 			SCTP_TCB_SEND_LOCK(stcb);
12720 			if (sp->msg_is_complete) {
12721 				strm->last_msg_incomplete = 0;
12722 				asoc->stream_locked = 0;
12723 			} else {
12724 				/*
12725 				 * Just got locked to this guy in case of an
12726 				 * interrupt.
12727 				 */
12728 				strm->last_msg_incomplete = 1;
12729 				asoc->stream_locked = 1;
12730 				asoc->stream_locked_on = srcv->sinfo_stream;
12731 				sp->sender_all_done = 0;
12732 			}
12733 			sctp_snd_sb_alloc(stcb, sp->length);
12734 			atomic_add_int(&asoc->stream_queue_cnt, 1);
12735 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12736 				sp->strseq = strm->next_sequence_sent;
12737 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
12738 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12739 					    (uintptr_t) stcb, sp->length,
12740 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12741 				}
12742 				strm->next_sequence_sent++;
12743 			} else {
12744 				SCTP_STAT_INCR(sctps_sends_with_unord);
12745 			}
12746 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12747 			stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1);
12748 			SCTP_TCB_SEND_UNLOCK(stcb);
12749 		} else {
12750 			SCTP_TCB_SEND_LOCK(stcb);
12751 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12752 			SCTP_TCB_SEND_UNLOCK(stcb);
12753 			if (sp == NULL) {
12754 				/* ???? Huh ??? last msg is gone */
12755 #ifdef INVARIANTS
12756 				panic("Warning: Last msg marked incomplete, yet nothing left?");
12757 #else
12758 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12759 				strm->last_msg_incomplete = 0;
12760 #endif
12761 				goto do_a_copy_in;
12762 
12763 			}
12764 		}
12765 		while (uio->uio_resid > 0) {
12766 			/* How much room do we have? */
12767 			struct mbuf *new_tail, *mm;
12768 
12769 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12770 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
12771 			else
12772 				max_len = 0;
12773 
12774 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
12775 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
12776 			    (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
12777 				sndout = 0;
12778 				new_tail = NULL;
12779 				if (hold_tcblock) {
12780 					SCTP_TCB_UNLOCK(stcb);
12781 					hold_tcblock = 0;
12782 				}
12783 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
12784 				if ((mm == NULL) || error) {
12785 					if (mm) {
12786 						sctp_m_freem(mm);
12787 					}
12788 					goto out;
12789 				}
12790 				/* Update the mbuf and count */
12791 				SCTP_TCB_SEND_LOCK(stcb);
12792 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12793 					/*
12794 					 * we need to get out. Peer probably
12795 					 * aborted.
12796 					 */
12797 					sctp_m_freem(mm);
12798 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
12799 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12800 						error = ECONNRESET;
12801 					}
12802 					SCTP_TCB_SEND_UNLOCK(stcb);
12803 					goto out;
12804 				}
12805 				if (sp->tail_mbuf) {
12806 					/* tack it to the end */
12807 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
12808 					sp->tail_mbuf = new_tail;
12809 				} else {
12810 					/* A stolen mbuf */
12811 					sp->data = mm;
12812 					sp->tail_mbuf = new_tail;
12813 				}
12814 				sctp_snd_sb_alloc(stcb, sndout);
12815 				atomic_add_int(&sp->length, sndout);
12816 				len += sndout;
12817 
12818 				/* Did we reach EOR? */
12819 				if ((uio->uio_resid == 0) &&
12820 				    ((user_marks_eor == 0) ||
12821 				    (srcv->sinfo_flags & SCTP_EOF) ||
12822 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12823 					sp->msg_is_complete = 1;
12824 				} else {
12825 					sp->msg_is_complete = 0;
12826 				}
12827 				SCTP_TCB_SEND_UNLOCK(stcb);
12828 			}
12829 			if (uio->uio_resid == 0) {
12830 				/* got it all? */
12831 				continue;
12832 			}
12833 			/* PR-SCTP? */
12834 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
12835 				/*
12836 				 * This is ugly but we must assure locking
12837 				 * order
12838 				 */
12839 				if (hold_tcblock == 0) {
12840 					SCTP_TCB_LOCK(stcb);
12841 					hold_tcblock = 1;
12842 				}
12843 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
12844 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12845 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12846 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12847 				else
12848 					max_len = 0;
12849 				if (max_len > 0) {
12850 					continue;
12851 				}
12852 				SCTP_TCB_UNLOCK(stcb);
12853 				hold_tcblock = 0;
12854 			}
12855 			/* wait for space now */
12856 			if (non_blocking) {
12857 				/* Non-blocking io in place out */
12858 				goto skip_out_eof;
12859 			}
12860 			/* What about the INIT, send it maybe */
12861 			if (queue_only_for_init) {
12862 				if (hold_tcblock == 0) {
12863 					SCTP_TCB_LOCK(stcb);
12864 					hold_tcblock = 1;
12865 				}
12866 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
12867 					/* a collision took us forward? */
12868 					queue_only = 0;
12869 				} else {
12870 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
12871 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12872 					queue_only = 1;
12873 				}
12874 			}
12875 			if ((net->flight_size > net->cwnd) &&
12876 			    (asoc->sctp_cmt_on_off == 0)) {
12877 				SCTP_STAT_INCR(sctps_send_cwnd_avoid);
12878 				queue_only = 1;
12879 			} else if (asoc->ifp_had_enobuf) {
12880 				SCTP_STAT_INCR(sctps_ifnomemqueued);
12881 				if (net->flight_size > (2 * net->mtu)) {
12882 					queue_only = 1;
12883 				}
12884 				asoc->ifp_had_enobuf = 0;
12885 			}
12886 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12887 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12888 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
12889 			    (stcb->asoc.total_flight > 0) &&
12890 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
12891 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
12892 
12893 				/*-
12894 				 * Ok, Nagle is set on and we have data outstanding.
12895 				 * Don't send anything and let SACKs drive out the
12896 				 * data unless wen have a "full" segment to send.
12897 				 */
12898 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12899 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
12900 				}
12901 				SCTP_STAT_INCR(sctps_naglequeued);
12902 				nagle_applies = 1;
12903 			} else {
12904 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
12905 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
12906 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
12907 				}
12908 				SCTP_STAT_INCR(sctps_naglesent);
12909 				nagle_applies = 0;
12910 			}
12911 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12912 
12913 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
12914 				    nagle_applies, un_sent);
12915 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
12916 				    stcb->asoc.total_flight,
12917 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
12918 			}
12919 			if (queue_only_for_init)
12920 				queue_only_for_init = 0;
12921 			if ((queue_only == 0) && (nagle_applies == 0)) {
12922 				/*-
12923 				 * need to start chunk output
12924 				 * before blocking.. note that if
12925 				 * a lock is already applied, then
12926 				 * the input via the net is happening
12927 				 * and I don't need to start output :-D
12928 				 */
12929 				if (hold_tcblock == 0) {
12930 					if (SCTP_TCB_TRYLOCK(stcb)) {
12931 						hold_tcblock = 1;
12932 						sctp_chunk_output(inp,
12933 						    stcb,
12934 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12935 					}
12936 				} else {
12937 					sctp_chunk_output(inp,
12938 					    stcb,
12939 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
12940 				}
12941 				if (hold_tcblock == 1) {
12942 					SCTP_TCB_UNLOCK(stcb);
12943 					hold_tcblock = 0;
12944 				}
12945 			}
12946 			SOCKBUF_LOCK(&so->so_snd);
12947 			/*-
12948 			 * This is a bit strange, but I think it will
12949 			 * work. The total_output_queue_size is locked and
12950 			 * protected by the TCB_LOCK, which we just released.
12951 			 * There is a race that can occur between releasing it
12952 			 * above, and me getting the socket lock, where sacks
12953 			 * come in but we have not put the SB_WAIT on the
12954 			 * so_snd buffer to get the wakeup. After the LOCK
12955 			 * is applied the sack_processing will also need to
12956 			 * LOCK the so->so_snd to do the actual sowwakeup(). So
12957 			 * once we have the socket buffer lock if we recheck the
12958 			 * size we KNOW we will get to sleep safely with the
12959 			 * wakeup flag in place.
12960 			 */
12961 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
12962 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
12963 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12964 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
12965 					    so, asoc, uio->uio_resid);
12966 				}
12967 				be.error = 0;
12968 				stcb->block_entry = &be;
12969 				error = sbwait(&so->so_snd);
12970 				stcb->block_entry = NULL;
12971 
12972 				if (error || so->so_error || be.error) {
12973 					if (error == 0) {
12974 						if (so->so_error)
12975 							error = so->so_error;
12976 						if (be.error) {
12977 							error = be.error;
12978 						}
12979 					}
12980 					SOCKBUF_UNLOCK(&so->so_snd);
12981 					goto out_unlocked;
12982 				}
12983 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12984 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12985 					    so, asoc, stcb->asoc.total_output_queue_size);
12986 				}
12987 			}
12988 			SOCKBUF_UNLOCK(&so->so_snd);
12989 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12990 				goto out_unlocked;
12991 			}
12992 		}
12993 		SCTP_TCB_SEND_LOCK(stcb);
12994 		if (sp) {
12995 			if (sp->msg_is_complete == 0) {
12996 				strm->last_msg_incomplete = 1;
12997 				asoc->stream_locked = 1;
12998 				asoc->stream_locked_on = srcv->sinfo_stream;
12999 			} else {
13000 				sp->sender_all_done = 1;
13001 				strm->last_msg_incomplete = 0;
13002 				asoc->stream_locked = 0;
13003 			}
13004 		} else {
13005 			SCTP_PRINTF("Huh no sp TSNH?\n");
13006 			strm->last_msg_incomplete = 0;
13007 			asoc->stream_locked = 0;
13008 		}
13009 		SCTP_TCB_SEND_UNLOCK(stcb);
13010 		if (uio->uio_resid == 0) {
13011 			got_all_of_the_send = 1;
13012 		}
13013 	} else {
13014 		/* We send in a 0, since we do NOT have any locks */
13015 		error = sctp_msg_append(stcb, net, top, srcv, 0);
13016 		top = NULL;
13017 		if (srcv->sinfo_flags & SCTP_EOF) {
13018 			/*
13019 			 * This should only happen for Panda for the mbuf
13020 			 * send case, which does NOT yet support EEOR mode.
13021 			 * Thus, we can just set this flag to do the proper
13022 			 * EOF handling.
13023 			 */
13024 			got_all_of_the_send = 1;
13025 		}
13026 	}
13027 	if (error) {
13028 		goto out;
13029 	}
13030 dataless_eof:
13031 	/* EOF thing ? */
13032 	if ((srcv->sinfo_flags & SCTP_EOF) &&
13033 	    (got_all_of_the_send == 1) &&
13034 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13035 		int cnt;
13036 
13037 		SCTP_STAT_INCR(sctps_sends_with_eof);
13038 		error = 0;
13039 		if (hold_tcblock == 0) {
13040 			SCTP_TCB_LOCK(stcb);
13041 			hold_tcblock = 1;
13042 		}
13043 		cnt = sctp_is_there_unsent_data(stcb);
13044 		if (TAILQ_EMPTY(&asoc->send_queue) &&
13045 		    TAILQ_EMPTY(&asoc->sent_queue) &&
13046 		    (cnt == 0)) {
13047 			if (asoc->locked_on_sending) {
13048 				goto abort_anyway;
13049 			}
13050 			/* there is nothing queued to send, so I'm done... */
13051 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13052 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13053 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13054 				/* only send SHUTDOWN the first time through */
13055 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
13056 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13057 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13058 				}
13059 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13060 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13061 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13062 				    asoc->primary_destination);
13063 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13064 				    asoc->primary_destination);
13065 			}
13066 		} else {
13067 			/*-
13068 			 * we still got (or just got) data to send, so set
13069 			 * SHUTDOWN_PENDING
13070 			 */
13071 			/*-
13072 			 * XXX sockets draft says that SCTP_EOF should be
13073 			 * sent with no data.  currently, we will allow user
13074 			 * data to be sent first and move to
13075 			 * SHUTDOWN-PENDING
13076 			 */
13077 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13078 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13079 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13080 				if (hold_tcblock == 0) {
13081 					SCTP_TCB_LOCK(stcb);
13082 					hold_tcblock = 1;
13083 				}
13084 				if (asoc->locked_on_sending) {
13085 					/* Locked to send out the data */
13086 					struct sctp_stream_queue_pending *sp;
13087 
13088 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13089 					if (sp) {
13090 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
13091 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13092 					}
13093 				}
13094 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13095 				if (TAILQ_EMPTY(&asoc->send_queue) &&
13096 				    TAILQ_EMPTY(&asoc->sent_queue) &&
13097 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13098 			abort_anyway:
13099 					if (free_cnt_applied) {
13100 						atomic_add_int(&stcb->asoc.refcnt, -1);
13101 						free_cnt_applied = 0;
13102 					}
13103 					sctp_abort_an_association(stcb->sctp_ep, stcb,
13104 					    SCTP_RESPONSE_TO_USER_REQ,
13105 					    NULL, SCTP_SO_LOCKED);
13106 					/*
13107 					 * now relock the stcb so everything
13108 					 * is sane
13109 					 */
13110 					hold_tcblock = 0;
13111 					stcb = NULL;
13112 					goto out;
13113 				}
13114 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13115 				    asoc->primary_destination);
13116 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13117 			}
13118 		}
13119 	}
13120 skip_out_eof:
13121 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13122 		some_on_control = 1;
13123 	}
13124 	if (queue_only_for_init) {
13125 		if (hold_tcblock == 0) {
13126 			SCTP_TCB_LOCK(stcb);
13127 			hold_tcblock = 1;
13128 		}
13129 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13130 			/* a collision took us forward? */
13131 			queue_only = 0;
13132 		} else {
13133 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13134 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13135 			queue_only = 1;
13136 		}
13137 	}
13138 	if ((net->flight_size > net->cwnd) &&
13139 	    (stcb->asoc.sctp_cmt_on_off == 0)) {
13140 		SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13141 		queue_only = 1;
13142 	} else if (asoc->ifp_had_enobuf) {
13143 		SCTP_STAT_INCR(sctps_ifnomemqueued);
13144 		if (net->flight_size > (2 * net->mtu)) {
13145 			queue_only = 1;
13146 		}
13147 		asoc->ifp_had_enobuf = 0;
13148 	}
13149 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13150 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13151 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13152 	    (stcb->asoc.total_flight > 0) &&
13153 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13154 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13155 		/*-
13156 		 * Ok, Nagle is set on and we have data outstanding.
13157 		 * Don't send anything and let SACKs drive out the
13158 		 * data unless wen have a "full" segment to send.
13159 		 */
13160 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13161 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13162 		}
13163 		SCTP_STAT_INCR(sctps_naglequeued);
13164 		nagle_applies = 1;
13165 	} else {
13166 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13167 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13168 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13169 		}
13170 		SCTP_STAT_INCR(sctps_naglesent);
13171 		nagle_applies = 0;
13172 	}
13173 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13174 		sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13175 		    nagle_applies, un_sent);
13176 		sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13177 		    stcb->asoc.total_flight,
13178 		    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13179 	}
13180 	if (queue_only_for_init)
13181 		queue_only_for_init = 0;
13182 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13183 		/* we can attempt to send too. */
13184 		if (hold_tcblock == 0) {
13185 			/*
13186 			 * If there is activity recv'ing sacks no need to
13187 			 * send
13188 			 */
13189 			if (SCTP_TCB_TRYLOCK(stcb)) {
13190 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13191 				hold_tcblock = 1;
13192 			}
13193 		} else {
13194 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13195 		}
13196 	} else if ((queue_only == 0) &&
13197 		    (stcb->asoc.peers_rwnd == 0) &&
13198 	    (stcb->asoc.total_flight == 0)) {
13199 		/* We get to have a probe outstanding */
13200 		if (hold_tcblock == 0) {
13201 			hold_tcblock = 1;
13202 			SCTP_TCB_LOCK(stcb);
13203 		}
13204 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13205 	} else if (some_on_control) {
13206 		int num_out, reason, frag_point;
13207 
13208 		/* Here we do control only */
13209 		if (hold_tcblock == 0) {
13210 			hold_tcblock = 1;
13211 			SCTP_TCB_LOCK(stcb);
13212 		}
13213 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13214 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13215 		    &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13216 	}
13217 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13218 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
13219 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13220 	    stcb->asoc.total_output_queue_size, error);
13221 
13222 out:
13223 out_unlocked:
13224 
13225 	if (local_soresv && stcb) {
13226 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13227 		local_soresv = 0;
13228 	}
13229 	if (create_lock_applied) {
13230 		SCTP_ASOC_CREATE_UNLOCK(inp);
13231 		create_lock_applied = 0;
13232 	}
13233 	if ((stcb) && hold_tcblock) {
13234 		SCTP_TCB_UNLOCK(stcb);
13235 	}
13236 	if (stcb && free_cnt_applied) {
13237 		atomic_add_int(&stcb->asoc.refcnt, -1);
13238 	}
13239 #ifdef INVARIANTS
13240 	if (stcb) {
13241 		if (mtx_owned(&stcb->tcb_mtx)) {
13242 			panic("Leaving with tcb mtx owned?");
13243 		}
13244 		if (mtx_owned(&stcb->tcb_send_mtx)) {
13245 			panic("Leaving with tcb send mtx owned?");
13246 		}
13247 	}
13248 #endif
13249 #ifdef INVARIANTS
13250 	if (inp) {
13251 		sctp_validate_no_locks(inp);
13252 	} else {
13253 		printf("Warning - inp is NULL so cant validate locks\n");
13254 	}
13255 #endif
13256 	if (top) {
13257 		sctp_m_freem(top);
13258 	}
13259 	if (control) {
13260 		sctp_m_freem(control);
13261 	}
13262 	return (error);
13263 }
13264 
13265 
13266 /*
13267  * generate an AUTHentication chunk, if required
13268  */
13269 struct mbuf *
13270 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13271     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13272     struct sctp_tcb *stcb, uint8_t chunk)
13273 {
13274 	struct mbuf *m_auth;
13275 	struct sctp_auth_chunk *auth;
13276 	int chunk_len;
13277 
13278 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13279 	    (stcb == NULL))
13280 		return (m);
13281 
13282 	/* sysctl disabled auth? */
13283 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13284 		return (m);
13285 
13286 	/* peer doesn't do auth... */
13287 	if (!stcb->asoc.peer_supports_auth) {
13288 		return (m);
13289 	}
13290 	/* does the requested chunk require auth? */
13291 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13292 		return (m);
13293 	}
13294 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13295 	if (m_auth == NULL) {
13296 		/* no mbuf's */
13297 		return (m);
13298 	}
13299 	/* reserve some space if this will be the first mbuf */
13300 	if (m == NULL)
13301 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13302 	/* fill in the AUTH chunk details */
13303 	auth = mtod(m_auth, struct sctp_auth_chunk *);
13304 	bzero(auth, sizeof(*auth));
13305 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
13306 	auth->ch.chunk_flags = 0;
13307 	chunk_len = sizeof(*auth) +
13308 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13309 	auth->ch.chunk_length = htons(chunk_len);
13310 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13311 	/* key id and hmac digest will be computed and filled in upon send */
13312 
13313 	/* save the offset where the auth was inserted into the chain */
13314 	if (m != NULL) {
13315 		struct mbuf *cn;
13316 
13317 		*offset = 0;
13318 		cn = m;
13319 		while (cn) {
13320 			*offset += SCTP_BUF_LEN(cn);
13321 			cn = SCTP_BUF_NEXT(cn);
13322 		}
13323 	} else
13324 		*offset = 0;
13325 
13326 	/* update length and return pointer to the auth chunk */
13327 	SCTP_BUF_LEN(m_auth) = chunk_len;
13328 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13329 	if (auth_ret != NULL)
13330 		*auth_ret = auth;
13331 
13332 	return (m);
13333 }
13334 
13335 #ifdef INET6
13336 int
13337 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13338 {
13339 	struct nd_prefix *pfx = NULL;
13340 	struct nd_pfxrouter *pfxrtr = NULL;
13341 	struct sockaddr_in6 gw6;
13342 
13343 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13344 		return (0);
13345 
13346 	/* get prefix entry of address */
13347 	LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13348 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13349 			continue;
13350 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13351 		    &src6->sin6_addr, &pfx->ndpr_mask))
13352 			break;
13353 	}
13354 	/* no prefix entry in the prefix list */
13355 	if (pfx == NULL) {
13356 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13357 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13358 		return (0);
13359 	}
13360 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13361 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13362 
13363 	/* search installed gateway from prefix entry */
13364 	for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13365 	    pfxrtr->pfr_next) {
13366 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
13367 		gw6.sin6_family = AF_INET6;
13368 		gw6.sin6_len = sizeof(struct sockaddr_in6);
13369 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13370 		    sizeof(struct in6_addr));
13371 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13372 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13373 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13374 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13375 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
13376 		    ro->ro_rt->rt_gateway)) {
13377 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13378 			return (1);
13379 		}
13380 	}
13381 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13382 	return (0);
13383 }
13384 
13385 #endif
13386 
13387 int
13388 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13389 {
13390 	struct sockaddr_in *sin, *mask;
13391 	struct ifaddr *ifa;
13392 	struct in_addr srcnetaddr, gwnetaddr;
13393 
13394 	if (ro == NULL || ro->ro_rt == NULL ||
13395 	    sifa->address.sa.sa_family != AF_INET) {
13396 		return (0);
13397 	}
13398 	ifa = (struct ifaddr *)sifa->ifa;
13399 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13400 	sin = (struct sockaddr_in *)&sifa->address.sin;
13401 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13402 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13403 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13404 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13405 
13406 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13407 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13408 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13409 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13410 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13411 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13412 		return (1);
13413 	}
13414 	return (0);
13415 }
13416