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