xref: /freebsd/sys/netinet/sctp_output.c (revision 0ca4746bcb3706a2addff90cd71ca9c0b22ad8ff)
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 void
7449 sctp_remove_from_wheel(struct sctp_tcb *stcb,
7450     struct sctp_association *asoc,
7451     struct sctp_stream_out *strq,
7452     int holds_lock)
7453 {
7454 	/* take off and then setup so we know it is not on the wheel */
7455 	if (holds_lock == 0)
7456 		SCTP_TCB_SEND_LOCK(stcb);
7457 	if (TAILQ_FIRST(&strq->outqueue)) {
7458 		/* more was added */
7459 		if (holds_lock == 0)
7460 			SCTP_TCB_SEND_UNLOCK(stcb);
7461 		return;
7462 	}
7463 	TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
7464 	strq->next_spoke.tqe_next = NULL;
7465 	strq->next_spoke.tqe_prev = NULL;
7466 	if (holds_lock == 0)
7467 		SCTP_TCB_SEND_UNLOCK(stcb);
7468 }
7469 
7470 static void
7471 sctp_prune_prsctp(struct sctp_tcb *stcb,
7472     struct sctp_association *asoc,
7473     struct sctp_sndrcvinfo *srcv,
7474     int dataout)
7475 {
7476 	int freed_spc = 0;
7477 	struct sctp_tmit_chunk *chk, *nchk;
7478 
7479 	SCTP_TCB_LOCK_ASSERT(stcb);
7480 	if ((asoc->peer_supports_prsctp) &&
7481 	    (asoc->sent_queue_cnt_removeable > 0)) {
7482 		TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
7483 			/*
7484 			 * Look for chunks marked with the PR_SCTP flag AND
7485 			 * the buffer space flag. If the one being sent is
7486 			 * equal or greater priority then purge the old one
7487 			 * and free some space.
7488 			 */
7489 			if (PR_SCTP_BUF_ENABLED(chk->flags)) {
7490 				/*
7491 				 * This one is PR-SCTP AND buffer space
7492 				 * limited type
7493 				 */
7494 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
7495 					/*
7496 					 * Lower numbers equates to higher
7497 					 * priority so if the one we are
7498 					 * looking at has a larger or equal
7499 					 * priority we want to drop the data
7500 					 * and NOT retransmit it.
7501 					 */
7502 					if (chk->data) {
7503 						/*
7504 						 * We release the book_size
7505 						 * if the mbuf is here
7506 						 */
7507 						int ret_spc;
7508 						int cause;
7509 
7510 						if (chk->sent > SCTP_DATAGRAM_UNSENT)
7511 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
7512 						else
7513 							cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
7514 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
7515 						    cause,
7516 						    &asoc->sent_queue, SCTP_SO_LOCKED);
7517 						freed_spc += ret_spc;
7518 						if (freed_spc >= dataout) {
7519 							return;
7520 						}
7521 					}	/* if chunk was present */
7522 				}	/* if of sufficent priority */
7523 			}	/* if chunk has enabled */
7524 		}		/* tailqforeach */
7525 
7526 		chk = TAILQ_FIRST(&asoc->send_queue);
7527 		while (chk) {
7528 			nchk = TAILQ_NEXT(chk, sctp_next);
7529 			/* Here we must move to the sent queue and mark */
7530 			if (PR_SCTP_TTL_ENABLED(chk->flags)) {
7531 				if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
7532 					if (chk->data) {
7533 						/*
7534 						 * We release the book_size
7535 						 * if the mbuf is here
7536 						 */
7537 						int ret_spc;
7538 
7539 						ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
7540 						    SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
7541 						    &asoc->send_queue, SCTP_SO_LOCKED);
7542 
7543 						freed_spc += ret_spc;
7544 						if (freed_spc >= dataout) {
7545 							return;
7546 						}
7547 					}	/* end if chk->data */
7548 				}	/* end if right class */
7549 			}	/* end if chk pr-sctp */
7550 			chk = nchk;
7551 		}		/* end while (chk) */
7552 	}			/* if enabled in asoc */
7553 }
7554 
7555 int
7556 sctp_get_frag_point(struct sctp_tcb *stcb,
7557     struct sctp_association *asoc)
7558 {
7559 	int siz, ovh;
7560 
7561 	/*
7562 	 * For endpoints that have both v6 and v4 addresses we must reserve
7563 	 * room for the ipv6 header, for those that are only dealing with V4
7564 	 * we use a larger frag point.
7565 	 */
7566 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7567 		ovh = SCTP_MED_OVERHEAD;
7568 	} else {
7569 		ovh = SCTP_MED_V4_OVERHEAD;
7570 	}
7571 
7572 	if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
7573 		siz = asoc->smallest_mtu - ovh;
7574 	else
7575 		siz = (stcb->asoc.sctp_frag_point - ovh);
7576 	/*
7577 	 * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
7578 	 */
7579 	/* A data chunk MUST fit in a cluster */
7580 	/* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
7581 	/* } */
7582 
7583 	/* adjust for an AUTH chunk if DATA requires auth */
7584 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
7585 		siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7586 
7587 	if (siz % 4) {
7588 		/* make it an even word boundary please */
7589 		siz -= (siz % 4);
7590 	}
7591 	return (siz);
7592 }
7593 
7594 static void
7595 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
7596     struct sctp_stream_queue_pending *sp)
7597 {
7598 	sp->pr_sctp_on = 0;
7599 	if (stcb->asoc.peer_supports_prsctp) {
7600 		/*
7601 		 * We assume that the user wants PR_SCTP_TTL if the user
7602 		 * provides a positive lifetime but does not specify any
7603 		 * PR_SCTP policy. This is a BAD assumption and causes
7604 		 * problems at least with the U-Vancovers MPI folks. I will
7605 		 * change this to be no policy means NO PR-SCTP.
7606 		 */
7607 		if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
7608 			sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
7609 			sp->pr_sctp_on = 1;
7610 		} else {
7611 			return;
7612 		}
7613 		switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
7614 		case CHUNK_FLAGS_PR_SCTP_BUF:
7615 			/*
7616 			 * Time to live is a priority stored in tv_sec when
7617 			 * doing the buffer drop thing.
7618 			 */
7619 			sp->ts.tv_sec = sp->timetolive;
7620 			sp->ts.tv_usec = 0;
7621 			break;
7622 		case CHUNK_FLAGS_PR_SCTP_TTL:
7623 			{
7624 				struct timeval tv;
7625 
7626 				(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
7627 				tv.tv_sec = sp->timetolive / 1000;
7628 				tv.tv_usec = (sp->timetolive * 1000) % 1000000;
7629 				/*
7630 				 * TODO sctp_constants.h needs alternative
7631 				 * time macros when _KERNEL is undefined.
7632 				 */
7633 				timevaladd(&sp->ts, &tv);
7634 			}
7635 			break;
7636 		case CHUNK_FLAGS_PR_SCTP_RTX:
7637 			/*
7638 			 * Time to live is a the number or retransmissions
7639 			 * stored in tv_sec.
7640 			 */
7641 			sp->ts.tv_sec = sp->timetolive;
7642 			sp->ts.tv_usec = 0;
7643 			break;
7644 		default:
7645 			SCTPDBG(SCTP_DEBUG_USRREQ1,
7646 			    "Unknown PR_SCTP policy %u.\n",
7647 			    PR_SCTP_POLICY(sp->sinfo_flags));
7648 			break;
7649 		}
7650 	}
7651 }
7652 
7653 static int
7654 sctp_msg_append(struct sctp_tcb *stcb,
7655     struct sctp_nets *net,
7656     struct mbuf *m,
7657     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
7658 {
7659 	int error = 0, holds_lock;
7660 	struct mbuf *at;
7661 	struct sctp_stream_queue_pending *sp = NULL;
7662 	struct sctp_stream_out *strm;
7663 
7664 	/*
7665 	 * Given an mbuf chain, put it into the association send queue and
7666 	 * place it on the wheel
7667 	 */
7668 	holds_lock = hold_stcb_lock;
7669 	if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
7670 		/* Invalid stream number */
7671 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
7672 		error = EINVAL;
7673 		goto out_now;
7674 	}
7675 	if ((stcb->asoc.stream_locked) &&
7676 	    (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
7677 		SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
7678 		error = EINVAL;
7679 		goto out_now;
7680 	}
7681 	strm = &stcb->asoc.strmout[srcv->sinfo_stream];
7682 	/* Now can we send this? */
7683 	if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
7684 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
7685 	    (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7686 	    (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
7687 		/* got data while shutting down */
7688 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
7689 		error = ECONNRESET;
7690 		goto out_now;
7691 	}
7692 	sctp_alloc_a_strmoq(stcb, sp);
7693 	if (sp == NULL) {
7694 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7695 		error = ENOMEM;
7696 		goto out_now;
7697 	}
7698 	sp->sinfo_flags = srcv->sinfo_flags;
7699 	sp->timetolive = srcv->sinfo_timetolive;
7700 	sp->ppid = srcv->sinfo_ppid;
7701 	sp->context = srcv->sinfo_context;
7702 	sp->strseq = 0;
7703 	if (sp->sinfo_flags & SCTP_ADDR_OVER) {
7704 		sp->net = net;
7705 		sp->addr_over = 1;
7706 	} else {
7707 		sp->net = stcb->asoc.primary_destination;
7708 		sp->addr_over = 0;
7709 	}
7710 	atomic_add_int(&sp->net->ref_count, 1);
7711 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
7712 	sp->stream = srcv->sinfo_stream;
7713 	sp->msg_is_complete = 1;
7714 	sp->sender_all_done = 1;
7715 	sp->some_taken = 0;
7716 	sp->data = m;
7717 	sp->tail_mbuf = NULL;
7718 	sp->length = 0;
7719 	at = m;
7720 	sctp_set_prsctp_policy(stcb, sp);
7721 	/*
7722 	 * We could in theory (for sendall) sifa the length in, but we would
7723 	 * still have to hunt through the chain since we need to setup the
7724 	 * tail_mbuf
7725 	 */
7726 	while (at) {
7727 		if (SCTP_BUF_NEXT(at) == NULL)
7728 			sp->tail_mbuf = at;
7729 		sp->length += SCTP_BUF_LEN(at);
7730 		at = SCTP_BUF_NEXT(at);
7731 	}
7732 	SCTP_TCB_SEND_LOCK(stcb);
7733 	sctp_snd_sb_alloc(stcb, sp->length);
7734 	atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
7735 	TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
7736 	if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
7737 		sp->strseq = strm->next_sequence_sent;
7738 		strm->next_sequence_sent++;
7739 	}
7740 	if ((strm->next_spoke.tqe_next == NULL) &&
7741 	    (strm->next_spoke.tqe_prev == NULL)) {
7742 		/* Not on wheel, insert */
7743 		sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
7744 	}
7745 	m = NULL;
7746 	SCTP_TCB_SEND_UNLOCK(stcb);
7747 out_now:
7748 	if (m) {
7749 		sctp_m_freem(m);
7750 	}
7751 	return (error);
7752 }
7753 
7754 
7755 static struct mbuf *
7756 sctp_copy_mbufchain(struct mbuf *clonechain,
7757     struct mbuf *outchain,
7758     struct mbuf **endofchain,
7759     int can_take_mbuf,
7760     int sizeofcpy,
7761     uint8_t copy_by_ref)
7762 {
7763 	struct mbuf *m;
7764 	struct mbuf *appendchain;
7765 	caddr_t cp;
7766 	int len;
7767 
7768 	if (endofchain == NULL) {
7769 		/* error */
7770 error_out:
7771 		if (outchain)
7772 			sctp_m_freem(outchain);
7773 		return (NULL);
7774 	}
7775 	if (can_take_mbuf) {
7776 		appendchain = clonechain;
7777 	} else {
7778 		if (!copy_by_ref &&
7779 		    (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
7780 		    ) {
7781 			/* Its not in a cluster */
7782 			if (*endofchain == NULL) {
7783 				/* lets get a mbuf cluster */
7784 				if (outchain == NULL) {
7785 					/* This is the general case */
7786 			new_mbuf:
7787 					outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
7788 					if (outchain == NULL) {
7789 						goto error_out;
7790 					}
7791 					SCTP_BUF_LEN(outchain) = 0;
7792 					*endofchain = outchain;
7793 					/* get the prepend space */
7794 					SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
7795 				} else {
7796 					/*
7797 					 * We really should not get a NULL
7798 					 * in endofchain
7799 					 */
7800 					/* find end */
7801 					m = outchain;
7802 					while (m) {
7803 						if (SCTP_BUF_NEXT(m) == NULL) {
7804 							*endofchain = m;
7805 							break;
7806 						}
7807 						m = SCTP_BUF_NEXT(m);
7808 					}
7809 					/* sanity */
7810 					if (*endofchain == NULL) {
7811 						/*
7812 						 * huh, TSNH XXX maybe we
7813 						 * should panic
7814 						 */
7815 						sctp_m_freem(outchain);
7816 						goto new_mbuf;
7817 					}
7818 				}
7819 				/* get the new end of length */
7820 				len = M_TRAILINGSPACE(*endofchain);
7821 			} else {
7822 				/* how much is left at the end? */
7823 				len = M_TRAILINGSPACE(*endofchain);
7824 			}
7825 			/* Find the end of the data, for appending */
7826 			cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
7827 
7828 			/* Now lets copy it out */
7829 			if (len >= sizeofcpy) {
7830 				/* It all fits, copy it in */
7831 				m_copydata(clonechain, 0, sizeofcpy, cp);
7832 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
7833 			} else {
7834 				/* fill up the end of the chain */
7835 				if (len > 0) {
7836 					m_copydata(clonechain, 0, len, cp);
7837 					SCTP_BUF_LEN((*endofchain)) += len;
7838 					/* now we need another one */
7839 					sizeofcpy -= len;
7840 				}
7841 				m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
7842 				if (m == NULL) {
7843 					/* We failed */
7844 					goto error_out;
7845 				}
7846 				SCTP_BUF_NEXT((*endofchain)) = m;
7847 				*endofchain = m;
7848 				cp = mtod((*endofchain), caddr_t);
7849 				m_copydata(clonechain, len, sizeofcpy, cp);
7850 				SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
7851 			}
7852 			return (outchain);
7853 		} else {
7854 			/* copy the old fashion way */
7855 			appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
7856 #ifdef SCTP_MBUF_LOGGING
7857 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7858 				struct mbuf *mat;
7859 
7860 				mat = appendchain;
7861 				while (mat) {
7862 					if (SCTP_BUF_IS_EXTENDED(mat)) {
7863 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7864 					}
7865 					mat = SCTP_BUF_NEXT(mat);
7866 				}
7867 			}
7868 #endif
7869 		}
7870 	}
7871 	if (appendchain == NULL) {
7872 		/* error */
7873 		if (outchain)
7874 			sctp_m_freem(outchain);
7875 		return (NULL);
7876 	}
7877 	if (outchain) {
7878 		/* tack on to the end */
7879 		if (*endofchain != NULL) {
7880 			SCTP_BUF_NEXT(((*endofchain))) = appendchain;
7881 		} else {
7882 			m = outchain;
7883 			while (m) {
7884 				if (SCTP_BUF_NEXT(m) == NULL) {
7885 					SCTP_BUF_NEXT(m) = appendchain;
7886 					break;
7887 				}
7888 				m = SCTP_BUF_NEXT(m);
7889 			}
7890 		}
7891 		/*
7892 		 * save off the end and update the end-chain postion
7893 		 */
7894 		m = appendchain;
7895 		while (m) {
7896 			if (SCTP_BUF_NEXT(m) == NULL) {
7897 				*endofchain = m;
7898 				break;
7899 			}
7900 			m = SCTP_BUF_NEXT(m);
7901 		}
7902 		return (outchain);
7903 	} else {
7904 		/* save off the end and update the end-chain postion */
7905 		m = appendchain;
7906 		while (m) {
7907 			if (SCTP_BUF_NEXT(m) == NULL) {
7908 				*endofchain = m;
7909 				break;
7910 			}
7911 			m = SCTP_BUF_NEXT(m);
7912 		}
7913 		return (appendchain);
7914 	}
7915 }
7916 
7917 int
7918 sctp_med_chunk_output(struct sctp_inpcb *inp,
7919     struct sctp_tcb *stcb,
7920     struct sctp_association *asoc,
7921     int *num_out,
7922     int *reason_code,
7923     int control_only, int *cwnd_full, int from_where,
7924     struct timeval *now, int *now_filled, int frag_point, int so_locked
7925 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7926     SCTP_UNUSED
7927 #endif
7928 );
7929 
7930 static void
7931 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
7932     uint32_t val)
7933 {
7934 	struct sctp_copy_all *ca;
7935 	struct mbuf *m;
7936 	int ret = 0;
7937 	int added_control = 0;
7938 	int un_sent, do_chunk_output = 1;
7939 	struct sctp_association *asoc;
7940 
7941 	ca = (struct sctp_copy_all *)ptr;
7942 	if (ca->m == NULL) {
7943 		return;
7944 	}
7945 	if (ca->inp != inp) {
7946 		/* TSNH */
7947 		return;
7948 	}
7949 	if ((ca->m) && ca->sndlen) {
7950 		m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
7951 		if (m == NULL) {
7952 			/* can't copy so we are done */
7953 			ca->cnt_failed++;
7954 			return;
7955 		}
7956 #ifdef SCTP_MBUF_LOGGING
7957 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7958 			struct mbuf *mat;
7959 
7960 			mat = m;
7961 			while (mat) {
7962 				if (SCTP_BUF_IS_EXTENDED(mat)) {
7963 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7964 				}
7965 				mat = SCTP_BUF_NEXT(mat);
7966 			}
7967 		}
7968 #endif
7969 	} else {
7970 		m = NULL;
7971 	}
7972 	SCTP_TCB_LOCK_ASSERT(stcb);
7973 	if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
7974 		/* Abort this assoc with m as the user defined reason */
7975 		if (m) {
7976 			struct sctp_paramhdr *ph;
7977 
7978 			SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
7979 			if (m) {
7980 				ph = mtod(m, struct sctp_paramhdr *);
7981 				ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
7982 				ph->param_length = htons(ca->sndlen);
7983 			}
7984 			/*
7985 			 * We add one here to keep the assoc from
7986 			 * dis-appearing on us.
7987 			 */
7988 			atomic_add_int(&stcb->asoc.refcnt, 1);
7989 			sctp_abort_an_association(inp, stcb,
7990 			    SCTP_RESPONSE_TO_USER_REQ,
7991 			    m, SCTP_SO_NOT_LOCKED);
7992 			/*
7993 			 * sctp_abort_an_association calls sctp_free_asoc()
7994 			 * free association will NOT free it since we
7995 			 * incremented the refcnt .. we do this to prevent
7996 			 * it being freed and things getting tricky since we
7997 			 * could end up (from free_asoc) calling inpcb_free
7998 			 * which would get a recursive lock call to the
7999 			 * iterator lock.. But as a consequence of that the
8000 			 * stcb will return to us un-locked.. since
8001 			 * free_asoc returns with either no TCB or the TCB
8002 			 * unlocked, we must relock.. to unlock in the
8003 			 * iterator timer :-0
8004 			 */
8005 			SCTP_TCB_LOCK(stcb);
8006 			atomic_add_int(&stcb->asoc.refcnt, -1);
8007 			goto no_chunk_output;
8008 		}
8009 	} else {
8010 		if (m) {
8011 			ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
8012 			    &ca->sndrcv, 1);
8013 		}
8014 		asoc = &stcb->asoc;
8015 		if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
8016 			/* shutdown this assoc */
8017 			int cnt;
8018 
8019 			cnt = sctp_is_there_unsent_data(stcb);
8020 
8021 			if (TAILQ_EMPTY(&asoc->send_queue) &&
8022 			    TAILQ_EMPTY(&asoc->sent_queue) &&
8023 			    (cnt == 0)) {
8024 				if (asoc->locked_on_sending) {
8025 					goto abort_anyway;
8026 				}
8027 				/*
8028 				 * there is nothing queued to send, so I'm
8029 				 * done...
8030 				 */
8031 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
8032 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
8033 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
8034 					/*
8035 					 * only send SHUTDOWN the first time
8036 					 * through
8037 					 */
8038 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
8039 					if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
8040 						SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8041 					}
8042 					SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
8043 					SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8044 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
8045 					    asoc->primary_destination);
8046 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
8047 					    asoc->primary_destination);
8048 					added_control = 1;
8049 					do_chunk_output = 0;
8050 				}
8051 			} else {
8052 				/*
8053 				 * we still got (or just got) data to send,
8054 				 * so set SHUTDOWN_PENDING
8055 				 */
8056 				/*
8057 				 * XXX sockets draft says that SCTP_EOF
8058 				 * should be sent with no data.  currently,
8059 				 * we will allow user data to be sent first
8060 				 * and move to SHUTDOWN-PENDING
8061 				 */
8062 				if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
8063 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
8064 				    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
8065 					if (asoc->locked_on_sending) {
8066 						/*
8067 						 * Locked to send out the
8068 						 * data
8069 						 */
8070 						struct sctp_stream_queue_pending *sp;
8071 
8072 						sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
8073 						if (sp) {
8074 							if ((sp->length == 0) && (sp->msg_is_complete == 0))
8075 								asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
8076 						}
8077 					}
8078 					asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
8079 					if (TAILQ_EMPTY(&asoc->send_queue) &&
8080 					    TAILQ_EMPTY(&asoc->sent_queue) &&
8081 					    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
8082 				abort_anyway:
8083 						atomic_add_int(&stcb->asoc.refcnt, 1);
8084 						sctp_abort_an_association(stcb->sctp_ep, stcb,
8085 						    SCTP_RESPONSE_TO_USER_REQ,
8086 						    NULL, SCTP_SO_NOT_LOCKED);
8087 						atomic_add_int(&stcb->asoc.refcnt, -1);
8088 						goto no_chunk_output;
8089 					}
8090 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
8091 					    asoc->primary_destination);
8092 				}
8093 			}
8094 
8095 		}
8096 	}
8097 	un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
8098 	    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
8099 
8100 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
8101 	    (stcb->asoc.total_flight > 0) &&
8102 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
8103 	    ) {
8104 		do_chunk_output = 0;
8105 	}
8106 	if (do_chunk_output)
8107 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
8108 	else if (added_control) {
8109 		int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
8110 		struct timeval now;
8111 		int frag_point;
8112 
8113 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
8114 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
8115 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
8116 	}
8117 no_chunk_output:
8118 	if (ret) {
8119 		ca->cnt_failed++;
8120 	} else {
8121 		ca->cnt_sent++;
8122 	}
8123 }
8124 
8125 static void
8126 sctp_sendall_completes(void *ptr, uint32_t val)
8127 {
8128 	struct sctp_copy_all *ca;
8129 
8130 	ca = (struct sctp_copy_all *)ptr;
8131 	/*
8132 	 * Do a notify here? Kacheong suggests that the notify be done at
8133 	 * the send time.. so you would push up a notification if any send
8134 	 * failed. Don't know if this is feasable since the only failures we
8135 	 * have is "memory" related and if you cannot get an mbuf to send
8136 	 * the data you surely can't get an mbuf to send up to notify the
8137 	 * user you can't send the data :->
8138 	 */
8139 
8140 	/* now free everything */
8141 	sctp_m_freem(ca->m);
8142 	SCTP_FREE(ca, SCTP_M_COPYAL);
8143 }
8144 
8145 
8146 #define	MC_ALIGN(m, len) do {						\
8147 	SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1));	\
8148 } while (0)
8149 
8150 
8151 
8152 static struct mbuf *
8153 sctp_copy_out_all(struct uio *uio, int len)
8154 {
8155 	struct mbuf *ret, *at;
8156 	int left, willcpy, cancpy, error;
8157 
8158 	ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
8159 	if (ret == NULL) {
8160 		/* TSNH */
8161 		return (NULL);
8162 	}
8163 	left = len;
8164 	SCTP_BUF_LEN(ret) = 0;
8165 	/* save space for the data chunk header */
8166 	cancpy = M_TRAILINGSPACE(ret);
8167 	willcpy = min(cancpy, left);
8168 	at = ret;
8169 	while (left > 0) {
8170 		/* Align data to the end */
8171 		error = uiomove(mtod(at, caddr_t), willcpy, uio);
8172 		if (error) {
8173 	err_out_now:
8174 			sctp_m_freem(at);
8175 			return (NULL);
8176 		}
8177 		SCTP_BUF_LEN(at) = willcpy;
8178 		SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
8179 		left -= willcpy;
8180 		if (left > 0) {
8181 			SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
8182 			if (SCTP_BUF_NEXT(at) == NULL) {
8183 				goto err_out_now;
8184 			}
8185 			at = SCTP_BUF_NEXT(at);
8186 			SCTP_BUF_LEN(at) = 0;
8187 			cancpy = M_TRAILINGSPACE(at);
8188 			willcpy = min(cancpy, left);
8189 		}
8190 	}
8191 	return (ret);
8192 }
8193 
8194 static int
8195 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
8196     struct sctp_sndrcvinfo *srcv)
8197 {
8198 	int ret;
8199 	struct sctp_copy_all *ca;
8200 
8201 	SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
8202 	    SCTP_M_COPYAL);
8203 	if (ca == NULL) {
8204 		sctp_m_freem(m);
8205 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8206 		return (ENOMEM);
8207 	}
8208 	memset(ca, 0, sizeof(struct sctp_copy_all));
8209 
8210 	ca->inp = inp;
8211 	memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
8212 	/*
8213 	 * take off the sendall flag, it would be bad if we failed to do
8214 	 * this :-0
8215 	 */
8216 	ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
8217 	/* get length and mbuf chain */
8218 	if (uio) {
8219 		ca->sndlen = uio->uio_resid;
8220 		ca->m = sctp_copy_out_all(uio, ca->sndlen);
8221 		if (ca->m == NULL) {
8222 			SCTP_FREE(ca, SCTP_M_COPYAL);
8223 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8224 			return (ENOMEM);
8225 		}
8226 	} else {
8227 		/* Gather the length of the send */
8228 		struct mbuf *mat;
8229 
8230 		mat = m;
8231 		ca->sndlen = 0;
8232 		while (m) {
8233 			ca->sndlen += SCTP_BUF_LEN(m);
8234 			m = SCTP_BUF_NEXT(m);
8235 		}
8236 		ca->m = mat;
8237 	}
8238 	ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
8239 	    SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
8240 	    SCTP_ASOC_ANY_STATE,
8241 	    (void *)ca, 0,
8242 	    sctp_sendall_completes, inp, 1);
8243 	if (ret) {
8244 		SCTP_PRINTF("Failed to initiate iterator for sendall\n");
8245 		SCTP_FREE(ca, SCTP_M_COPYAL);
8246 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
8247 		return (EFAULT);
8248 	}
8249 	return (0);
8250 }
8251 
8252 
8253 void
8254 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
8255 {
8256 	struct sctp_tmit_chunk *chk, *nchk;
8257 
8258 	chk = TAILQ_FIRST(&asoc->control_send_queue);
8259 	while (chk) {
8260 		nchk = TAILQ_NEXT(chk, sctp_next);
8261 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8262 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
8263 			if (chk->data) {
8264 				sctp_m_freem(chk->data);
8265 				chk->data = NULL;
8266 			}
8267 			asoc->ctrl_queue_cnt--;
8268 			sctp_free_a_chunk(stcb, chk);
8269 		}
8270 		chk = nchk;
8271 	}
8272 }
8273 
8274 void
8275 sctp_toss_old_asconf(struct sctp_tcb *stcb)
8276 {
8277 	struct sctp_association *asoc;
8278 	struct sctp_tmit_chunk *chk, *chk_tmp;
8279 	struct sctp_asconf_chunk *acp;
8280 
8281 	asoc = &stcb->asoc;
8282 	for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
8283 	    chk = chk_tmp) {
8284 		/* get next chk */
8285 		chk_tmp = TAILQ_NEXT(chk, sctp_next);
8286 		/* find SCTP_ASCONF chunk in queue */
8287 		if (chk->rec.chunk_id.id == SCTP_ASCONF) {
8288 			if (chk->data) {
8289 				acp = mtod(chk->data, struct sctp_asconf_chunk *);
8290 				if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
8291 					/* Not Acked yet */
8292 					break;
8293 				}
8294 			}
8295 			TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
8296 			if (chk->data) {
8297 				sctp_m_freem(chk->data);
8298 				chk->data = NULL;
8299 			}
8300 			asoc->ctrl_queue_cnt--;
8301 			sctp_free_a_chunk(stcb, chk);
8302 		}
8303 	}
8304 }
8305 
8306 
8307 static void
8308 sctp_clean_up_datalist(struct sctp_tcb *stcb,
8309 
8310     struct sctp_association *asoc,
8311     struct sctp_tmit_chunk **data_list,
8312     int bundle_at,
8313     struct sctp_nets *net)
8314 {
8315 	int i;
8316 	struct sctp_tmit_chunk *tp1;
8317 
8318 	for (i = 0; i < bundle_at; i++) {
8319 		/* off of the send queue */
8320 		if (i) {
8321 			/*
8322 			 * Any chunk NOT 0 you zap the time chunk 0 gets
8323 			 * zapped or set based on if a RTO measurment is
8324 			 * needed.
8325 			 */
8326 			data_list[i]->do_rtt = 0;
8327 		}
8328 		/* record time */
8329 		data_list[i]->sent_rcv_time = net->last_sent_time;
8330 		data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
8331 		TAILQ_REMOVE(&asoc->send_queue,
8332 		    data_list[i],
8333 		    sctp_next);
8334 		/* on to the sent queue */
8335 		tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
8336 		if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
8337 		    data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
8338 			struct sctp_tmit_chunk *tpp;
8339 
8340 			/* need to move back */
8341 	back_up_more:
8342 			tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
8343 			if (tpp == NULL) {
8344 				TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
8345 				goto all_done;
8346 			}
8347 			tp1 = tpp;
8348 			if (compare_with_wrap(tp1->rec.data.TSN_seq,
8349 			    data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
8350 				goto back_up_more;
8351 			}
8352 			TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
8353 		} else {
8354 			TAILQ_INSERT_TAIL(&asoc->sent_queue,
8355 			    data_list[i],
8356 			    sctp_next);
8357 		}
8358 all_done:
8359 		/* This does not lower until the cum-ack passes it */
8360 		asoc->sent_queue_cnt++;
8361 		asoc->send_queue_cnt--;
8362 		if ((asoc->peers_rwnd <= 0) &&
8363 		    (asoc->total_flight == 0) &&
8364 		    (bundle_at == 1)) {
8365 			/* Mark the chunk as being a window probe */
8366 			SCTP_STAT_INCR(sctps_windowprobed);
8367 		}
8368 #ifdef SCTP_AUDITING_ENABLED
8369 		sctp_audit_log(0xC2, 3);
8370 #endif
8371 		data_list[i]->sent = SCTP_DATAGRAM_SENT;
8372 		data_list[i]->snd_count = 1;
8373 		data_list[i]->rec.data.chunk_was_revoked = 0;
8374 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
8375 			sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
8376 			    data_list[i]->whoTo->flight_size,
8377 			    data_list[i]->book_size,
8378 			    (uintptr_t) data_list[i]->whoTo,
8379 			    data_list[i]->rec.data.TSN_seq);
8380 		}
8381 		sctp_flight_size_increase(data_list[i]);
8382 		sctp_total_flight_increase(stcb, data_list[i]);
8383 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
8384 			sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
8385 			    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
8386 		}
8387 		asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
8388 		    (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
8389 		if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8390 			/* SWS sender side engages */
8391 			asoc->peers_rwnd = 0;
8392 		}
8393 	}
8394 }
8395 
8396 static void
8397 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
8398 {
8399 	struct sctp_tmit_chunk *chk, *nchk;
8400 
8401 	for (chk = TAILQ_FIRST(&asoc->control_send_queue);
8402 	    chk; chk = nchk) {
8403 		nchk = TAILQ_NEXT(chk, sctp_next);
8404 		if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8405 		    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
8406 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8407 		    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8408 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8409 		    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8410 		    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8411 		    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8412 		    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8413 		    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8414 		    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8415 			/* Stray chunks must be cleaned up */
8416 	clean_up_anyway:
8417 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
8418 			if (chk->data) {
8419 				sctp_m_freem(chk->data);
8420 				chk->data = NULL;
8421 			}
8422 			asoc->ctrl_queue_cnt--;
8423 			sctp_free_a_chunk(stcb, chk);
8424 		} else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8425 			/* special handling, we must look into the param */
8426 			if (chk != asoc->str_reset) {
8427 				goto clean_up_anyway;
8428 			}
8429 		}
8430 	}
8431 }
8432 
8433 
8434 static int
8435 sctp_can_we_split_this(struct sctp_tcb *stcb,
8436     uint32_t length,
8437     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
8438 {
8439 	/*
8440 	 * Make a decision on if I should split a msg into multiple parts.
8441 	 * This is only asked of incomplete messages.
8442 	 */
8443 	if (eeor_on) {
8444 		/*
8445 		 * If we are doing EEOR we need to always send it if its the
8446 		 * entire thing, since it might be all the guy is putting in
8447 		 * the hopper.
8448 		 */
8449 		if (goal_mtu >= length) {
8450 			/*-
8451 			 * If we have data outstanding,
8452 			 * we get another chance when the sack
8453 			 * arrives to transmit - wait for more data
8454 			 */
8455 			if (stcb->asoc.total_flight == 0) {
8456 				/*
8457 				 * If nothing is in flight, we zero the
8458 				 * packet counter.
8459 				 */
8460 				return (length);
8461 			}
8462 			return (0);
8463 
8464 		} else {
8465 			/* You can fill the rest */
8466 			return (goal_mtu);
8467 		}
8468 	}
8469 	/*-
8470 	 * For those strange folk that make the send buffer
8471 	 * smaller than our fragmentation point, we can't
8472 	 * get a full msg in so we have to allow splitting.
8473 	 */
8474 	if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
8475 		return (length);
8476 	}
8477 	if ((length <= goal_mtu) ||
8478 	    ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
8479 		/* Sub-optimial residual don't split in non-eeor mode. */
8480 		return (0);
8481 	}
8482 	/*
8483 	 * If we reach here length is larger than the goal_mtu. Do we wish
8484 	 * to split it for the sake of packet putting together?
8485 	 */
8486 	if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
8487 		/* Its ok to split it */
8488 		return (min(goal_mtu, frag_point));
8489 	}
8490 	/* Nope, can't split */
8491 	return (0);
8492 
8493 }
8494 
8495 static uint32_t
8496 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
8497     struct sctp_stream_out *strq,
8498     uint32_t goal_mtu,
8499     uint32_t frag_point,
8500     int *locked,
8501     int *giveup,
8502     int eeor_mode,
8503     int *bail)
8504 {
8505 	/* Move from the stream to the send_queue keeping track of the total */
8506 	struct sctp_association *asoc;
8507 	struct sctp_stream_queue_pending *sp;
8508 	struct sctp_tmit_chunk *chk;
8509 	struct sctp_data_chunk *dchkh;
8510 	uint32_t to_move, length;
8511 	uint8_t rcv_flags = 0;
8512 	uint8_t some_taken;
8513 	uint8_t send_lock_up = 0;
8514 
8515 	SCTP_TCB_LOCK_ASSERT(stcb);
8516 	asoc = &stcb->asoc;
8517 one_more_time:
8518 	/* sa_ignore FREED_MEMORY */
8519 	sp = TAILQ_FIRST(&strq->outqueue);
8520 	if (sp == NULL) {
8521 		*locked = 0;
8522 		if (send_lock_up == 0) {
8523 			SCTP_TCB_SEND_LOCK(stcb);
8524 			send_lock_up = 1;
8525 		}
8526 		sp = TAILQ_FIRST(&strq->outqueue);
8527 		if (sp) {
8528 			goto one_more_time;
8529 		}
8530 		if (strq->last_msg_incomplete) {
8531 			SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
8532 			    strq->stream_no,
8533 			    strq->last_msg_incomplete);
8534 			strq->last_msg_incomplete = 0;
8535 		}
8536 		to_move = 0;
8537 		if (send_lock_up) {
8538 			SCTP_TCB_SEND_UNLOCK(stcb);
8539 			send_lock_up = 0;
8540 		}
8541 		goto out_of;
8542 	}
8543 	if ((sp->msg_is_complete) && (sp->length == 0)) {
8544 		if (sp->sender_all_done) {
8545 			/*
8546 			 * We are doing differed cleanup. Last time through
8547 			 * when we took all the data the sender_all_done was
8548 			 * not set.
8549 			 */
8550 			if (sp->put_last_out == 0) {
8551 				SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
8552 				SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
8553 				    sp->sender_all_done,
8554 				    sp->length,
8555 				    sp->msg_is_complete,
8556 				    sp->put_last_out,
8557 				    send_lock_up);
8558 			}
8559 			if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
8560 				SCTP_TCB_SEND_LOCK(stcb);
8561 				send_lock_up = 1;
8562 			}
8563 			atomic_subtract_int(&asoc->stream_queue_cnt, 1);
8564 			TAILQ_REMOVE(&strq->outqueue, sp, next);
8565 			sctp_free_remote_addr(sp->net);
8566 			if (sp->data) {
8567 				sctp_m_freem(sp->data);
8568 				sp->data = NULL;
8569 			}
8570 			sctp_free_a_strmoq(stcb, sp);
8571 
8572 			/* we can't be locked to it */
8573 			*locked = 0;
8574 			stcb->asoc.locked_on_sending = NULL;
8575 			if (send_lock_up) {
8576 				SCTP_TCB_SEND_UNLOCK(stcb);
8577 				send_lock_up = 0;
8578 			}
8579 			/* back to get the next msg */
8580 			goto one_more_time;
8581 		} else {
8582 			/*
8583 			 * sender just finished this but still holds a
8584 			 * reference
8585 			 */
8586 			*locked = 1;
8587 			*giveup = 1;
8588 			to_move = 0;
8589 			goto out_of;
8590 		}
8591 	} else {
8592 		/* is there some to get */
8593 		if (sp->length == 0) {
8594 			/* no */
8595 			*locked = 1;
8596 			*giveup = 1;
8597 			to_move = 0;
8598 			goto out_of;
8599 		}
8600 	}
8601 	some_taken = sp->some_taken;
8602 	if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
8603 		sp->msg_is_complete = 1;
8604 	}
8605 re_look:
8606 	length = sp->length;
8607 	if (sp->msg_is_complete) {
8608 		/* The message is complete */
8609 		to_move = min(length, frag_point);
8610 		if (to_move == length) {
8611 			/* All of it fits in the MTU */
8612 			if (sp->some_taken) {
8613 				rcv_flags |= SCTP_DATA_LAST_FRAG;
8614 				sp->put_last_out = 1;
8615 			} else {
8616 				rcv_flags |= SCTP_DATA_NOT_FRAG;
8617 				sp->put_last_out = 1;
8618 			}
8619 		} else {
8620 			/* Not all of it fits, we fragment */
8621 			if (sp->some_taken == 0) {
8622 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
8623 			}
8624 			sp->some_taken = 1;
8625 		}
8626 	} else {
8627 		to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
8628 		if (to_move) {
8629 			/*-
8630 			 * We use a snapshot of length in case it
8631 			 * is expanding during the compare.
8632 			 */
8633 			uint32_t llen;
8634 
8635 			llen = length;
8636 			if (to_move >= llen) {
8637 				to_move = llen;
8638 				if (send_lock_up == 0) {
8639 					/*-
8640 					 * We are taking all of an incomplete msg
8641 					 * thus we need a send lock.
8642 					 */
8643 					SCTP_TCB_SEND_LOCK(stcb);
8644 					send_lock_up = 1;
8645 					if (sp->msg_is_complete) {
8646 						/*
8647 						 * the sender finished the
8648 						 * msg
8649 						 */
8650 						goto re_look;
8651 					}
8652 				}
8653 			}
8654 			if (sp->some_taken == 0) {
8655 				rcv_flags |= SCTP_DATA_FIRST_FRAG;
8656 				sp->some_taken = 1;
8657 			}
8658 		} else {
8659 			/* Nothing to take. */
8660 			if (sp->some_taken) {
8661 				*locked = 1;
8662 			}
8663 			*giveup = 1;
8664 			to_move = 0;
8665 			goto out_of;
8666 		}
8667 	}
8668 
8669 	/* If we reach here, we can copy out a chunk */
8670 	sctp_alloc_a_chunk(stcb, chk);
8671 	if (chk == NULL) {
8672 		/* No chunk memory */
8673 		*giveup = 1;
8674 		to_move = 0;
8675 		goto out_of;
8676 	}
8677 	/*
8678 	 * Setup for unordered if needed by looking at the user sent info
8679 	 * flags.
8680 	 */
8681 	if (sp->sinfo_flags & SCTP_UNORDERED) {
8682 		rcv_flags |= SCTP_DATA_UNORDERED;
8683 	}
8684 	if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
8685 	    ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
8686 		rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8687 	}
8688 	/* clear out the chunk before setting up */
8689 	memset(chk, 0, sizeof(*chk));
8690 	chk->rec.data.rcv_flags = rcv_flags;
8691 
8692 	if (to_move >= length) {
8693 		/* we think we can steal the whole thing */
8694 		if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
8695 			SCTP_TCB_SEND_LOCK(stcb);
8696 			send_lock_up = 1;
8697 		}
8698 		if (to_move < sp->length) {
8699 			/* bail, it changed */
8700 			goto dont_do_it;
8701 		}
8702 		chk->data = sp->data;
8703 		chk->last_mbuf = sp->tail_mbuf;
8704 		/* register the stealing */
8705 		sp->data = sp->tail_mbuf = NULL;
8706 	} else {
8707 		struct mbuf *m;
8708 
8709 dont_do_it:
8710 		chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
8711 		chk->last_mbuf = NULL;
8712 		if (chk->data == NULL) {
8713 			sp->some_taken = some_taken;
8714 			sctp_free_a_chunk(stcb, chk);
8715 			*bail = 1;
8716 			to_move = 0;
8717 			goto out_of;
8718 		}
8719 #ifdef SCTP_MBUF_LOGGING
8720 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8721 			struct mbuf *mat;
8722 
8723 			mat = chk->data;
8724 			while (mat) {
8725 				if (SCTP_BUF_IS_EXTENDED(mat)) {
8726 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8727 				}
8728 				mat = SCTP_BUF_NEXT(mat);
8729 			}
8730 		}
8731 #endif
8732 		/* Pull off the data */
8733 		m_adj(sp->data, to_move);
8734 		/* Now lets work our way down and compact it */
8735 		m = sp->data;
8736 		while (m && (SCTP_BUF_LEN(m) == 0)) {
8737 			sp->data = SCTP_BUF_NEXT(m);
8738 			SCTP_BUF_NEXT(m) = NULL;
8739 			if (sp->tail_mbuf == m) {
8740 				/*-
8741 				 * Freeing tail? TSNH since
8742 				 * we supposedly were taking less
8743 				 * than the sp->length.
8744 				 */
8745 #ifdef INVARIANTS
8746 				panic("Huh, freing tail? - TSNH");
8747 #else
8748 				SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
8749 				sp->tail_mbuf = sp->data = NULL;
8750 				sp->length = 0;
8751 #endif
8752 
8753 			}
8754 			sctp_m_free(m);
8755 			m = sp->data;
8756 		}
8757 	}
8758 	if (SCTP_BUF_IS_EXTENDED(chk->data)) {
8759 		chk->copy_by_ref = 1;
8760 	} else {
8761 		chk->copy_by_ref = 0;
8762 	}
8763 	/*
8764 	 * get last_mbuf and counts of mb useage This is ugly but hopefully
8765 	 * its only one mbuf.
8766 	 */
8767 	if (chk->last_mbuf == NULL) {
8768 		chk->last_mbuf = chk->data;
8769 		while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
8770 			chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
8771 		}
8772 	}
8773 	if (to_move > length) {
8774 		/*- This should not happen either
8775 		 * since we always lower to_move to the size
8776 		 * of sp->length if its larger.
8777 		 */
8778 #ifdef INVARIANTS
8779 		panic("Huh, how can to_move be larger?");
8780 #else
8781 		SCTP_PRINTF("Huh, how can to_move be larger?\n");
8782 		sp->length = 0;
8783 #endif
8784 	} else {
8785 		atomic_subtract_int(&sp->length, to_move);
8786 	}
8787 	if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
8788 		/* Not enough room for a chunk header, get some */
8789 		struct mbuf *m;
8790 
8791 		m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
8792 		if (m == NULL) {
8793 			/*
8794 			 * we're in trouble here. _PREPEND below will free
8795 			 * all the data if there is no leading space, so we
8796 			 * must put the data back and restore.
8797 			 */
8798 			if (send_lock_up == 0) {
8799 				SCTP_TCB_SEND_LOCK(stcb);
8800 				send_lock_up = 1;
8801 			}
8802 			if (chk->data == NULL) {
8803 				/* unsteal the data */
8804 				sp->data = chk->data;
8805 				sp->tail_mbuf = chk->last_mbuf;
8806 			} else {
8807 				struct mbuf *m_tmp;
8808 
8809 				/* reassemble the data */
8810 				m_tmp = sp->data;
8811 				sp->data = chk->data;
8812 				SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
8813 			}
8814 			sp->some_taken = some_taken;
8815 			atomic_add_int(&sp->length, to_move);
8816 			chk->data = NULL;
8817 			*bail = 1;
8818 			sctp_free_a_chunk(stcb, chk);
8819 			to_move = 0;
8820 			goto out_of;
8821 		} else {
8822 			SCTP_BUF_LEN(m) = 0;
8823 			SCTP_BUF_NEXT(m) = chk->data;
8824 			chk->data = m;
8825 			M_ALIGN(chk->data, 4);
8826 		}
8827 	}
8828 	SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
8829 	if (chk->data == NULL) {
8830 		/* HELP, TSNH since we assured it would not above? */
8831 #ifdef INVARIANTS
8832 		panic("prepend failes HELP?");
8833 #else
8834 		SCTP_PRINTF("prepend fails HELP?\n");
8835 		sctp_free_a_chunk(stcb, chk);
8836 #endif
8837 		*bail = 1;
8838 		to_move = 0;
8839 		goto out_of;
8840 	}
8841 	sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
8842 	chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
8843 	chk->book_size_scale = 0;
8844 	chk->sent = SCTP_DATAGRAM_UNSENT;
8845 
8846 	chk->flags = 0;
8847 	chk->asoc = &stcb->asoc;
8848 	chk->pad_inplace = 0;
8849 	chk->no_fr_allowed = 0;
8850 	chk->rec.data.stream_seq = sp->strseq;
8851 	chk->rec.data.stream_number = sp->stream;
8852 	chk->rec.data.payloadtype = sp->ppid;
8853 	chk->rec.data.context = sp->context;
8854 	chk->rec.data.doing_fast_retransmit = 0;
8855 	chk->rec.data.ect_nonce = 0;	/* ECN Nonce */
8856 
8857 	chk->rec.data.timetodrop = sp->ts;
8858 	chk->flags = sp->act_flags;
8859 	chk->addr_over = sp->addr_over;
8860 
8861 	chk->whoTo = net;
8862 	atomic_add_int(&chk->whoTo->ref_count, 1);
8863 
8864 	if (sp->holds_key_ref) {
8865 		chk->auth_keyid = sp->auth_keyid;
8866 		sctp_auth_key_acquire(stcb, chk->auth_keyid);
8867 		chk->holds_key_ref = 1;
8868 	}
8869 	chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
8870 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
8871 		sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
8872 		    (uintptr_t) stcb, sp->length,
8873 		    (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
8874 		    chk->rec.data.TSN_seq);
8875 	}
8876 	dchkh = mtod(chk->data, struct sctp_data_chunk *);
8877 	/*
8878 	 * Put the rest of the things in place now. Size was done earlier in
8879 	 * previous loop prior to padding.
8880 	 */
8881 
8882 #ifdef SCTP_ASOCLOG_OF_TSNS
8883 	SCTP_TCB_LOCK_ASSERT(stcb);
8884 	if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
8885 		asoc->tsn_out_at = 0;
8886 		asoc->tsn_out_wrapped = 1;
8887 	}
8888 	asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
8889 	asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
8890 	asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
8891 	asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
8892 	asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
8893 	asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
8894 	asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
8895 	asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
8896 	asoc->tsn_out_at++;
8897 #endif
8898 
8899 	dchkh->ch.chunk_type = SCTP_DATA;
8900 	dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
8901 	dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
8902 	dchkh->dp.stream_id = htons(strq->stream_no);
8903 	dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
8904 	dchkh->dp.protocol_id = chk->rec.data.payloadtype;
8905 	dchkh->ch.chunk_length = htons(chk->send_size);
8906 	/* Now advance the chk->send_size by the actual pad needed. */
8907 	if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
8908 		/* need a pad */
8909 		struct mbuf *lm;
8910 		int pads;
8911 
8912 		pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
8913 		if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
8914 			chk->pad_inplace = 1;
8915 		}
8916 		if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
8917 			/* pad added an mbuf */
8918 			chk->last_mbuf = lm;
8919 		}
8920 		chk->send_size += pads;
8921 	}
8922 	/* We only re-set the policy if it is on */
8923 	if (sp->pr_sctp_on) {
8924 		sctp_set_prsctp_policy(stcb, sp);
8925 		asoc->pr_sctp_cnt++;
8926 		chk->pr_sctp_on = 1;
8927 	} else {
8928 		chk->pr_sctp_on = 0;
8929 	}
8930 	if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
8931 		/* All done pull and kill the message */
8932 		atomic_subtract_int(&asoc->stream_queue_cnt, 1);
8933 		if (sp->put_last_out == 0) {
8934 			SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
8935 			SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
8936 			    sp->sender_all_done,
8937 			    sp->length,
8938 			    sp->msg_is_complete,
8939 			    sp->put_last_out,
8940 			    send_lock_up);
8941 		}
8942 		if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
8943 			SCTP_TCB_SEND_LOCK(stcb);
8944 			send_lock_up = 1;
8945 		}
8946 		TAILQ_REMOVE(&strq->outqueue, sp, next);
8947 		sctp_free_remote_addr(sp->net);
8948 		if (sp->data) {
8949 			sctp_m_freem(sp->data);
8950 			sp->data = NULL;
8951 		}
8952 		sctp_free_a_strmoq(stcb, sp);
8953 
8954 		/* we can't be locked to it */
8955 		*locked = 0;
8956 		stcb->asoc.locked_on_sending = NULL;
8957 	} else {
8958 		/* more to go, we are locked */
8959 		*locked = 1;
8960 	}
8961 	asoc->chunks_on_out_queue++;
8962 	TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
8963 	asoc->send_queue_cnt++;
8964 out_of:
8965 	if (send_lock_up) {
8966 		SCTP_TCB_SEND_UNLOCK(stcb);
8967 		send_lock_up = 0;
8968 	}
8969 	return (to_move);
8970 }
8971 
8972 
8973 static struct sctp_stream_out *
8974 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
8975 {
8976 	struct sctp_stream_out *strq;
8977 
8978 	/* Find the next stream to use */
8979 	if (asoc->last_out_stream == NULL) {
8980 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
8981 		if (asoc->last_out_stream == NULL) {
8982 			/* huh nothing on the wheel, TSNH */
8983 			return (NULL);
8984 		}
8985 		goto done_it;
8986 	}
8987 	strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
8988 done_it:
8989 	if (strq == NULL) {
8990 		strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
8991 	}
8992 	/* Save off the last stream */
8993 	asoc->last_out_stream = strq;
8994 	return (strq);
8995 
8996 }
8997 
8998 
8999 static void
9000 sctp_fill_outqueue(struct sctp_tcb *stcb,
9001     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
9002 {
9003 	struct sctp_association *asoc;
9004 	struct sctp_stream_out *strq, *strqn, *strqt;
9005 	int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
9006 	int locked, giveup;
9007 	struct sctp_stream_queue_pending *sp;
9008 
9009 	SCTP_TCB_LOCK_ASSERT(stcb);
9010 	asoc = &stcb->asoc;
9011 #ifdef INET6
9012 	if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
9013 		goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
9014 	} else {
9015 		/* ?? not sure what else to do */
9016 		goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9017 	}
9018 #else
9019 	goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
9020 #endif
9021 	/* Need an allowance for the data chunk header too */
9022 	goal_mtu -= sizeof(struct sctp_data_chunk);
9023 
9024 	/* must make even word boundary */
9025 	goal_mtu &= 0xfffffffc;
9026 	if (asoc->locked_on_sending) {
9027 		/* We are stuck on one stream until the message completes. */
9028 		strqn = strq = asoc->locked_on_sending;
9029 		locked = 1;
9030 	} else {
9031 		strqn = strq = sctp_select_a_stream(stcb, asoc);
9032 		locked = 0;
9033 	}
9034 
9035 	while ((goal_mtu > 0) && strq) {
9036 		sp = TAILQ_FIRST(&strq->outqueue);
9037 		/*
9038 		 * If CMT is off, we must validate that the stream in
9039 		 * question has the first item pointed towards are network
9040 		 * destionation requested by the caller. Note that if we
9041 		 * turn out to be locked to a stream (assigning TSN's then
9042 		 * we must stop, since we cannot look for another stream
9043 		 * with data to send to that destination). In CMT's case, by
9044 		 * skipping this check, we will send one data packet towards
9045 		 * the requested net.
9046 		 */
9047 		if (sp == NULL) {
9048 			break;
9049 		}
9050 		if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
9051 			/* none for this network */
9052 			if (locked) {
9053 				break;
9054 			} else {
9055 				strq = sctp_select_a_stream(stcb, asoc);
9056 				if (strq == NULL)
9057 					/* none left */
9058 					break;
9059 				if (strqn == strq) {
9060 					/* I have circled */
9061 					break;
9062 				}
9063 				continue;
9064 			}
9065 		}
9066 		giveup = 0;
9067 		bail = 0;
9068 		moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
9069 		    &giveup, eeor_mode, &bail);
9070 		asoc->last_out_stream = strq;
9071 		if (locked) {
9072 			asoc->locked_on_sending = strq;
9073 			if ((moved_how_much == 0) || (giveup) || bail)
9074 				/* no more to move for now */
9075 				break;
9076 		} else {
9077 			asoc->locked_on_sending = NULL;
9078 			strqt = sctp_select_a_stream(stcb, asoc);
9079 			if (TAILQ_FIRST(&strq->outqueue) == NULL) {
9080 				if (strq == strqn) {
9081 					/* Must move start to next one */
9082 					strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
9083 					if (strqn == NULL) {
9084 						strqn = TAILQ_FIRST(&asoc->out_wheel);
9085 						if (strqn == NULL) {
9086 							break;
9087 						}
9088 					}
9089 				}
9090 				sctp_remove_from_wheel(stcb, asoc, strq, 0);
9091 			}
9092 			if ((giveup) || bail) {
9093 				break;
9094 			}
9095 			strq = strqt;
9096 			if (strq == NULL) {
9097 				break;
9098 			}
9099 		}
9100 		total_moved += moved_how_much;
9101 		goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
9102 		goal_mtu &= 0xfffffffc;
9103 	}
9104 	if (bail)
9105 		*quit_now = 1;
9106 
9107 	if (total_moved == 0) {
9108 		if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
9109 		    (net == stcb->asoc.primary_destination)) {
9110 			/* ran dry for primary network net */
9111 			SCTP_STAT_INCR(sctps_primary_randry);
9112 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
9113 			/* ran dry with CMT on */
9114 			SCTP_STAT_INCR(sctps_cmt_randry);
9115 		}
9116 	}
9117 }
9118 
9119 void
9120 sctp_fix_ecn_echo(struct sctp_association *asoc)
9121 {
9122 	struct sctp_tmit_chunk *chk;
9123 
9124 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9125 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
9126 			chk->sent = SCTP_DATAGRAM_UNSENT;
9127 		}
9128 	}
9129 }
9130 
9131 static void
9132 sctp_move_to_an_alt(struct sctp_tcb *stcb,
9133     struct sctp_association *asoc,
9134     struct sctp_nets *net)
9135 {
9136 	struct sctp_tmit_chunk *chk;
9137 	struct sctp_nets *a_net;
9138 
9139 	SCTP_TCB_LOCK_ASSERT(stcb);
9140 	/*
9141 	 * JRS 5/14/07 - If CMT PF is turned on, find an alternate
9142 	 * destination using the PF algorithm for finding alternate
9143 	 * destinations.
9144 	 */
9145 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
9146 		a_net = sctp_find_alternate_net(stcb, net, 2);
9147 	} else {
9148 		a_net = sctp_find_alternate_net(stcb, net, 0);
9149 	}
9150 	if ((a_net != net) &&
9151 	    ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
9152 		/*
9153 		 * We only proceed if a valid alternate is found that is not
9154 		 * this one and is reachable. Here we must move all chunks
9155 		 * queued in the send queue off of the destination address
9156 		 * to our alternate.
9157 		 */
9158 		TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
9159 			if (chk->whoTo == net) {
9160 				/* Move the chunk to our alternate */
9161 				sctp_free_remote_addr(chk->whoTo);
9162 				chk->whoTo = a_net;
9163 				atomic_add_int(&a_net->ref_count, 1);
9164 			}
9165 		}
9166 	}
9167 }
9168 
9169 int
9170 sctp_med_chunk_output(struct sctp_inpcb *inp,
9171     struct sctp_tcb *stcb,
9172     struct sctp_association *asoc,
9173     int *num_out,
9174     int *reason_code,
9175     int control_only, int *cwnd_full, int from_where,
9176     struct timeval *now, int *now_filled, int frag_point, int so_locked
9177 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9178     SCTP_UNUSED
9179 #endif
9180 )
9181 {
9182 	/*
9183 	 * Ok this is the generic chunk service queue. we must do the
9184 	 * following: - Service the stream queue that is next, moving any
9185 	 * message (note I must get a complete message i.e. FIRST/MIDDLE and
9186 	 * LAST to the out queue in one pass) and assigning TSN's - Check to
9187 	 * see if the cwnd/rwnd allows any output, if so we go ahead and
9188 	 * fomulate and send the low level chunks. Making sure to combine
9189 	 * any control in the control chunk queue also.
9190 	 */
9191 	struct sctp_nets *net;
9192 	struct mbuf *outchain, *endoutchain;
9193 	struct sctp_tmit_chunk *chk, *nchk;
9194 
9195 	/* temp arrays for unlinking */
9196 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9197 	int no_fragmentflg, error;
9198 	int one_chunk, hbflag, skip_data_for_this_net;
9199 	int asconf, cookie, no_out_cnt;
9200 	int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
9201 	unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
9202 	struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
9203 	int tsns_sent = 0;
9204 	uint32_t auth_offset = 0;
9205 	struct sctp_auth_chunk *auth = NULL;
9206 	uint16_t auth_keyid;
9207 	int override_ok = 1;
9208 	int data_auth_reqd = 0;
9209 
9210 	/*
9211 	 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
9212 	 * destination.
9213 	 */
9214 	int pf_hbflag = 0;
9215 	int quit_now = 0;
9216 
9217 	*num_out = 0;
9218 	cwnd_full_ind = 0;
9219 	auth_keyid = stcb->asoc.authinfo.active_keyid;
9220 
9221 	if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
9222 	    (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
9223 	    (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
9224 		eeor_mode = 1;
9225 	} else {
9226 		eeor_mode = 0;
9227 	}
9228 	ctl_cnt = no_out_cnt = asconf = cookie = 0;
9229 	/*
9230 	 * First lets prime the pump. For each destination, if there is room
9231 	 * in the flight size, attempt to pull an MTU's worth out of the
9232 	 * stream queues into the general send_queue
9233 	 */
9234 #ifdef SCTP_AUDITING_ENABLED
9235 	sctp_audit_log(0xC2, 2);
9236 #endif
9237 	SCTP_TCB_LOCK_ASSERT(stcb);
9238 	hbflag = 0;
9239 	if ((control_only) || (asoc->stream_reset_outstanding))
9240 		no_data_chunks = 1;
9241 	else
9242 		no_data_chunks = 0;
9243 
9244 	/* Nothing to possible to send? */
9245 	if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9246 	    TAILQ_EMPTY(&asoc->asconf_send_queue) &&
9247 	    TAILQ_EMPTY(&asoc->send_queue) &&
9248 	    TAILQ_EMPTY(&asoc->out_wheel)) {
9249 		*reason_code = 9;
9250 		return (0);
9251 	}
9252 	if (asoc->peers_rwnd == 0) {
9253 		/* No room in peers rwnd */
9254 		*cwnd_full = 1;
9255 		*reason_code = 1;
9256 		if (asoc->total_flight > 0) {
9257 			/* we are allowed one chunk in flight */
9258 			no_data_chunks = 1;
9259 		}
9260 	}
9261 	if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
9262 		if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
9263 			/*
9264 			 * for CMT we start at the next one past the one we
9265 			 * last added data to.
9266 			 */
9267 			if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
9268 				goto skip_the_fill_from_streams;
9269 			}
9270 			if (asoc->last_net_data_came_from) {
9271 				net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
9272 				if (net == NULL) {
9273 					net = TAILQ_FIRST(&asoc->nets);
9274 				}
9275 			} else {
9276 				/* back to start */
9277 				net = TAILQ_FIRST(&asoc->nets);
9278 			}
9279 
9280 			/*
9281 			 * JRI-TODO: CMT-MPI. Simply set the first
9282 			 * destination (net) to be optimized for the next
9283 			 * message to be pulled out of the outwheel. 1. peek
9284 			 * at outwheel 2. If large message, set net =
9285 			 * highest_cwnd 3. If small message, set net =
9286 			 * lowest rtt
9287 			 */
9288 		} else {
9289 			net = asoc->primary_destination;
9290 			if (net == NULL) {
9291 				/* TSNH */
9292 				net = TAILQ_FIRST(&asoc->nets);
9293 			}
9294 		}
9295 		start_at = net;
9296 
9297 one_more_time:
9298 		for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
9299 			net->window_probe = 0;
9300 			if (old_startat && (old_startat == net)) {
9301 				break;
9302 			}
9303 			/*
9304 			 * JRI: if dest is unreachable or unconfirmed, do
9305 			 * not send data to it
9306 			 */
9307 			if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
9308 				continue;
9309 			}
9310 			/*
9311 			 * JRI: if dest is in PF state, do not send data to
9312 			 * it
9313 			 */
9314 			if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9315 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9316 			    (net->dest_state & SCTP_ADDR_PF)) {
9317 				continue;
9318 			}
9319 			if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
9320 				/* nothing can be in queue for this guy */
9321 				continue;
9322 			}
9323 			if (net->flight_size >= net->cwnd) {
9324 				/* skip this network, no room */
9325 				cwnd_full_ind++;
9326 				continue;
9327 			}
9328 			/*
9329 			 * JRI : this for loop we are in takes in each net,
9330 			 * if its's got space in cwnd and has data sent to
9331 			 * it (when CMT is off) then it calls
9332 			 * sctp_fill_outqueue for the net. This gets data on
9333 			 * the send queue for that network.
9334 			 *
9335 			 * In sctp_fill_outqueue TSN's are assigned and data is
9336 			 * copied out of the stream buffers. Note mostly
9337 			 * copy by reference (we hope).
9338 			 */
9339 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9340 				sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
9341 			}
9342 			sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
9343 			if (quit_now) {
9344 				/* memory alloc failure */
9345 				no_data_chunks = 1;
9346 				goto skip_the_fill_from_streams;
9347 			}
9348 		}
9349 		if (start_at != TAILQ_FIRST(&asoc->nets)) {
9350 			/* got to pick up the beginning stuff. */
9351 			old_startat = start_at;
9352 			start_at = net = TAILQ_FIRST(&asoc->nets);
9353 			if (old_startat)
9354 				goto one_more_time;
9355 		}
9356 	}
9357 skip_the_fill_from_streams:
9358 	*cwnd_full = cwnd_full_ind;
9359 
9360 	/* now service each destination and send out what we can for it */
9361 	/* Nothing to send? */
9362 	if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
9363 	    (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
9364 	    (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
9365 		*reason_code = 8;
9366 		return (0);
9367 	}
9368 	if (no_data_chunks) {
9369 		chk = TAILQ_FIRST(&asoc->asconf_send_queue);
9370 		if (chk == NULL)
9371 			chk = TAILQ_FIRST(&asoc->control_send_queue);
9372 	} else {
9373 		chk = TAILQ_FIRST(&asoc->send_queue);
9374 	}
9375 	if (chk) {
9376 		send_start_at = chk->whoTo;
9377 	} else {
9378 		send_start_at = TAILQ_FIRST(&asoc->nets);
9379 	}
9380 	old_startat = NULL;
9381 again_one_more_time:
9382 	for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
9383 		/* how much can we send? */
9384 		/* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
9385 		if (old_startat && (old_startat == net)) {
9386 			/* through list ocmpletely. */
9387 			break;
9388 		}
9389 		tsns_sent = 0;
9390 		if (net->ref_count < 2) {
9391 			/*
9392 			 * Ref-count of 1 so we cannot have data or control
9393 			 * queued to this address. Skip it.
9394 			 */
9395 			continue;
9396 		}
9397 		ctl_cnt = bundle_at = 0;
9398 		endoutchain = outchain = NULL;
9399 		no_fragmentflg = 1;
9400 		one_chunk = 0;
9401 		if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
9402 			skip_data_for_this_net = 1;
9403 		} else {
9404 			skip_data_for_this_net = 0;
9405 		}
9406 		if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
9407 			/*
9408 			 * if we have a route and an ifp check to see if we
9409 			 * have room to send to this guy
9410 			 */
9411 			struct ifnet *ifp;
9412 
9413 			ifp = net->ro.ro_rt->rt_ifp;
9414 			if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
9415 				SCTP_STAT_INCR(sctps_ifnomemqueued);
9416 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9417 					sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
9418 				}
9419 				continue;
9420 			}
9421 		}
9422 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
9423 		case AF_INET:
9424 			mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
9425 			break;
9426 #ifdef INET6
9427 		case AF_INET6:
9428 			mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
9429 			break;
9430 #endif
9431 		default:
9432 			/* TSNH */
9433 			mtu = net->mtu;
9434 			break;
9435 		}
9436 		mx_mtu = mtu;
9437 		to_out = 0;
9438 		if (mtu > asoc->peers_rwnd) {
9439 			if (asoc->total_flight > 0) {
9440 				/* We have a packet in flight somewhere */
9441 				r_mtu = asoc->peers_rwnd;
9442 			} else {
9443 				/* We are always allowed to send one MTU out */
9444 				one_chunk = 1;
9445 				r_mtu = mtu;
9446 			}
9447 		} else {
9448 			r_mtu = mtu;
9449 		}
9450 		/************************/
9451 		/* ASCONF transmission */
9452 		/************************/
9453 		/* Now first lets go through the asconf queue */
9454 		for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
9455 		    chk; chk = nchk) {
9456 			nchk = TAILQ_NEXT(chk, sctp_next);
9457 			if (chk->rec.chunk_id.id != SCTP_ASCONF) {
9458 				continue;
9459 			}
9460 			if (chk->whoTo != net) {
9461 				/*
9462 				 * No, not sent to the network we are
9463 				 * looking at
9464 				 */
9465 				break;
9466 			}
9467 			if (chk->data == NULL) {
9468 				break;
9469 			}
9470 			if (chk->sent != SCTP_DATAGRAM_UNSENT &&
9471 			    chk->sent != SCTP_DATAGRAM_RESEND) {
9472 				break;
9473 			}
9474 			/*
9475 			 * if no AUTH is yet included and this chunk
9476 			 * requires it, make sure to account for it.  We
9477 			 * don't apply the size until the AUTH chunk is
9478 			 * actually added below in case there is no room for
9479 			 * this chunk. NOTE: we overload the use of "omtu"
9480 			 * here
9481 			 */
9482 			if ((auth == NULL) &&
9483 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9484 			    stcb->asoc.peer_auth_chunks)) {
9485 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9486 			} else
9487 				omtu = 0;
9488 			/* Here we do NOT factor the r_mtu */
9489 			if ((chk->send_size < (int)(mtu - omtu)) ||
9490 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9491 				/*
9492 				 * We probably should glom the mbuf chain
9493 				 * from the chk->data for control but the
9494 				 * problem is it becomes yet one more level
9495 				 * of tracking to do if for some reason
9496 				 * output fails. Then I have got to
9497 				 * reconstruct the merged control chain.. el
9498 				 * yucko.. for now we take the easy way and
9499 				 * do the copy
9500 				 */
9501 				/*
9502 				 * Add an AUTH chunk, if chunk requires it
9503 				 * save the offset into the chain for AUTH
9504 				 */
9505 				if ((auth == NULL) &&
9506 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9507 				    stcb->asoc.peer_auth_chunks))) {
9508 					outchain = sctp_add_auth_chunk(outchain,
9509 					    &endoutchain,
9510 					    &auth,
9511 					    &auth_offset,
9512 					    stcb,
9513 					    chk->rec.chunk_id.id);
9514 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9515 				}
9516 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
9517 				    (int)chk->rec.chunk_id.can_take_data,
9518 				    chk->send_size, chk->copy_by_ref);
9519 				if (outchain == NULL) {
9520 					*reason_code = 8;
9521 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9522 					return (ENOMEM);
9523 				}
9524 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9525 				/* update our MTU size */
9526 				if (mtu > (chk->send_size + omtu))
9527 					mtu -= (chk->send_size + omtu);
9528 				else
9529 					mtu = 0;
9530 				to_out += (chk->send_size + omtu);
9531 				/* Do clear IP_DF ? */
9532 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9533 					no_fragmentflg = 0;
9534 				}
9535 				if (chk->rec.chunk_id.can_take_data)
9536 					chk->data = NULL;
9537 				/*
9538 				 * set hb flag since we can use these for
9539 				 * RTO
9540 				 */
9541 				hbflag = 1;
9542 				asconf = 1;
9543 				/*
9544 				 * should sysctl this: don't bundle data
9545 				 * with ASCONF since it requires AUTH
9546 				 */
9547 				no_data_chunks = 1;
9548 				chk->sent = SCTP_DATAGRAM_SENT;
9549 				chk->snd_count++;
9550 				if (mtu == 0) {
9551 					/*
9552 					 * Ok we are out of room but we can
9553 					 * output without effecting the
9554 					 * flight size since this little guy
9555 					 * is a control only packet.
9556 					 */
9557 					sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
9558 					/*
9559 					 * do NOT clear the asconf flag as
9560 					 * it is used to do appropriate
9561 					 * source address selection.
9562 					 */
9563 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9564 					    (struct sockaddr *)&net->ro._l_addr,
9565 					    outchain, auth_offset, auth,
9566 					    stcb->asoc.authinfo.active_keyid,
9567 					    no_fragmentflg, 0, NULL, asconf,
9568 					    inp->sctp_lport, stcb->rport,
9569 					    htonl(stcb->asoc.peer_vtag),
9570 					    net->port, so_locked, NULL))) {
9571 						if (error == ENOBUFS) {
9572 							asoc->ifp_had_enobuf = 1;
9573 							SCTP_STAT_INCR(sctps_lowlevelerr);
9574 						}
9575 						if (from_where == 0) {
9576 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
9577 						}
9578 						if (*now_filled == 0) {
9579 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9580 							*now_filled = 1;
9581 							*now = net->last_sent_time;
9582 						} else {
9583 							net->last_sent_time = *now;
9584 						}
9585 						hbflag = 0;
9586 						/* error, could not output */
9587 						if (error == EHOSTUNREACH) {
9588 							/*
9589 							 * Destination went
9590 							 * unreachable
9591 							 * during this send
9592 							 */
9593 							sctp_move_to_an_alt(stcb, asoc, net);
9594 						}
9595 						*reason_code = 7;
9596 						continue;
9597 					} else
9598 						asoc->ifp_had_enobuf = 0;
9599 					if (*now_filled == 0) {
9600 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9601 						*now_filled = 1;
9602 						*now = net->last_sent_time;
9603 					} else {
9604 						net->last_sent_time = *now;
9605 					}
9606 					hbflag = 0;
9607 					/*
9608 					 * increase the number we sent, if a
9609 					 * cookie is sent we don't tell them
9610 					 * any was sent out.
9611 					 */
9612 					outchain = endoutchain = NULL;
9613 					auth = NULL;
9614 					auth_offset = 0;
9615 					if (!no_out_cnt)
9616 						*num_out += ctl_cnt;
9617 					/* recalc a clean slate and setup */
9618 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9619 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9620 					} else {
9621 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
9622 					}
9623 					to_out = 0;
9624 					no_fragmentflg = 1;
9625 				}
9626 			}
9627 		}
9628 		/************************/
9629 		/* Control transmission */
9630 		/************************/
9631 		/* Now first lets go through the control queue */
9632 		for (chk = TAILQ_FIRST(&asoc->control_send_queue);
9633 		    chk; chk = nchk) {
9634 			nchk = TAILQ_NEXT(chk, sctp_next);
9635 			if (chk->whoTo != net) {
9636 				/*
9637 				 * No, not sent to the network we are
9638 				 * looking at
9639 				 */
9640 				continue;
9641 			}
9642 			if (chk->data == NULL) {
9643 				continue;
9644 			}
9645 			if (chk->sent != SCTP_DATAGRAM_UNSENT) {
9646 				/*
9647 				 * It must be unsent. Cookies and ASCONF's
9648 				 * hang around but there timers will force
9649 				 * when marked for resend.
9650 				 */
9651 				continue;
9652 			}
9653 			/*
9654 			 * if no AUTH is yet included and this chunk
9655 			 * requires it, make sure to account for it.  We
9656 			 * don't apply the size until the AUTH chunk is
9657 			 * actually added below in case there is no room for
9658 			 * this chunk. NOTE: we overload the use of "omtu"
9659 			 * here
9660 			 */
9661 			if ((auth == NULL) &&
9662 			    sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9663 			    stcb->asoc.peer_auth_chunks)) {
9664 				omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9665 			} else
9666 				omtu = 0;
9667 			/* Here we do NOT factor the r_mtu */
9668 			if ((chk->send_size < (int)(mtu - omtu)) ||
9669 			    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9670 				/*
9671 				 * We probably should glom the mbuf chain
9672 				 * from the chk->data for control but the
9673 				 * problem is it becomes yet one more level
9674 				 * of tracking to do if for some reason
9675 				 * output fails. Then I have got to
9676 				 * reconstruct the merged control chain.. el
9677 				 * yucko.. for now we take the easy way and
9678 				 * do the copy
9679 				 */
9680 				/*
9681 				 * Add an AUTH chunk, if chunk requires it
9682 				 * save the offset into the chain for AUTH
9683 				 */
9684 				if ((auth == NULL) &&
9685 				    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9686 				    stcb->asoc.peer_auth_chunks))) {
9687 					outchain = sctp_add_auth_chunk(outchain,
9688 					    &endoutchain,
9689 					    &auth,
9690 					    &auth_offset,
9691 					    stcb,
9692 					    chk->rec.chunk_id.id);
9693 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9694 				}
9695 				outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
9696 				    (int)chk->rec.chunk_id.can_take_data,
9697 				    chk->send_size, chk->copy_by_ref);
9698 				if (outchain == NULL) {
9699 					*reason_code = 8;
9700 					SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9701 					return (ENOMEM);
9702 				}
9703 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9704 				/* update our MTU size */
9705 				if (mtu > (chk->send_size + omtu))
9706 					mtu -= (chk->send_size + omtu);
9707 				else
9708 					mtu = 0;
9709 				to_out += (chk->send_size + omtu);
9710 				/* Do clear IP_DF ? */
9711 				if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9712 					no_fragmentflg = 0;
9713 				}
9714 				if (chk->rec.chunk_id.can_take_data)
9715 					chk->data = NULL;
9716 				/* Mark things to be removed, if needed */
9717 				if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
9718 				    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||	/* EY */
9719 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
9720 				    (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
9721 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
9722 				    (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
9723 				    (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
9724 				    (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
9725 				    (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
9726 				    (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
9727 				    (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
9728 
9729 					if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
9730 						hbflag = 1;
9731 						/*
9732 						 * JRS 5/14/07 - Set the
9733 						 * flag to say a heartbeat
9734 						 * is being sent.
9735 						 */
9736 						pf_hbflag = 1;
9737 					}
9738 					/* remove these chunks at the end */
9739 					if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9740 						/* turn off the timer */
9741 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9742 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9743 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
9744 						}
9745 					}
9746 					/*
9747 					 * EY -Nr-sack version of the above
9748 					 * if statement
9749 					 */
9750 					if ((SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) &&
9751 					    (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {	/* EY !?! */
9752 						/* turn off the timer */
9753 						if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9754 							sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9755 							    inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
9756 						}
9757 					}
9758 					ctl_cnt++;
9759 				} else {
9760 					/*
9761 					 * Other chunks, since they have
9762 					 * timers running (i.e. COOKIE) we
9763 					 * just "trust" that it gets sent or
9764 					 * retransmitted.
9765 					 */
9766 					ctl_cnt++;
9767 					if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9768 						cookie = 1;
9769 						no_out_cnt = 1;
9770 					}
9771 					chk->sent = SCTP_DATAGRAM_SENT;
9772 					chk->snd_count++;
9773 				}
9774 				if (mtu == 0) {
9775 					/*
9776 					 * Ok we are out of room but we can
9777 					 * output without effecting the
9778 					 * flight size since this little guy
9779 					 * is a control only packet.
9780 					 */
9781 					if (asconf) {
9782 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
9783 						/*
9784 						 * do NOT clear the asconf
9785 						 * flag as it is used to do
9786 						 * appropriate source
9787 						 * address selection.
9788 						 */
9789 					}
9790 					if (cookie) {
9791 						sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
9792 						cookie = 0;
9793 					}
9794 					if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9795 					    (struct sockaddr *)&net->ro._l_addr,
9796 					    outchain,
9797 					    auth_offset, auth,
9798 					    stcb->asoc.authinfo.active_keyid,
9799 					    no_fragmentflg, 0, NULL, asconf,
9800 					    inp->sctp_lport, stcb->rport,
9801 					    htonl(stcb->asoc.peer_vtag),
9802 					    net->port, so_locked, NULL))) {
9803 						if (error == ENOBUFS) {
9804 							asoc->ifp_had_enobuf = 1;
9805 							SCTP_STAT_INCR(sctps_lowlevelerr);
9806 						}
9807 						if (from_where == 0) {
9808 							SCTP_STAT_INCR(sctps_lowlevelerrusr);
9809 						}
9810 						/* error, could not output */
9811 						if (hbflag) {
9812 							if (*now_filled == 0) {
9813 								(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9814 								*now_filled = 1;
9815 								*now = net->last_sent_time;
9816 							} else {
9817 								net->last_sent_time = *now;
9818 							}
9819 							hbflag = 0;
9820 						}
9821 						if (error == EHOSTUNREACH) {
9822 							/*
9823 							 * Destination went
9824 							 * unreachable
9825 							 * during this send
9826 							 */
9827 							sctp_move_to_an_alt(stcb, asoc, net);
9828 						}
9829 						*reason_code = 7;
9830 						continue;
9831 					} else
9832 						asoc->ifp_had_enobuf = 0;
9833 					/* Only HB or ASCONF advances time */
9834 					if (hbflag) {
9835 						if (*now_filled == 0) {
9836 							(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9837 							*now_filled = 1;
9838 							*now = net->last_sent_time;
9839 						} else {
9840 							net->last_sent_time = *now;
9841 						}
9842 						hbflag = 0;
9843 					}
9844 					/*
9845 					 * increase the number we sent, if a
9846 					 * cookie is sent we don't tell them
9847 					 * any was sent out.
9848 					 */
9849 					outchain = endoutchain = NULL;
9850 					auth = NULL;
9851 					auth_offset = 0;
9852 					if (!no_out_cnt)
9853 						*num_out += ctl_cnt;
9854 					/* recalc a clean slate and setup */
9855 					if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9856 						mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9857 					} else {
9858 						mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
9859 					}
9860 					to_out = 0;
9861 					no_fragmentflg = 1;
9862 				}
9863 			}
9864 		}
9865 		/*********************/
9866 		/* Data transmission */
9867 		/*********************/
9868 		/*
9869 		 * if AUTH for DATA is required and no AUTH has been added
9870 		 * yet, account for this in the mtu now... if no data can be
9871 		 * bundled, this adjustment won't matter anyways since the
9872 		 * packet will be going out...
9873 		 */
9874 		data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
9875 		    stcb->asoc.peer_auth_chunks);
9876 		if (data_auth_reqd && (auth == NULL)) {
9877 			mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9878 		}
9879 		/* now lets add any data within the MTU constraints */
9880 		switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
9881 		case AF_INET:
9882 			if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
9883 				omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
9884 			else
9885 				omtu = 0;
9886 			break;
9887 #ifdef INET6
9888 		case AF_INET6:
9889 			if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
9890 				omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
9891 			else
9892 				omtu = 0;
9893 			break;
9894 #endif
9895 		default:
9896 			/* TSNH */
9897 			omtu = 0;
9898 			break;
9899 		}
9900 		if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
9901 		    (cookie)) {
9902 			for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
9903 				if (no_data_chunks) {
9904 					/* let only control go out */
9905 					*reason_code = 1;
9906 					break;
9907 				}
9908 				if (net->flight_size >= net->cwnd) {
9909 					/* skip this net, no room for data */
9910 					*reason_code = 2;
9911 					break;
9912 				}
9913 				nchk = TAILQ_NEXT(chk, sctp_next);
9914 				if (chk->whoTo != net) {
9915 					/* No, not sent to this net */
9916 					continue;
9917 				}
9918 				if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
9919 					/*-
9920 					 * strange, we have a chunk that is
9921 					 * to big for its destination and
9922 					 * yet no fragment ok flag.
9923 					 * Something went wrong when the
9924 					 * PMTU changed...we did not mark
9925 					 * this chunk for some reason?? I
9926 					 * will fix it here by letting IP
9927 					 * fragment it for now and printing
9928 					 * a warning. This really should not
9929 					 * happen ...
9930 					 */
9931 					SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
9932 					    chk->send_size, mtu);
9933 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
9934 				}
9935 				if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
9936 				    ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
9937 					struct sctp_data_chunk *dchkh;
9938 
9939 					dchkh = mtod(chk->data, struct sctp_data_chunk *);
9940 					dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
9941 				}
9942 				if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
9943 				    ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
9944 					/* ok we will add this one */
9945 
9946 					/*
9947 					 * Add an AUTH chunk, if chunk
9948 					 * requires it, save the offset into
9949 					 * the chain for AUTH
9950 					 */
9951 					if (data_auth_reqd) {
9952 						if (auth == NULL) {
9953 							outchain = sctp_add_auth_chunk(outchain,
9954 							    &endoutchain,
9955 							    &auth,
9956 							    &auth_offset,
9957 							    stcb,
9958 							    SCTP_DATA);
9959 							auth_keyid = chk->auth_keyid;
9960 							override_ok = 0;
9961 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9962 						} else if (override_ok) {
9963 							/*
9964 							 * use this data's
9965 							 * keyid
9966 							 */
9967 							auth_keyid = chk->auth_keyid;
9968 							override_ok = 0;
9969 						} else if (auth_keyid != chk->auth_keyid) {
9970 							/*
9971 							 * different keyid,
9972 							 * so done bundling
9973 							 */
9974 							break;
9975 						}
9976 					}
9977 					outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
9978 					    chk->send_size, chk->copy_by_ref);
9979 					if (outchain == NULL) {
9980 						SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
9981 						if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9982 							sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9983 						}
9984 						*reason_code = 3;
9985 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9986 						return (ENOMEM);
9987 					}
9988 					/* upate our MTU size */
9989 					/* Do clear IP_DF ? */
9990 					if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9991 						no_fragmentflg = 0;
9992 					}
9993 					/* unsigned subtraction of mtu */
9994 					if (mtu > chk->send_size)
9995 						mtu -= chk->send_size;
9996 					else
9997 						mtu = 0;
9998 					/* unsigned subtraction of r_mtu */
9999 					if (r_mtu > chk->send_size)
10000 						r_mtu -= chk->send_size;
10001 					else
10002 						r_mtu = 0;
10003 
10004 					to_out += chk->send_size;
10005 					if ((to_out > mx_mtu) && no_fragmentflg) {
10006 #ifdef INVARIANTS
10007 						panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
10008 #else
10009 						SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
10010 						    mx_mtu, to_out);
10011 #endif
10012 					}
10013 					chk->window_probe = 0;
10014 					data_list[bundle_at++] = chk;
10015 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
10016 						mtu = 0;
10017 						break;
10018 					}
10019 					if (chk->sent == SCTP_DATAGRAM_UNSENT) {
10020 						if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
10021 							SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
10022 						} else {
10023 							SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
10024 						}
10025 						if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
10026 						    ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
10027 							/*
10028 							 * Count number of
10029 							 * user msg's that
10030 							 * were fragmented
10031 							 * we do this by
10032 							 * counting when we
10033 							 * see a LAST
10034 							 * fragment only.
10035 							 */
10036 							SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
10037 					}
10038 					if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
10039 						if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
10040 							data_list[0]->window_probe = 1;
10041 							net->window_probe = 1;
10042 						}
10043 						break;
10044 					}
10045 				} else {
10046 					/*
10047 					 * Must be sent in order of the
10048 					 * TSN's (on a network)
10049 					 */
10050 					break;
10051 				}
10052 			}	/* for (chunk gather loop for this net) */
10053 		}		/* if asoc.state OPEN */
10054 		/* Is there something to send for this destination? */
10055 		if (outchain) {
10056 			/* We may need to start a control timer or two */
10057 			if (asconf) {
10058 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
10059 				    stcb, net);
10060 				/*
10061 				 * do NOT clear the asconf flag as it is
10062 				 * used to do appropriate source address
10063 				 * selection.
10064 				 */
10065 			}
10066 			if (cookie) {
10067 				sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
10068 				cookie = 0;
10069 			}
10070 			/* must start a send timer if data is being sent */
10071 			if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
10072 				/*
10073 				 * no timer running on this destination
10074 				 * restart it.
10075 				 */
10076 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
10077 			} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
10078 				    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
10079 				    pf_hbflag &&
10080 				    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
10081 			    (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
10082 				/*
10083 				 * JRS 5/14/07 - If a HB has been sent to a
10084 				 * PF destination and no T3 timer is
10085 				 * currently running, start the T3 timer to
10086 				 * track the HBs that were sent.
10087 				 */
10088 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
10089 			}
10090 			/* Now send it, if there is anything to send :> */
10091 			if ((error = sctp_lowlevel_chunk_output(inp,
10092 			    stcb,
10093 			    net,
10094 			    (struct sockaddr *)&net->ro._l_addr,
10095 			    outchain,
10096 			    auth_offset,
10097 			    auth,
10098 			    auth_keyid,
10099 			    no_fragmentflg,
10100 			    bundle_at,
10101 			    data_list[0],
10102 			    asconf,
10103 			    inp->sctp_lport, stcb->rport,
10104 			    htonl(stcb->asoc.peer_vtag),
10105 			    net->port, so_locked, NULL))) {
10106 				/* error, we could not output */
10107 				if (error == ENOBUFS) {
10108 					SCTP_STAT_INCR(sctps_lowlevelerr);
10109 					asoc->ifp_had_enobuf = 1;
10110 				}
10111 				if (from_where == 0) {
10112 					SCTP_STAT_INCR(sctps_lowlevelerrusr);
10113 				}
10114 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10115 				if (hbflag) {
10116 					if (*now_filled == 0) {
10117 						(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
10118 						*now_filled = 1;
10119 						*now = net->last_sent_time;
10120 					} else {
10121 						net->last_sent_time = *now;
10122 					}
10123 					hbflag = 0;
10124 				}
10125 				if (error == EHOSTUNREACH) {
10126 					/*
10127 					 * Destination went unreachable
10128 					 * during this send
10129 					 */
10130 					sctp_move_to_an_alt(stcb, asoc, net);
10131 				}
10132 				*reason_code = 6;
10133 				/*-
10134 				 * I add this line to be paranoid. As far as
10135 				 * I can tell the continue, takes us back to
10136 				 * the top of the for, but just to make sure
10137 				 * I will reset these again here.
10138 				 */
10139 				ctl_cnt = bundle_at = 0;
10140 				continue;	/* This takes us back to the
10141 						 * for() for the nets. */
10142 			} else {
10143 				asoc->ifp_had_enobuf = 0;
10144 			}
10145 			outchain = endoutchain = NULL;
10146 			auth = NULL;
10147 			auth_offset = 0;
10148 			if (bundle_at || hbflag) {
10149 				/* For data/asconf and hb set time */
10150 				if (*now_filled == 0) {
10151 					(void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
10152 					*now_filled = 1;
10153 					*now = net->last_sent_time;
10154 				} else {
10155 					net->last_sent_time = *now;
10156 				}
10157 			}
10158 			if (!no_out_cnt) {
10159 				*num_out += (ctl_cnt + bundle_at);
10160 			}
10161 			if (bundle_at) {
10162 				/* setup for a RTO measurement */
10163 				tsns_sent = data_list[0]->rec.data.TSN_seq;
10164 				/* fill time if not already filled */
10165 				if (*now_filled == 0) {
10166 					(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
10167 					*now_filled = 1;
10168 					*now = asoc->time_last_sent;
10169 				} else {
10170 					asoc->time_last_sent = *now;
10171 				}
10172 				data_list[0]->do_rtt = 1;
10173 				SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
10174 				sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
10175 				if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
10176 					if (net->flight_size < net->cwnd) {
10177 						/* start or restart it */
10178 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
10179 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
10180 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
10181 						}
10182 						SCTP_STAT_INCR(sctps_earlyfrstrout);
10183 						sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
10184 					} else {
10185 						/* stop it if its running */
10186 						if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
10187 							SCTP_STAT_INCR(sctps_earlyfrstpout);
10188 							sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
10189 							    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
10190 						}
10191 					}
10192 				}
10193 			}
10194 			if (one_chunk) {
10195 				break;
10196 			}
10197 		}
10198 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10199 			sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
10200 		}
10201 	}
10202 	if (old_startat == NULL) {
10203 		old_startat = send_start_at;
10204 		send_start_at = TAILQ_FIRST(&asoc->nets);
10205 		if (old_startat)
10206 			goto again_one_more_time;
10207 	}
10208 	/*
10209 	 * At the end there should be no NON timed chunks hanging on this
10210 	 * queue.
10211 	 */
10212 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10213 		sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
10214 	}
10215 	if ((*num_out == 0) && (*reason_code == 0)) {
10216 		*reason_code = 4;
10217 	} else {
10218 		*reason_code = 5;
10219 	}
10220 	sctp_clean_up_ctl(stcb, asoc);
10221 	return (0);
10222 }
10223 
10224 void
10225 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
10226 {
10227 	/*-
10228 	 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
10229 	 * the control chunk queue.
10230 	 */
10231 	struct sctp_chunkhdr *hdr;
10232 	struct sctp_tmit_chunk *chk;
10233 	struct mbuf *mat;
10234 
10235 	SCTP_TCB_LOCK_ASSERT(stcb);
10236 	sctp_alloc_a_chunk(stcb, chk);
10237 	if (chk == NULL) {
10238 		/* no memory */
10239 		sctp_m_freem(op_err);
10240 		return;
10241 	}
10242 	chk->copy_by_ref = 0;
10243 	SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
10244 	if (op_err == NULL) {
10245 		sctp_free_a_chunk(stcb, chk);
10246 		return;
10247 	}
10248 	chk->send_size = 0;
10249 	mat = op_err;
10250 	while (mat != NULL) {
10251 		chk->send_size += SCTP_BUF_LEN(mat);
10252 		mat = SCTP_BUF_NEXT(mat);
10253 	}
10254 	chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
10255 	chk->rec.chunk_id.can_take_data = 1;
10256 	chk->sent = SCTP_DATAGRAM_UNSENT;
10257 	chk->snd_count = 0;
10258 	chk->flags = 0;
10259 	chk->asoc = &stcb->asoc;
10260 	chk->data = op_err;
10261 	chk->whoTo = chk->asoc->primary_destination;
10262 	atomic_add_int(&chk->whoTo->ref_count, 1);
10263 	hdr = mtod(op_err, struct sctp_chunkhdr *);
10264 	hdr->chunk_type = SCTP_OPERATION_ERROR;
10265 	hdr->chunk_flags = 0;
10266 	hdr->chunk_length = htons(chk->send_size);
10267 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
10268 	    chk,
10269 	    sctp_next);
10270 	chk->asoc->ctrl_queue_cnt++;
10271 }
10272 
10273 int
10274 sctp_send_cookie_echo(struct mbuf *m,
10275     int offset,
10276     struct sctp_tcb *stcb,
10277     struct sctp_nets *net)
10278 {
10279 	/*-
10280 	 * pull out the cookie and put it at the front of the control chunk
10281 	 * queue.
10282 	 */
10283 	int at;
10284 	struct mbuf *cookie;
10285 	struct sctp_paramhdr parm, *phdr;
10286 	struct sctp_chunkhdr *hdr;
10287 	struct sctp_tmit_chunk *chk;
10288 	uint16_t ptype, plen;
10289 
10290 	/* First find the cookie in the param area */
10291 	cookie = NULL;
10292 	at = offset + sizeof(struct sctp_init_chunk);
10293 
10294 	SCTP_TCB_LOCK_ASSERT(stcb);
10295 	do {
10296 		phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
10297 		if (phdr == NULL) {
10298 			return (-3);
10299 		}
10300 		ptype = ntohs(phdr->param_type);
10301 		plen = ntohs(phdr->param_length);
10302 		if (ptype == SCTP_STATE_COOKIE) {
10303 			int pad;
10304 
10305 			/* found the cookie */
10306 			if ((pad = (plen % 4))) {
10307 				plen += 4 - pad;
10308 			}
10309 			cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
10310 			if (cookie == NULL) {
10311 				/* No memory */
10312 				return (-2);
10313 			}
10314 #ifdef SCTP_MBUF_LOGGING
10315 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10316 				struct mbuf *mat;
10317 
10318 				mat = cookie;
10319 				while (mat) {
10320 					if (SCTP_BUF_IS_EXTENDED(mat)) {
10321 						sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10322 					}
10323 					mat = SCTP_BUF_NEXT(mat);
10324 				}
10325 			}
10326 #endif
10327 			break;
10328 		}
10329 		at += SCTP_SIZE32(plen);
10330 	} while (phdr);
10331 	if (cookie == NULL) {
10332 		/* Did not find the cookie */
10333 		return (-3);
10334 	}
10335 	/* ok, we got the cookie lets change it into a cookie echo chunk */
10336 
10337 	/* first the change from param to cookie */
10338 	hdr = mtod(cookie, struct sctp_chunkhdr *);
10339 	hdr->chunk_type = SCTP_COOKIE_ECHO;
10340 	hdr->chunk_flags = 0;
10341 	/* get the chunk stuff now and place it in the FRONT of the queue */
10342 	sctp_alloc_a_chunk(stcb, chk);
10343 	if (chk == NULL) {
10344 		/* no memory */
10345 		sctp_m_freem(cookie);
10346 		return (-5);
10347 	}
10348 	chk->copy_by_ref = 0;
10349 	chk->send_size = plen;
10350 	chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
10351 	chk->rec.chunk_id.can_take_data = 0;
10352 	chk->sent = SCTP_DATAGRAM_UNSENT;
10353 	chk->snd_count = 0;
10354 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
10355 	chk->asoc = &stcb->asoc;
10356 	chk->data = cookie;
10357 	chk->whoTo = chk->asoc->primary_destination;
10358 	atomic_add_int(&chk->whoTo->ref_count, 1);
10359 	TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
10360 	chk->asoc->ctrl_queue_cnt++;
10361 	return (0);
10362 }
10363 
10364 void
10365 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
10366     struct mbuf *m,
10367     int offset,
10368     int chk_length,
10369     struct sctp_nets *net)
10370 {
10371 	/*
10372 	 * take a HB request and make it into a HB ack and send it.
10373 	 */
10374 	struct mbuf *outchain;
10375 	struct sctp_chunkhdr *chdr;
10376 	struct sctp_tmit_chunk *chk;
10377 
10378 
10379 	if (net == NULL)
10380 		/* must have a net pointer */
10381 		return;
10382 
10383 	outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
10384 	if (outchain == NULL) {
10385 		/* gak out of memory */
10386 		return;
10387 	}
10388 #ifdef SCTP_MBUF_LOGGING
10389 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10390 		struct mbuf *mat;
10391 
10392 		mat = outchain;
10393 		while (mat) {
10394 			if (SCTP_BUF_IS_EXTENDED(mat)) {
10395 				sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10396 			}
10397 			mat = SCTP_BUF_NEXT(mat);
10398 		}
10399 	}
10400 #endif
10401 	chdr = mtod(outchain, struct sctp_chunkhdr *);
10402 	chdr->chunk_type = SCTP_HEARTBEAT_ACK;
10403 	chdr->chunk_flags = 0;
10404 	if (chk_length % 4) {
10405 		/* need pad */
10406 		uint32_t cpthis = 0;
10407 		int padlen;
10408 
10409 		padlen = 4 - (chk_length % 4);
10410 		m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
10411 	}
10412 	sctp_alloc_a_chunk(stcb, chk);
10413 	if (chk == NULL) {
10414 		/* no memory */
10415 		sctp_m_freem(outchain);
10416 		return;
10417 	}
10418 	chk->copy_by_ref = 0;
10419 	chk->send_size = chk_length;
10420 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
10421 	chk->rec.chunk_id.can_take_data = 1;
10422 	chk->sent = SCTP_DATAGRAM_UNSENT;
10423 	chk->snd_count = 0;
10424 	chk->flags = 0;
10425 	chk->asoc = &stcb->asoc;
10426 	chk->data = outchain;
10427 	chk->whoTo = net;
10428 	atomic_add_int(&chk->whoTo->ref_count, 1);
10429 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10430 	chk->asoc->ctrl_queue_cnt++;
10431 }
10432 
10433 void
10434 sctp_send_cookie_ack(struct sctp_tcb *stcb)
10435 {
10436 	/* formulate and queue a cookie-ack back to sender */
10437 	struct mbuf *cookie_ack;
10438 	struct sctp_chunkhdr *hdr;
10439 	struct sctp_tmit_chunk *chk;
10440 
10441 	cookie_ack = NULL;
10442 	SCTP_TCB_LOCK_ASSERT(stcb);
10443 
10444 	cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10445 	if (cookie_ack == NULL) {
10446 		/* no mbuf's */
10447 		return;
10448 	}
10449 	SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
10450 	sctp_alloc_a_chunk(stcb, chk);
10451 	if (chk == NULL) {
10452 		/* no memory */
10453 		sctp_m_freem(cookie_ack);
10454 		return;
10455 	}
10456 	chk->copy_by_ref = 0;
10457 	chk->send_size = sizeof(struct sctp_chunkhdr);
10458 	chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
10459 	chk->rec.chunk_id.can_take_data = 1;
10460 	chk->sent = SCTP_DATAGRAM_UNSENT;
10461 	chk->snd_count = 0;
10462 	chk->flags = 0;
10463 	chk->asoc = &stcb->asoc;
10464 	chk->data = cookie_ack;
10465 	if (chk->asoc->last_control_chunk_from != NULL) {
10466 		chk->whoTo = chk->asoc->last_control_chunk_from;
10467 	} else {
10468 		chk->whoTo = chk->asoc->primary_destination;
10469 	}
10470 	atomic_add_int(&chk->whoTo->ref_count, 1);
10471 	hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
10472 	hdr->chunk_type = SCTP_COOKIE_ACK;
10473 	hdr->chunk_flags = 0;
10474 	hdr->chunk_length = htons(chk->send_size);
10475 	SCTP_BUF_LEN(cookie_ack) = chk->send_size;
10476 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10477 	chk->asoc->ctrl_queue_cnt++;
10478 	return;
10479 }
10480 
10481 
10482 void
10483 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
10484 {
10485 	/* formulate and queue a SHUTDOWN-ACK back to the sender */
10486 	struct mbuf *m_shutdown_ack;
10487 	struct sctp_shutdown_ack_chunk *ack_cp;
10488 	struct sctp_tmit_chunk *chk;
10489 
10490 	m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10491 	if (m_shutdown_ack == NULL) {
10492 		/* no mbuf's */
10493 		return;
10494 	}
10495 	SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
10496 	sctp_alloc_a_chunk(stcb, chk);
10497 	if (chk == NULL) {
10498 		/* no memory */
10499 		sctp_m_freem(m_shutdown_ack);
10500 		return;
10501 	}
10502 	chk->copy_by_ref = 0;
10503 	chk->send_size = sizeof(struct sctp_chunkhdr);
10504 	chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
10505 	chk->rec.chunk_id.can_take_data = 1;
10506 	chk->sent = SCTP_DATAGRAM_UNSENT;
10507 	chk->snd_count = 0;
10508 	chk->flags = 0;
10509 	chk->asoc = &stcb->asoc;
10510 	chk->data = m_shutdown_ack;
10511 	chk->whoTo = net;
10512 	atomic_add_int(&net->ref_count, 1);
10513 
10514 	ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
10515 	ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
10516 	ack_cp->ch.chunk_flags = 0;
10517 	ack_cp->ch.chunk_length = htons(chk->send_size);
10518 	SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
10519 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10520 	chk->asoc->ctrl_queue_cnt++;
10521 	return;
10522 }
10523 
10524 void
10525 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
10526 {
10527 	/* formulate and queue a SHUTDOWN to the sender */
10528 	struct mbuf *m_shutdown;
10529 	struct sctp_shutdown_chunk *shutdown_cp;
10530 	struct sctp_tmit_chunk *chk;
10531 
10532 	m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10533 	if (m_shutdown == NULL) {
10534 		/* no mbuf's */
10535 		return;
10536 	}
10537 	SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
10538 	sctp_alloc_a_chunk(stcb, chk);
10539 	if (chk == NULL) {
10540 		/* no memory */
10541 		sctp_m_freem(m_shutdown);
10542 		return;
10543 	}
10544 	chk->copy_by_ref = 0;
10545 	chk->send_size = sizeof(struct sctp_shutdown_chunk);
10546 	chk->rec.chunk_id.id = SCTP_SHUTDOWN;
10547 	chk->rec.chunk_id.can_take_data = 1;
10548 	chk->sent = SCTP_DATAGRAM_UNSENT;
10549 	chk->snd_count = 0;
10550 	chk->flags = 0;
10551 	chk->asoc = &stcb->asoc;
10552 	chk->data = m_shutdown;
10553 	chk->whoTo = net;
10554 	atomic_add_int(&net->ref_count, 1);
10555 
10556 	shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
10557 	shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
10558 	shutdown_cp->ch.chunk_flags = 0;
10559 	shutdown_cp->ch.chunk_length = htons(chk->send_size);
10560 	shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
10561 	SCTP_BUF_LEN(m_shutdown) = chk->send_size;
10562 	TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10563 	chk->asoc->ctrl_queue_cnt++;
10564 	return;
10565 }
10566 
10567 void
10568 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
10569 {
10570 	/*
10571 	 * formulate and queue an ASCONF to the peer. ASCONF parameters
10572 	 * should be queued on the assoc queue.
10573 	 */
10574 	struct sctp_tmit_chunk *chk;
10575 	struct mbuf *m_asconf;
10576 	int len;
10577 
10578 	SCTP_TCB_LOCK_ASSERT(stcb);
10579 
10580 	if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
10581 	    (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
10582 		/* can't send a new one if there is one in flight already */
10583 		return;
10584 	}
10585 	/* compose an ASCONF chunk, maximum length is PMTU */
10586 	m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
10587 	if (m_asconf == NULL) {
10588 		return;
10589 	}
10590 	sctp_alloc_a_chunk(stcb, chk);
10591 	if (chk == NULL) {
10592 		/* no memory */
10593 		sctp_m_freem(m_asconf);
10594 		return;
10595 	}
10596 	chk->copy_by_ref = 0;
10597 	chk->data = m_asconf;
10598 	chk->send_size = len;
10599 	chk->rec.chunk_id.id = SCTP_ASCONF;
10600 	chk->rec.chunk_id.can_take_data = 0;
10601 	chk->sent = SCTP_DATAGRAM_UNSENT;
10602 	chk->snd_count = 0;
10603 	chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
10604 	chk->asoc = &stcb->asoc;
10605 	chk->whoTo = net;
10606 	atomic_add_int(&chk->whoTo->ref_count, 1);
10607 	TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
10608 	chk->asoc->ctrl_queue_cnt++;
10609 	return;
10610 }
10611 
10612 void
10613 sctp_send_asconf_ack(struct sctp_tcb *stcb)
10614 {
10615 	/*
10616 	 * formulate and queue a asconf-ack back to sender. the asconf-ack
10617 	 * must be stored in the tcb.
10618 	 */
10619 	struct sctp_tmit_chunk *chk;
10620 	struct sctp_asconf_ack *ack, *latest_ack;
10621 	struct mbuf *m_ack, *m;
10622 	struct sctp_nets *net = NULL;
10623 
10624 	SCTP_TCB_LOCK_ASSERT(stcb);
10625 	/* Get the latest ASCONF-ACK */
10626 	latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
10627 	if (latest_ack == NULL) {
10628 		return;
10629 	}
10630 	if (latest_ack->last_sent_to != NULL &&
10631 	    latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
10632 		/* we're doing a retransmission */
10633 		net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
10634 		if (net == NULL) {
10635 			/* no alternate */
10636 			if (stcb->asoc.last_control_chunk_from == NULL)
10637 				net = stcb->asoc.primary_destination;
10638 			else
10639 				net = stcb->asoc.last_control_chunk_from;
10640 		}
10641 	} else {
10642 		/* normal case */
10643 		if (stcb->asoc.last_control_chunk_from == NULL)
10644 			net = stcb->asoc.primary_destination;
10645 		else
10646 			net = stcb->asoc.last_control_chunk_from;
10647 	}
10648 	latest_ack->last_sent_to = net;
10649 
10650 	TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
10651 		if (ack->data == NULL) {
10652 			continue;
10653 		}
10654 		/* copy the asconf_ack */
10655 		m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
10656 		if (m_ack == NULL) {
10657 			/* couldn't copy it */
10658 			return;
10659 		}
10660 #ifdef SCTP_MBUF_LOGGING
10661 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10662 			struct mbuf *mat;
10663 
10664 			mat = m_ack;
10665 			while (mat) {
10666 				if (SCTP_BUF_IS_EXTENDED(mat)) {
10667 					sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10668 				}
10669 				mat = SCTP_BUF_NEXT(mat);
10670 			}
10671 		}
10672 #endif
10673 
10674 		sctp_alloc_a_chunk(stcb, chk);
10675 		if (chk == NULL) {
10676 			/* no memory */
10677 			if (m_ack)
10678 				sctp_m_freem(m_ack);
10679 			return;
10680 		}
10681 		chk->copy_by_ref = 0;
10682 
10683 		chk->whoTo = net;
10684 		chk->data = m_ack;
10685 		chk->send_size = 0;
10686 		/* Get size */
10687 		m = m_ack;
10688 		chk->send_size = ack->len;
10689 		chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
10690 		chk->rec.chunk_id.can_take_data = 1;
10691 		chk->sent = SCTP_DATAGRAM_UNSENT;
10692 		chk->snd_count = 0;
10693 		chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;	/* XXX */
10694 		chk->asoc = &stcb->asoc;
10695 		atomic_add_int(&chk->whoTo->ref_count, 1);
10696 
10697 		TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10698 		chk->asoc->ctrl_queue_cnt++;
10699 	}
10700 	return;
10701 }
10702 
10703 
10704 static int
10705 sctp_chunk_retransmission(struct sctp_inpcb *inp,
10706     struct sctp_tcb *stcb,
10707     struct sctp_association *asoc,
10708     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
10709 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10710     SCTP_UNUSED
10711 #endif
10712 )
10713 {
10714 	/*-
10715 	 * send out one MTU of retransmission. If fast_retransmit is
10716 	 * happening we ignore the cwnd. Otherwise we obey the cwnd and
10717 	 * rwnd. For a Cookie or Asconf in the control chunk queue we
10718 	 * retransmit them by themselves.
10719 	 *
10720 	 * For data chunks we will pick out the lowest TSN's in the sent_queue
10721 	 * marked for resend and bundle them all together (up to a MTU of
10722 	 * destination). The address to send to should have been
10723 	 * selected/changed where the retransmission was marked (i.e. in FR
10724 	 * or t3-timeout routines).
10725 	 */
10726 	struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
10727 	struct sctp_tmit_chunk *chk, *fwd;
10728 	struct mbuf *m, *endofchain;
10729 	struct sctp_nets *net = NULL;
10730 	uint32_t tsns_sent = 0;
10731 	int no_fragmentflg, bundle_at, cnt_thru;
10732 	unsigned int mtu;
10733 	int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
10734 	struct sctp_auth_chunk *auth = NULL;
10735 	uint32_t auth_offset = 0;
10736 	uint16_t auth_keyid;
10737 	int override_ok = 1;
10738 	int data_auth_reqd = 0;
10739 	uint32_t dmtu = 0;
10740 
10741 	SCTP_TCB_LOCK_ASSERT(stcb);
10742 	tmr_started = ctl_cnt = bundle_at = error = 0;
10743 	no_fragmentflg = 1;
10744 	fwd_tsn = 0;
10745 	*cnt_out = 0;
10746 	fwd = NULL;
10747 	endofchain = m = NULL;
10748 	auth_keyid = stcb->asoc.authinfo.active_keyid;
10749 #ifdef SCTP_AUDITING_ENABLED
10750 	sctp_audit_log(0xC3, 1);
10751 #endif
10752 	if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
10753 	    (TAILQ_EMPTY(&asoc->control_send_queue))) {
10754 		SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
10755 		    asoc->sent_queue_retran_cnt);
10756 		asoc->sent_queue_cnt = 0;
10757 		asoc->sent_queue_cnt_removeable = 0;
10758 		/* send back 0/0 so we enter normal transmission */
10759 		*cnt_out = 0;
10760 		return (0);
10761 	}
10762 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10763 		if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
10764 		    (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
10765 		    (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
10766 			if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
10767 				if (chk != asoc->str_reset) {
10768 					/*
10769 					 * not eligible for retran if its
10770 					 * not ours
10771 					 */
10772 					continue;
10773 				}
10774 			}
10775 			ctl_cnt++;
10776 			if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10777 				fwd_tsn = 1;
10778 				fwd = chk;
10779 			}
10780 			/*
10781 			 * Add an AUTH chunk, if chunk requires it save the
10782 			 * offset into the chain for AUTH
10783 			 */
10784 			if ((auth == NULL) &&
10785 			    (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
10786 			    stcb->asoc.peer_auth_chunks))) {
10787 				m = sctp_add_auth_chunk(m, &endofchain,
10788 				    &auth, &auth_offset,
10789 				    stcb,
10790 				    chk->rec.chunk_id.id);
10791 				SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10792 			}
10793 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
10794 			break;
10795 		}
10796 	}
10797 	one_chunk = 0;
10798 	cnt_thru = 0;
10799 	/* do we have control chunks to retransmit? */
10800 	if (m != NULL) {
10801 		/* Start a timer no matter if we suceed or fail */
10802 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
10803 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
10804 		} else if (chk->rec.chunk_id.id == SCTP_ASCONF)
10805 			sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
10806 		chk->snd_count++;	/* update our count */
10807 		if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
10808 		    (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
10809 		    auth_offset, auth, stcb->asoc.authinfo.active_keyid,
10810 		    no_fragmentflg, 0, NULL, 0,
10811 		    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10812 		    chk->whoTo->port, so_locked, NULL))) {
10813 			SCTP_STAT_INCR(sctps_lowlevelerr);
10814 			return (error);
10815 		}
10816 		m = endofchain = NULL;
10817 		auth = NULL;
10818 		auth_offset = 0;
10819 		/*
10820 		 * We don't want to mark the net->sent time here since this
10821 		 * we use this for HB and retrans cannot measure RTT
10822 		 */
10823 		/* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
10824 		*cnt_out += 1;
10825 		chk->sent = SCTP_DATAGRAM_SENT;
10826 		sctp_ucount_decr(asoc->sent_queue_retran_cnt);
10827 		if (fwd_tsn == 0) {
10828 			return (0);
10829 		} else {
10830 			/* Clean up the fwd-tsn list */
10831 			sctp_clean_up_ctl(stcb, asoc);
10832 			return (0);
10833 		}
10834 	}
10835 	/*
10836 	 * Ok, it is just data retransmission we need to do or that and a
10837 	 * fwd-tsn with it all.
10838 	 */
10839 	if (TAILQ_EMPTY(&asoc->sent_queue)) {
10840 		return (SCTP_RETRAN_DONE);
10841 	}
10842 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
10843 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
10844 		/* not yet open, resend the cookie and that is it */
10845 		return (1);
10846 	}
10847 #ifdef SCTP_AUDITING_ENABLED
10848 	sctp_auditing(20, inp, stcb, NULL);
10849 #endif
10850 	data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
10851 	TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
10852 		if (chk->sent != SCTP_DATAGRAM_RESEND) {
10853 			/* No, not sent to this net or not ready for rtx */
10854 			continue;
10855 		}
10856 		if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
10857 		    (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
10858 			/* Gak, we have exceeded max unlucky retran, abort! */
10859 			SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
10860 			    chk->snd_count,
10861 			    SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
10862 			atomic_add_int(&stcb->asoc.refcnt, 1);
10863 			sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
10864 			SCTP_TCB_LOCK(stcb);
10865 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
10866 			return (SCTP_RETRAN_EXIT);
10867 		}
10868 		/* pick up the net */
10869 		net = chk->whoTo;
10870 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10871 			mtu = (net->mtu - SCTP_MIN_OVERHEAD);
10872 		} else {
10873 			mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
10874 		}
10875 
10876 		if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
10877 			/* No room in peers rwnd */
10878 			uint32_t tsn;
10879 
10880 			tsn = asoc->last_acked_seq + 1;
10881 			if (tsn == chk->rec.data.TSN_seq) {
10882 				/*
10883 				 * we make a special exception for this
10884 				 * case. The peer has no rwnd but is missing
10885 				 * the lowest chunk.. which is probably what
10886 				 * is holding up the rwnd.
10887 				 */
10888 				goto one_chunk_around;
10889 			}
10890 			return (1);
10891 		}
10892 one_chunk_around:
10893 		if (asoc->peers_rwnd < mtu) {
10894 			one_chunk = 1;
10895 			if ((asoc->peers_rwnd == 0) &&
10896 			    (asoc->total_flight == 0)) {
10897 				chk->window_probe = 1;
10898 				chk->whoTo->window_probe = 1;
10899 			}
10900 		}
10901 #ifdef SCTP_AUDITING_ENABLED
10902 		sctp_audit_log(0xC3, 2);
10903 #endif
10904 		bundle_at = 0;
10905 		m = NULL;
10906 		net->fast_retran_ip = 0;
10907 		if (chk->rec.data.doing_fast_retransmit == 0) {
10908 			/*
10909 			 * if no FR in progress skip destination that have
10910 			 * flight_size > cwnd.
10911 			 */
10912 			if (net->flight_size >= net->cwnd) {
10913 				continue;
10914 			}
10915 		} else {
10916 			/*
10917 			 * Mark the destination net to have FR recovery
10918 			 * limits put on it.
10919 			 */
10920 			*fr_done = 1;
10921 			net->fast_retran_ip = 1;
10922 		}
10923 
10924 		/*
10925 		 * if no AUTH is yet included and this chunk requires it,
10926 		 * make sure to account for it.  We don't apply the size
10927 		 * until the AUTH chunk is actually added below in case
10928 		 * there is no room for this chunk.
10929 		 */
10930 		if (data_auth_reqd && (auth == NULL)) {
10931 			dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
10932 		} else
10933 			dmtu = 0;
10934 
10935 		if ((chk->send_size <= (mtu - dmtu)) ||
10936 		    (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
10937 			/* ok we will add this one */
10938 			if (data_auth_reqd) {
10939 				if (auth == NULL) {
10940 					m = sctp_add_auth_chunk(m,
10941 					    &endofchain,
10942 					    &auth,
10943 					    &auth_offset,
10944 					    stcb,
10945 					    SCTP_DATA);
10946 					auth_keyid = chk->auth_keyid;
10947 					override_ok = 0;
10948 					SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10949 				} else if (override_ok) {
10950 					auth_keyid = chk->auth_keyid;
10951 					override_ok = 0;
10952 				} else if (chk->auth_keyid != auth_keyid) {
10953 					/* different keyid, so done bundling */
10954 					break;
10955 				}
10956 			}
10957 			m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
10958 			if (m == NULL) {
10959 				SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10960 				return (ENOMEM);
10961 			}
10962 			/* Do clear IP_DF ? */
10963 			if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
10964 				no_fragmentflg = 0;
10965 			}
10966 			/* upate our MTU size */
10967 			if (mtu > (chk->send_size + dmtu))
10968 				mtu -= (chk->send_size + dmtu);
10969 			else
10970 				mtu = 0;
10971 			data_list[bundle_at++] = chk;
10972 			if (one_chunk && (asoc->total_flight <= 0)) {
10973 				SCTP_STAT_INCR(sctps_windowprobed);
10974 			}
10975 		}
10976 		if (one_chunk == 0) {
10977 			/*
10978 			 * now are there anymore forward from chk to pick
10979 			 * up?
10980 			 */
10981 			fwd = TAILQ_NEXT(chk, sctp_next);
10982 			while (fwd) {
10983 				if (fwd->sent != SCTP_DATAGRAM_RESEND) {
10984 					/* Nope, not for retran */
10985 					fwd = TAILQ_NEXT(fwd, sctp_next);
10986 					continue;
10987 				}
10988 				if (fwd->whoTo != net) {
10989 					/* Nope, not the net in question */
10990 					fwd = TAILQ_NEXT(fwd, sctp_next);
10991 					continue;
10992 				}
10993 				if (data_auth_reqd && (auth == NULL)) {
10994 					dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
10995 				} else
10996 					dmtu = 0;
10997 				if (fwd->send_size <= (mtu - dmtu)) {
10998 					if (data_auth_reqd) {
10999 						if (auth == NULL) {
11000 							m = sctp_add_auth_chunk(m,
11001 							    &endofchain,
11002 							    &auth,
11003 							    &auth_offset,
11004 							    stcb,
11005 							    SCTP_DATA);
11006 							auth_keyid = fwd->auth_keyid;
11007 							override_ok = 0;
11008 							SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11009 						} else if (override_ok) {
11010 							auth_keyid = fwd->auth_keyid;
11011 							override_ok = 0;
11012 						} else if (fwd->auth_keyid != auth_keyid) {
11013 							/*
11014 							 * different keyid,
11015 							 * so done bundling
11016 							 */
11017 							break;
11018 						}
11019 					}
11020 					m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
11021 					if (m == NULL) {
11022 						SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11023 						return (ENOMEM);
11024 					}
11025 					/* Do clear IP_DF ? */
11026 					if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
11027 						no_fragmentflg = 0;
11028 					}
11029 					/* upate our MTU size */
11030 					if (mtu > (fwd->send_size + dmtu))
11031 						mtu -= (fwd->send_size + dmtu);
11032 					else
11033 						mtu = 0;
11034 					data_list[bundle_at++] = fwd;
11035 					if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
11036 						break;
11037 					}
11038 					fwd = TAILQ_NEXT(fwd, sctp_next);
11039 				} else {
11040 					/* can't fit so we are done */
11041 					break;
11042 				}
11043 			}
11044 		}
11045 		/* Is there something to send for this destination? */
11046 		if (m) {
11047 			/*
11048 			 * No matter if we fail/or suceed we should start a
11049 			 * timer. A failure is like a lost IP packet :-)
11050 			 */
11051 			if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
11052 				/*
11053 				 * no timer running on this destination
11054 				 * restart it.
11055 				 */
11056 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
11057 				tmr_started = 1;
11058 			}
11059 			/* Now lets send it, if there is anything to send :> */
11060 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
11061 			    (struct sockaddr *)&net->ro._l_addr, m,
11062 			    auth_offset, auth, auth_keyid,
11063 			    no_fragmentflg, 0, NULL, 0,
11064 			    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
11065 			    net->port, so_locked, NULL))) {
11066 				/* error, we could not output */
11067 				SCTP_STAT_INCR(sctps_lowlevelerr);
11068 				return (error);
11069 			}
11070 			m = endofchain = NULL;
11071 			auth = NULL;
11072 			auth_offset = 0;
11073 			/* For HB's */
11074 			/*
11075 			 * We don't want to mark the net->sent time here
11076 			 * since this we use this for HB and retrans cannot
11077 			 * measure RTT
11078 			 */
11079 			/* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
11080 
11081 			/* For auto-close */
11082 			cnt_thru++;
11083 			if (*now_filled == 0) {
11084 				(void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
11085 				*now = asoc->time_last_sent;
11086 				*now_filled = 1;
11087 			} else {
11088 				asoc->time_last_sent = *now;
11089 			}
11090 			*cnt_out += bundle_at;
11091 #ifdef SCTP_AUDITING_ENABLED
11092 			sctp_audit_log(0xC4, bundle_at);
11093 #endif
11094 			if (bundle_at) {
11095 				tsns_sent = data_list[0]->rec.data.TSN_seq;
11096 			}
11097 			for (i = 0; i < bundle_at; i++) {
11098 				SCTP_STAT_INCR(sctps_sendretransdata);
11099 				data_list[i]->sent = SCTP_DATAGRAM_SENT;
11100 				/*
11101 				 * When we have a revoked data, and we
11102 				 * retransmit it, then we clear the revoked
11103 				 * flag since this flag dictates if we
11104 				 * subtracted from the fs
11105 				 */
11106 				if (data_list[i]->rec.data.chunk_was_revoked) {
11107 					/* Deflate the cwnd */
11108 					data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
11109 					data_list[i]->rec.data.chunk_was_revoked = 0;
11110 				}
11111 				data_list[i]->snd_count++;
11112 				sctp_ucount_decr(asoc->sent_queue_retran_cnt);
11113 				/* record the time */
11114 				data_list[i]->sent_rcv_time = asoc->time_last_sent;
11115 				if (data_list[i]->book_size_scale) {
11116 					/*
11117 					 * need to double the book size on
11118 					 * this one
11119 					 */
11120 					data_list[i]->book_size_scale = 0;
11121 					/*
11122 					 * Since we double the booksize, we
11123 					 * must also double the output queue
11124 					 * size, since this get shrunk when
11125 					 * we free by this amount.
11126 					 */
11127 					atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
11128 					data_list[i]->book_size *= 2;
11129 
11130 
11131 				} else {
11132 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
11133 						sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
11134 						    asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
11135 					}
11136 					asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
11137 					    (uint32_t) (data_list[i]->send_size +
11138 					    SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
11139 				}
11140 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
11141 					sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
11142 					    data_list[i]->whoTo->flight_size,
11143 					    data_list[i]->book_size,
11144 					    (uintptr_t) data_list[i]->whoTo,
11145 					    data_list[i]->rec.data.TSN_seq);
11146 				}
11147 				sctp_flight_size_increase(data_list[i]);
11148 				sctp_total_flight_increase(stcb, data_list[i]);
11149 				if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
11150 					/* SWS sender side engages */
11151 					asoc->peers_rwnd = 0;
11152 				}
11153 				if ((i == 0) &&
11154 				    (data_list[i]->rec.data.doing_fast_retransmit)) {
11155 					SCTP_STAT_INCR(sctps_sendfastretrans);
11156 					if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
11157 					    (tmr_started == 0)) {
11158 						/*-
11159 						 * ok we just fast-retrans'd
11160 						 * the lowest TSN, i.e the
11161 						 * first on the list. In
11162 						 * this case we want to give
11163 						 * some more time to get a
11164 						 * SACK back without a
11165 						 * t3-expiring.
11166 						 */
11167 						sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
11168 						    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
11169 						sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
11170 					}
11171 				}
11172 			}
11173 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11174 				sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
11175 			}
11176 #ifdef SCTP_AUDITING_ENABLED
11177 			sctp_auditing(21, inp, stcb, NULL);
11178 #endif
11179 		} else {
11180 			/* None will fit */
11181 			return (1);
11182 		}
11183 		if (asoc->sent_queue_retran_cnt <= 0) {
11184 			/* all done we have no more to retran */
11185 			asoc->sent_queue_retran_cnt = 0;
11186 			break;
11187 		}
11188 		if (one_chunk) {
11189 			/* No more room in rwnd */
11190 			return (1);
11191 		}
11192 		/* stop the for loop here. we sent out a packet */
11193 		break;
11194 	}
11195 	return (0);
11196 }
11197 
11198 
11199 static int
11200 sctp_timer_validation(struct sctp_inpcb *inp,
11201     struct sctp_tcb *stcb,
11202     struct sctp_association *asoc,
11203     int ret)
11204 {
11205 	struct sctp_nets *net;
11206 
11207 	/* Validate that a timer is running somewhere */
11208 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
11209 		if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
11210 			/* Here is a timer */
11211 			return (ret);
11212 		}
11213 	}
11214 	SCTP_TCB_LOCK_ASSERT(stcb);
11215 	/* Gak, we did not have a timer somewhere */
11216 	SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
11217 	sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
11218 	return (ret);
11219 }
11220 
11221 void
11222 sctp_chunk_output(struct sctp_inpcb *inp,
11223     struct sctp_tcb *stcb,
11224     int from_where,
11225     int so_locked
11226 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
11227     SCTP_UNUSED
11228 #endif
11229 )
11230 {
11231 	/*-
11232 	 * Ok this is the generic chunk service queue. we must do the
11233 	 * following:
11234 	 * - See if there are retransmits pending, if so we must
11235 	 *   do these first.
11236 	 * - Service the stream queue that is next, moving any
11237 	 *   message (note I must get a complete message i.e.
11238 	 *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
11239 	 *   TSN's
11240 	 * - Check to see if the cwnd/rwnd allows any output, if so we
11241 	 *   go ahead and fomulate and send the low level chunks. Making sure
11242 	 *   to combine any control in the control chunk queue also.
11243 	 */
11244 	struct sctp_association *asoc;
11245 	struct sctp_nets *net;
11246 	int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
11247 	    burst_cnt = 0, burst_limit = 0;
11248 	struct timeval now;
11249 	int now_filled = 0;
11250 	int cwnd_full = 0;
11251 	int nagle_on = 0;
11252 	int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
11253 	int un_sent = 0;
11254 	int fr_done, tot_frs = 0;
11255 
11256 	asoc = &stcb->asoc;
11257 	if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
11258 		if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
11259 			nagle_on = 0;
11260 		} else {
11261 			nagle_on = 1;
11262 		}
11263 	}
11264 	SCTP_TCB_LOCK_ASSERT(stcb);
11265 
11266 	un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
11267 
11268 	if ((un_sent <= 0) &&
11269 	    (TAILQ_EMPTY(&asoc->control_send_queue)) &&
11270 	    (asoc->sent_queue_retran_cnt == 0)) {
11271 		/* Nothing to do unless there is something to be sent left */
11272 		return;
11273 	}
11274 	/*
11275 	 * Do we have something to send, data or control AND a sack timer
11276 	 * running, if so piggy-back the sack.
11277 	 */
11278 	if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
11279 		/*
11280 		 * EY if nr_sacks used then send an nr-sack , a sack
11281 		 * otherwise
11282 		 */
11283 		if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
11284 			sctp_send_nr_sack(stcb);
11285 		else
11286 			sctp_send_sack(stcb);
11287 		(void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
11288 	}
11289 	while (asoc->sent_queue_retran_cnt) {
11290 		/*-
11291 		 * Ok, it is retransmission time only, we send out only ONE
11292 		 * packet with a single call off to the retran code.
11293 		 */
11294 		if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
11295 			/*-
11296 			 * Special hook for handling cookiess discarded
11297 			 * by peer that carried data. Send cookie-ack only
11298 			 * and then the next call with get the retran's.
11299 			 */
11300 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
11301 			    &cwnd_full, from_where,
11302 			    &now, &now_filled, frag_point, so_locked);
11303 			return;
11304 		} else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
11305 			/* if its not from a HB then do it */
11306 			fr_done = 0;
11307 			ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
11308 			if (fr_done) {
11309 				tot_frs++;
11310 			}
11311 		} else {
11312 			/*
11313 			 * its from any other place, we don't allow retran
11314 			 * output (only control)
11315 			 */
11316 			ret = 1;
11317 		}
11318 		if (ret > 0) {
11319 			/* Can't send anymore */
11320 			/*-
11321 			 * now lets push out control by calling med-level
11322 			 * output once. this assures that we WILL send HB's
11323 			 * if queued too.
11324 			 */
11325 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
11326 			    &cwnd_full, from_where,
11327 			    &now, &now_filled, frag_point, so_locked);
11328 #ifdef SCTP_AUDITING_ENABLED
11329 			sctp_auditing(8, inp, stcb, NULL);
11330 #endif
11331 			(void)sctp_timer_validation(inp, stcb, asoc, ret);
11332 			return;
11333 		}
11334 		if (ret < 0) {
11335 			/*-
11336 			 * The count was off.. retran is not happening so do
11337 			 * the normal retransmission.
11338 			 */
11339 #ifdef SCTP_AUDITING_ENABLED
11340 			sctp_auditing(9, inp, stcb, NULL);
11341 #endif
11342 			if (ret == SCTP_RETRAN_EXIT) {
11343 				return;
11344 			}
11345 			break;
11346 		}
11347 		if (from_where == SCTP_OUTPUT_FROM_T3) {
11348 			/* Only one transmission allowed out of a timeout */
11349 #ifdef SCTP_AUDITING_ENABLED
11350 			sctp_auditing(10, inp, stcb, NULL);
11351 #endif
11352 			/* Push out any control */
11353 			(void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
11354 			    &now, &now_filled, frag_point, so_locked);
11355 			return;
11356 		}
11357 		if (tot_frs > asoc->max_burst) {
11358 			/* Hit FR burst limit */
11359 			return;
11360 		}
11361 		if ((num_out == 0) && (ret == 0)) {
11362 
11363 			/* No more retrans to send */
11364 			break;
11365 		}
11366 	}
11367 #ifdef SCTP_AUDITING_ENABLED
11368 	sctp_auditing(12, inp, stcb, NULL);
11369 #endif
11370 	/* Check for bad destinations, if they exist move chunks around. */
11371 	burst_limit = asoc->max_burst;
11372 	TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
11373 		if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
11374 		    SCTP_ADDR_NOT_REACHABLE) {
11375 			/*-
11376 			 * if possible move things off of this address we
11377 			 * still may send below due to the dormant state but
11378 			 * we try to find an alternate address to send to
11379 			 * and if we have one we move all queued data on the
11380 			 * out wheel to this alternate address.
11381 			 */
11382 			if (net->ref_count > 1)
11383 				sctp_move_to_an_alt(stcb, asoc, net);
11384 		} else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
11385 			    SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
11386 		    ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
11387 			/*
11388 			 * JRS 5/14/07 - If CMT PF is on and the current
11389 			 * destination is in PF state, move all queued data
11390 			 * to an alternate desination.
11391 			 */
11392 			if (net->ref_count > 1)
11393 				sctp_move_to_an_alt(stcb, asoc, net);
11394 		} else {
11395 			/*-
11396 			 * if ((asoc->sat_network) || (net->addr_is_local))
11397 			 * { burst_limit = asoc->max_burst *
11398 			 * SCTP_SAT_NETWORK_BURST_INCR; }
11399 			 */
11400 			if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
11401 				if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
11402 					/*
11403 					 * JRS - Use the congestion control
11404 					 * given in the congestion control
11405 					 * module
11406 					 */
11407 					asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
11408 					if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11409 						sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
11410 					}
11411 					SCTP_STAT_INCR(sctps_maxburstqueued);
11412 				}
11413 				net->fast_retran_ip = 0;
11414 			} else {
11415 				if (net->flight_size == 0) {
11416 					/* Should be decaying the cwnd here */
11417 					;
11418 				}
11419 			}
11420 		}
11421 
11422 	}
11423 	burst_cnt = 0;
11424 	cwnd_full = 0;
11425 	do {
11426 		error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
11427 		    &reason_code, 0, &cwnd_full, from_where,
11428 		    &now, &now_filled, frag_point, so_locked);
11429 		if (error) {
11430 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
11431 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11432 				sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
11433 			}
11434 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11435 				sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
11436 				sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
11437 			}
11438 			break;
11439 		}
11440 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
11441 
11442 		tot_out += num_out;
11443 		burst_cnt++;
11444 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11445 			sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
11446 			if (num_out == 0) {
11447 				sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
11448 			}
11449 		}
11450 		if (nagle_on) {
11451 			/*-
11452 			 * When nagle is on, we look at how much is un_sent, then
11453 			 * if its smaller than an MTU and we have data in
11454 			 * flight we stop.
11455 			 */
11456 			un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11457 			    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
11458 			if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
11459 			    (stcb->asoc.total_flight > 0)) {
11460 				break;
11461 			}
11462 		}
11463 		if (TAILQ_EMPTY(&asoc->control_send_queue) &&
11464 		    TAILQ_EMPTY(&asoc->send_queue) &&
11465 		    TAILQ_EMPTY(&asoc->out_wheel)) {
11466 			/* Nothing left to send */
11467 			break;
11468 		}
11469 		if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
11470 			/* Nothing left to send */
11471 			break;
11472 		}
11473 	} while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
11474 	    (burst_cnt < burst_limit)));
11475 
11476 	if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
11477 		if (burst_cnt >= burst_limit) {
11478 			SCTP_STAT_INCR(sctps_maxburstqueued);
11479 			asoc->burst_limit_applied = 1;
11480 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11481 				sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
11482 			}
11483 		} else {
11484 			asoc->burst_limit_applied = 0;
11485 		}
11486 	}
11487 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11488 		sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
11489 	}
11490 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
11491 	    tot_out);
11492 
11493 	/*-
11494 	 * Now we need to clean up the control chunk chain if a ECNE is on
11495 	 * it. It must be marked as UNSENT again so next call will continue
11496 	 * to send it until such time that we get a CWR, to remove it.
11497 	 */
11498 	if (stcb->asoc.ecn_echo_cnt_onq)
11499 		sctp_fix_ecn_echo(asoc);
11500 	return;
11501 }
11502 
11503 
11504 int
11505 sctp_output(inp, m, addr, control, p, flags)
11506 	struct sctp_inpcb *inp;
11507 	struct mbuf *m;
11508 	struct sockaddr *addr;
11509 	struct mbuf *control;
11510 	struct thread *p;
11511 	int flags;
11512 {
11513 	if (inp == NULL) {
11514 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11515 		return (EINVAL);
11516 	}
11517 	if (inp->sctp_socket == NULL) {
11518 		SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11519 		return (EINVAL);
11520 	}
11521 	return (sctp_sosend(inp->sctp_socket,
11522 	    addr,
11523 	    (struct uio *)NULL,
11524 	    m,
11525 	    control,
11526 	    flags, p
11527 	    ));
11528 }
11529 
11530 void
11531 send_forward_tsn(struct sctp_tcb *stcb,
11532     struct sctp_association *asoc)
11533 {
11534 	struct sctp_tmit_chunk *chk;
11535 	struct sctp_forward_tsn_chunk *fwdtsn;
11536 
11537 	SCTP_TCB_LOCK_ASSERT(stcb);
11538 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11539 		if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
11540 			/* mark it to unsent */
11541 			chk->sent = SCTP_DATAGRAM_UNSENT;
11542 			chk->snd_count = 0;
11543 			/* Do we correct its output location? */
11544 			if (chk->whoTo != asoc->primary_destination) {
11545 				sctp_free_remote_addr(chk->whoTo);
11546 				chk->whoTo = asoc->primary_destination;
11547 				atomic_add_int(&chk->whoTo->ref_count, 1);
11548 			}
11549 			goto sctp_fill_in_rest;
11550 		}
11551 	}
11552 	/* Ok if we reach here we must build one */
11553 	sctp_alloc_a_chunk(stcb, chk);
11554 	if (chk == NULL) {
11555 		return;
11556 	}
11557 	chk->copy_by_ref = 0;
11558 	chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
11559 	chk->rec.chunk_id.can_take_data = 0;
11560 	chk->asoc = asoc;
11561 	chk->whoTo = NULL;
11562 
11563 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11564 	if (chk->data == NULL) {
11565 		sctp_free_a_chunk(stcb, chk);
11566 		return;
11567 	}
11568 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11569 	chk->sent = SCTP_DATAGRAM_UNSENT;
11570 	chk->snd_count = 0;
11571 	chk->whoTo = asoc->primary_destination;
11572 	atomic_add_int(&chk->whoTo->ref_count, 1);
11573 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
11574 	asoc->ctrl_queue_cnt++;
11575 sctp_fill_in_rest:
11576 	/*-
11577 	 * Here we go through and fill out the part that deals with
11578 	 * stream/seq of the ones we skip.
11579 	 */
11580 	SCTP_BUF_LEN(chk->data) = 0;
11581 	{
11582 		struct sctp_tmit_chunk *at, *tp1, *last;
11583 		struct sctp_strseq *strseq;
11584 		unsigned int cnt_of_space, i, ovh;
11585 		unsigned int space_needed;
11586 		unsigned int cnt_of_skipped = 0;
11587 
11588 		TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
11589 			if (at->sent != SCTP_FORWARD_TSN_SKIP) {
11590 				/* no more to look at */
11591 				break;
11592 			}
11593 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
11594 				/* We don't report these */
11595 				continue;
11596 			}
11597 			cnt_of_skipped++;
11598 		}
11599 		space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
11600 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
11601 
11602 		cnt_of_space = M_TRAILINGSPACE(chk->data);
11603 
11604 		if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
11605 			ovh = SCTP_MIN_OVERHEAD;
11606 		} else {
11607 			ovh = SCTP_MIN_V4_OVERHEAD;
11608 		}
11609 		if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
11610 			/* trim to a mtu size */
11611 			cnt_of_space = asoc->smallest_mtu - ovh;
11612 		}
11613 		if (cnt_of_space < space_needed) {
11614 			/*-
11615 			 * ok we must trim down the chunk by lowering the
11616 			 * advance peer ack point.
11617 			 */
11618 			cnt_of_skipped = (cnt_of_space -
11619 			    ((sizeof(struct sctp_forward_tsn_chunk)) /
11620 			    sizeof(struct sctp_strseq)));
11621 			/*-
11622 			 * Go through and find the TSN that will be the one
11623 			 * we report.
11624 			 */
11625 			at = TAILQ_FIRST(&asoc->sent_queue);
11626 			for (i = 0; i < cnt_of_skipped; i++) {
11627 				tp1 = TAILQ_NEXT(at, sctp_next);
11628 				at = tp1;
11629 			}
11630 			last = at;
11631 			/*-
11632 			 * last now points to last one I can report, update
11633 			 * peer ack point
11634 			 */
11635 			asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
11636 			space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
11637 		}
11638 		chk->send_size = space_needed;
11639 		/* Setup the chunk */
11640 		fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
11641 		fwdtsn->ch.chunk_length = htons(chk->send_size);
11642 		fwdtsn->ch.chunk_flags = 0;
11643 		fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
11644 		fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
11645 		chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
11646 		    (cnt_of_skipped * sizeof(struct sctp_strseq)));
11647 		SCTP_BUF_LEN(chk->data) = chk->send_size;
11648 		fwdtsn++;
11649 		/*-
11650 		 * Move pointer to after the fwdtsn and transfer to the
11651 		 * strseq pointer.
11652 		 */
11653 		strseq = (struct sctp_strseq *)fwdtsn;
11654 		/*-
11655 		 * Now populate the strseq list. This is done blindly
11656 		 * without pulling out duplicate stream info. This is
11657 		 * inefficent but won't harm the process since the peer will
11658 		 * look at these in sequence and will thus release anything.
11659 		 * It could mean we exceed the PMTU and chop off some that
11660 		 * we could have included.. but this is unlikely (aka 1432/4
11661 		 * would mean 300+ stream seq's would have to be reported in
11662 		 * one FWD-TSN. With a bit of work we can later FIX this to
11663 		 * optimize and pull out duplcates.. but it does add more
11664 		 * overhead. So for now... not!
11665 		 */
11666 		at = TAILQ_FIRST(&asoc->sent_queue);
11667 		for (i = 0; i < cnt_of_skipped; i++) {
11668 			tp1 = TAILQ_NEXT(at, sctp_next);
11669 			if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
11670 				/* We don't report these */
11671 				i--;
11672 				at = tp1;
11673 				continue;
11674 			}
11675 			strseq->stream = ntohs(at->rec.data.stream_number);
11676 			strseq->sequence = ntohs(at->rec.data.stream_seq);
11677 			strseq++;
11678 			at = tp1;
11679 		}
11680 	}
11681 	return;
11682 
11683 }
11684 
11685 void
11686 sctp_send_sack(struct sctp_tcb *stcb)
11687 {
11688 	/*-
11689 	 * Queue up a SACK in the control queue. We must first check to see
11690 	 * if a SACK is somehow on the control queue. If so, we will take
11691 	 * and and remove the old one.
11692 	 */
11693 	struct sctp_association *asoc;
11694 	struct sctp_tmit_chunk *chk, *a_chk;
11695 	struct sctp_sack_chunk *sack;
11696 	struct sctp_gap_ack_block *gap_descriptor;
11697 	struct sack_track *selector;
11698 	int mergeable = 0;
11699 	int offset;
11700 	caddr_t limit;
11701 	uint32_t *dup;
11702 	int limit_reached = 0;
11703 	unsigned int i, jstart, siz, j;
11704 	unsigned int num_gap_blocks = 0, space;
11705 	int num_dups = 0;
11706 	int space_req;
11707 
11708 	a_chk = NULL;
11709 	asoc = &stcb->asoc;
11710 	SCTP_TCB_LOCK_ASSERT(stcb);
11711 	if (asoc->last_data_chunk_from == NULL) {
11712 		/* Hmm we never received anything */
11713 		return;
11714 	}
11715 	sctp_set_rwnd(stcb, asoc);
11716 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11717 		if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
11718 			/* Hmm, found a sack already on queue, remove it */
11719 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
11720 			asoc->ctrl_queue_cnt++;
11721 			a_chk = chk;
11722 			if (a_chk->data) {
11723 				sctp_m_freem(a_chk->data);
11724 				a_chk->data = NULL;
11725 			}
11726 			sctp_free_remote_addr(a_chk->whoTo);
11727 			a_chk->whoTo = NULL;
11728 			break;
11729 		}
11730 	}
11731 	if (a_chk == NULL) {
11732 		sctp_alloc_a_chunk(stcb, a_chk);
11733 		if (a_chk == NULL) {
11734 			/* No memory so we drop the idea, and set a timer */
11735 			if (stcb->asoc.delayed_ack) {
11736 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
11737 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
11738 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
11739 				    stcb->sctp_ep, stcb, NULL);
11740 			} else {
11741 				stcb->asoc.send_sack = 1;
11742 			}
11743 			return;
11744 		}
11745 		a_chk->copy_by_ref = 0;
11746 		/* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
11747 		a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
11748 		a_chk->rec.chunk_id.can_take_data = 1;
11749 	}
11750 	/* Clear our pkt counts */
11751 	asoc->data_pkts_seen = 0;
11752 
11753 	a_chk->asoc = asoc;
11754 	a_chk->snd_count = 0;
11755 	a_chk->send_size = 0;	/* fill in later */
11756 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
11757 	a_chk->whoTo = NULL;
11758 
11759 	if ((asoc->numduptsns) ||
11760 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
11761 	    ) {
11762 		/*-
11763 		 * Ok, we have some duplicates or the destination for the
11764 		 * sack is unreachable, lets see if we can select an
11765 		 * alternate than asoc->last_data_chunk_from
11766 		 */
11767 		if ((!(asoc->last_data_chunk_from->dest_state &
11768 		    SCTP_ADDR_NOT_REACHABLE)) &&
11769 		    (asoc->used_alt_onsack > asoc->numnets)) {
11770 			/* We used an alt last time, don't this time */
11771 			a_chk->whoTo = NULL;
11772 		} else {
11773 			asoc->used_alt_onsack++;
11774 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
11775 		}
11776 		if (a_chk->whoTo == NULL) {
11777 			/* Nope, no alternate */
11778 			a_chk->whoTo = asoc->last_data_chunk_from;
11779 			asoc->used_alt_onsack = 0;
11780 		}
11781 	} else {
11782 		/*
11783 		 * No duplicates so we use the last place we received data
11784 		 * from.
11785 		 */
11786 		asoc->used_alt_onsack = 0;
11787 		a_chk->whoTo = asoc->last_data_chunk_from;
11788 	}
11789 	if (a_chk->whoTo) {
11790 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
11791 	}
11792 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
11793 		/* no gaps */
11794 		space_req = sizeof(struct sctp_sack_chunk);
11795 	} else {
11796 		/* gaps get a cluster */
11797 		space_req = MCLBYTES;
11798 	}
11799 	/* Ok now lets formulate a MBUF with our sack */
11800 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
11801 	if ((a_chk->data == NULL) ||
11802 	    (a_chk->whoTo == NULL)) {
11803 		/* rats, no mbuf memory */
11804 		if (a_chk->data) {
11805 			/* was a problem with the destination */
11806 			sctp_m_freem(a_chk->data);
11807 			a_chk->data = NULL;
11808 		}
11809 		sctp_free_a_chunk(stcb, a_chk);
11810 		/* sa_ignore NO_NULL_CHK */
11811 		if (stcb->asoc.delayed_ack) {
11812 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
11813 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
11814 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
11815 			    stcb->sctp_ep, stcb, NULL);
11816 		} else {
11817 			stcb->asoc.send_sack = 1;
11818 		}
11819 		return;
11820 	}
11821 	/* ok, lets go through and fill it in */
11822 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
11823 	space = M_TRAILINGSPACE(a_chk->data);
11824 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
11825 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
11826 	}
11827 	limit = mtod(a_chk->data, caddr_t);
11828 	limit += space;
11829 
11830 	sack = mtod(a_chk->data, struct sctp_sack_chunk *);
11831 	sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
11832 	/* 0x01 is used by nonce for ecn */
11833 	if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
11834 	    (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
11835 	    (asoc->peer_supports_ecn_nonce))
11836 		sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
11837 	else
11838 		sack->ch.chunk_flags = 0;
11839 
11840 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
11841 		/*-
11842 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
11843 		 * received, then set high bit to 1, else 0. Reset
11844 		 * pkts_rcvd.
11845 		 */
11846 		sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
11847 		asoc->cmt_dac_pkts_rcvd = 0;
11848 	}
11849 #ifdef SCTP_ASOCLOG_OF_TSNS
11850 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
11851 	stcb->asoc.cumack_log_atsnt++;
11852 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
11853 		stcb->asoc.cumack_log_atsnt = 0;
11854 	}
11855 #endif
11856 	sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
11857 	sack->sack.a_rwnd = htonl(asoc->my_rwnd);
11858 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
11859 
11860 	/* reset the readers interpretation */
11861 	stcb->freed_by_sorcv_sincelast = 0;
11862 
11863 	gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
11864 
11865 	siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
11866 	if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
11867 		offset = 1;
11868 		/*-
11869 		 * cum-ack behind the mapping array, so we start and use all
11870 		 * entries.
11871 		 */
11872 		jstart = 0;
11873 	} else {
11874 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
11875 		/*-
11876 		 * we skip the first one when the cum-ack is at or above the
11877 		 * mapping array base. Note this only works if
11878 		 */
11879 		jstart = 1;
11880 	}
11881 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
11882 		/* we have a gap .. maybe */
11883 		for (i = 0; i < siz; i++) {
11884 			selector = &sack_array[asoc->mapping_array[i]];
11885 			if (mergeable && selector->right_edge) {
11886 				/*
11887 				 * Backup, left and right edges were ok to
11888 				 * merge.
11889 				 */
11890 				num_gap_blocks--;
11891 				gap_descriptor--;
11892 			}
11893 			if (selector->num_entries == 0)
11894 				mergeable = 0;
11895 			else {
11896 				for (j = jstart; j < selector->num_entries; j++) {
11897 					if (mergeable && selector->right_edge) {
11898 						/*
11899 						 * do a merge by NOT setting
11900 						 * the left side
11901 						 */
11902 						mergeable = 0;
11903 					} else {
11904 						/*
11905 						 * no merge, set the left
11906 						 * side
11907 						 */
11908 						mergeable = 0;
11909 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
11910 					}
11911 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
11912 					num_gap_blocks++;
11913 					gap_descriptor++;
11914 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
11915 						/* no more room */
11916 						limit_reached = 1;
11917 						break;
11918 					}
11919 				}
11920 				if (selector->left_edge) {
11921 					mergeable = 1;
11922 				}
11923 			}
11924 			if (limit_reached) {
11925 				/* Reached the limit stop */
11926 				break;
11927 			}
11928 			jstart = 0;
11929 			offset += 8;
11930 		}
11931 		if (num_gap_blocks == 0) {
11932 			/*
11933 			 * slide not yet happened, and somehow we got called
11934 			 * to send a sack. Cumack needs to move up.
11935 			 */
11936 			int abort_flag = 0;
11937 
11938 			asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
11939 			sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
11940 			sctp_sack_check(stcb, 0, 0, &abort_flag);
11941 		}
11942 	}
11943 	/* now we must add any dups we are going to report. */
11944 	if ((limit_reached == 0) && (asoc->numduptsns)) {
11945 		dup = (uint32_t *) gap_descriptor;
11946 		for (i = 0; i < asoc->numduptsns; i++) {
11947 			*dup = htonl(asoc->dup_tsns[i]);
11948 			dup++;
11949 			num_dups++;
11950 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
11951 				/* no more room */
11952 				break;
11953 			}
11954 		}
11955 		asoc->numduptsns = 0;
11956 	}
11957 	/*
11958 	 * now that the chunk is prepared queue it to the control chunk
11959 	 * queue.
11960 	 */
11961 	a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
11962 	    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
11963 	    (num_dups * sizeof(int32_t)));
11964 	SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
11965 	sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
11966 	sack->sack.num_dup_tsns = htons(num_dups);
11967 	sack->ch.chunk_length = htons(a_chk->send_size);
11968 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
11969 	asoc->ctrl_queue_cnt++;
11970 	asoc->send_sack = 0;
11971 	SCTP_STAT_INCR(sctps_sendsacks);
11972 	return;
11973 }
11974 
11975 /* EY - This method will replace sctp_send_sack method if nr_sacks negotiated*/
11976 void
11977 sctp_send_nr_sack(struct sctp_tcb *stcb)
11978 {
11979 	/*-
11980 	 * Queue up an NR-SACK in the control queue. We must first check to see
11981 	 * if an NR-SACK is somehow on the control queue. If so, we will take
11982 	 * and and remove the old one.
11983 	 */
11984 	struct sctp_association *asoc;
11985 	struct sctp_tmit_chunk *chk, *a_chk;
11986 
11987 	struct sctp_nr_sack_chunk *nr_sack;
11988 
11989 	struct sctp_gap_ack_block *gap_descriptor;
11990 	struct sctp_nr_gap_ack_block *nr_gap_descriptor;
11991 
11992 	struct sack_track *selector;
11993 	struct nr_sack_track *nr_selector;
11994 
11995 	/* EY do we need nr_mergeable, NO */
11996 	int mergeable = 0;
11997 	int offset;
11998 	caddr_t limit;
11999 	uint32_t *dup;
12000 	int limit_reached = 0;
12001 	unsigned int i, jstart, siz, j;
12002 	unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
12003 	int num_dups = 0;
12004 	int space_req;
12005 	unsigned int reserved = 0;
12006 
12007 	a_chk = NULL;
12008 	asoc = &stcb->asoc;
12009 	SCTP_TCB_LOCK_ASSERT(stcb);
12010 	if (asoc->last_data_chunk_from == NULL) {
12011 		/* Hmm we never received anything */
12012 		return;
12013 	}
12014 	sctp_set_rwnd(stcb, asoc);
12015 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
12016 		if (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) {
12017 			/* Hmm, found a sack already on queue, remove it */
12018 			TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
12019 			asoc->ctrl_queue_cnt++;
12020 			a_chk = chk;
12021 			if (a_chk->data) {
12022 				sctp_m_freem(a_chk->data);
12023 				a_chk->data = NULL;
12024 			}
12025 			sctp_free_remote_addr(a_chk->whoTo);
12026 			a_chk->whoTo = NULL;
12027 			break;
12028 		}
12029 	}
12030 	if (a_chk == NULL) {
12031 		sctp_alloc_a_chunk(stcb, a_chk);
12032 		if (a_chk == NULL) {
12033 			/* No memory so we drop the idea, and set a timer */
12034 			if (stcb->asoc.delayed_ack) {
12035 				sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
12036 				    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
12037 				sctp_timer_start(SCTP_TIMER_TYPE_RECV,
12038 				    stcb->sctp_ep, stcb, NULL);
12039 			} else {
12040 				stcb->asoc.send_sack = 1;
12041 			}
12042 			return;
12043 		}
12044 		a_chk->copy_by_ref = 0;
12045 		/* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
12046 		a_chk->rec.chunk_id.id = SCTP_NR_SELECTIVE_ACK;
12047 		a_chk->rec.chunk_id.can_take_data = 1;
12048 	}
12049 	/* Clear our pkt counts */
12050 	asoc->data_pkts_seen = 0;
12051 
12052 	a_chk->asoc = asoc;
12053 	a_chk->snd_count = 0;
12054 	a_chk->send_size = 0;	/* fill in later */
12055 	a_chk->sent = SCTP_DATAGRAM_UNSENT;
12056 	a_chk->whoTo = NULL;
12057 
12058 	if ((asoc->numduptsns) ||
12059 	    (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
12060 	    ) {
12061 		/*-
12062 		 * Ok, we have some duplicates or the destination for the
12063 		 * sack is unreachable, lets see if we can select an
12064 		 * alternate than asoc->last_data_chunk_from
12065 		 */
12066 		if ((!(asoc->last_data_chunk_from->dest_state &
12067 		    SCTP_ADDR_NOT_REACHABLE)) &&
12068 		    (asoc->used_alt_onsack > asoc->numnets)) {
12069 			/* We used an alt last time, don't this time */
12070 			a_chk->whoTo = NULL;
12071 		} else {
12072 			asoc->used_alt_onsack++;
12073 			a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
12074 		}
12075 		if (a_chk->whoTo == NULL) {
12076 			/* Nope, no alternate */
12077 			a_chk->whoTo = asoc->last_data_chunk_from;
12078 			asoc->used_alt_onsack = 0;
12079 		}
12080 	} else {
12081 		/*
12082 		 * No duplicates so we use the last place we received data
12083 		 * from.
12084 		 */
12085 		asoc->used_alt_onsack = 0;
12086 		a_chk->whoTo = asoc->last_data_chunk_from;
12087 	}
12088 	if (a_chk->whoTo) {
12089 		atomic_add_int(&a_chk->whoTo->ref_count, 1);
12090 	}
12091 	if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
12092 		/* no gaps */
12093 		space_req = sizeof(struct sctp_nr_sack_chunk);
12094 	} else {
12095 		/* EY - what is this about? */
12096 		/* gaps get a cluster */
12097 		space_req = MCLBYTES;
12098 	}
12099 	/* Ok now lets formulate a MBUF with our sack */
12100 	a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
12101 	if ((a_chk->data == NULL) ||
12102 	    (a_chk->whoTo == NULL)) {
12103 		/* rats, no mbuf memory */
12104 		if (a_chk->data) {
12105 			/* was a problem with the destination */
12106 			sctp_m_freem(a_chk->data);
12107 			a_chk->data = NULL;
12108 		}
12109 		sctp_free_a_chunk(stcb, a_chk);
12110 		/* sa_ignore NO_NULL_CHK */
12111 		if (stcb->asoc.delayed_ack) {
12112 			sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
12113 			    stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
12114 			sctp_timer_start(SCTP_TIMER_TYPE_RECV,
12115 			    stcb->sctp_ep, stcb, NULL);
12116 		} else {
12117 			stcb->asoc.send_sack = 1;
12118 		}
12119 		return;
12120 	}
12121 	/* ok, lets go through and fill it in */
12122 	SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
12123 	space = M_TRAILINGSPACE(a_chk->data);
12124 	if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
12125 		space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
12126 	}
12127 	limit = mtod(a_chk->data, caddr_t);
12128 	limit += space;
12129 
12130 	nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
12131 	nr_sack->ch.chunk_type = SCTP_NR_SELECTIVE_ACK;
12132 	/* EYJ */
12133 	/* 0x01 is used by nonce for ecn */
12134 	if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
12135 	    (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
12136 	    (asoc->peer_supports_ecn_nonce))
12137 		nr_sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
12138 	else
12139 		nr_sack->ch.chunk_flags = 0;
12140 
12141 	if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
12142 		/*-
12143 		 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
12144 		 * received, then set high bit to 1, else 0. Reset
12145 		 * pkts_rcvd.
12146 		 */
12147 		/* EY - TODO: which chunk flag is used in here? -The LSB */
12148 		nr_sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
12149 		asoc->cmt_dac_pkts_rcvd = 0;
12150 	}
12151 	/*
12152 	 * EY - this is a never reneging receiver, that makes all gaps are
12153 	 * nr-gaps, set the All bit
12154 	 */
12155 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
12156 		nr_sack->ch.chunk_flags |= SCTP_NR_SACK_ALL_BIT;
12157 	}
12158 #ifdef SCTP_ASOCLOG_OF_TSNS
12159 	stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
12160 	stcb->asoc.cumack_log_atsnt++;
12161 	if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
12162 		stcb->asoc.cumack_log_atsnt = 0;
12163 	}
12164 #endif
12165 	nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
12166 	nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
12167 	asoc->my_last_reported_rwnd = asoc->my_rwnd;
12168 
12169 	/* reset the readers interpretation */
12170 	stcb->freed_by_sorcv_sincelast = 0;
12171 
12172 	gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
12173 	nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
12174 
12175 	siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
12176 	if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
12177 		offset = 1;
12178 		/*-
12179 		 * cum-ack behind the mapping array, so we start and use all
12180 		 * entries.
12181 		 */
12182 		jstart = 0;
12183 	} else {
12184 		offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
12185 		/*-
12186 		 * we skip the first one when the cum-ack is at or above the
12187 		 * mapping array base. Note this only works if
12188 		 */
12189 		jstart = 1;
12190 	}
12191 	if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
12192 		/* we have a gap .. maybe */
12193 		for (i = 0; i < siz; i++) {
12194 			selector = &sack_array[asoc->mapping_array[i]];
12195 			if (mergeable && selector->right_edge) {
12196 				/*
12197 				 * Backup, left and right edges were ok to
12198 				 * merge.
12199 				 */
12200 				num_gap_blocks--;
12201 				gap_descriptor--;
12202 			}
12203 			if (selector->num_entries == 0)
12204 				mergeable = 0;
12205 			else {
12206 				for (j = jstart; j < selector->num_entries; j++) {
12207 					if (mergeable && selector->right_edge) {
12208 						/*
12209 						 * do a merge by NOT setting
12210 						 * the left side
12211 						 */
12212 						mergeable = 0;
12213 					} else {
12214 						/*
12215 						 * no merge, set the left
12216 						 * side
12217 						 */
12218 						mergeable = 0;
12219 						gap_descriptor->start = htons((selector->gaps[j].start + offset));
12220 					}
12221 					gap_descriptor->end = htons((selector->gaps[j].end + offset));
12222 					num_gap_blocks++;
12223 					gap_descriptor++;
12224 					if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
12225 						/* no more room */
12226 						limit_reached = 1;
12227 						break;
12228 					}
12229 				}
12230 				if (selector->left_edge) {
12231 					mergeable = 1;
12232 				}
12233 			}
12234 			if (limit_reached) {
12235 				/* Reached the limit stop */
12236 				break;
12237 			}
12238 			jstart = 0;
12239 			offset += 8;
12240 		}
12241 		if (num_gap_blocks == 0) {
12242 			/*
12243 			 * slide not yet happened, and somehow we got called
12244 			 * to send a sack. Cumack needs to move up.
12245 			 */
12246 			int abort_flag = 0;
12247 
12248 			asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
12249 			nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
12250 			sctp_sack_check(stcb, 0, 0, &abort_flag);
12251 		}
12252 	}
12253 	/*---------------------------------------------------------filling the nr_gap_ack blocks----------------------------------------------------*/
12254 
12255 	nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)gap_descriptor;
12256 
12257 	/* EY - there will be gaps + nr_gaps if draining is possible */
12258 	if (SCTP_BASE_SYSCTL(sctp_do_drain)) {
12259 
12260 		mergeable = 0;
12261 
12262 		siz = (((asoc->highest_tsn_inside_nr_map - asoc->nr_mapping_array_base_tsn) + 1) + 7) / 8;
12263 		if (compare_with_wrap(asoc->nr_mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
12264 			offset = 1;
12265 			/*-
12266 			* cum-ack behind the mapping array, so we start and use all
12267 			* entries.
12268 			*/
12269 			jstart = 0;
12270 		} else {
12271 			offset = asoc->nr_mapping_array_base_tsn - asoc->cumulative_tsn;
12272 			/*-
12273 			* we skip the first one when the cum-ack is at or above the
12274 			* mapping array base. Note this only works if
12275 			*/
12276 			jstart = 1;
12277 		}
12278 		if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
12279 			/* we have a gap .. maybe */
12280 			for (i = 0; i < siz; i++) {
12281 				nr_selector = &nr_sack_array[asoc->nr_mapping_array[i]];
12282 				if (mergeable && nr_selector->right_edge) {
12283 					/*
12284 					 * Backup, left and right edges were
12285 					 * ok to merge.
12286 					 */
12287 					num_nr_gap_blocks--;
12288 					nr_gap_descriptor--;
12289 				}
12290 				if (nr_selector->num_entries == 0)
12291 					mergeable = 0;
12292 				else {
12293 					for (j = jstart; j < nr_selector->num_entries; j++) {
12294 						if (mergeable && nr_selector->right_edge) {
12295 							/*
12296 							 * do a merge by NOT
12297 							 * setting the left
12298 							 * side
12299 							 */
12300 							mergeable = 0;
12301 						} else {
12302 							/*
12303 							 * no merge, set the
12304 							 * left side
12305 							 */
12306 							mergeable = 0;
12307 							nr_gap_descriptor->start = htons((nr_selector->nr_gaps[j].start + offset));
12308 						}
12309 						nr_gap_descriptor->end = htons((nr_selector->nr_gaps[j].end + offset));
12310 						num_nr_gap_blocks++;
12311 						nr_gap_descriptor++;
12312 						if (((caddr_t)nr_gap_descriptor + sizeof(struct sctp_nr_gap_ack_block)) > limit) {
12313 							/* no more room */
12314 							limit_reached = 1;
12315 							break;
12316 						}
12317 					}
12318 					if (nr_selector->left_edge) {
12319 						mergeable = 1;
12320 					}
12321 				}
12322 				if (limit_reached) {
12323 					/* Reached the limit stop */
12324 					break;
12325 				}
12326 				jstart = 0;
12327 				offset += 8;
12328 			}
12329 		}
12330 	}
12331 	/*---------------------------------------------------End of---filling the nr_gap_ack blocks----------------------------------------------------*/
12332 
12333 	/* now we must add any dups we are going to report. */
12334 	if ((limit_reached == 0) && (asoc->numduptsns)) {
12335 		dup = (uint32_t *) nr_gap_descriptor;
12336 		for (i = 0; i < asoc->numduptsns; i++) {
12337 			*dup = htonl(asoc->dup_tsns[i]);
12338 			dup++;
12339 			num_dups++;
12340 			if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
12341 				/* no more room */
12342 				break;
12343 			}
12344 		}
12345 		asoc->numduptsns = 0;
12346 	}
12347 	/*
12348 	 * now that the chunk is prepared queue it to the control chunk
12349 	 * queue.
12350 	 */
12351 	if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
12352 		num_nr_gap_blocks = num_gap_blocks;
12353 		num_gap_blocks = 0;
12354 	}
12355 	a_chk->send_size = (sizeof(struct sctp_nr_sack_chunk) +
12356 	    (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
12357 	    (num_nr_gap_blocks * sizeof(struct sctp_nr_gap_ack_block)) +
12358 	    (num_dups * sizeof(int32_t)));
12359 
12360 	SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
12361 	nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
12362 	nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
12363 	nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
12364 	nr_sack->nr_sack.reserved = htons(reserved);
12365 	nr_sack->ch.chunk_length = htons(a_chk->send_size);
12366 	TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
12367 	asoc->ctrl_queue_cnt++;
12368 	asoc->send_sack = 0;
12369 	SCTP_STAT_INCR(sctps_sendsacks);
12370 	return;
12371 }
12372 
12373 void
12374 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
12375 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
12376     SCTP_UNUSED
12377 #endif
12378 )
12379 {
12380 	struct mbuf *m_abort;
12381 	struct mbuf *m_out = NULL, *m_end = NULL;
12382 	struct sctp_abort_chunk *abort = NULL;
12383 	int sz;
12384 	uint32_t auth_offset = 0;
12385 	struct sctp_auth_chunk *auth = NULL;
12386 
12387 	/*-
12388 	 * Add an AUTH chunk, if chunk requires it and save the offset into
12389 	 * the chain for AUTH
12390 	 */
12391 	if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
12392 	    stcb->asoc.peer_auth_chunks)) {
12393 		m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
12394 		    stcb, SCTP_ABORT_ASSOCIATION);
12395 		SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12396 	}
12397 	SCTP_TCB_LOCK_ASSERT(stcb);
12398 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
12399 	if (m_abort == NULL) {
12400 		/* no mbuf's */
12401 		if (m_out)
12402 			sctp_m_freem(m_out);
12403 		return;
12404 	}
12405 	/* link in any error */
12406 	SCTP_BUF_NEXT(m_abort) = operr;
12407 	sz = 0;
12408 	if (operr) {
12409 		struct mbuf *n;
12410 
12411 		n = operr;
12412 		while (n) {
12413 			sz += SCTP_BUF_LEN(n);
12414 			n = SCTP_BUF_NEXT(n);
12415 		}
12416 	}
12417 	SCTP_BUF_LEN(m_abort) = sizeof(*abort);
12418 	if (m_out == NULL) {
12419 		/* NO Auth chunk prepended, so reserve space in front */
12420 		SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
12421 		m_out = m_abort;
12422 	} else {
12423 		/* Put AUTH chunk at the front of the chain */
12424 		SCTP_BUF_NEXT(m_end) = m_abort;
12425 	}
12426 
12427 	/* fill in the ABORT chunk */
12428 	abort = mtod(m_abort, struct sctp_abort_chunk *);
12429 	abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
12430 	abort->ch.chunk_flags = 0;
12431 	abort->ch.chunk_length = htons(sizeof(*abort) + sz);
12432 
12433 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
12434 	    stcb->asoc.primary_destination,
12435 	    (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
12436 	    m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
12437 	    stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
12438 	    stcb->asoc.primary_destination->port, so_locked, NULL);
12439 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12440 }
12441 
12442 void
12443 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
12444     struct sctp_nets *net)
12445 {
12446 	/* formulate and SEND a SHUTDOWN-COMPLETE */
12447 	struct mbuf *m_shutdown_comp;
12448 	struct sctp_shutdown_complete_chunk *shutdown_complete;
12449 
12450 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
12451 	if (m_shutdown_comp == NULL) {
12452 		/* no mbuf's */
12453 		return;
12454 	}
12455 	shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
12456 	shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
12457 	shutdown_complete->ch.chunk_flags = 0;
12458 	shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
12459 	SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
12460 	(void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
12461 	    (struct sockaddr *)&net->ro._l_addr,
12462 	    m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
12463 	    stcb->sctp_ep->sctp_lport, stcb->rport,
12464 	    htonl(stcb->asoc.peer_vtag),
12465 	    net->port, SCTP_SO_NOT_LOCKED, NULL);
12466 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12467 	return;
12468 }
12469 
12470 void
12471 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
12472     uint32_t vrf_id, uint16_t port)
12473 {
12474 	/* formulate and SEND a SHUTDOWN-COMPLETE */
12475 	struct mbuf *o_pak;
12476 	struct mbuf *mout;
12477 	struct ip *iph, *iph_out;
12478 	struct udphdr *udp = NULL;
12479 
12480 #ifdef INET6
12481 	struct ip6_hdr *ip6, *ip6_out;
12482 
12483 #endif
12484 	int offset_out, len, mlen;
12485 	struct sctp_shutdown_complete_msg *comp_cp;
12486 
12487 	iph = mtod(m, struct ip *);
12488 	switch (iph->ip_v) {
12489 	case IPVERSION:
12490 		len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
12491 		break;
12492 #ifdef INET6
12493 	case IPV6_VERSION >> 4:
12494 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
12495 		break;
12496 #endif
12497 	default:
12498 		return;
12499 	}
12500 	if (port) {
12501 		len += sizeof(struct udphdr);
12502 	}
12503 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
12504 	if (mout == NULL) {
12505 		return;
12506 	}
12507 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
12508 	SCTP_BUF_LEN(mout) = len;
12509 	SCTP_BUF_NEXT(mout) = NULL;
12510 	iph_out = NULL;
12511 #ifdef INET6
12512 	ip6_out = NULL;
12513 #endif
12514 	offset_out = 0;
12515 
12516 	switch (iph->ip_v) {
12517 	case IPVERSION:
12518 		iph_out = mtod(mout, struct ip *);
12519 
12520 		/* Fill in the IP header for the ABORT */
12521 		iph_out->ip_v = IPVERSION;
12522 		iph_out->ip_hl = (sizeof(struct ip) / 4);
12523 		iph_out->ip_tos = (u_char)0;
12524 		iph_out->ip_id = 0;
12525 		iph_out->ip_off = 0;
12526 		iph_out->ip_ttl = MAXTTL;
12527 		if (port) {
12528 			iph_out->ip_p = IPPROTO_UDP;
12529 		} else {
12530 			iph_out->ip_p = IPPROTO_SCTP;
12531 		}
12532 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12533 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12534 
12535 		/* let IP layer calculate this */
12536 		iph_out->ip_sum = 0;
12537 		offset_out += sizeof(*iph_out);
12538 		comp_cp = (struct sctp_shutdown_complete_msg *)(
12539 		    (caddr_t)iph_out + offset_out);
12540 		break;
12541 #ifdef INET6
12542 	case IPV6_VERSION >> 4:
12543 		ip6 = (struct ip6_hdr *)iph;
12544 		ip6_out = mtod(mout, struct ip6_hdr *);
12545 
12546 		/* Fill in the IPv6 header for the ABORT */
12547 		ip6_out->ip6_flow = ip6->ip6_flow;
12548 		ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
12549 		if (port) {
12550 			ip6_out->ip6_nxt = IPPROTO_UDP;
12551 		} else {
12552 			ip6_out->ip6_nxt = IPPROTO_SCTP;
12553 		}
12554 		ip6_out->ip6_src = ip6->ip6_dst;
12555 		ip6_out->ip6_dst = ip6->ip6_src;
12556 		/*
12557 		 * ?? The old code had both the iph len + payload, I think
12558 		 * this is wrong and would never have worked
12559 		 */
12560 		ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
12561 		offset_out += sizeof(*ip6_out);
12562 		comp_cp = (struct sctp_shutdown_complete_msg *)(
12563 		    (caddr_t)ip6_out + offset_out);
12564 		break;
12565 #endif				/* INET6 */
12566 	default:
12567 		/* Currently not supported. */
12568 		sctp_m_freem(mout);
12569 		return;
12570 	}
12571 	if (port) {
12572 		udp = (struct udphdr *)comp_cp;
12573 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12574 		udp->uh_dport = port;
12575 		udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
12576 		udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12577 		offset_out += sizeof(struct udphdr);
12578 		comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
12579 	}
12580 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12581 		/* no mbuf's */
12582 		sctp_m_freem(mout);
12583 		return;
12584 	}
12585 	/* Now copy in and fill in the ABORT tags etc. */
12586 	comp_cp->sh.src_port = sh->dest_port;
12587 	comp_cp->sh.dest_port = sh->src_port;
12588 	comp_cp->sh.checksum = 0;
12589 	comp_cp->sh.v_tag = sh->v_tag;
12590 	comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
12591 	comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
12592 	comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
12593 
12594 	if (iph_out != NULL) {
12595 		sctp_route_t ro;
12596 		int ret;
12597 		struct sctp_tcb *stcb = NULL;
12598 
12599 		mlen = SCTP_BUF_LEN(mout);
12600 		bzero(&ro, sizeof ro);
12601 		/* set IPv4 length */
12602 		iph_out->ip_len = mlen;
12603 #ifdef  SCTP_PACKET_LOGGING
12604 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12605 			sctp_packet_log(mout, mlen);
12606 #endif
12607 		if (port) {
12608 			comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
12609 			SCTP_STAT_INCR(sctps_sendswcrc);
12610 			SCTP_ENABLE_UDP_CSUM(mout);
12611 		} else {
12612 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
12613 			mout->m_pkthdr.csum_data = 0;	/* FIXME MT */
12614 			SCTP_STAT_INCR(sctps_sendhwcrc);
12615 		}
12616 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
12617 		/* out it goes */
12618 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
12619 
12620 		/* Free the route if we got one back */
12621 		if (ro.ro_rt)
12622 			RTFREE(ro.ro_rt);
12623 	}
12624 #ifdef INET6
12625 	if (ip6_out != NULL) {
12626 		struct route_in6 ro;
12627 		int ret;
12628 		struct sctp_tcb *stcb = NULL;
12629 		struct ifnet *ifp = NULL;
12630 
12631 		bzero(&ro, sizeof(ro));
12632 		mlen = SCTP_BUF_LEN(mout);
12633 #ifdef  SCTP_PACKET_LOGGING
12634 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12635 			sctp_packet_log(mout, mlen);
12636 #endif
12637 		comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
12638 		SCTP_STAT_INCR(sctps_sendswcrc);
12639 		SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
12640 		if (port) {
12641 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
12642 			    sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
12643 				udp->uh_sum = 0xffff;
12644 			}
12645 		}
12646 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
12647 
12648 		/* Free the route if we got one back */
12649 		if (ro.ro_rt)
12650 			RTFREE(ro.ro_rt);
12651 	}
12652 #endif
12653 	SCTP_STAT_INCR(sctps_sendpackets);
12654 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12655 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12656 	return;
12657 
12658 }
12659 
12660 static struct sctp_nets *
12661 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
12662 {
12663 	struct sctp_nets *net, *hnet;
12664 	int ms_goneby, highest_ms, state_overide = 0;
12665 
12666 	(void)SCTP_GETTIME_TIMEVAL(now);
12667 	highest_ms = 0;
12668 	hnet = NULL;
12669 	SCTP_TCB_LOCK_ASSERT(stcb);
12670 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
12671 		if (
12672 		    ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
12673 		    (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
12674 		    ) {
12675 			/*
12676 			 * Skip this guy from consideration if HB is off AND
12677 			 * its confirmed
12678 			 */
12679 			continue;
12680 		}
12681 		if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
12682 			/* skip this dest net from consideration */
12683 			continue;
12684 		}
12685 		if (net->last_sent_time.tv_sec) {
12686 			/* Sent to so we subtract */
12687 			ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
12688 		} else
12689 			/* Never been sent to */
12690 			ms_goneby = 0x7fffffff;
12691 		/*-
12692 		 * When the address state is unconfirmed but still
12693 		 * considered reachable, we HB at a higher rate. Once it
12694 		 * goes confirmed OR reaches the "unreachable" state, thenw
12695 		 * we cut it back to HB at a more normal pace.
12696 		 */
12697 		if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
12698 			state_overide = 1;
12699 		} else {
12700 			state_overide = 0;
12701 		}
12702 
12703 		if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
12704 		    (ms_goneby > highest_ms)) {
12705 			highest_ms = ms_goneby;
12706 			hnet = net;
12707 		}
12708 	}
12709 	if (hnet &&
12710 	    ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
12711 		state_overide = 1;
12712 	} else {
12713 		state_overide = 0;
12714 	}
12715 
12716 	if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
12717 		/*-
12718 		 * Found the one with longest delay bounds OR it is
12719 		 * unconfirmed and still not marked unreachable.
12720 		 */
12721 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
12722 #ifdef SCTP_DEBUG
12723 		if (hnet) {
12724 			SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
12725 			    (struct sockaddr *)&hnet->ro._l_addr);
12726 		} else {
12727 			SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
12728 		}
12729 #endif
12730 		/* update the timer now */
12731 		hnet->last_sent_time = *now;
12732 		return (hnet);
12733 	}
12734 	/* Nothing to HB */
12735 	return (NULL);
12736 }
12737 
12738 int
12739 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
12740 {
12741 	struct sctp_tmit_chunk *chk;
12742 	struct sctp_nets *net;
12743 	struct sctp_heartbeat_chunk *hb;
12744 	struct timeval now;
12745 	struct sockaddr_in *sin;
12746 	struct sockaddr_in6 *sin6;
12747 
12748 	SCTP_TCB_LOCK_ASSERT(stcb);
12749 	if (user_req == 0) {
12750 		net = sctp_select_hb_destination(stcb, &now);
12751 		if (net == NULL) {
12752 			/*-
12753 			 * All our busy none to send to, just start the
12754 			 * timer again.
12755 			 */
12756 			if (stcb->asoc.state == 0) {
12757 				return (0);
12758 			}
12759 			sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
12760 			    stcb->sctp_ep,
12761 			    stcb,
12762 			    net);
12763 			return (0);
12764 		}
12765 	} else {
12766 		net = u_net;
12767 		if (net == NULL) {
12768 			return (0);
12769 		}
12770 		(void)SCTP_GETTIME_TIMEVAL(&now);
12771 	}
12772 	sin = (struct sockaddr_in *)&net->ro._l_addr;
12773 	if (sin->sin_family != AF_INET) {
12774 		if (sin->sin_family != AF_INET6) {
12775 			/* huh */
12776 			return (0);
12777 		}
12778 	}
12779 	sctp_alloc_a_chunk(stcb, chk);
12780 	if (chk == NULL) {
12781 		SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
12782 		return (0);
12783 	}
12784 	chk->copy_by_ref = 0;
12785 	chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
12786 	chk->rec.chunk_id.can_take_data = 1;
12787 	chk->asoc = &stcb->asoc;
12788 	chk->send_size = sizeof(struct sctp_heartbeat_chunk);
12789 
12790 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
12791 	if (chk->data == NULL) {
12792 		sctp_free_a_chunk(stcb, chk);
12793 		return (0);
12794 	}
12795 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12796 	SCTP_BUF_LEN(chk->data) = chk->send_size;
12797 	chk->sent = SCTP_DATAGRAM_UNSENT;
12798 	chk->snd_count = 0;
12799 	chk->whoTo = net;
12800 	atomic_add_int(&chk->whoTo->ref_count, 1);
12801 	/* Now we have a mbuf that we can fill in with the details */
12802 	hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
12803 	memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
12804 	/* fill out chunk header */
12805 	hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
12806 	hb->ch.chunk_flags = 0;
12807 	hb->ch.chunk_length = htons(chk->send_size);
12808 	/* Fill out hb parameter */
12809 	hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
12810 	hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
12811 	hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
12812 	hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
12813 	/* Did our user request this one, put it in */
12814 	hb->heartbeat.hb_info.user_req = user_req;
12815 	hb->heartbeat.hb_info.addr_family = sin->sin_family;
12816 	hb->heartbeat.hb_info.addr_len = sin->sin_len;
12817 	if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
12818 		/*
12819 		 * we only take from the entropy pool if the address is not
12820 		 * confirmed.
12821 		 */
12822 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
12823 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
12824 	} else {
12825 		net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
12826 		net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
12827 	}
12828 	if (sin->sin_family == AF_INET) {
12829 		memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
12830 	} else if (sin->sin_family == AF_INET6) {
12831 		/* We leave the scope the way it is in our lookup table. */
12832 		sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
12833 		memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
12834 	} else {
12835 		/* huh compiler bug */
12836 		return (0);
12837 	}
12838 
12839 	/*
12840 	 * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
12841 	 * PF-heartbeats.  Because of this, threshold management is done by
12842 	 * the t3 timer handler, and does not need to be done upon the send
12843 	 * of a PF-heartbeat. If CMT PF is on and the destination to which a
12844 	 * heartbeat is being sent is in PF state, do NOT do threshold
12845 	 * management.
12846 	 */
12847 	if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
12848 		/* ok we have a destination that needs a beat */
12849 		/* lets do the theshold management Qiaobing style */
12850 		if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
12851 		    stcb->asoc.max_send_times)) {
12852 			/*-
12853 			 * we have lost the association, in a way this is
12854 			 * quite bad since we really are one less time since
12855 			 * we really did not send yet. This is the down side
12856 			 * to the Q's style as defined in the RFC and not my
12857 			 * alternate style defined in the RFC.
12858 			 */
12859 			if (chk->data != NULL) {
12860 				sctp_m_freem(chk->data);
12861 				chk->data = NULL;
12862 			}
12863 			/*
12864 			 * Here we do NOT use the macro since the
12865 			 * association is now gone.
12866 			 */
12867 			if (chk->whoTo) {
12868 				sctp_free_remote_addr(chk->whoTo);
12869 				chk->whoTo = NULL;
12870 			}
12871 			sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
12872 			return (-1);
12873 		}
12874 	}
12875 	net->hb_responded = 0;
12876 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
12877 	stcb->asoc.ctrl_queue_cnt++;
12878 	SCTP_STAT_INCR(sctps_sendheartbeat);
12879 	/*-
12880 	 * Call directly med level routine to put out the chunk. It will
12881 	 * always tumble out control chunks aka HB but it may even tumble
12882 	 * out data too.
12883 	 */
12884 	return (1);
12885 }
12886 
12887 void
12888 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
12889     uint32_t high_tsn)
12890 {
12891 	struct sctp_association *asoc;
12892 	struct sctp_ecne_chunk *ecne;
12893 	struct sctp_tmit_chunk *chk;
12894 
12895 	asoc = &stcb->asoc;
12896 	SCTP_TCB_LOCK_ASSERT(stcb);
12897 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
12898 		if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
12899 			/* found a previous ECN_ECHO update it if needed */
12900 			ecne = mtod(chk->data, struct sctp_ecne_chunk *);
12901 			ecne->tsn = htonl(high_tsn);
12902 			return;
12903 		}
12904 	}
12905 	/* nope could not find one to update so we must build one */
12906 	sctp_alloc_a_chunk(stcb, chk);
12907 	if (chk == NULL) {
12908 		return;
12909 	}
12910 	chk->copy_by_ref = 0;
12911 	SCTP_STAT_INCR(sctps_sendecne);
12912 	chk->rec.chunk_id.id = SCTP_ECN_ECHO;
12913 	chk->rec.chunk_id.can_take_data = 0;
12914 	chk->asoc = &stcb->asoc;
12915 	chk->send_size = sizeof(struct sctp_ecne_chunk);
12916 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
12917 	if (chk->data == NULL) {
12918 		sctp_free_a_chunk(stcb, chk);
12919 		return;
12920 	}
12921 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12922 	SCTP_BUF_LEN(chk->data) = chk->send_size;
12923 	chk->sent = SCTP_DATAGRAM_UNSENT;
12924 	chk->snd_count = 0;
12925 	chk->whoTo = net;
12926 	atomic_add_int(&chk->whoTo->ref_count, 1);
12927 	stcb->asoc.ecn_echo_cnt_onq++;
12928 	ecne = mtod(chk->data, struct sctp_ecne_chunk *);
12929 	ecne->ch.chunk_type = SCTP_ECN_ECHO;
12930 	ecne->ch.chunk_flags = 0;
12931 	ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
12932 	ecne->tsn = htonl(high_tsn);
12933 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
12934 	asoc->ctrl_queue_cnt++;
12935 }
12936 
12937 void
12938 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
12939     struct mbuf *m, int iphlen, int bad_crc)
12940 {
12941 	struct sctp_association *asoc;
12942 	struct sctp_pktdrop_chunk *drp;
12943 	struct sctp_tmit_chunk *chk;
12944 	uint8_t *datap;
12945 	int len;
12946 	int was_trunc = 0;
12947 	struct ip *iph;
12948 
12949 #ifdef INET6
12950 	struct ip6_hdr *ip6h;
12951 
12952 #endif
12953 	int fullsz = 0, extra = 0;
12954 	long spc;
12955 	int offset;
12956 	struct sctp_chunkhdr *ch, chunk_buf;
12957 	unsigned int chk_length;
12958 
12959 	if (!stcb) {
12960 		return;
12961 	}
12962 	asoc = &stcb->asoc;
12963 	SCTP_TCB_LOCK_ASSERT(stcb);
12964 	if (asoc->peer_supports_pktdrop == 0) {
12965 		/*-
12966 		 * peer must declare support before I send one.
12967 		 */
12968 		return;
12969 	}
12970 	if (stcb->sctp_socket == NULL) {
12971 		return;
12972 	}
12973 	sctp_alloc_a_chunk(stcb, chk);
12974 	if (chk == NULL) {
12975 		return;
12976 	}
12977 	chk->copy_by_ref = 0;
12978 	iph = mtod(m, struct ip *);
12979 	if (iph == NULL) {
12980 		sctp_free_a_chunk(stcb, chk);
12981 		return;
12982 	}
12983 	switch (iph->ip_v) {
12984 	case IPVERSION:
12985 		/* IPv4 */
12986 		len = chk->send_size = iph->ip_len;
12987 		break;
12988 #ifdef INET6
12989 	case IPV6_VERSION >> 4:
12990 		/* IPv6 */
12991 		ip6h = mtod(m, struct ip6_hdr *);
12992 		len = chk->send_size = htons(ip6h->ip6_plen);
12993 		break;
12994 #endif
12995 	default:
12996 		return;
12997 	}
12998 	/* Validate that we do not have an ABORT in here. */
12999 	offset = iphlen + sizeof(struct sctphdr);
13000 	ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
13001 	    sizeof(*ch), (uint8_t *) & chunk_buf);
13002 	while (ch != NULL) {
13003 		chk_length = ntohs(ch->chunk_length);
13004 		if (chk_length < sizeof(*ch)) {
13005 			/* break to abort land */
13006 			break;
13007 		}
13008 		switch (ch->chunk_type) {
13009 		case SCTP_PACKET_DROPPED:
13010 		case SCTP_ABORT_ASSOCIATION:
13011 			/*-
13012 			 * we don't respond with an PKT-DROP to an ABORT
13013 			 * or PKT-DROP
13014 			 */
13015 			sctp_free_a_chunk(stcb, chk);
13016 			return;
13017 		default:
13018 			break;
13019 		}
13020 		offset += SCTP_SIZE32(chk_length);
13021 		ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
13022 		    sizeof(*ch), (uint8_t *) & chunk_buf);
13023 	}
13024 
13025 	if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
13026 	    min(stcb->asoc.smallest_mtu, MCLBYTES)) {
13027 		/*
13028 		 * only send 1 mtu worth, trim off the excess on the end.
13029 		 */
13030 		fullsz = len - extra;
13031 		len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
13032 		was_trunc = 1;
13033 	}
13034 	chk->asoc = &stcb->asoc;
13035 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
13036 	if (chk->data == NULL) {
13037 jump_out:
13038 		sctp_free_a_chunk(stcb, chk);
13039 		return;
13040 	}
13041 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13042 	drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
13043 	if (drp == NULL) {
13044 		sctp_m_freem(chk->data);
13045 		chk->data = NULL;
13046 		goto jump_out;
13047 	}
13048 	chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
13049 	    sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
13050 	chk->book_size_scale = 0;
13051 	if (was_trunc) {
13052 		drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
13053 		drp->trunc_len = htons(fullsz);
13054 		/*
13055 		 * Len is already adjusted to size minus overhead above take
13056 		 * out the pkt_drop chunk itself from it.
13057 		 */
13058 		chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
13059 		len = chk->send_size;
13060 	} else {
13061 		/* no truncation needed */
13062 		drp->ch.chunk_flags = 0;
13063 		drp->trunc_len = htons(0);
13064 	}
13065 	if (bad_crc) {
13066 		drp->ch.chunk_flags |= SCTP_BADCRC;
13067 	}
13068 	chk->send_size += sizeof(struct sctp_pktdrop_chunk);
13069 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13070 	chk->sent = SCTP_DATAGRAM_UNSENT;
13071 	chk->snd_count = 0;
13072 	if (net) {
13073 		/* we should hit here */
13074 		chk->whoTo = net;
13075 	} else {
13076 		chk->whoTo = asoc->primary_destination;
13077 	}
13078 	atomic_add_int(&chk->whoTo->ref_count, 1);
13079 	chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
13080 	chk->rec.chunk_id.can_take_data = 1;
13081 	drp->ch.chunk_type = SCTP_PACKET_DROPPED;
13082 	drp->ch.chunk_length = htons(chk->send_size);
13083 	spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
13084 	if (spc < 0) {
13085 		spc = 0;
13086 	}
13087 	drp->bottle_bw = htonl(spc);
13088 	if (asoc->my_rwnd) {
13089 		drp->current_onq = htonl(asoc->size_on_reasm_queue +
13090 		    asoc->size_on_all_streams +
13091 		    asoc->my_rwnd_control_len +
13092 		    stcb->sctp_socket->so_rcv.sb_cc);
13093 	} else {
13094 		/*-
13095 		 * If my rwnd is 0, possibly from mbuf depletion as well as
13096 		 * space used, tell the peer there is NO space aka onq == bw
13097 		 */
13098 		drp->current_onq = htonl(spc);
13099 	}
13100 	drp->reserved = 0;
13101 	datap = drp->data;
13102 	m_copydata(m, iphlen, len, (caddr_t)datap);
13103 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
13104 	asoc->ctrl_queue_cnt++;
13105 }
13106 
13107 void
13108 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
13109 {
13110 	struct sctp_association *asoc;
13111 	struct sctp_cwr_chunk *cwr;
13112 	struct sctp_tmit_chunk *chk;
13113 
13114 	asoc = &stcb->asoc;
13115 	SCTP_TCB_LOCK_ASSERT(stcb);
13116 	TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
13117 		if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
13118 			/* found a previous ECN_CWR update it if needed */
13119 			cwr = mtod(chk->data, struct sctp_cwr_chunk *);
13120 			if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
13121 			    MAX_TSN)) {
13122 				cwr->tsn = htonl(high_tsn);
13123 			}
13124 			return;
13125 		}
13126 	}
13127 	/* nope could not find one to update so we must build one */
13128 	sctp_alloc_a_chunk(stcb, chk);
13129 	if (chk == NULL) {
13130 		return;
13131 	}
13132 	chk->copy_by_ref = 0;
13133 	chk->rec.chunk_id.id = SCTP_ECN_CWR;
13134 	chk->rec.chunk_id.can_take_data = 1;
13135 	chk->asoc = &stcb->asoc;
13136 	chk->send_size = sizeof(struct sctp_cwr_chunk);
13137 	chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
13138 	if (chk->data == NULL) {
13139 		sctp_free_a_chunk(stcb, chk);
13140 		return;
13141 	}
13142 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13143 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13144 	chk->sent = SCTP_DATAGRAM_UNSENT;
13145 	chk->snd_count = 0;
13146 	chk->whoTo = net;
13147 	atomic_add_int(&chk->whoTo->ref_count, 1);
13148 	cwr = mtod(chk->data, struct sctp_cwr_chunk *);
13149 	cwr->ch.chunk_type = SCTP_ECN_CWR;
13150 	cwr->ch.chunk_flags = 0;
13151 	cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
13152 	cwr->tsn = htonl(high_tsn);
13153 	TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
13154 	asoc->ctrl_queue_cnt++;
13155 }
13156 
13157 void
13158 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
13159     int number_entries, uint16_t * list,
13160     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
13161 {
13162 	int len, old_len, i;
13163 	struct sctp_stream_reset_out_request *req_out;
13164 	struct sctp_chunkhdr *ch;
13165 
13166 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13167 
13168 
13169 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13170 
13171 	/* get to new offset for the param. */
13172 	req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
13173 	/* now how long will this param be? */
13174 	len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
13175 	req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
13176 	req_out->ph.param_length = htons(len);
13177 	req_out->request_seq = htonl(seq);
13178 	req_out->response_seq = htonl(resp_seq);
13179 	req_out->send_reset_at_tsn = htonl(last_sent);
13180 	if (number_entries) {
13181 		for (i = 0; i < number_entries; i++) {
13182 			req_out->list_of_streams[i] = htons(list[i]);
13183 		}
13184 	}
13185 	if (SCTP_SIZE32(len) > len) {
13186 		/*-
13187 		 * Need to worry about the pad we may end up adding to the
13188 		 * end. This is easy since the struct is either aligned to 4
13189 		 * bytes or 2 bytes off.
13190 		 */
13191 		req_out->list_of_streams[number_entries] = 0;
13192 	}
13193 	/* now fix the chunk length */
13194 	ch->chunk_length = htons(len + old_len);
13195 	chk->book_size = len + old_len;
13196 	chk->book_size_scale = 0;
13197 	chk->send_size = SCTP_SIZE32(chk->book_size);
13198 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13199 	return;
13200 }
13201 
13202 
13203 void
13204 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
13205     int number_entries, uint16_t * list,
13206     uint32_t seq)
13207 {
13208 	int len, old_len, i;
13209 	struct sctp_stream_reset_in_request *req_in;
13210 	struct sctp_chunkhdr *ch;
13211 
13212 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13213 
13214 
13215 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13216 
13217 	/* get to new offset for the param. */
13218 	req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
13219 	/* now how long will this param be? */
13220 	len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
13221 	req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
13222 	req_in->ph.param_length = htons(len);
13223 	req_in->request_seq = htonl(seq);
13224 	if (number_entries) {
13225 		for (i = 0; i < number_entries; i++) {
13226 			req_in->list_of_streams[i] = htons(list[i]);
13227 		}
13228 	}
13229 	if (SCTP_SIZE32(len) > len) {
13230 		/*-
13231 		 * Need to worry about the pad we may end up adding to the
13232 		 * end. This is easy since the struct is either aligned to 4
13233 		 * bytes or 2 bytes off.
13234 		 */
13235 		req_in->list_of_streams[number_entries] = 0;
13236 	}
13237 	/* now fix the chunk length */
13238 	ch->chunk_length = htons(len + old_len);
13239 	chk->book_size = len + old_len;
13240 	chk->book_size_scale = 0;
13241 	chk->send_size = SCTP_SIZE32(chk->book_size);
13242 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13243 	return;
13244 }
13245 
13246 
13247 void
13248 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
13249     uint32_t seq)
13250 {
13251 	int len, old_len;
13252 	struct sctp_stream_reset_tsn_request *req_tsn;
13253 	struct sctp_chunkhdr *ch;
13254 
13255 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13256 
13257 
13258 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13259 
13260 	/* get to new offset for the param. */
13261 	req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
13262 	/* now how long will this param be? */
13263 	len = sizeof(struct sctp_stream_reset_tsn_request);
13264 	req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
13265 	req_tsn->ph.param_length = htons(len);
13266 	req_tsn->request_seq = htonl(seq);
13267 
13268 	/* now fix the chunk length */
13269 	ch->chunk_length = htons(len + old_len);
13270 	chk->send_size = len + old_len;
13271 	chk->book_size = SCTP_SIZE32(chk->send_size);
13272 	chk->book_size_scale = 0;
13273 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
13274 	return;
13275 }
13276 
13277 void
13278 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
13279     uint32_t resp_seq, uint32_t result)
13280 {
13281 	int len, old_len;
13282 	struct sctp_stream_reset_response *resp;
13283 	struct sctp_chunkhdr *ch;
13284 
13285 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13286 
13287 
13288 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13289 
13290 	/* get to new offset for the param. */
13291 	resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
13292 	/* now how long will this param be? */
13293 	len = sizeof(struct sctp_stream_reset_response);
13294 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
13295 	resp->ph.param_length = htons(len);
13296 	resp->response_seq = htonl(resp_seq);
13297 	resp->result = ntohl(result);
13298 
13299 	/* now fix the chunk length */
13300 	ch->chunk_length = htons(len + old_len);
13301 	chk->book_size = len + old_len;
13302 	chk->book_size_scale = 0;
13303 	chk->send_size = SCTP_SIZE32(chk->book_size);
13304 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13305 	return;
13306 
13307 }
13308 
13309 
13310 void
13311 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
13312     uint32_t resp_seq, uint32_t result,
13313     uint32_t send_una, uint32_t recv_next)
13314 {
13315 	int len, old_len;
13316 	struct sctp_stream_reset_response_tsn *resp;
13317 	struct sctp_chunkhdr *ch;
13318 
13319 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13320 
13321 
13322 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13323 
13324 	/* get to new offset for the param. */
13325 	resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
13326 	/* now how long will this param be? */
13327 	len = sizeof(struct sctp_stream_reset_response_tsn);
13328 	resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
13329 	resp->ph.param_length = htons(len);
13330 	resp->response_seq = htonl(resp_seq);
13331 	resp->result = htonl(result);
13332 	resp->senders_next_tsn = htonl(send_una);
13333 	resp->receivers_next_tsn = htonl(recv_next);
13334 
13335 	/* now fix the chunk length */
13336 	ch->chunk_length = htons(len + old_len);
13337 	chk->book_size = len + old_len;
13338 	chk->send_size = SCTP_SIZE32(chk->book_size);
13339 	chk->book_size_scale = 0;
13340 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13341 	return;
13342 }
13343 
13344 static void
13345 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
13346     uint32_t seq,
13347     uint16_t adding)
13348 {
13349 	int len, old_len;
13350 	struct sctp_chunkhdr *ch;
13351 	struct sctp_stream_reset_add_strm *addstr;
13352 
13353 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13354 	old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13355 
13356 	/* get to new offset for the param. */
13357 	addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
13358 	/* now how long will this param be? */
13359 	len = sizeof(struct sctp_stream_reset_add_strm);
13360 
13361 	/* Fill it out. */
13362 	addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
13363 	addstr->ph.param_length = htons(len);
13364 	addstr->request_seq = htonl(seq);
13365 	addstr->number_of_streams = htons(adding);
13366 	addstr->reserved = 0;
13367 
13368 	/* now fix the chunk length */
13369 	ch->chunk_length = htons(len + old_len);
13370 	chk->send_size = len + old_len;
13371 	chk->book_size = SCTP_SIZE32(chk->send_size);
13372 	chk->book_size_scale = 0;
13373 	SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
13374 	return;
13375 }
13376 
13377 int
13378 sctp_send_str_reset_req(struct sctp_tcb *stcb,
13379     int number_entries, uint16_t * list,
13380     uint8_t send_out_req,
13381     uint32_t resp_seq,
13382     uint8_t send_in_req,
13383     uint8_t send_tsn_req,
13384     uint8_t add_stream,
13385     uint16_t adding
13386 )
13387 {
13388 
13389 	struct sctp_association *asoc;
13390 	struct sctp_tmit_chunk *chk;
13391 	struct sctp_chunkhdr *ch;
13392 	uint32_t seq;
13393 
13394 	asoc = &stcb->asoc;
13395 	if (asoc->stream_reset_outstanding) {
13396 		/*-
13397 		 * Already one pending, must get ACK back to clear the flag.
13398 		 */
13399 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
13400 		return (EBUSY);
13401 	}
13402 	if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
13403 	    (add_stream == 0)) {
13404 		/* nothing to do */
13405 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13406 		return (EINVAL);
13407 	}
13408 	if (send_tsn_req && (send_out_req || send_in_req)) {
13409 		/* error, can't do that */
13410 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13411 		return (EINVAL);
13412 	}
13413 	sctp_alloc_a_chunk(stcb, chk);
13414 	if (chk == NULL) {
13415 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13416 		return (ENOMEM);
13417 	}
13418 	chk->copy_by_ref = 0;
13419 	chk->rec.chunk_id.id = SCTP_STREAM_RESET;
13420 	chk->rec.chunk_id.can_take_data = 0;
13421 	chk->asoc = &stcb->asoc;
13422 	chk->book_size = sizeof(struct sctp_chunkhdr);
13423 	chk->send_size = SCTP_SIZE32(chk->book_size);
13424 	chk->book_size_scale = 0;
13425 
13426 	chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
13427 	if (chk->data == NULL) {
13428 		sctp_free_a_chunk(stcb, chk);
13429 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13430 		return (ENOMEM);
13431 	}
13432 	SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13433 
13434 	/* setup chunk parameters */
13435 	chk->sent = SCTP_DATAGRAM_UNSENT;
13436 	chk->snd_count = 0;
13437 	chk->whoTo = asoc->primary_destination;
13438 	atomic_add_int(&chk->whoTo->ref_count, 1);
13439 
13440 	ch = mtod(chk->data, struct sctp_chunkhdr *);
13441 	ch->chunk_type = SCTP_STREAM_RESET;
13442 	ch->chunk_flags = 0;
13443 	ch->chunk_length = htons(chk->book_size);
13444 	SCTP_BUF_LEN(chk->data) = chk->send_size;
13445 
13446 	seq = stcb->asoc.str_reset_seq_out;
13447 	if (send_out_req) {
13448 		sctp_add_stream_reset_out(chk, number_entries, list,
13449 		    seq, resp_seq, (stcb->asoc.sending_seq - 1));
13450 		asoc->stream_reset_out_is_outstanding = 1;
13451 		seq++;
13452 		asoc->stream_reset_outstanding++;
13453 	}
13454 	if (add_stream) {
13455 		sctp_add_a_stream(chk, seq, adding);
13456 		seq++;
13457 		asoc->stream_reset_outstanding++;
13458 	}
13459 	if (send_in_req) {
13460 		sctp_add_stream_reset_in(chk, number_entries, list, seq);
13461 		asoc->stream_reset_outstanding++;
13462 	}
13463 	if (send_tsn_req) {
13464 		sctp_add_stream_reset_tsn(chk, seq);
13465 		asoc->stream_reset_outstanding++;
13466 	}
13467 	asoc->str_reset = chk;
13468 
13469 	/* insert the chunk for sending */
13470 	TAILQ_INSERT_TAIL(&asoc->control_send_queue,
13471 	    chk,
13472 	    sctp_next);
13473 	asoc->ctrl_queue_cnt++;
13474 	sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
13475 	return (0);
13476 }
13477 
13478 void
13479 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
13480     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
13481 {
13482 	/*-
13483 	 * Formulate the abort message, and send it back down.
13484 	 */
13485 	struct mbuf *o_pak;
13486 	struct mbuf *mout;
13487 	struct sctp_abort_msg *abm;
13488 	struct ip *iph, *iph_out;
13489 	struct udphdr *udp;
13490 
13491 #ifdef INET6
13492 	struct ip6_hdr *ip6, *ip6_out;
13493 
13494 #endif
13495 	int iphlen_out, len;
13496 
13497 	/* don't respond to ABORT with ABORT */
13498 	if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
13499 		if (err_cause)
13500 			sctp_m_freem(err_cause);
13501 		return;
13502 	}
13503 	iph = mtod(m, struct ip *);
13504 	switch (iph->ip_v) {
13505 	case IPVERSION:
13506 		len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
13507 		break;
13508 #ifdef INET6
13509 	case IPV6_VERSION >> 4:
13510 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
13511 		break;
13512 #endif
13513 	default:
13514 		if (err_cause) {
13515 			sctp_m_freem(err_cause);
13516 		}
13517 		return;
13518 	}
13519 	if (port) {
13520 		len += sizeof(struct udphdr);
13521 	}
13522 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
13523 	if (mout == NULL) {
13524 		if (err_cause) {
13525 			sctp_m_freem(err_cause);
13526 		}
13527 		return;
13528 	}
13529 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
13530 	SCTP_BUF_LEN(mout) = len;
13531 	SCTP_BUF_NEXT(mout) = err_cause;
13532 	iph_out = NULL;
13533 #ifdef INET6
13534 	ip6_out = NULL;
13535 #endif
13536 	switch (iph->ip_v) {
13537 	case IPVERSION:
13538 		iph_out = mtod(mout, struct ip *);
13539 
13540 		/* Fill in the IP header for the ABORT */
13541 		iph_out->ip_v = IPVERSION;
13542 		iph_out->ip_hl = (sizeof(struct ip) / 4);
13543 		iph_out->ip_tos = (u_char)0;
13544 		iph_out->ip_id = 0;
13545 		iph_out->ip_off = 0;
13546 		iph_out->ip_ttl = MAXTTL;
13547 		if (port) {
13548 			iph_out->ip_p = IPPROTO_UDP;
13549 		} else {
13550 			iph_out->ip_p = IPPROTO_SCTP;
13551 		}
13552 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
13553 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
13554 		/* let IP layer calculate this */
13555 		iph_out->ip_sum = 0;
13556 
13557 		iphlen_out = sizeof(*iph_out);
13558 		abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
13559 		break;
13560 #ifdef INET6
13561 	case IPV6_VERSION >> 4:
13562 		ip6 = (struct ip6_hdr *)iph;
13563 		ip6_out = mtod(mout, struct ip6_hdr *);
13564 
13565 		/* Fill in the IP6 header for the ABORT */
13566 		ip6_out->ip6_flow = ip6->ip6_flow;
13567 		ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
13568 		if (port) {
13569 			ip6_out->ip6_nxt = IPPROTO_UDP;
13570 		} else {
13571 			ip6_out->ip6_nxt = IPPROTO_SCTP;
13572 		}
13573 		ip6_out->ip6_src = ip6->ip6_dst;
13574 		ip6_out->ip6_dst = ip6->ip6_src;
13575 
13576 		iphlen_out = sizeof(*ip6_out);
13577 		abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
13578 		break;
13579 #endif				/* INET6 */
13580 	default:
13581 		/* Currently not supported */
13582 		sctp_m_freem(mout);
13583 		return;
13584 	}
13585 
13586 	udp = (struct udphdr *)abm;
13587 	if (port) {
13588 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
13589 		udp->uh_dport = port;
13590 		/* set udp->uh_ulen later */
13591 		udp->uh_sum = 0;
13592 		iphlen_out += sizeof(struct udphdr);
13593 		abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
13594 	}
13595 	abm->sh.src_port = sh->dest_port;
13596 	abm->sh.dest_port = sh->src_port;
13597 	abm->sh.checksum = 0;
13598 	if (vtag == 0) {
13599 		abm->sh.v_tag = sh->v_tag;
13600 		abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
13601 	} else {
13602 		abm->sh.v_tag = htonl(vtag);
13603 		abm->msg.ch.chunk_flags = 0;
13604 	}
13605 	abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
13606 
13607 	if (err_cause) {
13608 		struct mbuf *m_tmp = err_cause;
13609 		int err_len = 0;
13610 
13611 		/* get length of the err_cause chain */
13612 		while (m_tmp != NULL) {
13613 			err_len += SCTP_BUF_LEN(m_tmp);
13614 			m_tmp = SCTP_BUF_NEXT(m_tmp);
13615 		}
13616 		len = SCTP_BUF_LEN(mout) + err_len;
13617 		if (err_len % 4) {
13618 			/* need pad at end of chunk */
13619 			uint32_t cpthis = 0;
13620 			int padlen;
13621 
13622 			padlen = 4 - (len % 4);
13623 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
13624 			len += padlen;
13625 		}
13626 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
13627 	} else {
13628 		len = SCTP_BUF_LEN(mout);
13629 		abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
13630 	}
13631 
13632 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
13633 		/* no mbuf's */
13634 		sctp_m_freem(mout);
13635 		return;
13636 	}
13637 	if (iph_out != NULL) {
13638 		sctp_route_t ro;
13639 		struct sctp_tcb *stcb = NULL;
13640 		int ret;
13641 
13642 		/* zap the stack pointer to the route */
13643 		bzero(&ro, sizeof ro);
13644 		if (port) {
13645 			udp->uh_ulen = htons(len - sizeof(struct ip));
13646 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
13647 		}
13648 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
13649 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
13650 		/* set IPv4 length */
13651 		iph_out->ip_len = len;
13652 		/* out it goes */
13653 #ifdef  SCTP_PACKET_LOGGING
13654 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13655 			sctp_packet_log(mout, len);
13656 #endif
13657 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
13658 		if (port) {
13659 			abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
13660 			SCTP_STAT_INCR(sctps_sendswcrc);
13661 			SCTP_ENABLE_UDP_CSUM(o_pak);
13662 		} else {
13663 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
13664 			mout->m_pkthdr.csum_data = 0;	/* FIXME MT */
13665 			SCTP_STAT_INCR(sctps_sendhwcrc);
13666 		}
13667 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
13668 
13669 		/* Free the route if we got one back */
13670 		if (ro.ro_rt)
13671 			RTFREE(ro.ro_rt);
13672 	}
13673 #ifdef INET6
13674 	if (ip6_out != NULL) {
13675 		struct route_in6 ro;
13676 		int ret;
13677 		struct sctp_tcb *stcb = NULL;
13678 		struct ifnet *ifp = NULL;
13679 
13680 		/* zap the stack pointer to the route */
13681 		bzero(&ro, sizeof(ro));
13682 		if (port) {
13683 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
13684 		}
13685 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
13686 		SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
13687 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
13688 #ifdef  SCTP_PACKET_LOGGING
13689 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13690 			sctp_packet_log(mout, len);
13691 #endif
13692 		abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
13693 		SCTP_STAT_INCR(sctps_sendswcrc);
13694 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
13695 		if (port) {
13696 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
13697 				udp->uh_sum = 0xffff;
13698 			}
13699 		}
13700 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
13701 
13702 		/* Free the route if we got one back */
13703 		if (ro.ro_rt)
13704 			RTFREE(ro.ro_rt);
13705 	}
13706 #endif
13707 	SCTP_STAT_INCR(sctps_sendpackets);
13708 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
13709 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
13710 }
13711 
13712 void
13713 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
13714     uint32_t vrf_id, uint16_t port)
13715 {
13716 	struct mbuf *o_pak;
13717 	struct sctphdr *sh, *sh_out;
13718 	struct sctp_chunkhdr *ch;
13719 	struct ip *iph, *iph_out;
13720 	struct udphdr *udp = NULL;
13721 	struct mbuf *mout;
13722 
13723 #ifdef INET6
13724 	struct ip6_hdr *ip6, *ip6_out;
13725 
13726 #endif
13727 	int iphlen_out, len;
13728 
13729 	iph = mtod(m, struct ip *);
13730 	sh = (struct sctphdr *)((caddr_t)iph + iphlen);
13731 	switch (iph->ip_v) {
13732 	case IPVERSION:
13733 		len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
13734 		break;
13735 #ifdef INET6
13736 	case IPV6_VERSION >> 4:
13737 		len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
13738 		break;
13739 #endif
13740 	default:
13741 		if (scm) {
13742 			sctp_m_freem(scm);
13743 		}
13744 		return;
13745 	}
13746 	if (port) {
13747 		len += sizeof(struct udphdr);
13748 	}
13749 	mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
13750 	if (mout == NULL) {
13751 		if (scm) {
13752 			sctp_m_freem(scm);
13753 		}
13754 		return;
13755 	}
13756 	SCTP_BUF_RESV_UF(mout, max_linkhdr);
13757 	SCTP_BUF_LEN(mout) = len;
13758 	SCTP_BUF_NEXT(mout) = scm;
13759 	iph_out = NULL;
13760 #ifdef INET6
13761 	ip6_out = NULL;
13762 #endif
13763 	switch (iph->ip_v) {
13764 	case IPVERSION:
13765 		iph_out = mtod(mout, struct ip *);
13766 
13767 		/* Fill in the IP header for the ABORT */
13768 		iph_out->ip_v = IPVERSION;
13769 		iph_out->ip_hl = (sizeof(struct ip) / 4);
13770 		iph_out->ip_tos = (u_char)0;
13771 		iph_out->ip_id = 0;
13772 		iph_out->ip_off = 0;
13773 		iph_out->ip_ttl = MAXTTL;
13774 		if (port) {
13775 			iph_out->ip_p = IPPROTO_UDP;
13776 		} else {
13777 			iph_out->ip_p = IPPROTO_SCTP;
13778 		}
13779 		iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
13780 		iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
13781 		/* let IP layer calculate this */
13782 		iph_out->ip_sum = 0;
13783 
13784 		iphlen_out = sizeof(struct ip);
13785 		sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
13786 		break;
13787 #ifdef INET6
13788 	case IPV6_VERSION >> 4:
13789 		ip6 = (struct ip6_hdr *)iph;
13790 		ip6_out = mtod(mout, struct ip6_hdr *);
13791 
13792 		/* Fill in the IP6 header for the ABORT */
13793 		ip6_out->ip6_flow = ip6->ip6_flow;
13794 		ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
13795 		if (port) {
13796 			ip6_out->ip6_nxt = IPPROTO_UDP;
13797 		} else {
13798 			ip6_out->ip6_nxt = IPPROTO_SCTP;
13799 		}
13800 		ip6_out->ip6_src = ip6->ip6_dst;
13801 		ip6_out->ip6_dst = ip6->ip6_src;
13802 
13803 		iphlen_out = sizeof(struct ip6_hdr);
13804 		sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
13805 		break;
13806 #endif				/* INET6 */
13807 	default:
13808 		/* Currently not supported */
13809 		sctp_m_freem(mout);
13810 		return;
13811 	}
13812 
13813 	udp = (struct udphdr *)sh_out;
13814 	if (port) {
13815 		udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
13816 		udp->uh_dport = port;
13817 		/* set udp->uh_ulen later */
13818 		udp->uh_sum = 0;
13819 		iphlen_out += sizeof(struct udphdr);
13820 		sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
13821 	}
13822 	sh_out->src_port = sh->dest_port;
13823 	sh_out->dest_port = sh->src_port;
13824 	sh_out->v_tag = vtag;
13825 	sh_out->checksum = 0;
13826 
13827 	ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
13828 	ch->chunk_type = SCTP_OPERATION_ERROR;
13829 	ch->chunk_flags = 0;
13830 
13831 	if (scm) {
13832 		struct mbuf *m_tmp = scm;
13833 		int cause_len = 0;
13834 
13835 		/* get length of the err_cause chain */
13836 		while (m_tmp != NULL) {
13837 			cause_len += SCTP_BUF_LEN(m_tmp);
13838 			m_tmp = SCTP_BUF_NEXT(m_tmp);
13839 		}
13840 		len = SCTP_BUF_LEN(mout) + cause_len;
13841 		if (cause_len % 4) {
13842 			/* need pad at end of chunk */
13843 			uint32_t cpthis = 0;
13844 			int padlen;
13845 
13846 			padlen = 4 - (len % 4);
13847 			m_copyback(mout, len, padlen, (caddr_t)&cpthis);
13848 			len += padlen;
13849 		}
13850 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
13851 	} else {
13852 		len = SCTP_BUF_LEN(mout);
13853 		ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
13854 	}
13855 
13856 	if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
13857 		/* no mbuf's */
13858 		sctp_m_freem(mout);
13859 		return;
13860 	}
13861 	if (iph_out != NULL) {
13862 		sctp_route_t ro;
13863 		struct sctp_tcb *stcb = NULL;
13864 		int ret;
13865 
13866 		/* zap the stack pointer to the route */
13867 		bzero(&ro, sizeof ro);
13868 		if (port) {
13869 			udp->uh_ulen = htons(len - sizeof(struct ip));
13870 			udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
13871 		}
13872 		/* set IPv4 length */
13873 		iph_out->ip_len = len;
13874 		/* out it goes */
13875 #ifdef  SCTP_PACKET_LOGGING
13876 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13877 			sctp_packet_log(mout, len);
13878 #endif
13879 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
13880 		if (port) {
13881 			sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
13882 			SCTP_STAT_INCR(sctps_sendswcrc);
13883 			SCTP_ENABLE_UDP_CSUM(o_pak);
13884 		} else {
13885 			mout->m_pkthdr.csum_flags = CSUM_SCTP;
13886 			mout->m_pkthdr.csum_data = 0;	/* FIXME MT */
13887 			SCTP_STAT_INCR(sctps_sendhwcrc);
13888 		}
13889 		SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
13890 
13891 		/* Free the route if we got one back */
13892 		if (ro.ro_rt)
13893 			RTFREE(ro.ro_rt);
13894 	}
13895 #ifdef INET6
13896 	if (ip6_out != NULL) {
13897 		struct route_in6 ro;
13898 		int ret;
13899 		struct sctp_tcb *stcb = NULL;
13900 		struct ifnet *ifp = NULL;
13901 
13902 		/* zap the stack pointer to the route */
13903 		bzero(&ro, sizeof(ro));
13904 		if (port) {
13905 			udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
13906 		}
13907 		ip6_out->ip6_plen = len - sizeof(*ip6_out);
13908 #ifdef  SCTP_PACKET_LOGGING
13909 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13910 			sctp_packet_log(mout, len);
13911 #endif
13912 		sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
13913 		SCTP_STAT_INCR(sctps_sendswcrc);
13914 		SCTP_ATTACH_CHAIN(o_pak, mout, len);
13915 		if (port) {
13916 			if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
13917 				udp->uh_sum = 0xffff;
13918 			}
13919 		}
13920 		SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
13921 
13922 		/* Free the route if we got one back */
13923 		if (ro.ro_rt)
13924 			RTFREE(ro.ro_rt);
13925 	}
13926 #endif
13927 	SCTP_STAT_INCR(sctps_sendpackets);
13928 	SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
13929 	SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
13930 }
13931 
13932 static struct mbuf *
13933 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
13934     struct uio *uio,
13935     struct sctp_sndrcvinfo *srcv,
13936     int max_send_len,
13937     int user_marks_eor,
13938     int *error,
13939     uint32_t * sndout,
13940     struct mbuf **new_tail)
13941 {
13942 	struct mbuf *m;
13943 
13944 	m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
13945 	    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
13946 	if (m == NULL) {
13947 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13948 		*error = ENOMEM;
13949 	} else {
13950 		*sndout = m_length(m, NULL);
13951 		*new_tail = m_last(m);
13952 	}
13953 	return (m);
13954 }
13955 
13956 static int
13957 sctp_copy_one(struct sctp_stream_queue_pending *sp,
13958     struct uio *uio,
13959     int resv_upfront)
13960 {
13961 	int left;
13962 
13963 	left = sp->length;
13964 	sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
13965 	    resv_upfront, 0);
13966 	if (sp->data == NULL) {
13967 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13968 		return (ENOMEM);
13969 	}
13970 	sp->tail_mbuf = m_last(sp->data);
13971 	return (0);
13972 }
13973 
13974 
13975 
13976 static struct sctp_stream_queue_pending *
13977 sctp_copy_it_in(struct sctp_tcb *stcb,
13978     struct sctp_association *asoc,
13979     struct sctp_sndrcvinfo *srcv,
13980     struct uio *uio,
13981     struct sctp_nets *net,
13982     int max_send_len,
13983     int user_marks_eor,
13984     int *error,
13985     int non_blocking)
13986 {
13987 	/*-
13988 	 * This routine must be very careful in its work. Protocol
13989 	 * processing is up and running so care must be taken to spl...()
13990 	 * when you need to do something that may effect the stcb/asoc. The
13991 	 * sb is locked however. When data is copied the protocol processing
13992 	 * should be enabled since this is a slower operation...
13993 	 */
13994 	struct sctp_stream_queue_pending *sp = NULL;
13995 	int resv_in_first;
13996 
13997 	*error = 0;
13998 	/* Now can we send this? */
13999 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
14000 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
14001 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
14002 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
14003 		/* got data while shutting down */
14004 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14005 		*error = ECONNRESET;
14006 		goto out_now;
14007 	}
14008 	sctp_alloc_a_strmoq(stcb, sp);
14009 	if (sp == NULL) {
14010 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
14011 		*error = ENOMEM;
14012 		goto out_now;
14013 	}
14014 	sp->act_flags = 0;
14015 	sp->sender_all_done = 0;
14016 	sp->sinfo_flags = srcv->sinfo_flags;
14017 	sp->timetolive = srcv->sinfo_timetolive;
14018 	sp->ppid = srcv->sinfo_ppid;
14019 	sp->context = srcv->sinfo_context;
14020 	sp->strseq = 0;
14021 	(void)SCTP_GETTIME_TIMEVAL(&sp->ts);
14022 
14023 	sp->stream = srcv->sinfo_stream;
14024 	sp->length = min(uio->uio_resid, max_send_len);
14025 	if ((sp->length == (uint32_t) uio->uio_resid) &&
14026 	    ((user_marks_eor == 0) ||
14027 	    (srcv->sinfo_flags & SCTP_EOF) ||
14028 	    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
14029 		sp->msg_is_complete = 1;
14030 	} else {
14031 		sp->msg_is_complete = 0;
14032 	}
14033 	sp->sender_all_done = 0;
14034 	sp->some_taken = 0;
14035 	sp->put_last_out = 0;
14036 	resv_in_first = sizeof(struct sctp_data_chunk);
14037 	sp->data = sp->tail_mbuf = NULL;
14038 	if (sp->length == 0) {
14039 		*error = 0;
14040 		goto skip_copy;
14041 	}
14042 	sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
14043 	if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
14044 		sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
14045 		sp->holds_key_ref = 1;
14046 	}
14047 	*error = sctp_copy_one(sp, uio, resv_in_first);
14048 skip_copy:
14049 	if (*error) {
14050 		sctp_free_a_strmoq(stcb, sp);
14051 		sp = NULL;
14052 	} else {
14053 		if (sp->sinfo_flags & SCTP_ADDR_OVER) {
14054 			sp->net = net;
14055 			sp->addr_over = 1;
14056 		} else {
14057 			sp->net = asoc->primary_destination;
14058 			sp->addr_over = 0;
14059 		}
14060 		atomic_add_int(&sp->net->ref_count, 1);
14061 		sctp_set_prsctp_policy(stcb, sp);
14062 	}
14063 out_now:
14064 	return (sp);
14065 }
14066 
14067 
14068 int
14069 sctp_sosend(struct socket *so,
14070     struct sockaddr *addr,
14071     struct uio *uio,
14072     struct mbuf *top,
14073     struct mbuf *control,
14074     int flags,
14075     struct thread *p
14076 )
14077 {
14078 	struct sctp_inpcb *inp;
14079 	int error, use_rcvinfo = 0;
14080 	struct sctp_sndrcvinfo srcv;
14081 	struct sockaddr *addr_to_use;
14082 
14083 #ifdef INET6
14084 	struct sockaddr_in sin;
14085 
14086 #endif
14087 
14088 	inp = (struct sctp_inpcb *)so->so_pcb;
14089 	if (control) {
14090 		/* process cmsg snd/rcv info (maybe a assoc-id) */
14091 		if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
14092 		    sizeof(srcv))) {
14093 			/* got one */
14094 			use_rcvinfo = 1;
14095 		}
14096 	}
14097 	addr_to_use = addr;
14098 #if defined(INET6)  && !defined(__Userspace__)	/* TODO port in6_sin6_2_sin */
14099 	if ((addr) && (addr->sa_family == AF_INET6)) {
14100 		struct sockaddr_in6 *sin6;
14101 
14102 		sin6 = (struct sockaddr_in6 *)addr;
14103 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
14104 			in6_sin6_2_sin(&sin, sin6);
14105 			addr_to_use = (struct sockaddr *)&sin;
14106 		}
14107 	}
14108 #endif
14109 	error = sctp_lower_sosend(so, addr_to_use, uio, top,
14110 	    control,
14111 	    flags,
14112 	    use_rcvinfo, &srcv
14113 	    ,p
14114 	    );
14115 	return (error);
14116 }
14117 
14118 
14119 int
14120 sctp_lower_sosend(struct socket *so,
14121     struct sockaddr *addr,
14122     struct uio *uio,
14123     struct mbuf *i_pak,
14124     struct mbuf *control,
14125     int flags,
14126     int use_rcvinfo,
14127     struct sctp_sndrcvinfo *srcv
14128     ,
14129     struct thread *p
14130 )
14131 {
14132 	unsigned int sndlen = 0, max_len;
14133 	int error, len;
14134 	struct mbuf *top = NULL;
14135 	int queue_only = 0, queue_only_for_init = 0;
14136 	int free_cnt_applied = 0;
14137 	int un_sent = 0;
14138 	int now_filled = 0;
14139 	unsigned int inqueue_bytes = 0;
14140 	struct sctp_block_entry be;
14141 	struct sctp_inpcb *inp;
14142 	struct sctp_tcb *stcb = NULL;
14143 	struct timeval now;
14144 	struct sctp_nets *net;
14145 	struct sctp_association *asoc;
14146 	struct sctp_inpcb *t_inp;
14147 	int user_marks_eor;
14148 	int create_lock_applied = 0;
14149 	int nagle_applies = 0;
14150 	int some_on_control = 0;
14151 	int got_all_of_the_send = 0;
14152 	int hold_tcblock = 0;
14153 	int non_blocking = 0;
14154 	int temp_flags = 0;
14155 	uint32_t local_add_more, local_soresv = 0;
14156 
14157 	error = 0;
14158 	net = NULL;
14159 	stcb = NULL;
14160 	asoc = NULL;
14161 
14162 	t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
14163 	if (inp == NULL) {
14164 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14165 		error = EFAULT;
14166 		if (i_pak) {
14167 			SCTP_RELEASE_PKT(i_pak);
14168 		}
14169 		return (error);
14170 	}
14171 	if ((uio == NULL) && (i_pak == NULL)) {
14172 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14173 		return (EINVAL);
14174 	}
14175 	user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
14176 	atomic_add_int(&inp->total_sends, 1);
14177 	if (uio) {
14178 		if (uio->uio_resid < 0) {
14179 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14180 			return (EINVAL);
14181 		}
14182 		sndlen = uio->uio_resid;
14183 	} else {
14184 		top = SCTP_HEADER_TO_CHAIN(i_pak);
14185 		sndlen = SCTP_HEADER_LEN(i_pak);
14186 	}
14187 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
14188 	    addr,
14189 	    sndlen);
14190 	/*-
14191 	 * Pre-screen address, if one is given the sin-len
14192 	 * must be set correctly!
14193 	 */
14194 	if (addr) {
14195 		if ((addr->sa_family == AF_INET) &&
14196 		    (addr->sa_len != sizeof(struct sockaddr_in))) {
14197 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14198 			error = EINVAL;
14199 			goto out_unlocked;
14200 		} else if ((addr->sa_family == AF_INET6) &&
14201 		    (addr->sa_len != sizeof(struct sockaddr_in6))) {
14202 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14203 			error = EINVAL;
14204 			goto out_unlocked;
14205 		}
14206 	}
14207 	hold_tcblock = 0;
14208 
14209 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
14210 	    (inp->sctp_socket->so_qlimit)) {
14211 		/* The listener can NOT send */
14212 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14213 		error = EFAULT;
14214 		goto out_unlocked;
14215 	}
14216 	if ((use_rcvinfo) && srcv) {
14217 		if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
14218 		    PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
14219 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14220 			error = EINVAL;
14221 			goto out_unlocked;
14222 		}
14223 		if (srcv->sinfo_flags)
14224 			SCTP_STAT_INCR(sctps_sends_with_flags);
14225 
14226 		if (srcv->sinfo_flags & SCTP_SENDALL) {
14227 			/* its a sendall */
14228 			error = sctp_sendall(inp, uio, top, srcv);
14229 			top = NULL;
14230 			goto out_unlocked;
14231 		}
14232 	}
14233 	/* now we must find the assoc */
14234 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
14235 	    (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
14236 		SCTP_INP_RLOCK(inp);
14237 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
14238 		if (stcb == NULL) {
14239 			SCTP_INP_RUNLOCK(inp);
14240 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
14241 			error = ENOTCONN;
14242 			goto out_unlocked;
14243 		}
14244 		hold_tcblock = 0;
14245 		SCTP_INP_RUNLOCK(inp);
14246 		if (addr) {
14247 			/* Must locate the net structure if addr given */
14248 			net = sctp_findnet(stcb, addr);
14249 			if (net) {
14250 				/* validate port was 0 or correct */
14251 				struct sockaddr_in *sin;
14252 
14253 				sin = (struct sockaddr_in *)addr;
14254 				if ((sin->sin_port != 0) &&
14255 				    (sin->sin_port != stcb->rport)) {
14256 					net = NULL;
14257 				}
14258 			}
14259 			temp_flags |= SCTP_ADDR_OVER;
14260 		} else
14261 			net = stcb->asoc.primary_destination;
14262 		if (addr && (net == NULL)) {
14263 			/* Could not find address, was it legal */
14264 			if (addr->sa_family == AF_INET) {
14265 				struct sockaddr_in *sin;
14266 
14267 				sin = (struct sockaddr_in *)addr;
14268 				if (sin->sin_addr.s_addr == 0) {
14269 					if ((sin->sin_port == 0) ||
14270 					    (sin->sin_port == stcb->rport)) {
14271 						net = stcb->asoc.primary_destination;
14272 					}
14273 				}
14274 			} else {
14275 				struct sockaddr_in6 *sin6;
14276 
14277 				sin6 = (struct sockaddr_in6 *)addr;
14278 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
14279 					if ((sin6->sin6_port == 0) ||
14280 					    (sin6->sin6_port == stcb->rport)) {
14281 						net = stcb->asoc.primary_destination;
14282 					}
14283 				}
14284 			}
14285 		}
14286 		if (net == NULL) {
14287 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14288 			error = EINVAL;
14289 			goto out_unlocked;
14290 		}
14291 	} else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
14292 		stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
14293 		if (stcb) {
14294 			if (addr)
14295 				/*
14296 				 * Must locate the net structure if addr
14297 				 * given
14298 				 */
14299 				net = sctp_findnet(stcb, addr);
14300 			else
14301 				net = stcb->asoc.primary_destination;
14302 			if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
14303 			    ((net == NULL) || (addr == NULL))) {
14304 				struct sockaddr_in *sin;
14305 
14306 				if (addr == NULL) {
14307 					SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14308 					error = EINVAL;
14309 					goto out_unlocked;
14310 				}
14311 				sin = (struct sockaddr_in *)addr;
14312 				/* Validate port is 0 or correct */
14313 				if ((sin->sin_port != 0) &&
14314 				    (sin->sin_port != stcb->rport)) {
14315 					net = NULL;
14316 				}
14317 			}
14318 		}
14319 		hold_tcblock = 0;
14320 	} else if (addr) {
14321 		/*-
14322 		 * Since we did not use findep we must
14323 		 * increment it, and if we don't find a tcb
14324 		 * decrement it.
14325 		 */
14326 		SCTP_INP_WLOCK(inp);
14327 		SCTP_INP_INCR_REF(inp);
14328 		SCTP_INP_WUNLOCK(inp);
14329 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
14330 		if (stcb == NULL) {
14331 			SCTP_INP_WLOCK(inp);
14332 			SCTP_INP_DECR_REF(inp);
14333 			SCTP_INP_WUNLOCK(inp);
14334 		} else {
14335 			hold_tcblock = 1;
14336 		}
14337 	}
14338 	if ((stcb == NULL) && (addr)) {
14339 		/* Possible implicit send? */
14340 		SCTP_ASOC_CREATE_LOCK(inp);
14341 		create_lock_applied = 1;
14342 		if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
14343 		    (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
14344 			/* Should I really unlock ? */
14345 			SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14346 			error = EFAULT;
14347 			goto out_unlocked;
14348 
14349 		}
14350 		if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
14351 		    (addr->sa_family == AF_INET6)) {
14352 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14353 			error = EINVAL;
14354 			goto out_unlocked;
14355 		}
14356 		SCTP_INP_WLOCK(inp);
14357 		SCTP_INP_INCR_REF(inp);
14358 		SCTP_INP_WUNLOCK(inp);
14359 		/* With the lock applied look again */
14360 		stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
14361 		if (stcb == NULL) {
14362 			SCTP_INP_WLOCK(inp);
14363 			SCTP_INP_DECR_REF(inp);
14364 			SCTP_INP_WUNLOCK(inp);
14365 		} else {
14366 			hold_tcblock = 1;
14367 		}
14368 		if (t_inp != inp) {
14369 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
14370 			error = ENOTCONN;
14371 			goto out_unlocked;
14372 		}
14373 	}
14374 	if (stcb == NULL) {
14375 		if (addr == NULL) {
14376 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
14377 			error = ENOENT;
14378 			goto out_unlocked;
14379 		} else {
14380 			/*
14381 			 * UDP style, we must go ahead and start the INIT
14382 			 * process
14383 			 */
14384 			uint32_t vrf_id;
14385 
14386 			if ((use_rcvinfo) && (srcv) &&
14387 			    ((srcv->sinfo_flags & SCTP_ABORT) ||
14388 			    ((srcv->sinfo_flags & SCTP_EOF) &&
14389 			    (sndlen == 0)))) {
14390 				/*-
14391 				 * User asks to abort a non-existant assoc,
14392 				 * or EOF a non-existant assoc with no data
14393 				 */
14394 				SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
14395 				error = ENOENT;
14396 				goto out_unlocked;
14397 			}
14398 			/* get an asoc/stcb struct */
14399 			vrf_id = inp->def_vrf_id;
14400 #ifdef INVARIANTS
14401 			if (create_lock_applied == 0) {
14402 				panic("Error, should hold create lock and I don't?");
14403 			}
14404 #endif
14405 			stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
14406 			    p
14407 			    );
14408 			if (stcb == NULL) {
14409 				/* Error is setup for us in the call */
14410 				goto out_unlocked;
14411 			}
14412 			if (create_lock_applied) {
14413 				SCTP_ASOC_CREATE_UNLOCK(inp);
14414 				create_lock_applied = 0;
14415 			} else {
14416 				SCTP_PRINTF("Huh-3? create lock should have been on??\n");
14417 			}
14418 			/*
14419 			 * Turn on queue only flag to prevent data from
14420 			 * being sent
14421 			 */
14422 			queue_only = 1;
14423 			asoc = &stcb->asoc;
14424 			SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
14425 			(void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
14426 
14427 			/* initialize authentication params for the assoc */
14428 			sctp_initialize_auth_params(inp, stcb);
14429 
14430 			if (control) {
14431 				/*
14432 				 * see if a init structure exists in cmsg
14433 				 * headers
14434 				 */
14435 				struct sctp_initmsg initm;
14436 				int i;
14437 
14438 				if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
14439 				    sizeof(initm))) {
14440 					/*
14441 					 * we have an INIT override of the
14442 					 * default
14443 					 */
14444 					if (initm.sinit_max_attempts)
14445 						asoc->max_init_times = initm.sinit_max_attempts;
14446 					if (initm.sinit_num_ostreams)
14447 						asoc->pre_open_streams = initm.sinit_num_ostreams;
14448 					if (initm.sinit_max_instreams)
14449 						asoc->max_inbound_streams = initm.sinit_max_instreams;
14450 					if (initm.sinit_max_init_timeo)
14451 						asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
14452 					if (asoc->streamoutcnt < asoc->pre_open_streams) {
14453 						struct sctp_stream_out *tmp_str;
14454 						int had_lock = 0;
14455 
14456 						/* Default is NOT correct */
14457 						SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
14458 						    asoc->streamoutcnt, asoc->pre_open_streams);
14459 						/*
14460 						 * What happens if this
14461 						 * fails? we panic ...
14462 						 */
14463 
14464 						if (hold_tcblock) {
14465 							had_lock = 1;
14466 							SCTP_TCB_UNLOCK(stcb);
14467 						}
14468 						SCTP_MALLOC(tmp_str,
14469 						    struct sctp_stream_out *,
14470 						    (asoc->pre_open_streams *
14471 						    sizeof(struct sctp_stream_out)),
14472 						    SCTP_M_STRMO);
14473 						if (had_lock) {
14474 							SCTP_TCB_LOCK(stcb);
14475 						}
14476 						if (tmp_str != NULL) {
14477 							SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
14478 							asoc->strmout = tmp_str;
14479 							asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
14480 						} else {
14481 							asoc->pre_open_streams = asoc->streamoutcnt;
14482 						}
14483 						for (i = 0; i < asoc->streamoutcnt; i++) {
14484 							/*-
14485 							 * inbound side must be set
14486 							 * to 0xffff, also NOTE when
14487 							 * we get the INIT-ACK back
14488 							 * (for INIT sender) we MUST
14489 							 * reduce the count
14490 							 * (streamoutcnt) but first
14491 							 * check if we sent to any
14492 							 * of the upper streams that
14493 							 * were dropped (if some
14494 							 * were). Those that were
14495 							 * dropped must be notified
14496 							 * to the upper layer as
14497 							 * failed to send.
14498 							 */
14499 							asoc->strmout[i].next_sequence_sent = 0x0;
14500 							TAILQ_INIT(&asoc->strmout[i].outqueue);
14501 							asoc->strmout[i].stream_no = i;
14502 							asoc->strmout[i].last_msg_incomplete = 0;
14503 							asoc->strmout[i].next_spoke.tqe_next = 0;
14504 							asoc->strmout[i].next_spoke.tqe_prev = 0;
14505 						}
14506 					}
14507 				}
14508 			}
14509 			hold_tcblock = 1;
14510 			/* out with the INIT */
14511 			queue_only_for_init = 1;
14512 			/*-
14513 			 * we may want to dig in after this call and adjust the MTU
14514 			 * value. It defaulted to 1500 (constant) but the ro
14515 			 * structure may now have an update and thus we may need to
14516 			 * change it BEFORE we append the message.
14517 			 */
14518 			net = stcb->asoc.primary_destination;
14519 			asoc = &stcb->asoc;
14520 		}
14521 	}
14522 	if ((SCTP_SO_IS_NBIO(so)
14523 	    || (flags & MSG_NBIO)
14524 	    )) {
14525 		non_blocking = 1;
14526 	}
14527 	asoc = &stcb->asoc;
14528 	atomic_add_int(&stcb->total_sends, 1);
14529 
14530 	if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
14531 		if (sndlen > asoc->smallest_mtu) {
14532 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14533 			error = EMSGSIZE;
14534 			goto out_unlocked;
14535 		}
14536 	}
14537 	/* would we block? */
14538 	if (non_blocking) {
14539 		if (hold_tcblock == 0) {
14540 			SCTP_TCB_LOCK(stcb);
14541 			hold_tcblock = 1;
14542 		}
14543 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14544 		if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
14545 		    (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14546 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
14547 			if (sndlen > SCTP_SB_LIMIT_SND(so))
14548 				error = EMSGSIZE;
14549 			else
14550 				error = EWOULDBLOCK;
14551 			goto out_unlocked;
14552 		}
14553 		stcb->asoc.sb_send_resv += sndlen;
14554 		SCTP_TCB_UNLOCK(stcb);
14555 		hold_tcblock = 0;
14556 	} else {
14557 		atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
14558 	}
14559 	local_soresv = sndlen;
14560 	/* Keep the stcb from being freed under our feet */
14561 	if (free_cnt_applied) {
14562 #ifdef INVARIANTS
14563 		panic("refcnt already incremented");
14564 #else
14565 		printf("refcnt:1 already incremented?\n");
14566 #endif
14567 	} else {
14568 		atomic_add_int(&stcb->asoc.refcnt, 1);
14569 		free_cnt_applied = 1;
14570 	}
14571 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14572 		SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14573 		error = ECONNRESET;
14574 		goto out_unlocked;
14575 	}
14576 	if (create_lock_applied) {
14577 		SCTP_ASOC_CREATE_UNLOCK(inp);
14578 		create_lock_applied = 0;
14579 	}
14580 	if (asoc->stream_reset_outstanding) {
14581 		/*
14582 		 * Can't queue any data while stream reset is underway.
14583 		 */
14584 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
14585 		error = EAGAIN;
14586 		goto out_unlocked;
14587 	}
14588 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
14589 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
14590 		queue_only = 1;
14591 	}
14592 	if ((use_rcvinfo == 0) || (srcv == NULL)) {
14593 		/* Grab the default stuff from the asoc */
14594 		srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
14595 	}
14596 	/* we are now done with all control */
14597 	if (control) {
14598 		sctp_m_freem(control);
14599 		control = NULL;
14600 	}
14601 	if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
14602 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
14603 	    (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
14604 	    (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
14605 		if ((use_rcvinfo) &&
14606 		    (srcv->sinfo_flags & SCTP_ABORT)) {
14607 			;
14608 		} else {
14609 			SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14610 			error = ECONNRESET;
14611 			goto out_unlocked;
14612 		}
14613 	}
14614 	/* Ok, we will attempt a msgsnd :> */
14615 	if (p) {
14616 		p->td_ru.ru_msgsnd++;
14617 	}
14618 	if (stcb) {
14619 		if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
14620 			net = stcb->asoc.primary_destination;
14621 		}
14622 	}
14623 	if (net == NULL) {
14624 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14625 		error = EINVAL;
14626 		goto out_unlocked;
14627 	}
14628 	if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
14629 		/*-
14630 		 * CMT: Added check for CMT above. net above is the primary
14631 		 * dest. If CMT is ON, sender should always attempt to send
14632 		 * with the output routine sctp_fill_outqueue() that loops
14633 		 * through all destination addresses. Therefore, if CMT is
14634 		 * ON, queue_only is NOT set to 1 here, so that
14635 		 * sctp_chunk_output() can be called below.
14636 		 */
14637 		queue_only = 1;
14638 	} else if (asoc->ifp_had_enobuf) {
14639 		SCTP_STAT_INCR(sctps_ifnomemqueued);
14640 		if (net->flight_size > (net->mtu * 2))
14641 			queue_only = 1;
14642 		asoc->ifp_had_enobuf = 0;
14643 	} else {
14644 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
14645 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
14646 	}
14647 	/* Are we aborting? */
14648 	if (srcv->sinfo_flags & SCTP_ABORT) {
14649 		struct mbuf *mm;
14650 		int tot_demand, tot_out = 0, max_out;
14651 
14652 		SCTP_STAT_INCR(sctps_sends_with_abort);
14653 		if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
14654 		    (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
14655 			/* It has to be up before we abort */
14656 			/* how big is the user initiated abort? */
14657 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14658 			error = EINVAL;
14659 			goto out;
14660 		}
14661 		if (hold_tcblock) {
14662 			SCTP_TCB_UNLOCK(stcb);
14663 			hold_tcblock = 0;
14664 		}
14665 		if (top) {
14666 			struct mbuf *cntm = NULL;
14667 
14668 			mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
14669 			if (sndlen != 0) {
14670 				cntm = top;
14671 				while (cntm) {
14672 					tot_out += SCTP_BUF_LEN(cntm);
14673 					cntm = SCTP_BUF_NEXT(cntm);
14674 				}
14675 			}
14676 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
14677 		} else {
14678 			/* Must fit in a MTU */
14679 			tot_out = sndlen;
14680 			tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
14681 			if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
14682 				/* To big */
14683 				SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14684 				error = EMSGSIZE;
14685 				goto out;
14686 			}
14687 			mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
14688 		}
14689 		if (mm == NULL) {
14690 			SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
14691 			error = ENOMEM;
14692 			goto out;
14693 		}
14694 		max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
14695 		max_out -= sizeof(struct sctp_abort_msg);
14696 		if (tot_out > max_out) {
14697 			tot_out = max_out;
14698 		}
14699 		if (mm) {
14700 			struct sctp_paramhdr *ph;
14701 
14702 			/* now move forward the data pointer */
14703 			ph = mtod(mm, struct sctp_paramhdr *);
14704 			ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
14705 			ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
14706 			ph++;
14707 			SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
14708 			if (top == NULL) {
14709 				error = uiomove((caddr_t)ph, (int)tot_out, uio);
14710 				if (error) {
14711 					/*-
14712 					 * Here if we can't get his data we
14713 					 * still abort we just don't get to
14714 					 * send the users note :-0
14715 					 */
14716 					sctp_m_freem(mm);
14717 					mm = NULL;
14718 				}
14719 			} else {
14720 				if (sndlen != 0) {
14721 					SCTP_BUF_NEXT(mm) = top;
14722 				}
14723 			}
14724 		}
14725 		if (hold_tcblock == 0) {
14726 			SCTP_TCB_LOCK(stcb);
14727 			hold_tcblock = 1;
14728 		}
14729 		atomic_add_int(&stcb->asoc.refcnt, -1);
14730 		free_cnt_applied = 0;
14731 		/* release this lock, otherwise we hang on ourselves */
14732 		sctp_abort_an_association(stcb->sctp_ep, stcb,
14733 		    SCTP_RESPONSE_TO_USER_REQ,
14734 		    mm, SCTP_SO_LOCKED);
14735 		/* now relock the stcb so everything is sane */
14736 		hold_tcblock = 0;
14737 		stcb = NULL;
14738 		/*
14739 		 * In this case top is already chained to mm avoid double
14740 		 * free, since we free it below if top != NULL and driver
14741 		 * would free it after sending the packet out
14742 		 */
14743 		if (sndlen != 0) {
14744 			top = NULL;
14745 		}
14746 		goto out_unlocked;
14747 	}
14748 	/* Calculate the maximum we can send */
14749 	inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14750 	if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14751 		if (non_blocking) {
14752 			/* we already checked for non-blocking above. */
14753 			max_len = sndlen;
14754 		} else {
14755 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14756 		}
14757 	} else {
14758 		max_len = 0;
14759 	}
14760 	if (hold_tcblock) {
14761 		SCTP_TCB_UNLOCK(stcb);
14762 		hold_tcblock = 0;
14763 	}
14764 	/* Is the stream no. valid? */
14765 	if (srcv->sinfo_stream >= asoc->streamoutcnt) {
14766 		/* Invalid stream number */
14767 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14768 		error = EINVAL;
14769 		goto out_unlocked;
14770 	}
14771 	if (asoc->strmout == NULL) {
14772 		/* huh? software error */
14773 		SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14774 		error = EFAULT;
14775 		goto out_unlocked;
14776 	}
14777 	/* Unless E_EOR mode is on, we must make a send FIT in one call. */
14778 	if ((user_marks_eor == 0) &&
14779 	    (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
14780 		/* It will NEVER fit */
14781 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14782 		error = EMSGSIZE;
14783 		goto out_unlocked;
14784 	}
14785 	if ((uio == NULL) && user_marks_eor) {
14786 		/*-
14787 		 * We do not support eeor mode for
14788 		 * sending with mbuf chains (like sendfile).
14789 		 */
14790 		SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14791 		error = EINVAL;
14792 		goto out_unlocked;
14793 	}
14794 	if (user_marks_eor) {
14795 		local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
14796 	} else {
14797 		/*-
14798 		 * For non-eeor the whole message must fit in
14799 		 * the socket send buffer.
14800 		 */
14801 		local_add_more = sndlen;
14802 	}
14803 	len = 0;
14804 	if (non_blocking) {
14805 		goto skip_preblock;
14806 	}
14807 	if (((max_len <= local_add_more) &&
14808 	    (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
14809 	    (max_len == 0) ||
14810 	    ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14811 		/* No room right now ! */
14812 		SOCKBUF_LOCK(&so->so_snd);
14813 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14814 		while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
14815 		    ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14816 			SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
14817 			    (unsigned int)SCTP_SB_LIMIT_SND(so),
14818 			    inqueue_bytes,
14819 			    local_add_more,
14820 			    stcb->asoc.stream_queue_cnt,
14821 			    stcb->asoc.chunks_on_out_queue,
14822 			    SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
14823 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14824 				sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
14825 			}
14826 			be.error = 0;
14827 			stcb->block_entry = &be;
14828 			error = sbwait(&so->so_snd);
14829 			stcb->block_entry = NULL;
14830 			if (error || so->so_error || be.error) {
14831 				if (error == 0) {
14832 					if (so->so_error)
14833 						error = so->so_error;
14834 					if (be.error) {
14835 						error = be.error;
14836 					}
14837 				}
14838 				SOCKBUF_UNLOCK(&so->so_snd);
14839 				goto out_unlocked;
14840 			}
14841 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14842 				sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
14843 				    so, asoc, stcb->asoc.total_output_queue_size);
14844 			}
14845 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14846 				goto out_unlocked;
14847 			}
14848 			inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14849 		}
14850 		inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14851 		if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14852 			max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14853 		} else {
14854 			max_len = 0;
14855 		}
14856 		SOCKBUF_UNLOCK(&so->so_snd);
14857 	}
14858 skip_preblock:
14859 	if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14860 		goto out_unlocked;
14861 	}
14862 	/*
14863 	 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
14864 	 * case NOTE: uio will be null when top/mbuf is passed
14865 	 */
14866 	if (sndlen == 0) {
14867 		if (srcv->sinfo_flags & SCTP_EOF) {
14868 			got_all_of_the_send = 1;
14869 			goto dataless_eof;
14870 		} else {
14871 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14872 			error = EINVAL;
14873 			goto out;
14874 		}
14875 	}
14876 	if (top == NULL) {
14877 		struct sctp_stream_queue_pending *sp;
14878 		struct sctp_stream_out *strm;
14879 		uint32_t sndout, initial_out;
14880 
14881 		initial_out = uio->uio_resid;
14882 
14883 		SCTP_TCB_SEND_LOCK(stcb);
14884 		if ((asoc->stream_locked) &&
14885 		    (asoc->stream_locked_on != srcv->sinfo_stream)) {
14886 			SCTP_TCB_SEND_UNLOCK(stcb);
14887 			SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14888 			error = EINVAL;
14889 			goto out;
14890 		}
14891 		SCTP_TCB_SEND_UNLOCK(stcb);
14892 
14893 		strm = &stcb->asoc.strmout[srcv->sinfo_stream];
14894 		if (strm->last_msg_incomplete == 0) {
14895 	do_a_copy_in:
14896 			sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
14897 			if ((sp == NULL) || (error)) {
14898 				goto out;
14899 			}
14900 			SCTP_TCB_SEND_LOCK(stcb);
14901 			if (sp->msg_is_complete) {
14902 				strm->last_msg_incomplete = 0;
14903 				asoc->stream_locked = 0;
14904 			} else {
14905 				/*
14906 				 * Just got locked to this guy in case of an
14907 				 * interrupt.
14908 				 */
14909 				strm->last_msg_incomplete = 1;
14910 				asoc->stream_locked = 1;
14911 				asoc->stream_locked_on = srcv->sinfo_stream;
14912 				sp->sender_all_done = 0;
14913 			}
14914 			sctp_snd_sb_alloc(stcb, sp->length);
14915 			atomic_add_int(&asoc->stream_queue_cnt, 1);
14916 			if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
14917 				sp->strseq = strm->next_sequence_sent;
14918 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
14919 					sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
14920 					    (uintptr_t) stcb, sp->length,
14921 					    (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
14922 				}
14923 				strm->next_sequence_sent++;
14924 			} else {
14925 				SCTP_STAT_INCR(sctps_sends_with_unord);
14926 			}
14927 			TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
14928 			if ((strm->next_spoke.tqe_next == NULL) &&
14929 			    (strm->next_spoke.tqe_prev == NULL)) {
14930 				/* Not on wheel, insert */
14931 				sctp_insert_on_wheel(stcb, asoc, strm, 1);
14932 			}
14933 			SCTP_TCB_SEND_UNLOCK(stcb);
14934 		} else {
14935 			SCTP_TCB_SEND_LOCK(stcb);
14936 			sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
14937 			SCTP_TCB_SEND_UNLOCK(stcb);
14938 			if (sp == NULL) {
14939 				/* ???? Huh ??? last msg is gone */
14940 #ifdef INVARIANTS
14941 				panic("Warning: Last msg marked incomplete, yet nothing left?");
14942 #else
14943 				SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
14944 				strm->last_msg_incomplete = 0;
14945 #endif
14946 				goto do_a_copy_in;
14947 
14948 			}
14949 		}
14950 		while (uio->uio_resid > 0) {
14951 			/* How much room do we have? */
14952 			struct mbuf *new_tail, *mm;
14953 
14954 			if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
14955 				max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
14956 			else
14957 				max_len = 0;
14958 
14959 			if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
14960 			    (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
14961 			    (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
14962 				sndout = 0;
14963 				new_tail = NULL;
14964 				if (hold_tcblock) {
14965 					SCTP_TCB_UNLOCK(stcb);
14966 					hold_tcblock = 0;
14967 				}
14968 				mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
14969 				if ((mm == NULL) || error) {
14970 					if (mm) {
14971 						sctp_m_freem(mm);
14972 					}
14973 					goto out;
14974 				}
14975 				/* Update the mbuf and count */
14976 				SCTP_TCB_SEND_LOCK(stcb);
14977 				if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14978 					/*
14979 					 * we need to get out. Peer probably
14980 					 * aborted.
14981 					 */
14982 					sctp_m_freem(mm);
14983 					if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
14984 						SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14985 						error = ECONNRESET;
14986 					}
14987 					SCTP_TCB_SEND_UNLOCK(stcb);
14988 					goto out;
14989 				}
14990 				if (sp->tail_mbuf) {
14991 					/* tack it to the end */
14992 					SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
14993 					sp->tail_mbuf = new_tail;
14994 				} else {
14995 					/* A stolen mbuf */
14996 					sp->data = mm;
14997 					sp->tail_mbuf = new_tail;
14998 				}
14999 				sctp_snd_sb_alloc(stcb, sndout);
15000 				atomic_add_int(&sp->length, sndout);
15001 				len += sndout;
15002 
15003 				/* Did we reach EOR? */
15004 				if ((uio->uio_resid == 0) &&
15005 				    ((user_marks_eor == 0) ||
15006 				    (srcv->sinfo_flags & SCTP_EOF) ||
15007 				    (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
15008 					sp->msg_is_complete = 1;
15009 				} else {
15010 					sp->msg_is_complete = 0;
15011 				}
15012 				SCTP_TCB_SEND_UNLOCK(stcb);
15013 			}
15014 			if (uio->uio_resid == 0) {
15015 				/* got it all? */
15016 				continue;
15017 			}
15018 			/* PR-SCTP? */
15019 			if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
15020 				/*
15021 				 * This is ugly but we must assure locking
15022 				 * order
15023 				 */
15024 				if (hold_tcblock == 0) {
15025 					SCTP_TCB_LOCK(stcb);
15026 					hold_tcblock = 1;
15027 				}
15028 				sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
15029 				inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
15030 				if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
15031 					max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
15032 				else
15033 					max_len = 0;
15034 				if (max_len > 0) {
15035 					continue;
15036 				}
15037 				SCTP_TCB_UNLOCK(stcb);
15038 				hold_tcblock = 0;
15039 			}
15040 			/* wait for space now */
15041 			if (non_blocking) {
15042 				/* Non-blocking io in place out */
15043 				goto skip_out_eof;
15044 			}
15045 			if ((net->flight_size > net->cwnd) &&
15046 			    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
15047 				queue_only = 1;
15048 			} else if (asoc->ifp_had_enobuf) {
15049 				SCTP_STAT_INCR(sctps_ifnomemqueued);
15050 				if (net->flight_size > (net->mtu * 2)) {
15051 					queue_only = 1;
15052 				} else {
15053 					queue_only = 0;
15054 				}
15055 				asoc->ifp_had_enobuf = 0;
15056 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15057 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15058 			} else {
15059 				un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15060 				    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15061 				if (net->flight_size > net->cwnd) {
15062 					queue_only = 1;
15063 					SCTP_STAT_INCR(sctps_send_cwnd_avoid);
15064 				} else {
15065 					queue_only = 0;
15066 				}
15067 			}
15068 			if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
15069 			    (stcb->asoc.total_flight > 0) &&
15070 			    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
15071 			    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
15072 
15073 				/*-
15074 				 * Ok, Nagle is set on and we have data outstanding.
15075 				 * Don't send anything and let SACKs drive out the
15076 				 * data unless wen have a "full" segment to send.
15077 				 */
15078 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15079 					sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
15080 				}
15081 				SCTP_STAT_INCR(sctps_naglequeued);
15082 				nagle_applies = 1;
15083 			} else {
15084 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15085 					if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
15086 						sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
15087 				}
15088 				SCTP_STAT_INCR(sctps_naglesent);
15089 				nagle_applies = 0;
15090 			}
15091 			/* What about the INIT, send it maybe */
15092 			if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15093 
15094 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
15095 				    nagle_applies, un_sent);
15096 				sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
15097 				    stcb->asoc.total_flight,
15098 				    stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
15099 			}
15100 			if (queue_only_for_init) {
15101 				if (hold_tcblock == 0) {
15102 					SCTP_TCB_LOCK(stcb);
15103 					hold_tcblock = 1;
15104 				}
15105 				if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
15106 					/* a collision took us forward? */
15107 					queue_only_for_init = 0;
15108 					queue_only = 0;
15109 				} else {
15110 					sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
15111 					SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
15112 					queue_only_for_init = 0;
15113 					queue_only = 1;
15114 				}
15115 			}
15116 			if ((queue_only == 0) && (nagle_applies == 0)) {
15117 				/*-
15118 				 * need to start chunk output
15119 				 * before blocking.. note that if
15120 				 * a lock is already applied, then
15121 				 * the input via the net is happening
15122 				 * and I don't need to start output :-D
15123 				 */
15124 				if (hold_tcblock == 0) {
15125 					if (SCTP_TCB_TRYLOCK(stcb)) {
15126 						hold_tcblock = 1;
15127 						sctp_chunk_output(inp,
15128 						    stcb,
15129 						    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15130 					}
15131 				} else {
15132 					sctp_chunk_output(inp,
15133 					    stcb,
15134 					    SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15135 				}
15136 				if (hold_tcblock == 1) {
15137 					SCTP_TCB_UNLOCK(stcb);
15138 					hold_tcblock = 0;
15139 				}
15140 			}
15141 			SOCKBUF_LOCK(&so->so_snd);
15142 			/*-
15143 			 * This is a bit strange, but I think it will
15144 			 * work. The total_output_queue_size is locked and
15145 			 * protected by the TCB_LOCK, which we just released.
15146 			 * There is a race that can occur between releasing it
15147 			 * above, and me getting the socket lock, where sacks
15148 			 * come in but we have not put the SB_WAIT on the
15149 			 * so_snd buffer to get the wakeup. After the LOCK
15150 			 * is applied the sack_processing will also need to
15151 			 * LOCK the so->so_snd to do the actual sowwakeup(). So
15152 			 * once we have the socket buffer lock if we recheck the
15153 			 * size we KNOW we will get to sleep safely with the
15154 			 * wakeup flag in place.
15155 			 */
15156 			if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
15157 			    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
15158 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15159 					sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
15160 					    so, asoc, uio->uio_resid);
15161 				}
15162 				be.error = 0;
15163 				stcb->block_entry = &be;
15164 				error = sbwait(&so->so_snd);
15165 				stcb->block_entry = NULL;
15166 
15167 				if (error || so->so_error || be.error) {
15168 					if (error == 0) {
15169 						if (so->so_error)
15170 							error = so->so_error;
15171 						if (be.error) {
15172 							error = be.error;
15173 						}
15174 					}
15175 					SOCKBUF_UNLOCK(&so->so_snd);
15176 					goto out_unlocked;
15177 				}
15178 				if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15179 					sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
15180 					    so, asoc, stcb->asoc.total_output_queue_size);
15181 				}
15182 			}
15183 			SOCKBUF_UNLOCK(&so->so_snd);
15184 			if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
15185 				goto out_unlocked;
15186 			}
15187 		}
15188 		SCTP_TCB_SEND_LOCK(stcb);
15189 		if (sp) {
15190 			if (sp->msg_is_complete == 0) {
15191 				strm->last_msg_incomplete = 1;
15192 				asoc->stream_locked = 1;
15193 				asoc->stream_locked_on = srcv->sinfo_stream;
15194 			} else {
15195 				sp->sender_all_done = 1;
15196 				strm->last_msg_incomplete = 0;
15197 				asoc->stream_locked = 0;
15198 			}
15199 		} else {
15200 			SCTP_PRINTF("Huh no sp TSNH?\n");
15201 			strm->last_msg_incomplete = 0;
15202 			asoc->stream_locked = 0;
15203 		}
15204 		SCTP_TCB_SEND_UNLOCK(stcb);
15205 		if (uio->uio_resid == 0) {
15206 			got_all_of_the_send = 1;
15207 		}
15208 	} else if (top) {
15209 		/* We send in a 0, since we do NOT have any locks */
15210 		error = sctp_msg_append(stcb, net, top, srcv, 0);
15211 		top = NULL;
15212 		if (srcv->sinfo_flags & SCTP_EOF) {
15213 			/*
15214 			 * This should only happen for Panda for the mbuf
15215 			 * send case, which does NOT yet support EEOR mode.
15216 			 * Thus, we can just set this flag to do the proper
15217 			 * EOF handling.
15218 			 */
15219 			got_all_of_the_send = 1;
15220 		}
15221 	}
15222 	if (error) {
15223 		goto out;
15224 	}
15225 dataless_eof:
15226 	/* EOF thing ? */
15227 	if ((srcv->sinfo_flags & SCTP_EOF) &&
15228 	    (got_all_of_the_send == 1) &&
15229 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
15230 		int cnt;
15231 
15232 		SCTP_STAT_INCR(sctps_sends_with_eof);
15233 		error = 0;
15234 		if (hold_tcblock == 0) {
15235 			SCTP_TCB_LOCK(stcb);
15236 			hold_tcblock = 1;
15237 		}
15238 		cnt = sctp_is_there_unsent_data(stcb);
15239 		if (TAILQ_EMPTY(&asoc->send_queue) &&
15240 		    TAILQ_EMPTY(&asoc->sent_queue) &&
15241 		    (cnt == 0)) {
15242 			if (asoc->locked_on_sending) {
15243 				goto abort_anyway;
15244 			}
15245 			/* there is nothing queued to send, so I'm done... */
15246 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
15247 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
15248 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
15249 				/* only send SHUTDOWN the first time through */
15250 				sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
15251 				if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
15252 					SCTP_STAT_DECR_GAUGE32(sctps_currestab);
15253 				}
15254 				SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
15255 				SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
15256 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
15257 				    asoc->primary_destination);
15258 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
15259 				    asoc->primary_destination);
15260 			}
15261 		} else {
15262 			/*-
15263 			 * we still got (or just got) data to send, so set
15264 			 * SHUTDOWN_PENDING
15265 			 */
15266 			/*-
15267 			 * XXX sockets draft says that SCTP_EOF should be
15268 			 * sent with no data.  currently, we will allow user
15269 			 * data to be sent first and move to
15270 			 * SHUTDOWN-PENDING
15271 			 */
15272 			if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
15273 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
15274 			    (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
15275 				if (hold_tcblock == 0) {
15276 					SCTP_TCB_LOCK(stcb);
15277 					hold_tcblock = 1;
15278 				}
15279 				if (asoc->locked_on_sending) {
15280 					/* Locked to send out the data */
15281 					struct sctp_stream_queue_pending *sp;
15282 
15283 					sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
15284 					if (sp) {
15285 						if ((sp->length == 0) && (sp->msg_is_complete == 0))
15286 							asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
15287 					}
15288 				}
15289 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
15290 				if (TAILQ_EMPTY(&asoc->send_queue) &&
15291 				    TAILQ_EMPTY(&asoc->sent_queue) &&
15292 				    (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
15293 			abort_anyway:
15294 					if (free_cnt_applied) {
15295 						atomic_add_int(&stcb->asoc.refcnt, -1);
15296 						free_cnt_applied = 0;
15297 					}
15298 					sctp_abort_an_association(stcb->sctp_ep, stcb,
15299 					    SCTP_RESPONSE_TO_USER_REQ,
15300 					    NULL, SCTP_SO_LOCKED);
15301 					/*
15302 					 * now relock the stcb so everything
15303 					 * is sane
15304 					 */
15305 					hold_tcblock = 0;
15306 					stcb = NULL;
15307 					goto out;
15308 				}
15309 				sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
15310 				    asoc->primary_destination);
15311 				sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
15312 			}
15313 		}
15314 	}
15315 skip_out_eof:
15316 	if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
15317 		some_on_control = 1;
15318 	}
15319 	if ((net->flight_size > net->cwnd) &&
15320 	    (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
15321 		queue_only = 1;
15322 	} else if (asoc->ifp_had_enobuf) {
15323 		SCTP_STAT_INCR(sctps_ifnomemqueued);
15324 		if (net->flight_size > (net->mtu * 2)) {
15325 			queue_only = 1;
15326 		} else {
15327 			queue_only = 0;
15328 		}
15329 		asoc->ifp_had_enobuf = 0;
15330 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15331 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15332 	} else {
15333 		un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15334 		    (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15335 		if (net->flight_size > net->cwnd) {
15336 			queue_only = 1;
15337 			SCTP_STAT_INCR(sctps_send_cwnd_avoid);
15338 		} else {
15339 			queue_only = 0;
15340 		}
15341 	}
15342 	if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
15343 	    (stcb->asoc.total_flight > 0) &&
15344 	    (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
15345 	    (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
15346 		/*-
15347 		 * Ok, Nagle is set on and we have data outstanding.
15348 		 * Don't send anything and let SACKs drive out the
15349 		 * data unless wen have a "full" segment to send.
15350 		 */
15351 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15352 			sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
15353 		}
15354 		SCTP_STAT_INCR(sctps_naglequeued);
15355 		nagle_applies = 1;
15356 	} else {
15357 		if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15358 			if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
15359 				sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
15360 		}
15361 		SCTP_STAT_INCR(sctps_naglesent);
15362 		nagle_applies = 0;
15363 	}
15364 	if (queue_only_for_init) {
15365 		if (hold_tcblock == 0) {
15366 			SCTP_TCB_LOCK(stcb);
15367 			hold_tcblock = 1;
15368 		}
15369 		if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
15370 			/* a collision took us forward? */
15371 			queue_only_for_init = 0;
15372 			queue_only = 0;
15373 		} else {
15374 			sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
15375 			SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
15376 			queue_only_for_init = 0;
15377 			queue_only = 1;
15378 		}
15379 	}
15380 	if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
15381 		/* we can attempt to send too. */
15382 		if (hold_tcblock == 0) {
15383 			/*
15384 			 * If there is activity recv'ing sacks no need to
15385 			 * send
15386 			 */
15387 			if (SCTP_TCB_TRYLOCK(stcb)) {
15388 				sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15389 				hold_tcblock = 1;
15390 			}
15391 		} else {
15392 			sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15393 		}
15394 	} else if ((queue_only == 0) &&
15395 		    (stcb->asoc.peers_rwnd == 0) &&
15396 	    (stcb->asoc.total_flight == 0)) {
15397 		/* We get to have a probe outstanding */
15398 		if (hold_tcblock == 0) {
15399 			hold_tcblock = 1;
15400 			SCTP_TCB_LOCK(stcb);
15401 		}
15402 		sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15403 	} else if (some_on_control) {
15404 		int num_out, reason, cwnd_full, frag_point;
15405 
15406 		/* Here we do control only */
15407 		if (hold_tcblock == 0) {
15408 			hold_tcblock = 1;
15409 			SCTP_TCB_LOCK(stcb);
15410 		}
15411 		frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
15412 		(void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
15413 		    &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
15414 	}
15415 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
15416 	    queue_only, stcb->asoc.peers_rwnd, un_sent,
15417 	    stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
15418 	    stcb->asoc.total_output_queue_size, error);
15419 
15420 out:
15421 out_unlocked:
15422 
15423 	if (local_soresv && stcb) {
15424 		atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
15425 		local_soresv = 0;
15426 	}
15427 	if (create_lock_applied) {
15428 		SCTP_ASOC_CREATE_UNLOCK(inp);
15429 		create_lock_applied = 0;
15430 	}
15431 	if ((stcb) && hold_tcblock) {
15432 		SCTP_TCB_UNLOCK(stcb);
15433 	}
15434 	if (stcb && free_cnt_applied) {
15435 		atomic_add_int(&stcb->asoc.refcnt, -1);
15436 	}
15437 #ifdef INVARIANTS
15438 	if (stcb) {
15439 		if (mtx_owned(&stcb->tcb_mtx)) {
15440 			panic("Leaving with tcb mtx owned?");
15441 		}
15442 		if (mtx_owned(&stcb->tcb_send_mtx)) {
15443 			panic("Leaving with tcb send mtx owned?");
15444 		}
15445 	}
15446 #endif
15447 	if (top) {
15448 		sctp_m_freem(top);
15449 	}
15450 	if (control) {
15451 		sctp_m_freem(control);
15452 	}
15453 	return (error);
15454 }
15455 
15456 
15457 /*
15458  * generate an AUTHentication chunk, if required
15459  */
15460 struct mbuf *
15461 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
15462     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
15463     struct sctp_tcb *stcb, uint8_t chunk)
15464 {
15465 	struct mbuf *m_auth;
15466 	struct sctp_auth_chunk *auth;
15467 	int chunk_len;
15468 
15469 	if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
15470 	    (stcb == NULL))
15471 		return (m);
15472 
15473 	/* sysctl disabled auth? */
15474 	if (SCTP_BASE_SYSCTL(sctp_auth_disable))
15475 		return (m);
15476 
15477 	/* peer doesn't do auth... */
15478 	if (!stcb->asoc.peer_supports_auth) {
15479 		return (m);
15480 	}
15481 	/* does the requested chunk require auth? */
15482 	if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
15483 		return (m);
15484 	}
15485 	m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
15486 	if (m_auth == NULL) {
15487 		/* no mbuf's */
15488 		return (m);
15489 	}
15490 	/* reserve some space if this will be the first mbuf */
15491 	if (m == NULL)
15492 		SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
15493 	/* fill in the AUTH chunk details */
15494 	auth = mtod(m_auth, struct sctp_auth_chunk *);
15495 	bzero(auth, sizeof(*auth));
15496 	auth->ch.chunk_type = SCTP_AUTHENTICATION;
15497 	auth->ch.chunk_flags = 0;
15498 	chunk_len = sizeof(*auth) +
15499 	    sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
15500 	auth->ch.chunk_length = htons(chunk_len);
15501 	auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
15502 	/* key id and hmac digest will be computed and filled in upon send */
15503 
15504 	/* save the offset where the auth was inserted into the chain */
15505 	if (m != NULL) {
15506 		struct mbuf *cn;
15507 
15508 		*offset = 0;
15509 		cn = m;
15510 		while (cn) {
15511 			*offset += SCTP_BUF_LEN(cn);
15512 			cn = SCTP_BUF_NEXT(cn);
15513 		}
15514 	} else
15515 		*offset = 0;
15516 
15517 	/* update length and return pointer to the auth chunk */
15518 	SCTP_BUF_LEN(m_auth) = chunk_len;
15519 	m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
15520 	if (auth_ret != NULL)
15521 		*auth_ret = auth;
15522 
15523 	return (m);
15524 }
15525 
15526 #ifdef INET6
15527 int
15528 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
15529 {
15530 	struct nd_prefix *pfx = NULL;
15531 	struct nd_pfxrouter *pfxrtr = NULL;
15532 	struct sockaddr_in6 gw6;
15533 
15534 	if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
15535 		return (0);
15536 
15537 	/* get prefix entry of address */
15538 	LIST_FOREACH(pfx, &MODULE_GLOBAL(MOD_INET6, nd_prefix), ndpr_entry) {
15539 		if (pfx->ndpr_stateflags & NDPRF_DETACHED)
15540 			continue;
15541 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
15542 		    &src6->sin6_addr, &pfx->ndpr_mask))
15543 			break;
15544 	}
15545 	/* no prefix entry in the prefix list */
15546 	if (pfx == NULL) {
15547 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
15548 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
15549 		return (0);
15550 	}
15551 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
15552 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
15553 
15554 	/* search installed gateway from prefix entry */
15555 	for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
15556 	    pfxrtr->pfr_next) {
15557 		memset(&gw6, 0, sizeof(struct sockaddr_in6));
15558 		gw6.sin6_family = AF_INET6;
15559 		gw6.sin6_len = sizeof(struct sockaddr_in6);
15560 		memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
15561 		    sizeof(struct in6_addr));
15562 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
15563 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
15564 		SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
15565 		SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
15566 		if (sctp_cmpaddr((struct sockaddr *)&gw6,
15567 		    ro->ro_rt->rt_gateway)) {
15568 			SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
15569 			return (1);
15570 		}
15571 	}
15572 	SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
15573 	return (0);
15574 }
15575 
15576 #endif
15577 
15578 int
15579 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
15580 {
15581 	struct sockaddr_in *sin, *mask;
15582 	struct ifaddr *ifa;
15583 	struct in_addr srcnetaddr, gwnetaddr;
15584 
15585 	if (ro == NULL || ro->ro_rt == NULL ||
15586 	    sifa->address.sa.sa_family != AF_INET) {
15587 		return (0);
15588 	}
15589 	ifa = (struct ifaddr *)sifa->ifa;
15590 	mask = (struct sockaddr_in *)(ifa->ifa_netmask);
15591 	sin = (struct sockaddr_in *)&sifa->address.sin;
15592 	srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
15593 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
15594 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
15595 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
15596 
15597 	sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
15598 	gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
15599 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
15600 	SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
15601 	SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
15602 	if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
15603 		return (1);
15604 	}
15605 	return (0);
15606 }
15607