]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/sctp_output.c
vtnet: don't leak pfil(9) data on detach
[FreeBSD/FreeBSD.git] / sys / netinet / sctp_output.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <sys/cdefs.h>
36 #include <netinet/sctp_os.h>
37 #include <sys/proc.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_crc32.h>
53 #include <netinet/sctp_kdtrace.h>
54 #if defined(INET) || defined(INET6)
55 #include <netinet/udp.h>
56 #endif
57 #include <netinet/udp_var.h>
58 #include <machine/in_cksum.h>
59
60 #define SCTP_MAX_GAPS_INARRAY 4
61 struct sack_track {
62         uint8_t right_edge;     /* mergable on the right edge */
63         uint8_t left_edge;      /* mergable on the left edge */
64         uint8_t num_entries;
65         uint8_t spare;
66         struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
67 };
68
69 const struct sack_track sack_array[256] = {
70         {0, 0, 0, 0,            /* 0x00 */
71                 {{0, 0},
72                 {0, 0},
73                 {0, 0},
74                 {0, 0}
75                 }
76         },
77         {1, 0, 1, 0,            /* 0x01 */
78                 {{0, 0},
79                 {0, 0},
80                 {0, 0},
81                 {0, 0}
82                 }
83         },
84         {0, 0, 1, 0,            /* 0x02 */
85                 {{1, 1},
86                 {0, 0},
87                 {0, 0},
88                 {0, 0}
89                 }
90         },
91         {1, 0, 1, 0,            /* 0x03 */
92                 {{0, 1},
93                 {0, 0},
94                 {0, 0},
95                 {0, 0}
96                 }
97         },
98         {0, 0, 1, 0,            /* 0x04 */
99                 {{2, 2},
100                 {0, 0},
101                 {0, 0},
102                 {0, 0}
103                 }
104         },
105         {1, 0, 2, 0,            /* 0x05 */
106                 {{0, 0},
107                 {2, 2},
108                 {0, 0},
109                 {0, 0}
110                 }
111         },
112         {0, 0, 1, 0,            /* 0x06 */
113                 {{1, 2},
114                 {0, 0},
115                 {0, 0},
116                 {0, 0}
117                 }
118         },
119         {1, 0, 1, 0,            /* 0x07 */
120                 {{0, 2},
121                 {0, 0},
122                 {0, 0},
123                 {0, 0}
124                 }
125         },
126         {0, 0, 1, 0,            /* 0x08 */
127                 {{3, 3},
128                 {0, 0},
129                 {0, 0},
130                 {0, 0}
131                 }
132         },
133         {1, 0, 2, 0,            /* 0x09 */
134                 {{0, 0},
135                 {3, 3},
136                 {0, 0},
137                 {0, 0}
138                 }
139         },
140         {0, 0, 2, 0,            /* 0x0a */
141                 {{1, 1},
142                 {3, 3},
143                 {0, 0},
144                 {0, 0}
145                 }
146         },
147         {1, 0, 2, 0,            /* 0x0b */
148                 {{0, 1},
149                 {3, 3},
150                 {0, 0},
151                 {0, 0}
152                 }
153         },
154         {0, 0, 1, 0,            /* 0x0c */
155                 {{2, 3},
156                 {0, 0},
157                 {0, 0},
158                 {0, 0}
159                 }
160         },
161         {1, 0, 2, 0,            /* 0x0d */
162                 {{0, 0},
163                 {2, 3},
164                 {0, 0},
165                 {0, 0}
166                 }
167         },
168         {0, 0, 1, 0,            /* 0x0e */
169                 {{1, 3},
170                 {0, 0},
171                 {0, 0},
172                 {0, 0}
173                 }
174         },
175         {1, 0, 1, 0,            /* 0x0f */
176                 {{0, 3},
177                 {0, 0},
178                 {0, 0},
179                 {0, 0}
180                 }
181         },
182         {0, 0, 1, 0,            /* 0x10 */
183                 {{4, 4},
184                 {0, 0},
185                 {0, 0},
186                 {0, 0}
187                 }
188         },
189         {1, 0, 2, 0,            /* 0x11 */
190                 {{0, 0},
191                 {4, 4},
192                 {0, 0},
193                 {0, 0}
194                 }
195         },
196         {0, 0, 2, 0,            /* 0x12 */
197                 {{1, 1},
198                 {4, 4},
199                 {0, 0},
200                 {0, 0}
201                 }
202         },
203         {1, 0, 2, 0,            /* 0x13 */
204                 {{0, 1},
205                 {4, 4},
206                 {0, 0},
207                 {0, 0}
208                 }
209         },
210         {0, 0, 2, 0,            /* 0x14 */
211                 {{2, 2},
212                 {4, 4},
213                 {0, 0},
214                 {0, 0}
215                 }
216         },
217         {1, 0, 3, 0,            /* 0x15 */
218                 {{0, 0},
219                 {2, 2},
220                 {4, 4},
221                 {0, 0}
222                 }
223         },
224         {0, 0, 2, 0,            /* 0x16 */
225                 {{1, 2},
226                 {4, 4},
227                 {0, 0},
228                 {0, 0}
229                 }
230         },
231         {1, 0, 2, 0,            /* 0x17 */
232                 {{0, 2},
233                 {4, 4},
234                 {0, 0},
235                 {0, 0}
236                 }
237         },
238         {0, 0, 1, 0,            /* 0x18 */
239                 {{3, 4},
240                 {0, 0},
241                 {0, 0},
242                 {0, 0}
243                 }
244         },
245         {1, 0, 2, 0,            /* 0x19 */
246                 {{0, 0},
247                 {3, 4},
248                 {0, 0},
249                 {0, 0}
250                 }
251         },
252         {0, 0, 2, 0,            /* 0x1a */
253                 {{1, 1},
254                 {3, 4},
255                 {0, 0},
256                 {0, 0}
257                 }
258         },
259         {1, 0, 2, 0,            /* 0x1b */
260                 {{0, 1},
261                 {3, 4},
262                 {0, 0},
263                 {0, 0}
264                 }
265         },
266         {0, 0, 1, 0,            /* 0x1c */
267                 {{2, 4},
268                 {0, 0},
269                 {0, 0},
270                 {0, 0}
271                 }
272         },
273         {1, 0, 2, 0,            /* 0x1d */
274                 {{0, 0},
275                 {2, 4},
276                 {0, 0},
277                 {0, 0}
278                 }
279         },
280         {0, 0, 1, 0,            /* 0x1e */
281                 {{1, 4},
282                 {0, 0},
283                 {0, 0},
284                 {0, 0}
285                 }
286         },
287         {1, 0, 1, 0,            /* 0x1f */
288                 {{0, 4},
289                 {0, 0},
290                 {0, 0},
291                 {0, 0}
292                 }
293         },
294         {0, 0, 1, 0,            /* 0x20 */
295                 {{5, 5},
296                 {0, 0},
297                 {0, 0},
298                 {0, 0}
299                 }
300         },
301         {1, 0, 2, 0,            /* 0x21 */
302                 {{0, 0},
303                 {5, 5},
304                 {0, 0},
305                 {0, 0}
306                 }
307         },
308         {0, 0, 2, 0,            /* 0x22 */
309                 {{1, 1},
310                 {5, 5},
311                 {0, 0},
312                 {0, 0}
313                 }
314         },
315         {1, 0, 2, 0,            /* 0x23 */
316                 {{0, 1},
317                 {5, 5},
318                 {0, 0},
319                 {0, 0}
320                 }
321         },
322         {0, 0, 2, 0,            /* 0x24 */
323                 {{2, 2},
324                 {5, 5},
325                 {0, 0},
326                 {0, 0}
327                 }
328         },
329         {1, 0, 3, 0,            /* 0x25 */
330                 {{0, 0},
331                 {2, 2},
332                 {5, 5},
333                 {0, 0}
334                 }
335         },
336         {0, 0, 2, 0,            /* 0x26 */
337                 {{1, 2},
338                 {5, 5},
339                 {0, 0},
340                 {0, 0}
341                 }
342         },
343         {1, 0, 2, 0,            /* 0x27 */
344                 {{0, 2},
345                 {5, 5},
346                 {0, 0},
347                 {0, 0}
348                 }
349         },
350         {0, 0, 2, 0,            /* 0x28 */
351                 {{3, 3},
352                 {5, 5},
353                 {0, 0},
354                 {0, 0}
355                 }
356         },
357         {1, 0, 3, 0,            /* 0x29 */
358                 {{0, 0},
359                 {3, 3},
360                 {5, 5},
361                 {0, 0}
362                 }
363         },
364         {0, 0, 3, 0,            /* 0x2a */
365                 {{1, 1},
366                 {3, 3},
367                 {5, 5},
368                 {0, 0}
369                 }
370         },
371         {1, 0, 3, 0,            /* 0x2b */
372                 {{0, 1},
373                 {3, 3},
374                 {5, 5},
375                 {0, 0}
376                 }
377         },
378         {0, 0, 2, 0,            /* 0x2c */
379                 {{2, 3},
380                 {5, 5},
381                 {0, 0},
382                 {0, 0}
383                 }
384         },
385         {1, 0, 3, 0,            /* 0x2d */
386                 {{0, 0},
387                 {2, 3},
388                 {5, 5},
389                 {0, 0}
390                 }
391         },
392         {0, 0, 2, 0,            /* 0x2e */
393                 {{1, 3},
394                 {5, 5},
395                 {0, 0},
396                 {0, 0}
397                 }
398         },
399         {1, 0, 2, 0,            /* 0x2f */
400                 {{0, 3},
401                 {5, 5},
402                 {0, 0},
403                 {0, 0}
404                 }
405         },
406         {0, 0, 1, 0,            /* 0x30 */
407                 {{4, 5},
408                 {0, 0},
409                 {0, 0},
410                 {0, 0}
411                 }
412         },
413         {1, 0, 2, 0,            /* 0x31 */
414                 {{0, 0},
415                 {4, 5},
416                 {0, 0},
417                 {0, 0}
418                 }
419         },
420         {0, 0, 2, 0,            /* 0x32 */
421                 {{1, 1},
422                 {4, 5},
423                 {0, 0},
424                 {0, 0}
425                 }
426         },
427         {1, 0, 2, 0,            /* 0x33 */
428                 {{0, 1},
429                 {4, 5},
430                 {0, 0},
431                 {0, 0}
432                 }
433         },
434         {0, 0, 2, 0,            /* 0x34 */
435                 {{2, 2},
436                 {4, 5},
437                 {0, 0},
438                 {0, 0}
439                 }
440         },
441         {1, 0, 3, 0,            /* 0x35 */
442                 {{0, 0},
443                 {2, 2},
444                 {4, 5},
445                 {0, 0}
446                 }
447         },
448         {0, 0, 2, 0,            /* 0x36 */
449                 {{1, 2},
450                 {4, 5},
451                 {0, 0},
452                 {0, 0}
453                 }
454         },
455         {1, 0, 2, 0,            /* 0x37 */
456                 {{0, 2},
457                 {4, 5},
458                 {0, 0},
459                 {0, 0}
460                 }
461         },
462         {0, 0, 1, 0,            /* 0x38 */
463                 {{3, 5},
464                 {0, 0},
465                 {0, 0},
466                 {0, 0}
467                 }
468         },
469         {1, 0, 2, 0,            /* 0x39 */
470                 {{0, 0},
471                 {3, 5},
472                 {0, 0},
473                 {0, 0}
474                 }
475         },
476         {0, 0, 2, 0,            /* 0x3a */
477                 {{1, 1},
478                 {3, 5},
479                 {0, 0},
480                 {0, 0}
481                 }
482         },
483         {1, 0, 2, 0,            /* 0x3b */
484                 {{0, 1},
485                 {3, 5},
486                 {0, 0},
487                 {0, 0}
488                 }
489         },
490         {0, 0, 1, 0,            /* 0x3c */
491                 {{2, 5},
492                 {0, 0},
493                 {0, 0},
494                 {0, 0}
495                 }
496         },
497         {1, 0, 2, 0,            /* 0x3d */
498                 {{0, 0},
499                 {2, 5},
500                 {0, 0},
501                 {0, 0}
502                 }
503         },
504         {0, 0, 1, 0,            /* 0x3e */
505                 {{1, 5},
506                 {0, 0},
507                 {0, 0},
508                 {0, 0}
509                 }
510         },
511         {1, 0, 1, 0,            /* 0x3f */
512                 {{0, 5},
513                 {0, 0},
514                 {0, 0},
515                 {0, 0}
516                 }
517         },
518         {0, 0, 1, 0,            /* 0x40 */
519                 {{6, 6},
520                 {0, 0},
521                 {0, 0},
522                 {0, 0}
523                 }
524         },
525         {1, 0, 2, 0,            /* 0x41 */
526                 {{0, 0},
527                 {6, 6},
528                 {0, 0},
529                 {0, 0}
530                 }
531         },
532         {0, 0, 2, 0,            /* 0x42 */
533                 {{1, 1},
534                 {6, 6},
535                 {0, 0},
536                 {0, 0}
537                 }
538         },
539         {1, 0, 2, 0,            /* 0x43 */
540                 {{0, 1},
541                 {6, 6},
542                 {0, 0},
543                 {0, 0}
544                 }
545         },
546         {0, 0, 2, 0,            /* 0x44 */
547                 {{2, 2},
548                 {6, 6},
549                 {0, 0},
550                 {0, 0}
551                 }
552         },
553         {1, 0, 3, 0,            /* 0x45 */
554                 {{0, 0},
555                 {2, 2},
556                 {6, 6},
557                 {0, 0}
558                 }
559         },
560         {0, 0, 2, 0,            /* 0x46 */
561                 {{1, 2},
562                 {6, 6},
563                 {0, 0},
564                 {0, 0}
565                 }
566         },
567         {1, 0, 2, 0,            /* 0x47 */
568                 {{0, 2},
569                 {6, 6},
570                 {0, 0},
571                 {0, 0}
572                 }
573         },
574         {0, 0, 2, 0,            /* 0x48 */
575                 {{3, 3},
576                 {6, 6},
577                 {0, 0},
578                 {0, 0}
579                 }
580         },
581         {1, 0, 3, 0,            /* 0x49 */
582                 {{0, 0},
583                 {3, 3},
584                 {6, 6},
585                 {0, 0}
586                 }
587         },
588         {0, 0, 3, 0,            /* 0x4a */
589                 {{1, 1},
590                 {3, 3},
591                 {6, 6},
592                 {0, 0}
593                 }
594         },
595         {1, 0, 3, 0,            /* 0x4b */
596                 {{0, 1},
597                 {3, 3},
598                 {6, 6},
599                 {0, 0}
600                 }
601         },
602         {0, 0, 2, 0,            /* 0x4c */
603                 {{2, 3},
604                 {6, 6},
605                 {0, 0},
606                 {0, 0}
607                 }
608         },
609         {1, 0, 3, 0,            /* 0x4d */
610                 {{0, 0},
611                 {2, 3},
612                 {6, 6},
613                 {0, 0}
614                 }
615         },
616         {0, 0, 2, 0,            /* 0x4e */
617                 {{1, 3},
618                 {6, 6},
619                 {0, 0},
620                 {0, 0}
621                 }
622         },
623         {1, 0, 2, 0,            /* 0x4f */
624                 {{0, 3},
625                 {6, 6},
626                 {0, 0},
627                 {0, 0}
628                 }
629         },
630         {0, 0, 2, 0,            /* 0x50 */
631                 {{4, 4},
632                 {6, 6},
633                 {0, 0},
634                 {0, 0}
635                 }
636         },
637         {1, 0, 3, 0,            /* 0x51 */
638                 {{0, 0},
639                 {4, 4},
640                 {6, 6},
641                 {0, 0}
642                 }
643         },
644         {0, 0, 3, 0,            /* 0x52 */
645                 {{1, 1},
646                 {4, 4},
647                 {6, 6},
648                 {0, 0}
649                 }
650         },
651         {1, 0, 3, 0,            /* 0x53 */
652                 {{0, 1},
653                 {4, 4},
654                 {6, 6},
655                 {0, 0}
656                 }
657         },
658         {0, 0, 3, 0,            /* 0x54 */
659                 {{2, 2},
660                 {4, 4},
661                 {6, 6},
662                 {0, 0}
663                 }
664         },
665         {1, 0, 4, 0,            /* 0x55 */
666                 {{0, 0},
667                 {2, 2},
668                 {4, 4},
669                 {6, 6}
670                 }
671         },
672         {0, 0, 3, 0,            /* 0x56 */
673                 {{1, 2},
674                 {4, 4},
675                 {6, 6},
676                 {0, 0}
677                 }
678         },
679         {1, 0, 3, 0,            /* 0x57 */
680                 {{0, 2},
681                 {4, 4},
682                 {6, 6},
683                 {0, 0}
684                 }
685         },
686         {0, 0, 2, 0,            /* 0x58 */
687                 {{3, 4},
688                 {6, 6},
689                 {0, 0},
690                 {0, 0}
691                 }
692         },
693         {1, 0, 3, 0,            /* 0x59 */
694                 {{0, 0},
695                 {3, 4},
696                 {6, 6},
697                 {0, 0}
698                 }
699         },
700         {0, 0, 3, 0,            /* 0x5a */
701                 {{1, 1},
702                 {3, 4},
703                 {6, 6},
704                 {0, 0}
705                 }
706         },
707         {1, 0, 3, 0,            /* 0x5b */
708                 {{0, 1},
709                 {3, 4},
710                 {6, 6},
711                 {0, 0}
712                 }
713         },
714         {0, 0, 2, 0,            /* 0x5c */
715                 {{2, 4},
716                 {6, 6},
717                 {0, 0},
718                 {0, 0}
719                 }
720         },
721         {1, 0, 3, 0,            /* 0x5d */
722                 {{0, 0},
723                 {2, 4},
724                 {6, 6},
725                 {0, 0}
726                 }
727         },
728         {0, 0, 2, 0,            /* 0x5e */
729                 {{1, 4},
730                 {6, 6},
731                 {0, 0},
732                 {0, 0}
733                 }
734         },
735         {1, 0, 2, 0,            /* 0x5f */
736                 {{0, 4},
737                 {6, 6},
738                 {0, 0},
739                 {0, 0}
740                 }
741         },
742         {0, 0, 1, 0,            /* 0x60 */
743                 {{5, 6},
744                 {0, 0},
745                 {0, 0},
746                 {0, 0}
747                 }
748         },
749         {1, 0, 2, 0,            /* 0x61 */
750                 {{0, 0},
751                 {5, 6},
752                 {0, 0},
753                 {0, 0}
754                 }
755         },
756         {0, 0, 2, 0,            /* 0x62 */
757                 {{1, 1},
758                 {5, 6},
759                 {0, 0},
760                 {0, 0}
761                 }
762         },
763         {1, 0, 2, 0,            /* 0x63 */
764                 {{0, 1},
765                 {5, 6},
766                 {0, 0},
767                 {0, 0}
768                 }
769         },
770         {0, 0, 2, 0,            /* 0x64 */
771                 {{2, 2},
772                 {5, 6},
773                 {0, 0},
774                 {0, 0}
775                 }
776         },
777         {1, 0, 3, 0,            /* 0x65 */
778                 {{0, 0},
779                 {2, 2},
780                 {5, 6},
781                 {0, 0}
782                 }
783         },
784         {0, 0, 2, 0,            /* 0x66 */
785                 {{1, 2},
786                 {5, 6},
787                 {0, 0},
788                 {0, 0}
789                 }
790         },
791         {1, 0, 2, 0,            /* 0x67 */
792                 {{0, 2},
793                 {5, 6},
794                 {0, 0},
795                 {0, 0}
796                 }
797         },
798         {0, 0, 2, 0,            /* 0x68 */
799                 {{3, 3},
800                 {5, 6},
801                 {0, 0},
802                 {0, 0}
803                 }
804         },
805         {1, 0, 3, 0,            /* 0x69 */
806                 {{0, 0},
807                 {3, 3},
808                 {5, 6},
809                 {0, 0}
810                 }
811         },
812         {0, 0, 3, 0,            /* 0x6a */
813                 {{1, 1},
814                 {3, 3},
815                 {5, 6},
816                 {0, 0}
817                 }
818         },
819         {1, 0, 3, 0,            /* 0x6b */
820                 {{0, 1},
821                 {3, 3},
822                 {5, 6},
823                 {0, 0}
824                 }
825         },
826         {0, 0, 2, 0,            /* 0x6c */
827                 {{2, 3},
828                 {5, 6},
829                 {0, 0},
830                 {0, 0}
831                 }
832         },
833         {1, 0, 3, 0,            /* 0x6d */
834                 {{0, 0},
835                 {2, 3},
836                 {5, 6},
837                 {0, 0}
838                 }
839         },
840         {0, 0, 2, 0,            /* 0x6e */
841                 {{1, 3},
842                 {5, 6},
843                 {0, 0},
844                 {0, 0}
845                 }
846         },
847         {1, 0, 2, 0,            /* 0x6f */
848                 {{0, 3},
849                 {5, 6},
850                 {0, 0},
851                 {0, 0}
852                 }
853         },
854         {0, 0, 1, 0,            /* 0x70 */
855                 {{4, 6},
856                 {0, 0},
857                 {0, 0},
858                 {0, 0}
859                 }
860         },
861         {1, 0, 2, 0,            /* 0x71 */
862                 {{0, 0},
863                 {4, 6},
864                 {0, 0},
865                 {0, 0}
866                 }
867         },
868         {0, 0, 2, 0,            /* 0x72 */
869                 {{1, 1},
870                 {4, 6},
871                 {0, 0},
872                 {0, 0}
873                 }
874         },
875         {1, 0, 2, 0,            /* 0x73 */
876                 {{0, 1},
877                 {4, 6},
878                 {0, 0},
879                 {0, 0}
880                 }
881         },
882         {0, 0, 2, 0,            /* 0x74 */
883                 {{2, 2},
884                 {4, 6},
885                 {0, 0},
886                 {0, 0}
887                 }
888         },
889         {1, 0, 3, 0,            /* 0x75 */
890                 {{0, 0},
891                 {2, 2},
892                 {4, 6},
893                 {0, 0}
894                 }
895         },
896         {0, 0, 2, 0,            /* 0x76 */
897                 {{1, 2},
898                 {4, 6},
899                 {0, 0},
900                 {0, 0}
901                 }
902         },
903         {1, 0, 2, 0,            /* 0x77 */
904                 {{0, 2},
905                 {4, 6},
906                 {0, 0},
907                 {0, 0}
908                 }
909         },
910         {0, 0, 1, 0,            /* 0x78 */
911                 {{3, 6},
912                 {0, 0},
913                 {0, 0},
914                 {0, 0}
915                 }
916         },
917         {1, 0, 2, 0,            /* 0x79 */
918                 {{0, 0},
919                 {3, 6},
920                 {0, 0},
921                 {0, 0}
922                 }
923         },
924         {0, 0, 2, 0,            /* 0x7a */
925                 {{1, 1},
926                 {3, 6},
927                 {0, 0},
928                 {0, 0}
929                 }
930         },
931         {1, 0, 2, 0,            /* 0x7b */
932                 {{0, 1},
933                 {3, 6},
934                 {0, 0},
935                 {0, 0}
936                 }
937         },
938         {0, 0, 1, 0,            /* 0x7c */
939                 {{2, 6},
940                 {0, 0},
941                 {0, 0},
942                 {0, 0}
943                 }
944         },
945         {1, 0, 2, 0,            /* 0x7d */
946                 {{0, 0},
947                 {2, 6},
948                 {0, 0},
949                 {0, 0}
950                 }
951         },
952         {0, 0, 1, 0,            /* 0x7e */
953                 {{1, 6},
954                 {0, 0},
955                 {0, 0},
956                 {0, 0}
957                 }
958         },
959         {1, 0, 1, 0,            /* 0x7f */
960                 {{0, 6},
961                 {0, 0},
962                 {0, 0},
963                 {0, 0}
964                 }
965         },
966         {0, 1, 1, 0,            /* 0x80 */
967                 {{7, 7},
968                 {0, 0},
969                 {0, 0},
970                 {0, 0}
971                 }
972         },
973         {1, 1, 2, 0,            /* 0x81 */
974                 {{0, 0},
975                 {7, 7},
976                 {0, 0},
977                 {0, 0}
978                 }
979         },
980         {0, 1, 2, 0,            /* 0x82 */
981                 {{1, 1},
982                 {7, 7},
983                 {0, 0},
984                 {0, 0}
985                 }
986         },
987         {1, 1, 2, 0,            /* 0x83 */
988                 {{0, 1},
989                 {7, 7},
990                 {0, 0},
991                 {0, 0}
992                 }
993         },
994         {0, 1, 2, 0,            /* 0x84 */
995                 {{2, 2},
996                 {7, 7},
997                 {0, 0},
998                 {0, 0}
999                 }
1000         },
1001         {1, 1, 3, 0,            /* 0x85 */
1002                 {{0, 0},
1003                 {2, 2},
1004                 {7, 7},
1005                 {0, 0}
1006                 }
1007         },
1008         {0, 1, 2, 0,            /* 0x86 */
1009                 {{1, 2},
1010                 {7, 7},
1011                 {0, 0},
1012                 {0, 0}
1013                 }
1014         },
1015         {1, 1, 2, 0,            /* 0x87 */
1016                 {{0, 2},
1017                 {7, 7},
1018                 {0, 0},
1019                 {0, 0}
1020                 }
1021         },
1022         {0, 1, 2, 0,            /* 0x88 */
1023                 {{3, 3},
1024                 {7, 7},
1025                 {0, 0},
1026                 {0, 0}
1027                 }
1028         },
1029         {1, 1, 3, 0,            /* 0x89 */
1030                 {{0, 0},
1031                 {3, 3},
1032                 {7, 7},
1033                 {0, 0}
1034                 }
1035         },
1036         {0, 1, 3, 0,            /* 0x8a */
1037                 {{1, 1},
1038                 {3, 3},
1039                 {7, 7},
1040                 {0, 0}
1041                 }
1042         },
1043         {1, 1, 3, 0,            /* 0x8b */
1044                 {{0, 1},
1045                 {3, 3},
1046                 {7, 7},
1047                 {0, 0}
1048                 }
1049         },
1050         {0, 1, 2, 0,            /* 0x8c */
1051                 {{2, 3},
1052                 {7, 7},
1053                 {0, 0},
1054                 {0, 0}
1055                 }
1056         },
1057         {1, 1, 3, 0,            /* 0x8d */
1058                 {{0, 0},
1059                 {2, 3},
1060                 {7, 7},
1061                 {0, 0}
1062                 }
1063         },
1064         {0, 1, 2, 0,            /* 0x8e */
1065                 {{1, 3},
1066                 {7, 7},
1067                 {0, 0},
1068                 {0, 0}
1069                 }
1070         },
1071         {1, 1, 2, 0,            /* 0x8f */
1072                 {{0, 3},
1073                 {7, 7},
1074                 {0, 0},
1075                 {0, 0}
1076                 }
1077         },
1078         {0, 1, 2, 0,            /* 0x90 */
1079                 {{4, 4},
1080                 {7, 7},
1081                 {0, 0},
1082                 {0, 0}
1083                 }
1084         },
1085         {1, 1, 3, 0,            /* 0x91 */
1086                 {{0, 0},
1087                 {4, 4},
1088                 {7, 7},
1089                 {0, 0}
1090                 }
1091         },
1092         {0, 1, 3, 0,            /* 0x92 */
1093                 {{1, 1},
1094                 {4, 4},
1095                 {7, 7},
1096                 {0, 0}
1097                 }
1098         },
1099         {1, 1, 3, 0,            /* 0x93 */
1100                 {{0, 1},
1101                 {4, 4},
1102                 {7, 7},
1103                 {0, 0}
1104                 }
1105         },
1106         {0, 1, 3, 0,            /* 0x94 */
1107                 {{2, 2},
1108                 {4, 4},
1109                 {7, 7},
1110                 {0, 0}
1111                 }
1112         },
1113         {1, 1, 4, 0,            /* 0x95 */
1114                 {{0, 0},
1115                 {2, 2},
1116                 {4, 4},
1117                 {7, 7}
1118                 }
1119         },
1120         {0, 1, 3, 0,            /* 0x96 */
1121                 {{1, 2},
1122                 {4, 4},
1123                 {7, 7},
1124                 {0, 0}
1125                 }
1126         },
1127         {1, 1, 3, 0,            /* 0x97 */
1128                 {{0, 2},
1129                 {4, 4},
1130                 {7, 7},
1131                 {0, 0}
1132                 }
1133         },
1134         {0, 1, 2, 0,            /* 0x98 */
1135                 {{3, 4},
1136                 {7, 7},
1137                 {0, 0},
1138                 {0, 0}
1139                 }
1140         },
1141         {1, 1, 3, 0,            /* 0x99 */
1142                 {{0, 0},
1143                 {3, 4},
1144                 {7, 7},
1145                 {0, 0}
1146                 }
1147         },
1148         {0, 1, 3, 0,            /* 0x9a */
1149                 {{1, 1},
1150                 {3, 4},
1151                 {7, 7},
1152                 {0, 0}
1153                 }
1154         },
1155         {1, 1, 3, 0,            /* 0x9b */
1156                 {{0, 1},
1157                 {3, 4},
1158                 {7, 7},
1159                 {0, 0}
1160                 }
1161         },
1162         {0, 1, 2, 0,            /* 0x9c */
1163                 {{2, 4},
1164                 {7, 7},
1165                 {0, 0},
1166                 {0, 0}
1167                 }
1168         },
1169         {1, 1, 3, 0,            /* 0x9d */
1170                 {{0, 0},
1171                 {2, 4},
1172                 {7, 7},
1173                 {0, 0}
1174                 }
1175         },
1176         {0, 1, 2, 0,            /* 0x9e */
1177                 {{1, 4},
1178                 {7, 7},
1179                 {0, 0},
1180                 {0, 0}
1181                 }
1182         },
1183         {1, 1, 2, 0,            /* 0x9f */
1184                 {{0, 4},
1185                 {7, 7},
1186                 {0, 0},
1187                 {0, 0}
1188                 }
1189         },
1190         {0, 1, 2, 0,            /* 0xa0 */
1191                 {{5, 5},
1192                 {7, 7},
1193                 {0, 0},
1194                 {0, 0}
1195                 }
1196         },
1197         {1, 1, 3, 0,            /* 0xa1 */
1198                 {{0, 0},
1199                 {5, 5},
1200                 {7, 7},
1201                 {0, 0}
1202                 }
1203         },
1204         {0, 1, 3, 0,            /* 0xa2 */
1205                 {{1, 1},
1206                 {5, 5},
1207                 {7, 7},
1208                 {0, 0}
1209                 }
1210         },
1211         {1, 1, 3, 0,            /* 0xa3 */
1212                 {{0, 1},
1213                 {5, 5},
1214                 {7, 7},
1215                 {0, 0}
1216                 }
1217         },
1218         {0, 1, 3, 0,            /* 0xa4 */
1219                 {{2, 2},
1220                 {5, 5},
1221                 {7, 7},
1222                 {0, 0}
1223                 }
1224         },
1225         {1, 1, 4, 0,            /* 0xa5 */
1226                 {{0, 0},
1227                 {2, 2},
1228                 {5, 5},
1229                 {7, 7}
1230                 }
1231         },
1232         {0, 1, 3, 0,            /* 0xa6 */
1233                 {{1, 2},
1234                 {5, 5},
1235                 {7, 7},
1236                 {0, 0}
1237                 }
1238         },
1239         {1, 1, 3, 0,            /* 0xa7 */
1240                 {{0, 2},
1241                 {5, 5},
1242                 {7, 7},
1243                 {0, 0}
1244                 }
1245         },
1246         {0, 1, 3, 0,            /* 0xa8 */
1247                 {{3, 3},
1248                 {5, 5},
1249                 {7, 7},
1250                 {0, 0}
1251                 }
1252         },
1253         {1, 1, 4, 0,            /* 0xa9 */
1254                 {{0, 0},
1255                 {3, 3},
1256                 {5, 5},
1257                 {7, 7}
1258                 }
1259         },
1260         {0, 1, 4, 0,            /* 0xaa */
1261                 {{1, 1},
1262                 {3, 3},
1263                 {5, 5},
1264                 {7, 7}
1265                 }
1266         },
1267         {1, 1, 4, 0,            /* 0xab */
1268                 {{0, 1},
1269                 {3, 3},
1270                 {5, 5},
1271                 {7, 7}
1272                 }
1273         },
1274         {0, 1, 3, 0,            /* 0xac */
1275                 {{2, 3},
1276                 {5, 5},
1277                 {7, 7},
1278                 {0, 0}
1279                 }
1280         },
1281         {1, 1, 4, 0,            /* 0xad */
1282                 {{0, 0},
1283                 {2, 3},
1284                 {5, 5},
1285                 {7, 7}
1286                 }
1287         },
1288         {0, 1, 3, 0,            /* 0xae */
1289                 {{1, 3},
1290                 {5, 5},
1291                 {7, 7},
1292                 {0, 0}
1293                 }
1294         },
1295         {1, 1, 3, 0,            /* 0xaf */
1296                 {{0, 3},
1297                 {5, 5},
1298                 {7, 7},
1299                 {0, 0}
1300                 }
1301         },
1302         {0, 1, 2, 0,            /* 0xb0 */
1303                 {{4, 5},
1304                 {7, 7},
1305                 {0, 0},
1306                 {0, 0}
1307                 }
1308         },
1309         {1, 1, 3, 0,            /* 0xb1 */
1310                 {{0, 0},
1311                 {4, 5},
1312                 {7, 7},
1313                 {0, 0}
1314                 }
1315         },
1316         {0, 1, 3, 0,            /* 0xb2 */
1317                 {{1, 1},
1318                 {4, 5},
1319                 {7, 7},
1320                 {0, 0}
1321                 }
1322         },
1323         {1, 1, 3, 0,            /* 0xb3 */
1324                 {{0, 1},
1325                 {4, 5},
1326                 {7, 7},
1327                 {0, 0}
1328                 }
1329         },
1330         {0, 1, 3, 0,            /* 0xb4 */
1331                 {{2, 2},
1332                 {4, 5},
1333                 {7, 7},
1334                 {0, 0}
1335                 }
1336         },
1337         {1, 1, 4, 0,            /* 0xb5 */
1338                 {{0, 0},
1339                 {2, 2},
1340                 {4, 5},
1341                 {7, 7}
1342                 }
1343         },
1344         {0, 1, 3, 0,            /* 0xb6 */
1345                 {{1, 2},
1346                 {4, 5},
1347                 {7, 7},
1348                 {0, 0}
1349                 }
1350         },
1351         {1, 1, 3, 0,            /* 0xb7 */
1352                 {{0, 2},
1353                 {4, 5},
1354                 {7, 7},
1355                 {0, 0}
1356                 }
1357         },
1358         {0, 1, 2, 0,            /* 0xb8 */
1359                 {{3, 5},
1360                 {7, 7},
1361                 {0, 0},
1362                 {0, 0}
1363                 }
1364         },
1365         {1, 1, 3, 0,            /* 0xb9 */
1366                 {{0, 0},
1367                 {3, 5},
1368                 {7, 7},
1369                 {0, 0}
1370                 }
1371         },
1372         {0, 1, 3, 0,            /* 0xba */
1373                 {{1, 1},
1374                 {3, 5},
1375                 {7, 7},
1376                 {0, 0}
1377                 }
1378         },
1379         {1, 1, 3, 0,            /* 0xbb */
1380                 {{0, 1},
1381                 {3, 5},
1382                 {7, 7},
1383                 {0, 0}
1384                 }
1385         },
1386         {0, 1, 2, 0,            /* 0xbc */
1387                 {{2, 5},
1388                 {7, 7},
1389                 {0, 0},
1390                 {0, 0}
1391                 }
1392         },
1393         {1, 1, 3, 0,            /* 0xbd */
1394                 {{0, 0},
1395                 {2, 5},
1396                 {7, 7},
1397                 {0, 0}
1398                 }
1399         },
1400         {0, 1, 2, 0,            /* 0xbe */
1401                 {{1, 5},
1402                 {7, 7},
1403                 {0, 0},
1404                 {0, 0}
1405                 }
1406         },
1407         {1, 1, 2, 0,            /* 0xbf */
1408                 {{0, 5},
1409                 {7, 7},
1410                 {0, 0},
1411                 {0, 0}
1412                 }
1413         },
1414         {0, 1, 1, 0,            /* 0xc0 */
1415                 {{6, 7},
1416                 {0, 0},
1417                 {0, 0},
1418                 {0, 0}
1419                 }
1420         },
1421         {1, 1, 2, 0,            /* 0xc1 */
1422                 {{0, 0},
1423                 {6, 7},
1424                 {0, 0},
1425                 {0, 0}
1426                 }
1427         },
1428         {0, 1, 2, 0,            /* 0xc2 */
1429                 {{1, 1},
1430                 {6, 7},
1431                 {0, 0},
1432                 {0, 0}
1433                 }
1434         },
1435         {1, 1, 2, 0,            /* 0xc3 */
1436                 {{0, 1},
1437                 {6, 7},
1438                 {0, 0},
1439                 {0, 0}
1440                 }
1441         },
1442         {0, 1, 2, 0,            /* 0xc4 */
1443                 {{2, 2},
1444                 {6, 7},
1445                 {0, 0},
1446                 {0, 0}
1447                 }
1448         },
1449         {1, 1, 3, 0,            /* 0xc5 */
1450                 {{0, 0},
1451                 {2, 2},
1452                 {6, 7},
1453                 {0, 0}
1454                 }
1455         },
1456         {0, 1, 2, 0,            /* 0xc6 */
1457                 {{1, 2},
1458                 {6, 7},
1459                 {0, 0},
1460                 {0, 0}
1461                 }
1462         },
1463         {1, 1, 2, 0,            /* 0xc7 */
1464                 {{0, 2},
1465                 {6, 7},
1466                 {0, 0},
1467                 {0, 0}
1468                 }
1469         },
1470         {0, 1, 2, 0,            /* 0xc8 */
1471                 {{3, 3},
1472                 {6, 7},
1473                 {0, 0},
1474                 {0, 0}
1475                 }
1476         },
1477         {1, 1, 3, 0,            /* 0xc9 */
1478                 {{0, 0},
1479                 {3, 3},
1480                 {6, 7},
1481                 {0, 0}
1482                 }
1483         },
1484         {0, 1, 3, 0,            /* 0xca */
1485                 {{1, 1},
1486                 {3, 3},
1487                 {6, 7},
1488                 {0, 0}
1489                 }
1490         },
1491         {1, 1, 3, 0,            /* 0xcb */
1492                 {{0, 1},
1493                 {3, 3},
1494                 {6, 7},
1495                 {0, 0}
1496                 }
1497         },
1498         {0, 1, 2, 0,            /* 0xcc */
1499                 {{2, 3},
1500                 {6, 7},
1501                 {0, 0},
1502                 {0, 0}
1503                 }
1504         },
1505         {1, 1, 3, 0,            /* 0xcd */
1506                 {{0, 0},
1507                 {2, 3},
1508                 {6, 7},
1509                 {0, 0}
1510                 }
1511         },
1512         {0, 1, 2, 0,            /* 0xce */
1513                 {{1, 3},
1514                 {6, 7},
1515                 {0, 0},
1516                 {0, 0}
1517                 }
1518         },
1519         {1, 1, 2, 0,            /* 0xcf */
1520                 {{0, 3},
1521                 {6, 7},
1522                 {0, 0},
1523                 {0, 0}
1524                 }
1525         },
1526         {0, 1, 2, 0,            /* 0xd0 */
1527                 {{4, 4},
1528                 {6, 7},
1529                 {0, 0},
1530                 {0, 0}
1531                 }
1532         },
1533         {1, 1, 3, 0,            /* 0xd1 */
1534                 {{0, 0},
1535                 {4, 4},
1536                 {6, 7},
1537                 {0, 0}
1538                 }
1539         },
1540         {0, 1, 3, 0,            /* 0xd2 */
1541                 {{1, 1},
1542                 {4, 4},
1543                 {6, 7},
1544                 {0, 0}
1545                 }
1546         },
1547         {1, 1, 3, 0,            /* 0xd3 */
1548                 {{0, 1},
1549                 {4, 4},
1550                 {6, 7},
1551                 {0, 0}
1552                 }
1553         },
1554         {0, 1, 3, 0,            /* 0xd4 */
1555                 {{2, 2},
1556                 {4, 4},
1557                 {6, 7},
1558                 {0, 0}
1559                 }
1560         },
1561         {1, 1, 4, 0,            /* 0xd5 */
1562                 {{0, 0},
1563                 {2, 2},
1564                 {4, 4},
1565                 {6, 7}
1566                 }
1567         },
1568         {0, 1, 3, 0,            /* 0xd6 */
1569                 {{1, 2},
1570                 {4, 4},
1571                 {6, 7},
1572                 {0, 0}
1573                 }
1574         },
1575         {1, 1, 3, 0,            /* 0xd7 */
1576                 {{0, 2},
1577                 {4, 4},
1578                 {6, 7},
1579                 {0, 0}
1580                 }
1581         },
1582         {0, 1, 2, 0,            /* 0xd8 */
1583                 {{3, 4},
1584                 {6, 7},
1585                 {0, 0},
1586                 {0, 0}
1587                 }
1588         },
1589         {1, 1, 3, 0,            /* 0xd9 */
1590                 {{0, 0},
1591                 {3, 4},
1592                 {6, 7},
1593                 {0, 0}
1594                 }
1595         },
1596         {0, 1, 3, 0,            /* 0xda */
1597                 {{1, 1},
1598                 {3, 4},
1599                 {6, 7},
1600                 {0, 0}
1601                 }
1602         },
1603         {1, 1, 3, 0,            /* 0xdb */
1604                 {{0, 1},
1605                 {3, 4},
1606                 {6, 7},
1607                 {0, 0}
1608                 }
1609         },
1610         {0, 1, 2, 0,            /* 0xdc */
1611                 {{2, 4},
1612                 {6, 7},
1613                 {0, 0},
1614                 {0, 0}
1615                 }
1616         },
1617         {1, 1, 3, 0,            /* 0xdd */
1618                 {{0, 0},
1619                 {2, 4},
1620                 {6, 7},
1621                 {0, 0}
1622                 }
1623         },
1624         {0, 1, 2, 0,            /* 0xde */
1625                 {{1, 4},
1626                 {6, 7},
1627                 {0, 0},
1628                 {0, 0}
1629                 }
1630         },
1631         {1, 1, 2, 0,            /* 0xdf */
1632                 {{0, 4},
1633                 {6, 7},
1634                 {0, 0},
1635                 {0, 0}
1636                 }
1637         },
1638         {0, 1, 1, 0,            /* 0xe0 */
1639                 {{5, 7},
1640                 {0, 0},
1641                 {0, 0},
1642                 {0, 0}
1643                 }
1644         },
1645         {1, 1, 2, 0,            /* 0xe1 */
1646                 {{0, 0},
1647                 {5, 7},
1648                 {0, 0},
1649                 {0, 0}
1650                 }
1651         },
1652         {0, 1, 2, 0,            /* 0xe2 */
1653                 {{1, 1},
1654                 {5, 7},
1655                 {0, 0},
1656                 {0, 0}
1657                 }
1658         },
1659         {1, 1, 2, 0,            /* 0xe3 */
1660                 {{0, 1},
1661                 {5, 7},
1662                 {0, 0},
1663                 {0, 0}
1664                 }
1665         },
1666         {0, 1, 2, 0,            /* 0xe4 */
1667                 {{2, 2},
1668                 {5, 7},
1669                 {0, 0},
1670                 {0, 0}
1671                 }
1672         },
1673         {1, 1, 3, 0,            /* 0xe5 */
1674                 {{0, 0},
1675                 {2, 2},
1676                 {5, 7},
1677                 {0, 0}
1678                 }
1679         },
1680         {0, 1, 2, 0,            /* 0xe6 */
1681                 {{1, 2},
1682                 {5, 7},
1683                 {0, 0},
1684                 {0, 0}
1685                 }
1686         },
1687         {1, 1, 2, 0,            /* 0xe7 */
1688                 {{0, 2},
1689                 {5, 7},
1690                 {0, 0},
1691                 {0, 0}
1692                 }
1693         },
1694         {0, 1, 2, 0,            /* 0xe8 */
1695                 {{3, 3},
1696                 {5, 7},
1697                 {0, 0},
1698                 {0, 0}
1699                 }
1700         },
1701         {1, 1, 3, 0,            /* 0xe9 */
1702                 {{0, 0},
1703                 {3, 3},
1704                 {5, 7},
1705                 {0, 0}
1706                 }
1707         },
1708         {0, 1, 3, 0,            /* 0xea */
1709                 {{1, 1},
1710                 {3, 3},
1711                 {5, 7},
1712                 {0, 0}
1713                 }
1714         },
1715         {1, 1, 3, 0,            /* 0xeb */
1716                 {{0, 1},
1717                 {3, 3},
1718                 {5, 7},
1719                 {0, 0}
1720                 }
1721         },
1722         {0, 1, 2, 0,            /* 0xec */
1723                 {{2, 3},
1724                 {5, 7},
1725                 {0, 0},
1726                 {0, 0}
1727                 }
1728         },
1729         {1, 1, 3, 0,            /* 0xed */
1730                 {{0, 0},
1731                 {2, 3},
1732                 {5, 7},
1733                 {0, 0}
1734                 }
1735         },
1736         {0, 1, 2, 0,            /* 0xee */
1737                 {{1, 3},
1738                 {5, 7},
1739                 {0, 0},
1740                 {0, 0}
1741                 }
1742         },
1743         {1, 1, 2, 0,            /* 0xef */
1744                 {{0, 3},
1745                 {5, 7},
1746                 {0, 0},
1747                 {0, 0}
1748                 }
1749         },
1750         {0, 1, 1, 0,            /* 0xf0 */
1751                 {{4, 7},
1752                 {0, 0},
1753                 {0, 0},
1754                 {0, 0}
1755                 }
1756         },
1757         {1, 1, 2, 0,            /* 0xf1 */
1758                 {{0, 0},
1759                 {4, 7},
1760                 {0, 0},
1761                 {0, 0}
1762                 }
1763         },
1764         {0, 1, 2, 0,            /* 0xf2 */
1765                 {{1, 1},
1766                 {4, 7},
1767                 {0, 0},
1768                 {0, 0}
1769                 }
1770         },
1771         {1, 1, 2, 0,            /* 0xf3 */
1772                 {{0, 1},
1773                 {4, 7},
1774                 {0, 0},
1775                 {0, 0}
1776                 }
1777         },
1778         {0, 1, 2, 0,            /* 0xf4 */
1779                 {{2, 2},
1780                 {4, 7},
1781                 {0, 0},
1782                 {0, 0}
1783                 }
1784         },
1785         {1, 1, 3, 0,            /* 0xf5 */
1786                 {{0, 0},
1787                 {2, 2},
1788                 {4, 7},
1789                 {0, 0}
1790                 }
1791         },
1792         {0, 1, 2, 0,            /* 0xf6 */
1793                 {{1, 2},
1794                 {4, 7},
1795                 {0, 0},
1796                 {0, 0}
1797                 }
1798         },
1799         {1, 1, 2, 0,            /* 0xf7 */
1800                 {{0, 2},
1801                 {4, 7},
1802                 {0, 0},
1803                 {0, 0}
1804                 }
1805         },
1806         {0, 1, 1, 0,            /* 0xf8 */
1807                 {{3, 7},
1808                 {0, 0},
1809                 {0, 0},
1810                 {0, 0}
1811                 }
1812         },
1813         {1, 1, 2, 0,            /* 0xf9 */
1814                 {{0, 0},
1815                 {3, 7},
1816                 {0, 0},
1817                 {0, 0}
1818                 }
1819         },
1820         {0, 1, 2, 0,            /* 0xfa */
1821                 {{1, 1},
1822                 {3, 7},
1823                 {0, 0},
1824                 {0, 0}
1825                 }
1826         },
1827         {1, 1, 2, 0,            /* 0xfb */
1828                 {{0, 1},
1829                 {3, 7},
1830                 {0, 0},
1831                 {0, 0}
1832                 }
1833         },
1834         {0, 1, 1, 0,            /* 0xfc */
1835                 {{2, 7},
1836                 {0, 0},
1837                 {0, 0},
1838                 {0, 0}
1839                 }
1840         },
1841         {1, 1, 2, 0,            /* 0xfd */
1842                 {{0, 0},
1843                 {2, 7},
1844                 {0, 0},
1845                 {0, 0}
1846                 }
1847         },
1848         {0, 1, 1, 0,            /* 0xfe */
1849                 {{1, 7},
1850                 {0, 0},
1851                 {0, 0},
1852                 {0, 0}
1853                 }
1854         },
1855         {1, 1, 1, 0,            /* 0xff */
1856                 {{0, 7},
1857                 {0, 0},
1858                 {0, 0},
1859                 {0, 0}
1860                 }
1861         }
1862 };
1863
1864 int
1865 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1866     struct sctp_scoping *scope,
1867     int do_update)
1868 {
1869         if ((scope->loopback_scope == 0) &&
1870             (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1871                 /*
1872                  * skip loopback if not in scope *
1873                  */
1874                 return (0);
1875         }
1876         switch (ifa->address.sa.sa_family) {
1877 #ifdef INET
1878         case AF_INET:
1879                 if (scope->ipv4_addr_legal) {
1880                         struct sockaddr_in *sin;
1881
1882                         sin = &ifa->address.sin;
1883                         if (sin->sin_addr.s_addr == 0) {
1884                                 /* not in scope , unspecified */
1885                                 return (0);
1886                         }
1887                         if ((scope->ipv4_local_scope == 0) &&
1888                             (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1889                                 /* private address not in scope */
1890                                 return (0);
1891                         }
1892                 } else {
1893                         return (0);
1894                 }
1895                 break;
1896 #endif
1897 #ifdef INET6
1898         case AF_INET6:
1899                 if (scope->ipv6_addr_legal) {
1900                         struct sockaddr_in6 *sin6;
1901
1902                         /*
1903                          * Must update the flags,  bummer, which means any
1904                          * IFA locks must now be applied HERE <->
1905                          */
1906                         if (do_update) {
1907                                 sctp_gather_internal_ifa_flags(ifa);
1908                         }
1909                         if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1910                                 return (0);
1911                         }
1912                         /* ok to use deprecated addresses? */
1913                         sin6 = &ifa->address.sin6;
1914                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1915                                 /* skip unspecified addresses */
1916                                 return (0);
1917                         }
1918                         if (    /* (local_scope == 0) && */
1919                             (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1920                                 return (0);
1921                         }
1922                         if ((scope->site_scope == 0) &&
1923                             (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1924                                 return (0);
1925                         }
1926                 } else {
1927                         return (0);
1928                 }
1929                 break;
1930 #endif
1931         default:
1932                 return (0);
1933         }
1934         return (1);
1935 }
1936
1937 static struct mbuf *
1938 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa, uint16_t *len)
1939 {
1940 #if defined(INET) || defined(INET6)
1941         struct sctp_paramhdr *paramh;
1942         struct mbuf *mret;
1943         uint16_t plen;
1944 #endif
1945
1946         switch (ifa->address.sa.sa_family) {
1947 #ifdef INET
1948         case AF_INET:
1949                 plen = (uint16_t)sizeof(struct sctp_ipv4addr_param);
1950                 break;
1951 #endif
1952 #ifdef INET6
1953         case AF_INET6:
1954                 plen = (uint16_t)sizeof(struct sctp_ipv6addr_param);
1955                 break;
1956 #endif
1957         default:
1958                 return (m);
1959         }
1960 #if defined(INET) || defined(INET6)
1961         if (M_TRAILINGSPACE(m) >= plen) {
1962                 /* easy side we just drop it on the end */
1963                 paramh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1964                 mret = m;
1965         } else {
1966                 /* Need more space */
1967                 mret = m;
1968                 while (SCTP_BUF_NEXT(mret) != NULL) {
1969                         mret = SCTP_BUF_NEXT(mret);
1970                 }
1971                 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(plen, 0, M_NOWAIT, 1, MT_DATA);
1972                 if (SCTP_BUF_NEXT(mret) == NULL) {
1973                         /* We are hosed, can't add more addresses */
1974                         return (m);
1975                 }
1976                 mret = SCTP_BUF_NEXT(mret);
1977                 paramh = mtod(mret, struct sctp_paramhdr *);
1978         }
1979         /* now add the parameter */
1980         switch (ifa->address.sa.sa_family) {
1981 #ifdef INET
1982         case AF_INET:
1983                 {
1984                         struct sctp_ipv4addr_param *ipv4p;
1985                         struct sockaddr_in *sin;
1986
1987                         sin = &ifa->address.sin;
1988                         ipv4p = (struct sctp_ipv4addr_param *)paramh;
1989                         paramh->param_type = htons(SCTP_IPV4_ADDRESS);
1990                         paramh->param_length = htons(plen);
1991                         ipv4p->addr = sin->sin_addr.s_addr;
1992                         SCTP_BUF_LEN(mret) += plen;
1993                         break;
1994                 }
1995 #endif
1996 #ifdef INET6
1997         case AF_INET6:
1998                 {
1999                         struct sctp_ipv6addr_param *ipv6p;
2000                         struct sockaddr_in6 *sin6;
2001
2002                         sin6 = &ifa->address.sin6;
2003                         ipv6p = (struct sctp_ipv6addr_param *)paramh;
2004                         paramh->param_type = htons(SCTP_IPV6_ADDRESS);
2005                         paramh->param_length = htons(plen);
2006                         memcpy(ipv6p->addr, &sin6->sin6_addr,
2007                             sizeof(ipv6p->addr));
2008                         /* clear embedded scope in the address */
2009                         in6_clearscope((struct in6_addr *)ipv6p->addr);
2010                         SCTP_BUF_LEN(mret) += plen;
2011                         break;
2012                 }
2013 #endif
2014         default:
2015                 return (m);
2016         }
2017         if (len != NULL) {
2018                 *len += plen;
2019         }
2020         return (mret);
2021 #endif
2022 }
2023
2024 struct mbuf *
2025 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2026     struct sctp_scoping *scope,
2027     struct mbuf *m_at, int cnt_inits_to,
2028     uint16_t *padding_len, uint16_t *chunk_len)
2029 {
2030         struct sctp_vrf *vrf = NULL;
2031         int cnt, limit_out = 0, total_count;
2032         uint32_t vrf_id;
2033
2034         vrf_id = inp->def_vrf_id;
2035         SCTP_IPI_ADDR_RLOCK();
2036         vrf = sctp_find_vrf(vrf_id);
2037         if (vrf == NULL) {
2038                 SCTP_IPI_ADDR_RUNLOCK();
2039                 return (m_at);
2040         }
2041         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2042                 struct sctp_ifa *sctp_ifap;
2043                 struct sctp_ifn *sctp_ifnp;
2044
2045                 cnt = cnt_inits_to;
2046                 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2047                         limit_out = 1;
2048                         cnt = SCTP_ADDRESS_LIMIT;
2049                         goto skip_count;
2050                 }
2051                 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2052                         if ((scope->loopback_scope == 0) &&
2053                             SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2054                                 /*
2055                                  * Skip loopback devices if loopback_scope
2056                                  * not set
2057                                  */
2058                                 continue;
2059                         }
2060                         LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2061 #ifdef INET
2062                                 if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2063                                     (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2064                                     &sctp_ifap->address.sin.sin_addr) != 0)) {
2065                                         continue;
2066                                 }
2067 #endif
2068 #ifdef INET6
2069                                 if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2070                                     (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2071                                     &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2072                                         continue;
2073                                 }
2074 #endif
2075                                 if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2076                                         continue;
2077                                 }
2078                                 if (sctp_is_address_in_scope(sctp_ifap, scope, 1) == 0) {
2079                                         continue;
2080                                 }
2081                                 cnt++;
2082                                 if (cnt > SCTP_ADDRESS_LIMIT) {
2083                                         break;
2084                                 }
2085                         }
2086                         if (cnt > SCTP_ADDRESS_LIMIT) {
2087                                 break;
2088                         }
2089                 }
2090 skip_count:
2091                 if (cnt > 1) {
2092                         total_count = 0;
2093                         LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2094                                 cnt = 0;
2095                                 if ((scope->loopback_scope == 0) &&
2096                                     SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2097                                         /*
2098                                          * Skip loopback devices if
2099                                          * loopback_scope not set
2100                                          */
2101                                         continue;
2102                                 }
2103                                 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2104 #ifdef INET
2105                                         if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2106                                             (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2107                                             &sctp_ifap->address.sin.sin_addr) != 0)) {
2108                                                 continue;
2109                                         }
2110 #endif
2111 #ifdef INET6
2112                                         if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2113                                             (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2114                                             &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2115                                                 continue;
2116                                         }
2117 #endif
2118                                         if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2119                                                 continue;
2120                                         }
2121                                         if (sctp_is_address_in_scope(sctp_ifap,
2122                                             scope, 0) == 0) {
2123                                                 continue;
2124                                         }
2125                                         if ((chunk_len != NULL) &&
2126                                             (padding_len != NULL) &&
2127                                             (*padding_len > 0)) {
2128                                                 memset(mtod(m_at, caddr_t)+*chunk_len, 0, *padding_len);
2129                                                 SCTP_BUF_LEN(m_at) += *padding_len;
2130                                                 *chunk_len += *padding_len;
2131                                                 *padding_len = 0;
2132                                         }
2133                                         m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap, chunk_len);
2134                                         if (limit_out) {
2135                                                 cnt++;
2136                                                 total_count++;
2137                                                 if (cnt >= 2) {
2138                                                         /*
2139                                                          * two from each
2140                                                          * address
2141                                                          */
2142                                                         break;
2143                                                 }
2144                                                 if (total_count > SCTP_ADDRESS_LIMIT) {
2145                                                         /* No more addresses */
2146                                                         break;
2147                                                 }
2148                                         }
2149                                 }
2150                         }
2151                 }
2152         } else {
2153                 struct sctp_laddr *laddr;
2154
2155                 cnt = cnt_inits_to;
2156                 /* First, how many ? */
2157                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2158                         if (laddr->ifa == NULL) {
2159                                 continue;
2160                         }
2161                         if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2162                                 /*
2163                                  * Address being deleted by the system, dont
2164                                  * list.
2165                                  */
2166                                 continue;
2167                         if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2168                                 /*
2169                                  * Address being deleted on this ep don't
2170                                  * list.
2171                                  */
2172                                 continue;
2173                         }
2174                         if (sctp_is_address_in_scope(laddr->ifa,
2175                             scope, 1) == 0) {
2176                                 continue;
2177                         }
2178                         cnt++;
2179                 }
2180                 /*
2181                  * To get through a NAT we only list addresses if we have
2182                  * more than one. That way if you just bind a single address
2183                  * we let the source of the init dictate our address.
2184                  */
2185                 if (cnt > 1) {
2186                         cnt = cnt_inits_to;
2187                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2188                                 if (laddr->ifa == NULL) {
2189                                         continue;
2190                                 }
2191                                 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2192                                         continue;
2193                                 }
2194                                 if (sctp_is_address_in_scope(laddr->ifa,
2195                                     scope, 0) == 0) {
2196                                         continue;
2197                                 }
2198                                 if ((chunk_len != NULL) &&
2199                                     (padding_len != NULL) &&
2200                                     (*padding_len > 0)) {
2201                                         memset(mtod(m_at, caddr_t)+*chunk_len, 0, *padding_len);
2202                                         SCTP_BUF_LEN(m_at) += *padding_len;
2203                                         *chunk_len += *padding_len;
2204                                         *padding_len = 0;
2205                                 }
2206                                 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa, chunk_len);
2207                                 cnt++;
2208                                 if (cnt >= SCTP_ADDRESS_LIMIT) {
2209                                         break;
2210                                 }
2211                         }
2212                 }
2213         }
2214         SCTP_IPI_ADDR_RUNLOCK();
2215         return (m_at);
2216 }
2217
2218 static struct sctp_ifa *
2219 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2220     uint8_t dest_is_loop,
2221     uint8_t dest_is_priv,
2222     sa_family_t fam)
2223 {
2224         uint8_t dest_is_global = 0;
2225
2226         /* dest_is_priv is true if destination is a private address */
2227         /* dest_is_loop is true if destination is a loopback addresses */
2228
2229         /**
2230          * Here we determine if its a preferred address. A preferred address
2231          * means it is the same scope or higher scope then the destination.
2232          * L = loopback, P = private, G = global
2233          * -----------------------------------------
2234          *    src    |  dest | result
2235          *  ----------------------------------------
2236          *     L     |    L  |    yes
2237          *  -----------------------------------------
2238          *     P     |    L  |    yes-v4 no-v6
2239          *  -----------------------------------------
2240          *     G     |    L  |    yes-v4 no-v6
2241          *  -----------------------------------------
2242          *     L     |    P  |    no
2243          *  -----------------------------------------
2244          *     P     |    P  |    yes
2245          *  -----------------------------------------
2246          *     G     |    P  |    no
2247          *   -----------------------------------------
2248          *     L     |    G  |    no
2249          *   -----------------------------------------
2250          *     P     |    G  |    no
2251          *    -----------------------------------------
2252          *     G     |    G  |    yes
2253          *    -----------------------------------------
2254          */
2255
2256         if (ifa->address.sa.sa_family != fam) {
2257                 /* forget mis-matched family */
2258                 return (NULL);
2259         }
2260         if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2261                 dest_is_global = 1;
2262         }
2263         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2264         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2265         /* Ok the address may be ok */
2266 #ifdef INET6
2267         if (fam == AF_INET6) {
2268                 /* ok to use deprecated addresses? no lets not! */
2269                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2270                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2271                         return (NULL);
2272                 }
2273                 if (ifa->src_is_priv && !ifa->src_is_loop) {
2274                         if (dest_is_loop) {
2275                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2276                                 return (NULL);
2277                         }
2278                 }
2279                 if (ifa->src_is_glob) {
2280                         if (dest_is_loop) {
2281                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2282                                 return (NULL);
2283                         }
2284                 }
2285         }
2286 #endif
2287         /*
2288          * Now that we know what is what, implement or table this could in
2289          * theory be done slicker (it used to be), but this is
2290          * straightforward and easier to validate :-)
2291          */
2292         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2293             ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2294         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2295             dest_is_loop, dest_is_priv, dest_is_global);
2296
2297         if ((ifa->src_is_loop) && (dest_is_priv)) {
2298                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2299                 return (NULL);
2300         }
2301         if ((ifa->src_is_glob) && (dest_is_priv)) {
2302                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2303                 return (NULL);
2304         }
2305         if ((ifa->src_is_loop) && (dest_is_global)) {
2306                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2307                 return (NULL);
2308         }
2309         if ((ifa->src_is_priv) && (dest_is_global)) {
2310                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2311                 return (NULL);
2312         }
2313         SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2314         /* its a preferred address */
2315         return (ifa);
2316 }
2317
2318 static struct sctp_ifa *
2319 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2320     uint8_t dest_is_loop,
2321     uint8_t dest_is_priv,
2322     sa_family_t fam)
2323 {
2324         uint8_t dest_is_global = 0;
2325
2326         /**
2327          * Here we determine if its a acceptable address. A acceptable
2328          * address means it is the same scope or higher scope but we can
2329          * allow for NAT which means its ok to have a global dest and a
2330          * private src.
2331          *
2332          * L = loopback, P = private, G = global
2333          * -----------------------------------------
2334          *  src    |  dest | result
2335          * -----------------------------------------
2336          *   L     |   L   |    yes
2337          *  -----------------------------------------
2338          *   P     |   L   |    yes-v4 no-v6
2339          *  -----------------------------------------
2340          *   G     |   L   |    yes
2341          * -----------------------------------------
2342          *   L     |   P   |    no
2343          * -----------------------------------------
2344          *   P     |   P   |    yes
2345          * -----------------------------------------
2346          *   G     |   P   |    yes - May not work
2347          * -----------------------------------------
2348          *   L     |   G   |    no
2349          * -----------------------------------------
2350          *   P     |   G   |    yes - May not work
2351          * -----------------------------------------
2352          *   G     |   G   |    yes
2353          * -----------------------------------------
2354          */
2355
2356         if (ifa->address.sa.sa_family != fam) {
2357                 /* forget non matching family */
2358                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2359                     ifa->address.sa.sa_family, fam);
2360                 return (NULL);
2361         }
2362         /* Ok the address may be ok */
2363         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2364         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2365             dest_is_loop, dest_is_priv);
2366         if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2367                 dest_is_global = 1;
2368         }
2369 #ifdef INET6
2370         if (fam == AF_INET6) {
2371                 /* ok to use deprecated addresses? */
2372                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2373                         return (NULL);
2374                 }
2375                 if (ifa->src_is_priv) {
2376                         /* Special case, linklocal to loop */
2377                         if (dest_is_loop)
2378                                 return (NULL);
2379                 }
2380         }
2381 #endif
2382         /*
2383          * Now that we know what is what, implement our table. This could in
2384          * theory be done slicker (it used to be), but this is
2385          * straightforward and easier to validate :-)
2386          */
2387         SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2388             ifa->src_is_loop,
2389             dest_is_priv);
2390         if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2391                 return (NULL);
2392         }
2393         SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2394             ifa->src_is_loop,
2395             dest_is_global);
2396         if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2397                 return (NULL);
2398         }
2399         SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2400         /* its an acceptable address */
2401         return (ifa);
2402 }
2403
2404 int
2405 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2406 {
2407         struct sctp_laddr *laddr;
2408
2409         if (stcb == NULL) {
2410                 /* There are no restrictions, no TCB :-) */
2411                 return (0);
2412         }
2413         LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2414                 if (laddr->ifa == NULL) {
2415                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2416                             __func__);
2417                         continue;
2418                 }
2419                 if (laddr->ifa == ifa) {
2420                         /* Yes it is on the list */
2421                         return (1);
2422                 }
2423         }
2424         return (0);
2425 }
2426
2427 int
2428 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2429 {
2430         struct sctp_laddr *laddr;
2431
2432         if (ifa == NULL)
2433                 return (0);
2434         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2435                 if (laddr->ifa == NULL) {
2436                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2437                             __func__);
2438                         continue;
2439                 }
2440                 if ((laddr->ifa == ifa) && laddr->action == 0)
2441                         /* same pointer */
2442                         return (1);
2443         }
2444         return (0);
2445 }
2446
2447 static struct sctp_ifa *
2448 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2449     sctp_route_t *ro,
2450     uint32_t vrf_id,
2451     int non_asoc_addr_ok,
2452     uint8_t dest_is_priv,
2453     uint8_t dest_is_loop,
2454     sa_family_t fam)
2455 {
2456         struct sctp_laddr *laddr, *starting_point;
2457         void *ifn;
2458         int resettotop = 0;
2459         struct sctp_ifn *sctp_ifn;
2460         struct sctp_ifa *sctp_ifa, *sifa;
2461         struct sctp_vrf *vrf;
2462         uint32_t ifn_index;
2463
2464         vrf = sctp_find_vrf(vrf_id);
2465         if (vrf == NULL)
2466                 return (NULL);
2467
2468         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2469         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2470         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2471         /*
2472          * first question, is the ifn we will emit on in our list, if so, we
2473          * want such an address. Note that we first looked for a preferred
2474          * address.
2475          */
2476         if (sctp_ifn) {
2477                 /* is a preferred one on the interface we route out? */
2478                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2479 #ifdef INET
2480                         if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2481                             (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2482                             &sctp_ifa->address.sin.sin_addr) != 0)) {
2483                                 continue;
2484                         }
2485 #endif
2486 #ifdef INET6
2487                         if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2488                             (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2489                             &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2490                                 continue;
2491                         }
2492 #endif
2493                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2494                             (non_asoc_addr_ok == 0))
2495                                 continue;
2496                         sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2497                             dest_is_loop,
2498                             dest_is_priv, fam);
2499                         if (sifa == NULL)
2500                                 continue;
2501                         if (sctp_is_addr_in_ep(inp, sifa)) {
2502                                 atomic_add_int(&sifa->refcount, 1);
2503                                 return (sifa);
2504                         }
2505                 }
2506         }
2507         /*
2508          * ok, now we now need to find one on the list of the addresses. We
2509          * can't get one on the emitting interface so let's find first a
2510          * preferred one. If not that an acceptable one otherwise... we
2511          * return NULL.
2512          */
2513         starting_point = inp->next_addr_touse;
2514 once_again:
2515         if (inp->next_addr_touse == NULL) {
2516                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2517                 resettotop = 1;
2518         }
2519         for (laddr = inp->next_addr_touse; laddr;
2520             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2521                 if (laddr->ifa == NULL) {
2522                         /* address has been removed */
2523                         continue;
2524                 }
2525                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2526                         /* address is being deleted */
2527                         continue;
2528                 }
2529                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2530                     dest_is_priv, fam);
2531                 if (sifa == NULL)
2532                         continue;
2533                 atomic_add_int(&sifa->refcount, 1);
2534                 return (sifa);
2535         }
2536         if (resettotop == 0) {
2537                 inp->next_addr_touse = NULL;
2538                 goto once_again;
2539         }
2540
2541         inp->next_addr_touse = starting_point;
2542         resettotop = 0;
2543 once_again_too:
2544         if (inp->next_addr_touse == NULL) {
2545                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2546                 resettotop = 1;
2547         }
2548
2549         /* ok, what about an acceptable address in the inp */
2550         for (laddr = inp->next_addr_touse; laddr;
2551             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2552                 if (laddr->ifa == NULL) {
2553                         /* address has been removed */
2554                         continue;
2555                 }
2556                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2557                         /* address is being deleted */
2558                         continue;
2559                 }
2560                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2561                     dest_is_priv, fam);
2562                 if (sifa == NULL)
2563                         continue;
2564                 atomic_add_int(&sifa->refcount, 1);
2565                 return (sifa);
2566         }
2567         if (resettotop == 0) {
2568                 inp->next_addr_touse = NULL;
2569                 goto once_again_too;
2570         }
2571
2572         /*
2573          * no address bound can be a source for the destination we are in
2574          * trouble
2575          */
2576         return (NULL);
2577 }
2578
2579 static struct sctp_ifa *
2580 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2581     struct sctp_tcb *stcb,
2582     sctp_route_t *ro,
2583     uint32_t vrf_id,
2584     uint8_t dest_is_priv,
2585     uint8_t dest_is_loop,
2586     int non_asoc_addr_ok,
2587     sa_family_t fam)
2588 {
2589         struct sctp_laddr *laddr, *starting_point;
2590         void *ifn;
2591         struct sctp_ifn *sctp_ifn;
2592         struct sctp_ifa *sctp_ifa, *sifa;
2593         uint8_t start_at_beginning = 0;
2594         struct sctp_vrf *vrf;
2595         uint32_t ifn_index;
2596
2597         /*
2598          * first question, is the ifn we will emit on in our list, if so, we
2599          * want that one.
2600          */
2601         vrf = sctp_find_vrf(vrf_id);
2602         if (vrf == NULL)
2603                 return (NULL);
2604
2605         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2606         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2607         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2608
2609         /*
2610          * first question, is the ifn we will emit on in our list?  If so,
2611          * we want that one. First we look for a preferred. Second, we go
2612          * for an acceptable.
2613          */
2614         if (sctp_ifn) {
2615                 /* first try for a preferred address on the ep */
2616                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2617 #ifdef INET
2618                         if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2619                             (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2620                             &sctp_ifa->address.sin.sin_addr) != 0)) {
2621                                 continue;
2622                         }
2623 #endif
2624 #ifdef INET6
2625                         if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2626                             (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2627                             &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2628                                 continue;
2629                         }
2630 #endif
2631                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2632                                 continue;
2633                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2634                                 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2635                                 if (sifa == NULL)
2636                                         continue;
2637                                 if (((non_asoc_addr_ok == 0) &&
2638                                     (sctp_is_addr_restricted(stcb, sifa))) ||
2639                                     (non_asoc_addr_ok &&
2640                                     (sctp_is_addr_restricted(stcb, sifa)) &&
2641                                     (!sctp_is_addr_pending(stcb, sifa)))) {
2642                                         /* on the no-no list */
2643                                         continue;
2644                                 }
2645                                 atomic_add_int(&sifa->refcount, 1);
2646                                 return (sifa);
2647                         }
2648                 }
2649                 /* next try for an acceptable address on the ep */
2650                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2651 #ifdef INET
2652                         if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2653                             (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2654                             &sctp_ifa->address.sin.sin_addr) != 0)) {
2655                                 continue;
2656                         }
2657 #endif
2658 #ifdef INET6
2659                         if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2660                             (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2661                             &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2662                                 continue;
2663                         }
2664 #endif
2665                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2666                                 continue;
2667                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2668                                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2669                                 if (sifa == NULL)
2670                                         continue;
2671                                 if (((non_asoc_addr_ok == 0) &&
2672                                     (sctp_is_addr_restricted(stcb, sifa))) ||
2673                                     (non_asoc_addr_ok &&
2674                                     (sctp_is_addr_restricted(stcb, sifa)) &&
2675                                     (!sctp_is_addr_pending(stcb, sifa)))) {
2676                                         /* on the no-no list */
2677                                         continue;
2678                                 }
2679                                 atomic_add_int(&sifa->refcount, 1);
2680                                 return (sifa);
2681                         }
2682                 }
2683         }
2684         /*
2685          * if we can't find one like that then we must look at all addresses
2686          * bound to pick one at first preferable then secondly acceptable.
2687          */
2688         starting_point = stcb->asoc.last_used_address;
2689 sctp_from_the_top:
2690         if (stcb->asoc.last_used_address == NULL) {
2691                 start_at_beginning = 1;
2692                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2693         }
2694         /* search beginning with the last used address */
2695         for (laddr = stcb->asoc.last_used_address; laddr;
2696             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2697                 if (laddr->ifa == NULL) {
2698                         /* address has been removed */
2699                         continue;
2700                 }
2701                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2702                         /* address is being deleted */
2703                         continue;
2704                 }
2705                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2706                 if (sifa == NULL)
2707                         continue;
2708                 if (((non_asoc_addr_ok == 0) &&
2709                     (sctp_is_addr_restricted(stcb, sifa))) ||
2710                     (non_asoc_addr_ok &&
2711                     (sctp_is_addr_restricted(stcb, sifa)) &&
2712                     (!sctp_is_addr_pending(stcb, sifa)))) {
2713                         /* on the no-no list */
2714                         continue;
2715                 }
2716                 stcb->asoc.last_used_address = laddr;
2717                 atomic_add_int(&sifa->refcount, 1);
2718                 return (sifa);
2719         }
2720         if (start_at_beginning == 0) {
2721                 stcb->asoc.last_used_address = NULL;
2722                 goto sctp_from_the_top;
2723         }
2724         /* now try for any higher scope than the destination */
2725         stcb->asoc.last_used_address = starting_point;
2726         start_at_beginning = 0;
2727 sctp_from_the_top2:
2728         if (stcb->asoc.last_used_address == NULL) {
2729                 start_at_beginning = 1;
2730                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2731         }
2732         /* search beginning with the last used address */
2733         for (laddr = stcb->asoc.last_used_address; laddr;
2734             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2735                 if (laddr->ifa == NULL) {
2736                         /* address has been removed */
2737                         continue;
2738                 }
2739                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2740                         /* address is being deleted */
2741                         continue;
2742                 }
2743                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2744                     dest_is_priv, fam);
2745                 if (sifa == NULL)
2746                         continue;
2747                 if (((non_asoc_addr_ok == 0) &&
2748                     (sctp_is_addr_restricted(stcb, sifa))) ||
2749                     (non_asoc_addr_ok &&
2750                     (sctp_is_addr_restricted(stcb, sifa)) &&
2751                     (!sctp_is_addr_pending(stcb, sifa)))) {
2752                         /* on the no-no list */
2753                         continue;
2754                 }
2755                 stcb->asoc.last_used_address = laddr;
2756                 atomic_add_int(&sifa->refcount, 1);
2757                 return (sifa);
2758         }
2759         if (start_at_beginning == 0) {
2760                 stcb->asoc.last_used_address = NULL;
2761                 goto sctp_from_the_top2;
2762         }
2763         return (NULL);
2764 }
2765
2766 static struct sctp_ifa *
2767 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2768     struct sctp_inpcb *inp,
2769     struct sctp_tcb *stcb,
2770     int non_asoc_addr_ok,
2771     uint8_t dest_is_loop,
2772     uint8_t dest_is_priv,
2773     int addr_wanted,
2774     sa_family_t fam,
2775     sctp_route_t *ro)
2776 {
2777         struct sctp_ifa *ifa, *sifa;
2778         int num_eligible_addr = 0;
2779 #ifdef INET6
2780         struct sockaddr_in6 sin6, lsa6;
2781
2782         if (fam == AF_INET6) {
2783                 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2784                 (void)sa6_recoverscope(&sin6);
2785         }
2786 #endif                          /* INET6 */
2787         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2788 #ifdef INET
2789                 if ((ifa->address.sa.sa_family == AF_INET) &&
2790                     (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2791                     &ifa->address.sin.sin_addr) != 0)) {
2792                         continue;
2793                 }
2794 #endif
2795 #ifdef INET6
2796                 if ((ifa->address.sa.sa_family == AF_INET6) &&
2797                     (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2798                     &ifa->address.sin6.sin6_addr) != 0)) {
2799                         continue;
2800                 }
2801 #endif
2802                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2803                     (non_asoc_addr_ok == 0))
2804                         continue;
2805                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2806                     dest_is_priv, fam);
2807                 if (sifa == NULL)
2808                         continue;
2809 #ifdef INET6
2810                 if (fam == AF_INET6 &&
2811                     dest_is_loop &&
2812                     sifa->src_is_loop && sifa->src_is_priv) {
2813                         /*
2814                          * don't allow fe80::1 to be a src on loop ::1, we
2815                          * don't list it to the peer so we will get an
2816                          * abort.
2817                          */
2818                         continue;
2819                 }
2820                 if (fam == AF_INET6 &&
2821                     IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2822                     IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2823                         /*
2824                          * link-local <-> link-local must belong to the same
2825                          * scope.
2826                          */
2827                         memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2828                         (void)sa6_recoverscope(&lsa6);
2829                         if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2830                                 continue;
2831                         }
2832                 }
2833 #endif                          /* INET6 */
2834
2835                 /*
2836                  * Check if the IPv6 address matches to next-hop. In the
2837                  * mobile case, old IPv6 address may be not deleted from the
2838                  * interface. Then, the interface has previous and new
2839                  * addresses.  We should use one corresponding to the
2840                  * next-hop.  (by micchie)
2841                  */
2842 #ifdef INET6
2843                 if (stcb && fam == AF_INET6 &&
2844                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2845                         if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) == 0) {
2846                                 continue;
2847                         }
2848                 }
2849 #endif
2850 #ifdef INET
2851                 /* Avoid topologically incorrect IPv4 address */
2852                 if (stcb && fam == AF_INET &&
2853                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2854                         if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2855                                 continue;
2856                         }
2857                 }
2858 #endif
2859                 if (stcb) {
2860                         if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2861                                 continue;
2862                         }
2863                         if (((non_asoc_addr_ok == 0) &&
2864                             (sctp_is_addr_restricted(stcb, sifa))) ||
2865                             (non_asoc_addr_ok &&
2866                             (sctp_is_addr_restricted(stcb, sifa)) &&
2867                             (!sctp_is_addr_pending(stcb, sifa)))) {
2868                                 /*
2869                                  * It is restricted for some reason..
2870                                  * probably not yet added.
2871                                  */
2872                                 continue;
2873                         }
2874                 }
2875                 if (num_eligible_addr >= addr_wanted) {
2876                         return (sifa);
2877                 }
2878                 num_eligible_addr++;
2879         }
2880         return (NULL);
2881 }
2882
2883 static int
2884 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2885     struct sctp_inpcb *inp,
2886     struct sctp_tcb *stcb,
2887     int non_asoc_addr_ok,
2888     uint8_t dest_is_loop,
2889     uint8_t dest_is_priv,
2890     sa_family_t fam)
2891 {
2892         struct sctp_ifa *ifa, *sifa;
2893         int num_eligible_addr = 0;
2894
2895         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2896 #ifdef INET
2897                 if ((ifa->address.sa.sa_family == AF_INET) &&
2898                     (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2899                     &ifa->address.sin.sin_addr) != 0)) {
2900                         continue;
2901                 }
2902 #endif
2903 #ifdef INET6
2904                 if ((ifa->address.sa.sa_family == AF_INET6) &&
2905                     (stcb != NULL) &&
2906                     (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2907                     &ifa->address.sin6.sin6_addr) != 0)) {
2908                         continue;
2909                 }
2910 #endif
2911                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2912                     (non_asoc_addr_ok == 0)) {
2913                         continue;
2914                 }
2915                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2916                     dest_is_priv, fam);
2917                 if (sifa == NULL) {
2918                         continue;
2919                 }
2920                 if (stcb) {
2921                         if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2922                                 continue;
2923                         }
2924                         if (((non_asoc_addr_ok == 0) &&
2925                             (sctp_is_addr_restricted(stcb, sifa))) ||
2926                             (non_asoc_addr_ok &&
2927                             (sctp_is_addr_restricted(stcb, sifa)) &&
2928                             (!sctp_is_addr_pending(stcb, sifa)))) {
2929                                 /*
2930                                  * It is restricted for some reason..
2931                                  * probably not yet added.
2932                                  */
2933                                 continue;
2934                         }
2935                 }
2936                 num_eligible_addr++;
2937         }
2938         return (num_eligible_addr);
2939 }
2940
2941 static struct sctp_ifa *
2942 sctp_choose_boundall(struct sctp_inpcb *inp,
2943     struct sctp_tcb *stcb,
2944     struct sctp_nets *net,
2945     sctp_route_t *ro,
2946     uint32_t vrf_id,
2947     uint8_t dest_is_priv,
2948     uint8_t dest_is_loop,
2949     int non_asoc_addr_ok,
2950     sa_family_t fam)
2951 {
2952         int cur_addr_num = 0, num_preferred = 0;
2953         void *ifn;
2954         struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2955         struct sctp_ifa *sctp_ifa, *sifa;
2956         uint32_t ifn_index;
2957         struct sctp_vrf *vrf;
2958 #ifdef INET
2959         int retried = 0;
2960 #endif
2961
2962         /*-
2963          * For boundall we can use any address in the association.
2964          * If non_asoc_addr_ok is set we can use any address (at least in
2965          * theory). So we look for preferred addresses first. If we find one,
2966          * we use it. Otherwise we next try to get an address on the
2967          * interface, which we should be able to do (unless non_asoc_addr_ok
2968          * is false and we are routed out that way). In these cases where we
2969          * can't use the address of the interface we go through all the
2970          * ifn's looking for an address we can use and fill that in. Punting
2971          * means we send back address 0, which will probably cause problems
2972          * actually since then IP will fill in the address of the route ifn,
2973          * which means we probably already rejected it.. i.e. here comes an
2974          * abort :-<.
2975          */
2976         vrf = sctp_find_vrf(vrf_id);
2977         if (vrf == NULL)
2978                 return (NULL);
2979
2980         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2981         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2982         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
2983         emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2984         if (sctp_ifn == NULL) {
2985                 /* ?? We don't have this guy ?? */
2986                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2987                 goto bound_all_plan_b;
2988         }
2989         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2990             ifn_index, sctp_ifn->ifn_name);
2991
2992         if (net) {
2993                 cur_addr_num = net->indx_of_eligible_next_to_use;
2994         }
2995         num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2996             inp, stcb,
2997             non_asoc_addr_ok,
2998             dest_is_loop,
2999             dest_is_priv, fam);
3000         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
3001             num_preferred, sctp_ifn->ifn_name);
3002         if (num_preferred == 0) {
3003                 /*
3004                  * no eligible addresses, we must use some other interface
3005                  * address if we can find one.
3006                  */
3007                 goto bound_all_plan_b;
3008         }
3009         /*
3010          * Ok we have num_eligible_addr set with how many we can use, this
3011          * may vary from call to call due to addresses being deprecated
3012          * etc..
3013          */
3014         if (cur_addr_num >= num_preferred) {
3015                 cur_addr_num = 0;
3016         }
3017         /*
3018          * select the nth address from the list (where cur_addr_num is the
3019          * nth) and 0 is the first one, 1 is the second one etc...
3020          */
3021         SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
3022
3023         sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3024             dest_is_priv, cur_addr_num, fam, ro);
3025
3026         /* if sctp_ifa is NULL something changed??, fall to plan b. */
3027         if (sctp_ifa) {
3028                 atomic_add_int(&sctp_ifa->refcount, 1);
3029                 if (net) {
3030                         /* save off where the next one we will want */
3031                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3032                 }
3033                 return (sctp_ifa);
3034         }
3035         /*
3036          * plan_b: Look at all interfaces and find a preferred address. If
3037          * no preferred fall through to plan_c.
3038          */
3039 bound_all_plan_b:
3040         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
3041         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3042                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
3043                     sctp_ifn->ifn_name);
3044                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3045                         /* wrong base scope */
3046                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
3047                         continue;
3048                 }
3049                 if ((sctp_ifn == looked_at) && looked_at) {
3050                         /* already looked at this guy */
3051                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
3052                         continue;
3053                 }
3054                 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok,
3055                     dest_is_loop, dest_is_priv, fam);
3056                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
3057                     "Found ifn:%p %d preferred source addresses\n",
3058                     ifn, num_preferred);
3059                 if (num_preferred == 0) {
3060                         /* None on this interface. */
3061                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "No preferred -- skipping to next\n");
3062                         continue;
3063                 }
3064                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
3065                     "num preferred:%d on interface:%p cur_addr_num:%d\n",
3066                     num_preferred, (void *)sctp_ifn, cur_addr_num);
3067
3068                 /*
3069                  * Ok we have num_eligible_addr set with how many we can
3070                  * use, this may vary from call to call due to addresses
3071                  * being deprecated etc..
3072                  */
3073                 if (cur_addr_num >= num_preferred) {
3074                         cur_addr_num = 0;
3075                 }
3076                 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3077                     dest_is_priv, cur_addr_num, fam, ro);
3078                 if (sifa == NULL)
3079                         continue;
3080                 if (net) {
3081                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3082                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3083                             cur_addr_num);
3084                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3085                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3086                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3087                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3088                 }
3089                 atomic_add_int(&sifa->refcount, 1);
3090                 return (sifa);
3091         }
3092 #ifdef INET
3093 again_with_private_addresses_allowed:
3094 #endif
3095         /* plan_c: do we have an acceptable address on the emit interface */
3096         sifa = NULL;
3097         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
3098         if (emit_ifn == NULL) {
3099                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
3100                 goto plan_d;
3101         }
3102         LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3103                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", (void *)sctp_ifa);
3104 #ifdef INET
3105                 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3106                     (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3107                     &sctp_ifa->address.sin.sin_addr) != 0)) {
3108                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jailed\n");
3109                         continue;
3110                 }
3111 #endif
3112 #ifdef INET6
3113                 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3114                     (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3115                     &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3116                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jailed\n");
3117                         continue;
3118                 }
3119 #endif
3120                 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3121                     (non_asoc_addr_ok == 0)) {
3122                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
3123                         continue;
3124                 }
3125                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3126                     dest_is_priv, fam);
3127                 if (sifa == NULL) {
3128                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3129                         continue;
3130                 }
3131                 if (stcb) {
3132                         if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3133                                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3134                                 sifa = NULL;
3135                                 continue;
3136                         }
3137                         if (((non_asoc_addr_ok == 0) &&
3138                             (sctp_is_addr_restricted(stcb, sifa))) ||
3139                             (non_asoc_addr_ok &&
3140                             (sctp_is_addr_restricted(stcb, sifa)) &&
3141                             (!sctp_is_addr_pending(stcb, sifa)))) {
3142                                 /*
3143                                  * It is restricted for some reason..
3144                                  * probably not yet added.
3145                                  */
3146                                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its restricted\n");
3147                                 sifa = NULL;
3148                                 continue;
3149                         }
3150                 }
3151                 atomic_add_int(&sifa->refcount, 1);
3152                 goto out;
3153         }
3154 plan_d:
3155         /*
3156          * plan_d: We are in trouble. No preferred address on the emit
3157          * interface. And not even a preferred address on all interfaces. Go
3158          * out and see if we can find an acceptable address somewhere
3159          * amongst all interfaces.
3160          */
3161         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", (void *)looked_at);
3162         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3163                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3164                         /* wrong base scope */
3165                         continue;
3166                 }
3167                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3168 #ifdef INET
3169                         if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3170                             (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3171                             &sctp_ifa->address.sin.sin_addr) != 0)) {
3172                                 continue;
3173                         }
3174 #endif
3175 #ifdef INET6
3176                         if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3177                             (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3178                             &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3179                                 continue;
3180                         }
3181 #endif
3182                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3183                             (non_asoc_addr_ok == 0))
3184                                 continue;
3185                         sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3186                             dest_is_loop,
3187                             dest_is_priv, fam);
3188                         if (sifa == NULL)
3189                                 continue;
3190                         if (stcb) {
3191                                 if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3192                                         sifa = NULL;
3193                                         continue;
3194                                 }
3195                                 if (((non_asoc_addr_ok == 0) &&
3196                                     (sctp_is_addr_restricted(stcb, sifa))) ||
3197                                     (non_asoc_addr_ok &&
3198                                     (sctp_is_addr_restricted(stcb, sifa)) &&
3199                                     (!sctp_is_addr_pending(stcb, sifa)))) {
3200                                         /*
3201                                          * It is restricted for some
3202                                          * reason.. probably not yet added.
3203                                          */
3204                                         sifa = NULL;
3205                                         continue;
3206                                 }
3207                         }
3208                         goto out;
3209                 }
3210         }
3211 #ifdef INET
3212         if (stcb) {
3213                 if ((retried == 0) && (stcb->asoc.scope.ipv4_local_scope == 0)) {
3214                         stcb->asoc.scope.ipv4_local_scope = 1;
3215                         retried = 1;
3216                         goto again_with_private_addresses_allowed;
3217                 } else if (retried == 1) {
3218                         stcb->asoc.scope.ipv4_local_scope = 0;
3219                 }
3220         }
3221 #endif
3222 out:
3223 #ifdef INET
3224         if (sifa) {
3225                 if (retried == 1) {
3226                         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3227                                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3228                                         /* wrong base scope */
3229                                         continue;
3230                                 }
3231                                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3232                                         struct sctp_ifa *tmp_sifa;
3233
3234 #ifdef INET
3235                                         if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3236                                             (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3237                                             &sctp_ifa->address.sin.sin_addr) != 0)) {
3238                                                 continue;
3239                                         }
3240 #endif
3241 #ifdef INET6
3242                                         if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3243                                             (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3244                                             &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3245                                                 continue;
3246                                         }
3247 #endif
3248                                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3249                                             (non_asoc_addr_ok == 0))
3250                                                 continue;
3251                                         tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3252                                             dest_is_loop,
3253                                             dest_is_priv, fam);
3254                                         if (tmp_sifa == NULL) {
3255                                                 continue;
3256                                         }
3257                                         if (tmp_sifa == sifa) {
3258                                                 continue;
3259                                         }
3260                                         if (stcb) {
3261                                                 if (sctp_is_address_in_scope(tmp_sifa,
3262                                                     &stcb->asoc.scope, 0) == 0) {
3263                                                         continue;
3264                                                 }
3265                                                 if (((non_asoc_addr_ok == 0) &&
3266                                                     (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3267                                                     (non_asoc_addr_ok &&
3268                                                     (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3269                                                     (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3270                                                         /*
3271                                                          * It is restricted
3272                                                          * for some reason..
3273                                                          * probably not yet
3274                                                          * added.
3275                                                          */
3276                                                         continue;
3277                                                 }
3278                                         }
3279                                         if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3280                                             (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3281                                                 sctp_add_local_addr_restricted(stcb, tmp_sifa);
3282                                         }
3283                                 }
3284                         }
3285                 }
3286                 atomic_add_int(&sifa->refcount, 1);
3287         }
3288 #endif
3289         return (sifa);
3290 }
3291
3292 /* tcb may be NULL */
3293 struct sctp_ifa *
3294 sctp_source_address_selection(struct sctp_inpcb *inp,
3295     struct sctp_tcb *stcb,
3296     sctp_route_t *ro,
3297     struct sctp_nets *net,
3298     int non_asoc_addr_ok, uint32_t vrf_id)
3299 {
3300         struct sctp_ifa *answer;
3301         uint8_t dest_is_priv, dest_is_loop;
3302         sa_family_t fam;
3303 #ifdef INET
3304         struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3305 #endif
3306 #ifdef INET6
3307         struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3308 #endif
3309
3310         /**
3311          * Rules:
3312          * - Find the route if needed, cache if I can.
3313          * - Look at interface address in route, Is it in the bound list. If so we
3314          *   have the best source.
3315          * - If not we must rotate amongst the addresses.
3316          *
3317          * Caveats and issues
3318          *
3319          * Do we need to pay attention to scope. We can have a private address
3320          * or a global address we are sourcing or sending to. So if we draw
3321          * it out
3322          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3323          * For V4
3324          * ------------------------------------------
3325          *      source     *      dest  *  result
3326          * -----------------------------------------
3327          * <a>  Private    *    Global  *  NAT
3328          * -----------------------------------------
3329          * <b>  Private    *    Private *  No problem
3330          * -----------------------------------------
3331          * <c>  Global     *    Private *  Huh, How will this work?
3332          * -----------------------------------------
3333          * <d>  Global     *    Global  *  No Problem
3334          *------------------------------------------
3335          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3336          * For V6
3337          *------------------------------------------
3338          *      source     *      dest  *  result
3339          * -----------------------------------------
3340          * <a>  Linklocal  *    Global  *
3341          * -----------------------------------------
3342          * <b>  Linklocal  * Linklocal  *  No problem
3343          * -----------------------------------------
3344          * <c>  Global     * Linklocal  *  Huh, How will this work?
3345          * -----------------------------------------
3346          * <d>  Global     *    Global  *  No Problem
3347          *------------------------------------------
3348          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3349          *
3350          * And then we add to that what happens if there are multiple addresses
3351          * assigned to an interface. Remember the ifa on a ifn is a linked
3352          * list of addresses. So one interface can have more than one IP
3353          * address. What happens if we have both a private and a global
3354          * address? Do we then use context of destination to sort out which
3355          * one is best? And what about NAT's sending P->G may get you a NAT
3356          * translation, or should you select the G thats on the interface in
3357          * preference.
3358          *
3359          * Decisions:
3360          *
3361          * - count the number of addresses on the interface.
3362          * - if it is one, no problem except case <c>.
3363          *   For <a> we will assume a NAT out there.
3364          * - if there are more than one, then we need to worry about scope P
3365          *   or G. We should prefer G -> G and P -> P if possible.
3366          *   Then as a secondary fall back to mixed types G->P being a last
3367          *   ditch one.
3368          * - The above all works for bound all, but bound specific we need to
3369          *   use the same concept but instead only consider the bound
3370          *   addresses. If the bound set is NOT assigned to the interface then
3371          *   we must use rotation amongst the bound addresses..
3372          */
3373         if (ro->ro_nh == NULL) {
3374                 /*
3375                  * Need a route to cache.
3376                  */
3377                 SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
3378         }
3379         if (ro->ro_nh == NULL) {
3380                 return (NULL);
3381         }
3382         fam = ro->ro_dst.sa_family;
3383         dest_is_priv = dest_is_loop = 0;
3384         /* Setup our scopes for the destination */
3385         switch (fam) {
3386 #ifdef INET
3387         case AF_INET:
3388                 /* Scope based on outbound address */
3389                 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3390                         dest_is_loop = 1;
3391                         if (net != NULL) {
3392                                 /* mark it as local */
3393                                 net->addr_is_local = 1;
3394                         }
3395                 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3396                         dest_is_priv = 1;
3397                 }
3398                 break;
3399 #endif
3400 #ifdef INET6
3401         case AF_INET6:
3402                 /* Scope based on outbound address */
3403                 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3404                     SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3405                         /*
3406                          * If the address is a loopback address, which
3407                          * consists of "::1" OR "fe80::1%lo0", we are
3408                          * loopback scope. But we don't use dest_is_priv
3409                          * (link local addresses).
3410                          */
3411                         dest_is_loop = 1;
3412                         if (net != NULL) {
3413                                 /* mark it as local */
3414                                 net->addr_is_local = 1;
3415                         }
3416                 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3417                         dest_is_priv = 1;
3418                 }
3419                 break;
3420 #endif
3421         }
3422         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3423         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3424         SCTP_IPI_ADDR_RLOCK();
3425         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3426                 /*
3427                  * Bound all case
3428                  */
3429                 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3430                     dest_is_priv, dest_is_loop,
3431                     non_asoc_addr_ok, fam);
3432                 SCTP_IPI_ADDR_RUNLOCK();
3433                 return (answer);
3434         }
3435         /*
3436          * Subset bound case
3437          */
3438         if (stcb) {
3439                 answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
3440                     vrf_id, dest_is_priv,
3441                     dest_is_loop,
3442                     non_asoc_addr_ok, fam);
3443         } else {
3444                 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3445                     non_asoc_addr_ok,
3446                     dest_is_priv,
3447                     dest_is_loop, fam);
3448         }
3449         SCTP_IPI_ADDR_RUNLOCK();
3450         return (answer);
3451 }
3452
3453 static bool
3454 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
3455 {
3456         struct cmsghdr cmh;
3457         struct sctp_sndinfo sndinfo;
3458         struct sctp_prinfo prinfo;
3459         struct sctp_authinfo authinfo;
3460         int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3461         bool found;
3462
3463         /*
3464          * Independent of how many mbufs, find the c_type inside the control
3465          * structure and copy out the data.
3466          */
3467         found = false;
3468         tot_len = SCTP_BUF_LEN(control);
3469         for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3470                 rem_len = tot_len - off;
3471                 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3472                         /* There is not enough room for one more. */
3473                         return (found);
3474                 }
3475                 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3476                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3477                         /* We dont't have a complete CMSG header. */
3478                         return (found);
3479                 }
3480                 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3481                         /* We don't have the complete CMSG. */
3482                         return (found);
3483                 }
3484                 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3485                 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3486                 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3487                     ((c_type == cmh.cmsg_type) ||
3488                     ((c_type == SCTP_SNDRCV) &&
3489                     ((cmh.cmsg_type == SCTP_SNDINFO) ||
3490                     (cmh.cmsg_type == SCTP_PRINFO) ||
3491                     (cmh.cmsg_type == SCTP_AUTHINFO))))) {
3492                         if (c_type == cmh.cmsg_type) {
3493                                 if (cpsize > INT_MAX) {
3494                                         return (found);
3495                                 }
3496                                 if (cmsg_data_len < (int)cpsize) {
3497                                         return (found);
3498                                 }
3499                                 /* It is exactly what we want. Copy it out. */
3500                                 m_copydata(control, cmsg_data_off, (int)cpsize, (caddr_t)data);
3501                                 return (1);
3502                         } else {
3503                                 struct sctp_sndrcvinfo *sndrcvinfo;
3504
3505                                 sndrcvinfo = (struct sctp_sndrcvinfo *)data;
3506                                 if (!found) {
3507                                         if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
3508                                                 return (found);
3509                                         }
3510                                         memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
3511                                 }
3512                                 switch (cmh.cmsg_type) {
3513                                 case SCTP_SNDINFO:
3514                                         if (cmsg_data_len < (int)sizeof(struct sctp_sndinfo)) {
3515                                                 return (found);
3516                                         }
3517                                         m_copydata(control, cmsg_data_off, sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
3518                                         sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
3519                                         sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
3520                                         sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
3521                                         sndrcvinfo->sinfo_context = sndinfo.snd_context;
3522                                         sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
3523                                         break;
3524                                 case SCTP_PRINFO:
3525                                         if (cmsg_data_len < (int)sizeof(struct sctp_prinfo)) {
3526                                                 return (found);
3527                                         }
3528                                         m_copydata(control, cmsg_data_off, sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
3529                                         if (prinfo.pr_policy != SCTP_PR_SCTP_NONE) {
3530                                                 sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
3531                                         } else {
3532                                                 sndrcvinfo->sinfo_timetolive = 0;
3533                                         }
3534                                         sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
3535                                         break;
3536                                 case SCTP_AUTHINFO:
3537                                         if (cmsg_data_len < (int)sizeof(struct sctp_authinfo)) {
3538                                                 return (found);
3539                                         }
3540                                         m_copydata(control, cmsg_data_off, sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
3541                                         sndrcvinfo->sinfo_keynumber_valid = 1;
3542                                         sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber;
3543                                         break;
3544                                 default:
3545                                         return (found);
3546                                 }
3547                                 found = true;
3548                         }
3549                 }
3550         }
3551         return (found);
3552 }
3553
3554 static int
3555 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
3556 {
3557         struct cmsghdr cmh;
3558         struct sctp_initmsg initmsg;
3559 #ifdef INET
3560         struct sockaddr_in sin;
3561 #endif
3562 #ifdef INET6
3563         struct sockaddr_in6 sin6;
3564 #endif
3565         int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3566
3567         tot_len = SCTP_BUF_LEN(control);
3568         for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3569                 rem_len = tot_len - off;
3570                 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3571                         /* There is not enough room for one more. */
3572                         *error = EINVAL;
3573                         return (1);
3574                 }
3575                 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3576                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3577                         /* We dont't have a complete CMSG header. */
3578                         *error = EINVAL;
3579                         return (1);
3580                 }
3581                 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3582                         /* We don't have the complete CMSG. */
3583                         *error = EINVAL;
3584                         return (1);
3585                 }
3586                 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3587                 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3588                 if (cmh.cmsg_level == IPPROTO_SCTP) {
3589                         switch (cmh.cmsg_type) {
3590                         case SCTP_INIT:
3591                                 if (cmsg_data_len < (int)sizeof(struct sctp_initmsg)) {
3592                                         *error = EINVAL;
3593                                         return (1);
3594                                 }
3595                                 m_copydata(control, cmsg_data_off, sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
3596                                 if (initmsg.sinit_max_attempts)
3597                                         stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
3598                                 if (initmsg.sinit_num_ostreams)
3599                                         stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
3600                                 if (initmsg.sinit_max_instreams)
3601                                         stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
3602                                 if (initmsg.sinit_max_init_timeo)
3603                                         stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
3604                                 if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
3605                                         struct sctp_stream_out *tmp_str;
3606                                         unsigned int i;
3607 #if defined(SCTP_DETAILED_STR_STATS)
3608                                         int j;
3609 #endif
3610
3611                                         /* Default is NOT correct */
3612                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
3613                                             stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
3614                                         SCTP_TCB_UNLOCK(stcb);
3615                                         SCTP_MALLOC(tmp_str,
3616                                             struct sctp_stream_out *,
3617                                             (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
3618                                             SCTP_M_STRMO);
3619                                         SCTP_TCB_LOCK(stcb);
3620                                         if (tmp_str != NULL) {
3621                                                 SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
3622                                                 stcb->asoc.strmout = tmp_str;
3623                                                 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
3624                                         } else {
3625                                                 stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
3626                                         }
3627                                         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3628                                                 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3629                                                 stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
3630                                                 stcb->asoc.strmout[i].chunks_on_queues = 0;
3631 #if defined(SCTP_DETAILED_STR_STATS)
3632                                                 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
3633                                                         stcb->asoc.strmout[i].abandoned_sent[j] = 0;
3634                                                         stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
3635                                                 }
3636 #else
3637                                                 stcb->asoc.strmout[i].abandoned_sent[0] = 0;
3638                                                 stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
3639 #endif
3640                                                 stcb->asoc.strmout[i].next_mid_ordered = 0;
3641                                                 stcb->asoc.strmout[i].next_mid_unordered = 0;
3642                                                 stcb->asoc.strmout[i].sid = i;
3643                                                 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3644                                                 stcb->asoc.strmout[i].state = SCTP_STREAM_OPENING;
3645                                         }
3646                                 }
3647                                 break;
3648 #ifdef INET
3649                         case SCTP_DSTADDRV4:
3650                                 if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3651                                         *error = EINVAL;
3652                                         return (1);
3653                                 }
3654                                 memset(&sin, 0, sizeof(struct sockaddr_in));
3655                                 sin.sin_family = AF_INET;
3656                                 sin.sin_len = sizeof(struct sockaddr_in);
3657                                 sin.sin_port = stcb->rport;
3658                                 m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3659                                 if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3660                                     (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3661                                     IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3662                                         *error = EINVAL;
3663                                         return (1);
3664                                 }
3665                                 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3666                                     SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3667                                         *error = ENOBUFS;
3668                                         return (1);
3669                                 }
3670                                 break;
3671 #endif
3672 #ifdef INET6
3673                         case SCTP_DSTADDRV6:
3674                                 if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3675                                         *error = EINVAL;
3676                                         return (1);
3677                                 }
3678                                 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3679                                 sin6.sin6_family = AF_INET6;
3680                                 sin6.sin6_len = sizeof(struct sockaddr_in6);
3681                                 sin6.sin6_port = stcb->rport;
3682                                 m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3683                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
3684                                     IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
3685                                         *error = EINVAL;
3686                                         return (1);
3687                                 }
3688 #ifdef INET
3689                                 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3690                                         in6_sin6_2_sin(&sin, &sin6);
3691                                         if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3692                                             (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3693                                             IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3694                                                 *error = EINVAL;
3695                                                 return (1);
3696                                         }
3697                                         if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3698                                             SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3699                                                 *error = ENOBUFS;
3700                                                 return (1);
3701                                         }
3702                                 } else
3703 #endif
3704                                         if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, stcb->asoc.port,
3705                                     SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3706                                         *error = ENOBUFS;
3707                                         return (1);
3708                                 }
3709                                 break;
3710 #endif
3711                         default:
3712                                 break;
3713                         }
3714                 }
3715         }
3716         return (0);
3717 }
3718
3719 #if defined(INET) || defined(INET6)
3720 static struct sctp_tcb *
3721 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
3722     uint16_t port,
3723     struct mbuf *control,
3724     struct sctp_nets **net_p,
3725     int *error)
3726 {
3727         struct cmsghdr cmh;
3728         struct sctp_tcb *stcb;
3729         struct sockaddr *addr;
3730 #ifdef INET
3731         struct sockaddr_in sin;
3732 #endif
3733 #ifdef INET6
3734         struct sockaddr_in6 sin6;
3735 #endif
3736         int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3737
3738         tot_len = SCTP_BUF_LEN(control);
3739         for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3740                 rem_len = tot_len - off;
3741                 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3742                         /* There is not enough room for one more. */
3743                         *error = EINVAL;
3744                         return (NULL);
3745                 }
3746                 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3747                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3748                         /* We dont't have a complete CMSG header. */
3749                         *error = EINVAL;
3750                         return (NULL);
3751                 }
3752                 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3753                         /* We don't have the complete CMSG. */
3754                         *error = EINVAL;
3755                         return (NULL);
3756                 }
3757                 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3758                 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3759                 if (cmh.cmsg_level == IPPROTO_SCTP) {
3760                         switch (cmh.cmsg_type) {
3761 #ifdef INET
3762                         case SCTP_DSTADDRV4:
3763                                 if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3764                                         *error = EINVAL;
3765                                         return (NULL);
3766                                 }
3767                                 memset(&sin, 0, sizeof(struct sockaddr_in));
3768                                 sin.sin_family = AF_INET;
3769                                 sin.sin_len = sizeof(struct sockaddr_in);
3770                                 sin.sin_port = port;
3771                                 m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3772                                 addr = (struct sockaddr *)&sin;
3773                                 break;
3774 #endif
3775 #ifdef INET6
3776                         case SCTP_DSTADDRV6:
3777                                 if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3778                                         *error = EINVAL;
3779                                         return (NULL);
3780                                 }
3781                                 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3782                                 sin6.sin6_family = AF_INET6;
3783                                 sin6.sin6_len = sizeof(struct sockaddr_in6);
3784                                 sin6.sin6_port = port;
3785                                 m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3786 #ifdef INET
3787                                 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3788                                         in6_sin6_2_sin(&sin, &sin6);
3789                                         addr = (struct sockaddr *)&sin;
3790                                 } else
3791 #endif
3792                                         addr = (struct sockaddr *)&sin6;
3793                                 break;
3794 #endif
3795                         default:
3796                                 addr = NULL;
3797                                 break;
3798                         }
3799                         if (addr) {
3800                                 stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
3801                                 if (stcb != NULL) {
3802                                         return (stcb);
3803                                 }
3804                         }
3805                 }
3806         }
3807         return (NULL);
3808 }
3809 #endif
3810
3811 static struct mbuf *
3812 sctp_add_cookie(struct mbuf *init, int init_offset,
3813     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t **signature)
3814 {
3815         struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3816         struct sctp_state_cookie *stc;
3817         struct sctp_paramhdr *ph;
3818         uint16_t cookie_sz;
3819
3820         mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3821             sizeof(struct sctp_paramhdr)), 0,
3822             M_NOWAIT, 1, MT_DATA);
3823         if (mret == NULL) {
3824                 return (NULL);
3825         }
3826         copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_NOWAIT);
3827         if (copy_init == NULL) {
3828                 sctp_m_freem(mret);
3829                 return (NULL);
3830         }
3831 #ifdef SCTP_MBUF_LOGGING
3832         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3833                 sctp_log_mbc(copy_init, SCTP_MBUF_ICOPY);
3834         }
3835 #endif
3836         copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3837             M_NOWAIT);
3838         if (copy_initack == NULL) {
3839                 sctp_m_freem(mret);
3840                 sctp_m_freem(copy_init);
3841                 return (NULL);
3842         }
3843 #ifdef SCTP_MBUF_LOGGING
3844         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3845                 sctp_log_mbc(copy_initack, SCTP_MBUF_ICOPY);
3846         }
3847 #endif
3848         /* easy side we just drop it on the end */
3849         ph = mtod(mret, struct sctp_paramhdr *);
3850         SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3851             sizeof(struct sctp_paramhdr);
3852         stc = (struct sctp_state_cookie *)((caddr_t)ph +
3853             sizeof(struct sctp_paramhdr));
3854         ph->param_type = htons(SCTP_STATE_COOKIE);
3855         ph->param_length = 0;   /* fill in at the end */
3856         /* Fill in the stc cookie data */
3857         memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3858
3859         /* tack the INIT and then the INIT-ACK onto the chain */
3860         cookie_sz = 0;
3861         for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3862                 cookie_sz += SCTP_BUF_LEN(m_at);
3863                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3864                         SCTP_BUF_NEXT(m_at) = copy_init;
3865                         break;
3866                 }
3867         }
3868         for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3869                 cookie_sz += SCTP_BUF_LEN(m_at);
3870                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3871                         SCTP_BUF_NEXT(m_at) = copy_initack;
3872                         break;
3873                 }
3874         }
3875         for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3876                 cookie_sz += SCTP_BUF_LEN(m_at);
3877                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3878                         break;
3879                 }
3880         }
3881         sig = sctp_get_mbuf_for_msg(SCTP_SIGNATURE_SIZE, 0, M_NOWAIT, 1, MT_DATA);
3882         if (sig == NULL) {
3883                 /* no space, so free the entire chain */
3884                 sctp_m_freem(mret);
3885                 return (NULL);
3886         }
3887         SCTP_BUF_NEXT(m_at) = sig;
3888         SCTP_BUF_LEN(sig) = SCTP_SIGNATURE_SIZE;
3889         cookie_sz += SCTP_SIGNATURE_SIZE;
3890         ph->param_length = htons(cookie_sz);
3891         *signature = (uint8_t *)mtod(sig, caddr_t);
3892         memset(*signature, 0, SCTP_SIGNATURE_SIZE);
3893         return (mret);
3894 }
3895
3896 static uint8_t
3897 sctp_get_ect(struct sctp_tcb *stcb)
3898 {
3899         if ((stcb != NULL) && (stcb->asoc.ecn_supported == 1)) {
3900                 return (SCTP_ECT0_BIT);
3901         } else {
3902                 return (0);
3903         }
3904 }
3905
3906 #if defined(INET) || defined(INET6)
3907 static void
3908 sctp_handle_no_route(struct sctp_tcb *stcb,
3909     struct sctp_nets *net,
3910     int so_locked)
3911 {
3912         SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
3913
3914         if (net) {
3915                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
3916                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
3917                 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3918                         if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3919                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", (void *)net);
3920                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3921                                     stcb, 0,
3922                                     (void *)net,
3923                                     so_locked);
3924                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3925                                 net->dest_state &= ~SCTP_ADDR_PF;
3926                         }
3927                 }
3928                 if (stcb) {
3929                         if (net == stcb->asoc.primary_destination) {
3930                                 /* need a new primary */
3931                                 struct sctp_nets *alt;
3932
3933                                 alt = sctp_find_alternate_net(stcb, net, 0);
3934                                 if (alt != net) {
3935                                         if (stcb->asoc.alternate) {
3936                                                 sctp_free_remote_addr(stcb->asoc.alternate);
3937                                         }
3938                                         stcb->asoc.alternate = alt;
3939                                         atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
3940                                         if (net->ro._s_addr) {
3941                                                 sctp_free_ifa(net->ro._s_addr);
3942                                                 net->ro._s_addr = NULL;
3943                                         }
3944                                         net->src_addr_selected = 0;
3945                                 }
3946                         }
3947                 }
3948         }
3949 }
3950 #endif
3951
3952 static int
3953 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3954     struct sctp_tcb *stcb,      /* may be NULL */
3955     struct sctp_nets *net,
3956     struct sockaddr *to,
3957     struct mbuf *m,
3958     uint32_t auth_offset,
3959     struct sctp_auth_chunk *auth,
3960     uint16_t auth_keyid,
3961     int nofragment_flag,
3962     int ecn_ok,
3963     int out_of_asoc_ok,
3964     uint16_t src_port,
3965     uint16_t dest_port,
3966     uint32_t v_tag,
3967     uint16_t port,
3968     union sctp_sockstore *over_addr,
3969     uint8_t mflowtype, uint32_t mflowid,
3970     int so_locked)
3971 {
3972 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3973         /**
3974          * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
3975          * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
3976          * - fill in the HMAC digest of any AUTH chunk in the packet.
3977          * - calculate and fill in the SCTP checksum.
3978          * - prepend an IP address header.
3979          * - if boundall use INADDR_ANY.
3980          * - if boundspecific do source address selection.
3981          * - set fragmentation option for ipV4.
3982          * - On return from IP output, check/adjust mtu size of output
3983          *   interface and smallest_mtu size as well.
3984          */
3985         /* Will need ifdefs around this */
3986         struct mbuf *newm;
3987         struct sctphdr *sctphdr;
3988         int packet_length;
3989         int ret;
3990 #if defined(INET) || defined(INET6)
3991         uint32_t vrf_id;
3992 #endif
3993 #if defined(INET) || defined(INET6)
3994         struct mbuf *o_pak;
3995         sctp_route_t *ro = NULL;
3996         struct udphdr *udp = NULL;
3997 #endif
3998         uint8_t tos_value;
3999
4000         if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
4001                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4002                 sctp_m_freem(m);
4003                 return (EFAULT);
4004         }
4005 #if defined(INET) || defined(INET6)
4006         if (stcb) {
4007                 vrf_id = stcb->asoc.vrf_id;
4008         } else {
4009                 vrf_id = inp->def_vrf_id;
4010         }
4011 #endif
4012         /* fill in the HMAC digest for any AUTH chunk in the packet */
4013         if ((auth != NULL) && (stcb != NULL)) {
4014                 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
4015         }
4016
4017         if (net) {
4018                 tos_value = net->dscp;
4019         } else if (stcb) {
4020                 tos_value = stcb->asoc.default_dscp;
4021         } else {
4022                 tos_value = inp->sctp_ep.default_dscp;
4023         }
4024
4025         switch (to->sa_family) {
4026 #ifdef INET
4027         case AF_INET:
4028                 {
4029                         struct ip *ip = NULL;
4030                         sctp_route_t iproute;
4031                         int len;
4032
4033                         len = SCTP_MIN_V4_OVERHEAD;
4034                         if (port) {
4035                                 len += sizeof(struct udphdr);
4036                         }
4037                         newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4038                         if (newm == NULL) {
4039                                 sctp_m_freem(m);
4040                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4041                                 return (ENOMEM);
4042                         }
4043                         SCTP_ALIGN_TO_END(newm, len);
4044                         SCTP_BUF_LEN(newm) = len;
4045                         SCTP_BUF_NEXT(newm) = m;
4046                         m = newm;
4047                         if (net != NULL) {
4048                                 m->m_pkthdr.flowid = net->flowid;
4049                                 M_HASHTYPE_SET(m, net->flowtype);
4050                         } else {
4051                                 m->m_pkthdr.flowid = mflowid;
4052                                 M_HASHTYPE_SET(m, mflowtype);
4053                         }
4054                         packet_length = sctp_calculate_len(m);
4055                         ip = mtod(m, struct ip *);
4056                         ip->ip_v = IPVERSION;
4057                         ip->ip_hl = (sizeof(struct ip) >> 2);
4058                         if (tos_value == 0) {
4059                                 /*
4060                                  * This means especially, that it is not set
4061                                  * at the SCTP layer. So use the value from
4062                                  * the IP layer.
4063                                  */
4064                                 tos_value = inp->ip_inp.inp.inp_ip_tos;
4065                         }
4066                         tos_value &= 0xfc;
4067                         if (ecn_ok) {
4068                                 tos_value |= sctp_get_ect(stcb);
4069                         }
4070                         if ((nofragment_flag) && (port == 0)) {
4071                                 ip->ip_off = htons(IP_DF);
4072                         } else {
4073                                 ip->ip_off = htons(0);
4074                         }
4075                         /* FreeBSD has a function for ip_id's */
4076                         ip_fillid(ip);
4077
4078                         ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
4079                         ip->ip_len = htons(packet_length);
4080                         ip->ip_tos = tos_value;
4081                         if (port) {
4082                                 ip->ip_p = IPPROTO_UDP;
4083                         } else {
4084                                 ip->ip_p = IPPROTO_SCTP;
4085                         }
4086                         ip->ip_sum = 0;
4087                         if (net == NULL) {
4088                                 ro = &iproute;
4089                                 memset(&iproute, 0, sizeof(iproute));
4090                                 memcpy(&ro->ro_dst, to, to->sa_len);
4091                         } else {
4092                                 ro = (sctp_route_t *)&net->ro;
4093                         }
4094                         /* Now the address selection part */
4095                         ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
4096
4097                         /* call the routine to select the src address */
4098                         if (net && out_of_asoc_ok == 0) {
4099                                 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4100                                         sctp_free_ifa(net->ro._s_addr);
4101                                         net->ro._s_addr = NULL;
4102                                         net->src_addr_selected = 0;
4103                                         RO_NHFREE(ro);
4104                                 }
4105                                 if (net->src_addr_selected == 0) {
4106                                         /* Cache the source address */
4107                                         net->ro._s_addr = sctp_source_address_selection(inp, stcb,
4108                                             ro, net, 0,
4109                                             vrf_id);
4110                                         net->src_addr_selected = 1;
4111                                 }
4112                                 if (net->ro._s_addr == NULL) {
4113                                         /* No route to host */
4114                                         net->src_addr_selected = 0;
4115                                         sctp_handle_no_route(stcb, net, so_locked);
4116                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4117                                         sctp_m_freem(m);
4118                                         return (EHOSTUNREACH);
4119                                 }
4120                                 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
4121                         } else {
4122                                 if (over_addr == NULL) {
4123                                         struct sctp_ifa *_lsrc;
4124
4125                                         _lsrc = sctp_source_address_selection(inp, stcb, ro,
4126                                             net,
4127                                             out_of_asoc_ok,
4128                                             vrf_id);
4129                                         if (_lsrc == NULL) {
4130                                                 sctp_handle_no_route(stcb, net, so_locked);
4131                                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4132                                                 sctp_m_freem(m);
4133                                                 return (EHOSTUNREACH);
4134                                         }
4135                                         ip->ip_src = _lsrc->address.sin.sin_addr;
4136                                         sctp_free_ifa(_lsrc);
4137                                 } else {
4138                                         ip->ip_src = over_addr->sin.sin_addr;
4139                                         SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4140                                 }
4141                         }
4142                         if (port) {
4143                                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4144                                         sctp_handle_no_route(stcb, net, so_locked);
4145                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4146                                         sctp_m_freem(m);
4147                                         return (EHOSTUNREACH);
4148                                 }
4149                                 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
4150                                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4151                                 udp->uh_dport = port;
4152                                 udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip)));
4153                                 if (V_udp_cksum) {
4154                                         udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
4155                                 } else {
4156                                         udp->uh_sum = 0;
4157                                 }
4158                                 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4159                         } else {
4160                                 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
4161                         }
4162
4163                         sctphdr->src_port = src_port;
4164                         sctphdr->dest_port = dest_port;
4165                         sctphdr->v_tag = v_tag;
4166                         sctphdr->checksum = 0;
4167
4168                         /*
4169                          * If source address selection fails and we find no
4170                          * route then the ip_output should fail as well with
4171                          * a NO_ROUTE_TO_HOST type error. We probably should
4172                          * catch that somewhere and abort the association
4173                          * right away (assuming this is an INIT being sent).
4174                          */
4175                         if (ro->ro_nh == NULL) {
4176                                 /*
4177                                  * src addr selection failed to find a route
4178                                  * (or valid source addr), so we can't get
4179                                  * there from here (yet)!
4180                                  */
4181                                 sctp_handle_no_route(stcb, net, so_locked);
4182                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4183                                 sctp_m_freem(m);
4184                                 return (EHOSTUNREACH);
4185                         }
4186                         if (ro != &iproute) {
4187                                 memcpy(&iproute, ro, sizeof(*ro));
4188                         }
4189                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
4190                             (uint32_t)(ntohl(ip->ip_src.s_addr)));
4191                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
4192                             (uint32_t)(ntohl(ip->ip_dst.s_addr)));
4193                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
4194                             (void *)ro->ro_nh);
4195
4196                         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4197                                 /* failed to prepend data, give up */
4198                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4199                                 sctp_m_freem(m);
4200                                 return (ENOMEM);
4201                         }
4202                         SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4203                         if (port) {
4204                                 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
4205                                 SCTP_STAT_INCR(sctps_sendswcrc);
4206                                 if (V_udp_cksum) {
4207                                         SCTP_ENABLE_UDP_CSUM(o_pak);
4208                                 }
4209                         } else {
4210                                 m->m_pkthdr.csum_flags = CSUM_SCTP;
4211                                 m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4212                                 SCTP_STAT_INCR(sctps_sendhwcrc);
4213                         }
4214 #ifdef SCTP_PACKET_LOGGING
4215                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4216                                 sctp_packet_log(o_pak);
4217 #endif
4218                         /* send it out.  table id is taken from stcb */
4219                         SCTP_PROBE5(send, NULL, stcb, ip, stcb, sctphdr);
4220                         SCTP_IP_OUTPUT(ret, o_pak, ro, inp, vrf_id);
4221                         if (port) {
4222                                 UDPSTAT_INC(udps_opackets);
4223                         }
4224                         SCTP_STAT_INCR(sctps_sendpackets);
4225                         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4226                         if (ret)
4227                                 SCTP_STAT_INCR(sctps_senderrors);
4228
4229                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
4230                         if (net == NULL) {
4231                                 /* free tempy routes */
4232                                 RO_NHFREE(ro);
4233                         } else {
4234                                 if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4235                                     ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4236                                         uint32_t mtu;
4237
4238                                         mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4239                                         if (mtu > 0) {
4240                                                 if (net->port) {
4241                                                         mtu -= sizeof(struct udphdr);
4242                                                 }
4243                                                 if (mtu < net->mtu) {
4244                                                         net->mtu = mtu;
4245                                                         if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4246                                                                 sctp_pathmtu_adjustment(stcb, mtu, true);
4247                                                         }
4248                                                 }
4249                                         }
4250                                 } else if (ro->ro_nh == NULL) {
4251                                         /* route was freed */
4252                                         if (net->ro._s_addr &&
4253                                             net->src_addr_selected) {
4254                                                 sctp_free_ifa(net->ro._s_addr);
4255                                                 net->ro._s_addr = NULL;
4256                                         }
4257                                         net->src_addr_selected = 0;
4258                                 }
4259                         }
4260                         return (ret);
4261                 }
4262 #endif
4263 #ifdef INET6
4264         case AF_INET6:
4265                 {
4266                         uint32_t flowlabel, flowinfo;
4267                         struct ip6_hdr *ip6h;
4268                         struct route_in6 ip6route;
4269                         struct ifnet *ifp;
4270                         struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
4271                         int prev_scope = 0;
4272                         struct sockaddr_in6 lsa6_storage;
4273                         int error;
4274                         u_short prev_port = 0;
4275                         int len;
4276
4277                         if (net) {
4278                                 flowlabel = net->flowlabel;
4279                         } else if (stcb) {
4280                                 flowlabel = stcb->asoc.default_flowlabel;
4281                         } else {
4282                                 flowlabel = inp->sctp_ep.default_flowlabel;
4283                         }
4284                         if (flowlabel == 0) {
4285                                 /*
4286                                  * This means especially, that it is not set
4287                                  * at the SCTP layer. So use the value from
4288                                  * the IP layer.
4289                                  */
4290                                 flowlabel = ntohl(((struct inpcb *)inp)->inp_flow);
4291                         }
4292                         flowlabel &= 0x000fffff;
4293                         len = SCTP_MIN_OVERHEAD;
4294                         if (port) {
4295                                 len += sizeof(struct udphdr);
4296                         }
4297                         newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4298                         if (newm == NULL) {
4299                                 sctp_m_freem(m);
4300                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4301                                 return (ENOMEM);
4302                         }
4303                         SCTP_ALIGN_TO_END(newm, len);
4304                         SCTP_BUF_LEN(newm) = len;
4305                         SCTP_BUF_NEXT(newm) = m;
4306                         m = newm;
4307                         if (net != NULL) {
4308                                 m->m_pkthdr.flowid = net->flowid;
4309                                 M_HASHTYPE_SET(m, net->flowtype);
4310                         } else {
4311                                 m->m_pkthdr.flowid = mflowid;
4312                                 M_HASHTYPE_SET(m, mflowtype);
4313                         }
4314                         packet_length = sctp_calculate_len(m);
4315
4316                         ip6h = mtod(m, struct ip6_hdr *);
4317                         /* protect *sin6 from overwrite */
4318                         sin6 = (struct sockaddr_in6 *)to;
4319                         tmp = *sin6;
4320                         sin6 = &tmp;
4321
4322                         /* KAME hack: embed scopeid */
4323                         if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4324                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4325                                 sctp_m_freem(m);
4326                                 return (EINVAL);
4327                         }
4328                         if (net == NULL) {
4329                                 memset(&ip6route, 0, sizeof(ip6route));
4330                                 ro = (sctp_route_t *)&ip6route;
4331                                 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
4332                         } else {
4333                                 ro = (sctp_route_t *)&net->ro;
4334                         }
4335                         /*
4336                          * We assume here that inp_flow is in host byte
4337                          * order within the TCB!
4338                          */
4339                         if (tos_value == 0) {
4340                                 /*
4341                                  * This means especially, that it is not set
4342                                  * at the SCTP layer. So use the value from
4343                                  * the IP layer.
4344                                  */
4345                                 tos_value = (ntohl(((struct inpcb *)inp)->inp_flow) >> 20) & 0xff;
4346                         }
4347                         tos_value &= 0xfc;
4348                         if (ecn_ok) {
4349                                 tos_value |= sctp_get_ect(stcb);
4350                         }
4351                         flowinfo = 0x06;
4352                         flowinfo <<= 8;
4353                         flowinfo |= tos_value;
4354                         flowinfo <<= 20;
4355                         flowinfo |= flowlabel;
4356                         ip6h->ip6_flow = htonl(flowinfo);
4357                         if (port) {
4358                                 ip6h->ip6_nxt = IPPROTO_UDP;
4359                         } else {
4360                                 ip6h->ip6_nxt = IPPROTO_SCTP;
4361                         }
4362                         ip6h->ip6_plen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4363                         ip6h->ip6_dst = sin6->sin6_addr;
4364
4365                         /*
4366                          * Add SRC address selection here: we can only reuse
4367                          * to a limited degree the kame src-addr-sel, since
4368                          * we can try their selection but it may not be
4369                          * bound.
4370                          */
4371                         memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
4372                         lsa6_tmp.sin6_family = AF_INET6;
4373                         lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
4374                         lsa6 = &lsa6_tmp;
4375                         if (net && out_of_asoc_ok == 0) {
4376                                 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4377                                         sctp_free_ifa(net->ro._s_addr);
4378                                         net->ro._s_addr = NULL;
4379                                         net->src_addr_selected = 0;
4380                                         RO_NHFREE(ro);
4381                                 }
4382                                 if (net->src_addr_selected == 0) {
4383                                         sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4384                                         /* KAME hack: embed scopeid */
4385                                         if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4386                                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4387                                                 sctp_m_freem(m);
4388                                                 return (EINVAL);
4389                                         }
4390                                         /* Cache the source address */
4391                                         net->ro._s_addr = sctp_source_address_selection(inp,
4392                                             stcb,
4393                                             ro,
4394                                             net,
4395                                             0,
4396                                             vrf_id);
4397                                         (void)sa6_recoverscope(sin6);
4398                                         net->src_addr_selected = 1;
4399                                 }
4400                                 if (net->ro._s_addr == NULL) {
4401                                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
4402                                         net->src_addr_selected = 0;
4403                                         sctp_handle_no_route(stcb, net, so_locked);
4404                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4405                                         sctp_m_freem(m);
4406                                         return (EHOSTUNREACH);
4407                                 }
4408                                 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
4409                         } else {
4410                                 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
4411                                 /* KAME hack: embed scopeid */
4412                                 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4413                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4414                                         sctp_m_freem(m);
4415                                         return (EINVAL);
4416                                 }
4417                                 if (over_addr == NULL) {
4418                                         struct sctp_ifa *_lsrc;
4419
4420                                         _lsrc = sctp_source_address_selection(inp, stcb, ro,
4421                                             net,
4422                                             out_of_asoc_ok,
4423                                             vrf_id);
4424                                         if (_lsrc == NULL) {
4425                                                 sctp_handle_no_route(stcb, net, so_locked);
4426                                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4427                                                 sctp_m_freem(m);
4428                                                 return (EHOSTUNREACH);
4429                                         }
4430                                         lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
4431                                         sctp_free_ifa(_lsrc);
4432                                 } else {
4433                                         lsa6->sin6_addr = over_addr->sin6.sin6_addr;
4434                                         SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4435                                 }
4436                                 (void)sa6_recoverscope(sin6);
4437                         }
4438                         lsa6->sin6_port = inp->sctp_lport;
4439
4440                         if (ro->ro_nh == NULL) {
4441                                 /*
4442                                  * src addr selection failed to find a route
4443                                  * (or valid source addr), so we can't get
4444                                  * there from here!
4445                                  */
4446                                 sctp_handle_no_route(stcb, net, so_locked);
4447                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4448                                 sctp_m_freem(m);
4449                                 return (EHOSTUNREACH);
4450                         }
4451                         /*
4452                          * XXX: sa6 may not have a valid sin6_scope_id in
4453                          * the non-SCOPEDROUTING case.
4454                          */
4455                         memset(&lsa6_storage, 0, sizeof(lsa6_storage));
4456                         lsa6_storage.sin6_family = AF_INET6;
4457                         lsa6_storage.sin6_len = sizeof(lsa6_storage);
4458                         lsa6_storage.sin6_addr = lsa6->sin6_addr;
4459                         if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
4460                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
4461                                 sctp_m_freem(m);
4462                                 return (error);
4463                         }
4464                         /* XXX */
4465                         lsa6_storage.sin6_addr = lsa6->sin6_addr;
4466                         lsa6_storage.sin6_port = inp->sctp_lport;
4467                         lsa6 = &lsa6_storage;
4468                         ip6h->ip6_src = lsa6->sin6_addr;
4469
4470                         if (port) {
4471                                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4472                                         sctp_handle_no_route(stcb, net, so_locked);
4473                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4474                                         sctp_m_freem(m);
4475                                         return (EHOSTUNREACH);
4476                                 }
4477                                 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4478                                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4479                                 udp->uh_dport = port;
4480                                 udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4481                                 udp->uh_sum = 0;
4482                                 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4483                         } else {
4484                                 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4485                         }
4486
4487                         sctphdr->src_port = src_port;
4488                         sctphdr->dest_port = dest_port;
4489                         sctphdr->v_tag = v_tag;
4490                         sctphdr->checksum = 0;
4491
4492                         /*
4493                          * We set the hop limit now since there is a good
4494                          * chance that our ro pointer is now filled
4495                          */
4496                         ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4497                         ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4498
4499 #ifdef SCTP_DEBUG
4500                         /* Copy to be sure something bad is not happening */
4501                         sin6->sin6_addr = ip6h->ip6_dst;
4502                         lsa6->sin6_addr = ip6h->ip6_src;
4503 #endif
4504
4505                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4506                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4507                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4508                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4509                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4510                         if (net) {
4511                                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4512                                 /*
4513                                  * preserve the port and scope for link
4514                                  * local send
4515                                  */
4516                                 prev_scope = sin6->sin6_scope_id;
4517                                 prev_port = sin6->sin6_port;
4518                         }
4519
4520                         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4521                                 /* failed to prepend data, give up */
4522                                 sctp_m_freem(m);
4523                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4524                                 return (ENOMEM);
4525                         }
4526                         SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4527                         if (port) {
4528                                 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4529                                 SCTP_STAT_INCR(sctps_sendswcrc);
4530                                 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4531                                         udp->uh_sum = 0xffff;
4532                                 }
4533                         } else {
4534                                 m->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
4535                                 m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4536                                 SCTP_STAT_INCR(sctps_sendhwcrc);
4537                         }
4538                         /* send it out. table id is taken from stcb */
4539 #ifdef SCTP_PACKET_LOGGING
4540                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4541                                 sctp_packet_log(o_pak);
4542 #endif
4543                         SCTP_PROBE5(send, NULL, stcb, ip6h, stcb, sctphdr);
4544                         SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, inp, vrf_id);
4545                         if (net) {
4546                                 /* for link local this must be done */
4547                                 sin6->sin6_scope_id = prev_scope;
4548                                 sin6->sin6_port = prev_port;
4549                         }
4550                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4551                         if (port) {
4552                                 UDPSTAT_INC(udps_opackets);
4553                         }
4554                         SCTP_STAT_INCR(sctps_sendpackets);
4555                         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4556                         if (ret) {
4557                                 SCTP_STAT_INCR(sctps_senderrors);
4558                         }
4559                         if (net == NULL) {
4560                                 /* Now if we had a temp route free it */
4561                                 RO_NHFREE(ro);
4562                         } else {
4563                                 /*
4564                                  * PMTU check versus smallest asoc MTU goes
4565                                  * here
4566                                  */
4567                                 if (ro->ro_nh == NULL) {
4568                                         /* Route was freed */
4569                                         if (net->ro._s_addr &&
4570                                             net->src_addr_selected) {
4571                                                 sctp_free_ifa(net->ro._s_addr);
4572                                                 net->ro._s_addr = NULL;
4573                                         }
4574                                         net->src_addr_selected = 0;
4575                                 }
4576                                 if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4577                                     ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4578                                         uint32_t mtu;
4579
4580                                         mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4581                                         if (mtu > 0) {
4582                                                 if (net->port) {
4583                                                         mtu -= sizeof(struct udphdr);
4584                                                 }
4585                                                 if (mtu < net->mtu) {
4586                                                         net->mtu = mtu;
4587                                                         if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4588                                                                 sctp_pathmtu_adjustment(stcb, mtu, false);
4589                                                         }
4590                                                 }
4591                                         }
4592                                 } else if (ifp != NULL) {
4593                                         if ((ND_IFINFO(ifp)->linkmtu > 0) &&
4594                                             (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4595                                                 sctp_pathmtu_adjustment(stcb, ND_IFINFO(ifp)->linkmtu, false);
4596                                         }
4597                                 }
4598                         }
4599                         return (ret);
4600                 }
4601 #endif
4602         default:
4603                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4604                     ((struct sockaddr *)to)->sa_family);
4605                 sctp_m_freem(m);
4606                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4607                 return (EFAULT);
4608         }
4609 }
4610
4611 void
4612 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked)
4613 {
4614         struct mbuf *m, *m_last;
4615         struct sctp_nets *net;
4616         struct sctp_init_chunk *init;
4617         struct sctp_supported_addr_param *sup_addr;
4618         struct sctp_adaptation_layer_indication *ali;
4619         struct sctp_supported_chunk_types_param *pr_supported;
4620         struct sctp_paramhdr *ph;
4621         int cnt_inits_to = 0;
4622         int error;
4623         uint16_t num_ext, chunk_len, padding_len, parameter_len;
4624
4625         /* INIT's always go to the primary (and usually ONLY address) */
4626         net = stcb->asoc.primary_destination;
4627         if (net == NULL) {
4628                 net = TAILQ_FIRST(&stcb->asoc.nets);
4629                 if (net == NULL) {
4630                         /* TSNH */
4631                         return;
4632                 }
4633                 /* we confirm any address we send an INIT to */
4634                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4635                 (void)sctp_set_primary_addr(stcb, NULL, net);
4636         } else {
4637                 /* we confirm any address we send an INIT to */
4638                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4639         }
4640         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4641 #ifdef INET6
4642         if (net->ro._l_addr.sa.sa_family == AF_INET6) {
4643                 /*
4644                  * special hook, if we are sending to link local it will not
4645                  * show up in our private address count.
4646                  */
4647                 if (IN6_IS_ADDR_LINKLOCAL(&net->ro._l_addr.sin6.sin6_addr))
4648                         cnt_inits_to = 1;
4649         }
4650 #endif
4651         if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4652                 /* This case should not happen */
4653                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4654                 return;
4655         }
4656         /* start the INIT timer */
4657         sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4658
4659         m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_NOWAIT, 1, MT_DATA);
4660         if (m == NULL) {
4661                 /* No memory, INIT timer will re-attempt. */
4662                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4663                 return;
4664         }
4665         chunk_len = (uint16_t)sizeof(struct sctp_init_chunk);
4666         padding_len = 0;
4667         /* Now lets put the chunk header in place */
4668         init = mtod(m, struct sctp_init_chunk *);
4669         /* now the chunk header */
4670         init->ch.chunk_type = SCTP_INITIATION;
4671         init->ch.chunk_flags = 0;
4672         /* fill in later from mbuf we build */
4673         init->ch.chunk_length = 0;
4674         /* place in my tag */
4675         init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4676         /* set up some of the credits. */
4677         init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4678             SCTP_MINIMAL_RWND));
4679         init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4680         init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4681         init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4682
4683         /* Adaptation layer indication parameter */
4684         if (inp->sctp_ep.adaptation_layer_indicator_provided) {
4685                 parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
4686                 ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t)+chunk_len);
4687                 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4688                 ali->ph.param_length = htons(parameter_len);
4689                 ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
4690                 chunk_len += parameter_len;
4691         }
4692
4693         /* ECN parameter */
4694         if (stcb->asoc.ecn_supported == 1) {
4695                 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4696                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4697                 ph->param_type = htons(SCTP_ECN_CAPABLE);
4698                 ph->param_length = htons(parameter_len);
4699                 chunk_len += parameter_len;
4700         }
4701
4702         /* PR-SCTP supported parameter */
4703         if (stcb->asoc.prsctp_supported == 1) {
4704                 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4705                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4706                 ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
4707                 ph->param_length = htons(parameter_len);
4708                 chunk_len += parameter_len;
4709         }
4710
4711         /* Add NAT friendly parameter. */
4712         if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4713                 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4714                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4715                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4716                 ph->param_length = htons(parameter_len);
4717                 chunk_len += parameter_len;
4718         }
4719
4720         /* And now tell the peer which extensions we support */
4721         num_ext = 0;
4722         pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+chunk_len);
4723         if (stcb->asoc.prsctp_supported == 1) {
4724                 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4725                 if (stcb->asoc.idata_supported) {
4726                         pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
4727                 }
4728         }
4729         if (stcb->asoc.auth_supported == 1) {
4730                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4731         }
4732         if (stcb->asoc.asconf_supported == 1) {
4733                 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4734                 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4735         }
4736         if (stcb->asoc.reconfig_supported == 1) {
4737                 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4738         }
4739         if (stcb->asoc.idata_supported) {
4740                 pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
4741         }
4742         if (stcb->asoc.nrsack_supported == 1) {
4743                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4744         }
4745         if (stcb->asoc.pktdrop_supported == 1) {
4746                 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4747         }
4748         if (num_ext > 0) {
4749                 parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
4750                 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4751                 pr_supported->ph.param_length = htons(parameter_len);
4752                 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4753                 chunk_len += parameter_len;
4754         }
4755         /* add authentication parameters */
4756         if (stcb->asoc.auth_supported) {
4757                 /* attach RANDOM parameter, if available */
4758                 if (stcb->asoc.authinfo.random != NULL) {
4759                         struct sctp_auth_random *randp;
4760
4761                         if (padding_len > 0) {
4762                                 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4763                                 chunk_len += padding_len;
4764                                 padding_len = 0;
4765                         }
4766                         randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+chunk_len);
4767                         parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + stcb->asoc.authinfo.random_len;
4768                         /* random key already contains the header */
4769                         memcpy(randp, stcb->asoc.authinfo.random->key, parameter_len);
4770                         padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4771                         chunk_len += parameter_len;
4772                 }
4773                 /* add HMAC_ALGO parameter */
4774                 if (stcb->asoc.local_hmacs != NULL) {
4775                         struct sctp_auth_hmac_algo *hmacs;
4776
4777                         if (padding_len > 0) {
4778                                 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4779                                 chunk_len += padding_len;
4780                                 padding_len = 0;
4781                         }
4782                         hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+chunk_len);
4783                         parameter_len = (uint16_t)(sizeof(struct sctp_auth_hmac_algo) +
4784                             stcb->asoc.local_hmacs->num_algo * sizeof(uint16_t));
4785                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4786                         hmacs->ph.param_length = htons(parameter_len);
4787                         sctp_serialize_hmaclist(stcb->asoc.local_hmacs, (uint8_t *)hmacs->hmac_ids);
4788                         padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4789                         chunk_len += parameter_len;
4790                 }
4791                 /* add CHUNKS parameter */
4792                 if (stcb->asoc.local_auth_chunks != NULL) {
4793                         struct sctp_auth_chunk_list *chunks;
4794
4795                         if (padding_len > 0) {
4796                                 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4797                                 chunk_len += padding_len;
4798                                 padding_len = 0;
4799                         }
4800                         chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+chunk_len);
4801                         parameter_len = (uint16_t)(sizeof(struct sctp_auth_chunk_list) +
4802                             sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks));
4803                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4804                         chunks->ph.param_length = htons(parameter_len);
4805                         sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, chunks->chunk_types);
4806                         padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4807                         chunk_len += parameter_len;
4808                 }
4809         }
4810
4811         /* now any cookie time extensions */
4812         if (stcb->asoc.cookie_preserve_req > 0) {
4813                 struct sctp_cookie_perserve_param *cookie_preserve;
4814
4815                 if (padding_len > 0) {
4816                         memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4817                         chunk_len += padding_len;
4818                         padding_len = 0;
4819                 }
4820                 parameter_len = (uint16_t)sizeof(struct sctp_cookie_perserve_param);
4821                 cookie_preserve = (struct sctp_cookie_perserve_param *)(mtod(m, caddr_t)+chunk_len);
4822                 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4823                 cookie_preserve->ph.param_length = htons(parameter_len);
4824                 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4825                 stcb->asoc.cookie_preserve_req = 0;
4826                 chunk_len += parameter_len;
4827         }
4828
4829         if (stcb->asoc.scope.ipv4_addr_legal || stcb->asoc.scope.ipv6_addr_legal) {
4830                 uint8_t i;
4831
4832                 if (padding_len > 0) {
4833                         memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4834                         chunk_len += padding_len;
4835                         padding_len = 0;
4836                 }
4837                 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4838                 if (stcb->asoc.scope.ipv4_addr_legal) {
4839                         parameter_len += (uint16_t)sizeof(uint16_t);
4840                 }
4841                 if (stcb->asoc.scope.ipv6_addr_legal) {
4842                         parameter_len += (uint16_t)sizeof(uint16_t);
4843                 }
4844                 sup_addr = (struct sctp_supported_addr_param *)(mtod(m, caddr_t)+chunk_len);
4845                 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4846                 sup_addr->ph.param_length = htons(parameter_len);
4847                 i = 0;
4848                 if (stcb->asoc.scope.ipv4_addr_legal) {
4849                         sup_addr->addr_type[i++] = htons(SCTP_IPV4_ADDRESS);
4850                 }
4851                 if (stcb->asoc.scope.ipv6_addr_legal) {
4852                         sup_addr->addr_type[i++] = htons(SCTP_IPV6_ADDRESS);
4853                 }
4854                 padding_len = 4 - 2 * i;
4855                 chunk_len += parameter_len;
4856         }
4857
4858         SCTP_BUF_LEN(m) = chunk_len;
4859         /* now the addresses */
4860         /*
4861          * To optimize this we could put the scoping stuff into a structure
4862          * and remove the individual uint8's from the assoc structure. Then
4863          * we could just sifa in the address within the stcb. But for now
4864          * this is a quick hack to get the address stuff teased apart.
4865          */
4866         m_last = sctp_add_addresses_to_i_ia(inp, stcb, &stcb->asoc.scope,
4867             m, cnt_inits_to,
4868             &padding_len, &chunk_len);
4869
4870         init->ch.chunk_length = htons(chunk_len);
4871         if (padding_len > 0) {
4872                 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
4873                         sctp_m_freem(m);
4874                         return;
4875                 }
4876         }
4877         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4878         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
4879             (struct sockaddr *)&net->ro._l_addr,
4880             m, 0, NULL, 0, 0, 0, 0,
4881             inp->sctp_lport, stcb->rport, htonl(0),
4882             net->port, NULL,
4883             0, 0,
4884             so_locked))) {
4885                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
4886                 if (error == ENOBUFS) {
4887                         stcb->asoc.ifp_had_enobuf = 1;
4888                         SCTP_STAT_INCR(sctps_lowlevelerr);
4889                 }
4890         } else {
4891                 stcb->asoc.ifp_had_enobuf = 0;
4892         }
4893         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4894         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4895 }
4896
4897 struct mbuf *
4898 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4899     int param_offset, int *abort_processing,
4900     struct sctp_chunkhdr *cp,
4901     int *nat_friendly,
4902     int *cookie_found)
4903 {
4904         /*
4905          * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4906          * being equal to the beginning of the params i.e. (iphlen +
4907          * sizeof(struct sctp_init_msg) parse through the parameters to the
4908          * end of the mbuf verifying that all parameters are known.
4909          *
4910          * For unknown parameters build and return a mbuf with
4911          * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4912          * processing this chunk stop, and set *abort_processing to 1.
4913          *
4914          * By having param_offset be pre-set to where parameters begin it is
4915          * hoped that this routine may be reused in the future by new
4916          * features.
4917          */
4918         struct sctp_paramhdr *phdr, params;
4919
4920         struct mbuf *mat, *m_tmp, *op_err, *op_err_last;
4921         int at, limit, pad_needed;
4922         uint16_t ptype, plen, padded_size;
4923
4924         *abort_processing = 0;
4925         if (cookie_found != NULL) {
4926                 *cookie_found = 0;
4927         }
4928         mat = in_initpkt;
4929         limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4930         at = param_offset;
4931         op_err = NULL;
4932         op_err_last = NULL;
4933         pad_needed = 0;
4934         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4935         phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4936         while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4937                 ptype = ntohs(phdr->param_type);
4938                 plen = ntohs(phdr->param_length);
4939                 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4940                         /* wacked parameter */
4941                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4942                         goto invalid_size;
4943                 }
4944                 limit -= SCTP_SIZE32(plen);
4945                 /*-
4946                  * All parameters for all chunks that we know/understand are
4947                  * listed here. We process them other places and make
4948                  * appropriate stop actions per the upper bits. However this
4949                  * is the generic routine processor's can call to get back
4950                  * an operr.. to either incorporate (init-ack) or send.
4951                  */
4952                 padded_size = SCTP_SIZE32(plen);
4953                 switch (ptype) {
4954                         /* Param's with variable size */
4955                 case SCTP_HEARTBEAT_INFO:
4956                 case SCTP_UNRECOG_PARAM:
4957                 case SCTP_ERROR_CAUSE_IND:
4958                         /* ok skip fwd */
4959                         at += padded_size;
4960                         break;
4961                 case SCTP_STATE_COOKIE:
4962                         if (cookie_found != NULL) {
4963                                 *cookie_found = 1;
4964                         }
4965                         at += padded_size;
4966                         break;
4967                         /* Param's with variable size within a range */
4968                 case SCTP_CHUNK_LIST:
4969                 case SCTP_SUPPORTED_CHUNK_EXT:
4970                         if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4971                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4972                                 goto invalid_size;
4973                         }
4974                         at += padded_size;
4975                         break;
4976                 case SCTP_SUPPORTED_ADDRTYPE:
4977                         if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4978                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4979                                 goto invalid_size;
4980                         }
4981                         at += padded_size;
4982                         break;
4983                 case SCTP_RANDOM:
4984                         if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4985                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4986                                 goto invalid_size;
4987                         }
4988                         at += padded_size;
4989                         break;
4990                 case SCTP_SET_PRIM_ADDR:
4991                 case SCTP_DEL_IP_ADDRESS:
4992                 case SCTP_ADD_IP_ADDRESS:
4993                         if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4994                             (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4995                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4996                                 goto invalid_size;
4997                         }
4998                         at += padded_size;
4999                         break;
5000                         /* Param's with a fixed size */
5001                 case SCTP_IPV4_ADDRESS:
5002                         if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
5003                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
5004                                 goto invalid_size;
5005                         }
5006                         at += padded_size;
5007                         break;
5008                 case SCTP_IPV6_ADDRESS:
5009                         if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
5010                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
5011                                 goto invalid_size;
5012                         }
5013                         at += padded_size;
5014                         break;
5015                 case SCTP_COOKIE_PRESERVE:
5016                         if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
5017                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
5018                                 goto invalid_size;
5019                         }
5020                         at += padded_size;
5021                         break;
5022                 case SCTP_HAS_NAT_SUPPORT:
5023                         *nat_friendly = 1;
5024                         /* fall through */
5025                 case SCTP_PRSCTP_SUPPORTED:
5026                         if (padded_size != sizeof(struct sctp_paramhdr)) {
5027                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
5028                                 goto invalid_size;
5029                         }
5030                         at += padded_size;
5031                         break;
5032                 case SCTP_ECN_CAPABLE:
5033                         if (padded_size != sizeof(struct sctp_paramhdr)) {
5034                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
5035                                 goto invalid_size;
5036                         }
5037                         at += padded_size;
5038                         break;
5039                 case SCTP_ULP_ADAPTATION:
5040                         if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
5041                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
5042                                 goto invalid_size;
5043                         }
5044                         at += padded_size;
5045                         break;
5046                 case SCTP_SUCCESS_REPORT:
5047                         if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
5048                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
5049                                 goto invalid_size;
5050                         }
5051                         at += padded_size;
5052                         break;
5053                 case SCTP_HOSTNAME_ADDRESS:
5054                         {
5055                                 /* Hostname parameters are deprecated. */
5056                                 struct sctp_gen_error_cause *cause;
5057                                 int l_len;
5058
5059                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
5060                                 *abort_processing = 1;
5061                                 sctp_m_freem(op_err);
5062                                 op_err = NULL;
5063                                 op_err_last = NULL;
5064 #ifdef INET6
5065                                 l_len = SCTP_MIN_OVERHEAD;
5066 #else
5067                                 l_len = SCTP_MIN_V4_OVERHEAD;
5068 #endif
5069                                 l_len += sizeof(struct sctp_chunkhdr);
5070                                 l_len += sizeof(struct sctp_gen_error_cause);
5071                                 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5072                                 if (op_err != NULL) {
5073                                         /*
5074                                          * Pre-reserve space for IP, SCTP,
5075                                          * and chunk header.
5076                                          */
5077 #ifdef INET6
5078                                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5079 #else
5080                                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5081 #endif
5082                                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5083                                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5084                                         SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5085                                         cause = mtod(op_err, struct sctp_gen_error_cause *);
5086                                         cause->code = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
5087                                         cause->length = htons((uint16_t)(sizeof(struct sctp_gen_error_cause) + plen));
5088                                         SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5089                                         if (SCTP_BUF_NEXT(op_err) == NULL) {
5090                                                 sctp_m_freem(op_err);
5091                                                 op_err = NULL;
5092                                                 op_err_last = NULL;
5093                                         }
5094                                 }
5095                                 return (op_err);
5096                         }
5097                 default:
5098                         /*
5099                          * we do not recognize the parameter figure out what
5100                          * we do.
5101                          */
5102                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
5103                         if ((ptype & 0x4000) == 0x4000) {
5104                                 /* Report bit is set?? */
5105                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
5106                                 if (op_err == NULL) {
5107                                         int l_len;
5108
5109                                         /* Ok need to try to get an mbuf */
5110 #ifdef INET6
5111                                         l_len = SCTP_MIN_OVERHEAD;
5112 #else
5113                                         l_len = SCTP_MIN_V4_OVERHEAD;
5114 #endif
5115                                         l_len += sizeof(struct sctp_chunkhdr);
5116                                         l_len += sizeof(struct sctp_paramhdr);
5117                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5118                                         if (op_err) {
5119                                                 SCTP_BUF_LEN(op_err) = 0;
5120 #ifdef INET6
5121                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5122 #else
5123                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5124 #endif
5125                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5126                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5127                                                 op_err_last = op_err;
5128                                         }
5129                                 }
5130                                 if (op_err != NULL) {
5131                                         /* If we have space */
5132                                         struct sctp_paramhdr *param;
5133
5134                                         if (pad_needed > 0) {
5135                                                 op_err_last = sctp_add_pad_tombuf(op_err_last, pad_needed);
5136                                         }
5137                                         if (op_err_last == NULL) {
5138                                                 sctp_m_freem(op_err);
5139                                                 op_err = NULL;
5140                                                 op_err_last = NULL;
5141                                                 goto more_processing;
5142                                         }
5143                                         if (M_TRAILINGSPACE(op_err_last) < (int)sizeof(struct sctp_paramhdr)) {
5144                                                 m_tmp = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
5145                                                 if (m_tmp == NULL) {
5146                                                         sctp_m_freem(op_err);
5147                                                         op_err = NULL;
5148                                                         op_err_last = NULL;
5149                                                         goto more_processing;
5150                                                 }
5151                                                 SCTP_BUF_LEN(m_tmp) = 0;
5152                                                 SCTP_BUF_NEXT(m_tmp) = NULL;
5153                                                 SCTP_BUF_NEXT(op_err_last) = m_tmp;
5154                                                 op_err_last = m_tmp;
5155                                         }
5156                                         param = (struct sctp_paramhdr *)(mtod(op_err_last, caddr_t)+SCTP_BUF_LEN(op_err_last));
5157                                         param->param_type = htons(SCTP_UNRECOG_PARAM);
5158                                         param->param_length = htons((uint16_t)sizeof(struct sctp_paramhdr) + plen);
5159                                         SCTP_BUF_LEN(op_err_last) += sizeof(struct sctp_paramhdr);
5160                                         SCTP_BUF_NEXT(op_err_last) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5161                                         if (SCTP_BUF_NEXT(op_err_last) == NULL) {
5162                                                 sctp_m_freem(op_err);
5163                                                 op_err = NULL;
5164                                                 op_err_last = NULL;
5165                                                 goto more_processing;
5166                                         } else {
5167                                                 while (SCTP_BUF_NEXT(op_err_last) != NULL) {
5168                                                         op_err_last = SCTP_BUF_NEXT(op_err_last);
5169                                                 }
5170                                         }
5171                                         if (plen % 4 != 0) {
5172                                                 pad_needed = 4 - (plen % 4);
5173                                         } else {
5174                                                 pad_needed = 0;
5175                                         }
5176                                 }
5177                         }
5178         more_processing:
5179                         if ((ptype & 0x8000) == 0x0000) {
5180                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
5181                                 return (op_err);
5182                         } else {
5183                                 /* skip this chunk and continue processing */
5184                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
5185                                 at += SCTP_SIZE32(plen);
5186                         }
5187                         break;
5188                 }
5189                 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
5190         }
5191         return (op_err);
5192 invalid_size:
5193         SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
5194         *abort_processing = 1;
5195         sctp_m_freem(op_err);
5196         op_err = NULL;
5197         op_err_last = NULL;
5198         if (phdr != NULL) {
5199                 struct sctp_paramhdr *param;
5200                 int l_len;
5201 #ifdef INET6
5202                 l_len = SCTP_MIN_OVERHEAD;
5203 #else
5204                 l_len = SCTP_MIN_V4_OVERHEAD;
5205 #endif
5206                 l_len += sizeof(struct sctp_chunkhdr);
5207                 l_len += (2 * sizeof(struct sctp_paramhdr));
5208                 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5209                 if (op_err) {
5210                         SCTP_BUF_LEN(op_err) = 0;
5211 #ifdef INET6
5212                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5213 #else
5214                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5215 #endif
5216                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5217                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5218                         SCTP_BUF_LEN(op_err) = 2 * sizeof(struct sctp_paramhdr);
5219                         param = mtod(op_err, struct sctp_paramhdr *);
5220                         param->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5221                         param->param_length = htons(2 * sizeof(struct sctp_paramhdr));
5222                         param++;
5223                         param->param_type = htons(ptype);
5224                         param->param_length = htons(plen);
5225                 }
5226         }
5227         return (op_err);
5228 }
5229
5230 /*
5231  * Given a INIT chunk, look through the parameters to verify that there
5232  * are no new addresses.
5233  * Return true, if there is a new address or there is a problem parsing
5234    the parameters. Provide an optional error cause used when sending an ABORT.
5235  * Return false, if there are no new addresses and there is no problem in
5236    parameter processing.
5237  */
5238 static bool
5239 sctp_are_there_new_addresses(struct sctp_association *asoc,
5240     struct mbuf *in_initpkt, int offset, int limit, struct sockaddr *src,
5241     struct mbuf **op_err)
5242 {
5243         struct sockaddr *sa_touse;
5244         struct sockaddr *sa;
5245         struct sctp_paramhdr *phdr, params;
5246         struct sctp_nets *net;
5247 #ifdef INET
5248         struct sockaddr_in sin4, *sa4;
5249 #endif
5250 #ifdef INET6
5251         struct sockaddr_in6 sin6, *sa6;
5252 #endif
5253         uint16_t ptype, plen;
5254         bool fnd, check_src;
5255
5256         *op_err = NULL;
5257 #ifdef INET
5258         memset(&sin4, 0, sizeof(sin4));
5259         sin4.sin_family = AF_INET;
5260         sin4.sin_len = sizeof(sin4);
5261 #endif
5262 #ifdef INET6
5263         memset(&sin6, 0, sizeof(sin6));
5264         sin6.sin6_family = AF_INET6;
5265         sin6.sin6_len = sizeof(sin6);
5266 #endif
5267         /* First what about the src address of the pkt ? */
5268         check_src = false;
5269         switch (src->sa_family) {
5270 #ifdef INET
5271         case AF_INET:
5272                 if (asoc->scope.ipv4_addr_legal) {
5273                         check_src = true;
5274                 }
5275                 break;
5276 #endif
5277 #ifdef INET6
5278         case AF_INET6:
5279                 if (asoc->scope.ipv6_addr_legal) {
5280                         check_src = true;
5281                 }
5282                 break;
5283 #endif
5284         default:
5285                 /* TSNH */
5286                 break;
5287         }
5288         if (check_src) {
5289                 fnd = false;
5290                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5291                         sa = (struct sockaddr *)&net->ro._l_addr;
5292                         if (sa->sa_family == src->sa_family) {
5293 #ifdef INET
5294                                 if (sa->sa_family == AF_INET) {
5295                                         struct sockaddr_in *src4;
5296
5297                                         sa4 = (struct sockaddr_in *)sa;
5298                                         src4 = (struct sockaddr_in *)src;
5299                                         if (sa4->sin_addr.s_addr == src4->sin_addr.s_addr) {
5300                                                 fnd = true;
5301                                                 break;
5302                                         }
5303                                 }
5304 #endif
5305 #ifdef INET6
5306                                 if (sa->sa_family == AF_INET6) {
5307                                         struct sockaddr_in6 *src6;
5308
5309                                         sa6 = (struct sockaddr_in6 *)sa;
5310                                         src6 = (struct sockaddr_in6 *)src;
5311                                         if (SCTP6_ARE_ADDR_EQUAL(sa6, src6)) {
5312                                                 fnd = true;
5313                                                 break;
5314                                         }
5315                                 }
5316 #endif
5317                         }
5318                 }
5319                 if (!fnd) {
5320                         /*
5321                          * If sending an ABORT in case of an additional
5322                          * address, don't use the new address error cause.
5323                          * This looks no different than if no listener was
5324                          * present.
5325                          */
5326                         *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5327                         return (true);
5328                 }
5329         }
5330         /* Ok so far lets munge through the rest of the packet */
5331         offset += sizeof(struct sctp_init_chunk);
5332         phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5333         while (phdr) {
5334                 sa_touse = NULL;
5335                 ptype = ntohs(phdr->param_type);
5336                 plen = ntohs(phdr->param_length);
5337                 if (offset + plen > limit) {
5338                         *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Partial parameter");
5339                         return (true);
5340                 }
5341                 if (plen < sizeof(struct sctp_paramhdr)) {
5342                         *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length too small");
5343                         return (true);
5344                 }
5345                 switch (ptype) {
5346 #ifdef INET
5347                 case SCTP_IPV4_ADDRESS:
5348                         {
5349                                 struct sctp_ipv4addr_param *p4, p4_buf;
5350
5351                                 if (plen != sizeof(struct sctp_ipv4addr_param)) {
5352                                         *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5353                                         return (true);
5354                                 }
5355                                 phdr = sctp_get_next_param(in_initpkt, offset,
5356                                     (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5357                                 if (phdr == NULL) {
5358                                         *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5359                                         return (true);
5360                                 }
5361                                 if (asoc->scope.ipv4_addr_legal) {
5362                                         p4 = (struct sctp_ipv4addr_param *)phdr;
5363                                         sin4.sin_addr.s_addr = p4->addr;
5364                                         sa_touse = (struct sockaddr *)&sin4;
5365                                 }
5366                                 break;
5367                         }
5368 #endif
5369 #ifdef INET6
5370                 case SCTP_IPV6_ADDRESS:
5371                         {
5372                                 struct sctp_ipv6addr_param *p6, p6_buf;
5373
5374                                 if (plen != sizeof(struct sctp_ipv6addr_param)) {
5375                                         *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5376                                         return (true);
5377                                 }
5378                                 phdr = sctp_get_next_param(in_initpkt, offset,
5379                                     (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5380                                 if (phdr == NULL) {
5381                                         *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5382                                         return (true);
5383                                 }
5384                                 if (asoc->scope.ipv6_addr_legal) {
5385                                         p6 = (struct sctp_ipv6addr_param *)phdr;
5386                                         memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5387                                             sizeof(p6->addr));
5388                                         sa_touse = (struct sockaddr *)&sin6;
5389                                 }
5390                                 break;
5391                         }
5392 #endif
5393                 default:
5394                         sa_touse = NULL;
5395                         break;
5396                 }
5397                 if (sa_touse) {
5398                         /* ok, sa_touse points to one to check */
5399                         fnd = false;
5400                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5401                                 sa = (struct sockaddr *)&net->ro._l_addr;
5402                                 if (sa->sa_family != sa_touse->sa_family) {
5403                                         continue;
5404                                 }
5405 #ifdef INET
5406                                 if (sa->sa_family == AF_INET) {
5407                                         sa4 = (struct sockaddr_in *)sa;
5408                                         if (sa4->sin_addr.s_addr ==
5409                                             sin4.sin_addr.s_addr) {
5410                                                 fnd = true;
5411                                                 break;
5412                                         }
5413                                 }
5414 #endif
5415 #ifdef INET6
5416                                 if (sa->sa_family == AF_INET6) {
5417                                         sa6 = (struct sockaddr_in6 *)sa;
5418                                         if (SCTP6_ARE_ADDR_EQUAL(
5419                                             sa6, &sin6)) {
5420                                                 fnd = true;
5421                                                 break;
5422                                         }
5423                                 }
5424 #endif
5425                         }
5426                         if (!fnd) {
5427                                 /*
5428                                  * If sending an ABORT in case of an
5429                                  * additional address, don't use the new
5430                                  * address error cause. This looks no
5431                                  * different than if no listener was
5432                                  * present.
5433                                  */
5434                                 *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5435                                 return (true);
5436                         }
5437                 }
5438                 offset += SCTP_SIZE32(plen);
5439                 if (offset >= limit) {
5440                         break;
5441                 }
5442                 phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5443         }
5444         return (false);
5445 }
5446
5447 /*
5448  * Given a MBUF chain that was sent into us containing an INIT. Build a
5449  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5450  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5451  * message (i.e. the struct sctp_init_msg).
5452  */
5453 void
5454 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5455     struct sctp_nets *src_net, struct mbuf *init_pkt,
5456     int iphlen, int offset,
5457     struct sockaddr *src, struct sockaddr *dst,
5458     struct sctphdr *sh, struct sctp_init_chunk *init_chk,
5459     uint8_t mflowtype, uint32_t mflowid,
5460     uint32_t vrf_id, uint16_t port)
5461 {
5462         struct sctp_association *asoc;
5463         struct mbuf *m, *m_tmp, *m_last, *m_cookie, *op_err;
5464         struct sctp_init_ack_chunk *initack;
5465         struct sctp_adaptation_layer_indication *ali;
5466         struct sctp_supported_chunk_types_param *pr_supported;
5467         struct sctp_paramhdr *ph;
5468         union sctp_sockstore *over_addr;
5469         struct sctp_scoping scp;
5470         struct timeval now;
5471 #ifdef INET
5472         struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
5473         struct sockaddr_in *src4 = (struct sockaddr_in *)src;
5474         struct sockaddr_in *sin;
5475 #endif
5476 #ifdef INET6
5477         struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
5478         struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)src;
5479         struct sockaddr_in6 *sin6;
5480 #endif
5481         struct sockaddr *to;
5482         struct sctp_state_cookie stc;
5483         struct sctp_nets *net = NULL;
5484         uint8_t *signature = NULL;
5485         int cnt_inits_to = 0;
5486         uint16_t his_limit, i_want;
5487         int abort_flag;
5488         int nat_friendly = 0;
5489         int error;
5490         struct socket *so;
5491         uint16_t num_ext, chunk_len, padding_len, parameter_len;
5492
5493         if (stcb) {
5494                 asoc = &stcb->asoc;
5495         } else {
5496                 asoc = NULL;
5497         }
5498         if ((asoc != NULL) &&
5499             (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
5500                 if (sctp_are_there_new_addresses(asoc, init_pkt, offset, offset + ntohs(init_chk->ch.chunk_length), src, &op_err)) {
5501                         /*
5502                          * new addresses, out of here in non-cookie-wait
5503                          * states
5504                          */
5505                         sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
5506                             mflowtype, mflowid, inp->fibnum,
5507                             vrf_id, port);
5508                         return;
5509                 }
5510                 if (src_net != NULL && (src_net->port != port)) {
5511                         /*
5512                          * change of remote encapsulation port, out of here
5513                          * in non-cookie-wait states
5514                          *
5515                          * Send an ABORT, without an specific error cause.
5516                          * This looks no different than if no listener was
5517                          * present.
5518                          */
5519                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5520                             "Remote encapsulation port changed");
5521                         sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
5522                             mflowtype, mflowid, inp->fibnum,
5523                             vrf_id, port);
5524                         return;
5525                 }
5526         }
5527         abort_flag = 0;
5528         op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5529             (offset + sizeof(struct sctp_init_chunk)),
5530             &abort_flag,
5531             (struct sctp_chunkhdr *)init_chk,
5532             &nat_friendly, NULL);
5533         if (abort_flag) {
5534 do_a_abort:
5535                 if (op_err == NULL) {
5536                         char msg[SCTP_DIAG_INFO_LEN];
5537
5538                         SCTP_SNPRINTF(msg, sizeof(msg), "%s:%d at %s", __FILE__, __LINE__, __func__);
5539                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5540                             msg);
5541                 }
5542                 sctp_send_abort(init_pkt, iphlen, src, dst, sh,
5543                     init_chk->init.initiate_tag, op_err,
5544                     mflowtype, mflowid, inp->fibnum,
5545                     vrf_id, port);
5546                 return;
5547         }
5548         m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
5549         if (m == NULL) {
5550                 /* No memory, INIT timer will re-attempt. */
5551                 sctp_m_freem(op_err);
5552                 return;
5553         }
5554         chunk_len = (uint16_t)sizeof(struct sctp_init_ack_chunk);
5555         padding_len = 0;
5556
5557         /*
5558          * We might not overwrite the identification[] completely and on
5559          * some platforms time_entered will contain some padding. Therefore
5560          * zero out the cookie to avoid putting uninitialized memory on the
5561          * wire.
5562          */
5563         memset(&stc, 0, sizeof(struct sctp_state_cookie));
5564
5565         /* the time I built cookie */
5566         (void)SCTP_GETTIME_TIMEVAL(&now);
5567         stc.time_entered.tv_sec = now.tv_sec;
5568         stc.time_entered.tv_usec = now.tv_usec;
5569
5570         /* populate any tie tags */
5571         if (asoc != NULL) {
5572                 /* unlock before tag selections */
5573                 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5574                 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5575                 stc.cookie_life = asoc->cookie_life;
5576                 net = asoc->primary_destination;
5577         } else {
5578                 stc.tie_tag_my_vtag = 0;
5579                 stc.tie_tag_peer_vtag = 0;
5580                 /* life I will award this cookie */
5581                 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5582         }
5583
5584         /* copy in the ports for later check */
5585         stc.myport = sh->dest_port;
5586         stc.peerport = sh->src_port;
5587
5588         /*
5589          * If we wanted to honor cookie life extensions, we would add to
5590          * stc.cookie_life. For now we should NOT honor any extension
5591          */
5592         stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5593         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5594                 stc.ipv6_addr_legal = 1;
5595                 if (SCTP_IPV6_V6ONLY(inp)) {
5596                         stc.ipv4_addr_legal = 0;
5597                 } else {
5598                         stc.ipv4_addr_legal = 1;
5599                 }
5600         } else {
5601                 stc.ipv6_addr_legal = 0;
5602                 stc.ipv4_addr_legal = 1;
5603         }
5604         stc.ipv4_scope = 0;
5605         if (net == NULL) {
5606                 to = src;
5607                 switch (dst->sa_family) {
5608 #ifdef INET
5609                 case AF_INET:
5610                         {
5611                                 /* lookup address */
5612                                 stc.address[0] = src4->sin_addr.s_addr;
5613                                 stc.address[1] = 0;
5614                                 stc.address[2] = 0;
5615                                 stc.address[3] = 0;
5616                                 stc.addr_type = SCTP_IPV4_ADDRESS;
5617                                 /* local from address */
5618                                 stc.laddress[0] = dst4->sin_addr.s_addr;
5619                                 stc.laddress[1] = 0;
5620                                 stc.laddress[2] = 0;
5621                                 stc.laddress[3] = 0;
5622                                 stc.laddr_type = SCTP_IPV4_ADDRESS;
5623                                 /* scope_id is only for v6 */
5624                                 stc.scope_id = 0;
5625                                 if ((IN4_ISPRIVATE_ADDRESS(&src4->sin_addr)) ||
5626                                     (IN4_ISPRIVATE_ADDRESS(&dst4->sin_addr))) {
5627                                         stc.ipv4_scope = 1;
5628                                 }
5629                                 /* Must use the address in this case */
5630                                 if (sctp_is_address_on_local_host(src, vrf_id)) {
5631                                         stc.loopback_scope = 1;
5632                                         stc.ipv4_scope = 1;
5633                                         stc.site_scope = 1;
5634                                         stc.local_scope = 0;
5635                                 }
5636                                 break;
5637                         }
5638 #endif
5639 #ifdef INET6
5640                 case AF_INET6:
5641                         {
5642                                 stc.addr_type = SCTP_IPV6_ADDRESS;
5643                                 memcpy(&stc.address, &src6->sin6_addr, sizeof(struct in6_addr));
5644                                 stc.scope_id = ntohs(in6_getscope(&src6->sin6_addr));
5645                                 if (sctp_is_address_on_local_host(src, vrf_id)) {
5646                                         stc.loopback_scope = 1;
5647                                         stc.local_scope = 0;
5648                                         stc.site_scope = 1;
5649                                         stc.ipv4_scope = 1;
5650                                 } else if (IN6_IS_ADDR_LINKLOCAL(&src6->sin6_addr) ||
5651                                     IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr)) {
5652                                         /*
5653                                          * If the new destination or source
5654                                          * is a LINK_LOCAL we must have
5655                                          * common both site and local scope.
5656                                          * Don't set local scope though
5657                                          * since we must depend on the
5658                                          * source to be added implicitly. We
5659                                          * cannot assure just because we
5660                                          * share one link that all links are
5661                                          * common.
5662                                          */
5663                                         stc.local_scope = 0;
5664                                         stc.site_scope = 1;
5665                                         stc.ipv4_scope = 1;
5666                                         /*
5667                                          * we start counting for the private
5668                                          * address stuff at 1. since the
5669                                          * link local we source from won't
5670                                          * show up in our scoped count.
5671                                          */
5672                                         cnt_inits_to = 1;
5673                                         /*
5674                                          * pull out the scope_id from
5675                                          * incoming pkt
5676                                          */
5677                                 } else if (IN6_IS_ADDR_SITELOCAL(&src6->sin6_addr) ||
5678                                     IN6_IS_ADDR_SITELOCAL(&dst6->sin6_addr)) {
5679                                         /*
5680                                          * If the new destination or source
5681                                          * is SITE_LOCAL then we must have
5682                                          * site scope in common.
5683                                          */
5684                                         stc.site_scope = 1;
5685                                 }
5686                                 memcpy(&stc.laddress, &dst6->sin6_addr, sizeof(struct in6_addr));
5687                                 stc.laddr_type = SCTP_IPV6_ADDRESS;
5688                                 break;
5689                         }
5690 #endif
5691                 default:
5692                         /* TSNH */
5693                         goto do_a_abort;
5694                         break;
5695                 }
5696         } else {
5697                 /* set the scope per the existing tcb */
5698
5699 #ifdef INET6
5700                 struct sctp_nets *lnet;
5701 #endif
5702
5703                 stc.loopback_scope = asoc->scope.loopback_scope;
5704                 stc.ipv4_scope = asoc->scope.ipv4_local_scope;
5705                 stc.site_scope = asoc->scope.site_scope;
5706                 stc.local_scope = asoc->scope.local_scope;
5707 #ifdef INET6
5708                 /* Why do we not consider IPv4 LL addresses? */
5709                 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5710                         if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5711                                 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5712                                         /*
5713                                          * if we have a LL address, start
5714                                          * counting at 1.
5715                                          */
5716                                         cnt_inits_to = 1;
5717                                 }
5718                         }
5719                 }
5720 #endif
5721                 /* use the net pointer */
5722                 to = (struct sockaddr *)&net->ro._l_addr;
5723                 switch (to->sa_family) {
5724 #ifdef INET
5725                 case AF_INET:
5726                         sin = (struct sockaddr_in *)to;
5727                         stc.address[0] = sin->sin_addr.s_addr;
5728                         stc.address[1] = 0;
5729                         stc.address[2] = 0;
5730                         stc.address[3] = 0;
5731                         stc.addr_type = SCTP_IPV4_ADDRESS;
5732                         if (net->src_addr_selected == 0) {
5733                                 /*
5734                                  * strange case here, the INIT should have
5735                                  * did the selection.
5736                                  */
5737                                 net->ro._s_addr = sctp_source_address_selection(inp,
5738                                     stcb, (sctp_route_t *)&net->ro,
5739                                     net, 0, vrf_id);
5740                                 if (net->ro._s_addr == NULL) {
5741                                         sctp_m_freem(op_err);
5742                                         sctp_m_freem(m);
5743                                         return;
5744                                 }
5745
5746                                 net->src_addr_selected = 1;
5747                         }
5748                         stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5749                         stc.laddress[1] = 0;
5750                         stc.laddress[2] = 0;
5751                         stc.laddress[3] = 0;
5752                         stc.laddr_type = SCTP_IPV4_ADDRESS;
5753                         /* scope_id is only for v6 */
5754                         stc.scope_id = 0;
5755                         break;
5756 #endif
5757 #ifdef INET6
5758                 case AF_INET6:
5759                         sin6 = (struct sockaddr_in6 *)to;
5760                         memcpy(&stc.address, &sin6->sin6_addr,
5761                             sizeof(struct in6_addr));
5762                         stc.addr_type = SCTP_IPV6_ADDRESS;
5763                         stc.scope_id = sin6->sin6_scope_id;
5764                         if (net->src_addr_selected == 0) {
5765                                 /*
5766                                  * strange case here, the INIT should have
5767                                  * done the selection.
5768                                  */
5769                                 net->ro._s_addr = sctp_source_address_selection(inp,
5770                                     stcb, (sctp_route_t *)&net->ro,
5771                                     net, 0, vrf_id);
5772                                 if (net->ro._s_addr == NULL) {
5773                                         sctp_m_freem(op_err);
5774                                         sctp_m_freem(m);
5775                                         return;
5776                                 }
5777
5778                                 net->src_addr_selected = 1;
5779                         }
5780                         memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5781                             sizeof(struct in6_addr));
5782                         stc.laddr_type = SCTP_IPV6_ADDRESS;
5783                         break;
5784 #endif
5785                 }
5786         }
5787         /* Now lets put the SCTP header in place */
5788         initack = mtod(m, struct sctp_init_ack_chunk *);
5789         /* Save it off for quick ref */
5790         stc.peers_vtag = ntohl(init_chk->init.initiate_tag);
5791         /* who are we */
5792         memcpy(stc.identification, SCTP_VERSION_STRING,
5793             min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5794         memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
5795         /* now the chunk header */
5796         initack->ch.chunk_type = SCTP_INITIATION_ACK;
5797         initack->ch.chunk_flags = 0;
5798         /* fill in later from mbuf we build */
5799         initack->ch.chunk_length = 0;
5800         /* place in my tag */
5801         if ((asoc != NULL) &&
5802             ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
5803             (SCTP_GET_STATE(stcb) == SCTP_STATE_INUSE) ||
5804             (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED))) {
5805                 /* re-use the v-tags and init-seq here */
5806                 initack->init.initiate_tag = htonl(asoc->my_vtag);
5807                 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5808         } else {
5809                 uint32_t vtag, itsn;
5810
5811                 if (asoc) {
5812                         atomic_add_int(&asoc->refcnt, 1);
5813                         SCTP_TCB_UNLOCK(stcb);
5814         new_tag:
5815                         SCTP_INP_INFO_RLOCK();
5816                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5817                         SCTP_INP_INFO_RUNLOCK();
5818                         if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5819                                 /*
5820                                  * Got a duplicate vtag on some guy behind a
5821                                  * nat make sure we don't use it.
5822                                  */
5823                                 goto new_tag;
5824                         }
5825                         initack->init.initiate_tag = htonl(vtag);
5826                         /* get a TSN to use too */
5827                         itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5828                         initack->init.initial_tsn = htonl(itsn);
5829                         SCTP_TCB_LOCK(stcb);
5830                         atomic_subtract_int(&asoc->refcnt, 1);
5831                 } else {
5832                         SCTP_INP_INCR_REF(inp);
5833                         SCTP_INP_RUNLOCK(inp);
5834                         SCTP_INP_INFO_RLOCK();
5835                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5836                         SCTP_INP_INFO_RUNLOCK();
5837                         initack->init.initiate_tag = htonl(vtag);
5838                         /* get a TSN to use too */
5839                         initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5840                         SCTP_INP_RLOCK(inp);
5841                         SCTP_INP_DECR_REF(inp);
5842                 }
5843         }
5844         /* save away my tag to */
5845         stc.my_vtag = initack->init.initiate_tag;
5846
5847         /* set up some of the credits. */
5848         so = inp->sctp_socket;
5849         if (so == NULL) {
5850                 /* memory problem */
5851                 sctp_m_freem(op_err);
5852                 sctp_m_freem(m);
5853                 return;
5854         } else {
5855                 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5856         }
5857         /* set what I want */
5858         his_limit = ntohs(init_chk->init.num_inbound_streams);
5859         /* choose what I want */
5860         if (asoc != NULL) {
5861                 if (asoc->streamoutcnt > asoc->pre_open_streams) {
5862                         i_want = asoc->streamoutcnt;
5863                 } else {
5864                         i_want = asoc->pre_open_streams;
5865                 }
5866         } else {
5867                 i_want = inp->sctp_ep.pre_open_stream_count;
5868         }
5869         if (his_limit < i_want) {
5870                 /* I Want more :< */
5871                 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5872         } else {
5873                 /* I can have what I want :> */
5874                 initack->init.num_outbound_streams = htons(i_want);
5875         }
5876         /* tell him his limit. */
5877         initack->init.num_inbound_streams =
5878             htons(inp->sctp_ep.max_open_streams_intome);
5879
5880         /* adaptation layer indication parameter */
5881         if (inp->sctp_ep.adaptation_layer_indicator_provided) {
5882                 parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
5883                 ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t)+chunk_len);
5884                 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5885                 ali->ph.param_length = htons(parameter_len);
5886                 ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
5887                 chunk_len += parameter_len;
5888         }
5889
5890         /* ECN parameter */
5891         if (((asoc != NULL) && (asoc->ecn_supported == 1)) ||
5892             ((asoc == NULL) && (inp->ecn_supported == 1))) {
5893                 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5894                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5895                 ph->param_type = htons(SCTP_ECN_CAPABLE);
5896                 ph->param_length = htons(parameter_len);
5897                 chunk_len += parameter_len;
5898         }
5899
5900         /* PR-SCTP supported parameter */
5901         if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
5902             ((asoc == NULL) && (inp->prsctp_supported == 1))) {
5903                 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5904                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5905                 ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
5906                 ph->param_length = htons(parameter_len);
5907                 chunk_len += parameter_len;
5908         }
5909
5910         /* Add NAT friendly parameter */
5911         if (nat_friendly) {
5912                 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5913                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5914                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5915                 ph->param_length = htons(parameter_len);
5916                 chunk_len += parameter_len;
5917         }
5918
5919         /* And now tell the peer which extensions we support */
5920         num_ext = 0;
5921         pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+chunk_len);
5922         if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
5923             ((asoc == NULL) && (inp->prsctp_supported == 1))) {
5924                 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5925                 if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
5926                     ((asoc == NULL) && (inp->idata_supported == 1))) {
5927                         pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
5928                 }
5929         }
5930         if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
5931             ((asoc == NULL) && (inp->auth_supported == 1))) {
5932                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5933         }
5934         if (((asoc != NULL) && (asoc->asconf_supported == 1)) ||
5935             ((asoc == NULL) && (inp->asconf_supported == 1))) {
5936                 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5937                 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5938         }
5939         if (((asoc != NULL) && (asoc->reconfig_supported == 1)) ||
5940             ((asoc == NULL) && (inp->reconfig_supported == 1))) {
5941                 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5942         }
5943         if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
5944             ((asoc == NULL) && (inp->idata_supported == 1))) {
5945                 pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
5946         }
5947         if (((asoc != NULL) && (asoc->nrsack_supported == 1)) ||
5948             ((asoc == NULL) && (inp->nrsack_supported == 1))) {
5949                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5950         }
5951         if (((asoc != NULL) && (asoc->pktdrop_supported == 1)) ||
5952             ((asoc == NULL) && (inp->pktdrop_supported == 1))) {
5953                 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5954         }
5955         if (num_ext > 0) {
5956                 parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
5957                 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5958                 pr_supported->ph.param_length = htons(parameter_len);
5959                 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5960                 chunk_len += parameter_len;
5961         }
5962
5963         /* add authentication parameters */
5964         if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
5965             ((asoc == NULL) && (inp->auth_supported == 1))) {
5966                 struct sctp_auth_random *randp;
5967                 struct sctp_auth_hmac_algo *hmacs;
5968                 struct sctp_auth_chunk_list *chunks;
5969
5970                 if (padding_len > 0) {
5971                         memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
5972                         chunk_len += padding_len;
5973                         padding_len = 0;
5974                 }
5975                 /* generate and add RANDOM parameter */
5976                 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+chunk_len);
5977                 parameter_len = (uint16_t)sizeof(struct sctp_auth_random) +
5978                     SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5979                 randp->ph.param_type = htons(SCTP_RANDOM);
5980                 randp->ph.param_length = htons(parameter_len);
5981                 SCTP_READ_RANDOM(randp->random_data, SCTP_AUTH_RANDOM_SIZE_DEFAULT);
5982                 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5983                 chunk_len += parameter_len;
5984
5985                 if (padding_len > 0) {
5986                         memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
5987                         chunk_len += padding_len;
5988                         padding_len = 0;
5989                 }
5990                 /* add HMAC_ALGO parameter */
5991                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+chunk_len);
5992                 parameter_len = (uint16_t)sizeof(struct sctp_auth_hmac_algo) +
5993                     sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5994                     (uint8_t *)hmacs->hmac_ids);
5995                 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5996                 hmacs->ph.param_length = htons(parameter_len);
5997                 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5998                 chunk_len += parameter_len;
5999
6000                 if (padding_len > 0) {
6001                         memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6002                         chunk_len += padding_len;
6003                         padding_len = 0;
6004                 }
6005                 /* add CHUNKS parameter */
6006                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+chunk_len);
6007                 parameter_len = (uint16_t)sizeof(struct sctp_auth_chunk_list) +
6008                     sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
6009                     chunks->chunk_types);
6010                 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
6011                 chunks->ph.param_length = htons(parameter_len);
6012                 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6013                 chunk_len += parameter_len;
6014         }
6015         SCTP_BUF_LEN(m) = chunk_len;
6016         m_last = m;
6017         /* now the addresses */
6018         /*
6019          * To optimize this we could put the scoping stuff into a structure
6020          * and remove the individual uint8's from the stc structure. Then we
6021          * could just sifa in the address within the stc.. but for now this
6022          * is a quick hack to get the address stuff teased apart.
6023          */
6024         scp.ipv4_addr_legal = stc.ipv4_addr_legal;
6025         scp.ipv6_addr_legal = stc.ipv6_addr_legal;
6026         scp.loopback_scope = stc.loopback_scope;
6027         scp.ipv4_local_scope = stc.ipv4_scope;
6028         scp.local_scope = stc.local_scope;
6029         scp.site_scope = stc.site_scope;
6030         m_last = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_last,
6031             cnt_inits_to,
6032             &padding_len, &chunk_len);
6033         /* padding_len can only be positive, if no addresses have been added */
6034         if (padding_len > 0) {
6035                 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6036                 chunk_len += padding_len;
6037                 SCTP_BUF_LEN(m) += padding_len;
6038                 padding_len = 0;
6039         }
6040
6041         /* tack on the operational error if present */
6042         if (op_err) {
6043                 parameter_len = 0;
6044                 for (m_tmp = op_err; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6045                         parameter_len += SCTP_BUF_LEN(m_tmp);
6046                 }
6047                 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6048                 SCTP_BUF_NEXT(m_last) = op_err;
6049                 while (SCTP_BUF_NEXT(m_last) != NULL) {
6050                         m_last = SCTP_BUF_NEXT(m_last);
6051                 }
6052                 chunk_len += parameter_len;
6053         }
6054         if (padding_len > 0) {
6055                 m_last = sctp_add_pad_tombuf(m_last, padding_len);
6056                 if (m_last == NULL) {
6057                         /* Houston we have a problem, no space */
6058                         sctp_m_freem(m);
6059                         return;
6060                 }
6061                 chunk_len += padding_len;
6062                 padding_len = 0;
6063         }
6064         /* Now we must build a cookie */
6065         m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
6066         if (m_cookie == NULL) {
6067                 /* memory problem */
6068                 sctp_m_freem(m);
6069                 return;
6070         }
6071         /* Now append the cookie to the end and update the space/size */
6072         SCTP_BUF_NEXT(m_last) = m_cookie;
6073         parameter_len = 0;
6074         for (m_tmp = m_cookie; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6075                 parameter_len += SCTP_BUF_LEN(m_tmp);
6076                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6077                         m_last = m_tmp;
6078                 }
6079         }
6080         padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6081         chunk_len += parameter_len;
6082
6083         /*
6084          * Place in the size, but we don't include the last pad (if any) in
6085          * the INIT-ACK.
6086          */
6087         initack->ch.chunk_length = htons(chunk_len);
6088
6089         /*
6090          * Time to sign the cookie, we don't sign over the cookie signature
6091          * though thus we set trailer.
6092          */
6093         (void)sctp_hmac_m(SCTP_HMAC,
6094             (uint8_t *)inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
6095             SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
6096             (uint8_t *)signature, SCTP_SIGNATURE_SIZE);
6097         /*
6098          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6099          * here since the timer will drive a retranmission.
6100          */
6101         if (padding_len > 0) {
6102                 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
6103                         sctp_m_freem(m);
6104                         return;
6105                 }
6106         }
6107         if (stc.loopback_scope) {
6108                 over_addr = (union sctp_sockstore *)dst;
6109         } else {
6110                 over_addr = NULL;
6111         }
6112
6113         if ((error = sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
6114             0, 0,
6115             inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
6116             port, over_addr,
6117             mflowtype, mflowid,
6118             SCTP_SO_NOT_LOCKED))) {
6119                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
6120                 if (error == ENOBUFS) {
6121                         if (asoc != NULL) {
6122                                 asoc->ifp_had_enobuf = 1;
6123                         }
6124                         SCTP_STAT_INCR(sctps_lowlevelerr);
6125                 }
6126         } else {
6127                 if (asoc != NULL) {
6128                         asoc->ifp_had_enobuf = 0;
6129                 }
6130         }
6131         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6132 }
6133
6134 static void
6135 sctp_prune_prsctp(struct sctp_tcb *stcb,
6136     struct sctp_association *asoc,
6137     struct sctp_nonpad_sndrcvinfo *srcv,
6138     int dataout)
6139 {
6140         int freed_spc = 0;
6141         struct sctp_tmit_chunk *chk, *nchk;
6142
6143         SCTP_TCB_LOCK_ASSERT(stcb);
6144         if ((asoc->prsctp_supported) &&
6145             (asoc->sent_queue_cnt_removeable > 0)) {
6146                 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6147                         /*
6148                          * Look for chunks marked with the PR_SCTP flag AND
6149                          * the buffer space flag. If the one being sent is
6150                          * equal or greater priority then purge the old one
6151                          * and free some space.
6152                          */
6153                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6154                                 /*
6155                                  * This one is PR-SCTP AND buffer space
6156                                  * limited type
6157                                  */
6158                                 if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6159                                         /*
6160                                          * Lower numbers equates to higher
6161                                          * priority. So if the one we are
6162                                          * looking at has a larger priority,
6163                                          * we want to drop the data and NOT
6164                                          * retransmit it.
6165                                          */
6166                                         if (chk->data) {
6167                                                 /*
6168                                                  * We release the book_size
6169                                                  * if the mbuf is here
6170                                                  */
6171                                                 int ret_spc;
6172                                                 uint8_t sent;
6173
6174                                                 if (chk->sent > SCTP_DATAGRAM_UNSENT)
6175                                                         sent = 1;
6176                                                 else
6177                                                         sent = 0;
6178                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6179                                                     sent,
6180                                                     SCTP_SO_LOCKED);
6181                                                 freed_spc += ret_spc;
6182                                                 if (freed_spc >= dataout) {
6183                                                         return;
6184                                                 }
6185                                         }       /* if chunk was present */
6186                                 }       /* if of sufficient priority */
6187                         }       /* if chunk has enabled */
6188                 }               /* tailqforeach */
6189
6190                 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
6191                         /* Here we must move to the sent queue and mark */
6192                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6193                                 if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6194                                         if (chk->data) {
6195                                                 /*
6196                                                  * We release the book_size
6197                                                  * if the mbuf is here
6198                                                  */
6199                                                 int ret_spc;
6200
6201                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6202                                                     0, SCTP_SO_LOCKED);
6203
6204                                                 freed_spc += ret_spc;
6205                                                 if (freed_spc >= dataout) {
6206                                                         return;
6207                                                 }
6208                                         }       /* end if chk->data */
6209                                 }       /* end if right class */
6210                         }       /* end if chk pr-sctp */
6211                 }               /* tailqforeachsafe (chk) */
6212         }                       /* if enabled in asoc */
6213 }
6214
6215 uint32_t
6216 sctp_get_frag_point(struct sctp_tcb *stcb)
6217 {
6218         struct sctp_association *asoc;
6219         uint32_t frag_point, overhead;
6220
6221         asoc = &stcb->asoc;
6222         /* Consider IP header and SCTP common header. */
6223         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6224                 overhead = SCTP_MIN_OVERHEAD;
6225         } else {
6226                 overhead = SCTP_MIN_V4_OVERHEAD;
6227         }
6228         /* Consider DATA/IDATA chunk header and AUTH header, if needed. */
6229         if (asoc->idata_supported) {
6230                 overhead += sizeof(struct sctp_idata_chunk);
6231                 if (sctp_auth_is_required_chunk(SCTP_IDATA, asoc->peer_auth_chunks)) {
6232                         overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6233                 }
6234         } else {
6235                 overhead += sizeof(struct sctp_data_chunk);
6236                 if (sctp_auth_is_required_chunk(SCTP_DATA, asoc->peer_auth_chunks)) {
6237                         overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6238                 }
6239         }
6240         KASSERT(overhead % 4 == 0,
6241             ("overhead (%u) not a multiple of 4", overhead));
6242         /* Consider padding. */
6243         if (asoc->smallest_mtu % 4 > 0) {
6244                 overhead += (asoc->smallest_mtu % 4);
6245         }
6246         KASSERT(asoc->smallest_mtu > overhead,
6247             ("Association MTU (%u) too small for overhead (%u)",
6248             asoc->smallest_mtu, overhead));
6249         frag_point = asoc->smallest_mtu - overhead;
6250         KASSERT(frag_point % 4 == 0,
6251             ("frag_point (%u) not a multiple of 4", frag_point));
6252         /* Honor MAXSEG socket option. */
6253         if ((asoc->sctp_frag_point > 0) &&
6254             (asoc->sctp_frag_point < frag_point)) {
6255                 frag_point = asoc->sctp_frag_point;
6256         }
6257         return (frag_point);
6258 }
6259
6260 static void
6261 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
6262 {
6263         /*
6264          * We assume that the user wants PR_SCTP_TTL if the user provides a
6265          * positive lifetime but does not specify any PR_SCTP policy.
6266          */
6267         if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
6268                 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6269         } else if (sp->timetolive > 0) {
6270                 sp->sinfo_flags |= SCTP_PR_SCTP_TTL;
6271                 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6272         } else {
6273                 return;
6274         }
6275         switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
6276         case CHUNK_FLAGS_PR_SCTP_BUF:
6277                 /*
6278                  * Time to live is a priority stored in tv_sec when doing
6279                  * the buffer drop thing.
6280                  */
6281                 sp->ts.tv_sec = sp->timetolive;
6282                 sp->ts.tv_usec = 0;
6283                 break;
6284         case CHUNK_FLAGS_PR_SCTP_TTL:
6285                 {
6286                         struct timeval tv;
6287
6288                         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6289                         tv.tv_sec = sp->timetolive / 1000;
6290                         tv.tv_usec = (sp->timetolive * 1000) % 1000000;
6291                         /*
6292                          * TODO sctp_constants.h needs alternative time
6293                          * macros when _KERNEL is undefined.
6294                          */
6295                         timevaladd(&sp->ts, &tv);
6296                 }
6297                 break;
6298         case CHUNK_FLAGS_PR_SCTP_RTX:
6299                 /*
6300                  * Time to live is a the number or retransmissions stored in
6301                  * tv_sec.
6302                  */
6303                 sp->ts.tv_sec = sp->timetolive;
6304                 sp->ts.tv_usec = 0;
6305                 break;
6306         default:
6307                 SCTPDBG(SCTP_DEBUG_USRREQ1,
6308                     "Unknown PR_SCTP policy %u.\n",
6309                     PR_SCTP_POLICY(sp->sinfo_flags));
6310                 break;
6311         }
6312 }
6313
6314 static int
6315 sctp_msg_append(struct sctp_tcb *stcb,
6316     struct sctp_nets *net,
6317     struct mbuf *m,
6318     struct sctp_nonpad_sndrcvinfo *srcv)
6319 {
6320         int error = 0;
6321         struct mbuf *at;
6322         struct sctp_stream_queue_pending *sp = NULL;
6323         struct sctp_stream_out *strm;
6324
6325         SCTP_TCB_LOCK_ASSERT(stcb);
6326
6327         /*
6328          * Given an mbuf chain, put it into the association send queue and
6329          * place it on the wheel
6330          */
6331         if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
6332                 /* Invalid stream number */
6333                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6334                 error = EINVAL;
6335                 goto out_now;
6336         }
6337         if ((stcb->asoc.stream_locked) &&
6338             (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
6339                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6340                 error = EINVAL;
6341                 goto out_now;
6342         }
6343         if ((stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPEN) &&
6344             (stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPENING)) {
6345                 /*
6346                  * Can't queue any data while stream reset is underway.
6347                  */
6348                 if (stcb->asoc.strmout[srcv->sinfo_stream].state > SCTP_STREAM_OPEN) {
6349                         error = EAGAIN;
6350                 } else {
6351                         error = EINVAL;
6352                 }
6353                 goto out_now;
6354         }
6355         /* Now can we send this? */
6356         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
6357             (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6358             (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
6359             (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
6360                 /* got data while shutting down */
6361                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EPIPE);
6362                 error = EPIPE;
6363                 goto out_now;
6364         }
6365         sctp_alloc_a_strmoq(stcb, sp);
6366         if (sp == NULL) {
6367                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6368                 error = ENOMEM;
6369                 goto out_now;
6370         }
6371         sp->sinfo_flags = srcv->sinfo_flags;
6372         sp->timetolive = srcv->sinfo_timetolive;
6373         sp->ppid = srcv->sinfo_ppid;
6374         sp->context = srcv->sinfo_context;
6375         sp->fsn = 0;
6376         if (sp->sinfo_flags & SCTP_ADDR_OVER) {
6377                 sp->net = net;
6378                 atomic_add_int(&sp->net->ref_count, 1);
6379         } else {
6380                 sp->net = NULL;
6381         }
6382         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6383         sp->sid = srcv->sinfo_stream;
6384         sp->msg_is_complete = 1;
6385         sp->sender_all_done = 1;
6386         sp->some_taken = 0;
6387         sp->data = m;
6388         sp->tail_mbuf = NULL;
6389         sctp_set_prsctp_policy(sp);
6390         /*
6391          * We could in theory (for sendall) sifa the length in, but we would
6392          * still have to hunt through the chain since we need to setup the
6393          * tail_mbuf
6394          */
6395         sp->length = 0;
6396         for (at = m; at; at = SCTP_BUF_NEXT(at)) {
6397                 if (SCTP_BUF_NEXT(at) == NULL)
6398                         sp->tail_mbuf = at;
6399                 sp->length += SCTP_BUF_LEN(at);
6400         }
6401         if (srcv->sinfo_keynumber_valid) {
6402                 sp->auth_keyid = srcv->sinfo_keynumber;
6403         } else {
6404                 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
6405         }
6406         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
6407                 sctp_auth_key_acquire(stcb, sp->auth_keyid);
6408                 sp->holds_key_ref = 1;
6409         }
6410         strm = &stcb->asoc.strmout[srcv->sinfo_stream];
6411         sctp_snd_sb_alloc(stcb, sp->length);
6412         atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
6413         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
6414         stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp);
6415         m = NULL;
6416 out_now:
6417         if (m) {
6418                 sctp_m_freem(m);
6419         }
6420         return (error);
6421 }
6422
6423 static struct mbuf *
6424 sctp_copy_mbufchain(struct mbuf *clonechain,
6425     struct mbuf *outchain,
6426     struct mbuf **endofchain,
6427     int can_take_mbuf,
6428     int sizeofcpy,
6429     uint8_t copy_by_ref)
6430 {
6431         struct mbuf *m;
6432         struct mbuf *appendchain;
6433         caddr_t cp;
6434         int len;
6435
6436         if (endofchain == NULL) {
6437                 /* error */
6438 error_out:
6439                 if (outchain)
6440                         sctp_m_freem(outchain);
6441                 return (NULL);
6442         }
6443         if (can_take_mbuf) {
6444                 appendchain = clonechain;
6445         } else {
6446                 if (!copy_by_ref &&
6447                     (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))) {
6448                         /* Its not in a cluster */
6449                         if (*endofchain == NULL) {
6450                                 /* lets get a mbuf cluster */
6451                                 if (outchain == NULL) {
6452                                         /* This is the general case */
6453                         new_mbuf:
6454                                         outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
6455                                         if (outchain == NULL) {
6456                                                 goto error_out;
6457                                         }
6458                                         SCTP_BUF_LEN(outchain) = 0;
6459                                         *endofchain = outchain;
6460                                         /* get the prepend space */
6461                                         SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6462                                 } else {
6463                                         /*
6464                                          * We really should not get a NULL
6465                                          * in endofchain
6466                                          */
6467                                         /* find end */
6468                                         m = outchain;
6469                                         while (m) {
6470                                                 if (SCTP_BUF_NEXT(m) == NULL) {
6471                                                         *endofchain = m;
6472                                                         break;
6473                                                 }
6474                                                 m = SCTP_BUF_NEXT(m);
6475                                         }
6476                                         /* sanity */
6477                                         if (*endofchain == NULL) {
6478                                                 /*
6479                                                  * huh, TSNH XXX maybe we
6480                                                  * should panic
6481                                                  */
6482                                                 sctp_m_freem(outchain);
6483                                                 goto new_mbuf;
6484                                         }
6485                                 }
6486                                 /* get the new end of length */
6487                                 len = (int)M_TRAILINGSPACE(*endofchain);
6488                         } else {
6489                                 /* how much is left at the end? */
6490                                 len = (int)M_TRAILINGSPACE(*endofchain);
6491                         }
6492                         /* Find the end of the data, for appending */
6493                         cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6494
6495                         /* Now lets copy it out */
6496                         if (len >= sizeofcpy) {
6497                                 /* It all fits, copy it in */
6498                                 m_copydata(clonechain, 0, sizeofcpy, cp);
6499                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6500                         } else {
6501                                 /* fill up the end of the chain */
6502                                 if (len > 0) {
6503                                         m_copydata(clonechain, 0, len, cp);
6504                                         SCTP_BUF_LEN((*endofchain)) += len;
6505                                         /* now we need another one */
6506                                         sizeofcpy -= len;
6507                                 }
6508                                 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
6509                                 if (m == NULL) {
6510                                         /* We failed */
6511                                         goto error_out;
6512                                 }
6513                                 SCTP_BUF_NEXT((*endofchain)) = m;
6514                                 *endofchain = m;
6515                                 cp = mtod((*endofchain), caddr_t);
6516                                 m_copydata(clonechain, len, sizeofcpy, cp);
6517                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6518                         }
6519                         return (outchain);
6520                 } else {
6521                         /* copy the old fashion way */
6522                         appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_NOWAIT);
6523 #ifdef SCTP_MBUF_LOGGING
6524                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6525                                 sctp_log_mbc(appendchain, SCTP_MBUF_ICOPY);
6526                         }
6527 #endif
6528                 }
6529         }
6530         if (appendchain == NULL) {
6531                 /* error */
6532                 if (outchain)
6533                         sctp_m_freem(outchain);
6534                 return (NULL);
6535         }
6536         if (outchain) {
6537                 /* tack on to the end */
6538                 if (*endofchain != NULL) {
6539                         SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6540                 } else {
6541                         m = outchain;
6542                         while (m) {
6543                                 if (SCTP_BUF_NEXT(m) == NULL) {
6544                                         SCTP_BUF_NEXT(m) = appendchain;
6545                                         break;
6546                                 }
6547                                 m = SCTP_BUF_NEXT(m);
6548                         }
6549                 }
6550                 /*
6551                  * save off the end and update the end-chain position
6552                  */
6553                 m = appendchain;
6554                 while (m) {
6555                         if (SCTP_BUF_NEXT(m) == NULL) {
6556                                 *endofchain = m;
6557                                 break;
6558                         }
6559                         m = SCTP_BUF_NEXT(m);
6560                 }
6561                 return (outchain);
6562         } else {
6563                 /* save off the end and update the end-chain position */
6564                 m = appendchain;
6565                 while (m) {
6566                         if (SCTP_BUF_NEXT(m) == NULL) {
6567                                 *endofchain = m;
6568                                 break;
6569                         }
6570                         m = SCTP_BUF_NEXT(m);
6571                 }
6572                 return (appendchain);
6573         }
6574 }
6575
6576 static int
6577 sctp_med_chunk_output(struct sctp_inpcb *inp,
6578     struct sctp_tcb *stcb,
6579     struct sctp_association *asoc,
6580     int *num_out,
6581     int *reason_code,
6582     int control_only, int from_where,
6583     struct timeval *now, int *now_filled,
6584     uint32_t frag_point, int so_locked);
6585
6586 static void
6587 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6588     uint32_t val SCTP_UNUSED)
6589 {
6590         struct sctp_copy_all *ca;
6591         struct mbuf *m;
6592         int ret = 0;
6593         int added_control = 0;
6594         int un_sent, do_chunk_output = 1;
6595         struct sctp_association *asoc;
6596         struct sctp_nets *net;
6597
6598         ca = (struct sctp_copy_all *)ptr;
6599         if (ca->m == NULL) {
6600                 return;
6601         }
6602         if (ca->inp != inp) {
6603                 /* TSNH */
6604                 return;
6605         }
6606         if (ca->sndlen > 0) {
6607                 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_NOWAIT);
6608                 if (m == NULL) {
6609                         /* can't copy so we are done */
6610                         ca->cnt_failed++;
6611                         return;
6612                 }
6613 #ifdef SCTP_MBUF_LOGGING
6614                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6615                         sctp_log_mbc(m, SCTP_MBUF_ICOPY);
6616                 }
6617 #endif
6618         } else {
6619                 m = NULL;
6620         }
6621         SCTP_TCB_LOCK_ASSERT(stcb);
6622         if (stcb->asoc.alternate) {
6623                 net = stcb->asoc.alternate;
6624         } else {
6625                 net = stcb->asoc.primary_destination;
6626         }
6627         if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6628                 /* Abort this assoc with m as the user defined reason */
6629                 if (m != NULL) {
6630                         SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_NOWAIT);
6631                 } else {
6632                         m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
6633                             0, M_NOWAIT, 1, MT_DATA);
6634                         SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
6635                 }
6636                 if (m != NULL) {
6637                         struct sctp_paramhdr *ph;
6638
6639                         ph = mtod(m, struct sctp_paramhdr *);
6640                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6641                         ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + ca->sndlen));
6642                 }
6643                 /*
6644                  * We add one here to keep the assoc from dis-appearing on
6645                  * us.
6646                  */
6647                 atomic_add_int(&stcb->asoc.refcnt, 1);
6648                 sctp_abort_an_association(inp, stcb, m, false, SCTP_SO_NOT_LOCKED);
6649                 /*
6650                  * sctp_abort_an_association calls sctp_free_asoc() free
6651                  * association will NOT free it since we incremented the
6652                  * refcnt .. we do this to prevent it being freed and things
6653                  * getting tricky since we could end up (from free_asoc)
6654                  * calling inpcb_free which would get a recursive lock call
6655                  * to the iterator lock.. But as a consequence of that the
6656                  * stcb will return to us un-locked.. since free_asoc
6657                  * returns with either no TCB or the TCB unlocked, we must
6658                  * relock.. to unlock in the iterator timer :-0
6659                  */
6660                 SCTP_TCB_LOCK(stcb);
6661                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
6662                 goto no_chunk_output;
6663         } else {
6664                 if (m != NULL) {
6665                         ret = sctp_msg_append(stcb, net, m, &ca->sndrcv);
6666                 }
6667                 asoc = &stcb->asoc;
6668                 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6669                         /* shutdown this assoc */
6670                         if (TAILQ_EMPTY(&asoc->send_queue) &&
6671                             TAILQ_EMPTY(&asoc->sent_queue) &&
6672                             sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED) == 0) {
6673                                 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
6674                                         goto abort_anyway;
6675                                 }
6676                                 /*
6677                                  * there is nothing queued to send, so I'm
6678                                  * done...
6679                                  */
6680                                 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
6681                                     (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6682                                     (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6683                                         /*
6684                                          * only send SHUTDOWN the first time
6685                                          * through
6686                                          */
6687                                         if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
6688                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6689                                         }
6690                                         SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
6691                                         sctp_stop_timers_for_shutdown(stcb);
6692                                         sctp_send_shutdown(stcb, net);
6693                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6694                                             net);
6695                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6696                                             NULL);
6697                                         added_control = 1;
6698                                         do_chunk_output = 0;
6699                                 }
6700                         } else {
6701                                 /*
6702                                  * we still got (or just got) data to send,
6703                                  * so set SHUTDOWN_PENDING
6704                                  */
6705                                 /*
6706                                  * XXX sockets draft says that SCTP_EOF
6707                                  * should be sent with no data.  currently,
6708                                  * we will allow user data to be sent first
6709                                  * and move to SHUTDOWN-PENDING
6710                                  */
6711                                 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
6712                                     (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6713                                     (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6714                                         if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
6715                                                 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
6716                                         }
6717                                         SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
6718                                         if (TAILQ_EMPTY(&asoc->send_queue) &&
6719                                             TAILQ_EMPTY(&asoc->sent_queue) &&
6720                                             (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6721                                                 struct mbuf *op_err;
6722                                                 char msg[SCTP_DIAG_INFO_LEN];
6723
6724                                 abort_anyway:
6725                                                 SCTP_SNPRINTF(msg, sizeof(msg),
6726                                                     "%s:%d at %s", __FILE__, __LINE__, __func__);
6727                                                 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
6728                                                     msg);
6729                                                 atomic_add_int(&stcb->asoc.refcnt, 1);
6730                                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
6731                                                     op_err, false, SCTP_SO_NOT_LOCKED);
6732                                                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
6733                                                 goto no_chunk_output;
6734                                         }
6735                                 }
6736                         }
6737                 }
6738         }
6739         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6740             (stcb->asoc.stream_queue_cnt * SCTP_DATA_CHUNK_OVERHEAD(stcb)));
6741
6742         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6743             (stcb->asoc.total_flight > 0) &&
6744             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
6745                 do_chunk_output = 0;
6746         }
6747         if (do_chunk_output)
6748                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6749         else if (added_control) {
6750                 struct timeval now;
6751                 int num_out, reason, now_filled = 0;
6752
6753                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6754                     &reason, 1, 1, &now, &now_filled,
6755                     sctp_get_frag_point(stcb),
6756                     SCTP_SO_NOT_LOCKED);
6757         }
6758 no_chunk_output:
6759         if (ret) {
6760                 ca->cnt_failed++;
6761         } else {
6762                 ca->cnt_sent++;
6763         }
6764 }
6765
6766 static void
6767 sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
6768 {
6769         struct sctp_copy_all *ca;
6770
6771         ca = (struct sctp_copy_all *)ptr;
6772         /*
6773          * Do a notify here? Kacheong suggests that the notify be done at
6774          * the send time.. so you would push up a notification if any send
6775          * failed. Don't know if this is feasible since the only failures we
6776          * have is "memory" related and if you cannot get an mbuf to send
6777          * the data you surely can't get an mbuf to send up to notify the
6778          * user you can't send the data :->
6779          */
6780
6781         /* now free everything */
6782         if (ca->inp) {
6783                 /* Lets clear the flag to allow others to run. */
6784                 SCTP_INP_WLOCK(ca->inp);
6785                 ca->inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6786                 SCTP_INP_WUNLOCK(ca->inp);
6787         }
6788         sctp_m_freem(ca->m);
6789         SCTP_FREE(ca, SCTP_M_COPYAL);
6790 }
6791
6792 static struct mbuf *
6793 sctp_copy_out_all(struct uio *uio, ssize_t len)
6794 {
6795         struct mbuf *ret, *at;
6796         ssize_t left, willcpy, cancpy, error;
6797
6798         ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAITOK, 1, MT_DATA);
6799         if (ret == NULL) {
6800                 /* TSNH */
6801                 return (NULL);
6802         }
6803         left = len;
6804         SCTP_BUF_LEN(ret) = 0;
6805         /* save space for the data chunk header */
6806         cancpy = (int)M_TRAILINGSPACE(ret);
6807         willcpy = min(cancpy, left);
6808         at = ret;
6809         while (left > 0) {
6810                 /* Align data to the end */
6811                 error = uiomove(mtod(at, caddr_t), (int)willcpy, uio);
6812                 if (error) {
6813         err_out_now:
6814                         sctp_m_freem(at);
6815                         return (NULL);
6816                 }
6817                 SCTP_BUF_LEN(at) = (int)willcpy;
6818                 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6819                 left -= willcpy;
6820                 if (left > 0) {
6821                         SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg((unsigned int)left, 0, M_WAITOK, 1, MT_DATA);
6822                         if (SCTP_BUF_NEXT(at) == NULL) {
6823                                 goto err_out_now;
6824                         }
6825                         at = SCTP_BUF_NEXT(at);
6826                         SCTP_BUF_LEN(at) = 0;
6827                         cancpy = (int)M_TRAILINGSPACE(at);
6828                         willcpy = min(cancpy, left);
6829                 }
6830         }
6831         return (ret);
6832 }
6833
6834 static int
6835 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6836     struct sctp_nonpad_sndrcvinfo *srcv)
6837 {
6838         int ret;
6839         struct sctp_copy_all *ca;
6840
6841         if (uio->uio_resid > (ssize_t)SCTP_BASE_SYSCTL(sctp_sendall_limit)) {
6842                 /* You must not be larger than the limit! */
6843                 return (EMSGSIZE);
6844         }
6845         SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6846             SCTP_M_COPYAL);
6847         if (ca == NULL) {
6848                 sctp_m_freem(m);
6849                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6850                 return (ENOMEM);
6851         }
6852         memset(ca, 0, sizeof(struct sctp_copy_all));
6853
6854         ca->inp = inp;
6855         if (srcv != NULL) {
6856                 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6857         }
6858
6859         /* Serialize. */
6860         SCTP_INP_WLOCK(inp);
6861         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SND_ITERATOR_UP) != 0) {
6862                 SCTP_INP_WUNLOCK(inp);
6863                 sctp_m_freem(m);
6864                 SCTP_FREE(ca, SCTP_M_COPYAL);
6865                 return (EBUSY);
6866         }
6867         inp->sctp_flags |= SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6868         SCTP_INP_WUNLOCK(inp);
6869
6870         /*
6871          * take off the sendall flag, it would be bad if we failed to do
6872          * this :-0
6873          */
6874         ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6875         /* get length and mbuf chain */
6876         if (uio) {
6877                 ca->sndlen = uio->uio_resid;
6878                 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6879                 if (ca->m == NULL) {
6880                         SCTP_FREE(ca, SCTP_M_COPYAL);
6881                         sctp_m_freem(m);
6882                         SCTP_INP_WLOCK(inp);
6883                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6884                         SCTP_INP_WUNLOCK(inp);
6885                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6886                         return (ENOMEM);
6887                 }
6888         } else {
6889                 /* Gather the length of the send */
6890                 struct mbuf *mat;
6891
6892                 ca->sndlen = 0;
6893                 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6894                         ca->sndlen += SCTP_BUF_LEN(mat);
6895                 }
6896         }
6897         ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6898             SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6899             SCTP_ASOC_ANY_STATE,
6900             (void *)ca, 0,
6901             sctp_sendall_completes, inp, 1);
6902         if (ret) {
6903                 SCTP_INP_WLOCK(inp);
6904                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6905                 SCTP_INP_WUNLOCK(inp);
6906                 SCTP_FREE(ca, SCTP_M_COPYAL);
6907                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6908                 return (EFAULT);
6909         }
6910         return (0);
6911 }
6912
6913 void
6914 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6915 {
6916         struct sctp_tmit_chunk *chk, *nchk;
6917
6918         TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6919                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6920                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6921                         asoc->ctrl_queue_cnt--;
6922                         if (chk->data) {
6923                                 sctp_m_freem(chk->data);
6924                                 chk->data = NULL;
6925                         }
6926                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6927                 }
6928         }
6929 }
6930
6931 void
6932 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6933 {
6934         struct sctp_association *asoc;
6935         struct sctp_tmit_chunk *chk, *nchk;
6936         struct sctp_asconf_chunk *acp;
6937
6938         asoc = &stcb->asoc;
6939         TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6940                 /* find SCTP_ASCONF chunk in queue */
6941                 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6942                         if (chk->data) {
6943                                 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6944                                 if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6945                                         /* Not Acked yet */
6946                                         break;
6947                                 }
6948                         }
6949                         TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6950                         asoc->ctrl_queue_cnt--;
6951                         if (chk->data) {
6952                                 sctp_m_freem(chk->data);
6953                                 chk->data = NULL;
6954                         }
6955                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6956                 }
6957         }
6958 }
6959
6960 static void
6961 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6962     struct sctp_association *asoc,
6963     struct sctp_tmit_chunk **data_list,
6964     int bundle_at,
6965     struct sctp_nets *net)
6966 {
6967         int i;
6968         struct sctp_tmit_chunk *tp1;
6969
6970         for (i = 0; i < bundle_at; i++) {
6971                 /* off of the send queue */
6972                 TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
6973                 asoc->send_queue_cnt--;
6974                 if (i > 0) {
6975                         /*
6976                          * Any chunk NOT 0 you zap the time chunk 0 gets
6977                          * zapped or set based on if a RTO measurement is
6978                          * needed.
6979                          */
6980                         data_list[i]->do_rtt = 0;
6981                 }
6982                 /* record time */
6983                 data_list[i]->sent_rcv_time = net->last_sent_time;
6984                 data_list[i]->rec.data.cwnd_at_send = net->cwnd;
6985                 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.tsn;
6986                 if (data_list[i]->whoTo == NULL) {
6987                         data_list[i]->whoTo = net;
6988                         atomic_add_int(&net->ref_count, 1);
6989                 }
6990                 /* on to the sent queue */
6991                 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6992                 if ((tp1) && SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
6993                         struct sctp_tmit_chunk *tpp;
6994
6995                         /* need to move back */
6996         back_up_more:
6997                         tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6998                         if (tpp == NULL) {
6999                                 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
7000                                 goto all_done;
7001                         }
7002                         tp1 = tpp;
7003                         if (SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
7004                                 goto back_up_more;
7005                         }
7006                         TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
7007                 } else {
7008                         TAILQ_INSERT_TAIL(&asoc->sent_queue,
7009                             data_list[i],
7010                             sctp_next);
7011                 }
7012 all_done:
7013                 /* This does not lower until the cum-ack passes it */
7014                 asoc->sent_queue_cnt++;
7015                 if ((asoc->peers_rwnd <= 0) &&
7016                     (asoc->total_flight == 0) &&
7017                     (bundle_at == 1)) {
7018                         /* Mark the chunk as being a window probe */
7019                         SCTP_STAT_INCR(sctps_windowprobed);
7020                 }
7021 #ifdef SCTP_AUDITING_ENABLED
7022                 sctp_audit_log(0xC2, 3);
7023 #endif
7024                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
7025                 data_list[i]->snd_count = 1;
7026                 data_list[i]->rec.data.chunk_was_revoked = 0;
7027                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7028                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
7029                             data_list[i]->whoTo->flight_size,
7030                             data_list[i]->book_size,
7031                             (uint32_t)(uintptr_t)data_list[i]->whoTo,
7032                             data_list[i]->rec.data.tsn);
7033                 }
7034                 sctp_flight_size_increase(data_list[i]);
7035                 sctp_total_flight_increase(stcb, data_list[i]);
7036                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7037                         sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
7038                             asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
7039                 }
7040                 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
7041                     (uint32_t)(data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
7042                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7043                         /* SWS sender side engages */
7044                         asoc->peers_rwnd = 0;
7045                 }
7046         }
7047         if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
7048                 (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
7049         }
7050 }
7051
7052 static void
7053 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked)
7054 {
7055         struct sctp_tmit_chunk *chk, *nchk;
7056
7057         TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
7058                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7059                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
7060                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7061                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7062                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
7063                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7064                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7065                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7066                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7067                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7068                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7069                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7070                         /* Stray chunks must be cleaned up */
7071         clean_up_anyway:
7072                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7073                         asoc->ctrl_queue_cnt--;
7074                         if (chk->data) {
7075                                 sctp_m_freem(chk->data);
7076                                 chk->data = NULL;
7077                         }
7078                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
7079                                 asoc->fwd_tsn_cnt--;
7080                         }
7081                         sctp_free_a_chunk(stcb, chk, so_locked);
7082                 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
7083                         /* special handling, we must look into the param */
7084                         if (chk != asoc->str_reset) {
7085                                 goto clean_up_anyway;
7086                         }
7087                 }
7088         }
7089 }
7090
7091 static uint32_t
7092 sctp_can_we_split_this(struct sctp_tcb *stcb, uint32_t length,
7093     uint32_t space_left, uint32_t frag_point, int eeor_on)
7094 {
7095         /*
7096          * Make a decision on if I should split a msg into multiple parts.
7097          * This is only asked of incomplete messages.
7098          */
7099         if (eeor_on) {
7100                 /*
7101                  * If we are doing EEOR we need to always send it if its the
7102                  * entire thing, since it might be all the guy is putting in
7103                  * the hopper.
7104                  */
7105                 if (space_left >= length) {
7106                         /*-
7107                          * If we have data outstanding,
7108                          * we get another chance when the sack
7109                          * arrives to transmit - wait for more data
7110                          */
7111                         if (stcb->asoc.total_flight == 0) {
7112                                 /*
7113                                  * If nothing is in flight, we zero the
7114                                  * packet counter.
7115                                  */
7116                                 return (length);
7117                         }
7118                         return (0);
7119
7120                 } else {
7121                         /* You can fill the rest */
7122                         return (space_left);
7123                 }
7124         }
7125         /*-
7126          * For those strange folk that make the send buffer
7127          * smaller than our fragmentation point, we can't
7128          * get a full msg in so we have to allow splitting.
7129          */
7130         if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
7131                 return (length);
7132         }
7133         if ((length <= space_left) ||
7134             ((length - space_left) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
7135                 /* Sub-optimal residual don't split in non-eeor mode. */
7136                 return (0);
7137         }
7138         /*
7139          * If we reach here length is larger than the space_left. Do we wish
7140          * to split it for the sake of packet putting together?
7141          */
7142         if (space_left >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
7143                 /* Its ok to split it */
7144                 return (min(space_left, frag_point));
7145         }
7146         /* Nope, can't split */
7147         return (0);
7148 }
7149
7150 static uint32_t
7151 sctp_move_to_outqueue(struct sctp_tcb *stcb,
7152     struct sctp_nets *net,
7153     struct sctp_stream_out *strq,
7154     uint32_t space_left,
7155     uint32_t frag_point,
7156     int *giveup,
7157     int eeor_mode,
7158     int *bail,
7159     int so_locked)
7160 {
7161         /* Move from the stream to the send_queue keeping track of the total */
7162         struct sctp_association *asoc;
7163         struct sctp_stream_queue_pending *sp;
7164         struct sctp_tmit_chunk *chk;
7165         struct sctp_data_chunk *dchkh = NULL;
7166         struct sctp_idata_chunk *ndchkh = NULL;
7167         uint32_t to_move, length;
7168         int leading;
7169         uint8_t rcv_flags = 0;
7170         uint8_t some_taken;
7171
7172         SCTP_TCB_LOCK_ASSERT(stcb);
7173         asoc = &stcb->asoc;
7174 one_more_time:
7175         /* sa_ignore FREED_MEMORY */
7176         sp = TAILQ_FIRST(&strq->outqueue);
7177         if (sp == NULL) {
7178                 sp = TAILQ_FIRST(&strq->outqueue);
7179                 if (sp) {
7180                         goto one_more_time;
7181                 }
7182                 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_EXPLICIT_EOR) == 0) &&
7183                     (stcb->asoc.idata_supported == 0) &&
7184                     (strq->last_msg_incomplete)) {
7185                         SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
7186                             strq->sid,
7187                             strq->last_msg_incomplete);
7188                         strq->last_msg_incomplete = 0;
7189                 }
7190                 to_move = 0;
7191                 goto out_of;
7192         }
7193         if ((sp->msg_is_complete) && (sp->length == 0)) {
7194                 if (sp->sender_all_done) {
7195                         /*
7196                          * We are doing deferred cleanup. Last time through
7197                          * when we took all the data the sender_all_done was
7198                          * not set.
7199                          */
7200                         if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
7201                                 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
7202                                 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7203                                     sp->sender_all_done,
7204                                     sp->length,
7205                                     sp->msg_is_complete,
7206                                     sp->put_last_out);
7207                         }
7208                         atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7209                         TAILQ_REMOVE(&strq->outqueue, sp, next);
7210                         stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7211                         if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7212                             (strq->chunks_on_queues == 0) &&
7213                             TAILQ_EMPTY(&strq->outqueue)) {
7214                                 stcb->asoc.trigger_reset = 1;
7215                         }
7216                         if (sp->net) {
7217                                 sctp_free_remote_addr(sp->net);
7218                                 sp->net = NULL;
7219                         }
7220                         if (sp->data) {
7221                                 sctp_m_freem(sp->data);
7222                                 sp->data = NULL;
7223                         }
7224                         sctp_free_a_strmoq(stcb, sp, so_locked);
7225                         /* back to get the next msg */
7226                         goto one_more_time;
7227                 } else {
7228                         /*
7229                          * sender just finished this but still holds a
7230                          * reference
7231                          */
7232                         *giveup = 1;
7233                         to_move = 0;
7234                         goto out_of;
7235                 }
7236         } else {
7237                 /* is there some to get */
7238                 if (sp->length == 0) {
7239                         /* no */
7240                         *giveup = 1;
7241                         to_move = 0;
7242                         goto out_of;
7243                 } else if (sp->discard_rest) {
7244                         /* Whack down the size */
7245                         atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
7246                         if ((stcb->sctp_socket != NULL) &&
7247                             ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7248                             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
7249                                 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
7250                         }
7251                         if (sp->data) {
7252                                 sctp_m_freem(sp->data);
7253                                 sp->data = NULL;
7254                                 sp->tail_mbuf = NULL;
7255                         }
7256                         sp->length = 0;
7257                         sp->some_taken = 1;
7258                         *giveup = 1;
7259                         to_move = 0;
7260                         goto out_of;
7261                 }
7262         }
7263         some_taken = sp->some_taken;
7264         length = sp->length;
7265         if (sp->msg_is_complete) {
7266                 /* The message is complete */
7267                 to_move = min(length, frag_point);
7268                 if (to_move == length) {
7269                         /* All of it fits in the MTU */
7270                         if (sp->some_taken) {
7271                                 rcv_flags |= SCTP_DATA_LAST_FRAG;
7272                         } else {
7273                                 rcv_flags |= SCTP_DATA_NOT_FRAG;
7274                         }
7275                         sp->put_last_out = 1;
7276                         if (sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) {
7277                                 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7278                         }
7279                 } else {
7280                         /* Not all of it fits, we fragment */
7281                         if (sp->some_taken == 0) {
7282                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7283                         }
7284                         sp->some_taken = 1;
7285                 }
7286         } else {
7287                 to_move = sctp_can_we_split_this(stcb, length, space_left, frag_point, eeor_mode);
7288                 if (to_move > 0) {
7289                         if (to_move >= length) {
7290                                 to_move = length;
7291                         }
7292                         if (sp->some_taken == 0) {
7293                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7294                                 sp->some_taken = 1;
7295                         }
7296                 } else {
7297                         /* Nothing to take. */
7298                         *giveup = 1;
7299                         to_move = 0;
7300                         goto out_of;
7301                 }
7302         }
7303
7304         /* If we reach here, we can copy out a chunk */
7305         sctp_alloc_a_chunk(stcb, chk);
7306         if (chk == NULL) {
7307                 /* No chunk memory */
7308                 *giveup = 1;
7309                 to_move = 0;
7310                 goto out_of;
7311         }
7312         /*
7313          * Setup for unordered if needed by looking at the user sent info
7314          * flags.
7315          */
7316         if (sp->sinfo_flags & SCTP_UNORDERED) {
7317                 rcv_flags |= SCTP_DATA_UNORDERED;
7318         }
7319         if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
7320             (sp->sinfo_flags & SCTP_EOF) == SCTP_EOF) {
7321                 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7322         }
7323         /* clear out the chunk before setting up */
7324         memset(chk, 0, sizeof(*chk));
7325         chk->rec.data.rcv_flags = rcv_flags;
7326
7327         if (to_move >= length) {
7328                 /* we think we can steal the whole thing */
7329                 if (to_move < sp->length) {
7330                         /* bail, it changed */
7331                         goto dont_do_it;
7332                 }
7333                 chk->data = sp->data;
7334                 chk->last_mbuf = sp->tail_mbuf;
7335                 /* register the stealing */
7336                 sp->data = sp->tail_mbuf = NULL;
7337         } else {
7338                 struct mbuf *m;
7339
7340 dont_do_it:
7341                 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_NOWAIT);
7342                 chk->last_mbuf = NULL;
7343                 if (chk->data == NULL) {
7344                         sp->some_taken = some_taken;
7345                         sctp_free_a_chunk(stcb, chk, so_locked);
7346                         *bail = 1;
7347                         to_move = 0;
7348                         goto out_of;
7349                 }
7350 #ifdef SCTP_MBUF_LOGGING
7351                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7352                         sctp_log_mbc(chk->data, SCTP_MBUF_ICOPY);
7353                 }
7354 #endif
7355                 /* Pull off the data */
7356                 m_adj(sp->data, to_move);
7357                 /* Now lets work our way down and compact it */
7358                 m = sp->data;
7359                 while (m && (SCTP_BUF_LEN(m) == 0)) {
7360                         sp->data = SCTP_BUF_NEXT(m);
7361                         SCTP_BUF_NEXT(m) = NULL;
7362                         if (sp->tail_mbuf == m) {
7363                                 /*-
7364                                  * Freeing tail? TSNH since
7365                                  * we supposedly were taking less
7366                                  * than the sp->length.
7367                                  */
7368 #ifdef INVARIANTS
7369                                 panic("Huh, freeing tail? - TSNH");
7370 #else
7371                                 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
7372                                 sp->tail_mbuf = sp->data = NULL;
7373                                 sp->length = 0;
7374 #endif
7375                         }
7376                         sctp_m_free(m);
7377                         m = sp->data;
7378                 }
7379         }
7380         if (SCTP_BUF_IS_EXTENDED(chk->data)) {
7381                 chk->copy_by_ref = 1;
7382         } else {
7383                 chk->copy_by_ref = 0;
7384         }
7385         /*
7386          * get last_mbuf and counts of mb usage This is ugly but hopefully
7387          * its only one mbuf.
7388          */
7389         if (chk->last_mbuf == NULL) {
7390                 chk->last_mbuf = chk->data;
7391                 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7392                         chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7393                 }
7394         }
7395
7396         if (to_move > length) {
7397                 /*- This should not happen either
7398                  * since we always lower to_move to the size
7399                  * of sp->length if its larger.
7400                  */
7401 #ifdef INVARIANTS
7402                 panic("Huh, how can to_move be larger?");
7403 #else
7404                 SCTP_PRINTF("Huh, how can to_move be larger?\n");
7405                 sp->length = 0;
7406 #endif
7407         } else {
7408                 atomic_subtract_int(&sp->length, to_move);
7409         }
7410         leading = SCTP_DATA_CHUNK_OVERHEAD(stcb);
7411         if (M_LEADINGSPACE(chk->data) < leading) {
7412                 /* Not enough room for a chunk header, get some */
7413                 struct mbuf *m;
7414
7415                 m = sctp_get_mbuf_for_msg(1, 0, M_NOWAIT, 1, MT_DATA);
7416                 if (m == NULL) {
7417                         /*
7418                          * we're in trouble here. _PREPEND below will free
7419                          * all the data if there is no leading space, so we
7420                          * must put the data back and restore.
7421                          */
7422                         if (sp->data == NULL) {
7423                                 /* unsteal the data */
7424                                 sp->data = chk->data;
7425                                 sp->tail_mbuf = chk->last_mbuf;
7426                         } else {
7427                                 struct mbuf *m_tmp;
7428
7429                                 /* reassemble the data */
7430                                 m_tmp = sp->data;
7431                                 sp->data = chk->data;
7432                                 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7433                         }
7434                         sp->some_taken = some_taken;
7435                         atomic_add_int(&sp->length, to_move);
7436                         chk->data = NULL;
7437                         *bail = 1;
7438                         sctp_free_a_chunk(stcb, chk, so_locked);
7439                         to_move = 0;
7440                         goto out_of;
7441                 } else {
7442                         SCTP_BUF_LEN(m) = 0;
7443                         SCTP_BUF_NEXT(m) = chk->data;
7444                         chk->data = m;
7445                         M_ALIGN(chk->data, 4);
7446                 }
7447         }
7448         SCTP_BUF_PREPEND(chk->data, SCTP_DATA_CHUNK_OVERHEAD(stcb), M_NOWAIT);
7449         if (chk->data == NULL) {
7450                 /* HELP, TSNH since we assured it would not above? */
7451 #ifdef INVARIANTS
7452                 panic("prepend fails HELP?");
7453 #else
7454                 SCTP_PRINTF("prepend fails HELP?\n");
7455                 sctp_free_a_chunk(stcb, chk, so_locked);
7456 #endif
7457                 *bail = 1;
7458                 to_move = 0;
7459                 goto out_of;
7460         }
7461         sctp_snd_sb_alloc(stcb, SCTP_DATA_CHUNK_OVERHEAD(stcb));
7462         chk->book_size = chk->send_size = (uint16_t)(to_move + SCTP_DATA_CHUNK_OVERHEAD(stcb));
7463         chk->book_size_scale = 0;
7464         chk->sent = SCTP_DATAGRAM_UNSENT;
7465
7466         chk->flags = 0;
7467         chk->asoc = &stcb->asoc;
7468         chk->pad_inplace = 0;
7469         chk->no_fr_allowed = 0;
7470         if (stcb->asoc.idata_supported == 0) {
7471                 if (rcv_flags & SCTP_DATA_UNORDERED) {
7472                         /* Just use 0. The receiver ignores the values. */
7473                         chk->rec.data.mid = 0;
7474                 } else {
7475                         chk->rec.data.mid = strq->next_mid_ordered;
7476                         if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7477                                 strq->next_mid_ordered++;
7478                         }
7479                 }
7480         } else {
7481                 if (rcv_flags & SCTP_DATA_UNORDERED) {
7482                         chk->rec.data.mid = strq->next_mid_unordered;
7483                         if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7484                                 strq->next_mid_unordered++;
7485                         }
7486                 } else {
7487                         chk->rec.data.mid = strq->next_mid_ordered;
7488                         if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7489                                 strq->next_mid_ordered++;
7490                         }
7491                 }
7492         }
7493         chk->rec.data.sid = sp->sid;
7494         chk->rec.data.ppid = sp->ppid;
7495         chk->rec.data.context = sp->context;
7496         chk->rec.data.doing_fast_retransmit = 0;
7497
7498         chk->rec.data.timetodrop = sp->ts;
7499         chk->flags = sp->act_flags;
7500
7501         if (sp->net) {
7502                 chk->whoTo = sp->net;
7503                 atomic_add_int(&chk->whoTo->ref_count, 1);
7504         } else
7505                 chk->whoTo = NULL;
7506
7507         if (sp->holds_key_ref) {
7508                 chk->auth_keyid = sp->auth_keyid;
7509                 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7510                 chk->holds_key_ref = 1;
7511         }
7512         stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, to_move);
7513         chk->rec.data.tsn = atomic_fetchadd_int(&asoc->sending_seq, 1);
7514         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7515                 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7516                     (uint32_t)(uintptr_t)stcb, sp->length,
7517                     (uint32_t)((chk->rec.data.sid << 16) | (0x0000ffff & chk->rec.data.mid)),
7518                     chk->rec.data.tsn);
7519         }
7520         if (stcb->asoc.idata_supported == 0) {
7521                 dchkh = mtod(chk->data, struct sctp_data_chunk *);
7522         } else {
7523                 ndchkh = mtod(chk->data, struct sctp_idata_chunk *);
7524         }
7525         /*
7526          * Put the rest of the things in place now. Size was done earlier in
7527          * previous loop prior to padding.
7528          */
7529
7530         SCTP_TCB_LOCK_ASSERT(stcb);
7531 #ifdef SCTP_ASOCLOG_OF_TSNS
7532         if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7533                 asoc->tsn_out_at = 0;
7534                 asoc->tsn_out_wrapped = 1;
7535         }
7536         asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.tsn;
7537         asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.sid;
7538         asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.mid;
7539         asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7540         asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7541         asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7542         asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7543         asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7544         asoc->tsn_out_at++;
7545 #endif
7546         if (stcb->asoc.idata_supported == 0) {
7547                 dchkh->ch.chunk_type = SCTP_DATA;
7548                 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7549                 dchkh->dp.tsn = htonl(chk->rec.data.tsn);
7550                 dchkh->dp.sid = htons(strq->sid);
7551                 dchkh->dp.ssn = htons((uint16_t)chk->rec.data.mid);
7552                 dchkh->dp.ppid = chk->rec.data.ppid;
7553                 dchkh->ch.chunk_length = htons(chk->send_size);
7554         } else {
7555                 ndchkh->ch.chunk_type = SCTP_IDATA;
7556                 ndchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7557                 ndchkh->dp.tsn = htonl(chk->rec.data.tsn);
7558                 ndchkh->dp.sid = htons(strq->sid);
7559                 ndchkh->dp.reserved = htons(0);
7560                 ndchkh->dp.mid = htonl(chk->rec.data.mid);
7561                 if (sp->fsn == 0)
7562                         ndchkh->dp.ppid_fsn.ppid = chk->rec.data.ppid;
7563                 else
7564                         ndchkh->dp.ppid_fsn.fsn = htonl(sp->fsn);
7565                 sp->fsn++;
7566                 ndchkh->ch.chunk_length = htons(chk->send_size);
7567         }
7568         /* Now advance the chk->send_size by the actual pad needed. */
7569         if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7570                 /* need a pad */
7571                 struct mbuf *lm;
7572                 int pads;
7573
7574                 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7575                 lm = sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf);
7576                 if (lm != NULL) {
7577                         chk->last_mbuf = lm;
7578                         chk->pad_inplace = 1;
7579                 }
7580                 chk->send_size += pads;
7581         }
7582         if (PR_SCTP_ENABLED(chk->flags)) {
7583                 asoc->pr_sctp_cnt++;
7584         }
7585         if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7586                 /* All done pull and kill the message */
7587                 if (sp->put_last_out == 0) {
7588                         SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7589                         SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7590                             sp->sender_all_done,
7591                             sp->length,
7592                             sp->msg_is_complete,
7593                             sp->put_last_out);
7594                 }
7595                 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7596                 TAILQ_REMOVE(&strq->outqueue, sp, next);
7597                 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7598                 if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7599                     (strq->chunks_on_queues == 0) &&
7600                     TAILQ_EMPTY(&strq->outqueue)) {
7601                         stcb->asoc.trigger_reset = 1;
7602                 }
7603                 if (sp->net) {
7604                         sctp_free_remote_addr(sp->net);
7605                         sp->net = NULL;
7606                 }
7607                 if (sp->data) {
7608                         sctp_m_freem(sp->data);
7609                         sp->data = NULL;
7610                 }
7611                 sctp_free_a_strmoq(stcb, sp, so_locked);
7612         }
7613         asoc->chunks_on_out_queue++;
7614         strq->chunks_on_queues++;
7615         TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7616         asoc->send_queue_cnt++;
7617 out_of:
7618         return (to_move);
7619 }
7620
7621 static void
7622 sctp_fill_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
7623     uint32_t frag_point, int eeor_mode, int *quit_now,
7624     int so_locked)
7625 {
7626         struct sctp_association *asoc;
7627         struct sctp_stream_out *strq;
7628         uint32_t space_left, moved, total_moved;
7629         int bail, giveup;
7630
7631         SCTP_TCB_LOCK_ASSERT(stcb);
7632         asoc = &stcb->asoc;
7633         total_moved = 0;
7634         switch (net->ro._l_addr.sa.sa_family) {
7635 #ifdef INET
7636         case AF_INET:
7637                 space_left = net->mtu - SCTP_MIN_V4_OVERHEAD;
7638                 break;
7639 #endif
7640 #ifdef INET6
7641         case AF_INET6:
7642                 space_left = net->mtu - SCTP_MIN_OVERHEAD;
7643                 break;
7644 #endif
7645         default:
7646                 /* TSNH */
7647                 space_left = net->mtu;
7648                 break;
7649         }
7650         /* Need an allowance for the data chunk header too */
7651         space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
7652
7653         /* must make even word boundary */
7654         space_left &= 0xfffffffc;
7655         strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7656         giveup = 0;
7657         bail = 0;
7658         while ((space_left > 0) && (strq != NULL)) {
7659                 moved = sctp_move_to_outqueue(stcb, net, strq, space_left,
7660                     frag_point, &giveup, eeor_mode,
7661                     &bail, so_locked);
7662                 if ((giveup != 0) || (bail != 0)) {
7663                         break;
7664                 }
7665                 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7666                 total_moved += moved;
7667                 if (space_left >= moved) {
7668                         space_left -= moved;
7669                 } else {
7670                         space_left = 0;
7671                 }
7672                 if (space_left >= SCTP_DATA_CHUNK_OVERHEAD(stcb)) {
7673                         space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
7674                 } else {
7675                         space_left = 0;
7676                 }
7677                 space_left &= 0xfffffffc;
7678         }
7679         if (bail != 0)
7680                 *quit_now = 1;
7681
7682         stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7683
7684         if (total_moved == 0) {
7685                 if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7686                     (net == stcb->asoc.primary_destination)) {
7687                         /* ran dry for primary network net */
7688                         SCTP_STAT_INCR(sctps_primary_randry);
7689                 } else if (stcb->asoc.sctp_cmt_on_off > 0) {
7690                         /* ran dry with CMT on */
7691                         SCTP_STAT_INCR(sctps_cmt_randry);
7692                 }
7693         }
7694 }
7695
7696 void
7697 sctp_fix_ecn_echo(struct sctp_association *asoc)
7698 {
7699         struct sctp_tmit_chunk *chk;
7700
7701         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7702                 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7703                         chk->sent = SCTP_DATAGRAM_UNSENT;
7704                 }
7705         }
7706 }
7707
7708 void
7709 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7710 {
7711         struct sctp_association *asoc;
7712         struct sctp_tmit_chunk *chk;
7713         struct sctp_stream_queue_pending *sp;
7714         unsigned int i;
7715
7716         if (net == NULL) {
7717                 return;
7718         }
7719         asoc = &stcb->asoc;
7720         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7721                 TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7722                         if (sp->net == net) {
7723                                 sctp_free_remote_addr(sp->net);
7724                                 sp->net = NULL;
7725                         }
7726                 }
7727         }
7728         TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7729                 if (chk->whoTo == net) {
7730                         sctp_free_remote_addr(chk->whoTo);
7731                         chk->whoTo = NULL;
7732                 }
7733         }
7734 }
7735
7736 int
7737 sctp_med_chunk_output(struct sctp_inpcb *inp,
7738     struct sctp_tcb *stcb,
7739     struct sctp_association *asoc,
7740     int *num_out,
7741     int *reason_code,
7742     int control_only, int from_where,
7743     struct timeval *now, int *now_filled,
7744     uint32_t frag_point, int so_locked)
7745 {
7746         /**
7747          * Ok this is the generic chunk service queue. we must do the
7748          * following:
7749          * - Service the stream queue that is next, moving any
7750          *   message (note I must get a complete message i.e. FIRST/MIDDLE and
7751          *   LAST to the out queue in one pass) and assigning TSN's. This
7752          *   only applies though if the peer does not support NDATA. For NDATA
7753          *   chunks its ok to not send the entire message ;-)
7754          * - Check to see if the cwnd/rwnd allows any output, if so we go ahead and
7755          *   formulate and send the low level chunks. Making sure to combine
7756          *   any control in the control chunk queue also.
7757          */
7758         struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7759         struct mbuf *outchain, *endoutchain;
7760         struct sctp_tmit_chunk *chk, *nchk;
7761
7762         /* temp arrays for unlinking */
7763         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7764         int no_fragmentflg, error;
7765         unsigned int max_rwnd_per_dest, max_send_per_dest;
7766         int one_chunk, hbflag, skip_data_for_this_net;
7767         int asconf, cookie, no_out_cnt;
7768         int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7769         unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7770         int tsns_sent = 0;
7771         uint32_t auth_offset;
7772         struct sctp_auth_chunk *auth;
7773         uint16_t auth_keyid;
7774         int override_ok = 1;
7775         int skip_fill_up = 0;
7776         int data_auth_reqd = 0;
7777
7778         /*
7779          * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7780          * destination.
7781          */
7782         int quit_now = 0;
7783
7784         *num_out = 0;
7785         *reason_code = 0;
7786         auth_keyid = stcb->asoc.authinfo.active_keyid;
7787         if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7788             (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7789             (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7790                 eeor_mode = 1;
7791         } else {
7792                 eeor_mode = 0;
7793         }
7794         ctl_cnt = no_out_cnt = asconf = cookie = 0;
7795         /*
7796          * First lets prime the pump. For each destination, if there is room
7797          * in the flight size, attempt to pull an MTU's worth out of the
7798          * stream queues into the general send_queue
7799          */
7800 #ifdef SCTP_AUDITING_ENABLED
7801         sctp_audit_log(0xC2, 2);
7802 #endif
7803         SCTP_TCB_LOCK_ASSERT(stcb);
7804         hbflag = 0;
7805         if (control_only)
7806                 no_data_chunks = 1;
7807         else
7808                 no_data_chunks = 0;
7809
7810         /* Nothing to possible to send? */
7811         if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7812             (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7813             TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7814             TAILQ_EMPTY(&asoc->send_queue) &&
7815             sctp_is_there_unsent_data(stcb, so_locked) == 0) {
7816 nothing_to_send:
7817                 *reason_code = 9;
7818                 return (0);
7819         }
7820         if (asoc->peers_rwnd == 0) {
7821                 /* No room in peers rwnd */
7822                 *reason_code = 1;
7823                 if (asoc->total_flight > 0) {
7824                         /* we are allowed one chunk in flight */
7825                         no_data_chunks = 1;
7826                 }
7827         }
7828         if (stcb->asoc.ecn_echo_cnt_onq) {
7829                 /* Record where a sack goes, if any */
7830                 if (no_data_chunks &&
7831                     (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7832                         /* Nothing but ECNe to send - we don't do that */
7833                         goto nothing_to_send;
7834                 }
7835                 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7836                         if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7837                             (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7838                                 sack_goes_to = chk->whoTo;
7839                                 break;
7840                         }
7841                 }
7842         }
7843         max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7844         if (stcb->sctp_socket)
7845                 max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7846         else
7847                 max_send_per_dest = 0;
7848         if (no_data_chunks == 0) {
7849                 /* How many non-directed chunks are there? */
7850                 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7851                         if (chk->whoTo == NULL) {
7852                                 /*
7853                                  * We already have non-directed chunks on
7854                                  * the queue, no need to do a fill-up.
7855                                  */
7856                                 skip_fill_up = 1;
7857                                 break;
7858                         }
7859                 }
7860         }
7861         if ((no_data_chunks == 0) &&
7862             (skip_fill_up == 0) &&
7863             (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7864                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7865                         /*
7866                          * This for loop we are in takes in each net, if
7867                          * its's got space in cwnd and has data sent to it
7868                          * (when CMT is off) then it calls
7869                          * sctp_fill_outqueue for the net. This gets data on
7870                          * the send queue for that network.
7871                          *
7872                          * In sctp_fill_outqueue TSN's are assigned and data
7873                          * is copied out of the stream buffers. Note mostly
7874                          * copy by reference (we hope).
7875                          */
7876                         net->window_probe = 0;
7877                         if ((net != stcb->asoc.alternate) &&
7878                             ((net->dest_state & SCTP_ADDR_PF) ||
7879                             ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) ||
7880                             (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
7881                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7882                                         sctp_log_cwnd(stcb, net, 1,
7883                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7884                                 }
7885                                 continue;
7886                         }
7887                         if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
7888                             (net->flight_size == 0)) {
7889                                 (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
7890                         }
7891                         if (net->flight_size >= net->cwnd) {
7892                                 /* skip this network, no room - can't fill */
7893                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7894                                         sctp_log_cwnd(stcb, net, 3,
7895                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7896                                 }
7897                                 continue;
7898                         }
7899                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7900                                 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7901                         }
7902                         sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
7903                         if (quit_now) {
7904                                 /* memory alloc failure */
7905                                 no_data_chunks = 1;
7906                                 break;
7907                         }
7908                 }
7909         }
7910         /* now service each destination and send out what we can for it */
7911         /* Nothing to send? */
7912         if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7913             TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7914             TAILQ_EMPTY(&asoc->send_queue)) {
7915                 *reason_code = 8;
7916                 return (0);
7917         }
7918
7919         if (asoc->sctp_cmt_on_off > 0) {
7920                 /* get the last start point */
7921                 start_at = asoc->last_net_cmt_send_started;
7922                 if (start_at == NULL) {
7923                         /* null so to beginning */
7924                         start_at = TAILQ_FIRST(&asoc->nets);
7925                 } else {
7926                         start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7927                         if (start_at == NULL) {
7928                                 start_at = TAILQ_FIRST(&asoc->nets);
7929                         }
7930                 }
7931                 asoc->last_net_cmt_send_started = start_at;
7932         } else {
7933                 start_at = TAILQ_FIRST(&asoc->nets);
7934         }
7935         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7936                 if (chk->whoTo == NULL) {
7937                         if (asoc->alternate) {
7938                                 chk->whoTo = asoc->alternate;
7939                         } else {
7940                                 chk->whoTo = asoc->primary_destination;
7941                         }
7942                         atomic_add_int(&chk->whoTo->ref_count, 1);
7943                 }
7944         }
7945         old_start_at = NULL;
7946 again_one_more_time:
7947         for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7948                 /* how much can we send? */
7949                 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7950                 if (old_start_at && (old_start_at == net)) {
7951                         /* through list completely. */
7952                         break;
7953                 }
7954                 tsns_sent = 0xa;
7955                 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7956                     TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7957                     (net->flight_size >= net->cwnd)) {
7958                         /*
7959                          * Nothing on control or asconf and flight is full,
7960                          * we can skip even in the CMT case.
7961                          */
7962                         continue;
7963                 }
7964                 bundle_at = 0;
7965                 endoutchain = outchain = NULL;
7966                 auth = NULL;
7967                 auth_offset = 0;
7968                 no_fragmentflg = 1;
7969                 one_chunk = 0;
7970                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7971                         skip_data_for_this_net = 1;
7972                 } else {
7973                         skip_data_for_this_net = 0;
7974                 }
7975                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7976 #ifdef INET
7977                 case AF_INET:
7978                         mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7979                         break;
7980 #endif
7981 #ifdef INET6
7982                 case AF_INET6:
7983                         mtu = net->mtu - SCTP_MIN_OVERHEAD;
7984                         break;
7985 #endif
7986                 default:
7987                         /* TSNH */
7988                         mtu = net->mtu;
7989                         break;
7990                 }
7991                 mx_mtu = mtu;
7992                 to_out = 0;
7993                 if (mtu > asoc->peers_rwnd) {
7994                         if (asoc->total_flight > 0) {
7995                                 /* We have a packet in flight somewhere */
7996                                 r_mtu = asoc->peers_rwnd;
7997                         } else {
7998                                 /* We are always allowed to send one MTU out */
7999                                 one_chunk = 1;
8000                                 r_mtu = mtu;
8001                         }
8002                 } else {
8003                         r_mtu = mtu;
8004                 }
8005                 error = 0;
8006                 /************************/
8007                 /* ASCONF transmission */
8008                 /************************/
8009                 /* Now first lets go through the asconf queue */
8010                 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
8011                         if (chk->rec.chunk_id.id != SCTP_ASCONF) {
8012                                 continue;
8013                         }
8014                         if (chk->whoTo == NULL) {
8015                                 if (asoc->alternate == NULL) {
8016                                         if (asoc->primary_destination != net) {
8017                                                 break;
8018                                         }
8019                                 } else {
8020                                         if (asoc->alternate != net) {
8021                                                 break;
8022                                         }
8023                                 }
8024                         } else {
8025                                 if (chk->whoTo != net) {
8026                                         break;
8027                                 }
8028                         }
8029                         if (chk->data == NULL) {
8030                                 break;
8031                         }
8032                         if (chk->sent != SCTP_DATAGRAM_UNSENT &&
8033                             chk->sent != SCTP_DATAGRAM_RESEND) {
8034                                 break;
8035                         }
8036                         /*
8037                          * if no AUTH is yet included and this chunk
8038                          * requires it, make sure to account for it.  We
8039                          * don't apply the size until the AUTH chunk is
8040                          * actually added below in case there is no room for
8041                          * this chunk. NOTE: we overload the use of "omtu"
8042                          * here
8043                          */
8044                         if ((auth == NULL) &&
8045                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8046                             stcb->asoc.peer_auth_chunks)) {
8047                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8048                         } else
8049                                 omtu = 0;
8050                         /* Here we do NOT factor the r_mtu */
8051                         if ((chk->send_size < (int)(mtu - omtu)) ||
8052                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8053                                 /*
8054                                  * We probably should glom the mbuf chain
8055                                  * from the chk->data for control but the
8056                                  * problem is it becomes yet one more level
8057                                  * of tracking to do if for some reason
8058                                  * output fails. Then I have got to
8059                                  * reconstruct the merged control chain.. el
8060                                  * yucko.. for now we take the easy way and
8061                                  * do the copy
8062                                  */
8063                                 /*
8064                                  * Add an AUTH chunk, if chunk requires it
8065                                  * save the offset into the chain for AUTH
8066                                  */
8067                                 if ((auth == NULL) &&
8068                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8069                                     stcb->asoc.peer_auth_chunks))) {
8070                                         outchain = sctp_add_auth_chunk(outchain,
8071                                             &endoutchain,
8072                                             &auth,
8073                                             &auth_offset,
8074                                             stcb,
8075                                             chk->rec.chunk_id.id);
8076                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8077                                 }
8078                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8079                                     (int)chk->rec.chunk_id.can_take_data,
8080                                     chk->send_size, chk->copy_by_ref);
8081                                 if (outchain == NULL) {
8082                                         *reason_code = 8;
8083                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8084                                         return (ENOMEM);
8085                                 }
8086                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8087                                 /* update our MTU size */
8088                                 if (mtu > (chk->send_size + omtu))
8089                                         mtu -= (chk->send_size + omtu);
8090                                 else
8091                                         mtu = 0;
8092                                 to_out += (chk->send_size + omtu);
8093                                 /* Do clear IP_DF ? */
8094                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8095                                         no_fragmentflg = 0;
8096                                 }
8097                                 if (chk->rec.chunk_id.can_take_data)
8098                                         chk->data = NULL;
8099                                 /*
8100                                  * set hb flag since we can use these for
8101                                  * RTO
8102                                  */
8103                                 hbflag = 1;
8104                                 asconf = 1;
8105                                 /*
8106                                  * should sysctl this: don't bundle data
8107                                  * with ASCONF since it requires AUTH
8108                                  */
8109                                 no_data_chunks = 1;
8110                                 chk->sent = SCTP_DATAGRAM_SENT;
8111                                 if (chk->whoTo == NULL) {
8112                                         chk->whoTo = net;
8113                                         atomic_add_int(&net->ref_count, 1);
8114                                 }
8115                                 chk->snd_count++;
8116                                 if (mtu == 0) {
8117                                         /*
8118                                          * Ok we are out of room but we can
8119                                          * output without effecting the
8120                                          * flight size since this little guy
8121                                          * is a control only packet.
8122                                          */
8123                                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8124                                         /*
8125                                          * do NOT clear the asconf flag as
8126                                          * it is used to do appropriate
8127                                          * source address selection.
8128                                          */
8129                                         if (*now_filled == 0) {
8130                                                 (void)SCTP_GETTIME_TIMEVAL(now);
8131                                                 *now_filled = 1;
8132                                         }
8133                                         net->last_sent_time = *now;
8134                                         hbflag = 0;
8135                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8136                                             (struct sockaddr *)&net->ro._l_addr,
8137                                             outchain, auth_offset, auth,
8138                                             stcb->asoc.authinfo.active_keyid,
8139                                             no_fragmentflg, 0, asconf,
8140                                             inp->sctp_lport, stcb->rport,
8141                                             htonl(stcb->asoc.peer_vtag),
8142                                             net->port, NULL,
8143                                             0, 0,
8144                                             so_locked))) {
8145                                                 /*
8146                                                  * error, we could not
8147                                                  * output
8148                                                  */
8149                                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8150                                                 if (from_where == 0) {
8151                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
8152                                                 }
8153                                                 if (error == ENOBUFS) {
8154                                                         asoc->ifp_had_enobuf = 1;
8155                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
8156                                                 }
8157                                                 /* error, could not output */
8158                                                 if (error == EHOSTUNREACH) {
8159                                                         /*
8160                                                          * Destination went
8161                                                          * unreachable
8162                                                          * during this send
8163                                                          */
8164                                                         sctp_move_chunks_from_net(stcb, net);
8165                                                 }
8166                                                 *reason_code = 7;
8167                                                 break;
8168                                         } else {
8169                                                 asoc->ifp_had_enobuf = 0;
8170                                         }
8171                                         /*
8172                                          * increase the number we sent, if a
8173                                          * cookie is sent we don't tell them
8174                                          * any was sent out.
8175                                          */
8176                                         outchain = endoutchain = NULL;
8177                                         auth = NULL;
8178                                         auth_offset = 0;
8179                                         if (!no_out_cnt)
8180                                                 *num_out += ctl_cnt;
8181                                         /* recalc a clean slate and setup */
8182                                         switch (net->ro._l_addr.sa.sa_family) {
8183 #ifdef INET
8184                                         case AF_INET:
8185                                                 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8186                                                 break;
8187 #endif
8188 #ifdef INET6
8189                                         case AF_INET6:
8190                                                 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8191                                                 break;
8192 #endif
8193                                         default:
8194                                                 /* TSNH */
8195                                                 mtu = net->mtu;
8196                                                 break;
8197                                         }
8198                                         to_out = 0;
8199                                         no_fragmentflg = 1;
8200                                 }
8201                         }
8202                 }
8203                 if (error != 0) {
8204                         /* try next net */
8205                         continue;
8206                 }
8207                 /************************/
8208                 /* Control transmission */
8209                 /************************/
8210                 /* Now first lets go through the control queue */
8211                 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
8212                         if ((sack_goes_to) &&
8213                             (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
8214                             (chk->whoTo != sack_goes_to)) {
8215                                 /*
8216                                  * if we have a sack in queue, and we are
8217                                  * looking at an ecn echo that is NOT queued
8218                                  * to where the sack is going..
8219                                  */
8220                                 if (chk->whoTo == net) {
8221                                         /*
8222                                          * Don't transmit it to where its
8223                                          * going (current net)
8224                                          */
8225                                         continue;
8226                                 } else if (sack_goes_to == net) {
8227                                         /*
8228                                          * But do transmit it to this
8229                                          * address
8230                                          */
8231                                         goto skip_net_check;
8232                                 }
8233                         }
8234                         if (chk->whoTo == NULL) {
8235                                 if (asoc->alternate == NULL) {
8236                                         if (asoc->primary_destination != net) {
8237                                                 continue;
8238                                         }
8239                                 } else {
8240                                         if (asoc->alternate != net) {
8241                                                 continue;
8242                                         }
8243                                 }
8244                         } else {
8245                                 if (chk->whoTo != net) {
8246                                         continue;
8247                                 }
8248                         }
8249         skip_net_check:
8250                         if (chk->data == NULL) {
8251                                 continue;
8252                         }
8253                         if (chk->sent != SCTP_DATAGRAM_UNSENT) {
8254                                 /*
8255                                  * It must be unsent. Cookies and ASCONF's
8256                                  * hang around but there timers will force
8257                                  * when marked for resend.
8258                                  */
8259                                 continue;
8260                         }
8261                         /*
8262                          * if no AUTH is yet included and this chunk
8263                          * requires it, make sure to account for it.  We
8264                          * don't apply the size until the AUTH chunk is
8265                          * actually added below in case there is no room for
8266                          * this chunk. NOTE: we overload the use of "omtu"
8267                          * here
8268                          */
8269                         if ((auth == NULL) &&
8270                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8271                             stcb->asoc.peer_auth_chunks)) {
8272                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8273                         } else
8274                                 omtu = 0;
8275                         /* Here we do NOT factor the r_mtu */
8276                         if ((chk->send_size <= (int)(mtu - omtu)) ||
8277                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8278                                 /*
8279                                  * We probably should glom the mbuf chain
8280                                  * from the chk->data for control but the
8281                                  * problem is it becomes yet one more level
8282                                  * of tracking to do if for some reason
8283                                  * output fails. Then I have got to
8284                                  * reconstruct the merged control chain.. el
8285                                  * yucko.. for now we take the easy way and
8286                                  * do the copy
8287                                  */
8288                                 /*
8289                                  * Add an AUTH chunk, if chunk requires it
8290                                  * save the offset into the chain for AUTH
8291                                  */
8292                                 if ((auth == NULL) &&
8293                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8294                                     stcb->asoc.peer_auth_chunks))) {
8295                                         outchain = sctp_add_auth_chunk(outchain,
8296                                             &endoutchain,
8297                                             &auth,
8298                                             &auth_offset,
8299                                             stcb,
8300                                             chk->rec.chunk_id.id);
8301                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8302                                 }
8303                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8304                                     (int)chk->rec.chunk_id.can_take_data,
8305                                     chk->send_size, chk->copy_by_ref);
8306                                 if (outchain == NULL) {
8307                                         *reason_code = 8;
8308                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8309                                         return (ENOMEM);
8310                                 }
8311                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8312                                 /* update our MTU size */
8313                                 if (mtu > (chk->send_size + omtu))
8314                                         mtu -= (chk->send_size + omtu);
8315                                 else
8316                                         mtu = 0;
8317                                 to_out += (chk->send_size + omtu);
8318                                 /* Do clear IP_DF ? */
8319                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8320                                         no_fragmentflg = 0;
8321                                 }
8322                                 if (chk->rec.chunk_id.can_take_data)
8323                                         chk->data = NULL;
8324                                 /* Mark things to be removed, if needed */
8325                                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8326                                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
8327                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8328                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8329                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8330                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8331                                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8332                                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8333                                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8334                                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8335                                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8336                                         if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
8337                                                 hbflag = 1;
8338                                         }
8339                                         /* remove these chunks at the end */
8340                                         if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8341                                             (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
8342                                                 /* turn off the timer */
8343                                                 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8344                                                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8345                                                             inp, stcb, NULL,
8346                                                             SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
8347                                                 }
8348                                         }
8349                                         ctl_cnt++;
8350                                 } else {
8351                                         /*
8352                                          * Other chunks, since they have
8353                                          * timers running (i.e. COOKIE) we
8354                                          * just "trust" that it gets sent or
8355                                          * retransmitted.
8356                                          */
8357                                         ctl_cnt++;
8358                                         if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8359                                                 cookie = 1;
8360                                                 no_out_cnt = 1;
8361                                         } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8362                                                 /*
8363                                                  * Increment ecne send count
8364                                                  * here this means we may be
8365                                                  * over-zealous in our
8366                                                  * counting if the send
8367                                                  * fails, but its the best
8368                                                  * place to do it (we used
8369                                                  * to do it in the queue of
8370                                                  * the chunk, but that did
8371                                                  * not tell how many times
8372                                                  * it was sent.
8373                                                  */
8374                                                 SCTP_STAT_INCR(sctps_sendecne);
8375                                         }
8376                                         chk->sent = SCTP_DATAGRAM_SENT;
8377                                         if (chk->whoTo == NULL) {
8378                                                 chk->whoTo = net;
8379                                                 atomic_add_int(&net->ref_count, 1);
8380                                         }
8381                                         chk->snd_count++;
8382                                 }
8383                                 if (mtu == 0) {
8384                                         /*
8385                                          * Ok we are out of room but we can
8386                                          * output without effecting the
8387                                          * flight size since this little guy
8388                                          * is a control only packet.
8389                                          */
8390                                         if (asconf) {
8391                                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8392                                                 /*
8393                                                  * do NOT clear the asconf
8394                                                  * flag as it is used to do
8395                                                  * appropriate source
8396                                                  * address selection.
8397                                                  */
8398                                         }
8399                                         if (cookie) {
8400                                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8401                                                 cookie = 0;
8402                                         }
8403                                         /* Only HB or ASCONF advances time */
8404                                         if (hbflag) {
8405                                                 if (*now_filled == 0) {
8406                                                         (void)SCTP_GETTIME_TIMEVAL(now);
8407                                                         *now_filled = 1;
8408                                                 }
8409                                                 net->last_sent_time = *now;
8410                                                 hbflag = 0;
8411                                         }
8412                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8413                                             (struct sockaddr *)&net->ro._l_addr,
8414                                             outchain,
8415                                             auth_offset, auth,
8416                                             stcb->asoc.authinfo.active_keyid,
8417                                             no_fragmentflg, 0, asconf,
8418                                             inp->sctp_lport, stcb->rport,
8419                                             htonl(stcb->asoc.peer_vtag),
8420                                             net->port, NULL,
8421                                             0, 0,
8422                                             so_locked))) {
8423                                                 /*
8424                                                  * error, we could not
8425                                                  * output
8426                                                  */
8427                                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8428                                                 if (from_where == 0) {
8429                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
8430                                                 }
8431                                                 if (error == ENOBUFS) {
8432                                                         asoc->ifp_had_enobuf = 1;
8433                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
8434                                                 }
8435                                                 if (error == EHOSTUNREACH) {
8436                                                         /*
8437                                                          * Destination went
8438                                                          * unreachable
8439                                                          * during this send
8440                                                          */
8441                                                         sctp_move_chunks_from_net(stcb, net);
8442                                                 }
8443                                                 *reason_code = 7;
8444                                                 break;
8445                                         } else {
8446                                                 asoc->ifp_had_enobuf = 0;
8447                                         }
8448                                         /*
8449                                          * increase the number we sent, if a
8450                                          * cookie is sent we don't tell them
8451                                          * any was sent out.
8452                                          */
8453                                         outchain = endoutchain = NULL;
8454                                         auth = NULL;
8455                                         auth_offset = 0;
8456                                         if (!no_out_cnt)
8457                                                 *num_out += ctl_cnt;
8458                                         /* recalc a clean slate and setup */
8459                                         switch (net->ro._l_addr.sa.sa_family) {
8460 #ifdef INET
8461                                         case AF_INET:
8462                                                 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8463                                                 break;
8464 #endif
8465 #ifdef INET6
8466                                         case AF_INET6:
8467                                                 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8468                                                 break;
8469 #endif
8470                                         default:
8471                                                 /* TSNH */
8472                                                 mtu = net->mtu;
8473                                                 break;
8474                                         }
8475                                         to_out = 0;
8476                                         no_fragmentflg = 1;
8477                                 }
8478                         }
8479                 }
8480                 if (error != 0) {
8481                         /* try next net */
8482                         continue;
8483                 }
8484                 /* JRI: if dest is in PF state, do not send data to it */
8485                 if ((asoc->sctp_cmt_on_off > 0) &&
8486                     (net != stcb->asoc.alternate) &&
8487                     (net->dest_state & SCTP_ADDR_PF)) {
8488                         goto no_data_fill;
8489                 }
8490                 if (net->flight_size >= net->cwnd) {
8491                         goto no_data_fill;
8492                 }
8493                 if ((asoc->sctp_cmt_on_off > 0) &&
8494                     (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
8495                     (net->flight_size > max_rwnd_per_dest)) {
8496                         goto no_data_fill;
8497                 }
8498                 /*
8499                  * We need a specific accounting for the usage of the send
8500                  * buffer. We also need to check the number of messages per
8501                  * net. For now, this is better than nothing and it disabled
8502                  * by default...
8503                  */
8504                 if ((asoc->sctp_cmt_on_off > 0) &&
8505                     (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
8506                     (max_send_per_dest > 0) &&
8507                     (net->flight_size > max_send_per_dest)) {
8508                         goto no_data_fill;
8509                 }
8510                 /*********************/
8511                 /* Data transmission */
8512                 /*********************/
8513                 /*
8514                  * if AUTH for DATA is required and no AUTH has been added
8515                  * yet, account for this in the mtu now... if no data can be
8516                  * bundled, this adjustment won't matter anyways since the
8517                  * packet will be going out...
8518                  */
8519                 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8520                     stcb->asoc.peer_auth_chunks);
8521                 if (data_auth_reqd && (auth == NULL)) {
8522                         mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8523                 }
8524                 /* now lets add any data within the MTU constraints */
8525                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8526 #ifdef INET
8527                 case AF_INET:
8528                         if (net->mtu > SCTP_MIN_V4_OVERHEAD)
8529                                 omtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8530                         else
8531                                 omtu = 0;
8532                         break;
8533 #endif
8534 #ifdef INET6
8535                 case AF_INET6:
8536                         if (net->mtu > SCTP_MIN_OVERHEAD)
8537                                 omtu = net->mtu - SCTP_MIN_OVERHEAD;
8538                         else
8539                                 omtu = 0;
8540                         break;
8541 #endif
8542                 default:
8543                         /* TSNH */
8544                         omtu = 0;
8545                         break;
8546                 }
8547                 if ((((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
8548                     (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
8549                     (skip_data_for_this_net == 0)) ||
8550                     (cookie)) {
8551                         TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
8552                                 if (no_data_chunks) {
8553                                         /* let only control go out */
8554                                         *reason_code = 1;
8555                                         break;
8556                                 }
8557                                 if (net->flight_size >= net->cwnd) {
8558                                         /* skip this net, no room for data */
8559                                         *reason_code = 2;
8560                                         break;
8561                                 }
8562                                 if ((chk->whoTo != NULL) &&
8563                                     (chk->whoTo != net)) {
8564                                         /* Don't send the chunk on this net */
8565                                         continue;
8566                                 }
8567
8568                                 if (asoc->sctp_cmt_on_off == 0) {
8569                                         if ((asoc->alternate) &&
8570                                             (asoc->alternate != net) &&
8571                                             (chk->whoTo == NULL)) {
8572                                                 continue;
8573                                         } else if ((net != asoc->primary_destination) &&
8574                                                     (asoc->alternate == NULL) &&
8575                                             (chk->whoTo == NULL)) {
8576                                                 continue;
8577                                         }
8578                                 }
8579                                 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8580                                         /*-
8581                                          * strange, we have a chunk that is
8582                                          * to big for its destination and
8583                                          * yet no fragment ok flag.
8584                                          * Something went wrong when the
8585                                          * PMTU changed...we did not mark
8586                                          * this chunk for some reason?? I
8587                                          * will fix it here by letting IP
8588                                          * fragment it for now and printing
8589                                          * a warning. This really should not
8590                                          * happen ...
8591                                          */
8592                                         SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8593                                             chk->send_size, mtu);
8594                                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8595                                 }
8596                                 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8597                                     (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
8598                                         struct sctp_data_chunk *dchkh;
8599
8600                                         dchkh = mtod(chk->data, struct sctp_data_chunk *);
8601                                         dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8602                                 }
8603                                 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8604                                     ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8605                                         /* ok we will add this one */
8606
8607                                         /*
8608                                          * Add an AUTH chunk, if chunk
8609                                          * requires it, save the offset into
8610                                          * the chain for AUTH
8611                                          */
8612                                         if (data_auth_reqd) {
8613                                                 if (auth == NULL) {
8614                                                         outchain = sctp_add_auth_chunk(outchain,
8615                                                             &endoutchain,
8616                                                             &auth,
8617                                                             &auth_offset,
8618                                                             stcb,
8619                                                             SCTP_DATA);
8620                                                         auth_keyid = chk->auth_keyid;
8621                                                         override_ok = 0;
8622                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8623                                                 } else if (override_ok) {
8624                                                         /*
8625                                                          * use this data's
8626                                                          * keyid
8627                                                          */
8628                                                         auth_keyid = chk->auth_keyid;
8629                                                         override_ok = 0;
8630                                                 } else if (auth_keyid != chk->auth_keyid) {
8631                                                         /*
8632                                                          * different keyid,
8633                                                          * so done bundling
8634                                                          */
8635                                                         break;
8636                                                 }
8637                                         }
8638                                         outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8639                                             chk->send_size, chk->copy_by_ref);
8640                                         if (outchain == NULL) {
8641                                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8642                                                 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8643                                                         sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8644                                                 }
8645                                                 *reason_code = 3;
8646                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8647                                                 return (ENOMEM);
8648                                         }
8649                                         /* update our MTU size */
8650                                         /* Do clear IP_DF ? */
8651                                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8652                                                 no_fragmentflg = 0;
8653                                         }
8654                                         /* unsigned subtraction of mtu */
8655                                         if (mtu > chk->send_size)
8656                                                 mtu -= chk->send_size;
8657                                         else
8658                                                 mtu = 0;
8659                                         /* unsigned subtraction of r_mtu */
8660                                         if (r_mtu > chk->send_size)
8661                                                 r_mtu -= chk->send_size;
8662                                         else
8663                                                 r_mtu = 0;
8664
8665                                         to_out += chk->send_size;
8666                                         if ((to_out > mx_mtu) && no_fragmentflg) {
8667 #ifdef INVARIANTS
8668                                                 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8669 #else
8670                                                 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8671                                                     mx_mtu, to_out);
8672 #endif
8673                                         }
8674                                         chk->window_probe = 0;
8675                                         data_list[bundle_at++] = chk;
8676                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8677                                                 break;
8678                                         }
8679                                         if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8680                                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8681                                                         SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8682                                                 } else {
8683                                                         SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8684                                                 }
8685                                                 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8686                                                     ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8687                                                         /*
8688                                                          * Count number of
8689                                                          * user msg's that
8690                                                          * were fragmented
8691                                                          * we do this by
8692                                                          * counting when we
8693                                                          * see a LAST
8694                                                          * fragment only.
8695                                                          */
8696                                                         SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8697                                         }
8698                                         if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8699                                                 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8700                                                         data_list[0]->window_probe = 1;
8701                                                         net->window_probe = 1;
8702                                                 }
8703                                                 break;
8704                                         }
8705                                 } else {
8706                                         /*
8707                                          * Must be sent in order of the
8708                                          * TSN's (on a network)
8709                                          */
8710                                         break;
8711                                 }
8712                         }       /* for (chunk gather loop for this net) */
8713                 }               /* if asoc.state OPEN */
8714 no_data_fill:
8715                 /* Is there something to send for this destination? */
8716                 if (outchain) {
8717                         /* We may need to start a control timer or two */
8718                         if (asconf) {
8719                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8720                                     stcb, net);
8721                                 /*
8722                                  * do NOT clear the asconf flag as it is
8723                                  * used to do appropriate source address
8724                                  * selection.
8725                                  */
8726                         }
8727                         if (cookie) {
8728                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8729                                 cookie = 0;
8730                         }
8731                         /* must start a send timer if data is being sent */
8732                         if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8733                                 /*
8734                                  * no timer running on this destination
8735                                  * restart it.
8736                                  */
8737                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8738                         }
8739                         if (bundle_at || hbflag) {
8740                                 /* For data/asconf and hb set time */
8741                                 if (*now_filled == 0) {
8742                                         (void)SCTP_GETTIME_TIMEVAL(now);
8743                                         *now_filled = 1;
8744                                 }
8745                                 net->last_sent_time = *now;
8746                         }
8747                         /* Now send it, if there is anything to send :> */
8748                         if ((error = sctp_lowlevel_chunk_output(inp,
8749                             stcb,
8750                             net,
8751                             (struct sockaddr *)&net->ro._l_addr,
8752                             outchain,
8753                             auth_offset,
8754                             auth,
8755                             auth_keyid,
8756                             no_fragmentflg,
8757                             bundle_at,
8758                             asconf,
8759                             inp->sctp_lport, stcb->rport,
8760                             htonl(stcb->asoc.peer_vtag),
8761                             net->port, NULL,
8762                             0, 0,
8763                             so_locked))) {
8764                                 /* error, we could not output */
8765                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8766                                 if (from_where == 0) {
8767                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
8768                                 }
8769                                 if (error == ENOBUFS) {
8770                                         asoc->ifp_had_enobuf = 1;
8771                                         SCTP_STAT_INCR(sctps_lowlevelerr);
8772                                 }
8773                                 if (error == EHOSTUNREACH) {
8774                                         /*
8775                                          * Destination went unreachable
8776                                          * during this send
8777                                          */
8778                                         sctp_move_chunks_from_net(stcb, net);
8779                                 }
8780                                 *reason_code = 6;
8781                                 /*-
8782                                  * I add this line to be paranoid. As far as
8783                                  * I can tell the continue, takes us back to
8784                                  * the top of the for, but just to make sure
8785                                  * I will reset these again here.
8786                                  */
8787                                 ctl_cnt = 0;
8788                                 continue;       /* This takes us back to the
8789                                                  * for() for the nets. */
8790                         } else {
8791                                 asoc->ifp_had_enobuf = 0;
8792                         }
8793                         endoutchain = NULL;
8794                         auth = NULL;
8795                         auth_offset = 0;
8796                         if (!no_out_cnt) {
8797                                 *num_out += (ctl_cnt + bundle_at);
8798                         }
8799                         if (bundle_at) {
8800                                 /* setup for a RTO measurement */
8801                                 tsns_sent = data_list[0]->rec.data.tsn;
8802                                 /* fill time if not already filled */
8803                                 if (*now_filled == 0) {
8804                                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8805                                         *now_filled = 1;
8806                                         *now = asoc->time_last_sent;
8807                                 } else {
8808                                         asoc->time_last_sent = *now;
8809                                 }
8810                                 if (net->rto_needed) {
8811                                         data_list[0]->do_rtt = 1;
8812                                         net->rto_needed = 0;
8813                                 }
8814                                 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8815                                 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8816                         }
8817                         if (one_chunk) {
8818                                 break;
8819                         }
8820                 }
8821                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8822                         sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8823                 }
8824         }
8825         if (old_start_at == NULL) {
8826                 old_start_at = start_at;
8827                 start_at = TAILQ_FIRST(&asoc->nets);
8828                 if (old_start_at)
8829                         goto again_one_more_time;
8830         }
8831
8832         /*
8833          * At the end there should be no NON timed chunks hanging on this
8834          * queue.
8835          */
8836         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8837                 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8838         }
8839         if ((*num_out == 0) && (*reason_code == 0)) {
8840                 *reason_code = 4;
8841         } else {
8842                 *reason_code = 5;
8843         }
8844         sctp_clean_up_ctl(stcb, asoc, so_locked);
8845         return (0);
8846 }
8847
8848 void
8849 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8850 {
8851         /*-
8852          * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8853          * the control chunk queue.
8854          */
8855         struct sctp_chunkhdr *hdr;
8856         struct sctp_tmit_chunk *chk;
8857         struct mbuf *mat, *last_mbuf;
8858         uint32_t chunk_length;
8859         uint16_t padding_length;
8860
8861         SCTP_TCB_LOCK_ASSERT(stcb);
8862         SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_NOWAIT);
8863         if (op_err == NULL) {
8864                 return;
8865         }
8866         last_mbuf = NULL;
8867         chunk_length = 0;
8868         for (mat = op_err; mat != NULL; mat = SCTP_BUF_NEXT(mat)) {
8869                 chunk_length += SCTP_BUF_LEN(mat);
8870                 if (SCTP_BUF_NEXT(mat) == NULL) {
8871                         last_mbuf = mat;
8872                 }
8873         }
8874         if (chunk_length > SCTP_MAX_CHUNK_LENGTH) {
8875                 sctp_m_freem(op_err);
8876                 return;
8877         }
8878         padding_length = chunk_length % 4;
8879         if (padding_length != 0) {
8880                 padding_length = 4 - padding_length;
8881         }
8882         if (padding_length != 0) {
8883                 if (sctp_add_pad_tombuf(last_mbuf, padding_length) == NULL) {
8884                         sctp_m_freem(op_err);
8885                         return;
8886                 }
8887         }
8888         sctp_alloc_a_chunk(stcb, chk);
8889         if (chk == NULL) {
8890                 /* no memory */
8891                 sctp_m_freem(op_err);
8892                 return;
8893         }
8894         chk->copy_by_ref = 0;
8895         chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8896         chk->rec.chunk_id.can_take_data = 0;
8897         chk->flags = 0;
8898         chk->send_size = (uint16_t)chunk_length;
8899         chk->sent = SCTP_DATAGRAM_UNSENT;
8900         chk->snd_count = 0;
8901         chk->asoc = &stcb->asoc;
8902         chk->data = op_err;
8903         chk->whoTo = NULL;
8904         hdr = mtod(op_err, struct sctp_chunkhdr *);
8905         hdr->chunk_type = SCTP_OPERATION_ERROR;
8906         hdr->chunk_flags = 0;
8907         hdr->chunk_length = htons(chk->send_size);
8908         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8909         chk->asoc->ctrl_queue_cnt++;
8910 }
8911
8912 int
8913 sctp_send_cookie_echo(struct mbuf *m,
8914     int offset, int limit,
8915     struct sctp_tcb *stcb,
8916     struct sctp_nets *net)
8917 {
8918         /*-
8919          * pull out the cookie and put it at the front of the control chunk
8920          * queue.
8921          */
8922         int at;
8923         struct mbuf *cookie;
8924         struct sctp_paramhdr param, *phdr;
8925         struct sctp_chunkhdr *hdr;
8926         struct sctp_tmit_chunk *chk;
8927         uint16_t ptype, plen;
8928
8929         SCTP_TCB_LOCK_ASSERT(stcb);
8930         /* First find the cookie in the param area */
8931         cookie = NULL;
8932         at = offset + sizeof(struct sctp_init_chunk);
8933         for (;;) {
8934                 phdr = sctp_get_next_param(m, at, &param, sizeof(param));
8935                 if (phdr == NULL) {
8936                         return (-3);
8937                 }
8938                 ptype = ntohs(phdr->param_type);
8939                 plen = ntohs(phdr->param_length);
8940                 if (plen < sizeof(struct sctp_paramhdr)) {
8941                         return (-6);
8942                 }
8943                 if (ptype == SCTP_STATE_COOKIE) {
8944                         int pad;
8945
8946                         /* found the cookie */
8947                         if (at + plen > limit) {
8948                                 return (-7);
8949                         }
8950                         cookie = SCTP_M_COPYM(m, at, plen, M_NOWAIT);
8951                         if (cookie == NULL) {
8952                                 /* No memory */
8953                                 return (-2);
8954                         }
8955                         if ((pad = (plen % 4)) > 0) {
8956                                 pad = 4 - pad;
8957                         }
8958                         if (pad > 0) {
8959                                 if (sctp_pad_lastmbuf(cookie, pad, NULL) == NULL) {
8960                                         return (-8);
8961                                 }
8962                         }
8963 #ifdef SCTP_MBUF_LOGGING
8964                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8965                                 sctp_log_mbc(cookie, SCTP_MBUF_ICOPY);
8966                         }
8967 #endif
8968                         break;
8969                 }
8970                 at += SCTP_SIZE32(plen);
8971         }
8972         /* ok, we got the cookie lets change it into a cookie echo chunk */
8973         /* first the change from param to cookie */
8974         hdr = mtod(cookie, struct sctp_chunkhdr *);
8975         hdr->chunk_type = SCTP_COOKIE_ECHO;
8976         hdr->chunk_flags = 0;
8977         /* get the chunk stuff now and place it in the FRONT of the queue */
8978         sctp_alloc_a_chunk(stcb, chk);
8979         if (chk == NULL) {
8980                 /* no memory */
8981                 sctp_m_freem(cookie);
8982                 return (-5);
8983         }
8984         chk->copy_by_ref = 0;
8985         chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8986         chk->rec.chunk_id.can_take_data = 0;
8987         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8988         chk->send_size = SCTP_SIZE32(plen);
8989         chk->sent = SCTP_DATAGRAM_UNSENT;
8990         chk->snd_count = 0;
8991         chk->asoc = &stcb->asoc;
8992         chk->data = cookie;
8993         chk->whoTo = net;
8994         atomic_add_int(&chk->whoTo->ref_count, 1);
8995         TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8996         chk->asoc->ctrl_queue_cnt++;
8997         return (0);
8998 }
8999
9000 void
9001 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
9002     struct mbuf *m,
9003     int offset,
9004     int chk_length,
9005     struct sctp_nets *net)
9006 {
9007         /*
9008          * take a HB request and make it into a HB ack and send it.
9009          */
9010         struct mbuf *outchain;
9011         struct sctp_chunkhdr *chdr;
9012         struct sctp_tmit_chunk *chk;
9013
9014         if (net == NULL)
9015                 /* must have a net pointer */
9016                 return;
9017
9018         outchain = SCTP_M_COPYM(m, offset, chk_length, M_NOWAIT);
9019         if (outchain == NULL) {
9020                 /* gak out of memory */
9021                 return;
9022         }
9023 #ifdef SCTP_MBUF_LOGGING
9024         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9025                 sctp_log_mbc(outchain, SCTP_MBUF_ICOPY);
9026         }
9027 #endif
9028         chdr = mtod(outchain, struct sctp_chunkhdr *);
9029         chdr->chunk_type = SCTP_HEARTBEAT_ACK;
9030         chdr->chunk_flags = 0;
9031         if (chk_length % 4 != 0) {
9032                 sctp_pad_lastmbuf(outchain, 4 - (chk_length % 4), NULL);
9033         }
9034         sctp_alloc_a_chunk(stcb, chk);
9035         if (chk == NULL) {
9036                 /* no memory */
9037                 sctp_m_freem(outchain);
9038                 return;
9039         }
9040         chk->copy_by_ref = 0;
9041         chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
9042         chk->rec.chunk_id.can_take_data = 1;
9043         chk->flags = 0;
9044         chk->send_size = chk_length;
9045         chk->sent = SCTP_DATAGRAM_UNSENT;
9046         chk->snd_count = 0;
9047         chk->asoc = &stcb->asoc;
9048         chk->data = outchain;
9049         chk->whoTo = net;
9050         atomic_add_int(&chk->whoTo->ref_count, 1);
9051         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9052         chk->asoc->ctrl_queue_cnt++;
9053 }
9054
9055 void
9056 sctp_send_cookie_ack(struct sctp_tcb *stcb)
9057 {
9058         /* formulate and queue a cookie-ack back to sender */
9059         struct mbuf *cookie_ack;
9060         struct sctp_chunkhdr *hdr;
9061         struct sctp_tmit_chunk *chk;
9062
9063         SCTP_TCB_LOCK_ASSERT(stcb);
9064
9065         cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
9066         if (cookie_ack == NULL) {
9067                 /* no mbuf's */
9068                 return;
9069         }
9070         SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
9071         sctp_alloc_a_chunk(stcb, chk);
9072         if (chk == NULL) {
9073                 /* no memory */
9074                 sctp_m_freem(cookie_ack);
9075                 return;
9076         }
9077         chk->copy_by_ref = 0;
9078         chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
9079         chk->rec.chunk_id.can_take_data = 1;
9080         chk->flags = 0;
9081         chk->send_size = sizeof(struct sctp_chunkhdr);
9082         chk->sent = SCTP_DATAGRAM_UNSENT;
9083         chk->snd_count = 0;
9084         chk->asoc = &stcb->asoc;
9085         chk->data = cookie_ack;
9086         if (chk->asoc->last_control_chunk_from != NULL) {
9087                 chk->whoTo = chk->asoc->last_control_chunk_from;
9088                 atomic_add_int(&chk->whoTo->ref_count, 1);
9089         } else {
9090                 chk->whoTo = NULL;
9091         }
9092         hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
9093         hdr->chunk_type = SCTP_COOKIE_ACK;
9094         hdr->chunk_flags = 0;
9095         hdr->chunk_length = htons(chk->send_size);
9096         SCTP_BUF_LEN(cookie_ack) = chk->send_size;
9097         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9098         chk->asoc->ctrl_queue_cnt++;
9099         return;
9100 }
9101
9102 void
9103 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
9104 {
9105         /* formulate and queue a SHUTDOWN-ACK back to the sender */
9106         struct mbuf *m_shutdown_ack;
9107         struct sctp_shutdown_ack_chunk *ack_cp;
9108         struct sctp_tmit_chunk *chk;
9109
9110         m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9111         if (m_shutdown_ack == NULL) {
9112                 /* no mbuf's */
9113                 return;
9114         }
9115         SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
9116         sctp_alloc_a_chunk(stcb, chk);
9117         if (chk == NULL) {
9118                 /* no memory */
9119                 sctp_m_freem(m_shutdown_ack);
9120                 return;
9121         }
9122         chk->copy_by_ref = 0;
9123         chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
9124         chk->rec.chunk_id.can_take_data = 1;
9125         chk->flags = 0;
9126         chk->send_size = sizeof(struct sctp_chunkhdr);
9127         chk->sent = SCTP_DATAGRAM_UNSENT;
9128         chk->snd_count = 0;
9129         chk->asoc = &stcb->asoc;
9130         chk->data = m_shutdown_ack;
9131         chk->whoTo = net;
9132         if (chk->whoTo) {
9133                 atomic_add_int(&chk->whoTo->ref_count, 1);
9134         }
9135         ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
9136         ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
9137         ack_cp->ch.chunk_flags = 0;
9138         ack_cp->ch.chunk_length = htons(chk->send_size);
9139         SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
9140         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9141         chk->asoc->ctrl_queue_cnt++;
9142         return;
9143 }
9144
9145 void
9146 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
9147 {
9148         /* formulate and queue a SHUTDOWN to the sender */
9149         struct mbuf *m_shutdown;
9150         struct sctp_shutdown_chunk *shutdown_cp;
9151         struct sctp_tmit_chunk *chk;
9152
9153         TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
9154                 if (chk->rec.chunk_id.id == SCTP_SHUTDOWN) {
9155                         /* We already have a SHUTDOWN queued. Reuse it. */
9156                         if (chk->whoTo) {
9157                                 sctp_free_remote_addr(chk->whoTo);
9158                                 chk->whoTo = NULL;
9159                         }
9160                         break;
9161                 }
9162         }
9163         if (chk == NULL) {
9164                 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9165                 if (m_shutdown == NULL) {
9166                         /* no mbuf's */
9167                         return;
9168                 }
9169                 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
9170                 sctp_alloc_a_chunk(stcb, chk);
9171                 if (chk == NULL) {
9172                         /* no memory */
9173                         sctp_m_freem(m_shutdown);
9174                         return;
9175                 }
9176                 chk->copy_by_ref = 0;
9177                 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
9178                 chk->rec.chunk_id.can_take_data = 1;
9179                 chk->flags = 0;
9180                 chk->send_size = sizeof(struct sctp_shutdown_chunk);
9181                 chk->sent = SCTP_DATAGRAM_UNSENT;
9182                 chk->snd_count = 0;
9183                 chk->asoc = &stcb->asoc;
9184                 chk->data = m_shutdown;
9185                 chk->whoTo = net;
9186                 if (chk->whoTo) {
9187                         atomic_add_int(&chk->whoTo->ref_count, 1);
9188                 }
9189                 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
9190                 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
9191                 shutdown_cp->ch.chunk_flags = 0;
9192                 shutdown_cp->ch.chunk_length = htons(chk->send_size);
9193                 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9194                 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
9195                 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9196                 chk->asoc->ctrl_queue_cnt++;
9197         } else {
9198                 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, sctp_next);
9199                 chk->whoTo = net;
9200                 if (chk->whoTo) {
9201                         atomic_add_int(&chk->whoTo->ref_count, 1);
9202                 }
9203                 shutdown_cp = mtod(chk->data, struct sctp_shutdown_chunk *);
9204                 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9205                 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9206         }
9207         return;
9208 }
9209
9210 void
9211 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
9212 {
9213         /*
9214          * formulate and queue an ASCONF to the peer. ASCONF parameters
9215          * should be queued on the assoc queue.
9216          */
9217         struct sctp_tmit_chunk *chk;
9218         struct mbuf *m_asconf;
9219         int len;
9220
9221         SCTP_TCB_LOCK_ASSERT(stcb);
9222
9223         if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
9224             (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
9225                 /* can't send a new one if there is one in flight already */
9226                 return;
9227         }
9228
9229         /* compose an ASCONF chunk, maximum length is PMTU */
9230         m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
9231         if (m_asconf == NULL) {
9232                 return;
9233         }
9234
9235         sctp_alloc_a_chunk(stcb, chk);
9236         if (chk == NULL) {
9237                 /* no memory */
9238                 sctp_m_freem(m_asconf);
9239                 return;
9240         }
9241
9242         chk->copy_by_ref = 0;
9243         chk->rec.chunk_id.id = SCTP_ASCONF;
9244         chk->rec.chunk_id.can_take_data = 0;
9245         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9246         chk->data = m_asconf;
9247         chk->send_size = len;
9248         chk->sent = SCTP_DATAGRAM_UNSENT;
9249         chk->snd_count = 0;
9250         chk->asoc = &stcb->asoc;
9251         chk->whoTo = net;
9252         if (chk->whoTo) {
9253                 atomic_add_int(&chk->whoTo->ref_count, 1);
9254         }
9255         TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
9256         chk->asoc->ctrl_queue_cnt++;
9257         return;
9258 }
9259
9260 void
9261 sctp_send_asconf_ack(struct sctp_tcb *stcb)
9262 {
9263         /*
9264          * formulate and queue a asconf-ack back to sender. the asconf-ack
9265          * must be stored in the tcb.
9266          */
9267         struct sctp_tmit_chunk *chk;
9268         struct sctp_asconf_ack *ack, *latest_ack;
9269         struct mbuf *m_ack;
9270         struct sctp_nets *net = NULL;
9271
9272         SCTP_TCB_LOCK_ASSERT(stcb);
9273         /* Get the latest ASCONF-ACK */
9274         latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
9275         if (latest_ack == NULL) {
9276                 return;
9277         }
9278         if (latest_ack->last_sent_to != NULL &&
9279             latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
9280                 /* we're doing a retransmission */
9281                 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
9282                 if (net == NULL) {
9283                         /* no alternate */
9284                         if (stcb->asoc.last_control_chunk_from == NULL) {
9285                                 if (stcb->asoc.alternate) {
9286                                         net = stcb->asoc.alternate;
9287                                 } else {
9288                                         net = stcb->asoc.primary_destination;
9289                                 }
9290                         } else {
9291                                 net = stcb->asoc.last_control_chunk_from;
9292                         }
9293                 }
9294         } else {
9295                 /* normal case */
9296                 if (stcb->asoc.last_control_chunk_from == NULL) {
9297                         if (stcb->asoc.alternate) {
9298                                 net = stcb->asoc.alternate;
9299                         } else {
9300                                 net = stcb->asoc.primary_destination;
9301                         }
9302                 } else {
9303                         net = stcb->asoc.last_control_chunk_from;
9304                 }
9305         }
9306         latest_ack->last_sent_to = net;
9307
9308         TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
9309                 if (ack->data == NULL) {
9310                         continue;
9311                 }
9312
9313                 /* copy the asconf_ack */
9314                 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_NOWAIT);
9315                 if (m_ack == NULL) {
9316                         /* couldn't copy it */
9317                         return;
9318                 }
9319 #ifdef SCTP_MBUF_LOGGING
9320                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9321                         sctp_log_mbc(m_ack, SCTP_MBUF_ICOPY);
9322                 }
9323 #endif
9324
9325                 sctp_alloc_a_chunk(stcb, chk);
9326                 if (chk == NULL) {
9327                         /* no memory */
9328                         if (m_ack)
9329                                 sctp_m_freem(m_ack);
9330                         return;
9331                 }
9332                 chk->copy_by_ref = 0;
9333                 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
9334                 chk->rec.chunk_id.can_take_data = 1;
9335                 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9336                 chk->whoTo = net;
9337                 if (chk->whoTo) {
9338                         atomic_add_int(&chk->whoTo->ref_count, 1);
9339                 }
9340                 chk->data = m_ack;
9341                 chk->send_size = ack->len;
9342                 chk->sent = SCTP_DATAGRAM_UNSENT;
9343                 chk->snd_count = 0;
9344                 chk->asoc = &stcb->asoc;
9345
9346                 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9347                 chk->asoc->ctrl_queue_cnt++;
9348         }
9349         return;
9350 }
9351
9352 static int
9353 sctp_chunk_retransmission(struct sctp_inpcb *inp,
9354     struct sctp_tcb *stcb,
9355     struct sctp_association *asoc,
9356     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked)
9357 {
9358         /*-
9359          * send out one MTU of retransmission. If fast_retransmit is
9360          * happening we ignore the cwnd. Otherwise we obey the cwnd and
9361          * rwnd. For a Cookie or Asconf in the control chunk queue we
9362          * retransmit them by themselves.
9363          *
9364          * For data chunks we will pick out the lowest TSN's in the sent_queue
9365          * marked for resend and bundle them all together (up to a MTU of
9366          * destination). The address to send to should have been
9367          * selected/changed where the retransmission was marked (i.e. in FR
9368          * or t3-timeout routines).
9369          */
9370         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9371         struct sctp_tmit_chunk *chk, *fwd;
9372         struct mbuf *m, *endofchain;
9373         struct sctp_nets *net = NULL;
9374         uint32_t tsns_sent = 0;
9375         int no_fragmentflg, bundle_at;
9376         unsigned int mtu;
9377         int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
9378         struct sctp_auth_chunk *auth = NULL;
9379         uint32_t auth_offset = 0;
9380         uint16_t auth_keyid;
9381         int override_ok = 1;
9382         int data_auth_reqd = 0;
9383         uint32_t dmtu = 0;
9384
9385         SCTP_TCB_LOCK_ASSERT(stcb);
9386         tmr_started = ctl_cnt = 0;
9387         no_fragmentflg = 1;
9388         fwd_tsn = 0;
9389         *cnt_out = 0;
9390         fwd = NULL;
9391         endofchain = m = NULL;
9392         auth_keyid = stcb->asoc.authinfo.active_keyid;
9393 #ifdef SCTP_AUDITING_ENABLED
9394         sctp_audit_log(0xC3, 1);
9395 #endif
9396         if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
9397             (TAILQ_EMPTY(&asoc->control_send_queue))) {
9398                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
9399                     asoc->sent_queue_retran_cnt);
9400                 asoc->sent_queue_cnt = 0;
9401                 asoc->sent_queue_cnt_removeable = 0;
9402                 /* send back 0/0 so we enter normal transmission */
9403                 *cnt_out = 0;
9404                 return (0);
9405         }
9406         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9407                 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
9408                     (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
9409                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
9410                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
9411                                 continue;
9412                         }
9413                         if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
9414                                 if (chk != asoc->str_reset) {
9415                                         /*
9416                                          * not eligible for retran if its
9417                                          * not ours
9418                                          */
9419                                         continue;
9420                                 }
9421                         }
9422                         ctl_cnt++;
9423                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9424                                 fwd_tsn = 1;
9425                         }
9426                         /*
9427                          * Add an AUTH chunk, if chunk requires it save the
9428                          * offset into the chain for AUTH
9429                          */
9430                         if ((auth == NULL) &&
9431                             (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9432                             stcb->asoc.peer_auth_chunks))) {
9433                                 m = sctp_add_auth_chunk(m, &endofchain,
9434                                     &auth, &auth_offset,
9435                                     stcb,
9436                                     chk->rec.chunk_id.id);
9437                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9438                         }
9439                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9440                         break;
9441                 }
9442         }
9443         one_chunk = 0;
9444         /* do we have control chunks to retransmit? */
9445         if (m != NULL) {
9446                 /* Start a timer no matter if we succeed or fail */
9447                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9448                         sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9449                 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9450                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9451                 chk->snd_count++;       /* update our count */
9452                 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9453                     (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9454                     auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9455                     no_fragmentflg, 0, 0,
9456                     inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9457                     chk->whoTo->port, NULL,
9458                     0, 0,
9459                     so_locked))) {
9460                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9461                         if (error == ENOBUFS) {
9462                                 asoc->ifp_had_enobuf = 1;
9463                                 SCTP_STAT_INCR(sctps_lowlevelerr);
9464                         }
9465                         return (error);
9466                 } else {
9467                         asoc->ifp_had_enobuf = 0;
9468                 }
9469                 endofchain = NULL;
9470                 auth = NULL;
9471                 auth_offset = 0;
9472                 /*
9473                  * We don't want to mark the net->sent time here since this
9474                  * we use this for HB and retrans cannot measure RTT
9475                  */
9476                 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9477                 *cnt_out += 1;
9478                 chk->sent = SCTP_DATAGRAM_SENT;
9479                 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9480                 if (fwd_tsn == 0) {
9481                         return (0);
9482                 } else {
9483                         /* Clean up the fwd-tsn list */
9484                         sctp_clean_up_ctl(stcb, asoc, so_locked);
9485                         return (0);
9486                 }
9487         }
9488         /*
9489          * Ok, it is just data retransmission we need to do or that and a
9490          * fwd-tsn with it all.
9491          */
9492         if (TAILQ_EMPTY(&asoc->sent_queue)) {
9493                 return (SCTP_RETRAN_DONE);
9494         }
9495         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) ||
9496             (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT)) {
9497                 /* not yet open, resend the cookie and that is it */
9498                 return (1);
9499         }
9500 #ifdef SCTP_AUDITING_ENABLED
9501         sctp_auditing(20, inp, stcb, NULL);
9502 #endif
9503         data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9504         TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9505                 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9506                         /* No, not sent to this net or not ready for rtx */
9507                         continue;
9508                 }
9509                 if (chk->data == NULL) {
9510                         SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9511                             chk->rec.data.tsn, chk->snd_count, chk->sent);
9512                         continue;
9513                 }
9514                 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9515                     (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9516                         struct mbuf *op_err;
9517                         char msg[SCTP_DIAG_INFO_LEN];
9518
9519                         SCTP_SNPRINTF(msg, sizeof(msg), "TSN %8.8x retransmitted %d times, giving up",
9520                             chk->rec.data.tsn, chk->snd_count);
9521                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
9522                             msg);
9523                         atomic_add_int(&stcb->asoc.refcnt, 1);
9524                         sctp_abort_an_association(stcb->sctp_ep, stcb, op_err,
9525                             false, so_locked);
9526                         SCTP_TCB_LOCK(stcb);
9527                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
9528                         return (SCTP_RETRAN_EXIT);
9529                 }
9530                 /* pick up the net */
9531                 net = chk->whoTo;
9532                 switch (net->ro._l_addr.sa.sa_family) {
9533 #ifdef INET
9534                 case AF_INET:
9535                         mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9536                         break;
9537 #endif
9538 #ifdef INET6
9539                 case AF_INET6:
9540                         mtu = net->mtu - SCTP_MIN_OVERHEAD;
9541                         break;
9542 #endif
9543                 default:
9544                         /* TSNH */
9545                         mtu = net->mtu;
9546                         break;
9547                 }
9548
9549                 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9550                         /* No room in peers rwnd */
9551                         uint32_t tsn;
9552
9553                         tsn = asoc->last_acked_seq + 1;
9554                         if (tsn == chk->rec.data.tsn) {
9555                                 /*
9556                                  * we make a special exception for this
9557                                  * case. The peer has no rwnd but is missing
9558                                  * the lowest chunk.. which is probably what
9559                                  * is holding up the rwnd.
9560                                  */
9561                                 goto one_chunk_around;
9562                         }
9563                         return (1);
9564                 }
9565 one_chunk_around:
9566                 if (asoc->peers_rwnd < mtu) {
9567                         one_chunk = 1;
9568                         if ((asoc->peers_rwnd == 0) &&
9569                             (asoc->total_flight == 0)) {
9570                                 chk->window_probe = 1;
9571                                 chk->whoTo->window_probe = 1;
9572                         }
9573                 }
9574 #ifdef SCTP_AUDITING_ENABLED
9575                 sctp_audit_log(0xC3, 2);
9576 #endif
9577                 bundle_at = 0;
9578                 m = NULL;
9579                 net->fast_retran_ip = 0;
9580                 if (chk->rec.data.doing_fast_retransmit == 0) {
9581                         /*
9582                          * if no FR in progress skip destination that have
9583                          * flight_size > cwnd.
9584                          */
9585                         if (net->flight_size >= net->cwnd) {
9586                                 continue;
9587                         }
9588                 } else {
9589                         /*
9590                          * Mark the destination net to have FR recovery
9591                          * limits put on it.
9592                          */
9593                         *fr_done = 1;
9594                         net->fast_retran_ip = 1;
9595                 }
9596
9597                 /*
9598                  * if no AUTH is yet included and this chunk requires it,
9599                  * make sure to account for it.  We don't apply the size
9600                  * until the AUTH chunk is actually added below in case
9601                  * there is no room for this chunk.
9602                  */
9603                 if (data_auth_reqd && (auth == NULL)) {
9604                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9605                 } else
9606                         dmtu = 0;
9607
9608                 if ((chk->send_size <= (mtu - dmtu)) ||
9609                     (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9610                         /* ok we will add this one */
9611                         if (data_auth_reqd) {
9612                                 if (auth == NULL) {
9613                                         m = sctp_add_auth_chunk(m,
9614                                             &endofchain,
9615                                             &auth,
9616                                             &auth_offset,
9617                                             stcb,
9618                                             SCTP_DATA);
9619                                         auth_keyid = chk->auth_keyid;
9620                                         override_ok = 0;
9621                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9622                                 } else if (override_ok) {
9623                                         auth_keyid = chk->auth_keyid;
9624                                         override_ok = 0;
9625                                 } else if (chk->auth_keyid != auth_keyid) {
9626                                         /* different keyid, so done bundling */
9627                                         break;
9628                                 }
9629                         }
9630                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9631                         if (m == NULL) {
9632                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9633                                 return (ENOMEM);
9634                         }
9635                         /* Do clear IP_DF ? */
9636                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9637                                 no_fragmentflg = 0;
9638                         }
9639                         /* update our MTU size */
9640                         if (mtu > (chk->send_size + dmtu))
9641                                 mtu -= (chk->send_size + dmtu);
9642                         else
9643                                 mtu = 0;
9644                         data_list[bundle_at++] = chk;
9645                         if (one_chunk && (asoc->total_flight <= 0)) {
9646                                 SCTP_STAT_INCR(sctps_windowprobed);
9647                         }
9648                 }
9649                 if (one_chunk == 0) {
9650                         /*
9651                          * now are there anymore forward from chk to pick
9652                          * up?
9653                          */
9654                         for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
9655                                 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9656                                         /* Nope, not for retran */
9657                                         continue;
9658                                 }
9659                                 if (fwd->whoTo != net) {
9660                                         /* Nope, not the net in question */
9661                                         continue;
9662                                 }
9663                                 if (data_auth_reqd && (auth == NULL)) {
9664                                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9665                                 } else
9666                                         dmtu = 0;
9667                                 if (fwd->send_size <= (mtu - dmtu)) {
9668                                         if (data_auth_reqd) {
9669                                                 if (auth == NULL) {
9670                                                         m = sctp_add_auth_chunk(m,
9671                                                             &endofchain,
9672                                                             &auth,
9673                                                             &auth_offset,
9674                                                             stcb,
9675                                                             SCTP_DATA);
9676                                                         auth_keyid = fwd->auth_keyid;
9677                                                         override_ok = 0;
9678                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9679                                                 } else if (override_ok) {
9680                                                         auth_keyid = fwd->auth_keyid;
9681                                                         override_ok = 0;
9682                                                 } else if (fwd->auth_keyid != auth_keyid) {
9683                                                         /*
9684                                                          * different keyid,
9685                                                          * so done bundling
9686                                                          */
9687                                                         break;
9688                                                 }
9689                                         }
9690                                         m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9691                                         if (m == NULL) {
9692                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9693                                                 return (ENOMEM);
9694                                         }
9695                                         /* Do clear IP_DF ? */
9696                                         if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9697                                                 no_fragmentflg = 0;
9698                                         }
9699                                         /* update our MTU size */
9700                                         if (mtu > (fwd->send_size + dmtu))
9701                                                 mtu -= (fwd->send_size + dmtu);
9702                                         else
9703                                                 mtu = 0;
9704                                         data_list[bundle_at++] = fwd;
9705                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9706                                                 break;
9707                                         }
9708                                 } else {
9709                                         /* can't fit so we are done */
9710                                         break;
9711                                 }
9712                         }
9713                 }
9714                 /* Is there something to send for this destination? */
9715                 if (m) {
9716                         /*
9717                          * No matter if we fail/or succeed we should start a
9718                          * timer. A failure is like a lost IP packet :-)
9719                          */
9720                         if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9721                                 /*
9722                                  * no timer running on this destination
9723                                  * restart it.
9724                                  */
9725                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9726                                 tmr_started = 1;
9727                         }
9728                         /* Now lets send it, if there is anything to send :> */
9729                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9730                             (struct sockaddr *)&net->ro._l_addr, m,
9731                             auth_offset, auth, auth_keyid,
9732                             no_fragmentflg, 0, 0,
9733                             inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9734                             net->port, NULL,
9735                             0, 0,
9736                             so_locked))) {
9737                                 /* error, we could not output */
9738                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9739                                 if (error == ENOBUFS) {
9740                                         asoc->ifp_had_enobuf = 1;
9741                                         SCTP_STAT_INCR(sctps_lowlevelerr);
9742                                 }
9743                                 return (error);
9744                         } else {
9745                                 asoc->ifp_had_enobuf = 0;
9746                         }
9747                         endofchain = NULL;
9748                         auth = NULL;
9749                         auth_offset = 0;
9750                         /* For HB's */
9751                         /*
9752                          * We don't want to mark the net->sent time here
9753                          * since this we use this for HB and retrans cannot
9754                          * measure RTT
9755                          */
9756                         /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9757
9758                         /* For auto-close */
9759                         if (*now_filled == 0) {
9760                                 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9761                                 *now = asoc->time_last_sent;
9762                                 *now_filled = 1;
9763                         } else {
9764                                 asoc->time_last_sent = *now;
9765                         }
9766                         *cnt_out += bundle_at;
9767 #ifdef SCTP_AUDITING_ENABLED
9768                         sctp_audit_log(0xC4, bundle_at);
9769 #endif
9770                         if (bundle_at) {
9771                                 tsns_sent = data_list[0]->rec.data.tsn;
9772                         }
9773                         for (i = 0; i < bundle_at; i++) {
9774                                 SCTP_STAT_INCR(sctps_sendretransdata);
9775                                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9776                                 /*
9777                                  * When we have a revoked data, and we
9778                                  * retransmit it, then we clear the revoked
9779                                  * flag since this flag dictates if we
9780                                  * subtracted from the fs
9781                                  */
9782                                 if (data_list[i]->rec.data.chunk_was_revoked) {
9783                                         /* Deflate the cwnd */
9784                                         data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9785                                         data_list[i]->rec.data.chunk_was_revoked = 0;
9786                                 }
9787                                 data_list[i]->snd_count++;
9788                                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9789                                 /* record the time */
9790                                 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9791                                 if (data_list[i]->book_size_scale) {
9792                                         /*
9793                                          * need to double the book size on
9794                                          * this one
9795                                          */
9796                                         data_list[i]->book_size_scale = 0;
9797                                         /*
9798                                          * Since we double the booksize, we
9799                                          * must also double the output queue
9800                                          * size, since this get shrunk when
9801                                          * we free by this amount.
9802                                          */
9803                                         atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9804                                         data_list[i]->book_size *= 2;
9805                                 } else {
9806                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9807                                                 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9808                                                     asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9809                                         }
9810                                         asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9811                                             (uint32_t)(data_list[i]->send_size +
9812                                             SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9813                                 }
9814                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9815                                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9816                                             data_list[i]->whoTo->flight_size,
9817                                             data_list[i]->book_size,
9818                                             (uint32_t)(uintptr_t)data_list[i]->whoTo,
9819                                             data_list[i]->rec.data.tsn);
9820                                 }
9821                                 sctp_flight_size_increase(data_list[i]);
9822                                 sctp_total_flight_increase(stcb, data_list[i]);
9823                                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9824                                         /* SWS sender side engages */
9825                                         asoc->peers_rwnd = 0;
9826                                 }
9827                                 if ((i == 0) &&
9828                                     (data_list[i]->rec.data.doing_fast_retransmit)) {
9829                                         SCTP_STAT_INCR(sctps_sendfastretrans);
9830                                         if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9831                                             (tmr_started == 0)) {
9832                                                 /*-
9833                                                  * ok we just fast-retrans'd
9834                                                  * the lowest TSN, i.e the
9835                                                  * first on the list. In
9836                                                  * this case we want to give
9837                                                  * some more time to get a
9838                                                  * SACK back without a
9839                                                  * t3-expiring.
9840                                                  */
9841                                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9842                                                     SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
9843                                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9844                                         }
9845                                 }
9846                         }
9847                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9848                                 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9849                         }
9850 #ifdef SCTP_AUDITING_ENABLED
9851                         sctp_auditing(21, inp, stcb, NULL);
9852 #endif
9853                 } else {
9854                         /* None will fit */
9855                         return (1);
9856                 }
9857                 if (asoc->sent_queue_retran_cnt <= 0) {
9858                         /* all done we have no more to retran */
9859                         asoc->sent_queue_retran_cnt = 0;
9860                         break;
9861                 }
9862                 if (one_chunk) {
9863                         /* No more room in rwnd */
9864                         return (1);
9865                 }
9866                 /* stop the for loop here. we sent out a packet */
9867                 break;
9868         }
9869         return (0);
9870 }
9871
9872 static void
9873 sctp_timer_validation(struct sctp_inpcb *inp,
9874     struct sctp_tcb *stcb,
9875     struct sctp_association *asoc)
9876 {
9877         struct sctp_nets *net;
9878
9879         /* Validate that a timer is running somewhere */
9880         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9881                 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9882                         /* Here is a timer */
9883                         return;
9884                 }
9885         }
9886         SCTP_TCB_LOCK_ASSERT(stcb);
9887         /* Gak, we did not have a timer somewhere */
9888         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9889         if (asoc->alternate) {
9890                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
9891         } else {
9892                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9893         }
9894         return;
9895 }
9896
9897 void
9898 sctp_chunk_output(struct sctp_inpcb *inp,
9899     struct sctp_tcb *stcb,
9900     int from_where,
9901     int so_locked)
9902 {
9903         /*-
9904          * Ok this is the generic chunk service queue. we must do the
9905          * following:
9906          * - See if there are retransmits pending, if so we must
9907          *   do these first.
9908          * - Service the stream queue that is next, moving any
9909          *   message (note I must get a complete message i.e.
9910          *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9911          *   TSN's
9912          * - Check to see if the cwnd/rwnd allows any output, if so we
9913          *   go ahead and formulate and send the low level chunks. Making sure
9914          *   to combine any control in the control chunk queue also.
9915          */
9916         struct sctp_association *asoc;
9917         struct sctp_nets *net;
9918         int error = 0, num_out, tot_out = 0, ret = 0, reason_code;
9919         unsigned int burst_cnt = 0;
9920         struct timeval now;
9921         int now_filled = 0;
9922         int nagle_on;
9923         uint32_t frag_point = sctp_get_frag_point(stcb);
9924         int un_sent = 0;
9925         int fr_done;
9926         unsigned int tot_frs = 0;
9927
9928         asoc = &stcb->asoc;
9929 do_it_again:
9930         /* The Nagle algorithm is only applied when handling a send call. */
9931         if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9932                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9933                         nagle_on = 0;
9934                 } else {
9935                         nagle_on = 1;
9936                 }
9937         } else {
9938                 nagle_on = 0;
9939         }
9940         SCTP_TCB_LOCK_ASSERT(stcb);
9941
9942         un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9943
9944         if ((un_sent <= 0) &&
9945             (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9946             (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9947             (asoc->sent_queue_retran_cnt == 0) &&
9948             (asoc->trigger_reset == 0)) {
9949                 /* Nothing to do unless there is something to be sent left */
9950                 return;
9951         }
9952         /*
9953          * Do we have something to send, data or control AND a sack timer
9954          * running, if so piggy-back the sack.
9955          */
9956         if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9957                 sctp_send_sack(stcb, so_locked);
9958                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
9959                     SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
9960         }
9961         while (asoc->sent_queue_retran_cnt) {
9962                 /*-
9963                  * Ok, it is retransmission time only, we send out only ONE
9964                  * packet with a single call off to the retran code.
9965                  */
9966                 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9967                         /*-
9968                          * Special hook for handling cookies discarded
9969                          * by peer that carried data. Send cookie-ack only
9970                          * and then the next call with get the retran's.
9971                          */
9972                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9973                             from_where,
9974                             &now, &now_filled, frag_point, so_locked);
9975                         return;
9976                 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9977                         /* if its not from a HB then do it */
9978                         fr_done = 0;
9979                         ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9980                         if (fr_done) {
9981                                 tot_frs++;
9982                         }
9983                 } else {
9984                         /*
9985                          * its from any other place, we don't allow retran
9986                          * output (only control)
9987                          */
9988                         ret = 1;
9989                 }
9990                 if (ret > 0) {
9991                         /* Can't send anymore */
9992                         /*-
9993                          * now lets push out control by calling med-level
9994                          * output once. this assures that we WILL send HB's
9995                          * if queued too.
9996                          */
9997                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9998                             from_where,
9999                             &now, &now_filled, frag_point, so_locked);
10000 #ifdef SCTP_AUDITING_ENABLED
10001                         sctp_auditing(8, inp, stcb, NULL);
10002 #endif
10003                         sctp_timer_validation(inp, stcb, asoc);
10004                         return;
10005                 }
10006                 if (ret < 0) {
10007                         /*-
10008                          * The count was off.. retran is not happening so do
10009                          * the normal retransmission.
10010                          */
10011 #ifdef SCTP_AUDITING_ENABLED
10012                         sctp_auditing(9, inp, stcb, NULL);
10013 #endif
10014                         if (ret == SCTP_RETRAN_EXIT) {
10015                                 return;
10016                         }
10017                         break;
10018                 }
10019                 if (from_where == SCTP_OUTPUT_FROM_T3) {
10020                         /* Only one transmission allowed out of a timeout */
10021 #ifdef SCTP_AUDITING_ENABLED
10022                         sctp_auditing(10, inp, stcb, NULL);
10023 #endif
10024                         /* Push out any control */
10025                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
10026                             &now, &now_filled, frag_point, so_locked);
10027                         return;
10028                 }
10029                 if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
10030                         /* Hit FR burst limit */
10031                         return;
10032                 }
10033                 if ((num_out == 0) && (ret == 0)) {
10034                         /* No more retrans to send */
10035                         break;
10036                 }
10037         }
10038 #ifdef SCTP_AUDITING_ENABLED
10039         sctp_auditing(12, inp, stcb, NULL);
10040 #endif
10041         /* Check for bad destinations, if they exist move chunks around. */
10042         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10043                 if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10044                         /*-
10045                          * if possible move things off of this address we
10046                          * still may send below due to the dormant state but
10047                          * we try to find an alternate address to send to
10048                          * and if we have one we move all queued data on the
10049                          * out wheel to this alternate address.
10050                          */
10051                         if (net->ref_count > 1)
10052                                 sctp_move_chunks_from_net(stcb, net);
10053                 } else {
10054                         /*-
10055                          * if ((asoc->sat_network) || (net->addr_is_local))
10056                          * { burst_limit = asoc->max_burst *
10057                          * SCTP_SAT_NETWORK_BURST_INCR; }
10058                          */
10059                         if (asoc->max_burst > 0) {
10060                                 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10061                                         if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10062                                                 /*
10063                                                  * JRS - Use the congestion
10064                                                  * control given in the
10065                                                  * congestion control module
10066                                                  */
10067                                                 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10068                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10069                                                         sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10070                                                 }
10071                                                 SCTP_STAT_INCR(sctps_maxburstqueued);
10072                                         }
10073                                         net->fast_retran_ip = 0;
10074                                 } else {
10075                                         if (net->flight_size == 0) {
10076                                                 /*
10077                                                  * Should be decaying the
10078                                                  * cwnd here
10079                                                  */
10080                                                 ;
10081                                         }
10082                                 }
10083                         }
10084                 }
10085         }
10086         burst_cnt = 0;
10087         do {
10088                 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10089                     &reason_code, 0, from_where,
10090                     &now, &now_filled, frag_point, so_locked);
10091                 if (error) {
10092                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10093                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10094                                 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10095                         }
10096                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10097                                 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10098                                 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10099                         }
10100                         break;
10101                 }
10102                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10103
10104                 tot_out += num_out;
10105                 burst_cnt++;
10106                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10107                         sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10108                         if (num_out == 0) {
10109                                 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10110                         }
10111                 }
10112                 if (nagle_on) {
10113                         /*
10114                          * When the Nagle algorithm is used, look at how
10115                          * much is unsent, then if its smaller than an MTU
10116                          * and we have data in flight we stop, except if we
10117                          * are handling a fragmented user message.
10118                          */
10119                         un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight;
10120                         if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10121                             (stcb->asoc.total_flight > 0)) {
10122 /*      &&                   sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {*/
10123                                 break;
10124                         }
10125                 }
10126                 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10127                     TAILQ_EMPTY(&asoc->send_queue) &&
10128                     sctp_is_there_unsent_data(stcb, so_locked) == 0) {
10129                         /* Nothing left to send */
10130                         break;
10131                 }
10132                 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10133                         /* Nothing left to send */
10134                         break;
10135                 }
10136         } while (num_out &&
10137             ((asoc->max_burst == 0) ||
10138             SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10139             (burst_cnt < asoc->max_burst)));
10140
10141         if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10142                 if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10143                         SCTP_STAT_INCR(sctps_maxburstqueued);
10144                         asoc->burst_limit_applied = 1;
10145                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10146                                 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10147                         }
10148                 } else {
10149                         asoc->burst_limit_applied = 0;
10150                 }
10151         }
10152         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10153                 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10154         }
10155         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10156             tot_out);
10157
10158         /*-
10159          * Now we need to clean up the control chunk chain if a ECNE is on
10160          * it. It must be marked as UNSENT again so next call will continue
10161          * to send it until such time that we get a CWR, to remove it.
10162          */
10163         if (stcb->asoc.ecn_echo_cnt_onq)
10164                 sctp_fix_ecn_echo(asoc);
10165
10166         if (stcb->asoc.trigger_reset) {
10167                 if (sctp_send_stream_reset_out_if_possible(stcb, so_locked) == 0) {
10168                         goto do_it_again;
10169                 }
10170         }
10171         return;
10172 }
10173
10174 int
10175 sctp_output(
10176     struct sctp_inpcb *inp,
10177     struct mbuf *m,
10178     struct sockaddr *addr,
10179     struct mbuf *control,
10180     struct thread *p,
10181     int flags)
10182 {
10183         if (inp == NULL) {
10184                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10185                 return (EINVAL);
10186         }
10187
10188         if (inp->sctp_socket == NULL) {
10189                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10190                 return (EINVAL);
10191         }
10192         return (sctp_sosend(inp->sctp_socket,
10193             addr,
10194             (struct uio *)NULL,
10195             m,
10196             control,
10197             flags, p
10198             ));
10199 }
10200
10201 void
10202 send_forward_tsn(struct sctp_tcb *stcb,
10203     struct sctp_association *asoc)
10204 {
10205         struct sctp_tmit_chunk *chk, *at, *tp1, *last;
10206         struct sctp_forward_tsn_chunk *fwdtsn;
10207         struct sctp_strseq *strseq;
10208         struct sctp_strseq_mid *strseq_m;
10209         uint32_t advance_peer_ack_point;
10210         unsigned int cnt_of_space, i, ovh;
10211         unsigned int space_needed;
10212         unsigned int cnt_of_skipped = 0;
10213
10214         SCTP_TCB_LOCK_ASSERT(stcb);
10215         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10216                 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10217                         /* mark it to unsent */
10218                         chk->sent = SCTP_DATAGRAM_UNSENT;
10219                         chk->snd_count = 0;
10220                         /* Do we correct its output location? */
10221                         if (chk->whoTo) {
10222                                 sctp_free_remote_addr(chk->whoTo);
10223                                 chk->whoTo = NULL;
10224                         }
10225                         goto sctp_fill_in_rest;
10226                 }
10227         }
10228         /* Ok if we reach here we must build one */
10229         sctp_alloc_a_chunk(stcb, chk);
10230         if (chk == NULL) {
10231                 return;
10232         }
10233         asoc->fwd_tsn_cnt++;
10234         chk->copy_by_ref = 0;
10235         /*
10236          * We don't do the old thing here since this is used not for on-wire
10237          * but to tell if we are sending a fwd-tsn by the stack during
10238          * output. And if its a IFORWARD or a FORWARD it is a fwd-tsn.
10239          */
10240         chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10241         chk->rec.chunk_id.can_take_data = 0;
10242         chk->flags = 0;
10243         chk->asoc = asoc;
10244         chk->whoTo = NULL;
10245         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
10246         if (chk->data == NULL) {
10247                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10248                 return;
10249         }
10250         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10251         chk->sent = SCTP_DATAGRAM_UNSENT;
10252         chk->snd_count = 0;
10253         TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10254         asoc->ctrl_queue_cnt++;
10255 sctp_fill_in_rest:
10256         /*-
10257          * Here we go through and fill out the part that deals with
10258          * stream/seq of the ones we skip.
10259          */
10260         SCTP_BUF_LEN(chk->data) = 0;
10261         TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10262                 if ((at->sent != SCTP_FORWARD_TSN_SKIP) &&
10263                     (at->sent != SCTP_DATAGRAM_NR_ACKED)) {
10264                         /* no more to look at */
10265                         break;
10266                 }
10267                 if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
10268                         /* We don't report these */
10269                         continue;
10270                 }
10271                 cnt_of_skipped++;
10272         }
10273         if (asoc->idata_supported) {
10274                 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10275                     (cnt_of_skipped * sizeof(struct sctp_strseq_mid)));
10276         } else {
10277                 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10278                     (cnt_of_skipped * sizeof(struct sctp_strseq)));
10279         }
10280         cnt_of_space = (unsigned int)M_TRAILINGSPACE(chk->data);
10281
10282         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10283                 ovh = SCTP_MIN_OVERHEAD;
10284         } else {
10285                 ovh = SCTP_MIN_V4_OVERHEAD;
10286         }
10287         if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
10288                 /* trim to a mtu size */
10289                 cnt_of_space = asoc->smallest_mtu - ovh;
10290         }
10291         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10292                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10293                     0xff, 0, cnt_of_skipped,
10294                     asoc->advanced_peer_ack_point);
10295         }
10296         advance_peer_ack_point = asoc->advanced_peer_ack_point;
10297         if (cnt_of_space < space_needed) {
10298                 /*-
10299                  * ok we must trim down the chunk by lowering the
10300                  * advance peer ack point.
10301                  */
10302                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10303                         sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10304                             0xff, 0xff, cnt_of_space,
10305                             space_needed);
10306                 }
10307                 cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
10308                 if (asoc->idata_supported) {
10309                         cnt_of_skipped /= sizeof(struct sctp_strseq_mid);
10310                 } else {
10311                         cnt_of_skipped /= sizeof(struct sctp_strseq);
10312                 }
10313                 /*-
10314                  * Go through and find the TSN that will be the one
10315                  * we report.
10316                  */
10317                 at = TAILQ_FIRST(&asoc->sent_queue);
10318                 if (at != NULL) {
10319                         for (i = 0; i < cnt_of_skipped; i++) {
10320                                 tp1 = TAILQ_NEXT(at, sctp_next);
10321                                 if (tp1 == NULL) {
10322                                         break;
10323                                 }
10324                                 at = tp1;
10325                         }
10326                 }
10327                 if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10328                         sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10329                             0xff, cnt_of_skipped, at->rec.data.tsn,
10330                             asoc->advanced_peer_ack_point);
10331                 }
10332                 last = at;
10333                 /*-
10334                  * last now points to last one I can report, update
10335                  * peer ack point
10336                  */
10337                 if (last) {
10338                         advance_peer_ack_point = last->rec.data.tsn;
10339                 }
10340                 if (asoc->idata_supported) {
10341                         space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10342                             cnt_of_skipped * sizeof(struct sctp_strseq_mid);
10343                 } else {
10344                         space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10345                             cnt_of_skipped * sizeof(struct sctp_strseq);
10346                 }
10347         }
10348         chk->send_size = space_needed;
10349         /* Setup the chunk */
10350         fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
10351         fwdtsn->ch.chunk_length = htons(chk->send_size);
10352         fwdtsn->ch.chunk_flags = 0;
10353         if (asoc->idata_supported) {
10354                 fwdtsn->ch.chunk_type = SCTP_IFORWARD_CUM_TSN;
10355         } else {
10356                 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
10357         }
10358         fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
10359         SCTP_BUF_LEN(chk->data) = chk->send_size;
10360         fwdtsn++;
10361         /*-
10362          * Move pointer to after the fwdtsn and transfer to the
10363          * strseq pointer.
10364          */
10365         if (asoc->idata_supported) {
10366                 strseq_m = (struct sctp_strseq_mid *)fwdtsn;
10367                 strseq = NULL;
10368         } else {
10369                 strseq = (struct sctp_strseq *)fwdtsn;
10370                 strseq_m = NULL;
10371         }
10372         /*-
10373          * Now populate the strseq list. This is done blindly
10374          * without pulling out duplicate stream info. This is
10375          * inefficient but won't harm the process since the peer will
10376          * look at these in sequence and will thus release anything.
10377          * It could mean we exceed the PMTU and chop off some that
10378          * we could have included.. but this is unlikely (aka 1432/4
10379          * would mean 300+ stream seq's would have to be reported in
10380          * one FWD-TSN. With a bit of work we can later FIX this to
10381          * optimize and pull out duplicates.. but it does add more
10382          * overhead. So for now... not!
10383          */
10384         i = 0;
10385         TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10386                 if (i >= cnt_of_skipped) {
10387                         break;
10388                 }
10389                 if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
10390                         /* We don't report these */
10391                         continue;
10392                 }
10393                 if (at->rec.data.tsn == advance_peer_ack_point) {
10394                         at->rec.data.fwd_tsn_cnt = 0;
10395                 }
10396                 if (asoc->idata_supported) {
10397                         strseq_m->sid = htons(at->rec.data.sid);
10398                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10399                                 strseq_m->flags = htons(PR_SCTP_UNORDERED_FLAG);
10400                         } else {
10401                                 strseq_m->flags = 0;
10402                         }
10403                         strseq_m->mid = htonl(at->rec.data.mid);
10404                         strseq_m++;
10405                 } else {
10406                         strseq->sid = htons(at->rec.data.sid);
10407                         strseq->ssn = htons((uint16_t)at->rec.data.mid);
10408                         strseq++;
10409                 }
10410                 i++;
10411         }
10412         return;
10413 }
10414
10415 void
10416 sctp_send_sack(struct sctp_tcb *stcb, int so_locked)
10417 {
10418         /*-
10419          * Queue up a SACK or NR-SACK in the control queue.
10420          * We must first check to see if a SACK or NR-SACK is
10421          * somehow on the control queue.
10422          * If so, we will take and and remove the old one.
10423          */
10424         struct sctp_association *asoc;
10425         struct sctp_tmit_chunk *chk, *a_chk;
10426         struct sctp_sack_chunk *sack;
10427         struct sctp_nr_sack_chunk *nr_sack;
10428         struct sctp_gap_ack_block *gap_descriptor;
10429         const struct sack_track *selector;
10430         int mergeable = 0;
10431         int offset;
10432         caddr_t limit;
10433         uint32_t *dup;
10434         int limit_reached = 0;
10435         unsigned int i, siz, j;
10436         unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10437         int num_dups = 0;
10438         int space_req;
10439         uint32_t highest_tsn;
10440         uint8_t flags;
10441         uint8_t type;
10442         uint8_t tsn_map;
10443
10444         if (stcb->asoc.nrsack_supported == 1) {
10445                 type = SCTP_NR_SELECTIVE_ACK;
10446         } else {
10447                 type = SCTP_SELECTIVE_ACK;
10448         }
10449         a_chk = NULL;
10450         asoc = &stcb->asoc;
10451         SCTP_TCB_LOCK_ASSERT(stcb);
10452         if (asoc->last_data_chunk_from == NULL) {
10453                 /* Hmm we never received anything */
10454                 return;
10455         }
10456         sctp_slide_mapping_arrays(stcb);
10457         sctp_set_rwnd(stcb, asoc);
10458         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10459                 if (chk->rec.chunk_id.id == type) {
10460                         /* Hmm, found a sack already on queue, remove it */
10461                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10462                         asoc->ctrl_queue_cnt--;
10463                         a_chk = chk;
10464                         if (a_chk->data) {
10465                                 sctp_m_freem(a_chk->data);
10466                                 a_chk->data = NULL;
10467                         }
10468                         if (a_chk->whoTo) {
10469                                 sctp_free_remote_addr(a_chk->whoTo);
10470                                 a_chk->whoTo = NULL;
10471                         }
10472                         break;
10473                 }
10474         }
10475         if (a_chk == NULL) {
10476                 sctp_alloc_a_chunk(stcb, a_chk);
10477                 if (a_chk == NULL) {
10478                         /* No memory so we drop the idea, and set a timer */
10479                         if (stcb->asoc.delayed_ack) {
10480                                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10481                                     stcb->sctp_ep, stcb, NULL,
10482                                     SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
10483                                 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10484                                     stcb->sctp_ep, stcb, NULL);
10485                         } else {
10486                                 stcb->asoc.send_sack = 1;
10487                         }
10488                         return;
10489                 }
10490                 a_chk->copy_by_ref = 0;
10491                 a_chk->rec.chunk_id.id = type;
10492                 a_chk->rec.chunk_id.can_take_data = 1;
10493         }
10494         /* Clear our pkt counts */
10495         asoc->data_pkts_seen = 0;
10496
10497         a_chk->flags = 0;
10498         a_chk->asoc = asoc;
10499         a_chk->snd_count = 0;
10500         a_chk->send_size = 0;   /* fill in later */
10501         a_chk->sent = SCTP_DATAGRAM_UNSENT;
10502         a_chk->whoTo = NULL;
10503
10504         if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10505                 /*-
10506                  * Ok, the destination for the SACK is unreachable, lets see if
10507                  * we can select an alternate to asoc->last_data_chunk_from
10508                  */
10509                 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10510                 if (a_chk->whoTo == NULL) {
10511                         /* Nope, no alternate */
10512                         a_chk->whoTo = asoc->last_data_chunk_from;
10513                 }
10514         } else {
10515                 a_chk->whoTo = asoc->last_data_chunk_from;
10516         }
10517         if (a_chk->whoTo) {
10518                 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10519         }
10520         if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10521                 highest_tsn = asoc->highest_tsn_inside_map;
10522         } else {
10523                 highest_tsn = asoc->highest_tsn_inside_nr_map;
10524         }
10525         if (highest_tsn == asoc->cumulative_tsn) {
10526                 /* no gaps */
10527                 if (type == SCTP_SELECTIVE_ACK) {
10528                         space_req = sizeof(struct sctp_sack_chunk);
10529                 } else {
10530                         space_req = sizeof(struct sctp_nr_sack_chunk);
10531                 }
10532         } else {
10533                 /* gaps get a cluster */
10534                 space_req = MCLBYTES;
10535         }
10536         /* Ok now lets formulate a MBUF with our sack */
10537         a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_NOWAIT, 1, MT_DATA);
10538         if ((a_chk->data == NULL) ||
10539             (a_chk->whoTo == NULL)) {
10540                 /* rats, no mbuf memory */
10541                 if (a_chk->data) {
10542                         /* was a problem with the destination */
10543                         sctp_m_freem(a_chk->data);
10544                         a_chk->data = NULL;
10545                 }
10546                 sctp_free_a_chunk(stcb, a_chk, so_locked);
10547                 /* sa_ignore NO_NULL_CHK */
10548                 if (stcb->asoc.delayed_ack) {
10549                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10550                             stcb->sctp_ep, stcb, NULL,
10551                             SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10552                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10553                             stcb->sctp_ep, stcb, NULL);
10554                 } else {
10555                         stcb->asoc.send_sack = 1;
10556                 }
10557                 return;
10558         }
10559         /* ok, lets go through and fill it in */
10560         SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10561         space = (unsigned int)M_TRAILINGSPACE(a_chk->data);
10562         if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10563                 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10564         }
10565         limit = mtod(a_chk->data, caddr_t);
10566         limit += space;
10567
10568         flags = 0;
10569
10570         if ((asoc->sctp_cmt_on_off > 0) &&
10571             SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10572                 /*-
10573                  * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10574                  * received, then set high bit to 1, else 0. Reset
10575                  * pkts_rcvd.
10576                  */
10577                 flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10578                 asoc->cmt_dac_pkts_rcvd = 0;
10579         }
10580 #ifdef SCTP_ASOCLOG_OF_TSNS
10581         stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10582         stcb->asoc.cumack_log_atsnt++;
10583         if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10584                 stcb->asoc.cumack_log_atsnt = 0;
10585         }
10586 #endif
10587         /* reset the readers interpretation */
10588         stcb->freed_by_sorcv_sincelast = 0;
10589
10590         if (type == SCTP_SELECTIVE_ACK) {
10591                 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10592                 nr_sack = NULL;
10593                 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10594                 if (highest_tsn > asoc->mapping_array_base_tsn) {
10595                         siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10596                 } else {
10597                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + highest_tsn + 7) / 8;
10598                 }
10599         } else {
10600                 sack = NULL;
10601                 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10602                 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10603                 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10604                         siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10605                 } else {
10606                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10607                 }
10608         }
10609
10610         if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10611                 offset = 1;
10612         } else {
10613                 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10614         }
10615         if (((type == SCTP_SELECTIVE_ACK) &&
10616             SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10617             ((type == SCTP_NR_SELECTIVE_ACK) &&
10618             SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10619                 /* we have a gap .. maybe */
10620                 for (i = 0; i < siz; i++) {
10621                         tsn_map = asoc->mapping_array[i];
10622                         if (type == SCTP_SELECTIVE_ACK) {
10623                                 tsn_map |= asoc->nr_mapping_array[i];
10624                         }
10625                         if (i == 0) {
10626                                 /*
10627                                  * Clear all bits corresponding to TSNs
10628                                  * smaller or equal to the cumulative TSN.
10629                                  */
10630                                 tsn_map &= (~0U << (1 - offset));
10631                         }
10632                         selector = &sack_array[tsn_map];
10633                         if (mergeable && selector->right_edge) {
10634                                 /*
10635                                  * Backup, left and right edges were ok to
10636                                  * merge.
10637                                  */
10638                                 num_gap_blocks--;
10639                                 gap_descriptor--;
10640                         }
10641                         if (selector->num_entries == 0)
10642                                 mergeable = 0;
10643                         else {
10644                                 for (j = 0; j < selector->num_entries; j++) {
10645                                         if (mergeable && selector->right_edge) {
10646                                                 /*
10647                                                  * do a merge by NOT setting
10648                                                  * the left side
10649                                                  */
10650                                                 mergeable = 0;
10651                                         } else {
10652                                                 /*
10653                                                  * no merge, set the left
10654                                                  * side
10655                                                  */
10656                                                 mergeable = 0;
10657                                                 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10658                                         }
10659                                         gap_descriptor->end = htons((selector->gaps[j].end + offset));
10660                                         num_gap_blocks++;
10661                                         gap_descriptor++;
10662                                         if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10663                                                 /* no more room */
10664                                                 limit_reached = 1;
10665                                                 break;
10666                                         }
10667                                 }
10668                                 if (selector->left_edge) {
10669                                         mergeable = 1;
10670                                 }
10671                         }
10672                         if (limit_reached) {
10673                                 /* Reached the limit stop */
10674                                 break;
10675                         }
10676                         offset += 8;
10677                 }
10678         }
10679         if ((type == SCTP_NR_SELECTIVE_ACK) &&
10680             (limit_reached == 0)) {
10681                 mergeable = 0;
10682
10683                 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10684                         siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10685                 } else {
10686                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10687                 }
10688
10689                 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10690                         offset = 1;
10691                 } else {
10692                         offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10693                 }
10694                 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10695                         /* we have a gap .. maybe */
10696                         for (i = 0; i < siz; i++) {
10697                                 tsn_map = asoc->nr_mapping_array[i];
10698                                 if (i == 0) {
10699                                         /*
10700                                          * Clear all bits corresponding to
10701                                          * TSNs smaller or equal to the
10702                                          * cumulative TSN.
10703                                          */
10704                                         tsn_map &= (~0U << (1 - offset));
10705                                 }
10706                                 selector = &sack_array[tsn_map];
10707                                 if (mergeable && selector->right_edge) {
10708                                         /*
10709                                          * Backup, left and right edges were
10710                                          * ok to merge.
10711                                          */
10712                                         num_nr_gap_blocks--;
10713                                         gap_descriptor--;
10714                                 }
10715                                 if (selector->num_entries == 0)
10716                                         mergeable = 0;
10717                                 else {
10718                                         for (j = 0; j < selector->num_entries; j++) {
10719                                                 if (mergeable && selector->right_edge) {
10720                                                         /*
10721                                                          * do a merge by NOT
10722                                                          * setting the left
10723                                                          * side
10724                                                          */
10725                                                         mergeable = 0;
10726                                                 } else {
10727                                                         /*
10728                                                          * no merge, set the
10729                                                          * left side
10730                                                          */
10731                                                         mergeable = 0;
10732                                                         gap_descriptor->start = htons((selector->gaps[j].start + offset));
10733                                                 }
10734                                                 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10735                                                 num_nr_gap_blocks++;
10736                                                 gap_descriptor++;
10737                                                 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10738                                                         /* no more room */
10739                                                         limit_reached = 1;
10740                                                         break;
10741                                                 }
10742                                         }
10743                                         if (selector->left_edge) {
10744                                                 mergeable = 1;
10745                                         }
10746                                 }
10747                                 if (limit_reached) {
10748                                         /* Reached the limit stop */
10749                                         break;
10750                                 }
10751                                 offset += 8;
10752                         }
10753                 }
10754         }
10755         /* now we must add any dups we are going to report. */
10756         if ((limit_reached == 0) && (asoc->numduptsns)) {
10757                 dup = (uint32_t *)gap_descriptor;
10758                 for (i = 0; i < asoc->numduptsns; i++) {
10759                         *dup = htonl(asoc->dup_tsns[i]);
10760                         dup++;
10761                         num_dups++;
10762                         if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10763                                 /* no more room */
10764                                 break;
10765                         }
10766                 }
10767                 asoc->numduptsns = 0;
10768         }
10769         /*
10770          * now that the chunk is prepared queue it to the control chunk
10771          * queue.
10772          */
10773         if (type == SCTP_SELECTIVE_ACK) {
10774                 a_chk->send_size = (uint16_t)(sizeof(struct sctp_sack_chunk) +
10775                     (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10776                     num_dups * sizeof(int32_t));
10777                 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10778                 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10779                 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10780                 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10781                 sack->sack.num_dup_tsns = htons(num_dups);
10782                 sack->ch.chunk_type = type;
10783                 sack->ch.chunk_flags = flags;
10784                 sack->ch.chunk_length = htons(a_chk->send_size);
10785         } else {
10786                 a_chk->send_size = (uint16_t)(sizeof(struct sctp_nr_sack_chunk) +
10787                     (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10788                     num_dups * sizeof(int32_t));
10789                 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10790                 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10791                 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10792                 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10793                 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10794                 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10795                 nr_sack->nr_sack.reserved = 0;
10796                 nr_sack->ch.chunk_type = type;
10797                 nr_sack->ch.chunk_flags = flags;
10798                 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10799         }
10800         TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10801         asoc->my_last_reported_rwnd = asoc->my_rwnd;
10802         asoc->ctrl_queue_cnt++;
10803         asoc->send_sack = 0;
10804         SCTP_STAT_INCR(sctps_sendsacks);
10805         return;
10806 }
10807
10808 void
10809 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
10810 {
10811         struct mbuf *m_abort, *m, *m_last;
10812         struct mbuf *m_out, *m_end = NULL;
10813         struct sctp_abort_chunk *abort;
10814         struct sctp_auth_chunk *auth = NULL;
10815         struct sctp_nets *net;
10816         uint32_t vtag;
10817         uint32_t auth_offset = 0;
10818         int error;
10819         uint16_t cause_len, chunk_len, padding_len;
10820
10821         SCTP_TCB_LOCK_ASSERT(stcb);
10822         /*-
10823          * Add an AUTH chunk, if chunk requires it and save the offset into
10824          * the chain for AUTH
10825          */
10826         if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10827             stcb->asoc.peer_auth_chunks)) {
10828                 m_out = sctp_add_auth_chunk(NULL, &m_end, &auth, &auth_offset,
10829                     stcb, SCTP_ABORT_ASSOCIATION);
10830                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10831         } else {
10832                 m_out = NULL;
10833         }
10834         m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER);
10835         if (m_abort == NULL) {
10836                 if (m_out) {
10837                         sctp_m_freem(m_out);
10838                 }
10839                 if (operr) {
10840                         sctp_m_freem(operr);
10841                 }
10842                 return;
10843         }
10844         /* link in any error */
10845         SCTP_BUF_NEXT(m_abort) = operr;
10846         cause_len = 0;
10847         m_last = NULL;
10848         for (m = operr; m; m = SCTP_BUF_NEXT(m)) {
10849                 cause_len += (uint16_t)SCTP_BUF_LEN(m);
10850                 if (SCTP_BUF_NEXT(m) == NULL) {
10851                         m_last = m;
10852                 }
10853         }
10854         SCTP_BUF_LEN(m_abort) = sizeof(struct sctp_abort_chunk);
10855         chunk_len = (uint16_t)sizeof(struct sctp_abort_chunk) + cause_len;
10856         padding_len = SCTP_SIZE32(chunk_len) - chunk_len;
10857         if (m_out == NULL) {
10858                 /* NO Auth chunk prepended, so reserve space in front */
10859                 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10860                 m_out = m_abort;
10861         } else {
10862                 /* Put AUTH chunk at the front of the chain */
10863                 SCTP_BUF_NEXT(m_end) = m_abort;
10864         }
10865         if (stcb->asoc.alternate) {
10866                 net = stcb->asoc.alternate;
10867         } else {
10868                 net = stcb->asoc.primary_destination;
10869         }
10870         /* Fill in the ABORT chunk header. */
10871         abort = mtod(m_abort, struct sctp_abort_chunk *);
10872         abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10873         if (stcb->asoc.peer_vtag == 0) {
10874                 /* This happens iff the assoc is in COOKIE-WAIT state. */
10875                 vtag = stcb->asoc.my_vtag;
10876                 abort->ch.chunk_flags = SCTP_HAD_NO_TCB;
10877         } else {
10878                 vtag = stcb->asoc.peer_vtag;
10879                 abort->ch.chunk_flags = 0;
10880         }
10881         abort->ch.chunk_length = htons(chunk_len);
10882         /* Add padding, if necessary. */
10883         if (padding_len > 0) {
10884                 if ((m_last == NULL) ||
10885                     (sctp_add_pad_tombuf(m_last, padding_len) == NULL)) {
10886                         sctp_m_freem(m_out);
10887                         return;
10888                 }
10889         }
10890         if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10891             (struct sockaddr *)&net->ro._l_addr,
10892             m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
10893             stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag),
10894             stcb->asoc.primary_destination->port, NULL,
10895             0, 0,
10896             so_locked))) {
10897                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10898                 if (error == ENOBUFS) {
10899                         stcb->asoc.ifp_had_enobuf = 1;
10900                         SCTP_STAT_INCR(sctps_lowlevelerr);
10901                 }
10902         } else {
10903                 stcb->asoc.ifp_had_enobuf = 0;
10904         }
10905         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10906 }
10907
10908 void
10909 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10910     struct sctp_nets *net,
10911     int reflect_vtag)
10912 {
10913         /* formulate and SEND a SHUTDOWN-COMPLETE */
10914         struct mbuf *m_shutdown_comp;
10915         struct sctp_shutdown_complete_chunk *shutdown_complete;
10916         uint32_t vtag;
10917         int error;
10918         uint8_t flags;
10919
10920         m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
10921         if (m_shutdown_comp == NULL) {
10922                 /* no mbuf's */
10923                 return;
10924         }
10925         if (reflect_vtag) {
10926                 flags = SCTP_HAD_NO_TCB;
10927                 vtag = stcb->asoc.my_vtag;
10928         } else {
10929                 flags = 0;
10930                 vtag = stcb->asoc.peer_vtag;
10931         }
10932         shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10933         shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10934         shutdown_complete->ch.chunk_flags = flags;
10935         shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10936         SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10937         if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10938             (struct sockaddr *)&net->ro._l_addr,
10939             m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
10940             stcb->sctp_ep->sctp_lport, stcb->rport,
10941             htonl(vtag),
10942             net->port, NULL,
10943             0, 0,
10944             SCTP_SO_NOT_LOCKED))) {
10945                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10946                 if (error == ENOBUFS) {
10947                         stcb->asoc.ifp_had_enobuf = 1;
10948                         SCTP_STAT_INCR(sctps_lowlevelerr);
10949                 }
10950         } else {
10951                 stcb->asoc.ifp_had_enobuf = 0;
10952         }
10953         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10954         return;
10955 }
10956
10957 static void
10958 sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst,
10959     struct sctphdr *sh, uint32_t vtag,
10960     uint8_t type, struct mbuf *cause,
10961     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
10962     uint32_t vrf_id, uint16_t port)
10963 {
10964         struct mbuf *o_pak;
10965         struct mbuf *mout;
10966         struct sctphdr *shout;
10967         struct sctp_chunkhdr *ch;
10968 #if defined(INET) || defined(INET6)
10969         struct udphdr *udp;
10970 #endif
10971         int ret, len, cause_len, padding_len;
10972 #ifdef INET
10973         struct sockaddr_in *src_sin, *dst_sin;
10974         struct ip *ip;
10975 #endif
10976 #ifdef INET6
10977         struct sockaddr_in6 *src_sin6, *dst_sin6;
10978         struct ip6_hdr *ip6;
10979 #endif
10980
10981         /* Compute the length of the cause and add final padding. */
10982         cause_len = 0;
10983         if (cause != NULL) {
10984                 struct mbuf *m_at, *m_last = NULL;
10985
10986                 for (m_at = cause; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
10987                         if (SCTP_BUF_NEXT(m_at) == NULL)
10988                                 m_last = m_at;
10989                         cause_len += SCTP_BUF_LEN(m_at);
10990                 }
10991                 padding_len = cause_len % 4;
10992                 if (padding_len != 0) {
10993                         padding_len = 4 - padding_len;
10994                 }
10995                 if (padding_len != 0) {
10996                         if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
10997                                 sctp_m_freem(cause);
10998                                 return;
10999                         }
11000                 }
11001         } else {
11002                 padding_len = 0;
11003         }
11004         /* Get an mbuf for the header. */
11005         len = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
11006         switch (dst->sa_family) {
11007 #ifdef INET
11008         case AF_INET:
11009                 len += sizeof(struct ip);
11010                 break;
11011 #endif
11012 #ifdef INET6
11013         case AF_INET6:
11014                 len += sizeof(struct ip6_hdr);
11015                 break;
11016 #endif
11017         default:
11018                 break;
11019         }
11020 #if defined(INET) || defined(INET6)
11021         if (port) {
11022                 len += sizeof(struct udphdr);
11023         }
11024 #endif
11025         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA);
11026         if (mout == NULL) {
11027                 if (cause) {
11028                         sctp_m_freem(cause);
11029                 }
11030                 return;
11031         }
11032         SCTP_BUF_RESV_UF(mout, max_linkhdr);
11033         SCTP_BUF_LEN(mout) = len;
11034         SCTP_BUF_NEXT(mout) = cause;
11035         M_SETFIB(mout, fibnum);
11036         mout->m_pkthdr.flowid = mflowid;
11037         M_HASHTYPE_SET(mout, mflowtype);
11038 #ifdef INET
11039         ip = NULL;
11040 #endif
11041 #ifdef INET6
11042         ip6 = NULL;
11043 #endif
11044         switch (dst->sa_family) {
11045 #ifdef INET
11046         case AF_INET:
11047                 src_sin = (struct sockaddr_in *)src;
11048                 dst_sin = (struct sockaddr_in *)dst;
11049                 ip = mtod(mout, struct ip *);
11050                 ip->ip_v = IPVERSION;
11051                 ip->ip_hl = (sizeof(struct ip) >> 2);
11052                 ip->ip_tos = 0;
11053                 ip->ip_off = htons(IP_DF);
11054                 ip_fillid(ip);
11055                 ip->ip_ttl = MODULE_GLOBAL(ip_defttl);
11056                 if (port) {
11057                         ip->ip_p = IPPROTO_UDP;
11058                 } else {
11059                         ip->ip_p = IPPROTO_SCTP;
11060                 }
11061                 ip->ip_src.s_addr = dst_sin->sin_addr.s_addr;
11062                 ip->ip_dst.s_addr = src_sin->sin_addr.s_addr;
11063                 ip->ip_sum = 0;
11064                 len = sizeof(struct ip);
11065                 shout = (struct sctphdr *)((caddr_t)ip + len);
11066                 break;
11067 #endif
11068 #ifdef INET6
11069         case AF_INET6:
11070                 src_sin6 = (struct sockaddr_in6 *)src;
11071                 dst_sin6 = (struct sockaddr_in6 *)dst;
11072                 ip6 = mtod(mout, struct ip6_hdr *);
11073                 ip6->ip6_flow = htonl(0x60000000);
11074                 if (V_ip6_auto_flowlabel) {
11075                         ip6->ip6_flow |= (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
11076                 }
11077                 ip6->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11078                 if (port) {
11079                         ip6->ip6_nxt = IPPROTO_UDP;
11080                 } else {
11081                         ip6->ip6_nxt = IPPROTO_SCTP;
11082                 }
11083                 ip6->ip6_src = dst_sin6->sin6_addr;
11084                 ip6->ip6_dst = src_sin6->sin6_addr;
11085                 len = sizeof(struct ip6_hdr);
11086                 shout = (struct sctphdr *)((caddr_t)ip6 + len);
11087                 break;
11088 #endif
11089         default:
11090                 len = 0;
11091                 shout = mtod(mout, struct sctphdr *);
11092                 break;
11093         }
11094 #if defined(INET) || defined(INET6)
11095         if (port) {
11096                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
11097                         sctp_m_freem(mout);
11098                         return;
11099                 }
11100                 udp = (struct udphdr *)shout;
11101                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11102                 udp->uh_dport = port;
11103                 udp->uh_sum = 0;
11104                 udp->uh_ulen = htons((uint16_t)(sizeof(struct udphdr) +
11105                     sizeof(struct sctphdr) +
11106                     sizeof(struct sctp_chunkhdr) +
11107                     cause_len + padding_len));
11108                 len += sizeof(struct udphdr);
11109                 shout = (struct sctphdr *)((caddr_t)shout + sizeof(struct udphdr));
11110         } else {
11111                 udp = NULL;
11112         }
11113 #endif
11114         shout->src_port = sh->dest_port;
11115         shout->dest_port = sh->src_port;
11116         shout->checksum = 0;
11117         if (vtag) {
11118                 shout->v_tag = htonl(vtag);
11119         } else {
11120                 shout->v_tag = sh->v_tag;
11121         }
11122         len += sizeof(struct sctphdr);
11123         ch = (struct sctp_chunkhdr *)((caddr_t)shout + sizeof(struct sctphdr));
11124         ch->chunk_type = type;
11125         if (vtag) {
11126                 ch->chunk_flags = 0;
11127         } else {
11128                 ch->chunk_flags = SCTP_HAD_NO_TCB;
11129         }
11130         ch->chunk_length = htons((uint16_t)(sizeof(struct sctp_chunkhdr) + cause_len));
11131         len += sizeof(struct sctp_chunkhdr);
11132         len += cause_len + padding_len;
11133
11134         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11135                 sctp_m_freem(mout);
11136                 return;
11137         }
11138         SCTP_ATTACH_CHAIN(o_pak, mout, len);
11139         switch (dst->sa_family) {
11140 #ifdef INET
11141         case AF_INET:
11142                 if (port) {
11143                         if (V_udp_cksum) {
11144                                 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11145                         } else {
11146                                 udp->uh_sum = 0;
11147                         }
11148                 }
11149                 ip->ip_len = htons(len);
11150                 if (port) {
11151                         shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip) + sizeof(struct udphdr));
11152                         SCTP_STAT_INCR(sctps_sendswcrc);
11153                         if (V_udp_cksum) {
11154                                 SCTP_ENABLE_UDP_CSUM(o_pak);
11155                         }
11156                 } else {
11157                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
11158                         mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
11159                         SCTP_STAT_INCR(sctps_sendhwcrc);
11160                 }
11161 #ifdef SCTP_PACKET_LOGGING
11162                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
11163                         sctp_packet_log(o_pak);
11164                 }
11165 #endif
11166                 SCTP_PROBE5(send, NULL, NULL, ip, NULL, shout);
11167                 SCTP_IP_OUTPUT(ret, o_pak, NULL, NULL, vrf_id);
11168                 break;
11169 #endif
11170 #ifdef INET6
11171         case AF_INET6:
11172                 ip6->ip6_plen = htons((uint16_t)(len - sizeof(struct ip6_hdr)));
11173                 if (port) {
11174                         shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11175                         SCTP_STAT_INCR(sctps_sendswcrc);
11176                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11177                                 udp->uh_sum = 0xffff;
11178                         }
11179                 } else {
11180                         mout->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
11181                         mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
11182                         SCTP_STAT_INCR(sctps_sendhwcrc);
11183                 }
11184 #ifdef SCTP_PACKET_LOGGING
11185                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
11186                         sctp_packet_log(o_pak);
11187                 }
11188 #endif
11189                 SCTP_PROBE5(send, NULL, NULL, ip6, NULL, shout);
11190                 SCTP_IP6_OUTPUT(ret, o_pak, NULL, NULL, NULL, vrf_id);
11191                 break;
11192 #endif
11193         default:
11194                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
11195                     dst->sa_family);
11196                 sctp_m_freem(mout);
11197                 SCTP_LTRACE_ERR_RET_PKT(mout, NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11198                 return;
11199         }
11200         SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
11201         if (port) {
11202                 UDPSTAT_INC(udps_opackets);
11203         }
11204         SCTP_STAT_INCR(sctps_sendpackets);
11205         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11206         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11207         if (ret) {
11208                 SCTP_STAT_INCR(sctps_senderrors);
11209         }
11210         return;
11211 }
11212
11213 void
11214 sctp_send_shutdown_complete2(struct sockaddr *src, struct sockaddr *dst,
11215     struct sctphdr *sh,
11216     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
11217     uint32_t vrf_id, uint16_t port)
11218 {
11219         sctp_send_resp_msg(src, dst, sh, 0, SCTP_SHUTDOWN_COMPLETE, NULL,
11220             mflowtype, mflowid, fibnum,
11221             vrf_id, port);
11222 }
11223
11224 void
11225 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked)
11226 {
11227         struct sctp_tmit_chunk *chk;
11228         struct sctp_heartbeat_chunk *hb;
11229         struct timeval now;
11230
11231         SCTP_TCB_LOCK_ASSERT(stcb);
11232         if (net == NULL) {
11233                 return;
11234         }
11235         (void)SCTP_GETTIME_TIMEVAL(&now);
11236         switch (net->ro._l_addr.sa.sa_family) {
11237 #ifdef INET
11238         case AF_INET:
11239                 break;
11240 #endif
11241 #ifdef INET6
11242         case AF_INET6:
11243                 break;
11244 #endif
11245         default:
11246                 return;
11247         }
11248         sctp_alloc_a_chunk(stcb, chk);
11249         if (chk == NULL) {
11250                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11251                 return;
11252         }
11253
11254         chk->copy_by_ref = 0;
11255         chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11256         chk->rec.chunk_id.can_take_data = 1;
11257         chk->flags = 0;
11258         chk->asoc = &stcb->asoc;
11259         chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11260
11261         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11262         if (chk->data == NULL) {
11263                 sctp_free_a_chunk(stcb, chk, so_locked);
11264                 return;
11265         }
11266         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11267         SCTP_BUF_LEN(chk->data) = chk->send_size;
11268         chk->sent = SCTP_DATAGRAM_UNSENT;
11269         chk->snd_count = 0;
11270         chk->whoTo = net;
11271         atomic_add_int(&chk->whoTo->ref_count, 1);
11272         /* Now we have a mbuf that we can fill in with the details */
11273         hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11274         memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11275         /* fill out chunk header */
11276         hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11277         hb->ch.chunk_flags = 0;
11278         hb->ch.chunk_length = htons(chk->send_size);
11279         /* Fill out hb parameter */
11280         hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11281         hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11282         hb->heartbeat.hb_info.time_value_1 = (uint32_t)now.tv_sec;
11283         hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11284         /* Did our user request this one, put it in */
11285         hb->heartbeat.hb_info.addr_family = (uint8_t)net->ro._l_addr.sa.sa_family;
11286         hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
11287         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11288                 /*
11289                  * we only take from the entropy pool if the address is not
11290                  * confirmed.
11291                  */
11292                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11293                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11294         } else {
11295                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11296                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11297         }
11298         switch (net->ro._l_addr.sa.sa_family) {
11299 #ifdef INET
11300         case AF_INET:
11301                 memcpy(hb->heartbeat.hb_info.address,
11302                     &net->ro._l_addr.sin.sin_addr,
11303                     sizeof(net->ro._l_addr.sin.sin_addr));
11304                 break;
11305 #endif
11306 #ifdef INET6
11307         case AF_INET6:
11308                 memcpy(hb->heartbeat.hb_info.address,
11309                     &net->ro._l_addr.sin6.sin6_addr,
11310                     sizeof(net->ro._l_addr.sin6.sin6_addr));
11311                 break;
11312 #endif
11313         default:
11314                 if (chk->data) {
11315                         sctp_m_freem(chk->data);
11316                         chk->data = NULL;
11317                 }
11318                 sctp_free_a_chunk(stcb, chk, so_locked);
11319                 return;
11320                 break;
11321         }
11322         net->hb_responded = 0;
11323         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11324         stcb->asoc.ctrl_queue_cnt++;
11325         SCTP_STAT_INCR(sctps_sendheartbeat);
11326         return;
11327 }
11328
11329 void
11330 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11331     uint32_t high_tsn)
11332 {
11333         struct sctp_association *asoc;
11334         struct sctp_ecne_chunk *ecne;
11335         struct sctp_tmit_chunk *chk;
11336
11337         if (net == NULL) {
11338                 return;
11339         }
11340         asoc = &stcb->asoc;
11341         SCTP_TCB_LOCK_ASSERT(stcb);
11342         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11343                 if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
11344                         /* found a previous ECN_ECHO update it if needed */
11345                         uint32_t cnt, ctsn;
11346
11347                         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11348                         ctsn = ntohl(ecne->tsn);
11349                         if (SCTP_TSN_GT(high_tsn, ctsn)) {
11350                                 ecne->tsn = htonl(high_tsn);
11351                                 SCTP_STAT_INCR(sctps_queue_upd_ecne);
11352                         }
11353                         cnt = ntohl(ecne->num_pkts_since_cwr);
11354                         cnt++;
11355                         ecne->num_pkts_since_cwr = htonl(cnt);
11356                         return;
11357                 }
11358         }
11359         /* nope could not find one to update so we must build one */
11360         sctp_alloc_a_chunk(stcb, chk);
11361         if (chk == NULL) {
11362                 return;
11363         }
11364         SCTP_STAT_INCR(sctps_queue_upd_ecne);
11365         chk->copy_by_ref = 0;
11366         chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11367         chk->rec.chunk_id.can_take_data = 0;
11368         chk->flags = 0;
11369         chk->asoc = &stcb->asoc;
11370         chk->send_size = sizeof(struct sctp_ecne_chunk);
11371         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11372         if (chk->data == NULL) {
11373                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11374                 return;
11375         }
11376         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11377         SCTP_BUF_LEN(chk->data) = chk->send_size;
11378         chk->sent = SCTP_DATAGRAM_UNSENT;
11379         chk->snd_count = 0;
11380         chk->whoTo = net;
11381         atomic_add_int(&chk->whoTo->ref_count, 1);
11382
11383         stcb->asoc.ecn_echo_cnt_onq++;
11384         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11385         ecne->ch.chunk_type = SCTP_ECN_ECHO;
11386         ecne->ch.chunk_flags = 0;
11387         ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11388         ecne->tsn = htonl(high_tsn);
11389         ecne->num_pkts_since_cwr = htonl(1);
11390         TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11391         asoc->ctrl_queue_cnt++;
11392 }
11393
11394 void
11395 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11396     struct mbuf *m, int len, int iphlen, int bad_crc)
11397 {
11398         struct sctp_association *asoc;
11399         struct sctp_pktdrop_chunk *drp;
11400         struct sctp_tmit_chunk *chk;
11401         uint8_t *datap;
11402         int was_trunc = 0;
11403         int fullsz = 0;
11404         long spc;
11405         int offset;
11406         struct sctp_chunkhdr *ch, chunk_buf;
11407         unsigned int chk_length;
11408
11409         if (!stcb) {
11410                 return;
11411         }
11412         asoc = &stcb->asoc;
11413         SCTP_TCB_LOCK_ASSERT(stcb);
11414         if (asoc->pktdrop_supported == 0) {
11415                 /*-
11416                  * peer must declare support before I send one.
11417                  */
11418                 return;
11419         }
11420         if (stcb->sctp_socket == NULL) {
11421                 return;
11422         }
11423         sctp_alloc_a_chunk(stcb, chk);
11424         if (chk == NULL) {
11425                 return;
11426         }
11427         chk->copy_by_ref = 0;
11428         chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11429         chk->rec.chunk_id.can_take_data = 1;
11430         chk->flags = 0;
11431         len -= iphlen;
11432         chk->send_size = len;
11433         /* Validate that we do not have an ABORT in here. */
11434         offset = iphlen + sizeof(struct sctphdr);
11435         ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11436             sizeof(*ch), (uint8_t *)&chunk_buf);
11437         while (ch != NULL) {
11438                 chk_length = ntohs(ch->chunk_length);
11439                 if (chk_length < sizeof(*ch)) {
11440                         /* break to abort land */
11441                         break;
11442                 }
11443                 switch (ch->chunk_type) {
11444                 case SCTP_PACKET_DROPPED:
11445                 case SCTP_ABORT_ASSOCIATION:
11446                 case SCTP_INITIATION_ACK:
11447                         /**
11448                          * We don't respond with an PKT-DROP to an ABORT
11449                          * or PKT-DROP. We also do not respond to an
11450                          * INIT-ACK, because we can't know if the initiation
11451                          * tag is correct or not.
11452                          */
11453                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11454                         return;
11455                 default:
11456                         break;
11457                 }
11458                 offset += SCTP_SIZE32(chk_length);
11459                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11460                     sizeof(*ch), (uint8_t *)&chunk_buf);
11461         }
11462
11463         if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11464             min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11465                 /*
11466                  * only send 1 mtu worth, trim off the excess on the end.
11467                  */
11468                 fullsz = len;
11469                 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11470                 was_trunc = 1;
11471         }
11472         chk->asoc = &stcb->asoc;
11473         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11474         if (chk->data == NULL) {
11475 jump_out:
11476                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11477                 return;
11478         }
11479         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11480         drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11481         if (drp == NULL) {
11482                 sctp_m_freem(chk->data);
11483                 chk->data = NULL;
11484                 goto jump_out;
11485         }
11486         chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11487             sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11488         chk->book_size_scale = 0;
11489         if (was_trunc) {
11490                 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11491                 drp->trunc_len = htons(fullsz);
11492                 /*
11493                  * Len is already adjusted to size minus overhead above take
11494                  * out the pkt_drop chunk itself from it.
11495                  */
11496                 chk->send_size = (uint16_t)(len - sizeof(struct sctp_pktdrop_chunk));
11497                 len = chk->send_size;
11498         } else {
11499                 /* no truncation needed */
11500                 drp->ch.chunk_flags = 0;
11501                 drp->trunc_len = htons(0);
11502         }
11503         if (bad_crc) {
11504                 drp->ch.chunk_flags |= SCTP_BADCRC;
11505         }
11506         chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11507         SCTP_BUF_LEN(chk->data) = chk->send_size;
11508         chk->sent = SCTP_DATAGRAM_UNSENT;
11509         chk->snd_count = 0;
11510         if (net) {
11511                 /* we should hit here */
11512                 chk->whoTo = net;
11513                 atomic_add_int(&chk->whoTo->ref_count, 1);
11514         } else {
11515                 chk->whoTo = NULL;
11516         }
11517         drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11518         drp->ch.chunk_length = htons(chk->send_size);
11519         spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11520         if (spc < 0) {
11521                 spc = 0;
11522         }
11523         drp->bottle_bw = htonl(spc);
11524         if (asoc->my_rwnd) {
11525                 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11526                     asoc->size_on_all_streams +
11527                     asoc->my_rwnd_control_len +
11528                     SCTP_SBAVAIL(&stcb->sctp_socket->so_rcv));
11529         } else {
11530                 /*-
11531                  * If my rwnd is 0, possibly from mbuf depletion as well as
11532                  * space used, tell the peer there is NO space aka onq == bw
11533                  */
11534                 drp->current_onq = htonl(spc);
11535         }
11536         drp->reserved = 0;
11537         datap = drp->data;
11538         m_copydata(m, iphlen, len, (caddr_t)datap);
11539         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11540         asoc->ctrl_queue_cnt++;
11541 }
11542
11543 void
11544 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11545 {
11546         struct sctp_association *asoc;
11547         struct sctp_cwr_chunk *cwr;
11548         struct sctp_tmit_chunk *chk;
11549
11550         SCTP_TCB_LOCK_ASSERT(stcb);
11551         if (net == NULL) {
11552                 return;
11553         }
11554         asoc = &stcb->asoc;
11555         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11556                 if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11557                         /*
11558                          * found a previous CWR queued to same destination
11559                          * update it if needed
11560                          */
11561                         uint32_t ctsn;
11562
11563                         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11564                         ctsn = ntohl(cwr->tsn);
11565                         if (SCTP_TSN_GT(high_tsn, ctsn)) {
11566                                 cwr->tsn = htonl(high_tsn);
11567                         }
11568                         if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11569                                 /* Make sure override is carried */
11570                                 cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11571                         }
11572                         return;
11573                 }
11574         }
11575         sctp_alloc_a_chunk(stcb, chk);
11576         if (chk == NULL) {
11577                 return;
11578         }
11579         chk->copy_by_ref = 0;
11580         chk->rec.chunk_id.id = SCTP_ECN_CWR;
11581         chk->rec.chunk_id.can_take_data = 1;
11582         chk->flags = 0;
11583         chk->asoc = asoc;
11584         chk->send_size = sizeof(struct sctp_cwr_chunk);
11585         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11586         if (chk->data == NULL) {
11587                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11588                 return;
11589         }
11590         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11591         SCTP_BUF_LEN(chk->data) = chk->send_size;
11592         chk->sent = SCTP_DATAGRAM_UNSENT;
11593         chk->snd_count = 0;
11594         chk->whoTo = net;
11595         atomic_add_int(&chk->whoTo->ref_count, 1);
11596         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11597         cwr->ch.chunk_type = SCTP_ECN_CWR;
11598         cwr->ch.chunk_flags = override;
11599         cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11600         cwr->tsn = htonl(high_tsn);
11601         TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
11602         asoc->ctrl_queue_cnt++;
11603 }
11604
11605 static int
11606 sctp_add_stream_reset_out(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
11607     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11608 {
11609         uint16_t len, old_len, i;
11610         struct sctp_stream_reset_out_request *req_out;
11611         struct sctp_chunkhdr *ch;
11612         int at;
11613         int number_entries = 0;
11614
11615         ch = mtod(chk->data, struct sctp_chunkhdr *);
11616         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11617         /* get to new offset for the param. */
11618         req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11619         /* now how long will this param be? */
11620         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11621                 if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
11622                     (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
11623                     TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
11624                         number_entries++;
11625                 }
11626         }
11627         if (number_entries == 0) {
11628                 return (0);
11629         }
11630         if (number_entries == stcb->asoc.streamoutcnt) {
11631                 number_entries = 0;
11632         }
11633         if (number_entries > SCTP_MAX_STREAMS_AT_ONCE_RESET) {
11634                 number_entries = SCTP_MAX_STREAMS_AT_ONCE_RESET;
11635         }
11636         len = (uint16_t)(sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11637         req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11638         req_out->ph.param_length = htons(len);
11639         req_out->request_seq = htonl(seq);
11640         req_out->response_seq = htonl(resp_seq);
11641         req_out->send_reset_at_tsn = htonl(last_sent);
11642         at = 0;
11643         if (number_entries) {
11644                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11645                         if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
11646                             (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
11647                             TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
11648                                 req_out->list_of_streams[at] = htons(i);
11649                                 at++;
11650                                 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
11651                                 if (at >= number_entries) {
11652                                         break;
11653                                 }
11654                         }
11655                 }
11656         } else {
11657                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11658                         stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
11659                 }
11660         }
11661         if (SCTP_SIZE32(len) > len) {
11662                 /*-
11663                  * Need to worry about the pad we may end up adding to the
11664                  * end. This is easy since the struct is either aligned to 4
11665                  * bytes or 2 bytes off.
11666                  */
11667                 req_out->list_of_streams[number_entries] = 0;
11668         }
11669         /* now fix the chunk length */
11670         ch->chunk_length = htons(len + old_len);
11671         chk->book_size = len + old_len;
11672         chk->book_size_scale = 0;
11673         chk->send_size = SCTP_SIZE32(chk->book_size);
11674         SCTP_BUF_LEN(chk->data) = chk->send_size;
11675         return (1);
11676 }
11677
11678 static void
11679 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11680     int number_entries, uint16_t *list,
11681     uint32_t seq)
11682 {
11683         uint16_t len, old_len, i;
11684         struct sctp_stream_reset_in_request *req_in;
11685         struct sctp_chunkhdr *ch;
11686
11687         ch = mtod(chk->data, struct sctp_chunkhdr *);
11688         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11689
11690         /* get to new offset for the param. */
11691         req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11692         /* now how long will this param be? */
11693         len = (uint16_t)(sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11694         req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11695         req_in->ph.param_length = htons(len);
11696         req_in->request_seq = htonl(seq);
11697         if (number_entries) {
11698                 for (i = 0; i < number_entries; i++) {
11699                         req_in->list_of_streams[i] = htons(list[i]);
11700                 }
11701         }
11702         if (SCTP_SIZE32(len) > len) {
11703                 /*-
11704                  * Need to worry about the pad we may end up adding to the
11705                  * end. This is easy since the struct is either aligned to 4
11706                  * bytes or 2 bytes off.
11707                  */
11708                 req_in->list_of_streams[number_entries] = 0;
11709         }
11710         /* now fix the chunk length */
11711         ch->chunk_length = htons(len + old_len);
11712         chk->book_size = len + old_len;
11713         chk->book_size_scale = 0;
11714         chk->send_size = SCTP_SIZE32(chk->book_size);
11715         SCTP_BUF_LEN(chk->data) = chk->send_size;
11716         return;
11717 }
11718
11719 static void
11720 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11721     uint32_t seq)
11722 {
11723         uint16_t len, old_len;
11724         struct sctp_stream_reset_tsn_request *req_tsn;
11725         struct sctp_chunkhdr *ch;
11726
11727         ch = mtod(chk->data, struct sctp_chunkhdr *);
11728         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11729
11730         /* get to new offset for the param. */
11731         req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11732         /* now how long will this param be? */
11733         len = sizeof(struct sctp_stream_reset_tsn_request);
11734         req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11735         req_tsn->ph.param_length = htons(len);
11736         req_tsn->request_seq = htonl(seq);
11737
11738         /* now fix the chunk length */
11739         ch->chunk_length = htons(len + old_len);
11740         chk->send_size = len + old_len;
11741         chk->book_size = SCTP_SIZE32(chk->send_size);
11742         chk->book_size_scale = 0;
11743         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11744         return;
11745 }
11746
11747 void
11748 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11749     uint32_t resp_seq, uint32_t result)
11750 {
11751         uint16_t len, old_len;
11752         struct sctp_stream_reset_response *resp;
11753         struct sctp_chunkhdr *ch;
11754
11755         ch = mtod(chk->data, struct sctp_chunkhdr *);
11756         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11757
11758         /* get to new offset for the param. */
11759         resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11760         /* now how long will this param be? */
11761         len = sizeof(struct sctp_stream_reset_response);
11762         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11763         resp->ph.param_length = htons(len);
11764         resp->response_seq = htonl(resp_seq);
11765         resp->result = ntohl(result);
11766
11767         /* now fix the chunk length */
11768         ch->chunk_length = htons(len + old_len);
11769         chk->book_size = len + old_len;
11770         chk->book_size_scale = 0;
11771         chk->send_size = SCTP_SIZE32(chk->book_size);
11772         SCTP_BUF_LEN(chk->data) = chk->send_size;
11773         return;
11774 }
11775
11776 void
11777 sctp_send_deferred_reset_response(struct sctp_tcb *stcb,
11778     struct sctp_stream_reset_list *ent,
11779     int response)
11780 {
11781         struct sctp_association *asoc;
11782         struct sctp_tmit_chunk *chk;
11783         struct sctp_chunkhdr *ch;
11784
11785         asoc = &stcb->asoc;
11786
11787         /*
11788          * Reset our last reset action to the new one IP -> response
11789          * (PERFORMED probably). This assures that if we fail to send, a
11790          * retran from the peer will get the new response.
11791          */
11792         asoc->last_reset_action[0] = response;
11793         if (asoc->stream_reset_outstanding) {
11794                 return;
11795         }
11796         sctp_alloc_a_chunk(stcb, chk);
11797         if (chk == NULL) {
11798                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11799                 return;
11800         }
11801         chk->copy_by_ref = 0;
11802         chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11803         chk->rec.chunk_id.can_take_data = 0;
11804         chk->flags = 0;
11805         chk->asoc = &stcb->asoc;
11806         chk->book_size = sizeof(struct sctp_chunkhdr);
11807         chk->send_size = SCTP_SIZE32(chk->book_size);
11808         chk->book_size_scale = 0;
11809         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11810         if (chk->data == NULL) {
11811                 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11812                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11813                 return;
11814         }
11815         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11816         /* setup chunk parameters */
11817         chk->sent = SCTP_DATAGRAM_UNSENT;
11818         chk->snd_count = 0;
11819         if (stcb->asoc.alternate) {
11820                 chk->whoTo = stcb->asoc.alternate;
11821         } else {
11822                 chk->whoTo = stcb->asoc.primary_destination;
11823         }
11824         ch = mtod(chk->data, struct sctp_chunkhdr *);
11825         ch->chunk_type = SCTP_STREAM_RESET;
11826         ch->chunk_flags = 0;
11827         ch->chunk_length = htons(chk->book_size);
11828         atomic_add_int(&chk->whoTo->ref_count, 1);
11829         SCTP_BUF_LEN(chk->data) = chk->send_size;
11830         sctp_add_stream_reset_result(chk, ent->seq, response);
11831         /* insert the chunk for sending */
11832         TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11833             chk,
11834             sctp_next);
11835         asoc->ctrl_queue_cnt++;
11836 }
11837
11838 void
11839 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11840     uint32_t resp_seq, uint32_t result,
11841     uint32_t send_una, uint32_t recv_next)
11842 {
11843         uint16_t len, old_len;
11844         struct sctp_stream_reset_response_tsn *resp;
11845         struct sctp_chunkhdr *ch;
11846
11847         ch = mtod(chk->data, struct sctp_chunkhdr *);
11848         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11849
11850         /* get to new offset for the param. */
11851         resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11852         /* now how long will this param be? */
11853         len = sizeof(struct sctp_stream_reset_response_tsn);
11854         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11855         resp->ph.param_length = htons(len);
11856         resp->response_seq = htonl(resp_seq);
11857         resp->result = htonl(result);
11858         resp->senders_next_tsn = htonl(send_una);
11859         resp->receivers_next_tsn = htonl(recv_next);
11860
11861         /* now fix the chunk length */
11862         ch->chunk_length = htons(len + old_len);
11863         chk->book_size = len + old_len;
11864         chk->send_size = SCTP_SIZE32(chk->book_size);
11865         chk->book_size_scale = 0;
11866         SCTP_BUF_LEN(chk->data) = chk->send_size;
11867         return;
11868 }
11869
11870 static void
11871 sctp_add_an_out_stream(struct sctp_tmit_chunk *chk,
11872     uint32_t seq,
11873     uint16_t adding)
11874 {
11875         uint16_t len, old_len;
11876         struct sctp_chunkhdr *ch;
11877         struct sctp_stream_reset_add_strm *addstr;
11878
11879         ch = mtod(chk->data, struct sctp_chunkhdr *);
11880         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11881
11882         /* get to new offset for the param. */
11883         addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11884         /* now how long will this param be? */
11885         len = sizeof(struct sctp_stream_reset_add_strm);
11886
11887         /* Fill it out. */
11888         addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS);
11889         addstr->ph.param_length = htons(len);
11890         addstr->request_seq = htonl(seq);
11891         addstr->number_of_streams = htons(adding);
11892         addstr->reserved = 0;
11893
11894         /* now fix the chunk length */
11895         ch->chunk_length = htons(len + old_len);
11896         chk->send_size = len + old_len;
11897         chk->book_size = SCTP_SIZE32(chk->send_size);
11898         chk->book_size_scale = 0;
11899         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11900         return;
11901 }
11902
11903 static void
11904 sctp_add_an_in_stream(struct sctp_tmit_chunk *chk,
11905     uint32_t seq,
11906     uint16_t adding)
11907 {
11908         uint16_t len, old_len;
11909         struct sctp_chunkhdr *ch;
11910         struct sctp_stream_reset_add_strm *addstr;
11911
11912         ch = mtod(chk->data, struct sctp_chunkhdr *);
11913         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11914
11915         /* get to new offset for the param. */
11916         addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11917         /* now how long will this param be? */
11918         len = sizeof(struct sctp_stream_reset_add_strm);
11919         /* Fill it out. */
11920         addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS);
11921         addstr->ph.param_length = htons(len);
11922         addstr->request_seq = htonl(seq);
11923         addstr->number_of_streams = htons(adding);
11924         addstr->reserved = 0;
11925
11926         /* now fix the chunk length */
11927         ch->chunk_length = htons(len + old_len);
11928         chk->send_size = len + old_len;
11929         chk->book_size = SCTP_SIZE32(chk->send_size);
11930         chk->book_size_scale = 0;
11931         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11932         return;
11933 }
11934
11935 int
11936 sctp_send_stream_reset_out_if_possible(struct sctp_tcb *stcb, int so_locked)
11937 {
11938         struct sctp_association *asoc;
11939         struct sctp_tmit_chunk *chk;
11940         struct sctp_chunkhdr *ch;
11941         uint32_t seq;
11942
11943         asoc = &stcb->asoc;
11944         asoc->trigger_reset = 0;
11945         if (asoc->stream_reset_outstanding) {
11946                 return (EALREADY);
11947         }
11948         sctp_alloc_a_chunk(stcb, chk);
11949         if (chk == NULL) {
11950                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11951                 return (ENOMEM);
11952         }
11953         chk->copy_by_ref = 0;
11954         chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11955         chk->rec.chunk_id.can_take_data = 0;
11956         chk->flags = 0;
11957         chk->asoc = &stcb->asoc;
11958         chk->book_size = sizeof(struct sctp_chunkhdr);
11959         chk->send_size = SCTP_SIZE32(chk->book_size);
11960         chk->book_size_scale = 0;
11961         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11962         if (chk->data == NULL) {
11963                 sctp_free_a_chunk(stcb, chk, so_locked);
11964                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11965                 return (ENOMEM);
11966         }
11967         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11968
11969         /* setup chunk parameters */
11970         chk->sent = SCTP_DATAGRAM_UNSENT;
11971         chk->snd_count = 0;
11972         if (stcb->asoc.alternate) {
11973                 chk->whoTo = stcb->asoc.alternate;
11974         } else {
11975                 chk->whoTo = stcb->asoc.primary_destination;
11976         }
11977         ch = mtod(chk->data, struct sctp_chunkhdr *);
11978         ch->chunk_type = SCTP_STREAM_RESET;
11979         ch->chunk_flags = 0;
11980         ch->chunk_length = htons(chk->book_size);
11981         atomic_add_int(&chk->whoTo->ref_count, 1);
11982         SCTP_BUF_LEN(chk->data) = chk->send_size;
11983         seq = stcb->asoc.str_reset_seq_out;
11984         if (sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1))) {
11985                 seq++;
11986                 asoc->stream_reset_outstanding++;
11987         } else {
11988                 m_freem(chk->data);
11989                 chk->data = NULL;
11990                 sctp_free_a_chunk(stcb, chk, so_locked);
11991                 return (ENOENT);
11992         }
11993         asoc->str_reset = chk;
11994         /* insert the chunk for sending */
11995         TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11996             chk,
11997             sctp_next);
11998         asoc->ctrl_queue_cnt++;
11999
12000         if (stcb->asoc.send_sack) {
12001                 sctp_send_sack(stcb, so_locked);
12002         }
12003         sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12004         return (0);
12005 }
12006
12007 int
12008 sctp_send_str_reset_req(struct sctp_tcb *stcb,
12009     uint16_t number_entries, uint16_t *list,
12010     uint8_t send_in_req,
12011     uint8_t send_tsn_req,
12012     uint8_t add_stream,
12013     uint16_t adding_o,
12014     uint16_t adding_i, uint8_t peer_asked)
12015 {
12016         struct sctp_association *asoc;
12017         struct sctp_tmit_chunk *chk;
12018         struct sctp_chunkhdr *ch;
12019         int can_send_out_req = 0;
12020         uint32_t seq;
12021
12022         SCTP_TCB_LOCK_ASSERT(stcb);
12023
12024         asoc = &stcb->asoc;
12025         if (asoc->stream_reset_outstanding) {
12026                 /*-
12027                  * Already one pending, must get ACK back to clear the flag.
12028                  */
12029                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
12030                 return (EBUSY);
12031         }
12032         if ((send_in_req == 0) && (send_tsn_req == 0) &&
12033             (add_stream == 0)) {
12034                 /* nothing to do */
12035                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12036                 return (EINVAL);
12037         }
12038         if (send_tsn_req && send_in_req) {
12039                 /* error, can't do that */
12040                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12041                 return (EINVAL);
12042         } else if (send_in_req) {
12043                 can_send_out_req = 1;
12044         }
12045         if (number_entries > (MCLBYTES -
12046             SCTP_MIN_OVERHEAD -
12047             sizeof(struct sctp_chunkhdr) -
12048             sizeof(struct sctp_stream_reset_out_request)) /
12049             sizeof(uint16_t)) {
12050                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12051                 return (ENOMEM);
12052         }
12053         sctp_alloc_a_chunk(stcb, chk);
12054         if (chk == NULL) {
12055                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12056                 return (ENOMEM);
12057         }
12058         chk->copy_by_ref = 0;
12059         chk->rec.chunk_id.id = SCTP_STREAM_RESET;
12060         chk->rec.chunk_id.can_take_data = 0;
12061         chk->flags = 0;
12062         chk->asoc = &stcb->asoc;
12063         chk->book_size = sizeof(struct sctp_chunkhdr);
12064         chk->send_size = SCTP_SIZE32(chk->book_size);
12065         chk->book_size_scale = 0;
12066         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
12067         if (chk->data == NULL) {
12068                 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
12069                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12070                 return (ENOMEM);
12071         }
12072         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12073
12074         /* setup chunk parameters */
12075         chk->sent = SCTP_DATAGRAM_UNSENT;
12076         chk->snd_count = 0;
12077         if (stcb->asoc.alternate) {
12078                 chk->whoTo = stcb->asoc.alternate;
12079         } else {
12080                 chk->whoTo = stcb->asoc.primary_destination;
12081         }
12082         atomic_add_int(&chk->whoTo->ref_count, 1);
12083         ch = mtod(chk->data, struct sctp_chunkhdr *);
12084         ch->chunk_type = SCTP_STREAM_RESET;
12085         ch->chunk_flags = 0;
12086         ch->chunk_length = htons(chk->book_size);
12087         SCTP_BUF_LEN(chk->data) = chk->send_size;
12088
12089         seq = stcb->asoc.str_reset_seq_out;
12090         if (can_send_out_req) {
12091                 int ret;
12092
12093                 ret = sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1));
12094                 if (ret) {
12095                         seq++;
12096                         asoc->stream_reset_outstanding++;
12097                 }
12098         }
12099         if ((add_stream & 1) &&
12100             ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) {
12101                 /* Need to allocate more */
12102                 struct sctp_stream_out *oldstream;
12103                 struct sctp_stream_queue_pending *sp, *nsp;
12104                 int i;
12105 #if defined(SCTP_DETAILED_STR_STATS)
12106                 int j;
12107 #endif
12108
12109                 oldstream = stcb->asoc.strmout;
12110                 /* get some more */
12111                 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
12112                     (stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out),
12113                     SCTP_M_STRMO);
12114                 if (stcb->asoc.strmout == NULL) {
12115                         uint8_t x;
12116
12117                         stcb->asoc.strmout = oldstream;
12118                         /* Turn off the bit */
12119                         x = add_stream & 0xfe;
12120                         add_stream = x;
12121                         goto skip_stuff;
12122                 }
12123                 /*
12124                  * Ok now we proceed with copying the old out stuff and
12125                  * initializing the new stuff.
12126                  */
12127                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, false);
12128                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
12129                         TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
12130                         /* FIX ME FIX ME */
12131                         /*
12132                          * This should be a SS_COPY operation FIX ME STREAM
12133                          * SCHEDULER EXPERT
12134                          */
12135                         stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], &oldstream[i]);
12136                         stcb->asoc.strmout[i].chunks_on_queues = oldstream[i].chunks_on_queues;
12137 #if defined(SCTP_DETAILED_STR_STATS)
12138                         for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
12139                                 stcb->asoc.strmout[i].abandoned_sent[j] = oldstream[i].abandoned_sent[j];
12140                                 stcb->asoc.strmout[i].abandoned_unsent[j] = oldstream[i].abandoned_unsent[j];
12141                         }
12142 #else
12143                         stcb->asoc.strmout[i].abandoned_sent[0] = oldstream[i].abandoned_sent[0];
12144                         stcb->asoc.strmout[i].abandoned_unsent[0] = oldstream[i].abandoned_unsent[0];
12145 #endif
12146                         stcb->asoc.strmout[i].next_mid_ordered = oldstream[i].next_mid_ordered;
12147                         stcb->asoc.strmout[i].next_mid_unordered = oldstream[i].next_mid_unordered;
12148                         stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
12149                         stcb->asoc.strmout[i].sid = i;
12150                         stcb->asoc.strmout[i].state = oldstream[i].state;
12151                         /* now anything on those queues? */
12152                         TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
12153                                 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
12154                                 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
12155                         }
12156                 }
12157                 /* now the new streams */
12158                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
12159                 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) {
12160                         TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
12161                         stcb->asoc.strmout[i].chunks_on_queues = 0;
12162 #if defined(SCTP_DETAILED_STR_STATS)
12163                         for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
12164                                 stcb->asoc.strmout[i].abandoned_sent[j] = 0;
12165                                 stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
12166                         }
12167 #else
12168                         stcb->asoc.strmout[i].abandoned_sent[0] = 0;
12169                         stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
12170 #endif
12171                         stcb->asoc.strmout[i].next_mid_ordered = 0;
12172                         stcb->asoc.strmout[i].next_mid_unordered = 0;
12173                         stcb->asoc.strmout[i].sid = i;
12174                         stcb->asoc.strmout[i].last_msg_incomplete = 0;
12175                         stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
12176                         stcb->asoc.strmout[i].state = SCTP_STREAM_CLOSED;
12177                 }
12178                 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o;
12179                 SCTP_FREE(oldstream, SCTP_M_STRMO);
12180         }
12181 skip_stuff:
12182         if ((add_stream & 1) && (adding_o > 0)) {
12183                 asoc->strm_pending_add_size = adding_o;
12184                 asoc->peer_req_out = peer_asked;
12185                 sctp_add_an_out_stream(chk, seq, adding_o);
12186                 seq++;
12187                 asoc->stream_reset_outstanding++;
12188         }
12189         if ((add_stream & 2) && (adding_i > 0)) {
12190                 sctp_add_an_in_stream(chk, seq, adding_i);
12191                 seq++;
12192                 asoc->stream_reset_outstanding++;
12193         }
12194         if (send_in_req) {
12195                 sctp_add_stream_reset_in(chk, number_entries, list, seq);
12196                 seq++;
12197                 asoc->stream_reset_outstanding++;
12198         }
12199         if (send_tsn_req) {
12200                 sctp_add_stream_reset_tsn(chk, seq);
12201                 asoc->stream_reset_outstanding++;
12202         }
12203         asoc->str_reset = chk;
12204         /* insert the chunk for sending */
12205         TAILQ_INSERT_TAIL(&asoc->control_send_queue,
12206             chk,
12207             sctp_next);
12208         asoc->ctrl_queue_cnt++;
12209         if (stcb->asoc.send_sack) {
12210                 sctp_send_sack(stcb, SCTP_SO_LOCKED);
12211         }
12212         sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12213         return (0);
12214 }
12215
12216 void
12217 sctp_send_abort(struct mbuf *m, int iphlen, struct sockaddr *src, struct sockaddr *dst,
12218     struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
12219     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12220     uint32_t vrf_id, uint16_t port)
12221 {
12222         /* Don't respond to an ABORT with an ABORT. */
12223         if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
12224                 if (cause)
12225                         sctp_m_freem(cause);
12226                 return;
12227         }
12228         sctp_send_resp_msg(src, dst, sh, vtag, SCTP_ABORT_ASSOCIATION, cause,
12229             mflowtype, mflowid, fibnum,
12230             vrf_id, port);
12231         return;
12232 }
12233
12234 void
12235 sctp_send_operr_to(struct sockaddr *src, struct sockaddr *dst,
12236     struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
12237     uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12238     uint32_t vrf_id, uint16_t port)
12239 {
12240         sctp_send_resp_msg(src, dst, sh, vtag, SCTP_OPERATION_ERROR, cause,
12241             mflowtype, mflowid, fibnum,
12242             vrf_id, port);
12243         return;
12244 }
12245
12246 static struct mbuf *
12247 sctp_copy_resume(struct uio *uio,
12248     int max_send_len,
12249     int user_marks_eor,
12250     int *error,
12251     uint32_t *sndout,
12252     struct mbuf **new_tail)
12253 {
12254         struct mbuf *m;
12255
12256         m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12257             (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12258         if (m == NULL) {
12259                 /* The only possible error is EFAULT. */
12260                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12261                 *error = EFAULT;
12262         } else {
12263                 *sndout = m_length(m, NULL);
12264                 *new_tail = m_last(m);
12265         }
12266         return (m);
12267 }
12268
12269 static int
12270 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12271     struct uio *uio,
12272     int resv_upfront)
12273 {
12274         sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, resv_upfront, 0);
12275         if (sp->data == NULL) {
12276                 /* The only possible error is EFAULT. */
12277                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12278                 return (EFAULT);
12279         }
12280         sp->tail_mbuf = m_last(sp->data);
12281         return (0);
12282 }
12283
12284 static struct sctp_stream_queue_pending *
12285 sctp_copy_it_in(struct sctp_tcb *stcb,
12286     struct sctp_association *asoc,
12287     struct sctp_nonpad_sndrcvinfo *srcv,
12288     struct uio *uio,
12289     struct sctp_nets *net,
12290     ssize_t max_send_len,
12291     int user_marks_eor,
12292     int *error)
12293 {
12294
12295         /*-
12296          * This routine must be very careful in its work. Protocol
12297          * processing is up and running so care must be taken to spl...()
12298          * when you need to do something that may effect the stcb/asoc. The
12299          * sb is locked however. When data is copied the protocol processing
12300          * should be enabled since this is a slower operation...
12301          */
12302         struct sctp_stream_queue_pending *sp;
12303         int resv_in_first;
12304
12305         *error = 0;
12306         sctp_alloc_a_strmoq(stcb, sp);
12307         if (sp == NULL) {
12308                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12309                 *error = ENOMEM;
12310                 goto out_now;
12311         }
12312         sp->act_flags = 0;
12313         sp->sender_all_done = 0;
12314         sp->sinfo_flags = srcv->sinfo_flags;
12315         sp->timetolive = srcv->sinfo_timetolive;
12316         sp->ppid = srcv->sinfo_ppid;
12317         sp->context = srcv->sinfo_context;
12318         sp->fsn = 0;
12319         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12320         sp->sid = srcv->sinfo_stream;
12321         sp->length = (uint32_t)min(uio->uio_resid, max_send_len);
12322         if ((sp->length == (uint32_t)uio->uio_resid) &&
12323             ((user_marks_eor == 0) ||
12324             (srcv->sinfo_flags & SCTP_EOF) ||
12325             (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12326                 sp->msg_is_complete = 1;
12327         } else {
12328                 sp->msg_is_complete = 0;
12329         }
12330         sp->sender_all_done = 0;
12331         sp->some_taken = 0;
12332         sp->put_last_out = 0;
12333         resv_in_first = SCTP_DATA_CHUNK_OVERHEAD(stcb);
12334         sp->data = sp->tail_mbuf = NULL;
12335         if (sp->length == 0) {
12336                 goto skip_copy;
12337         }
12338         if (srcv->sinfo_keynumber_valid) {
12339                 sp->auth_keyid = srcv->sinfo_keynumber;
12340         } else {
12341                 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12342         }
12343         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12344                 sctp_auth_key_acquire(stcb, sp->auth_keyid);
12345                 sp->holds_key_ref = 1;
12346         }
12347         *error = sctp_copy_one(sp, uio, resv_in_first);
12348 skip_copy:
12349         if (*error) {
12350                 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12351                 sp = NULL;
12352         } else {
12353                 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12354                         sp->net = net;
12355                         atomic_add_int(&sp->net->ref_count, 1);
12356                 } else {
12357                         sp->net = NULL;
12358                 }
12359                 sctp_set_prsctp_policy(sp);
12360         }
12361 out_now:
12362         return (sp);
12363 }
12364
12365 int
12366 sctp_sosend(struct socket *so,
12367     struct sockaddr *addr,
12368     struct uio *uio,
12369     struct mbuf *top,
12370     struct mbuf *control,
12371     int flags,
12372     struct thread *p)
12373 {
12374         struct sctp_sndrcvinfo sndrcvninfo;
12375 #if defined(INET) && defined(INET6)
12376         struct sockaddr_in sin;
12377 #endif
12378         struct sockaddr *addr_to_use;
12379         int error;
12380         bool use_sndinfo;
12381
12382         if (control != NULL) {
12383                 /* process cmsg snd/rcv info (maybe a assoc-id) */
12384                 use_sndinfo = sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, sizeof(sndrcvninfo));
12385         } else {
12386                 use_sndinfo = false;
12387         }
12388 #if defined(INET) && defined(INET6)
12389         if ((addr != NULL) && (addr->sa_family == AF_INET6)) {
12390                 struct sockaddr_in6 *sin6;
12391
12392                 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
12393                         SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12394                         return (EINVAL);
12395                 }
12396                 sin6 = (struct sockaddr_in6 *)addr;
12397                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12398                         in6_sin6_2_sin(&sin, sin6);
12399                         addr_to_use = (struct sockaddr *)&sin;
12400                 } else {
12401                         addr_to_use = addr;
12402                 }
12403         } else {
12404                 addr_to_use = addr;
12405         }
12406 #else
12407         addr_to_use = addr;
12408 #endif
12409         error = sctp_lower_sosend(so, addr_to_use, uio, top, control, flags,
12410             use_sndinfo ? &sndrcvninfo : NULL, p);
12411         return (error);
12412 }
12413
12414 int
12415 sctp_lower_sosend(struct socket *so,
12416     struct sockaddr *addr,
12417     struct uio *uio,
12418     struct mbuf *top,
12419     struct mbuf *control,
12420     int flags,
12421     struct sctp_sndrcvinfo *srcv,
12422     struct thread *p)
12423 {
12424         struct sctp_nonpad_sndrcvinfo sndrcvninfo_buf;
12425         struct epoch_tracker et;
12426         struct timeval now;
12427         struct sctp_block_entry be;
12428         struct sctp_inpcb *inp;
12429         struct sctp_tcb *stcb = NULL;
12430         struct sctp_nets *net;
12431         struct sctp_association *asoc;
12432         struct sctp_inpcb *t_inp;
12433         struct sctp_nonpad_sndrcvinfo *sndrcvninfo;
12434         ssize_t sndlen = 0, max_len, local_add_more;
12435         ssize_t local_soresv = 0;
12436         sctp_assoc_t sinfo_assoc_id;
12437         int user_marks_eor;
12438         int nagle_applies = 0;
12439         int error;
12440         int queue_only = 0, queue_only_for_init = 0;
12441         int un_sent;
12442         int now_filled = 0;
12443         unsigned int inqueue_bytes = 0;
12444         uint16_t port;
12445         uint16_t sinfo_flags;
12446         uint16_t sinfo_stream;
12447         bool create_lock_applied = false;
12448         bool free_cnt_applied = false;
12449         bool some_on_control;
12450         bool got_all_of_the_send = false;
12451         bool non_blocking = false;
12452
12453         error = 0;
12454         net = NULL;
12455         stcb = NULL;
12456
12457         if ((uio == NULL) && (top == NULL)) {
12458                 error = EINVAL;
12459                 goto out_unlocked;
12460         }
12461         if (addr != NULL) {
12462                 union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12463
12464                 switch (raddr->sa.sa_family) {
12465 #ifdef INET
12466                 case AF_INET:
12467                         if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12468                                 error = EINVAL;
12469                                 goto out_unlocked;
12470                         }
12471                         port = raddr->sin.sin_port;
12472                         break;
12473 #endif
12474 #ifdef INET6
12475                 case AF_INET6:
12476                         if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12477                                 error = EINVAL;
12478                                 goto out_unlocked;
12479                         }
12480                         port = raddr->sin6.sin6_port;
12481                         break;
12482 #endif
12483                 default:
12484                         error = EAFNOSUPPORT;
12485                         goto out_unlocked;
12486                 }
12487         } else {
12488                 port = 0;
12489         }
12490         if (uio != NULL) {
12491                 if (uio->uio_resid < 0) {
12492                         error = EINVAL;
12493                         goto out_unlocked;
12494                 }
12495                 sndlen = uio->uio_resid;
12496         } else {
12497                 sndlen = SCTP_HEADER_LEN(top);
12498         }
12499         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %zd\n",
12500             (void *)addr, sndlen);
12501
12502         t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12503         if (inp == NULL) {
12504                 error = EINVAL;
12505                 goto out_unlocked;
12506         }
12507         user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12508         if ((uio == NULL) && (user_marks_eor != 0)) {
12509                 /*-
12510                  * We do not support eeor mode for
12511                  * sending with mbuf chains (like sendfile).
12512                  */
12513                 error = EINVAL;
12514                 goto out_unlocked;
12515         }
12516         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12517             SCTP_IS_LISTENING(inp)) {
12518                 /* The listener can NOT send. */
12519                 error = EINVAL;
12520                 goto out_unlocked;
12521         }
12522         atomic_add_int(&inp->total_sends, 1);
12523
12524         if (srcv != NULL) {
12525                 sndrcvninfo = (struct sctp_nonpad_sndrcvinfo *)srcv;
12526                 sinfo_assoc_id = sndrcvninfo->sinfo_assoc_id;
12527                 sinfo_flags = sndrcvninfo->sinfo_flags;
12528                 if (INVALID_SINFO_FLAG(sinfo_flags) ||
12529                     PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12530                         error = EINVAL;
12531                         goto out_unlocked;
12532                 }
12533                 if (sinfo_flags != 0) {
12534                         SCTP_STAT_INCR(sctps_sends_with_flags);
12535                 }
12536         } else {
12537                 sndrcvninfo = NULL;
12538                 sinfo_flags = inp->def_send.sinfo_flags;
12539                 sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12540         }
12541         if (flags & MSG_EOR) {
12542                 sinfo_flags |= SCTP_EOR;
12543         }
12544         if (flags & MSG_EOF) {
12545                 sinfo_flags |= SCTP_EOF;
12546         }
12547         if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12548                 error = EINVAL;
12549                 goto out_unlocked;
12550         }
12551         SCTP_INP_RLOCK(inp);
12552         if ((sinfo_flags & SCTP_SENDALL) &&
12553             (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
12554                 SCTP_INP_RUNLOCK(inp);
12555                 error = sctp_sendall(inp, uio, top, sndrcvninfo);
12556                 top = NULL;
12557                 goto out_unlocked;
12558         }
12559         /* Now we must find the association. */
12560         if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12561             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12562                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12563                 if (stcb != NULL) {
12564                         SCTP_TCB_LOCK(stcb);
12565                 }
12566                 SCTP_INP_RUNLOCK(inp);
12567         } else if (sinfo_assoc_id > SCTP_ALL_ASSOC) {
12568                 stcb = sctp_findasoc_ep_asocid_locked(inp, sinfo_assoc_id, 1);
12569                 SCTP_INP_RUNLOCK(inp);
12570                 if (stcb != NULL) {
12571                         SCTP_TCB_LOCK_ASSERT(stcb);
12572                 }
12573         } else if (addr != NULL) {
12574                 /*-
12575                  * Since we did not use findep we must
12576                  * increment it, and if we don't find a tcb
12577                  * decrement it.
12578                  */
12579                 SCTP_INP_INCR_REF(inp);
12580                 SCTP_INP_RUNLOCK(inp);
12581                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12582                 if (stcb == NULL) {
12583                         SCTP_INP_WLOCK(inp);
12584                         SCTP_INP_DECR_REF(inp);
12585                         SCTP_INP_WUNLOCK(inp);
12586                 } else {
12587                         SCTP_TCB_LOCK_ASSERT(stcb);
12588                 }
12589         } else {
12590                 SCTP_INP_RUNLOCK(inp);
12591         }
12592
12593 #ifdef INVARIANTS
12594         if (stcb != NULL) {
12595                 SCTP_TCB_LOCK_ASSERT(stcb);
12596         }
12597 #endif
12598
12599         if ((stcb == NULL) && (addr != NULL)) {
12600                 /* Possible implicit send? */
12601                 SCTP_ASOC_CREATE_LOCK(inp);
12602                 create_lock_applied = true;
12603                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12604                     (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12605                         error = EINVAL;
12606                         goto out_unlocked;
12607                 }
12608                 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12609                     (addr->sa_family == AF_INET6)) {
12610                         error = EINVAL;
12611                         goto out_unlocked;
12612                 }
12613                 SCTP_INP_WLOCK(inp);
12614                 SCTP_INP_INCR_REF(inp);
12615                 SCTP_INP_WUNLOCK(inp);
12616                 /* With the lock applied look again */
12617                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12618 #if defined(INET) || defined(INET6)
12619                 if ((stcb == NULL) && (control != NULL) && (port > 0)) {
12620                         stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
12621                 }
12622 #endif
12623                 if (stcb == NULL) {
12624                         SCTP_INP_WLOCK(inp);
12625                         SCTP_INP_DECR_REF(inp);
12626                         SCTP_INP_WUNLOCK(inp);
12627                 } else {
12628                         SCTP_TCB_LOCK_ASSERT(stcb);
12629                         SCTP_ASOC_CREATE_UNLOCK(inp);
12630                         create_lock_applied = false;
12631                 }
12632                 if (error != 0) {
12633                         goto out_unlocked;
12634                 }
12635                 if (t_inp != inp) {
12636                         error = ENOTCONN;
12637                         goto out_unlocked;
12638                 }
12639         }
12640         if (stcb == NULL) {
12641                 if (addr == NULL) {
12642                         error = ENOENT;
12643                         goto out_unlocked;
12644                 } else {
12645                         /* We must go ahead and start the INIT process */
12646                         uint32_t vrf_id;
12647
12648                         if ((sinfo_flags & SCTP_ABORT) ||
12649                             ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12650                                 /*-
12651                                  * User asks to abort a non-existent assoc,
12652                                  * or EOF a non-existent assoc with no data
12653                                  */
12654                                 error = ENOENT;
12655                                 goto out_unlocked;
12656                         }
12657                         /* get an asoc/stcb struct */
12658                         vrf_id = inp->def_vrf_id;
12659                         KASSERT(create_lock_applied, ("create_lock_applied is false"));
12660                         stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
12661                             inp->sctp_ep.pre_open_stream_count,
12662                             inp->sctp_ep.port,
12663                             p,
12664                             SCTP_INITIALIZE_AUTH_PARAMS);
12665                         if (stcb == NULL) {
12666                                 /* error is setup for us in the call. */
12667                                 KASSERT(error != 0, ("error is 0 although stcb is NULL"));
12668                                 goto out_unlocked;
12669                         }
12670                         SCTP_TCB_LOCK_ASSERT(stcb);
12671                         SCTP_ASOC_CREATE_UNLOCK(inp);
12672                         create_lock_applied = false;
12673                         /*
12674                          * Turn on queue only flag to prevent data from
12675                          * being sent
12676                          */
12677                         queue_only = 1;
12678                         SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
12679                         (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
12680                         if (control != NULL) {
12681                                 if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
12682                                         sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
12683                                             SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
12684                                         stcb = NULL;
12685                                         KASSERT(error != 0,
12686                                             ("error is 0 although sctp_process_cmsgs_for_init() indicated an error"));
12687                                         goto out_unlocked;
12688                                 }
12689                         }
12690                         /* out with the INIT */
12691                         queue_only_for_init = 1;
12692                         /*-
12693                          * we may want to dig in after this call and adjust the MTU
12694                          * value. It defaulted to 1500 (constant) but the ro
12695                          * structure may now have an update and thus we may need to
12696                          * change it BEFORE we append the message.
12697                          */
12698                 }
12699         }
12700
12701         KASSERT(!create_lock_applied, ("create_lock_applied is true"));
12702         KASSERT(stcb != NULL, ("stcb is NULL"));
12703         SCTP_TCB_LOCK_ASSERT(stcb);
12704
12705         asoc = &stcb->asoc;
12706         if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12707             (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12708                 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12709                         /* XXX: Could also be ECONNABORTED, not enough info. */
12710                         error = ECONNRESET;
12711                 } else {
12712                         error = ENOTCONN;
12713                 }
12714                 goto out_unlocked;
12715         }
12716         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
12717             (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
12718                 queue_only = 1;
12719         }
12720         /* Keep the stcb from being freed under our feet. */
12721         atomic_add_int(&asoc->refcnt, 1);
12722         free_cnt_applied = true;
12723         if (sndrcvninfo == NULL) {
12724                 /* Use a local copy to have a consistent view. */
12725                 sndrcvninfo_buf = asoc->def_send;
12726                 sndrcvninfo = &sndrcvninfo_buf;
12727                 sinfo_flags = sndrcvninfo->sinfo_flags;
12728                 if (flags & MSG_EOR) {
12729                         sinfo_flags |= SCTP_EOR;
12730                 }
12731                 if (flags & MSG_EOF) {
12732                         sinfo_flags |= SCTP_EOF;
12733                 }
12734         }
12735         /* Are we aborting? */
12736         if (sinfo_flags & SCTP_ABORT) {
12737                 struct mbuf *mm;
12738                 struct sctp_paramhdr *ph;
12739                 ssize_t tot_demand, tot_out = 0, max_out;
12740
12741                 SCTP_STAT_INCR(sctps_sends_with_abort);
12742                 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
12743                     (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
12744                         /* It has to be up before we abort. */
12745                         error = EINVAL;
12746                         goto out_unlocked;
12747                 }
12748                 /* How big is the user initiated abort? */
12749                 if (top != NULL) {
12750                         struct mbuf *cntm;
12751
12752                         if (sndlen != 0) {
12753                                 for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
12754                                         tot_out += SCTP_BUF_LEN(cntm);
12755                                 }
12756                         }
12757                         mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
12758                 } else {
12759                         /* Must fit in a MTU */
12760                         tot_out = sndlen;
12761                         tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12762                         if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12763                                 error = EMSGSIZE;
12764                                 goto out_unlocked;
12765                         }
12766                         mm = sctp_get_mbuf_for_msg((unsigned int)tot_demand, 0, M_NOWAIT, 1, MT_DATA);
12767                 }
12768                 if (mm == NULL) {
12769                         error = ENOMEM;
12770                         goto out_unlocked;
12771                 }
12772                 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12773                 max_out -= sizeof(struct sctp_abort_msg);
12774                 if (tot_out > max_out) {
12775                         tot_out = max_out;
12776                 }
12777                 ph = mtod(mm, struct sctp_paramhdr *);
12778                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12779                 ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + tot_out));
12780                 ph++;
12781                 SCTP_BUF_LEN(mm) = (int)(tot_out + sizeof(struct sctp_paramhdr));
12782                 if (top == NULL) {
12783                         SCTP_TCB_UNLOCK(stcb);
12784                         error = uiomove((caddr_t)ph, (int)tot_out, uio);
12785                         SCTP_TCB_LOCK(stcb);
12786                         if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12787                             (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12788                                 sctp_m_freem(mm);
12789                                 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12790                                         /*
12791                                          * XXX: Could also be ECONNABORTED,
12792                                          * not enough info.
12793                                          */
12794                                         error = ECONNRESET;
12795                                 } else {
12796                                         error = ENOTCONN;
12797                                 }
12798                                 goto out_unlocked;
12799                         }
12800                         if (error != 0) {
12801                                 /*-
12802                                  * Here if we can't get his data we
12803                                  * still abort we just don't get to
12804                                  * send the users note :-0
12805                                  */
12806                                 sctp_m_freem(mm);
12807                                 mm = NULL;
12808                                 error = 0;
12809                         }
12810                 } else {
12811                         if (sndlen != 0) {
12812                                 SCTP_BUF_NEXT(mm) = top;
12813                         }
12814                 }
12815                 atomic_subtract_int(&asoc->refcnt, 1);
12816                 free_cnt_applied = false;
12817                 /* release this lock, otherwise we hang on ourselves */
12818                 NET_EPOCH_ENTER(et);
12819                 sctp_abort_an_association(stcb->sctp_ep, stcb, mm, false, SCTP_SO_LOCKED);
12820                 NET_EPOCH_EXIT(et);
12821                 stcb = NULL;
12822                 /*
12823                  * In this case top is already chained to mm avoid double
12824                  * free, since we free it below if top != NULL and driver
12825                  * would free it after sending the packet out
12826                  */
12827                 if (sndlen != 0) {
12828                         top = NULL;
12829                 }
12830                 goto out_unlocked;
12831         }
12832
12833         KASSERT(stcb != NULL, ("stcb is NULL"));
12834         SCTP_TCB_LOCK_ASSERT(stcb);
12835         KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
12836             ("Association about to be freed"));
12837         KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
12838             ("Association was aborted"));
12839
12840         if (sinfo_flags & SCTP_ADDR_OVER) {
12841                 if (addr != NULL) {
12842                         net = sctp_findnet(stcb, addr);
12843                 } else {
12844                         net = NULL;
12845                 }
12846                 if ((net == NULL) ||
12847                     ((port != 0) && (port != stcb->rport))) {
12848                         error = EINVAL;
12849                         goto out_unlocked;
12850                 }
12851         } else {
12852                 if (asoc->alternate != NULL) {
12853                         net = asoc->alternate;
12854                 } else {
12855                         net = asoc->primary_destination;
12856                 }
12857         }
12858         if (sndlen == 0) {
12859                 if (sinfo_flags & SCTP_EOF) {
12860                         got_all_of_the_send = true;
12861                         goto dataless_eof;
12862                 } else {
12863                         error = EINVAL;
12864                         goto out_unlocked;
12865                 }
12866         }
12867         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12868                 if (sndlen > (ssize_t)asoc->smallest_mtu) {
12869                         error = EMSGSIZE;
12870                         goto out_unlocked;
12871                 }
12872         }
12873         sinfo_stream = sndrcvninfo->sinfo_stream;
12874         /* Is the stream no. valid? */
12875         if (sinfo_stream >= asoc->streamoutcnt) {
12876                 /* Invalid stream number */
12877                 error = EINVAL;
12878                 goto out_unlocked;
12879         }
12880         if ((asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPEN) &&
12881             (asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPENING)) {
12882                 /*
12883                  * Can't queue any data while stream reset is underway.
12884                  */
12885                 if (asoc->strmout[sinfo_stream].state > SCTP_STREAM_OPEN) {
12886                         error = EAGAIN;
12887                 } else {
12888                         error = EINVAL;
12889                 }
12890                 goto out_unlocked;
12891         }
12892         atomic_add_int(&stcb->total_sends, 1);
12893         if (SCTP_SO_IS_NBIO(so) || (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
12894                 non_blocking = true;
12895         }
12896         if (non_blocking) {
12897                 ssize_t amount;
12898
12899                 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
12900                 if (user_marks_eor == 0) {
12901                         amount = sndlen;
12902                 } else {
12903                         amount = 1;
12904                 }
12905                 if ((SCTP_SB_LIMIT_SND(so) < (amount + inqueue_bytes + asoc->sb_send_resv)) ||
12906                     (asoc->chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12907                         if ((sndlen > (ssize_t)SCTP_SB_LIMIT_SND(so)) &&
12908                             (user_marks_eor == 0)) {
12909                                 error = EMSGSIZE;
12910                         } else {
12911                                 error = EWOULDBLOCK;
12912                         }
12913                         goto out_unlocked;
12914                 }
12915         }
12916         atomic_add_int(&asoc->sb_send_resv, (int)sndlen);
12917         local_soresv = sndlen;
12918
12919         KASSERT(stcb != NULL, ("stcb is NULL"));
12920         SCTP_TCB_LOCK_ASSERT(stcb);
12921         KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
12922             ("Association about to be freed"));
12923         KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
12924             ("Association was aborted"));
12925
12926         /* Ok, we will attempt a msgsnd :> */
12927         if (p != NULL) {
12928                 p->td_ru.ru_msgsnd++;
12929         }
12930         /* Calculate the maximum we can send */
12931         inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
12932         if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12933                 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12934         } else {
12935                 max_len = 0;
12936         }
12937         /* Unless E_EOR mode is on, we must make a send FIT in one call. */
12938         if ((user_marks_eor == 0) &&
12939             (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12940                 /* It will NEVER fit. */
12941                 error = EMSGSIZE;
12942                 goto out_unlocked;
12943         }
12944         if (user_marks_eor != 0) {
12945                 local_add_more = (ssize_t)min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12946         } else {
12947                 /*-
12948                  * For non-eeor the whole message must fit in
12949                  * the socket send buffer.
12950                  */
12951                 local_add_more = sndlen;
12952         }
12953         if (non_blocking) {
12954                 goto skip_preblock;
12955         }
12956         if (((max_len <= local_add_more) && ((ssize_t)SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12957             (max_len == 0) ||
12958             ((asoc->chunks_on_out_queue + asoc->stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12959                 /* No room right now! */
12960                 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
12961                 SOCKBUF_LOCK(&so->so_snd);
12962                 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12963                     ((asoc->stream_queue_cnt + asoc->chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12964                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %zd) || (%d+%d > %d)\n",
12965                             (unsigned int)SCTP_SB_LIMIT_SND(so),
12966                             inqueue_bytes,
12967                             local_add_more,
12968                             asoc->stream_queue_cnt,
12969                             asoc->chunks_on_out_queue,
12970                             SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12971                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12972                                 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
12973                         }
12974                         be.error = 0;
12975                         stcb->block_entry = &be;
12976                         SCTP_TCB_UNLOCK(stcb);
12977                         error = sbwait(&so->so_snd);
12978                         if (error == 0) {
12979                                 if (so->so_error != 0) {
12980                                         error = so->so_error;
12981                                 }
12982                                 if (be.error != 0) {
12983                                         error = be.error;
12984                                 }
12985                         }
12986                         SOCKBUF_UNLOCK(&so->so_snd);
12987                         SCTP_TCB_LOCK(stcb);
12988                         stcb->block_entry = NULL;
12989                         if (error != 0) {
12990                                 goto out_unlocked;
12991                         }
12992                         if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12993                             (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12994                                 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12995                                         /*
12996                                          * XXX: Could also be ECONNABORTED,
12997                                          * not enough info.
12998                                          */
12999                                         error = ECONNRESET;
13000                                 } else {
13001                                         error = ENOTCONN;
13002                                 }
13003                                 goto out_unlocked;
13004                         }
13005                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13006                                 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13007                                     asoc, asoc->total_output_queue_size);
13008                         }
13009                         inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13010                         SOCKBUF_LOCK(&so->so_snd);
13011                 }
13012                 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13013                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13014                 } else {
13015                         max_len = 0;
13016                 }
13017                 SOCKBUF_UNLOCK(&so->so_snd);
13018         }
13019
13020 skip_preblock:
13021         KASSERT(stcb != NULL, ("stcb is NULL"));
13022         SCTP_TCB_LOCK_ASSERT(stcb);
13023         KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13024             ("Association about to be freed"));
13025         KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13026             ("Association was aborted"));
13027
13028         /*
13029          * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13030          * case NOTE: uio will be null when top/mbuf is passed
13031          */
13032         if (top == NULL) {
13033                 struct sctp_stream_queue_pending *sp;
13034                 struct sctp_stream_out *strm;
13035                 uint32_t sndout;
13036
13037                 if ((asoc->stream_locked) &&
13038                     (asoc->stream_locked_on != sinfo_stream)) {
13039                         error = EINVAL;
13040                         goto out;
13041                 }
13042                 strm = &asoc->strmout[sinfo_stream];
13043                 if (strm->last_msg_incomplete == 0) {
13044         do_a_copy_in:
13045                         SCTP_TCB_UNLOCK(stcb);
13046                         sp = sctp_copy_it_in(stcb, asoc, sndrcvninfo, uio, net, max_len, user_marks_eor, &error);
13047                         SCTP_TCB_LOCK(stcb);
13048                         if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13049                             (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13050                                 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13051                                         /*
13052                                          * XXX: Could also be ECONNABORTED,
13053                                          * not enough info.
13054                                          */
13055                                         error = ECONNRESET;
13056                                 } else {
13057                                         error = ENOTCONN;
13058                                 }
13059                                 goto out;
13060                         }
13061                         if (error != 0) {
13062                                 goto out;
13063                         }
13064                         /*
13065                          * Reject the sending of a new user message, if the
13066                          * association is about to be shut down.
13067                          */
13068                         if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
13069                             (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13070                             (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13071                             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13072                                 if (sp->data != 0) {
13073                                         sctp_m_freem(sp->data);
13074                                         sp->data = NULL;
13075                                         sp->tail_mbuf = NULL;
13076                                         sp->length = 0;
13077                                 }
13078                                 if (sp->net != NULL) {
13079                                         sctp_free_remote_addr(sp->net);
13080                                         sp->net = NULL;
13081                                 }
13082                                 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
13083                                 error = EPIPE;
13084                                 goto out_unlocked;
13085                         }
13086                         /* The out streams might be reallocated. */
13087                         strm = &asoc->strmout[sinfo_stream];
13088                         if (sp->msg_is_complete) {
13089                                 strm->last_msg_incomplete = 0;
13090                                 asoc->stream_locked = 0;
13091                         } else {
13092                                 /*
13093                                  * Just got locked to this guy in case of an
13094                                  * interrupt.
13095                                  */
13096                                 strm->last_msg_incomplete = 1;
13097                                 if (asoc->idata_supported == 0) {
13098                                         asoc->stream_locked = 1;
13099                                         asoc->stream_locked_on = sinfo_stream;
13100                                 }
13101                                 sp->sender_all_done = 0;
13102                         }
13103                         sctp_snd_sb_alloc(stcb, sp->length);
13104                         atomic_add_int(&asoc->stream_queue_cnt, 1);
13105                         if (sinfo_flags & SCTP_UNORDERED) {
13106                                 SCTP_STAT_INCR(sctps_sends_with_unord);
13107                         }
13108                         sp->processing = 1;
13109                         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13110                         asoc->ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp);
13111                 } else {
13112                         sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13113                         if (sp == NULL) {
13114                                 /* ???? Huh ??? last msg is gone */
13115 #ifdef INVARIANTS
13116                                 panic("Warning: Last msg marked incomplete, yet nothing left?");
13117 #else
13118                                 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13119                                 strm->last_msg_incomplete = 0;
13120 #endif
13121                                 goto do_a_copy_in;
13122                         }
13123                         if (sp->processing != 0) {
13124                                 error = EINVAL;
13125                                 goto out;
13126                         } else {
13127                                 sp->processing = 1;
13128                         }
13129                 }
13130
13131                 KASSERT(stcb != NULL, ("stcb is NULL"));
13132                 SCTP_TCB_LOCK_ASSERT(stcb);
13133                 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13134                     ("Association about to be freed"));
13135                 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13136                     ("Association was aborted"));
13137
13138                 while (uio->uio_resid > 0) {
13139                         /* How much room do we have? */
13140                         struct mbuf *new_tail, *mm;
13141
13142                         inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13143                         if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13144                                 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13145                         } else {
13146                                 max_len = 0;
13147                         }
13148                         if ((max_len > (ssize_t)SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13149                             ((max_len > 0) && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13150                             (uio->uio_resid <= max_len)) {
13151                                 SCTP_TCB_UNLOCK(stcb);
13152                                 sndout = 0;
13153                                 new_tail = NULL;
13154                                 mm = sctp_copy_resume(uio, (int)max_len, user_marks_eor, &error, &sndout, &new_tail);
13155                                 SCTP_TCB_LOCK(stcb);
13156                                 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13157                                     (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13158                                         /*
13159                                          * We need to get out. Peer probably
13160                                          * aborted.
13161                                          */
13162                                         sctp_m_freem(mm);
13163                                         if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13164                                                 /*
13165                                                  * XXX: Could also be
13166                                                  * ECONNABORTED, not enough
13167                                                  * info.
13168                                                  */
13169                                                 error = ECONNRESET;
13170                                         } else {
13171                                                 error = ENOTCONN;
13172                                         }
13173                                         goto out;
13174                                 }
13175                                 if ((mm == NULL) || (error != 0)) {
13176                                         if (mm != NULL) {
13177                                                 sctp_m_freem(mm);
13178                                         }
13179                                         if (sp != NULL) {
13180                                                 sp->processing = 0;
13181                                         }
13182                                         goto out;
13183                                 }
13184                                 /* Update the mbuf and count */
13185                                 if (sp->tail_mbuf != NULL) {
13186                                         /* Tack it to the end. */
13187                                         SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13188                                 } else {
13189                                         /* A stolen mbuf. */
13190                                         sp->data = mm;
13191                                 }
13192                                 sp->tail_mbuf = new_tail;
13193                                 sctp_snd_sb_alloc(stcb, sndout);
13194                                 atomic_add_int(&sp->length, sndout);
13195                                 if (sinfo_flags & SCTP_SACK_IMMEDIATELY) {
13196                                         sp->sinfo_flags |= SCTP_SACK_IMMEDIATELY;
13197                                 }
13198
13199                                 /* Did we reach EOR? */
13200                                 if ((uio->uio_resid == 0) &&
13201                                     ((user_marks_eor == 0) ||
13202                                     (sinfo_flags & SCTP_EOF) ||
13203                                     (user_marks_eor && (sinfo_flags & SCTP_EOR)))) {
13204                                         sp->msg_is_complete = 1;
13205                                 } else {
13206                                         sp->msg_is_complete = 0;
13207                                 }
13208                         }
13209
13210                         KASSERT(stcb != NULL, ("stcb is NULL"));
13211                         SCTP_TCB_LOCK_ASSERT(stcb);
13212                         KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13213                             ("Association about to be freed"));
13214                         KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13215                             ("Association was aborted"));
13216
13217                         if (uio->uio_resid == 0) {
13218                                 /* got it all? */
13219                                 continue;
13220                         }
13221                         /* PR-SCTP? */
13222                         if ((asoc->prsctp_supported) && (asoc->sent_queue_cnt_removeable > 0)) {
13223                                 /*
13224                                  * This is ugly but we must assure locking
13225                                  * order
13226                                  */
13227                                 sctp_prune_prsctp(stcb, asoc, sndrcvninfo, (int)sndlen);
13228                                 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13229                                 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes)
13230                                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13231                                 else
13232                                         max_len = 0;
13233                                 if (max_len > 0) {
13234                                         continue;
13235                                 }
13236                         }
13237                         /* wait for space now */
13238                         if (non_blocking) {
13239                                 /* Non-blocking io in place out */
13240                                 if (sp != NULL) {
13241                                         sp->processing = 0;
13242                                 }
13243                                 goto skip_out_eof;
13244                         }
13245                         /* What about the INIT, send it maybe */
13246                         if (queue_only_for_init) {
13247                                 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13248                                         /* a collision took us forward? */
13249                                         queue_only = 0;
13250                                 } else {
13251                                         NET_EPOCH_ENTER(et);
13252                                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13253                                         NET_EPOCH_EXIT(et);
13254                                         SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13255                                         queue_only = 1;
13256                                 }
13257                         }
13258                         if ((net->flight_size > net->cwnd) &&
13259                             (asoc->sctp_cmt_on_off == 0)) {
13260                                 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13261                                 queue_only = 1;
13262                         } else if (asoc->ifp_had_enobuf) {
13263                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
13264                                 if (net->flight_size > (2 * net->mtu)) {
13265                                         queue_only = 1;
13266                                 }
13267                                 asoc->ifp_had_enobuf = 0;
13268                         }
13269                         un_sent = asoc->total_output_queue_size - asoc->total_flight;
13270                         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13271                             (asoc->total_flight > 0) &&
13272                             (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13273                             (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
13274                                 /*-
13275                                  * Ok, Nagle is set on and we have data outstanding.
13276                                  * Don't send anything and let SACKs drive out the
13277                                  * data unless we have a "full" segment to send.
13278                                  */
13279                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13280                                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13281                                 }
13282                                 SCTP_STAT_INCR(sctps_naglequeued);
13283                                 nagle_applies = 1;
13284                         } else {
13285                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13286                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13287                                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13288                                 }
13289                                 SCTP_STAT_INCR(sctps_naglesent);
13290                                 nagle_applies = 0;
13291                         }
13292                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13293                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13294                                     nagle_applies, un_sent);
13295                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
13296                                     asoc->total_flight,
13297                                     asoc->chunks_on_out_queue, asoc->total_flight_count);
13298                         }
13299                         if (queue_only_for_init) {
13300                                 queue_only_for_init = 0;
13301                         }
13302                         if ((queue_only == 0) && (nagle_applies == 0)) {
13303                                 /*-
13304                                  * need to start chunk output
13305                                  * before blocking.. note that if
13306                                  * a lock is already applied, then
13307                                  * the input via the net is happening
13308                                  * and I don't need to start output :-D
13309                                  */
13310                                 NET_EPOCH_ENTER(et);
13311                                 sctp_chunk_output(inp, stcb,
13312                                     SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13313                                 NET_EPOCH_EXIT(et);
13314                         }
13315                         /*-
13316                          * This is a bit strange, but I think it will
13317                          * work. The total_output_queue_size is locked and
13318                          * protected by the TCB_LOCK, which we just released.
13319                          * There is a race that can occur between releasing it
13320                          * above, and me getting the socket lock, where sacks
13321                          * come in but we have not put the SB_WAIT on the
13322                          * so_snd buffer to get the wakeup. After the LOCK
13323                          * is applied the sack_processing will also need to
13324                          * LOCK the so->so_snd to do the actual sowwakeup(). So
13325                          * once we have the socket buffer lock if we recheck the
13326                          * size we KNOW we will get to sleep safely with the
13327                          * wakeup flag in place.
13328                          */
13329                         inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13330                         SOCKBUF_LOCK(&so->so_snd);
13331                         if (SCTP_SB_LIMIT_SND(so) <= (inqueue_bytes +
13332                             min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13333                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13334                                         sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13335                                             asoc, uio->uio_resid);
13336                                 }
13337                                 be.error = 0;
13338                                 stcb->block_entry = &be;
13339                                 SCTP_TCB_UNLOCK(stcb);
13340                                 error = sbwait(&so->so_snd);
13341                                 if (error == 0) {
13342                                         if (so->so_error != 0)
13343                                                 error = so->so_error;
13344                                         if (be.error != 0) {
13345                                                 error = be.error;
13346                                         }
13347                                 }
13348                                 SOCKBUF_UNLOCK(&so->so_snd);
13349                                 SCTP_TCB_LOCK(stcb);
13350                                 stcb->block_entry = NULL;
13351                                 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13352                                     (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13353                                         if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13354                                                 /*
13355                                                  * XXX: Could also be
13356                                                  * ECONNABORTED, not enough
13357                                                  * info.
13358                                                  */
13359                                                 error = ECONNRESET;
13360                                         } else {
13361                                                 error = ENOTCONN;
13362                                         }
13363                                         goto out_unlocked;
13364                                 }
13365                                 if (error != 0) {
13366                                         if (sp != NULL) {
13367                                                 sp->processing = 0;
13368                                         }
13369                                         goto out_unlocked;
13370                                 }
13371                         } else {
13372                                 SOCKBUF_UNLOCK(&so->so_snd);
13373                         }
13374                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13375                                 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13376                                     asoc, asoc->total_output_queue_size);
13377                         }
13378                 }
13379
13380                 KASSERT(stcb != NULL, ("stcb is NULL"));
13381                 SCTP_TCB_LOCK_ASSERT(stcb);
13382                 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13383                     ("Association about to be freed"));
13384                 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13385                     ("Association was aborted"));
13386
13387                 /* The out streams might be reallocated. */
13388                 strm = &asoc->strmout[sinfo_stream];
13389                 if (sp != NULL) {
13390                         if (sp->msg_is_complete == 0) {
13391                                 strm->last_msg_incomplete = 1;
13392                                 if (asoc->idata_supported == 0) {
13393                                         asoc->stream_locked = 1;
13394                                         asoc->stream_locked_on = sinfo_stream;
13395                                 }
13396                         } else {
13397                                 sp->sender_all_done = 1;
13398                                 strm->last_msg_incomplete = 0;
13399                                 asoc->stream_locked = 0;
13400                         }
13401                         sp->processing = 0;
13402                 } else {
13403                         SCTP_PRINTF("Huh no sp TSNH?\n");
13404                         strm->last_msg_incomplete = 0;
13405                         asoc->stream_locked = 0;
13406                 }
13407                 if (uio->uio_resid == 0) {
13408                         got_all_of_the_send = true;
13409                 }
13410         } else {
13411                 error = sctp_msg_append(stcb, net, top, sndrcvninfo);
13412                 top = NULL;
13413                 if ((sinfo_flags & SCTP_EOF) != 0) {
13414                         got_all_of_the_send = true;
13415                 }
13416         }
13417         if (error != 0) {
13418                 goto out;
13419         }
13420
13421 dataless_eof:
13422         KASSERT(stcb != NULL, ("stcb is NULL"));
13423         SCTP_TCB_LOCK_ASSERT(stcb);
13424         KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13425             ("Association about to be freed"));
13426         KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13427             ("Association was aborted"));
13428
13429         /* EOF thing ? */
13430         if ((sinfo_flags & SCTP_EOF) && got_all_of_the_send) {
13431                 SCTP_STAT_INCR(sctps_sends_with_eof);
13432                 error = 0;
13433                 if (TAILQ_EMPTY(&asoc->send_queue) &&
13434                     TAILQ_EMPTY(&asoc->sent_queue) &&
13435                     sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED) == 0) {
13436                         if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
13437                                 goto abort_anyway;
13438                         }
13439                         /* there is nothing queued to send, so I'm done... */
13440                         if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
13441                             (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13442                             (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13443                                 struct sctp_nets *netp;
13444
13445                                 /* only send SHUTDOWN the first time through */
13446                                 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13447                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13448                                 }
13449                                 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
13450                                 sctp_stop_timers_for_shutdown(stcb);
13451                                 if (asoc->alternate != NULL) {
13452                                         netp = asoc->alternate;
13453                                 } else {
13454                                         netp = asoc->primary_destination;
13455                                 }
13456                                 sctp_send_shutdown(stcb, netp);
13457                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13458                                     netp);
13459                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13460                                     NULL);
13461                         }
13462                 } else {
13463                         /*-
13464                          * we still got (or just got) data to send, so set
13465                          * SHUTDOWN_PENDING
13466                          */
13467                         /*-
13468                          * XXX sockets draft says that SCTP_EOF should be
13469                          * sent with no data.  currently, we will allow user
13470                          * data to be sent first and move to
13471                          * SHUTDOWN-PENDING
13472                          */
13473                         if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
13474                             (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13475                             (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13476                                 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
13477                                         SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
13478                                 }
13479                                 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
13480                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
13481                                     TAILQ_EMPTY(&asoc->sent_queue) &&
13482                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13483                                         struct mbuf *op_err;
13484                                         char msg[SCTP_DIAG_INFO_LEN];
13485
13486                         abort_anyway:
13487                                         if (free_cnt_applied) {
13488                                                 atomic_subtract_int(&asoc->refcnt, 1);
13489                                                 free_cnt_applied = false;
13490                                         }
13491                                         SCTP_SNPRINTF(msg, sizeof(msg),
13492                                             "%s:%d at %s", __FILE__, __LINE__, __func__);
13493                                         op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
13494                                             msg);
13495                                         NET_EPOCH_ENTER(et);
13496                                         sctp_abort_an_association(stcb->sctp_ep, stcb,
13497                                             op_err, false, SCTP_SO_LOCKED);
13498                                         NET_EPOCH_EXIT(et);
13499                                         stcb = NULL;
13500                                         error = ECONNABORTED;
13501                                         goto out;
13502                                 }
13503                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13504                         }
13505                 }
13506         }
13507
13508 skip_out_eof:
13509         KASSERT(stcb != NULL, ("stcb is NULL"));
13510         SCTP_TCB_LOCK_ASSERT(stcb);
13511         KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13512             ("Association about to be freed"));
13513         KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13514             ("Association was aborted"));
13515
13516         some_on_control = !TAILQ_EMPTY(&asoc->control_send_queue);
13517         if (queue_only_for_init) {
13518                 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13519                         /* a collision took us forward? */
13520                         queue_only = 0;
13521                 } else {
13522                         NET_EPOCH_ENTER(et);
13523                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13524                         NET_EPOCH_EXIT(et);
13525                         SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13526                         queue_only = 1;
13527                 }
13528         }
13529
13530         KASSERT(stcb != NULL, ("stcb is NULL"));
13531         SCTP_TCB_LOCK_ASSERT(stcb);
13532         KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13533             ("Association about to be freed"));
13534         KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13535             ("Association was aborted"));
13536
13537         if ((net->flight_size > net->cwnd) &&
13538             (asoc->sctp_cmt_on_off == 0)) {
13539                 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13540                 queue_only = 1;
13541         } else if (asoc->ifp_had_enobuf) {
13542                 SCTP_STAT_INCR(sctps_ifnomemqueued);
13543                 if (net->flight_size > (2 * net->mtu)) {
13544                         queue_only = 1;
13545                 }
13546                 asoc->ifp_had_enobuf = 0;
13547         }
13548         un_sent = asoc->total_output_queue_size - asoc->total_flight;
13549         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13550             (asoc->total_flight > 0) &&
13551             (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13552             (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
13553                 /*-
13554                  * Ok, Nagle is set on and we have data outstanding.
13555                  * Don't send anything and let SACKs drive out the
13556                  * data unless wen have a "full" segment to send.
13557                  */
13558                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13559                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13560                 }
13561                 SCTP_STAT_INCR(sctps_naglequeued);
13562                 nagle_applies = 1;
13563         } else {
13564                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13565                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13566                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13567                 }
13568                 SCTP_STAT_INCR(sctps_naglesent);
13569                 nagle_applies = 0;
13570         }
13571         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13572                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13573                     nagle_applies, un_sent);
13574                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
13575                     asoc->total_flight,
13576                     asoc->chunks_on_out_queue, asoc->total_flight_count);
13577         }
13578
13579         KASSERT(stcb != NULL, ("stcb is NULL"));
13580         SCTP_TCB_LOCK_ASSERT(stcb);
13581         KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13582             ("Association about to be freed"));
13583         KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13584             ("Association was aborted"));
13585
13586         NET_EPOCH_ENTER(et);
13587         if ((queue_only == 0) && (nagle_applies == 0) && (asoc->peers_rwnd && un_sent)) {
13588                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13589         } else if ((queue_only == 0) &&
13590                     (asoc->peers_rwnd == 0) &&
13591             (asoc->total_flight == 0)) {
13592                 /* We get to have a probe outstanding */
13593                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13594         } else if (some_on_control) {
13595                 int num_out, reason;
13596
13597                 /* Here we do control only */
13598                 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out,
13599                     &reason, 1, 1, &now, &now_filled,
13600                     sctp_get_frag_point(stcb),
13601                     SCTP_SO_LOCKED);
13602         }
13603         NET_EPOCH_EXIT(et);
13604         SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13605             queue_only, asoc->peers_rwnd, un_sent,
13606             asoc->total_flight, asoc->chunks_on_out_queue,
13607             asoc->total_output_queue_size, error);
13608
13609         KASSERT(stcb != NULL, ("stcb is NULL"));
13610         SCTP_TCB_LOCK_ASSERT(stcb);
13611         KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13612             ("Association about to be freed"));
13613         KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13614             ("Association was aborted"));
13615
13616 out:
13617 out_unlocked:
13618         if (create_lock_applied) {
13619                 SCTP_ASOC_CREATE_UNLOCK(inp);
13620         }
13621         if (stcb != NULL) {
13622                 if (local_soresv) {
13623                         atomic_subtract_int(&asoc->sb_send_resv, (int)sndlen);
13624                 }
13625                 if (free_cnt_applied) {
13626                         atomic_subtract_int(&asoc->refcnt, 1);
13627                 }
13628                 SCTP_TCB_UNLOCK(stcb);
13629         }
13630         if (top != NULL) {
13631                 sctp_m_freem(top);
13632         }
13633         if (control != NULL) {
13634                 sctp_m_freem(control);
13635         }
13636         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, error);
13637         return (error);
13638 }
13639
13640 /*
13641  * generate an AUTHentication chunk, if required
13642  */
13643 struct mbuf *
13644 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13645     struct sctp_auth_chunk **auth_ret, uint32_t *offset,
13646     struct sctp_tcb *stcb, uint8_t chunk)
13647 {
13648         struct mbuf *m_auth;
13649         struct sctp_auth_chunk *auth;
13650         int chunk_len;
13651         struct mbuf *cn;
13652
13653         if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13654             (stcb == NULL))
13655                 return (m);
13656
13657         if (stcb->asoc.auth_supported == 0) {
13658                 return (m);
13659         }
13660         /* does the requested chunk require auth? */
13661         if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13662                 return (m);
13663         }
13664         m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_NOWAIT, 1, MT_HEADER);
13665         if (m_auth == NULL) {
13666                 /* no mbuf's */
13667                 return (m);
13668         }
13669         /* reserve some space if this will be the first mbuf */
13670         if (m == NULL)
13671                 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13672         /* fill in the AUTH chunk details */
13673         auth = mtod(m_auth, struct sctp_auth_chunk *);
13674         memset(auth, 0, sizeof(*auth));
13675         auth->ch.chunk_type = SCTP_AUTHENTICATION;
13676         auth->ch.chunk_flags = 0;
13677         chunk_len = sizeof(*auth) +
13678             sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13679         auth->ch.chunk_length = htons(chunk_len);
13680         auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13681         /* key id and hmac digest will be computed and filled in upon send */
13682
13683         /* save the offset where the auth was inserted into the chain */
13684         *offset = 0;
13685         for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
13686                 *offset += SCTP_BUF_LEN(cn);
13687         }
13688
13689         /* update length and return pointer to the auth chunk */
13690         SCTP_BUF_LEN(m_auth) = chunk_len;
13691         m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13692         if (auth_ret != NULL)
13693                 *auth_ret = auth;
13694
13695         return (m);
13696 }
13697
13698 #ifdef INET6
13699 int
13700 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro)
13701 {
13702         struct nd_prefix *pfx = NULL;
13703         struct nd_pfxrouter *pfxrtr = NULL;
13704         struct sockaddr_in6 gw6;
13705
13706         if (ro == NULL || ro->ro_nh == NULL || src6->sin6_family != AF_INET6)
13707                 return (0);
13708
13709         /* get prefix entry of address */
13710         ND6_RLOCK();
13711         LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13712                 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13713                         continue;
13714                 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13715                     &src6->sin6_addr, &pfx->ndpr_mask))
13716                         break;
13717         }
13718         /* no prefix entry in the prefix list */
13719         if (pfx == NULL) {
13720                 ND6_RUNLOCK();
13721                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13722                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13723                 return (0);
13724         }
13725
13726         SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13727         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13728
13729         /* search installed gateway from prefix entry */
13730         LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) {
13731                 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13732                 gw6.sin6_family = AF_INET6;
13733                 gw6.sin6_len = sizeof(struct sockaddr_in6);
13734                 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13735                     sizeof(struct in6_addr));
13736                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13737                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13738                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13739                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
13740                 if (sctp_cmpaddr((struct sockaddr *)&gw6, &ro->ro_nh->gw_sa)) {
13741                         ND6_RUNLOCK();
13742                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13743                         return (1);
13744                 }
13745         }
13746         ND6_RUNLOCK();
13747         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13748         return (0);
13749 }
13750 #endif
13751
13752 int
13753 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro)
13754 {
13755 #ifdef INET
13756         struct sockaddr_in *sin, *mask;
13757         struct ifaddr *ifa;
13758         struct in_addr srcnetaddr, gwnetaddr;
13759
13760         if (ro == NULL || ro->ro_nh == NULL ||
13761             sifa->address.sa.sa_family != AF_INET) {
13762                 return (0);
13763         }
13764         ifa = (struct ifaddr *)sifa->ifa;
13765         mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13766         sin = &sifa->address.sin;
13767         srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13768         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13769         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13770         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13771
13772         sin = &ro->ro_nh->gw4_sa;
13773         gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13774         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13775         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
13776         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13777         if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13778                 return (1);
13779         }
13780 #endif
13781         return (0);
13782 }