xref: /freebsd/sys/netinet/sctp_output.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
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 /* EY  below are nr_sacks version of the preceeding two data structures, identical except their names */
1863 #define SCTP_MAX_NR_GAPS_INARRAY 4
1864 struct nr_sack_track {
1865 	uint8_t right_edge;	/* mergable on the right edge */
1866 	uint8_t left_edge;	/* mergable on the left edge */
1867 	uint8_t num_entries;
1868 	uint8_t spare;
1869 	struct sctp_nr_gap_ack_block nr_gaps[SCTP_MAX_NR_GAPS_INARRAY];
1870 };
1871 
1872 struct nr_sack_track nr_sack_array[256] = {
1873 	{0, 0, 0, 0,		/* 0x00 */
1874 		{{0, 0},
1875 		{0, 0},
1876 		{0, 0},
1877 		{0, 0}
1878 		}
1879 	},
1880 	{1, 0, 1, 0,		/* 0x01 */
1881 		{{0, 0},
1882 		{0, 0},
1883 		{0, 0},
1884 		{0, 0}
1885 		}
1886 	},
1887 	{0, 0, 1, 0,		/* 0x02 */
1888 		{{1, 1},
1889 		{0, 0},
1890 		{0, 0},
1891 		{0, 0}
1892 		}
1893 	},
1894 	{1, 0, 1, 0,		/* 0x03 */
1895 		{{0, 1},
1896 		{0, 0},
1897 		{0, 0},
1898 		{0, 0}
1899 		}
1900 	},
1901 	{0, 0, 1, 0,		/* 0x04 */
1902 		{{2, 2},
1903 		{0, 0},
1904 		{0, 0},
1905 		{0, 0}
1906 		}
1907 	},
1908 	{1, 0, 2, 0,		/* 0x05 */
1909 		{{0, 0},
1910 		{2, 2},
1911 		{0, 0},
1912 		{0, 0}
1913 		}
1914 	},
1915 	{0, 0, 1, 0,		/* 0x06 */
1916 		{{1, 2},
1917 		{0, 0},
1918 		{0, 0},
1919 		{0, 0}
1920 		}
1921 	},
1922 	{1, 0, 1, 0,		/* 0x07 */
1923 		{{0, 2},
1924 		{0, 0},
1925 		{0, 0},
1926 		{0, 0}
1927 		}
1928 	},
1929 	{0, 0, 1, 0,		/* 0x08 */
1930 		{{3, 3},
1931 		{0, 0},
1932 		{0, 0},
1933 		{0, 0}
1934 		}
1935 	},
1936 	{1, 0, 2, 0,		/* 0x09 */
1937 		{{0, 0},
1938 		{3, 3},
1939 		{0, 0},
1940 		{0, 0}
1941 		}
1942 	},
1943 	{0, 0, 2, 0,		/* 0x0a */
1944 		{{1, 1},
1945 		{3, 3},
1946 		{0, 0},
1947 		{0, 0}
1948 		}
1949 	},
1950 	{1, 0, 2, 0,		/* 0x0b */
1951 		{{0, 1},
1952 		{3, 3},
1953 		{0, 0},
1954 		{0, 0}
1955 		}
1956 	},
1957 	{0, 0, 1, 0,		/* 0x0c */
1958 		{{2, 3},
1959 		{0, 0},
1960 		{0, 0},
1961 		{0, 0}
1962 		}
1963 	},
1964 	{1, 0, 2, 0,		/* 0x0d */
1965 		{{0, 0},
1966 		{2, 3},
1967 		{0, 0},
1968 		{0, 0}
1969 		}
1970 	},
1971 	{0, 0, 1, 0,		/* 0x0e */
1972 		{{1, 3},
1973 		{0, 0},
1974 		{0, 0},
1975 		{0, 0}
1976 		}
1977 	},
1978 	{1, 0, 1, 0,		/* 0x0f */
1979 		{{0, 3},
1980 		{0, 0},
1981 		{0, 0},
1982 		{0, 0}
1983 		}
1984 	},
1985 	{0, 0, 1, 0,		/* 0x10 */
1986 		{{4, 4},
1987 		{0, 0},
1988 		{0, 0},
1989 		{0, 0}
1990 		}
1991 	},
1992 	{1, 0, 2, 0,		/* 0x11 */
1993 		{{0, 0},
1994 		{4, 4},
1995 		{0, 0},
1996 		{0, 0}
1997 		}
1998 	},
1999 	{0, 0, 2, 0,		/* 0x12 */
2000 		{{1, 1},
2001 		{4, 4},
2002 		{0, 0},
2003 		{0, 0}
2004 		}
2005 	},
2006 	{1, 0, 2, 0,		/* 0x13 */
2007 		{{0, 1},
2008 		{4, 4},
2009 		{0, 0},
2010 		{0, 0}
2011 		}
2012 	},
2013 	{0, 0, 2, 0,		/* 0x14 */
2014 		{{2, 2},
2015 		{4, 4},
2016 		{0, 0},
2017 		{0, 0}
2018 		}
2019 	},
2020 	{1, 0, 3, 0,		/* 0x15 */
2021 		{{0, 0},
2022 		{2, 2},
2023 		{4, 4},
2024 		{0, 0}
2025 		}
2026 	},
2027 	{0, 0, 2, 0,		/* 0x16 */
2028 		{{1, 2},
2029 		{4, 4},
2030 		{0, 0},
2031 		{0, 0}
2032 		}
2033 	},
2034 	{1, 0, 2, 0,		/* 0x17 */
2035 		{{0, 2},
2036 		{4, 4},
2037 		{0, 0},
2038 		{0, 0}
2039 		}
2040 	},
2041 	{0, 0, 1, 0,		/* 0x18 */
2042 		{{3, 4},
2043 		{0, 0},
2044 		{0, 0},
2045 		{0, 0}
2046 		}
2047 	},
2048 	{1, 0, 2, 0,		/* 0x19 */
2049 		{{0, 0},
2050 		{3, 4},
2051 		{0, 0},
2052 		{0, 0}
2053 		}
2054 	},
2055 	{0, 0, 2, 0,		/* 0x1a */
2056 		{{1, 1},
2057 		{3, 4},
2058 		{0, 0},
2059 		{0, 0}
2060 		}
2061 	},
2062 	{1, 0, 2, 0,		/* 0x1b */
2063 		{{0, 1},
2064 		{3, 4},
2065 		{0, 0},
2066 		{0, 0}
2067 		}
2068 	},
2069 	{0, 0, 1, 0,		/* 0x1c */
2070 		{{2, 4},
2071 		{0, 0},
2072 		{0, 0},
2073 		{0, 0}
2074 		}
2075 	},
2076 	{1, 0, 2, 0,		/* 0x1d */
2077 		{{0, 0},
2078 		{2, 4},
2079 		{0, 0},
2080 		{0, 0}
2081 		}
2082 	},
2083 	{0, 0, 1, 0,		/* 0x1e */
2084 		{{1, 4},
2085 		{0, 0},
2086 		{0, 0},
2087 		{0, 0}
2088 		}
2089 	},
2090 	{1, 0, 1, 0,		/* 0x1f */
2091 		{{0, 4},
2092 		{0, 0},
2093 		{0, 0},
2094 		{0, 0}
2095 		}
2096 	},
2097 	{0, 0, 1, 0,		/* 0x20 */
2098 		{{5, 5},
2099 		{0, 0},
2100 		{0, 0},
2101 		{0, 0}
2102 		}
2103 	},
2104 	{1, 0, 2, 0,		/* 0x21 */
2105 		{{0, 0},
2106 		{5, 5},
2107 		{0, 0},
2108 		{0, 0}
2109 		}
2110 	},
2111 	{0, 0, 2, 0,		/* 0x22 */
2112 		{{1, 1},
2113 		{5, 5},
2114 		{0, 0},
2115 		{0, 0}
2116 		}
2117 	},
2118 	{1, 0, 2, 0,		/* 0x23 */
2119 		{{0, 1},
2120 		{5, 5},
2121 		{0, 0},
2122 		{0, 0}
2123 		}
2124 	},
2125 	{0, 0, 2, 0,		/* 0x24 */
2126 		{{2, 2},
2127 		{5, 5},
2128 		{0, 0},
2129 		{0, 0}
2130 		}
2131 	},
2132 	{1, 0, 3, 0,		/* 0x25 */
2133 		{{0, 0},
2134 		{2, 2},
2135 		{5, 5},
2136 		{0, 0}
2137 		}
2138 	},
2139 	{0, 0, 2, 0,		/* 0x26 */
2140 		{{1, 2},
2141 		{5, 5},
2142 		{0, 0},
2143 		{0, 0}
2144 		}
2145 	},
2146 	{1, 0, 2, 0,		/* 0x27 */
2147 		{{0, 2},
2148 		{5, 5},
2149 		{0, 0},
2150 		{0, 0}
2151 		}
2152 	},
2153 	{0, 0, 2, 0,		/* 0x28 */
2154 		{{3, 3},
2155 		{5, 5},
2156 		{0, 0},
2157 		{0, 0}
2158 		}
2159 	},
2160 	{1, 0, 3, 0,		/* 0x29 */
2161 		{{0, 0},
2162 		{3, 3},
2163 		{5, 5},
2164 		{0, 0}
2165 		}
2166 	},
2167 	{0, 0, 3, 0,		/* 0x2a */
2168 		{{1, 1},
2169 		{3, 3},
2170 		{5, 5},
2171 		{0, 0}
2172 		}
2173 	},
2174 	{1, 0, 3, 0,		/* 0x2b */
2175 		{{0, 1},
2176 		{3, 3},
2177 		{5, 5},
2178 		{0, 0}
2179 		}
2180 	},
2181 	{0, 0, 2, 0,		/* 0x2c */
2182 		{{2, 3},
2183 		{5, 5},
2184 		{0, 0},
2185 		{0, 0}
2186 		}
2187 	},
2188 	{1, 0, 3, 0,		/* 0x2d */
2189 		{{0, 0},
2190 		{2, 3},
2191 		{5, 5},
2192 		{0, 0}
2193 		}
2194 	},
2195 	{0, 0, 2, 0,		/* 0x2e */
2196 		{{1, 3},
2197 		{5, 5},
2198 		{0, 0},
2199 		{0, 0}
2200 		}
2201 	},
2202 	{1, 0, 2, 0,		/* 0x2f */
2203 		{{0, 3},
2204 		{5, 5},
2205 		{0, 0},
2206 		{0, 0}
2207 		}
2208 	},
2209 	{0, 0, 1, 0,		/* 0x30 */
2210 		{{4, 5},
2211 		{0, 0},
2212 		{0, 0},
2213 		{0, 0}
2214 		}
2215 	},
2216 	{1, 0, 2, 0,		/* 0x31 */
2217 		{{0, 0},
2218 		{4, 5},
2219 		{0, 0},
2220 		{0, 0}
2221 		}
2222 	},
2223 	{0, 0, 2, 0,		/* 0x32 */
2224 		{{1, 1},
2225 		{4, 5},
2226 		{0, 0},
2227 		{0, 0}
2228 		}
2229 	},
2230 	{1, 0, 2, 0,		/* 0x33 */
2231 		{{0, 1},
2232 		{4, 5},
2233 		{0, 0},
2234 		{0, 0}
2235 		}
2236 	},
2237 	{0, 0, 2, 0,		/* 0x34 */
2238 		{{2, 2},
2239 		{4, 5},
2240 		{0, 0},
2241 		{0, 0}
2242 		}
2243 	},
2244 	{1, 0, 3, 0,		/* 0x35 */
2245 		{{0, 0},
2246 		{2, 2},
2247 		{4, 5},
2248 		{0, 0}
2249 		}
2250 	},
2251 	{0, 0, 2, 0,		/* 0x36 */
2252 		{{1, 2},
2253 		{4, 5},
2254 		{0, 0},
2255 		{0, 0}
2256 		}
2257 	},
2258 	{1, 0, 2, 0,		/* 0x37 */
2259 		{{0, 2},
2260 		{4, 5},
2261 		{0, 0},
2262 		{0, 0}
2263 		}
2264 	},
2265 	{0, 0, 1, 0,		/* 0x38 */
2266 		{{3, 5},
2267 		{0, 0},
2268 		{0, 0},
2269 		{0, 0}
2270 		}
2271 	},
2272 	{1, 0, 2, 0,		/* 0x39 */
2273 		{{0, 0},
2274 		{3, 5},
2275 		{0, 0},
2276 		{0, 0}
2277 		}
2278 	},
2279 	{0, 0, 2, 0,		/* 0x3a */
2280 		{{1, 1},
2281 		{3, 5},
2282 		{0, 0},
2283 		{0, 0}
2284 		}
2285 	},
2286 	{1, 0, 2, 0,		/* 0x3b */
2287 		{{0, 1},
2288 		{3, 5},
2289 		{0, 0},
2290 		{0, 0}
2291 		}
2292 	},
2293 	{0, 0, 1, 0,		/* 0x3c */
2294 		{{2, 5},
2295 		{0, 0},
2296 		{0, 0},
2297 		{0, 0}
2298 		}
2299 	},
2300 	{1, 0, 2, 0,		/* 0x3d */
2301 		{{0, 0},
2302 		{2, 5},
2303 		{0, 0},
2304 		{0, 0}
2305 		}
2306 	},
2307 	{0, 0, 1, 0,		/* 0x3e */
2308 		{{1, 5},
2309 		{0, 0},
2310 		{0, 0},
2311 		{0, 0}
2312 		}
2313 	},
2314 	{1, 0, 1, 0,		/* 0x3f */
2315 		{{0, 5},
2316 		{0, 0},
2317 		{0, 0},
2318 		{0, 0}
2319 		}
2320 	},
2321 	{0, 0, 1, 0,		/* 0x40 */
2322 		{{6, 6},
2323 		{0, 0},
2324 		{0, 0},
2325 		{0, 0}
2326 		}
2327 	},
2328 	{1, 0, 2, 0,		/* 0x41 */
2329 		{{0, 0},
2330 		{6, 6},
2331 		{0, 0},
2332 		{0, 0}
2333 		}
2334 	},
2335 	{0, 0, 2, 0,		/* 0x42 */
2336 		{{1, 1},
2337 		{6, 6},
2338 		{0, 0},
2339 		{0, 0}
2340 		}
2341 	},
2342 	{1, 0, 2, 0,		/* 0x43 */
2343 		{{0, 1},
2344 		{6, 6},
2345 		{0, 0},
2346 		{0, 0}
2347 		}
2348 	},
2349 	{0, 0, 2, 0,		/* 0x44 */
2350 		{{2, 2},
2351 		{6, 6},
2352 		{0, 0},
2353 		{0, 0}
2354 		}
2355 	},
2356 	{1, 0, 3, 0,		/* 0x45 */
2357 		{{0, 0},
2358 		{2, 2},
2359 		{6, 6},
2360 		{0, 0}
2361 		}
2362 	},
2363 	{0, 0, 2, 0,		/* 0x46 */
2364 		{{1, 2},
2365 		{6, 6},
2366 		{0, 0},
2367 		{0, 0}
2368 		}
2369 	},
2370 	{1, 0, 2, 0,		/* 0x47 */
2371 		{{0, 2},
2372 		{6, 6},
2373 		{0, 0},
2374 		{0, 0}
2375 		}
2376 	},
2377 	{0, 0, 2, 0,		/* 0x48 */
2378 		{{3, 3},
2379 		{6, 6},
2380 		{0, 0},
2381 		{0, 0}
2382 		}
2383 	},
2384 	{1, 0, 3, 0,		/* 0x49 */
2385 		{{0, 0},
2386 		{3, 3},
2387 		{6, 6},
2388 		{0, 0}
2389 		}
2390 	},
2391 	{0, 0, 3, 0,		/* 0x4a */
2392 		{{1, 1},
2393 		{3, 3},
2394 		{6, 6},
2395 		{0, 0}
2396 		}
2397 	},
2398 	{1, 0, 3, 0,		/* 0x4b */
2399 		{{0, 1},
2400 		{3, 3},
2401 		{6, 6},
2402 		{0, 0}
2403 		}
2404 	},
2405 	{0, 0, 2, 0,		/* 0x4c */
2406 		{{2, 3},
2407 		{6, 6},
2408 		{0, 0},
2409 		{0, 0}
2410 		}
2411 	},
2412 	{1, 0, 3, 0,		/* 0x4d */
2413 		{{0, 0},
2414 		{2, 3},
2415 		{6, 6},
2416 		{0, 0}
2417 		}
2418 	},
2419 	{0, 0, 2, 0,		/* 0x4e */
2420 		{{1, 3},
2421 		{6, 6},
2422 		{0, 0},
2423 		{0, 0}
2424 		}
2425 	},
2426 	{1, 0, 2, 0,		/* 0x4f */
2427 		{{0, 3},
2428 		{6, 6},
2429 		{0, 0},
2430 		{0, 0}
2431 		}
2432 	},
2433 	{0, 0, 2, 0,		/* 0x50 */
2434 		{{4, 4},
2435 		{6, 6},
2436 		{0, 0},
2437 		{0, 0}
2438 		}
2439 	},
2440 	{1, 0, 3, 0,		/* 0x51 */
2441 		{{0, 0},
2442 		{4, 4},
2443 		{6, 6},
2444 		{0, 0}
2445 		}
2446 	},
2447 	{0, 0, 3, 0,		/* 0x52 */
2448 		{{1, 1},
2449 		{4, 4},
2450 		{6, 6},
2451 		{0, 0}
2452 		}
2453 	},
2454 	{1, 0, 3, 0,		/* 0x53 */
2455 		{{0, 1},
2456 		{4, 4},
2457 		{6, 6},
2458 		{0, 0}
2459 		}
2460 	},
2461 	{0, 0, 3, 0,		/* 0x54 */
2462 		{{2, 2},
2463 		{4, 4},
2464 		{6, 6},
2465 		{0, 0}
2466 		}
2467 	},
2468 	{1, 0, 4, 0,		/* 0x55 */
2469 		{{0, 0},
2470 		{2, 2},
2471 		{4, 4},
2472 		{6, 6}
2473 		}
2474 	},
2475 	{0, 0, 3, 0,		/* 0x56 */
2476 		{{1, 2},
2477 		{4, 4},
2478 		{6, 6},
2479 		{0, 0}
2480 		}
2481 	},
2482 	{1, 0, 3, 0,		/* 0x57 */
2483 		{{0, 2},
2484 		{4, 4},
2485 		{6, 6},
2486 		{0, 0}
2487 		}
2488 	},
2489 	{0, 0, 2, 0,		/* 0x58 */
2490 		{{3, 4},
2491 		{6, 6},
2492 		{0, 0},
2493 		{0, 0}
2494 		}
2495 	},
2496 	{1, 0, 3, 0,		/* 0x59 */
2497 		{{0, 0},
2498 		{3, 4},
2499 		{6, 6},
2500 		{0, 0}
2501 		}
2502 	},
2503 	{0, 0, 3, 0,		/* 0x5a */
2504 		{{1, 1},
2505 		{3, 4},
2506 		{6, 6},
2507 		{0, 0}
2508 		}
2509 	},
2510 	{1, 0, 3, 0,		/* 0x5b */
2511 		{{0, 1},
2512 		{3, 4},
2513 		{6, 6},
2514 		{0, 0}
2515 		}
2516 	},
2517 	{0, 0, 2, 0,		/* 0x5c */
2518 		{{2, 4},
2519 		{6, 6},
2520 		{0, 0},
2521 		{0, 0}
2522 		}
2523 	},
2524 	{1, 0, 3, 0,		/* 0x5d */
2525 		{{0, 0},
2526 		{2, 4},
2527 		{6, 6},
2528 		{0, 0}
2529 		}
2530 	},
2531 	{0, 0, 2, 0,		/* 0x5e */
2532 		{{1, 4},
2533 		{6, 6},
2534 		{0, 0},
2535 		{0, 0}
2536 		}
2537 	},
2538 	{1, 0, 2, 0,		/* 0x5f */
2539 		{{0, 4},
2540 		{6, 6},
2541 		{0, 0},
2542 		{0, 0}
2543 		}
2544 	},
2545 	{0, 0, 1, 0,		/* 0x60 */
2546 		{{5, 6},
2547 		{0, 0},
2548 		{0, 0},
2549 		{0, 0}
2550 		}
2551 	},
2552 	{1, 0, 2, 0,		/* 0x61 */
2553 		{{0, 0},
2554 		{5, 6},
2555 		{0, 0},
2556 		{0, 0}
2557 		}
2558 	},
2559 	{0, 0, 2, 0,		/* 0x62 */
2560 		{{1, 1},
2561 		{5, 6},
2562 		{0, 0},
2563 		{0, 0}
2564 		}
2565 	},
2566 	{1, 0, 2, 0,		/* 0x63 */
2567 		{{0, 1},
2568 		{5, 6},
2569 		{0, 0},
2570 		{0, 0}
2571 		}
2572 	},
2573 	{0, 0, 2, 0,		/* 0x64 */
2574 		{{2, 2},
2575 		{5, 6},
2576 		{0, 0},
2577 		{0, 0}
2578 		}
2579 	},
2580 	{1, 0, 3, 0,		/* 0x65 */
2581 		{{0, 0},
2582 		{2, 2},
2583 		{5, 6},
2584 		{0, 0}
2585 		}
2586 	},
2587 	{0, 0, 2, 0,		/* 0x66 */
2588 		{{1, 2},
2589 		{5, 6},
2590 		{0, 0},
2591 		{0, 0}
2592 		}
2593 	},
2594 	{1, 0, 2, 0,		/* 0x67 */
2595 		{{0, 2},
2596 		{5, 6},
2597 		{0, 0},
2598 		{0, 0}
2599 		}
2600 	},
2601 	{0, 0, 2, 0,		/* 0x68 */
2602 		{{3, 3},
2603 		{5, 6},
2604 		{0, 0},
2605 		{0, 0}
2606 		}
2607 	},
2608 	{1, 0, 3, 0,		/* 0x69 */
2609 		{{0, 0},
2610 		{3, 3},
2611 		{5, 6},
2612 		{0, 0}
2613 		}
2614 	},
2615 	{0, 0, 3, 0,		/* 0x6a */
2616 		{{1, 1},
2617 		{3, 3},
2618 		{5, 6},
2619 		{0, 0}
2620 		}
2621 	},
2622 	{1, 0, 3, 0,		/* 0x6b */
2623 		{{0, 1},
2624 		{3, 3},
2625 		{5, 6},
2626 		{0, 0}
2627 		}
2628 	},
2629 	{0, 0, 2, 0,		/* 0x6c */
2630 		{{2, 3},
2631 		{5, 6},
2632 		{0, 0},
2633 		{0, 0}
2634 		}
2635 	},
2636 	{1, 0, 3, 0,		/* 0x6d */
2637 		{{0, 0},
2638 		{2, 3},
2639 		{5, 6},
2640 		{0, 0}
2641 		}
2642 	},
2643 	{0, 0, 2, 0,		/* 0x6e */
2644 		{{1, 3},
2645 		{5, 6},
2646 		{0, 0},
2647 		{0, 0}
2648 		}
2649 	},
2650 	{1, 0, 2, 0,		/* 0x6f */
2651 		{{0, 3},
2652 		{5, 6},
2653 		{0, 0},
2654 		{0, 0}
2655 		}
2656 	},
2657 	{0, 0, 1, 0,		/* 0x70 */
2658 		{{4, 6},
2659 		{0, 0},
2660 		{0, 0},
2661 		{0, 0}
2662 		}
2663 	},
2664 	{1, 0, 2, 0,		/* 0x71 */
2665 		{{0, 0},
2666 		{4, 6},
2667 		{0, 0},
2668 		{0, 0}
2669 		}
2670 	},
2671 	{0, 0, 2, 0,		/* 0x72 */
2672 		{{1, 1},
2673 		{4, 6},
2674 		{0, 0},
2675 		{0, 0}
2676 		}
2677 	},
2678 	{1, 0, 2, 0,		/* 0x73 */
2679 		{{0, 1},
2680 		{4, 6},
2681 		{0, 0},
2682 		{0, 0}
2683 		}
2684 	},
2685 	{0, 0, 2, 0,		/* 0x74 */
2686 		{{2, 2},
2687 		{4, 6},
2688 		{0, 0},
2689 		{0, 0}
2690 		}
2691 	},
2692 	{1, 0, 3, 0,		/* 0x75 */
2693 		{{0, 0},
2694 		{2, 2},
2695 		{4, 6},
2696 		{0, 0}
2697 		}
2698 	},
2699 	{0, 0, 2, 0,		/* 0x76 */
2700 		{{1, 2},
2701 		{4, 6},
2702 		{0, 0},
2703 		{0, 0}
2704 		}
2705 	},
2706 	{1, 0, 2, 0,		/* 0x77 */
2707 		{{0, 2},
2708 		{4, 6},
2709 		{0, 0},
2710 		{0, 0}
2711 		}
2712 	},
2713 	{0, 0, 1, 0,		/* 0x78 */
2714 		{{3, 6},
2715 		{0, 0},
2716 		{0, 0},
2717 		{0, 0}
2718 		}
2719 	},
2720 	{1, 0, 2, 0,		/* 0x79 */
2721 		{{0, 0},
2722 		{3, 6},
2723 		{0, 0},
2724 		{0, 0}
2725 		}
2726 	},
2727 	{0, 0, 2, 0,		/* 0x7a */
2728 		{{1, 1},
2729 		{3, 6},
2730 		{0, 0},
2731 		{0, 0}
2732 		}
2733 	},
2734 	{1, 0, 2, 0,		/* 0x7b */
2735 		{{0, 1},
2736 		{3, 6},
2737 		{0, 0},
2738 		{0, 0}
2739 		}
2740 	},
2741 	{0, 0, 1, 0,		/* 0x7c */
2742 		{{2, 6},
2743 		{0, 0},
2744 		{0, 0},
2745 		{0, 0}
2746 		}
2747 	},
2748 	{1, 0, 2, 0,		/* 0x7d */
2749 		{{0, 0},
2750 		{2, 6},
2751 		{0, 0},
2752 		{0, 0}
2753 		}
2754 	},
2755 	{0, 0, 1, 0,		/* 0x7e */
2756 		{{1, 6},
2757 		{0, 0},
2758 		{0, 0},
2759 		{0, 0}
2760 		}
2761 	},
2762 	{1, 0, 1, 0,		/* 0x7f */
2763 		{{0, 6},
2764 		{0, 0},
2765 		{0, 0},
2766 		{0, 0}
2767 		}
2768 	},
2769 	{0, 1, 1, 0,		/* 0x80 */
2770 		{{7, 7},
2771 		{0, 0},
2772 		{0, 0},
2773 		{0, 0}
2774 		}
2775 	},
2776 	{1, 1, 2, 0,		/* 0x81 */
2777 		{{0, 0},
2778 		{7, 7},
2779 		{0, 0},
2780 		{0, 0}
2781 		}
2782 	},
2783 	{0, 1, 2, 0,		/* 0x82 */
2784 		{{1, 1},
2785 		{7, 7},
2786 		{0, 0},
2787 		{0, 0}
2788 		}
2789 	},
2790 	{1, 1, 2, 0,		/* 0x83 */
2791 		{{0, 1},
2792 		{7, 7},
2793 		{0, 0},
2794 		{0, 0}
2795 		}
2796 	},
2797 	{0, 1, 2, 0,		/* 0x84 */
2798 		{{2, 2},
2799 		{7, 7},
2800 		{0, 0},
2801 		{0, 0}
2802 		}
2803 	},
2804 	{1, 1, 3, 0,		/* 0x85 */
2805 		{{0, 0},
2806 		{2, 2},
2807 		{7, 7},
2808 		{0, 0}
2809 		}
2810 	},
2811 	{0, 1, 2, 0,		/* 0x86 */
2812 		{{1, 2},
2813 		{7, 7},
2814 		{0, 0},
2815 		{0, 0}
2816 		}
2817 	},
2818 	{1, 1, 2, 0,		/* 0x87 */
2819 		{{0, 2},
2820 		{7, 7},
2821 		{0, 0},
2822 		{0, 0}
2823 		}
2824 	},
2825 	{0, 1, 2, 0,		/* 0x88 */
2826 		{{3, 3},
2827 		{7, 7},
2828 		{0, 0},
2829 		{0, 0}
2830 		}
2831 	},
2832 	{1, 1, 3, 0,		/* 0x89 */
2833 		{{0, 0},
2834 		{3, 3},
2835 		{7, 7},
2836 		{0, 0}
2837 		}
2838 	},
2839 	{0, 1, 3, 0,		/* 0x8a */
2840 		{{1, 1},
2841 		{3, 3},
2842 		{7, 7},
2843 		{0, 0}
2844 		}
2845 	},
2846 	{1, 1, 3, 0,		/* 0x8b */
2847 		{{0, 1},
2848 		{3, 3},
2849 		{7, 7},
2850 		{0, 0}
2851 		}
2852 	},
2853 	{0, 1, 2, 0,		/* 0x8c */
2854 		{{2, 3},
2855 		{7, 7},
2856 		{0, 0},
2857 		{0, 0}
2858 		}
2859 	},
2860 	{1, 1, 3, 0,		/* 0x8d */
2861 		{{0, 0},
2862 		{2, 3},
2863 		{7, 7},
2864 		{0, 0}
2865 		}
2866 	},
2867 	{0, 1, 2, 0,		/* 0x8e */
2868 		{{1, 3},
2869 		{7, 7},
2870 		{0, 0},
2871 		{0, 0}
2872 		}
2873 	},
2874 	{1, 1, 2, 0,		/* 0x8f */
2875 		{{0, 3},
2876 		{7, 7},
2877 		{0, 0},
2878 		{0, 0}
2879 		}
2880 	},
2881 	{0, 1, 2, 0,		/* 0x90 */
2882 		{{4, 4},
2883 		{7, 7},
2884 		{0, 0},
2885 		{0, 0}
2886 		}
2887 	},
2888 	{1, 1, 3, 0,		/* 0x91 */
2889 		{{0, 0},
2890 		{4, 4},
2891 		{7, 7},
2892 		{0, 0}
2893 		}
2894 	},
2895 	{0, 1, 3, 0,		/* 0x92 */
2896 		{{1, 1},
2897 		{4, 4},
2898 		{7, 7},
2899 		{0, 0}
2900 		}
2901 	},
2902 	{1, 1, 3, 0,		/* 0x93 */
2903 		{{0, 1},
2904 		{4, 4},
2905 		{7, 7},
2906 		{0, 0}
2907 		}
2908 	},
2909 	{0, 1, 3, 0,		/* 0x94 */
2910 		{{2, 2},
2911 		{4, 4},
2912 		{7, 7},
2913 		{0, 0}
2914 		}
2915 	},
2916 	{1, 1, 4, 0,		/* 0x95 */
2917 		{{0, 0},
2918 		{2, 2},
2919 		{4, 4},
2920 		{7, 7}
2921 		}
2922 	},
2923 	{0, 1, 3, 0,		/* 0x96 */
2924 		{{1, 2},
2925 		{4, 4},
2926 		{7, 7},
2927 		{0, 0}
2928 		}
2929 	},
2930 	{1, 1, 3, 0,		/* 0x97 */
2931 		{{0, 2},
2932 		{4, 4},
2933 		{7, 7},
2934 		{0, 0}
2935 		}
2936 	},
2937 	{0, 1, 2, 0,		/* 0x98 */
2938 		{{3, 4},
2939 		{7, 7},
2940 		{0, 0},
2941 		{0, 0}
2942 		}
2943 	},
2944 	{1, 1, 3, 0,		/* 0x99 */
2945 		{{0, 0},
2946 		{3, 4},
2947 		{7, 7},
2948 		{0, 0}
2949 		}
2950 	},
2951 	{0, 1, 3, 0,		/* 0x9a */
2952 		{{1, 1},
2953 		{3, 4},
2954 		{7, 7},
2955 		{0, 0}
2956 		}
2957 	},
2958 	{1, 1, 3, 0,		/* 0x9b */
2959 		{{0, 1},
2960 		{3, 4},
2961 		{7, 7},
2962 		{0, 0}
2963 		}
2964 	},
2965 	{0, 1, 2, 0,		/* 0x9c */
2966 		{{2, 4},
2967 		{7, 7},
2968 		{0, 0},
2969 		{0, 0}
2970 		}
2971 	},
2972 	{1, 1, 3, 0,		/* 0x9d */
2973 		{{0, 0},
2974 		{2, 4},
2975 		{7, 7},
2976 		{0, 0}
2977 		}
2978 	},
2979 	{0, 1, 2, 0,		/* 0x9e */
2980 		{{1, 4},
2981 		{7, 7},
2982 		{0, 0},
2983 		{0, 0}
2984 		}
2985 	},
2986 	{1, 1, 2, 0,		/* 0x9f */
2987 		{{0, 4},
2988 		{7, 7},
2989 		{0, 0},
2990 		{0, 0}
2991 		}
2992 	},
2993 	{0, 1, 2, 0,		/* 0xa0 */
2994 		{{5, 5},
2995 		{7, 7},
2996 		{0, 0},
2997 		{0, 0}
2998 		}
2999 	},
3000 	{1, 1, 3, 0,		/* 0xa1 */
3001 		{{0, 0},
3002 		{5, 5},
3003 		{7, 7},
3004 		{0, 0}
3005 		}
3006 	},
3007 	{0, 1, 3, 0,		/* 0xa2 */
3008 		{{1, 1},
3009 		{5, 5},
3010 		{7, 7},
3011 		{0, 0}
3012 		}
3013 	},
3014 	{1, 1, 3, 0,		/* 0xa3 */
3015 		{{0, 1},
3016 		{5, 5},
3017 		{7, 7},
3018 		{0, 0}
3019 		}
3020 	},
3021 	{0, 1, 3, 0,		/* 0xa4 */
3022 		{{2, 2},
3023 		{5, 5},
3024 		{7, 7},
3025 		{0, 0}
3026 		}
3027 	},
3028 	{1, 1, 4, 0,		/* 0xa5 */
3029 		{{0, 0},
3030 		{2, 2},
3031 		{5, 5},
3032 		{7, 7}
3033 		}
3034 	},
3035 	{0, 1, 3, 0,		/* 0xa6 */
3036 		{{1, 2},
3037 		{5, 5},
3038 		{7, 7},
3039 		{0, 0}
3040 		}
3041 	},
3042 	{1, 1, 3, 0,		/* 0xa7 */
3043 		{{0, 2},
3044 		{5, 5},
3045 		{7, 7},
3046 		{0, 0}
3047 		}
3048 	},
3049 	{0, 1, 3, 0,		/* 0xa8 */
3050 		{{3, 3},
3051 		{5, 5},
3052 		{7, 7},
3053 		{0, 0}
3054 		}
3055 	},
3056 	{1, 1, 4, 0,		/* 0xa9 */
3057 		{{0, 0},
3058 		{3, 3},
3059 		{5, 5},
3060 		{7, 7}
3061 		}
3062 	},
3063 	{0, 1, 4, 0,		/* 0xaa */
3064 		{{1, 1},
3065 		{3, 3},
3066 		{5, 5},
3067 		{7, 7}
3068 		}
3069 	},
3070 	{1, 1, 4, 0,		/* 0xab */
3071 		{{0, 1},
3072 		{3, 3},
3073 		{5, 5},
3074 		{7, 7}
3075 		}
3076 	},
3077 	{0, 1, 3, 0,		/* 0xac */
3078 		{{2, 3},
3079 		{5, 5},
3080 		{7, 7},
3081 		{0, 0}
3082 		}
3083 	},
3084 	{1, 1, 4, 0,		/* 0xad */
3085 		{{0, 0},
3086 		{2, 3},
3087 		{5, 5},
3088 		{7, 7}
3089 		}
3090 	},
3091 	{0, 1, 3, 0,		/* 0xae */
3092 		{{1, 3},
3093 		{5, 5},
3094 		{7, 7},
3095 		{0, 0}
3096 		}
3097 	},
3098 	{1, 1, 3, 0,		/* 0xaf */
3099 		{{0, 3},
3100 		{5, 5},
3101 		{7, 7},
3102 		{0, 0}
3103 		}
3104 	},
3105 	{0, 1, 2, 0,		/* 0xb0 */
3106 		{{4, 5},
3107 		{7, 7},
3108 		{0, 0},
3109 		{0, 0}
3110 		}
3111 	},
3112 	{1, 1, 3, 0,		/* 0xb1 */
3113 		{{0, 0},
3114 		{4, 5},
3115 		{7, 7},
3116 		{0, 0}
3117 		}
3118 	},
3119 	{0, 1, 3, 0,		/* 0xb2 */
3120 		{{1, 1},
3121 		{4, 5},
3122 		{7, 7},
3123 		{0, 0}
3124 		}
3125 	},
3126 	{1, 1, 3, 0,		/* 0xb3 */
3127 		{{0, 1},
3128 		{4, 5},
3129 		{7, 7},
3130 		{0, 0}
3131 		}
3132 	},
3133 	{0, 1, 3, 0,		/* 0xb4 */
3134 		{{2, 2},
3135 		{4, 5},
3136 		{7, 7},
3137 		{0, 0}
3138 		}
3139 	},
3140 	{1, 1, 4, 0,		/* 0xb5 */
3141 		{{0, 0},
3142 		{2, 2},
3143 		{4, 5},
3144 		{7, 7}
3145 		}
3146 	},
3147 	{0, 1, 3, 0,		/* 0xb6 */
3148 		{{1, 2},
3149 		{4, 5},
3150 		{7, 7},
3151 		{0, 0}
3152 		}
3153 	},
3154 	{1, 1, 3, 0,		/* 0xb7 */
3155 		{{0, 2},
3156 		{4, 5},
3157 		{7, 7},
3158 		{0, 0}
3159 		}
3160 	},
3161 	{0, 1, 2, 0,		/* 0xb8 */
3162 		{{3, 5},
3163 		{7, 7},
3164 		{0, 0},
3165 		{0, 0}
3166 		}
3167 	},
3168 	{1, 1, 3, 0,		/* 0xb9 */
3169 		{{0, 0},
3170 		{3, 5},
3171 		{7, 7},
3172 		{0, 0}
3173 		}
3174 	},
3175 	{0, 1, 3, 0,		/* 0xba */
3176 		{{1, 1},
3177 		{3, 5},
3178 		{7, 7},
3179 		{0, 0}
3180 		}
3181 	},
3182 	{1, 1, 3, 0,		/* 0xbb */
3183 		{{0, 1},
3184 		{3, 5},
3185 		{7, 7},
3186 		{0, 0}
3187 		}
3188 	},
3189 	{0, 1, 2, 0,		/* 0xbc */
3190 		{{2, 5},
3191 		{7, 7},
3192 		{0, 0},
3193 		{0, 0}
3194 		}
3195 	},
3196 	{1, 1, 3, 0,		/* 0xbd */
3197 		{{0, 0},
3198 		{2, 5},
3199 		{7, 7},
3200 		{0, 0}
3201 		}
3202 	},
3203 	{0, 1, 2, 0,		/* 0xbe */
3204 		{{1, 5},
3205 		{7, 7},
3206 		{0, 0},
3207 		{0, 0}
3208 		}
3209 	},
3210 	{1, 1, 2, 0,		/* 0xbf */
3211 		{{0, 5},
3212 		{7, 7},
3213 		{0, 0},
3214 		{0, 0}
3215 		}
3216 	},
3217 	{0, 1, 1, 0,		/* 0xc0 */
3218 		{{6, 7},
3219 		{0, 0},
3220 		{0, 0},
3221 		{0, 0}
3222 		}
3223 	},
3224 	{1, 1, 2, 0,		/* 0xc1 */
3225 		{{0, 0},
3226 		{6, 7},
3227 		{0, 0},
3228 		{0, 0}
3229 		}
3230 	},
3231 	{0, 1, 2, 0,		/* 0xc2 */
3232 		{{1, 1},
3233 		{6, 7},
3234 		{0, 0},
3235 		{0, 0}
3236 		}
3237 	},
3238 	{1, 1, 2, 0,		/* 0xc3 */
3239 		{{0, 1},
3240 		{6, 7},
3241 		{0, 0},
3242 		{0, 0}
3243 		}
3244 	},
3245 	{0, 1, 2, 0,		/* 0xc4 */
3246 		{{2, 2},
3247 		{6, 7},
3248 		{0, 0},
3249 		{0, 0}
3250 		}
3251 	},
3252 	{1, 1, 3, 0,		/* 0xc5 */
3253 		{{0, 0},
3254 		{2, 2},
3255 		{6, 7},
3256 		{0, 0}
3257 		}
3258 	},
3259 	{0, 1, 2, 0,		/* 0xc6 */
3260 		{{1, 2},
3261 		{6, 7},
3262 		{0, 0},
3263 		{0, 0}
3264 		}
3265 	},
3266 	{1, 1, 2, 0,		/* 0xc7 */
3267 		{{0, 2},
3268 		{6, 7},
3269 		{0, 0},
3270 		{0, 0}
3271 		}
3272 	},
3273 	{0, 1, 2, 0,		/* 0xc8 */
3274 		{{3, 3},
3275 		{6, 7},
3276 		{0, 0},
3277 		{0, 0}
3278 		}
3279 	},
3280 	{1, 1, 3, 0,		/* 0xc9 */
3281 		{{0, 0},
3282 		{3, 3},
3283 		{6, 7},
3284 		{0, 0}
3285 		}
3286 	},
3287 	{0, 1, 3, 0,		/* 0xca */
3288 		{{1, 1},
3289 		{3, 3},
3290 		{6, 7},
3291 		{0, 0}
3292 		}
3293 	},
3294 	{1, 1, 3, 0,		/* 0xcb */
3295 		{{0, 1},
3296 		{3, 3},
3297 		{6, 7},
3298 		{0, 0}
3299 		}
3300 	},
3301 	{0, 1, 2, 0,		/* 0xcc */
3302 		{{2, 3},
3303 		{6, 7},
3304 		{0, 0},
3305 		{0, 0}
3306 		}
3307 	},
3308 	{1, 1, 3, 0,		/* 0xcd */
3309 		{{0, 0},
3310 		{2, 3},
3311 		{6, 7},
3312 		{0, 0}
3313 		}
3314 	},
3315 	{0, 1, 2, 0,		/* 0xce */
3316 		{{1, 3},
3317 		{6, 7},
3318 		{0, 0},
3319 		{0, 0}
3320 		}
3321 	},
3322 	{1, 1, 2, 0,		/* 0xcf */
3323 		{{0, 3},
3324 		{6, 7},
3325 		{0, 0},
3326 		{0, 0}
3327 		}
3328 	},
3329 	{0, 1, 2, 0,		/* 0xd0 */
3330 		{{4, 4},
3331 		{6, 7},
3332 		{0, 0},
3333 		{0, 0}
3334 		}
3335 	},
3336 	{1, 1, 3, 0,		/* 0xd1 */
3337 		{{0, 0},
3338 		{4, 4},
3339 		{6, 7},
3340 		{0, 0}
3341 		}
3342 	},
3343 	{0, 1, 3, 0,		/* 0xd2 */
3344 		{{1, 1},
3345 		{4, 4},
3346 		{6, 7},
3347 		{0, 0}
3348 		}
3349 	},
3350 	{1, 1, 3, 0,		/* 0xd3 */
3351 		{{0, 1},
3352 		{4, 4},
3353 		{6, 7},
3354 		{0, 0}
3355 		}
3356 	},
3357 	{0, 1, 3, 0,		/* 0xd4 */
3358 		{{2, 2},
3359 		{4, 4},
3360 		{6, 7},
3361 		{0, 0}
3362 		}
3363 	},
3364 	{1, 1, 4, 0,		/* 0xd5 */
3365 		{{0, 0},
3366 		{2, 2},
3367 		{4, 4},
3368 		{6, 7}
3369 		}
3370 	},
3371 	{0, 1, 3, 0,		/* 0xd6 */
3372 		{{1, 2},
3373 		{4, 4},
3374 		{6, 7},
3375 		{0, 0}
3376 		}
3377 	},
3378 	{1, 1, 3, 0,		/* 0xd7 */
3379 		{{0, 2},
3380 		{4, 4},
3381 		{6, 7},
3382 		{0, 0}
3383 		}
3384 	},
3385 	{0, 1, 2, 0,		/* 0xd8 */
3386 		{{3, 4},
3387 		{6, 7},
3388 		{0, 0},
3389 		{0, 0}
3390 		}
3391 	},
3392 	{1, 1, 3, 0,		/* 0xd9 */
3393 		{{0, 0},
3394 		{3, 4},
3395 		{6, 7},
3396 		{0, 0}
3397 		}
3398 	},
3399 	{0, 1, 3, 0,		/* 0xda */
3400 		{{1, 1},
3401 		{3, 4},
3402 		{6, 7},
3403 		{0, 0}
3404 		}
3405 	},
3406 	{1, 1, 3, 0,		/* 0xdb */
3407 		{{0, 1},
3408 		{3, 4},
3409 		{6, 7},
3410 		{0, 0}
3411 		}
3412 	},
3413 	{0, 1, 2, 0,		/* 0xdc */
3414 		{{2, 4},
3415 		{6, 7},
3416 		{0, 0},
3417 		{0, 0}
3418 		}
3419 	},
3420 	{1, 1, 3, 0,		/* 0xdd */
3421 		{{0, 0},
3422 		{2, 4},
3423 		{6, 7},
3424 		{0, 0}
3425 		}
3426 	},
3427 	{0, 1, 2, 0,		/* 0xde */
3428 		{{1, 4},
3429 		{6, 7},
3430 		{0, 0},
3431 		{0, 0}
3432 		}
3433 	},
3434 	{1, 1, 2, 0,		/* 0xdf */
3435 		{{0, 4},
3436 		{6, 7},
3437 		{0, 0},
3438 		{0, 0}
3439 		}
3440 	},
3441 	{0, 1, 1, 0,		/* 0xe0 */
3442 		{{5, 7},
3443 		{0, 0},
3444 		{0, 0},
3445 		{0, 0}
3446 		}
3447 	},
3448 	{1, 1, 2, 0,		/* 0xe1 */
3449 		{{0, 0},
3450 		{5, 7},
3451 		{0, 0},
3452 		{0, 0}
3453 		}
3454 	},
3455 	{0, 1, 2, 0,		/* 0xe2 */
3456 		{{1, 1},
3457 		{5, 7},
3458 		{0, 0},
3459 		{0, 0}
3460 		}
3461 	},
3462 	{1, 1, 2, 0,		/* 0xe3 */
3463 		{{0, 1},
3464 		{5, 7},
3465 		{0, 0},
3466 		{0, 0}
3467 		}
3468 	},
3469 	{0, 1, 2, 0,		/* 0xe4 */
3470 		{{2, 2},
3471 		{5, 7},
3472 		{0, 0},
3473 		{0, 0}
3474 		}
3475 	},
3476 	{1, 1, 3, 0,		/* 0xe5 */
3477 		{{0, 0},
3478 		{2, 2},
3479 		{5, 7},
3480 		{0, 0}
3481 		}
3482 	},
3483 	{0, 1, 2, 0,		/* 0xe6 */
3484 		{{1, 2},
3485 		{5, 7},
3486 		{0, 0},
3487 		{0, 0}
3488 		}
3489 	},
3490 	{1, 1, 2, 0,		/* 0xe7 */
3491 		{{0, 2},
3492 		{5, 7},
3493 		{0, 0},
3494 		{0, 0}
3495 		}
3496 	},
3497 	{0, 1, 2, 0,		/* 0xe8 */
3498 		{{3, 3},
3499 		{5, 7},
3500 		{0, 0},
3501 		{0, 0}
3502 		}
3503 	},
3504 	{1, 1, 3, 0,		/* 0xe9 */
3505 		{{0, 0},
3506 		{3, 3},
3507 		{5, 7},
3508 		{0, 0}
3509 		}
3510 	},
3511 	{0, 1, 3, 0,		/* 0xea */
3512 		{{1, 1},
3513 		{3, 3},
3514 		{5, 7},
3515 		{0, 0}
3516 		}
3517 	},
3518 	{1, 1, 3, 0,		/* 0xeb */
3519 		{{0, 1},
3520 		{3, 3},
3521 		{5, 7},
3522 		{0, 0}
3523 		}
3524 	},
3525 	{0, 1, 2, 0,		/* 0xec */
3526 		{{2, 3},
3527 		{5, 7},
3528 		{0, 0},
3529 		{0, 0}
3530 		}
3531 	},
3532 	{1, 1, 3, 0,		/* 0xed */
3533 		{{0, 0},
3534 		{2, 3},
3535 		{5, 7},
3536 		{0, 0}
3537 		}
3538 	},
3539 	{0, 1, 2, 0,		/* 0xee */
3540 		{{1, 3},
3541 		{5, 7},
3542 		{0, 0},
3543 		{0, 0}
3544 		}
3545 	},
3546 	{1, 1, 2, 0,		/* 0xef */
3547 		{{0, 3},
3548 		{5, 7},
3549 		{0, 0},
3550 		{0, 0}
3551 		}
3552 	},
3553 	{0, 1, 1, 0,		/* 0xf0 */
3554 		{{4, 7},
3555 		{0, 0},
3556 		{0, 0},
3557 		{0, 0}
3558 		}
3559 	},
3560 	{1, 1, 2, 0,		/* 0xf1 */
3561 		{{0, 0},
3562 		{4, 7},
3563 		{0, 0},
3564 		{0, 0}
3565 		}
3566 	},
3567 	{0, 1, 2, 0,		/* 0xf2 */
3568 		{{1, 1},
3569 		{4, 7},
3570 		{0, 0},
3571 		{0, 0}
3572 		}
3573 	},
3574 	{1, 1, 2, 0,		/* 0xf3 */
3575 		{{0, 1},
3576 		{4, 7},
3577 		{0, 0},
3578 		{0, 0}
3579 		}
3580 	},
3581 	{0, 1, 2, 0,		/* 0xf4 */
3582 		{{2, 2},
3583 		{4, 7},
3584 		{0, 0},
3585 		{0, 0}
3586 		}
3587 	},
3588 	{1, 1, 3, 0,		/* 0xf5 */
3589 		{{0, 0},
3590 		{2, 2},
3591 		{4, 7},
3592 		{0, 0}
3593 		}
3594 	},
3595 	{0, 1, 2, 0,		/* 0xf6 */
3596 		{{1, 2},
3597 		{4, 7},
3598 		{0, 0},
3599 		{0, 0}
3600 		}
3601 	},
3602 	{1, 1, 2, 0,		/* 0xf7 */
3603 		{{0, 2},
3604 		{4, 7},
3605 		{0, 0},
3606 		{0, 0}
3607 		}
3608 	},
3609 	{0, 1, 1, 0,		/* 0xf8 */
3610 		{{3, 7},
3611 		{0, 0},
3612 		{0, 0},
3613 		{0, 0}
3614 		}
3615 	},
3616 	{1, 1, 2, 0,		/* 0xf9 */
3617 		{{0, 0},
3618 		{3, 7},
3619 		{0, 0},
3620 		{0, 0}
3621 		}
3622 	},
3623 	{0, 1, 2, 0,		/* 0xfa */
3624 		{{1, 1},
3625 		{3, 7},
3626 		{0, 0},
3627 		{0, 0}
3628 		}
3629 	},
3630 	{1, 1, 2, 0,		/* 0xfb */
3631 		{{0, 1},
3632 		{3, 7},
3633 		{0, 0},
3634 		{0, 0}
3635 		}
3636 	},
3637 	{0, 1, 1, 0,		/* 0xfc */
3638 		{{2, 7},
3639 		{0, 0},
3640 		{0, 0},
3641 		{0, 0}
3642 		}
3643 	},
3644 	{1, 1, 2, 0,		/* 0xfd */
3645 		{{0, 0},
3646 		{2, 7},
3647 		{0, 0},
3648 		{0, 0}
3649 		}
3650 	},
3651 	{0, 1, 1, 0,		/* 0xfe */
3652 		{{1, 7},
3653 		{0, 0},
3654 		{0, 0},
3655 		{0, 0}
3656 		}
3657 	},
3658 	{1, 1, 1, 0,		/* 0xff */
3659 		{{0, 7},
3660 		{0, 0},
3661 		{0, 0},
3662 		{0, 0}
3663 		}
3664 	}
3665 };
3666 
3667 
3668 
3669 int
3670 sctp_is_address_in_scope(struct sctp_ifa *ifa,
3671     int ipv4_addr_legal,
3672     int ipv6_addr_legal,
3673     int loopback_scope,
3674     int ipv4_local_scope,
3675     int local_scope,
3676     int site_scope,
3677     int do_update)
3678 {
3679 	if ((loopback_scope == 0) &&
3680 	    (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
3681 		/*
3682 		 * skip loopback if not in scope *
3683 		 */
3684 		return (0);
3685 	}
3686 	switch (ifa->address.sa.sa_family) {
3687 	case AF_INET:
3688 		if (ipv4_addr_legal) {
3689 			struct sockaddr_in *sin;
3690 
3691 			sin = (struct sockaddr_in *)&ifa->address.sin;
3692 			if (sin->sin_addr.s_addr == 0) {
3693 				/* not in scope , unspecified */
3694 				return (0);
3695 			}
3696 			if ((ipv4_local_scope == 0) &&
3697 			    (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
3698 				/* private address not in scope */
3699 				return (0);
3700 			}
3701 		} else {
3702 			return (0);
3703 		}
3704 		break;
3705 #ifdef INET6
3706 	case AF_INET6:
3707 		if (ipv6_addr_legal) {
3708 			struct sockaddr_in6 *sin6;
3709 
3710 			/*
3711 			 * Must update the flags,  bummer, which means any
3712 			 * IFA locks must now be applied HERE <->
3713 			 */
3714 			if (do_update) {
3715 				sctp_gather_internal_ifa_flags(ifa);
3716 			}
3717 			if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3718 				return (0);
3719 			}
3720 			/* ok to use deprecated addresses? */
3721 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
3722 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3723 				/* skip unspecifed addresses */
3724 				return (0);
3725 			}
3726 			if (	/* (local_scope == 0) && */
3727 			    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
3728 				return (0);
3729 			}
3730 			if ((site_scope == 0) &&
3731 			    (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
3732 				return (0);
3733 			}
3734 		} else {
3735 			return (0);
3736 		}
3737 		break;
3738 #endif
3739 	default:
3740 		return (0);
3741 	}
3742 	return (1);
3743 }
3744 
3745 static struct mbuf *
3746 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
3747 {
3748 	struct sctp_paramhdr *parmh;
3749 	struct mbuf *mret;
3750 	int len;
3751 
3752 	if (ifa->address.sa.sa_family == AF_INET) {
3753 		len = sizeof(struct sctp_ipv4addr_param);
3754 	} else if (ifa->address.sa.sa_family == AF_INET6) {
3755 		len = sizeof(struct sctp_ipv6addr_param);
3756 	} else {
3757 		/* unknown type */
3758 		return (m);
3759 	}
3760 	if (M_TRAILINGSPACE(m) >= len) {
3761 		/* easy side we just drop it on the end */
3762 		parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
3763 		mret = m;
3764 	} else {
3765 		/* Need more space */
3766 		mret = m;
3767 		while (SCTP_BUF_NEXT(mret) != NULL) {
3768 			mret = SCTP_BUF_NEXT(mret);
3769 		}
3770 		SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
3771 		if (SCTP_BUF_NEXT(mret) == NULL) {
3772 			/* We are hosed, can't add more addresses */
3773 			return (m);
3774 		}
3775 		mret = SCTP_BUF_NEXT(mret);
3776 		parmh = mtod(mret, struct sctp_paramhdr *);
3777 	}
3778 	/* now add the parameter */
3779 	switch (ifa->address.sa.sa_family) {
3780 	case AF_INET:
3781 		{
3782 			struct sctp_ipv4addr_param *ipv4p;
3783 			struct sockaddr_in *sin;
3784 
3785 			sin = (struct sockaddr_in *)&ifa->address.sin;
3786 			ipv4p = (struct sctp_ipv4addr_param *)parmh;
3787 			parmh->param_type = htons(SCTP_IPV4_ADDRESS);
3788 			parmh->param_length = htons(len);
3789 			ipv4p->addr = sin->sin_addr.s_addr;
3790 			SCTP_BUF_LEN(mret) += len;
3791 			break;
3792 		}
3793 #ifdef INET6
3794 	case AF_INET6:
3795 		{
3796 			struct sctp_ipv6addr_param *ipv6p;
3797 			struct sockaddr_in6 *sin6;
3798 
3799 			sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
3800 			ipv6p = (struct sctp_ipv6addr_param *)parmh;
3801 			parmh->param_type = htons(SCTP_IPV6_ADDRESS);
3802 			parmh->param_length = htons(len);
3803 			memcpy(ipv6p->addr, &sin6->sin6_addr,
3804 			    sizeof(ipv6p->addr));
3805 			/* clear embedded scope in the address */
3806 			in6_clearscope((struct in6_addr *)ipv6p->addr);
3807 			SCTP_BUF_LEN(mret) += len;
3808 			break;
3809 		}
3810 #endif
3811 	default:
3812 		return (m);
3813 	}
3814 	return (mret);
3815 }
3816 
3817 
3818 struct mbuf *
3819 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
3820     struct mbuf *m_at, int cnt_inits_to)
3821 {
3822 	struct sctp_vrf *vrf = NULL;
3823 	int cnt, limit_out = 0, total_count;
3824 	uint32_t vrf_id;
3825 
3826 	vrf_id = inp->def_vrf_id;
3827 	SCTP_IPI_ADDR_RLOCK();
3828 	vrf = sctp_find_vrf(vrf_id);
3829 	if (vrf == NULL) {
3830 		SCTP_IPI_ADDR_RUNLOCK();
3831 		return (m_at);
3832 	}
3833 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3834 		struct sctp_ifa *sctp_ifap;
3835 		struct sctp_ifn *sctp_ifnp;
3836 
3837 		cnt = cnt_inits_to;
3838 		if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
3839 			limit_out = 1;
3840 			cnt = SCTP_ADDRESS_LIMIT;
3841 			goto skip_count;
3842 		}
3843 		LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3844 			if ((scope->loopback_scope == 0) &&
3845 			    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
3846 				/*
3847 				 * Skip loopback devices if loopback_scope
3848 				 * not set
3849 				 */
3850 				continue;
3851 			}
3852 			LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3853 				if (sctp_is_address_in_scope(sctp_ifap,
3854 				    scope->ipv4_addr_legal,
3855 				    scope->ipv6_addr_legal,
3856 				    scope->loopback_scope,
3857 				    scope->ipv4_local_scope,
3858 				    scope->local_scope,
3859 				    scope->site_scope, 1) == 0) {
3860 					continue;
3861 				}
3862 				cnt++;
3863 				if (cnt > SCTP_ADDRESS_LIMIT) {
3864 					break;
3865 				}
3866 			}
3867 			if (cnt > SCTP_ADDRESS_LIMIT) {
3868 				break;
3869 			}
3870 		}
3871 skip_count:
3872 		if (cnt > 1) {
3873 			total_count = 0;
3874 			LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3875 				cnt = 0;
3876 				if ((scope->loopback_scope == 0) &&
3877 				    SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
3878 					/*
3879 					 * Skip loopback devices if
3880 					 * loopback_scope not set
3881 					 */
3882 					continue;
3883 				}
3884 				LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3885 					if (sctp_is_address_in_scope(sctp_ifap,
3886 					    scope->ipv4_addr_legal,
3887 					    scope->ipv6_addr_legal,
3888 					    scope->loopback_scope,
3889 					    scope->ipv4_local_scope,
3890 					    scope->local_scope,
3891 					    scope->site_scope, 0) == 0) {
3892 						continue;
3893 					}
3894 					m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
3895 					if (limit_out) {
3896 						cnt++;
3897 						total_count++;
3898 						if (cnt >= 2) {
3899 							/*
3900 							 * two from each
3901 							 * address
3902 							 */
3903 							break;
3904 						}
3905 						if (total_count > SCTP_ADDRESS_LIMIT) {
3906 							/* No more addresses */
3907 							break;
3908 						}
3909 					}
3910 				}
3911 			}
3912 		}
3913 	} else {
3914 		struct sctp_laddr *laddr;
3915 
3916 		cnt = cnt_inits_to;
3917 		/* First, how many ? */
3918 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3919 			if (laddr->ifa == NULL) {
3920 				continue;
3921 			}
3922 			if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3923 				/*
3924 				 * Address being deleted by the system, dont
3925 				 * list.
3926 				 */
3927 				continue;
3928 			if (laddr->action == SCTP_DEL_IP_ADDRESS) {
3929 				/*
3930 				 * Address being deleted on this ep don't
3931 				 * list.
3932 				 */
3933 				continue;
3934 			}
3935 			if (sctp_is_address_in_scope(laddr->ifa,
3936 			    scope->ipv4_addr_legal,
3937 			    scope->ipv6_addr_legal,
3938 			    scope->loopback_scope,
3939 			    scope->ipv4_local_scope,
3940 			    scope->local_scope,
3941 			    scope->site_scope, 1) == 0) {
3942 				continue;
3943 			}
3944 			cnt++;
3945 		}
3946 		if (cnt > SCTP_ADDRESS_LIMIT) {
3947 			limit_out = 1;
3948 		}
3949 		/*
3950 		 * To get through a NAT we only list addresses if we have
3951 		 * more than one. That way if you just bind a single address
3952 		 * we let the source of the init dictate our address.
3953 		 */
3954 		if (cnt > 1) {
3955 			LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3956 				cnt = 0;
3957 				if (laddr->ifa == NULL) {
3958 					continue;
3959 				}
3960 				if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3961 					continue;
3962 
3963 				if (sctp_is_address_in_scope(laddr->ifa,
3964 				    scope->ipv4_addr_legal,
3965 				    scope->ipv6_addr_legal,
3966 				    scope->loopback_scope,
3967 				    scope->ipv4_local_scope,
3968 				    scope->local_scope,
3969 				    scope->site_scope, 0) == 0) {
3970 					continue;
3971 				}
3972 				m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
3973 				cnt++;
3974 				if (cnt >= SCTP_ADDRESS_LIMIT) {
3975 					break;
3976 				}
3977 			}
3978 		}
3979 	}
3980 	SCTP_IPI_ADDR_RUNLOCK();
3981 	return (m_at);
3982 }
3983 
3984 static struct sctp_ifa *
3985 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
3986     uint8_t dest_is_loop,
3987     uint8_t dest_is_priv,
3988     sa_family_t fam)
3989 {
3990 	uint8_t dest_is_global = 0;
3991 
3992 	/* dest_is_priv is true if destination is a private address */
3993 	/* dest_is_loop is true if destination is a loopback addresses */
3994 
3995 	/*
3996 	 * Here we determine if its a preferred address. A preferred address
3997 	 * means it is the same scope or higher scope then the destination.
3998 	 * L = loopback, P = private, G = global
3999 	 * ----------------------------------------- src    |  dest | result
4000 	 * ---------------------------------------- L     |    L  |    yes
4001 	 * ----------------------------------------- P     |    L  |
4002 	 * yes-v4 no-v6 ----------------------------------------- G     |
4003 	 * L  |    yes-v4 no-v6 ----------------------------------------- L
4004 	 * |    P  |    no ----------------------------------------- P     |
4005 	 * P  |    yes ----------------------------------------- G     |
4006 	 * P  |    no ----------------------------------------- L     |    G
4007 	 * |    no ----------------------------------------- P     |    G  |
4008 	 * no ----------------------------------------- G     |    G  |
4009 	 * yes -----------------------------------------
4010 	 */
4011 
4012 	if (ifa->address.sa.sa_family != fam) {
4013 		/* forget mis-matched family */
4014 		return (NULL);
4015 	}
4016 	if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
4017 		dest_is_global = 1;
4018 	}
4019 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
4020 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
4021 	/* Ok the address may be ok */
4022 	if (fam == AF_INET6) {
4023 		/* ok to use deprecated addresses? no lets not! */
4024 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4025 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
4026 			return (NULL);
4027 		}
4028 		if (ifa->src_is_priv && !ifa->src_is_loop) {
4029 			if (dest_is_loop) {
4030 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
4031 				return (NULL);
4032 			}
4033 		}
4034 		if (ifa->src_is_glob) {
4035 			if (dest_is_loop) {
4036 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
4037 				return (NULL);
4038 			}
4039 		}
4040 	}
4041 	/*
4042 	 * Now that we know what is what, implement or table this could in
4043 	 * theory be done slicker (it used to be), but this is
4044 	 * straightforward and easier to validate :-)
4045 	 */
4046 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
4047 	    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
4048 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
4049 	    dest_is_loop, dest_is_priv, dest_is_global);
4050 
4051 	if ((ifa->src_is_loop) && (dest_is_priv)) {
4052 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
4053 		return (NULL);
4054 	}
4055 	if ((ifa->src_is_glob) && (dest_is_priv)) {
4056 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
4057 		return (NULL);
4058 	}
4059 	if ((ifa->src_is_loop) && (dest_is_global)) {
4060 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
4061 		return (NULL);
4062 	}
4063 	if ((ifa->src_is_priv) && (dest_is_global)) {
4064 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
4065 		return (NULL);
4066 	}
4067 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
4068 	/* its a preferred address */
4069 	return (ifa);
4070 }
4071 
4072 static struct sctp_ifa *
4073 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
4074     uint8_t dest_is_loop,
4075     uint8_t dest_is_priv,
4076     sa_family_t fam)
4077 {
4078 	uint8_t dest_is_global = 0;
4079 
4080 
4081 	/*
4082 	 * Here we determine if its a acceptable address. A acceptable
4083 	 * address means it is the same scope or higher scope but we can
4084 	 * allow for NAT which means its ok to have a global dest and a
4085 	 * private src.
4086 	 *
4087 	 * L = loopback, P = private, G = global
4088 	 * ----------------------------------------- src    |  dest | result
4089 	 * ----------------------------------------- L     |   L   |    yes
4090 	 * ----------------------------------------- P     |   L   |
4091 	 * yes-v4 no-v6 ----------------------------------------- G     |
4092 	 * L   |    yes ----------------------------------------- L     |
4093 	 * P   |    no ----------------------------------------- P     |   P
4094 	 * |    yes ----------------------------------------- G     |   P
4095 	 * |    yes - May not work -----------------------------------------
4096 	 * L     |   G   |    no ----------------------------------------- P
4097 	 * |   G   |    yes - May not work
4098 	 * ----------------------------------------- G     |   G   |    yes
4099 	 * -----------------------------------------
4100 	 */
4101 
4102 	if (ifa->address.sa.sa_family != fam) {
4103 		/* forget non matching family */
4104 		return (NULL);
4105 	}
4106 	/* Ok the address may be ok */
4107 	if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
4108 		dest_is_global = 1;
4109 	}
4110 	if (fam == AF_INET6) {
4111 		/* ok to use deprecated addresses? */
4112 		if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4113 			return (NULL);
4114 		}
4115 		if (ifa->src_is_priv) {
4116 			/* Special case, linklocal to loop */
4117 			if (dest_is_loop)
4118 				return (NULL);
4119 		}
4120 	}
4121 	/*
4122 	 * Now that we know what is what, implement our table. This could in
4123 	 * theory be done slicker (it used to be), but this is
4124 	 * straightforward and easier to validate :-)
4125 	 */
4126 	if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
4127 		return (NULL);
4128 	}
4129 	if ((ifa->src_is_loop == 1) && (dest_is_global)) {
4130 		return (NULL);
4131 	}
4132 	/* its an acceptable address */
4133 	return (ifa);
4134 }
4135 
4136 int
4137 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
4138 {
4139 	struct sctp_laddr *laddr;
4140 
4141 	if (stcb == NULL) {
4142 		/* There are no restrictions, no TCB :-) */
4143 		return (0);
4144 	}
4145 	LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
4146 		if (laddr->ifa == NULL) {
4147 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4148 			    __FUNCTION__);
4149 			continue;
4150 		}
4151 		if (laddr->ifa == ifa) {
4152 			/* Yes it is on the list */
4153 			return (1);
4154 		}
4155 	}
4156 	return (0);
4157 }
4158 
4159 
4160 int
4161 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
4162 {
4163 	struct sctp_laddr *laddr;
4164 
4165 	if (ifa == NULL)
4166 		return (0);
4167 	LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4168 		if (laddr->ifa == NULL) {
4169 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4170 			    __FUNCTION__);
4171 			continue;
4172 		}
4173 		if ((laddr->ifa == ifa) && laddr->action == 0)
4174 			/* same pointer */
4175 			return (1);
4176 	}
4177 	return (0);
4178 }
4179 
4180 
4181 
4182 static struct sctp_ifa *
4183 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
4184     sctp_route_t * ro,
4185     uint32_t vrf_id,
4186     int non_asoc_addr_ok,
4187     uint8_t dest_is_priv,
4188     uint8_t dest_is_loop,
4189     sa_family_t fam)
4190 {
4191 	struct sctp_laddr *laddr, *starting_point;
4192 	void *ifn;
4193 	int resettotop = 0;
4194 	struct sctp_ifn *sctp_ifn;
4195 	struct sctp_ifa *sctp_ifa, *sifa;
4196 	struct sctp_vrf *vrf;
4197 	uint32_t ifn_index;
4198 
4199 	vrf = sctp_find_vrf(vrf_id);
4200 	if (vrf == NULL)
4201 		return (NULL);
4202 
4203 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4204 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
4205 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
4206 	/*
4207 	 * first question, is the ifn we will emit on in our list, if so, we
4208 	 * want such an address. Note that we first looked for a preferred
4209 	 * address.
4210 	 */
4211 	if (sctp_ifn) {
4212 		/* is a preferred one on the interface we route out? */
4213 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4214 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4215 			    (non_asoc_addr_ok == 0))
4216 				continue;
4217 			sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
4218 			    dest_is_loop,
4219 			    dest_is_priv, fam);
4220 			if (sifa == NULL)
4221 				continue;
4222 			if (sctp_is_addr_in_ep(inp, sifa)) {
4223 				atomic_add_int(&sifa->refcount, 1);
4224 				return (sifa);
4225 			}
4226 		}
4227 	}
4228 	/*
4229 	 * ok, now we now need to find one on the list of the addresses. We
4230 	 * can't get one on the emitting interface so let's find first a
4231 	 * preferred one. If not that an acceptable one otherwise... we
4232 	 * return NULL.
4233 	 */
4234 	starting_point = inp->next_addr_touse;
4235 once_again:
4236 	if (inp->next_addr_touse == NULL) {
4237 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
4238 		resettotop = 1;
4239 	}
4240 	for (laddr = inp->next_addr_touse; laddr;
4241 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4242 		if (laddr->ifa == NULL) {
4243 			/* address has been removed */
4244 			continue;
4245 		}
4246 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4247 			/* address is being deleted */
4248 			continue;
4249 		}
4250 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
4251 		    dest_is_priv, fam);
4252 		if (sifa == NULL)
4253 			continue;
4254 		atomic_add_int(&sifa->refcount, 1);
4255 		return (sifa);
4256 	}
4257 	if (resettotop == 0) {
4258 		inp->next_addr_touse = NULL;
4259 		goto once_again;
4260 	}
4261 	inp->next_addr_touse = starting_point;
4262 	resettotop = 0;
4263 once_again_too:
4264 	if (inp->next_addr_touse == NULL) {
4265 		inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
4266 		resettotop = 1;
4267 	}
4268 	/* ok, what about an acceptable address in the inp */
4269 	for (laddr = inp->next_addr_touse; laddr;
4270 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4271 		if (laddr->ifa == NULL) {
4272 			/* address has been removed */
4273 			continue;
4274 		}
4275 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4276 			/* address is being deleted */
4277 			continue;
4278 		}
4279 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
4280 		    dest_is_priv, fam);
4281 		if (sifa == NULL)
4282 			continue;
4283 		atomic_add_int(&sifa->refcount, 1);
4284 		return (sifa);
4285 	}
4286 	if (resettotop == 0) {
4287 		inp->next_addr_touse = NULL;
4288 		goto once_again_too;
4289 	}
4290 	/*
4291 	 * no address bound can be a source for the destination we are in
4292 	 * trouble
4293 	 */
4294 	return (NULL);
4295 }
4296 
4297 
4298 
4299 static struct sctp_ifa *
4300 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
4301     struct sctp_tcb *stcb,
4302     struct sctp_nets *net,
4303     sctp_route_t * ro,
4304     uint32_t vrf_id,
4305     uint8_t dest_is_priv,
4306     uint8_t dest_is_loop,
4307     int non_asoc_addr_ok,
4308     sa_family_t fam)
4309 {
4310 	struct sctp_laddr *laddr, *starting_point;
4311 	void *ifn;
4312 	struct sctp_ifn *sctp_ifn;
4313 	struct sctp_ifa *sctp_ifa, *sifa;
4314 	uint8_t start_at_beginning = 0;
4315 	struct sctp_vrf *vrf;
4316 	uint32_t ifn_index;
4317 
4318 	/*
4319 	 * first question, is the ifn we will emit on in our list, if so, we
4320 	 * want that one.
4321 	 */
4322 	vrf = sctp_find_vrf(vrf_id);
4323 	if (vrf == NULL)
4324 		return (NULL);
4325 
4326 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4327 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
4328 	sctp_ifn = sctp_find_ifn(ifn, ifn_index);
4329 
4330 	/*
4331 	 * first question, is the ifn we will emit on in our list?  If so,
4332 	 * we want that one. First we look for a preferred. Second, we go
4333 	 * for an acceptable.
4334 	 */
4335 	if (sctp_ifn) {
4336 		/* first try for a preferred address on the ep */
4337 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4338 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
4339 				continue;
4340 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
4341 				sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
4342 				if (sifa == NULL)
4343 					continue;
4344 				if (((non_asoc_addr_ok == 0) &&
4345 				    (sctp_is_addr_restricted(stcb, sifa))) ||
4346 				    (non_asoc_addr_ok &&
4347 				    (sctp_is_addr_restricted(stcb, sifa)) &&
4348 				    (!sctp_is_addr_pending(stcb, sifa)))) {
4349 					/* on the no-no list */
4350 					continue;
4351 				}
4352 				atomic_add_int(&sifa->refcount, 1);
4353 				return (sifa);
4354 			}
4355 		}
4356 		/* next try for an acceptable address on the ep */
4357 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4358 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
4359 				continue;
4360 			if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
4361 				sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
4362 				if (sifa == NULL)
4363 					continue;
4364 				if (((non_asoc_addr_ok == 0) &&
4365 				    (sctp_is_addr_restricted(stcb, sifa))) ||
4366 				    (non_asoc_addr_ok &&
4367 				    (sctp_is_addr_restricted(stcb, sifa)) &&
4368 				    (!sctp_is_addr_pending(stcb, sifa)))) {
4369 					/* on the no-no list */
4370 					continue;
4371 				}
4372 				atomic_add_int(&sifa->refcount, 1);
4373 				return (sifa);
4374 			}
4375 		}
4376 
4377 	}
4378 	/*
4379 	 * if we can't find one like that then we must look at all addresses
4380 	 * bound to pick one at first preferable then secondly acceptable.
4381 	 */
4382 	starting_point = stcb->asoc.last_used_address;
4383 sctp_from_the_top:
4384 	if (stcb->asoc.last_used_address == NULL) {
4385 		start_at_beginning = 1;
4386 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
4387 	}
4388 	/* search beginning with the last used address */
4389 	for (laddr = stcb->asoc.last_used_address; laddr;
4390 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4391 		if (laddr->ifa == NULL) {
4392 			/* address has been removed */
4393 			continue;
4394 		}
4395 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4396 			/* address is being deleted */
4397 			continue;
4398 		}
4399 		sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
4400 		if (sifa == NULL)
4401 			continue;
4402 		if (((non_asoc_addr_ok == 0) &&
4403 		    (sctp_is_addr_restricted(stcb, sifa))) ||
4404 		    (non_asoc_addr_ok &&
4405 		    (sctp_is_addr_restricted(stcb, sifa)) &&
4406 		    (!sctp_is_addr_pending(stcb, sifa)))) {
4407 			/* on the no-no list */
4408 			continue;
4409 		}
4410 		stcb->asoc.last_used_address = laddr;
4411 		atomic_add_int(&sifa->refcount, 1);
4412 		return (sifa);
4413 	}
4414 	if (start_at_beginning == 0) {
4415 		stcb->asoc.last_used_address = NULL;
4416 		goto sctp_from_the_top;
4417 	}
4418 	/* now try for any higher scope than the destination */
4419 	stcb->asoc.last_used_address = starting_point;
4420 	start_at_beginning = 0;
4421 sctp_from_the_top2:
4422 	if (stcb->asoc.last_used_address == NULL) {
4423 		start_at_beginning = 1;
4424 		stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
4425 	}
4426 	/* search beginning with the last used address */
4427 	for (laddr = stcb->asoc.last_used_address; laddr;
4428 	    laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4429 		if (laddr->ifa == NULL) {
4430 			/* address has been removed */
4431 			continue;
4432 		}
4433 		if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4434 			/* address is being deleted */
4435 			continue;
4436 		}
4437 		sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
4438 		    dest_is_priv, fam);
4439 		if (sifa == NULL)
4440 			continue;
4441 		if (((non_asoc_addr_ok == 0) &&
4442 		    (sctp_is_addr_restricted(stcb, sifa))) ||
4443 		    (non_asoc_addr_ok &&
4444 		    (sctp_is_addr_restricted(stcb, sifa)) &&
4445 		    (!sctp_is_addr_pending(stcb, sifa)))) {
4446 			/* on the no-no list */
4447 			continue;
4448 		}
4449 		stcb->asoc.last_used_address = laddr;
4450 		atomic_add_int(&sifa->refcount, 1);
4451 		return (sifa);
4452 	}
4453 	if (start_at_beginning == 0) {
4454 		stcb->asoc.last_used_address = NULL;
4455 		goto sctp_from_the_top2;
4456 	}
4457 	return (NULL);
4458 }
4459 
4460 static struct sctp_ifa *
4461 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
4462     struct sctp_tcb *stcb,
4463     int non_asoc_addr_ok,
4464     uint8_t dest_is_loop,
4465     uint8_t dest_is_priv,
4466     int addr_wanted,
4467     sa_family_t fam,
4468     sctp_route_t * ro
4469 )
4470 {
4471 	struct sctp_ifa *ifa, *sifa;
4472 	int num_eligible_addr = 0;
4473 
4474 #ifdef INET6
4475 	struct sockaddr_in6 sin6, lsa6;
4476 
4477 	if (fam == AF_INET6) {
4478 		memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
4479 		(void)sa6_recoverscope(&sin6);
4480 	}
4481 #endif				/* INET6 */
4482 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
4483 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4484 		    (non_asoc_addr_ok == 0))
4485 			continue;
4486 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
4487 		    dest_is_priv, fam);
4488 		if (sifa == NULL)
4489 			continue;
4490 #ifdef INET6
4491 		if (fam == AF_INET6 &&
4492 		    dest_is_loop &&
4493 		    sifa->src_is_loop && sifa->src_is_priv) {
4494 			/*
4495 			 * don't allow fe80::1 to be a src on loop ::1, we
4496 			 * don't list it to the peer so we will get an
4497 			 * abort.
4498 			 */
4499 			continue;
4500 		}
4501 		if (fam == AF_INET6 &&
4502 		    IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
4503 		    IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
4504 			/*
4505 			 * link-local <-> link-local must belong to the same
4506 			 * scope.
4507 			 */
4508 			memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
4509 			(void)sa6_recoverscope(&lsa6);
4510 			if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
4511 				continue;
4512 			}
4513 		}
4514 #endif				/* INET6 */
4515 
4516 		/*
4517 		 * Check if the IPv6 address matches to next-hop. In the
4518 		 * mobile case, old IPv6 address may be not deleted from the
4519 		 * interface. Then, the interface has previous and new
4520 		 * addresses.  We should use one corresponding to the
4521 		 * next-hop.  (by micchie)
4522 		 */
4523 #ifdef INET6
4524 		if (stcb && fam == AF_INET6 &&
4525 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
4526 			if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
4527 			    == 0) {
4528 				continue;
4529 			}
4530 		}
4531 #endif
4532 		/* Avoid topologically incorrect IPv4 address */
4533 		if (stcb && fam == AF_INET &&
4534 		    sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
4535 			if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
4536 				continue;
4537 			}
4538 		}
4539 		if (stcb) {
4540 			if (((non_asoc_addr_ok == 0) &&
4541 			    (sctp_is_addr_restricted(stcb, sifa))) ||
4542 			    (non_asoc_addr_ok &&
4543 			    (sctp_is_addr_restricted(stcb, sifa)) &&
4544 			    (!sctp_is_addr_pending(stcb, sifa)))) {
4545 				/*
4546 				 * It is restricted for some reason..
4547 				 * probably not yet added.
4548 				 */
4549 				continue;
4550 			}
4551 		}
4552 		if (num_eligible_addr >= addr_wanted) {
4553 			return (sifa);
4554 		}
4555 		num_eligible_addr++;
4556 	}
4557 	return (NULL);
4558 }
4559 
4560 
4561 static int
4562 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
4563     struct sctp_tcb *stcb,
4564     int non_asoc_addr_ok,
4565     uint8_t dest_is_loop,
4566     uint8_t dest_is_priv,
4567     sa_family_t fam)
4568 {
4569 	struct sctp_ifa *ifa, *sifa;
4570 	int num_eligible_addr = 0;
4571 
4572 	LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
4573 		if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4574 		    (non_asoc_addr_ok == 0)) {
4575 			continue;
4576 		}
4577 		sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
4578 		    dest_is_priv, fam);
4579 		if (sifa == NULL) {
4580 			continue;
4581 		}
4582 		if (stcb) {
4583 			if (((non_asoc_addr_ok == 0) &&
4584 			    (sctp_is_addr_restricted(stcb, sifa))) ||
4585 			    (non_asoc_addr_ok &&
4586 			    (sctp_is_addr_restricted(stcb, sifa)) &&
4587 			    (!sctp_is_addr_pending(stcb, sifa)))) {
4588 				/*
4589 				 * It is restricted for some reason..
4590 				 * probably not yet added.
4591 				 */
4592 				continue;
4593 			}
4594 		}
4595 		num_eligible_addr++;
4596 	}
4597 	return (num_eligible_addr);
4598 }
4599 
4600 static struct sctp_ifa *
4601 sctp_choose_boundall(struct sctp_inpcb *inp,
4602     struct sctp_tcb *stcb,
4603     struct sctp_nets *net,
4604     sctp_route_t * ro,
4605     uint32_t vrf_id,
4606     uint8_t dest_is_priv,
4607     uint8_t dest_is_loop,
4608     int non_asoc_addr_ok,
4609     sa_family_t fam)
4610 {
4611 	int cur_addr_num = 0, num_preferred = 0;
4612 	void *ifn;
4613 	struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
4614 	struct sctp_ifa *sctp_ifa, *sifa;
4615 	uint32_t ifn_index;
4616 	struct sctp_vrf *vrf;
4617 
4618 	/*-
4619 	 * For boundall we can use any address in the association.
4620 	 * If non_asoc_addr_ok is set we can use any address (at least in
4621 	 * theory). So we look for preferred addresses first. If we find one,
4622 	 * we use it. Otherwise we next try to get an address on the
4623 	 * interface, which we should be able to do (unless non_asoc_addr_ok
4624 	 * is false and we are routed out that way). In these cases where we
4625 	 * can't use the address of the interface we go through all the
4626 	 * ifn's looking for an address we can use and fill that in. Punting
4627 	 * means we send back address 0, which will probably cause problems
4628 	 * actually since then IP will fill in the address of the route ifn,
4629 	 * which means we probably already rejected it.. i.e. here comes an
4630 	 * abort :-<.
4631 	 */
4632 	vrf = sctp_find_vrf(vrf_id);
4633 	if (vrf == NULL)
4634 		return (NULL);
4635 
4636 	ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4637 	ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
4638 	emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
4639 	if (sctp_ifn == NULL) {
4640 		/* ?? We don't have this guy ?? */
4641 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
4642 		goto bound_all_plan_b;
4643 	}
4644 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
4645 	    ifn_index, sctp_ifn->ifn_name);
4646 
4647 	if (net) {
4648 		cur_addr_num = net->indx_of_eligible_next_to_use;
4649 	}
4650 	num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
4651 	    stcb,
4652 	    non_asoc_addr_ok,
4653 	    dest_is_loop,
4654 	    dest_is_priv, fam);
4655 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
4656 	    num_preferred, sctp_ifn->ifn_name);
4657 	if (num_preferred == 0) {
4658 		/*
4659 		 * no eligible addresses, we must use some other interface
4660 		 * address if we can find one.
4661 		 */
4662 		goto bound_all_plan_b;
4663 	}
4664 	/*
4665 	 * Ok we have num_eligible_addr set with how many we can use, this
4666 	 * may vary from call to call due to addresses being deprecated
4667 	 * etc..
4668 	 */
4669 	if (cur_addr_num >= num_preferred) {
4670 		cur_addr_num = 0;
4671 	}
4672 	/*
4673 	 * select the nth address from the list (where cur_addr_num is the
4674 	 * nth) and 0 is the first one, 1 is the second one etc...
4675 	 */
4676 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
4677 
4678 	sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
4679 	    dest_is_priv, cur_addr_num, fam, ro);
4680 
4681 	/* if sctp_ifa is NULL something changed??, fall to plan b. */
4682 	if (sctp_ifa) {
4683 		atomic_add_int(&sctp_ifa->refcount, 1);
4684 		if (net) {
4685 			/* save off where the next one we will want */
4686 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
4687 		}
4688 		return (sctp_ifa);
4689 	}
4690 	/*
4691 	 * plan_b: Look at all interfaces and find a preferred address. If
4692 	 * no preferred fall through to plan_c.
4693 	 */
4694 bound_all_plan_b:
4695 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
4696 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
4697 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
4698 		    sctp_ifn->ifn_name);
4699 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
4700 			/* wrong base scope */
4701 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
4702 			continue;
4703 		}
4704 		if ((sctp_ifn == looked_at) && looked_at) {
4705 			/* already looked at this guy */
4706 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
4707 			continue;
4708 		}
4709 		num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
4710 		    dest_is_loop, dest_is_priv, fam);
4711 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
4712 		    "Found ifn:%p %d preferred source addresses\n",
4713 		    ifn, num_preferred);
4714 		if (num_preferred == 0) {
4715 			/* None on this interface. */
4716 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
4717 			continue;
4718 		}
4719 		SCTPDBG(SCTP_DEBUG_OUTPUT2,
4720 		    "num preferred:%d on interface:%p cur_addr_num:%d\n",
4721 		    num_preferred, sctp_ifn, cur_addr_num);
4722 
4723 		/*
4724 		 * Ok we have num_eligible_addr set with how many we can
4725 		 * use, this may vary from call to call due to addresses
4726 		 * being deprecated etc..
4727 		 */
4728 		if (cur_addr_num >= num_preferred) {
4729 			cur_addr_num = 0;
4730 		}
4731 		sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
4732 		    dest_is_priv, cur_addr_num, fam, ro);
4733 		if (sifa == NULL)
4734 			continue;
4735 		if (net) {
4736 			net->indx_of_eligible_next_to_use = cur_addr_num + 1;
4737 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
4738 			    cur_addr_num);
4739 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
4740 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
4741 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
4742 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
4743 		}
4744 		atomic_add_int(&sifa->refcount, 1);
4745 		return (sifa);
4746 
4747 	}
4748 
4749 	/* plan_c: do we have an acceptable address on the emit interface */
4750 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
4751 	if (emit_ifn == NULL) {
4752 		goto plan_d;
4753 	}
4754 	LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
4755 		if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4756 		    (non_asoc_addr_ok == 0))
4757 			continue;
4758 		sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
4759 		    dest_is_priv, fam);
4760 		if (sifa == NULL)
4761 			continue;
4762 		if (stcb) {
4763 			if (((non_asoc_addr_ok == 0) &&
4764 			    (sctp_is_addr_restricted(stcb, sifa))) ||
4765 			    (non_asoc_addr_ok &&
4766 			    (sctp_is_addr_restricted(stcb, sifa)) &&
4767 			    (!sctp_is_addr_pending(stcb, sifa)))) {
4768 				/*
4769 				 * It is restricted for some reason..
4770 				 * probably not yet added.
4771 				 */
4772 				continue;
4773 			}
4774 		}
4775 		atomic_add_int(&sifa->refcount, 1);
4776 		return (sifa);
4777 	}
4778 plan_d:
4779 	/*
4780 	 * plan_d: We are in trouble. No preferred address on the emit
4781 	 * interface. And not even a preferred address on all interfaces. Go
4782 	 * out and see if we can find an acceptable address somewhere
4783 	 * amongst all interfaces.
4784 	 */
4785 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
4786 	LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
4787 		if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
4788 			/* wrong base scope */
4789 			continue;
4790 		}
4791 		if ((sctp_ifn == looked_at) && looked_at)
4792 			/* already looked at this guy */
4793 			continue;
4794 
4795 		LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4796 			if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4797 			    (non_asoc_addr_ok == 0))
4798 				continue;
4799 			sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
4800 			    dest_is_loop,
4801 			    dest_is_priv, fam);
4802 			if (sifa == NULL)
4803 				continue;
4804 			if (stcb) {
4805 				if (((non_asoc_addr_ok == 0) &&
4806 				    (sctp_is_addr_restricted(stcb, sifa))) ||
4807 				    (non_asoc_addr_ok &&
4808 				    (sctp_is_addr_restricted(stcb, sifa)) &&
4809 				    (!sctp_is_addr_pending(stcb, sifa)))) {
4810 					/*
4811 					 * It is restricted for some
4812 					 * reason.. probably not yet added.
4813 					 */
4814 					continue;
4815 				}
4816 			}
4817 			atomic_add_int(&sifa->refcount, 1);
4818 			return (sifa);
4819 		}
4820 	}
4821 	/*
4822 	 * Ok we can find NO address to source from that is not on our
4823 	 * restricted list and non_asoc_address is NOT ok, or it is on our
4824 	 * restricted list. We can't source to it :-(
4825 	 */
4826 	return (NULL);
4827 }
4828 
4829 
4830 
4831 /* tcb may be NULL */
4832 struct sctp_ifa *
4833 sctp_source_address_selection(struct sctp_inpcb *inp,
4834     struct sctp_tcb *stcb,
4835     sctp_route_t * ro,
4836     struct sctp_nets *net,
4837     int non_asoc_addr_ok, uint32_t vrf_id)
4838 {
4839 	struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
4840 
4841 #ifdef INET6
4842 	struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
4843 
4844 #endif
4845 	struct sctp_ifa *answer;
4846 	uint8_t dest_is_priv, dest_is_loop;
4847 	sa_family_t fam;
4848 
4849 	/*-
4850 	 * Rules: - Find the route if needed, cache if I can. - Look at
4851 	 * interface address in route, Is it in the bound list. If so we
4852 	 * have the best source. - If not we must rotate amongst the
4853 	 * addresses.
4854 	 *
4855 	 * Cavets and issues
4856 	 *
4857 	 * Do we need to pay attention to scope. We can have a private address
4858 	 * or a global address we are sourcing or sending to. So if we draw
4859 	 * it out
4860 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
4861 	 * For V4
4862          *------------------------------------------
4863 	 *      source     *      dest  *  result
4864 	 * -----------------------------------------
4865 	 * <a>  Private    *    Global  *  NAT
4866 	 * -----------------------------------------
4867 	 * <b>  Private    *    Private *  No problem
4868 	 * -----------------------------------------
4869          * <c>  Global     *    Private *  Huh, How will this work?
4870 	 * -----------------------------------------
4871          * <d>  Global     *    Global  *  No Problem
4872          *------------------------------------------
4873 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
4874 	 * For V6
4875          *------------------------------------------
4876 	 *      source     *      dest  *  result
4877 	 * -----------------------------------------
4878 	 * <a>  Linklocal  *    Global  *
4879 	 * -----------------------------------------
4880 	 * <b>  Linklocal  * Linklocal  *  No problem
4881 	 * -----------------------------------------
4882          * <c>  Global     * Linklocal  *  Huh, How will this work?
4883 	 * -----------------------------------------
4884          * <d>  Global     *    Global  *  No Problem
4885          *------------------------------------------
4886 	 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
4887          *
4888 	 * And then we add to that what happens if there are multiple addresses
4889 	 * assigned to an interface. Remember the ifa on a ifn is a linked
4890 	 * list of addresses. So one interface can have more than one IP
4891 	 * address. What happens if we have both a private and a global
4892 	 * address? Do we then use context of destination to sort out which
4893 	 * one is best? And what about NAT's sending P->G may get you a NAT
4894 	 * translation, or should you select the G thats on the interface in
4895 	 * preference.
4896 	 *
4897 	 * Decisions:
4898 	 *
4899 	 * - count the number of addresses on the interface.
4900          * - if it is one, no problem except case <c>.
4901          *   For <a> we will assume a NAT out there.
4902 	 * - if there are more than one, then we need to worry about scope P
4903 	 *   or G. We should prefer G -> G and P -> P if possible.
4904 	 *   Then as a secondary fall back to mixed types G->P being a last
4905 	 *   ditch one.
4906          * - The above all works for bound all, but bound specific we need to
4907 	 *   use the same concept but instead only consider the bound
4908 	 *   addresses. If the bound set is NOT assigned to the interface then
4909 	 *   we must use rotation amongst the bound addresses..
4910 	 */
4911 	if (ro->ro_rt == NULL) {
4912 		/*
4913 		 * Need a route to cache.
4914 		 */
4915 		SCTP_RTALLOC(ro, vrf_id);
4916 	}
4917 	if (ro->ro_rt == NULL) {
4918 		return (NULL);
4919 	}
4920 	fam = to->sin_family;
4921 	dest_is_priv = dest_is_loop = 0;
4922 	/* Setup our scopes for the destination */
4923 	switch (fam) {
4924 	case AF_INET:
4925 		/* Scope based on outbound address */
4926 		if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
4927 			dest_is_loop = 1;
4928 			if (net != NULL) {
4929 				/* mark it as local */
4930 				net->addr_is_local = 1;
4931 			}
4932 		} else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
4933 			dest_is_priv = 1;
4934 		}
4935 		break;
4936 #ifdef INET6
4937 	case AF_INET6:
4938 		/* Scope based on outbound address */
4939 		if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
4940 		    SCTP_ROUTE_IS_REAL_LOOP(ro)) {
4941 			/*
4942 			 * If the address is a loopback address, which
4943 			 * consists of "::1" OR "fe80::1%lo0", we are
4944 			 * loopback scope. But we don't use dest_is_priv
4945 			 * (link local addresses).
4946 			 */
4947 			dest_is_loop = 1;
4948 			if (net != NULL) {
4949 				/* mark it as local */
4950 				net->addr_is_local = 1;
4951 			}
4952 		} else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
4953 			dest_is_priv = 1;
4954 		}
4955 		break;
4956 #endif
4957 	}
4958 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
4959 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
4960 	SCTP_IPI_ADDR_RLOCK();
4961 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4962 		/*
4963 		 * Bound all case
4964 		 */
4965 		answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
4966 		    dest_is_priv, dest_is_loop,
4967 		    non_asoc_addr_ok, fam);
4968 		SCTP_IPI_ADDR_RUNLOCK();
4969 		return (answer);
4970 	}
4971 	/*
4972 	 * Subset bound case
4973 	 */
4974 	if (stcb) {
4975 		answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
4976 		    vrf_id, dest_is_priv,
4977 		    dest_is_loop,
4978 		    non_asoc_addr_ok, fam);
4979 	} else {
4980 		answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
4981 		    non_asoc_addr_ok,
4982 		    dest_is_priv,
4983 		    dest_is_loop, fam);
4984 	}
4985 	SCTP_IPI_ADDR_RUNLOCK();
4986 	return (answer);
4987 }
4988 
4989 static int
4990 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
4991 {
4992 	struct cmsghdr cmh;
4993 	int tlen, at;
4994 
4995 	tlen = SCTP_BUF_LEN(control);
4996 	at = 0;
4997 	/*
4998 	 * Independent of how many mbufs, find the c_type inside the control
4999 	 * structure and copy out the data.
5000 	 */
5001 	while (at < tlen) {
5002 		if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
5003 			/* not enough room for one more we are done. */
5004 			return (0);
5005 		}
5006 		m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
5007 		if (((int)cmh.cmsg_len + at) > tlen) {
5008 			/*
5009 			 * this is real messed up since there is not enough
5010 			 * data here to cover the cmsg header. We are done.
5011 			 */
5012 			return (0);
5013 		}
5014 		if ((cmh.cmsg_level == IPPROTO_SCTP) &&
5015 		    (c_type == cmh.cmsg_type)) {
5016 			/* found the one we want, copy it out */
5017 			at += CMSG_ALIGN(sizeof(struct cmsghdr));
5018 			if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
5019 				/*
5020 				 * space of cmsg_len after header not big
5021 				 * enough
5022 				 */
5023 				return (0);
5024 			}
5025 			m_copydata(control, at, cpsize, data);
5026 			return (1);
5027 		} else {
5028 			at += CMSG_ALIGN(cmh.cmsg_len);
5029 			if (cmh.cmsg_len == 0) {
5030 				break;
5031 			}
5032 		}
5033 	}
5034 	/* not found */
5035 	return (0);
5036 }
5037 
5038 static struct mbuf *
5039 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
5040     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
5041 {
5042 	struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
5043 	struct sctp_state_cookie *stc;
5044 	struct sctp_paramhdr *ph;
5045 	uint8_t *foo;
5046 	int sig_offset;
5047 	uint16_t cookie_sz;
5048 
5049 	mret = NULL;
5050 	mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
5051 	    sizeof(struct sctp_paramhdr)), 0,
5052 	    M_DONTWAIT, 1, MT_DATA);
5053 	if (mret == NULL) {
5054 		return (NULL);
5055 	}
5056 	copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
5057 	if (copy_init == NULL) {
5058 		sctp_m_freem(mret);
5059 		return (NULL);
5060 	}
5061 #ifdef SCTP_MBUF_LOGGING
5062 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5063 		struct mbuf *mat;
5064 
5065 		mat = copy_init;
5066 		while (mat) {
5067 			if (SCTP_BUF_IS_EXTENDED(mat)) {
5068 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5069 			}
5070 			mat = SCTP_BUF_NEXT(mat);
5071 		}
5072 	}
5073 #endif
5074 	copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
5075 	    M_DONTWAIT);
5076 	if (copy_initack == NULL) {
5077 		sctp_m_freem(mret);
5078 		sctp_m_freem(copy_init);
5079 		return (NULL);
5080 	}
5081 #ifdef SCTP_MBUF_LOGGING
5082 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5083 		struct mbuf *mat;
5084 
5085 		mat = copy_initack;
5086 		while (mat) {
5087 			if (SCTP_BUF_IS_EXTENDED(mat)) {
5088 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5089 			}
5090 			mat = SCTP_BUF_NEXT(mat);
5091 		}
5092 	}
5093 #endif
5094 	/* easy side we just drop it on the end */
5095 	ph = mtod(mret, struct sctp_paramhdr *);
5096 	SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
5097 	    sizeof(struct sctp_paramhdr);
5098 	stc = (struct sctp_state_cookie *)((caddr_t)ph +
5099 	    sizeof(struct sctp_paramhdr));
5100 	ph->param_type = htons(SCTP_STATE_COOKIE);
5101 	ph->param_length = 0;	/* fill in at the end */
5102 	/* Fill in the stc cookie data */
5103 	memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
5104 
5105 	/* tack the INIT and then the INIT-ACK onto the chain */
5106 	cookie_sz = 0;
5107 	m_at = mret;
5108 	for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
5109 		cookie_sz += SCTP_BUF_LEN(m_at);
5110 		if (SCTP_BUF_NEXT(m_at) == NULL) {
5111 			SCTP_BUF_NEXT(m_at) = copy_init;
5112 			break;
5113 		}
5114 	}
5115 
5116 	for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
5117 		cookie_sz += SCTP_BUF_LEN(m_at);
5118 		if (SCTP_BUF_NEXT(m_at) == NULL) {
5119 			SCTP_BUF_NEXT(m_at) = copy_initack;
5120 			break;
5121 		}
5122 	}
5123 
5124 	for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
5125 		cookie_sz += SCTP_BUF_LEN(m_at);
5126 		if (SCTP_BUF_NEXT(m_at) == NULL) {
5127 			break;
5128 		}
5129 	}
5130 	sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
5131 	if (sig == NULL) {
5132 		/* no space, so free the entire chain */
5133 		sctp_m_freem(mret);
5134 		return (NULL);
5135 	}
5136 	SCTP_BUF_LEN(sig) = 0;
5137 	SCTP_BUF_NEXT(m_at) = sig;
5138 	sig_offset = 0;
5139 	foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
5140 	memset(foo, 0, SCTP_SIGNATURE_SIZE);
5141 	*signature = foo;
5142 	SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
5143 	cookie_sz += SCTP_SIGNATURE_SIZE;
5144 	ph->param_length = htons(cookie_sz);
5145 	return (mret);
5146 }
5147 
5148 
5149 static uint8_t
5150 sctp_get_ect(struct sctp_tcb *stcb,
5151     struct sctp_tmit_chunk *chk)
5152 {
5153 	uint8_t this_random;
5154 
5155 	/* Huh? */
5156 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
5157 		return (0);
5158 
5159 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
5160 		/* no nonce, always return ECT0 */
5161 		return (SCTP_ECT0_BIT);
5162 
5163 	if (stcb->asoc.peer_supports_ecn_nonce == 0) {
5164 		/* Peer does NOT support it, so we send a ECT0 only */
5165 		return (SCTP_ECT0_BIT);
5166 	}
5167 	if (chk == NULL)
5168 		return (SCTP_ECT0_BIT);
5169 
5170 	if ((stcb->asoc.hb_random_idx > 3) ||
5171 	    ((stcb->asoc.hb_random_idx == 3) &&
5172 	    (stcb->asoc.hb_ect_randombit > 7))) {
5173 		uint32_t rndval;
5174 
5175 warp_drive_sa:
5176 		rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
5177 		memcpy(stcb->asoc.hb_random_values, &rndval,
5178 		    sizeof(stcb->asoc.hb_random_values));
5179 		this_random = stcb->asoc.hb_random_values[0];
5180 		stcb->asoc.hb_random_idx = 0;
5181 		stcb->asoc.hb_ect_randombit = 0;
5182 	} else {
5183 		if (stcb->asoc.hb_ect_randombit > 7) {
5184 			stcb->asoc.hb_ect_randombit = 0;
5185 			stcb->asoc.hb_random_idx++;
5186 			if (stcb->asoc.hb_random_idx > 3) {
5187 				goto warp_drive_sa;
5188 			}
5189 		}
5190 		this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
5191 	}
5192 	if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
5193 		if (chk != NULL)
5194 			/* ECN Nonce stuff */
5195 			chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
5196 		stcb->asoc.hb_ect_randombit++;
5197 		return (SCTP_ECT1_BIT);
5198 	} else {
5199 		stcb->asoc.hb_ect_randombit++;
5200 		return (SCTP_ECT0_BIT);
5201 	}
5202 }
5203 
5204 static int
5205 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
5206     struct sctp_tcb *stcb,	/* may be NULL */
5207     struct sctp_nets *net,
5208     struct sockaddr *to,
5209     struct mbuf *m,
5210     uint32_t auth_offset,
5211     struct sctp_auth_chunk *auth,
5212     uint16_t auth_keyid,
5213     int nofragment_flag,
5214     int ecn_ok,
5215     struct sctp_tmit_chunk *chk,
5216     int out_of_asoc_ok,
5217     uint16_t src_port,
5218     uint16_t dest_port,
5219     uint32_t v_tag,
5220     uint16_t port,
5221     int so_locked,
5222 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5223     SCTP_UNUSED
5224 #endif
5225     union sctp_sockstore *over_addr
5226 )
5227 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
5228 {
5229 	/*
5230 	 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
5231 	 * header WITH an SCTPHDR but no IP header, endpoint inp and sa
5232 	 * structure: - fill in the HMAC digest of any AUTH chunk in the
5233 	 * packet. - calculate and fill in the SCTP checksum. - prepend an
5234 	 * IP address header. - if boundall use INADDR_ANY. - if
5235 	 * boundspecific do source address selection. - set fragmentation
5236 	 * option for ipV4. - On return from IP output, check/adjust mtu
5237 	 * size of output interface and smallest_mtu size as well.
5238 	 */
5239 	/* Will need ifdefs around this */
5240 	struct mbuf *o_pak;
5241 	struct mbuf *newm;
5242 	struct sctphdr *sctphdr;
5243 	int packet_length;
5244 	int ret;
5245 	uint32_t vrf_id;
5246 	sctp_route_t *ro = NULL;
5247 	struct udphdr *udp = NULL;
5248 
5249 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5250 	struct socket *so = NULL;
5251 
5252 #endif
5253 
5254 	if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
5255 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
5256 		sctp_m_freem(m);
5257 		return (EFAULT);
5258 	}
5259 	if (stcb) {
5260 		vrf_id = stcb->asoc.vrf_id;
5261 	} else {
5262 		vrf_id = inp->def_vrf_id;
5263 	}
5264 
5265 	/* fill in the HMAC digest for any AUTH chunk in the packet */
5266 	if ((auth != NULL) && (stcb != NULL)) {
5267 		sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
5268 	}
5269 	if (to->sa_family == AF_INET) {
5270 		struct ip *ip = NULL;
5271 		sctp_route_t iproute;
5272 		uint8_t tos_value;
5273 		int len;
5274 
5275 		len = sizeof(struct ip) + sizeof(struct sctphdr);
5276 		if (port) {
5277 			len += sizeof(struct udphdr);
5278 		}
5279 		newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
5280 		if (newm == NULL) {
5281 			sctp_m_freem(m);
5282 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5283 			return (ENOMEM);
5284 		}
5285 		SCTP_ALIGN_TO_END(newm, len);
5286 		SCTP_BUF_LEN(newm) = len;
5287 		SCTP_BUF_NEXT(newm) = m;
5288 		m = newm;
5289 		packet_length = sctp_calculate_len(m);
5290 		ip = mtod(m, struct ip *);
5291 		ip->ip_v = IPVERSION;
5292 		ip->ip_hl = (sizeof(struct ip) >> 2);
5293 		if (net) {
5294 			tos_value = net->tos_flowlabel & 0x000000ff;
5295 		} else {
5296 			tos_value = inp->ip_inp.inp.inp_ip_tos;
5297 		}
5298 		if ((nofragment_flag) && (port == 0)) {
5299 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
5300 			ip->ip_off = IP_DF;
5301 #else
5302 			ip->ip_off = htons(IP_DF);
5303 #endif
5304 		} else
5305 			ip->ip_off = 0;
5306 
5307 		/* FreeBSD has a function for ip_id's */
5308 		ip->ip_id = ip_newid();
5309 
5310 		ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
5311 		ip->ip_len = packet_length;
5312 		if (stcb) {
5313 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
5314 				/* Enable ECN */
5315 				ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
5316 			} else {
5317 				/* No ECN */
5318 				ip->ip_tos = (u_char)(tos_value & 0xfc);
5319 			}
5320 		} else {
5321 			/* no association at all */
5322 			ip->ip_tos = (tos_value & 0xfc);
5323 		}
5324 		if (port) {
5325 			ip->ip_p = IPPROTO_UDP;
5326 		} else {
5327 			ip->ip_p = IPPROTO_SCTP;
5328 		}
5329 		ip->ip_sum = 0;
5330 		if (net == NULL) {
5331 			ro = &iproute;
5332 			memset(&iproute, 0, sizeof(iproute));
5333 			memcpy(&ro->ro_dst, to, to->sa_len);
5334 		} else {
5335 			ro = (sctp_route_t *) & net->ro;
5336 		}
5337 		/* Now the address selection part */
5338 		ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
5339 
5340 		/* call the routine to select the src address */
5341 		if (net && out_of_asoc_ok == 0) {
5342 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
5343 				sctp_free_ifa(net->ro._s_addr);
5344 				net->ro._s_addr = NULL;
5345 				net->src_addr_selected = 0;
5346 				if (ro->ro_rt) {
5347 					RTFREE(ro->ro_rt);
5348 					ro->ro_rt = NULL;
5349 				}
5350 			}
5351 			if (net->src_addr_selected == 0) {
5352 				/* Cache the source address */
5353 				net->ro._s_addr = sctp_source_address_selection(inp, stcb,
5354 				    ro, net, 0,
5355 				    vrf_id);
5356 				net->src_addr_selected = 1;
5357 			}
5358 			if (net->ro._s_addr == NULL) {
5359 				/* No route to host */
5360 				net->src_addr_selected = 0;
5361 				goto no_route;
5362 			}
5363 			ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
5364 		} else {
5365 			if (over_addr == NULL) {
5366 				struct sctp_ifa *_lsrc;
5367 
5368 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
5369 				    net,
5370 				    out_of_asoc_ok,
5371 				    vrf_id);
5372 				if (_lsrc == NULL) {
5373 					goto no_route;
5374 				}
5375 				ip->ip_src = _lsrc->address.sin.sin_addr;
5376 				sctp_free_ifa(_lsrc);
5377 			} else {
5378 				ip->ip_src = over_addr->sin.sin_addr;
5379 				SCTP_RTALLOC((&ro->ro_rt), vrf_id);
5380 			}
5381 		}
5382 		if (port) {
5383 			udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
5384 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
5385 			udp->uh_dport = port;
5386 			udp->uh_ulen = htons(packet_length - sizeof(struct ip));
5387 			udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
5388 			sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
5389 		} else {
5390 			sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
5391 		}
5392 
5393 		sctphdr->src_port = src_port;
5394 		sctphdr->dest_port = dest_port;
5395 		sctphdr->v_tag = v_tag;
5396 		sctphdr->checksum = 0;
5397 
5398 		/*
5399 		 * If source address selection fails and we find no route
5400 		 * then the ip_output should fail as well with a
5401 		 * NO_ROUTE_TO_HOST type error. We probably should catch
5402 		 * that somewhere and abort the association right away
5403 		 * (assuming this is an INIT being sent).
5404 		 */
5405 		if ((ro->ro_rt == NULL)) {
5406 			/*
5407 			 * src addr selection failed to find a route (or
5408 			 * valid source addr), so we can't get there from
5409 			 * here (yet)!
5410 			 */
5411 	no_route:
5412 			SCTPDBG(SCTP_DEBUG_OUTPUT1,
5413 			    "%s: dropped packet - no valid source addr\n",
5414 			    __FUNCTION__);
5415 			if (net) {
5416 				SCTPDBG(SCTP_DEBUG_OUTPUT1,
5417 				    "Destination was ");
5418 				SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
5419 				    &net->ro._l_addr.sa);
5420 				if (net->dest_state & SCTP_ADDR_CONFIRMED) {
5421 					if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
5422 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
5423 						sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
5424 						    stcb,
5425 						    SCTP_FAILED_THRESHOLD,
5426 						    (void *)net,
5427 						    so_locked);
5428 						net->dest_state &= ~SCTP_ADDR_REACHABLE;
5429 						net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
5430 						/*
5431 						 * JRS 5/14/07 - If a
5432 						 * destination is
5433 						 * unreachable, the PF bit
5434 						 * is turned off.  This
5435 						 * allows an unambiguous use
5436 						 * of the PF bit for
5437 						 * destinations that are
5438 						 * reachable but potentially
5439 						 * failed. If the
5440 						 * destination is set to the
5441 						 * unreachable state, also
5442 						 * set the destination to
5443 						 * the PF state.
5444 						 */
5445 						/*
5446 						 * Add debug message here if
5447 						 * destination is not in PF
5448 						 * state.
5449 						 */
5450 						/*
5451 						 * Stop any running T3
5452 						 * timers here?
5453 						 */
5454 						if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
5455 							net->dest_state &= ~SCTP_ADDR_PF;
5456 							SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
5457 							    net);
5458 						}
5459 					}
5460 				}
5461 				if (stcb) {
5462 					if (net == stcb->asoc.primary_destination) {
5463 						/* need a new primary */
5464 						struct sctp_nets *alt;
5465 
5466 						alt = sctp_find_alternate_net(stcb, net, 0);
5467 						if (alt != net) {
5468 							if (sctp_set_primary_addr(stcb,
5469 							    (struct sockaddr *)NULL,
5470 							    alt) == 0) {
5471 								net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
5472 								if (net->ro._s_addr) {
5473 									sctp_free_ifa(net->ro._s_addr);
5474 									net->ro._s_addr = NULL;
5475 								}
5476 								net->src_addr_selected = 0;
5477 							}
5478 						}
5479 					}
5480 				}
5481 			}
5482 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
5483 			sctp_m_freem(m);
5484 			return (EHOSTUNREACH);
5485 		}
5486 		if (ro != &iproute) {
5487 			memcpy(&iproute, ro, sizeof(*ro));
5488 		}
5489 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
5490 		    (uint32_t) (ntohl(ip->ip_src.s_addr)));
5491 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
5492 		    (uint32_t) (ntohl(ip->ip_dst.s_addr)));
5493 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
5494 		    ro->ro_rt);
5495 
5496 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
5497 			/* failed to prepend data, give up */
5498 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5499 			sctp_m_freem(m);
5500 			return (ENOMEM);
5501 		}
5502 #ifdef  SCTP_PACKET_LOGGING
5503 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5504 			sctp_packet_log(m, packet_length);
5505 #endif
5506 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
5507 		if (port) {
5508 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
5509 			    (stcb) &&
5510 			    (stcb->asoc.loopback_scope))) {
5511 				sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
5512 				SCTP_STAT_INCR(sctps_sendswcrc);
5513 			} else {
5514 				SCTP_STAT_INCR(sctps_sendnocrc);
5515 			}
5516 			SCTP_ENABLE_UDP_CSUM(o_pak);
5517 		} else {
5518 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
5519 			    (stcb) &&
5520 			    (stcb->asoc.loopback_scope))) {
5521 				m->m_pkthdr.csum_flags = CSUM_SCTP;
5522 				m->m_pkthdr.csum_data = 0;	/* FIXME MT */
5523 				SCTP_STAT_INCR(sctps_sendhwcrc);
5524 			} else {
5525 				SCTP_STAT_INCR(sctps_sendnocrc);
5526 			}
5527 		}
5528 		/* send it out.  table id is taken from stcb */
5529 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5530 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5531 			so = SCTP_INP_SO(inp);
5532 			SCTP_SOCKET_UNLOCK(so, 0);
5533 		}
5534 #endif
5535 		SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
5536 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5537 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5538 			atomic_add_int(&stcb->asoc.refcnt, 1);
5539 			SCTP_TCB_UNLOCK(stcb);
5540 			SCTP_SOCKET_LOCK(so, 0);
5541 			SCTP_TCB_LOCK(stcb);
5542 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
5543 		}
5544 #endif
5545 		SCTP_STAT_INCR(sctps_sendpackets);
5546 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
5547 		if (ret)
5548 			SCTP_STAT_INCR(sctps_senderrors);
5549 
5550 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
5551 		if (net == NULL) {
5552 			/* free tempy routes */
5553 			if (ro->ro_rt) {
5554 				RTFREE(ro->ro_rt);
5555 				ro->ro_rt = NULL;
5556 			}
5557 		} else {
5558 			/* PMTU check versus smallest asoc MTU goes here */
5559 			if ((ro->ro_rt != NULL) &&
5560 			    (net->ro._s_addr)) {
5561 				uint32_t mtu;
5562 
5563 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
5564 				if (net->port) {
5565 					mtu -= sizeof(struct udphdr);
5566 				}
5567 				if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
5568 #ifdef SCTP_PRINT_FOR_B_AND_M
5569 					SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
5570 #endif
5571 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
5572 					net->mtu = mtu;
5573 				}
5574 			} else if (ro->ro_rt == NULL) {
5575 				/* route was freed */
5576 				if (net->ro._s_addr &&
5577 				    net->src_addr_selected) {
5578 					sctp_free_ifa(net->ro._s_addr);
5579 					net->ro._s_addr = NULL;
5580 				}
5581 				net->src_addr_selected = 0;
5582 			}
5583 		}
5584 		return (ret);
5585 	}
5586 #ifdef INET6
5587 	else if (to->sa_family == AF_INET6) {
5588 		uint32_t flowlabel;
5589 		struct ip6_hdr *ip6h;
5590 		struct route_in6 ip6route;
5591 		struct ifnet *ifp;
5592 		u_char flowTop;
5593 		uint16_t flowBottom;
5594 		u_char tosBottom, tosTop;
5595 		struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
5596 		int prev_scope = 0;
5597 		struct sockaddr_in6 lsa6_storage;
5598 		int error;
5599 		u_short prev_port = 0;
5600 		int len;
5601 
5602 		if (net != NULL) {
5603 			flowlabel = net->tos_flowlabel;
5604 		} else {
5605 			flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
5606 		}
5607 
5608 		len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
5609 		if (port) {
5610 			len += sizeof(struct udphdr);
5611 		}
5612 		newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
5613 		if (newm == NULL) {
5614 			sctp_m_freem(m);
5615 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5616 			return (ENOMEM);
5617 		}
5618 		SCTP_ALIGN_TO_END(newm, len);
5619 		SCTP_BUF_LEN(newm) = len;
5620 		SCTP_BUF_NEXT(newm) = m;
5621 		m = newm;
5622 		packet_length = sctp_calculate_len(m);
5623 
5624 		ip6h = mtod(m, struct ip6_hdr *);
5625 		/*
5626 		 * We assume here that inp_flow is in host byte order within
5627 		 * the TCB!
5628 		 */
5629 		flowBottom = flowlabel & 0x0000ffff;
5630 		flowTop = ((flowlabel & 0x000f0000) >> 16);
5631 		tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
5632 		/* protect *sin6 from overwrite */
5633 		sin6 = (struct sockaddr_in6 *)to;
5634 		tmp = *sin6;
5635 		sin6 = &tmp;
5636 
5637 		/* KAME hack: embed scopeid */
5638 		if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
5639 			SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5640 			return (EINVAL);
5641 		}
5642 		if (net == NULL) {
5643 			memset(&ip6route, 0, sizeof(ip6route));
5644 			ro = (sctp_route_t *) & ip6route;
5645 			memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
5646 		} else {
5647 			ro = (sctp_route_t *) & net->ro;
5648 		}
5649 		if (stcb != NULL) {
5650 			if ((stcb->asoc.ecn_allowed) && ecn_ok) {
5651 				/* Enable ECN */
5652 				tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
5653 			} else {
5654 				/* No ECN */
5655 				tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
5656 			}
5657 		} else {
5658 			/* we could get no asoc if it is a O-O-T-B packet */
5659 			tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
5660 		}
5661 		ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
5662 		if (port) {
5663 			ip6h->ip6_nxt = IPPROTO_UDP;
5664 		} else {
5665 			ip6h->ip6_nxt = IPPROTO_SCTP;
5666 		}
5667 		ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
5668 		ip6h->ip6_dst = sin6->sin6_addr;
5669 
5670 		/*
5671 		 * Add SRC address selection here: we can only reuse to a
5672 		 * limited degree the kame src-addr-sel, since we can try
5673 		 * their selection but it may not be bound.
5674 		 */
5675 		bzero(&lsa6_tmp, sizeof(lsa6_tmp));
5676 		lsa6_tmp.sin6_family = AF_INET6;
5677 		lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
5678 		lsa6 = &lsa6_tmp;
5679 		if (net && out_of_asoc_ok == 0) {
5680 			if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
5681 				sctp_free_ifa(net->ro._s_addr);
5682 				net->ro._s_addr = NULL;
5683 				net->src_addr_selected = 0;
5684 				if (ro->ro_rt) {
5685 					RTFREE(ro->ro_rt);
5686 					ro->ro_rt = NULL;
5687 				}
5688 			}
5689 			if (net->src_addr_selected == 0) {
5690 				sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
5691 				/* KAME hack: embed scopeid */
5692 				if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
5693 					SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5694 					return (EINVAL);
5695 				}
5696 				/* Cache the source address */
5697 				net->ro._s_addr = sctp_source_address_selection(inp,
5698 				    stcb,
5699 				    ro,
5700 				    net,
5701 				    0,
5702 				    vrf_id);
5703 				(void)sa6_recoverscope(sin6);
5704 				net->src_addr_selected = 1;
5705 			}
5706 			if (net->ro._s_addr == NULL) {
5707 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
5708 				net->src_addr_selected = 0;
5709 				goto no_route;
5710 			}
5711 			lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
5712 		} else {
5713 			sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
5714 			/* KAME hack: embed scopeid */
5715 			if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
5716 				SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5717 				return (EINVAL);
5718 			}
5719 			if (over_addr == NULL) {
5720 				struct sctp_ifa *_lsrc;
5721 
5722 				_lsrc = sctp_source_address_selection(inp, stcb, ro,
5723 				    net,
5724 				    out_of_asoc_ok,
5725 				    vrf_id);
5726 				if (_lsrc == NULL) {
5727 					goto no_route;
5728 				}
5729 				lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
5730 				sctp_free_ifa(_lsrc);
5731 			} else {
5732 				lsa6->sin6_addr = over_addr->sin6.sin6_addr;
5733 				SCTP_RTALLOC((&ro->ro_rt), vrf_id);
5734 			}
5735 			(void)sa6_recoverscope(sin6);
5736 		}
5737 		lsa6->sin6_port = inp->sctp_lport;
5738 
5739 		if (ro->ro_rt == NULL) {
5740 			/*
5741 			 * src addr selection failed to find a route (or
5742 			 * valid source addr), so we can't get there from
5743 			 * here!
5744 			 */
5745 			goto no_route;
5746 		}
5747 		/*
5748 		 * XXX: sa6 may not have a valid sin6_scope_id in the
5749 		 * non-SCOPEDROUTING case.
5750 		 */
5751 		bzero(&lsa6_storage, sizeof(lsa6_storage));
5752 		lsa6_storage.sin6_family = AF_INET6;
5753 		lsa6_storage.sin6_len = sizeof(lsa6_storage);
5754 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
5755 		if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
5756 			SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
5757 			sctp_m_freem(m);
5758 			return (error);
5759 		}
5760 		/* XXX */
5761 		lsa6_storage.sin6_addr = lsa6->sin6_addr;
5762 		lsa6_storage.sin6_port = inp->sctp_lport;
5763 		lsa6 = &lsa6_storage;
5764 		ip6h->ip6_src = lsa6->sin6_addr;
5765 
5766 		if (port) {
5767 			udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
5768 			udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
5769 			udp->uh_dport = port;
5770 			udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
5771 			udp->uh_sum = 0;
5772 			sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
5773 		} else {
5774 			sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
5775 		}
5776 
5777 		sctphdr->src_port = src_port;
5778 		sctphdr->dest_port = dest_port;
5779 		sctphdr->v_tag = v_tag;
5780 		sctphdr->checksum = 0;
5781 
5782 		/*
5783 		 * We set the hop limit now since there is a good chance
5784 		 * that our ro pointer is now filled
5785 		 */
5786 		ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
5787 		ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
5788 
5789 #ifdef SCTP_DEBUG
5790 		/* Copy to be sure something bad is not happening */
5791 		sin6->sin6_addr = ip6h->ip6_dst;
5792 		lsa6->sin6_addr = ip6h->ip6_src;
5793 #endif
5794 
5795 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
5796 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
5797 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
5798 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
5799 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
5800 		if (net) {
5801 			sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
5802 			/* preserve the port and scope for link local send */
5803 			prev_scope = sin6->sin6_scope_id;
5804 			prev_port = sin6->sin6_port;
5805 		}
5806 		if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
5807 			/* failed to prepend data, give up */
5808 			sctp_m_freem(m);
5809 			SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5810 			return (ENOMEM);
5811 		}
5812 #ifdef  SCTP_PACKET_LOGGING
5813 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5814 			sctp_packet_log(m, packet_length);
5815 #endif
5816 		SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
5817 		if (port) {
5818 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
5819 			    (stcb) &&
5820 			    (stcb->asoc.loopback_scope))) {
5821 				sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
5822 				SCTP_STAT_INCR(sctps_sendswcrc);
5823 			} else {
5824 				SCTP_STAT_INCR(sctps_sendnocrc);
5825 			}
5826 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
5827 				udp->uh_sum = 0xffff;
5828 			}
5829 		} else {
5830 			if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
5831 			    (stcb) &&
5832 			    (stcb->asoc.loopback_scope))) {
5833 				m->m_pkthdr.csum_flags = CSUM_SCTP;
5834 				m->m_pkthdr.csum_data = 0;	/* FIXME MT */
5835 				SCTP_STAT_INCR(sctps_sendhwcrc);
5836 			} else {
5837 				SCTP_STAT_INCR(sctps_sendnocrc);
5838 			}
5839 		}
5840 		/* send it out. table id is taken from stcb */
5841 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5842 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5843 			so = SCTP_INP_SO(inp);
5844 			SCTP_SOCKET_UNLOCK(so, 0);
5845 		}
5846 #endif
5847 		SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
5848 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5849 		if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5850 			atomic_add_int(&stcb->asoc.refcnt, 1);
5851 			SCTP_TCB_UNLOCK(stcb);
5852 			SCTP_SOCKET_LOCK(so, 0);
5853 			SCTP_TCB_LOCK(stcb);
5854 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
5855 		}
5856 #endif
5857 		if (net) {
5858 			/* for link local this must be done */
5859 			sin6->sin6_scope_id = prev_scope;
5860 			sin6->sin6_port = prev_port;
5861 		}
5862 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
5863 		SCTP_STAT_INCR(sctps_sendpackets);
5864 		SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
5865 		if (ret) {
5866 			SCTP_STAT_INCR(sctps_senderrors);
5867 		}
5868 		if (net == NULL) {
5869 			/* Now if we had a temp route free it */
5870 			if (ro->ro_rt) {
5871 				RTFREE(ro->ro_rt);
5872 			}
5873 		} else {
5874 			/* PMTU check versus smallest asoc MTU goes here */
5875 			if (ro->ro_rt == NULL) {
5876 				/* Route was freed */
5877 				if (net->ro._s_addr &&
5878 				    net->src_addr_selected) {
5879 					sctp_free_ifa(net->ro._s_addr);
5880 					net->ro._s_addr = NULL;
5881 				}
5882 				net->src_addr_selected = 0;
5883 			}
5884 			if ((ro->ro_rt != NULL) &&
5885 			    (net->ro._s_addr)) {
5886 				uint32_t mtu;
5887 
5888 				mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
5889 				if (mtu &&
5890 				    (stcb->asoc.smallest_mtu > mtu)) {
5891 #ifdef SCTP_PRINT_FOR_B_AND_M
5892 					SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
5893 					    mtu);
5894 #endif
5895 					sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
5896 					net->mtu = mtu;
5897 					if (net->port) {
5898 						net->mtu -= sizeof(struct udphdr);
5899 					}
5900 				}
5901 			} else if (ifp) {
5902 				if (ND_IFINFO(ifp)->linkmtu &&
5903 				    (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
5904 #ifdef SCTP_PRINT_FOR_B_AND_M
5905 					SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
5906 					    ND_IFINFO(ifp)->linkmtu);
5907 #endif
5908 					sctp_mtu_size_reset(inp,
5909 					    &stcb->asoc,
5910 					    ND_IFINFO(ifp)->linkmtu);
5911 				}
5912 			}
5913 		}
5914 		return (ret);
5915 	}
5916 #endif
5917 	else {
5918 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
5919 		    ((struct sockaddr *)to)->sa_family);
5920 		sctp_m_freem(m);
5921 		SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
5922 		return (EFAULT);
5923 	}
5924 }
5925 
5926 
5927 void
5928 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
5929 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5930     SCTP_UNUSED
5931 #endif
5932 )
5933 {
5934 	struct mbuf *m, *m_at, *mp_last;
5935 	struct sctp_nets *net;
5936 	struct sctp_init_chunk *init;
5937 	struct sctp_supported_addr_param *sup_addr;
5938 	struct sctp_adaptation_layer_indication *ali;
5939 	struct sctp_ecn_supported_param *ecn;
5940 	struct sctp_prsctp_supported_param *prsctp;
5941 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
5942 	struct sctp_supported_chunk_types_param *pr_supported;
5943 	int cnt_inits_to = 0;
5944 	int padval, ret;
5945 	int num_ext;
5946 	int p_len;
5947 
5948 	/* INIT's always go to the primary (and usually ONLY address) */
5949 	mp_last = NULL;
5950 	net = stcb->asoc.primary_destination;
5951 	if (net == NULL) {
5952 		net = TAILQ_FIRST(&stcb->asoc.nets);
5953 		if (net == NULL) {
5954 			/* TSNH */
5955 			return;
5956 		}
5957 		/* we confirm any address we send an INIT to */
5958 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
5959 		(void)sctp_set_primary_addr(stcb, NULL, net);
5960 	} else {
5961 		/* we confirm any address we send an INIT to */
5962 		net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
5963 	}
5964 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
5965 #ifdef INET6
5966 	if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
5967 		/*
5968 		 * special hook, if we are sending to link local it will not
5969 		 * show up in our private address count.
5970 		 */
5971 		struct sockaddr_in6 *sin6l;
5972 
5973 		sin6l = &net->ro._l_addr.sin6;
5974 		if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
5975 			cnt_inits_to = 1;
5976 	}
5977 #endif
5978 	if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5979 		/* This case should not happen */
5980 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
5981 		return;
5982 	}
5983 	/* start the INIT timer */
5984 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
5985 
5986 	m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
5987 	if (m == NULL) {
5988 		/* No memory, INIT timer will re-attempt. */
5989 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
5990 		return;
5991 	}
5992 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5993 	/*
5994 	 * assume peer supports asconf in order to be able to queue local
5995 	 * address changes while an INIT is in flight and before the assoc
5996 	 * is established.
5997 	 */
5998 	stcb->asoc.peer_supports_asconf = 1;
5999 	/* Now lets put the SCTP header in place */
6000 	init = mtod(m, struct sctp_init_chunk *);
6001 	/* now the chunk header */
6002 	init->ch.chunk_type = SCTP_INITIATION;
6003 	init->ch.chunk_flags = 0;
6004 	/* fill in later from mbuf we build */
6005 	init->ch.chunk_length = 0;
6006 	/* place in my tag */
6007 	init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
6008 	/* set up some of the credits. */
6009 	init->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket),
6010 	    SCTP_MINIMAL_RWND));
6011 
6012 	init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
6013 	init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
6014 	init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
6015 	/* now the address restriction */
6016 	sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
6017 	    sizeof(*init));
6018 	sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
6019 #ifdef INET6
6020 	/* we support 2 types: IPv6/IPv4 */
6021 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
6022 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
6023 	sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
6024 #else
6025 	/* we support 1 type: IPv4 */
6026 	sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
6027 	sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
6028 	sup_addr->addr_type[1] = htons(0);	/* this is the padding */
6029 #endif
6030 	SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
6031 	/* adaptation layer indication parameter */
6032 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
6033 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
6034 	ali->ph.param_length = htons(sizeof(*ali));
6035 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
6036 	SCTP_BUF_LEN(m) += sizeof(*ali);
6037 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
6038 
6039 	if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
6040 		/* Add NAT friendly parameter */
6041 		struct sctp_paramhdr *ph;
6042 
6043 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6044 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
6045 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
6046 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
6047 		ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
6048 	}
6049 	/* now any cookie time extensions */
6050 	if (stcb->asoc.cookie_preserve_req) {
6051 		struct sctp_cookie_perserve_param *cookie_preserve;
6052 
6053 		cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
6054 		cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
6055 		cookie_preserve->ph.param_length = htons(
6056 		    sizeof(*cookie_preserve));
6057 		cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
6058 		SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
6059 		ecn = (struct sctp_ecn_supported_param *)(
6060 		    (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
6061 		stcb->asoc.cookie_preserve_req = 0;
6062 	}
6063 	/* ECN parameter */
6064 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
6065 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
6066 		ecn->ph.param_length = htons(sizeof(*ecn));
6067 		SCTP_BUF_LEN(m) += sizeof(*ecn);
6068 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
6069 		    sizeof(*ecn));
6070 	} else {
6071 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
6072 	}
6073 	/* And now tell the peer we do pr-sctp */
6074 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
6075 	prsctp->ph.param_length = htons(sizeof(*prsctp));
6076 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
6077 
6078 	/* And now tell the peer we do all the extensions */
6079 	pr_supported = (struct sctp_supported_chunk_types_param *)
6080 	    ((caddr_t)prsctp + sizeof(*prsctp));
6081 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
6082 	num_ext = 0;
6083 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
6084 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
6085 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
6086 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
6087 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
6088 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
6089 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
6090 	}
6091 	/*
6092 	 * EY  if the initiator supports nr_sacks, need to report that to
6093 	 * responder in INIT chunk
6094 	 */
6095 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off)) {
6096 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
6097 	}
6098 	p_len = sizeof(*pr_supported) + num_ext;
6099 	pr_supported->ph.param_length = htons(p_len);
6100 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
6101 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6102 
6103 
6104 	/* ECN nonce: And now tell the peer we support ECN nonce */
6105 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
6106 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
6107 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
6108 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
6109 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
6110 		SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
6111 	}
6112 	/* add authentication parameters */
6113 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
6114 		struct sctp_auth_random *randp;
6115 		struct sctp_auth_hmac_algo *hmacs;
6116 		struct sctp_auth_chunk_list *chunks;
6117 
6118 		/* attach RANDOM parameter, if available */
6119 		if (stcb->asoc.authinfo.random != NULL) {
6120 			randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6121 			p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
6122 #ifdef SCTP_AUTH_DRAFT_04
6123 			randp->ph.param_type = htons(SCTP_RANDOM);
6124 			randp->ph.param_length = htons(p_len);
6125 			bcopy(stcb->asoc.authinfo.random->key,
6126 			    randp->random_data,
6127 			    stcb->asoc.authinfo.random_len);
6128 #else
6129 			/* random key already contains the header */
6130 			bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
6131 #endif
6132 			/* zero out any padding required */
6133 			bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
6134 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6135 		}
6136 		/* add HMAC_ALGO parameter */
6137 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6138 		p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
6139 		    (uint8_t *) hmacs->hmac_ids);
6140 		if (p_len > 0) {
6141 			p_len += sizeof(*hmacs);
6142 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
6143 			hmacs->ph.param_length = htons(p_len);
6144 			/* zero out any padding required */
6145 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
6146 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6147 		}
6148 		/* add CHUNKS parameter */
6149 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6150 		p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
6151 		    chunks->chunk_types);
6152 		if (p_len > 0) {
6153 			p_len += sizeof(*chunks);
6154 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
6155 			chunks->ph.param_length = htons(p_len);
6156 			/* zero out any padding required */
6157 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
6158 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6159 		}
6160 	}
6161 	m_at = m;
6162 	/* now the addresses */
6163 	{
6164 		struct sctp_scoping scp;
6165 
6166 		/*
6167 		 * To optimize this we could put the scoping stuff into a
6168 		 * structure and remove the individual uint8's from the
6169 		 * assoc structure. Then we could just sifa in the address
6170 		 * within the stcb.. but for now this is a quick hack to get
6171 		 * the address stuff teased apart.
6172 		 */
6173 		scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
6174 		scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
6175 		scp.loopback_scope = stcb->asoc.loopback_scope;
6176 		scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
6177 		scp.local_scope = stcb->asoc.local_scope;
6178 		scp.site_scope = stcb->asoc.site_scope;
6179 
6180 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
6181 	}
6182 
6183 	/* calulate the size and update pkt header and chunk header */
6184 	p_len = 0;
6185 	for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
6186 		if (SCTP_BUF_NEXT(m_at) == NULL)
6187 			mp_last = m_at;
6188 		p_len += SCTP_BUF_LEN(m_at);
6189 	}
6190 	init->ch.chunk_length = htons(p_len);
6191 	/*
6192 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6193 	 * here since the timer will drive a retranmission.
6194 	 */
6195 
6196 	/* I don't expect this to execute but we will be safe here */
6197 	padval = p_len % 4;
6198 	if ((padval) && (mp_last)) {
6199 		/*
6200 		 * The compiler worries that mp_last may not be set even
6201 		 * though I think it is impossible :-> however we add
6202 		 * mp_last here just in case.
6203 		 */
6204 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
6205 		if (ret) {
6206 			/* Houston we have a problem, no space */
6207 			sctp_m_freem(m);
6208 			return;
6209 		}
6210 		p_len += padval;
6211 	}
6212 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
6213 	ret = sctp_lowlevel_chunk_output(inp, stcb, net,
6214 	    (struct sockaddr *)&net->ro._l_addr,
6215 	    m, 0, NULL, 0, 0, 0, NULL, 0,
6216 	    inp->sctp_lport, stcb->rport, htonl(0),
6217 	    net->port, so_locked, NULL);
6218 	SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
6219 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6220 	sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
6221 	(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6222 }
6223 
6224 struct mbuf *
6225 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
6226     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
6227 {
6228 	/*
6229 	 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
6230 	 * being equal to the beginning of the params i.e. (iphlen +
6231 	 * sizeof(struct sctp_init_msg) parse through the parameters to the
6232 	 * end of the mbuf verifying that all parameters are known.
6233 	 *
6234 	 * For unknown parameters build and return a mbuf with
6235 	 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
6236 	 * processing this chunk stop, and set *abort_processing to 1.
6237 	 *
6238 	 * By having param_offset be pre-set to where parameters begin it is
6239 	 * hoped that this routine may be reused in the future by new
6240 	 * features.
6241 	 */
6242 	struct sctp_paramhdr *phdr, params;
6243 
6244 	struct mbuf *mat, *op_err;
6245 	char tempbuf[SCTP_PARAM_BUFFER_SIZE];
6246 	int at, limit, pad_needed;
6247 	uint16_t ptype, plen, padded_size;
6248 	int err_at;
6249 
6250 	*abort_processing = 0;
6251 	mat = in_initpkt;
6252 	err_at = 0;
6253 	limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
6254 	at = param_offset;
6255 	op_err = NULL;
6256 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
6257 	phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
6258 	while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
6259 		ptype = ntohs(phdr->param_type);
6260 		plen = ntohs(phdr->param_length);
6261 		if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
6262 			/* wacked parameter */
6263 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
6264 			goto invalid_size;
6265 		}
6266 		limit -= SCTP_SIZE32(plen);
6267 		/*-
6268 		 * All parameters for all chunks that we know/understand are
6269 		 * listed here. We process them other places and make
6270 		 * appropriate stop actions per the upper bits. However this
6271 		 * is the generic routine processor's can call to get back
6272 		 * an operr.. to either incorporate (init-ack) or send.
6273 		 */
6274 		padded_size = SCTP_SIZE32(plen);
6275 		switch (ptype) {
6276 			/* Param's with variable size */
6277 		case SCTP_HEARTBEAT_INFO:
6278 		case SCTP_STATE_COOKIE:
6279 		case SCTP_UNRECOG_PARAM:
6280 		case SCTP_ERROR_CAUSE_IND:
6281 			/* ok skip fwd */
6282 			at += padded_size;
6283 			break;
6284 			/* Param's with variable size within a range */
6285 		case SCTP_CHUNK_LIST:
6286 		case SCTP_SUPPORTED_CHUNK_EXT:
6287 			if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
6288 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
6289 				goto invalid_size;
6290 			}
6291 			at += padded_size;
6292 			break;
6293 		case SCTP_SUPPORTED_ADDRTYPE:
6294 			if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
6295 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
6296 				goto invalid_size;
6297 			}
6298 			at += padded_size;
6299 			break;
6300 		case SCTP_RANDOM:
6301 			if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
6302 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
6303 				goto invalid_size;
6304 			}
6305 			at += padded_size;
6306 			break;
6307 		case SCTP_SET_PRIM_ADDR:
6308 		case SCTP_DEL_IP_ADDRESS:
6309 		case SCTP_ADD_IP_ADDRESS:
6310 			if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
6311 			    (padded_size != sizeof(struct sctp_asconf_addr_param))) {
6312 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
6313 				goto invalid_size;
6314 			}
6315 			at += padded_size;
6316 			break;
6317 			/* Param's with a fixed size */
6318 		case SCTP_IPV4_ADDRESS:
6319 			if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
6320 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
6321 				goto invalid_size;
6322 			}
6323 			at += padded_size;
6324 			break;
6325 		case SCTP_IPV6_ADDRESS:
6326 			if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
6327 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
6328 				goto invalid_size;
6329 			}
6330 			at += padded_size;
6331 			break;
6332 		case SCTP_COOKIE_PRESERVE:
6333 			if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
6334 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
6335 				goto invalid_size;
6336 			}
6337 			at += padded_size;
6338 			break;
6339 		case SCTP_HAS_NAT_SUPPORT:
6340 			*nat_friendly = 1;
6341 			/* fall through */
6342 		case SCTP_ECN_NONCE_SUPPORTED:
6343 		case SCTP_PRSCTP_SUPPORTED:
6344 
6345 			if (padded_size != sizeof(struct sctp_paramhdr)) {
6346 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
6347 				goto invalid_size;
6348 			}
6349 			at += padded_size;
6350 			break;
6351 		case SCTP_ECN_CAPABLE:
6352 			if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
6353 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
6354 				goto invalid_size;
6355 			}
6356 			at += padded_size;
6357 			break;
6358 		case SCTP_ULP_ADAPTATION:
6359 			if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
6360 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
6361 				goto invalid_size;
6362 			}
6363 			at += padded_size;
6364 			break;
6365 		case SCTP_SUCCESS_REPORT:
6366 			if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
6367 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
6368 				goto invalid_size;
6369 			}
6370 			at += padded_size;
6371 			break;
6372 		case SCTP_HOSTNAME_ADDRESS:
6373 			{
6374 				/* We can NOT handle HOST NAME addresses!! */
6375 				int l_len;
6376 
6377 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
6378 				*abort_processing = 1;
6379 				if (op_err == NULL) {
6380 					/* Ok need to try to get a mbuf */
6381 #ifdef INET6
6382 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6383 #else
6384 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6385 #endif
6386 					l_len += plen;
6387 					l_len += sizeof(struct sctp_paramhdr);
6388 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
6389 					if (op_err) {
6390 						SCTP_BUF_LEN(op_err) = 0;
6391 						/*
6392 						 * pre-reserve space for ip
6393 						 * and sctp header  and
6394 						 * chunk hdr
6395 						 */
6396 #ifdef INET6
6397 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
6398 #else
6399 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
6400 #endif
6401 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
6402 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
6403 					}
6404 				}
6405 				if (op_err) {
6406 					/* If we have space */
6407 					struct sctp_paramhdr s;
6408 
6409 					if (err_at % 4) {
6410 						uint32_t cpthis = 0;
6411 
6412 						pad_needed = 4 - (err_at % 4);
6413 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
6414 						err_at += pad_needed;
6415 					}
6416 					s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
6417 					s.param_length = htons(sizeof(s) + plen);
6418 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
6419 					err_at += sizeof(s);
6420 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
6421 					if (phdr == NULL) {
6422 						sctp_m_freem(op_err);
6423 						/*
6424 						 * we are out of memory but
6425 						 * we still need to have a
6426 						 * look at what to do (the
6427 						 * system is in trouble
6428 						 * though).
6429 						 */
6430 						return (NULL);
6431 					}
6432 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
6433 					err_at += plen;
6434 				}
6435 				return (op_err);
6436 				break;
6437 			}
6438 		default:
6439 			/*
6440 			 * we do not recognize the parameter figure out what
6441 			 * we do.
6442 			 */
6443 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
6444 			if ((ptype & 0x4000) == 0x4000) {
6445 				/* Report bit is set?? */
6446 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
6447 				if (op_err == NULL) {
6448 					int l_len;
6449 
6450 					/* Ok need to try to get an mbuf */
6451 #ifdef INET6
6452 					l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6453 #else
6454 					l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6455 #endif
6456 					l_len += plen;
6457 					l_len += sizeof(struct sctp_paramhdr);
6458 					op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
6459 					if (op_err) {
6460 						SCTP_BUF_LEN(op_err) = 0;
6461 #ifdef INET6
6462 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
6463 #else
6464 						SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
6465 #endif
6466 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
6467 						SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
6468 					}
6469 				}
6470 				if (op_err) {
6471 					/* If we have space */
6472 					struct sctp_paramhdr s;
6473 
6474 					if (err_at % 4) {
6475 						uint32_t cpthis = 0;
6476 
6477 						pad_needed = 4 - (err_at % 4);
6478 						m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
6479 						err_at += pad_needed;
6480 					}
6481 					s.param_type = htons(SCTP_UNRECOG_PARAM);
6482 					s.param_length = htons(sizeof(s) + plen);
6483 					m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
6484 					err_at += sizeof(s);
6485 					if (plen > sizeof(tempbuf)) {
6486 						plen = sizeof(tempbuf);
6487 					}
6488 					phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
6489 					if (phdr == NULL) {
6490 						sctp_m_freem(op_err);
6491 						/*
6492 						 * we are out of memory but
6493 						 * we still need to have a
6494 						 * look at what to do (the
6495 						 * system is in trouble
6496 						 * though).
6497 						 */
6498 						op_err = NULL;
6499 						goto more_processing;
6500 					}
6501 					m_copyback(op_err, err_at, plen, (caddr_t)phdr);
6502 					err_at += plen;
6503 				}
6504 			}
6505 	more_processing:
6506 			if ((ptype & 0x8000) == 0x0000) {
6507 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
6508 				return (op_err);
6509 			} else {
6510 				/* skip this chunk and continue processing */
6511 				SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
6512 				at += SCTP_SIZE32(plen);
6513 			}
6514 			break;
6515 
6516 		}
6517 		phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
6518 	}
6519 	return (op_err);
6520 invalid_size:
6521 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
6522 	*abort_processing = 1;
6523 	if ((op_err == NULL) && phdr) {
6524 		int l_len;
6525 
6526 #ifdef INET6
6527 		l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6528 #else
6529 		l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6530 #endif
6531 		l_len += (2 * sizeof(struct sctp_paramhdr));
6532 		op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
6533 		if (op_err) {
6534 			SCTP_BUF_LEN(op_err) = 0;
6535 #ifdef INET6
6536 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
6537 #else
6538 			SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
6539 #endif
6540 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
6541 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
6542 		}
6543 	}
6544 	if ((op_err) && phdr) {
6545 		struct sctp_paramhdr s;
6546 
6547 		if (err_at % 4) {
6548 			uint32_t cpthis = 0;
6549 
6550 			pad_needed = 4 - (err_at % 4);
6551 			m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
6552 			err_at += pad_needed;
6553 		}
6554 		s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6555 		s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
6556 		m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
6557 		err_at += sizeof(s);
6558 		/* Only copy back the p-hdr that caused the issue */
6559 		m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
6560 	}
6561 	return (op_err);
6562 }
6563 
6564 static int
6565 sctp_are_there_new_addresses(struct sctp_association *asoc,
6566     struct mbuf *in_initpkt, int iphlen, int offset)
6567 {
6568 	/*
6569 	 * Given a INIT packet, look through the packet to verify that there
6570 	 * are NO new addresses. As we go through the parameters add reports
6571 	 * of any un-understood parameters that require an error.  Also we
6572 	 * must return (1) to drop the packet if we see a un-understood
6573 	 * parameter that tells us to drop the chunk.
6574 	 */
6575 	struct sockaddr_in sin4, *sa4;
6576 
6577 #ifdef INET6
6578 	struct sockaddr_in6 sin6, *sa6;
6579 
6580 #endif
6581 	struct sockaddr *sa_touse;
6582 	struct sockaddr *sa;
6583 	struct sctp_paramhdr *phdr, params;
6584 	struct ip *iph;
6585 
6586 #ifdef INET6
6587 	struct ip6_hdr *ip6h;
6588 
6589 #endif
6590 	struct mbuf *mat;
6591 	uint16_t ptype, plen;
6592 	int err_at;
6593 	uint8_t fnd;
6594 	struct sctp_nets *net;
6595 
6596 	memset(&sin4, 0, sizeof(sin4));
6597 #ifdef INET6
6598 	memset(&sin6, 0, sizeof(sin6));
6599 #endif
6600 	sin4.sin_family = AF_INET;
6601 	sin4.sin_len = sizeof(sin4);
6602 #ifdef INET6
6603 	sin6.sin6_family = AF_INET6;
6604 	sin6.sin6_len = sizeof(sin6);
6605 #endif
6606 	sa_touse = NULL;
6607 	/* First what about the src address of the pkt ? */
6608 	iph = mtod(in_initpkt, struct ip *);
6609 	switch (iph->ip_v) {
6610 	case IPVERSION:
6611 		/* source addr is IPv4 */
6612 		sin4.sin_addr = iph->ip_src;
6613 		sa_touse = (struct sockaddr *)&sin4;
6614 		break;
6615 #ifdef INET6
6616 	case IPV6_VERSION >> 4:
6617 		/* source addr is IPv6 */
6618 		ip6h = mtod(in_initpkt, struct ip6_hdr *);
6619 		sin6.sin6_addr = ip6h->ip6_src;
6620 		sa_touse = (struct sockaddr *)&sin6;
6621 		break;
6622 #endif
6623 	default:
6624 		return (1);
6625 	}
6626 
6627 	fnd = 0;
6628 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6629 		sa = (struct sockaddr *)&net->ro._l_addr;
6630 		if (sa->sa_family == sa_touse->sa_family) {
6631 			if (sa->sa_family == AF_INET) {
6632 				sa4 = (struct sockaddr_in *)sa;
6633 				if (sa4->sin_addr.s_addr ==
6634 				    sin4.sin_addr.s_addr) {
6635 					fnd = 1;
6636 					break;
6637 				}
6638 			}
6639 #ifdef INET6
6640 			if (sa->sa_family == AF_INET6) {
6641 				sa6 = (struct sockaddr_in6 *)sa;
6642 				if (SCTP6_ARE_ADDR_EQUAL(sa6,
6643 				    &sin6)) {
6644 					fnd = 1;
6645 					break;
6646 				}
6647 			}
6648 #endif
6649 		}
6650 	}
6651 	if (fnd == 0) {
6652 		/* New address added! no need to look futher. */
6653 		return (1);
6654 	}
6655 	/* Ok so far lets munge through the rest of the packet */
6656 	mat = in_initpkt;
6657 	err_at = 0;
6658 	sa_touse = NULL;
6659 	offset += sizeof(struct sctp_init_chunk);
6660 	phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
6661 	while (phdr) {
6662 		ptype = ntohs(phdr->param_type);
6663 		plen = ntohs(phdr->param_length);
6664 		if (ptype == SCTP_IPV4_ADDRESS) {
6665 			struct sctp_ipv4addr_param *p4, p4_buf;
6666 
6667 			phdr = sctp_get_next_param(mat, offset,
6668 			    (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
6669 			if (plen != sizeof(struct sctp_ipv4addr_param) ||
6670 			    phdr == NULL) {
6671 				return (1);
6672 			}
6673 			p4 = (struct sctp_ipv4addr_param *)phdr;
6674 			sin4.sin_addr.s_addr = p4->addr;
6675 			sa_touse = (struct sockaddr *)&sin4;
6676 		} else if (ptype == SCTP_IPV6_ADDRESS) {
6677 			struct sctp_ipv6addr_param *p6, p6_buf;
6678 
6679 			phdr = sctp_get_next_param(mat, offset,
6680 			    (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
6681 			if (plen != sizeof(struct sctp_ipv6addr_param) ||
6682 			    phdr == NULL) {
6683 				return (1);
6684 			}
6685 			p6 = (struct sctp_ipv6addr_param *)phdr;
6686 #ifdef INET6
6687 			memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
6688 			    sizeof(p6->addr));
6689 #endif
6690 			sa_touse = (struct sockaddr *)&sin4;
6691 		}
6692 		if (sa_touse) {
6693 			/* ok, sa_touse points to one to check */
6694 			fnd = 0;
6695 			TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6696 				sa = (struct sockaddr *)&net->ro._l_addr;
6697 				if (sa->sa_family != sa_touse->sa_family) {
6698 					continue;
6699 				}
6700 				if (sa->sa_family == AF_INET) {
6701 					sa4 = (struct sockaddr_in *)sa;
6702 					if (sa4->sin_addr.s_addr ==
6703 					    sin4.sin_addr.s_addr) {
6704 						fnd = 1;
6705 						break;
6706 					}
6707 				}
6708 #ifdef INET6
6709 				if (sa->sa_family == AF_INET6) {
6710 					sa6 = (struct sockaddr_in6 *)sa;
6711 					if (SCTP6_ARE_ADDR_EQUAL(
6712 					    sa6, &sin6)) {
6713 						fnd = 1;
6714 						break;
6715 					}
6716 				}
6717 #endif
6718 			}
6719 			if (!fnd) {
6720 				/* New addr added! no need to look further */
6721 				return (1);
6722 			}
6723 		}
6724 		offset += SCTP_SIZE32(plen);
6725 		phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
6726 	}
6727 	return (0);
6728 }
6729 
6730 /*
6731  * Given a MBUF chain that was sent into us containing an INIT. Build a
6732  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
6733  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
6734  * message (i.e. the struct sctp_init_msg).
6735  */
6736 void
6737 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
6738     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
6739     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
6740 {
6741 	struct sctp_association *asoc;
6742 	struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
6743 	struct sctp_init_ack_chunk *initack;
6744 	struct sctp_adaptation_layer_indication *ali;
6745 	struct sctp_ecn_supported_param *ecn;
6746 	struct sctp_prsctp_supported_param *prsctp;
6747 	struct sctp_ecn_nonce_supported_param *ecn_nonce;
6748 	struct sctp_supported_chunk_types_param *pr_supported;
6749 	union sctp_sockstore store, store1, *over_addr;
6750 	struct sockaddr_in *sin, *to_sin;
6751 
6752 #ifdef INET6
6753 	struct sockaddr_in6 *sin6, *to_sin6;
6754 
6755 #endif
6756 	struct ip *iph;
6757 
6758 #ifdef INET6
6759 	struct ip6_hdr *ip6;
6760 
6761 #endif
6762 	struct sockaddr *to;
6763 	struct sctp_state_cookie stc;
6764 	struct sctp_nets *net = NULL;
6765 	uint8_t *signature = NULL;
6766 	int cnt_inits_to = 0;
6767 	uint16_t his_limit, i_want;
6768 	int abort_flag, padval;
6769 	int num_ext;
6770 	int p_len;
6771 	int nat_friendly = 0;
6772 	struct socket *so;
6773 
6774 	if (stcb)
6775 		asoc = &stcb->asoc;
6776 	else
6777 		asoc = NULL;
6778 	mp_last = NULL;
6779 	if ((asoc != NULL) &&
6780 	    (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
6781 	    (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
6782 		/* new addresses, out of here in non-cookie-wait states */
6783 		/*
6784 		 * Send a ABORT, we don't add the new address error clause
6785 		 * though we even set the T bit and copy in the 0 tag.. this
6786 		 * looks no different than if no listener was present.
6787 		 */
6788 		sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
6789 		return;
6790 	}
6791 	abort_flag = 0;
6792 	op_err = sctp_arethere_unrecognized_parameters(init_pkt,
6793 	    (offset + sizeof(struct sctp_init_chunk)),
6794 	    &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
6795 	if (abort_flag) {
6796 do_a_abort:
6797 		sctp_send_abort(init_pkt, iphlen, sh,
6798 		    init_chk->init.initiate_tag, op_err, vrf_id, port);
6799 		return;
6800 	}
6801 	m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
6802 	if (m == NULL) {
6803 		/* No memory, INIT timer will re-attempt. */
6804 		if (op_err)
6805 			sctp_m_freem(op_err);
6806 		return;
6807 	}
6808 	SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
6809 
6810 	/* the time I built cookie */
6811 	(void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
6812 
6813 	/* populate any tie tags */
6814 	if (asoc != NULL) {
6815 		/* unlock before tag selections */
6816 		stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
6817 		stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
6818 		stc.cookie_life = asoc->cookie_life;
6819 		net = asoc->primary_destination;
6820 	} else {
6821 		stc.tie_tag_my_vtag = 0;
6822 		stc.tie_tag_peer_vtag = 0;
6823 		/* life I will award this cookie */
6824 		stc.cookie_life = inp->sctp_ep.def_cookie_life;
6825 	}
6826 
6827 	/* copy in the ports for later check */
6828 	stc.myport = sh->dest_port;
6829 	stc.peerport = sh->src_port;
6830 
6831 	/*
6832 	 * If we wanted to honor cookie life extentions, we would add to
6833 	 * stc.cookie_life. For now we should NOT honor any extension
6834 	 */
6835 	stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
6836 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6837 		struct inpcb *in_inp;
6838 
6839 		/* Its a V6 socket */
6840 		in_inp = (struct inpcb *)inp;
6841 		stc.ipv6_addr_legal = 1;
6842 		/* Now look at the binding flag to see if V4 will be legal */
6843 		if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
6844 			stc.ipv4_addr_legal = 1;
6845 		} else {
6846 			/* V4 addresses are NOT legal on the association */
6847 			stc.ipv4_addr_legal = 0;
6848 		}
6849 	} else {
6850 		/* Its a V4 socket, no - V6 */
6851 		stc.ipv4_addr_legal = 1;
6852 		stc.ipv6_addr_legal = 0;
6853 	}
6854 
6855 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
6856 	stc.ipv4_scope = 1;
6857 #else
6858 	stc.ipv4_scope = 0;
6859 #endif
6860 	/* now for scope setup */
6861 	memset((caddr_t)&store, 0, sizeof(store));
6862 	memset((caddr_t)&store1, 0, sizeof(store1));
6863 	sin = &store.sin;
6864 	to_sin = &store1.sin;
6865 #ifdef INET6
6866 	sin6 = &store.sin6;
6867 	to_sin6 = &store1.sin6;
6868 #endif
6869 	iph = mtod(init_pkt, struct ip *);
6870 	/* establish the to_addr's */
6871 	switch (iph->ip_v) {
6872 	case IPVERSION:
6873 		to_sin->sin_port = sh->dest_port;
6874 		to_sin->sin_family = AF_INET;
6875 		to_sin->sin_len = sizeof(struct sockaddr_in);
6876 		to_sin->sin_addr = iph->ip_dst;
6877 		break;
6878 #ifdef INET6
6879 	case IPV6_VERSION >> 4:
6880 		ip6 = mtod(init_pkt, struct ip6_hdr *);
6881 		to_sin6->sin6_addr = ip6->ip6_dst;
6882 		to_sin6->sin6_scope_id = 0;
6883 		to_sin6->sin6_port = sh->dest_port;
6884 		to_sin6->sin6_family = AF_INET6;
6885 		to_sin6->sin6_len = sizeof(struct sockaddr_in6);
6886 		break;
6887 #endif
6888 	default:
6889 		goto do_a_abort;
6890 		break;
6891 	};
6892 
6893 	if (net == NULL) {
6894 		to = (struct sockaddr *)&store;
6895 		switch (iph->ip_v) {
6896 		case IPVERSION:
6897 			{
6898 				sin->sin_family = AF_INET;
6899 				sin->sin_len = sizeof(struct sockaddr_in);
6900 				sin->sin_port = sh->src_port;
6901 				sin->sin_addr = iph->ip_src;
6902 				/* lookup address */
6903 				stc.address[0] = sin->sin_addr.s_addr;
6904 				stc.address[1] = 0;
6905 				stc.address[2] = 0;
6906 				stc.address[3] = 0;
6907 				stc.addr_type = SCTP_IPV4_ADDRESS;
6908 				/* local from address */
6909 				stc.laddress[0] = to_sin->sin_addr.s_addr;
6910 				stc.laddress[1] = 0;
6911 				stc.laddress[2] = 0;
6912 				stc.laddress[3] = 0;
6913 				stc.laddr_type = SCTP_IPV4_ADDRESS;
6914 				/* scope_id is only for v6 */
6915 				stc.scope_id = 0;
6916 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
6917 				if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
6918 					stc.ipv4_scope = 1;
6919 				}
6920 #else
6921 				stc.ipv4_scope = 1;
6922 #endif				/* SCTP_DONT_DO_PRIVADDR_SCOPE */
6923 				/* Must use the address in this case */
6924 				if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
6925 					stc.loopback_scope = 1;
6926 					stc.ipv4_scope = 1;
6927 					stc.site_scope = 1;
6928 					stc.local_scope = 0;
6929 				}
6930 				break;
6931 			}
6932 #ifdef INET6
6933 		case IPV6_VERSION >> 4:
6934 			{
6935 				ip6 = mtod(init_pkt, struct ip6_hdr *);
6936 				sin6->sin6_family = AF_INET6;
6937 				sin6->sin6_len = sizeof(struct sockaddr_in6);
6938 				sin6->sin6_port = sh->src_port;
6939 				sin6->sin6_addr = ip6->ip6_src;
6940 				/* lookup address */
6941 				memcpy(&stc.address, &sin6->sin6_addr,
6942 				    sizeof(struct in6_addr));
6943 				sin6->sin6_scope_id = 0;
6944 				stc.addr_type = SCTP_IPV6_ADDRESS;
6945 				stc.scope_id = 0;
6946 				if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
6947 					/*
6948 					 * FIX ME: does this have scope from
6949 					 * rcvif?
6950 					 */
6951 					(void)sa6_recoverscope(sin6);
6952 					stc.scope_id = sin6->sin6_scope_id;
6953 					sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
6954 					stc.loopback_scope = 1;
6955 					stc.local_scope = 0;
6956 					stc.site_scope = 1;
6957 					stc.ipv4_scope = 1;
6958 				} else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6959 					/*
6960 					 * If the new destination is a
6961 					 * LINK_LOCAL we must have common
6962 					 * both site and local scope. Don't
6963 					 * set local scope though since we
6964 					 * must depend on the source to be
6965 					 * added implicitly. We cannot
6966 					 * assure just because we share one
6967 					 * link that all links are common.
6968 					 */
6969 					stc.local_scope = 0;
6970 					stc.site_scope = 1;
6971 					stc.ipv4_scope = 1;
6972 					/*
6973 					 * we start counting for the private
6974 					 * address stuff at 1. since the
6975 					 * link local we source from won't
6976 					 * show up in our scoped count.
6977 					 */
6978 					cnt_inits_to = 1;
6979 					/*
6980 					 * pull out the scope_id from
6981 					 * incoming pkt
6982 					 */
6983 					/*
6984 					 * FIX ME: does this have scope from
6985 					 * rcvif?
6986 					 */
6987 					(void)sa6_recoverscope(sin6);
6988 					stc.scope_id = sin6->sin6_scope_id;
6989 					sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
6990 				} else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
6991 					/*
6992 					 * If the new destination is
6993 					 * SITE_LOCAL then we must have site
6994 					 * scope in common.
6995 					 */
6996 					stc.site_scope = 1;
6997 				}
6998 				memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
6999 				stc.laddr_type = SCTP_IPV6_ADDRESS;
7000 				break;
7001 			}
7002 #endif
7003 		default:
7004 			/* TSNH */
7005 			goto do_a_abort;
7006 			break;
7007 		}
7008 	} else {
7009 		/* set the scope per the existing tcb */
7010 
7011 #ifdef INET6
7012 		struct sctp_nets *lnet;
7013 
7014 #endif
7015 
7016 		stc.loopback_scope = asoc->loopback_scope;
7017 		stc.ipv4_scope = asoc->ipv4_local_scope;
7018 		stc.site_scope = asoc->site_scope;
7019 		stc.local_scope = asoc->local_scope;
7020 #ifdef INET6
7021 		/* Why do we not consider IPv4 LL addresses? */
7022 		TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
7023 			if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
7024 				if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
7025 					/*
7026 					 * if we have a LL address, start
7027 					 * counting at 1.
7028 					 */
7029 					cnt_inits_to = 1;
7030 				}
7031 			}
7032 		}
7033 #endif
7034 		/* use the net pointer */
7035 		to = (struct sockaddr *)&net->ro._l_addr;
7036 		switch (to->sa_family) {
7037 		case AF_INET:
7038 			sin = (struct sockaddr_in *)to;
7039 			stc.address[0] = sin->sin_addr.s_addr;
7040 			stc.address[1] = 0;
7041 			stc.address[2] = 0;
7042 			stc.address[3] = 0;
7043 			stc.addr_type = SCTP_IPV4_ADDRESS;
7044 			if (net->src_addr_selected == 0) {
7045 				/*
7046 				 * strange case here, the INIT should have
7047 				 * did the selection.
7048 				 */
7049 				net->ro._s_addr = sctp_source_address_selection(inp,
7050 				    stcb, (sctp_route_t *) & net->ro,
7051 				    net, 0, vrf_id);
7052 				if (net->ro._s_addr == NULL)
7053 					return;
7054 
7055 				net->src_addr_selected = 1;
7056 
7057 			}
7058 			stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
7059 			stc.laddress[1] = 0;
7060 			stc.laddress[2] = 0;
7061 			stc.laddress[3] = 0;
7062 			stc.laddr_type = SCTP_IPV4_ADDRESS;
7063 			break;
7064 #ifdef INET6
7065 		case AF_INET6:
7066 			sin6 = (struct sockaddr_in6 *)to;
7067 			memcpy(&stc.address, &sin6->sin6_addr,
7068 			    sizeof(struct in6_addr));
7069 			stc.addr_type = SCTP_IPV6_ADDRESS;
7070 			if (net->src_addr_selected == 0) {
7071 				/*
7072 				 * strange case here, the INIT should have
7073 				 * did the selection.
7074 				 */
7075 				net->ro._s_addr = sctp_source_address_selection(inp,
7076 				    stcb, (sctp_route_t *) & net->ro,
7077 				    net, 0, vrf_id);
7078 				if (net->ro._s_addr == NULL)
7079 					return;
7080 
7081 				net->src_addr_selected = 1;
7082 			}
7083 			memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
7084 			    sizeof(struct in6_addr));
7085 			stc.laddr_type = SCTP_IPV6_ADDRESS;
7086 			break;
7087 #endif
7088 		}
7089 	}
7090 	/* Now lets put the SCTP header in place */
7091 	initack = mtod(m, struct sctp_init_ack_chunk *);
7092 	/* Save it off for quick ref */
7093 	stc.peers_vtag = init_chk->init.initiate_tag;
7094 	/* who are we */
7095 	memcpy(stc.identification, SCTP_VERSION_STRING,
7096 	    min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
7097 	/* now the chunk header */
7098 	initack->ch.chunk_type = SCTP_INITIATION_ACK;
7099 	initack->ch.chunk_flags = 0;
7100 	/* fill in later from mbuf we build */
7101 	initack->ch.chunk_length = 0;
7102 	/* place in my tag */
7103 	if ((asoc != NULL) &&
7104 	    ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
7105 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
7106 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
7107 		/* re-use the v-tags and init-seq here */
7108 		initack->init.initiate_tag = htonl(asoc->my_vtag);
7109 		initack->init.initial_tsn = htonl(asoc->init_seq_number);
7110 	} else {
7111 		uint32_t vtag, itsn;
7112 
7113 		if (hold_inp_lock) {
7114 			SCTP_INP_INCR_REF(inp);
7115 			SCTP_INP_RUNLOCK(inp);
7116 		}
7117 		if (asoc) {
7118 			atomic_add_int(&asoc->refcnt, 1);
7119 			SCTP_TCB_UNLOCK(stcb);
7120 	new_tag:
7121 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
7122 			if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
7123 				/*
7124 				 * Got a duplicate vtag on some guy behind a
7125 				 * nat make sure we don't use it.
7126 				 */
7127 				goto new_tag;
7128 			}
7129 			initack->init.initiate_tag = htonl(vtag);
7130 			/* get a TSN to use too */
7131 			itsn = sctp_select_initial_TSN(&inp->sctp_ep);
7132 			initack->init.initial_tsn = htonl(itsn);
7133 			SCTP_TCB_LOCK(stcb);
7134 			atomic_add_int(&asoc->refcnt, -1);
7135 		} else {
7136 			vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
7137 			initack->init.initiate_tag = htonl(vtag);
7138 			/* get a TSN to use too */
7139 			initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
7140 		}
7141 		if (hold_inp_lock) {
7142 			SCTP_INP_RLOCK(inp);
7143 			SCTP_INP_DECR_REF(inp);
7144 		}
7145 	}
7146 	/* save away my tag to */
7147 	stc.my_vtag = initack->init.initiate_tag;
7148 
7149 	/* set up some of the credits. */
7150 	so = inp->sctp_socket;
7151 	if (so == NULL) {
7152 		/* memory problem */
7153 		sctp_m_freem(m);
7154 		return;
7155 	} else {
7156 		initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
7157 	}
7158 	/* set what I want */
7159 	his_limit = ntohs(init_chk->init.num_inbound_streams);
7160 	/* choose what I want */
7161 	if (asoc != NULL) {
7162 		if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
7163 			i_want = asoc->streamoutcnt;
7164 		} else {
7165 			i_want = inp->sctp_ep.pre_open_stream_count;
7166 		}
7167 	} else {
7168 		i_want = inp->sctp_ep.pre_open_stream_count;
7169 	}
7170 	if (his_limit < i_want) {
7171 		/* I Want more :< */
7172 		initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
7173 	} else {
7174 		/* I can have what I want :> */
7175 		initack->init.num_outbound_streams = htons(i_want);
7176 	}
7177 	/* tell him his limt. */
7178 	initack->init.num_inbound_streams =
7179 	    htons(inp->sctp_ep.max_open_streams_intome);
7180 
7181 	/* adaptation layer indication parameter */
7182 	ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
7183 	ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
7184 	ali->ph.param_length = htons(sizeof(*ali));
7185 	ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
7186 	SCTP_BUF_LEN(m) += sizeof(*ali);
7187 	ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
7188 
7189 	/* ECN parameter */
7190 	if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
7191 		ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
7192 		ecn->ph.param_length = htons(sizeof(*ecn));
7193 		SCTP_BUF_LEN(m) += sizeof(*ecn);
7194 
7195 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
7196 		    sizeof(*ecn));
7197 	} else {
7198 		prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
7199 	}
7200 	/* And now tell the peer we do  pr-sctp */
7201 	prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
7202 	prsctp->ph.param_length = htons(sizeof(*prsctp));
7203 	SCTP_BUF_LEN(m) += sizeof(*prsctp);
7204 	if (nat_friendly) {
7205 		/* Add NAT friendly parameter */
7206 		struct sctp_paramhdr *ph;
7207 
7208 		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7209 		ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
7210 		ph->param_length = htons(sizeof(struct sctp_paramhdr));
7211 		SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
7212 	}
7213 	/* And now tell the peer we do all the extensions */
7214 	pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7215 	pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
7216 	num_ext = 0;
7217 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
7218 	pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
7219 	pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
7220 	pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
7221 	pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
7222 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
7223 		pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
7224 	/*
7225 	 * EY  if the sysctl variable is set, tell the assoc. initiator that
7226 	 * we do nr_sack
7227 	 */
7228 	if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
7229 		pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
7230 	p_len = sizeof(*pr_supported) + num_ext;
7231 	pr_supported->ph.param_length = htons(p_len);
7232 	bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
7233 	SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7234 
7235 	/* ECN nonce: And now tell the peer we support ECN nonce */
7236 	if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
7237 		ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
7238 		    ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
7239 		ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
7240 		ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
7241 		SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
7242 	}
7243 	/* add authentication parameters */
7244 	if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
7245 		struct sctp_auth_random *randp;
7246 		struct sctp_auth_hmac_algo *hmacs;
7247 		struct sctp_auth_chunk_list *chunks;
7248 		uint16_t random_len;
7249 
7250 		/* generate and add RANDOM parameter */
7251 		random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
7252 		randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7253 		randp->ph.param_type = htons(SCTP_RANDOM);
7254 		p_len = sizeof(*randp) + random_len;
7255 		randp->ph.param_length = htons(p_len);
7256 		SCTP_READ_RANDOM(randp->random_data, random_len);
7257 		/* zero out any padding required */
7258 		bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
7259 		SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7260 
7261 		/* add HMAC_ALGO parameter */
7262 		hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7263 		p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
7264 		    (uint8_t *) hmacs->hmac_ids);
7265 		if (p_len > 0) {
7266 			p_len += sizeof(*hmacs);
7267 			hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
7268 			hmacs->ph.param_length = htons(p_len);
7269 			/* zero out any padding required */
7270 			bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
7271 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7272 		}
7273 		/* add CHUNKS parameter */
7274 		chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7275 		p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
7276 		    chunks->chunk_types);
7277 		if (p_len > 0) {
7278 			p_len += sizeof(*chunks);
7279 			chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
7280 			chunks->ph.param_length = htons(p_len);
7281 			/* zero out any padding required */
7282 			bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
7283 			SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7284 		}
7285 	}
7286 	m_at = m;
7287 	/* now the addresses */
7288 	{
7289 		struct sctp_scoping scp;
7290 
7291 		/*
7292 		 * To optimize this we could put the scoping stuff into a
7293 		 * structure and remove the individual uint8's from the stc
7294 		 * structure. Then we could just sifa in the address within
7295 		 * the stc.. but for now this is a quick hack to get the
7296 		 * address stuff teased apart.
7297 		 */
7298 		scp.ipv4_addr_legal = stc.ipv4_addr_legal;
7299 		scp.ipv6_addr_legal = stc.ipv6_addr_legal;
7300 		scp.loopback_scope = stc.loopback_scope;
7301 		scp.ipv4_local_scope = stc.ipv4_scope;
7302 		scp.local_scope = stc.local_scope;
7303 		scp.site_scope = stc.site_scope;
7304 		m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
7305 	}
7306 
7307 	/* tack on the operational error if present */
7308 	if (op_err) {
7309 		struct mbuf *ol;
7310 		int llen;
7311 
7312 		llen = 0;
7313 		ol = op_err;
7314 		while (ol) {
7315 			llen += SCTP_BUF_LEN(ol);
7316 			ol = SCTP_BUF_NEXT(ol);
7317 		}
7318 		if (llen % 4) {
7319 			/* must add a pad to the param */
7320 			uint32_t cpthis = 0;
7321 			int padlen;
7322 
7323 			padlen = 4 - (llen % 4);
7324 			m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
7325 		}
7326 		while (SCTP_BUF_NEXT(m_at) != NULL) {
7327 			m_at = SCTP_BUF_NEXT(m_at);
7328 		}
7329 		SCTP_BUF_NEXT(m_at) = op_err;
7330 		while (SCTP_BUF_NEXT(m_at) != NULL) {
7331 			m_at = SCTP_BUF_NEXT(m_at);
7332 		}
7333 	}
7334 	/* pre-calulate the size and update pkt header and chunk header */
7335 	p_len = 0;
7336 	for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
7337 		p_len += SCTP_BUF_LEN(m_tmp);
7338 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
7339 			/* m_tmp should now point to last one */
7340 			break;
7341 		}
7342 	}
7343 
7344 	/* Now we must build a cookie */
7345 	m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
7346 	if (m_cookie == NULL) {
7347 		/* memory problem */
7348 		sctp_m_freem(m);
7349 		return;
7350 	}
7351 	/* Now append the cookie to the end and update the space/size */
7352 	SCTP_BUF_NEXT(m_tmp) = m_cookie;
7353 
7354 	for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
7355 		p_len += SCTP_BUF_LEN(m_tmp);
7356 		if (SCTP_BUF_NEXT(m_tmp) == NULL) {
7357 			/* m_tmp should now point to last one */
7358 			mp_last = m_tmp;
7359 			break;
7360 		}
7361 	}
7362 	/*
7363 	 * Place in the size, but we don't include the last pad (if any) in
7364 	 * the INIT-ACK.
7365 	 */
7366 	initack->ch.chunk_length = htons(p_len);
7367 
7368 	/*
7369 	 * Time to sign the cookie, we don't sign over the cookie signature
7370 	 * though thus we set trailer.
7371 	 */
7372 	(void)sctp_hmac_m(SCTP_HMAC,
7373 	    (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
7374 	    SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
7375 	    (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
7376 	/*
7377 	 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
7378 	 * here since the timer will drive a retranmission.
7379 	 */
7380 	padval = p_len % 4;
7381 	if ((padval) && (mp_last)) {
7382 		/* see my previous comments on mp_last */
7383 		int ret;
7384 
7385 		ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
7386 		if (ret) {
7387 			/* Houston we have a problem, no space */
7388 			sctp_m_freem(m);
7389 			return;
7390 		}
7391 		p_len += padval;
7392 	}
7393 	if (stc.loopback_scope) {
7394 		over_addr = &store1;
7395 	} else {
7396 		over_addr = NULL;
7397 	}
7398 
7399 	(void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
7400 	    0, NULL, 0,
7401 	    inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
7402 	    port, SCTP_SO_NOT_LOCKED, over_addr);
7403 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7404 }
7405 
7406 
7407 void
7408 sctp_insert_on_wheel(struct sctp_tcb *stcb,
7409     struct sctp_association *asoc,
7410     struct sctp_stream_out *strq, int holds_lock)
7411 {
7412 	struct sctp_stream_out *stre, *strn;
7413 
7414 	if (holds_lock == 0) {
7415 		SCTP_TCB_SEND_LOCK(stcb);
7416 	}
7417 	if ((strq->next_spoke.tqe_next) ||
7418 	    (strq->next_spoke.tqe_prev)) {
7419 		/* already on wheel */
7420 		goto outof_here;
7421 	}
7422 	stre = TAILQ_FIRST(&asoc->out_wheel);
7423 	if (stre == NULL) {
7424 		/* only one on wheel */
7425 		TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
7426 		goto outof_here;
7427 	}
7428 	for (; stre; stre = strn) {
7429 		strn = TAILQ_NEXT(stre, next_spoke);
7430 		if (stre->stream_no > strq->stream_no) {
7431 			TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
7432 			goto outof_here;
7433 		} else if (stre->stream_no == strq->stream_no) {
7434 			/* huh, should not happen */
7435 			goto outof_here;
7436 		} else if (strn == NULL) {
7437 			/* next one is null */
7438 			TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
7439 			    next_spoke);
7440 		}
7441 	}
7442 outof_here:
7443 	if (holds_lock == 0) {
7444 		SCTP_TCB_SEND_UNLOCK(stcb);
7445 	}
7446 }
7447 
7448 static void
7449 sctp_remove_from_wheel(struct sctp_tcb *stcb,
7450     struct sctp_association *asoc,
7451     struct sctp_stream_out *strq)
7452 {
7453 	/* take off and then setup so we know it is not on the wheel */
7454 	SCTP_TCB_SEND_LOCK(stcb);
7455 	if (TAILQ_FIRST(&strq->outqueue)) {
7456 		/* more was added */
7457 		SCTP_TCB_SEND_UNLOCK(stcb);
7458 		return;
7459 	}
7460 	TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
7461 	strq->next_spoke.tqe_next = NULL;
7462 	strq->next_spoke.tqe_prev = NULL;
7463 	SCTP_TCB_SEND_UNLOCK(stcb);
7464 }
7465 
7466 static void
7467 sctp_prune_prsctp(struct sctp_tcb *stcb,
7468     struct sctp_association *asoc,
7469     struct sctp_sndrcvinfo *srcv,
7470     int dataout)
7471 {
7472 	int freed_spc = 0;
7473 	struct sctp_tmit_chunk *chk, *nchk;
7474 
7475 	SCTP_TCB_LOCK_ASSERT(stcb);
7476 	if ((asoc->peer_supports_prsctp) &&
7477 	    (asoc->sent_queue_cnt_removeable > 0)) {
7478 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
7479 			/*
7480 			 * Look for chunks marked with the PR_SCTP flag AND
7481 			 * the buffer space flag. If the one being sent is
7482 			 * equal or greater priority then purge the old one
7483 			 * and free some space.
7484 			 */
7485 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
7486 				/*
7487 				 * This one is PR-SCTP AND buffer space
7488 				 * limited type
7489 				 */
7490 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
7491 					/*
7492 					 * Lower numbers equates to higher
7493 					 * priority so if the one we are
7494 					 * looking at has a larger or equal
7495 					 * priority we want to drop the data
7496 					 * and NOT retransmit it.
7497 					 */
7498 					if (chk->data) {
7499 						/*
7500 						 * We release the book_size
7501 						 * if the mbuf is here
7502 						 */
7503 						int ret_spc;
7504 						int cause;
7505 
7506 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
7507 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
7508 						else
7509 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
7510 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
7511 						    cause,
7512 						    &asoc->sent_queue, SCTP_SO_LOCKED);
7513 						freed_spc += ret_spc;
7514 						if (freed_spc >= dataout) {
7515 							return;
7516 						}
7517 					}	/* if chunk was present */
7518 				}	/* if of sufficent priority */
7519 			}	/* if chunk has enabled */
7520 		}		/* tailqforeach */
7521 
7522 		chk = TAILQ_FIRST(&asoc->send_queue);
7523 		while (chk) {
7524 			nchk = TAILQ_NEXT(chk, sctp_next);
7525 			/* Here we must move to the sent queue and mark */
7526 			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
7527 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
7528 					if (chk->data) {
7529 						/*
7530 						 * We release the book_size
7531 						 * if the mbuf is here
7532 						 */
7533 						int ret_spc;
7534 
7535 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
7536 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
7537 						    &asoc->send_queue, SCTP_SO_LOCKED);
7538 
7539 						freed_spc += ret_spc;
7540 						if (freed_spc >= dataout) {
7541 							return;
7542 						}
7543 					}	/* end if chk->data */
7544 				}	/* end if right class */
7545 			}	/* end if chk pr-sctp */
7546 			chk = nchk;
7547 		}		/* end while (chk) */
7548 	}			/* if enabled in asoc */
7549 }
7550 
7551 int
7552 sctp_get_frag_point(struct sctp_tcb *stcb,
7553     struct sctp_association *asoc)
7554 {
7555 	int siz, ovh;
7556 
7557 	/*
7558 	 * For endpoints that have both v6 and v4 addresses we must reserve
7559 	 * room for the ipv6 header, for those that are only dealing with V4
7560 	 * we use a larger frag point.
7561 	 */
7562 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7563 		ovh = SCTP_MED_OVERHEAD;
7564 	} else {
7565 		ovh = SCTP_MED_V4_OVERHEAD;
7566 	}
7567 
7568 	if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
7569 		siz = asoc->smallest_mtu - ovh;
7570 	else
7571 		siz = (stcb->asoc.sctp_frag_point - ovh);
7572 	/*
7573 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
7574 	 */
7575 	/* A data chunk MUST fit in a cluster */
7576 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
7577 	/* } */
7578 
7579 	/* adjust for an AUTH chunk if DATA requires auth */
7580 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
7581 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7582 
7583 	if (siz % 4) {
7584 		/* make it an even word boundary please */
7585 		siz -= (siz % 4);
7586 	}
7587 	return (siz);
7588 }
7589 
7590 static void
7591 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
7592     struct sctp_stream_queue_pending *sp)
7593 {
7594 	sp->pr_sctp_on = 0;
7595 	if (stcb->asoc.peer_supports_prsctp) {
7596 		/*
7597 		 * We assume that the user wants PR_SCTP_TTL if the user
7598 		 * provides a positive lifetime but does not specify any
7599 		 * PR_SCTP policy. This is a BAD assumption and causes
7600 		 * problems at least with the U-Vancovers MPI folks. I will
7601 		 * change this to be no policy means NO PR-SCTP.
7602 		 */
7603 		if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
7604 			sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
7605 			sp->pr_sctp_on = 1;
7606 		} else {
7607 			return;
7608 		}
7609 		switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
7610 		case CHUNK_FLAGS_PR_SCTP_BUF:
7611 			/*
7612 			 * Time to live is a priority stored in tv_sec when
7613 			 * doing the buffer drop thing.
7614 			 */
7615 			sp->ts.tv_sec = sp->timetolive;
7616 			sp->ts.tv_usec = 0;
7617 			break;
7618 		case CHUNK_FLAGS_PR_SCTP_TTL:
7619 			{
7620 				struct timeval tv;
7621 
7622 				(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
7623 				tv.tv_sec = sp->timetolive / 1000;
7624 				tv.tv_usec = (sp->timetolive * 1000) % 1000000;
7625 				/*
7626 				 * TODO sctp_constants.h needs alternative
7627 				 * time macros when _KERNEL is undefined.
7628 				 */
7629 				timevaladd(&sp->ts, &tv);
7630 			}
7631 			break;
7632 		case CHUNK_FLAGS_PR_SCTP_RTX:
7633 			/*
7634 			 * Time to live is a the number or retransmissions
7635 			 * stored in tv_sec.
7636 			 */
7637 			sp->ts.tv_sec = sp->timetolive;
7638 			sp->ts.tv_usec = 0;
7639 			break;
7640 		default:
7641 			SCTPDBG(SCTP_DEBUG_USRREQ1,
7642 			    "Unknown PR_SCTP policy %u.\n",
7643 			    PR_SCTP_POLICY(sp->sinfo_flags));
7644 			break;
7645 		}
7646 	}
7647 }
7648 
7649 static int
7650 sctp_msg_append(struct sctp_tcb *stcb,
7651     struct sctp_nets *net,
7652     struct mbuf *m,
7653     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
7654 {
7655 	int error = 0, holds_lock;
7656 	struct mbuf *at;
7657 	struct sctp_stream_queue_pending *sp = NULL;
7658 	struct sctp_stream_out *strm;
7659 
7660 	/*
7661 	 * Given an mbuf chain, put it into the association send queue and
7662 	 * place it on the wheel
7663 	 */
7664 	holds_lock = hold_stcb_lock;
7665 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
7666 		/* Invalid stream number */
7667 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
7668 		error = EINVAL;
7669 		goto out_now;
7670 	}
7671 	if ((stcb->asoc.stream_locked) &&
7672 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
7673 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
7674 		error = EINVAL;
7675 		goto out_now;
7676 	}
7677 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
7678 	/* Now can we send this? */
7679 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
7680 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
7681 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7682 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
7683 		/* got data while shutting down */
7684 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
7685 		error = ECONNRESET;
7686 		goto out_now;
7687 	}
7688 	sctp_alloc_a_strmoq(stcb, sp);
7689 	if (sp == NULL) {
7690 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7691 		error = ENOMEM;
7692 		goto out_now;
7693 	}
7694 	sp->sinfo_flags = srcv->sinfo_flags;
7695 	sp->timetolive = srcv->sinfo_timetolive;
7696 	sp->ppid = srcv->sinfo_ppid;
7697 	sp->context = srcv->sinfo_context;
7698 	sp->strseq = 0;
7699 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
7700 		sp->net = net;
7701 		sp->addr_over = 1;
7702 	} else {
7703 		sp->net = stcb->asoc.primary_destination;
7704 		sp->addr_over = 0;
7705 	}
7706 	atomic_add_int(&sp->net->ref_count, 1);
7707 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
7708 	sp->stream = srcv->sinfo_stream;
7709 	sp->msg_is_complete = 1;
7710 	sp->sender_all_done = 1;
7711 	sp->some_taken = 0;
7712 	sp->data = m;
7713 	sp->tail_mbuf = NULL;
7714 	sp->length = 0;
7715 	at = m;
7716 	sctp_set_prsctp_policy(stcb, sp);
7717 	/*
7718 	 * We could in theory (for sendall) sifa the length in, but we would
7719 	 * still have to hunt through the chain since we need to setup the
7720 	 * tail_mbuf
7721 	 */
7722 	while (at) {
7723 		if (SCTP_BUF_NEXT(at) == NULL)
7724 			sp->tail_mbuf = at;
7725 		sp->length += SCTP_BUF_LEN(at);
7726 		at = SCTP_BUF_NEXT(at);
7727 	}
7728 	SCTP_TCB_SEND_LOCK(stcb);
7729 	sctp_snd_sb_alloc(stcb, sp->length);
7730 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
7731 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
7732 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
7733 		sp->strseq = strm->next_sequence_sent;
7734 		strm->next_sequence_sent++;
7735 	}
7736 	if ((strm->next_spoke.tqe_next == NULL) &&
7737 	    (strm->next_spoke.tqe_prev == NULL)) {
7738 		/* Not on wheel, insert */
7739 		sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
7740 	}
7741 	m = NULL;
7742 	SCTP_TCB_SEND_UNLOCK(stcb);
7743 out_now:
7744 	if (m) {
7745 		sctp_m_freem(m);
7746 	}
7747 	return (error);
7748 }
7749 
7750 
7751 static struct mbuf *
7752 sctp_copy_mbufchain(struct mbuf *clonechain,
7753     struct mbuf *outchain,
7754     struct mbuf **endofchain,
7755     int can_take_mbuf,
7756     int sizeofcpy,
7757     uint8_t copy_by_ref)
7758 {
7759 	struct mbuf *m;
7760 	struct mbuf *appendchain;
7761 	caddr_t cp;
7762 	int len;
7763 
7764 	if (endofchain == NULL) {
7765 		/* error */
7766 error_out:
7767 		if (outchain)
7768 			sctp_m_freem(outchain);
7769 		return (NULL);
7770 	}
7771 	if (can_take_mbuf) {
7772 		appendchain = clonechain;
7773 	} else {
7774 		if (!copy_by_ref &&
7775 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
7776 		    ) {
7777 			/* Its not in a cluster */
7778 			if (*endofchain == NULL) {
7779 				/* lets get a mbuf cluster */
7780 				if (outchain == NULL) {
7781 					/* This is the general case */
7782 			new_mbuf:
7783 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
7784 					if (outchain == NULL) {
7785 						goto error_out;
7786 					}
7787 					SCTP_BUF_LEN(outchain) = 0;
7788 					*endofchain = outchain;
7789 					/* get the prepend space */
7790 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
7791 				} else {
7792 					/*
7793 					 * We really should not get a NULL
7794 					 * in endofchain
7795 					 */
7796 					/* find end */
7797 					m = outchain;
7798 					while (m) {
7799 						if (SCTP_BUF_NEXT(m) == NULL) {
7800 							*endofchain = m;
7801 							break;
7802 						}
7803 						m = SCTP_BUF_NEXT(m);
7804 					}
7805 					/* sanity */
7806 					if (*endofchain == NULL) {
7807 						/*
7808 						 * huh, TSNH XXX maybe we
7809 						 * should panic
7810 						 */
7811 						sctp_m_freem(outchain);
7812 						goto new_mbuf;
7813 					}
7814 				}
7815 				/* get the new end of length */
7816 				len = M_TRAILINGSPACE(*endofchain);
7817 			} else {
7818 				/* how much is left at the end? */
7819 				len = M_TRAILINGSPACE(*endofchain);
7820 			}
7821 			/* Find the end of the data, for appending */
7822 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
7823 
7824 			/* Now lets copy it out */
7825 			if (len >= sizeofcpy) {
7826 				/* It all fits, copy it in */
7827 				m_copydata(clonechain, 0, sizeofcpy, cp);
7828 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
7829 			} else {
7830 				/* fill up the end of the chain */
7831 				if (len > 0) {
7832 					m_copydata(clonechain, 0, len, cp);
7833 					SCTP_BUF_LEN((*endofchain)) += len;
7834 					/* now we need another one */
7835 					sizeofcpy -= len;
7836 				}
7837 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
7838 				if (m == NULL) {
7839 					/* We failed */
7840 					goto error_out;
7841 				}
7842 				SCTP_BUF_NEXT((*endofchain)) = m;
7843 				*endofchain = m;
7844 				cp = mtod((*endofchain), caddr_t);
7845 				m_copydata(clonechain, len, sizeofcpy, cp);
7846 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
7847 			}
7848 			return (outchain);
7849 		} else {
7850 			/* copy the old fashion way */
7851 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
7852 #ifdef SCTP_MBUF_LOGGING
7853 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7854 				struct mbuf *mat;
7855 
7856 				mat = appendchain;
7857 				while (mat) {
7858 					if (SCTP_BUF_IS_EXTENDED(mat)) {
7859 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7860 					}
7861 					mat = SCTP_BUF_NEXT(mat);
7862 				}
7863 			}
7864 #endif
7865 		}
7866 	}
7867 	if (appendchain == NULL) {
7868 		/* error */
7869 		if (outchain)
7870 			sctp_m_freem(outchain);
7871 		return (NULL);
7872 	}
7873 	if (outchain) {
7874 		/* tack on to the end */
7875 		if (*endofchain != NULL) {
7876 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
7877 		} else {
7878 			m = outchain;
7879 			while (m) {
7880 				if (SCTP_BUF_NEXT(m) == NULL) {
7881 					SCTP_BUF_NEXT(m) = appendchain;
7882 					break;
7883 				}
7884 				m = SCTP_BUF_NEXT(m);
7885 			}
7886 		}
7887 		/*
7888 		 * save off the end and update the end-chain postion
7889 		 */
7890 		m = appendchain;
7891 		while (m) {
7892 			if (SCTP_BUF_NEXT(m) == NULL) {
7893 				*endofchain = m;
7894 				break;
7895 			}
7896 			m = SCTP_BUF_NEXT(m);
7897 		}
7898 		return (outchain);
7899 	} else {
7900 		/* save off the end and update the end-chain postion */
7901 		m = appendchain;
7902 		while (m) {
7903 			if (SCTP_BUF_NEXT(m) == NULL) {
7904 				*endofchain = m;
7905 				break;
7906 			}
7907 			m = SCTP_BUF_NEXT(m);
7908 		}
7909 		return (appendchain);
7910 	}
7911 }
7912 
7913 int
7914 sctp_med_chunk_output(struct sctp_inpcb *inp,
7915     struct sctp_tcb *stcb,
7916     struct sctp_association *asoc,
7917     int *num_out,
7918     int *reason_code,
7919     int control_only, int *cwnd_full, int from_where,
7920     struct timeval *now, int *now_filled, int frag_point, int so_locked
7921 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7922     SCTP_UNUSED
7923 #endif
7924 );
7925 
7926 static void
7927 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
7928     uint32_t val)
7929 {
7930 	struct sctp_copy_all *ca;
7931 	struct mbuf *m;
7932 	int ret = 0;
7933 	int added_control = 0;
7934 	int un_sent, do_chunk_output = 1;
7935 	struct sctp_association *asoc;
7936 
7937 	ca = (struct sctp_copy_all *)ptr;
7938 	if (ca->m == NULL) {
7939 		return;
7940 	}
7941 	if (ca->inp != inp) {
7942 		/* TSNH */
7943 		return;
7944 	}
7945 	if ((ca->m) && ca->sndlen) {
7946 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
7947 		if (m == NULL) {
7948 			/* can't copy so we are done */
7949 			ca->cnt_failed++;
7950 			return;
7951 		}
7952 #ifdef SCTP_MBUF_LOGGING
7953 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7954 			struct mbuf *mat;
7955 
7956 			mat = m;
7957 			while (mat) {
7958 				if (SCTP_BUF_IS_EXTENDED(mat)) {
7959 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7960 				}
7961 				mat = SCTP_BUF_NEXT(mat);
7962 			}
7963 		}
7964 #endif
7965 	} else {
7966 		m = NULL;
7967 	}
7968 	SCTP_TCB_LOCK_ASSERT(stcb);
7969 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
7970 		/* Abort this assoc with m as the user defined reason */
7971 		if (m) {
7972 			struct sctp_paramhdr *ph;
7973 
7974 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
7975 			if (m) {
7976 				ph = mtod(m, struct sctp_paramhdr *);
7977 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
7978 				ph->param_length = htons(ca->sndlen);
7979 			}
7980 			/*
7981 			 * We add one here to keep the assoc from
7982 			 * dis-appearing on us.
7983 			 */
7984 			atomic_add_int(&stcb->asoc.refcnt, 1);
7985 			sctp_abort_an_association(inp, stcb,
7986 			    SCTP_RESPONSE_TO_USER_REQ,
7987 			    m, SCTP_SO_NOT_LOCKED);
7988 			/*
7989 			 * sctp_abort_an_association calls sctp_free_asoc()
7990 			 * free association will NOT free it since we
7991 			 * incremented the refcnt .. we do this to prevent
7992 			 * it being freed and things getting tricky since we
7993 			 * could end up (from free_asoc) calling inpcb_free
7994 			 * which would get a recursive lock call to the
7995 			 * iterator lock.. But as a consequence of that the
7996 			 * stcb will return to us un-locked.. since
7997 			 * free_asoc returns with either no TCB or the TCB
7998 			 * unlocked, we must relock.. to unlock in the
7999 			 * iterator timer :-0
8000 			 */
8001 			SCTP_TCB_LOCK(stcb);
8002 			atomic_add_int(&stcb->asoc.refcnt, -1);
8003 			goto no_chunk_output;
8004 		}
8005 	} else {
8006 		if (m) {
8007 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
8008 			    &ca->sndrcv, 1);
8009 		}
8010 		asoc = &stcb->asoc;
8011 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
8012 			/* shutdown this assoc */
8013 			int cnt;
8014 
8015 			cnt = sctp_is_there_unsent_data(stcb);
8016 
8017 			if (TAILQ_EMPTY(&asoc->send_queue) &&
8018 			    TAILQ_EMPTY(&asoc->sent_queue) &&
8019 			    (cnt == 0)) {
8020 				if (asoc->locked_on_sending) {
8021 					goto abort_anyway;
8022 				}
8023 				/*
8024 				 * there is nothing queued to send, so I'm
8025 				 * done...
8026 				 */
8027 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
8028 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
8029 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
8030 					/*
8031 					 * only send SHUTDOWN the first time
8032 					 * through
8033 					 */
8034 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
8035 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
8036 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8037 					}
8038 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
8039 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8040 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
8041 					    asoc->primary_destination);
8042 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
8043 					    asoc->primary_destination);
8044 					added_control = 1;
8045 					do_chunk_output = 0;
8046 				}
8047 			} else {
8048 				/*
8049 				 * we still got (or just got) data to send,
8050 				 * so set SHUTDOWN_PENDING
8051 				 */
8052 				/*
8053 				 * XXX sockets draft says that SCTP_EOF
8054 				 * should be sent with no data.  currently,
8055 				 * we will allow user data to be sent first
8056 				 * and move to SHUTDOWN-PENDING
8057 				 */
8058 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
8059 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
8060 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
8061 					if (asoc->locked_on_sending) {
8062 						/*
8063 						 * Locked to send out the
8064 						 * data
8065 						 */
8066 						struct sctp_stream_queue_pending *sp;
8067 
8068 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
8069 						if (sp) {
8070 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
8071 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
8072 						}
8073 					}
8074 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
8075 					if (TAILQ_EMPTY(&asoc->send_queue) &&
8076 					    TAILQ_EMPTY(&asoc->sent_queue) &&
8077 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
8078 				abort_anyway:
8079 						atomic_add_int(&stcb->asoc.refcnt, 1);
8080 						sctp_abort_an_association(stcb->sctp_ep, stcb,
8081 						    SCTP_RESPONSE_TO_USER_REQ,
8082 						    NULL, SCTP_SO_NOT_LOCKED);
8083 						atomic_add_int(&stcb->asoc.refcnt, -1);
8084 						goto no_chunk_output;
8085 					}
8086 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
8087 					    asoc->primary_destination);
8088 				}
8089 			}
8090 
8091 		}
8092 	}
8093 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
8094 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
8095 
8096 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
8097 	    (stcb->asoc.total_flight > 0) &&
8098 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
8099 	    ) {
8100 		do_chunk_output = 0;
8101 	}
8102 	if (do_chunk_output)
8103 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
8104 	else if (added_control) {
8105 		int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
8106 		struct timeval now;
8107 		int frag_point;
8108 
8109 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
8110 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
8111 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
8112 	}
8113 no_chunk_output:
8114 	if (ret) {
8115 		ca->cnt_failed++;
8116 	} else {
8117 		ca->cnt_sent++;
8118 	}
8119 }
8120 
8121 static void
8122 sctp_sendall_completes(void *ptr, uint32_t val)
8123 {
8124 	struct sctp_copy_all *ca;
8125 
8126 	ca = (struct sctp_copy_all *)ptr;
8127 	/*
8128 	 * Do a notify here? Kacheong suggests that the notify be done at
8129 	 * the send time.. so you would push up a notification if any send
8130 	 * failed. Don't know if this is feasable since the only failures we
8131 	 * have is "memory" related and if you cannot get an mbuf to send
8132 	 * the data you surely can't get an mbuf to send up to notify the
8133 	 * user you can't send the data :->
8134 	 */
8135 
8136 	/* now free everything */
8137 	sctp_m_freem(ca->m);
8138 	SCTP_FREE(ca, SCTP_M_COPYAL);
8139 }
8140 
8141 
8142 #define	MC_ALIGN(m, len) do {						\
8143 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
8144 } while (0)
8145 
8146 
8147 
8148 static struct mbuf *
8149 sctp_copy_out_all(struct uio *uio, int len)
8150 {
8151 	struct mbuf *ret, *at;
8152 	int left, willcpy, cancpy, error;
8153 
8154 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
8155 	if (ret == NULL) {
8156 		/* TSNH */
8157 		return (NULL);
8158 	}
8159 	left = len;
8160 	SCTP_BUF_LEN(ret) = 0;
8161 	/* save space for the data chunk header */
8162 	cancpy = M_TRAILINGSPACE(ret);
8163 	willcpy = min(cancpy, left);
8164 	at = ret;
8165 	while (left > 0) {
8166 		/* Align data to the end */
8167 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
8168 		if (error) {
8169 	err_out_now:
8170 			sctp_m_freem(at);
8171 			return (NULL);
8172 		}
8173 		SCTP_BUF_LEN(at) = willcpy;
8174 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
8175 		left -= willcpy;
8176 		if (left > 0) {
8177 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
8178 			if (SCTP_BUF_NEXT(at) == NULL) {
8179 				goto err_out_now;
8180 			}
8181 			at = SCTP_BUF_NEXT(at);
8182 			SCTP_BUF_LEN(at) = 0;
8183 			cancpy = M_TRAILINGSPACE(at);
8184 			willcpy = min(cancpy, left);
8185 		}
8186 	}
8187 	return (ret);
8188 }
8189 
8190 static int
8191 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
8192     struct sctp_sndrcvinfo *srcv)
8193 {
8194 	int ret;
8195 	struct sctp_copy_all *ca;
8196 
8197 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
8198 	    SCTP_M_COPYAL);
8199 	if (ca == NULL) {
8200 		sctp_m_freem(m);
8201 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8202 		return (ENOMEM);
8203 	}
8204 	memset(ca, 0, sizeof(struct sctp_copy_all));
8205 
8206 	ca->inp = inp;
8207 	memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
8208 	/*
8209 	 * take off the sendall flag, it would be bad if we failed to do
8210 	 * this :-0
8211 	 */
8212 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
8213 	/* get length and mbuf chain */
8214 	if (uio) {
8215 		ca->sndlen = uio->uio_resid;
8216 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
8217 		if (ca->m == NULL) {
8218 			SCTP_FREE(ca, SCTP_M_COPYAL);
8219 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8220 			return (ENOMEM);
8221 		}
8222 	} else {
8223 		/* Gather the length of the send */
8224 		struct mbuf *mat;
8225 
8226 		mat = m;
8227 		ca->sndlen = 0;
8228 		while (m) {
8229 			ca->sndlen += SCTP_BUF_LEN(m);
8230 			m = SCTP_BUF_NEXT(m);
8231 		}
8232 		ca->m = mat;
8233 	}
8234 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
8235 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
8236 	    SCTP_ASOC_ANY_STATE,
8237 	    (void *)ca, 0,
8238 	    sctp_sendall_completes, inp, 1);
8239 	if (ret) {
8240 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
8241 		SCTP_FREE(ca, SCTP_M_COPYAL);
8242 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
8243 		return (EFAULT);
8244 	}
8245 	return (0);
8246 }
8247 
8248 
8249 void
8250 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
8251 {
8252 	struct sctp_tmit_chunk *chk, *nchk;
8253 
8254 	chk = TAILQ_FIRST(&asoc->control_send_queue);
8255 	while (chk) {
8256 		nchk = TAILQ_NEXT(chk, sctp_next);
8257 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8258 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
8259 			if (chk->data) {
8260 				sctp_m_freem(chk->data);
8261 				chk->data = NULL;
8262 			}
8263 			asoc->ctrl_queue_cnt--;
8264 			sctp_free_a_chunk(stcb, chk);
8265 		}
8266 		chk = nchk;
8267 	}
8268 }
8269 
8270 void
8271 sctp_toss_old_asconf(struct sctp_tcb *stcb)
8272 {
8273 	struct sctp_association *asoc;
8274 	struct sctp_tmit_chunk *chk, *chk_tmp;
8275 	struct sctp_asconf_chunk *acp;
8276 
8277 	asoc = &stcb->asoc;
8278 	for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
8279 	    chk = chk_tmp) {
8280 		/* get next chk */
8281 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
8282 		/* find SCTP_ASCONF chunk in queue */
8283 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
8284 			if (chk->data) {
8285 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
8286 				if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
8287 					/* Not Acked yet */
8288 					break;
8289 				}
8290 			}
8291 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
8292 			if (chk->data) {
8293 				sctp_m_freem(chk->data);
8294 				chk->data = NULL;
8295 			}
8296 			asoc->ctrl_queue_cnt--;
8297 			sctp_free_a_chunk(stcb, chk);
8298 		}
8299 	}
8300 }
8301 
8302 
8303 static void
8304 sctp_clean_up_datalist(struct sctp_tcb *stcb,
8305 
8306     struct sctp_association *asoc,
8307     struct sctp_tmit_chunk **data_list,
8308     int bundle_at,
8309     struct sctp_nets *net)
8310 {
8311 	int i;
8312 	struct sctp_tmit_chunk *tp1;
8313 
8314 	for (i = 0; i < bundle_at; i++) {
8315 		/* off of the send queue */
8316 		if (i) {
8317 			/*
8318 			 * Any chunk NOT 0 you zap the time chunk 0 gets
8319 			 * zapped or set based on if a RTO measurment is
8320 			 * needed.
8321 			 */
8322 			data_list[i]->do_rtt = 0;
8323 		}
8324 		/* record time */
8325 		data_list[i]->sent_rcv_time = net->last_sent_time;
8326 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
8327 		TAILQ_REMOVE(&asoc->send_queue,
8328 		    data_list[i],
8329 		    sctp_next);
8330 		/* on to the sent queue */
8331 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
8332 		if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
8333 		    data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
8334 			struct sctp_tmit_chunk *tpp;
8335 
8336 			/* need to move back */
8337 	back_up_more:
8338 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
8339 			if (tpp == NULL) {
8340 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
8341 				goto all_done;
8342 			}
8343 			tp1 = tpp;
8344 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
8345 			    data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
8346 				goto back_up_more;
8347 			}
8348 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
8349 		} else {
8350 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
8351 			    data_list[i],
8352 			    sctp_next);
8353 		}
8354 all_done:
8355 		/* This does not lower until the cum-ack passes it */
8356 		asoc->sent_queue_cnt++;
8357 		asoc->send_queue_cnt--;
8358 		if ((asoc->peers_rwnd <= 0) &&
8359 		    (asoc->total_flight == 0) &&
8360 		    (bundle_at == 1)) {
8361 			/* Mark the chunk as being a window probe */
8362 			SCTP_STAT_INCR(sctps_windowprobed);
8363 		}
8364 #ifdef SCTP_AUDITING_ENABLED
8365 		sctp_audit_log(0xC2, 3);
8366 #endif
8367 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
8368 		data_list[i]->snd_count = 1;
8369 		data_list[i]->rec.data.chunk_was_revoked = 0;
8370 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
8371 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
8372 			    data_list[i]->whoTo->flight_size,
8373 			    data_list[i]->book_size,
8374 			    (uintptr_t) data_list[i]->whoTo,
8375 			    data_list[i]->rec.data.TSN_seq);
8376 		}
8377 		sctp_flight_size_increase(data_list[i]);
8378 		sctp_total_flight_increase(stcb, data_list[i]);
8379 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
8380 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
8381 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
8382 		}
8383 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
8384 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
8385 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8386 			/* SWS sender side engages */
8387 			asoc->peers_rwnd = 0;
8388 		}
8389 	}
8390 }
8391 
8392 static void
8393 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
8394 {
8395 	struct sctp_tmit_chunk *chk, *nchk;
8396 
8397 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
8398 	    chk; chk = nchk) {
8399 		nchk = TAILQ_NEXT(chk, sctp_next);
8400 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8401 		    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
8402 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8403 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8404 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8405 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8406 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8407 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8408 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8409 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8410 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8411 			/* Stray chunks must be cleaned up */
8412 	clean_up_anyway:
8413 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
8414 			if (chk->data) {
8415 				sctp_m_freem(chk->data);
8416 				chk->data = NULL;
8417 			}
8418 			asoc->ctrl_queue_cnt--;
8419 			sctp_free_a_chunk(stcb, chk);
8420 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8421 			/* special handling, we must look into the param */
8422 			if (chk != asoc->str_reset) {
8423 				goto clean_up_anyway;
8424 			}
8425 		}
8426 	}
8427 }
8428 
8429 
8430 static int
8431 sctp_can_we_split_this(struct sctp_tcb *stcb,
8432     uint32_t length,
8433     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
8434 {
8435 	/*
8436 	 * Make a decision on if I should split a msg into multiple parts.
8437 	 * This is only asked of incomplete messages.
8438 	 */
8439 	if (eeor_on) {
8440 		/*
8441 		 * If we are doing EEOR we need to always send it if its the
8442 		 * entire thing, since it might be all the guy is putting in
8443 		 * the hopper.
8444 		 */
8445 		if (goal_mtu >= length) {
8446 			/*-
8447 			 * If we have data outstanding,
8448 			 * we get another chance when the sack
8449 			 * arrives to transmit - wait for more data
8450 			 */
8451 			if (stcb->asoc.total_flight == 0) {
8452 				/*
8453 				 * If nothing is in flight, we zero the
8454 				 * packet counter.
8455 				 */
8456 				return (length);
8457 			}
8458 			return (0);
8459 
8460 		} else {
8461 			/* You can fill the rest */
8462 			return (goal_mtu);
8463 		}
8464 	}
8465 	/*-
8466 	 * For those strange folk that make the send buffer
8467 	 * smaller than our fragmentation point, we can't
8468 	 * get a full msg in so we have to allow splitting.
8469 	 */
8470 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
8471 		return (length);
8472 	}
8473 	if ((length <= goal_mtu) ||
8474 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
8475 		/* Sub-optimial residual don't split in non-eeor mode. */
8476 		return (0);
8477 	}
8478 	/*
8479 	 * If we reach here length is larger than the goal_mtu. Do we wish
8480 	 * to split it for the sake of packet putting together?
8481 	 */
8482 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
8483 		/* Its ok to split it */
8484 		return (min(goal_mtu, frag_point));
8485 	}
8486 	/* Nope, can't split */
8487 	return (0);
8488 
8489 }
8490 
8491 static uint32_t
8492 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
8493     struct sctp_stream_out *strq,
8494     uint32_t goal_mtu,
8495     uint32_t frag_point,
8496     int *locked,
8497     int *giveup,
8498     int eeor_mode,
8499     int *bail)
8500 {
8501 	/* Move from the stream to the send_queue keeping track of the total */
8502 	struct sctp_association *asoc;
8503 	struct sctp_stream_queue_pending *sp;
8504 	struct sctp_tmit_chunk *chk;
8505 	struct sctp_data_chunk *dchkh;
8506 	uint32_t to_move, length;
8507 	uint8_t rcv_flags = 0;
8508 	uint8_t some_taken;
8509 	uint8_t send_lock_up = 0;
8510 
8511 	SCTP_TCB_LOCK_ASSERT(stcb);
8512 	asoc = &stcb->asoc;
8513 one_more_time:
8514 	/* sa_ignore FREED_MEMORY */
8515 	sp = TAILQ_FIRST(&strq->outqueue);
8516 	if (sp == NULL) {
8517 		*locked = 0;
8518 		if (send_lock_up == 0) {
8519 			SCTP_TCB_SEND_LOCK(stcb);
8520 			send_lock_up = 1;
8521 		}
8522 		sp = TAILQ_FIRST(&strq->outqueue);
8523 		if (sp) {
8524 			goto one_more_time;
8525 		}
8526 		if (strq->last_msg_incomplete) {
8527 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
8528 			    strq->stream_no,
8529 			    strq->last_msg_incomplete);
8530 			strq->last_msg_incomplete = 0;
8531 		}
8532 		to_move = 0;
8533 		if (send_lock_up) {
8534 			SCTP_TCB_SEND_UNLOCK(stcb);
8535 			send_lock_up = 0;
8536 		}
8537 		goto out_of;
8538 	}
8539 	if ((sp->msg_is_complete) && (sp->length == 0)) {
8540 		if (sp->sender_all_done) {
8541 			/*
8542 			 * We are doing differed cleanup. Last time through
8543 			 * when we took all the data the sender_all_done was
8544 			 * not set.
8545 			 */
8546 			if (sp->put_last_out == 0) {
8547 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
8548 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
8549 				    sp->sender_all_done,
8550 				    sp->length,
8551 				    sp->msg_is_complete,
8552 				    sp->put_last_out,
8553 				    send_lock_up);
8554 			}
8555 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
8556 				SCTP_TCB_SEND_LOCK(stcb);
8557 				send_lock_up = 1;
8558 			}
8559 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
8560 			TAILQ_REMOVE(&strq->outqueue, sp, next);
8561 			sctp_free_remote_addr(sp->net);
8562 			if (sp->data) {
8563 				sctp_m_freem(sp->data);
8564 				sp->data = NULL;
8565 			}
8566 			sctp_free_a_strmoq(stcb, sp);
8567 
8568 			/* we can't be locked to it */
8569 			*locked = 0;
8570 			stcb->asoc.locked_on_sending = NULL;
8571 			if (send_lock_up) {
8572 				SCTP_TCB_SEND_UNLOCK(stcb);
8573 				send_lock_up = 0;
8574 			}
8575 			/* back to get the next msg */
8576 			goto one_more_time;
8577 		} else {
8578 			/*
8579 			 * sender just finished this but still holds a
8580 			 * reference
8581 			 */
8582 			*locked = 1;
8583 			*giveup = 1;
8584 			to_move = 0;
8585 			goto out_of;
8586 		}
8587 	} else {
8588 		/* is there some to get */
8589 		if (sp->length == 0) {
8590 			/* no */
8591 			*locked = 1;
8592 			*giveup = 1;
8593 			to_move = 0;
8594 			goto out_of;
8595 		}
8596 	}
8597 	some_taken = sp->some_taken;
8598 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
8599 		sp->msg_is_complete = 1;
8600 	}
8601 re_look:
8602 	length = sp->length;
8603 	if (sp->msg_is_complete) {
8604 		/* The message is complete */
8605 		to_move = min(length, frag_point);
8606 		if (to_move == length) {
8607 			/* All of it fits in the MTU */
8608 			if (sp->some_taken) {
8609 				rcv_flags |= SCTP_DATA_LAST_FRAG;
8610 				sp->put_last_out = 1;
8611 			} else {
8612 				rcv_flags |= SCTP_DATA_NOT_FRAG;
8613 				sp->put_last_out = 1;
8614 			}
8615 		} else {
8616 			/* Not all of it fits, we fragment */
8617 			if (sp->some_taken == 0) {
8618 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
8619 			}
8620 			sp->some_taken = 1;
8621 		}
8622 	} else {
8623 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
8624 		if (to_move) {
8625 			/*-
8626 			 * We use a snapshot of length in case it
8627 			 * is expanding during the compare.
8628 			 */
8629 			uint32_t llen;
8630 
8631 			llen = length;
8632 			if (to_move >= llen) {
8633 				to_move = llen;
8634 				if (send_lock_up == 0) {
8635 					/*-
8636 					 * We are taking all of an incomplete msg
8637 					 * thus we need a send lock.
8638 					 */
8639 					SCTP_TCB_SEND_LOCK(stcb);
8640 					send_lock_up = 1;
8641 					if (sp->msg_is_complete) {
8642 						/*
8643 						 * the sender finished the
8644 						 * msg
8645 						 */
8646 						goto re_look;
8647 					}
8648 				}
8649 			}
8650 			if (sp->some_taken == 0) {
8651 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
8652 				sp->some_taken = 1;
8653 			}
8654 		} else {
8655 			/* Nothing to take. */
8656 			if (sp->some_taken) {
8657 				*locked = 1;
8658 			}
8659 			*giveup = 1;
8660 			to_move = 0;
8661 			goto out_of;
8662 		}
8663 	}
8664 
8665 	/* If we reach here, we can copy out a chunk */
8666 	sctp_alloc_a_chunk(stcb, chk);
8667 	if (chk == NULL) {
8668 		/* No chunk memory */
8669 		*giveup = 1;
8670 		to_move = 0;
8671 		goto out_of;
8672 	}
8673 	/*
8674 	 * Setup for unordered if needed by looking at the user sent info
8675 	 * flags.
8676 	 */
8677 	if (sp->sinfo_flags & SCTP_UNORDERED) {
8678 		rcv_flags |= SCTP_DATA_UNORDERED;
8679 	}
8680 	if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
8681 	    ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
8682 		rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8683 	}
8684 	/* clear out the chunk before setting up */
8685 	memset(chk, 0, sizeof(*chk));
8686 	chk->rec.data.rcv_flags = rcv_flags;
8687 
8688 	if (to_move >= length) {
8689 		/* we think we can steal the whole thing */
8690 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
8691 			SCTP_TCB_SEND_LOCK(stcb);
8692 			send_lock_up = 1;
8693 		}
8694 		if (to_move < sp->length) {
8695 			/* bail, it changed */
8696 			goto dont_do_it;
8697 		}
8698 		chk->data = sp->data;
8699 		chk->last_mbuf = sp->tail_mbuf;
8700 		/* register the stealing */
8701 		sp->data = sp->tail_mbuf = NULL;
8702 	} else {
8703 		struct mbuf *m;
8704 
8705 dont_do_it:
8706 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
8707 		chk->last_mbuf = NULL;
8708 		if (chk->data == NULL) {
8709 			sp->some_taken = some_taken;
8710 			sctp_free_a_chunk(stcb, chk);
8711 			*bail = 1;
8712 			to_move = 0;
8713 			goto out_of;
8714 		}
8715 #ifdef SCTP_MBUF_LOGGING
8716 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8717 			struct mbuf *mat;
8718 
8719 			mat = chk->data;
8720 			while (mat) {
8721 				if (SCTP_BUF_IS_EXTENDED(mat)) {
8722 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8723 				}
8724 				mat = SCTP_BUF_NEXT(mat);
8725 			}
8726 		}
8727 #endif
8728 		/* Pull off the data */
8729 		m_adj(sp->data, to_move);
8730 		/* Now lets work our way down and compact it */
8731 		m = sp->data;
8732 		while (m && (SCTP_BUF_LEN(m) == 0)) {
8733 			sp->data = SCTP_BUF_NEXT(m);
8734 			SCTP_BUF_NEXT(m) = NULL;
8735 			if (sp->tail_mbuf == m) {
8736 				/*-
8737 				 * Freeing tail? TSNH since
8738 				 * we supposedly were taking less
8739 				 * than the sp->length.
8740 				 */
8741 #ifdef INVARIANTS
8742 				panic("Huh, freing tail? - TSNH");
8743 #else
8744 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
8745 				sp->tail_mbuf = sp->data = NULL;
8746 				sp->length = 0;
8747 #endif
8748 
8749 			}
8750 			sctp_m_free(m);
8751 			m = sp->data;
8752 		}
8753 	}
8754 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
8755 		chk->copy_by_ref = 1;
8756 	} else {
8757 		chk->copy_by_ref = 0;
8758 	}
8759 	/*
8760 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
8761 	 * its only one mbuf.
8762 	 */
8763 	if (chk->last_mbuf == NULL) {
8764 		chk->last_mbuf = chk->data;
8765 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
8766 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
8767 		}
8768 	}
8769 	if (to_move > length) {
8770 		/*- This should not happen either
8771 		 * since we always lower to_move to the size
8772 		 * of sp->length if its larger.
8773 		 */
8774 #ifdef INVARIANTS
8775 		panic("Huh, how can to_move be larger?");
8776 #else
8777 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
8778 		sp->length = 0;
8779 #endif
8780 	} else {
8781 		atomic_subtract_int(&sp->length, to_move);
8782 	}
8783 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
8784 		/* Not enough room for a chunk header, get some */
8785 		struct mbuf *m;
8786 
8787 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
8788 		if (m == NULL) {
8789 			/*
8790 			 * we're in trouble here. _PREPEND below will free
8791 			 * all the data if there is no leading space, so we
8792 			 * must put the data back and restore.
8793 			 */
8794 			if (send_lock_up == 0) {
8795 				SCTP_TCB_SEND_LOCK(stcb);
8796 				send_lock_up = 1;
8797 			}
8798 			if (chk->data == NULL) {
8799 				/* unsteal the data */
8800 				sp->data = chk->data;
8801 				sp->tail_mbuf = chk->last_mbuf;
8802 			} else {
8803 				struct mbuf *m_tmp;
8804 
8805 				/* reassemble the data */
8806 				m_tmp = sp->data;
8807 				sp->data = chk->data;
8808 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
8809 			}
8810 			sp->some_taken = some_taken;
8811 			atomic_add_int(&sp->length, to_move);
8812 			chk->data = NULL;
8813 			*bail = 1;
8814 			sctp_free_a_chunk(stcb, chk);
8815 			to_move = 0;
8816 			goto out_of;
8817 		} else {
8818 			SCTP_BUF_LEN(m) = 0;
8819 			SCTP_BUF_NEXT(m) = chk->data;
8820 			chk->data = m;
8821 			M_ALIGN(chk->data, 4);
8822 		}
8823 	}
8824 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
8825 	if (chk->data == NULL) {
8826 		/* HELP, TSNH since we assured it would not above? */
8827 #ifdef INVARIANTS
8828 		panic("prepend failes HELP?");
8829 #else
8830 		SCTP_PRINTF("prepend fails HELP?\n");
8831 		sctp_free_a_chunk(stcb, chk);
8832 #endif
8833 		*bail = 1;
8834 		to_move = 0;
8835 		goto out_of;
8836 	}
8837 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
8838 	chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
8839 	chk->book_size_scale = 0;
8840 	chk->sent = SCTP_DATAGRAM_UNSENT;
8841 
8842 	chk->flags = 0;
8843 	chk->asoc = &stcb->asoc;
8844 	chk->pad_inplace = 0;
8845 	chk->no_fr_allowed = 0;
8846 	chk->rec.data.stream_seq = sp->strseq;
8847 	chk->rec.data.stream_number = sp->stream;
8848 	chk->rec.data.payloadtype = sp->ppid;
8849 	chk->rec.data.context = sp->context;
8850 	chk->rec.data.doing_fast_retransmit = 0;
8851 	chk->rec.data.ect_nonce = 0;	/* ECN Nonce */
8852 
8853 	chk->rec.data.timetodrop = sp->ts;
8854 	chk->flags = sp->act_flags;
8855 	chk->addr_over = sp->addr_over;
8856 
8857 	chk->whoTo = net;
8858 	atomic_add_int(&chk->whoTo->ref_count, 1);
8859 
8860 	if (sp->holds_key_ref) {
8861 		chk->auth_keyid = sp->auth_keyid;
8862 		sctp_auth_key_acquire(stcb, chk->auth_keyid);
8863 		chk->holds_key_ref = 1;
8864 	}
8865 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
8866 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
8867 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
8868 		    (uintptr_t) stcb, sp->length,
8869 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
8870 		    chk->rec.data.TSN_seq);
8871 	}
8872 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
8873 	/*
8874 	 * Put the rest of the things in place now. Size was done earlier in
8875 	 * previous loop prior to padding.
8876 	 */
8877 
8878 #ifdef SCTP_ASOCLOG_OF_TSNS
8879 	SCTP_TCB_LOCK_ASSERT(stcb);
8880 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
8881 		asoc->tsn_out_at = 0;
8882 		asoc->tsn_out_wrapped = 1;
8883 	}
8884 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
8885 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
8886 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
8887 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
8888 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
8889 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
8890 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
8891 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
8892 	asoc->tsn_out_at++;
8893 #endif
8894 
8895 	dchkh->ch.chunk_type = SCTP_DATA;
8896 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
8897 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
8898 	dchkh->dp.stream_id = htons(strq->stream_no);
8899 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
8900 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
8901 	dchkh->ch.chunk_length = htons(chk->send_size);
8902 	/* Now advance the chk->send_size by the actual pad needed. */
8903 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
8904 		/* need a pad */
8905 		struct mbuf *lm;
8906 		int pads;
8907 
8908 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
8909 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
8910 			chk->pad_inplace = 1;
8911 		}
8912 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
8913 			/* pad added an mbuf */
8914 			chk->last_mbuf = lm;
8915 		}
8916 		chk->send_size += pads;
8917 	}
8918 	/* We only re-set the policy if it is on */
8919 	if (sp->pr_sctp_on) {
8920 		sctp_set_prsctp_policy(stcb, sp);
8921 		asoc->pr_sctp_cnt++;
8922 		chk->pr_sctp_on = 1;
8923 	} else {
8924 		chk->pr_sctp_on = 0;
8925 	}
8926 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
8927 		/* All done pull and kill the message */
8928 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
8929 		if (sp->put_last_out == 0) {
8930 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
8931 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
8932 			    sp->sender_all_done,
8933 			    sp->length,
8934 			    sp->msg_is_complete,
8935 			    sp->put_last_out,
8936 			    send_lock_up);
8937 		}
8938 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
8939 			SCTP_TCB_SEND_LOCK(stcb);
8940 			send_lock_up = 1;
8941 		}
8942 		TAILQ_REMOVE(&strq->outqueue, sp, next);
8943 		sctp_free_remote_addr(sp->net);
8944 		if (sp->data) {
8945 			sctp_m_freem(sp->data);
8946 			sp->data = NULL;
8947 		}
8948 		sctp_free_a_strmoq(stcb, sp);
8949 
8950 		/* we can't be locked to it */
8951 		*locked = 0;
8952 		stcb->asoc.locked_on_sending = NULL;
8953 	} else {
8954 		/* more to go, we are locked */
8955 		*locked = 1;
8956 	}
8957 	asoc->chunks_on_out_queue++;
8958 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
8959 	asoc->send_queue_cnt++;
8960 out_of:
8961 	if (send_lock_up) {
8962 		SCTP_TCB_SEND_UNLOCK(stcb);
8963 		send_lock_up = 0;
8964 	}
8965 	return (to_move);
8966 }
8967 
8968 
8969 static struct sctp_stream_out *
8970 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
8971 {
8972 	struct sctp_stream_out *strq;
8973 
8974 	/* Find the next stream to use */
8975 	if (asoc->last_out_stream == NULL) {
8976 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
8977 		if (asoc->last_out_stream == NULL) {
8978 			/* huh nothing on the wheel, TSNH */
8979 			return (NULL);
8980 		}
8981 		goto done_it;
8982 	}
8983 	strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
8984 done_it:
8985 	if (strq == NULL) {
8986 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
8987 	}
8988 	/* Save off the last stream */
8989 	asoc->last_out_stream = strq;
8990 	return (strq);
8991 
8992 }
8993 
8994 
8995 static void
8996 sctp_fill_outqueue(struct sctp_tcb *stcb,
8997     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
8998 {
8999 	struct sctp_association *asoc;
9000 	struct sctp_stream_out *strq, *strqn, *strqt;
9001 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
9002 	int locked, giveup;
9003 	struct sctp_stream_queue_pending *sp;
9004 
9005 	SCTP_TCB_LOCK_ASSERT(stcb);
9006 	asoc = &stcb->asoc;
9007 #ifdef INET6
9008 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
9009 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
9010 	} else {
9011 		/* ?? not sure what else to do */
9012 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9013 	}
9014 #else
9015 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
9016 #endif
9017 	/* Need an allowance for the data chunk header too */
9018 	goal_mtu -= sizeof(struct sctp_data_chunk);
9019 
9020 	/* must make even word boundary */
9021 	goal_mtu &= 0xfffffffc;
9022 	if (asoc->locked_on_sending) {
9023 		/* We are stuck on one stream until the message completes. */
9024 		strqn = strq = asoc->locked_on_sending;
9025 		locked = 1;
9026 	} else {
9027 		strqn = strq = sctp_select_a_stream(stcb, asoc);
9028 		locked = 0;
9029 	}
9030 
9031 	while ((goal_mtu > 0) && strq) {
9032 		sp = TAILQ_FIRST(&strq->outqueue);
9033 		/*
9034 		 * If CMT is off, we must validate that the stream in
9035 		 * question has the first item pointed towards are network
9036 		 * destionation requested by the caller. Note that if we
9037 		 * turn out to be locked to a stream (assigning TSN's then
9038 		 * we must stop, since we cannot look for another stream
9039 		 * with data to send to that destination). In CMT's case, by
9040 		 * skipping this check, we will send one data packet towards
9041 		 * the requested net.
9042 		 */
9043 		if (sp == NULL) {
9044 			break;
9045 		}
9046 		if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
9047 			/* none for this network */
9048 			if (locked) {
9049 				break;
9050 			} else {
9051 				strq = sctp_select_a_stream(stcb, asoc);
9052 				if (strq == NULL)
9053 					/* none left */
9054 					break;
9055 				if (strqn == strq) {
9056 					/* I have circled */
9057 					break;
9058 				}
9059 				continue;
9060 			}
9061 		}
9062 		giveup = 0;
9063 		bail = 0;
9064 		moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
9065 		    &giveup, eeor_mode, &bail);
9066 		asoc->last_out_stream = strq;
9067 		if (locked) {
9068 			asoc->locked_on_sending = strq;
9069 			if ((moved_how_much == 0) || (giveup) || bail)
9070 				/* no more to move for now */
9071 				break;
9072 		} else {
9073 			asoc->locked_on_sending = NULL;
9074 			strqt = sctp_select_a_stream(stcb, asoc);
9075 			if (TAILQ_FIRST(&strq->outqueue) == NULL) {
9076 				if (strq == strqn) {
9077 					/* Must move start to next one */
9078 					strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
9079 					if (strqn == NULL) {
9080 						strqn = TAILQ_FIRST(&asoc->out_wheel);
9081 						if (strqn == NULL) {
9082 							break;
9083 						}
9084 					}
9085 				}
9086 				sctp_remove_from_wheel(stcb, asoc, strq);
9087 			}
9088 			if ((giveup) || bail) {
9089 				break;
9090 			}
9091 			strq = strqt;
9092 			if (strq == NULL) {
9093 				break;
9094 			}
9095 		}
9096 		total_moved += moved_how_much;
9097 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
9098 		goal_mtu &= 0xfffffffc;
9099 	}
9100 	if (bail)
9101 		*quit_now = 1;
9102 
9103 	if (total_moved == 0) {
9104 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
9105 		    (net == stcb->asoc.primary_destination)) {
9106 			/* ran dry for primary network net */
9107 			SCTP_STAT_INCR(sctps_primary_randry);
9108 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
9109 			/* ran dry with CMT on */
9110 			SCTP_STAT_INCR(sctps_cmt_randry);
9111 		}
9112 	}
9113 }
9114 
9115 void
9116 sctp_fix_ecn_echo(struct sctp_association *asoc)
9117 {
9118 	struct sctp_tmit_chunk *chk;
9119 
9120 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9121 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
9122 			chk->sent = SCTP_DATAGRAM_UNSENT;
9123 		}
9124 	}
9125 }
9126 
9127 static void
9128 sctp_move_to_an_alt(struct sctp_tcb *stcb,
9129     struct sctp_association *asoc,
9130     struct sctp_nets *net)
9131 {
9132 	struct sctp_tmit_chunk *chk;
9133 	struct sctp_nets *a_net;
9134 
9135 	SCTP_TCB_LOCK_ASSERT(stcb);
9136 	/*
9137 	 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
9138 	 * destination using the PF algorithm for finding alternate
9139 	 * destinations.
9140 	 */
9141 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
9142 		a_net = sctp_find_alternate_net(stcb, net, 2);
9143 	} else {
9144 		a_net = sctp_find_alternate_net(stcb, net, 0);
9145 	}
9146 	if ((a_net != net) &&
9147 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
9148 		/*
9149 		 * We only proceed if a valid alternate is found that is not
9150 		 * this one and is reachable. Here we must move all chunks
9151 		 * queued in the send queue off of the destination address
9152 		 * to our alternate.
9153 		 */
9154 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
9155 			if (chk->whoTo == net) {
9156 				/* Move the chunk to our alternate */
9157 				sctp_free_remote_addr(chk->whoTo);
9158 				chk->whoTo = a_net;
9159 				atomic_add_int(&a_net->ref_count, 1);
9160 			}
9161 		}
9162 	}
9163 }
9164 
9165 int
9166 sctp_med_chunk_output(struct sctp_inpcb *inp,
9167     struct sctp_tcb *stcb,
9168     struct sctp_association *asoc,
9169     int *num_out,
9170     int *reason_code,
9171     int control_only, int *cwnd_full, int from_where,
9172     struct timeval *now, int *now_filled, int frag_point, int so_locked
9173 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9174     SCTP_UNUSED
9175 #endif
9176 )
9177 {
9178 	/*
9179 	 * Ok this is the generic chunk service queue. we must do the
9180 	 * following: - Service the stream queue that is next, moving any
9181 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
9182 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
9183 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
9184 	 * fomulate and send the low level chunks. Making sure to combine
9185 	 * any control in the control chunk queue also.
9186 	 */
9187 	struct sctp_nets *net;
9188 	struct mbuf *outchain, *endoutchain;
9189 	struct sctp_tmit_chunk *chk, *nchk;
9190 
9191 	/* temp arrays for unlinking */
9192 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9193 	int no_fragmentflg, error;
9194 	int one_chunk, hbflag, skip_data_for_this_net;
9195 	int asconf, cookie, no_out_cnt;
9196 	int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
9197 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
9198 	struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
9199 	int tsns_sent = 0;
9200 	uint32_t auth_offset = 0;
9201 	struct sctp_auth_chunk *auth = NULL;
9202 	uint16_t auth_keyid;
9203 	int override_ok = 1;
9204 	int data_auth_reqd = 0;
9205 
9206 	/*
9207 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
9208 	 * destination.
9209 	 */
9210 	int pf_hbflag = 0;
9211 	int quit_now = 0;
9212 
9213 	*num_out = 0;
9214 	cwnd_full_ind = 0;
9215 	auth_keyid = stcb->asoc.authinfo.active_keyid;
9216 
9217 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
9218 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
9219 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
9220 		eeor_mode = 1;
9221 	} else {
9222 		eeor_mode = 0;
9223 	}
9224 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
9225 	/*
9226 	 * First lets prime the pump. For each destination, if there is room
9227 	 * in the flight size, attempt to pull an MTU's worth out of the
9228 	 * stream queues into the general send_queue
9229 	 */
9230 #ifdef SCTP_AUDITING_ENABLED
9231 	sctp_audit_log(0xC2, 2);
9232 #endif
9233 	SCTP_TCB_LOCK_ASSERT(stcb);
9234 	hbflag = 0;
9235 	if ((control_only) || (asoc->stream_reset_outstanding))
9236 		no_data_chunks = 1;
9237 	else
9238 		no_data_chunks = 0;
9239 
9240 	/* Nothing to possible to send? */
9241 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9242 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
9243 	    TAILQ_EMPTY(&asoc->send_queue) &&
9244 	    TAILQ_EMPTY(&asoc->out_wheel)) {
9245 		*reason_code = 9;
9246 		return (0);
9247 	}
9248 	if (asoc->peers_rwnd == 0) {
9249 		/* No room in peers rwnd */
9250 		*cwnd_full = 1;
9251 		*reason_code = 1;
9252 		if (asoc->total_flight > 0) {
9253 			/* we are allowed one chunk in flight */
9254 			no_data_chunks = 1;
9255 		}
9256 	}
9257 	if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
9258 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
9259 			/*
9260 			 * for CMT we start at the next one past the one we
9261 			 * last added data to.
9262 			 */
9263 			if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
9264 				goto skip_the_fill_from_streams;
9265 			}
9266 			if (asoc->last_net_data_came_from) {
9267 				net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
9268 				if (net == NULL) {
9269 					net = TAILQ_FIRST(&asoc->nets);
9270 				}
9271 			} else {
9272 				/* back to start */
9273 				net = TAILQ_FIRST(&asoc->nets);
9274 			}
9275 
9276 			/*
9277 			 * JRI-TODO: CMT-MPI. Simply set the first
9278 			 * destination (net) to be optimized for the next
9279 			 * message to be pulled out of the outwheel. 1. peek
9280 			 * at outwheel 2. If large message, set net =
9281 			 * highest_cwnd 3. If small message, set net =
9282 			 * lowest rtt
9283 			 */
9284 		} else {
9285 			net = asoc->primary_destination;
9286 			if (net == NULL) {
9287 				/* TSNH */
9288 				net = TAILQ_FIRST(&asoc->nets);
9289 			}
9290 		}
9291 		start_at = net;
9292 
9293 one_more_time:
9294 		for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
9295 			net->window_probe = 0;
9296 			if (old_startat && (old_startat == net)) {
9297 				break;
9298 			}
9299 			/*
9300 			 * JRI: if dest is unreachable or unconfirmed, do
9301 			 * not send data to it
9302 			 */
9303 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
9304 				continue;
9305 			}
9306 			/*
9307 			 * JRI: if dest is in PF state, do not send data to
9308 			 * it
9309 			 */
9310 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9311 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9312 			    (net->dest_state & SCTP_ADDR_PF)) {
9313 				continue;
9314 			}
9315 			if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
9316 				/* nothing can be in queue for this guy */
9317 				continue;
9318 			}
9319 			if (net->flight_size >= net->cwnd) {
9320 				/* skip this network, no room */
9321 				cwnd_full_ind++;
9322 				continue;
9323 			}
9324 			/*
9325 			 * JRI : this for loop we are in takes in each net,
9326 			 * if its's got space in cwnd and has data sent to
9327 			 * it (when CMT is off) then it calls
9328 			 * sctp_fill_outqueue for the net. This gets data on
9329 			 * the send queue for that network.
9330 			 *
9331 			 * In sctp_fill_outqueue TSN's are assigned and data is
9332 			 * copied out of the stream buffers. Note mostly
9333 			 * copy by reference (we hope).
9334 			 */
9335 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9336 				sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
9337 			}
9338 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
9339 			if (quit_now) {
9340 				/* memory alloc failure */
9341 				no_data_chunks = 1;
9342 				goto skip_the_fill_from_streams;
9343 			}
9344 		}
9345 		if (start_at != TAILQ_FIRST(&asoc->nets)) {
9346 			/* got to pick up the beginning stuff. */
9347 			old_startat = start_at;
9348 			start_at = net = TAILQ_FIRST(&asoc->nets);
9349 			if (old_startat)
9350 				goto one_more_time;
9351 		}
9352 	}
9353 skip_the_fill_from_streams:
9354 	*cwnd_full = cwnd_full_ind;
9355 
9356 	/* now service each destination and send out what we can for it */
9357 	/* Nothing to send? */
9358 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
9359 	    (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
9360 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
9361 		*reason_code = 8;
9362 		return (0);
9363 	}
9364 	if (no_data_chunks) {
9365 		chk = TAILQ_FIRST(&asoc->asconf_send_queue);
9366 		if (chk == NULL)
9367 			chk = TAILQ_FIRST(&asoc->control_send_queue);
9368 	} else {
9369 		chk = TAILQ_FIRST(&asoc->send_queue);
9370 	}
9371 	if (chk) {
9372 		send_start_at = chk->whoTo;
9373 	} else {
9374 		send_start_at = TAILQ_FIRST(&asoc->nets);
9375 	}
9376 	old_startat = NULL;
9377 again_one_more_time:
9378 	for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
9379 		/* how much can we send? */
9380 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
9381 		if (old_startat && (old_startat == net)) {
9382 			/* through list ocmpletely. */
9383 			break;
9384 		}
9385 		tsns_sent = 0;
9386 		if (net->ref_count < 2) {
9387 			/*
9388 			 * Ref-count of 1 so we cannot have data or control
9389 			 * queued to this address. Skip it.
9390 			 */
9391 			continue;
9392 		}
9393 		ctl_cnt = bundle_at = 0;
9394 		endoutchain = outchain = NULL;
9395 		no_fragmentflg = 1;
9396 		one_chunk = 0;
9397 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
9398 			skip_data_for_this_net = 1;
9399 		} else {
9400 			skip_data_for_this_net = 0;
9401 		}
9402 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
9403 			/*
9404 			 * if we have a route and an ifp check to see if we
9405 			 * have room to send to this guy
9406 			 */
9407 			struct ifnet *ifp;
9408 
9409 			ifp = net->ro.ro_rt->rt_ifp;
9410 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
9411 				SCTP_STAT_INCR(sctps_ifnomemqueued);
9412 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9413 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
9414 				}
9415 				continue;
9416 			}
9417 		}
9418 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
9419 		case AF_INET:
9420 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
9421 			break;
9422 #ifdef INET6
9423 		case AF_INET6:
9424 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
9425 			break;
9426 #endif
9427 		default:
9428 			/* TSNH */
9429 			mtu = net->mtu;
9430 			break;
9431 		}
9432 		mx_mtu = mtu;
9433 		to_out = 0;
9434 		if (mtu > asoc->peers_rwnd) {
9435 			if (asoc->total_flight > 0) {
9436 				/* We have a packet in flight somewhere */
9437 				r_mtu = asoc->peers_rwnd;
9438 			} else {
9439 				/* We are always allowed to send one MTU out */
9440 				one_chunk = 1;
9441 				r_mtu = mtu;
9442 			}
9443 		} else {
9444 			r_mtu = mtu;
9445 		}
9446 		/************************/
9447 		/* ASCONF transmission */
9448 		/************************/
9449 		/* Now first lets go through the asconf queue */
9450 		for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
9451 		    chk; chk = nchk) {
9452 			nchk = TAILQ_NEXT(chk, sctp_next);
9453 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
9454 				continue;
9455 			}
9456 			if (chk->whoTo != net) {
9457 				/*
9458 				 * No, not sent to the network we are
9459 				 * looking at
9460 				 */
9461 				break;
9462 			}
9463 			if (chk->data == NULL) {
9464 				break;
9465 			}
9466 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
9467 			    chk->sent != SCTP_DATAGRAM_RESEND) {
9468 				break;
9469 			}
9470 			/*
9471 			 * if no AUTH is yet included and this chunk
9472 			 * requires it, make sure to account for it.  We
9473 			 * don't apply the size until the AUTH chunk is
9474 			 * actually added below in case there is no room for
9475 			 * this chunk. NOTE: we overload the use of "omtu"
9476 			 * here
9477 			 */
9478 			if ((auth == NULL) &&
9479 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9480 			    stcb->asoc.peer_auth_chunks)) {
9481 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9482 			} else
9483 				omtu = 0;
9484 			/* Here we do NOT factor the r_mtu */
9485 			if ((chk->send_size < (int)(mtu - omtu)) ||
9486 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9487 				/*
9488 				 * We probably should glom the mbuf chain
9489 				 * from the chk->data for control but the
9490 				 * problem is it becomes yet one more level
9491 				 * of tracking to do if for some reason
9492 				 * output fails. Then I have got to
9493 				 * reconstruct the merged control chain.. el
9494 				 * yucko.. for now we take the easy way and
9495 				 * do the copy
9496 				 */
9497 				/*
9498 				 * Add an AUTH chunk, if chunk requires it
9499 				 * save the offset into the chain for AUTH
9500 				 */
9501 				if ((auth == NULL) &&
9502 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9503 				    stcb->asoc.peer_auth_chunks))) {
9504 					outchain = sctp_add_auth_chunk(outchain,
9505 					    &endoutchain,
9506 					    &auth,
9507 					    &auth_offset,
9508 					    stcb,
9509 					    chk->rec.chunk_id.id);
9510 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9511 				}
9512 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
9513 				    (int)chk->rec.chunk_id.can_take_data,
9514 				    chk->send_size, chk->copy_by_ref);
9515 				if (outchain == NULL) {
9516 					*reason_code = 8;
9517 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9518 					return (ENOMEM);
9519 				}
9520 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9521 				/* update our MTU size */
9522 				if (mtu > (chk->send_size + omtu))
9523 					mtu -= (chk->send_size + omtu);
9524 				else
9525 					mtu = 0;
9526 				to_out += (chk->send_size + omtu);
9527 				/* Do clear IP_DF ? */
9528 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9529 					no_fragmentflg = 0;
9530 				}
9531 				if (chk->rec.chunk_id.can_take_data)
9532 					chk->data = NULL;
9533 				/*
9534 				 * set hb flag since we can use these for
9535 				 * RTO
9536 				 */
9537 				hbflag = 1;
9538 				asconf = 1;
9539 				/*
9540 				 * should sysctl this: don't bundle data
9541 				 * with ASCONF since it requires AUTH
9542 				 */
9543 				no_data_chunks = 1;
9544 				chk->sent = SCTP_DATAGRAM_SENT;
9545 				chk->snd_count++;
9546 				if (mtu == 0) {
9547 					/*
9548 					 * Ok we are out of room but we can
9549 					 * output without effecting the
9550 					 * flight size since this little guy
9551 					 * is a control only packet.
9552 					 */
9553 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
9554 					/*
9555 					 * do NOT clear the asconf flag as
9556 					 * it is used to do appropriate
9557 					 * source address selection.
9558 					 */
9559 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9560 					    (struct sockaddr *)&net->ro._l_addr,
9561 					    outchain, auth_offset, auth,
9562 					    stcb->asoc.authinfo.active_keyid,
9563 					    no_fragmentflg, 0, NULL, asconf,
9564 					    inp->sctp_lport, stcb->rport,
9565 					    htonl(stcb->asoc.peer_vtag),
9566 					    net->port, so_locked, NULL))) {
9567 						if (error == ENOBUFS) {
9568 							asoc->ifp_had_enobuf = 1;
9569 							SCTP_STAT_INCR(sctps_lowlevelerr);
9570 						}
9571 						if (from_where == 0) {
9572 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
9573 						}
9574 						if (*now_filled == 0) {
9575 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9576 							*now_filled = 1;
9577 							*now = net->last_sent_time;
9578 						} else {
9579 							net->last_sent_time = *now;
9580 						}
9581 						hbflag = 0;
9582 						/* error, could not output */
9583 						if (error == EHOSTUNREACH) {
9584 							/*
9585 							 * Destination went
9586 							 * unreachable
9587 							 * during this send
9588 							 */
9589 							sctp_move_to_an_alt(stcb, asoc, net);
9590 						}
9591 						*reason_code = 7;
9592 						continue;
9593 					} else
9594 						asoc->ifp_had_enobuf = 0;
9595 					if (*now_filled == 0) {
9596 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9597 						*now_filled = 1;
9598 						*now = net->last_sent_time;
9599 					} else {
9600 						net->last_sent_time = *now;
9601 					}
9602 					hbflag = 0;
9603 					/*
9604 					 * increase the number we sent, if a
9605 					 * cookie is sent we don't tell them
9606 					 * any was sent out.
9607 					 */
9608 					outchain = endoutchain = NULL;
9609 					auth = NULL;
9610 					auth_offset = 0;
9611 					if (!no_out_cnt)
9612 						*num_out += ctl_cnt;
9613 					/* recalc a clean slate and setup */
9614 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9615 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9616 					} else {
9617 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
9618 					}
9619 					to_out = 0;
9620 					no_fragmentflg = 1;
9621 				}
9622 			}
9623 		}
9624 		/************************/
9625 		/* Control transmission */
9626 		/************************/
9627 		/* Now first lets go through the control queue */
9628 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
9629 		    chk; chk = nchk) {
9630 			nchk = TAILQ_NEXT(chk, sctp_next);
9631 			if (chk->whoTo != net) {
9632 				/*
9633 				 * No, not sent to the network we are
9634 				 * looking at
9635 				 */
9636 				continue;
9637 			}
9638 			if (chk->data == NULL) {
9639 				continue;
9640 			}
9641 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
9642 				/*
9643 				 * It must be unsent. Cookies and ASCONF's
9644 				 * hang around but there timers will force
9645 				 * when marked for resend.
9646 				 */
9647 				continue;
9648 			}
9649 			/*
9650 			 * if no AUTH is yet included and this chunk
9651 			 * requires it, make sure to account for it.  We
9652 			 * don't apply the size until the AUTH chunk is
9653 			 * actually added below in case there is no room for
9654 			 * this chunk. NOTE: we overload the use of "omtu"
9655 			 * here
9656 			 */
9657 			if ((auth == NULL) &&
9658 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9659 			    stcb->asoc.peer_auth_chunks)) {
9660 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9661 			} else
9662 				omtu = 0;
9663 			/* Here we do NOT factor the r_mtu */
9664 			if ((chk->send_size < (int)(mtu - omtu)) ||
9665 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9666 				/*
9667 				 * We probably should glom the mbuf chain
9668 				 * from the chk->data for control but the
9669 				 * problem is it becomes yet one more level
9670 				 * of tracking to do if for some reason
9671 				 * output fails. Then I have got to
9672 				 * reconstruct the merged control chain.. el
9673 				 * yucko.. for now we take the easy way and
9674 				 * do the copy
9675 				 */
9676 				/*
9677 				 * Add an AUTH chunk, if chunk requires it
9678 				 * save the offset into the chain for AUTH
9679 				 */
9680 				if ((auth == NULL) &&
9681 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9682 				    stcb->asoc.peer_auth_chunks))) {
9683 					outchain = sctp_add_auth_chunk(outchain,
9684 					    &endoutchain,
9685 					    &auth,
9686 					    &auth_offset,
9687 					    stcb,
9688 					    chk->rec.chunk_id.id);
9689 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9690 				}
9691 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
9692 				    (int)chk->rec.chunk_id.can_take_data,
9693 				    chk->send_size, chk->copy_by_ref);
9694 				if (outchain == NULL) {
9695 					*reason_code = 8;
9696 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9697 					return (ENOMEM);
9698 				}
9699 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9700 				/* update our MTU size */
9701 				if (mtu > (chk->send_size + omtu))
9702 					mtu -= (chk->send_size + omtu);
9703 				else
9704 					mtu = 0;
9705 				to_out += (chk->send_size + omtu);
9706 				/* Do clear IP_DF ? */
9707 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9708 					no_fragmentflg = 0;
9709 				}
9710 				if (chk->rec.chunk_id.can_take_data)
9711 					chk->data = NULL;
9712 				/* Mark things to be removed, if needed */
9713 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
9714 				    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
9715 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
9716 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
9717 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
9718 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
9719 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
9720 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
9721 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
9722 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
9723 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
9724 
9725 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
9726 						hbflag = 1;
9727 						/*
9728 						 * JRS 5/14/07 - Set the
9729 						 * flag to say a heartbeat
9730 						 * is being sent.
9731 						 */
9732 						pf_hbflag = 1;
9733 					}
9734 					/* remove these chunks at the end */
9735 					if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9736 						/* turn off the timer */
9737 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9738 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9739 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
9740 						}
9741 					}
9742 					/*
9743 					 * EY -Nr-sack version of the above
9744 					 * if statement
9745 					 */
9746 					if ((SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) &&
9747 					    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {	/* EY !?! */
9748 						/* turn off the timer */
9749 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9750 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9751 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
9752 						}
9753 					}
9754 					ctl_cnt++;
9755 				} else {
9756 					/*
9757 					 * Other chunks, since they have
9758 					 * timers running (i.e. COOKIE) we
9759 					 * just "trust" that it gets sent or
9760 					 * retransmitted.
9761 					 */
9762 					ctl_cnt++;
9763 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9764 						cookie = 1;
9765 						no_out_cnt = 1;
9766 					}
9767 					chk->sent = SCTP_DATAGRAM_SENT;
9768 					chk->snd_count++;
9769 				}
9770 				if (mtu == 0) {
9771 					/*
9772 					 * Ok we are out of room but we can
9773 					 * output without effecting the
9774 					 * flight size since this little guy
9775 					 * is a control only packet.
9776 					 */
9777 					if (asconf) {
9778 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
9779 						/*
9780 						 * do NOT clear the asconf
9781 						 * flag as it is used to do
9782 						 * appropriate source
9783 						 * address selection.
9784 						 */
9785 					}
9786 					if (cookie) {
9787 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
9788 						cookie = 0;
9789 					}
9790 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9791 					    (struct sockaddr *)&net->ro._l_addr,
9792 					    outchain,
9793 					    auth_offset, auth,
9794 					    stcb->asoc.authinfo.active_keyid,
9795 					    no_fragmentflg, 0, NULL, asconf,
9796 					    inp->sctp_lport, stcb->rport,
9797 					    htonl(stcb->asoc.peer_vtag),
9798 					    net->port, so_locked, NULL))) {
9799 						if (error == ENOBUFS) {
9800 							asoc->ifp_had_enobuf = 1;
9801 							SCTP_STAT_INCR(sctps_lowlevelerr);
9802 						}
9803 						if (from_where == 0) {
9804 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
9805 						}
9806 						/* error, could not output */
9807 						if (hbflag) {
9808 							if (*now_filled == 0) {
9809 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9810 								*now_filled = 1;
9811 								*now = net->last_sent_time;
9812 							} else {
9813 								net->last_sent_time = *now;
9814 							}
9815 							hbflag = 0;
9816 						}
9817 						if (error == EHOSTUNREACH) {
9818 							/*
9819 							 * Destination went
9820 							 * unreachable
9821 							 * during this send
9822 							 */
9823 							sctp_move_to_an_alt(stcb, asoc, net);
9824 						}
9825 						*reason_code = 7;
9826 						continue;
9827 					} else
9828 						asoc->ifp_had_enobuf = 0;
9829 					/* Only HB or ASCONF advances time */
9830 					if (hbflag) {
9831 						if (*now_filled == 0) {
9832 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9833 							*now_filled = 1;
9834 							*now = net->last_sent_time;
9835 						} else {
9836 							net->last_sent_time = *now;
9837 						}
9838 						hbflag = 0;
9839 					}
9840 					/*
9841 					 * increase the number we sent, if a
9842 					 * cookie is sent we don't tell them
9843 					 * any was sent out.
9844 					 */
9845 					outchain = endoutchain = NULL;
9846 					auth = NULL;
9847 					auth_offset = 0;
9848 					if (!no_out_cnt)
9849 						*num_out += ctl_cnt;
9850 					/* recalc a clean slate and setup */
9851 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9852 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9853 					} else {
9854 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
9855 					}
9856 					to_out = 0;
9857 					no_fragmentflg = 1;
9858 				}
9859 			}
9860 		}
9861 		/*********************/
9862 		/* Data transmission */
9863 		/*********************/
9864 		/*
9865 		 * if AUTH for DATA is required and no AUTH has been added
9866 		 * yet, account for this in the mtu now... if no data can be
9867 		 * bundled, this adjustment won't matter anyways since the
9868 		 * packet will be going out...
9869 		 */
9870 		data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
9871 		    stcb->asoc.peer_auth_chunks);
9872 		if (data_auth_reqd && (auth == NULL)) {
9873 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9874 		}
9875 		/* now lets add any data within the MTU constraints */
9876 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
9877 		case AF_INET:
9878 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
9879 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
9880 			else
9881 				omtu = 0;
9882 			break;
9883 #ifdef INET6
9884 		case AF_INET6:
9885 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
9886 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
9887 			else
9888 				omtu = 0;
9889 			break;
9890 #endif
9891 		default:
9892 			/* TSNH */
9893 			omtu = 0;
9894 			break;
9895 		}
9896 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
9897 		    (cookie)) {
9898 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
9899 				if (no_data_chunks) {
9900 					/* let only control go out */
9901 					*reason_code = 1;
9902 					break;
9903 				}
9904 				if (net->flight_size >= net->cwnd) {
9905 					/* skip this net, no room for data */
9906 					*reason_code = 2;
9907 					break;
9908 				}
9909 				nchk = TAILQ_NEXT(chk, sctp_next);
9910 				if (chk->whoTo != net) {
9911 					/* No, not sent to this net */
9912 					continue;
9913 				}
9914 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
9915 					/*-
9916 					 * strange, we have a chunk that is
9917 					 * to big for its destination and
9918 					 * yet no fragment ok flag.
9919 					 * Something went wrong when the
9920 					 * PMTU changed...we did not mark
9921 					 * this chunk for some reason?? I
9922 					 * will fix it here by letting IP
9923 					 * fragment it for now and printing
9924 					 * a warning. This really should not
9925 					 * happen ...
9926 					 */
9927 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
9928 					    chk->send_size, mtu);
9929 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
9930 				}
9931 				if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
9932 				    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
9933 					struct sctp_data_chunk *dchkh;
9934 
9935 					dchkh = mtod(chk->data, struct sctp_data_chunk *);
9936 					dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
9937 				}
9938 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
9939 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
9940 					/* ok we will add this one */
9941 
9942 					/*
9943 					 * Add an AUTH chunk, if chunk
9944 					 * requires it, save the offset into
9945 					 * the chain for AUTH
9946 					 */
9947 					if (data_auth_reqd) {
9948 						if (auth == NULL) {
9949 							outchain = sctp_add_auth_chunk(outchain,
9950 							    &endoutchain,
9951 							    &auth,
9952 							    &auth_offset,
9953 							    stcb,
9954 							    SCTP_DATA);
9955 							auth_keyid = chk->auth_keyid;
9956 							override_ok = 0;
9957 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9958 						} else if (override_ok) {
9959 							/*
9960 							 * use this data's
9961 							 * keyid
9962 							 */
9963 							auth_keyid = chk->auth_keyid;
9964 							override_ok = 0;
9965 						} else if (auth_keyid != chk->auth_keyid) {
9966 							/*
9967 							 * different keyid,
9968 							 * so done bundling
9969 							 */
9970 							break;
9971 						}
9972 					}
9973 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
9974 					    chk->send_size, chk->copy_by_ref);
9975 					if (outchain == NULL) {
9976 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
9977 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9978 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9979 						}
9980 						*reason_code = 3;
9981 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9982 						return (ENOMEM);
9983 					}
9984 					/* upate our MTU size */
9985 					/* Do clear IP_DF ? */
9986 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9987 						no_fragmentflg = 0;
9988 					}
9989 					/* unsigned subtraction of mtu */
9990 					if (mtu > chk->send_size)
9991 						mtu -= chk->send_size;
9992 					else
9993 						mtu = 0;
9994 					/* unsigned subtraction of r_mtu */
9995 					if (r_mtu > chk->send_size)
9996 						r_mtu -= chk->send_size;
9997 					else
9998 						r_mtu = 0;
9999 
10000 					to_out += chk->send_size;
10001 					if ((to_out > mx_mtu) && no_fragmentflg) {
10002 #ifdef INVARIANTS
10003 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
10004 #else
10005 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
10006 						    mx_mtu, to_out);
10007 #endif
10008 					}
10009 					chk->window_probe = 0;
10010 					data_list[bundle_at++] = chk;
10011 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
10012 						mtu = 0;
10013 						break;
10014 					}
10015 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
10016 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
10017 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
10018 						} else {
10019 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
10020 						}
10021 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
10022 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
10023 							/*
10024 							 * Count number of
10025 							 * user msg's that
10026 							 * were fragmented
10027 							 * we do this by
10028 							 * counting when we
10029 							 * see a LAST
10030 							 * fragment only.
10031 							 */
10032 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
10033 					}
10034 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
10035 						if (one_chunk) {
10036 							data_list[0]->window_probe = 1;
10037 							net->window_probe = 1;
10038 						}
10039 						break;
10040 					}
10041 				} else {
10042 					/*
10043 					 * Must be sent in order of the
10044 					 * TSN's (on a network)
10045 					 */
10046 					break;
10047 				}
10048 			}	/* for (chunk gather loop for this net) */
10049 		}		/* if asoc.state OPEN */
10050 		/* Is there something to send for this destination? */
10051 		if (outchain) {
10052 			/* We may need to start a control timer or two */
10053 			if (asconf) {
10054 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
10055 				    stcb, net);
10056 				/*
10057 				 * do NOT clear the asconf flag as it is
10058 				 * used to do appropriate source address
10059 				 * selection.
10060 				 */
10061 			}
10062 			if (cookie) {
10063 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
10064 				cookie = 0;
10065 			}
10066 			/* must start a send timer if data is being sent */
10067 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
10068 				/*
10069 				 * no timer running on this destination
10070 				 * restart it.
10071 				 */
10072 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
10073 			} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
10074 				    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
10075 				    pf_hbflag &&
10076 				    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
10077 			    (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
10078 				/*
10079 				 * JRS 5/14/07 - If a HB has been sent to a
10080 				 * PF destination and no T3 timer is
10081 				 * currently running, start the T3 timer to
10082 				 * track the HBs that were sent.
10083 				 */
10084 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
10085 			}
10086 			/* Now send it, if there is anything to send :> */
10087 			if ((error = sctp_lowlevel_chunk_output(inp,
10088 			    stcb,
10089 			    net,
10090 			    (struct sockaddr *)&net->ro._l_addr,
10091 			    outchain,
10092 			    auth_offset,
10093 			    auth,
10094 			    auth_keyid,
10095 			    no_fragmentflg,
10096 			    bundle_at,
10097 			    data_list[0],
10098 			    asconf,
10099 			    inp->sctp_lport, stcb->rport,
10100 			    htonl(stcb->asoc.peer_vtag),
10101 			    net->port, so_locked, NULL))) {
10102 				/* error, we could not output */
10103 				if (error == ENOBUFS) {
10104 					SCTP_STAT_INCR(sctps_lowlevelerr);
10105 					asoc->ifp_had_enobuf = 1;
10106 				}
10107 				if (from_where == 0) {
10108 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
10109 				}
10110 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10111 				if (hbflag) {
10112 					if (*now_filled == 0) {
10113 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
10114 						*now_filled = 1;
10115 						*now = net->last_sent_time;
10116 					} else {
10117 						net->last_sent_time = *now;
10118 					}
10119 					hbflag = 0;
10120 				}
10121 				if (error == EHOSTUNREACH) {
10122 					/*
10123 					 * Destination went unreachable
10124 					 * during this send
10125 					 */
10126 					sctp_move_to_an_alt(stcb, asoc, net);
10127 				}
10128 				*reason_code = 6;
10129 				/*-
10130 				 * I add this line to be paranoid. As far as
10131 				 * I can tell the continue, takes us back to
10132 				 * the top of the for, but just to make sure
10133 				 * I will reset these again here.
10134 				 */
10135 				ctl_cnt = bundle_at = 0;
10136 				continue;	/* This takes us back to the
10137 						 * for() for the nets. */
10138 			} else {
10139 				asoc->ifp_had_enobuf = 0;
10140 			}
10141 			outchain = endoutchain = NULL;
10142 			auth = NULL;
10143 			auth_offset = 0;
10144 			if (bundle_at || hbflag) {
10145 				/* For data/asconf and hb set time */
10146 				if (*now_filled == 0) {
10147 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
10148 					*now_filled = 1;
10149 					*now = net->last_sent_time;
10150 				} else {
10151 					net->last_sent_time = *now;
10152 				}
10153 			}
10154 			if (!no_out_cnt) {
10155 				*num_out += (ctl_cnt + bundle_at);
10156 			}
10157 			if (bundle_at) {
10158 				/* setup for a RTO measurement */
10159 				tsns_sent = data_list[0]->rec.data.TSN_seq;
10160 				/* fill time if not already filled */
10161 				if (*now_filled == 0) {
10162 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
10163 					*now_filled = 1;
10164 					*now = asoc->time_last_sent;
10165 				} else {
10166 					asoc->time_last_sent = *now;
10167 				}
10168 				data_list[0]->do_rtt = 1;
10169 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
10170 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
10171 				if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
10172 					if (net->flight_size < net->cwnd) {
10173 						/* start or restart it */
10174 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
10175 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
10176 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
10177 						}
10178 						SCTP_STAT_INCR(sctps_earlyfrstrout);
10179 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
10180 					} else {
10181 						/* stop it if its running */
10182 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
10183 							SCTP_STAT_INCR(sctps_earlyfrstpout);
10184 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
10185 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
10186 						}
10187 					}
10188 				}
10189 			}
10190 			if (one_chunk) {
10191 				break;
10192 			}
10193 		}
10194 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10195 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
10196 		}
10197 	}
10198 	if (old_startat == NULL) {
10199 		old_startat = send_start_at;
10200 		send_start_at = TAILQ_FIRST(&asoc->nets);
10201 		if (old_startat)
10202 			goto again_one_more_time;
10203 	}
10204 	/*
10205 	 * At the end there should be no NON timed chunks hanging on this
10206 	 * queue.
10207 	 */
10208 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10209 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
10210 	}
10211 	if ((*num_out == 0) && (*reason_code == 0)) {
10212 		*reason_code = 4;
10213 	} else {
10214 		*reason_code = 5;
10215 	}
10216 	sctp_clean_up_ctl(stcb, asoc);
10217 	return (0);
10218 }
10219 
10220 void
10221 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
10222 {
10223 	/*-
10224 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
10225 	 * the control chunk queue.
10226 	 */
10227 	struct sctp_chunkhdr *hdr;
10228 	struct sctp_tmit_chunk *chk;
10229 	struct mbuf *mat;
10230 
10231 	SCTP_TCB_LOCK_ASSERT(stcb);
10232 	sctp_alloc_a_chunk(stcb, chk);
10233 	if (chk == NULL) {
10234 		/* no memory */
10235 		sctp_m_freem(op_err);
10236 		return;
10237 	}
10238 	chk->copy_by_ref = 0;
10239 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
10240 	if (op_err == NULL) {
10241 		sctp_free_a_chunk(stcb, chk);
10242 		return;
10243 	}
10244 	chk->send_size = 0;
10245 	mat = op_err;
10246 	while (mat != NULL) {
10247 		chk->send_size += SCTP_BUF_LEN(mat);
10248 		mat = SCTP_BUF_NEXT(mat);
10249 	}
10250 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
10251 	chk->rec.chunk_id.can_take_data = 1;
10252 	chk->sent = SCTP_DATAGRAM_UNSENT;
10253 	chk->snd_count = 0;
10254 	chk->flags = 0;
10255 	chk->asoc = &stcb->asoc;
10256 	chk->data = op_err;
10257 	chk->whoTo = chk->asoc->primary_destination;
10258 	atomic_add_int(&chk->whoTo->ref_count, 1);
10259 	hdr = mtod(op_err, struct sctp_chunkhdr *);
10260 	hdr->chunk_type = SCTP_OPERATION_ERROR;
10261 	hdr->chunk_flags = 0;
10262 	hdr->chunk_length = htons(chk->send_size);
10263 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
10264 	    chk,
10265 	    sctp_next);
10266 	chk->asoc->ctrl_queue_cnt++;
10267 }
10268 
10269 int
10270 sctp_send_cookie_echo(struct mbuf *m,
10271     int offset,
10272     struct sctp_tcb *stcb,
10273     struct sctp_nets *net)
10274 {
10275 	/*-
10276 	 * pull out the cookie and put it at the front of the control chunk
10277 	 * queue.
10278 	 */
10279 	int at;
10280 	struct mbuf *cookie;
10281 	struct sctp_paramhdr parm, *phdr;
10282 	struct sctp_chunkhdr *hdr;
10283 	struct sctp_tmit_chunk *chk;
10284 	uint16_t ptype, plen;
10285 
10286 	/* First find the cookie in the param area */
10287 	cookie = NULL;
10288 	at = offset + sizeof(struct sctp_init_chunk);
10289 
10290 	SCTP_TCB_LOCK_ASSERT(stcb);
10291 	do {
10292 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
10293 		if (phdr == NULL) {
10294 			return (-3);
10295 		}
10296 		ptype = ntohs(phdr->param_type);
10297 		plen = ntohs(phdr->param_length);
10298 		if (ptype == SCTP_STATE_COOKIE) {
10299 			int pad;
10300 
10301 			/* found the cookie */
10302 			if ((pad = (plen % 4))) {
10303 				plen += 4 - pad;
10304 			}
10305 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
10306 			if (cookie == NULL) {
10307 				/* No memory */
10308 				return (-2);
10309 			}
10310 #ifdef SCTP_MBUF_LOGGING
10311 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10312 				struct mbuf *mat;
10313 
10314 				mat = cookie;
10315 				while (mat) {
10316 					if (SCTP_BUF_IS_EXTENDED(mat)) {
10317 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10318 					}
10319 					mat = SCTP_BUF_NEXT(mat);
10320 				}
10321 			}
10322 #endif
10323 			break;
10324 		}
10325 		at += SCTP_SIZE32(plen);
10326 	} while (phdr);
10327 	if (cookie == NULL) {
10328 		/* Did not find the cookie */
10329 		return (-3);
10330 	}
10331 	/* ok, we got the cookie lets change it into a cookie echo chunk */
10332 
10333 	/* first the change from param to cookie */
10334 	hdr = mtod(cookie, struct sctp_chunkhdr *);
10335 	hdr->chunk_type = SCTP_COOKIE_ECHO;
10336 	hdr->chunk_flags = 0;
10337 	/* get the chunk stuff now and place it in the FRONT of the queue */
10338 	sctp_alloc_a_chunk(stcb, chk);
10339 	if (chk == NULL) {
10340 		/* no memory */
10341 		sctp_m_freem(cookie);
10342 		return (-5);
10343 	}
10344 	chk->copy_by_ref = 0;
10345 	chk->send_size = plen;
10346 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
10347 	chk->rec.chunk_id.can_take_data = 0;
10348 	chk->sent = SCTP_DATAGRAM_UNSENT;
10349 	chk->snd_count = 0;
10350 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
10351 	chk->asoc = &stcb->asoc;
10352 	chk->data = cookie;
10353 	chk->whoTo = chk->asoc->primary_destination;
10354 	atomic_add_int(&chk->whoTo->ref_count, 1);
10355 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
10356 	chk->asoc->ctrl_queue_cnt++;
10357 	return (0);
10358 }
10359 
10360 void
10361 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
10362     struct mbuf *m,
10363     int offset,
10364     int chk_length,
10365     struct sctp_nets *net)
10366 {
10367 	/*
10368 	 * take a HB request and make it into a HB ack and send it.
10369 	 */
10370 	struct mbuf *outchain;
10371 	struct sctp_chunkhdr *chdr;
10372 	struct sctp_tmit_chunk *chk;
10373 
10374 
10375 	if (net == NULL)
10376 		/* must have a net pointer */
10377 		return;
10378 
10379 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
10380 	if (outchain == NULL) {
10381 		/* gak out of memory */
10382 		return;
10383 	}
10384 #ifdef SCTP_MBUF_LOGGING
10385 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10386 		struct mbuf *mat;
10387 
10388 		mat = outchain;
10389 		while (mat) {
10390 			if (SCTP_BUF_IS_EXTENDED(mat)) {
10391 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10392 			}
10393 			mat = SCTP_BUF_NEXT(mat);
10394 		}
10395 	}
10396 #endif
10397 	chdr = mtod(outchain, struct sctp_chunkhdr *);
10398 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
10399 	chdr->chunk_flags = 0;
10400 	if (chk_length % 4) {
10401 		/* need pad */
10402 		uint32_t cpthis = 0;
10403 		int padlen;
10404 
10405 		padlen = 4 - (chk_length % 4);
10406 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
10407 	}
10408 	sctp_alloc_a_chunk(stcb, chk);
10409 	if (chk == NULL) {
10410 		/* no memory */
10411 		sctp_m_freem(outchain);
10412 		return;
10413 	}
10414 	chk->copy_by_ref = 0;
10415 	chk->send_size = chk_length;
10416 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
10417 	chk->rec.chunk_id.can_take_data = 1;
10418 	chk->sent = SCTP_DATAGRAM_UNSENT;
10419 	chk->snd_count = 0;
10420 	chk->flags = 0;
10421 	chk->asoc = &stcb->asoc;
10422 	chk->data = outchain;
10423 	chk->whoTo = net;
10424 	atomic_add_int(&chk->whoTo->ref_count, 1);
10425 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10426 	chk->asoc->ctrl_queue_cnt++;
10427 }
10428 
10429 void
10430 sctp_send_cookie_ack(struct sctp_tcb *stcb)
10431 {
10432 	/* formulate and queue a cookie-ack back to sender */
10433 	struct mbuf *cookie_ack;
10434 	struct sctp_chunkhdr *hdr;
10435 	struct sctp_tmit_chunk *chk;
10436 
10437 	cookie_ack = NULL;
10438 	SCTP_TCB_LOCK_ASSERT(stcb);
10439 
10440 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10441 	if (cookie_ack == NULL) {
10442 		/* no mbuf's */
10443 		return;
10444 	}
10445 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
10446 	sctp_alloc_a_chunk(stcb, chk);
10447 	if (chk == NULL) {
10448 		/* no memory */
10449 		sctp_m_freem(cookie_ack);
10450 		return;
10451 	}
10452 	chk->copy_by_ref = 0;
10453 	chk->send_size = sizeof(struct sctp_chunkhdr);
10454 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
10455 	chk->rec.chunk_id.can_take_data = 1;
10456 	chk->sent = SCTP_DATAGRAM_UNSENT;
10457 	chk->snd_count = 0;
10458 	chk->flags = 0;
10459 	chk->asoc = &stcb->asoc;
10460 	chk->data = cookie_ack;
10461 	if (chk->asoc->last_control_chunk_from != NULL) {
10462 		chk->whoTo = chk->asoc->last_control_chunk_from;
10463 	} else {
10464 		chk->whoTo = chk->asoc->primary_destination;
10465 	}
10466 	atomic_add_int(&chk->whoTo->ref_count, 1);
10467 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
10468 	hdr->chunk_type = SCTP_COOKIE_ACK;
10469 	hdr->chunk_flags = 0;
10470 	hdr->chunk_length = htons(chk->send_size);
10471 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
10472 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10473 	chk->asoc->ctrl_queue_cnt++;
10474 	return;
10475 }
10476 
10477 
10478 void
10479 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
10480 {
10481 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
10482 	struct mbuf *m_shutdown_ack;
10483 	struct sctp_shutdown_ack_chunk *ack_cp;
10484 	struct sctp_tmit_chunk *chk;
10485 
10486 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10487 	if (m_shutdown_ack == NULL) {
10488 		/* no mbuf's */
10489 		return;
10490 	}
10491 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
10492 	sctp_alloc_a_chunk(stcb, chk);
10493 	if (chk == NULL) {
10494 		/* no memory */
10495 		sctp_m_freem(m_shutdown_ack);
10496 		return;
10497 	}
10498 	chk->copy_by_ref = 0;
10499 	chk->send_size = sizeof(struct sctp_chunkhdr);
10500 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
10501 	chk->rec.chunk_id.can_take_data = 1;
10502 	chk->sent = SCTP_DATAGRAM_UNSENT;
10503 	chk->snd_count = 0;
10504 	chk->flags = 0;
10505 	chk->asoc = &stcb->asoc;
10506 	chk->data = m_shutdown_ack;
10507 	chk->whoTo = net;
10508 	atomic_add_int(&net->ref_count, 1);
10509 
10510 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
10511 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
10512 	ack_cp->ch.chunk_flags = 0;
10513 	ack_cp->ch.chunk_length = htons(chk->send_size);
10514 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
10515 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10516 	chk->asoc->ctrl_queue_cnt++;
10517 	return;
10518 }
10519 
10520 void
10521 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
10522 {
10523 	/* formulate and queue a SHUTDOWN to the sender */
10524 	struct mbuf *m_shutdown;
10525 	struct sctp_shutdown_chunk *shutdown_cp;
10526 	struct sctp_tmit_chunk *chk;
10527 
10528 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10529 	if (m_shutdown == NULL) {
10530 		/* no mbuf's */
10531 		return;
10532 	}
10533 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
10534 	sctp_alloc_a_chunk(stcb, chk);
10535 	if (chk == NULL) {
10536 		/* no memory */
10537 		sctp_m_freem(m_shutdown);
10538 		return;
10539 	}
10540 	chk->copy_by_ref = 0;
10541 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
10542 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
10543 	chk->rec.chunk_id.can_take_data = 1;
10544 	chk->sent = SCTP_DATAGRAM_UNSENT;
10545 	chk->snd_count = 0;
10546 	chk->flags = 0;
10547 	chk->asoc = &stcb->asoc;
10548 	chk->data = m_shutdown;
10549 	chk->whoTo = net;
10550 	atomic_add_int(&net->ref_count, 1);
10551 
10552 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
10553 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
10554 	shutdown_cp->ch.chunk_flags = 0;
10555 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
10556 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
10557 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
10558 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10559 	chk->asoc->ctrl_queue_cnt++;
10560 	return;
10561 }
10562 
10563 void
10564 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
10565 {
10566 	/*
10567 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
10568 	 * should be queued on the assoc queue.
10569 	 */
10570 	struct sctp_tmit_chunk *chk;
10571 	struct mbuf *m_asconf;
10572 	int len;
10573 
10574 	SCTP_TCB_LOCK_ASSERT(stcb);
10575 
10576 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
10577 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
10578 		/* can't send a new one if there is one in flight already */
10579 		return;
10580 	}
10581 	/* compose an ASCONF chunk, maximum length is PMTU */
10582 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
10583 	if (m_asconf == NULL) {
10584 		return;
10585 	}
10586 	sctp_alloc_a_chunk(stcb, chk);
10587 	if (chk == NULL) {
10588 		/* no memory */
10589 		sctp_m_freem(m_asconf);
10590 		return;
10591 	}
10592 	chk->copy_by_ref = 0;
10593 	chk->data = m_asconf;
10594 	chk->send_size = len;
10595 	chk->rec.chunk_id.id = SCTP_ASCONF;
10596 	chk->rec.chunk_id.can_take_data = 0;
10597 	chk->sent = SCTP_DATAGRAM_UNSENT;
10598 	chk->snd_count = 0;
10599 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
10600 	chk->asoc = &stcb->asoc;
10601 	chk->whoTo = net;
10602 	atomic_add_int(&chk->whoTo->ref_count, 1);
10603 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
10604 	chk->asoc->ctrl_queue_cnt++;
10605 	return;
10606 }
10607 
10608 void
10609 sctp_send_asconf_ack(struct sctp_tcb *stcb)
10610 {
10611 	/*
10612 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
10613 	 * must be stored in the tcb.
10614 	 */
10615 	struct sctp_tmit_chunk *chk;
10616 	struct sctp_asconf_ack *ack, *latest_ack;
10617 	struct mbuf *m_ack, *m;
10618 	struct sctp_nets *net = NULL;
10619 
10620 	SCTP_TCB_LOCK_ASSERT(stcb);
10621 	/* Get the latest ASCONF-ACK */
10622 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
10623 	if (latest_ack == NULL) {
10624 		return;
10625 	}
10626 	if (latest_ack->last_sent_to != NULL &&
10627 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
10628 		/* we're doing a retransmission */
10629 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
10630 		if (net == NULL) {
10631 			/* no alternate */
10632 			if (stcb->asoc.last_control_chunk_from == NULL)
10633 				net = stcb->asoc.primary_destination;
10634 			else
10635 				net = stcb->asoc.last_control_chunk_from;
10636 		}
10637 	} else {
10638 		/* normal case */
10639 		if (stcb->asoc.last_control_chunk_from == NULL)
10640 			net = stcb->asoc.primary_destination;
10641 		else
10642 			net = stcb->asoc.last_control_chunk_from;
10643 	}
10644 	latest_ack->last_sent_to = net;
10645 
10646 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
10647 		if (ack->data == NULL) {
10648 			continue;
10649 		}
10650 		/* copy the asconf_ack */
10651 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
10652 		if (m_ack == NULL) {
10653 			/* couldn't copy it */
10654 			return;
10655 		}
10656 #ifdef SCTP_MBUF_LOGGING
10657 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10658 			struct mbuf *mat;
10659 
10660 			mat = m_ack;
10661 			while (mat) {
10662 				if (SCTP_BUF_IS_EXTENDED(mat)) {
10663 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10664 				}
10665 				mat = SCTP_BUF_NEXT(mat);
10666 			}
10667 		}
10668 #endif
10669 
10670 		sctp_alloc_a_chunk(stcb, chk);
10671 		if (chk == NULL) {
10672 			/* no memory */
10673 			if (m_ack)
10674 				sctp_m_freem(m_ack);
10675 			return;
10676 		}
10677 		chk->copy_by_ref = 0;
10678 
10679 		chk->whoTo = net;
10680 		chk->data = m_ack;
10681 		chk->send_size = 0;
10682 		/* Get size */
10683 		m = m_ack;
10684 		chk->send_size = ack->len;
10685 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
10686 		chk->rec.chunk_id.can_take_data = 1;
10687 		chk->sent = SCTP_DATAGRAM_UNSENT;
10688 		chk->snd_count = 0;
10689 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
10690 		chk->asoc = &stcb->asoc;
10691 		atomic_add_int(&chk->whoTo->ref_count, 1);
10692 
10693 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10694 		chk->asoc->ctrl_queue_cnt++;
10695 	}
10696 	return;
10697 }
10698 
10699 
10700 static int
10701 sctp_chunk_retransmission(struct sctp_inpcb *inp,
10702     struct sctp_tcb *stcb,
10703     struct sctp_association *asoc,
10704     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
10705 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10706     SCTP_UNUSED
10707 #endif
10708 )
10709 {
10710 	/*-
10711 	 * send out one MTU of retransmission. If fast_retransmit is
10712 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
10713 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
10714 	 * retransmit them by themselves.
10715 	 *
10716 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
10717 	 * marked for resend and bundle them all together (up to a MTU of
10718 	 * destination). The address to send to should have been
10719 	 * selected/changed where the retransmission was marked (i.e. in FR
10720 	 * or t3-timeout routines).
10721 	 */
10722 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
10723 	struct sctp_tmit_chunk *chk, *fwd;
10724 	struct mbuf *m, *endofchain;
10725 	struct sctp_nets *net = NULL;
10726 	uint32_t tsns_sent = 0;
10727 	int no_fragmentflg, bundle_at, cnt_thru;
10728 	unsigned int mtu;
10729 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
10730 	struct sctp_auth_chunk *auth = NULL;
10731 	uint32_t auth_offset = 0;
10732 	uint16_t auth_keyid;
10733 	int override_ok = 1;
10734 	int data_auth_reqd = 0;
10735 	uint32_t dmtu = 0;
10736 
10737 	SCTP_TCB_LOCK_ASSERT(stcb);
10738 	tmr_started = ctl_cnt = bundle_at = error = 0;
10739 	no_fragmentflg = 1;
10740 	fwd_tsn = 0;
10741 	*cnt_out = 0;
10742 	fwd = NULL;
10743 	endofchain = m = NULL;
10744 	auth_keyid = stcb->asoc.authinfo.active_keyid;
10745 #ifdef SCTP_AUDITING_ENABLED
10746 	sctp_audit_log(0xC3, 1);
10747 #endif
10748 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
10749 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
10750 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
10751 		    asoc->sent_queue_retran_cnt);
10752 		asoc->sent_queue_cnt = 0;
10753 		asoc->sent_queue_cnt_removeable = 0;
10754 		/* send back 0/0 so we enter normal transmission */
10755 		*cnt_out = 0;
10756 		return (0);
10757 	}
10758 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10759 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
10760 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
10761 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
10762 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
10763 				if (chk != asoc->str_reset) {
10764 					/*
10765 					 * not eligible for retran if its
10766 					 * not ours
10767 					 */
10768 					continue;
10769 				}
10770 			}
10771 			ctl_cnt++;
10772 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10773 				fwd_tsn = 1;
10774 				fwd = chk;
10775 			}
10776 			/*
10777 			 * Add an AUTH chunk, if chunk requires it save the
10778 			 * offset into the chain for AUTH
10779 			 */
10780 			if ((auth == NULL) &&
10781 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
10782 			    stcb->asoc.peer_auth_chunks))) {
10783 				m = sctp_add_auth_chunk(m, &endofchain,
10784 				    &auth, &auth_offset,
10785 				    stcb,
10786 				    chk->rec.chunk_id.id);
10787 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10788 			}
10789 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
10790 			break;
10791 		}
10792 	}
10793 	one_chunk = 0;
10794 	cnt_thru = 0;
10795 	/* do we have control chunks to retransmit? */
10796 	if (m != NULL) {
10797 		/* Start a timer no matter if we suceed or fail */
10798 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
10799 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
10800 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
10801 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
10802 		chk->snd_count++;	/* update our count */
10803 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
10804 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
10805 		    auth_offset, auth, stcb->asoc.authinfo.active_keyid,
10806 		    no_fragmentflg, 0, NULL, 0,
10807 		    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10808 		    chk->whoTo->port, so_locked, NULL))) {
10809 			SCTP_STAT_INCR(sctps_lowlevelerr);
10810 			return (error);
10811 		}
10812 		m = endofchain = NULL;
10813 		auth = NULL;
10814 		auth_offset = 0;
10815 		/*
10816 		 * We don't want to mark the net->sent time here since this
10817 		 * we use this for HB and retrans cannot measure RTT
10818 		 */
10819 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
10820 		*cnt_out += 1;
10821 		chk->sent = SCTP_DATAGRAM_SENT;
10822 		sctp_ucount_decr(asoc->sent_queue_retran_cnt);
10823 		if (fwd_tsn == 0) {
10824 			return (0);
10825 		} else {
10826 			/* Clean up the fwd-tsn list */
10827 			sctp_clean_up_ctl(stcb, asoc);
10828 			return (0);
10829 		}
10830 	}
10831 	/*
10832 	 * Ok, it is just data retransmission we need to do or that and a
10833 	 * fwd-tsn with it all.
10834 	 */
10835 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
10836 		return (SCTP_RETRAN_DONE);
10837 	}
10838 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
10839 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
10840 		/* not yet open, resend the cookie and that is it */
10841 		return (1);
10842 	}
10843 #ifdef SCTP_AUDITING_ENABLED
10844 	sctp_auditing(20, inp, stcb, NULL);
10845 #endif
10846 	data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
10847 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
10848 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
10849 			/* No, not sent to this net or not ready for rtx */
10850 			continue;
10851 		}
10852 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
10853 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
10854 			/* Gak, we have exceeded max unlucky retran, abort! */
10855 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
10856 			    chk->snd_count,
10857 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
10858 			atomic_add_int(&stcb->asoc.refcnt, 1);
10859 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
10860 			SCTP_TCB_LOCK(stcb);
10861 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
10862 			return (SCTP_RETRAN_EXIT);
10863 		}
10864 		/* pick up the net */
10865 		net = chk->whoTo;
10866 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10867 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
10868 		} else {
10869 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
10870 		}
10871 
10872 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
10873 			/* No room in peers rwnd */
10874 			uint32_t tsn;
10875 
10876 			tsn = asoc->last_acked_seq + 1;
10877 			if (tsn == chk->rec.data.TSN_seq) {
10878 				/*
10879 				 * we make a special exception for this
10880 				 * case. The peer has no rwnd but is missing
10881 				 * the lowest chunk.. which is probably what
10882 				 * is holding up the rwnd.
10883 				 */
10884 				goto one_chunk_around;
10885 			}
10886 			return (1);
10887 		}
10888 one_chunk_around:
10889 		if (asoc->peers_rwnd < mtu) {
10890 			one_chunk = 1;
10891 			if ((asoc->peers_rwnd == 0) &&
10892 			    (asoc->total_flight == 0)) {
10893 				chk->window_probe = 1;
10894 				chk->whoTo->window_probe = 1;
10895 			}
10896 		}
10897 #ifdef SCTP_AUDITING_ENABLED
10898 		sctp_audit_log(0xC3, 2);
10899 #endif
10900 		bundle_at = 0;
10901 		m = NULL;
10902 		net->fast_retran_ip = 0;
10903 		if (chk->rec.data.doing_fast_retransmit == 0) {
10904 			/*
10905 			 * if no FR in progress skip destination that have
10906 			 * flight_size > cwnd.
10907 			 */
10908 			if (net->flight_size >= net->cwnd) {
10909 				continue;
10910 			}
10911 		} else {
10912 			/*
10913 			 * Mark the destination net to have FR recovery
10914 			 * limits put on it.
10915 			 */
10916 			*fr_done = 1;
10917 			net->fast_retran_ip = 1;
10918 		}
10919 
10920 		/*
10921 		 * if no AUTH is yet included and this chunk requires it,
10922 		 * make sure to account for it.  We don't apply the size
10923 		 * until the AUTH chunk is actually added below in case
10924 		 * there is no room for this chunk.
10925 		 */
10926 		if (data_auth_reqd && (auth == NULL)) {
10927 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
10928 		} else
10929 			dmtu = 0;
10930 
10931 		if ((chk->send_size <= (mtu - dmtu)) ||
10932 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
10933 			/* ok we will add this one */
10934 			if (data_auth_reqd) {
10935 				if (auth == NULL) {
10936 					m = sctp_add_auth_chunk(m,
10937 					    &endofchain,
10938 					    &auth,
10939 					    &auth_offset,
10940 					    stcb,
10941 					    SCTP_DATA);
10942 					auth_keyid = chk->auth_keyid;
10943 					override_ok = 0;
10944 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10945 				} else if (override_ok) {
10946 					auth_keyid = chk->auth_keyid;
10947 					override_ok = 0;
10948 				} else if (chk->auth_keyid != auth_keyid) {
10949 					/* different keyid, so done bundling */
10950 					break;
10951 				}
10952 			}
10953 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
10954 			if (m == NULL) {
10955 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10956 				return (ENOMEM);
10957 			}
10958 			/* Do clear IP_DF ? */
10959 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
10960 				no_fragmentflg = 0;
10961 			}
10962 			/* upate our MTU size */
10963 			if (mtu > (chk->send_size + dmtu))
10964 				mtu -= (chk->send_size + dmtu);
10965 			else
10966 				mtu = 0;
10967 			data_list[bundle_at++] = chk;
10968 			if (one_chunk && (asoc->total_flight <= 0)) {
10969 				SCTP_STAT_INCR(sctps_windowprobed);
10970 			}
10971 		}
10972 		if (one_chunk == 0) {
10973 			/*
10974 			 * now are there anymore forward from chk to pick
10975 			 * up?
10976 			 */
10977 			fwd = TAILQ_NEXT(chk, sctp_next);
10978 			while (fwd) {
10979 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
10980 					/* Nope, not for retran */
10981 					fwd = TAILQ_NEXT(fwd, sctp_next);
10982 					continue;
10983 				}
10984 				if (fwd->whoTo != net) {
10985 					/* Nope, not the net in question */
10986 					fwd = TAILQ_NEXT(fwd, sctp_next);
10987 					continue;
10988 				}
10989 				if (data_auth_reqd && (auth == NULL)) {
10990 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
10991 				} else
10992 					dmtu = 0;
10993 				if (fwd->send_size <= (mtu - dmtu)) {
10994 					if (data_auth_reqd) {
10995 						if (auth == NULL) {
10996 							m = sctp_add_auth_chunk(m,
10997 							    &endofchain,
10998 							    &auth,
10999 							    &auth_offset,
11000 							    stcb,
11001 							    SCTP_DATA);
11002 							auth_keyid = fwd->auth_keyid;
11003 							override_ok = 0;
11004 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11005 						} else if (override_ok) {
11006 							auth_keyid = fwd->auth_keyid;
11007 							override_ok = 0;
11008 						} else if (fwd->auth_keyid != auth_keyid) {
11009 							/*
11010 							 * different keyid,
11011 							 * so done bundling
11012 							 */
11013 							break;
11014 						}
11015 					}
11016 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
11017 					if (m == NULL) {
11018 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11019 						return (ENOMEM);
11020 					}
11021 					/* Do clear IP_DF ? */
11022 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
11023 						no_fragmentflg = 0;
11024 					}
11025 					/* upate our MTU size */
11026 					if (mtu > (fwd->send_size + dmtu))
11027 						mtu -= (fwd->send_size + dmtu);
11028 					else
11029 						mtu = 0;
11030 					data_list[bundle_at++] = fwd;
11031 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
11032 						break;
11033 					}
11034 					fwd = TAILQ_NEXT(fwd, sctp_next);
11035 				} else {
11036 					/* can't fit so we are done */
11037 					break;
11038 				}
11039 			}
11040 		}
11041 		/* Is there something to send for this destination? */
11042 		if (m) {
11043 			/*
11044 			 * No matter if we fail/or suceed we should start a
11045 			 * timer. A failure is like a lost IP packet :-)
11046 			 */
11047 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
11048 				/*
11049 				 * no timer running on this destination
11050 				 * restart it.
11051 				 */
11052 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
11053 				tmr_started = 1;
11054 			}
11055 			/* Now lets send it, if there is anything to send :> */
11056 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
11057 			    (struct sockaddr *)&net->ro._l_addr, m,
11058 			    auth_offset, auth, auth_keyid,
11059 			    no_fragmentflg, 0, NULL, 0,
11060 			    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
11061 			    net->port, so_locked, NULL))) {
11062 				/* error, we could not output */
11063 				SCTP_STAT_INCR(sctps_lowlevelerr);
11064 				return (error);
11065 			}
11066 			m = endofchain = NULL;
11067 			auth = NULL;
11068 			auth_offset = 0;
11069 			/* For HB's */
11070 			/*
11071 			 * We don't want to mark the net->sent time here
11072 			 * since this we use this for HB and retrans cannot
11073 			 * measure RTT
11074 			 */
11075 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
11076 
11077 			/* For auto-close */
11078 			cnt_thru++;
11079 			if (*now_filled == 0) {
11080 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
11081 				*now = asoc->time_last_sent;
11082 				*now_filled = 1;
11083 			} else {
11084 				asoc->time_last_sent = *now;
11085 			}
11086 			*cnt_out += bundle_at;
11087 #ifdef SCTP_AUDITING_ENABLED
11088 			sctp_audit_log(0xC4, bundle_at);
11089 #endif
11090 			if (bundle_at) {
11091 				tsns_sent = data_list[0]->rec.data.TSN_seq;
11092 			}
11093 			for (i = 0; i < bundle_at; i++) {
11094 				SCTP_STAT_INCR(sctps_sendretransdata);
11095 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
11096 				/*
11097 				 * When we have a revoked data, and we
11098 				 * retransmit it, then we clear the revoked
11099 				 * flag since this flag dictates if we
11100 				 * subtracted from the fs
11101 				 */
11102 				if (data_list[i]->rec.data.chunk_was_revoked) {
11103 					/* Deflate the cwnd */
11104 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
11105 					data_list[i]->rec.data.chunk_was_revoked = 0;
11106 				}
11107 				data_list[i]->snd_count++;
11108 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
11109 				/* record the time */
11110 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
11111 				if (data_list[i]->book_size_scale) {
11112 					/*
11113 					 * need to double the book size on
11114 					 * this one
11115 					 */
11116 					data_list[i]->book_size_scale = 0;
11117 					/*
11118 					 * Since we double the booksize, we
11119 					 * must also double the output queue
11120 					 * size, since this get shrunk when
11121 					 * we free by this amount.
11122 					 */
11123 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
11124 					data_list[i]->book_size *= 2;
11125 
11126 
11127 				} else {
11128 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
11129 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
11130 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
11131 					}
11132 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
11133 					    (uint32_t) (data_list[i]->send_size +
11134 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
11135 				}
11136 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
11137 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
11138 					    data_list[i]->whoTo->flight_size,
11139 					    data_list[i]->book_size,
11140 					    (uintptr_t) data_list[i]->whoTo,
11141 					    data_list[i]->rec.data.TSN_seq);
11142 				}
11143 				sctp_flight_size_increase(data_list[i]);
11144 				sctp_total_flight_increase(stcb, data_list[i]);
11145 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
11146 					/* SWS sender side engages */
11147 					asoc->peers_rwnd = 0;
11148 				}
11149 				if ((i == 0) &&
11150 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
11151 					SCTP_STAT_INCR(sctps_sendfastretrans);
11152 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
11153 					    (tmr_started == 0)) {
11154 						/*-
11155 						 * ok we just fast-retrans'd
11156 						 * the lowest TSN, i.e the
11157 						 * first on the list. In
11158 						 * this case we want to give
11159 						 * some more time to get a
11160 						 * SACK back without a
11161 						 * t3-expiring.
11162 						 */
11163 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
11164 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
11165 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
11166 					}
11167 				}
11168 			}
11169 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11170 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
11171 			}
11172 #ifdef SCTP_AUDITING_ENABLED
11173 			sctp_auditing(21, inp, stcb, NULL);
11174 #endif
11175 		} else {
11176 			/* None will fit */
11177 			return (1);
11178 		}
11179 		if (asoc->sent_queue_retran_cnt <= 0) {
11180 			/* all done we have no more to retran */
11181 			asoc->sent_queue_retran_cnt = 0;
11182 			break;
11183 		}
11184 		if (one_chunk) {
11185 			/* No more room in rwnd */
11186 			return (1);
11187 		}
11188 		/* stop the for loop here. we sent out a packet */
11189 		break;
11190 	}
11191 	return (0);
11192 }
11193 
11194 
11195 static int
11196 sctp_timer_validation(struct sctp_inpcb *inp,
11197     struct sctp_tcb *stcb,
11198     struct sctp_association *asoc,
11199     int ret)
11200 {
11201 	struct sctp_nets *net;
11202 
11203 	/* Validate that a timer is running somewhere */
11204 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
11205 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
11206 			/* Here is a timer */
11207 			return (ret);
11208 		}
11209 	}
11210 	SCTP_TCB_LOCK_ASSERT(stcb);
11211 	/* Gak, we did not have a timer somewhere */
11212 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
11213 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
11214 	return (ret);
11215 }
11216 
11217 void
11218 sctp_chunk_output(struct sctp_inpcb *inp,
11219     struct sctp_tcb *stcb,
11220     int from_where,
11221     int so_locked
11222 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
11223     SCTP_UNUSED
11224 #endif
11225 )
11226 {
11227 	/*-
11228 	 * Ok this is the generic chunk service queue. we must do the
11229 	 * following:
11230 	 * - See if there are retransmits pending, if so we must
11231 	 *   do these first.
11232 	 * - Service the stream queue that is next, moving any
11233 	 *   message (note I must get a complete message i.e.
11234 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
11235 	 *   TSN's
11236 	 * - Check to see if the cwnd/rwnd allows any output, if so we
11237 	 *   go ahead and fomulate and send the low level chunks. Making sure
11238 	 *   to combine any control in the control chunk queue also.
11239 	 */
11240 	struct sctp_association *asoc;
11241 	struct sctp_nets *net;
11242 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
11243 	    burst_cnt = 0, burst_limit = 0;
11244 	struct timeval now;
11245 	int now_filled = 0;
11246 	int cwnd_full = 0;
11247 	int nagle_on = 0;
11248 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
11249 	int un_sent = 0;
11250 	int fr_done, tot_frs = 0;
11251 
11252 	asoc = &stcb->asoc;
11253 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
11254 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
11255 			nagle_on = 0;
11256 		} else {
11257 			nagle_on = 1;
11258 		}
11259 	}
11260 	SCTP_TCB_LOCK_ASSERT(stcb);
11261 
11262 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
11263 
11264 	if ((un_sent <= 0) &&
11265 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
11266 	    (asoc->sent_queue_retran_cnt == 0)) {
11267 		/* Nothing to do unless there is something to be sent left */
11268 		return;
11269 	}
11270 	/*
11271 	 * Do we have something to send, data or control AND a sack timer
11272 	 * running, if so piggy-back the sack.
11273 	 */
11274 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
11275 		/*
11276 		 * EY if nr_sacks used then send an nr-sack , a sack
11277 		 * otherwise
11278 		 */
11279 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
11280 			sctp_send_nr_sack(stcb);
11281 		else
11282 			sctp_send_sack(stcb);
11283 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
11284 	}
11285 	while (asoc->sent_queue_retran_cnt) {
11286 		/*-
11287 		 * Ok, it is retransmission time only, we send out only ONE
11288 		 * packet with a single call off to the retran code.
11289 		 */
11290 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
11291 			/*-
11292 			 * Special hook for handling cookiess discarded
11293 			 * by peer that carried data. Send cookie-ack only
11294 			 * and then the next call with get the retran's.
11295 			 */
11296 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
11297 			    &cwnd_full, from_where,
11298 			    &now, &now_filled, frag_point, so_locked);
11299 			return;
11300 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
11301 			/* if its not from a HB then do it */
11302 			fr_done = 0;
11303 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
11304 			if (fr_done) {
11305 				tot_frs++;
11306 			}
11307 		} else {
11308 			/*
11309 			 * its from any other place, we don't allow retran
11310 			 * output (only control)
11311 			 */
11312 			ret = 1;
11313 		}
11314 		if (ret > 0) {
11315 			/* Can't send anymore */
11316 			/*-
11317 			 * now lets push out control by calling med-level
11318 			 * output once. this assures that we WILL send HB's
11319 			 * if queued too.
11320 			 */
11321 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
11322 			    &cwnd_full, from_where,
11323 			    &now, &now_filled, frag_point, so_locked);
11324 #ifdef SCTP_AUDITING_ENABLED
11325 			sctp_auditing(8, inp, stcb, NULL);
11326 #endif
11327 			(void)sctp_timer_validation(inp, stcb, asoc, ret);
11328 			return;
11329 		}
11330 		if (ret < 0) {
11331 			/*-
11332 			 * The count was off.. retran is not happening so do
11333 			 * the normal retransmission.
11334 			 */
11335 #ifdef SCTP_AUDITING_ENABLED
11336 			sctp_auditing(9, inp, stcb, NULL);
11337 #endif
11338 			if (ret == SCTP_RETRAN_EXIT) {
11339 				return;
11340 			}
11341 			break;
11342 		}
11343 		if (from_where == SCTP_OUTPUT_FROM_T3) {
11344 			/* Only one transmission allowed out of a timeout */
11345 #ifdef SCTP_AUDITING_ENABLED
11346 			sctp_auditing(10, inp, stcb, NULL);
11347 #endif
11348 			/* Push out any control */
11349 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
11350 			    &now, &now_filled, frag_point, so_locked);
11351 			return;
11352 		}
11353 		if (tot_frs > asoc->max_burst) {
11354 			/* Hit FR burst limit */
11355 			return;
11356 		}
11357 		if ((num_out == 0) && (ret == 0)) {
11358 
11359 			/* No more retrans to send */
11360 			break;
11361 		}
11362 	}
11363 #ifdef SCTP_AUDITING_ENABLED
11364 	sctp_auditing(12, inp, stcb, NULL);
11365 #endif
11366 	/* Check for bad destinations, if they exist move chunks around. */
11367 	burst_limit = asoc->max_burst;
11368 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
11369 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
11370 		    SCTP_ADDR_NOT_REACHABLE) {
11371 			/*-
11372 			 * if possible move things off of this address we
11373 			 * still may send below due to the dormant state but
11374 			 * we try to find an alternate address to send to
11375 			 * and if we have one we move all queued data on the
11376 			 * out wheel to this alternate address.
11377 			 */
11378 			if (net->ref_count > 1)
11379 				sctp_move_to_an_alt(stcb, asoc, net);
11380 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
11381 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
11382 		    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
11383 			/*
11384 			 * JRS 5/14/07 - If CMT PF is on and the current
11385 			 * destination is in PF state, move all queued data
11386 			 * to an alternate desination.
11387 			 */
11388 			if (net->ref_count > 1)
11389 				sctp_move_to_an_alt(stcb, asoc, net);
11390 		} else {
11391 			/*-
11392 			 * if ((asoc->sat_network) || (net->addr_is_local))
11393 			 * { burst_limit = asoc->max_burst *
11394 			 * SCTP_SAT_NETWORK_BURST_INCR; }
11395 			 */
11396 			if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
11397 				if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
11398 					/*
11399 					 * JRS - Use the congestion control
11400 					 * given in the congestion control
11401 					 * module
11402 					 */
11403 					asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
11404 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11405 						sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
11406 					}
11407 					SCTP_STAT_INCR(sctps_maxburstqueued);
11408 				}
11409 				net->fast_retran_ip = 0;
11410 			} else {
11411 				if (net->flight_size == 0) {
11412 					/* Should be decaying the cwnd here */
11413 					;
11414 				}
11415 			}
11416 		}
11417 
11418 	}
11419 	burst_cnt = 0;
11420 	cwnd_full = 0;
11421 	do {
11422 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
11423 		    &reason_code, 0, &cwnd_full, from_where,
11424 		    &now, &now_filled, frag_point, so_locked);
11425 		if (error) {
11426 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
11427 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11428 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
11429 			}
11430 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11431 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
11432 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
11433 			}
11434 			break;
11435 		}
11436 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
11437 
11438 		tot_out += num_out;
11439 		burst_cnt++;
11440 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11441 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
11442 			if (num_out == 0) {
11443 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
11444 			}
11445 		}
11446 		if (nagle_on) {
11447 			/*-
11448 			 * When nagle is on, we look at how much is un_sent, then
11449 			 * if its smaller than an MTU and we have data in
11450 			 * flight we stop.
11451 			 */
11452 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11453 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
11454 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
11455 			    (stcb->asoc.total_flight > 0)) {
11456 				break;
11457 			}
11458 		}
11459 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
11460 		    TAILQ_EMPTY(&asoc->send_queue) &&
11461 		    TAILQ_EMPTY(&asoc->out_wheel)) {
11462 			/* Nothing left to send */
11463 			break;
11464 		}
11465 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
11466 			/* Nothing left to send */
11467 			break;
11468 		}
11469 	} while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
11470 	    (burst_cnt < burst_limit)));
11471 
11472 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
11473 		if (burst_cnt >= burst_limit) {
11474 			SCTP_STAT_INCR(sctps_maxburstqueued);
11475 			asoc->burst_limit_applied = 1;
11476 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11477 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
11478 			}
11479 		} else {
11480 			asoc->burst_limit_applied = 0;
11481 		}
11482 	}
11483 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11484 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
11485 	}
11486 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
11487 	    tot_out);
11488 
11489 	/*-
11490 	 * Now we need to clean up the control chunk chain if a ECNE is on
11491 	 * it. It must be marked as UNSENT again so next call will continue
11492 	 * to send it until such time that we get a CWR, to remove it.
11493 	 */
11494 	if (stcb->asoc.ecn_echo_cnt_onq)
11495 		sctp_fix_ecn_echo(asoc);
11496 	return;
11497 }
11498 
11499 
11500 int
11501 sctp_output(inp, m, addr, control, p, flags)
11502 	struct sctp_inpcb *inp;
11503 	struct mbuf *m;
11504 	struct sockaddr *addr;
11505 	struct mbuf *control;
11506 	struct thread *p;
11507 	int flags;
11508 {
11509 	if (inp == NULL) {
11510 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11511 		return (EINVAL);
11512 	}
11513 	if (inp->sctp_socket == NULL) {
11514 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11515 		return (EINVAL);
11516 	}
11517 	return (sctp_sosend(inp->sctp_socket,
11518 	    addr,
11519 	    (struct uio *)NULL,
11520 	    m,
11521 	    control,
11522 	    flags, p
11523 	    ));
11524 }
11525 
11526 void
11527 send_forward_tsn(struct sctp_tcb *stcb,
11528     struct sctp_association *asoc)
11529 {
11530 	struct sctp_tmit_chunk *chk;
11531 	struct sctp_forward_tsn_chunk *fwdtsn;
11532 
11533 	SCTP_TCB_LOCK_ASSERT(stcb);
11534 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11535 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
11536 			/* mark it to unsent */
11537 			chk->sent = SCTP_DATAGRAM_UNSENT;
11538 			chk->snd_count = 0;
11539 			/* Do we correct its output location? */
11540 			if (chk->whoTo != asoc->primary_destination) {
11541 				sctp_free_remote_addr(chk->whoTo);
11542 				chk->whoTo = asoc->primary_destination;
11543 				atomic_add_int(&chk->whoTo->ref_count, 1);
11544 			}
11545 			goto sctp_fill_in_rest;
11546 		}
11547 	}
11548 	/* Ok if we reach here we must build one */
11549 	sctp_alloc_a_chunk(stcb, chk);
11550 	if (chk == NULL) {
11551 		return;
11552 	}
11553 	chk->copy_by_ref = 0;
11554 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
11555 	chk->rec.chunk_id.can_take_data = 0;
11556 	chk->asoc = asoc;
11557 	chk->whoTo = NULL;
11558 
11559 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11560 	if (chk->data == NULL) {
11561 		sctp_free_a_chunk(stcb, chk);
11562 		return;
11563 	}
11564 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11565 	chk->sent = SCTP_DATAGRAM_UNSENT;
11566 	chk->snd_count = 0;
11567 	chk->whoTo = asoc->primary_destination;
11568 	atomic_add_int(&chk->whoTo->ref_count, 1);
11569 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
11570 	asoc->ctrl_queue_cnt++;
11571 sctp_fill_in_rest:
11572 	/*-
11573 	 * Here we go through and fill out the part that deals with
11574 	 * stream/seq of the ones we skip.
11575 	 */
11576 	SCTP_BUF_LEN(chk->data) = 0;
11577 	{
11578 		struct sctp_tmit_chunk *at, *tp1, *last;
11579 		struct sctp_strseq *strseq;
11580 		unsigned int cnt_of_space, i, ovh;
11581 		unsigned int space_needed;
11582 		unsigned int cnt_of_skipped = 0;
11583 
11584 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
11585 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
11586 				/* no more to look at */
11587 				break;
11588 			}
11589 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
11590 				/* We don't report these */
11591 				continue;
11592 			}
11593 			cnt_of_skipped++;
11594 		}
11595 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
11596 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
11597 
11598 		cnt_of_space = M_TRAILINGSPACE(chk->data);
11599 
11600 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
11601 			ovh = SCTP_MIN_OVERHEAD;
11602 		} else {
11603 			ovh = SCTP_MIN_V4_OVERHEAD;
11604 		}
11605 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
11606 			/* trim to a mtu size */
11607 			cnt_of_space = asoc->smallest_mtu - ovh;
11608 		}
11609 		if (cnt_of_space < space_needed) {
11610 			/*-
11611 			 * ok we must trim down the chunk by lowering the
11612 			 * advance peer ack point.
11613 			 */
11614 			cnt_of_skipped = (cnt_of_space -
11615 			    ((sizeof(struct sctp_forward_tsn_chunk)) /
11616 			    sizeof(struct sctp_strseq)));
11617 			/*-
11618 			 * Go through and find the TSN that will be the one
11619 			 * we report.
11620 			 */
11621 			at = TAILQ_FIRST(&asoc->sent_queue);
11622 			for (i = 0; i < cnt_of_skipped; i++) {
11623 				tp1 = TAILQ_NEXT(at, sctp_next);
11624 				at = tp1;
11625 			}
11626 			last = at;
11627 			/*-
11628 			 * last now points to last one I can report, update
11629 			 * peer ack point
11630 			 */
11631 			asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
11632 			space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
11633 		}
11634 		chk->send_size = space_needed;
11635 		/* Setup the chunk */
11636 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
11637 		fwdtsn->ch.chunk_length = htons(chk->send_size);
11638 		fwdtsn->ch.chunk_flags = 0;
11639 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
11640 		fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
11641 		chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
11642 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
11643 		SCTP_BUF_LEN(chk->data) = chk->send_size;
11644 		fwdtsn++;
11645 		/*-
11646 		 * Move pointer to after the fwdtsn and transfer to the
11647 		 * strseq pointer.
11648 		 */
11649 		strseq = (struct sctp_strseq *)fwdtsn;
11650 		/*-
11651 		 * Now populate the strseq list. This is done blindly
11652 		 * without pulling out duplicate stream info. This is
11653 		 * inefficent but won't harm the process since the peer will
11654 		 * look at these in sequence and will thus release anything.
11655 		 * It could mean we exceed the PMTU and chop off some that
11656 		 * we could have included.. but this is unlikely (aka 1432/4
11657 		 * would mean 300+ stream seq's would have to be reported in
11658 		 * one FWD-TSN. With a bit of work we can later FIX this to
11659 		 * optimize and pull out duplcates.. but it does add more
11660 		 * overhead. So for now... not!
11661 		 */
11662 		at = TAILQ_FIRST(&asoc->sent_queue);
11663 		for (i = 0; i < cnt_of_skipped; i++) {
11664 			tp1 = TAILQ_NEXT(at, sctp_next);
11665 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
11666 				/* We don't report these */
11667 				i--;
11668 				at = tp1;
11669 				continue;
11670 			}
11671 			strseq->stream = ntohs(at->rec.data.stream_number);
11672 			strseq->sequence = ntohs(at->rec.data.stream_seq);
11673 			strseq++;
11674 			at = tp1;
11675 		}
11676 	}
11677 	return;
11678 
11679 }
11680 
11681 void
11682 sctp_send_sack(struct sctp_tcb *stcb)
11683 {
11684 	/*-
11685 	 * Queue up a SACK in the control queue. We must first check to see
11686 	 * if a SACK is somehow on the control queue. If so, we will take
11687 	 * and and remove the old one.
11688 	 */
11689 	struct sctp_association *asoc;
11690 	struct sctp_tmit_chunk *chk, *a_chk;
11691 	struct sctp_sack_chunk *sack;
11692 	struct sctp_gap_ack_block *gap_descriptor;
11693 	struct sack_track *selector;
11694 	int mergeable = 0;
11695 	int offset;
11696 	caddr_t limit;
11697 	uint32_t *dup;
11698 	int limit_reached = 0;
11699 	unsigned int i, jstart, siz, j;
11700 	unsigned int num_gap_blocks = 0, space;
11701 	int num_dups = 0;
11702 	int space_req;
11703 
11704 	a_chk = NULL;
11705 	asoc = &stcb->asoc;
11706 	SCTP_TCB_LOCK_ASSERT(stcb);
11707 	if (asoc->last_data_chunk_from == NULL) {
11708 		/* Hmm we never received anything */
11709 		return;
11710 	}
11711 	sctp_set_rwnd(stcb, asoc);
11712 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11713 		if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
11714 			/* Hmm, found a sack already on queue, remove it */
11715 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
11716 			asoc->ctrl_queue_cnt++;
11717 			a_chk = chk;
11718 			if (a_chk->data) {
11719 				sctp_m_freem(a_chk->data);
11720 				a_chk->data = NULL;
11721 			}
11722 			sctp_free_remote_addr(a_chk->whoTo);
11723 			a_chk->whoTo = NULL;
11724 			break;
11725 		}
11726 	}
11727 	if (a_chk == NULL) {
11728 		sctp_alloc_a_chunk(stcb, a_chk);
11729 		if (a_chk == NULL) {
11730 			/* No memory so we drop the idea, and set a timer */
11731 			if (stcb->asoc.delayed_ack) {
11732 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
11733 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
11734 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
11735 				    stcb->sctp_ep, stcb, NULL);
11736 			} else {
11737 				stcb->asoc.send_sack = 1;
11738 			}
11739 			return;
11740 		}
11741 		a_chk->copy_by_ref = 0;
11742 		/* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
11743 		a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
11744 		a_chk->rec.chunk_id.can_take_data = 1;
11745 	}
11746 	/* Clear our pkt counts */
11747 	asoc->data_pkts_seen = 0;
11748 
11749 	a_chk->asoc = asoc;
11750 	a_chk->snd_count = 0;
11751 	a_chk->send_size = 0;	/* fill in later */
11752 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
11753 	a_chk->whoTo = NULL;
11754 
11755 	if ((asoc->numduptsns) ||
11756 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
11757 	    ) {
11758 		/*-
11759 		 * Ok, we have some duplicates or the destination for the
11760 		 * sack is unreachable, lets see if we can select an
11761 		 * alternate than asoc->last_data_chunk_from
11762 		 */
11763 		if ((!(asoc->last_data_chunk_from->dest_state &
11764 		    SCTP_ADDR_NOT_REACHABLE)) &&
11765 		    (asoc->used_alt_onsack > asoc->numnets)) {
11766 			/* We used an alt last time, don't this time */
11767 			a_chk->whoTo = NULL;
11768 		} else {
11769 			asoc->used_alt_onsack++;
11770 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
11771 		}
11772 		if (a_chk->whoTo == NULL) {
11773 			/* Nope, no alternate */
11774 			a_chk->whoTo = asoc->last_data_chunk_from;
11775 			asoc->used_alt_onsack = 0;
11776 		}
11777 	} else {
11778 		/*
11779 		 * No duplicates so we use the last place we received data
11780 		 * from.
11781 		 */
11782 		asoc->used_alt_onsack = 0;
11783 		a_chk->whoTo = asoc->last_data_chunk_from;
11784 	}
11785 	if (a_chk->whoTo) {
11786 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
11787 	}
11788 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
11789 		/* no gaps */
11790 		space_req = sizeof(struct sctp_sack_chunk);
11791 	} else {
11792 		/* gaps get a cluster */
11793 		space_req = MCLBYTES;
11794 	}
11795 	/* Ok now lets formulate a MBUF with our sack */
11796 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
11797 	if ((a_chk->data == NULL) ||
11798 	    (a_chk->whoTo == NULL)) {
11799 		/* rats, no mbuf memory */
11800 		if (a_chk->data) {
11801 			/* was a problem with the destination */
11802 			sctp_m_freem(a_chk->data);
11803 			a_chk->data = NULL;
11804 		}
11805 		sctp_free_a_chunk(stcb, a_chk);
11806 		/* sa_ignore NO_NULL_CHK */
11807 		if (stcb->asoc.delayed_ack) {
11808 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
11809 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
11810 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
11811 			    stcb->sctp_ep, stcb, NULL);
11812 		} else {
11813 			stcb->asoc.send_sack = 1;
11814 		}
11815 		return;
11816 	}
11817 	/* ok, lets go through and fill it in */
11818 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
11819 	space = M_TRAILINGSPACE(a_chk->data);
11820 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
11821 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
11822 	}
11823 	limit = mtod(a_chk->data, caddr_t);
11824 	limit += space;
11825 
11826 	sack = mtod(a_chk->data, struct sctp_sack_chunk *);
11827 	sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
11828 	/* 0x01 is used by nonce for ecn */
11829 	if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
11830 	    (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
11831 	    (asoc->peer_supports_ecn_nonce))
11832 		sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
11833 	else
11834 		sack->ch.chunk_flags = 0;
11835 
11836 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
11837 		/*-
11838 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
11839 		 * received, then set high bit to 1, else 0. Reset
11840 		 * pkts_rcvd.
11841 		 */
11842 		sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
11843 		asoc->cmt_dac_pkts_rcvd = 0;
11844 	}
11845 #ifdef SCTP_ASOCLOG_OF_TSNS
11846 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
11847 	stcb->asoc.cumack_log_atsnt++;
11848 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
11849 		stcb->asoc.cumack_log_atsnt = 0;
11850 	}
11851 #endif
11852 	sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
11853 	sack->sack.a_rwnd = htonl(asoc->my_rwnd);
11854 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
11855 
11856 	/* reset the readers interpretation */
11857 	stcb->freed_by_sorcv_sincelast = 0;
11858 
11859 	gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
11860 
11861 	siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
11862 	if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
11863 		offset = 1;
11864 		/*-
11865 		 * cum-ack behind the mapping array, so we start and use all
11866 		 * entries.
11867 		 */
11868 		jstart = 0;
11869 	} else {
11870 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
11871 		/*-
11872 		 * we skip the first one when the cum-ack is at or above the
11873 		 * mapping array base. Note this only works if
11874 		 */
11875 		jstart = 1;
11876 	}
11877 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
11878 		/* we have a gap .. maybe */
11879 		for (i = 0; i < siz; i++) {
11880 			selector = &sack_array[asoc->mapping_array[i]];
11881 			if (mergeable && selector->right_edge) {
11882 				/*
11883 				 * Backup, left and right edges were ok to
11884 				 * merge.
11885 				 */
11886 				num_gap_blocks--;
11887 				gap_descriptor--;
11888 			}
11889 			if (selector->num_entries == 0)
11890 				mergeable = 0;
11891 			else {
11892 				for (j = jstart; j < selector->num_entries; j++) {
11893 					if (mergeable && selector->right_edge) {
11894 						/*
11895 						 * do a merge by NOT setting
11896 						 * the left side
11897 						 */
11898 						mergeable = 0;
11899 					} else {
11900 						/*
11901 						 * no merge, set the left
11902 						 * side
11903 						 */
11904 						mergeable = 0;
11905 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
11906 					}
11907 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
11908 					num_gap_blocks++;
11909 					gap_descriptor++;
11910 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
11911 						/* no more room */
11912 						limit_reached = 1;
11913 						break;
11914 					}
11915 				}
11916 				if (selector->left_edge) {
11917 					mergeable = 1;
11918 				}
11919 			}
11920 			if (limit_reached) {
11921 				/* Reached the limit stop */
11922 				break;
11923 			}
11924 			jstart = 0;
11925 			offset += 8;
11926 		}
11927 		if (num_gap_blocks == 0) {
11928 			/*
11929 			 * slide not yet happened, and somehow we got called
11930 			 * to send a sack. Cumack needs to move up.
11931 			 */
11932 			int abort_flag = 0;
11933 
11934 			asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
11935 			sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
11936 			sctp_sack_check(stcb, 0, 0, &abort_flag);
11937 		}
11938 	}
11939 	/* now we must add any dups we are going to report. */
11940 	if ((limit_reached == 0) && (asoc->numduptsns)) {
11941 		dup = (uint32_t *) gap_descriptor;
11942 		for (i = 0; i < asoc->numduptsns; i++) {
11943 			*dup = htonl(asoc->dup_tsns[i]);
11944 			dup++;
11945 			num_dups++;
11946 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
11947 				/* no more room */
11948 				break;
11949 			}
11950 		}
11951 		asoc->numduptsns = 0;
11952 	}
11953 	/*
11954 	 * now that the chunk is prepared queue it to the control chunk
11955 	 * queue.
11956 	 */
11957 	a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
11958 	    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
11959 	    (num_dups * sizeof(int32_t)));
11960 	SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
11961 	sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
11962 	sack->sack.num_dup_tsns = htons(num_dups);
11963 	sack->ch.chunk_length = htons(a_chk->send_size);
11964 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
11965 	asoc->ctrl_queue_cnt++;
11966 	asoc->send_sack = 0;
11967 	SCTP_STAT_INCR(sctps_sendsacks);
11968 	return;
11969 }
11970 
11971 /* EY - This method will replace sctp_send_sack method if nr_sacks negotiated*/
11972 void
11973 sctp_send_nr_sack(struct sctp_tcb *stcb)
11974 {
11975 	/*-
11976 	 * Queue up an NR-SACK in the control queue. We must first check to see
11977 	 * if an NR-SACK is somehow on the control queue. If so, we will take
11978 	 * and and remove the old one.
11979 	 */
11980 	struct sctp_association *asoc;
11981 	struct sctp_tmit_chunk *chk, *a_chk;
11982 
11983 	struct sctp_nr_sack_chunk *nr_sack;
11984 
11985 	struct sctp_gap_ack_block *gap_descriptor;
11986 	struct sctp_nr_gap_ack_block *nr_gap_descriptor;
11987 
11988 	struct sack_track *selector;
11989 	struct nr_sack_track *nr_selector;
11990 
11991 	/* EY do we need nr_mergeable, NO */
11992 	int mergeable = 0;
11993 	int offset;
11994 	caddr_t limit;
11995 	uint32_t *dup;
11996 	int limit_reached = 0;
11997 	unsigned int i, jstart, siz, j;
11998 	unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
11999 	int num_dups = 0;
12000 	int space_req;
12001 	unsigned int reserved = 0;
12002 
12003 	a_chk = NULL;
12004 	asoc = &stcb->asoc;
12005 	SCTP_TCB_LOCK_ASSERT(stcb);
12006 	if (asoc->last_data_chunk_from == NULL) {
12007 		/* Hmm we never received anything */
12008 		return;
12009 	}
12010 	sctp_set_rwnd(stcb, asoc);
12011 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
12012 		if (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) {
12013 			/* Hmm, found a sack already on queue, remove it */
12014 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
12015 			asoc->ctrl_queue_cnt++;
12016 			a_chk = chk;
12017 			if (a_chk->data) {
12018 				sctp_m_freem(a_chk->data);
12019 				a_chk->data = NULL;
12020 			}
12021 			sctp_free_remote_addr(a_chk->whoTo);
12022 			a_chk->whoTo = NULL;
12023 			break;
12024 		}
12025 	}
12026 	if (a_chk == NULL) {
12027 		sctp_alloc_a_chunk(stcb, a_chk);
12028 		if (a_chk == NULL) {
12029 			/* No memory so we drop the idea, and set a timer */
12030 			if (stcb->asoc.delayed_ack) {
12031 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
12032 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
12033 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
12034 				    stcb->sctp_ep, stcb, NULL);
12035 			} else {
12036 				stcb->asoc.send_sack = 1;
12037 			}
12038 			return;
12039 		}
12040 		a_chk->copy_by_ref = 0;
12041 		/* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
12042 		a_chk->rec.chunk_id.id = SCTP_NR_SELECTIVE_ACK;
12043 		a_chk->rec.chunk_id.can_take_data = 1;
12044 	}
12045 	/* Clear our pkt counts */
12046 	asoc->data_pkts_seen = 0;
12047 
12048 	a_chk->asoc = asoc;
12049 	a_chk->snd_count = 0;
12050 	a_chk->send_size = 0;	/* fill in later */
12051 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
12052 	a_chk->whoTo = NULL;
12053 
12054 	if ((asoc->numduptsns) ||
12055 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
12056 	    ) {
12057 		/*-
12058 		 * Ok, we have some duplicates or the destination for the
12059 		 * sack is unreachable, lets see if we can select an
12060 		 * alternate than asoc->last_data_chunk_from
12061 		 */
12062 		if ((!(asoc->last_data_chunk_from->dest_state &
12063 		    SCTP_ADDR_NOT_REACHABLE)) &&
12064 		    (asoc->used_alt_onsack > asoc->numnets)) {
12065 			/* We used an alt last time, don't this time */
12066 			a_chk->whoTo = NULL;
12067 		} else {
12068 			asoc->used_alt_onsack++;
12069 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
12070 		}
12071 		if (a_chk->whoTo == NULL) {
12072 			/* Nope, no alternate */
12073 			a_chk->whoTo = asoc->last_data_chunk_from;
12074 			asoc->used_alt_onsack = 0;
12075 		}
12076 	} else {
12077 		/*
12078 		 * No duplicates so we use the last place we received data
12079 		 * from.
12080 		 */
12081 		asoc->used_alt_onsack = 0;
12082 		a_chk->whoTo = asoc->last_data_chunk_from;
12083 	}
12084 	if (a_chk->whoTo) {
12085 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
12086 	}
12087 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
12088 		/* no gaps */
12089 		space_req = sizeof(struct sctp_nr_sack_chunk);
12090 	} else {
12091 		/* EY - what is this about? */
12092 		/* gaps get a cluster */
12093 		space_req = MCLBYTES;
12094 	}
12095 	/* Ok now lets formulate a MBUF with our sack */
12096 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
12097 	if ((a_chk->data == NULL) ||
12098 	    (a_chk->whoTo == NULL)) {
12099 		/* rats, no mbuf memory */
12100 		if (a_chk->data) {
12101 			/* was a problem with the destination */
12102 			sctp_m_freem(a_chk->data);
12103 			a_chk->data = NULL;
12104 		}
12105 		sctp_free_a_chunk(stcb, a_chk);
12106 		/* sa_ignore NO_NULL_CHK */
12107 		if (stcb->asoc.delayed_ack) {
12108 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
12109 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
12110 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
12111 			    stcb->sctp_ep, stcb, NULL);
12112 		} else {
12113 			stcb->asoc.send_sack = 1;
12114 		}
12115 		return;
12116 	}
12117 	/* ok, lets go through and fill it in */
12118 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
12119 	space = M_TRAILINGSPACE(a_chk->data);
12120 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
12121 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
12122 	}
12123 	limit = mtod(a_chk->data, caddr_t);
12124 	limit += space;
12125 
12126 	nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
12127 	nr_sack->ch.chunk_type = SCTP_NR_SELECTIVE_ACK;
12128 	/* EYJ */
12129 	/* 0x01 is used by nonce for ecn */
12130 	if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
12131 	    (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
12132 	    (asoc->peer_supports_ecn_nonce))
12133 		nr_sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
12134 	else
12135 		nr_sack->ch.chunk_flags = 0;
12136 
12137 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
12138 		/*-
12139 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
12140 		 * received, then set high bit to 1, else 0. Reset
12141 		 * pkts_rcvd.
12142 		 */
12143 		/* EY - TODO: which chunk flag is used in here? -The LSB */
12144 		nr_sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
12145 		asoc->cmt_dac_pkts_rcvd = 0;
12146 	}
12147 	/*
12148 	 * EY - this is a never reneging receiver, that makes all gaps are
12149 	 * nr-gaps, set the All bit
12150 	 */
12151 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
12152 		nr_sack->ch.chunk_flags |= SCTP_NR_SACK_ALL_BIT;
12153 	}
12154 #ifdef SCTP_ASOCLOG_OF_TSNS
12155 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
12156 	stcb->asoc.cumack_log_atsnt++;
12157 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
12158 		stcb->asoc.cumack_log_atsnt = 0;
12159 	}
12160 #endif
12161 	nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
12162 	nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
12163 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
12164 
12165 	/* reset the readers interpretation */
12166 	stcb->freed_by_sorcv_sincelast = 0;
12167 
12168 	gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
12169 	nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
12170 
12171 	siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
12172 	if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
12173 		offset = 1;
12174 		/*-
12175 		 * cum-ack behind the mapping array, so we start and use all
12176 		 * entries.
12177 		 */
12178 		jstart = 0;
12179 	} else {
12180 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
12181 		/*-
12182 		 * we skip the first one when the cum-ack is at or above the
12183 		 * mapping array base. Note this only works if
12184 		 */
12185 		jstart = 1;
12186 	}
12187 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
12188 		/* we have a gap .. maybe */
12189 		for (i = 0; i < siz; i++) {
12190 			selector = &sack_array[asoc->mapping_array[i]];
12191 			if (mergeable && selector->right_edge) {
12192 				/*
12193 				 * Backup, left and right edges were ok to
12194 				 * merge.
12195 				 */
12196 				num_gap_blocks--;
12197 				gap_descriptor--;
12198 			}
12199 			if (selector->num_entries == 0)
12200 				mergeable = 0;
12201 			else {
12202 				for (j = jstart; j < selector->num_entries; j++) {
12203 					if (mergeable && selector->right_edge) {
12204 						/*
12205 						 * do a merge by NOT setting
12206 						 * the left side
12207 						 */
12208 						mergeable = 0;
12209 					} else {
12210 						/*
12211 						 * no merge, set the left
12212 						 * side
12213 						 */
12214 						mergeable = 0;
12215 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
12216 					}
12217 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
12218 					num_gap_blocks++;
12219 					gap_descriptor++;
12220 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
12221 						/* no more room */
12222 						limit_reached = 1;
12223 						break;
12224 					}
12225 				}
12226 				if (selector->left_edge) {
12227 					mergeable = 1;
12228 				}
12229 			}
12230 			if (limit_reached) {
12231 				/* Reached the limit stop */
12232 				break;
12233 			}
12234 			jstart = 0;
12235 			offset += 8;
12236 		}
12237 		if (num_gap_blocks == 0) {
12238 			/*
12239 			 * slide not yet happened, and somehow we got called
12240 			 * to send a sack. Cumack needs to move up.
12241 			 */
12242 			int abort_flag = 0;
12243 
12244 			asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
12245 			nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
12246 			sctp_sack_check(stcb, 0, 0, &abort_flag);
12247 		}
12248 	}
12249 	/*---------------------------------------------------------filling the nr_gap_ack blocks----------------------------------------------------*/
12250 
12251 	nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)gap_descriptor;
12252 
12253 	/* EY - there will be gaps + nr_gaps if draining is possible */
12254 	if (SCTP_BASE_SYSCTL(sctp_do_drain)) {
12255 
12256 		mergeable = 0;
12257 
12258 		siz = (((asoc->highest_tsn_inside_nr_map - asoc->nr_mapping_array_base_tsn) + 1) + 7) / 8;
12259 		if (compare_with_wrap(asoc->nr_mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
12260 			offset = 1;
12261 			/*-
12262 			* cum-ack behind the mapping array, so we start and use all
12263 			* entries.
12264 			*/
12265 			jstart = 0;
12266 		} else {
12267 			offset = asoc->nr_mapping_array_base_tsn - asoc->cumulative_tsn;
12268 			/*-
12269 			* we skip the first one when the cum-ack is at or above the
12270 			* mapping array base. Note this only works if
12271 			*/
12272 			jstart = 1;
12273 		}
12274 		if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
12275 			/* we have a gap .. maybe */
12276 			for (i = 0; i < siz; i++) {
12277 				nr_selector = &nr_sack_array[asoc->nr_mapping_array[i]];
12278 				if (mergeable && nr_selector->right_edge) {
12279 					/*
12280 					 * Backup, left and right edges were
12281 					 * ok to merge.
12282 					 */
12283 					num_nr_gap_blocks--;
12284 					nr_gap_descriptor--;
12285 				}
12286 				if (nr_selector->num_entries == 0)
12287 					mergeable = 0;
12288 				else {
12289 					for (j = jstart; j < nr_selector->num_entries; j++) {
12290 						if (mergeable && nr_selector->right_edge) {
12291 							/*
12292 							 * do a merge by NOT
12293 							 * setting the left
12294 							 * side
12295 							 */
12296 							mergeable = 0;
12297 						} else {
12298 							/*
12299 							 * no merge, set the
12300 							 * left side
12301 							 */
12302 							mergeable = 0;
12303 							nr_gap_descriptor->start = htons((nr_selector->nr_gaps[j].start + offset));
12304 						}
12305 						nr_gap_descriptor->end = htons((nr_selector->nr_gaps[j].end + offset));
12306 						num_nr_gap_blocks++;
12307 						nr_gap_descriptor++;
12308 						if (((caddr_t)nr_gap_descriptor + sizeof(struct sctp_nr_gap_ack_block)) > limit) {
12309 							/* no more room */
12310 							limit_reached = 1;
12311 							break;
12312 						}
12313 					}
12314 					if (nr_selector->left_edge) {
12315 						mergeable = 1;
12316 					}
12317 				}
12318 				if (limit_reached) {
12319 					/* Reached the limit stop */
12320 					break;
12321 				}
12322 				jstart = 0;
12323 				offset += 8;
12324 			}
12325 		}
12326 	}
12327 	/*---------------------------------------------------End of---filling the nr_gap_ack blocks----------------------------------------------------*/
12328 
12329 	/* now we must add any dups we are going to report. */
12330 	if ((limit_reached == 0) && (asoc->numduptsns)) {
12331 		dup = (uint32_t *) nr_gap_descriptor;
12332 		for (i = 0; i < asoc->numduptsns; i++) {
12333 			*dup = htonl(asoc->dup_tsns[i]);
12334 			dup++;
12335 			num_dups++;
12336 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
12337 				/* no more room */
12338 				break;
12339 			}
12340 		}
12341 		asoc->numduptsns = 0;
12342 	}
12343 	/*
12344 	 * now that the chunk is prepared queue it to the control chunk
12345 	 * queue.
12346 	 */
12347 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
12348 		num_nr_gap_blocks = num_gap_blocks;
12349 		num_gap_blocks = 0;
12350 	}
12351 	a_chk->send_size = (sizeof(struct sctp_nr_sack_chunk) +
12352 	    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
12353 	    (num_nr_gap_blocks * sizeof(struct sctp_nr_gap_ack_block)) +
12354 	    (num_dups * sizeof(int32_t)));
12355 
12356 	SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
12357 	nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
12358 	nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
12359 	nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
12360 	nr_sack->nr_sack.reserved = htons(reserved);
12361 	nr_sack->ch.chunk_length = htons(a_chk->send_size);
12362 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
12363 	asoc->ctrl_queue_cnt++;
12364 	asoc->send_sack = 0;
12365 	SCTP_STAT_INCR(sctps_sendsacks);
12366 	return;
12367 }
12368 
12369 void
12370 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
12371 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
12372     SCTP_UNUSED
12373 #endif
12374 )
12375 {
12376 	struct mbuf *m_abort;
12377 	struct mbuf *m_out = NULL, *m_end = NULL;
12378 	struct sctp_abort_chunk *abort = NULL;
12379 	int sz;
12380 	uint32_t auth_offset = 0;
12381 	struct sctp_auth_chunk *auth = NULL;
12382 
12383 	/*-
12384 	 * Add an AUTH chunk, if chunk requires it and save the offset into
12385 	 * the chain for AUTH
12386 	 */
12387 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
12388 	    stcb->asoc.peer_auth_chunks)) {
12389 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
12390 		    stcb, SCTP_ABORT_ASSOCIATION);
12391 		SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12392 	}
12393 	SCTP_TCB_LOCK_ASSERT(stcb);
12394 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
12395 	if (m_abort == NULL) {
12396 		/* no mbuf's */
12397 		if (m_out)
12398 			sctp_m_freem(m_out);
12399 		return;
12400 	}
12401 	/* link in any error */
12402 	SCTP_BUF_NEXT(m_abort) = operr;
12403 	sz = 0;
12404 	if (operr) {
12405 		struct mbuf *n;
12406 
12407 		n = operr;
12408 		while (n) {
12409 			sz += SCTP_BUF_LEN(n);
12410 			n = SCTP_BUF_NEXT(n);
12411 		}
12412 	}
12413 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
12414 	if (m_out == NULL) {
12415 		/* NO Auth chunk prepended, so reserve space in front */
12416 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
12417 		m_out = m_abort;
12418 	} else {
12419 		/* Put AUTH chunk at the front of the chain */
12420 		SCTP_BUF_NEXT(m_end) = m_abort;
12421 	}
12422 
12423 	/* fill in the ABORT chunk */
12424 	abort = mtod(m_abort, struct sctp_abort_chunk *);
12425 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
12426 	abort->ch.chunk_flags = 0;
12427 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
12428 
12429 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
12430 	    stcb->asoc.primary_destination,
12431 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
12432 	    m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
12433 	    stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
12434 	    stcb->asoc.primary_destination->port, so_locked, NULL);
12435 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12436 }
12437 
12438 void
12439 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
12440     struct sctp_nets *net)
12441 {
12442 	/* formulate and SEND a SHUTDOWN-COMPLETE */
12443 	struct mbuf *m_shutdown_comp;
12444 	struct sctp_shutdown_complete_chunk *shutdown_complete;
12445 
12446 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
12447 	if (m_shutdown_comp == NULL) {
12448 		/* no mbuf's */
12449 		return;
12450 	}
12451 	shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
12452 	shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
12453 	shutdown_complete->ch.chunk_flags = 0;
12454 	shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
12455 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
12456 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
12457 	    (struct sockaddr *)&net->ro._l_addr,
12458 	    m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
12459 	    stcb->sctp_ep->sctp_lport, stcb->rport,
12460 	    htonl(stcb->asoc.peer_vtag),
12461 	    net->port, SCTP_SO_NOT_LOCKED, NULL);
12462 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12463 	return;
12464 }
12465 
12466 void
12467 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
12468     uint32_t vrf_id, uint16_t port)
12469 {
12470 	/* formulate and SEND a SHUTDOWN-COMPLETE */
12471 	struct mbuf *o_pak;
12472 	struct mbuf *mout;
12473 	struct ip *iph, *iph_out;
12474 	struct udphdr *udp = NULL;
12475 
12476 #ifdef INET6
12477 	struct ip6_hdr *ip6, *ip6_out;
12478 
12479 #endif
12480 	int offset_out, len, mlen;
12481 	struct sctp_shutdown_complete_msg *comp_cp;
12482 
12483 	iph = mtod(m, struct ip *);
12484 	switch (iph->ip_v) {
12485 	case IPVERSION:
12486 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
12487 		break;
12488 #ifdef INET6
12489 	case IPV6_VERSION >> 4:
12490 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
12491 		break;
12492 #endif
12493 	default:
12494 		return;
12495 	}
12496 	if (port) {
12497 		len += sizeof(struct udphdr);
12498 	}
12499 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
12500 	if (mout == NULL) {
12501 		return;
12502 	}
12503 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
12504 	SCTP_BUF_LEN(mout) = len;
12505 	SCTP_BUF_NEXT(mout) = NULL;
12506 	iph_out = NULL;
12507 #ifdef INET6
12508 	ip6_out = NULL;
12509 #endif
12510 	offset_out = 0;
12511 
12512 	switch (iph->ip_v) {
12513 	case IPVERSION:
12514 		iph_out = mtod(mout, struct ip *);
12515 
12516 		/* Fill in the IP header for the ABORT */
12517 		iph_out->ip_v = IPVERSION;
12518 		iph_out->ip_hl = (sizeof(struct ip) / 4);
12519 		iph_out->ip_tos = (u_char)0;
12520 		iph_out->ip_id = 0;
12521 		iph_out->ip_off = 0;
12522 		iph_out->ip_ttl = MAXTTL;
12523 		if (port) {
12524 			iph_out->ip_p = IPPROTO_UDP;
12525 		} else {
12526 			iph_out->ip_p = IPPROTO_SCTP;
12527 		}
12528 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12529 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12530 
12531 		/* let IP layer calculate this */
12532 		iph_out->ip_sum = 0;
12533 		offset_out += sizeof(*iph_out);
12534 		comp_cp = (struct sctp_shutdown_complete_msg *)(
12535 		    (caddr_t)iph_out + offset_out);
12536 		break;
12537 #ifdef INET6
12538 	case IPV6_VERSION >> 4:
12539 		ip6 = (struct ip6_hdr *)iph;
12540 		ip6_out = mtod(mout, struct ip6_hdr *);
12541 
12542 		/* Fill in the IPv6 header for the ABORT */
12543 		ip6_out->ip6_flow = ip6->ip6_flow;
12544 		ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
12545 		if (port) {
12546 			ip6_out->ip6_nxt = IPPROTO_UDP;
12547 		} else {
12548 			ip6_out->ip6_nxt = IPPROTO_SCTP;
12549 		}
12550 		ip6_out->ip6_src = ip6->ip6_dst;
12551 		ip6_out->ip6_dst = ip6->ip6_src;
12552 		/*
12553 		 * ?? The old code had both the iph len + payload, I think
12554 		 * this is wrong and would never have worked
12555 		 */
12556 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
12557 		offset_out += sizeof(*ip6_out);
12558 		comp_cp = (struct sctp_shutdown_complete_msg *)(
12559 		    (caddr_t)ip6_out + offset_out);
12560 		break;
12561 #endif				/* INET6 */
12562 	default:
12563 		/* Currently not supported. */
12564 		sctp_m_freem(mout);
12565 		return;
12566 	}
12567 	if (port) {
12568 		udp = (struct udphdr *)comp_cp;
12569 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12570 		udp->uh_dport = port;
12571 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
12572 		udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12573 		offset_out += sizeof(struct udphdr);
12574 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
12575 	}
12576 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12577 		/* no mbuf's */
12578 		sctp_m_freem(mout);
12579 		return;
12580 	}
12581 	/* Now copy in and fill in the ABORT tags etc. */
12582 	comp_cp->sh.src_port = sh->dest_port;
12583 	comp_cp->sh.dest_port = sh->src_port;
12584 	comp_cp->sh.checksum = 0;
12585 	comp_cp->sh.v_tag = sh->v_tag;
12586 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
12587 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
12588 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
12589 
12590 	if (iph_out != NULL) {
12591 		sctp_route_t ro;
12592 		int ret;
12593 		struct sctp_tcb *stcb = NULL;
12594 
12595 		mlen = SCTP_BUF_LEN(mout);
12596 		bzero(&ro, sizeof ro);
12597 		/* set IPv4 length */
12598 		iph_out->ip_len = mlen;
12599 #ifdef  SCTP_PACKET_LOGGING
12600 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12601 			sctp_packet_log(mout, mlen);
12602 #endif
12603 		if (port) {
12604 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
12605 			SCTP_STAT_INCR(sctps_sendswcrc);
12606 			SCTP_ENABLE_UDP_CSUM(mout);
12607 		} else {
12608 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12609 			mout->m_pkthdr.csum_data = 0;	/* FIXME MT */
12610 			SCTP_STAT_INCR(sctps_sendhwcrc);
12611 		}
12612 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
12613 		/* out it goes */
12614 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
12615 
12616 		/* Free the route if we got one back */
12617 		if (ro.ro_rt)
12618 			RTFREE(ro.ro_rt);
12619 	}
12620 #ifdef INET6
12621 	if (ip6_out != NULL) {
12622 		struct route_in6 ro;
12623 		int ret;
12624 		struct sctp_tcb *stcb = NULL;
12625 		struct ifnet *ifp = NULL;
12626 
12627 		bzero(&ro, sizeof(ro));
12628 		mlen = SCTP_BUF_LEN(mout);
12629 #ifdef  SCTP_PACKET_LOGGING
12630 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12631 			sctp_packet_log(mout, mlen);
12632 #endif
12633 		comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
12634 		SCTP_STAT_INCR(sctps_sendswcrc);
12635 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
12636 		if (port) {
12637 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
12638 			    sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
12639 				udp->uh_sum = 0xffff;
12640 			}
12641 		}
12642 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
12643 
12644 		/* Free the route if we got one back */
12645 		if (ro.ro_rt)
12646 			RTFREE(ro.ro_rt);
12647 	}
12648 #endif
12649 	SCTP_STAT_INCR(sctps_sendpackets);
12650 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12651 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12652 	return;
12653 
12654 }
12655 
12656 static struct sctp_nets *
12657 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
12658 {
12659 	struct sctp_nets *net, *hnet;
12660 	int ms_goneby, highest_ms, state_overide = 0;
12661 
12662 	(void)SCTP_GETTIME_TIMEVAL(now);
12663 	highest_ms = 0;
12664 	hnet = NULL;
12665 	SCTP_TCB_LOCK_ASSERT(stcb);
12666 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
12667 		if (
12668 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
12669 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
12670 		    ) {
12671 			/*
12672 			 * Skip this guy from consideration if HB is off AND
12673 			 * its confirmed
12674 			 */
12675 			continue;
12676 		}
12677 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
12678 			/* skip this dest net from consideration */
12679 			continue;
12680 		}
12681 		if (net->last_sent_time.tv_sec) {
12682 			/* Sent to so we subtract */
12683 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
12684 		} else
12685 			/* Never been sent to */
12686 			ms_goneby = 0x7fffffff;
12687 		/*-
12688 		 * When the address state is unconfirmed but still
12689 		 * considered reachable, we HB at a higher rate. Once it
12690 		 * goes confirmed OR reaches the "unreachable" state, thenw
12691 		 * we cut it back to HB at a more normal pace.
12692 		 */
12693 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
12694 			state_overide = 1;
12695 		} else {
12696 			state_overide = 0;
12697 		}
12698 
12699 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
12700 		    (ms_goneby > highest_ms)) {
12701 			highest_ms = ms_goneby;
12702 			hnet = net;
12703 		}
12704 	}
12705 	if (hnet &&
12706 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
12707 		state_overide = 1;
12708 	} else {
12709 		state_overide = 0;
12710 	}
12711 
12712 	if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
12713 		/*-
12714 		 * Found the one with longest delay bounds OR it is
12715 		 * unconfirmed and still not marked unreachable.
12716 		 */
12717 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
12718 #ifdef SCTP_DEBUG
12719 		if (hnet) {
12720 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
12721 			    (struct sockaddr *)&hnet->ro._l_addr);
12722 		} else {
12723 			SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
12724 		}
12725 #endif
12726 		/* update the timer now */
12727 		hnet->last_sent_time = *now;
12728 		return (hnet);
12729 	}
12730 	/* Nothing to HB */
12731 	return (NULL);
12732 }
12733 
12734 int
12735 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
12736 {
12737 	struct sctp_tmit_chunk *chk;
12738 	struct sctp_nets *net;
12739 	struct sctp_heartbeat_chunk *hb;
12740 	struct timeval now;
12741 	struct sockaddr_in *sin;
12742 	struct sockaddr_in6 *sin6;
12743 
12744 	SCTP_TCB_LOCK_ASSERT(stcb);
12745 	if (user_req == 0) {
12746 		net = sctp_select_hb_destination(stcb, &now);
12747 		if (net == NULL) {
12748 			/*-
12749 			 * All our busy none to send to, just start the
12750 			 * timer again.
12751 			 */
12752 			if (stcb->asoc.state == 0) {
12753 				return (0);
12754 			}
12755 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
12756 			    stcb->sctp_ep,
12757 			    stcb,
12758 			    net);
12759 			return (0);
12760 		}
12761 	} else {
12762 		net = u_net;
12763 		if (net == NULL) {
12764 			return (0);
12765 		}
12766 		(void)SCTP_GETTIME_TIMEVAL(&now);
12767 	}
12768 	sin = (struct sockaddr_in *)&net->ro._l_addr;
12769 	if (sin->sin_family != AF_INET) {
12770 		if (sin->sin_family != AF_INET6) {
12771 			/* huh */
12772 			return (0);
12773 		}
12774 	}
12775 	sctp_alloc_a_chunk(stcb, chk);
12776 	if (chk == NULL) {
12777 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
12778 		return (0);
12779 	}
12780 	chk->copy_by_ref = 0;
12781 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
12782 	chk->rec.chunk_id.can_take_data = 1;
12783 	chk->asoc = &stcb->asoc;
12784 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
12785 
12786 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
12787 	if (chk->data == NULL) {
12788 		sctp_free_a_chunk(stcb, chk);
12789 		return (0);
12790 	}
12791 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12792 	SCTP_BUF_LEN(chk->data) = chk->send_size;
12793 	chk->sent = SCTP_DATAGRAM_UNSENT;
12794 	chk->snd_count = 0;
12795 	chk->whoTo = net;
12796 	atomic_add_int(&chk->whoTo->ref_count, 1);
12797 	/* Now we have a mbuf that we can fill in with the details */
12798 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
12799 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
12800 	/* fill out chunk header */
12801 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
12802 	hb->ch.chunk_flags = 0;
12803 	hb->ch.chunk_length = htons(chk->send_size);
12804 	/* Fill out hb parameter */
12805 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
12806 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
12807 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
12808 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
12809 	/* Did our user request this one, put it in */
12810 	hb->heartbeat.hb_info.user_req = user_req;
12811 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
12812 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
12813 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
12814 		/*
12815 		 * we only take from the entropy pool if the address is not
12816 		 * confirmed.
12817 		 */
12818 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
12819 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
12820 	} else {
12821 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
12822 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
12823 	}
12824 	if (sin->sin_family == AF_INET) {
12825 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
12826 	} else if (sin->sin_family == AF_INET6) {
12827 		/* We leave the scope the way it is in our lookup table. */
12828 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
12829 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
12830 	} else {
12831 		/* huh compiler bug */
12832 		return (0);
12833 	}
12834 
12835 	/*
12836 	 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
12837 	 * PF-heartbeats.  Because of this, threshold management is done by
12838 	 * the t3 timer handler, and does not need to be done upon the send
12839 	 * of a PF-heartbeat. If CMT PF is on and the destination to which a
12840 	 * heartbeat is being sent is in PF state, do NOT do threshold
12841 	 * management.
12842 	 */
12843 	if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
12844 		/* ok we have a destination that needs a beat */
12845 		/* lets do the theshold management Qiaobing style */
12846 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
12847 		    stcb->asoc.max_send_times)) {
12848 			/*-
12849 			 * we have lost the association, in a way this is
12850 			 * quite bad since we really are one less time since
12851 			 * we really did not send yet. This is the down side
12852 			 * to the Q's style as defined in the RFC and not my
12853 			 * alternate style defined in the RFC.
12854 			 */
12855 			if (chk->data != NULL) {
12856 				sctp_m_freem(chk->data);
12857 				chk->data = NULL;
12858 			}
12859 			/*
12860 			 * Here we do NOT use the macro since the
12861 			 * association is now gone.
12862 			 */
12863 			if (chk->whoTo) {
12864 				sctp_free_remote_addr(chk->whoTo);
12865 				chk->whoTo = NULL;
12866 			}
12867 			sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
12868 			return (-1);
12869 		}
12870 	}
12871 	net->hb_responded = 0;
12872 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
12873 	stcb->asoc.ctrl_queue_cnt++;
12874 	SCTP_STAT_INCR(sctps_sendheartbeat);
12875 	/*-
12876 	 * Call directly med level routine to put out the chunk. It will
12877 	 * always tumble out control chunks aka HB but it may even tumble
12878 	 * out data too.
12879 	 */
12880 	return (1);
12881 }
12882 
12883 void
12884 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
12885     uint32_t high_tsn)
12886 {
12887 	struct sctp_association *asoc;
12888 	struct sctp_ecne_chunk *ecne;
12889 	struct sctp_tmit_chunk *chk;
12890 
12891 	asoc = &stcb->asoc;
12892 	SCTP_TCB_LOCK_ASSERT(stcb);
12893 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
12894 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
12895 			/* found a previous ECN_ECHO update it if needed */
12896 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
12897 			ecne->tsn = htonl(high_tsn);
12898 			return;
12899 		}
12900 	}
12901 	/* nope could not find one to update so we must build one */
12902 	sctp_alloc_a_chunk(stcb, chk);
12903 	if (chk == NULL) {
12904 		return;
12905 	}
12906 	chk->copy_by_ref = 0;
12907 	SCTP_STAT_INCR(sctps_sendecne);
12908 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
12909 	chk->rec.chunk_id.can_take_data = 0;
12910 	chk->asoc = &stcb->asoc;
12911 	chk->send_size = sizeof(struct sctp_ecne_chunk);
12912 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
12913 	if (chk->data == NULL) {
12914 		sctp_free_a_chunk(stcb, chk);
12915 		return;
12916 	}
12917 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12918 	SCTP_BUF_LEN(chk->data) = chk->send_size;
12919 	chk->sent = SCTP_DATAGRAM_UNSENT;
12920 	chk->snd_count = 0;
12921 	chk->whoTo = net;
12922 	atomic_add_int(&chk->whoTo->ref_count, 1);
12923 	stcb->asoc.ecn_echo_cnt_onq++;
12924 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
12925 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
12926 	ecne->ch.chunk_flags = 0;
12927 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
12928 	ecne->tsn = htonl(high_tsn);
12929 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
12930 	asoc->ctrl_queue_cnt++;
12931 }
12932 
12933 void
12934 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
12935     struct mbuf *m, int iphlen, int bad_crc)
12936 {
12937 	struct sctp_association *asoc;
12938 	struct sctp_pktdrop_chunk *drp;
12939 	struct sctp_tmit_chunk *chk;
12940 	uint8_t *datap;
12941 	int len;
12942 	int was_trunc = 0;
12943 	struct ip *iph;
12944 
12945 #ifdef INET6
12946 	struct ip6_hdr *ip6h;
12947 
12948 #endif
12949 	int fullsz = 0, extra = 0;
12950 	long spc;
12951 	int offset;
12952 	struct sctp_chunkhdr *ch, chunk_buf;
12953 	unsigned int chk_length;
12954 
12955 	if (!stcb) {
12956 		return;
12957 	}
12958 	asoc = &stcb->asoc;
12959 	SCTP_TCB_LOCK_ASSERT(stcb);
12960 	if (asoc->peer_supports_pktdrop == 0) {
12961 		/*-
12962 		 * peer must declare support before I send one.
12963 		 */
12964 		return;
12965 	}
12966 	if (stcb->sctp_socket == NULL) {
12967 		return;
12968 	}
12969 	sctp_alloc_a_chunk(stcb, chk);
12970 	if (chk == NULL) {
12971 		return;
12972 	}
12973 	chk->copy_by_ref = 0;
12974 	iph = mtod(m, struct ip *);
12975 	if (iph == NULL) {
12976 		sctp_free_a_chunk(stcb, chk);
12977 		return;
12978 	}
12979 	switch (iph->ip_v) {
12980 	case IPVERSION:
12981 		/* IPv4 */
12982 		len = chk->send_size = iph->ip_len;
12983 		break;
12984 #ifdef INET6
12985 	case IPV6_VERSION >> 4:
12986 		/* IPv6 */
12987 		ip6h = mtod(m, struct ip6_hdr *);
12988 		len = chk->send_size = htons(ip6h->ip6_plen);
12989 		break;
12990 #endif
12991 	default:
12992 		return;
12993 	}
12994 	/* Validate that we do not have an ABORT in here. */
12995 	offset = iphlen + sizeof(struct sctphdr);
12996 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
12997 	    sizeof(*ch), (uint8_t *) & chunk_buf);
12998 	while (ch != NULL) {
12999 		chk_length = ntohs(ch->chunk_length);
13000 		if (chk_length < sizeof(*ch)) {
13001 			/* break to abort land */
13002 			break;
13003 		}
13004 		switch (ch->chunk_type) {
13005 		case SCTP_PACKET_DROPPED:
13006 		case SCTP_ABORT_ASSOCIATION:
13007 			/*-
13008 			 * we don't respond with an PKT-DROP to an ABORT
13009 			 * or PKT-DROP
13010 			 */
13011 			sctp_free_a_chunk(stcb, chk);
13012 			return;
13013 		default:
13014 			break;
13015 		}
13016 		offset += SCTP_SIZE32(chk_length);
13017 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
13018 		    sizeof(*ch), (uint8_t *) & chunk_buf);
13019 	}
13020 
13021 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
13022 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
13023 		/*
13024 		 * only send 1 mtu worth, trim off the excess on the end.
13025 		 */
13026 		fullsz = len - extra;
13027 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
13028 		was_trunc = 1;
13029 	}
13030 	chk->asoc = &stcb->asoc;
13031 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
13032 	if (chk->data == NULL) {
13033 jump_out:
13034 		sctp_free_a_chunk(stcb, chk);
13035 		return;
13036 	}
13037 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13038 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
13039 	if (drp == NULL) {
13040 		sctp_m_freem(chk->data);
13041 		chk->data = NULL;
13042 		goto jump_out;
13043 	}
13044 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
13045 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
13046 	chk->book_size_scale = 0;
13047 	if (was_trunc) {
13048 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
13049 		drp->trunc_len = htons(fullsz);
13050 		/*
13051 		 * Len is already adjusted to size minus overhead above take
13052 		 * out the pkt_drop chunk itself from it.
13053 		 */
13054 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
13055 		len = chk->send_size;
13056 	} else {
13057 		/* no truncation needed */
13058 		drp->ch.chunk_flags = 0;
13059 		drp->trunc_len = htons(0);
13060 	}
13061 	if (bad_crc) {
13062 		drp->ch.chunk_flags |= SCTP_BADCRC;
13063 	}
13064 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
13065 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13066 	chk->sent = SCTP_DATAGRAM_UNSENT;
13067 	chk->snd_count = 0;
13068 	if (net) {
13069 		/* we should hit here */
13070 		chk->whoTo = net;
13071 	} else {
13072 		chk->whoTo = asoc->primary_destination;
13073 	}
13074 	atomic_add_int(&chk->whoTo->ref_count, 1);
13075 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
13076 	chk->rec.chunk_id.can_take_data = 1;
13077 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
13078 	drp->ch.chunk_length = htons(chk->send_size);
13079 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
13080 	if (spc < 0) {
13081 		spc = 0;
13082 	}
13083 	drp->bottle_bw = htonl(spc);
13084 	if (asoc->my_rwnd) {
13085 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
13086 		    asoc->size_on_all_streams +
13087 		    asoc->my_rwnd_control_len +
13088 		    stcb->sctp_socket->so_rcv.sb_cc);
13089 	} else {
13090 		/*-
13091 		 * If my rwnd is 0, possibly from mbuf depletion as well as
13092 		 * space used, tell the peer there is NO space aka onq == bw
13093 		 */
13094 		drp->current_onq = htonl(spc);
13095 	}
13096 	drp->reserved = 0;
13097 	datap = drp->data;
13098 	m_copydata(m, iphlen, len, (caddr_t)datap);
13099 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
13100 	asoc->ctrl_queue_cnt++;
13101 }
13102 
13103 void
13104 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
13105 {
13106 	struct sctp_association *asoc;
13107 	struct sctp_cwr_chunk *cwr;
13108 	struct sctp_tmit_chunk *chk;
13109 
13110 	asoc = &stcb->asoc;
13111 	SCTP_TCB_LOCK_ASSERT(stcb);
13112 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
13113 		if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
13114 			/* found a previous ECN_CWR update it if needed */
13115 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
13116 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
13117 			    MAX_TSN)) {
13118 				cwr->tsn = htonl(high_tsn);
13119 			}
13120 			return;
13121 		}
13122 	}
13123 	/* nope could not find one to update so we must build one */
13124 	sctp_alloc_a_chunk(stcb, chk);
13125 	if (chk == NULL) {
13126 		return;
13127 	}
13128 	chk->copy_by_ref = 0;
13129 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
13130 	chk->rec.chunk_id.can_take_data = 1;
13131 	chk->asoc = &stcb->asoc;
13132 	chk->send_size = sizeof(struct sctp_cwr_chunk);
13133 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
13134 	if (chk->data == NULL) {
13135 		sctp_free_a_chunk(stcb, chk);
13136 		return;
13137 	}
13138 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13139 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13140 	chk->sent = SCTP_DATAGRAM_UNSENT;
13141 	chk->snd_count = 0;
13142 	chk->whoTo = net;
13143 	atomic_add_int(&chk->whoTo->ref_count, 1);
13144 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
13145 	cwr->ch.chunk_type = SCTP_ECN_CWR;
13146 	cwr->ch.chunk_flags = 0;
13147 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
13148 	cwr->tsn = htonl(high_tsn);
13149 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
13150 	asoc->ctrl_queue_cnt++;
13151 }
13152 
13153 void
13154 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
13155     int number_entries, uint16_t * list,
13156     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
13157 {
13158 	int len, old_len, i;
13159 	struct sctp_stream_reset_out_request *req_out;
13160 	struct sctp_chunkhdr *ch;
13161 
13162 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13163 
13164 
13165 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13166 
13167 	/* get to new offset for the param. */
13168 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
13169 	/* now how long will this param be? */
13170 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
13171 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
13172 	req_out->ph.param_length = htons(len);
13173 	req_out->request_seq = htonl(seq);
13174 	req_out->response_seq = htonl(resp_seq);
13175 	req_out->send_reset_at_tsn = htonl(last_sent);
13176 	if (number_entries) {
13177 		for (i = 0; i < number_entries; i++) {
13178 			req_out->list_of_streams[i] = htons(list[i]);
13179 		}
13180 	}
13181 	if (SCTP_SIZE32(len) > len) {
13182 		/*-
13183 		 * Need to worry about the pad we may end up adding to the
13184 		 * end. This is easy since the struct is either aligned to 4
13185 		 * bytes or 2 bytes off.
13186 		 */
13187 		req_out->list_of_streams[number_entries] = 0;
13188 	}
13189 	/* now fix the chunk length */
13190 	ch->chunk_length = htons(len + old_len);
13191 	chk->book_size = len + old_len;
13192 	chk->book_size_scale = 0;
13193 	chk->send_size = SCTP_SIZE32(chk->book_size);
13194 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13195 	return;
13196 }
13197 
13198 
13199 void
13200 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
13201     int number_entries, uint16_t * list,
13202     uint32_t seq)
13203 {
13204 	int len, old_len, i;
13205 	struct sctp_stream_reset_in_request *req_in;
13206 	struct sctp_chunkhdr *ch;
13207 
13208 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13209 
13210 
13211 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13212 
13213 	/* get to new offset for the param. */
13214 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
13215 	/* now how long will this param be? */
13216 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
13217 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
13218 	req_in->ph.param_length = htons(len);
13219 	req_in->request_seq = htonl(seq);
13220 	if (number_entries) {
13221 		for (i = 0; i < number_entries; i++) {
13222 			req_in->list_of_streams[i] = htons(list[i]);
13223 		}
13224 	}
13225 	if (SCTP_SIZE32(len) > len) {
13226 		/*-
13227 		 * Need to worry about the pad we may end up adding to the
13228 		 * end. This is easy since the struct is either aligned to 4
13229 		 * bytes or 2 bytes off.
13230 		 */
13231 		req_in->list_of_streams[number_entries] = 0;
13232 	}
13233 	/* now fix the chunk length */
13234 	ch->chunk_length = htons(len + old_len);
13235 	chk->book_size = len + old_len;
13236 	chk->book_size_scale = 0;
13237 	chk->send_size = SCTP_SIZE32(chk->book_size);
13238 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13239 	return;
13240 }
13241 
13242 
13243 void
13244 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
13245     uint32_t seq)
13246 {
13247 	int len, old_len;
13248 	struct sctp_stream_reset_tsn_request *req_tsn;
13249 	struct sctp_chunkhdr *ch;
13250 
13251 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13252 
13253 
13254 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13255 
13256 	/* get to new offset for the param. */
13257 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
13258 	/* now how long will this param be? */
13259 	len = sizeof(struct sctp_stream_reset_tsn_request);
13260 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
13261 	req_tsn->ph.param_length = htons(len);
13262 	req_tsn->request_seq = htonl(seq);
13263 
13264 	/* now fix the chunk length */
13265 	ch->chunk_length = htons(len + old_len);
13266 	chk->send_size = len + old_len;
13267 	chk->book_size = SCTP_SIZE32(chk->send_size);
13268 	chk->book_size_scale = 0;
13269 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
13270 	return;
13271 }
13272 
13273 void
13274 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
13275     uint32_t resp_seq, uint32_t result)
13276 {
13277 	int len, old_len;
13278 	struct sctp_stream_reset_response *resp;
13279 	struct sctp_chunkhdr *ch;
13280 
13281 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13282 
13283 
13284 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13285 
13286 	/* get to new offset for the param. */
13287 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
13288 	/* now how long will this param be? */
13289 	len = sizeof(struct sctp_stream_reset_response);
13290 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
13291 	resp->ph.param_length = htons(len);
13292 	resp->response_seq = htonl(resp_seq);
13293 	resp->result = ntohl(result);
13294 
13295 	/* now fix the chunk length */
13296 	ch->chunk_length = htons(len + old_len);
13297 	chk->book_size = len + old_len;
13298 	chk->book_size_scale = 0;
13299 	chk->send_size = SCTP_SIZE32(chk->book_size);
13300 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13301 	return;
13302 
13303 }
13304 
13305 
13306 void
13307 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
13308     uint32_t resp_seq, uint32_t result,
13309     uint32_t send_una, uint32_t recv_next)
13310 {
13311 	int len, old_len;
13312 	struct sctp_stream_reset_response_tsn *resp;
13313 	struct sctp_chunkhdr *ch;
13314 
13315 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13316 
13317 
13318 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13319 
13320 	/* get to new offset for the param. */
13321 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
13322 	/* now how long will this param be? */
13323 	len = sizeof(struct sctp_stream_reset_response_tsn);
13324 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
13325 	resp->ph.param_length = htons(len);
13326 	resp->response_seq = htonl(resp_seq);
13327 	resp->result = htonl(result);
13328 	resp->senders_next_tsn = htonl(send_una);
13329 	resp->receivers_next_tsn = htonl(recv_next);
13330 
13331 	/* now fix the chunk length */
13332 	ch->chunk_length = htons(len + old_len);
13333 	chk->book_size = len + old_len;
13334 	chk->send_size = SCTP_SIZE32(chk->book_size);
13335 	chk->book_size_scale = 0;
13336 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13337 	return;
13338 }
13339 
13340 static void
13341 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
13342     uint32_t seq,
13343     uint16_t adding)
13344 {
13345 	int len, old_len;
13346 	struct sctp_chunkhdr *ch;
13347 	struct sctp_stream_reset_add_strm *addstr;
13348 
13349 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13350 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13351 
13352 	/* get to new offset for the param. */
13353 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
13354 	/* now how long will this param be? */
13355 	len = sizeof(struct sctp_stream_reset_add_strm);
13356 
13357 	/* Fill it out. */
13358 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
13359 	addstr->ph.param_length = htons(len);
13360 	addstr->request_seq = htonl(seq);
13361 	addstr->number_of_streams = htons(adding);
13362 	addstr->reserved = 0;
13363 
13364 	/* now fix the chunk length */
13365 	ch->chunk_length = htons(len + old_len);
13366 	chk->send_size = len + old_len;
13367 	chk->book_size = SCTP_SIZE32(chk->send_size);
13368 	chk->book_size_scale = 0;
13369 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
13370 	return;
13371 }
13372 
13373 int
13374 sctp_send_str_reset_req(struct sctp_tcb *stcb,
13375     int number_entries, uint16_t * list,
13376     uint8_t send_out_req,
13377     uint32_t resp_seq,
13378     uint8_t send_in_req,
13379     uint8_t send_tsn_req,
13380     uint8_t add_stream,
13381     uint16_t adding
13382 )
13383 {
13384 
13385 	struct sctp_association *asoc;
13386 	struct sctp_tmit_chunk *chk;
13387 	struct sctp_chunkhdr *ch;
13388 	uint32_t seq;
13389 
13390 	asoc = &stcb->asoc;
13391 	if (asoc->stream_reset_outstanding) {
13392 		/*-
13393 		 * Already one pending, must get ACK back to clear the flag.
13394 		 */
13395 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
13396 		return (EBUSY);
13397 	}
13398 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
13399 	    (add_stream == 0)) {
13400 		/* nothing to do */
13401 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13402 		return (EINVAL);
13403 	}
13404 	if (send_tsn_req && (send_out_req || send_in_req)) {
13405 		/* error, can't do that */
13406 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13407 		return (EINVAL);
13408 	}
13409 	sctp_alloc_a_chunk(stcb, chk);
13410 	if (chk == NULL) {
13411 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13412 		return (ENOMEM);
13413 	}
13414 	chk->copy_by_ref = 0;
13415 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
13416 	chk->rec.chunk_id.can_take_data = 0;
13417 	chk->asoc = &stcb->asoc;
13418 	chk->book_size = sizeof(struct sctp_chunkhdr);
13419 	chk->send_size = SCTP_SIZE32(chk->book_size);
13420 	chk->book_size_scale = 0;
13421 
13422 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
13423 	if (chk->data == NULL) {
13424 		sctp_free_a_chunk(stcb, chk);
13425 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13426 		return (ENOMEM);
13427 	}
13428 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13429 
13430 	/* setup chunk parameters */
13431 	chk->sent = SCTP_DATAGRAM_UNSENT;
13432 	chk->snd_count = 0;
13433 	chk->whoTo = asoc->primary_destination;
13434 	atomic_add_int(&chk->whoTo->ref_count, 1);
13435 
13436 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13437 	ch->chunk_type = SCTP_STREAM_RESET;
13438 	ch->chunk_flags = 0;
13439 	ch->chunk_length = htons(chk->book_size);
13440 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13441 
13442 	seq = stcb->asoc.str_reset_seq_out;
13443 	if (send_out_req) {
13444 		sctp_add_stream_reset_out(chk, number_entries, list,
13445 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
13446 		asoc->stream_reset_out_is_outstanding = 1;
13447 		seq++;
13448 		asoc->stream_reset_outstanding++;
13449 	}
13450 	if (add_stream) {
13451 		sctp_add_a_stream(chk, seq, adding);
13452 		seq++;
13453 		asoc->stream_reset_outstanding++;
13454 	}
13455 	if (send_in_req) {
13456 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
13457 		asoc->stream_reset_outstanding++;
13458 	}
13459 	if (send_tsn_req) {
13460 		sctp_add_stream_reset_tsn(chk, seq);
13461 		asoc->stream_reset_outstanding++;
13462 	}
13463 	asoc->str_reset = chk;
13464 
13465 	/* insert the chunk for sending */
13466 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
13467 	    chk,
13468 	    sctp_next);
13469 	asoc->ctrl_queue_cnt++;
13470 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
13471 	return (0);
13472 }
13473 
13474 void
13475 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
13476     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
13477 {
13478 	/*-
13479 	 * Formulate the abort message, and send it back down.
13480 	 */
13481 	struct mbuf *o_pak;
13482 	struct mbuf *mout;
13483 	struct sctp_abort_msg *abm;
13484 	struct ip *iph, *iph_out;
13485 	struct udphdr *udp;
13486 
13487 #ifdef INET6
13488 	struct ip6_hdr *ip6, *ip6_out;
13489 
13490 #endif
13491 	int iphlen_out, len;
13492 
13493 	/* don't respond to ABORT with ABORT */
13494 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
13495 		if (err_cause)
13496 			sctp_m_freem(err_cause);
13497 		return;
13498 	}
13499 	iph = mtod(m, struct ip *);
13500 	switch (iph->ip_v) {
13501 	case IPVERSION:
13502 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
13503 		break;
13504 #ifdef INET6
13505 	case IPV6_VERSION >> 4:
13506 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
13507 		break;
13508 #endif
13509 	default:
13510 		if (err_cause) {
13511 			sctp_m_freem(err_cause);
13512 		}
13513 		return;
13514 	}
13515 	if (port) {
13516 		len += sizeof(struct udphdr);
13517 	}
13518 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
13519 	if (mout == NULL) {
13520 		if (err_cause) {
13521 			sctp_m_freem(err_cause);
13522 		}
13523 		return;
13524 	}
13525 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
13526 	SCTP_BUF_LEN(mout) = len;
13527 	SCTP_BUF_NEXT(mout) = err_cause;
13528 	iph_out = NULL;
13529 #ifdef INET6
13530 	ip6_out = NULL;
13531 #endif
13532 	switch (iph->ip_v) {
13533 	case IPVERSION:
13534 		iph_out = mtod(mout, struct ip *);
13535 
13536 		/* Fill in the IP header for the ABORT */
13537 		iph_out->ip_v = IPVERSION;
13538 		iph_out->ip_hl = (sizeof(struct ip) / 4);
13539 		iph_out->ip_tos = (u_char)0;
13540 		iph_out->ip_id = 0;
13541 		iph_out->ip_off = 0;
13542 		iph_out->ip_ttl = MAXTTL;
13543 		if (port) {
13544 			iph_out->ip_p = IPPROTO_UDP;
13545 		} else {
13546 			iph_out->ip_p = IPPROTO_SCTP;
13547 		}
13548 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
13549 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
13550 		/* let IP layer calculate this */
13551 		iph_out->ip_sum = 0;
13552 
13553 		iphlen_out = sizeof(*iph_out);
13554 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
13555 		break;
13556 #ifdef INET6
13557 	case IPV6_VERSION >> 4:
13558 		ip6 = (struct ip6_hdr *)iph;
13559 		ip6_out = mtod(mout, struct ip6_hdr *);
13560 
13561 		/* Fill in the IP6 header for the ABORT */
13562 		ip6_out->ip6_flow = ip6->ip6_flow;
13563 		ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
13564 		if (port) {
13565 			ip6_out->ip6_nxt = IPPROTO_UDP;
13566 		} else {
13567 			ip6_out->ip6_nxt = IPPROTO_SCTP;
13568 		}
13569 		ip6_out->ip6_src = ip6->ip6_dst;
13570 		ip6_out->ip6_dst = ip6->ip6_src;
13571 
13572 		iphlen_out = sizeof(*ip6_out);
13573 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
13574 		break;
13575 #endif				/* INET6 */
13576 	default:
13577 		/* Currently not supported */
13578 		sctp_m_freem(mout);
13579 		return;
13580 	}
13581 
13582 	udp = (struct udphdr *)abm;
13583 	if (port) {
13584 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
13585 		udp->uh_dport = port;
13586 		/* set udp->uh_ulen later */
13587 		udp->uh_sum = 0;
13588 		iphlen_out += sizeof(struct udphdr);
13589 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
13590 	}
13591 	abm->sh.src_port = sh->dest_port;
13592 	abm->sh.dest_port = sh->src_port;
13593 	abm->sh.checksum = 0;
13594 	if (vtag == 0) {
13595 		abm->sh.v_tag = sh->v_tag;
13596 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
13597 	} else {
13598 		abm->sh.v_tag = htonl(vtag);
13599 		abm->msg.ch.chunk_flags = 0;
13600 	}
13601 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
13602 
13603 	if (err_cause) {
13604 		struct mbuf *m_tmp = err_cause;
13605 		int err_len = 0;
13606 
13607 		/* get length of the err_cause chain */
13608 		while (m_tmp != NULL) {
13609 			err_len += SCTP_BUF_LEN(m_tmp);
13610 			m_tmp = SCTP_BUF_NEXT(m_tmp);
13611 		}
13612 		len = SCTP_BUF_LEN(mout) + err_len;
13613 		if (err_len % 4) {
13614 			/* need pad at end of chunk */
13615 			uint32_t cpthis = 0;
13616 			int padlen;
13617 
13618 			padlen = 4 - (len % 4);
13619 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
13620 			len += padlen;
13621 		}
13622 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
13623 	} else {
13624 		len = SCTP_BUF_LEN(mout);
13625 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
13626 	}
13627 
13628 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
13629 		/* no mbuf's */
13630 		sctp_m_freem(mout);
13631 		return;
13632 	}
13633 	if (iph_out != NULL) {
13634 		sctp_route_t ro;
13635 		struct sctp_tcb *stcb = NULL;
13636 		int ret;
13637 
13638 		/* zap the stack pointer to the route */
13639 		bzero(&ro, sizeof ro);
13640 		if (port) {
13641 			udp->uh_ulen = htons(len - sizeof(struct ip));
13642 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
13643 		}
13644 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
13645 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
13646 		/* set IPv4 length */
13647 		iph_out->ip_len = len;
13648 		/* out it goes */
13649 #ifdef  SCTP_PACKET_LOGGING
13650 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13651 			sctp_packet_log(mout, len);
13652 #endif
13653 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
13654 		if (port) {
13655 			abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
13656 			SCTP_STAT_INCR(sctps_sendswcrc);
13657 			SCTP_ENABLE_UDP_CSUM(o_pak);
13658 		} else {
13659 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
13660 			mout->m_pkthdr.csum_data = 0;	/* FIXME MT */
13661 			SCTP_STAT_INCR(sctps_sendhwcrc);
13662 		}
13663 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
13664 
13665 		/* Free the route if we got one back */
13666 		if (ro.ro_rt)
13667 			RTFREE(ro.ro_rt);
13668 	}
13669 #ifdef INET6
13670 	if (ip6_out != NULL) {
13671 		struct route_in6 ro;
13672 		int ret;
13673 		struct sctp_tcb *stcb = NULL;
13674 		struct ifnet *ifp = NULL;
13675 
13676 		/* zap the stack pointer to the route */
13677 		bzero(&ro, sizeof(ro));
13678 		if (port) {
13679 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
13680 		}
13681 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
13682 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
13683 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
13684 #ifdef  SCTP_PACKET_LOGGING
13685 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13686 			sctp_packet_log(mout, len);
13687 #endif
13688 		abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
13689 		SCTP_STAT_INCR(sctps_sendswcrc);
13690 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
13691 		if (port) {
13692 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
13693 				udp->uh_sum = 0xffff;
13694 			}
13695 		}
13696 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
13697 
13698 		/* Free the route if we got one back */
13699 		if (ro.ro_rt)
13700 			RTFREE(ro.ro_rt);
13701 	}
13702 #endif
13703 	SCTP_STAT_INCR(sctps_sendpackets);
13704 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
13705 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
13706 }
13707 
13708 void
13709 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
13710     uint32_t vrf_id, uint16_t port)
13711 {
13712 	struct mbuf *o_pak;
13713 	struct sctphdr *sh, *sh_out;
13714 	struct sctp_chunkhdr *ch;
13715 	struct ip *iph, *iph_out;
13716 	struct udphdr *udp = NULL;
13717 	struct mbuf *mout;
13718 
13719 #ifdef INET6
13720 	struct ip6_hdr *ip6, *ip6_out;
13721 
13722 #endif
13723 	int iphlen_out, len;
13724 
13725 	iph = mtod(m, struct ip *);
13726 	sh = (struct sctphdr *)((caddr_t)iph + iphlen);
13727 	switch (iph->ip_v) {
13728 	case IPVERSION:
13729 		len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
13730 		break;
13731 #ifdef INET6
13732 	case IPV6_VERSION >> 4:
13733 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
13734 		break;
13735 #endif
13736 	default:
13737 		if (scm) {
13738 			sctp_m_freem(scm);
13739 		}
13740 		return;
13741 	}
13742 	if (port) {
13743 		len += sizeof(struct udphdr);
13744 	}
13745 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
13746 	if (mout == NULL) {
13747 		if (scm) {
13748 			sctp_m_freem(scm);
13749 		}
13750 		return;
13751 	}
13752 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
13753 	SCTP_BUF_LEN(mout) = len;
13754 	SCTP_BUF_NEXT(mout) = scm;
13755 	iph_out = NULL;
13756 #ifdef INET6
13757 	ip6_out = NULL;
13758 #endif
13759 	switch (iph->ip_v) {
13760 	case IPVERSION:
13761 		iph_out = mtod(mout, struct ip *);
13762 
13763 		/* Fill in the IP header for the ABORT */
13764 		iph_out->ip_v = IPVERSION;
13765 		iph_out->ip_hl = (sizeof(struct ip) / 4);
13766 		iph_out->ip_tos = (u_char)0;
13767 		iph_out->ip_id = 0;
13768 		iph_out->ip_off = 0;
13769 		iph_out->ip_ttl = MAXTTL;
13770 		if (port) {
13771 			iph_out->ip_p = IPPROTO_UDP;
13772 		} else {
13773 			iph_out->ip_p = IPPROTO_SCTP;
13774 		}
13775 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
13776 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
13777 		/* let IP layer calculate this */
13778 		iph_out->ip_sum = 0;
13779 
13780 		iphlen_out = sizeof(struct ip);
13781 		sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
13782 		break;
13783 #ifdef INET6
13784 	case IPV6_VERSION >> 4:
13785 		ip6 = (struct ip6_hdr *)iph;
13786 		ip6_out = mtod(mout, struct ip6_hdr *);
13787 
13788 		/* Fill in the IP6 header for the ABORT */
13789 		ip6_out->ip6_flow = ip6->ip6_flow;
13790 		ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
13791 		if (port) {
13792 			ip6_out->ip6_nxt = IPPROTO_UDP;
13793 		} else {
13794 			ip6_out->ip6_nxt = IPPROTO_SCTP;
13795 		}
13796 		ip6_out->ip6_src = ip6->ip6_dst;
13797 		ip6_out->ip6_dst = ip6->ip6_src;
13798 
13799 		iphlen_out = sizeof(struct ip6_hdr);
13800 		sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
13801 		break;
13802 #endif				/* INET6 */
13803 	default:
13804 		/* Currently not supported */
13805 		sctp_m_freem(mout);
13806 		return;
13807 	}
13808 
13809 	udp = (struct udphdr *)sh_out;
13810 	if (port) {
13811 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
13812 		udp->uh_dport = port;
13813 		/* set udp->uh_ulen later */
13814 		udp->uh_sum = 0;
13815 		iphlen_out += sizeof(struct udphdr);
13816 		sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
13817 	}
13818 	sh_out->src_port = sh->dest_port;
13819 	sh_out->dest_port = sh->src_port;
13820 	sh_out->v_tag = vtag;
13821 	sh_out->checksum = 0;
13822 
13823 	ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
13824 	ch->chunk_type = SCTP_OPERATION_ERROR;
13825 	ch->chunk_flags = 0;
13826 
13827 	if (scm) {
13828 		struct mbuf *m_tmp = scm;
13829 		int cause_len = 0;
13830 
13831 		/* get length of the err_cause chain */
13832 		while (m_tmp != NULL) {
13833 			cause_len += SCTP_BUF_LEN(m_tmp);
13834 			m_tmp = SCTP_BUF_NEXT(m_tmp);
13835 		}
13836 		len = SCTP_BUF_LEN(mout) + cause_len;
13837 		if (cause_len % 4) {
13838 			/* need pad at end of chunk */
13839 			uint32_t cpthis = 0;
13840 			int padlen;
13841 
13842 			padlen = 4 - (len % 4);
13843 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
13844 			len += padlen;
13845 		}
13846 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
13847 	} else {
13848 		len = SCTP_BUF_LEN(mout);
13849 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
13850 	}
13851 
13852 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
13853 		/* no mbuf's */
13854 		sctp_m_freem(mout);
13855 		return;
13856 	}
13857 	if (iph_out != NULL) {
13858 		sctp_route_t ro;
13859 		struct sctp_tcb *stcb = NULL;
13860 		int ret;
13861 
13862 		/* zap the stack pointer to the route */
13863 		bzero(&ro, sizeof ro);
13864 		if (port) {
13865 			udp->uh_ulen = htons(len - sizeof(struct ip));
13866 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
13867 		}
13868 		/* set IPv4 length */
13869 		iph_out->ip_len = len;
13870 		/* out it goes */
13871 #ifdef  SCTP_PACKET_LOGGING
13872 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13873 			sctp_packet_log(mout, len);
13874 #endif
13875 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
13876 		if (port) {
13877 			sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
13878 			SCTP_STAT_INCR(sctps_sendswcrc);
13879 			SCTP_ENABLE_UDP_CSUM(o_pak);
13880 		} else {
13881 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
13882 			mout->m_pkthdr.csum_data = 0;	/* FIXME MT */
13883 			SCTP_STAT_INCR(sctps_sendhwcrc);
13884 		}
13885 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
13886 
13887 		/* Free the route if we got one back */
13888 		if (ro.ro_rt)
13889 			RTFREE(ro.ro_rt);
13890 	}
13891 #ifdef INET6
13892 	if (ip6_out != NULL) {
13893 		struct route_in6 ro;
13894 		int ret;
13895 		struct sctp_tcb *stcb = NULL;
13896 		struct ifnet *ifp = NULL;
13897 
13898 		/* zap the stack pointer to the route */
13899 		bzero(&ro, sizeof(ro));
13900 		if (port) {
13901 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
13902 		}
13903 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
13904 #ifdef  SCTP_PACKET_LOGGING
13905 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13906 			sctp_packet_log(mout, len);
13907 #endif
13908 		sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
13909 		SCTP_STAT_INCR(sctps_sendswcrc);
13910 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
13911 		if (port) {
13912 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
13913 				udp->uh_sum = 0xffff;
13914 			}
13915 		}
13916 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
13917 
13918 		/* Free the route if we got one back */
13919 		if (ro.ro_rt)
13920 			RTFREE(ro.ro_rt);
13921 	}
13922 #endif
13923 	SCTP_STAT_INCR(sctps_sendpackets);
13924 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
13925 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
13926 }
13927 
13928 static struct mbuf *
13929 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
13930     struct uio *uio,
13931     struct sctp_sndrcvinfo *srcv,
13932     int max_send_len,
13933     int user_marks_eor,
13934     int *error,
13935     uint32_t * sndout,
13936     struct mbuf **new_tail)
13937 {
13938 	struct mbuf *m;
13939 
13940 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
13941 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
13942 	if (m == NULL) {
13943 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13944 		*error = ENOMEM;
13945 	} else {
13946 		*sndout = m_length(m, NULL);
13947 		*new_tail = m_last(m);
13948 	}
13949 	return (m);
13950 }
13951 
13952 static int
13953 sctp_copy_one(struct sctp_stream_queue_pending *sp,
13954     struct uio *uio,
13955     int resv_upfront)
13956 {
13957 	int left;
13958 
13959 	left = sp->length;
13960 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
13961 	    resv_upfront, 0);
13962 	if (sp->data == NULL) {
13963 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13964 		return (ENOMEM);
13965 	}
13966 	sp->tail_mbuf = m_last(sp->data);
13967 	return (0);
13968 }
13969 
13970 
13971 
13972 static struct sctp_stream_queue_pending *
13973 sctp_copy_it_in(struct sctp_tcb *stcb,
13974     struct sctp_association *asoc,
13975     struct sctp_sndrcvinfo *srcv,
13976     struct uio *uio,
13977     struct sctp_nets *net,
13978     int max_send_len,
13979     int user_marks_eor,
13980     int *error,
13981     int non_blocking)
13982 {
13983 	/*-
13984 	 * This routine must be very careful in its work. Protocol
13985 	 * processing is up and running so care must be taken to spl...()
13986 	 * when you need to do something that may effect the stcb/asoc. The
13987 	 * sb is locked however. When data is copied the protocol processing
13988 	 * should be enabled since this is a slower operation...
13989 	 */
13990 	struct sctp_stream_queue_pending *sp = NULL;
13991 	int resv_in_first;
13992 
13993 	*error = 0;
13994 	/* Now can we send this? */
13995 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
13996 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13997 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13998 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13999 		/* got data while shutting down */
14000 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14001 		*error = ECONNRESET;
14002 		goto out_now;
14003 	}
14004 	sctp_alloc_a_strmoq(stcb, sp);
14005 	if (sp == NULL) {
14006 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
14007 		*error = ENOMEM;
14008 		goto out_now;
14009 	}
14010 	sp->act_flags = 0;
14011 	sp->sender_all_done = 0;
14012 	sp->sinfo_flags = srcv->sinfo_flags;
14013 	sp->timetolive = srcv->sinfo_timetolive;
14014 	sp->ppid = srcv->sinfo_ppid;
14015 	sp->context = srcv->sinfo_context;
14016 	sp->strseq = 0;
14017 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
14018 
14019 	sp->stream = srcv->sinfo_stream;
14020 	sp->length = min(uio->uio_resid, max_send_len);
14021 	if ((sp->length == (uint32_t) uio->uio_resid) &&
14022 	    ((user_marks_eor == 0) ||
14023 	    (srcv->sinfo_flags & SCTP_EOF) ||
14024 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
14025 		sp->msg_is_complete = 1;
14026 	} else {
14027 		sp->msg_is_complete = 0;
14028 	}
14029 	sp->sender_all_done = 0;
14030 	sp->some_taken = 0;
14031 	sp->put_last_out = 0;
14032 	resv_in_first = sizeof(struct sctp_data_chunk);
14033 	sp->data = sp->tail_mbuf = NULL;
14034 	if (sp->length == 0) {
14035 		*error = 0;
14036 		goto skip_copy;
14037 	}
14038 	sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
14039 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
14040 		sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
14041 		sp->holds_key_ref = 1;
14042 	}
14043 	*error = sctp_copy_one(sp, uio, resv_in_first);
14044 skip_copy:
14045 	if (*error) {
14046 		sctp_free_a_strmoq(stcb, sp);
14047 		sp = NULL;
14048 	} else {
14049 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
14050 			sp->net = net;
14051 			sp->addr_over = 1;
14052 		} else {
14053 			sp->net = asoc->primary_destination;
14054 			sp->addr_over = 0;
14055 		}
14056 		atomic_add_int(&sp->net->ref_count, 1);
14057 		sctp_set_prsctp_policy(stcb, sp);
14058 	}
14059 out_now:
14060 	return (sp);
14061 }
14062 
14063 
14064 int
14065 sctp_sosend(struct socket *so,
14066     struct sockaddr *addr,
14067     struct uio *uio,
14068     struct mbuf *top,
14069     struct mbuf *control,
14070     int flags,
14071     struct thread *p
14072 )
14073 {
14074 	struct sctp_inpcb *inp;
14075 	int error, use_rcvinfo = 0;
14076 	struct sctp_sndrcvinfo srcv;
14077 	struct sockaddr *addr_to_use;
14078 
14079 #ifdef INET6
14080 	struct sockaddr_in sin;
14081 
14082 #endif
14083 
14084 	inp = (struct sctp_inpcb *)so->so_pcb;
14085 	if (control) {
14086 		/* process cmsg snd/rcv info (maybe a assoc-id) */
14087 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
14088 		    sizeof(srcv))) {
14089 			/* got one */
14090 			use_rcvinfo = 1;
14091 		}
14092 	}
14093 	addr_to_use = addr;
14094 #if defined(INET6)  && !defined(__Userspace__)	/* TODO port in6_sin6_2_sin */
14095 	if ((addr) && (addr->sa_family == AF_INET6)) {
14096 		struct sockaddr_in6 *sin6;
14097 
14098 		sin6 = (struct sockaddr_in6 *)addr;
14099 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
14100 			in6_sin6_2_sin(&sin, sin6);
14101 			addr_to_use = (struct sockaddr *)&sin;
14102 		}
14103 	}
14104 #endif
14105 	error = sctp_lower_sosend(so, addr_to_use, uio, top,
14106 	    control,
14107 	    flags,
14108 	    use_rcvinfo, &srcv
14109 	    ,p
14110 	    );
14111 	return (error);
14112 }
14113 
14114 
14115 int
14116 sctp_lower_sosend(struct socket *so,
14117     struct sockaddr *addr,
14118     struct uio *uio,
14119     struct mbuf *i_pak,
14120     struct mbuf *control,
14121     int flags,
14122     int use_rcvinfo,
14123     struct sctp_sndrcvinfo *srcv
14124     ,
14125     struct thread *p
14126 )
14127 {
14128 	unsigned int sndlen = 0, max_len;
14129 	int error, len;
14130 	struct mbuf *top = NULL;
14131 	int queue_only = 0, queue_only_for_init = 0;
14132 	int free_cnt_applied = 0;
14133 	int un_sent = 0;
14134 	int now_filled = 0;
14135 	unsigned int inqueue_bytes = 0;
14136 	struct sctp_block_entry be;
14137 	struct sctp_inpcb *inp;
14138 	struct sctp_tcb *stcb = NULL;
14139 	struct timeval now;
14140 	struct sctp_nets *net;
14141 	struct sctp_association *asoc;
14142 	struct sctp_inpcb *t_inp;
14143 	int user_marks_eor;
14144 	int create_lock_applied = 0;
14145 	int nagle_applies = 0;
14146 	int some_on_control = 0;
14147 	int got_all_of_the_send = 0;
14148 	int hold_tcblock = 0;
14149 	int non_blocking = 0;
14150 	int temp_flags = 0;
14151 	uint32_t local_add_more, local_soresv = 0;
14152 
14153 	error = 0;
14154 	net = NULL;
14155 	stcb = NULL;
14156 	asoc = NULL;
14157 
14158 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
14159 	if (inp == NULL) {
14160 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14161 		error = EFAULT;
14162 		if (i_pak) {
14163 			SCTP_RELEASE_PKT(i_pak);
14164 		}
14165 		return (error);
14166 	}
14167 	if ((uio == NULL) && (i_pak == NULL)) {
14168 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14169 		return (EINVAL);
14170 	}
14171 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
14172 	atomic_add_int(&inp->total_sends, 1);
14173 	if (uio) {
14174 		if (uio->uio_resid < 0) {
14175 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14176 			return (EINVAL);
14177 		}
14178 		sndlen = uio->uio_resid;
14179 	} else {
14180 		top = SCTP_HEADER_TO_CHAIN(i_pak);
14181 		sndlen = SCTP_HEADER_LEN(i_pak);
14182 	}
14183 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
14184 	    addr,
14185 	    sndlen);
14186 	/*-
14187 	 * Pre-screen address, if one is given the sin-len
14188 	 * must be set correctly!
14189 	 */
14190 	if (addr) {
14191 		if ((addr->sa_family == AF_INET) &&
14192 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
14193 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14194 			error = EINVAL;
14195 			goto out_unlocked;
14196 		} else if ((addr->sa_family == AF_INET6) &&
14197 		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
14198 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14199 			error = EINVAL;
14200 			goto out_unlocked;
14201 		}
14202 	}
14203 	hold_tcblock = 0;
14204 
14205 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
14206 	    (inp->sctp_socket->so_qlimit)) {
14207 		/* The listener can NOT send */
14208 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14209 		error = EFAULT;
14210 		goto out_unlocked;
14211 	}
14212 	if ((use_rcvinfo) && srcv) {
14213 		if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
14214 		    PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
14215 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14216 			error = EINVAL;
14217 			goto out_unlocked;
14218 		}
14219 		if (srcv->sinfo_flags)
14220 			SCTP_STAT_INCR(sctps_sends_with_flags);
14221 
14222 		if (srcv->sinfo_flags & SCTP_SENDALL) {
14223 			/* its a sendall */
14224 			error = sctp_sendall(inp, uio, top, srcv);
14225 			top = NULL;
14226 			goto out_unlocked;
14227 		}
14228 	}
14229 	/* now we must find the assoc */
14230 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
14231 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
14232 		SCTP_INP_RLOCK(inp);
14233 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
14234 		if (stcb == NULL) {
14235 			SCTP_INP_RUNLOCK(inp);
14236 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
14237 			error = ENOTCONN;
14238 			goto out_unlocked;
14239 		}
14240 		hold_tcblock = 0;
14241 		SCTP_INP_RUNLOCK(inp);
14242 		if (addr) {
14243 			/* Must locate the net structure if addr given */
14244 			net = sctp_findnet(stcb, addr);
14245 			if (net) {
14246 				/* validate port was 0 or correct */
14247 				struct sockaddr_in *sin;
14248 
14249 				sin = (struct sockaddr_in *)addr;
14250 				if ((sin->sin_port != 0) &&
14251 				    (sin->sin_port != stcb->rport)) {
14252 					net = NULL;
14253 				}
14254 			}
14255 			temp_flags |= SCTP_ADDR_OVER;
14256 		} else
14257 			net = stcb->asoc.primary_destination;
14258 		if (addr && (net == NULL)) {
14259 			/* Could not find address, was it legal */
14260 			if (addr->sa_family == AF_INET) {
14261 				struct sockaddr_in *sin;
14262 
14263 				sin = (struct sockaddr_in *)addr;
14264 				if (sin->sin_addr.s_addr == 0) {
14265 					if ((sin->sin_port == 0) ||
14266 					    (sin->sin_port == stcb->rport)) {
14267 						net = stcb->asoc.primary_destination;
14268 					}
14269 				}
14270 			} else {
14271 				struct sockaddr_in6 *sin6;
14272 
14273 				sin6 = (struct sockaddr_in6 *)addr;
14274 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
14275 					if ((sin6->sin6_port == 0) ||
14276 					    (sin6->sin6_port == stcb->rport)) {
14277 						net = stcb->asoc.primary_destination;
14278 					}
14279 				}
14280 			}
14281 		}
14282 		if (net == NULL) {
14283 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14284 			error = EINVAL;
14285 			goto out_unlocked;
14286 		}
14287 	} else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
14288 		stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
14289 		if (stcb) {
14290 			if (addr)
14291 				/*
14292 				 * Must locate the net structure if addr
14293 				 * given
14294 				 */
14295 				net = sctp_findnet(stcb, addr);
14296 			else
14297 				net = stcb->asoc.primary_destination;
14298 			if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
14299 			    ((net == NULL) || (addr == NULL))) {
14300 				struct sockaddr_in *sin;
14301 
14302 				if (addr == NULL) {
14303 					SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14304 					error = EINVAL;
14305 					goto out_unlocked;
14306 				}
14307 				sin = (struct sockaddr_in *)addr;
14308 				/* Validate port is 0 or correct */
14309 				if ((sin->sin_port != 0) &&
14310 				    (sin->sin_port != stcb->rport)) {
14311 					net = NULL;
14312 				}
14313 			}
14314 		}
14315 		hold_tcblock = 0;
14316 	} else if (addr) {
14317 		/*-
14318 		 * Since we did not use findep we must
14319 		 * increment it, and if we don't find a tcb
14320 		 * decrement it.
14321 		 */
14322 		SCTP_INP_WLOCK(inp);
14323 		SCTP_INP_INCR_REF(inp);
14324 		SCTP_INP_WUNLOCK(inp);
14325 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
14326 		if (stcb == NULL) {
14327 			SCTP_INP_WLOCK(inp);
14328 			SCTP_INP_DECR_REF(inp);
14329 			SCTP_INP_WUNLOCK(inp);
14330 		} else {
14331 			hold_tcblock = 1;
14332 		}
14333 	}
14334 	if ((stcb == NULL) && (addr)) {
14335 		/* Possible implicit send? */
14336 		SCTP_ASOC_CREATE_LOCK(inp);
14337 		create_lock_applied = 1;
14338 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
14339 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
14340 			/* Should I really unlock ? */
14341 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14342 			error = EFAULT;
14343 			goto out_unlocked;
14344 
14345 		}
14346 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
14347 		    (addr->sa_family == AF_INET6)) {
14348 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14349 			error = EINVAL;
14350 			goto out_unlocked;
14351 		}
14352 		SCTP_INP_WLOCK(inp);
14353 		SCTP_INP_INCR_REF(inp);
14354 		SCTP_INP_WUNLOCK(inp);
14355 		/* With the lock applied look again */
14356 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
14357 		if (stcb == NULL) {
14358 			SCTP_INP_WLOCK(inp);
14359 			SCTP_INP_DECR_REF(inp);
14360 			SCTP_INP_WUNLOCK(inp);
14361 		} else {
14362 			hold_tcblock = 1;
14363 		}
14364 		if (t_inp != inp) {
14365 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
14366 			error = ENOTCONN;
14367 			goto out_unlocked;
14368 		}
14369 	}
14370 	if (stcb == NULL) {
14371 		if (addr == NULL) {
14372 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
14373 			error = ENOENT;
14374 			goto out_unlocked;
14375 		} else {
14376 			/*
14377 			 * UDP style, we must go ahead and start the INIT
14378 			 * process
14379 			 */
14380 			uint32_t vrf_id;
14381 
14382 			if ((use_rcvinfo) && (srcv) &&
14383 			    ((srcv->sinfo_flags & SCTP_ABORT) ||
14384 			    ((srcv->sinfo_flags & SCTP_EOF) &&
14385 			    (sndlen == 0)))) {
14386 				/*-
14387 				 * User asks to abort a non-existant assoc,
14388 				 * or EOF a non-existant assoc with no data
14389 				 */
14390 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
14391 				error = ENOENT;
14392 				goto out_unlocked;
14393 			}
14394 			/* get an asoc/stcb struct */
14395 			vrf_id = inp->def_vrf_id;
14396 #ifdef INVARIANTS
14397 			if (create_lock_applied == 0) {
14398 				panic("Error, should hold create lock and I don't?");
14399 			}
14400 #endif
14401 			stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
14402 			    p
14403 			    );
14404 			if (stcb == NULL) {
14405 				/* Error is setup for us in the call */
14406 				goto out_unlocked;
14407 			}
14408 			if (create_lock_applied) {
14409 				SCTP_ASOC_CREATE_UNLOCK(inp);
14410 				create_lock_applied = 0;
14411 			} else {
14412 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
14413 			}
14414 			/*
14415 			 * Turn on queue only flag to prevent data from
14416 			 * being sent
14417 			 */
14418 			queue_only = 1;
14419 			asoc = &stcb->asoc;
14420 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
14421 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
14422 
14423 			/* initialize authentication params for the assoc */
14424 			sctp_initialize_auth_params(inp, stcb);
14425 
14426 			if (control) {
14427 				/*
14428 				 * see if a init structure exists in cmsg
14429 				 * headers
14430 				 */
14431 				struct sctp_initmsg initm;
14432 				int i;
14433 
14434 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
14435 				    sizeof(initm))) {
14436 					/*
14437 					 * we have an INIT override of the
14438 					 * default
14439 					 */
14440 					if (initm.sinit_max_attempts)
14441 						asoc->max_init_times = initm.sinit_max_attempts;
14442 					if (initm.sinit_num_ostreams)
14443 						asoc->pre_open_streams = initm.sinit_num_ostreams;
14444 					if (initm.sinit_max_instreams)
14445 						asoc->max_inbound_streams = initm.sinit_max_instreams;
14446 					if (initm.sinit_max_init_timeo)
14447 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
14448 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
14449 						struct sctp_stream_out *tmp_str;
14450 						int had_lock = 0;
14451 
14452 						/* Default is NOT correct */
14453 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
14454 						    asoc->streamoutcnt, asoc->pre_open_streams);
14455 						/*
14456 						 * What happens if this
14457 						 * fails? we panic ...
14458 						 */
14459 
14460 						if (hold_tcblock) {
14461 							had_lock = 1;
14462 							SCTP_TCB_UNLOCK(stcb);
14463 						}
14464 						SCTP_MALLOC(tmp_str,
14465 						    struct sctp_stream_out *,
14466 						    (asoc->pre_open_streams *
14467 						    sizeof(struct sctp_stream_out)),
14468 						    SCTP_M_STRMO);
14469 						if (had_lock) {
14470 							SCTP_TCB_LOCK(stcb);
14471 						}
14472 						if (tmp_str != NULL) {
14473 							SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
14474 							asoc->strmout = tmp_str;
14475 							asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
14476 						} else {
14477 							asoc->pre_open_streams = asoc->streamoutcnt;
14478 						}
14479 						for (i = 0; i < asoc->streamoutcnt; i++) {
14480 							/*-
14481 							 * inbound side must be set
14482 							 * to 0xffff, also NOTE when
14483 							 * we get the INIT-ACK back
14484 							 * (for INIT sender) we MUST
14485 							 * reduce the count
14486 							 * (streamoutcnt) but first
14487 							 * check if we sent to any
14488 							 * of the upper streams that
14489 							 * were dropped (if some
14490 							 * were). Those that were
14491 							 * dropped must be notified
14492 							 * to the upper layer as
14493 							 * failed to send.
14494 							 */
14495 							asoc->strmout[i].next_sequence_sent = 0x0;
14496 							TAILQ_INIT(&asoc->strmout[i].outqueue);
14497 							asoc->strmout[i].stream_no = i;
14498 							asoc->strmout[i].last_msg_incomplete = 0;
14499 							asoc->strmout[i].next_spoke.tqe_next = 0;
14500 							asoc->strmout[i].next_spoke.tqe_prev = 0;
14501 						}
14502 					}
14503 				}
14504 			}
14505 			hold_tcblock = 1;
14506 			/* out with the INIT */
14507 			queue_only_for_init = 1;
14508 			/*-
14509 			 * we may want to dig in after this call and adjust the MTU
14510 			 * value. It defaulted to 1500 (constant) but the ro
14511 			 * structure may now have an update and thus we may need to
14512 			 * change it BEFORE we append the message.
14513 			 */
14514 			net = stcb->asoc.primary_destination;
14515 			asoc = &stcb->asoc;
14516 		}
14517 	}
14518 	if ((SCTP_SO_IS_NBIO(so)
14519 	    || (flags & MSG_NBIO)
14520 	    )) {
14521 		non_blocking = 1;
14522 	}
14523 	asoc = &stcb->asoc;
14524 	atomic_add_int(&stcb->total_sends, 1);
14525 
14526 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
14527 		if (sndlen > asoc->smallest_mtu) {
14528 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14529 			error = EMSGSIZE;
14530 			goto out_unlocked;
14531 		}
14532 	}
14533 	/* would we block? */
14534 	if (non_blocking) {
14535 		if (hold_tcblock == 0) {
14536 			SCTP_TCB_LOCK(stcb);
14537 			hold_tcblock = 1;
14538 		}
14539 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14540 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
14541 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14542 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
14543 			if (sndlen > SCTP_SB_LIMIT_SND(so))
14544 				error = EMSGSIZE;
14545 			else
14546 				error = EWOULDBLOCK;
14547 			goto out_unlocked;
14548 		}
14549 		stcb->asoc.sb_send_resv += sndlen;
14550 		SCTP_TCB_UNLOCK(stcb);
14551 		hold_tcblock = 0;
14552 	} else {
14553 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
14554 	}
14555 	local_soresv = sndlen;
14556 	/* Keep the stcb from being freed under our feet */
14557 	if (free_cnt_applied) {
14558 #ifdef INVARIANTS
14559 		panic("refcnt already incremented");
14560 #else
14561 		printf("refcnt:1 already incremented?\n");
14562 #endif
14563 	} else {
14564 		atomic_add_int(&stcb->asoc.refcnt, 1);
14565 		free_cnt_applied = 1;
14566 	}
14567 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14568 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14569 		error = ECONNRESET;
14570 		goto out_unlocked;
14571 	}
14572 	if (create_lock_applied) {
14573 		SCTP_ASOC_CREATE_UNLOCK(inp);
14574 		create_lock_applied = 0;
14575 	}
14576 	if (asoc->stream_reset_outstanding) {
14577 		/*
14578 		 * Can't queue any data while stream reset is underway.
14579 		 */
14580 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
14581 		error = EAGAIN;
14582 		goto out_unlocked;
14583 	}
14584 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
14585 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
14586 		queue_only = 1;
14587 	}
14588 	if ((use_rcvinfo == 0) || (srcv == NULL)) {
14589 		/* Grab the default stuff from the asoc */
14590 		srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
14591 	}
14592 	/* we are now done with all control */
14593 	if (control) {
14594 		sctp_m_freem(control);
14595 		control = NULL;
14596 	}
14597 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
14598 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
14599 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
14600 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
14601 		if ((use_rcvinfo) &&
14602 		    (srcv->sinfo_flags & SCTP_ABORT)) {
14603 			;
14604 		} else {
14605 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14606 			error = ECONNRESET;
14607 			goto out_unlocked;
14608 		}
14609 	}
14610 	/* Ok, we will attempt a msgsnd :> */
14611 	if (p) {
14612 		p->td_ru.ru_msgsnd++;
14613 	}
14614 	if (stcb) {
14615 		if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
14616 			net = stcb->asoc.primary_destination;
14617 		}
14618 	}
14619 	if (net == NULL) {
14620 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14621 		error = EINVAL;
14622 		goto out_unlocked;
14623 	}
14624 	if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
14625 		/*-
14626 		 * CMT: Added check for CMT above. net above is the primary
14627 		 * dest. If CMT is ON, sender should always attempt to send
14628 		 * with the output routine sctp_fill_outqueue() that loops
14629 		 * through all destination addresses. Therefore, if CMT is
14630 		 * ON, queue_only is NOT set to 1 here, so that
14631 		 * sctp_chunk_output() can be called below.
14632 		 */
14633 		queue_only = 1;
14634 	} else if (asoc->ifp_had_enobuf) {
14635 		SCTP_STAT_INCR(sctps_ifnomemqueued);
14636 		if (net->flight_size > (net->mtu * 2))
14637 			queue_only = 1;
14638 		asoc->ifp_had_enobuf = 0;
14639 	} else {
14640 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
14641 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
14642 	}
14643 	/* Are we aborting? */
14644 	if (srcv->sinfo_flags & SCTP_ABORT) {
14645 		struct mbuf *mm;
14646 		int tot_demand, tot_out = 0, max_out;
14647 
14648 		SCTP_STAT_INCR(sctps_sends_with_abort);
14649 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
14650 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
14651 			/* It has to be up before we abort */
14652 			/* how big is the user initiated abort? */
14653 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14654 			error = EINVAL;
14655 			goto out;
14656 		}
14657 		if (hold_tcblock) {
14658 			SCTP_TCB_UNLOCK(stcb);
14659 			hold_tcblock = 0;
14660 		}
14661 		if (top) {
14662 			struct mbuf *cntm = NULL;
14663 
14664 			mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
14665 			if (sndlen != 0) {
14666 				cntm = top;
14667 				while (cntm) {
14668 					tot_out += SCTP_BUF_LEN(cntm);
14669 					cntm = SCTP_BUF_NEXT(cntm);
14670 				}
14671 			}
14672 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
14673 		} else {
14674 			/* Must fit in a MTU */
14675 			tot_out = sndlen;
14676 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
14677 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
14678 				/* To big */
14679 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14680 				error = EMSGSIZE;
14681 				goto out;
14682 			}
14683 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
14684 		}
14685 		if (mm == NULL) {
14686 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
14687 			error = ENOMEM;
14688 			goto out;
14689 		}
14690 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
14691 		max_out -= sizeof(struct sctp_abort_msg);
14692 		if (tot_out > max_out) {
14693 			tot_out = max_out;
14694 		}
14695 		if (mm) {
14696 			struct sctp_paramhdr *ph;
14697 
14698 			/* now move forward the data pointer */
14699 			ph = mtod(mm, struct sctp_paramhdr *);
14700 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
14701 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
14702 			ph++;
14703 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
14704 			if (top == NULL) {
14705 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
14706 				if (error) {
14707 					/*-
14708 					 * Here if we can't get his data we
14709 					 * still abort we just don't get to
14710 					 * send the users note :-0
14711 					 */
14712 					sctp_m_freem(mm);
14713 					mm = NULL;
14714 				}
14715 			} else {
14716 				if (sndlen != 0) {
14717 					SCTP_BUF_NEXT(mm) = top;
14718 				}
14719 			}
14720 		}
14721 		if (hold_tcblock == 0) {
14722 			SCTP_TCB_LOCK(stcb);
14723 			hold_tcblock = 1;
14724 		}
14725 		atomic_add_int(&stcb->asoc.refcnt, -1);
14726 		free_cnt_applied = 0;
14727 		/* release this lock, otherwise we hang on ourselves */
14728 		sctp_abort_an_association(stcb->sctp_ep, stcb,
14729 		    SCTP_RESPONSE_TO_USER_REQ,
14730 		    mm, SCTP_SO_LOCKED);
14731 		/* now relock the stcb so everything is sane */
14732 		hold_tcblock = 0;
14733 		stcb = NULL;
14734 		/*
14735 		 * In this case top is already chained to mm avoid double
14736 		 * free, since we free it below if top != NULL and driver
14737 		 * would free it after sending the packet out
14738 		 */
14739 		if (sndlen != 0) {
14740 			top = NULL;
14741 		}
14742 		goto out_unlocked;
14743 	}
14744 	/* Calculate the maximum we can send */
14745 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14746 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14747 		if (non_blocking) {
14748 			/* we already checked for non-blocking above. */
14749 			max_len = sndlen;
14750 		} else {
14751 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14752 		}
14753 	} else {
14754 		max_len = 0;
14755 	}
14756 	if (hold_tcblock) {
14757 		SCTP_TCB_UNLOCK(stcb);
14758 		hold_tcblock = 0;
14759 	}
14760 	/* Is the stream no. valid? */
14761 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
14762 		/* Invalid stream number */
14763 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14764 		error = EINVAL;
14765 		goto out_unlocked;
14766 	}
14767 	if (asoc->strmout == NULL) {
14768 		/* huh? software error */
14769 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14770 		error = EFAULT;
14771 		goto out_unlocked;
14772 	}
14773 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
14774 	if ((user_marks_eor == 0) &&
14775 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
14776 		/* It will NEVER fit */
14777 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14778 		error = EMSGSIZE;
14779 		goto out_unlocked;
14780 	}
14781 	if ((uio == NULL) && user_marks_eor) {
14782 		/*-
14783 		 * We do not support eeor mode for
14784 		 * sending with mbuf chains (like sendfile).
14785 		 */
14786 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14787 		error = EINVAL;
14788 		goto out_unlocked;
14789 	}
14790 	if (user_marks_eor) {
14791 		local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
14792 	} else {
14793 		/*-
14794 		 * For non-eeor the whole message must fit in
14795 		 * the socket send buffer.
14796 		 */
14797 		local_add_more = sndlen;
14798 	}
14799 	len = 0;
14800 	if (non_blocking) {
14801 		goto skip_preblock;
14802 	}
14803 	if (((max_len <= local_add_more) &&
14804 	    (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
14805 	    (max_len == 0) ||
14806 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14807 		/* No room right now ! */
14808 		SOCKBUF_LOCK(&so->so_snd);
14809 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14810 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
14811 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14812 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
14813 			    (unsigned int)SCTP_SB_LIMIT_SND(so),
14814 			    inqueue_bytes,
14815 			    local_add_more,
14816 			    stcb->asoc.stream_queue_cnt,
14817 			    stcb->asoc.chunks_on_out_queue,
14818 			    SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
14819 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14820 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
14821 			}
14822 			be.error = 0;
14823 			stcb->block_entry = &be;
14824 			error = sbwait(&so->so_snd);
14825 			stcb->block_entry = NULL;
14826 			if (error || so->so_error || be.error) {
14827 				if (error == 0) {
14828 					if (so->so_error)
14829 						error = so->so_error;
14830 					if (be.error) {
14831 						error = be.error;
14832 					}
14833 				}
14834 				SOCKBUF_UNLOCK(&so->so_snd);
14835 				goto out_unlocked;
14836 			}
14837 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14838 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
14839 				    so, asoc, stcb->asoc.total_output_queue_size);
14840 			}
14841 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14842 				goto out_unlocked;
14843 			}
14844 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14845 		}
14846 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14847 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14848 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14849 		} else {
14850 			max_len = 0;
14851 		}
14852 		SOCKBUF_UNLOCK(&so->so_snd);
14853 	}
14854 skip_preblock:
14855 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14856 		goto out_unlocked;
14857 	}
14858 	/*
14859 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
14860 	 * case NOTE: uio will be null when top/mbuf is passed
14861 	 */
14862 	if (sndlen == 0) {
14863 		if (srcv->sinfo_flags & SCTP_EOF) {
14864 			got_all_of_the_send = 1;
14865 			goto dataless_eof;
14866 		} else {
14867 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14868 			error = EINVAL;
14869 			goto out;
14870 		}
14871 	}
14872 	if (top == NULL) {
14873 		struct sctp_stream_queue_pending *sp;
14874 		struct sctp_stream_out *strm;
14875 		uint32_t sndout, initial_out;
14876 
14877 		initial_out = uio->uio_resid;
14878 
14879 		SCTP_TCB_SEND_LOCK(stcb);
14880 		if ((asoc->stream_locked) &&
14881 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
14882 			SCTP_TCB_SEND_UNLOCK(stcb);
14883 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14884 			error = EINVAL;
14885 			goto out;
14886 		}
14887 		SCTP_TCB_SEND_UNLOCK(stcb);
14888 
14889 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
14890 		if (strm->last_msg_incomplete == 0) {
14891 	do_a_copy_in:
14892 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
14893 			if ((sp == NULL) || (error)) {
14894 				goto out;
14895 			}
14896 			SCTP_TCB_SEND_LOCK(stcb);
14897 			if (sp->msg_is_complete) {
14898 				strm->last_msg_incomplete = 0;
14899 				asoc->stream_locked = 0;
14900 			} else {
14901 				/*
14902 				 * Just got locked to this guy in case of an
14903 				 * interrupt.
14904 				 */
14905 				strm->last_msg_incomplete = 1;
14906 				asoc->stream_locked = 1;
14907 				asoc->stream_locked_on = srcv->sinfo_stream;
14908 				sp->sender_all_done = 0;
14909 			}
14910 			sctp_snd_sb_alloc(stcb, sp->length);
14911 			atomic_add_int(&asoc->stream_queue_cnt, 1);
14912 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
14913 				sp->strseq = strm->next_sequence_sent;
14914 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
14915 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
14916 					    (uintptr_t) stcb, sp->length,
14917 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
14918 				}
14919 				strm->next_sequence_sent++;
14920 			} else {
14921 				SCTP_STAT_INCR(sctps_sends_with_unord);
14922 			}
14923 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
14924 			if ((strm->next_spoke.tqe_next == NULL) &&
14925 			    (strm->next_spoke.tqe_prev == NULL)) {
14926 				/* Not on wheel, insert */
14927 				sctp_insert_on_wheel(stcb, asoc, strm, 1);
14928 			}
14929 			SCTP_TCB_SEND_UNLOCK(stcb);
14930 		} else {
14931 			SCTP_TCB_SEND_LOCK(stcb);
14932 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
14933 			SCTP_TCB_SEND_UNLOCK(stcb);
14934 			if (sp == NULL) {
14935 				/* ???? Huh ??? last msg is gone */
14936 #ifdef INVARIANTS
14937 				panic("Warning: Last msg marked incomplete, yet nothing left?");
14938 #else
14939 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
14940 				strm->last_msg_incomplete = 0;
14941 #endif
14942 				goto do_a_copy_in;
14943 
14944 			}
14945 		}
14946 		while (uio->uio_resid > 0) {
14947 			/* How much room do we have? */
14948 			struct mbuf *new_tail, *mm;
14949 
14950 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
14951 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
14952 			else
14953 				max_len = 0;
14954 
14955 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
14956 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
14957 			    (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
14958 				sndout = 0;
14959 				new_tail = NULL;
14960 				if (hold_tcblock) {
14961 					SCTP_TCB_UNLOCK(stcb);
14962 					hold_tcblock = 0;
14963 				}
14964 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
14965 				if ((mm == NULL) || error) {
14966 					if (mm) {
14967 						sctp_m_freem(mm);
14968 					}
14969 					goto out;
14970 				}
14971 				/* Update the mbuf and count */
14972 				SCTP_TCB_SEND_LOCK(stcb);
14973 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14974 					/*
14975 					 * we need to get out. Peer probably
14976 					 * aborted.
14977 					 */
14978 					sctp_m_freem(mm);
14979 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
14980 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14981 						error = ECONNRESET;
14982 					}
14983 					SCTP_TCB_SEND_UNLOCK(stcb);
14984 					goto out;
14985 				}
14986 				if (sp->tail_mbuf) {
14987 					/* tack it to the end */
14988 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
14989 					sp->tail_mbuf = new_tail;
14990 				} else {
14991 					/* A stolen mbuf */
14992 					sp->data = mm;
14993 					sp->tail_mbuf = new_tail;
14994 				}
14995 				sctp_snd_sb_alloc(stcb, sndout);
14996 				atomic_add_int(&sp->length, sndout);
14997 				len += sndout;
14998 
14999 				/* Did we reach EOR? */
15000 				if ((uio->uio_resid == 0) &&
15001 				    ((user_marks_eor == 0) ||
15002 				    (srcv->sinfo_flags & SCTP_EOF) ||
15003 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
15004 					sp->msg_is_complete = 1;
15005 				} else {
15006 					sp->msg_is_complete = 0;
15007 				}
15008 				SCTP_TCB_SEND_UNLOCK(stcb);
15009 			}
15010 			if (uio->uio_resid == 0) {
15011 				/* got it all? */
15012 				continue;
15013 			}
15014 			/* PR-SCTP? */
15015 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
15016 				/*
15017 				 * This is ugly but we must assure locking
15018 				 * order
15019 				 */
15020 				if (hold_tcblock == 0) {
15021 					SCTP_TCB_LOCK(stcb);
15022 					hold_tcblock = 1;
15023 				}
15024 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
15025 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
15026 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
15027 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
15028 				else
15029 					max_len = 0;
15030 				if (max_len > 0) {
15031 					continue;
15032 				}
15033 				SCTP_TCB_UNLOCK(stcb);
15034 				hold_tcblock = 0;
15035 			}
15036 			/* wait for space now */
15037 			if (non_blocking) {
15038 				/* Non-blocking io in place out */
15039 				goto skip_out_eof;
15040 			}
15041 			if ((net->flight_size > net->cwnd) &&
15042 			    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
15043 				queue_only = 1;
15044 			} else if (asoc->ifp_had_enobuf) {
15045 				SCTP_STAT_INCR(sctps_ifnomemqueued);
15046 				if (net->flight_size > (net->mtu * 2)) {
15047 					queue_only = 1;
15048 				} else {
15049 					queue_only = 0;
15050 				}
15051 				asoc->ifp_had_enobuf = 0;
15052 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15053 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15054 			} else {
15055 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15056 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15057 				if (net->flight_size > net->cwnd) {
15058 					queue_only = 1;
15059 					SCTP_STAT_INCR(sctps_send_cwnd_avoid);
15060 				} else {
15061 					queue_only = 0;
15062 				}
15063 			}
15064 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
15065 			    (stcb->asoc.total_flight > 0) &&
15066 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
15067 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
15068 
15069 				/*-
15070 				 * Ok, Nagle is set on and we have data outstanding.
15071 				 * Don't send anything and let SACKs drive out the
15072 				 * data unless wen have a "full" segment to send.
15073 				 */
15074 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15075 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
15076 				}
15077 				SCTP_STAT_INCR(sctps_naglequeued);
15078 				nagle_applies = 1;
15079 			} else {
15080 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15081 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
15082 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
15083 				}
15084 				SCTP_STAT_INCR(sctps_naglesent);
15085 				nagle_applies = 0;
15086 			}
15087 			/* What about the INIT, send it maybe */
15088 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15089 
15090 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
15091 				    nagle_applies, un_sent);
15092 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
15093 				    stcb->asoc.total_flight,
15094 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
15095 			}
15096 			if (queue_only_for_init) {
15097 				if (hold_tcblock == 0) {
15098 					SCTP_TCB_LOCK(stcb);
15099 					hold_tcblock = 1;
15100 				}
15101 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
15102 					/* a collision took us forward? */
15103 					queue_only_for_init = 0;
15104 					queue_only = 0;
15105 				} else {
15106 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
15107 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
15108 					queue_only_for_init = 0;
15109 					queue_only = 1;
15110 				}
15111 			}
15112 			if ((queue_only == 0) && (nagle_applies == 0)) {
15113 				/*-
15114 				 * need to start chunk output
15115 				 * before blocking.. note that if
15116 				 * a lock is already applied, then
15117 				 * the input via the net is happening
15118 				 * and I don't need to start output :-D
15119 				 */
15120 				if (hold_tcblock == 0) {
15121 					if (SCTP_TCB_TRYLOCK(stcb)) {
15122 						hold_tcblock = 1;
15123 						sctp_chunk_output(inp,
15124 						    stcb,
15125 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15126 					}
15127 				} else {
15128 					sctp_chunk_output(inp,
15129 					    stcb,
15130 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15131 				}
15132 				if (hold_tcblock == 1) {
15133 					SCTP_TCB_UNLOCK(stcb);
15134 					hold_tcblock = 0;
15135 				}
15136 			}
15137 			SOCKBUF_LOCK(&so->so_snd);
15138 			/*-
15139 			 * This is a bit strange, but I think it will
15140 			 * work. The total_output_queue_size is locked and
15141 			 * protected by the TCB_LOCK, which we just released.
15142 			 * There is a race that can occur between releasing it
15143 			 * above, and me getting the socket lock, where sacks
15144 			 * come in but we have not put the SB_WAIT on the
15145 			 * so_snd buffer to get the wakeup. After the LOCK
15146 			 * is applied the sack_processing will also need to
15147 			 * LOCK the so->so_snd to do the actual sowwakeup(). So
15148 			 * once we have the socket buffer lock if we recheck the
15149 			 * size we KNOW we will get to sleep safely with the
15150 			 * wakeup flag in place.
15151 			 */
15152 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
15153 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
15154 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15155 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
15156 					    so, asoc, uio->uio_resid);
15157 				}
15158 				be.error = 0;
15159 				stcb->block_entry = &be;
15160 				error = sbwait(&so->so_snd);
15161 				stcb->block_entry = NULL;
15162 
15163 				if (error || so->so_error || be.error) {
15164 					if (error == 0) {
15165 						if (so->so_error)
15166 							error = so->so_error;
15167 						if (be.error) {
15168 							error = be.error;
15169 						}
15170 					}
15171 					SOCKBUF_UNLOCK(&so->so_snd);
15172 					goto out_unlocked;
15173 				}
15174 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15175 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
15176 					    so, asoc, stcb->asoc.total_output_queue_size);
15177 				}
15178 			}
15179 			SOCKBUF_UNLOCK(&so->so_snd);
15180 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
15181 				goto out_unlocked;
15182 			}
15183 		}
15184 		SCTP_TCB_SEND_LOCK(stcb);
15185 		if (sp) {
15186 			if (sp->msg_is_complete == 0) {
15187 				strm->last_msg_incomplete = 1;
15188 				asoc->stream_locked = 1;
15189 				asoc->stream_locked_on = srcv->sinfo_stream;
15190 			} else {
15191 				sp->sender_all_done = 1;
15192 				strm->last_msg_incomplete = 0;
15193 				asoc->stream_locked = 0;
15194 			}
15195 		} else {
15196 			SCTP_PRINTF("Huh no sp TSNH?\n");
15197 			strm->last_msg_incomplete = 0;
15198 			asoc->stream_locked = 0;
15199 		}
15200 		SCTP_TCB_SEND_UNLOCK(stcb);
15201 		if (uio->uio_resid == 0) {
15202 			got_all_of_the_send = 1;
15203 		}
15204 	} else if (top) {
15205 		/* We send in a 0, since we do NOT have any locks */
15206 		error = sctp_msg_append(stcb, net, top, srcv, 0);
15207 		top = NULL;
15208 		if (srcv->sinfo_flags & SCTP_EOF) {
15209 			/*
15210 			 * This should only happen for Panda for the mbuf
15211 			 * send case, which does NOT yet support EEOR mode.
15212 			 * Thus, we can just set this flag to do the proper
15213 			 * EOF handling.
15214 			 */
15215 			got_all_of_the_send = 1;
15216 		}
15217 	}
15218 	if (error) {
15219 		goto out;
15220 	}
15221 dataless_eof:
15222 	/* EOF thing ? */
15223 	if ((srcv->sinfo_flags & SCTP_EOF) &&
15224 	    (got_all_of_the_send == 1) &&
15225 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
15226 		int cnt;
15227 
15228 		SCTP_STAT_INCR(sctps_sends_with_eof);
15229 		error = 0;
15230 		if (hold_tcblock == 0) {
15231 			SCTP_TCB_LOCK(stcb);
15232 			hold_tcblock = 1;
15233 		}
15234 		cnt = sctp_is_there_unsent_data(stcb);
15235 		if (TAILQ_EMPTY(&asoc->send_queue) &&
15236 		    TAILQ_EMPTY(&asoc->sent_queue) &&
15237 		    (cnt == 0)) {
15238 			if (asoc->locked_on_sending) {
15239 				goto abort_anyway;
15240 			}
15241 			/* there is nothing queued to send, so I'm done... */
15242 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
15243 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
15244 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
15245 				/* only send SHUTDOWN the first time through */
15246 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
15247 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
15248 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
15249 				}
15250 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
15251 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
15252 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
15253 				    asoc->primary_destination);
15254 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
15255 				    asoc->primary_destination);
15256 			}
15257 		} else {
15258 			/*-
15259 			 * we still got (or just got) data to send, so set
15260 			 * SHUTDOWN_PENDING
15261 			 */
15262 			/*-
15263 			 * XXX sockets draft says that SCTP_EOF should be
15264 			 * sent with no data.  currently, we will allow user
15265 			 * data to be sent first and move to
15266 			 * SHUTDOWN-PENDING
15267 			 */
15268 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
15269 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
15270 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
15271 				if (hold_tcblock == 0) {
15272 					SCTP_TCB_LOCK(stcb);
15273 					hold_tcblock = 1;
15274 				}
15275 				if (asoc->locked_on_sending) {
15276 					/* Locked to send out the data */
15277 					struct sctp_stream_queue_pending *sp;
15278 
15279 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
15280 					if (sp) {
15281 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
15282 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
15283 					}
15284 				}
15285 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
15286 				if (TAILQ_EMPTY(&asoc->send_queue) &&
15287 				    TAILQ_EMPTY(&asoc->sent_queue) &&
15288 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
15289 			abort_anyway:
15290 					if (free_cnt_applied) {
15291 						atomic_add_int(&stcb->asoc.refcnt, -1);
15292 						free_cnt_applied = 0;
15293 					}
15294 					sctp_abort_an_association(stcb->sctp_ep, stcb,
15295 					    SCTP_RESPONSE_TO_USER_REQ,
15296 					    NULL, SCTP_SO_LOCKED);
15297 					/*
15298 					 * now relock the stcb so everything
15299 					 * is sane
15300 					 */
15301 					hold_tcblock = 0;
15302 					stcb = NULL;
15303 					goto out;
15304 				}
15305 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
15306 				    asoc->primary_destination);
15307 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
15308 			}
15309 		}
15310 	}
15311 skip_out_eof:
15312 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
15313 		some_on_control = 1;
15314 	}
15315 	if ((net->flight_size > net->cwnd) &&
15316 	    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
15317 		queue_only = 1;
15318 	} else if (asoc->ifp_had_enobuf) {
15319 		SCTP_STAT_INCR(sctps_ifnomemqueued);
15320 		if (net->flight_size > (net->mtu * 2)) {
15321 			queue_only = 1;
15322 		} else {
15323 			queue_only = 0;
15324 		}
15325 		asoc->ifp_had_enobuf = 0;
15326 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15327 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15328 	} else {
15329 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15330 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15331 		if (net->flight_size > net->cwnd) {
15332 			queue_only = 1;
15333 			SCTP_STAT_INCR(sctps_send_cwnd_avoid);
15334 		} else {
15335 			queue_only = 0;
15336 		}
15337 	}
15338 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
15339 	    (stcb->asoc.total_flight > 0) &&
15340 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
15341 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
15342 		/*-
15343 		 * Ok, Nagle is set on and we have data outstanding.
15344 		 * Don't send anything and let SACKs drive out the
15345 		 * data unless wen have a "full" segment to send.
15346 		 */
15347 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15348 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
15349 		}
15350 		SCTP_STAT_INCR(sctps_naglequeued);
15351 		nagle_applies = 1;
15352 	} else {
15353 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15354 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
15355 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
15356 		}
15357 		SCTP_STAT_INCR(sctps_naglesent);
15358 		nagle_applies = 0;
15359 	}
15360 	if (queue_only_for_init) {
15361 		if (hold_tcblock == 0) {
15362 			SCTP_TCB_LOCK(stcb);
15363 			hold_tcblock = 1;
15364 		}
15365 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
15366 			/* a collision took us forward? */
15367 			queue_only_for_init = 0;
15368 			queue_only = 0;
15369 		} else {
15370 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
15371 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
15372 			queue_only_for_init = 0;
15373 			queue_only = 1;
15374 		}
15375 	}
15376 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
15377 		/* we can attempt to send too. */
15378 		if (hold_tcblock == 0) {
15379 			/*
15380 			 * If there is activity recv'ing sacks no need to
15381 			 * send
15382 			 */
15383 			if (SCTP_TCB_TRYLOCK(stcb)) {
15384 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15385 				hold_tcblock = 1;
15386 			}
15387 		} else {
15388 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15389 		}
15390 	} else if ((queue_only == 0) &&
15391 		    (stcb->asoc.peers_rwnd == 0) &&
15392 	    (stcb->asoc.total_flight == 0)) {
15393 		/* We get to have a probe outstanding */
15394 		if (hold_tcblock == 0) {
15395 			hold_tcblock = 1;
15396 			SCTP_TCB_LOCK(stcb);
15397 		}
15398 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15399 	} else if (some_on_control) {
15400 		int num_out, reason, cwnd_full, frag_point;
15401 
15402 		/* Here we do control only */
15403 		if (hold_tcblock == 0) {
15404 			hold_tcblock = 1;
15405 			SCTP_TCB_LOCK(stcb);
15406 		}
15407 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
15408 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
15409 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
15410 	}
15411 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
15412 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
15413 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
15414 	    stcb->asoc.total_output_queue_size, error);
15415 
15416 out:
15417 out_unlocked:
15418 
15419 	if (local_soresv && stcb) {
15420 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
15421 		local_soresv = 0;
15422 	}
15423 	if (create_lock_applied) {
15424 		SCTP_ASOC_CREATE_UNLOCK(inp);
15425 		create_lock_applied = 0;
15426 	}
15427 	if ((stcb) && hold_tcblock) {
15428 		SCTP_TCB_UNLOCK(stcb);
15429 	}
15430 	if (stcb && free_cnt_applied) {
15431 		atomic_add_int(&stcb->asoc.refcnt, -1);
15432 	}
15433 #ifdef INVARIANTS
15434 	if (stcb) {
15435 		if (mtx_owned(&stcb->tcb_mtx)) {
15436 			panic("Leaving with tcb mtx owned?");
15437 		}
15438 		if (mtx_owned(&stcb->tcb_send_mtx)) {
15439 			panic("Leaving with tcb send mtx owned?");
15440 		}
15441 	}
15442 #endif
15443 	if (top) {
15444 		sctp_m_freem(top);
15445 	}
15446 	if (control) {
15447 		sctp_m_freem(control);
15448 	}
15449 	return (error);
15450 }
15451 
15452 
15453 /*
15454  * generate an AUTHentication chunk, if required
15455  */
15456 struct mbuf *
15457 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
15458     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
15459     struct sctp_tcb *stcb, uint8_t chunk)
15460 {
15461 	struct mbuf *m_auth;
15462 	struct sctp_auth_chunk *auth;
15463 	int chunk_len;
15464 
15465 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
15466 	    (stcb == NULL))
15467 		return (m);
15468 
15469 	/* sysctl disabled auth? */
15470 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
15471 		return (m);
15472 
15473 	/* peer doesn't do auth... */
15474 	if (!stcb->asoc.peer_supports_auth) {
15475 		return (m);
15476 	}
15477 	/* does the requested chunk require auth? */
15478 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
15479 		return (m);
15480 	}
15481 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
15482 	if (m_auth == NULL) {
15483 		/* no mbuf's */
15484 		return (m);
15485 	}
15486 	/* reserve some space if this will be the first mbuf */
15487 	if (m == NULL)
15488 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
15489 	/* fill in the AUTH chunk details */
15490 	auth = mtod(m_auth, struct sctp_auth_chunk *);
15491 	bzero(auth, sizeof(*auth));
15492 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
15493 	auth->ch.chunk_flags = 0;
15494 	chunk_len = sizeof(*auth) +
15495 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
15496 	auth->ch.chunk_length = htons(chunk_len);
15497 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
15498 	/* key id and hmac digest will be computed and filled in upon send */
15499 
15500 	/* save the offset where the auth was inserted into the chain */
15501 	if (m != NULL) {
15502 		struct mbuf *cn;
15503 
15504 		*offset = 0;
15505 		cn = m;
15506 		while (cn) {
15507 			*offset += SCTP_BUF_LEN(cn);
15508 			cn = SCTP_BUF_NEXT(cn);
15509 		}
15510 	} else
15511 		*offset = 0;
15512 
15513 	/* update length and return pointer to the auth chunk */
15514 	SCTP_BUF_LEN(m_auth) = chunk_len;
15515 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
15516 	if (auth_ret != NULL)
15517 		*auth_ret = auth;
15518 
15519 	return (m);
15520 }
15521 
15522 #ifdef INET6
15523 int
15524 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
15525 {
15526 	struct nd_prefix *pfx = NULL;
15527 	struct nd_pfxrouter *pfxrtr = NULL;
15528 	struct sockaddr_in6 gw6;
15529 
15530 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
15531 		return (0);
15532 
15533 	/* get prefix entry of address */
15534 	LIST_FOREACH(pfx, &MODULE_GLOBAL(MOD_INET6, nd_prefix), ndpr_entry) {
15535 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
15536 			continue;
15537 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
15538 		    &src6->sin6_addr, &pfx->ndpr_mask))
15539 			break;
15540 	}
15541 	/* no prefix entry in the prefix list */
15542 	if (pfx == NULL) {
15543 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
15544 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
15545 		return (0);
15546 	}
15547 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
15548 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
15549 
15550 	/* search installed gateway from prefix entry */
15551 	for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
15552 	    pfxrtr->pfr_next) {
15553 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
15554 		gw6.sin6_family = AF_INET6;
15555 		gw6.sin6_len = sizeof(struct sockaddr_in6);
15556 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
15557 		    sizeof(struct in6_addr));
15558 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
15559 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
15560 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
15561 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
15562 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
15563 		    ro->ro_rt->rt_gateway)) {
15564 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
15565 			return (1);
15566 		}
15567 	}
15568 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
15569 	return (0);
15570 }
15571 
15572 #endif
15573 
15574 int
15575 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
15576 {
15577 	struct sockaddr_in *sin, *mask;
15578 	struct ifaddr *ifa;
15579 	struct in_addr srcnetaddr, gwnetaddr;
15580 
15581 	if (ro == NULL || ro->ro_rt == NULL ||
15582 	    sifa->address.sa.sa_family != AF_INET) {
15583 		return (0);
15584 	}
15585 	ifa = (struct ifaddr *)sifa->ifa;
15586 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
15587 	sin = (struct sockaddr_in *)&sifa->address.sin;
15588 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
15589 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
15590 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
15591 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
15592 
15593 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
15594 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
15595 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
15596 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
15597 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
15598 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
15599 		return (1);
15600 	}
15601 	return (0);
15602 }
15603