]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/netinet/sctp_output.c
MFC r228031:
[FreeBSD/releng/9.0.git] / sys / netinet / sctp_output.c
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *   the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <netinet/sctp_os.h>
39 #include <sys/proc.h>
40 #include <netinet/sctp_var.h>
41 #include <netinet/sctp_sysctl.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctp_pcb.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_uio.h>
47 #include <netinet/sctputil.h>
48 #include <netinet/sctp_auth.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_asconf.h>
51 #include <netinet/sctp_indata.h>
52 #include <netinet/sctp_bsd_addr.h>
53 #include <netinet/sctp_input.h>
54 #include <netinet/sctp_crc32.h>
55 #include <netinet/udp.h>
56 #include <machine/in_cksum.h>
57
58
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 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
1865 int
1866 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1867     int ipv4_addr_legal,
1868     int ipv6_addr_legal,
1869     int loopback_scope,
1870     int ipv4_local_scope,
1871     int local_scope,
1872     int site_scope,
1873     int do_update)
1874 {
1875         if ((loopback_scope == 0) &&
1876             (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1877                 /*
1878                  * skip loopback if not in scope *
1879                  */
1880                 return (0);
1881         }
1882         switch (ifa->address.sa.sa_family) {
1883 #ifdef INET
1884         case AF_INET:
1885                 if (ipv4_addr_legal) {
1886                         struct sockaddr_in *sin;
1887
1888                         sin = (struct sockaddr_in *)&ifa->address.sin;
1889                         if (sin->sin_addr.s_addr == 0) {
1890                                 /* not in scope , unspecified */
1891                                 return (0);
1892                         }
1893                         if ((ipv4_local_scope == 0) &&
1894                             (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1895                                 /* private address not in scope */
1896                                 return (0);
1897                         }
1898                 } else {
1899                         return (0);
1900                 }
1901                 break;
1902 #endif
1903 #ifdef INET6
1904         case AF_INET6:
1905                 if (ipv6_addr_legal) {
1906                         struct sockaddr_in6 *sin6;
1907
1908                         /*
1909                          * Must update the flags,  bummer, which means any
1910                          * IFA locks must now be applied HERE <->
1911                          */
1912                         if (do_update) {
1913                                 sctp_gather_internal_ifa_flags(ifa);
1914                         }
1915                         if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1916                                 return (0);
1917                         }
1918                         /* ok to use deprecated addresses? */
1919                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1920                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1921                                 /* skip unspecifed addresses */
1922                                 return (0);
1923                         }
1924                         if (    /* (local_scope == 0) && */
1925                             (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1926                                 return (0);
1927                         }
1928                         if ((site_scope == 0) &&
1929                             (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1930                                 return (0);
1931                         }
1932                 } else {
1933                         return (0);
1934                 }
1935                 break;
1936 #endif
1937         default:
1938                 return (0);
1939         }
1940         return (1);
1941 }
1942
1943 static struct mbuf *
1944 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1945 {
1946         struct sctp_paramhdr *parmh;
1947         struct mbuf *mret;
1948         int len;
1949
1950         switch (ifa->address.sa.sa_family) {
1951 #ifdef INET
1952         case AF_INET:
1953                 len = sizeof(struct sctp_ipv4addr_param);
1954                 break;
1955 #endif
1956 #ifdef INET6
1957         case AF_INET6:
1958                 len = sizeof(struct sctp_ipv6addr_param);
1959                 break;
1960 #endif
1961         default:
1962                 return (m);
1963         }
1964         if (M_TRAILINGSPACE(m) >= len) {
1965                 /* easy side we just drop it on the end */
1966                 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1967                 mret = m;
1968         } else {
1969                 /* Need more space */
1970                 mret = m;
1971                 while (SCTP_BUF_NEXT(mret) != NULL) {
1972                         mret = SCTP_BUF_NEXT(mret);
1973                 }
1974                 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1975                 if (SCTP_BUF_NEXT(mret) == NULL) {
1976                         /* We are hosed, can't add more addresses */
1977                         return (m);
1978                 }
1979                 mret = SCTP_BUF_NEXT(mret);
1980                 parmh = mtod(mret, struct sctp_paramhdr *);
1981         }
1982         /* now add the parameter */
1983         switch (ifa->address.sa.sa_family) {
1984 #ifdef INET
1985         case AF_INET:
1986                 {
1987                         struct sctp_ipv4addr_param *ipv4p;
1988                         struct sockaddr_in *sin;
1989
1990                         sin = (struct sockaddr_in *)&ifa->address.sin;
1991                         ipv4p = (struct sctp_ipv4addr_param *)parmh;
1992                         parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1993                         parmh->param_length = htons(len);
1994                         ipv4p->addr = sin->sin_addr.s_addr;
1995                         SCTP_BUF_LEN(mret) += len;
1996                         break;
1997                 }
1998 #endif
1999 #ifdef INET6
2000         case AF_INET6:
2001                 {
2002                         struct sctp_ipv6addr_param *ipv6p;
2003                         struct sockaddr_in6 *sin6;
2004
2005                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
2006                         ipv6p = (struct sctp_ipv6addr_param *)parmh;
2007                         parmh->param_type = htons(SCTP_IPV6_ADDRESS);
2008                         parmh->param_length = htons(len);
2009                         memcpy(ipv6p->addr, &sin6->sin6_addr,
2010                             sizeof(ipv6p->addr));
2011                         /* clear embedded scope in the address */
2012                         in6_clearscope((struct in6_addr *)ipv6p->addr);
2013                         SCTP_BUF_LEN(mret) += len;
2014                         break;
2015                 }
2016 #endif
2017         default:
2018                 return (m);
2019         }
2020         return (mret);
2021 }
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 {
2029         struct sctp_vrf *vrf = NULL;
2030         int cnt, limit_out = 0, total_count;
2031         uint32_t vrf_id;
2032
2033         vrf_id = inp->def_vrf_id;
2034         SCTP_IPI_ADDR_RLOCK();
2035         vrf = sctp_find_vrf(vrf_id);
2036         if (vrf == NULL) {
2037                 SCTP_IPI_ADDR_RUNLOCK();
2038                 return (m_at);
2039         }
2040         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2041                 struct sctp_ifa *sctp_ifap;
2042                 struct sctp_ifn *sctp_ifnp;
2043
2044                 cnt = cnt_inits_to;
2045                 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2046                         limit_out = 1;
2047                         cnt = SCTP_ADDRESS_LIMIT;
2048                         goto skip_count;
2049                 }
2050                 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2051                         if ((scope->loopback_scope == 0) &&
2052                             SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2053                                 /*
2054                                  * Skip loopback devices if loopback_scope
2055                                  * not set
2056                                  */
2057                                 continue;
2058                         }
2059                         LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2060                                 if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2061                                         continue;
2062                                 }
2063                                 if (sctp_is_address_in_scope(sctp_ifap,
2064                                     scope->ipv4_addr_legal,
2065                                     scope->ipv6_addr_legal,
2066                                     scope->loopback_scope,
2067                                     scope->ipv4_local_scope,
2068                                     scope->local_scope,
2069                                     scope->site_scope, 1) == 0) {
2070                                         continue;
2071                                 }
2072                                 cnt++;
2073                                 if (cnt > SCTP_ADDRESS_LIMIT) {
2074                                         break;
2075                                 }
2076                         }
2077                         if (cnt > SCTP_ADDRESS_LIMIT) {
2078                                 break;
2079                         }
2080                 }
2081 skip_count:
2082                 if (cnt > 1) {
2083                         total_count = 0;
2084                         LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2085                                 cnt = 0;
2086                                 if ((scope->loopback_scope == 0) &&
2087                                     SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2088                                         /*
2089                                          * Skip loopback devices if
2090                                          * loopback_scope not set
2091                                          */
2092                                         continue;
2093                                 }
2094                                 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2095                                         if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2096                                                 continue;
2097                                         }
2098                                         if (sctp_is_address_in_scope(sctp_ifap,
2099                                             scope->ipv4_addr_legal,
2100                                             scope->ipv6_addr_legal,
2101                                             scope->loopback_scope,
2102                                             scope->ipv4_local_scope,
2103                                             scope->local_scope,
2104                                             scope->site_scope, 0) == 0) {
2105                                                 continue;
2106                                         }
2107                                         m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2108                                         if (limit_out) {
2109                                                 cnt++;
2110                                                 total_count++;
2111                                                 if (cnt >= 2) {
2112                                                         /*
2113                                                          * two from each
2114                                                          * address
2115                                                          */
2116                                                         break;
2117                                                 }
2118                                                 if (total_count > SCTP_ADDRESS_LIMIT) {
2119                                                         /* No more addresses */
2120                                                         break;
2121                                                 }
2122                                         }
2123                                 }
2124                         }
2125                 }
2126         } else {
2127                 struct sctp_laddr *laddr;
2128
2129                 cnt = cnt_inits_to;
2130                 /* First, how many ? */
2131                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2132                         if (laddr->ifa == NULL) {
2133                                 continue;
2134                         }
2135                         if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2136                                 /*
2137                                  * Address being deleted by the system, dont
2138                                  * list.
2139                                  */
2140                                 continue;
2141                         if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2142                                 /*
2143                                  * Address being deleted on this ep don't
2144                                  * list.
2145                                  */
2146                                 continue;
2147                         }
2148                         if (sctp_is_address_in_scope(laddr->ifa,
2149                             scope->ipv4_addr_legal,
2150                             scope->ipv6_addr_legal,
2151                             scope->loopback_scope,
2152                             scope->ipv4_local_scope,
2153                             scope->local_scope,
2154                             scope->site_scope, 1) == 0) {
2155                                 continue;
2156                         }
2157                         cnt++;
2158                 }
2159                 if (cnt > SCTP_ADDRESS_LIMIT) {
2160                         limit_out = 1;
2161                 }
2162                 /*
2163                  * To get through a NAT we only list addresses if we have
2164                  * more than one. That way if you just bind a single address
2165                  * we let the source of the init dictate our address.
2166                  */
2167                 if (cnt > 1) {
2168                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2169                                 cnt = 0;
2170                                 if (laddr->ifa == NULL) {
2171                                         continue;
2172                                 }
2173                                 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2174                                         continue;
2175
2176                                 if (sctp_is_address_in_scope(laddr->ifa,
2177                                     scope->ipv4_addr_legal,
2178                                     scope->ipv6_addr_legal,
2179                                     scope->loopback_scope,
2180                                     scope->ipv4_local_scope,
2181                                     scope->local_scope,
2182                                     scope->site_scope, 0) == 0) {
2183                                         continue;
2184                                 }
2185                                 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2186                                 cnt++;
2187                                 if (cnt >= SCTP_ADDRESS_LIMIT) {
2188                                         break;
2189                                 }
2190                         }
2191                 }
2192         }
2193         SCTP_IPI_ADDR_RUNLOCK();
2194         return (m_at);
2195 }
2196
2197 static struct sctp_ifa *
2198 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2199     uint8_t dest_is_loop,
2200     uint8_t dest_is_priv,
2201     sa_family_t fam)
2202 {
2203         uint8_t dest_is_global = 0;
2204
2205         /* dest_is_priv is true if destination is a private address */
2206         /* dest_is_loop is true if destination is a loopback addresses */
2207
2208         /**
2209          * Here we determine if its a preferred address. A preferred address
2210          * means it is the same scope or higher scope then the destination.
2211          * L = loopback, P = private, G = global
2212          * -----------------------------------------
2213          *    src    |  dest | result
2214          *  ----------------------------------------
2215          *     L     |    L  |    yes
2216          *  -----------------------------------------
2217          *     P     |    L  |    yes-v4 no-v6
2218          *  -----------------------------------------
2219          *     G     |    L  |    yes-v4 no-v6
2220          *  -----------------------------------------
2221          *     L     |    P  |    no
2222          *  -----------------------------------------
2223          *     P     |    P  |    yes
2224          *  -----------------------------------------
2225          *     G     |    P  |    no
2226          *   -----------------------------------------
2227          *     L     |    G  |    no
2228          *   -----------------------------------------
2229          *     P     |    G  |    no
2230          *    -----------------------------------------
2231          *     G     |    G  |    yes
2232          *    -----------------------------------------
2233          */
2234
2235         if (ifa->address.sa.sa_family != fam) {
2236                 /* forget mis-matched family */
2237                 return (NULL);
2238         }
2239         if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2240                 dest_is_global = 1;
2241         }
2242         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2243         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2244         /* Ok the address may be ok */
2245 #ifdef INET6
2246         if (fam == AF_INET6) {
2247                 /* ok to use deprecated addresses? no lets not! */
2248                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2249                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2250                         return (NULL);
2251                 }
2252                 if (ifa->src_is_priv && !ifa->src_is_loop) {
2253                         if (dest_is_loop) {
2254                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2255                                 return (NULL);
2256                         }
2257                 }
2258                 if (ifa->src_is_glob) {
2259                         if (dest_is_loop) {
2260                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2261                                 return (NULL);
2262                         }
2263                 }
2264         }
2265 #endif
2266         /*
2267          * Now that we know what is what, implement or table this could in
2268          * theory be done slicker (it used to be), but this is
2269          * straightforward and easier to validate :-)
2270          */
2271         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2272             ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2273         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2274             dest_is_loop, dest_is_priv, dest_is_global);
2275
2276         if ((ifa->src_is_loop) && (dest_is_priv)) {
2277                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2278                 return (NULL);
2279         }
2280         if ((ifa->src_is_glob) && (dest_is_priv)) {
2281                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2282                 return (NULL);
2283         }
2284         if ((ifa->src_is_loop) && (dest_is_global)) {
2285                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2286                 return (NULL);
2287         }
2288         if ((ifa->src_is_priv) && (dest_is_global)) {
2289                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2290                 return (NULL);
2291         }
2292         SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2293         /* its a preferred address */
2294         return (ifa);
2295 }
2296
2297 static struct sctp_ifa *
2298 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2299     uint8_t dest_is_loop,
2300     uint8_t dest_is_priv,
2301     sa_family_t fam)
2302 {
2303         uint8_t dest_is_global = 0;
2304
2305         /**
2306          * Here we determine if its a acceptable address. A acceptable
2307          * address means it is the same scope or higher scope but we can
2308          * allow for NAT which means its ok to have a global dest and a
2309          * private src.
2310          *
2311          * L = loopback, P = private, G = global
2312          * -----------------------------------------
2313          *  src    |  dest | result
2314          * -----------------------------------------
2315          *   L     |   L   |    yes
2316          *  -----------------------------------------
2317          *   P     |   L   |    yes-v4 no-v6
2318          *  -----------------------------------------
2319          *   G     |   L   |    yes
2320          * -----------------------------------------
2321          *   L     |   P   |    no
2322          * -----------------------------------------
2323          *   P     |   P   |    yes
2324          * -----------------------------------------
2325          *   G     |   P   |    yes - May not work
2326          * -----------------------------------------
2327          *   L     |   G   |    no
2328          * -----------------------------------------
2329          *   P     |   G   |    yes - May not work
2330          * -----------------------------------------
2331          *   G     |   G   |    yes
2332          * -----------------------------------------
2333          */
2334
2335         if (ifa->address.sa.sa_family != fam) {
2336                 /* forget non matching family */
2337                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2338                     ifa->address.sa.sa_family, fam);
2339                 return (NULL);
2340         }
2341         /* Ok the address may be ok */
2342         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2343         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2344             dest_is_loop, dest_is_priv);
2345         if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2346                 dest_is_global = 1;
2347         }
2348 #ifdef INET6
2349         if (fam == AF_INET6) {
2350                 /* ok to use deprecated addresses? */
2351                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2352                         return (NULL);
2353                 }
2354                 if (ifa->src_is_priv) {
2355                         /* Special case, linklocal to loop */
2356                         if (dest_is_loop)
2357                                 return (NULL);
2358                 }
2359         }
2360 #endif
2361         /*
2362          * Now that we know what is what, implement our table. This could in
2363          * theory be done slicker (it used to be), but this is
2364          * straightforward and easier to validate :-)
2365          */
2366         SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2367             ifa->src_is_loop,
2368             dest_is_priv);
2369         if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2370                 return (NULL);
2371         }
2372         SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2373             ifa->src_is_loop,
2374             dest_is_global);
2375         if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2376                 return (NULL);
2377         }
2378         SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2379         /* its an acceptable address */
2380         return (ifa);
2381 }
2382
2383 int
2384 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2385 {
2386         struct sctp_laddr *laddr;
2387
2388         if (stcb == NULL) {
2389                 /* There are no restrictions, no TCB :-) */
2390                 return (0);
2391         }
2392         LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2393                 if (laddr->ifa == NULL) {
2394                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2395                             __FUNCTION__);
2396                         continue;
2397                 }
2398                 if (laddr->ifa == ifa) {
2399                         /* Yes it is on the list */
2400                         return (1);
2401                 }
2402         }
2403         return (0);
2404 }
2405
2406
2407 int
2408 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2409 {
2410         struct sctp_laddr *laddr;
2411
2412         if (ifa == NULL)
2413                 return (0);
2414         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2415                 if (laddr->ifa == NULL) {
2416                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2417                             __FUNCTION__);
2418                         continue;
2419                 }
2420                 if ((laddr->ifa == ifa) && laddr->action == 0)
2421                         /* same pointer */
2422                         return (1);
2423         }
2424         return (0);
2425 }
2426
2427
2428
2429 static struct sctp_ifa *
2430 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2431     sctp_route_t * ro,
2432     uint32_t vrf_id,
2433     int non_asoc_addr_ok,
2434     uint8_t dest_is_priv,
2435     uint8_t dest_is_loop,
2436     sa_family_t fam)
2437 {
2438         struct sctp_laddr *laddr, *starting_point;
2439         void *ifn;
2440         int resettotop = 0;
2441         struct sctp_ifn *sctp_ifn;
2442         struct sctp_ifa *sctp_ifa, *sifa;
2443         struct sctp_vrf *vrf;
2444         uint32_t ifn_index;
2445
2446         vrf = sctp_find_vrf(vrf_id);
2447         if (vrf == NULL)
2448                 return (NULL);
2449
2450         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2451         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2452         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2453         /*
2454          * first question, is the ifn we will emit on in our list, if so, we
2455          * want such an address. Note that we first looked for a preferred
2456          * address.
2457          */
2458         if (sctp_ifn) {
2459                 /* is a preferred one on the interface we route out? */
2460                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2461                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2462                             (non_asoc_addr_ok == 0))
2463                                 continue;
2464                         sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2465                             dest_is_loop,
2466                             dest_is_priv, fam);
2467                         if (sifa == NULL)
2468                                 continue;
2469                         if (sctp_is_addr_in_ep(inp, sifa)) {
2470                                 atomic_add_int(&sifa->refcount, 1);
2471                                 return (sifa);
2472                         }
2473                 }
2474         }
2475         /*
2476          * ok, now we now need to find one on the list of the addresses. We
2477          * can't get one on the emitting interface so let's find first a
2478          * preferred one. If not that an acceptable one otherwise... we
2479          * return NULL.
2480          */
2481         starting_point = inp->next_addr_touse;
2482 once_again:
2483         if (inp->next_addr_touse == NULL) {
2484                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2485                 resettotop = 1;
2486         }
2487         for (laddr = inp->next_addr_touse; laddr;
2488             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2489                 if (laddr->ifa == NULL) {
2490                         /* address has been removed */
2491                         continue;
2492                 }
2493                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2494                         /* address is being deleted */
2495                         continue;
2496                 }
2497                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2498                     dest_is_priv, fam);
2499                 if (sifa == NULL)
2500                         continue;
2501                 atomic_add_int(&sifa->refcount, 1);
2502                 return (sifa);
2503         }
2504         if (resettotop == 0) {
2505                 inp->next_addr_touse = NULL;
2506                 goto once_again;
2507         }
2508         inp->next_addr_touse = starting_point;
2509         resettotop = 0;
2510 once_again_too:
2511         if (inp->next_addr_touse == NULL) {
2512                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2513                 resettotop = 1;
2514         }
2515         /* ok, what about an acceptable address in the inp */
2516         for (laddr = inp->next_addr_touse; laddr;
2517             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2518                 if (laddr->ifa == NULL) {
2519                         /* address has been removed */
2520                         continue;
2521                 }
2522                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2523                         /* address is being deleted */
2524                         continue;
2525                 }
2526                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2527                     dest_is_priv, fam);
2528                 if (sifa == NULL)
2529                         continue;
2530                 atomic_add_int(&sifa->refcount, 1);
2531                 return (sifa);
2532         }
2533         if (resettotop == 0) {
2534                 inp->next_addr_touse = NULL;
2535                 goto once_again_too;
2536         }
2537         /*
2538          * no address bound can be a source for the destination we are in
2539          * trouble
2540          */
2541         return (NULL);
2542 }
2543
2544
2545
2546 static struct sctp_ifa *
2547 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2548     struct sctp_tcb *stcb,
2549     struct sctp_nets *net,
2550     sctp_route_t * ro,
2551     uint32_t vrf_id,
2552     uint8_t dest_is_priv,
2553     uint8_t dest_is_loop,
2554     int non_asoc_addr_ok,
2555     sa_family_t fam)
2556 {
2557         struct sctp_laddr *laddr, *starting_point;
2558         void *ifn;
2559         struct sctp_ifn *sctp_ifn;
2560         struct sctp_ifa *sctp_ifa, *sifa;
2561         uint8_t start_at_beginning = 0;
2562         struct sctp_vrf *vrf;
2563         uint32_t ifn_index;
2564
2565         /*
2566          * first question, is the ifn we will emit on in our list, if so, we
2567          * want that one.
2568          */
2569         vrf = sctp_find_vrf(vrf_id);
2570         if (vrf == NULL)
2571                 return (NULL);
2572
2573         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2574         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2575         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2576
2577         /*
2578          * first question, is the ifn we will emit on in our list?  If so,
2579          * we want that one. First we look for a preferred. Second, we go
2580          * for an acceptable.
2581          */
2582         if (sctp_ifn) {
2583                 /* first try for a preferred address on the ep */
2584                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2585                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2586                                 continue;
2587                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2588                                 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2589                                 if (sifa == NULL)
2590                                         continue;
2591                                 if (((non_asoc_addr_ok == 0) &&
2592                                     (sctp_is_addr_restricted(stcb, sifa))) ||
2593                                     (non_asoc_addr_ok &&
2594                                     (sctp_is_addr_restricted(stcb, sifa)) &&
2595                                     (!sctp_is_addr_pending(stcb, sifa)))) {
2596                                         /* on the no-no list */
2597                                         continue;
2598                                 }
2599                                 atomic_add_int(&sifa->refcount, 1);
2600                                 return (sifa);
2601                         }
2602                 }
2603                 /* next try for an acceptable address on the ep */
2604                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2605                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2606                                 continue;
2607                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2608                                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2609                                 if (sifa == NULL)
2610                                         continue;
2611                                 if (((non_asoc_addr_ok == 0) &&
2612                                     (sctp_is_addr_restricted(stcb, sifa))) ||
2613                                     (non_asoc_addr_ok &&
2614                                     (sctp_is_addr_restricted(stcb, sifa)) &&
2615                                     (!sctp_is_addr_pending(stcb, sifa)))) {
2616                                         /* on the no-no list */
2617                                         continue;
2618                                 }
2619                                 atomic_add_int(&sifa->refcount, 1);
2620                                 return (sifa);
2621                         }
2622                 }
2623
2624         }
2625         /*
2626          * if we can't find one like that then we must look at all addresses
2627          * bound to pick one at first preferable then secondly acceptable.
2628          */
2629         starting_point = stcb->asoc.last_used_address;
2630 sctp_from_the_top:
2631         if (stcb->asoc.last_used_address == NULL) {
2632                 start_at_beginning = 1;
2633                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2634         }
2635         /* search beginning with the last used address */
2636         for (laddr = stcb->asoc.last_used_address; laddr;
2637             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2638                 if (laddr->ifa == NULL) {
2639                         /* address has been removed */
2640                         continue;
2641                 }
2642                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2643                         /* address is being deleted */
2644                         continue;
2645                 }
2646                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2647                 if (sifa == NULL)
2648                         continue;
2649                 if (((non_asoc_addr_ok == 0) &&
2650                     (sctp_is_addr_restricted(stcb, sifa))) ||
2651                     (non_asoc_addr_ok &&
2652                     (sctp_is_addr_restricted(stcb, sifa)) &&
2653                     (!sctp_is_addr_pending(stcb, sifa)))) {
2654                         /* on the no-no list */
2655                         continue;
2656                 }
2657                 stcb->asoc.last_used_address = laddr;
2658                 atomic_add_int(&sifa->refcount, 1);
2659                 return (sifa);
2660         }
2661         if (start_at_beginning == 0) {
2662                 stcb->asoc.last_used_address = NULL;
2663                 goto sctp_from_the_top;
2664         }
2665         /* now try for any higher scope than the destination */
2666         stcb->asoc.last_used_address = starting_point;
2667         start_at_beginning = 0;
2668 sctp_from_the_top2:
2669         if (stcb->asoc.last_used_address == NULL) {
2670                 start_at_beginning = 1;
2671                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2672         }
2673         /* search beginning with the last used address */
2674         for (laddr = stcb->asoc.last_used_address; laddr;
2675             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2676                 if (laddr->ifa == NULL) {
2677                         /* address has been removed */
2678                         continue;
2679                 }
2680                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2681                         /* address is being deleted */
2682                         continue;
2683                 }
2684                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2685                     dest_is_priv, fam);
2686                 if (sifa == NULL)
2687                         continue;
2688                 if (((non_asoc_addr_ok == 0) &&
2689                     (sctp_is_addr_restricted(stcb, sifa))) ||
2690                     (non_asoc_addr_ok &&
2691                     (sctp_is_addr_restricted(stcb, sifa)) &&
2692                     (!sctp_is_addr_pending(stcb, sifa)))) {
2693                         /* on the no-no list */
2694                         continue;
2695                 }
2696                 stcb->asoc.last_used_address = laddr;
2697                 atomic_add_int(&sifa->refcount, 1);
2698                 return (sifa);
2699         }
2700         if (start_at_beginning == 0) {
2701                 stcb->asoc.last_used_address = NULL;
2702                 goto sctp_from_the_top2;
2703         }
2704         return (NULL);
2705 }
2706
2707 static struct sctp_ifa *
2708 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2709     struct sctp_tcb *stcb,
2710     int non_asoc_addr_ok,
2711     uint8_t dest_is_loop,
2712     uint8_t dest_is_priv,
2713     int addr_wanted,
2714     sa_family_t fam,
2715     sctp_route_t * ro
2716 )
2717 {
2718         struct sctp_ifa *ifa, *sifa;
2719         int num_eligible_addr = 0;
2720
2721 #ifdef INET6
2722         struct sockaddr_in6 sin6, lsa6;
2723
2724         if (fam == AF_INET6) {
2725                 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2726                 (void)sa6_recoverscope(&sin6);
2727         }
2728 #endif                          /* INET6 */
2729         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2730                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2731                     (non_asoc_addr_ok == 0))
2732                         continue;
2733                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2734                     dest_is_priv, fam);
2735                 if (sifa == NULL)
2736                         continue;
2737 #ifdef INET6
2738                 if (fam == AF_INET6 &&
2739                     dest_is_loop &&
2740                     sifa->src_is_loop && sifa->src_is_priv) {
2741                         /*
2742                          * don't allow fe80::1 to be a src on loop ::1, we
2743                          * don't list it to the peer so we will get an
2744                          * abort.
2745                          */
2746                         continue;
2747                 }
2748                 if (fam == AF_INET6 &&
2749                     IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2750                     IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2751                         /*
2752                          * link-local <-> link-local must belong to the same
2753                          * scope.
2754                          */
2755                         memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2756                         (void)sa6_recoverscope(&lsa6);
2757                         if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2758                                 continue;
2759                         }
2760                 }
2761 #endif                          /* INET6 */
2762
2763                 /*
2764                  * Check if the IPv6 address matches to next-hop. In the
2765                  * mobile case, old IPv6 address may be not deleted from the
2766                  * interface. Then, the interface has previous and new
2767                  * addresses.  We should use one corresponding to the
2768                  * next-hop.  (by micchie)
2769                  */
2770 #ifdef INET6
2771                 if (stcb && fam == AF_INET6 &&
2772                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2773                         if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2774                             == 0) {
2775                                 continue;
2776                         }
2777                 }
2778 #endif
2779 #ifdef INET
2780                 /* Avoid topologically incorrect IPv4 address */
2781                 if (stcb && fam == AF_INET &&
2782                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2783                         if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2784                                 continue;
2785                         }
2786                 }
2787 #endif
2788                 if (stcb) {
2789                         if (sctp_is_address_in_scope(ifa,
2790                             stcb->asoc.ipv4_addr_legal,
2791                             stcb->asoc.ipv6_addr_legal,
2792                             stcb->asoc.loopback_scope,
2793                             stcb->asoc.ipv4_local_scope,
2794                             stcb->asoc.local_scope,
2795                             stcb->asoc.site_scope, 0) == 0) {
2796                                 continue;
2797                         }
2798                         if (((non_asoc_addr_ok == 0) &&
2799                             (sctp_is_addr_restricted(stcb, sifa))) ||
2800                             (non_asoc_addr_ok &&
2801                             (sctp_is_addr_restricted(stcb, sifa)) &&
2802                             (!sctp_is_addr_pending(stcb, sifa)))) {
2803                                 /*
2804                                  * It is restricted for some reason..
2805                                  * probably not yet added.
2806                                  */
2807                                 continue;
2808                         }
2809                 }
2810                 if (num_eligible_addr >= addr_wanted) {
2811                         return (sifa);
2812                 }
2813                 num_eligible_addr++;
2814         }
2815         return (NULL);
2816 }
2817
2818
2819 static int
2820 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2821     struct sctp_tcb *stcb,
2822     int non_asoc_addr_ok,
2823     uint8_t dest_is_loop,
2824     uint8_t dest_is_priv,
2825     sa_family_t fam)
2826 {
2827         struct sctp_ifa *ifa, *sifa;
2828         int num_eligible_addr = 0;
2829
2830         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2831                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2832                     (non_asoc_addr_ok == 0)) {
2833                         continue;
2834                 }
2835                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2836                     dest_is_priv, fam);
2837                 if (sifa == NULL) {
2838                         continue;
2839                 }
2840                 if (stcb) {
2841                         if (sctp_is_address_in_scope(ifa,
2842                             stcb->asoc.ipv4_addr_legal,
2843                             stcb->asoc.ipv6_addr_legal,
2844                             stcb->asoc.loopback_scope,
2845                             stcb->asoc.ipv4_local_scope,
2846                             stcb->asoc.local_scope,
2847                             stcb->asoc.site_scope, 0) == 0) {
2848                                 continue;
2849                         }
2850                         if (((non_asoc_addr_ok == 0) &&
2851                             (sctp_is_addr_restricted(stcb, sifa))) ||
2852                             (non_asoc_addr_ok &&
2853                             (sctp_is_addr_restricted(stcb, sifa)) &&
2854                             (!sctp_is_addr_pending(stcb, sifa)))) {
2855                                 /*
2856                                  * It is restricted for some reason..
2857                                  * probably not yet added.
2858                                  */
2859                                 continue;
2860                         }
2861                 }
2862                 num_eligible_addr++;
2863         }
2864         return (num_eligible_addr);
2865 }
2866
2867 static struct sctp_ifa *
2868 sctp_choose_boundall(struct sctp_inpcb *inp,
2869     struct sctp_tcb *stcb,
2870     struct sctp_nets *net,
2871     sctp_route_t * ro,
2872     uint32_t vrf_id,
2873     uint8_t dest_is_priv,
2874     uint8_t dest_is_loop,
2875     int non_asoc_addr_ok,
2876     sa_family_t fam)
2877 {
2878         int cur_addr_num = 0, num_preferred = 0;
2879         void *ifn;
2880         struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2881         struct sctp_ifa *sctp_ifa, *sifa;
2882         uint32_t ifn_index;
2883         struct sctp_vrf *vrf;
2884
2885 #ifdef INET
2886         int retried = 0;
2887
2888 #endif
2889         /*-
2890          * For boundall we can use any address in the association.
2891          * If non_asoc_addr_ok is set we can use any address (at least in
2892          * theory). So we look for preferred addresses first. If we find one,
2893          * we use it. Otherwise we next try to get an address on the
2894          * interface, which we should be able to do (unless non_asoc_addr_ok
2895          * is false and we are routed out that way). In these cases where we
2896          * can't use the address of the interface we go through all the
2897          * ifn's looking for an address we can use and fill that in. Punting
2898          * means we send back address 0, which will probably cause problems
2899          * actually since then IP will fill in the address of the route ifn,
2900          * which means we probably already rejected it.. i.e. here comes an
2901          * abort :-<.
2902          */
2903         vrf = sctp_find_vrf(vrf_id);
2904         if (vrf == NULL)
2905                 return (NULL);
2906
2907         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2908         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2909         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
2910         emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2911         if (sctp_ifn == NULL) {
2912                 /* ?? We don't have this guy ?? */
2913                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2914                 goto bound_all_plan_b;
2915         }
2916         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2917             ifn_index, sctp_ifn->ifn_name);
2918
2919         if (net) {
2920                 cur_addr_num = net->indx_of_eligible_next_to_use;
2921         }
2922         num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2923             stcb,
2924             non_asoc_addr_ok,
2925             dest_is_loop,
2926             dest_is_priv, fam);
2927         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2928             num_preferred, sctp_ifn->ifn_name);
2929         if (num_preferred == 0) {
2930                 /*
2931                  * no eligible addresses, we must use some other interface
2932                  * address if we can find one.
2933                  */
2934                 goto bound_all_plan_b;
2935         }
2936         /*
2937          * Ok we have num_eligible_addr set with how many we can use, this
2938          * may vary from call to call due to addresses being deprecated
2939          * etc..
2940          */
2941         if (cur_addr_num >= num_preferred) {
2942                 cur_addr_num = 0;
2943         }
2944         /*
2945          * select the nth address from the list (where cur_addr_num is the
2946          * nth) and 0 is the first one, 1 is the second one etc...
2947          */
2948         SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2949
2950         sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2951             dest_is_priv, cur_addr_num, fam, ro);
2952
2953         /* if sctp_ifa is NULL something changed??, fall to plan b. */
2954         if (sctp_ifa) {
2955                 atomic_add_int(&sctp_ifa->refcount, 1);
2956                 if (net) {
2957                         /* save off where the next one we will want */
2958                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2959                 }
2960                 return (sctp_ifa);
2961         }
2962         /*
2963          * plan_b: Look at all interfaces and find a preferred address. If
2964          * no preferred fall through to plan_c.
2965          */
2966 bound_all_plan_b:
2967         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2968         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2969                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2970                     sctp_ifn->ifn_name);
2971                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2972                         /* wrong base scope */
2973                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2974                         continue;
2975                 }
2976                 if ((sctp_ifn == looked_at) && looked_at) {
2977                         /* already looked at this guy */
2978                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2979                         continue;
2980                 }
2981                 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2982                     dest_is_loop, dest_is_priv, fam);
2983                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2984                     "Found ifn:%p %d preferred source addresses\n",
2985                     ifn, num_preferred);
2986                 if (num_preferred == 0) {
2987                         /* None on this interface. */
2988                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2989                         continue;
2990                 }
2991                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2992                     "num preferred:%d on interface:%p cur_addr_num:%d\n",
2993                     num_preferred, sctp_ifn, cur_addr_num);
2994
2995                 /*
2996                  * Ok we have num_eligible_addr set with how many we can
2997                  * use, this may vary from call to call due to addresses
2998                  * being deprecated etc..
2999                  */
3000                 if (cur_addr_num >= num_preferred) {
3001                         cur_addr_num = 0;
3002                 }
3003                 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
3004                     dest_is_priv, cur_addr_num, fam, ro);
3005                 if (sifa == NULL)
3006                         continue;
3007                 if (net) {
3008                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3009                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3010                             cur_addr_num);
3011                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3012                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3013                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3014                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3015                 }
3016                 atomic_add_int(&sifa->refcount, 1);
3017                 return (sifa);
3018         }
3019 #ifdef INET
3020 again_with_private_addresses_allowed:
3021 #endif
3022         /* plan_c: do we have an acceptable address on the emit interface */
3023         sifa = NULL;
3024         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
3025         if (emit_ifn == NULL) {
3026                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
3027                 goto plan_d;
3028         }
3029         LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3030                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", sctp_ifa);
3031                 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3032                     (non_asoc_addr_ok == 0)) {
3033                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
3034                         continue;
3035                 }
3036                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3037                     dest_is_priv, fam);
3038                 if (sifa == NULL) {
3039                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3040                         continue;
3041                 }
3042                 if (stcb) {
3043                         if (sctp_is_address_in_scope(sifa,
3044                             stcb->asoc.ipv4_addr_legal,
3045                             stcb->asoc.ipv6_addr_legal,
3046                             stcb->asoc.loopback_scope,
3047                             stcb->asoc.ipv4_local_scope,
3048                             stcb->asoc.local_scope,
3049                             stcb->asoc.site_scope, 0) == 0) {
3050                                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3051                                 sifa = NULL;
3052                                 continue;
3053                         }
3054                         if (((non_asoc_addr_ok == 0) &&
3055                             (sctp_is_addr_restricted(stcb, sifa))) ||
3056                             (non_asoc_addr_ok &&
3057                             (sctp_is_addr_restricted(stcb, sifa)) &&
3058                             (!sctp_is_addr_pending(stcb, sifa)))) {
3059                                 /*
3060                                  * It is restricted for some reason..
3061                                  * probably not yet added.
3062                                  */
3063                                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its resticted\n");
3064                                 sifa = NULL;
3065                                 continue;
3066                         }
3067                 } else {
3068                         printf("Stcb is null - no print\n");
3069                 }
3070                 atomic_add_int(&sifa->refcount, 1);
3071                 goto out;
3072         }
3073 plan_d:
3074         /*
3075          * plan_d: We are in trouble. No preferred address on the emit
3076          * interface. And not even a preferred address on all interfaces. Go
3077          * out and see if we can find an acceptable address somewhere
3078          * amongst all interfaces.
3079          */
3080         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", looked_at);
3081         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3082                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3083                         /* wrong base scope */
3084                         continue;
3085                 }
3086                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3087                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3088                             (non_asoc_addr_ok == 0))
3089                                 continue;
3090                         sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3091                             dest_is_loop,
3092                             dest_is_priv, fam);
3093                         if (sifa == NULL)
3094                                 continue;
3095                         if (stcb) {
3096                                 if (sctp_is_address_in_scope(sifa,
3097                                     stcb->asoc.ipv4_addr_legal,
3098                                     stcb->asoc.ipv6_addr_legal,
3099                                     stcb->asoc.loopback_scope,
3100                                     stcb->asoc.ipv4_local_scope,
3101                                     stcb->asoc.local_scope,
3102                                     stcb->asoc.site_scope, 0) == 0) {
3103                                         sifa = NULL;
3104                                         continue;
3105                                 }
3106                                 if (((non_asoc_addr_ok == 0) &&
3107                                     (sctp_is_addr_restricted(stcb, sifa))) ||
3108                                     (non_asoc_addr_ok &&
3109                                     (sctp_is_addr_restricted(stcb, sifa)) &&
3110                                     (!sctp_is_addr_pending(stcb, sifa)))) {
3111                                         /*
3112                                          * It is restricted for some
3113                                          * reason.. probably not yet added.
3114                                          */
3115                                         sifa = NULL;
3116                                         continue;
3117                                 }
3118                         }
3119                         goto out;
3120                 }
3121         }
3122 #ifdef INET
3123         if ((retried == 0) && (stcb->asoc.ipv4_local_scope == 0)) {
3124                 stcb->asoc.ipv4_local_scope = 1;
3125                 retried = 1;
3126                 goto again_with_private_addresses_allowed;
3127         } else if (retried == 1) {
3128                 stcb->asoc.ipv4_local_scope = 0;
3129         }
3130 #endif
3131 out:
3132 #ifdef INET
3133         if (sifa) {
3134                 if (retried == 1) {
3135                         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3136                                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3137                                         /* wrong base scope */
3138                                         continue;
3139                                 }
3140                                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3141                                         struct sctp_ifa *tmp_sifa;
3142
3143                                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3144                                             (non_asoc_addr_ok == 0))
3145                                                 continue;
3146                                         tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3147                                             dest_is_loop,
3148                                             dest_is_priv, fam);
3149                                         if (tmp_sifa == NULL) {
3150                                                 continue;
3151                                         }
3152                                         if (tmp_sifa == sifa) {
3153                                                 continue;
3154                                         }
3155                                         if (stcb) {
3156                                                 if (sctp_is_address_in_scope(tmp_sifa,
3157                                                     stcb->asoc.ipv4_addr_legal,
3158                                                     stcb->asoc.ipv6_addr_legal,
3159                                                     stcb->asoc.loopback_scope,
3160                                                     stcb->asoc.ipv4_local_scope,
3161                                                     stcb->asoc.local_scope,
3162                                                     stcb->asoc.site_scope, 0) == 0) {
3163                                                         continue;
3164                                                 }
3165                                                 if (((non_asoc_addr_ok == 0) &&
3166                                                     (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3167                                                     (non_asoc_addr_ok &&
3168                                                     (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3169                                                     (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3170                                                         /*
3171                                                          * It is restricted
3172                                                          * for some reason..
3173                                                          * probably not yet
3174                                                          * added.
3175                                                          */
3176                                                         continue;
3177                                                 }
3178                                         }
3179                                         if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3180                                             (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3181                                                 sctp_add_local_addr_restricted(stcb, tmp_sifa);
3182                                         }
3183                                 }
3184                         }
3185                 }
3186                 atomic_add_int(&sifa->refcount, 1);
3187         }
3188 #endif
3189         return (sifa);
3190 }
3191
3192
3193
3194 /* tcb may be NULL */
3195 struct sctp_ifa *
3196 sctp_source_address_selection(struct sctp_inpcb *inp,
3197     struct sctp_tcb *stcb,
3198     sctp_route_t * ro,
3199     struct sctp_nets *net,
3200     int non_asoc_addr_ok, uint32_t vrf_id)
3201 {
3202         struct sctp_ifa *answer;
3203         uint8_t dest_is_priv, dest_is_loop;
3204         sa_family_t fam;
3205
3206 #ifdef INET
3207         struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3208
3209 #endif
3210 #ifdef INET6
3211         struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3212
3213 #endif
3214
3215         /**
3216          * Rules: - Find the route if needed, cache if I can. - Look at
3217          * interface address in route, Is it in the bound list. If so we
3218          * have the best source. - If not we must rotate amongst the
3219          * addresses.
3220          *
3221          * Cavets and issues
3222          *
3223          * Do we need to pay attention to scope. We can have a private address
3224          * or a global address we are sourcing or sending to. So if we draw
3225          * it out
3226          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3227          * For V4
3228          * ------------------------------------------
3229          *      source     *      dest  *  result
3230          * -----------------------------------------
3231          * <a>  Private    *    Global  *  NAT
3232          * -----------------------------------------
3233          * <b>  Private    *    Private *  No problem
3234          * -----------------------------------------
3235          * <c>  Global     *    Private *  Huh, How will this work?
3236          * -----------------------------------------
3237          * <d>  Global     *    Global  *  No Problem
3238          *------------------------------------------
3239          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3240          * For V6
3241          *------------------------------------------
3242          *      source     *      dest  *  result
3243          * -----------------------------------------
3244          * <a>  Linklocal  *    Global  *
3245          * -----------------------------------------
3246          * <b>  Linklocal  * Linklocal  *  No problem
3247          * -----------------------------------------
3248          * <c>  Global     * Linklocal  *  Huh, How will this work?
3249          * -----------------------------------------
3250          * <d>  Global     *    Global  *  No Problem
3251          *------------------------------------------
3252          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3253          *
3254          * And then we add to that what happens if there are multiple addresses
3255          * assigned to an interface. Remember the ifa on a ifn is a linked
3256          * list of addresses. So one interface can have more than one IP
3257          * address. What happens if we have both a private and a global
3258          * address? Do we then use context of destination to sort out which
3259          * one is best? And what about NAT's sending P->G may get you a NAT
3260          * translation, or should you select the G thats on the interface in
3261          * preference.
3262          *
3263          * Decisions:
3264          *
3265          * - count the number of addresses on the interface.
3266          * - if it is one, no problem except case <c>.
3267          *   For <a> we will assume a NAT out there.
3268          * - if there are more than one, then we need to worry about scope P
3269          *   or G. We should prefer G -> G and P -> P if possible.
3270          *   Then as a secondary fall back to mixed types G->P being a last
3271          *   ditch one.
3272          * - The above all works for bound all, but bound specific we need to
3273          *   use the same concept but instead only consider the bound
3274          *   addresses. If the bound set is NOT assigned to the interface then
3275          *   we must use rotation amongst the bound addresses..
3276          */
3277         if (ro->ro_rt == NULL) {
3278                 /*
3279                  * Need a route to cache.
3280                  */
3281                 SCTP_RTALLOC(ro, vrf_id);
3282         }
3283         if (ro->ro_rt == NULL) {
3284                 return (NULL);
3285         }
3286         fam = ro->ro_dst.sa_family;
3287         dest_is_priv = dest_is_loop = 0;
3288         /* Setup our scopes for the destination */
3289         switch (fam) {
3290 #ifdef INET
3291         case AF_INET:
3292                 /* Scope based on outbound address */
3293                 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3294                         dest_is_loop = 1;
3295                         if (net != NULL) {
3296                                 /* mark it as local */
3297                                 net->addr_is_local = 1;
3298                         }
3299                 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3300                         dest_is_priv = 1;
3301                 }
3302                 break;
3303 #endif
3304 #ifdef INET6
3305         case AF_INET6:
3306                 /* Scope based on outbound address */
3307                 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3308                     SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3309                         /*
3310                          * If the address is a loopback address, which
3311                          * consists of "::1" OR "fe80::1%lo0", we are
3312                          * loopback scope. But we don't use dest_is_priv
3313                          * (link local addresses).
3314                          */
3315                         dest_is_loop = 1;
3316                         if (net != NULL) {
3317                                 /* mark it as local */
3318                                 net->addr_is_local = 1;
3319                         }
3320                 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3321                         dest_is_priv = 1;
3322                 }
3323                 break;
3324 #endif
3325         }
3326         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3327         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3328         SCTP_IPI_ADDR_RLOCK();
3329         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3330                 /*
3331                  * Bound all case
3332                  */
3333                 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3334                     dest_is_priv, dest_is_loop,
3335                     non_asoc_addr_ok, fam);
3336                 SCTP_IPI_ADDR_RUNLOCK();
3337                 return (answer);
3338         }
3339         /*
3340          * Subset bound case
3341          */
3342         if (stcb) {
3343                 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3344                     vrf_id, dest_is_priv,
3345                     dest_is_loop,
3346                     non_asoc_addr_ok, fam);
3347         } else {
3348                 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3349                     non_asoc_addr_ok,
3350                     dest_is_priv,
3351                     dest_is_loop, fam);
3352         }
3353         SCTP_IPI_ADDR_RUNLOCK();
3354         return (answer);
3355 }
3356
3357 static int
3358 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
3359 {
3360         struct cmsghdr cmh;
3361         int tlen, at, found;
3362         struct sctp_sndinfo sndinfo;
3363         struct sctp_prinfo prinfo;
3364         struct sctp_authinfo authinfo;
3365
3366         tlen = SCTP_BUF_LEN(control);
3367         at = 0;
3368         found = 0;
3369         /*
3370          * Independent of how many mbufs, find the c_type inside the control
3371          * structure and copy out the data.
3372          */
3373         while (at < tlen) {
3374                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3375                         /* There is not enough room for one more. */
3376                         return (found);
3377                 }
3378                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3379                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3380                         /* We dont't have a complete CMSG header. */
3381                         return (found);
3382                 }
3383                 if (((int)cmh.cmsg_len + at) > tlen) {
3384                         /* We don't have the complete CMSG. */
3385                         return (found);
3386                 }
3387                 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3388                     ((c_type == cmh.cmsg_type) ||
3389                     ((c_type == SCTP_SNDRCV) &&
3390                     ((cmh.cmsg_type == SCTP_SNDINFO) ||
3391                     (cmh.cmsg_type == SCTP_PRINFO) ||
3392                     (cmh.cmsg_type == SCTP_AUTHINFO))))) {
3393                         if (c_type == cmh.cmsg_type) {
3394                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3395                                         return (found);
3396                                 }
3397                                 /* It is exactly what we want. Copy it out. */
3398                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), cpsize, (caddr_t)data);
3399                                 return (1);
3400                         } else {
3401                                 struct sctp_sndrcvinfo *sndrcvinfo;
3402
3403                                 sndrcvinfo = (struct sctp_sndrcvinfo *)data;
3404                                 if (found == 0) {
3405                                         if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
3406                                                 return (found);
3407                                         }
3408                                         memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
3409                                 }
3410                                 switch (cmh.cmsg_type) {
3411                                 case SCTP_SNDINFO:
3412                                         if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_sndinfo)) {
3413                                                 return (found);
3414                                         }
3415                                         m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
3416                                         sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
3417                                         sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
3418                                         sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
3419                                         sndrcvinfo->sinfo_context = sndinfo.snd_context;
3420                                         sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
3421                                         break;
3422                                 case SCTP_PRINFO:
3423                                         if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_prinfo)) {
3424                                                 return (found);
3425                                         }
3426                                         m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
3427                                         sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
3428                                         sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
3429                                         break;
3430                                 case SCTP_AUTHINFO:
3431                                         if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_authinfo)) {
3432                                                 return (found);
3433                                         }
3434                                         m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
3435                                         sndrcvinfo->sinfo_keynumber_valid = 1;
3436                                         sndrcvinfo->sinfo_keynumber = authinfo.auth_keyid;
3437                                         break;
3438                                 default:
3439                                         return (found);
3440                                 }
3441                                 found = 1;
3442                         }
3443                 }
3444                 at += CMSG_ALIGN(cmh.cmsg_len);
3445         }
3446         return (found);
3447 }
3448
3449 static int
3450 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
3451 {
3452         struct cmsghdr cmh;
3453         int tlen, at;
3454         struct sctp_initmsg initmsg;
3455
3456 #ifdef INET
3457         struct sockaddr_in sin;
3458
3459 #endif
3460 #ifdef INET6
3461         struct sockaddr_in6 sin6;
3462
3463 #endif
3464
3465         tlen = SCTP_BUF_LEN(control);
3466         at = 0;
3467         while (at < tlen) {
3468                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3469                         /* There is not enough room for one more. */
3470                         *error = EINVAL;
3471                         return (1);
3472                 }
3473                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3474                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3475                         /* We dont't have a complete CMSG header. */
3476                         *error = EINVAL;
3477                         return (1);
3478                 }
3479                 if (((int)cmh.cmsg_len + at) > tlen) {
3480                         /* We don't have the complete CMSG. */
3481                         *error = EINVAL;
3482                         return (1);
3483                 }
3484                 if (cmh.cmsg_level == IPPROTO_SCTP) {
3485                         switch (cmh.cmsg_type) {
3486                         case SCTP_INIT:
3487                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_initmsg)) {
3488                                         *error = EINVAL;
3489                                         return (1);
3490                                 }
3491                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
3492                                 if (initmsg.sinit_max_attempts)
3493                                         stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
3494                                 if (initmsg.sinit_num_ostreams)
3495                                         stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
3496                                 if (initmsg.sinit_max_instreams)
3497                                         stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
3498                                 if (initmsg.sinit_max_init_timeo)
3499                                         stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
3500                                 if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
3501                                         struct sctp_stream_out *tmp_str;
3502                                         unsigned int i;
3503
3504                                         /* Default is NOT correct */
3505                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
3506                                             stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
3507                                         SCTP_TCB_UNLOCK(stcb);
3508                                         SCTP_MALLOC(tmp_str,
3509                                             struct sctp_stream_out *,
3510                                             (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
3511                                             SCTP_M_STRMO);
3512                                         SCTP_TCB_LOCK(stcb);
3513                                         if (tmp_str != NULL) {
3514                                                 SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
3515                                                 stcb->asoc.strmout = tmp_str;
3516                                                 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
3517                                         } else {
3518                                                 stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
3519                                         }
3520                                         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3521                                                 stcb->asoc.strmout[i].next_sequence_sent = 0;
3522                                                 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3523                                                 stcb->asoc.strmout[i].stream_no = i;
3524                                                 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3525                                                 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
3526                                         }
3527                                 }
3528                                 break;
3529 #ifdef INET
3530                         case SCTP_DSTADDRV4:
3531                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) {
3532                                         *error = EINVAL;
3533                                         return (1);
3534                                 }
3535                                 memset(&sin, 0, sizeof(struct sockaddr_in));
3536                                 sin.sin_family = AF_INET;
3537                                 sin.sin_len = sizeof(struct sockaddr_in);
3538                                 sin.sin_port = stcb->rport;
3539                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3540                                 if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3541                                     (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3542                                     IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3543                                         *error = EINVAL;
3544                                         return (1);
3545                                 }
3546                                 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL,
3547                                     SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3548                                         *error = ENOBUFS;
3549                                         return (1);
3550                                 }
3551                                 break;
3552 #endif
3553 #ifdef INET6
3554                         case SCTP_DSTADDRV6:
3555                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) {
3556                                         *error = EINVAL;
3557                                         return (1);
3558                                 }
3559                                 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3560                                 sin6.sin6_family = AF_INET6;
3561                                 sin6.sin6_len = sizeof(struct sockaddr_in6);
3562                                 sin6.sin6_port = stcb->rport;
3563                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3564                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
3565                                     IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
3566                                         *error = EINVAL;
3567                                         return (1);
3568                                 }
3569 #ifdef INET
3570                                 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3571                                         in6_sin6_2_sin(&sin, &sin6);
3572                                         if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3573                                             (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3574                                             IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3575                                                 *error = EINVAL;
3576                                                 return (1);
3577                                         }
3578                                         if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL,
3579                                             SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3580                                                 *error = ENOBUFS;
3581                                                 return (1);
3582                                         }
3583                                 } else
3584 #endif
3585                                         if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL,
3586                                     SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3587                                         *error = ENOBUFS;
3588                                         return (1);
3589                                 }
3590                                 break;
3591 #endif
3592                         default:
3593                                 break;
3594                         }
3595                 }
3596                 at += CMSG_ALIGN(cmh.cmsg_len);
3597         }
3598         return (0);
3599 }
3600
3601 static struct sctp_tcb *
3602 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
3603     in_port_t port,
3604     struct mbuf *control,
3605     struct sctp_nets **net_p,
3606     int *error)
3607 {
3608         struct cmsghdr cmh;
3609         int tlen, at;
3610         struct sctp_tcb *stcb;
3611         struct sockaddr *addr;
3612
3613 #ifdef INET
3614         struct sockaddr_in sin;
3615
3616 #endif
3617 #ifdef INET6
3618         struct sockaddr_in6 sin6;
3619
3620 #endif
3621
3622         tlen = SCTP_BUF_LEN(control);
3623         at = 0;
3624         while (at < tlen) {
3625                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3626                         /* There is not enough room for one more. */
3627                         *error = EINVAL;
3628                         return (NULL);
3629                 }
3630                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3631                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3632                         /* We dont't have a complete CMSG header. */
3633                         *error = EINVAL;
3634                         return (NULL);
3635                 }
3636                 if (((int)cmh.cmsg_len + at) > tlen) {
3637                         /* We don't have the complete CMSG. */
3638                         *error = EINVAL;
3639                         return (NULL);
3640                 }
3641                 if (cmh.cmsg_level == IPPROTO_SCTP) {
3642                         switch (cmh.cmsg_type) {
3643 #ifdef INET
3644                         case SCTP_DSTADDRV4:
3645                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) {
3646                                         *error = EINVAL;
3647                                         return (NULL);
3648                                 }
3649                                 memset(&sin, 0, sizeof(struct sockaddr_in));
3650                                 sin.sin_family = AF_INET;
3651                                 sin.sin_len = sizeof(struct sockaddr_in);
3652                                 sin.sin_port = port;
3653                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3654                                 addr = (struct sockaddr *)&sin;
3655                                 break;
3656 #endif
3657 #ifdef INET6
3658                         case SCTP_DSTADDRV6:
3659                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) {
3660                                         *error = EINVAL;
3661                                         return (NULL);
3662                                 }
3663                                 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3664                                 sin6.sin6_family = AF_INET6;
3665                                 sin6.sin6_len = sizeof(struct sockaddr_in6);
3666                                 sin6.sin6_port = port;
3667                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3668 #ifdef INET
3669                                 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3670                                         in6_sin6_2_sin(&sin, &sin6);
3671                                         addr = (struct sockaddr *)&sin;
3672                                 } else
3673 #endif
3674                                         addr = (struct sockaddr *)&sin6;
3675                                 break;
3676 #endif
3677                         default:
3678                                 addr = NULL;
3679                                 break;
3680                         }
3681                         if (addr) {
3682                                 stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
3683                                 if (stcb != NULL) {
3684                                         return (stcb);
3685                                 }
3686                         }
3687                 }
3688                 at += CMSG_ALIGN(cmh.cmsg_len);
3689         }
3690         return (NULL);
3691 }
3692
3693 static struct mbuf *
3694 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3695     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3696 {
3697         struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3698         struct sctp_state_cookie *stc;
3699         struct sctp_paramhdr *ph;
3700         uint8_t *foo;
3701         int sig_offset;
3702         uint16_t cookie_sz;
3703
3704         mret = NULL;
3705         mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3706             sizeof(struct sctp_paramhdr)), 0,
3707             M_DONTWAIT, 1, MT_DATA);
3708         if (mret == NULL) {
3709                 return (NULL);
3710         }
3711         copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3712         if (copy_init == NULL) {
3713                 sctp_m_freem(mret);
3714                 return (NULL);
3715         }
3716 #ifdef SCTP_MBUF_LOGGING
3717         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3718                 struct mbuf *mat;
3719
3720                 mat = copy_init;
3721                 while (mat) {
3722                         if (SCTP_BUF_IS_EXTENDED(mat)) {
3723                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3724                         }
3725                         mat = SCTP_BUF_NEXT(mat);
3726                 }
3727         }
3728 #endif
3729         copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3730             M_DONTWAIT);
3731         if (copy_initack == NULL) {
3732                 sctp_m_freem(mret);
3733                 sctp_m_freem(copy_init);
3734                 return (NULL);
3735         }
3736 #ifdef SCTP_MBUF_LOGGING
3737         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3738                 struct mbuf *mat;
3739
3740                 mat = copy_initack;
3741                 while (mat) {
3742                         if (SCTP_BUF_IS_EXTENDED(mat)) {
3743                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3744                         }
3745                         mat = SCTP_BUF_NEXT(mat);
3746                 }
3747         }
3748 #endif
3749         /* easy side we just drop it on the end */
3750         ph = mtod(mret, struct sctp_paramhdr *);
3751         SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3752             sizeof(struct sctp_paramhdr);
3753         stc = (struct sctp_state_cookie *)((caddr_t)ph +
3754             sizeof(struct sctp_paramhdr));
3755         ph->param_type = htons(SCTP_STATE_COOKIE);
3756         ph->param_length = 0;   /* fill in at the end */
3757         /* Fill in the stc cookie data */
3758         memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3759
3760         /* tack the INIT and then the INIT-ACK onto the chain */
3761         cookie_sz = 0;
3762         m_at = mret;
3763         for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3764                 cookie_sz += SCTP_BUF_LEN(m_at);
3765                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3766                         SCTP_BUF_NEXT(m_at) = copy_init;
3767                         break;
3768                 }
3769         }
3770
3771         for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3772                 cookie_sz += SCTP_BUF_LEN(m_at);
3773                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3774                         SCTP_BUF_NEXT(m_at) = copy_initack;
3775                         break;
3776                 }
3777         }
3778
3779         for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3780                 cookie_sz += SCTP_BUF_LEN(m_at);
3781                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3782                         break;
3783                 }
3784         }
3785         sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3786         if (sig == NULL) {
3787                 /* no space, so free the entire chain */
3788                 sctp_m_freem(mret);
3789                 return (NULL);
3790         }
3791         SCTP_BUF_LEN(sig) = 0;
3792         SCTP_BUF_NEXT(m_at) = sig;
3793         sig_offset = 0;
3794         foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3795         memset(foo, 0, SCTP_SIGNATURE_SIZE);
3796         *signature = foo;
3797         SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3798         cookie_sz += SCTP_SIGNATURE_SIZE;
3799         ph->param_length = htons(cookie_sz);
3800         return (mret);
3801 }
3802
3803
3804 static uint8_t
3805 sctp_get_ect(struct sctp_tcb *stcb,
3806     struct sctp_tmit_chunk *chk)
3807 {
3808         if ((stcb != NULL) && (stcb->asoc.ecn_allowed == 1)) {
3809                 return (SCTP_ECT0_BIT);
3810         } else {
3811                 return (0);
3812         }
3813 }
3814
3815 static void
3816 sctp_handle_no_route(struct sctp_tcb *stcb,
3817     struct sctp_nets *net,
3818     int so_locked)
3819 {
3820         SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
3821
3822         if (net) {
3823                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
3824                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
3825                 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3826                         if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3827                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3828                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3829                                     stcb,
3830                                     SCTP_FAILED_THRESHOLD,
3831                                     (void *)net,
3832                                     so_locked);
3833                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3834                                 net->dest_state &= ~SCTP_ADDR_PF;
3835                         }
3836                 }
3837                 if (stcb) {
3838                         if (net == stcb->asoc.primary_destination) {
3839                                 /* need a new primary */
3840                                 struct sctp_nets *alt;
3841
3842                                 alt = sctp_find_alternate_net(stcb, net, 0);
3843                                 if (alt != net) {
3844                                         if (stcb->asoc.alternate) {
3845                                                 sctp_free_remote_addr(stcb->asoc.alternate);
3846                                         }
3847                                         stcb->asoc.alternate = alt;
3848                                         atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
3849                                         if (net->ro._s_addr) {
3850                                                 sctp_free_ifa(net->ro._s_addr);
3851                                                 net->ro._s_addr = NULL;
3852                                         }
3853                                         net->src_addr_selected = 0;
3854                                 }
3855                         }
3856                 }
3857         }
3858 }
3859
3860 static int
3861 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3862     struct sctp_tcb *stcb,      /* may be NULL */
3863     struct sctp_nets *net,
3864     struct sockaddr *to,
3865     struct mbuf *m,
3866     uint32_t auth_offset,
3867     struct sctp_auth_chunk *auth,
3868     uint16_t auth_keyid,
3869     int nofragment_flag,
3870     int ecn_ok,
3871     struct sctp_tmit_chunk *chk,
3872     int out_of_asoc_ok,
3873     uint16_t src_port,
3874     uint16_t dest_port,
3875     uint32_t v_tag,
3876     uint16_t port,
3877     int so_locked,
3878 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3879     SCTP_UNUSED
3880 #endif
3881     union sctp_sockstore *over_addr,
3882     struct mbuf *init
3883 )
3884 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3885 {
3886         /**
3887          * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
3888          * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
3889          * - fill in the HMAC digest of any AUTH chunk in the packet.
3890          * - calculate and fill in the SCTP checksum.
3891          * - prepend an IP address header.
3892          * - if boundall use INADDR_ANY.
3893          * - if boundspecific do source address selection.
3894          * - set fragmentation option for ipV4.
3895          * - On return from IP output, check/adjust mtu size of output
3896          *   interface and smallest_mtu size as well.
3897          */
3898         /* Will need ifdefs around this */
3899         struct mbuf *o_pak;
3900         struct mbuf *newm;
3901         struct sctphdr *sctphdr;
3902         int packet_length;
3903         int ret;
3904         uint32_t vrf_id;
3905         sctp_route_t *ro = NULL;
3906         struct udphdr *udp = NULL;
3907         uint8_t tos_value;
3908
3909 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3910         struct socket *so = NULL;
3911
3912 #endif
3913
3914         if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3915                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3916                 sctp_m_freem(m);
3917                 return (EFAULT);
3918         }
3919         if (stcb) {
3920                 vrf_id = stcb->asoc.vrf_id;
3921         } else {
3922                 vrf_id = inp->def_vrf_id;
3923         }
3924
3925         /* fill in the HMAC digest for any AUTH chunk in the packet */
3926         if ((auth != NULL) && (stcb != NULL)) {
3927                 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3928         }
3929         if (net) {
3930                 tos_value = net->dscp;
3931         } else if (stcb) {
3932                 tos_value = stcb->asoc.default_dscp;
3933         } else {
3934                 tos_value = inp->sctp_ep.default_dscp;
3935         }
3936
3937         switch (to->sa_family) {
3938 #ifdef INET
3939         case AF_INET:
3940                 {
3941                         struct ip *ip = NULL;
3942                         sctp_route_t iproute;
3943                         int len;
3944
3945                         len = sizeof(struct ip) + sizeof(struct sctphdr);
3946                         if (port) {
3947                                 len += sizeof(struct udphdr);
3948                         }
3949                         newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3950                         if (newm == NULL) {
3951                                 sctp_m_freem(m);
3952                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3953                                 return (ENOMEM);
3954                         }
3955                         SCTP_ALIGN_TO_END(newm, len);
3956                         SCTP_BUF_LEN(newm) = len;
3957                         SCTP_BUF_NEXT(newm) = m;
3958                         m = newm;
3959                         if (net != NULL) {
3960 #ifdef INVARIANTS
3961                                 if (net->flowidset == 0) {
3962                                         panic("Flow ID not set");
3963                                 }
3964 #endif
3965                                 m->m_pkthdr.flowid = net->flowid;
3966                                 m->m_flags |= M_FLOWID;
3967                         } else {
3968                                 if ((init != NULL) && (init->m_flags & M_FLOWID)) {
3969                                         m->m_pkthdr.flowid = init->m_pkthdr.flowid;
3970                                         m->m_flags |= M_FLOWID;
3971                                 }
3972                         }
3973                         packet_length = sctp_calculate_len(m);
3974                         ip = mtod(m, struct ip *);
3975                         ip->ip_v = IPVERSION;
3976                         ip->ip_hl = (sizeof(struct ip) >> 2);
3977                         if (tos_value == 0) {
3978                                 /*
3979                                  * This means especially, that it is not set
3980                                  * at the SCTP layer. So use the value from
3981                                  * the IP layer.
3982                                  */
3983                                 tos_value = inp->ip_inp.inp.inp_ip_tos;
3984                         }
3985                         tos_value &= 0xfc;
3986                         if (ecn_ok) {
3987                                 tos_value |= sctp_get_ect(stcb, chk);
3988                         }
3989                         if ((nofragment_flag) && (port == 0)) {
3990                                 ip->ip_off = IP_DF;
3991                         } else
3992                                 ip->ip_off = 0;
3993
3994                         /* FreeBSD has a function for ip_id's */
3995                         ip->ip_id = ip_newid();
3996
3997                         ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3998                         ip->ip_len = packet_length;
3999                         ip->ip_tos = tos_value;
4000                         if (port) {
4001                                 ip->ip_p = IPPROTO_UDP;
4002                         } else {
4003                                 ip->ip_p = IPPROTO_SCTP;
4004                         }
4005                         ip->ip_sum = 0;
4006                         if (net == NULL) {
4007                                 ro = &iproute;
4008                                 memset(&iproute, 0, sizeof(iproute));
4009                                 memcpy(&ro->ro_dst, to, to->sa_len);
4010                         } else {
4011                                 ro = (sctp_route_t *) & net->ro;
4012                         }
4013                         /* Now the address selection part */
4014                         ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
4015
4016                         /* call the routine to select the src address */
4017                         if (net && out_of_asoc_ok == 0) {
4018                                 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4019                                         sctp_free_ifa(net->ro._s_addr);
4020                                         net->ro._s_addr = NULL;
4021                                         net->src_addr_selected = 0;
4022                                         if (ro->ro_rt) {
4023                                                 RTFREE(ro->ro_rt);
4024                                                 ro->ro_rt = NULL;
4025                                         }
4026                                 }
4027                                 if (net->src_addr_selected == 0) {
4028                                         /* Cache the source address */
4029                                         net->ro._s_addr = sctp_source_address_selection(inp, stcb,
4030                                             ro, net, 0,
4031                                             vrf_id);
4032                                         net->src_addr_selected = 1;
4033                                 }
4034                                 if (net->ro._s_addr == NULL) {
4035                                         /* No route to host */
4036                                         net->src_addr_selected = 0;
4037                                         sctp_handle_no_route(stcb, net, so_locked);
4038                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4039                                         sctp_m_freem(m);
4040                                         return (EHOSTUNREACH);
4041                                 }
4042                                 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
4043                         } else {
4044                                 if (over_addr == NULL) {
4045                                         struct sctp_ifa *_lsrc;
4046
4047                                         _lsrc = sctp_source_address_selection(inp, stcb, ro,
4048                                             net,
4049                                             out_of_asoc_ok,
4050                                             vrf_id);
4051                                         if (_lsrc == NULL) {
4052                                                 sctp_handle_no_route(stcb, net, so_locked);
4053                                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4054                                                 sctp_m_freem(m);
4055                                                 return (EHOSTUNREACH);
4056                                         }
4057                                         ip->ip_src = _lsrc->address.sin.sin_addr;
4058                                         sctp_free_ifa(_lsrc);
4059                                 } else {
4060                                         ip->ip_src = over_addr->sin.sin_addr;
4061                                         SCTP_RTALLOC(ro, vrf_id);
4062                                 }
4063                         }
4064                         if (port) {
4065                                 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
4066                                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4067                                 udp->uh_dport = port;
4068                                 udp->uh_ulen = htons(packet_length - sizeof(struct ip));
4069                                 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
4070                                 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4071                         } else {
4072                                 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
4073                         }
4074
4075                         sctphdr->src_port = src_port;
4076                         sctphdr->dest_port = dest_port;
4077                         sctphdr->v_tag = v_tag;
4078                         sctphdr->checksum = 0;
4079
4080                         /*
4081                          * If source address selection fails and we find no
4082                          * route then the ip_output should fail as well with
4083                          * a NO_ROUTE_TO_HOST type error. We probably should
4084                          * catch that somewhere and abort the association
4085                          * right away (assuming this is an INIT being sent).
4086                          */
4087                         if (ro->ro_rt == NULL) {
4088                                 /*
4089                                  * src addr selection failed to find a route
4090                                  * (or valid source addr), so we can't get
4091                                  * there from here (yet)!
4092                                  */
4093                                 sctp_handle_no_route(stcb, net, so_locked);
4094                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4095                                 sctp_m_freem(m);
4096                                 return (EHOSTUNREACH);
4097                         }
4098                         if (ro != &iproute) {
4099                                 memcpy(&iproute, ro, sizeof(*ro));
4100                         }
4101                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
4102                             (uint32_t) (ntohl(ip->ip_src.s_addr)));
4103                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
4104                             (uint32_t) (ntohl(ip->ip_dst.s_addr)));
4105                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
4106                             ro->ro_rt);
4107
4108                         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4109                                 /* failed to prepend data, give up */
4110                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4111                                 sctp_m_freem(m);
4112                                 return (ENOMEM);
4113                         }
4114 #ifdef  SCTP_PACKET_LOGGING
4115                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4116                                 sctp_packet_log(m, packet_length);
4117 #endif
4118                         SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4119                         if (port) {
4120 #if defined(SCTP_WITH_NO_CSUM)
4121                                 SCTP_STAT_INCR(sctps_sendnocrc);
4122 #else
4123                                 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4124                                     (stcb) &&
4125                                     (stcb->asoc.loopback_scope))) {
4126                                         sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
4127                                         SCTP_STAT_INCR(sctps_sendswcrc);
4128                                 } else {
4129                                         SCTP_STAT_INCR(sctps_sendnocrc);
4130                                 }
4131 #endif
4132                                 SCTP_ENABLE_UDP_CSUM(o_pak);
4133                         } else {
4134 #if defined(SCTP_WITH_NO_CSUM)
4135                                 SCTP_STAT_INCR(sctps_sendnocrc);
4136 #else
4137                                 m->m_pkthdr.csum_flags = CSUM_SCTP;
4138                                 m->m_pkthdr.csum_data = 0;
4139                                 SCTP_STAT_INCR(sctps_sendhwcrc);
4140 #endif
4141                         }
4142                         /* send it out.  table id is taken from stcb */
4143 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4144                         if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4145                                 so = SCTP_INP_SO(inp);
4146                                 SCTP_SOCKET_UNLOCK(so, 0);
4147                         }
4148 #endif
4149                         SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
4150 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4151                         if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4152                                 atomic_add_int(&stcb->asoc.refcnt, 1);
4153                                 SCTP_TCB_UNLOCK(stcb);
4154                                 SCTP_SOCKET_LOCK(so, 0);
4155                                 SCTP_TCB_LOCK(stcb);
4156                                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4157                         }
4158 #endif
4159                         SCTP_STAT_INCR(sctps_sendpackets);
4160                         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4161                         if (ret)
4162                                 SCTP_STAT_INCR(sctps_senderrors);
4163
4164                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
4165                         if (net == NULL) {
4166                                 /* free tempy routes */
4167                                 if (ro->ro_rt) {
4168                                         RTFREE(ro->ro_rt);
4169                                         ro->ro_rt = NULL;
4170                                 }
4171                         } else {
4172                                 /*
4173                                  * PMTU check versus smallest asoc MTU goes
4174                                  * here
4175                                  */
4176                                 if ((ro->ro_rt != NULL) &&
4177                                     (net->ro._s_addr)) {
4178                                         uint32_t mtu;
4179
4180                                         mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4181                                         if (net->port) {
4182                                                 mtu -= sizeof(struct udphdr);
4183                                         }
4184                                         if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
4185                                                 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4186                                                 net->mtu = mtu;
4187                                         }
4188                                 } else if (ro->ro_rt == NULL) {
4189                                         /* route was freed */
4190                                         if (net->ro._s_addr &&
4191                                             net->src_addr_selected) {
4192                                                 sctp_free_ifa(net->ro._s_addr);
4193                                                 net->ro._s_addr = NULL;
4194                                         }
4195                                         net->src_addr_selected = 0;
4196                                 }
4197                         }
4198                         return (ret);
4199                 }
4200 #endif
4201 #ifdef INET6
4202         case AF_INET6:
4203                 {
4204                         uint32_t flowlabel, flowinfo;
4205                         struct ip6_hdr *ip6h;
4206                         struct route_in6 ip6route;
4207                         struct ifnet *ifp;
4208                         struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
4209                         int prev_scope = 0;
4210                         struct sockaddr_in6 lsa6_storage;
4211                         int error;
4212                         u_short prev_port = 0;
4213                         int len;
4214
4215                         if (net) {
4216                                 flowlabel = net->flowlabel;
4217                         } else if (stcb) {
4218                                 flowlabel = stcb->asoc.default_flowlabel;
4219                         } else {
4220                                 flowlabel = inp->sctp_ep.default_flowlabel;
4221                         }
4222                         if (flowlabel == 0) {
4223                                 /*
4224                                  * This means especially, that it is not set
4225                                  * at the SCTP layer. So use the value from
4226                                  * the IP layer.
4227                                  */
4228                                 flowlabel = ntohl(((struct in6pcb *)inp)->in6p_flowinfo);
4229                         }
4230                         flowlabel &= 0x000fffff;
4231                         len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
4232                         if (port) {
4233                                 len += sizeof(struct udphdr);
4234                         }
4235                         newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
4236                         if (newm == NULL) {
4237                                 sctp_m_freem(m);
4238                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4239                                 return (ENOMEM);
4240                         }
4241                         SCTP_ALIGN_TO_END(newm, len);
4242                         SCTP_BUF_LEN(newm) = len;
4243                         SCTP_BUF_NEXT(newm) = m;
4244                         m = newm;
4245                         if (net != NULL) {
4246 #ifdef INVARIANTS
4247                                 if (net->flowidset == 0) {
4248                                         panic("Flow ID not set");
4249                                 }
4250 #endif
4251                                 m->m_pkthdr.flowid = net->flowid;
4252                                 m->m_flags |= M_FLOWID;
4253                         } else {
4254                                 if ((init != NULL) && (init->m_flags & M_FLOWID)) {
4255                                         m->m_pkthdr.flowid = init->m_pkthdr.flowid;
4256                                         m->m_flags |= M_FLOWID;
4257                                 }
4258                         }
4259                         packet_length = sctp_calculate_len(m);
4260
4261                         ip6h = mtod(m, struct ip6_hdr *);
4262                         /* protect *sin6 from overwrite */
4263                         sin6 = (struct sockaddr_in6 *)to;
4264                         tmp = *sin6;
4265                         sin6 = &tmp;
4266
4267                         /* KAME hack: embed scopeid */
4268                         if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4269                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4270                                 return (EINVAL);
4271                         }
4272                         if (net == NULL) {
4273                                 memset(&ip6route, 0, sizeof(ip6route));
4274                                 ro = (sctp_route_t *) & ip6route;
4275                                 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
4276                         } else {
4277                                 ro = (sctp_route_t *) & net->ro;
4278                         }
4279                         /*
4280                          * We assume here that inp_flow is in host byte
4281                          * order within the TCB!
4282                          */
4283                         if (tos_value == 0) {
4284                                 /*
4285                                  * This means especially, that it is not set
4286                                  * at the SCTP layer. So use the value from
4287                                  * the IP layer.
4288                                  */
4289                                 tos_value = (ntohl(((struct in6pcb *)inp)->in6p_flowinfo) >> 20) & 0xff;
4290                         }
4291                         tos_value &= 0xfc;
4292                         if (ecn_ok) {
4293                                 tos_value |= sctp_get_ect(stcb, chk);
4294                         }
4295                         flowinfo = 0x06;
4296                         flowinfo <<= 8;
4297                         flowinfo |= tos_value;
4298                         flowinfo <<= 20;
4299                         flowinfo |= flowlabel;
4300                         ip6h->ip6_flow = htonl(flowinfo);
4301                         if (port) {
4302                                 ip6h->ip6_nxt = IPPROTO_UDP;
4303                         } else {
4304                                 ip6h->ip6_nxt = IPPROTO_SCTP;
4305                         }
4306                         ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
4307                         ip6h->ip6_dst = sin6->sin6_addr;
4308
4309                         /*
4310                          * Add SRC address selection here: we can only reuse
4311                          * to a limited degree the kame src-addr-sel, since
4312                          * we can try their selection but it may not be
4313                          * bound.
4314                          */
4315                         bzero(&lsa6_tmp, sizeof(lsa6_tmp));
4316                         lsa6_tmp.sin6_family = AF_INET6;
4317                         lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
4318                         lsa6 = &lsa6_tmp;
4319                         if (net && out_of_asoc_ok == 0) {
4320                                 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4321                                         sctp_free_ifa(net->ro._s_addr);
4322                                         net->ro._s_addr = NULL;
4323                                         net->src_addr_selected = 0;
4324                                         if (ro->ro_rt) {
4325                                                 RTFREE(ro->ro_rt);
4326                                                 ro->ro_rt = NULL;
4327                                         }
4328                                 }
4329                                 if (net->src_addr_selected == 0) {
4330                                         sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4331                                         /* KAME hack: embed scopeid */
4332                                         if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4333                                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4334                                                 return (EINVAL);
4335                                         }
4336                                         /* Cache the source address */
4337                                         net->ro._s_addr = sctp_source_address_selection(inp,
4338                                             stcb,
4339                                             ro,
4340                                             net,
4341                                             0,
4342                                             vrf_id);
4343                                         (void)sa6_recoverscope(sin6);
4344                                         net->src_addr_selected = 1;
4345                                 }
4346                                 if (net->ro._s_addr == NULL) {
4347                                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
4348                                         net->src_addr_selected = 0;
4349                                         sctp_handle_no_route(stcb, net, so_locked);
4350                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4351                                         sctp_m_freem(m);
4352                                         return (EHOSTUNREACH);
4353                                 }
4354                                 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
4355                         } else {
4356                                 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
4357                                 /* KAME hack: embed scopeid */
4358                                 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4359                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4360                                         return (EINVAL);
4361                                 }
4362                                 if (over_addr == NULL) {
4363                                         struct sctp_ifa *_lsrc;
4364
4365                                         _lsrc = sctp_source_address_selection(inp, stcb, ro,
4366                                             net,
4367                                             out_of_asoc_ok,
4368                                             vrf_id);
4369                                         if (_lsrc == NULL) {
4370                                                 sctp_handle_no_route(stcb, net, so_locked);
4371                                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4372                                                 sctp_m_freem(m);
4373                                                 return (EHOSTUNREACH);
4374                                         }
4375                                         lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
4376                                         sctp_free_ifa(_lsrc);
4377                                 } else {
4378                                         lsa6->sin6_addr = over_addr->sin6.sin6_addr;
4379                                         SCTP_RTALLOC(ro, vrf_id);
4380                                 }
4381                                 (void)sa6_recoverscope(sin6);
4382                         }
4383                         lsa6->sin6_port = inp->sctp_lport;
4384
4385                         if (ro->ro_rt == NULL) {
4386                                 /*
4387                                  * src addr selection failed to find a route
4388                                  * (or valid source addr), so we can't get
4389                                  * there from here!
4390                                  */
4391                                 sctp_handle_no_route(stcb, net, so_locked);
4392                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4393                                 sctp_m_freem(m);
4394                                 return (EHOSTUNREACH);
4395                         }
4396                         /*
4397                          * XXX: sa6 may not have a valid sin6_scope_id in
4398                          * the non-SCOPEDROUTING case.
4399                          */
4400                         bzero(&lsa6_storage, sizeof(lsa6_storage));
4401                         lsa6_storage.sin6_family = AF_INET6;
4402                         lsa6_storage.sin6_len = sizeof(lsa6_storage);
4403                         lsa6_storage.sin6_addr = lsa6->sin6_addr;
4404                         if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
4405                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
4406                                 sctp_m_freem(m);
4407                                 return (error);
4408                         }
4409                         /* XXX */
4410                         lsa6_storage.sin6_addr = lsa6->sin6_addr;
4411                         lsa6_storage.sin6_port = inp->sctp_lport;
4412                         lsa6 = &lsa6_storage;
4413                         ip6h->ip6_src = lsa6->sin6_addr;
4414
4415                         if (port) {
4416                                 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4417                                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4418                                 udp->uh_dport = port;
4419                                 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
4420                                 udp->uh_sum = 0;
4421                                 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4422                         } else {
4423                                 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4424                         }
4425
4426                         sctphdr->src_port = src_port;
4427                         sctphdr->dest_port = dest_port;
4428                         sctphdr->v_tag = v_tag;
4429                         sctphdr->checksum = 0;
4430
4431                         /*
4432                          * We set the hop limit now since there is a good
4433                          * chance that our ro pointer is now filled
4434                          */
4435                         ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4436                         ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4437
4438 #ifdef SCTP_DEBUG
4439                         /* Copy to be sure something bad is not happening */
4440                         sin6->sin6_addr = ip6h->ip6_dst;
4441                         lsa6->sin6_addr = ip6h->ip6_src;
4442 #endif
4443
4444                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4445                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4446                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4447                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4448                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4449                         if (net) {
4450                                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4451                                 /*
4452                                  * preserve the port and scope for link
4453                                  * local send
4454                                  */
4455                                 prev_scope = sin6->sin6_scope_id;
4456                                 prev_port = sin6->sin6_port;
4457                         }
4458                         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4459                                 /* failed to prepend data, give up */
4460                                 sctp_m_freem(m);
4461                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4462                                 return (ENOMEM);
4463                         }
4464 #ifdef  SCTP_PACKET_LOGGING
4465                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4466                                 sctp_packet_log(m, packet_length);
4467 #endif
4468                         SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4469                         if (port) {
4470 #if defined(SCTP_WITH_NO_CSUM)
4471                                 SCTP_STAT_INCR(sctps_sendnocrc);
4472 #else
4473                                 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4474                                     (stcb) &&
4475                                     (stcb->asoc.loopback_scope))) {
4476                                         sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4477                                         SCTP_STAT_INCR(sctps_sendswcrc);
4478                                 } else {
4479                                         SCTP_STAT_INCR(sctps_sendnocrc);
4480                                 }
4481 #endif
4482                                 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4483                                         udp->uh_sum = 0xffff;
4484                                 }
4485                         } else {
4486 #if defined(SCTP_WITH_NO_CSUM)
4487                                 SCTP_STAT_INCR(sctps_sendnocrc);
4488 #else
4489                                 m->m_pkthdr.csum_flags = CSUM_SCTP;
4490                                 m->m_pkthdr.csum_data = 0;
4491                                 SCTP_STAT_INCR(sctps_sendhwcrc);
4492 #endif
4493                         }
4494                         /* send it out. table id is taken from stcb */
4495 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4496                         if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4497                                 so = SCTP_INP_SO(inp);
4498                                 SCTP_SOCKET_UNLOCK(so, 0);
4499                         }
4500 #endif
4501                         SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4502 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4503                         if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4504                                 atomic_add_int(&stcb->asoc.refcnt, 1);
4505                                 SCTP_TCB_UNLOCK(stcb);
4506                                 SCTP_SOCKET_LOCK(so, 0);
4507                                 SCTP_TCB_LOCK(stcb);
4508                                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4509                         }
4510 #endif
4511                         if (net) {
4512                                 /* for link local this must be done */
4513                                 sin6->sin6_scope_id = prev_scope;
4514                                 sin6->sin6_port = prev_port;
4515                         }
4516                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4517                         SCTP_STAT_INCR(sctps_sendpackets);
4518                         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4519                         if (ret) {
4520                                 SCTP_STAT_INCR(sctps_senderrors);
4521                         }
4522                         if (net == NULL) {
4523                                 /* Now if we had a temp route free it */
4524                                 if (ro->ro_rt) {
4525                                         RTFREE(ro->ro_rt);
4526                                 }
4527                         } else {
4528                                 /*
4529                                  * PMTU check versus smallest asoc MTU goes
4530                                  * here
4531                                  */
4532                                 if (ro->ro_rt == NULL) {
4533                                         /* Route was freed */
4534                                         if (net->ro._s_addr &&
4535                                             net->src_addr_selected) {
4536                                                 sctp_free_ifa(net->ro._s_addr);
4537                                                 net->ro._s_addr = NULL;
4538                                         }
4539                                         net->src_addr_selected = 0;
4540                                 }
4541                                 if ((ro->ro_rt != NULL) &&
4542                                     (net->ro._s_addr)) {
4543                                         uint32_t mtu;
4544
4545                                         mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4546                                         if (mtu &&
4547                                             (stcb->asoc.smallest_mtu > mtu)) {
4548                                                 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4549                                                 net->mtu = mtu;
4550                                                 if (net->port) {
4551                                                         net->mtu -= sizeof(struct udphdr);
4552                                                 }
4553                                         }
4554                                 } else if (ifp) {
4555                                         if (ND_IFINFO(ifp)->linkmtu &&
4556                                             (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4557                                                 sctp_mtu_size_reset(inp,
4558                                                     &stcb->asoc,
4559                                                     ND_IFINFO(ifp)->linkmtu);
4560                                         }
4561                                 }
4562                         }
4563                         return (ret);
4564                 }
4565 #endif
4566         default:
4567                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4568                     ((struct sockaddr *)to)->sa_family);
4569                 sctp_m_freem(m);
4570                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4571                 return (EFAULT);
4572         }
4573 }
4574
4575
4576 void
4577 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4578 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4579     SCTP_UNUSED
4580 #endif
4581 )
4582 {
4583         struct mbuf *m, *m_at, *mp_last;
4584         struct sctp_nets *net;
4585         struct sctp_init_chunk *init;
4586         struct sctp_supported_addr_param *sup_addr;
4587         struct sctp_adaptation_layer_indication *ali;
4588         struct sctp_ecn_supported_param *ecn;
4589         struct sctp_prsctp_supported_param *prsctp;
4590         struct sctp_supported_chunk_types_param *pr_supported;
4591         int cnt_inits_to = 0;
4592         int padval, ret;
4593         int num_ext;
4594         int p_len;
4595
4596         /* INIT's always go to the primary (and usually ONLY address) */
4597         mp_last = NULL;
4598         net = stcb->asoc.primary_destination;
4599         if (net == NULL) {
4600                 net = TAILQ_FIRST(&stcb->asoc.nets);
4601                 if (net == NULL) {
4602                         /* TSNH */
4603                         return;
4604                 }
4605                 /* we confirm any address we send an INIT to */
4606                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4607                 (void)sctp_set_primary_addr(stcb, NULL, net);
4608         } else {
4609                 /* we confirm any address we send an INIT to */
4610                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4611         }
4612         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4613 #ifdef INET6
4614         if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4615                 /*
4616                  * special hook, if we are sending to link local it will not
4617                  * show up in our private address count.
4618                  */
4619                 struct sockaddr_in6 *sin6l;
4620
4621                 sin6l = &net->ro._l_addr.sin6;
4622                 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4623                         cnt_inits_to = 1;
4624         }
4625 #endif
4626         if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4627                 /* This case should not happen */
4628                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4629                 return;
4630         }
4631         /* start the INIT timer */
4632         sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4633
4634         m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4635         if (m == NULL) {
4636                 /* No memory, INIT timer will re-attempt. */
4637                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4638                 return;
4639         }
4640         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4641         /*
4642          * assume peer supports asconf in order to be able to queue local
4643          * address changes while an INIT is in flight and before the assoc
4644          * is established.
4645          */
4646         stcb->asoc.peer_supports_asconf = 1;
4647         /* Now lets put the SCTP header in place */
4648         init = mtod(m, struct sctp_init_chunk *);
4649         /* now the chunk header */
4650         init->ch.chunk_type = SCTP_INITIATION;
4651         init->ch.chunk_flags = 0;
4652         /* fill in later from mbuf we build */
4653         init->ch.chunk_length = 0;
4654         /* place in my tag */
4655         init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4656         /* set up some of the credits. */
4657         init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4658             SCTP_MINIMAL_RWND));
4659
4660         init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4661         init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4662         init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4663         /* now the address restriction */
4664         /* XXX Should we take the address family of the socket into account? */
4665         sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4666             sizeof(*init));
4667         sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4668 #ifdef INET6
4669 #ifdef INET
4670         /* we support 2 types: IPv4/IPv6 */
4671         sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + 2 * sizeof(uint16_t));
4672         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4673         sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4674 #else
4675         /* we support 1 type: IPv6 */
4676         sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t));
4677         sup_addr->addr_type[0] = htons(SCTP_IPV6_ADDRESS);
4678         sup_addr->addr_type[1] = htons(0);      /* this is the padding */
4679 #endif
4680 #else
4681         /* we support 1 type: IPv4 */
4682         sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t));
4683         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4684         sup_addr->addr_type[1] = htons(0);      /* this is the padding */
4685 #endif
4686         SCTP_BUF_LEN(m) += sizeof(struct sctp_supported_addr_param);
4687         /* adaptation layer indication parameter */
4688         ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(struct sctp_supported_addr_param));
4689         ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4690         ali->ph.param_length = htons(sizeof(*ali));
4691         ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4692         SCTP_BUF_LEN(m) += sizeof(*ali);
4693         ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4694
4695         if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4696                 /* Add NAT friendly parameter */
4697                 struct sctp_paramhdr *ph;
4698
4699                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4700                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4701                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4702                 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4703                 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4704         }
4705         /* now any cookie time extensions */
4706         if (stcb->asoc.cookie_preserve_req) {
4707                 struct sctp_cookie_perserve_param *cookie_preserve;
4708
4709                 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4710                 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4711                 cookie_preserve->ph.param_length = htons(
4712                     sizeof(*cookie_preserve));
4713                 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4714                 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4715                 ecn = (struct sctp_ecn_supported_param *)(
4716                     (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4717                 stcb->asoc.cookie_preserve_req = 0;
4718         }
4719         /* ECN parameter */
4720         if (stcb->asoc.ecn_allowed == 1) {
4721                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4722                 ecn->ph.param_length = htons(sizeof(*ecn));
4723                 SCTP_BUF_LEN(m) += sizeof(*ecn);
4724                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4725                     sizeof(*ecn));
4726         } else {
4727                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4728         }
4729         /* And now tell the peer we do pr-sctp */
4730         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4731         prsctp->ph.param_length = htons(sizeof(*prsctp));
4732         SCTP_BUF_LEN(m) += sizeof(*prsctp);
4733
4734         /* And now tell the peer we do all the extensions */
4735         pr_supported = (struct sctp_supported_chunk_types_param *)
4736             ((caddr_t)prsctp + sizeof(*prsctp));
4737         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4738         num_ext = 0;
4739         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4740         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4741         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4742         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4743         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4744         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4745                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4746         }
4747         if (stcb->asoc.sctp_nr_sack_on_off == 1) {
4748                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4749         }
4750         p_len = sizeof(*pr_supported) + num_ext;
4751         pr_supported->ph.param_length = htons(p_len);
4752         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4753         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4754
4755
4756         /* add authentication parameters */
4757         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4758                 struct sctp_auth_random *randp;
4759                 struct sctp_auth_hmac_algo *hmacs;
4760                 struct sctp_auth_chunk_list *chunks;
4761
4762                 /* attach RANDOM parameter, if available */
4763                 if (stcb->asoc.authinfo.random != NULL) {
4764                         randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4765                         p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4766                         /* random key already contains the header */
4767                         bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4768                         /* zero out any padding required */
4769                         bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4770                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4771                 }
4772                 /* add HMAC_ALGO parameter */
4773                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4774                 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4775                     (uint8_t *) hmacs->hmac_ids);
4776                 if (p_len > 0) {
4777                         p_len += sizeof(*hmacs);
4778                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4779                         hmacs->ph.param_length = htons(p_len);
4780                         /* zero out any padding required */
4781                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4782                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4783                 }
4784                 /* add CHUNKS parameter */
4785                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4786                 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4787                     chunks->chunk_types);
4788                 if (p_len > 0) {
4789                         p_len += sizeof(*chunks);
4790                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4791                         chunks->ph.param_length = htons(p_len);
4792                         /* zero out any padding required */
4793                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4794                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4795                 }
4796         }
4797         m_at = m;
4798         /* now the addresses */
4799         {
4800                 struct sctp_scoping scp;
4801
4802                 /*
4803                  * To optimize this we could put the scoping stuff into a
4804                  * structure and remove the individual uint8's from the
4805                  * assoc structure. Then we could just sifa in the address
4806                  * within the stcb.. but for now this is a quick hack to get
4807                  * the address stuff teased apart.
4808                  */
4809                 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4810                 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4811                 scp.loopback_scope = stcb->asoc.loopback_scope;
4812                 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4813                 scp.local_scope = stcb->asoc.local_scope;
4814                 scp.site_scope = stcb->asoc.site_scope;
4815
4816                 m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to);
4817         }
4818
4819         /* calulate the size and update pkt header and chunk header */
4820         p_len = 0;
4821         for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4822                 if (SCTP_BUF_NEXT(m_at) == NULL)
4823                         mp_last = m_at;
4824                 p_len += SCTP_BUF_LEN(m_at);
4825         }
4826         init->ch.chunk_length = htons(p_len);
4827         /*
4828          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4829          * here since the timer will drive a retranmission.
4830          */
4831
4832         /* I don't expect this to execute but we will be safe here */
4833         padval = p_len % 4;
4834         if ((padval) && (mp_last)) {
4835                 /*
4836                  * The compiler worries that mp_last may not be set even
4837                  * though I think it is impossible :-> however we add
4838                  * mp_last here just in case.
4839                  */
4840                 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4841                 if (ret) {
4842                         /* Houston we have a problem, no space */
4843                         sctp_m_freem(m);
4844                         return;
4845                 }
4846                 p_len += padval;
4847         }
4848         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4849         ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4850             (struct sockaddr *)&net->ro._l_addr,
4851             m, 0, NULL, 0, 0, 0, NULL, 0,
4852             inp->sctp_lport, stcb->rport, htonl(0),
4853             net->port, so_locked, NULL, NULL);
4854         SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4855         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4856         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4857 }
4858
4859 struct mbuf *
4860 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4861     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4862 {
4863         /*
4864          * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4865          * being equal to the beginning of the params i.e. (iphlen +
4866          * sizeof(struct sctp_init_msg) parse through the parameters to the
4867          * end of the mbuf verifying that all parameters are known.
4868          * 
4869          * For unknown parameters build and return a mbuf with
4870          * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4871          * processing this chunk stop, and set *abort_processing to 1.
4872          * 
4873          * By having param_offset be pre-set to where parameters begin it is
4874          * hoped that this routine may be reused in the future by new
4875          * features.
4876          */
4877         struct sctp_paramhdr *phdr, params;
4878
4879         struct mbuf *mat, *op_err;
4880         char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4881         int at, limit, pad_needed;
4882         uint16_t ptype, plen, padded_size;
4883         int err_at;
4884
4885         *abort_processing = 0;
4886         mat = in_initpkt;
4887         err_at = 0;
4888         limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4889         at = param_offset;
4890         op_err = NULL;
4891         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4892         phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4893         while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4894                 ptype = ntohs(phdr->param_type);
4895                 plen = ntohs(phdr->param_length);
4896                 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4897                         /* wacked parameter */
4898                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4899                         goto invalid_size;
4900                 }
4901                 limit -= SCTP_SIZE32(plen);
4902                 /*-
4903                  * All parameters for all chunks that we know/understand are
4904                  * listed here. We process them other places and make
4905                  * appropriate stop actions per the upper bits. However this
4906                  * is the generic routine processor's can call to get back
4907                  * an operr.. to either incorporate (init-ack) or send.
4908                  */
4909                 padded_size = SCTP_SIZE32(plen);
4910                 switch (ptype) {
4911                         /* Param's with variable size */
4912                 case SCTP_HEARTBEAT_INFO:
4913                 case SCTP_STATE_COOKIE:
4914                 case SCTP_UNRECOG_PARAM:
4915                 case SCTP_ERROR_CAUSE_IND:
4916                         /* ok skip fwd */
4917                         at += padded_size;
4918                         break;
4919                         /* Param's with variable size within a range */
4920                 case SCTP_CHUNK_LIST:
4921                 case SCTP_SUPPORTED_CHUNK_EXT:
4922                         if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4923                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4924                                 goto invalid_size;
4925                         }
4926                         at += padded_size;
4927                         break;
4928                 case SCTP_SUPPORTED_ADDRTYPE:
4929                         if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4930                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4931                                 goto invalid_size;
4932                         }
4933                         at += padded_size;
4934                         break;
4935                 case SCTP_RANDOM:
4936                         if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4937                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4938                                 goto invalid_size;
4939                         }
4940                         at += padded_size;
4941                         break;
4942                 case SCTP_SET_PRIM_ADDR:
4943                 case SCTP_DEL_IP_ADDRESS:
4944                 case SCTP_ADD_IP_ADDRESS:
4945                         if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4946                             (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4947                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4948                                 goto invalid_size;
4949                         }
4950                         at += padded_size;
4951                         break;
4952                         /* Param's with a fixed size */
4953                 case SCTP_IPV4_ADDRESS:
4954                         if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4955                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4956                                 goto invalid_size;
4957                         }
4958                         at += padded_size;
4959                         break;
4960                 case SCTP_IPV6_ADDRESS:
4961                         if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4962                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4963                                 goto invalid_size;
4964                         }
4965                         at += padded_size;
4966                         break;
4967                 case SCTP_COOKIE_PRESERVE:
4968                         if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4969                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4970                                 goto invalid_size;
4971                         }
4972                         at += padded_size;
4973                         break;
4974                 case SCTP_HAS_NAT_SUPPORT:
4975                         *nat_friendly = 1;
4976                         /* fall through */
4977                 case SCTP_PRSCTP_SUPPORTED:
4978
4979                         if (padded_size != sizeof(struct sctp_paramhdr)) {
4980                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
4981                                 goto invalid_size;
4982                         }
4983                         at += padded_size;
4984                         break;
4985                 case SCTP_ECN_CAPABLE:
4986                         if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4987                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4988                                 goto invalid_size;
4989                         }
4990                         at += padded_size;
4991                         break;
4992                 case SCTP_ULP_ADAPTATION:
4993                         if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4994                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4995                                 goto invalid_size;
4996                         }
4997                         at += padded_size;
4998                         break;
4999                 case SCTP_SUCCESS_REPORT:
5000                         if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
5001                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
5002                                 goto invalid_size;
5003                         }
5004                         at += padded_size;
5005                         break;
5006                 case SCTP_HOSTNAME_ADDRESS:
5007                         {
5008                                 /* We can NOT handle HOST NAME addresses!! */
5009                                 int l_len;
5010
5011                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
5012                                 *abort_processing = 1;
5013                                 if (op_err == NULL) {
5014                                         /* Ok need to try to get a mbuf */
5015 #ifdef INET6
5016                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5017 #else
5018                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5019 #endif
5020                                         l_len += plen;
5021                                         l_len += sizeof(struct sctp_paramhdr);
5022                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5023                                         if (op_err) {
5024                                                 SCTP_BUF_LEN(op_err) = 0;
5025                                                 /*
5026                                                  * pre-reserve space for ip
5027                                                  * and sctp header  and
5028                                                  * chunk hdr
5029                                                  */
5030 #ifdef INET6
5031                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5032 #else
5033                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5034 #endif
5035                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5036                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5037                                         }
5038                                 }
5039                                 if (op_err) {
5040                                         /* If we have space */
5041                                         struct sctp_paramhdr s;
5042
5043                                         if (err_at % 4) {
5044                                                 uint32_t cpthis = 0;
5045
5046                                                 pad_needed = 4 - (err_at % 4);
5047                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5048                                                 err_at += pad_needed;
5049                                         }
5050                                         s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
5051                                         s.param_length = htons(sizeof(s) + plen);
5052                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5053                                         err_at += sizeof(s);
5054                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
5055                                         if (phdr == NULL) {
5056                                                 sctp_m_freem(op_err);
5057                                                 /*
5058                                                  * we are out of memory but
5059                                                  * we still need to have a
5060                                                  * look at what to do (the
5061                                                  * system is in trouble
5062                                                  * though).
5063                                                  */
5064                                                 return (NULL);
5065                                         }
5066                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
5067                                         err_at += plen;
5068                                 }
5069                                 return (op_err);
5070                                 break;
5071                         }
5072                 default:
5073                         /*
5074                          * we do not recognize the parameter figure out what
5075                          * we do.
5076                          */
5077                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
5078                         if ((ptype & 0x4000) == 0x4000) {
5079                                 /* Report bit is set?? */
5080                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
5081                                 if (op_err == NULL) {
5082                                         int l_len;
5083
5084                                         /* Ok need to try to get an mbuf */
5085 #ifdef INET6
5086                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5087 #else
5088                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5089 #endif
5090                                         l_len += plen;
5091                                         l_len += sizeof(struct sctp_paramhdr);
5092                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5093                                         if (op_err) {
5094                                                 SCTP_BUF_LEN(op_err) = 0;
5095 #ifdef INET6
5096                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5097 #else
5098                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5099 #endif
5100                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5101                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5102                                         }
5103                                 }
5104                                 if (op_err) {
5105                                         /* If we have space */
5106                                         struct sctp_paramhdr s;
5107
5108                                         if (err_at % 4) {
5109                                                 uint32_t cpthis = 0;
5110
5111                                                 pad_needed = 4 - (err_at % 4);
5112                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5113                                                 err_at += pad_needed;
5114                                         }
5115                                         s.param_type = htons(SCTP_UNRECOG_PARAM);
5116                                         s.param_length = htons(sizeof(s) + plen);
5117                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5118                                         err_at += sizeof(s);
5119                                         if (plen > sizeof(tempbuf)) {
5120                                                 plen = sizeof(tempbuf);
5121                                         }
5122                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
5123                                         if (phdr == NULL) {
5124                                                 sctp_m_freem(op_err);
5125                                                 /*
5126                                                  * we are out of memory but
5127                                                  * we still need to have a
5128                                                  * look at what to do (the
5129                                                  * system is in trouble
5130                                                  * though).
5131                                                  */
5132                                                 op_err = NULL;
5133                                                 goto more_processing;
5134                                         }
5135                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
5136                                         err_at += plen;
5137                                 }
5138                         }
5139         more_processing:
5140                         if ((ptype & 0x8000) == 0x0000) {
5141                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
5142                                 return (op_err);
5143                         } else {
5144                                 /* skip this chunk and continue processing */
5145                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
5146                                 at += SCTP_SIZE32(plen);
5147                         }
5148                         break;
5149
5150                 }
5151                 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
5152         }
5153         return (op_err);
5154 invalid_size:
5155         SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
5156         *abort_processing = 1;
5157         if ((op_err == NULL) && phdr) {
5158                 int l_len;
5159
5160 #ifdef INET6
5161                 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5162 #else
5163                 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5164 #endif
5165                 l_len += (2 * sizeof(struct sctp_paramhdr));
5166                 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5167                 if (op_err) {
5168                         SCTP_BUF_LEN(op_err) = 0;
5169 #ifdef INET6
5170                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5171 #else
5172                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5173 #endif
5174                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5175                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5176                 }
5177         }
5178         if ((op_err) && phdr) {
5179                 struct sctp_paramhdr s;
5180
5181                 if (err_at % 4) {
5182                         uint32_t cpthis = 0;
5183
5184                         pad_needed = 4 - (err_at % 4);
5185                         m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5186                         err_at += pad_needed;
5187                 }
5188                 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5189                 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
5190                 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5191                 err_at += sizeof(s);
5192                 /* Only copy back the p-hdr that caused the issue */
5193                 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
5194         }
5195         return (op_err);
5196 }
5197
5198 static int
5199 sctp_are_there_new_addresses(struct sctp_association *asoc,
5200     struct mbuf *in_initpkt, int iphlen, int offset)
5201 {
5202         /*
5203          * Given a INIT packet, look through the packet to verify that there
5204          * are NO new addresses. As we go through the parameters add reports
5205          * of any un-understood parameters that require an error.  Also we
5206          * must return (1) to drop the packet if we see a un-understood
5207          * parameter that tells us to drop the chunk.
5208          */
5209         struct sockaddr *sa_touse;
5210         struct sockaddr *sa;
5211         struct sctp_paramhdr *phdr, params;
5212         uint16_t ptype, plen;
5213         uint8_t fnd;
5214         struct sctp_nets *net;
5215         struct ip *iph;
5216
5217 #ifdef INET
5218         struct sockaddr_in sin4, *sa4;
5219
5220 #endif
5221 #ifdef INET6
5222         struct sockaddr_in6 sin6, *sa6;
5223         struct ip6_hdr *ip6h;
5224
5225 #endif
5226
5227 #ifdef INET
5228         memset(&sin4, 0, sizeof(sin4));
5229         sin4.sin_family = AF_INET;
5230         sin4.sin_len = sizeof(sin4);
5231 #endif
5232 #ifdef INET6
5233         memset(&sin6, 0, sizeof(sin6));
5234         sin6.sin6_family = AF_INET6;
5235         sin6.sin6_len = sizeof(sin6);
5236 #endif
5237         sa_touse = NULL;
5238         /* First what about the src address of the pkt ? */
5239         iph = mtod(in_initpkt, struct ip *);
5240         switch (iph->ip_v) {
5241 #ifdef INET
5242         case IPVERSION:
5243                 /* source addr is IPv4 */
5244                 sin4.sin_addr = iph->ip_src;
5245                 sa_touse = (struct sockaddr *)&sin4;
5246                 break;
5247 #endif
5248 #ifdef INET6
5249         case IPV6_VERSION >> 4:
5250                 /* source addr is IPv6 */
5251                 ip6h = mtod(in_initpkt, struct ip6_hdr *);
5252                 sin6.sin6_addr = ip6h->ip6_src;
5253                 sa_touse = (struct sockaddr *)&sin6;
5254                 break;
5255 #endif
5256         default:
5257                 return (1);
5258         }
5259
5260         fnd = 0;
5261         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5262                 sa = (struct sockaddr *)&net->ro._l_addr;
5263                 if (sa->sa_family == sa_touse->sa_family) {
5264 #ifdef INET
5265                         if (sa->sa_family == AF_INET) {
5266                                 sa4 = (struct sockaddr_in *)sa;
5267                                 if (sa4->sin_addr.s_addr == sin4.sin_addr.s_addr) {
5268                                         fnd = 1;
5269                                         break;
5270                                 }
5271                         }
5272 #endif
5273 #ifdef INET6
5274                         if (sa->sa_family == AF_INET6) {
5275                                 sa6 = (struct sockaddr_in6 *)sa;
5276                                 if (SCTP6_ARE_ADDR_EQUAL(sa6, &sin6)) {
5277                                         fnd = 1;
5278                                         break;
5279                                 }
5280                         }
5281 #endif
5282                 }
5283         }
5284         if (fnd == 0) {
5285                 /* New address added! no need to look futher. */
5286                 return (1);
5287         }
5288         /* Ok so far lets munge through the rest of the packet */
5289         offset += sizeof(struct sctp_init_chunk);
5290         phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5291         while (phdr) {
5292                 sa_touse = NULL;
5293                 ptype = ntohs(phdr->param_type);
5294                 plen = ntohs(phdr->param_length);
5295                 switch (ptype) {
5296 #ifdef INET
5297                 case SCTP_IPV4_ADDRESS:
5298                         {
5299                                 struct sctp_ipv4addr_param *p4, p4_buf;
5300
5301                                 phdr = sctp_get_next_param(in_initpkt, offset,
5302                                     (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5303                                 if (plen != sizeof(struct sctp_ipv4addr_param) ||
5304                                     phdr == NULL) {
5305                                         return (1);
5306                                 }
5307                                 p4 = (struct sctp_ipv4addr_param *)phdr;
5308                                 sin4.sin_addr.s_addr = p4->addr;
5309                                 sa_touse = (struct sockaddr *)&sin4;
5310                         }
5311 #endif
5312 #ifdef INET6
5313                 case SCTP_IPV6_ADDRESS:
5314                         {
5315                                 struct sctp_ipv6addr_param *p6, p6_buf;
5316
5317                                 phdr = sctp_get_next_param(in_initpkt, offset,
5318                                     (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5319                                 if (plen != sizeof(struct sctp_ipv6addr_param) ||
5320                                     phdr == NULL) {
5321                                         return (1);
5322                                 }
5323                                 p6 = (struct sctp_ipv6addr_param *)phdr;
5324                                 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5325                                     sizeof(p6->addr));
5326                                 sa_touse = (struct sockaddr *)&sin6;
5327                         }
5328 #endif
5329                 default:
5330                         sa_touse = NULL;
5331                 }
5332                 if (sa_touse) {
5333                         /* ok, sa_touse points to one to check */
5334                         fnd = 0;
5335                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5336                                 sa = (struct sockaddr *)&net->ro._l_addr;
5337                                 if (sa->sa_family != sa_touse->sa_family) {
5338                                         continue;
5339                                 }
5340 #ifdef INET
5341                                 if (sa->sa_family == AF_INET) {
5342                                         sa4 = (struct sockaddr_in *)sa;
5343                                         if (sa4->sin_addr.s_addr ==
5344                                             sin4.sin_addr.s_addr) {
5345                                                 fnd = 1;
5346                                                 break;
5347                                         }
5348                                 }
5349 #endif
5350 #ifdef INET6
5351                                 if (sa->sa_family == AF_INET6) {
5352                                         sa6 = (struct sockaddr_in6 *)sa;
5353                                         if (SCTP6_ARE_ADDR_EQUAL(
5354                                             sa6, &sin6)) {
5355                                                 fnd = 1;
5356                                                 break;
5357                                         }
5358                                 }
5359 #endif
5360                         }
5361                         if (!fnd) {
5362                                 /* New addr added! no need to look further */
5363                                 return (1);
5364                         }
5365                 }
5366                 offset += SCTP_SIZE32(plen);
5367                 phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5368         }
5369         return (0);
5370 }
5371
5372 /*
5373  * Given a MBUF chain that was sent into us containing an INIT. Build a
5374  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5375  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5376  * message (i.e. the struct sctp_init_msg).
5377  */
5378 void
5379 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5380     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
5381     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
5382 {
5383         struct sctp_association *asoc;
5384         struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
5385         struct sctp_init_ack_chunk *initack;
5386         struct sctp_adaptation_layer_indication *ali;
5387         struct sctp_ecn_supported_param *ecn;
5388         struct sctp_prsctp_supported_param *prsctp;
5389         struct sctp_supported_chunk_types_param *pr_supported;
5390         union sctp_sockstore store, store1, *over_addr;
5391
5392 #ifdef INET
5393         struct sockaddr_in *sin, *to_sin;
5394
5395 #endif
5396 #ifdef INET6
5397         struct sockaddr_in6 *sin6, *to_sin6;
5398
5399 #endif
5400         struct ip *iph;
5401
5402 #ifdef INET6
5403         struct ip6_hdr *ip6;
5404
5405 #endif
5406         struct sockaddr *to;
5407         struct sctp_state_cookie stc;
5408         struct sctp_nets *net = NULL;
5409         uint8_t *signature = NULL;
5410         int cnt_inits_to = 0;
5411         uint16_t his_limit, i_want;
5412         int abort_flag, padval;
5413         int num_ext;
5414         int p_len;
5415         int nat_friendly = 0;
5416         struct socket *so;
5417
5418         if (stcb)
5419                 asoc = &stcb->asoc;
5420         else
5421                 asoc = NULL;
5422         mp_last = NULL;
5423         if ((asoc != NULL) &&
5424             (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
5425             (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
5426                 /* new addresses, out of here in non-cookie-wait states */
5427                 /*
5428                  * Send a ABORT, we don't add the new address error clause
5429                  * though we even set the T bit and copy in the 0 tag.. this
5430                  * looks no different than if no listener was present.
5431                  */
5432                 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
5433                 return;
5434         }
5435         abort_flag = 0;
5436         op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5437             (offset + sizeof(struct sctp_init_chunk)),
5438             &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
5439         if (abort_flag) {
5440 do_a_abort:
5441                 sctp_send_abort(init_pkt, iphlen, sh,
5442                     init_chk->init.initiate_tag, op_err, vrf_id, port);
5443                 return;
5444         }
5445         m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
5446         if (m == NULL) {
5447                 /* No memory, INIT timer will re-attempt. */
5448                 if (op_err)
5449                         sctp_m_freem(op_err);
5450                 return;
5451         }
5452         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5453
5454         /* the time I built cookie */
5455         (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5456
5457         /* populate any tie tags */
5458         if (asoc != NULL) {
5459                 /* unlock before tag selections */
5460                 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5461                 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5462                 stc.cookie_life = asoc->cookie_life;
5463                 net = asoc->primary_destination;
5464         } else {
5465                 stc.tie_tag_my_vtag = 0;
5466                 stc.tie_tag_peer_vtag = 0;
5467                 /* life I will award this cookie */
5468                 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5469         }
5470
5471         /* copy in the ports for later check */
5472         stc.myport = sh->dest_port;
5473         stc.peerport = sh->src_port;
5474
5475         /*
5476          * If we wanted to honor cookie life extentions, we would add to
5477          * stc.cookie_life. For now we should NOT honor any extension
5478          */
5479         stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5480         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5481                 struct inpcb *in_inp;
5482
5483                 /* Its a V6 socket */
5484                 in_inp = (struct inpcb *)inp;
5485                 stc.ipv6_addr_legal = 1;
5486                 /* Now look at the binding flag to see if V4 will be legal */
5487                 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5488                         stc.ipv4_addr_legal = 1;
5489                 } else {
5490                         /* V4 addresses are NOT legal on the association */
5491                         stc.ipv4_addr_legal = 0;
5492                 }
5493         } else {
5494                 /* Its a V4 socket, no - V6 */
5495                 stc.ipv4_addr_legal = 1;
5496                 stc.ipv6_addr_legal = 0;
5497         }
5498
5499 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5500         stc.ipv4_scope = 1;
5501 #else
5502         stc.ipv4_scope = 0;
5503 #endif
5504         /* now for scope setup */
5505         memset((caddr_t)&store, 0, sizeof(store));
5506         memset((caddr_t)&store1, 0, sizeof(store1));
5507 #ifdef INET
5508         sin = &store.sin;
5509         to_sin = &store1.sin;
5510 #endif
5511 #ifdef INET6
5512         sin6 = &store.sin6;
5513         to_sin6 = &store1.sin6;
5514 #endif
5515         iph = mtod(init_pkt, struct ip *);
5516         /* establish the to_addr's */
5517         switch (iph->ip_v) {
5518 #ifdef INET
5519         case IPVERSION:
5520                 to_sin->sin_port = sh->dest_port;
5521                 to_sin->sin_family = AF_INET;
5522                 to_sin->sin_len = sizeof(struct sockaddr_in);
5523                 to_sin->sin_addr = iph->ip_dst;
5524                 break;
5525 #endif
5526 #ifdef INET6
5527         case IPV6_VERSION >> 4:
5528                 ip6 = mtod(init_pkt, struct ip6_hdr *);
5529                 to_sin6->sin6_addr = ip6->ip6_dst;
5530                 to_sin6->sin6_scope_id = 0;
5531                 to_sin6->sin6_port = sh->dest_port;
5532                 to_sin6->sin6_family = AF_INET6;
5533                 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5534                 break;
5535 #endif
5536         default:
5537                 goto do_a_abort;
5538                 break;
5539         };
5540
5541         if (net == NULL) {
5542                 to = (struct sockaddr *)&store;
5543                 switch (iph->ip_v) {
5544 #ifdef INET
5545                 case IPVERSION:
5546                         {
5547                                 sin->sin_family = AF_INET;
5548                                 sin->sin_len = sizeof(struct sockaddr_in);
5549                                 sin->sin_port = sh->src_port;
5550                                 sin->sin_addr = iph->ip_src;
5551                                 /* lookup address */
5552                                 stc.address[0] = sin->sin_addr.s_addr;
5553                                 stc.address[1] = 0;
5554                                 stc.address[2] = 0;
5555                                 stc.address[3] = 0;
5556                                 stc.addr_type = SCTP_IPV4_ADDRESS;
5557                                 /* local from address */
5558                                 stc.laddress[0] = to_sin->sin_addr.s_addr;
5559                                 stc.laddress[1] = 0;
5560                                 stc.laddress[2] = 0;
5561                                 stc.laddress[3] = 0;
5562                                 stc.laddr_type = SCTP_IPV4_ADDRESS;
5563                                 /* scope_id is only for v6 */
5564                                 stc.scope_id = 0;
5565 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5566                                 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5567                                         stc.ipv4_scope = 1;
5568                                 }
5569 #else
5570                                 stc.ipv4_scope = 1;
5571 #endif                          /* SCTP_DONT_DO_PRIVADDR_SCOPE */
5572                                 /* Must use the address in this case */
5573                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5574                                         stc.loopback_scope = 1;
5575                                         stc.ipv4_scope = 1;
5576                                         stc.site_scope = 1;
5577                                         stc.local_scope = 0;
5578                                 }
5579                                 break;
5580                         }
5581 #endif
5582 #ifdef INET6
5583                 case IPV6_VERSION >> 4:
5584                         {
5585                                 ip6 = mtod(init_pkt, struct ip6_hdr *);
5586                                 sin6->sin6_family = AF_INET6;
5587                                 sin6->sin6_len = sizeof(struct sockaddr_in6);
5588                                 sin6->sin6_port = sh->src_port;
5589                                 sin6->sin6_addr = ip6->ip6_src;
5590                                 /* lookup address */
5591                                 memcpy(&stc.address, &sin6->sin6_addr,
5592                                     sizeof(struct in6_addr));
5593                                 sin6->sin6_scope_id = 0;
5594                                 stc.addr_type = SCTP_IPV6_ADDRESS;
5595                                 stc.scope_id = 0;
5596                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5597                                         /*
5598                                          * FIX ME: does this have scope from
5599                                          * rcvif?
5600                                          */
5601                                         (void)sa6_recoverscope(sin6);
5602                                         stc.scope_id = sin6->sin6_scope_id;
5603                                         sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5604                                         stc.loopback_scope = 1;
5605                                         stc.local_scope = 0;
5606                                         stc.site_scope = 1;
5607                                         stc.ipv4_scope = 1;
5608                                 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5609                                         /*
5610                                          * If the new destination is a
5611                                          * LINK_LOCAL we must have common
5612                                          * both site and local scope. Don't
5613                                          * set local scope though since we
5614                                          * must depend on the source to be
5615                                          * added implicitly. We cannot
5616                                          * assure just because we share one
5617                                          * link that all links are common.
5618                                          */
5619                                         stc.local_scope = 0;
5620                                         stc.site_scope = 1;
5621                                         stc.ipv4_scope = 1;
5622                                         /*
5623                                          * we start counting for the private
5624                                          * address stuff at 1. since the
5625                                          * link local we source from won't
5626                                          * show up in our scoped count.
5627                                          */
5628                                         cnt_inits_to = 1;
5629                                         /*
5630                                          * pull out the scope_id from
5631                                          * incoming pkt
5632                                          */
5633                                         /*
5634                                          * FIX ME: does this have scope from
5635                                          * rcvif?
5636                                          */
5637                                         (void)sa6_recoverscope(sin6);
5638                                         stc.scope_id = sin6->sin6_scope_id;
5639                                         sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5640                                 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5641                                         /*
5642                                          * If the new destination is
5643                                          * SITE_LOCAL then we must have site
5644                                          * scope in common.
5645                                          */
5646                                         stc.site_scope = 1;
5647                                 }
5648                                 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5649                                 stc.laddr_type = SCTP_IPV6_ADDRESS;
5650                                 break;
5651                         }
5652 #endif
5653                 default:
5654                         /* TSNH */
5655                         goto do_a_abort;
5656                         break;
5657                 }
5658         } else {
5659                 /* set the scope per the existing tcb */
5660
5661 #ifdef INET6
5662                 struct sctp_nets *lnet;
5663
5664 #endif
5665
5666                 stc.loopback_scope = asoc->loopback_scope;
5667                 stc.ipv4_scope = asoc->ipv4_local_scope;
5668                 stc.site_scope = asoc->site_scope;
5669                 stc.local_scope = asoc->local_scope;
5670 #ifdef INET6
5671                 /* Why do we not consider IPv4 LL addresses? */
5672                 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5673                         if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5674                                 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5675                                         /*
5676                                          * if we have a LL address, start
5677                                          * counting at 1.
5678                                          */
5679                                         cnt_inits_to = 1;
5680                                 }
5681                         }
5682                 }
5683 #endif
5684                 /* use the net pointer */
5685                 to = (struct sockaddr *)&net->ro._l_addr;
5686                 switch (to->sa_family) {
5687 #ifdef INET
5688                 case AF_INET:
5689                         sin = (struct sockaddr_in *)to;
5690                         stc.address[0] = sin->sin_addr.s_addr;
5691                         stc.address[1] = 0;
5692                         stc.address[2] = 0;
5693                         stc.address[3] = 0;
5694                         stc.addr_type = SCTP_IPV4_ADDRESS;
5695                         if (net->src_addr_selected == 0) {
5696                                 /*
5697                                  * strange case here, the INIT should have
5698                                  * did the selection.
5699                                  */
5700                                 net->ro._s_addr = sctp_source_address_selection(inp,
5701                                     stcb, (sctp_route_t *) & net->ro,
5702                                     net, 0, vrf_id);
5703                                 if (net->ro._s_addr == NULL)
5704                                         return;
5705
5706                                 net->src_addr_selected = 1;
5707
5708                         }
5709                         stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5710                         stc.laddress[1] = 0;
5711                         stc.laddress[2] = 0;
5712                         stc.laddress[3] = 0;
5713                         stc.laddr_type = SCTP_IPV4_ADDRESS;
5714                         /* scope_id is only for v6 */
5715                         stc.scope_id = 0;
5716                         break;
5717 #endif
5718 #ifdef INET6
5719                 case AF_INET6:
5720                         sin6 = (struct sockaddr_in6 *)to;
5721                         memcpy(&stc.address, &sin6->sin6_addr,
5722                             sizeof(struct in6_addr));
5723                         stc.addr_type = SCTP_IPV6_ADDRESS;
5724                         stc.scope_id = sin6->sin6_scope_id;
5725                         if (net->src_addr_selected == 0) {
5726                                 /*
5727                                  * strange case here, the INIT should have
5728                                  * did the selection.
5729                                  */
5730                                 net->ro._s_addr = sctp_source_address_selection(inp,
5731                                     stcb, (sctp_route_t *) & net->ro,
5732                                     net, 0, vrf_id);
5733                                 if (net->ro._s_addr == NULL)
5734                                         return;
5735
5736                                 net->src_addr_selected = 1;
5737                         }
5738                         memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5739                             sizeof(struct in6_addr));
5740                         stc.laddr_type = SCTP_IPV6_ADDRESS;
5741                         break;
5742 #endif
5743                 }
5744         }
5745         /* Now lets put the SCTP header in place */
5746         initack = mtod(m, struct sctp_init_ack_chunk *);
5747         /* Save it off for quick ref */
5748         stc.peers_vtag = init_chk->init.initiate_tag;
5749         /* who are we */
5750         memcpy(stc.identification, SCTP_VERSION_STRING,
5751             min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5752         memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
5753         /* now the chunk header */
5754         initack->ch.chunk_type = SCTP_INITIATION_ACK;
5755         initack->ch.chunk_flags = 0;
5756         /* fill in later from mbuf we build */
5757         initack->ch.chunk_length = 0;
5758         /* place in my tag */
5759         if ((asoc != NULL) &&
5760             ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5761             (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5762             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5763                 /* re-use the v-tags and init-seq here */
5764                 initack->init.initiate_tag = htonl(asoc->my_vtag);
5765                 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5766         } else {
5767                 uint32_t vtag, itsn;
5768
5769                 if (hold_inp_lock) {
5770                         SCTP_INP_INCR_REF(inp);
5771                         SCTP_INP_RUNLOCK(inp);
5772                 }
5773                 if (asoc) {
5774                         atomic_add_int(&asoc->refcnt, 1);
5775                         SCTP_TCB_UNLOCK(stcb);
5776         new_tag:
5777                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5778                         if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5779                                 /*
5780                                  * Got a duplicate vtag on some guy behind a
5781                                  * nat make sure we don't use it.
5782                                  */
5783                                 goto new_tag;
5784                         }
5785                         initack->init.initiate_tag = htonl(vtag);
5786                         /* get a TSN to use too */
5787                         itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5788                         initack->init.initial_tsn = htonl(itsn);
5789                         SCTP_TCB_LOCK(stcb);
5790                         atomic_add_int(&asoc->refcnt, -1);
5791                 } else {
5792                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5793                         initack->init.initiate_tag = htonl(vtag);
5794                         /* get a TSN to use too */
5795                         initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5796                 }
5797                 if (hold_inp_lock) {
5798                         SCTP_INP_RLOCK(inp);
5799                         SCTP_INP_DECR_REF(inp);
5800                 }
5801         }
5802         /* save away my tag to */
5803         stc.my_vtag = initack->init.initiate_tag;
5804
5805         /* set up some of the credits. */
5806         so = inp->sctp_socket;
5807         if (so == NULL) {
5808                 /* memory problem */
5809                 sctp_m_freem(m);
5810                 return;
5811         } else {
5812                 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5813         }
5814         /* set what I want */
5815         his_limit = ntohs(init_chk->init.num_inbound_streams);
5816         /* choose what I want */
5817         if (asoc != NULL) {
5818                 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5819                         i_want = asoc->streamoutcnt;
5820                 } else {
5821                         i_want = inp->sctp_ep.pre_open_stream_count;
5822                 }
5823         } else {
5824                 i_want = inp->sctp_ep.pre_open_stream_count;
5825         }
5826         if (his_limit < i_want) {
5827                 /* I Want more :< */
5828                 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5829         } else {
5830                 /* I can have what I want :> */
5831                 initack->init.num_outbound_streams = htons(i_want);
5832         }
5833         /* tell him his limt. */
5834         initack->init.num_inbound_streams =
5835             htons(inp->sctp_ep.max_open_streams_intome);
5836
5837         /* adaptation layer indication parameter */
5838         ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5839         ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5840         ali->ph.param_length = htons(sizeof(*ali));
5841         ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5842         SCTP_BUF_LEN(m) += sizeof(*ali);
5843         ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5844
5845         /* ECN parameter */
5846         if (((asoc != NULL) && (asoc->ecn_allowed == 1)) ||
5847             (inp->sctp_ecn_enable == 1)) {
5848                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5849                 ecn->ph.param_length = htons(sizeof(*ecn));
5850                 SCTP_BUF_LEN(m) += sizeof(*ecn);
5851
5852                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5853                     sizeof(*ecn));
5854         } else {
5855                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5856         }
5857         /* And now tell the peer we do  pr-sctp */
5858         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5859         prsctp->ph.param_length = htons(sizeof(*prsctp));
5860         SCTP_BUF_LEN(m) += sizeof(*prsctp);
5861         if (nat_friendly) {
5862                 /* Add NAT friendly parameter */
5863                 struct sctp_paramhdr *ph;
5864
5865                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5866                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5867                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
5868                 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5869         }
5870         /* And now tell the peer we do all the extensions */
5871         pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5872         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5873         num_ext = 0;
5874         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5875         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5876         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5877         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5878         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5879         if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5880                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5881         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5882                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5883         p_len = sizeof(*pr_supported) + num_ext;
5884         pr_supported->ph.param_length = htons(p_len);
5885         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5886         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5887
5888         /* add authentication parameters */
5889         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5890                 struct sctp_auth_random *randp;
5891                 struct sctp_auth_hmac_algo *hmacs;
5892                 struct sctp_auth_chunk_list *chunks;
5893                 uint16_t random_len;
5894
5895                 /* generate and add RANDOM parameter */
5896                 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5897                 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5898                 randp->ph.param_type = htons(SCTP_RANDOM);
5899                 p_len = sizeof(*randp) + random_len;
5900                 randp->ph.param_length = htons(p_len);
5901                 SCTP_READ_RANDOM(randp->random_data, random_len);
5902                 /* zero out any padding required */
5903                 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5904                 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5905
5906                 /* add HMAC_ALGO parameter */
5907                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5908                 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5909                     (uint8_t *) hmacs->hmac_ids);
5910                 if (p_len > 0) {
5911                         p_len += sizeof(*hmacs);
5912                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5913                         hmacs->ph.param_length = htons(p_len);
5914                         /* zero out any padding required */
5915                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5916                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5917                 }
5918                 /* add CHUNKS parameter */
5919                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5920                 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5921                     chunks->chunk_types);
5922                 if (p_len > 0) {
5923                         p_len += sizeof(*chunks);
5924                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5925                         chunks->ph.param_length = htons(p_len);
5926                         /* zero out any padding required */
5927                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5928                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5929                 }
5930         }
5931         m_at = m;
5932         /* now the addresses */
5933         {
5934                 struct sctp_scoping scp;
5935
5936                 /*
5937                  * To optimize this we could put the scoping stuff into a
5938                  * structure and remove the individual uint8's from the stc
5939                  * structure. Then we could just sifa in the address within
5940                  * the stc.. but for now this is a quick hack to get the
5941                  * address stuff teased apart.
5942                  */
5943                 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5944                 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5945                 scp.loopback_scope = stc.loopback_scope;
5946                 scp.ipv4_local_scope = stc.ipv4_scope;
5947                 scp.local_scope = stc.local_scope;
5948                 scp.site_scope = stc.site_scope;
5949                 m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to);
5950         }
5951
5952         /* tack on the operational error if present */
5953         if (op_err) {
5954                 struct mbuf *ol;
5955                 int llen;
5956
5957                 llen = 0;
5958                 ol = op_err;
5959                 while (ol) {
5960                         llen += SCTP_BUF_LEN(ol);
5961                         ol = SCTP_BUF_NEXT(ol);
5962                 }
5963                 if (llen % 4) {
5964                         /* must add a pad to the param */
5965                         uint32_t cpthis = 0;
5966                         int padlen;
5967
5968                         padlen = 4 - (llen % 4);
5969                         m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5970                 }
5971                 while (SCTP_BUF_NEXT(m_at) != NULL) {
5972                         m_at = SCTP_BUF_NEXT(m_at);
5973                 }
5974                 SCTP_BUF_NEXT(m_at) = op_err;
5975                 while (SCTP_BUF_NEXT(m_at) != NULL) {
5976                         m_at = SCTP_BUF_NEXT(m_at);
5977                 }
5978         }
5979         /* pre-calulate the size and update pkt header and chunk header */
5980         p_len = 0;
5981         for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5982                 p_len += SCTP_BUF_LEN(m_tmp);
5983                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5984                         /* m_tmp should now point to last one */
5985                         break;
5986                 }
5987         }
5988
5989         /* Now we must build a cookie */
5990         m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5991         if (m_cookie == NULL) {
5992                 /* memory problem */
5993                 sctp_m_freem(m);
5994                 return;
5995         }
5996         /* Now append the cookie to the end and update the space/size */
5997         SCTP_BUF_NEXT(m_tmp) = m_cookie;
5998
5999         for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6000                 p_len += SCTP_BUF_LEN(m_tmp);
6001                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6002                         /* m_tmp should now point to last one */
6003                         mp_last = m_tmp;
6004                         break;
6005                 }
6006         }
6007         /*
6008          * Place in the size, but we don't include the last pad (if any) in
6009          * the INIT-ACK.
6010          */
6011         initack->ch.chunk_length = htons(p_len);
6012
6013         /*
6014          * Time to sign the cookie, we don't sign over the cookie signature
6015          * though thus we set trailer.
6016          */
6017         (void)sctp_hmac_m(SCTP_HMAC,
6018             (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
6019             SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
6020             (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
6021         /*
6022          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6023          * here since the timer will drive a retranmission.
6024          */
6025         padval = p_len % 4;
6026         if ((padval) && (mp_last)) {
6027                 /* see my previous comments on mp_last */
6028                 int ret;
6029
6030                 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
6031                 if (ret) {
6032                         /* Houston we have a problem, no space */
6033                         sctp_m_freem(m);
6034                         return;
6035                 }
6036                 p_len += padval;
6037         }
6038         if (stc.loopback_scope) {
6039                 over_addr = &store1;
6040         } else {
6041                 over_addr = NULL;
6042         }
6043
6044         (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
6045             0, NULL, 0,
6046             inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
6047             port, SCTP_SO_NOT_LOCKED, over_addr, init_pkt);
6048         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6049 }
6050
6051
6052 static void
6053 sctp_prune_prsctp(struct sctp_tcb *stcb,
6054     struct sctp_association *asoc,
6055     struct sctp_sndrcvinfo *srcv,
6056     int dataout)
6057 {
6058         int freed_spc = 0;
6059         struct sctp_tmit_chunk *chk, *nchk;
6060
6061         SCTP_TCB_LOCK_ASSERT(stcb);
6062         if ((asoc->peer_supports_prsctp) &&
6063             (asoc->sent_queue_cnt_removeable > 0)) {
6064                 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6065                         /*
6066                          * Look for chunks marked with the PR_SCTP flag AND
6067                          * the buffer space flag. If the one being sent is
6068                          * equal or greater priority then purge the old one
6069                          * and free some space.
6070                          */
6071                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6072                                 /*
6073                                  * This one is PR-SCTP AND buffer space
6074                                  * limited type
6075                                  */
6076                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
6077                                         /*
6078                                          * Lower numbers equates to higher
6079                                          * priority so if the one we are
6080                                          * looking at has a larger or equal
6081                                          * priority we want to drop the data
6082                                          * and NOT retransmit it.
6083                                          */
6084                                         if (chk->data) {
6085                                                 /*
6086                                                  * We release the book_size
6087                                                  * if the mbuf is here
6088                                                  */
6089                                                 int ret_spc;
6090                                                 int cause;
6091
6092                                                 if (chk->sent > SCTP_DATAGRAM_UNSENT)
6093                                                         cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
6094                                                 else
6095                                                         cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
6096                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6097                                                     cause,
6098                                                     SCTP_SO_LOCKED);
6099                                                 freed_spc += ret_spc;
6100                                                 if (freed_spc >= dataout) {
6101                                                         return;
6102                                                 }
6103                                         }       /* if chunk was present */
6104                                 }       /* if of sufficent priority */
6105                         }       /* if chunk has enabled */
6106                 }               /* tailqforeach */
6107
6108                 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
6109                         /* Here we must move to the sent queue and mark */
6110                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6111                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
6112                                         if (chk->data) {
6113                                                 /*
6114                                                  * We release the book_size
6115                                                  * if the mbuf is here
6116                                                  */
6117                                                 int ret_spc;
6118
6119                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6120                                                     SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
6121                                                     SCTP_SO_LOCKED);
6122
6123                                                 freed_spc += ret_spc;
6124                                                 if (freed_spc >= dataout) {
6125                                                         return;
6126                                                 }
6127                                         }       /* end if chk->data */
6128                                 }       /* end if right class */
6129                         }       /* end if chk pr-sctp */
6130                 }               /* tailqforeachsafe (chk) */
6131         }                       /* if enabled in asoc */
6132 }
6133
6134 int
6135 sctp_get_frag_point(struct sctp_tcb *stcb,
6136     struct sctp_association *asoc)
6137 {
6138         int siz, ovh;
6139
6140         /*
6141          * For endpoints that have both v6 and v4 addresses we must reserve
6142          * room for the ipv6 header, for those that are only dealing with V4
6143          * we use a larger frag point.
6144          */
6145         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6146                 ovh = SCTP_MED_OVERHEAD;
6147         } else {
6148                 ovh = SCTP_MED_V4_OVERHEAD;
6149         }
6150
6151         if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
6152                 siz = asoc->smallest_mtu - ovh;
6153         else
6154                 siz = (stcb->asoc.sctp_frag_point - ovh);
6155         /*
6156          * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
6157          */
6158         /* A data chunk MUST fit in a cluster */
6159         /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
6160         /* } */
6161
6162         /* adjust for an AUTH chunk if DATA requires auth */
6163         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
6164                 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
6165
6166         if (siz % 4) {
6167                 /* make it an even word boundary please */
6168                 siz -= (siz % 4);
6169         }
6170         return (siz);
6171 }
6172
6173 static void
6174 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
6175 {
6176         sp->pr_sctp_on = 0;
6177         /*
6178          * We assume that the user wants PR_SCTP_TTL if the user provides a
6179          * positive lifetime but does not specify any PR_SCTP policy. This
6180          * is a BAD assumption and causes problems at least with the
6181          * U-Vancovers MPI folks. I will change this to be no policy means
6182          * NO PR-SCTP.
6183          */
6184         if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
6185                 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6186                 sp->pr_sctp_on = 1;
6187         } else {
6188                 return;
6189         }
6190         switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
6191         case CHUNK_FLAGS_PR_SCTP_BUF:
6192                 /*
6193                  * Time to live is a priority stored in tv_sec when doing
6194                  * the buffer drop thing.
6195                  */
6196                 sp->ts.tv_sec = sp->timetolive;
6197                 sp->ts.tv_usec = 0;
6198                 break;
6199         case CHUNK_FLAGS_PR_SCTP_TTL:
6200                 {
6201                         struct timeval tv;
6202
6203                         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6204                         tv.tv_sec = sp->timetolive / 1000;
6205                         tv.tv_usec = (sp->timetolive * 1000) % 1000000;
6206                         /*
6207                          * TODO sctp_constants.h needs alternative time
6208                          * macros when _KERNEL is undefined.
6209                          */
6210                         timevaladd(&sp->ts, &tv);
6211                 }
6212                 break;
6213         case CHUNK_FLAGS_PR_SCTP_RTX:
6214                 /*
6215                  * Time to live is a the number or retransmissions stored in
6216                  * tv_sec.
6217                  */
6218                 sp->ts.tv_sec = sp->timetolive;
6219                 sp->ts.tv_usec = 0;
6220                 break;
6221         default:
6222                 SCTPDBG(SCTP_DEBUG_USRREQ1,
6223                     "Unknown PR_SCTP policy %u.\n",
6224                     PR_SCTP_POLICY(sp->sinfo_flags));
6225                 break;
6226         }
6227 }
6228
6229 static int
6230 sctp_msg_append(struct sctp_tcb *stcb,
6231     struct sctp_nets *net,
6232     struct mbuf *m,
6233     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
6234 {
6235         int error = 0, holds_lock;
6236         struct mbuf *at;
6237         struct sctp_stream_queue_pending *sp = NULL;
6238         struct sctp_stream_out *strm;
6239
6240         /*
6241          * Given an mbuf chain, put it into the association send queue and
6242          * place it on the wheel
6243          */
6244         holds_lock = hold_stcb_lock;
6245         if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
6246                 /* Invalid stream number */
6247                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6248                 error = EINVAL;
6249                 goto out_now;
6250         }
6251         if ((stcb->asoc.stream_locked) &&
6252             (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
6253                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6254                 error = EINVAL;
6255                 goto out_now;
6256         }
6257         strm = &stcb->asoc.strmout[srcv->sinfo_stream];
6258         /* Now can we send this? */
6259         if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
6260             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6261             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
6262             (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
6263                 /* got data while shutting down */
6264                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
6265                 error = ECONNRESET;
6266                 goto out_now;
6267         }
6268         sctp_alloc_a_strmoq(stcb, sp);
6269         if (sp == NULL) {
6270                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6271                 error = ENOMEM;
6272                 goto out_now;
6273         }
6274         sp->sinfo_flags = srcv->sinfo_flags;
6275         sp->timetolive = srcv->sinfo_timetolive;
6276         sp->ppid = srcv->sinfo_ppid;
6277         sp->context = srcv->sinfo_context;
6278         sp->strseq = 0;
6279         if (sp->sinfo_flags & SCTP_ADDR_OVER) {
6280                 sp->net = net;
6281                 atomic_add_int(&sp->net->ref_count, 1);
6282         } else {
6283                 sp->net = NULL;
6284         }
6285         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6286         sp->stream = srcv->sinfo_stream;
6287         sp->msg_is_complete = 1;
6288         sp->sender_all_done = 1;
6289         sp->some_taken = 0;
6290         sp->data = m;
6291         sp->tail_mbuf = NULL;
6292         sctp_set_prsctp_policy(sp);
6293         /*
6294          * We could in theory (for sendall) sifa the length in, but we would
6295          * still have to hunt through the chain since we need to setup the
6296          * tail_mbuf
6297          */
6298         sp->length = 0;
6299         for (at = m; at; at = SCTP_BUF_NEXT(at)) {
6300                 if (SCTP_BUF_NEXT(at) == NULL)
6301                         sp->tail_mbuf = at;
6302                 sp->length += SCTP_BUF_LEN(at);
6303         }
6304         if (srcv->sinfo_keynumber_valid) {
6305                 sp->auth_keyid = srcv->sinfo_keynumber;
6306         } else {
6307                 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
6308         }
6309         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
6310                 sctp_auth_key_acquire(stcb, sp->auth_keyid);
6311                 sp->holds_key_ref = 1;
6312         }
6313         SCTP_TCB_SEND_LOCK(stcb);
6314         sctp_snd_sb_alloc(stcb, sp->length);
6315         atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
6316         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
6317         if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
6318                 sp->strseq = strm->next_sequence_sent;
6319                 strm->next_sequence_sent++;
6320         }
6321         stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1);
6322         m = NULL;
6323         SCTP_TCB_SEND_UNLOCK(stcb);
6324 out_now:
6325         if (m) {
6326                 sctp_m_freem(m);
6327         }
6328         return (error);
6329 }
6330
6331
6332 static struct mbuf *
6333 sctp_copy_mbufchain(struct mbuf *clonechain,
6334     struct mbuf *outchain,
6335     struct mbuf **endofchain,
6336     int can_take_mbuf,
6337     int sizeofcpy,
6338     uint8_t copy_by_ref)
6339 {
6340         struct mbuf *m;
6341         struct mbuf *appendchain;
6342         caddr_t cp;
6343         int len;
6344
6345         if (endofchain == NULL) {
6346                 /* error */
6347 error_out:
6348                 if (outchain)
6349                         sctp_m_freem(outchain);
6350                 return (NULL);
6351         }
6352         if (can_take_mbuf) {
6353                 appendchain = clonechain;
6354         } else {
6355                 if (!copy_by_ref &&
6356                     (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
6357                     ) {
6358                         /* Its not in a cluster */
6359                         if (*endofchain == NULL) {
6360                                 /* lets get a mbuf cluster */
6361                                 if (outchain == NULL) {
6362                                         /* This is the general case */
6363                         new_mbuf:
6364                                         outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6365                                         if (outchain == NULL) {
6366                                                 goto error_out;
6367                                         }
6368                                         SCTP_BUF_LEN(outchain) = 0;
6369                                         *endofchain = outchain;
6370                                         /* get the prepend space */
6371                                         SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6372                                 } else {
6373                                         /*
6374                                          * We really should not get a NULL
6375                                          * in endofchain
6376                                          */
6377                                         /* find end */
6378                                         m = outchain;
6379                                         while (m) {
6380                                                 if (SCTP_BUF_NEXT(m) == NULL) {
6381                                                         *endofchain = m;
6382                                                         break;
6383                                                 }
6384                                                 m = SCTP_BUF_NEXT(m);
6385                                         }
6386                                         /* sanity */
6387                                         if (*endofchain == NULL) {
6388                                                 /*
6389                                                  * huh, TSNH XXX maybe we
6390                                                  * should panic
6391                                                  */
6392                                                 sctp_m_freem(outchain);
6393                                                 goto new_mbuf;
6394                                         }
6395                                 }
6396                                 /* get the new end of length */
6397                                 len = M_TRAILINGSPACE(*endofchain);
6398                         } else {
6399                                 /* how much is left at the end? */
6400                                 len = M_TRAILINGSPACE(*endofchain);
6401                         }
6402                         /* Find the end of the data, for appending */
6403                         cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6404
6405                         /* Now lets copy it out */
6406                         if (len >= sizeofcpy) {
6407                                 /* It all fits, copy it in */
6408                                 m_copydata(clonechain, 0, sizeofcpy, cp);
6409                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6410                         } else {
6411                                 /* fill up the end of the chain */
6412                                 if (len > 0) {
6413                                         m_copydata(clonechain, 0, len, cp);
6414                                         SCTP_BUF_LEN((*endofchain)) += len;
6415                                         /* now we need another one */
6416                                         sizeofcpy -= len;
6417                                 }
6418                                 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6419                                 if (m == NULL) {
6420                                         /* We failed */
6421                                         goto error_out;
6422                                 }
6423                                 SCTP_BUF_NEXT((*endofchain)) = m;
6424                                 *endofchain = m;
6425                                 cp = mtod((*endofchain), caddr_t);
6426                                 m_copydata(clonechain, len, sizeofcpy, cp);
6427                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6428                         }
6429                         return (outchain);
6430                 } else {
6431                         /* copy the old fashion way */
6432                         appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6433 #ifdef SCTP_MBUF_LOGGING
6434                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6435                                 struct mbuf *mat;
6436
6437                                 mat = appendchain;
6438                                 while (mat) {
6439                                         if (SCTP_BUF_IS_EXTENDED(mat)) {
6440                                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6441                                         }
6442                                         mat = SCTP_BUF_NEXT(mat);
6443                                 }
6444                         }
6445 #endif
6446                 }
6447         }
6448         if (appendchain == NULL) {
6449                 /* error */
6450                 if (outchain)
6451                         sctp_m_freem(outchain);
6452                 return (NULL);
6453         }
6454         if (outchain) {
6455                 /* tack on to the end */
6456                 if (*endofchain != NULL) {
6457                         SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6458                 } else {
6459                         m = outchain;
6460                         while (m) {
6461                                 if (SCTP_BUF_NEXT(m) == NULL) {
6462                                         SCTP_BUF_NEXT(m) = appendchain;
6463                                         break;
6464                                 }
6465                                 m = SCTP_BUF_NEXT(m);
6466                         }
6467                 }
6468                 /*
6469                  * save off the end and update the end-chain postion
6470                  */
6471                 m = appendchain;
6472                 while (m) {
6473                         if (SCTP_BUF_NEXT(m) == NULL) {
6474                                 *endofchain = m;
6475                                 break;
6476                         }
6477                         m = SCTP_BUF_NEXT(m);
6478                 }
6479                 return (outchain);
6480         } else {
6481                 /* save off the end and update the end-chain postion */
6482                 m = appendchain;
6483                 while (m) {
6484                         if (SCTP_BUF_NEXT(m) == NULL) {
6485                                 *endofchain = m;
6486                                 break;
6487                         }
6488                         m = SCTP_BUF_NEXT(m);
6489                 }
6490                 return (appendchain);
6491         }
6492 }
6493
6494 int
6495 sctp_med_chunk_output(struct sctp_inpcb *inp,
6496     struct sctp_tcb *stcb,
6497     struct sctp_association *asoc,
6498     int *num_out,
6499     int *reason_code,
6500     int control_only, int from_where,
6501     struct timeval *now, int *now_filled, int frag_point, int so_locked
6502 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6503     SCTP_UNUSED
6504 #endif
6505 );
6506
6507 static void
6508 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6509     uint32_t val)
6510 {
6511         struct sctp_copy_all *ca;
6512         struct mbuf *m;
6513         int ret = 0;
6514         int added_control = 0;
6515         int un_sent, do_chunk_output = 1;
6516         struct sctp_association *asoc;
6517         struct sctp_nets *net;
6518
6519         ca = (struct sctp_copy_all *)ptr;
6520         if (ca->m == NULL) {
6521                 return;
6522         }
6523         if (ca->inp != inp) {
6524                 /* TSNH */
6525                 return;
6526         }
6527         if ((ca->m) && ca->sndlen) {
6528                 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6529                 if (m == NULL) {
6530                         /* can't copy so we are done */
6531                         ca->cnt_failed++;
6532                         return;
6533                 }
6534 #ifdef SCTP_MBUF_LOGGING
6535                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6536                         struct mbuf *mat;
6537
6538                         mat = m;
6539                         while (mat) {
6540                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
6541                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6542                                 }
6543                                 mat = SCTP_BUF_NEXT(mat);
6544                         }
6545                 }
6546 #endif
6547         } else {
6548                 m = NULL;
6549         }
6550         SCTP_TCB_LOCK_ASSERT(stcb);
6551         if (stcb->asoc.alternate) {
6552                 net = stcb->asoc.alternate;
6553         } else {
6554                 net = stcb->asoc.primary_destination;
6555         }
6556         if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6557                 /* Abort this assoc with m as the user defined reason */
6558                 if (m) {
6559                         struct sctp_paramhdr *ph;
6560
6561                         SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6562                         if (m) {
6563                                 ph = mtod(m, struct sctp_paramhdr *);
6564                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6565                                 ph->param_length = htons(ca->sndlen);
6566                         }
6567                         /*
6568                          * We add one here to keep the assoc from
6569                          * dis-appearing on us.
6570                          */
6571                         atomic_add_int(&stcb->asoc.refcnt, 1);
6572                         sctp_abort_an_association(inp, stcb,
6573                             SCTP_RESPONSE_TO_USER_REQ,
6574                             m, SCTP_SO_NOT_LOCKED);
6575                         /*
6576                          * sctp_abort_an_association calls sctp_free_asoc()
6577                          * free association will NOT free it since we
6578                          * incremented the refcnt .. we do this to prevent
6579                          * it being freed and things getting tricky since we
6580                          * could end up (from free_asoc) calling inpcb_free
6581                          * which would get a recursive lock call to the
6582                          * iterator lock.. But as a consequence of that the
6583                          * stcb will return to us un-locked.. since
6584                          * free_asoc returns with either no TCB or the TCB
6585                          * unlocked, we must relock.. to unlock in the
6586                          * iterator timer :-0
6587                          */
6588                         SCTP_TCB_LOCK(stcb);
6589                         atomic_add_int(&stcb->asoc.refcnt, -1);
6590                         goto no_chunk_output;
6591                 }
6592         } else {
6593                 if (m) {
6594                         ret = sctp_msg_append(stcb, net, m,
6595                             &ca->sndrcv, 1);
6596                 }
6597                 asoc = &stcb->asoc;
6598                 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6599                         /* shutdown this assoc */
6600                         int cnt;
6601
6602                         cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED);
6603
6604                         if (TAILQ_EMPTY(&asoc->send_queue) &&
6605                             TAILQ_EMPTY(&asoc->sent_queue) &&
6606                             (cnt == 0)) {
6607                                 if (asoc->locked_on_sending) {
6608                                         goto abort_anyway;
6609                                 }
6610                                 /*
6611                                  * there is nothing queued to send, so I'm
6612                                  * done...
6613                                  */
6614                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6615                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6616                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6617                                         /*
6618                                          * only send SHUTDOWN the first time
6619                                          * through
6620                                          */
6621                                         sctp_send_shutdown(stcb, net);
6622                                         if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6623                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6624                                         }
6625                                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6626                                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6627                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6628                                             net);
6629                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6630                                             asoc->primary_destination);
6631                                         added_control = 1;
6632                                         do_chunk_output = 0;
6633                                 }
6634                         } else {
6635                                 /*
6636                                  * we still got (or just got) data to send,
6637                                  * so set SHUTDOWN_PENDING
6638                                  */
6639                                 /*
6640                                  * XXX sockets draft says that SCTP_EOF
6641                                  * should be sent with no data.  currently,
6642                                  * we will allow user data to be sent first
6643                                  * and move to SHUTDOWN-PENDING
6644                                  */
6645                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6646                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6647                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6648                                         if (asoc->locked_on_sending) {
6649                                                 /*
6650                                                  * Locked to send out the
6651                                                  * data
6652                                                  */
6653                                                 struct sctp_stream_queue_pending *sp;
6654
6655                                                 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6656                                                 if (sp) {
6657                                                         if ((sp->length == 0) && (sp->msg_is_complete == 0))
6658                                                                 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6659                                                 }
6660                                         }
6661                                         asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6662                                         if (TAILQ_EMPTY(&asoc->send_queue) &&
6663                                             TAILQ_EMPTY(&asoc->sent_queue) &&
6664                                             (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6665                                 abort_anyway:
6666                                                 atomic_add_int(&stcb->asoc.refcnt, 1);
6667                                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
6668                                                     SCTP_RESPONSE_TO_USER_REQ,
6669                                                     NULL, SCTP_SO_NOT_LOCKED);
6670                                                 atomic_add_int(&stcb->asoc.refcnt, -1);
6671                                                 goto no_chunk_output;
6672                                         }
6673                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6674                                             asoc->primary_destination);
6675                                 }
6676                         }
6677
6678                 }
6679         }
6680         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6681             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6682
6683         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6684             (stcb->asoc.total_flight > 0) &&
6685             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6686             ) {
6687                 do_chunk_output = 0;
6688         }
6689         if (do_chunk_output)
6690                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6691         else if (added_control) {
6692                 int num_out = 0, reason = 0, now_filled = 0;
6693                 struct timeval now;
6694                 int frag_point;
6695
6696                 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6697                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6698                     &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6699         }
6700 no_chunk_output:
6701         if (ret) {
6702                 ca->cnt_failed++;
6703         } else {
6704                 ca->cnt_sent++;
6705         }
6706 }
6707
6708 static void
6709 sctp_sendall_completes(void *ptr, uint32_t val)
6710 {
6711         struct sctp_copy_all *ca;
6712
6713         ca = (struct sctp_copy_all *)ptr;
6714         /*
6715          * Do a notify here? Kacheong suggests that the notify be done at
6716          * the send time.. so you would push up a notification if any send
6717          * failed. Don't know if this is feasable since the only failures we
6718          * have is "memory" related and if you cannot get an mbuf to send
6719          * the data you surely can't get an mbuf to send up to notify the
6720          * user you can't send the data :->
6721          */
6722
6723         /* now free everything */
6724         sctp_m_freem(ca->m);
6725         SCTP_FREE(ca, SCTP_M_COPYAL);
6726 }
6727
6728
6729 #define MC_ALIGN(m, len) do {                                           \
6730         SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
6731 } while (0)
6732
6733
6734
6735 static struct mbuf *
6736 sctp_copy_out_all(struct uio *uio, int len)
6737 {
6738         struct mbuf *ret, *at;
6739         int left, willcpy, cancpy, error;
6740
6741         ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6742         if (ret == NULL) {
6743                 /* TSNH */
6744                 return (NULL);
6745         }
6746         left = len;
6747         SCTP_BUF_LEN(ret) = 0;
6748         /* save space for the data chunk header */
6749         cancpy = M_TRAILINGSPACE(ret);
6750         willcpy = min(cancpy, left);
6751         at = ret;
6752         while (left > 0) {
6753                 /* Align data to the end */
6754                 error = uiomove(mtod(at, caddr_t), willcpy, uio);
6755                 if (error) {
6756         err_out_now:
6757                         sctp_m_freem(at);
6758                         return (NULL);
6759                 }
6760                 SCTP_BUF_LEN(at) = willcpy;
6761                 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6762                 left -= willcpy;
6763                 if (left > 0) {
6764                         SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6765                         if (SCTP_BUF_NEXT(at) == NULL) {
6766                                 goto err_out_now;
6767                         }
6768                         at = SCTP_BUF_NEXT(at);
6769                         SCTP_BUF_LEN(at) = 0;
6770                         cancpy = M_TRAILINGSPACE(at);
6771                         willcpy = min(cancpy, left);
6772                 }
6773         }
6774         return (ret);
6775 }
6776
6777 static int
6778 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6779     struct sctp_sndrcvinfo *srcv)
6780 {
6781         int ret;
6782         struct sctp_copy_all *ca;
6783
6784         SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6785             SCTP_M_COPYAL);
6786         if (ca == NULL) {
6787                 sctp_m_freem(m);
6788                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6789                 return (ENOMEM);
6790         }
6791         memset(ca, 0, sizeof(struct sctp_copy_all));
6792
6793         ca->inp = inp;
6794         if (srcv) {
6795                 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6796         }
6797         /*
6798          * take off the sendall flag, it would be bad if we failed to do
6799          * this :-0
6800          */
6801         ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6802         /* get length and mbuf chain */
6803         if (uio) {
6804                 ca->sndlen = uio->uio_resid;
6805                 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6806                 if (ca->m == NULL) {
6807                         SCTP_FREE(ca, SCTP_M_COPYAL);
6808                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6809                         return (ENOMEM);
6810                 }
6811         } else {
6812                 /* Gather the length of the send */
6813                 struct mbuf *mat;
6814
6815                 mat = m;
6816                 ca->sndlen = 0;
6817                 while (m) {
6818                         ca->sndlen += SCTP_BUF_LEN(m);
6819                         m = SCTP_BUF_NEXT(m);
6820                 }
6821                 ca->m = mat;
6822         }
6823         ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6824             SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6825             SCTP_ASOC_ANY_STATE,
6826             (void *)ca, 0,
6827             sctp_sendall_completes, inp, 1);
6828         if (ret) {
6829                 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6830                 SCTP_FREE(ca, SCTP_M_COPYAL);
6831                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6832                 return (EFAULT);
6833         }
6834         return (0);
6835 }
6836
6837
6838 void
6839 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6840 {
6841         struct sctp_tmit_chunk *chk, *nchk;
6842
6843         TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6844                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6845                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6846                         if (chk->data) {
6847                                 sctp_m_freem(chk->data);
6848                                 chk->data = NULL;
6849                         }
6850                         asoc->ctrl_queue_cnt--;
6851                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6852                 }
6853         }
6854 }
6855
6856 void
6857 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6858 {
6859         struct sctp_association *asoc;
6860         struct sctp_tmit_chunk *chk, *nchk;
6861         struct sctp_asconf_chunk *acp;
6862
6863         asoc = &stcb->asoc;
6864         TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6865                 /* find SCTP_ASCONF chunk in queue */
6866                 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6867                         if (chk->data) {
6868                                 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6869                                 if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6870                                         /* Not Acked yet */
6871                                         break;
6872                                 }
6873                         }
6874                         TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6875                         if (chk->data) {
6876                                 sctp_m_freem(chk->data);
6877                                 chk->data = NULL;
6878                         }
6879                         asoc->ctrl_queue_cnt--;
6880                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6881                 }
6882         }
6883 }
6884
6885
6886 static void
6887 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6888     struct sctp_association *asoc,
6889     struct sctp_tmit_chunk **data_list,
6890     int bundle_at,
6891     struct sctp_nets *net)
6892 {
6893         int i;
6894         struct sctp_tmit_chunk *tp1;
6895
6896         for (i = 0; i < bundle_at; i++) {
6897                 /* off of the send queue */
6898                 TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
6899                 asoc->send_queue_cnt--;
6900                 if (i > 0) {
6901                         /*
6902                          * Any chunk NOT 0 you zap the time chunk 0 gets
6903                          * zapped or set based on if a RTO measurment is
6904                          * needed.
6905                          */
6906                         data_list[i]->do_rtt = 0;
6907                 }
6908                 /* record time */
6909                 data_list[i]->sent_rcv_time = net->last_sent_time;
6910                 data_list[i]->rec.data.cwnd_at_send = net->cwnd;
6911                 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6912                 if (data_list[i]->whoTo == NULL) {
6913                         data_list[i]->whoTo = net;
6914                         atomic_add_int(&net->ref_count, 1);
6915                 }
6916                 /* on to the sent queue */
6917                 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6918                 if ((tp1) && SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6919                         struct sctp_tmit_chunk *tpp;
6920
6921                         /* need to move back */
6922         back_up_more:
6923                         tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6924                         if (tpp == NULL) {
6925                                 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6926                                 goto all_done;
6927                         }
6928                         tp1 = tpp;
6929                         if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6930                                 goto back_up_more;
6931                         }
6932                         TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6933                 } else {
6934                         TAILQ_INSERT_TAIL(&asoc->sent_queue,
6935                             data_list[i],
6936                             sctp_next);
6937                 }
6938 all_done:
6939                 /* This does not lower until the cum-ack passes it */
6940                 asoc->sent_queue_cnt++;
6941                 if ((asoc->peers_rwnd <= 0) &&
6942                     (asoc->total_flight == 0) &&
6943                     (bundle_at == 1)) {
6944                         /* Mark the chunk as being a window probe */
6945                         SCTP_STAT_INCR(sctps_windowprobed);
6946                 }
6947 #ifdef SCTP_AUDITING_ENABLED
6948                 sctp_audit_log(0xC2, 3);
6949 #endif
6950                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6951                 data_list[i]->snd_count = 1;
6952                 data_list[i]->rec.data.chunk_was_revoked = 0;
6953                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6954                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6955                             data_list[i]->whoTo->flight_size,
6956                             data_list[i]->book_size,
6957                             (uintptr_t) data_list[i]->whoTo,
6958                             data_list[i]->rec.data.TSN_seq);
6959                 }
6960                 sctp_flight_size_increase(data_list[i]);
6961                 sctp_total_flight_increase(stcb, data_list[i]);
6962                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6963                         sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6964                             asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6965                 }
6966                 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6967                     (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6968                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6969                         /* SWS sender side engages */
6970                         asoc->peers_rwnd = 0;
6971                 }
6972         }
6973         if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
6974                 (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
6975         }
6976 }
6977
6978 static void
6979 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked
6980 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6981     SCTP_UNUSED
6982 #endif
6983 )
6984 {
6985         struct sctp_tmit_chunk *chk, *nchk;
6986
6987         TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6988                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6989                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
6990                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6991                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6992                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6993                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6994                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6995                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6996                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6997                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6998                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6999                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7000                         /* Stray chunks must be cleaned up */
7001         clean_up_anyway:
7002                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7003                         if (chk->data) {
7004                                 sctp_m_freem(chk->data);
7005                                 chk->data = NULL;
7006                         }
7007                         asoc->ctrl_queue_cnt--;
7008                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
7009                                 asoc->fwd_tsn_cnt--;
7010                         sctp_free_a_chunk(stcb, chk, so_locked);
7011                 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
7012                         /* special handling, we must look into the param */
7013                         if (chk != asoc->str_reset) {
7014                                 goto clean_up_anyway;
7015                         }
7016                 }
7017         }
7018 }
7019
7020
7021 static int
7022 sctp_can_we_split_this(struct sctp_tcb *stcb,
7023     uint32_t length,
7024     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
7025 {
7026         /*
7027          * Make a decision on if I should split a msg into multiple parts.
7028          * This is only asked of incomplete messages.
7029          */
7030         if (eeor_on) {
7031                 /*
7032                  * If we are doing EEOR we need to always send it if its the
7033                  * entire thing, since it might be all the guy is putting in
7034                  * the hopper.
7035                  */
7036                 if (goal_mtu >= length) {
7037                         /*-
7038                          * If we have data outstanding,
7039                          * we get another chance when the sack
7040                          * arrives to transmit - wait for more data
7041                          */
7042                         if (stcb->asoc.total_flight == 0) {
7043                                 /*
7044                                  * If nothing is in flight, we zero the
7045                                  * packet counter.
7046                                  */
7047                                 return (length);
7048                         }
7049                         return (0);
7050
7051                 } else {
7052                         /* You can fill the rest */
7053                         return (goal_mtu);
7054                 }
7055         }
7056         /*-
7057          * For those strange folk that make the send buffer
7058          * smaller than our fragmentation point, we can't
7059          * get a full msg in so we have to allow splitting.
7060          */
7061         if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
7062                 return (length);
7063         }
7064         if ((length <= goal_mtu) ||
7065             ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
7066                 /* Sub-optimial residual don't split in non-eeor mode. */
7067                 return (0);
7068         }
7069         /*
7070          * If we reach here length is larger than the goal_mtu. Do we wish
7071          * to split it for the sake of packet putting together?
7072          */
7073         if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
7074                 /* Its ok to split it */
7075                 return (min(goal_mtu, frag_point));
7076         }
7077         /* Nope, can't split */
7078         return (0);
7079
7080 }
7081
7082 static uint32_t
7083 sctp_move_to_outqueue(struct sctp_tcb *stcb,
7084     struct sctp_stream_out *strq,
7085     uint32_t goal_mtu,
7086     uint32_t frag_point,
7087     int *locked,
7088     int *giveup,
7089     int eeor_mode,
7090     int *bail,
7091     int so_locked
7092 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7093     SCTP_UNUSED
7094 #endif
7095 )
7096 {
7097         /* Move from the stream to the send_queue keeping track of the total */
7098         struct sctp_association *asoc;
7099         struct sctp_stream_queue_pending *sp;
7100         struct sctp_tmit_chunk *chk;
7101         struct sctp_data_chunk *dchkh;
7102         uint32_t to_move, length;
7103         uint8_t rcv_flags = 0;
7104         uint8_t some_taken;
7105         uint8_t send_lock_up = 0;
7106
7107         SCTP_TCB_LOCK_ASSERT(stcb);
7108         asoc = &stcb->asoc;
7109 one_more_time:
7110         /* sa_ignore FREED_MEMORY */
7111         sp = TAILQ_FIRST(&strq->outqueue);
7112         if (sp == NULL) {
7113                 *locked = 0;
7114                 if (send_lock_up == 0) {
7115                         SCTP_TCB_SEND_LOCK(stcb);
7116                         send_lock_up = 1;
7117                 }
7118                 sp = TAILQ_FIRST(&strq->outqueue);
7119                 if (sp) {
7120                         goto one_more_time;
7121                 }
7122                 if (strq->last_msg_incomplete) {
7123                         SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
7124                             strq->stream_no,
7125                             strq->last_msg_incomplete);
7126                         strq->last_msg_incomplete = 0;
7127                 }
7128                 to_move = 0;
7129                 if (send_lock_up) {
7130                         SCTP_TCB_SEND_UNLOCK(stcb);
7131                         send_lock_up = 0;
7132                 }
7133                 goto out_of;
7134         }
7135         if ((sp->msg_is_complete) && (sp->length == 0)) {
7136                 if (sp->sender_all_done) {
7137                         /*
7138                          * We are doing differed cleanup. Last time through
7139                          * when we took all the data the sender_all_done was
7140                          * not set.
7141                          */
7142                         if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
7143                                 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
7144                                 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7145                                     sp->sender_all_done,
7146                                     sp->length,
7147                                     sp->msg_is_complete,
7148                                     sp->put_last_out,
7149                                     send_lock_up);
7150                         }
7151                         if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
7152                                 SCTP_TCB_SEND_LOCK(stcb);
7153                                 send_lock_up = 1;
7154                         }
7155                         atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7156                         TAILQ_REMOVE(&strq->outqueue, sp, next);
7157                         stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
7158                         if (sp->net) {
7159                                 sctp_free_remote_addr(sp->net);
7160                                 sp->net = NULL;
7161                         }
7162                         if (sp->data) {
7163                                 sctp_m_freem(sp->data);
7164                                 sp->data = NULL;
7165                         }
7166                         sctp_free_a_strmoq(stcb, sp, so_locked);
7167                         /* we can't be locked to it */
7168                         *locked = 0;
7169                         stcb->asoc.locked_on_sending = NULL;
7170                         if (send_lock_up) {
7171                                 SCTP_TCB_SEND_UNLOCK(stcb);
7172                                 send_lock_up = 0;
7173                         }
7174                         /* back to get the next msg */
7175                         goto one_more_time;
7176                 } else {
7177                         /*
7178                          * sender just finished this but still holds a
7179                          * reference
7180                          */
7181                         *locked = 1;
7182                         *giveup = 1;
7183                         to_move = 0;
7184                         goto out_of;
7185                 }
7186         } else {
7187                 /* is there some to get */
7188                 if (sp->length == 0) {
7189                         /* no */
7190                         *locked = 1;
7191                         *giveup = 1;
7192                         to_move = 0;
7193                         goto out_of;
7194                 } else if (sp->discard_rest) {
7195                         if (send_lock_up == 0) {
7196                                 SCTP_TCB_SEND_LOCK(stcb);
7197                                 send_lock_up = 1;
7198                         }
7199                         /* Whack down the size */
7200                         atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
7201                         if ((stcb->sctp_socket != NULL) && \
7202                             ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7203                             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
7204                                 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
7205                         }
7206                         if (sp->data) {
7207                                 sctp_m_freem(sp->data);
7208                                 sp->data = NULL;
7209                                 sp->tail_mbuf = NULL;
7210                         }
7211                         sp->length = 0;
7212                         sp->some_taken = 1;
7213                         *locked = 1;
7214                         *giveup = 1;
7215                         to_move = 0;
7216                         goto out_of;
7217                 }
7218         }
7219         some_taken = sp->some_taken;
7220         if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7221                 sp->msg_is_complete = 1;
7222         }
7223 re_look:
7224         length = sp->length;
7225         if (sp->msg_is_complete) {
7226                 /* The message is complete */
7227                 to_move = min(length, frag_point);
7228                 if (to_move == length) {
7229                         /* All of it fits in the MTU */
7230                         if (sp->some_taken) {
7231                                 rcv_flags |= SCTP_DATA_LAST_FRAG;
7232                                 sp->put_last_out = 1;
7233                         } else {
7234                                 rcv_flags |= SCTP_DATA_NOT_FRAG;
7235                                 sp->put_last_out = 1;
7236                         }
7237                 } else {
7238                         /* Not all of it fits, we fragment */
7239                         if (sp->some_taken == 0) {
7240                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7241                         }
7242                         sp->some_taken = 1;
7243                 }
7244         } else {
7245                 to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
7246                 if (to_move) {
7247                         /*-
7248                          * We use a snapshot of length in case it
7249                          * is expanding during the compare.
7250                          */
7251                         uint32_t llen;
7252
7253                         llen = length;
7254                         if (to_move >= llen) {
7255                                 to_move = llen;
7256                                 if (send_lock_up == 0) {
7257                                         /*-
7258                                          * We are taking all of an incomplete msg
7259                                          * thus we need a send lock.
7260                                          */
7261                                         SCTP_TCB_SEND_LOCK(stcb);
7262                                         send_lock_up = 1;
7263                                         if (sp->msg_is_complete) {
7264                                                 /*
7265                                                  * the sender finished the
7266                                                  * msg
7267                                                  */
7268                                                 goto re_look;
7269                                         }
7270                                 }
7271                         }
7272                         if (sp->some_taken == 0) {
7273                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7274                                 sp->some_taken = 1;
7275                         }
7276                 } else {
7277                         /* Nothing to take. */
7278                         if (sp->some_taken) {
7279                                 *locked = 1;
7280                         }
7281                         *giveup = 1;
7282                         to_move = 0;
7283                         goto out_of;
7284                 }
7285         }
7286
7287         /* If we reach here, we can copy out a chunk */
7288         sctp_alloc_a_chunk(stcb, chk);
7289         if (chk == NULL) {
7290                 /* No chunk memory */
7291                 *giveup = 1;
7292                 to_move = 0;
7293                 goto out_of;
7294         }
7295         /*
7296          * Setup for unordered if needed by looking at the user sent info
7297          * flags.
7298          */
7299         if (sp->sinfo_flags & SCTP_UNORDERED) {
7300                 rcv_flags |= SCTP_DATA_UNORDERED;
7301         }
7302         if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
7303             ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
7304                 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7305         }
7306         /* clear out the chunk before setting up */
7307         memset(chk, 0, sizeof(*chk));
7308         chk->rec.data.rcv_flags = rcv_flags;
7309
7310         if (to_move >= length) {
7311                 /* we think we can steal the whole thing */
7312                 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
7313                         SCTP_TCB_SEND_LOCK(stcb);
7314                         send_lock_up = 1;
7315                 }
7316                 if (to_move < sp->length) {
7317                         /* bail, it changed */
7318                         goto dont_do_it;
7319                 }
7320                 chk->data = sp->data;
7321                 chk->last_mbuf = sp->tail_mbuf;
7322                 /* register the stealing */
7323                 sp->data = sp->tail_mbuf = NULL;
7324         } else {
7325                 struct mbuf *m;
7326
7327 dont_do_it:
7328                 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
7329                 chk->last_mbuf = NULL;
7330                 if (chk->data == NULL) {
7331                         sp->some_taken = some_taken;
7332                         sctp_free_a_chunk(stcb, chk, so_locked);
7333                         *bail = 1;
7334                         to_move = 0;
7335                         goto out_of;
7336                 }
7337 #ifdef SCTP_MBUF_LOGGING
7338                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7339                         struct mbuf *mat;
7340
7341                         mat = chk->data;
7342                         while (mat) {
7343                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
7344                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7345                                 }
7346                                 mat = SCTP_BUF_NEXT(mat);
7347                         }
7348                 }
7349 #endif
7350                 /* Pull off the data */
7351                 m_adj(sp->data, to_move);
7352                 /* Now lets work our way down and compact it */
7353                 m = sp->data;
7354                 while (m && (SCTP_BUF_LEN(m) == 0)) {
7355                         sp->data = SCTP_BUF_NEXT(m);
7356                         SCTP_BUF_NEXT(m) = NULL;
7357                         if (sp->tail_mbuf == m) {
7358                                 /*-
7359                                  * Freeing tail? TSNH since
7360                                  * we supposedly were taking less
7361                                  * than the sp->length.
7362                                  */
7363 #ifdef INVARIANTS
7364                                 panic("Huh, freing tail? - TSNH");
7365 #else
7366                                 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
7367                                 sp->tail_mbuf = sp->data = NULL;
7368                                 sp->length = 0;
7369 #endif
7370
7371                         }
7372                         sctp_m_free(m);
7373                         m = sp->data;
7374                 }
7375         }
7376         if (SCTP_BUF_IS_EXTENDED(chk->data)) {
7377                 chk->copy_by_ref = 1;
7378         } else {
7379                 chk->copy_by_ref = 0;
7380         }
7381         /*
7382          * get last_mbuf and counts of mb useage This is ugly but hopefully
7383          * its only one mbuf.
7384          */
7385         if (chk->last_mbuf == NULL) {
7386                 chk->last_mbuf = chk->data;
7387                 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7388                         chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7389                 }
7390         }
7391         if (to_move > length) {
7392                 /*- This should not happen either
7393                  * since we always lower to_move to the size
7394                  * of sp->length if its larger.
7395                  */
7396 #ifdef INVARIANTS
7397                 panic("Huh, how can to_move be larger?");
7398 #else
7399                 SCTP_PRINTF("Huh, how can to_move be larger?\n");
7400                 sp->length = 0;
7401 #endif
7402         } else {
7403                 atomic_subtract_int(&sp->length, to_move);
7404         }
7405         if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
7406                 /* Not enough room for a chunk header, get some */
7407                 struct mbuf *m;
7408
7409                 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
7410                 if (m == NULL) {
7411                         /*
7412                          * we're in trouble here. _PREPEND below will free
7413                          * all the data if there is no leading space, so we
7414                          * must put the data back and restore.
7415                          */
7416                         if (send_lock_up == 0) {
7417                                 SCTP_TCB_SEND_LOCK(stcb);
7418                                 send_lock_up = 1;
7419                         }
7420                         if (chk->data == NULL) {
7421                                 /* unsteal the data */
7422                                 sp->data = chk->data;
7423                                 sp->tail_mbuf = chk->last_mbuf;
7424                         } else {
7425                                 struct mbuf *m_tmp;
7426
7427                                 /* reassemble the data */
7428                                 m_tmp = sp->data;
7429                                 sp->data = chk->data;
7430                                 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7431                         }
7432                         sp->some_taken = some_taken;
7433                         atomic_add_int(&sp->length, to_move);
7434                         chk->data = NULL;
7435                         *bail = 1;
7436                         sctp_free_a_chunk(stcb, chk, so_locked);
7437                         to_move = 0;
7438                         goto out_of;
7439                 } else {
7440                         SCTP_BUF_LEN(m) = 0;
7441                         SCTP_BUF_NEXT(m) = chk->data;
7442                         chk->data = m;
7443                         M_ALIGN(chk->data, 4);
7444                 }
7445         }
7446         SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7447         if (chk->data == NULL) {
7448                 /* HELP, TSNH since we assured it would not above? */
7449 #ifdef INVARIANTS
7450                 panic("prepend failes HELP?");
7451 #else
7452                 SCTP_PRINTF("prepend fails HELP?\n");
7453                 sctp_free_a_chunk(stcb, chk, so_locked);
7454 #endif
7455                 *bail = 1;
7456                 to_move = 0;
7457                 goto out_of;
7458         }
7459         sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7460         chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7461         chk->book_size_scale = 0;
7462         chk->sent = SCTP_DATAGRAM_UNSENT;
7463
7464         chk->flags = 0;
7465         chk->asoc = &stcb->asoc;
7466         chk->pad_inplace = 0;
7467         chk->no_fr_allowed = 0;
7468         chk->rec.data.stream_seq = sp->strseq;
7469         chk->rec.data.stream_number = sp->stream;
7470         chk->rec.data.payloadtype = sp->ppid;
7471         chk->rec.data.context = sp->context;
7472         chk->rec.data.doing_fast_retransmit = 0;
7473
7474         chk->rec.data.timetodrop = sp->ts;
7475         chk->flags = sp->act_flags;
7476
7477         if (sp->net) {
7478                 chk->whoTo = sp->net;
7479                 atomic_add_int(&chk->whoTo->ref_count, 1);
7480         } else
7481                 chk->whoTo = NULL;
7482
7483         if (sp->holds_key_ref) {
7484                 chk->auth_keyid = sp->auth_keyid;
7485                 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7486                 chk->holds_key_ref = 1;
7487         }
7488         chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7489         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7490                 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7491                     (uintptr_t) stcb, sp->length,
7492                     (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7493                     chk->rec.data.TSN_seq);
7494         }
7495         dchkh = mtod(chk->data, struct sctp_data_chunk *);
7496         /*
7497          * Put the rest of the things in place now. Size was done earlier in
7498          * previous loop prior to padding.
7499          */
7500
7501 #ifdef SCTP_ASOCLOG_OF_TSNS
7502         SCTP_TCB_LOCK_ASSERT(stcb);
7503         if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7504                 asoc->tsn_out_at = 0;
7505                 asoc->tsn_out_wrapped = 1;
7506         }
7507         asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7508         asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7509         asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7510         asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7511         asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7512         asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7513         asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7514         asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7515         asoc->tsn_out_at++;
7516 #endif
7517
7518         dchkh->ch.chunk_type = SCTP_DATA;
7519         dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7520         dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7521         dchkh->dp.stream_id = htons(strq->stream_no);
7522         dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7523         dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7524         dchkh->ch.chunk_length = htons(chk->send_size);
7525         /* Now advance the chk->send_size by the actual pad needed. */
7526         if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7527                 /* need a pad */
7528                 struct mbuf *lm;
7529                 int pads;
7530
7531                 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7532                 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7533                         chk->pad_inplace = 1;
7534                 }
7535                 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7536                         /* pad added an mbuf */
7537                         chk->last_mbuf = lm;
7538                 }
7539                 chk->send_size += pads;
7540         }
7541         /* We only re-set the policy if it is on */
7542         if (sp->pr_sctp_on) {
7543                 sctp_set_prsctp_policy(sp);
7544                 asoc->pr_sctp_cnt++;
7545                 chk->pr_sctp_on = 1;
7546         } else {
7547                 chk->pr_sctp_on = 0;
7548         }
7549         if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7550                 /* All done pull and kill the message */
7551                 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7552                 if (sp->put_last_out == 0) {
7553                         SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7554                         SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7555                             sp->sender_all_done,
7556                             sp->length,
7557                             sp->msg_is_complete,
7558                             sp->put_last_out,
7559                             send_lock_up);
7560                 }
7561                 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7562                         SCTP_TCB_SEND_LOCK(stcb);
7563                         send_lock_up = 1;
7564                 }
7565                 TAILQ_REMOVE(&strq->outqueue, sp, next);
7566                 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
7567                 if (sp->net) {
7568                         sctp_free_remote_addr(sp->net);
7569                         sp->net = NULL;
7570                 }
7571                 if (sp->data) {
7572                         sctp_m_freem(sp->data);
7573                         sp->data = NULL;
7574                 }
7575                 sctp_free_a_strmoq(stcb, sp, so_locked);
7576
7577                 /* we can't be locked to it */
7578                 *locked = 0;
7579                 stcb->asoc.locked_on_sending = NULL;
7580         } else {
7581                 /* more to go, we are locked */
7582                 *locked = 1;
7583         }
7584         asoc->chunks_on_out_queue++;
7585         TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7586         asoc->send_queue_cnt++;
7587 out_of:
7588         if (send_lock_up) {
7589                 SCTP_TCB_SEND_UNLOCK(stcb);
7590                 send_lock_up = 0;
7591         }
7592         return (to_move);
7593 }
7594
7595
7596 static void
7597 sctp_fill_outqueue(struct sctp_tcb *stcb,
7598     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now, int so_locked
7599 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7600     SCTP_UNUSED
7601 #endif
7602 )
7603 {
7604         struct sctp_association *asoc;
7605         struct sctp_stream_out *strq, *strqn;
7606         int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7607         int locked, giveup;
7608
7609         SCTP_TCB_LOCK_ASSERT(stcb);
7610         asoc = &stcb->asoc;
7611 #ifdef INET6
7612         if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7613                 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7614         } else {
7615                 /* ?? not sure what else to do */
7616                 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7617         }
7618 #else
7619         goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7620 #endif
7621         /* Need an allowance for the data chunk header too */
7622         goal_mtu -= sizeof(struct sctp_data_chunk);
7623
7624         /* must make even word boundary */
7625         goal_mtu &= 0xfffffffc;
7626         if (asoc->locked_on_sending) {
7627                 /* We are stuck on one stream until the message completes. */
7628                 strq = asoc->locked_on_sending;
7629                 locked = 1;
7630         } else {
7631                 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7632                 locked = 0;
7633         }
7634         strqn = strq;
7635         while ((goal_mtu > 0) && strq) {
7636                 giveup = 0;
7637                 bail = 0;
7638                 moved_how_much = sctp_move_to_outqueue(stcb, strq, goal_mtu, frag_point, &locked,
7639                     &giveup, eeor_mode, &bail, so_locked);
7640                 if (moved_how_much)
7641                         stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved_how_much);
7642
7643                 if (locked) {
7644                         asoc->locked_on_sending = strq;
7645                         if ((moved_how_much == 0) || (giveup) || bail)
7646                                 /* no more to move for now */
7647                                 break;
7648                 } else {
7649                         asoc->locked_on_sending = NULL;
7650                         if ((giveup) || bail) {
7651                                 break;
7652                         }
7653                         strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7654                         if (strq == NULL) {
7655                                 break;
7656                         }
7657                 }
7658                 total_moved += moved_how_much;
7659                 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7660                 goal_mtu &= 0xfffffffc;
7661         }
7662         if (bail)
7663                 *quit_now = 1;
7664
7665         stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7666
7667         if (total_moved == 0) {
7668                 if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7669                     (net == stcb->asoc.primary_destination)) {
7670                         /* ran dry for primary network net */
7671                         SCTP_STAT_INCR(sctps_primary_randry);
7672                 } else if (stcb->asoc.sctp_cmt_on_off > 0) {
7673                         /* ran dry with CMT on */
7674                         SCTP_STAT_INCR(sctps_cmt_randry);
7675                 }
7676         }
7677 }
7678
7679 void
7680 sctp_fix_ecn_echo(struct sctp_association *asoc)
7681 {
7682         struct sctp_tmit_chunk *chk;
7683
7684         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7685                 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7686                         chk->sent = SCTP_DATAGRAM_UNSENT;
7687                 }
7688         }
7689 }
7690
7691 void
7692 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7693 {
7694         struct sctp_association *asoc;
7695         struct sctp_tmit_chunk *chk;
7696         struct sctp_stream_queue_pending *sp;
7697         unsigned int i;
7698
7699         if (net == NULL) {
7700                 return;
7701         }
7702         asoc = &stcb->asoc;
7703         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7704                 TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7705                         if (sp->net == net) {
7706                                 sctp_free_remote_addr(sp->net);
7707                                 sp->net = NULL;
7708                         }
7709                 }
7710         }
7711         TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7712                 if (chk->whoTo == net) {
7713                         sctp_free_remote_addr(chk->whoTo);
7714                         chk->whoTo = NULL;
7715                 }
7716         }
7717 }
7718
7719 int
7720 sctp_med_chunk_output(struct sctp_inpcb *inp,
7721     struct sctp_tcb *stcb,
7722     struct sctp_association *asoc,
7723     int *num_out,
7724     int *reason_code,
7725     int control_only, int from_where,
7726     struct timeval *now, int *now_filled, int frag_point, int so_locked
7727 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7728     SCTP_UNUSED
7729 #endif
7730 )
7731 {
7732         /*
7733          * Ok this is the generic chunk service queue. we must do the
7734          * following: - Service the stream queue that is next, moving any
7735          * message (note I must get a complete message i.e. FIRST/MIDDLE and
7736          * LAST to the out queue in one pass) and assigning TSN's - Check to
7737          * see if the cwnd/rwnd allows any output, if so we go ahead and
7738          * fomulate and send the low level chunks. Making sure to combine
7739          * any control in the control chunk queue also.
7740          */
7741         struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7742         struct mbuf *outchain, *endoutchain;
7743         struct sctp_tmit_chunk *chk, *nchk;
7744
7745         /* temp arrays for unlinking */
7746         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7747         int no_fragmentflg, error;
7748         unsigned int max_rwnd_per_dest, max_send_per_dest;
7749         int one_chunk, hbflag, skip_data_for_this_net;
7750         int asconf, cookie, no_out_cnt;
7751         int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7752         unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7753         int tsns_sent = 0;
7754         uint32_t auth_offset = 0;
7755         struct sctp_auth_chunk *auth = NULL;
7756         uint16_t auth_keyid;
7757         int override_ok = 1;
7758         int skip_fill_up = 0;
7759         int data_auth_reqd = 0;
7760
7761         /*
7762          * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7763          * destination.
7764          */
7765         int quit_now = 0;
7766
7767         *num_out = 0;
7768         auth_keyid = stcb->asoc.authinfo.active_keyid;
7769
7770         if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7771             (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7772             (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7773                 eeor_mode = 1;
7774         } else {
7775                 eeor_mode = 0;
7776         }
7777         ctl_cnt = no_out_cnt = asconf = cookie = 0;
7778         /*
7779          * First lets prime the pump. For each destination, if there is room
7780          * in the flight size, attempt to pull an MTU's worth out of the
7781          * stream queues into the general send_queue
7782          */
7783 #ifdef SCTP_AUDITING_ENABLED
7784         sctp_audit_log(0xC2, 2);
7785 #endif
7786         SCTP_TCB_LOCK_ASSERT(stcb);
7787         hbflag = 0;
7788         if ((control_only) || (asoc->stream_reset_outstanding))
7789                 no_data_chunks = 1;
7790         else
7791                 no_data_chunks = 0;
7792
7793         /* Nothing to possible to send? */
7794         if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7795             (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7796             TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7797             TAILQ_EMPTY(&asoc->send_queue) &&
7798             stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
7799 nothing_to_send:
7800                 *reason_code = 9;
7801                 return (0);
7802         }
7803         if (asoc->peers_rwnd == 0) {
7804                 /* No room in peers rwnd */
7805                 *reason_code = 1;
7806                 if (asoc->total_flight > 0) {
7807                         /* we are allowed one chunk in flight */
7808                         no_data_chunks = 1;
7809                 }
7810         }
7811         if (stcb->asoc.ecn_echo_cnt_onq) {
7812                 /* Record where a sack goes, if any */
7813                 if (no_data_chunks &&
7814                     (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7815                         /* Nothing but ECNe to send - we don't do that */
7816                         goto nothing_to_send;
7817                 }
7818                 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7819                         if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7820                             (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7821                                 sack_goes_to = chk->whoTo;
7822                                 break;
7823                         }
7824                 }
7825         }
7826         max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7827         if (stcb->sctp_socket)
7828                 max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7829         else
7830                 max_send_per_dest = 0;
7831         if (no_data_chunks == 0) {
7832                 /* How many non-directed chunks are there? */
7833                 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7834                         if (chk->whoTo == NULL) {
7835                                 /*
7836                                  * We already have non-directed chunks on
7837                                  * the queue, no need to do a fill-up.
7838                                  */
7839                                 skip_fill_up = 1;
7840                                 break;
7841                         }
7842                 }
7843
7844         }
7845         if ((no_data_chunks == 0) &&
7846             (skip_fill_up == 0) &&
7847             (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7848                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7849                         /*
7850                          * This for loop we are in takes in each net, if
7851                          * its's got space in cwnd and has data sent to it
7852                          * (when CMT is off) then it calls
7853                          * sctp_fill_outqueue for the net. This gets data on
7854                          * the send queue for that network.
7855                          * 
7856                          * In sctp_fill_outqueue TSN's are assigned and data is
7857                          * copied out of the stream buffers. Note mostly
7858                          * copy by reference (we hope).
7859                          */
7860                         net->window_probe = 0;
7861                         if ((net != stcb->asoc.alternate) &&
7862                             ((net->dest_state & SCTP_ADDR_PF) ||
7863                             (!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
7864                             (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
7865                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7866                                         sctp_log_cwnd(stcb, net, 1,
7867                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7868                                 }
7869                                 continue;
7870                         }
7871                         if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
7872                             (net->flight_size == 0)) {
7873                                 (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
7874                         }
7875                         if (net->flight_size >= net->cwnd) {
7876                                 /* skip this network, no room - can't fill */
7877                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7878                                         sctp_log_cwnd(stcb, net, 3,
7879                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7880                                 }
7881                                 continue;
7882                         }
7883                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7884                                 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7885                         }
7886                         sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
7887                         if (quit_now) {
7888                                 /* memory alloc failure */
7889                                 no_data_chunks = 1;
7890                                 break;
7891                         }
7892                 }
7893         }
7894         /* now service each destination and send out what we can for it */
7895         /* Nothing to send? */
7896         if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7897             TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7898             TAILQ_EMPTY(&asoc->send_queue)) {
7899                 *reason_code = 8;
7900                 return (0);
7901         }
7902         if (asoc->sctp_cmt_on_off > 0) {
7903                 /* get the last start point */
7904                 start_at = asoc->last_net_cmt_send_started;
7905                 if (start_at == NULL) {
7906                         /* null so to beginning */
7907                         start_at = TAILQ_FIRST(&asoc->nets);
7908                 } else {
7909                         start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7910                         if (start_at == NULL) {
7911                                 start_at = TAILQ_FIRST(&asoc->nets);
7912                         }
7913                 }
7914                 asoc->last_net_cmt_send_started = start_at;
7915         } else {
7916                 start_at = TAILQ_FIRST(&asoc->nets);
7917         }
7918         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7919                 if (chk->whoTo == NULL) {
7920                         if (asoc->alternate) {
7921                                 chk->whoTo = asoc->alternate;
7922                         } else {
7923                                 chk->whoTo = asoc->primary_destination;
7924                         }
7925                         atomic_add_int(&chk->whoTo->ref_count, 1);
7926                 }
7927         }
7928         old_start_at = NULL;
7929 again_one_more_time:
7930         for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7931                 /* how much can we send? */
7932                 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7933                 if (old_start_at && (old_start_at == net)) {
7934                         /* through list ocmpletely. */
7935                         break;
7936                 }
7937                 tsns_sent = 0xa;
7938                 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7939                     TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7940                     (net->flight_size >= net->cwnd)) {
7941                         /*
7942                          * Nothing on control or asconf and flight is full,
7943                          * we can skip even in the CMT case.
7944                          */
7945                         continue;
7946                 }
7947                 ctl_cnt = bundle_at = 0;
7948                 endoutchain = outchain = NULL;
7949                 no_fragmentflg = 1;
7950                 one_chunk = 0;
7951                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7952                         skip_data_for_this_net = 1;
7953                 } else {
7954                         skip_data_for_this_net = 0;
7955                 }
7956                 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7957                         /*
7958                          * if we have a route and an ifp check to see if we
7959                          * have room to send to this guy
7960                          */
7961                         struct ifnet *ifp;
7962
7963                         ifp = net->ro.ro_rt->rt_ifp;
7964                         if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7965                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
7966                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7967                                         sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7968                                 }
7969                                 continue;
7970                         }
7971                 }
7972                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7973 #ifdef INET
7974                 case AF_INET:
7975                         mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7976                         break;
7977 #endif
7978 #ifdef INET6
7979                 case AF_INET6:
7980                         mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7981                         break;
7982 #endif
7983                 default:
7984                         /* TSNH */
7985                         mtu = net->mtu;
7986                         break;
7987                 }
7988                 mx_mtu = mtu;
7989                 to_out = 0;
7990                 if (mtu > asoc->peers_rwnd) {
7991                         if (asoc->total_flight > 0) {
7992                                 /* We have a packet in flight somewhere */
7993                                 r_mtu = asoc->peers_rwnd;
7994                         } else {
7995                                 /* We are always allowed to send one MTU out */
7996                                 one_chunk = 1;
7997                                 r_mtu = mtu;
7998                         }
7999                 } else {
8000                         r_mtu = mtu;
8001                 }
8002                 /************************/
8003                 /* ASCONF transmission */
8004                 /************************/
8005                 /* Now first lets go through the asconf queue */
8006                 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
8007                         if (chk->rec.chunk_id.id != SCTP_ASCONF) {
8008                                 continue;
8009                         }
8010                         if (chk->whoTo == NULL) {
8011                                 if (asoc->alternate == NULL) {
8012                                         if (asoc->primary_destination != net) {
8013                                                 break;
8014                                         }
8015                                 } else {
8016                                         if (asoc->alternate != net) {
8017                                                 break;
8018                                         }
8019                                 }
8020                         } else {
8021                                 if (chk->whoTo != net) {
8022                                         break;
8023                                 }
8024                         }
8025                         if (chk->data == NULL) {
8026                                 break;
8027                         }
8028                         if (chk->sent != SCTP_DATAGRAM_UNSENT &&
8029                             chk->sent != SCTP_DATAGRAM_RESEND) {
8030                                 break;
8031                         }
8032                         /*
8033                          * if no AUTH is yet included and this chunk
8034                          * requires it, make sure to account for it.  We
8035                          * don't apply the size until the AUTH chunk is
8036                          * actually added below in case there is no room for
8037                          * this chunk. NOTE: we overload the use of "omtu"
8038                          * here
8039                          */
8040                         if ((auth == NULL) &&
8041                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8042                             stcb->asoc.peer_auth_chunks)) {
8043                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8044                         } else
8045                                 omtu = 0;
8046                         /* Here we do NOT factor the r_mtu */
8047                         if ((chk->send_size < (int)(mtu - omtu)) ||
8048                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8049                                 /*
8050                                  * We probably should glom the mbuf chain
8051                                  * from the chk->data for control but the
8052                                  * problem is it becomes yet one more level
8053                                  * of tracking to do if for some reason
8054                                  * output fails. Then I have got to
8055                                  * reconstruct the merged control chain.. el
8056                                  * yucko.. for now we take the easy way and
8057                                  * do the copy
8058                                  */
8059                                 /*
8060                                  * Add an AUTH chunk, if chunk requires it
8061                                  * save the offset into the chain for AUTH
8062                                  */
8063                                 if ((auth == NULL) &&
8064                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8065                                     stcb->asoc.peer_auth_chunks))) {
8066                                         outchain = sctp_add_auth_chunk(outchain,
8067                                             &endoutchain,
8068                                             &auth,
8069                                             &auth_offset,
8070                                             stcb,
8071                                             chk->rec.chunk_id.id);
8072                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8073                                 }
8074                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8075                                     (int)chk->rec.chunk_id.can_take_data,
8076                                     chk->send_size, chk->copy_by_ref);
8077                                 if (outchain == NULL) {
8078                                         *reason_code = 8;
8079                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8080                                         return (ENOMEM);
8081                                 }
8082                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8083                                 /* update our MTU size */
8084                                 if (mtu > (chk->send_size + omtu))
8085                                         mtu -= (chk->send_size + omtu);
8086                                 else
8087                                         mtu = 0;
8088                                 to_out += (chk->send_size + omtu);
8089                                 /* Do clear IP_DF ? */
8090                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8091                                         no_fragmentflg = 0;
8092                                 }
8093                                 if (chk->rec.chunk_id.can_take_data)
8094                                         chk->data = NULL;
8095                                 /*
8096                                  * set hb flag since we can use these for
8097                                  * RTO
8098                                  */
8099                                 hbflag = 1;
8100                                 asconf = 1;
8101                                 /*
8102                                  * should sysctl this: don't bundle data
8103                                  * with ASCONF since it requires AUTH
8104                                  */
8105                                 no_data_chunks = 1;
8106                                 chk->sent = SCTP_DATAGRAM_SENT;
8107                                 if (chk->whoTo == NULL) {
8108                                         chk->whoTo = net;
8109                                         atomic_add_int(&net->ref_count, 1);
8110                                 }
8111                                 chk->snd_count++;
8112                                 if (mtu == 0) {
8113                                         /*
8114                                          * Ok we are out of room but we can
8115                                          * output without effecting the
8116                                          * flight size since this little guy
8117                                          * is a control only packet.
8118                                          */
8119                                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8120                                         /*
8121                                          * do NOT clear the asconf flag as
8122                                          * it is used to do appropriate
8123                                          * source address selection.
8124                                          */
8125                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8126                                             (struct sockaddr *)&net->ro._l_addr,
8127                                             outchain, auth_offset, auth,
8128                                             stcb->asoc.authinfo.active_keyid,
8129                                             no_fragmentflg, 0, NULL, asconf,
8130                                             inp->sctp_lport, stcb->rport,
8131                                             htonl(stcb->asoc.peer_vtag),
8132                                             net->port, so_locked, NULL, NULL))) {
8133                                                 if (error == ENOBUFS) {
8134                                                         asoc->ifp_had_enobuf = 1;
8135                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
8136                                                 }
8137                                                 if (from_where == 0) {
8138                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
8139                                                 }
8140                                                 if (*now_filled == 0) {
8141                                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8142                                                         *now_filled = 1;
8143                                                         *now = net->last_sent_time;
8144                                                 } else {
8145                                                         net->last_sent_time = *now;
8146                                                 }
8147                                                 hbflag = 0;
8148                                                 /* error, could not output */
8149                                                 if (error == EHOSTUNREACH) {
8150                                                         /*
8151                                                          * Destination went
8152                                                          * unreachable
8153                                                          * during this send
8154                                                          */
8155                                                         sctp_move_chunks_from_net(stcb, net);
8156                                                 }
8157                                                 *reason_code = 7;
8158                                                 continue;
8159                                         } else
8160                                                 asoc->ifp_had_enobuf = 0;
8161                                         if (*now_filled == 0) {
8162                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8163                                                 *now_filled = 1;
8164                                                 *now = net->last_sent_time;
8165                                         } else {
8166                                                 net->last_sent_time = *now;
8167                                         }
8168                                         hbflag = 0;
8169                                         /*
8170                                          * increase the number we sent, if a
8171                                          * cookie is sent we don't tell them
8172                                          * any was sent out.
8173                                          */
8174                                         outchain = endoutchain = NULL;
8175                                         auth = NULL;
8176                                         auth_offset = 0;
8177                                         if (!no_out_cnt)
8178                                                 *num_out += ctl_cnt;
8179                                         /* recalc a clean slate and setup */
8180                                         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8181                                                 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8182                                         } else {
8183                                                 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
8184                                         }
8185                                         to_out = 0;
8186                                         no_fragmentflg = 1;
8187                                 }
8188                         }
8189                 }
8190                 /************************/
8191                 /* Control transmission */
8192                 /************************/
8193                 /* Now first lets go through the control queue */
8194                 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
8195                         if ((sack_goes_to) &&
8196                             (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
8197                             (chk->whoTo != sack_goes_to)) {
8198                                 /*
8199                                  * if we have a sack in queue, and we are
8200                                  * looking at an ecn echo that is NOT queued
8201                                  * to where the sack is going..
8202                                  */
8203                                 if (chk->whoTo == net) {
8204                                         /*
8205                                          * Don't transmit it to where its
8206                                          * going (current net)
8207                                          */
8208                                         continue;
8209                                 } else if (sack_goes_to == net) {
8210                                         /*
8211                                          * But do transmit it to this
8212                                          * address
8213                                          */
8214                                         goto skip_net_check;
8215                                 }
8216                         }
8217                         if (chk->whoTo == NULL) {
8218                                 if (asoc->alternate == NULL) {
8219                                         if (asoc->primary_destination != net) {
8220                                                 continue;
8221                                         }
8222                                 } else {
8223                                         if (asoc->alternate != net) {
8224                                                 continue;
8225                                         }
8226                                 }
8227                         } else {
8228                                 if (chk->whoTo != net) {
8229                                         continue;
8230                                 }
8231                         }
8232         skip_net_check:
8233                         if (chk->data == NULL) {
8234                                 continue;
8235                         }
8236                         if (chk->sent != SCTP_DATAGRAM_UNSENT) {
8237                                 /*
8238                                  * It must be unsent. Cookies and ASCONF's
8239                                  * hang around but there timers will force
8240                                  * when marked for resend.
8241                                  */
8242                                 continue;
8243                         }
8244                         /*
8245                          * if no AUTH is yet included and this chunk
8246                          * requires it, make sure to account for it.  We
8247                          * don't apply the size until the AUTH chunk is
8248                          * actually added below in case there is no room for
8249                          * this chunk. NOTE: we overload the use of "omtu"
8250                          * here
8251                          */
8252                         if ((auth == NULL) &&
8253                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8254                             stcb->asoc.peer_auth_chunks)) {
8255                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8256                         } else
8257                                 omtu = 0;
8258                         /* Here we do NOT factor the r_mtu */
8259                         if ((chk->send_size <= (int)(mtu - omtu)) ||
8260                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8261                                 /*
8262                                  * We probably should glom the mbuf chain
8263                                  * from the chk->data for control but the
8264                                  * problem is it becomes yet one more level
8265                                  * of tracking to do if for some reason
8266                                  * output fails. Then I have got to
8267                                  * reconstruct the merged control chain.. el
8268                                  * yucko.. for now we take the easy way and
8269                                  * do the copy
8270                                  */
8271                                 /*
8272                                  * Add an AUTH chunk, if chunk requires it
8273                                  * save the offset into the chain for AUTH
8274                                  */
8275                                 if ((auth == NULL) &&
8276                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8277                                     stcb->asoc.peer_auth_chunks))) {
8278                                         outchain = sctp_add_auth_chunk(outchain,
8279                                             &endoutchain,
8280                                             &auth,
8281                                             &auth_offset,
8282                                             stcb,
8283                                             chk->rec.chunk_id.id);
8284                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8285                                 }
8286                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8287                                     (int)chk->rec.chunk_id.can_take_data,
8288                                     chk->send_size, chk->copy_by_ref);
8289                                 if (outchain == NULL) {
8290                                         *reason_code = 8;
8291                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8292                                         return (ENOMEM);
8293                                 }
8294                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8295                                 /* update our MTU size */
8296                                 if (mtu > (chk->send_size + omtu))
8297                                         mtu -= (chk->send_size + omtu);
8298                                 else
8299                                         mtu = 0;
8300                                 to_out += (chk->send_size + omtu);
8301                                 /* Do clear IP_DF ? */
8302                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8303                                         no_fragmentflg = 0;
8304                                 }
8305                                 if (chk->rec.chunk_id.can_take_data)
8306                                         chk->data = NULL;
8307                                 /* Mark things to be removed, if needed */
8308                                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8309                                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
8310                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8311                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8312                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8313                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8314                                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8315                                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8316                                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8317                                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8318                                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8319                                         if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
8320                                                 hbflag = 1;
8321                                         }
8322                                         /* remove these chunks at the end */
8323                                         if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8324                                             (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
8325                                                 /* turn off the timer */
8326                                                 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8327                                                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8328                                                             inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
8329                                                 }
8330                                         }
8331                                         ctl_cnt++;
8332                                 } else {
8333                                         /*
8334                                          * Other chunks, since they have
8335                                          * timers running (i.e. COOKIE) we
8336                                          * just "trust" that it gets sent or
8337                                          * retransmitted.
8338                                          */
8339                                         ctl_cnt++;
8340                                         if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8341                                                 cookie = 1;
8342                                                 no_out_cnt = 1;
8343                                         } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8344                                                 /*
8345                                                  * Increment ecne send count
8346                                                  * here this means we may be
8347                                                  * over-zealous in our
8348                                                  * counting if the send
8349                                                  * fails, but its the best
8350                                                  * place to do it (we used
8351                                                  * to do it in the queue of
8352                                                  * the chunk, but that did
8353                                                  * not tell how many times
8354                                                  * it was sent.
8355                                                  */
8356                                                 SCTP_STAT_INCR(sctps_sendecne);
8357                                         }
8358                                         chk->sent = SCTP_DATAGRAM_SENT;
8359                                         if (chk->whoTo == NULL) {
8360                                                 chk->whoTo = net;
8361                                                 atomic_add_int(&net->ref_count, 1);
8362                                         }
8363                                         chk->snd_count++;
8364                                 }
8365                                 if (mtu == 0) {
8366                                         /*
8367                                          * Ok we are out of room but we can
8368                                          * output without effecting the
8369                                          * flight size since this little guy
8370                                          * is a control only packet.
8371                                          */
8372                                         if (asconf) {
8373                                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8374                                                 /*
8375                                                  * do NOT clear the asconf
8376                                                  * flag as it is used to do
8377                                                  * appropriate source
8378                                                  * address selection.
8379                                                  */
8380                                         }
8381                                         if (cookie) {
8382                                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8383                                                 cookie = 0;
8384                                         }
8385                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8386                                             (struct sockaddr *)&net->ro._l_addr,
8387                                             outchain,
8388                                             auth_offset, auth,
8389                                             stcb->asoc.authinfo.active_keyid,
8390                                             no_fragmentflg, 0, NULL, asconf,
8391                                             inp->sctp_lport, stcb->rport,
8392                                             htonl(stcb->asoc.peer_vtag),
8393                                             net->port, so_locked, NULL, NULL))) {
8394                                                 if (error == ENOBUFS) {
8395                                                         asoc->ifp_had_enobuf = 1;
8396                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
8397                                                 }
8398                                                 if (from_where == 0) {
8399                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
8400                                                 }
8401                                                 /* error, could not output */
8402                                                 if (hbflag) {
8403                                                         if (*now_filled == 0) {
8404                                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8405                                                                 *now_filled = 1;
8406                                                                 *now = net->last_sent_time;
8407                                                         } else {
8408                                                                 net->last_sent_time = *now;
8409                                                         }
8410                                                         hbflag = 0;
8411                                                 }
8412                                                 if (error == EHOSTUNREACH) {
8413                                                         /*
8414                                                          * Destination went
8415                                                          * unreachable
8416                                                          * during this send
8417                                                          */
8418                                                         sctp_move_chunks_from_net(stcb, net);
8419                                                 }
8420                                                 *reason_code = 7;
8421                                                 continue;
8422                                         } else
8423                                                 asoc->ifp_had_enobuf = 0;
8424                                         /* Only HB or ASCONF advances time */
8425                                         if (hbflag) {
8426                                                 if (*now_filled == 0) {
8427                                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8428                                                         *now_filled = 1;
8429                                                         *now = net->last_sent_time;
8430                                                 } else {
8431                                                         net->last_sent_time = *now;
8432                                                 }
8433                                                 hbflag = 0;
8434                                         }
8435                                         /*
8436                                          * increase the number we sent, if a
8437                                          * cookie is sent we don't tell them
8438                                          * any was sent out.
8439                                          */
8440                                         outchain = endoutchain = NULL;
8441                                         auth = NULL;
8442                                         auth_offset = 0;
8443                                         if (!no_out_cnt)
8444                                                 *num_out += ctl_cnt;
8445                                         /* recalc a clean slate and setup */
8446                                         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8447                                                 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8448                                         } else {
8449                                                 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
8450                                         }
8451                                         to_out = 0;
8452                                         no_fragmentflg = 1;
8453                                 }
8454                         }
8455                 }
8456                 /* JRI: if dest is in PF state, do not send data to it */
8457                 if ((asoc->sctp_cmt_on_off > 0) &&
8458                     (net != stcb->asoc.alternate) &&
8459                     (net->dest_state & SCTP_ADDR_PF)) {
8460                         goto no_data_fill;
8461                 }
8462                 if (net->flight_size >= net->cwnd) {
8463                         goto no_data_fill;
8464                 }
8465                 if ((asoc->sctp_cmt_on_off > 0) &&
8466                     (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
8467                     (net->flight_size > max_rwnd_per_dest)) {
8468                         goto no_data_fill;
8469                 }
8470                 /*
8471                  * We need a specific accounting for the usage of the send
8472                  * buffer. We also need to check the number of messages per
8473                  * net. For now, this is better than nothing and it disabled
8474                  * by default...
8475                  */
8476                 if ((asoc->sctp_cmt_on_off > 0) &&
8477                     (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
8478                     (max_send_per_dest > 0) &&
8479                     (net->flight_size > max_send_per_dest)) {
8480                         goto no_data_fill;
8481                 }
8482                 /*********************/
8483                 /* Data transmission */
8484                 /*********************/
8485                 /*
8486                  * if AUTH for DATA is required and no AUTH has been added
8487                  * yet, account for this in the mtu now... if no data can be
8488                  * bundled, this adjustment won't matter anyways since the
8489                  * packet will be going out...
8490                  */
8491                 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8492                     stcb->asoc.peer_auth_chunks);
8493                 if (data_auth_reqd && (auth == NULL)) {
8494                         mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8495                 }
8496                 /* now lets add any data within the MTU constraints */
8497                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8498                 case AF_INET:
8499                         if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8500                                 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8501                         else
8502                                 omtu = 0;
8503                         break;
8504 #ifdef INET6
8505                 case AF_INET6:
8506                         if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8507                                 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8508                         else
8509                                 omtu = 0;
8510                         break;
8511 #endif
8512                 default:
8513                         /* TSNH */
8514                         omtu = 0;
8515                         break;
8516                 }
8517                 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8518                     (skip_data_for_this_net == 0)) ||
8519                     (cookie)) {
8520                         TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
8521                                 if (no_data_chunks) {
8522                                         /* let only control go out */
8523                                         *reason_code = 1;
8524                                         break;
8525                                 }
8526                                 if (net->flight_size >= net->cwnd) {
8527                                         /* skip this net, no room for data */
8528                                         *reason_code = 2;
8529                                         break;
8530                                 }
8531                                 if ((chk->whoTo != NULL) &&
8532                                     (chk->whoTo != net)) {
8533                                         /* Don't send the chunk on this net */
8534                                         continue;
8535                                 }
8536                                 if (asoc->sctp_cmt_on_off == 0) {
8537                                         if ((asoc->alternate) &&
8538                                             (asoc->alternate != net) &&
8539                                             (chk->whoTo == NULL)) {
8540                                                 continue;
8541                                         } else if ((net != asoc->primary_destination) &&
8542                                                     (asoc->alternate == NULL) &&
8543                                             (chk->whoTo == NULL)) {
8544                                                 continue;
8545                                         }
8546                                 }
8547                                 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8548                                         /*-
8549                                          * strange, we have a chunk that is
8550                                          * to big for its destination and
8551                                          * yet no fragment ok flag.
8552                                          * Something went wrong when the
8553                                          * PMTU changed...we did not mark
8554                                          * this chunk for some reason?? I
8555                                          * will fix it here by letting IP
8556                                          * fragment it for now and printing
8557                                          * a warning. This really should not
8558                                          * happen ...
8559                                          */
8560                                         SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8561                                             chk->send_size, mtu);
8562                                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8563                                 }
8564                                 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8565                                     ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8566                                         struct sctp_data_chunk *dchkh;
8567
8568                                         dchkh = mtod(chk->data, struct sctp_data_chunk *);
8569                                         dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8570                                 }
8571                                 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8572                                     ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8573                                         /* ok we will add this one */
8574
8575                                         /*
8576                                          * Add an AUTH chunk, if chunk
8577                                          * requires it, save the offset into
8578                                          * the chain for AUTH
8579                                          */
8580                                         if (data_auth_reqd) {
8581                                                 if (auth == NULL) {
8582                                                         outchain = sctp_add_auth_chunk(outchain,
8583                                                             &endoutchain,
8584                                                             &auth,
8585                                                             &auth_offset,
8586                                                             stcb,
8587                                                             SCTP_DATA);
8588                                                         auth_keyid = chk->auth_keyid;
8589                                                         override_ok = 0;
8590                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8591                                                 } else if (override_ok) {
8592                                                         /*
8593                                                          * use this data's
8594                                                          * keyid
8595                                                          */
8596                                                         auth_keyid = chk->auth_keyid;
8597                                                         override_ok = 0;
8598                                                 } else if (auth_keyid != chk->auth_keyid) {
8599                                                         /*
8600                                                          * different keyid,
8601                                                          * so done bundling
8602                                                          */
8603                                                         break;
8604                                                 }
8605                                         }
8606                                         outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8607                                             chk->send_size, chk->copy_by_ref);
8608                                         if (outchain == NULL) {
8609                                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8610                                                 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8611                                                         sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8612                                                 }
8613                                                 *reason_code = 3;
8614                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8615                                                 return (ENOMEM);
8616                                         }
8617                                         /* upate our MTU size */
8618                                         /* Do clear IP_DF ? */
8619                                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8620                                                 no_fragmentflg = 0;
8621                                         }
8622                                         /* unsigned subtraction of mtu */
8623                                         if (mtu > chk->send_size)
8624                                                 mtu -= chk->send_size;
8625                                         else
8626                                                 mtu = 0;
8627                                         /* unsigned subtraction of r_mtu */
8628                                         if (r_mtu > chk->send_size)
8629                                                 r_mtu -= chk->send_size;
8630                                         else
8631                                                 r_mtu = 0;
8632
8633                                         to_out += chk->send_size;
8634                                         if ((to_out > mx_mtu) && no_fragmentflg) {
8635 #ifdef INVARIANTS
8636                                                 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8637 #else
8638                                                 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8639                                                     mx_mtu, to_out);
8640 #endif
8641                                         }
8642                                         chk->window_probe = 0;
8643                                         data_list[bundle_at++] = chk;
8644                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8645                                                 mtu = 0;
8646                                                 break;
8647                                         }
8648                                         if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8649                                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8650                                                         SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8651                                                 } else {
8652                                                         SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8653                                                 }
8654                                                 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8655                                                     ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8656                                                         /*
8657                                                          * Count number of
8658                                                          * user msg's that
8659                                                          * were fragmented
8660                                                          * we do this by
8661                                                          * counting when we
8662                                                          * see a LAST
8663                                                          * fragment only.
8664                                                          */
8665                                                         SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8666                                         }
8667                                         if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8668                                                 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8669                                                         data_list[0]->window_probe = 1;
8670                                                         net->window_probe = 1;
8671                                                 }
8672                                                 break;
8673                                         }
8674                                 } else {
8675                                         /*
8676                                          * Must be sent in order of the
8677                                          * TSN's (on a network)
8678                                          */
8679                                         break;
8680                                 }
8681                         }       /* for (chunk gather loop for this net) */
8682                 }               /* if asoc.state OPEN */
8683 no_data_fill:
8684                 /* Is there something to send for this destination? */
8685                 if (outchain) {
8686                         /* We may need to start a control timer or two */
8687                         if (asconf) {
8688                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8689                                     stcb, net);
8690                                 /*
8691                                  * do NOT clear the asconf flag as it is
8692                                  * used to do appropriate source address
8693                                  * selection.
8694                                  */
8695                         }
8696                         if (cookie) {
8697                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8698                                 cookie = 0;
8699                         }
8700                         /* must start a send timer if data is being sent */
8701                         if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8702                                 /*
8703                                  * no timer running on this destination
8704                                  * restart it.
8705                                  */
8706                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8707                         }
8708                         /* Now send it, if there is anything to send :> */
8709                         if ((error = sctp_lowlevel_chunk_output(inp,
8710                             stcb,
8711                             net,
8712                             (struct sockaddr *)&net->ro._l_addr,
8713                             outchain,
8714                             auth_offset,
8715                             auth,
8716                             auth_keyid,
8717                             no_fragmentflg,
8718                             bundle_at,
8719                             data_list[0],
8720                             asconf,
8721                             inp->sctp_lport, stcb->rport,
8722                             htonl(stcb->asoc.peer_vtag),
8723                             net->port, so_locked, NULL, NULL))) {
8724                                 /* error, we could not output */
8725                                 if (error == ENOBUFS) {
8726                                         SCTP_STAT_INCR(sctps_lowlevelerr);
8727                                         asoc->ifp_had_enobuf = 1;
8728                                 }
8729                                 if (from_where == 0) {
8730                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
8731                                 }
8732                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8733                                 if (hbflag) {
8734                                         if (*now_filled == 0) {
8735                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8736                                                 *now_filled = 1;
8737                                                 *now = net->last_sent_time;
8738                                         } else {
8739                                                 net->last_sent_time = *now;
8740                                         }
8741                                         hbflag = 0;
8742                                 }
8743                                 if (error == EHOSTUNREACH) {
8744                                         /*
8745                                          * Destination went unreachable
8746                                          * during this send
8747                                          */
8748                                         sctp_move_chunks_from_net(stcb, net);
8749                                 }
8750                                 *reason_code = 6;
8751                                 /*-
8752                                  * I add this line to be paranoid. As far as
8753                                  * I can tell the continue, takes us back to
8754                                  * the top of the for, but just to make sure
8755                                  * I will reset these again here.
8756                                  */
8757                                 ctl_cnt = bundle_at = 0;
8758                                 continue;       /* This takes us back to the
8759                                                  * for() for the nets. */
8760                         } else {
8761                                 asoc->ifp_had_enobuf = 0;
8762                         }
8763                         outchain = endoutchain = NULL;
8764                         auth = NULL;
8765                         auth_offset = 0;
8766                         if (bundle_at || hbflag) {
8767                                 /* For data/asconf and hb set time */
8768                                 if (*now_filled == 0) {
8769                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8770                                         *now_filled = 1;
8771                                         *now = net->last_sent_time;
8772                                 } else {
8773                                         net->last_sent_time = *now;
8774                                 }
8775                         }
8776                         if (!no_out_cnt) {
8777                                 *num_out += (ctl_cnt + bundle_at);
8778                         }
8779                         if (bundle_at) {
8780                                 /* setup for a RTO measurement */
8781                                 tsns_sent = data_list[0]->rec.data.TSN_seq;
8782                                 /* fill time if not already filled */
8783                                 if (*now_filled == 0) {
8784                                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8785                                         *now_filled = 1;
8786                                         *now = asoc->time_last_sent;
8787                                 } else {
8788                                         asoc->time_last_sent = *now;
8789                                 }
8790                                 if (net->rto_needed) {
8791                                         data_list[0]->do_rtt = 1;
8792                                         net->rto_needed = 0;
8793                                 }
8794                                 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8795                                 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8796                         }
8797                         if (one_chunk) {
8798                                 break;
8799                         }
8800                 }
8801                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8802                         sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8803                 }
8804         }
8805         if (old_start_at == NULL) {
8806                 old_start_at = start_at;
8807                 start_at = TAILQ_FIRST(&asoc->nets);
8808                 if (old_start_at)
8809                         goto again_one_more_time;
8810         }
8811         /*
8812          * At the end there should be no NON timed chunks hanging on this
8813          * queue.
8814          */
8815         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8816                 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8817         }
8818         if ((*num_out == 0) && (*reason_code == 0)) {
8819                 *reason_code = 4;
8820         } else {
8821                 *reason_code = 5;
8822         }
8823         sctp_clean_up_ctl(stcb, asoc, so_locked);
8824         return (0);
8825 }
8826
8827 void
8828 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8829 {
8830         /*-
8831          * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8832          * the control chunk queue.
8833          */
8834         struct sctp_chunkhdr *hdr;
8835         struct sctp_tmit_chunk *chk;
8836         struct mbuf *mat;
8837
8838         SCTP_TCB_LOCK_ASSERT(stcb);
8839         sctp_alloc_a_chunk(stcb, chk);
8840         if (chk == NULL) {
8841                 /* no memory */
8842                 sctp_m_freem(op_err);
8843                 return;
8844         }
8845         chk->copy_by_ref = 0;
8846         SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8847         if (op_err == NULL) {
8848                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
8849                 return;
8850         }
8851         chk->send_size = 0;
8852         mat = op_err;
8853         while (mat != NULL) {
8854                 chk->send_size += SCTP_BUF_LEN(mat);
8855                 mat = SCTP_BUF_NEXT(mat);
8856         }
8857         chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8858         chk->rec.chunk_id.can_take_data = 1;
8859         chk->sent = SCTP_DATAGRAM_UNSENT;
8860         chk->snd_count = 0;
8861         chk->flags = 0;
8862         chk->asoc = &stcb->asoc;
8863         chk->data = op_err;
8864         chk->whoTo = NULL;
8865         hdr = mtod(op_err, struct sctp_chunkhdr *);
8866         hdr->chunk_type = SCTP_OPERATION_ERROR;
8867         hdr->chunk_flags = 0;
8868         hdr->chunk_length = htons(chk->send_size);
8869         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8870             chk,
8871             sctp_next);
8872         chk->asoc->ctrl_queue_cnt++;
8873 }
8874
8875 int
8876 sctp_send_cookie_echo(struct mbuf *m,
8877     int offset,
8878     struct sctp_tcb *stcb,
8879     struct sctp_nets *net)
8880 {
8881         /*-
8882          * pull out the cookie and put it at the front of the control chunk
8883          * queue.
8884          */
8885         int at;
8886         struct mbuf *cookie;
8887         struct sctp_paramhdr parm, *phdr;
8888         struct sctp_chunkhdr *hdr;
8889         struct sctp_tmit_chunk *chk;
8890         uint16_t ptype, plen;
8891
8892         /* First find the cookie in the param area */
8893         cookie = NULL;
8894         at = offset + sizeof(struct sctp_init_chunk);
8895
8896         SCTP_TCB_LOCK_ASSERT(stcb);
8897         do {
8898                 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8899                 if (phdr == NULL) {
8900                         return (-3);
8901                 }
8902                 ptype = ntohs(phdr->param_type);
8903                 plen = ntohs(phdr->param_length);
8904                 if (ptype == SCTP_STATE_COOKIE) {
8905                         int pad;
8906
8907                         /* found the cookie */
8908                         if ((pad = (plen % 4))) {
8909                                 plen += 4 - pad;
8910                         }
8911                         cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8912                         if (cookie == NULL) {
8913                                 /* No memory */
8914                                 return (-2);
8915                         }
8916 #ifdef SCTP_MBUF_LOGGING
8917                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8918                                 struct mbuf *mat;
8919
8920                                 mat = cookie;
8921                                 while (mat) {
8922                                         if (SCTP_BUF_IS_EXTENDED(mat)) {
8923                                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8924                                         }
8925                                         mat = SCTP_BUF_NEXT(mat);
8926                                 }
8927                         }
8928 #endif
8929                         break;
8930                 }
8931                 at += SCTP_SIZE32(plen);
8932         } while (phdr);
8933         if (cookie == NULL) {
8934                 /* Did not find the cookie */
8935                 return (-3);
8936         }
8937         /* ok, we got the cookie lets change it into a cookie echo chunk */
8938
8939         /* first the change from param to cookie */
8940         hdr = mtod(cookie, struct sctp_chunkhdr *);
8941         hdr->chunk_type = SCTP_COOKIE_ECHO;
8942         hdr->chunk_flags = 0;
8943         /* get the chunk stuff now and place it in the FRONT of the queue */
8944         sctp_alloc_a_chunk(stcb, chk);
8945         if (chk == NULL) {
8946                 /* no memory */
8947                 sctp_m_freem(cookie);
8948                 return (-5);
8949         }
8950         chk->copy_by_ref = 0;
8951         chk->send_size = plen;
8952         chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8953         chk->rec.chunk_id.can_take_data = 0;
8954         chk->sent = SCTP_DATAGRAM_UNSENT;
8955         chk->snd_count = 0;
8956         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8957         chk->asoc = &stcb->asoc;
8958         chk->data = cookie;
8959         chk->whoTo = net;
8960         atomic_add_int(&chk->whoTo->ref_count, 1);
8961         TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8962         chk->asoc->ctrl_queue_cnt++;
8963         return (0);
8964 }
8965
8966 void
8967 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8968     struct mbuf *m,
8969     int offset,
8970     int chk_length,
8971     struct sctp_nets *net)
8972 {
8973         /*
8974          * take a HB request and make it into a HB ack and send it.
8975          */
8976         struct mbuf *outchain;
8977         struct sctp_chunkhdr *chdr;
8978         struct sctp_tmit_chunk *chk;
8979
8980
8981         if (net == NULL)
8982                 /* must have a net pointer */
8983                 return;
8984
8985         outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8986         if (outchain == NULL) {
8987                 /* gak out of memory */
8988                 return;
8989         }
8990 #ifdef SCTP_MBUF_LOGGING
8991         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8992                 struct mbuf *mat;
8993
8994                 mat = outchain;
8995                 while (mat) {
8996                         if (SCTP_BUF_IS_EXTENDED(mat)) {
8997                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8998                         }
8999                         mat = SCTP_BUF_NEXT(mat);
9000                 }
9001         }
9002 #endif
9003         chdr = mtod(outchain, struct sctp_chunkhdr *);
9004         chdr->chunk_type = SCTP_HEARTBEAT_ACK;
9005         chdr->chunk_flags = 0;
9006         if (chk_length % 4) {
9007                 /* need pad */
9008                 uint32_t cpthis = 0;
9009                 int padlen;
9010
9011                 padlen = 4 - (chk_length % 4);
9012                 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
9013         }
9014         sctp_alloc_a_chunk(stcb, chk);
9015         if (chk == NULL) {
9016                 /* no memory */
9017                 sctp_m_freem(outchain);
9018                 return;
9019         }
9020         chk->copy_by_ref = 0;
9021         chk->send_size = chk_length;
9022         chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
9023         chk->rec.chunk_id.can_take_data = 1;
9024         chk->sent = SCTP_DATAGRAM_UNSENT;
9025         chk->snd_count = 0;
9026         chk->flags = 0;
9027         chk->asoc = &stcb->asoc;
9028         chk->data = outchain;
9029         chk->whoTo = net;
9030         atomic_add_int(&chk->whoTo->ref_count, 1);
9031         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9032         chk->asoc->ctrl_queue_cnt++;
9033 }
9034
9035 void
9036 sctp_send_cookie_ack(struct sctp_tcb *stcb)
9037 {
9038         /* formulate and queue a cookie-ack back to sender */
9039         struct mbuf *cookie_ack;
9040         struct sctp_chunkhdr *hdr;
9041         struct sctp_tmit_chunk *chk;
9042
9043         cookie_ack = NULL;
9044         SCTP_TCB_LOCK_ASSERT(stcb);
9045
9046         cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
9047         if (cookie_ack == NULL) {
9048                 /* no mbuf's */
9049                 return;
9050         }
9051         SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
9052         sctp_alloc_a_chunk(stcb, chk);
9053         if (chk == NULL) {
9054                 /* no memory */
9055                 sctp_m_freem(cookie_ack);
9056                 return;
9057         }
9058         chk->copy_by_ref = 0;
9059         chk->send_size = sizeof(struct sctp_chunkhdr);
9060         chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
9061         chk->rec.chunk_id.can_take_data = 1;
9062         chk->sent = SCTP_DATAGRAM_UNSENT;
9063         chk->snd_count = 0;
9064         chk->flags = 0;
9065         chk->asoc = &stcb->asoc;
9066         chk->data = cookie_ack;
9067         if (chk->asoc->last_control_chunk_from != NULL) {
9068                 chk->whoTo = chk->asoc->last_control_chunk_from;
9069                 atomic_add_int(&chk->whoTo->ref_count, 1);
9070         } else {
9071                 chk->whoTo = NULL;
9072         }
9073         hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
9074         hdr->chunk_type = SCTP_COOKIE_ACK;
9075         hdr->chunk_flags = 0;
9076         hdr->chunk_length = htons(chk->send_size);
9077         SCTP_BUF_LEN(cookie_ack) = chk->send_size;
9078         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9079         chk->asoc->ctrl_queue_cnt++;
9080         return;
9081 }
9082
9083
9084 void
9085 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
9086 {
9087         /* formulate and queue a SHUTDOWN-ACK back to the sender */
9088         struct mbuf *m_shutdown_ack;
9089         struct sctp_shutdown_ack_chunk *ack_cp;
9090         struct sctp_tmit_chunk *chk;
9091
9092         m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9093         if (m_shutdown_ack == NULL) {
9094                 /* no mbuf's */
9095                 return;
9096         }
9097         SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
9098         sctp_alloc_a_chunk(stcb, chk);
9099         if (chk == NULL) {
9100                 /* no memory */
9101                 sctp_m_freem(m_shutdown_ack);
9102                 return;
9103         }
9104         chk->copy_by_ref = 0;
9105         chk->send_size = sizeof(struct sctp_chunkhdr);
9106         chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
9107         chk->rec.chunk_id.can_take_data = 1;
9108         chk->sent = SCTP_DATAGRAM_UNSENT;
9109         chk->snd_count = 0;
9110         chk->flags = 0;
9111         chk->asoc = &stcb->asoc;
9112         chk->data = m_shutdown_ack;
9113         chk->whoTo = net;
9114         if (chk->whoTo) {
9115                 atomic_add_int(&chk->whoTo->ref_count, 1);
9116         }
9117         ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
9118         ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
9119         ack_cp->ch.chunk_flags = 0;
9120         ack_cp->ch.chunk_length = htons(chk->send_size);
9121         SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
9122         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9123         chk->asoc->ctrl_queue_cnt++;
9124         return;
9125 }
9126
9127 void
9128 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
9129 {
9130         /* formulate and queue a SHUTDOWN to the sender */
9131         struct mbuf *m_shutdown;
9132         struct sctp_shutdown_chunk *shutdown_cp;
9133         struct sctp_tmit_chunk *chk;
9134
9135         m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9136         if (m_shutdown == NULL) {
9137                 /* no mbuf's */
9138                 return;
9139         }
9140         SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
9141         sctp_alloc_a_chunk(stcb, chk);
9142         if (chk == NULL) {
9143                 /* no memory */
9144                 sctp_m_freem(m_shutdown);
9145                 return;
9146         }
9147         chk->copy_by_ref = 0;
9148         chk->send_size = sizeof(struct sctp_shutdown_chunk);
9149         chk->rec.chunk_id.id = SCTP_SHUTDOWN;
9150         chk->rec.chunk_id.can_take_data = 1;
9151         chk->sent = SCTP_DATAGRAM_UNSENT;
9152         chk->snd_count = 0;
9153         chk->flags = 0;
9154         chk->asoc = &stcb->asoc;
9155         chk->data = m_shutdown;
9156         chk->whoTo = net;
9157         if (chk->whoTo) {
9158                 atomic_add_int(&chk->whoTo->ref_count, 1);
9159         }
9160         shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
9161         shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
9162         shutdown_cp->ch.chunk_flags = 0;
9163         shutdown_cp->ch.chunk_length = htons(chk->send_size);
9164         shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9165         SCTP_BUF_LEN(m_shutdown) = chk->send_size;
9166         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9167         chk->asoc->ctrl_queue_cnt++;
9168         return;
9169 }
9170
9171 void
9172 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
9173 {
9174         /*
9175          * formulate and queue an ASCONF to the peer. ASCONF parameters
9176          * should be queued on the assoc queue.
9177          */
9178         struct sctp_tmit_chunk *chk;
9179         struct mbuf *m_asconf;
9180         int len;
9181
9182         SCTP_TCB_LOCK_ASSERT(stcb);
9183
9184         if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
9185             (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
9186                 /* can't send a new one if there is one in flight already */
9187                 return;
9188         }
9189         /* compose an ASCONF chunk, maximum length is PMTU */
9190         m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
9191         if (m_asconf == NULL) {
9192                 return;
9193         }
9194         sctp_alloc_a_chunk(stcb, chk);
9195         if (chk == NULL) {
9196                 /* no memory */
9197                 sctp_m_freem(m_asconf);
9198                 return;
9199         }
9200         chk->copy_by_ref = 0;
9201         chk->data = m_asconf;
9202         chk->send_size = len;
9203         chk->rec.chunk_id.id = SCTP_ASCONF;
9204         chk->rec.chunk_id.can_take_data = 0;
9205         chk->sent = SCTP_DATAGRAM_UNSENT;
9206         chk->snd_count = 0;
9207         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9208         chk->asoc = &stcb->asoc;
9209         chk->whoTo = net;
9210         if (chk->whoTo) {
9211                 atomic_add_int(&chk->whoTo->ref_count, 1);
9212         }
9213         TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
9214         chk->asoc->ctrl_queue_cnt++;
9215         return;
9216 }
9217
9218 void
9219 sctp_send_asconf_ack(struct sctp_tcb *stcb)
9220 {
9221         /*
9222          * formulate and queue a asconf-ack back to sender. the asconf-ack
9223          * must be stored in the tcb.
9224          */
9225         struct sctp_tmit_chunk *chk;
9226         struct sctp_asconf_ack *ack, *latest_ack;
9227         struct mbuf *m_ack, *m;
9228         struct sctp_nets *net = NULL;
9229
9230         SCTP_TCB_LOCK_ASSERT(stcb);
9231         /* Get the latest ASCONF-ACK */
9232         latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
9233         if (latest_ack == NULL) {
9234                 return;
9235         }
9236         if (latest_ack->last_sent_to != NULL &&
9237             latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
9238                 /* we're doing a retransmission */
9239                 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
9240                 if (net == NULL) {
9241                         /* no alternate */
9242                         if (stcb->asoc.last_control_chunk_from == NULL) {
9243                                 if (stcb->asoc.alternate) {
9244                                         net = stcb->asoc.alternate;
9245                                 } else {
9246                                         net = stcb->asoc.primary_destination;
9247                                 }
9248                         } else {
9249                                 net = stcb->asoc.last_control_chunk_from;
9250                         }
9251                 }
9252         } else {
9253                 /* normal case */
9254                 if (stcb->asoc.last_control_chunk_from == NULL) {
9255                         if (stcb->asoc.alternate) {
9256                                 net = stcb->asoc.alternate;
9257                         } else {
9258                                 net = stcb->asoc.primary_destination;
9259                         }
9260                 } else {
9261                         net = stcb->asoc.last_control_chunk_from;
9262                 }
9263         }
9264         latest_ack->last_sent_to = net;
9265
9266         TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
9267                 if (ack->data == NULL) {
9268                         continue;
9269                 }
9270                 /* copy the asconf_ack */
9271                 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
9272                 if (m_ack == NULL) {
9273                         /* couldn't copy it */
9274                         return;
9275                 }
9276 #ifdef SCTP_MBUF_LOGGING
9277                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9278                         struct mbuf *mat;
9279
9280                         mat = m_ack;
9281                         while (mat) {
9282                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
9283                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
9284                                 }
9285                                 mat = SCTP_BUF_NEXT(mat);
9286                         }
9287                 }
9288 #endif
9289
9290                 sctp_alloc_a_chunk(stcb, chk);
9291                 if (chk == NULL) {
9292                         /* no memory */
9293                         if (m_ack)
9294                                 sctp_m_freem(m_ack);
9295                         return;
9296                 }
9297                 chk->copy_by_ref = 0;
9298
9299                 chk->whoTo = net;
9300                 if (chk->whoTo) {
9301                         atomic_add_int(&chk->whoTo->ref_count, 1);
9302                 }
9303                 chk->data = m_ack;
9304                 chk->send_size = 0;
9305                 /* Get size */
9306                 m = m_ack;
9307                 chk->send_size = ack->len;
9308                 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
9309                 chk->rec.chunk_id.can_take_data = 1;
9310                 chk->sent = SCTP_DATAGRAM_UNSENT;
9311                 chk->snd_count = 0;
9312                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;  /* XXX */
9313                 chk->asoc = &stcb->asoc;
9314
9315                 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9316                 chk->asoc->ctrl_queue_cnt++;
9317         }
9318         return;
9319 }
9320
9321
9322 static int
9323 sctp_chunk_retransmission(struct sctp_inpcb *inp,
9324     struct sctp_tcb *stcb,
9325     struct sctp_association *asoc,
9326     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
9327 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9328     SCTP_UNUSED
9329 #endif
9330 )
9331 {
9332         /*-
9333          * send out one MTU of retransmission. If fast_retransmit is
9334          * happening we ignore the cwnd. Otherwise we obey the cwnd and
9335          * rwnd. For a Cookie or Asconf in the control chunk queue we
9336          * retransmit them by themselves.
9337          *
9338          * For data chunks we will pick out the lowest TSN's in the sent_queue
9339          * marked for resend and bundle them all together (up to a MTU of
9340          * destination). The address to send to should have been
9341          * selected/changed where the retransmission was marked (i.e. in FR
9342          * or t3-timeout routines).
9343          */
9344         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9345         struct sctp_tmit_chunk *chk, *fwd;
9346         struct mbuf *m, *endofchain;
9347         struct sctp_nets *net = NULL;
9348         uint32_t tsns_sent = 0;
9349         int no_fragmentflg, bundle_at, cnt_thru;
9350         unsigned int mtu;
9351         int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
9352         struct sctp_auth_chunk *auth = NULL;
9353         uint32_t auth_offset = 0;
9354         uint16_t auth_keyid;
9355         int override_ok = 1;
9356         int data_auth_reqd = 0;
9357         uint32_t dmtu = 0;
9358
9359         SCTP_TCB_LOCK_ASSERT(stcb);
9360         tmr_started = ctl_cnt = bundle_at = error = 0;
9361         no_fragmentflg = 1;
9362         fwd_tsn = 0;
9363         *cnt_out = 0;
9364         fwd = NULL;
9365         endofchain = m = NULL;
9366         auth_keyid = stcb->asoc.authinfo.active_keyid;
9367 #ifdef SCTP_AUDITING_ENABLED
9368         sctp_audit_log(0xC3, 1);
9369 #endif
9370         if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
9371             (TAILQ_EMPTY(&asoc->control_send_queue))) {
9372                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
9373                     asoc->sent_queue_retran_cnt);
9374                 asoc->sent_queue_cnt = 0;
9375                 asoc->sent_queue_cnt_removeable = 0;
9376                 /* send back 0/0 so we enter normal transmission */
9377                 *cnt_out = 0;
9378                 return (0);
9379         }
9380         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9381                 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
9382                     (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
9383                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
9384                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
9385                                 continue;
9386                         }
9387                         if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
9388                                 if (chk != asoc->str_reset) {
9389                                         /*
9390                                          * not eligible for retran if its
9391                                          * not ours
9392                                          */
9393                                         continue;
9394                                 }
9395                         }
9396                         ctl_cnt++;
9397                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9398                                 fwd_tsn = 1;
9399                                 fwd = chk;
9400                         }
9401                         /*
9402                          * Add an AUTH chunk, if chunk requires it save the
9403                          * offset into the chain for AUTH
9404                          */
9405                         if ((auth == NULL) &&
9406                             (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9407                             stcb->asoc.peer_auth_chunks))) {
9408                                 m = sctp_add_auth_chunk(m, &endofchain,
9409                                     &auth, &auth_offset,
9410                                     stcb,
9411                                     chk->rec.chunk_id.id);
9412                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9413                         }
9414                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9415                         break;
9416                 }
9417         }
9418         one_chunk = 0;
9419         cnt_thru = 0;
9420         /* do we have control chunks to retransmit? */
9421         if (m != NULL) {
9422                 /* Start a timer no matter if we suceed or fail */
9423                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9424                         sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9425                 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9426                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9427                 chk->snd_count++;       /* update our count */
9428                 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9429                     (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9430                     auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9431                     no_fragmentflg, 0, NULL, 0,
9432                     inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9433                     chk->whoTo->port, so_locked, NULL, NULL))) {
9434                         SCTP_STAT_INCR(sctps_lowlevelerr);
9435                         return (error);
9436                 }
9437                 m = endofchain = NULL;
9438                 auth = NULL;
9439                 auth_offset = 0;
9440                 /*
9441                  * We don't want to mark the net->sent time here since this
9442                  * we use this for HB and retrans cannot measure RTT
9443                  */
9444                 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9445                 *cnt_out += 1;
9446                 chk->sent = SCTP_DATAGRAM_SENT;
9447                 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9448                 if (fwd_tsn == 0) {
9449                         return (0);
9450                 } else {
9451                         /* Clean up the fwd-tsn list */
9452                         sctp_clean_up_ctl(stcb, asoc, so_locked);
9453                         return (0);
9454                 }
9455         }
9456         /*
9457          * Ok, it is just data retransmission we need to do or that and a
9458          * fwd-tsn with it all.
9459          */
9460         if (TAILQ_EMPTY(&asoc->sent_queue)) {
9461                 return (SCTP_RETRAN_DONE);
9462         }
9463         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9464             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9465                 /* not yet open, resend the cookie and that is it */
9466                 return (1);
9467         }
9468 #ifdef SCTP_AUDITING_ENABLED
9469         sctp_auditing(20, inp, stcb, NULL);
9470 #endif
9471         data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9472         TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9473                 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9474                         /* No, not sent to this net or not ready for rtx */
9475                         continue;
9476                 }
9477                 if (chk->data == NULL) {
9478                         printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9479                             chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
9480                         continue;
9481                 }
9482                 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9483                     (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9484                         /* Gak, we have exceeded max unlucky retran, abort! */
9485                         SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9486                             chk->snd_count,
9487                             SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9488                         atomic_add_int(&stcb->asoc.refcnt, 1);
9489                         sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9490                         SCTP_TCB_LOCK(stcb);
9491                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
9492                         return (SCTP_RETRAN_EXIT);
9493                 }
9494                 /* pick up the net */
9495                 net = chk->whoTo;
9496                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9497                         mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9498                 } else {
9499                         mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9500                 }
9501
9502                 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9503                         /* No room in peers rwnd */
9504                         uint32_t tsn;
9505
9506                         tsn = asoc->last_acked_seq + 1;
9507                         if (tsn == chk->rec.data.TSN_seq) {
9508                                 /*
9509                                  * we make a special exception for this
9510                                  * case. The peer has no rwnd but is missing
9511                                  * the lowest chunk.. which is probably what
9512                                  * is holding up the rwnd.
9513                                  */
9514                                 goto one_chunk_around;
9515                         }
9516                         return (1);
9517                 }
9518 one_chunk_around:
9519                 if (asoc->peers_rwnd < mtu) {
9520                         one_chunk = 1;
9521                         if ((asoc->peers_rwnd == 0) &&
9522                             (asoc->total_flight == 0)) {
9523                                 chk->window_probe = 1;
9524                                 chk->whoTo->window_probe = 1;
9525                         }
9526                 }
9527 #ifdef SCTP_AUDITING_ENABLED
9528                 sctp_audit_log(0xC3, 2);
9529 #endif
9530                 bundle_at = 0;
9531                 m = NULL;
9532                 net->fast_retran_ip = 0;
9533                 if (chk->rec.data.doing_fast_retransmit == 0) {
9534                         /*
9535                          * if no FR in progress skip destination that have
9536                          * flight_size > cwnd.
9537                          */
9538                         if (net->flight_size >= net->cwnd) {
9539                                 continue;
9540                         }
9541                 } else {
9542                         /*
9543                          * Mark the destination net to have FR recovery
9544                          * limits put on it.
9545                          */
9546                         *fr_done = 1;
9547                         net->fast_retran_ip = 1;
9548                 }
9549
9550                 /*
9551                  * if no AUTH is yet included and this chunk requires it,
9552                  * make sure to account for it.  We don't apply the size
9553                  * until the AUTH chunk is actually added below in case
9554                  * there is no room for this chunk.
9555                  */
9556                 if (data_auth_reqd && (auth == NULL)) {
9557                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9558                 } else
9559                         dmtu = 0;
9560
9561                 if ((chk->send_size <= (mtu - dmtu)) ||
9562                     (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9563                         /* ok we will add this one */
9564                         if (data_auth_reqd) {
9565                                 if (auth == NULL) {
9566                                         m = sctp_add_auth_chunk(m,
9567                                             &endofchain,
9568                                             &auth,
9569                                             &auth_offset,
9570                                             stcb,
9571                                             SCTP_DATA);
9572                                         auth_keyid = chk->auth_keyid;
9573                                         override_ok = 0;
9574                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9575                                 } else if (override_ok) {
9576                                         auth_keyid = chk->auth_keyid;
9577                                         override_ok = 0;
9578                                 } else if (chk->auth_keyid != auth_keyid) {
9579                                         /* different keyid, so done bundling */
9580                                         break;
9581                                 }
9582                         }
9583                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9584                         if (m == NULL) {
9585                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9586                                 return (ENOMEM);
9587                         }
9588                         /* Do clear IP_DF ? */
9589                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9590                                 no_fragmentflg = 0;
9591                         }
9592                         /* upate our MTU size */
9593                         if (mtu > (chk->send_size + dmtu))
9594                                 mtu -= (chk->send_size + dmtu);
9595                         else
9596                                 mtu = 0;
9597                         data_list[bundle_at++] = chk;
9598                         if (one_chunk && (asoc->total_flight <= 0)) {
9599                                 SCTP_STAT_INCR(sctps_windowprobed);
9600                         }
9601                 }
9602                 if (one_chunk == 0) {
9603                         /*
9604                          * now are there anymore forward from chk to pick
9605                          * up?
9606                          */
9607                         fwd = TAILQ_NEXT(chk, sctp_next);
9608                         while (fwd) {
9609                                 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9610                                         /* Nope, not for retran */
9611                                         fwd = TAILQ_NEXT(fwd, sctp_next);
9612                                         continue;
9613                                 }
9614                                 if (fwd->whoTo != net) {
9615                                         /* Nope, not the net in question */
9616                                         fwd = TAILQ_NEXT(fwd, sctp_next);
9617                                         continue;
9618                                 }
9619                                 if (data_auth_reqd && (auth == NULL)) {
9620                                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9621                                 } else
9622                                         dmtu = 0;
9623                                 if (fwd->send_size <= (mtu - dmtu)) {
9624                                         if (data_auth_reqd) {
9625                                                 if (auth == NULL) {
9626                                                         m = sctp_add_auth_chunk(m,
9627                                                             &endofchain,
9628                                                             &auth,
9629                                                             &auth_offset,
9630                                                             stcb,
9631                                                             SCTP_DATA);
9632                                                         auth_keyid = fwd->auth_keyid;
9633                                                         override_ok = 0;
9634                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9635                                                 } else if (override_ok) {
9636                                                         auth_keyid = fwd->auth_keyid;
9637                                                         override_ok = 0;
9638                                                 } else if (fwd->auth_keyid != auth_keyid) {
9639                                                         /*
9640                                                          * different keyid,
9641                                                          * so done bundling
9642                                                          */
9643                                                         break;
9644                                                 }
9645                                         }
9646                                         m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9647                                         if (m == NULL) {
9648                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9649                                                 return (ENOMEM);
9650                                         }
9651                                         /* Do clear IP_DF ? */
9652                                         if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9653                                                 no_fragmentflg = 0;
9654                                         }
9655                                         /* upate our MTU size */
9656                                         if (mtu > (fwd->send_size + dmtu))
9657                                                 mtu -= (fwd->send_size + dmtu);
9658                                         else
9659                                                 mtu = 0;
9660                                         data_list[bundle_at++] = fwd;
9661                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9662                                                 break;
9663                                         }
9664                                         fwd = TAILQ_NEXT(fwd, sctp_next);
9665                                 } else {
9666                                         /* can't fit so we are done */
9667                                         break;
9668                                 }
9669                         }
9670                 }
9671                 /* Is there something to send for this destination? */
9672                 if (m) {
9673                         /*
9674                          * No matter if we fail/or suceed we should start a
9675                          * timer. A failure is like a lost IP packet :-)
9676                          */
9677                         if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9678                                 /*
9679                                  * no timer running on this destination
9680                                  * restart it.
9681                                  */
9682                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9683                                 tmr_started = 1;
9684                         }
9685                         /* Now lets send it, if there is anything to send :> */
9686                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9687                             (struct sockaddr *)&net->ro._l_addr, m,
9688                             auth_offset, auth, auth_keyid,
9689                             no_fragmentflg, 0, NULL, 0,
9690                             inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9691                             net->port, so_locked, NULL, NULL))) {
9692                                 /* error, we could not output */
9693                                 SCTP_STAT_INCR(sctps_lowlevelerr);
9694                                 return (error);
9695                         }
9696                         m = endofchain = NULL;
9697                         auth = NULL;
9698                         auth_offset = 0;
9699                         /* For HB's */
9700                         /*
9701                          * We don't want to mark the net->sent time here
9702                          * since this we use this for HB and retrans cannot
9703                          * measure RTT
9704                          */
9705                         /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9706
9707                         /* For auto-close */
9708                         cnt_thru++;
9709                         if (*now_filled == 0) {
9710                                 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9711                                 *now = asoc->time_last_sent;
9712                                 *now_filled = 1;
9713                         } else {
9714                                 asoc->time_last_sent = *now;
9715                         }
9716                         *cnt_out += bundle_at;
9717 #ifdef SCTP_AUDITING_ENABLED
9718                         sctp_audit_log(0xC4, bundle_at);
9719 #endif
9720                         if (bundle_at) {
9721                                 tsns_sent = data_list[0]->rec.data.TSN_seq;
9722                         }
9723                         for (i = 0; i < bundle_at; i++) {
9724                                 SCTP_STAT_INCR(sctps_sendretransdata);
9725                                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9726                                 /*
9727                                  * When we have a revoked data, and we
9728                                  * retransmit it, then we clear the revoked
9729                                  * flag since this flag dictates if we
9730                                  * subtracted from the fs
9731                                  */
9732                                 if (data_list[i]->rec.data.chunk_was_revoked) {
9733                                         /* Deflate the cwnd */
9734                                         data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9735                                         data_list[i]->rec.data.chunk_was_revoked = 0;
9736                                 }
9737                                 data_list[i]->snd_count++;
9738                                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9739                                 /* record the time */
9740                                 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9741                                 if (data_list[i]->book_size_scale) {
9742                                         /*
9743                                          * need to double the book size on
9744                                          * this one
9745                                          */
9746                                         data_list[i]->book_size_scale = 0;
9747                                         /*
9748                                          * Since we double the booksize, we
9749                                          * must also double the output queue
9750                                          * size, since this get shrunk when
9751                                          * we free by this amount.
9752                                          */
9753                                         atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9754                                         data_list[i]->book_size *= 2;
9755
9756
9757                                 } else {
9758                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9759                                                 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9760                                                     asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9761                                         }
9762                                         asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9763                                             (uint32_t) (data_list[i]->send_size +
9764                                             SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9765                                 }
9766                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9767                                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9768                                             data_list[i]->whoTo->flight_size,
9769                                             data_list[i]->book_size,
9770                                             (uintptr_t) data_list[i]->whoTo,
9771                                             data_list[i]->rec.data.TSN_seq);
9772                                 }
9773                                 sctp_flight_size_increase(data_list[i]);
9774                                 sctp_total_flight_increase(stcb, data_list[i]);
9775                                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9776                                         /* SWS sender side engages */
9777                                         asoc->peers_rwnd = 0;
9778                                 }
9779                                 if ((i == 0) &&
9780                                     (data_list[i]->rec.data.doing_fast_retransmit)) {
9781                                         SCTP_STAT_INCR(sctps_sendfastretrans);
9782                                         if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9783                                             (tmr_started == 0)) {
9784                                                 /*-
9785                                                  * ok we just fast-retrans'd
9786                                                  * the lowest TSN, i.e the
9787                                                  * first on the list. In
9788                                                  * this case we want to give
9789                                                  * some more time to get a
9790                                                  * SACK back without a
9791                                                  * t3-expiring.
9792                                                  */
9793                                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9794                                                     SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9795                                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9796                                         }
9797                                 }
9798                         }
9799                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9800                                 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9801                         }
9802 #ifdef SCTP_AUDITING_ENABLED
9803                         sctp_auditing(21, inp, stcb, NULL);
9804 #endif
9805                 } else {
9806                         /* None will fit */
9807                         return (1);
9808                 }
9809                 if (asoc->sent_queue_retran_cnt <= 0) {
9810                         /* all done we have no more to retran */
9811                         asoc->sent_queue_retran_cnt = 0;
9812                         break;
9813                 }
9814                 if (one_chunk) {
9815                         /* No more room in rwnd */
9816                         return (1);
9817                 }
9818                 /* stop the for loop here. we sent out a packet */
9819                 break;
9820         }
9821         return (0);
9822 }
9823
9824
9825 static int
9826 sctp_timer_validation(struct sctp_inpcb *inp,
9827     struct sctp_tcb *stcb,
9828     struct sctp_association *asoc,
9829     int ret)
9830 {
9831         struct sctp_nets *net;
9832
9833         /* Validate that a timer is running somewhere */
9834         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9835                 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9836                         /* Here is a timer */
9837                         return (ret);
9838                 }
9839         }
9840         SCTP_TCB_LOCK_ASSERT(stcb);
9841         /* Gak, we did not have a timer somewhere */
9842         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9843         if (asoc->alternate) {
9844                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
9845         } else {
9846                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9847         }
9848         return (ret);
9849 }
9850
9851 void
9852 sctp_chunk_output(struct sctp_inpcb *inp,
9853     struct sctp_tcb *stcb,
9854     int from_where,
9855     int so_locked
9856 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9857     SCTP_UNUSED
9858 #endif
9859 )
9860 {
9861         /*-
9862          * Ok this is the generic chunk service queue. we must do the
9863          * following:
9864          * - See if there are retransmits pending, if so we must
9865          *   do these first.
9866          * - Service the stream queue that is next, moving any
9867          *   message (note I must get a complete message i.e.
9868          *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9869          *   TSN's
9870          * - Check to see if the cwnd/rwnd allows any output, if so we
9871          *   go ahead and fomulate and send the low level chunks. Making sure
9872          *   to combine any control in the control chunk queue also.
9873          */
9874         struct sctp_association *asoc;
9875         struct sctp_nets *net;
9876         int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0;
9877         unsigned int burst_cnt = 0;
9878         struct timeval now;
9879         int now_filled = 0;
9880         int nagle_on;
9881         int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9882         int un_sent = 0;
9883         int fr_done;
9884         unsigned int tot_frs = 0;
9885
9886         asoc = &stcb->asoc;
9887         /* The Nagle algorithm is only applied when handling a send call. */
9888         if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9889                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9890                         nagle_on = 0;
9891                 } else {
9892                         nagle_on = 1;
9893                 }
9894         } else {
9895                 nagle_on = 0;
9896         }
9897         SCTP_TCB_LOCK_ASSERT(stcb);
9898
9899         un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9900
9901         if ((un_sent <= 0) &&
9902             (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9903             (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9904             (asoc->sent_queue_retran_cnt == 0)) {
9905                 /* Nothing to do unless there is something to be sent left */
9906                 return;
9907         }
9908         /*
9909          * Do we have something to send, data or control AND a sack timer
9910          * running, if so piggy-back the sack.
9911          */
9912         if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9913                 sctp_send_sack(stcb, so_locked);
9914                 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9915         }
9916         while (asoc->sent_queue_retran_cnt) {
9917                 /*-
9918                  * Ok, it is retransmission time only, we send out only ONE
9919                  * packet with a single call off to the retran code.
9920                  */
9921                 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9922                         /*-
9923                          * Special hook for handling cookiess discarded
9924                          * by peer that carried data. Send cookie-ack only
9925                          * and then the next call with get the retran's.
9926                          */
9927                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9928                             from_where,
9929                             &now, &now_filled, frag_point, so_locked);
9930                         return;
9931                 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9932                         /* if its not from a HB then do it */
9933                         fr_done = 0;
9934                         ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9935                         if (fr_done) {
9936                                 tot_frs++;
9937                         }
9938                 } else {
9939                         /*
9940                          * its from any other place, we don't allow retran
9941                          * output (only control)
9942                          */
9943                         ret = 1;
9944                 }
9945                 if (ret > 0) {
9946                         /* Can't send anymore */
9947                         /*-
9948                          * now lets push out control by calling med-level
9949                          * output once. this assures that we WILL send HB's
9950                          * if queued too.
9951                          */
9952                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9953                             from_where,
9954                             &now, &now_filled, frag_point, so_locked);
9955 #ifdef SCTP_AUDITING_ENABLED
9956                         sctp_auditing(8, inp, stcb, NULL);
9957 #endif
9958                         (void)sctp_timer_validation(inp, stcb, asoc, ret);
9959                         return;
9960                 }
9961                 if (ret < 0) {
9962                         /*-
9963                          * The count was off.. retran is not happening so do
9964                          * the normal retransmission.
9965                          */
9966 #ifdef SCTP_AUDITING_ENABLED
9967                         sctp_auditing(9, inp, stcb, NULL);
9968 #endif
9969                         if (ret == SCTP_RETRAN_EXIT) {
9970                                 return;
9971                         }
9972                         break;
9973                 }
9974                 if (from_where == SCTP_OUTPUT_FROM_T3) {
9975                         /* Only one transmission allowed out of a timeout */
9976 #ifdef SCTP_AUDITING_ENABLED
9977                         sctp_auditing(10, inp, stcb, NULL);
9978 #endif
9979                         /* Push out any control */
9980                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9981                             &now, &now_filled, frag_point, so_locked);
9982                         return;
9983                 }
9984                 if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
9985                         /* Hit FR burst limit */
9986                         return;
9987                 }
9988                 if ((num_out == 0) && (ret == 0)) {
9989                         /* No more retrans to send */
9990                         break;
9991                 }
9992         }
9993 #ifdef SCTP_AUDITING_ENABLED
9994         sctp_auditing(12, inp, stcb, NULL);
9995 #endif
9996         /* Check for bad destinations, if they exist move chunks around. */
9997         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9998                 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
9999                         /*-
10000                          * if possible move things off of this address we
10001                          * still may send below due to the dormant state but
10002                          * we try to find an alternate address to send to
10003                          * and if we have one we move all queued data on the
10004                          * out wheel to this alternate address.
10005                          */
10006                         if (net->ref_count > 1)
10007                                 sctp_move_chunks_from_net(stcb, net);
10008                 } else {
10009                         /*-
10010                          * if ((asoc->sat_network) || (net->addr_is_local))
10011                          * { burst_limit = asoc->max_burst *
10012                          * SCTP_SAT_NETWORK_BURST_INCR; }
10013                          */
10014                         if (asoc->max_burst > 0) {
10015                                 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10016                                         if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10017                                                 /*
10018                                                  * JRS - Use the congestion
10019                                                  * control given in the
10020                                                  * congestion control module
10021                                                  */
10022                                                 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10023                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10024                                                         sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10025                                                 }
10026                                                 SCTP_STAT_INCR(sctps_maxburstqueued);
10027                                         }
10028                                         net->fast_retran_ip = 0;
10029                                 } else {
10030                                         if (net->flight_size == 0) {
10031                                                 /*
10032                                                  * Should be decaying the
10033                                                  * cwnd here
10034                                                  */
10035                                                 ;
10036                                         }
10037                                 }
10038                         }
10039                 }
10040
10041         }
10042         burst_cnt = 0;
10043         do {
10044                 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10045                     &reason_code, 0, from_where,
10046                     &now, &now_filled, frag_point, so_locked);
10047                 if (error) {
10048                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10049                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10050                                 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10051                         }
10052                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10053                                 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10054                                 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10055                         }
10056                         break;
10057                 }
10058                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10059
10060                 tot_out += num_out;
10061                 burst_cnt++;
10062                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10063                         sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10064                         if (num_out == 0) {
10065                                 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10066                         }
10067                 }
10068                 if (nagle_on) {
10069                         /*
10070                          * When the Nagle algorithm is used, look at how
10071                          * much is unsent, then if its smaller than an MTU
10072                          * and we have data in flight we stop, except if we
10073                          * are handling a fragmented user message.
10074                          */
10075                         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10076                             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
10077                         if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10078                             (stcb->asoc.total_flight > 0) &&
10079                             ((stcb->asoc.locked_on_sending == NULL) ||
10080                             sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
10081                                 break;
10082                         }
10083                 }
10084                 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10085                     TAILQ_EMPTY(&asoc->send_queue) &&
10086                     stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
10087                         /* Nothing left to send */
10088                         break;
10089                 }
10090                 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10091                         /* Nothing left to send */
10092                         break;
10093                 }
10094         } while (num_out &&
10095             ((asoc->max_burst == 0) ||
10096             SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10097             (burst_cnt < asoc->max_burst)));
10098
10099         if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10100                 if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10101                         SCTP_STAT_INCR(sctps_maxburstqueued);
10102                         asoc->burst_limit_applied = 1;
10103                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10104                                 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10105                         }
10106                 } else {
10107                         asoc->burst_limit_applied = 0;
10108                 }
10109         }
10110         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10111                 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10112         }
10113         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10114             tot_out);
10115
10116         /*-
10117          * Now we need to clean up the control chunk chain if a ECNE is on
10118          * it. It must be marked as UNSENT again so next call will continue
10119          * to send it until such time that we get a CWR, to remove it.
10120          */
10121         if (stcb->asoc.ecn_echo_cnt_onq)
10122                 sctp_fix_ecn_echo(asoc);
10123         return;
10124 }
10125
10126
10127 int
10128 sctp_output(inp, m, addr, control, p, flags)
10129         struct sctp_inpcb *inp;
10130         struct mbuf *m;
10131         struct sockaddr *addr;
10132         struct mbuf *control;
10133         struct thread *p;
10134         int flags;
10135 {
10136         if (inp == NULL) {
10137                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10138                 return (EINVAL);
10139         }
10140         if (inp->sctp_socket == NULL) {
10141                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10142                 return (EINVAL);
10143         }
10144         return (sctp_sosend(inp->sctp_socket,
10145             addr,
10146             (struct uio *)NULL,
10147             m,
10148             control,
10149             flags, p
10150             ));
10151 }
10152
10153 void
10154 send_forward_tsn(struct sctp_tcb *stcb,
10155     struct sctp_association *asoc)
10156 {
10157         struct sctp_tmit_chunk *chk;
10158         struct sctp_forward_tsn_chunk *fwdtsn;
10159         uint32_t advance_peer_ack_point;
10160
10161         SCTP_TCB_LOCK_ASSERT(stcb);
10162         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10163                 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10164                         /* mark it to unsent */
10165                         chk->sent = SCTP_DATAGRAM_UNSENT;
10166                         chk->snd_count = 0;
10167                         /* Do we correct its output location? */
10168                         if (chk->whoTo) {
10169                                 sctp_free_remote_addr(chk->whoTo);
10170                                 chk->whoTo = NULL;
10171                         }
10172                         goto sctp_fill_in_rest;
10173                 }
10174         }
10175         /* Ok if we reach here we must build one */
10176         sctp_alloc_a_chunk(stcb, chk);
10177         if (chk == NULL) {
10178                 return;
10179         }
10180         asoc->fwd_tsn_cnt++;
10181         chk->copy_by_ref = 0;
10182         chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10183         chk->rec.chunk_id.can_take_data = 0;
10184         chk->asoc = asoc;
10185         chk->whoTo = NULL;
10186         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10187         if (chk->data == NULL) {
10188                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10189                 return;
10190         }
10191         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10192         chk->sent = SCTP_DATAGRAM_UNSENT;
10193         chk->snd_count = 0;
10194         TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10195         asoc->ctrl_queue_cnt++;
10196 sctp_fill_in_rest:
10197         /*-
10198          * Here we go through and fill out the part that deals with
10199          * stream/seq of the ones we skip.
10200          */
10201         SCTP_BUF_LEN(chk->data) = 0;
10202         {
10203                 struct sctp_tmit_chunk *at, *tp1, *last;
10204                 struct sctp_strseq *strseq;
10205                 unsigned int cnt_of_space, i, ovh;
10206                 unsigned int space_needed;
10207                 unsigned int cnt_of_skipped = 0;
10208
10209                 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10210                         if (at->sent != SCTP_FORWARD_TSN_SKIP) {
10211                                 /* no more to look at */
10212                                 break;
10213                         }
10214                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10215                                 /* We don't report these */
10216                                 continue;
10217                         }
10218                         cnt_of_skipped++;
10219                 }
10220                 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10221                     (cnt_of_skipped * sizeof(struct sctp_strseq)));
10222
10223                 cnt_of_space = M_TRAILINGSPACE(chk->data);
10224
10225                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10226                         ovh = SCTP_MIN_OVERHEAD;
10227                 } else {
10228                         ovh = SCTP_MIN_V4_OVERHEAD;
10229                 }
10230                 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
10231                         /* trim to a mtu size */
10232                         cnt_of_space = asoc->smallest_mtu - ovh;
10233                 }
10234                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10235                         sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10236                             0xff, 0, cnt_of_skipped,
10237                             asoc->advanced_peer_ack_point);
10238
10239                 }
10240                 advance_peer_ack_point = asoc->advanced_peer_ack_point;
10241                 if (cnt_of_space < space_needed) {
10242                         /*-
10243                          * ok we must trim down the chunk by lowering the
10244                          * advance peer ack point.
10245                          */
10246                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10247                                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10248                                     0xff, 0xff, cnt_of_space,
10249                                     space_needed);
10250                         }
10251                         cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
10252                         cnt_of_skipped /= sizeof(struct sctp_strseq);
10253                         /*-
10254                          * Go through and find the TSN that will be the one
10255                          * we report.
10256                          */
10257                         at = TAILQ_FIRST(&asoc->sent_queue);
10258                         for (i = 0; i < cnt_of_skipped; i++) {
10259                                 tp1 = TAILQ_NEXT(at, sctp_next);
10260                                 if (tp1 == NULL) {
10261                                         break;
10262                                 }
10263                                 at = tp1;
10264                         }
10265                         if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10266                                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10267                                     0xff, cnt_of_skipped, at->rec.data.TSN_seq,
10268                                     asoc->advanced_peer_ack_point);
10269                         }
10270                         last = at;
10271                         /*-
10272                          * last now points to last one I can report, update
10273                          * peer ack point
10274                          */
10275                         if (last)
10276                                 advance_peer_ack_point = last->rec.data.TSN_seq;
10277                         space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10278                             cnt_of_skipped * sizeof(struct sctp_strseq);
10279                 }
10280                 chk->send_size = space_needed;
10281                 /* Setup the chunk */
10282                 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
10283                 fwdtsn->ch.chunk_length = htons(chk->send_size);
10284                 fwdtsn->ch.chunk_flags = 0;
10285                 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
10286                 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
10287                 SCTP_BUF_LEN(chk->data) = chk->send_size;
10288                 fwdtsn++;
10289                 /*-
10290                  * Move pointer to after the fwdtsn and transfer to the
10291                  * strseq pointer.
10292                  */
10293                 strseq = (struct sctp_strseq *)fwdtsn;
10294                 /*-
10295                  * Now populate the strseq list. This is done blindly
10296                  * without pulling out duplicate stream info. This is
10297                  * inefficent but won't harm the process since the peer will
10298                  * look at these in sequence and will thus release anything.
10299                  * It could mean we exceed the PMTU and chop off some that
10300                  * we could have included.. but this is unlikely (aka 1432/4
10301                  * would mean 300+ stream seq's would have to be reported in
10302                  * one FWD-TSN. With a bit of work we can later FIX this to
10303                  * optimize and pull out duplcates.. but it does add more
10304                  * overhead. So for now... not!
10305                  */
10306                 at = TAILQ_FIRST(&asoc->sent_queue);
10307                 for (i = 0; i < cnt_of_skipped; i++) {
10308                         tp1 = TAILQ_NEXT(at, sctp_next);
10309                         if (tp1 == NULL)
10310                                 break;
10311                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10312                                 /* We don't report these */
10313                                 i--;
10314                                 at = tp1;
10315                                 continue;
10316                         }
10317                         if (at->rec.data.TSN_seq == advance_peer_ack_point) {
10318                                 at->rec.data.fwd_tsn_cnt = 0;
10319                         }
10320                         strseq->stream = ntohs(at->rec.data.stream_number);
10321                         strseq->sequence = ntohs(at->rec.data.stream_seq);
10322                         strseq++;
10323                         at = tp1;
10324                 }
10325         }
10326         return;
10327
10328 }
10329
10330 void
10331 sctp_send_sack(struct sctp_tcb *stcb, int so_locked
10332 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10333     SCTP_UNUSED
10334 #endif
10335 )
10336 {
10337         /*-
10338          * Queue up a SACK or NR-SACK in the control queue.
10339          * We must first check to see if a SACK or NR-SACK is
10340          * somehow on the control queue.
10341          * If so, we will take and and remove the old one.
10342          */
10343         struct sctp_association *asoc;
10344         struct sctp_tmit_chunk *chk, *a_chk;
10345         struct sctp_sack_chunk *sack;
10346         struct sctp_nr_sack_chunk *nr_sack;
10347         struct sctp_gap_ack_block *gap_descriptor;
10348         struct sack_track *selector;
10349         int mergeable = 0;
10350         int offset;
10351         caddr_t limit;
10352         uint32_t *dup;
10353         int limit_reached = 0;
10354         unsigned int i, siz, j;
10355         unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10356         int num_dups = 0;
10357         int space_req;
10358         uint32_t highest_tsn;
10359         uint8_t flags;
10360         uint8_t type;
10361         uint8_t tsn_map;
10362
10363         if ((stcb->asoc.sctp_nr_sack_on_off == 1) &&
10364             (stcb->asoc.peer_supports_nr_sack == 1)) {
10365                 type = SCTP_NR_SELECTIVE_ACK;
10366         } else {
10367                 type = SCTP_SELECTIVE_ACK;
10368         }
10369         a_chk = NULL;
10370         asoc = &stcb->asoc;
10371         SCTP_TCB_LOCK_ASSERT(stcb);
10372         if (asoc->last_data_chunk_from == NULL) {
10373                 /* Hmm we never received anything */
10374                 return;
10375         }
10376         sctp_slide_mapping_arrays(stcb);
10377         sctp_set_rwnd(stcb, asoc);
10378         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10379                 if (chk->rec.chunk_id.id == type) {
10380                         /* Hmm, found a sack already on queue, remove it */
10381                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10382                         asoc->ctrl_queue_cnt--;
10383                         a_chk = chk;
10384                         if (a_chk->data) {
10385                                 sctp_m_freem(a_chk->data);
10386                                 a_chk->data = NULL;
10387                         }
10388                         if (a_chk->whoTo) {
10389                                 sctp_free_remote_addr(a_chk->whoTo);
10390                                 a_chk->whoTo = NULL;
10391                         }
10392                         break;
10393                 }
10394         }
10395         if (a_chk == NULL) {
10396                 sctp_alloc_a_chunk(stcb, a_chk);
10397                 if (a_chk == NULL) {
10398                         /* No memory so we drop the idea, and set a timer */
10399                         if (stcb->asoc.delayed_ack) {
10400                                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10401                                     stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10402                                 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10403                                     stcb->sctp_ep, stcb, NULL);
10404                         } else {
10405                                 stcb->asoc.send_sack = 1;
10406                         }
10407                         return;
10408                 }
10409                 a_chk->copy_by_ref = 0;
10410                 a_chk->rec.chunk_id.id = type;
10411                 a_chk->rec.chunk_id.can_take_data = 1;
10412         }
10413         /* Clear our pkt counts */
10414         asoc->data_pkts_seen = 0;
10415
10416         a_chk->asoc = asoc;
10417         a_chk->snd_count = 0;
10418         a_chk->send_size = 0;   /* fill in later */
10419         a_chk->sent = SCTP_DATAGRAM_UNSENT;
10420         a_chk->whoTo = NULL;
10421
10422         if ((asoc->numduptsns) ||
10423             (!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE))) {
10424                 /*-
10425                  * Ok, we have some duplicates or the destination for the
10426                  * sack is unreachable, lets see if we can select an
10427                  * alternate than asoc->last_data_chunk_from
10428                  */
10429                 if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) &&
10430                     (asoc->used_alt_onsack > asoc->numnets)) {
10431                         /* We used an alt last time, don't this time */
10432                         a_chk->whoTo = NULL;
10433                 } else {
10434                         asoc->used_alt_onsack++;
10435                         a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10436                 }
10437                 if (a_chk->whoTo == NULL) {
10438                         /* Nope, no alternate */
10439                         a_chk->whoTo = asoc->last_data_chunk_from;
10440                         asoc->used_alt_onsack = 0;
10441                 }
10442         } else {
10443                 /*
10444                  * No duplicates so we use the last place we received data
10445                  * from.
10446                  */
10447                 asoc->used_alt_onsack = 0;
10448                 a_chk->whoTo = asoc->last_data_chunk_from;
10449         }
10450         if (a_chk->whoTo) {
10451                 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10452         }
10453         if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10454                 highest_tsn = asoc->highest_tsn_inside_map;
10455         } else {
10456                 highest_tsn = asoc->highest_tsn_inside_nr_map;
10457         }
10458         if (highest_tsn == asoc->cumulative_tsn) {
10459                 /* no gaps */
10460                 if (type == SCTP_SELECTIVE_ACK) {
10461                         space_req = sizeof(struct sctp_sack_chunk);
10462                 } else {
10463                         space_req = sizeof(struct sctp_nr_sack_chunk);
10464                 }
10465         } else {
10466                 /* gaps get a cluster */
10467                 space_req = MCLBYTES;
10468         }
10469         /* Ok now lets formulate a MBUF with our sack */
10470         a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10471         if ((a_chk->data == NULL) ||
10472             (a_chk->whoTo == NULL)) {
10473                 /* rats, no mbuf memory */
10474                 if (a_chk->data) {
10475                         /* was a problem with the destination */
10476                         sctp_m_freem(a_chk->data);
10477                         a_chk->data = NULL;
10478                 }
10479                 sctp_free_a_chunk(stcb, a_chk, so_locked);
10480                 /* sa_ignore NO_NULL_CHK */
10481                 if (stcb->asoc.delayed_ack) {
10482                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10483                             stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10484                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10485                             stcb->sctp_ep, stcb, NULL);
10486                 } else {
10487                         stcb->asoc.send_sack = 1;
10488                 }
10489                 return;
10490         }
10491         /* ok, lets go through and fill it in */
10492         SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10493         space = M_TRAILINGSPACE(a_chk->data);
10494         if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10495                 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10496         }
10497         limit = mtod(a_chk->data, caddr_t);
10498         limit += space;
10499
10500         flags = 0;
10501
10502         if ((asoc->sctp_cmt_on_off > 0) &&
10503             SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10504                 /*-
10505                  * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10506                  * received, then set high bit to 1, else 0. Reset
10507                  * pkts_rcvd.
10508                  */
10509                 flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10510                 asoc->cmt_dac_pkts_rcvd = 0;
10511         }
10512 #ifdef SCTP_ASOCLOG_OF_TSNS
10513         stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10514         stcb->asoc.cumack_log_atsnt++;
10515         if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10516                 stcb->asoc.cumack_log_atsnt = 0;
10517         }
10518 #endif
10519         /* reset the readers interpretation */
10520         stcb->freed_by_sorcv_sincelast = 0;
10521
10522         if (type == SCTP_SELECTIVE_ACK) {
10523                 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10524                 nr_sack = NULL;
10525                 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10526                 if (highest_tsn > asoc->mapping_array_base_tsn) {
10527                         siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10528                 } else {
10529                         siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10530                 }
10531         } else {
10532                 sack = NULL;
10533                 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10534                 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10535                 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10536                         siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10537                 } else {
10538                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10539                 }
10540         }
10541
10542         if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10543                 offset = 1;
10544         } else {
10545                 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10546         }
10547         if (((type == SCTP_SELECTIVE_ACK) &&
10548             SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10549             ((type == SCTP_NR_SELECTIVE_ACK) &&
10550             SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10551                 /* we have a gap .. maybe */
10552                 for (i = 0; i < siz; i++) {
10553                         tsn_map = asoc->mapping_array[i];
10554                         if (type == SCTP_SELECTIVE_ACK) {
10555                                 tsn_map |= asoc->nr_mapping_array[i];
10556                         }
10557                         if (i == 0) {
10558                                 /*
10559                                  * Clear all bits corresponding to TSNs
10560                                  * smaller or equal to the cumulative TSN.
10561                                  */
10562                                 tsn_map &= (~0 << (1 - offset));
10563                         }
10564                         selector = &sack_array[tsn_map];
10565                         if (mergeable && selector->right_edge) {
10566                                 /*
10567                                  * Backup, left and right edges were ok to
10568                                  * merge.
10569                                  */
10570                                 num_gap_blocks--;
10571                                 gap_descriptor--;
10572                         }
10573                         if (selector->num_entries == 0)
10574                                 mergeable = 0;
10575                         else {
10576                                 for (j = 0; j < selector->num_entries; j++) {
10577                                         if (mergeable && selector->right_edge) {
10578                                                 /*
10579                                                  * do a merge by NOT setting
10580                                                  * the left side
10581                                                  */
10582                                                 mergeable = 0;
10583                                         } else {
10584                                                 /*
10585                                                  * no merge, set the left
10586                                                  * side
10587                                                  */
10588                                                 mergeable = 0;
10589                                                 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10590                                         }
10591                                         gap_descriptor->end = htons((selector->gaps[j].end + offset));
10592                                         num_gap_blocks++;
10593                                         gap_descriptor++;
10594                                         if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10595                                                 /* no more room */
10596                                                 limit_reached = 1;
10597                                                 break;
10598                                         }
10599                                 }
10600                                 if (selector->left_edge) {
10601                                         mergeable = 1;
10602                                 }
10603                         }
10604                         if (limit_reached) {
10605                                 /* Reached the limit stop */
10606                                 break;
10607                         }
10608                         offset += 8;
10609                 }
10610         }
10611         if ((type == SCTP_NR_SELECTIVE_ACK) &&
10612             (limit_reached == 0)) {
10613
10614                 mergeable = 0;
10615
10616                 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10617                         siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10618                 } else {
10619                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10620                 }
10621
10622                 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10623                         offset = 1;
10624                 } else {
10625                         offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10626                 }
10627                 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10628                         /* we have a gap .. maybe */
10629                         for (i = 0; i < siz; i++) {
10630                                 tsn_map = asoc->nr_mapping_array[i];
10631                                 if (i == 0) {
10632                                         /*
10633                                          * Clear all bits corresponding to
10634                                          * TSNs smaller or equal to the
10635                                          * cumulative TSN.
10636                                          */
10637                                         tsn_map &= (~0 << (1 - offset));
10638                                 }
10639                                 selector = &sack_array[tsn_map];
10640                                 if (mergeable && selector->right_edge) {
10641                                         /*
10642                                          * Backup, left and right edges were
10643                                          * ok to merge.
10644                                          */
10645                                         num_nr_gap_blocks--;
10646                                         gap_descriptor--;
10647                                 }
10648                                 if (selector->num_entries == 0)
10649                                         mergeable = 0;
10650                                 else {
10651                                         for (j = 0; j < selector->num_entries; j++) {
10652                                                 if (mergeable && selector->right_edge) {
10653                                                         /*
10654                                                          * do a merge by NOT
10655                                                          * setting the left
10656                                                          * side
10657                                                          */
10658                                                         mergeable = 0;
10659                                                 } else {
10660                                                         /*
10661                                                          * no merge, set the
10662                                                          * left side
10663                                                          */
10664                                                         mergeable = 0;
10665                                                         gap_descriptor->start = htons((selector->gaps[j].start + offset));
10666                                                 }
10667                                                 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10668                                                 num_nr_gap_blocks++;
10669                                                 gap_descriptor++;
10670                                                 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10671                                                         /* no more room */
10672                                                         limit_reached = 1;
10673                                                         break;
10674                                                 }
10675                                         }
10676                                         if (selector->left_edge) {
10677                                                 mergeable = 1;
10678                                         }
10679                                 }
10680                                 if (limit_reached) {
10681                                         /* Reached the limit stop */
10682                                         break;
10683                                 }
10684                                 offset += 8;
10685                         }
10686                 }
10687         }
10688         /* now we must add any dups we are going to report. */
10689         if ((limit_reached == 0) && (asoc->numduptsns)) {
10690                 dup = (uint32_t *) gap_descriptor;
10691                 for (i = 0; i < asoc->numduptsns; i++) {
10692                         *dup = htonl(asoc->dup_tsns[i]);
10693                         dup++;
10694                         num_dups++;
10695                         if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10696                                 /* no more room */
10697                                 break;
10698                         }
10699                 }
10700                 asoc->numduptsns = 0;
10701         }
10702         /*
10703          * now that the chunk is prepared queue it to the control chunk
10704          * queue.
10705          */
10706         if (type == SCTP_SELECTIVE_ACK) {
10707                 a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10708                     (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10709                     num_dups * sizeof(int32_t);
10710                 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10711                 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10712                 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10713                 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10714                 sack->sack.num_dup_tsns = htons(num_dups);
10715                 sack->ch.chunk_type = type;
10716                 sack->ch.chunk_flags = flags;
10717                 sack->ch.chunk_length = htons(a_chk->send_size);
10718         } else {
10719                 a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10720                     (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10721                     num_dups * sizeof(int32_t);
10722                 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10723                 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10724                 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10725                 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10726                 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10727                 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10728                 nr_sack->nr_sack.reserved = 0;
10729                 nr_sack->ch.chunk_type = type;
10730                 nr_sack->ch.chunk_flags = flags;
10731                 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10732         }
10733         TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10734         asoc->my_last_reported_rwnd = asoc->my_rwnd;
10735         asoc->ctrl_queue_cnt++;
10736         asoc->send_sack = 0;
10737         SCTP_STAT_INCR(sctps_sendsacks);
10738         return;
10739 }
10740
10741 void
10742 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10743 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10744     SCTP_UNUSED
10745 #endif
10746 )
10747 {
10748         struct mbuf *m_abort;
10749         struct mbuf *m_out = NULL, *m_end = NULL;
10750         struct sctp_abort_chunk *abort = NULL;
10751         int sz;
10752         uint32_t auth_offset = 0;
10753         struct sctp_auth_chunk *auth = NULL;
10754         struct sctp_nets *net;
10755
10756         /*-
10757          * Add an AUTH chunk, if chunk requires it and save the offset into
10758          * the chain for AUTH
10759          */
10760         if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10761             stcb->asoc.peer_auth_chunks)) {
10762                 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10763                     stcb, SCTP_ABORT_ASSOCIATION);
10764                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10765         }
10766         SCTP_TCB_LOCK_ASSERT(stcb);
10767         m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10768         if (m_abort == NULL) {
10769                 /* no mbuf's */
10770                 if (m_out)
10771                         sctp_m_freem(m_out);
10772                 return;
10773         }
10774         /* link in any error */
10775         SCTP_BUF_NEXT(m_abort) = operr;
10776         sz = 0;
10777         if (operr) {
10778                 struct mbuf *n;
10779
10780                 n = operr;
10781                 while (n) {
10782                         sz += SCTP_BUF_LEN(n);
10783                         n = SCTP_BUF_NEXT(n);
10784                 }
10785         }
10786         SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10787         if (m_out == NULL) {
10788                 /* NO Auth chunk prepended, so reserve space in front */
10789                 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10790                 m_out = m_abort;
10791         } else {
10792                 /* Put AUTH chunk at the front of the chain */
10793                 SCTP_BUF_NEXT(m_end) = m_abort;
10794         }
10795         if (stcb->asoc.alternate) {
10796                 net = stcb->asoc.alternate;
10797         } else {
10798                 net = stcb->asoc.primary_destination;
10799         }
10800         /* fill in the ABORT chunk */
10801         abort = mtod(m_abort, struct sctp_abort_chunk *);
10802         abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10803         abort->ch.chunk_flags = 0;
10804         abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10805
10806         (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10807             (struct sockaddr *)&net->ro._l_addr,
10808             m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10809             stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10810             stcb->asoc.primary_destination->port, so_locked, NULL, NULL);
10811         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10812 }
10813
10814 void
10815 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10816     struct sctp_nets *net,
10817     int reflect_vtag)
10818 {
10819         /* formulate and SEND a SHUTDOWN-COMPLETE */
10820         struct mbuf *m_shutdown_comp;
10821         struct sctp_shutdown_complete_chunk *shutdown_complete;
10822         uint32_t vtag;
10823         uint8_t flags;
10824
10825         m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10826         if (m_shutdown_comp == NULL) {
10827                 /* no mbuf's */
10828                 return;
10829         }
10830         if (reflect_vtag) {
10831                 flags = SCTP_HAD_NO_TCB;
10832                 vtag = stcb->asoc.my_vtag;
10833         } else {
10834                 flags = 0;
10835                 vtag = stcb->asoc.peer_vtag;
10836         }
10837         shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10838         shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10839         shutdown_complete->ch.chunk_flags = flags;
10840         shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10841         SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10842         (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10843             (struct sockaddr *)&net->ro._l_addr,
10844             m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10845             stcb->sctp_ep->sctp_lport, stcb->rport,
10846             htonl(vtag),
10847             net->port, SCTP_SO_NOT_LOCKED, NULL, NULL);
10848         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10849         return;
10850 }
10851
10852 void
10853 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10854     uint32_t vrf_id, uint16_t port)
10855 {
10856         /* formulate and SEND a SHUTDOWN-COMPLETE */
10857         struct mbuf *o_pak;
10858         struct mbuf *mout;
10859         struct ip *iph;
10860         struct udphdr *udp = NULL;
10861         int offset_out, len, mlen;
10862         struct sctp_shutdown_complete_msg *comp_cp;
10863
10864 #ifdef INET
10865         struct ip *iph_out;
10866
10867 #endif
10868 #ifdef INET6
10869         struct ip6_hdr *ip6, *ip6_out;
10870
10871 #endif
10872
10873         iph = mtod(m, struct ip *);
10874         switch (iph->ip_v) {
10875 #ifdef INET
10876         case IPVERSION:
10877                 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10878                 break;
10879 #endif
10880 #ifdef INET6
10881         case IPV6_VERSION >> 4:
10882                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10883                 break;
10884 #endif
10885         default:
10886                 return;
10887         }
10888         if (port) {
10889                 len += sizeof(struct udphdr);
10890         }
10891         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10892         if (mout == NULL) {
10893                 return;
10894         }
10895         SCTP_BUF_RESV_UF(mout, max_linkhdr);
10896         SCTP_BUF_LEN(mout) = len;
10897         SCTP_BUF_NEXT(mout) = NULL;
10898         if (m->m_flags & M_FLOWID) {
10899                 mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
10900                 mout->m_flags |= M_FLOWID;
10901         }
10902 #ifdef INET
10903         iph_out = NULL;
10904 #endif
10905 #ifdef INET6
10906         ip6_out = NULL;
10907 #endif
10908         offset_out = 0;
10909
10910         switch (iph->ip_v) {
10911 #ifdef INET
10912         case IPVERSION:
10913                 iph_out = mtod(mout, struct ip *);
10914
10915                 /* Fill in the IP header for the ABORT */
10916                 iph_out->ip_v = IPVERSION;
10917                 iph_out->ip_hl = (sizeof(struct ip) / 4);
10918                 iph_out->ip_tos = (u_char)0;
10919                 iph_out->ip_id = 0;
10920                 iph_out->ip_off = 0;
10921                 iph_out->ip_ttl = MAXTTL;
10922                 if (port) {
10923                         iph_out->ip_p = IPPROTO_UDP;
10924                 } else {
10925                         iph_out->ip_p = IPPROTO_SCTP;
10926                 }
10927                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10928                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10929
10930                 /* let IP layer calculate this */
10931                 iph_out->ip_sum = 0;
10932                 offset_out += sizeof(*iph_out);
10933                 comp_cp = (struct sctp_shutdown_complete_msg *)(
10934                     (caddr_t)iph_out + offset_out);
10935                 break;
10936 #endif
10937 #ifdef INET6
10938         case IPV6_VERSION >> 4:
10939                 ip6 = (struct ip6_hdr *)iph;
10940                 ip6_out = mtod(mout, struct ip6_hdr *);
10941
10942                 /* Fill in the IPv6 header for the ABORT */
10943                 ip6_out->ip6_flow = ip6->ip6_flow;
10944                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10945                 if (port) {
10946                         ip6_out->ip6_nxt = IPPROTO_UDP;
10947                 } else {
10948                         ip6_out->ip6_nxt = IPPROTO_SCTP;
10949                 }
10950                 ip6_out->ip6_src = ip6->ip6_dst;
10951                 ip6_out->ip6_dst = ip6->ip6_src;
10952                 /*
10953                  * ?? The old code had both the iph len + payload, I think
10954                  * this is wrong and would never have worked
10955                  */
10956                 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10957                 offset_out += sizeof(*ip6_out);
10958                 comp_cp = (struct sctp_shutdown_complete_msg *)(
10959                     (caddr_t)ip6_out + offset_out);
10960                 break;
10961 #endif                          /* INET6 */
10962         default:
10963                 /* Currently not supported. */
10964                 sctp_m_freem(mout);
10965                 return;
10966         }
10967         if (port) {
10968                 udp = (struct udphdr *)comp_cp;
10969                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10970                 udp->uh_dport = port;
10971                 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10972 #ifdef INET
10973                 if (iph_out)
10974                         udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10975 #endif
10976                 offset_out += sizeof(struct udphdr);
10977                 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10978         }
10979         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10980                 /* no mbuf's */
10981                 sctp_m_freem(mout);
10982                 return;
10983         }
10984         /* Now copy in and fill in the ABORT tags etc. */
10985         comp_cp->sh.src_port = sh->dest_port;
10986         comp_cp->sh.dest_port = sh->src_port;
10987         comp_cp->sh.checksum = 0;
10988         comp_cp->sh.v_tag = sh->v_tag;
10989         comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10990         comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10991         comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10992
10993 #ifdef INET
10994         if (iph_out != NULL) {
10995                 sctp_route_t ro;
10996                 int ret;
10997
10998                 mlen = SCTP_BUF_LEN(mout);
10999                 bzero(&ro, sizeof ro);
11000                 /* set IPv4 length */
11001                 iph_out->ip_len = mlen;
11002 #ifdef  SCTP_PACKET_LOGGING
11003                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11004                         sctp_packet_log(mout, mlen);
11005 #endif
11006                 if (port) {
11007 #if defined(SCTP_WITH_NO_CSUM)
11008                         SCTP_STAT_INCR(sctps_sendnocrc);
11009 #else
11010                         comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
11011                         SCTP_STAT_INCR(sctps_sendswcrc);
11012 #endif
11013                         SCTP_ENABLE_UDP_CSUM(mout);
11014                 } else {
11015 #if defined(SCTP_WITH_NO_CSUM)
11016                         SCTP_STAT_INCR(sctps_sendnocrc);
11017 #else
11018                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
11019                         mout->m_pkthdr.csum_data = 0;
11020                         SCTP_STAT_INCR(sctps_sendhwcrc);
11021 #endif
11022                 }
11023                 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
11024                 /* out it goes */
11025                 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
11026
11027                 /* Free the route if we got one back */
11028                 if (ro.ro_rt)
11029                         RTFREE(ro.ro_rt);
11030         }
11031 #endif
11032 #ifdef INET6
11033         if (ip6_out != NULL) {
11034                 struct route_in6 ro;
11035                 int ret;
11036                 struct ifnet *ifp = NULL;
11037
11038                 bzero(&ro, sizeof(ro));
11039                 mlen = SCTP_BUF_LEN(mout);
11040 #ifdef  SCTP_PACKET_LOGGING
11041                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11042                         sctp_packet_log(mout, mlen);
11043 #endif
11044                 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
11045                 if (port) {
11046 #if defined(SCTP_WITH_NO_CSUM)
11047                         SCTP_STAT_INCR(sctps_sendnocrc);
11048 #else
11049                         comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11050                         SCTP_STAT_INCR(sctps_sendswcrc);
11051 #endif
11052                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
11053                                 udp->uh_sum = 0xffff;
11054                         }
11055                 } else {
11056 #if defined(SCTP_WITH_NO_CSUM)
11057                         SCTP_STAT_INCR(sctps_sendnocrc);
11058 #else
11059                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
11060                         mout->m_pkthdr.csum_data = 0;
11061                         SCTP_STAT_INCR(sctps_sendhwcrc);
11062 #endif
11063                 }
11064                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
11065
11066                 /* Free the route if we got one back */
11067                 if (ro.ro_rt)
11068                         RTFREE(ro.ro_rt);
11069         }
11070 #endif
11071         SCTP_STAT_INCR(sctps_sendpackets);
11072         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11073         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11074         return;
11075
11076 }
11077
11078 void
11079 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked
11080 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
11081     SCTP_UNUSED
11082 #endif
11083 )
11084 {
11085         struct sctp_tmit_chunk *chk;
11086         struct sctp_heartbeat_chunk *hb;
11087         struct timeval now;
11088
11089         SCTP_TCB_LOCK_ASSERT(stcb);
11090         if (net == NULL) {
11091                 return;
11092         }
11093         (void)SCTP_GETTIME_TIMEVAL(&now);
11094         switch (net->ro._l_addr.sa.sa_family) {
11095 #ifdef INET
11096         case AF_INET:
11097                 break;
11098 #endif
11099 #ifdef INET6
11100         case AF_INET6:
11101                 break;
11102 #endif
11103         default:
11104                 return;
11105         }
11106         sctp_alloc_a_chunk(stcb, chk);
11107         if (chk == NULL) {
11108                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11109                 return;
11110         }
11111         chk->copy_by_ref = 0;
11112         chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11113         chk->rec.chunk_id.can_take_data = 1;
11114         chk->asoc = &stcb->asoc;
11115         chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11116
11117         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11118         if (chk->data == NULL) {
11119                 sctp_free_a_chunk(stcb, chk, so_locked);
11120                 return;
11121         }
11122         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11123         SCTP_BUF_LEN(chk->data) = chk->send_size;
11124         chk->sent = SCTP_DATAGRAM_UNSENT;
11125         chk->snd_count = 0;
11126         chk->whoTo = net;
11127         atomic_add_int(&chk->whoTo->ref_count, 1);
11128         /* Now we have a mbuf that we can fill in with the details */
11129         hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11130         memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11131         /* fill out chunk header */
11132         hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11133         hb->ch.chunk_flags = 0;
11134         hb->ch.chunk_length = htons(chk->send_size);
11135         /* Fill out hb parameter */
11136         hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11137         hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11138         hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
11139         hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11140         /* Did our user request this one, put it in */
11141         hb->heartbeat.hb_info.addr_family = net->ro._l_addr.sa.sa_family;
11142         hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
11143         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11144                 /*
11145                  * we only take from the entropy pool if the address is not
11146                  * confirmed.
11147                  */
11148                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11149                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11150         } else {
11151                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11152                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11153         }
11154         switch (net->ro._l_addr.sa.sa_family) {
11155 #ifdef INET
11156         case AF_INET:
11157                 memcpy(hb->heartbeat.hb_info.address,
11158                     &net->ro._l_addr.sin.sin_addr,
11159                     sizeof(net->ro._l_addr.sin.sin_addr));
11160                 break;
11161 #endif
11162 #ifdef INET6
11163         case AF_INET6:
11164                 memcpy(hb->heartbeat.hb_info.address,
11165                     &net->ro._l_addr.sin6.sin6_addr,
11166                     sizeof(net->ro._l_addr.sin6.sin6_addr));
11167                 break;
11168 #endif
11169         default:
11170                 return;
11171                 break;
11172         }
11173         net->hb_responded = 0;
11174         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11175         stcb->asoc.ctrl_queue_cnt++;
11176         SCTP_STAT_INCR(sctps_sendheartbeat);
11177         return;
11178 }
11179
11180 void
11181 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11182     uint32_t high_tsn)
11183 {
11184         struct sctp_association *asoc;
11185         struct sctp_ecne_chunk *ecne;
11186         struct sctp_tmit_chunk *chk;
11187
11188         if (net == NULL) {
11189                 return;
11190         }
11191         asoc = &stcb->asoc;
11192         SCTP_TCB_LOCK_ASSERT(stcb);
11193         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11194                 if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
11195                         /* found a previous ECN_ECHO update it if needed */
11196                         uint32_t cnt, ctsn;
11197
11198                         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11199                         ctsn = ntohl(ecne->tsn);
11200                         if (SCTP_TSN_GT(high_tsn, ctsn)) {
11201                                 ecne->tsn = htonl(high_tsn);
11202                                 SCTP_STAT_INCR(sctps_queue_upd_ecne);
11203                         }
11204                         cnt = ntohl(ecne->num_pkts_since_cwr);
11205                         cnt++;
11206                         ecne->num_pkts_since_cwr = htonl(cnt);
11207                         return;
11208                 }
11209         }
11210         /* nope could not find one to update so we must build one */
11211         sctp_alloc_a_chunk(stcb, chk);
11212         if (chk == NULL) {
11213                 return;
11214         }
11215         chk->copy_by_ref = 0;
11216         SCTP_STAT_INCR(sctps_queue_upd_ecne);
11217         chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11218         chk->rec.chunk_id.can_take_data = 0;
11219         chk->asoc = &stcb->asoc;
11220         chk->send_size = sizeof(struct sctp_ecne_chunk);
11221         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11222         if (chk->data == NULL) {
11223                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11224                 return;
11225         }
11226         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11227         SCTP_BUF_LEN(chk->data) = chk->send_size;
11228         chk->sent = SCTP_DATAGRAM_UNSENT;
11229         chk->snd_count = 0;
11230         chk->whoTo = net;
11231         atomic_add_int(&chk->whoTo->ref_count, 1);
11232
11233         stcb->asoc.ecn_echo_cnt_onq++;
11234         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11235         ecne->ch.chunk_type = SCTP_ECN_ECHO;
11236         ecne->ch.chunk_flags = 0;
11237         ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11238         ecne->tsn = htonl(high_tsn);
11239         ecne->num_pkts_since_cwr = htonl(1);
11240         TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11241         asoc->ctrl_queue_cnt++;
11242 }
11243
11244 void
11245 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11246     struct mbuf *m, int iphlen, int bad_crc)
11247 {
11248         struct sctp_association *asoc;
11249         struct sctp_pktdrop_chunk *drp;
11250         struct sctp_tmit_chunk *chk;
11251         uint8_t *datap;
11252         int len;
11253         int was_trunc = 0;
11254         struct ip *iph;
11255
11256 #ifdef INET6
11257         struct ip6_hdr *ip6h;
11258
11259 #endif
11260         int fullsz = 0, extra = 0;
11261         long spc;
11262         int offset;
11263         struct sctp_chunkhdr *ch, chunk_buf;
11264         unsigned int chk_length;
11265
11266         if (!stcb) {
11267                 return;
11268         }
11269         asoc = &stcb->asoc;
11270         SCTP_TCB_LOCK_ASSERT(stcb);
11271         if (asoc->peer_supports_pktdrop == 0) {
11272                 /*-
11273                  * peer must declare support before I send one.
11274                  */
11275                 return;
11276         }
11277         if (stcb->sctp_socket == NULL) {
11278                 return;
11279         }
11280         sctp_alloc_a_chunk(stcb, chk);
11281         if (chk == NULL) {
11282                 return;
11283         }
11284         chk->copy_by_ref = 0;
11285         iph = mtod(m, struct ip *);
11286         if (iph == NULL) {
11287                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11288                 return;
11289         }
11290         switch (iph->ip_v) {
11291 #ifdef INET
11292         case IPVERSION:
11293                 /* IPv4 */
11294                 len = chk->send_size = iph->ip_len;
11295                 break;
11296 #endif
11297 #ifdef INET6
11298         case IPV6_VERSION >> 4:
11299                 /* IPv6 */
11300                 ip6h = mtod(m, struct ip6_hdr *);
11301                 len = chk->send_size = htons(ip6h->ip6_plen);
11302                 break;
11303 #endif
11304         default:
11305                 return;
11306         }
11307         /* Validate that we do not have an ABORT in here. */
11308         offset = iphlen + sizeof(struct sctphdr);
11309         ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11310             sizeof(*ch), (uint8_t *) & chunk_buf);
11311         while (ch != NULL) {
11312                 chk_length = ntohs(ch->chunk_length);
11313                 if (chk_length < sizeof(*ch)) {
11314                         /* break to abort land */
11315                         break;
11316                 }
11317                 switch (ch->chunk_type) {
11318                 case SCTP_PACKET_DROPPED:
11319                 case SCTP_ABORT_ASSOCIATION:
11320                 case SCTP_INITIATION_ACK:
11321                         /**
11322                          * We don't respond with an PKT-DROP to an ABORT
11323                          * or PKT-DROP. We also do not respond to an
11324                          * INIT-ACK, because we can't know if the initiation
11325                          * tag is correct or not.
11326                          */
11327                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11328                         return;
11329                 default:
11330                         break;
11331                 }
11332                 offset += SCTP_SIZE32(chk_length);
11333                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11334                     sizeof(*ch), (uint8_t *) & chunk_buf);
11335         }
11336
11337         if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11338             min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11339                 /*
11340                  * only send 1 mtu worth, trim off the excess on the end.
11341                  */
11342                 fullsz = len - extra;
11343                 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11344                 was_trunc = 1;
11345         }
11346         chk->asoc = &stcb->asoc;
11347         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11348         if (chk->data == NULL) {
11349 jump_out:
11350                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11351                 return;
11352         }
11353         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11354         drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11355         if (drp == NULL) {
11356                 sctp_m_freem(chk->data);
11357                 chk->data = NULL;
11358                 goto jump_out;
11359         }
11360         chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11361             sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11362         chk->book_size_scale = 0;
11363         if (was_trunc) {
11364                 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11365                 drp->trunc_len = htons(fullsz);
11366                 /*
11367                  * Len is already adjusted to size minus overhead above take
11368                  * out the pkt_drop chunk itself from it.
11369                  */
11370                 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11371                 len = chk->send_size;
11372         } else {
11373                 /* no truncation needed */
11374                 drp->ch.chunk_flags = 0;
11375                 drp->trunc_len = htons(0);
11376         }
11377         if (bad_crc) {
11378                 drp->ch.chunk_flags |= SCTP_BADCRC;
11379         }
11380         chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11381         SCTP_BUF_LEN(chk->data) = chk->send_size;
11382         chk->sent = SCTP_DATAGRAM_UNSENT;
11383         chk->snd_count = 0;
11384         if (net) {
11385                 /* we should hit here */
11386                 chk->whoTo = net;
11387                 atomic_add_int(&chk->whoTo->ref_count, 1);
11388         } else {
11389                 chk->whoTo = NULL;
11390         }
11391         chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11392         chk->rec.chunk_id.can_take_data = 1;
11393         drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11394         drp->ch.chunk_length = htons(chk->send_size);
11395         spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11396         if (spc < 0) {
11397                 spc = 0;
11398         }
11399         drp->bottle_bw = htonl(spc);
11400         if (asoc->my_rwnd) {
11401                 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11402                     asoc->size_on_all_streams +
11403                     asoc->my_rwnd_control_len +
11404                     stcb->sctp_socket->so_rcv.sb_cc);
11405         } else {
11406                 /*-
11407                  * If my rwnd is 0, possibly from mbuf depletion as well as
11408                  * space used, tell the peer there is NO space aka onq == bw
11409                  */
11410                 drp->current_onq = htonl(spc);
11411         }
11412         drp->reserved = 0;
11413         datap = drp->data;
11414         m_copydata(m, iphlen, len, (caddr_t)datap);
11415         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11416         asoc->ctrl_queue_cnt++;
11417 }
11418
11419 void
11420 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11421 {
11422         struct sctp_association *asoc;
11423         struct sctp_cwr_chunk *cwr;
11424         struct sctp_tmit_chunk *chk;
11425
11426         asoc = &stcb->asoc;
11427         SCTP_TCB_LOCK_ASSERT(stcb);
11428         if (net == NULL) {
11429                 return;
11430         }
11431         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11432                 if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11433                         /*
11434                          * found a previous CWR queued to same destination
11435                          * update it if needed
11436                          */
11437                         uint32_t ctsn;
11438
11439                         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11440                         ctsn = ntohl(cwr->tsn);
11441                         if (SCTP_TSN_GT(high_tsn, ctsn)) {
11442                                 cwr->tsn = htonl(high_tsn);
11443                         }
11444                         if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11445                                 /* Make sure override is carried */
11446                                 cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11447                         }
11448                         return;
11449                 }
11450         }
11451         sctp_alloc_a_chunk(stcb, chk);
11452         if (chk == NULL) {
11453                 return;
11454         }
11455         chk->copy_by_ref = 0;
11456         chk->rec.chunk_id.id = SCTP_ECN_CWR;
11457         chk->rec.chunk_id.can_take_data = 1;
11458         chk->asoc = &stcb->asoc;
11459         chk->send_size = sizeof(struct sctp_cwr_chunk);
11460         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11461         if (chk->data == NULL) {
11462                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11463                 return;
11464         }
11465         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11466         SCTP_BUF_LEN(chk->data) = chk->send_size;
11467         chk->sent = SCTP_DATAGRAM_UNSENT;
11468         chk->snd_count = 0;
11469         chk->whoTo = net;
11470         atomic_add_int(&chk->whoTo->ref_count, 1);
11471         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11472         cwr->ch.chunk_type = SCTP_ECN_CWR;
11473         cwr->ch.chunk_flags = override;
11474         cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11475         cwr->tsn = htonl(high_tsn);
11476         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11477         asoc->ctrl_queue_cnt++;
11478 }
11479
11480 void
11481 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11482     int number_entries, uint16_t * list,
11483     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11484 {
11485         int len, old_len, i;
11486         struct sctp_stream_reset_out_request *req_out;
11487         struct sctp_chunkhdr *ch;
11488
11489         ch = mtod(chk->data, struct sctp_chunkhdr *);
11490
11491
11492         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11493
11494         /* get to new offset for the param. */
11495         req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11496         /* now how long will this param be? */
11497         len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11498         req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11499         req_out->ph.param_length = htons(len);
11500         req_out->request_seq = htonl(seq);
11501         req_out->response_seq = htonl(resp_seq);
11502         req_out->send_reset_at_tsn = htonl(last_sent);
11503         if (number_entries) {
11504                 for (i = 0; i < number_entries; i++) {
11505                         req_out->list_of_streams[i] = htons(list[i]);
11506                 }
11507         }
11508         if (SCTP_SIZE32(len) > len) {
11509                 /*-
11510                  * Need to worry about the pad we may end up adding to the
11511                  * end. This is easy since the struct is either aligned to 4
11512                  * bytes or 2 bytes off.
11513                  */
11514                 req_out->list_of_streams[number_entries] = 0;
11515         }
11516         /* now fix the chunk length */
11517         ch->chunk_length = htons(len + old_len);
11518         chk->book_size = len + old_len;
11519         chk->book_size_scale = 0;
11520         chk->send_size = SCTP_SIZE32(chk->book_size);
11521         SCTP_BUF_LEN(chk->data) = chk->send_size;
11522         return;
11523 }
11524
11525
11526 void
11527 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11528     int number_entries, uint16_t * list,
11529     uint32_t seq)
11530 {
11531         int len, old_len, i;
11532         struct sctp_stream_reset_in_request *req_in;
11533         struct sctp_chunkhdr *ch;
11534
11535         ch = mtod(chk->data, struct sctp_chunkhdr *);
11536
11537
11538         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11539
11540         /* get to new offset for the param. */
11541         req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11542         /* now how long will this param be? */
11543         len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11544         req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11545         req_in->ph.param_length = htons(len);
11546         req_in->request_seq = htonl(seq);
11547         if (number_entries) {
11548                 for (i = 0; i < number_entries; i++) {
11549                         req_in->list_of_streams[i] = htons(list[i]);
11550                 }
11551         }
11552         if (SCTP_SIZE32(len) > len) {
11553                 /*-
11554                  * Need to worry about the pad we may end up adding to the
11555                  * end. This is easy since the struct is either aligned to 4
11556                  * bytes or 2 bytes off.
11557                  */
11558                 req_in->list_of_streams[number_entries] = 0;
11559         }
11560         /* now fix the chunk length */
11561         ch->chunk_length = htons(len + old_len);
11562         chk->book_size = len + old_len;
11563         chk->book_size_scale = 0;
11564         chk->send_size = SCTP_SIZE32(chk->book_size);
11565         SCTP_BUF_LEN(chk->data) = chk->send_size;
11566         return;
11567 }
11568
11569
11570 void
11571 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11572     uint32_t seq)
11573 {
11574         int len, old_len;
11575         struct sctp_stream_reset_tsn_request *req_tsn;
11576         struct sctp_chunkhdr *ch;
11577
11578         ch = mtod(chk->data, struct sctp_chunkhdr *);
11579
11580
11581         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11582
11583         /* get to new offset for the param. */
11584         req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11585         /* now how long will this param be? */
11586         len = sizeof(struct sctp_stream_reset_tsn_request);
11587         req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11588         req_tsn->ph.param_length = htons(len);
11589         req_tsn->request_seq = htonl(seq);
11590
11591         /* now fix the chunk length */
11592         ch->chunk_length = htons(len + old_len);
11593         chk->send_size = len + old_len;
11594         chk->book_size = SCTP_SIZE32(chk->send_size);
11595         chk->book_size_scale = 0;
11596         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11597         return;
11598 }
11599
11600 void
11601 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11602     uint32_t resp_seq, uint32_t result)
11603 {
11604         int len, old_len;
11605         struct sctp_stream_reset_response *resp;
11606         struct sctp_chunkhdr *ch;
11607
11608         ch = mtod(chk->data, struct sctp_chunkhdr *);
11609
11610
11611         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11612
11613         /* get to new offset for the param. */
11614         resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11615         /* now how long will this param be? */
11616         len = sizeof(struct sctp_stream_reset_response);
11617         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11618         resp->ph.param_length = htons(len);
11619         resp->response_seq = htonl(resp_seq);
11620         resp->result = ntohl(result);
11621
11622         /* now fix the chunk length */
11623         ch->chunk_length = htons(len + old_len);
11624         chk->book_size = len + old_len;
11625         chk->book_size_scale = 0;
11626         chk->send_size = SCTP_SIZE32(chk->book_size);
11627         SCTP_BUF_LEN(chk->data) = chk->send_size;
11628         return;
11629
11630 }
11631
11632
11633 void
11634 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11635     uint32_t resp_seq, uint32_t result,
11636     uint32_t send_una, uint32_t recv_next)
11637 {
11638         int len, old_len;
11639         struct sctp_stream_reset_response_tsn *resp;
11640         struct sctp_chunkhdr *ch;
11641
11642         ch = mtod(chk->data, struct sctp_chunkhdr *);
11643
11644
11645         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11646
11647         /* get to new offset for the param. */
11648         resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11649         /* now how long will this param be? */
11650         len = sizeof(struct sctp_stream_reset_response_tsn);
11651         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11652         resp->ph.param_length = htons(len);
11653         resp->response_seq = htonl(resp_seq);
11654         resp->result = htonl(result);
11655         resp->senders_next_tsn = htonl(send_una);
11656         resp->receivers_next_tsn = htonl(recv_next);
11657
11658         /* now fix the chunk length */
11659         ch->chunk_length = htons(len + old_len);
11660         chk->book_size = len + old_len;
11661         chk->send_size = SCTP_SIZE32(chk->book_size);
11662         chk->book_size_scale = 0;
11663         SCTP_BUF_LEN(chk->data) = chk->send_size;
11664         return;
11665 }
11666
11667 static void
11668 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11669     uint32_t seq,
11670     uint16_t adding)
11671 {
11672         int len, old_len;
11673         struct sctp_chunkhdr *ch;
11674         struct sctp_stream_reset_add_strm *addstr;
11675
11676         ch = mtod(chk->data, struct sctp_chunkhdr *);
11677         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11678
11679         /* get to new offset for the param. */
11680         addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11681         /* now how long will this param be? */
11682         len = sizeof(struct sctp_stream_reset_add_strm);
11683
11684         /* Fill it out. */
11685         addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11686         addstr->ph.param_length = htons(len);
11687         addstr->request_seq = htonl(seq);
11688         addstr->number_of_streams = htons(adding);
11689         addstr->reserved = 0;
11690
11691         /* now fix the chunk length */
11692         ch->chunk_length = htons(len + old_len);
11693         chk->send_size = len + old_len;
11694         chk->book_size = SCTP_SIZE32(chk->send_size);
11695         chk->book_size_scale = 0;
11696         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11697         return;
11698 }
11699
11700 int
11701 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11702     int number_entries, uint16_t * list,
11703     uint8_t send_out_req,
11704     uint32_t resp_seq,
11705     uint8_t send_in_req,
11706     uint8_t send_tsn_req,
11707     uint8_t add_stream,
11708     uint16_t adding
11709 )
11710 {
11711
11712         struct sctp_association *asoc;
11713         struct sctp_tmit_chunk *chk;
11714         struct sctp_chunkhdr *ch;
11715         uint32_t seq;
11716
11717         asoc = &stcb->asoc;
11718         if (asoc->stream_reset_outstanding) {
11719                 /*-
11720                  * Already one pending, must get ACK back to clear the flag.
11721                  */
11722                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11723                 return (EBUSY);
11724         }
11725         if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11726             (add_stream == 0)) {
11727                 /* nothing to do */
11728                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11729                 return (EINVAL);
11730         }
11731         if (send_tsn_req && (send_out_req || send_in_req)) {
11732                 /* error, can't do that */
11733                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11734                 return (EINVAL);
11735         }
11736         sctp_alloc_a_chunk(stcb, chk);
11737         if (chk == NULL) {
11738                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11739                 return (ENOMEM);
11740         }
11741         chk->copy_by_ref = 0;
11742         chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11743         chk->rec.chunk_id.can_take_data = 0;
11744         chk->asoc = &stcb->asoc;
11745         chk->book_size = sizeof(struct sctp_chunkhdr);
11746         chk->send_size = SCTP_SIZE32(chk->book_size);
11747         chk->book_size_scale = 0;
11748
11749         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11750         if (chk->data == NULL) {
11751                 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11752                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11753                 return (ENOMEM);
11754         }
11755         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11756
11757         /* setup chunk parameters */
11758         chk->sent = SCTP_DATAGRAM_UNSENT;
11759         chk->snd_count = 0;
11760         if (stcb->asoc.alternate) {
11761                 chk->whoTo = stcb->asoc.alternate;
11762         } else {
11763                 chk->whoTo = stcb->asoc.primary_destination;
11764         }
11765         atomic_add_int(&chk->whoTo->ref_count, 1);
11766         ch = mtod(chk->data, struct sctp_chunkhdr *);
11767         ch->chunk_type = SCTP_STREAM_RESET;
11768         ch->chunk_flags = 0;
11769         ch->chunk_length = htons(chk->book_size);
11770         SCTP_BUF_LEN(chk->data) = chk->send_size;
11771
11772         seq = stcb->asoc.str_reset_seq_out;
11773         if (send_out_req) {
11774                 sctp_add_stream_reset_out(chk, number_entries, list,
11775                     seq, resp_seq, (stcb->asoc.sending_seq - 1));
11776                 asoc->stream_reset_out_is_outstanding = 1;
11777                 seq++;
11778                 asoc->stream_reset_outstanding++;
11779         }
11780         if (add_stream) {
11781                 sctp_add_a_stream(chk, seq, adding);
11782                 seq++;
11783                 asoc->stream_reset_outstanding++;
11784         }
11785         if (send_in_req) {
11786                 sctp_add_stream_reset_in(chk, number_entries, list, seq);
11787                 asoc->stream_reset_outstanding++;
11788         }
11789         if (send_tsn_req) {
11790                 sctp_add_stream_reset_tsn(chk, seq);
11791                 asoc->stream_reset_outstanding++;
11792         }
11793         asoc->str_reset = chk;
11794
11795         /* insert the chunk for sending */
11796         TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11797             chk,
11798             sctp_next);
11799         asoc->ctrl_queue_cnt++;
11800         sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11801         return (0);
11802 }
11803
11804 void
11805 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11806     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11807 {
11808         /*-
11809          * Formulate the abort message, and send it back down.
11810          */
11811         struct mbuf *o_pak;
11812         struct mbuf *mout;
11813         struct sctp_abort_msg *abm;
11814         struct ip *iph;
11815         struct udphdr *udp;
11816         int iphlen_out, len;
11817
11818 #ifdef INET
11819         struct ip *iph_out;
11820
11821 #endif
11822 #ifdef INET6
11823         struct ip6_hdr *ip6, *ip6_out;
11824
11825 #endif
11826
11827         /* don't respond to ABORT with ABORT */
11828         if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11829                 if (err_cause)
11830                         sctp_m_freem(err_cause);
11831                 return;
11832         }
11833         iph = mtod(m, struct ip *);
11834         switch (iph->ip_v) {
11835 #ifdef INET
11836         case IPVERSION:
11837                 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11838                 break;
11839 #endif
11840 #ifdef INET6
11841         case IPV6_VERSION >> 4:
11842                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11843                 break;
11844 #endif
11845         default:
11846                 if (err_cause) {
11847                         sctp_m_freem(err_cause);
11848                 }
11849                 return;
11850         }
11851         if (port) {
11852                 len += sizeof(struct udphdr);
11853         }
11854         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11855         if (mout == NULL) {
11856                 if (err_cause) {
11857                         sctp_m_freem(err_cause);
11858                 }
11859                 return;
11860         }
11861         SCTP_BUF_RESV_UF(mout, max_linkhdr);
11862         SCTP_BUF_LEN(mout) = len;
11863         SCTP_BUF_NEXT(mout) = err_cause;
11864         if (m->m_flags & M_FLOWID) {
11865                 mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
11866                 mout->m_flags |= M_FLOWID;
11867         }
11868 #ifdef INET
11869         iph_out = NULL;
11870 #endif
11871 #ifdef INET6
11872         ip6_out = NULL;
11873 #endif
11874         switch (iph->ip_v) {
11875 #ifdef INET
11876         case IPVERSION:
11877                 iph_out = mtod(mout, struct ip *);
11878
11879                 /* Fill in the IP header for the ABORT */
11880                 iph_out->ip_v = IPVERSION;
11881                 iph_out->ip_hl = (sizeof(struct ip) / 4);
11882                 iph_out->ip_tos = (u_char)0;
11883                 iph_out->ip_id = 0;
11884                 iph_out->ip_off = 0;
11885                 iph_out->ip_ttl = MAXTTL;
11886                 if (port) {
11887                         iph_out->ip_p = IPPROTO_UDP;
11888                 } else {
11889                         iph_out->ip_p = IPPROTO_SCTP;
11890                 }
11891                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11892                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11893                 /* let IP layer calculate this */
11894                 iph_out->ip_sum = 0;
11895
11896                 iphlen_out = sizeof(*iph_out);
11897                 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11898                 break;
11899 #endif
11900 #ifdef INET6
11901         case IPV6_VERSION >> 4:
11902                 ip6 = (struct ip6_hdr *)iph;
11903                 ip6_out = mtod(mout, struct ip6_hdr *);
11904
11905                 /* Fill in the IP6 header for the ABORT */
11906                 ip6_out->ip6_flow = ip6->ip6_flow;
11907                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11908                 if (port) {
11909                         ip6_out->ip6_nxt = IPPROTO_UDP;
11910                 } else {
11911                         ip6_out->ip6_nxt = IPPROTO_SCTP;
11912                 }
11913                 ip6_out->ip6_src = ip6->ip6_dst;
11914                 ip6_out->ip6_dst = ip6->ip6_src;
11915
11916                 iphlen_out = sizeof(*ip6_out);
11917                 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11918                 break;
11919 #endif                          /* INET6 */
11920         default:
11921                 /* Currently not supported */
11922                 sctp_m_freem(mout);
11923                 return;
11924         }
11925
11926         udp = (struct udphdr *)abm;
11927         if (port) {
11928                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11929                 udp->uh_dport = port;
11930                 /* set udp->uh_ulen later */
11931                 udp->uh_sum = 0;
11932                 iphlen_out += sizeof(struct udphdr);
11933                 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11934         }
11935         abm->sh.src_port = sh->dest_port;
11936         abm->sh.dest_port = sh->src_port;
11937         abm->sh.checksum = 0;
11938         if (vtag == 0) {
11939                 abm->sh.v_tag = sh->v_tag;
11940                 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11941         } else {
11942                 abm->sh.v_tag = htonl(vtag);
11943                 abm->msg.ch.chunk_flags = 0;
11944         }
11945         abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11946
11947         if (err_cause) {
11948                 struct mbuf *m_tmp = err_cause;
11949                 int err_len = 0;
11950
11951                 /* get length of the err_cause chain */
11952                 while (m_tmp != NULL) {
11953                         err_len += SCTP_BUF_LEN(m_tmp);
11954                         m_tmp = SCTP_BUF_NEXT(m_tmp);
11955                 }
11956                 len = SCTP_BUF_LEN(mout) + err_len;
11957                 if (err_len % 4) {
11958                         /* need pad at end of chunk */
11959                         uint32_t cpthis = 0;
11960                         int padlen;
11961
11962                         padlen = 4 - (len % 4);
11963                         m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11964                         len += padlen;
11965                 }
11966                 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11967         } else {
11968                 len = SCTP_BUF_LEN(mout);
11969                 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11970         }
11971
11972         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11973                 /* no mbuf's */
11974                 sctp_m_freem(mout);
11975                 return;
11976         }
11977 #ifdef INET
11978         if (iph_out != NULL) {
11979                 sctp_route_t ro;
11980                 int ret;
11981
11982                 /* zap the stack pointer to the route */
11983                 bzero(&ro, sizeof ro);
11984                 if (port) {
11985                         udp->uh_ulen = htons(len - sizeof(struct ip));
11986                         udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11987                 }
11988                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11989                 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11990                 /* set IPv4 length */
11991                 iph_out->ip_len = len;
11992                 /* out it goes */
11993 #ifdef  SCTP_PACKET_LOGGING
11994                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11995                         sctp_packet_log(mout, len);
11996 #endif
11997                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11998                 if (port) {
11999 #if defined(SCTP_WITH_NO_CSUM)
12000                         SCTP_STAT_INCR(sctps_sendnocrc);
12001 #else
12002                         abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
12003                         SCTP_STAT_INCR(sctps_sendswcrc);
12004 #endif
12005                         SCTP_ENABLE_UDP_CSUM(o_pak);
12006                 } else {
12007 #if defined(SCTP_WITH_NO_CSUM)
12008                         SCTP_STAT_INCR(sctps_sendnocrc);
12009 #else
12010                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12011                         mout->m_pkthdr.csum_data = 0;
12012                         SCTP_STAT_INCR(sctps_sendhwcrc);
12013 #endif
12014                 }
12015                 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12016
12017                 /* Free the route if we got one back */
12018                 if (ro.ro_rt)
12019                         RTFREE(ro.ro_rt);
12020         }
12021 #endif
12022 #ifdef INET6
12023         if (ip6_out != NULL) {
12024                 struct route_in6 ro;
12025                 int ret;
12026                 struct ifnet *ifp = NULL;
12027
12028                 /* zap the stack pointer to the route */
12029                 bzero(&ro, sizeof(ro));
12030                 if (port) {
12031                         udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12032                 }
12033                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
12034                 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
12035                 ip6_out->ip6_plen = len - sizeof(*ip6_out);
12036 #ifdef  SCTP_PACKET_LOGGING
12037                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12038                         sctp_packet_log(mout, len);
12039 #endif
12040                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12041                 if (port) {
12042 #if defined(SCTP_WITH_NO_CSUM)
12043                         SCTP_STAT_INCR(sctps_sendnocrc);
12044 #else
12045                         abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12046                         SCTP_STAT_INCR(sctps_sendswcrc);
12047 #endif
12048                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12049                                 udp->uh_sum = 0xffff;
12050                         }
12051                 } else {
12052 #if defined(SCTP_WITH_NO_CSUM)
12053                         SCTP_STAT_INCR(sctps_sendnocrc);
12054 #else
12055                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12056                         mout->m_pkthdr.csum_data = 0;
12057                         SCTP_STAT_INCR(sctps_sendhwcrc);
12058 #endif
12059                 }
12060                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12061
12062                 /* Free the route if we got one back */
12063                 if (ro.ro_rt)
12064                         RTFREE(ro.ro_rt);
12065         }
12066 #endif
12067         SCTP_STAT_INCR(sctps_sendpackets);
12068         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12069         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12070 }
12071
12072 void
12073 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
12074     uint32_t vrf_id, uint16_t port)
12075 {
12076         struct mbuf *o_pak;
12077         struct sctphdr *sh, *sh_out;
12078         struct sctp_chunkhdr *ch;
12079         struct ip *iph;
12080         struct udphdr *udp = NULL;
12081         struct mbuf *mout;
12082         int iphlen_out, len;
12083
12084 #ifdef INET
12085         struct ip *iph_out;
12086
12087 #endif
12088 #ifdef INET6
12089         struct ip6_hdr *ip6, *ip6_out;
12090
12091 #endif
12092
12093         iph = mtod(m, struct ip *);
12094         sh = (struct sctphdr *)((caddr_t)iph + iphlen);
12095         switch (iph->ip_v) {
12096 #ifdef INET
12097         case IPVERSION:
12098                 len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
12099                 break;
12100 #endif
12101 #ifdef INET6
12102         case IPV6_VERSION >> 4:
12103                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
12104                 break;
12105 #endif
12106         default:
12107                 if (scm) {
12108                         sctp_m_freem(scm);
12109                 }
12110                 return;
12111         }
12112         if (port) {
12113                 len += sizeof(struct udphdr);
12114         }
12115         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
12116         if (mout == NULL) {
12117                 if (scm) {
12118                         sctp_m_freem(scm);
12119                 }
12120                 return;
12121         }
12122         SCTP_BUF_RESV_UF(mout, max_linkhdr);
12123         SCTP_BUF_LEN(mout) = len;
12124         SCTP_BUF_NEXT(mout) = scm;
12125         if (m->m_flags & M_FLOWID) {
12126                 mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
12127                 mout->m_flags |= M_FLOWID;
12128         }
12129 #ifdef INET
12130         iph_out = NULL;
12131 #endif
12132 #ifdef INET6
12133         ip6_out = NULL;
12134 #endif
12135         switch (iph->ip_v) {
12136 #ifdef INET
12137         case IPVERSION:
12138                 iph_out = mtod(mout, struct ip *);
12139
12140                 /* Fill in the IP header for the ABORT */
12141                 iph_out->ip_v = IPVERSION;
12142                 iph_out->ip_hl = (sizeof(struct ip) / 4);
12143                 iph_out->ip_tos = (u_char)0;
12144                 iph_out->ip_id = 0;
12145                 iph_out->ip_off = 0;
12146                 iph_out->ip_ttl = MAXTTL;
12147                 if (port) {
12148                         iph_out->ip_p = IPPROTO_UDP;
12149                 } else {
12150                         iph_out->ip_p = IPPROTO_SCTP;
12151                 }
12152                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12153                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12154                 /* let IP layer calculate this */
12155                 iph_out->ip_sum = 0;
12156
12157                 iphlen_out = sizeof(struct ip);
12158                 sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
12159                 break;
12160 #endif
12161 #ifdef INET6
12162         case IPV6_VERSION >> 4:
12163                 ip6 = (struct ip6_hdr *)iph;
12164                 ip6_out = mtod(mout, struct ip6_hdr *);
12165
12166                 /* Fill in the IP6 header for the ABORT */
12167                 ip6_out->ip6_flow = ip6->ip6_flow;
12168                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
12169                 if (port) {
12170                         ip6_out->ip6_nxt = IPPROTO_UDP;
12171                 } else {
12172                         ip6_out->ip6_nxt = IPPROTO_SCTP;
12173                 }
12174                 ip6_out->ip6_src = ip6->ip6_dst;
12175                 ip6_out->ip6_dst = ip6->ip6_src;
12176
12177                 iphlen_out = sizeof(struct ip6_hdr);
12178                 sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
12179                 break;
12180 #endif                          /* INET6 */
12181         default:
12182                 /* Currently not supported */
12183                 sctp_m_freem(mout);
12184                 return;
12185         }
12186
12187         udp = (struct udphdr *)sh_out;
12188         if (port) {
12189                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12190                 udp->uh_dport = port;
12191                 /* set udp->uh_ulen later */
12192                 udp->uh_sum = 0;
12193                 iphlen_out += sizeof(struct udphdr);
12194                 sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
12195         }
12196         sh_out->src_port = sh->dest_port;
12197         sh_out->dest_port = sh->src_port;
12198         sh_out->v_tag = vtag;
12199         sh_out->checksum = 0;
12200
12201         ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
12202         ch->chunk_type = SCTP_OPERATION_ERROR;
12203         ch->chunk_flags = 0;
12204
12205         if (scm) {
12206                 struct mbuf *m_tmp = scm;
12207                 int cause_len = 0;
12208
12209                 /* get length of the err_cause chain */
12210                 while (m_tmp != NULL) {
12211                         cause_len += SCTP_BUF_LEN(m_tmp);
12212                         m_tmp = SCTP_BUF_NEXT(m_tmp);
12213                 }
12214                 len = SCTP_BUF_LEN(mout) + cause_len;
12215                 if (cause_len % 4) {
12216                         /* need pad at end of chunk */
12217                         uint32_t cpthis = 0;
12218                         int padlen;
12219
12220                         padlen = 4 - (len % 4);
12221                         m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12222                         len += padlen;
12223                 }
12224                 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
12225         } else {
12226                 len = SCTP_BUF_LEN(mout);
12227                 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
12228         }
12229
12230         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12231                 /* no mbuf's */
12232                 sctp_m_freem(mout);
12233                 return;
12234         }
12235 #ifdef INET
12236         if (iph_out != NULL) {
12237                 sctp_route_t ro;
12238                 int ret;
12239
12240                 /* zap the stack pointer to the route */
12241                 bzero(&ro, sizeof ro);
12242                 if (port) {
12243                         udp->uh_ulen = htons(len - sizeof(struct ip));
12244                         udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12245                 }
12246                 /* set IPv4 length */
12247                 iph_out->ip_len = len;
12248                 /* out it goes */
12249 #ifdef  SCTP_PACKET_LOGGING
12250                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12251                         sctp_packet_log(mout, len);
12252 #endif
12253                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12254                 if (port) {
12255 #if defined(SCTP_WITH_NO_CSUM)
12256                         SCTP_STAT_INCR(sctps_sendnocrc);
12257 #else
12258                         sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12259                         SCTP_STAT_INCR(sctps_sendswcrc);
12260 #endif
12261                         SCTP_ENABLE_UDP_CSUM(o_pak);
12262                 } else {
12263 #if defined(SCTP_WITH_NO_CSUM)
12264                         SCTP_STAT_INCR(sctps_sendnocrc);
12265 #else
12266                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12267                         mout->m_pkthdr.csum_data = 0;
12268                         SCTP_STAT_INCR(sctps_sendhwcrc);
12269 #endif
12270                 }
12271                 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12272
12273                 /* Free the route if we got one back */
12274                 if (ro.ro_rt)
12275                         RTFREE(ro.ro_rt);
12276         }
12277 #endif
12278 #ifdef INET6
12279         if (ip6_out != NULL) {
12280                 struct route_in6 ro;
12281                 int ret;
12282                 struct ifnet *ifp = NULL;
12283
12284                 /* zap the stack pointer to the route */
12285                 bzero(&ro, sizeof(ro));
12286                 if (port) {
12287                         udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12288                 }
12289                 ip6_out->ip6_plen = len - sizeof(*ip6_out);
12290 #ifdef  SCTP_PACKET_LOGGING
12291                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12292                         sctp_packet_log(mout, len);
12293 #endif
12294                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12295                 if (port) {
12296 #if defined(SCTP_WITH_NO_CSUM)
12297                         SCTP_STAT_INCR(sctps_sendnocrc);
12298 #else
12299                         sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12300                         SCTP_STAT_INCR(sctps_sendswcrc);
12301 #endif
12302                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12303                                 udp->uh_sum = 0xffff;
12304                         }
12305                 } else {
12306 #if defined(SCTP_WITH_NO_CSUM)
12307                         SCTP_STAT_INCR(sctps_sendnocrc);
12308 #else
12309                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12310                         mout->m_pkthdr.csum_data = 0;
12311                         SCTP_STAT_INCR(sctps_sendhwcrc);
12312 #endif
12313                 }
12314                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12315
12316                 /* Free the route if we got one back */
12317                 if (ro.ro_rt)
12318                         RTFREE(ro.ro_rt);
12319         }
12320 #endif
12321         SCTP_STAT_INCR(sctps_sendpackets);
12322         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12323         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12324 }
12325
12326 static struct mbuf *
12327 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
12328     struct uio *uio,
12329     struct sctp_sndrcvinfo *srcv,
12330     int max_send_len,
12331     int user_marks_eor,
12332     int *error,
12333     uint32_t * sndout,
12334     struct mbuf **new_tail)
12335 {
12336         struct mbuf *m;
12337
12338         m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12339             (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12340         if (m == NULL) {
12341                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12342                 *error = ENOMEM;
12343         } else {
12344                 *sndout = m_length(m, NULL);
12345                 *new_tail = m_last(m);
12346         }
12347         return (m);
12348 }
12349
12350 static int
12351 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12352     struct uio *uio,
12353     int resv_upfront)
12354 {
12355         int left;
12356
12357         left = sp->length;
12358         sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
12359             resv_upfront, 0);
12360         if (sp->data == NULL) {
12361                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12362                 return (ENOMEM);
12363         }
12364         sp->tail_mbuf = m_last(sp->data);
12365         return (0);
12366 }
12367
12368
12369
12370 static struct sctp_stream_queue_pending *
12371 sctp_copy_it_in(struct sctp_tcb *stcb,
12372     struct sctp_association *asoc,
12373     struct sctp_sndrcvinfo *srcv,
12374     struct uio *uio,
12375     struct sctp_nets *net,
12376     int max_send_len,
12377     int user_marks_eor,
12378     int *error,
12379     int non_blocking)
12380 {
12381         /*-
12382          * This routine must be very careful in its work. Protocol
12383          * processing is up and running so care must be taken to spl...()
12384          * when you need to do something that may effect the stcb/asoc. The
12385          * sb is locked however. When data is copied the protocol processing
12386          * should be enabled since this is a slower operation...
12387          */
12388         struct sctp_stream_queue_pending *sp = NULL;
12389         int resv_in_first;
12390
12391         *error = 0;
12392         /* Now can we send this? */
12393         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12394             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12395             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12396             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12397                 /* got data while shutting down */
12398                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12399                 *error = ECONNRESET;
12400                 goto out_now;
12401         }
12402         sctp_alloc_a_strmoq(stcb, sp);
12403         if (sp == NULL) {
12404                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12405                 *error = ENOMEM;
12406                 goto out_now;
12407         }
12408         sp->act_flags = 0;
12409         sp->sender_all_done = 0;
12410         sp->sinfo_flags = srcv->sinfo_flags;
12411         sp->timetolive = srcv->sinfo_timetolive;
12412         sp->ppid = srcv->sinfo_ppid;
12413         sp->context = srcv->sinfo_context;
12414         sp->strseq = 0;
12415         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12416
12417         sp->stream = srcv->sinfo_stream;
12418         sp->length = min(uio->uio_resid, max_send_len);
12419         if ((sp->length == (uint32_t) uio->uio_resid) &&
12420             ((user_marks_eor == 0) ||
12421             (srcv->sinfo_flags & SCTP_EOF) ||
12422             (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12423                 sp->msg_is_complete = 1;
12424         } else {
12425                 sp->msg_is_complete = 0;
12426         }
12427         sp->sender_all_done = 0;
12428         sp->some_taken = 0;
12429         sp->put_last_out = 0;
12430         resv_in_first = sizeof(struct sctp_data_chunk);
12431         sp->data = sp->tail_mbuf = NULL;
12432         if (sp->length == 0) {
12433                 *error = 0;
12434                 goto skip_copy;
12435         }
12436         if (srcv->sinfo_keynumber_valid) {
12437                 sp->auth_keyid = srcv->sinfo_keynumber;
12438         } else {
12439                 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12440         }
12441         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12442                 sctp_auth_key_acquire(stcb, sp->auth_keyid);
12443                 sp->holds_key_ref = 1;
12444         }
12445         *error = sctp_copy_one(sp, uio, resv_in_first);
12446 skip_copy:
12447         if (*error) {
12448                 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12449                 sp = NULL;
12450         } else {
12451                 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12452                         sp->net = net;
12453                         atomic_add_int(&sp->net->ref_count, 1);
12454                 } else {
12455                         sp->net = NULL;
12456                 }
12457                 sctp_set_prsctp_policy(sp);
12458         }
12459 out_now:
12460         return (sp);
12461 }
12462
12463
12464 int
12465 sctp_sosend(struct socket *so,
12466     struct sockaddr *addr,
12467     struct uio *uio,
12468     struct mbuf *top,
12469     struct mbuf *control,
12470     int flags,
12471     struct thread *p
12472 )
12473 {
12474         int error, use_sndinfo = 0;
12475         struct sctp_sndrcvinfo sndrcvninfo;
12476         struct sockaddr *addr_to_use;
12477
12478 #if defined(INET) && defined(INET6)
12479         struct sockaddr_in sin;
12480
12481 #endif
12482
12483         if (control) {
12484                 /* process cmsg snd/rcv info (maybe a assoc-id) */
12485                 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control,
12486                     sizeof(sndrcvninfo))) {
12487                         /* got one */
12488                         use_sndinfo = 1;
12489                 }
12490         }
12491         addr_to_use = addr;
12492 #if defined(INET) && defined(INET6)
12493         if ((addr) && (addr->sa_family == AF_INET6)) {
12494                 struct sockaddr_in6 *sin6;
12495
12496                 sin6 = (struct sockaddr_in6 *)addr;
12497                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12498                         in6_sin6_2_sin(&sin, sin6);
12499                         addr_to_use = (struct sockaddr *)&sin;
12500                 }
12501         }
12502 #endif
12503         error = sctp_lower_sosend(so, addr_to_use, uio, top,
12504             control,
12505             flags,
12506             use_sndinfo ? &sndrcvninfo : NULL
12507             ,p
12508             );
12509         return (error);
12510 }
12511
12512
12513 int
12514 sctp_lower_sosend(struct socket *so,
12515     struct sockaddr *addr,
12516     struct uio *uio,
12517     struct mbuf *i_pak,
12518     struct mbuf *control,
12519     int flags,
12520     struct sctp_sndrcvinfo *srcv
12521     ,
12522     struct thread *p
12523 )
12524 {
12525         unsigned int sndlen = 0, max_len;
12526         int error, len;
12527         struct mbuf *top = NULL;
12528         int queue_only = 0, queue_only_for_init = 0;
12529         int free_cnt_applied = 0;
12530         int un_sent;
12531         int now_filled = 0;
12532         unsigned int inqueue_bytes = 0;
12533         struct sctp_block_entry be;
12534         struct sctp_inpcb *inp;
12535         struct sctp_tcb *stcb = NULL;
12536         struct timeval now;
12537         struct sctp_nets *net;
12538         struct sctp_association *asoc;
12539         struct sctp_inpcb *t_inp;
12540         int user_marks_eor;
12541         int create_lock_applied = 0;
12542         int nagle_applies = 0;
12543         int some_on_control = 0;
12544         int got_all_of_the_send = 0;
12545         int hold_tcblock = 0;
12546         int non_blocking = 0;
12547         uint32_t local_add_more, local_soresv = 0;
12548         uint16_t port;
12549         uint16_t sinfo_flags;
12550         sctp_assoc_t sinfo_assoc_id;
12551
12552         error = 0;
12553         net = NULL;
12554         stcb = NULL;
12555         asoc = NULL;
12556
12557         t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12558         if (inp == NULL) {
12559                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12560                 error = EINVAL;
12561                 if (i_pak) {
12562                         SCTP_RELEASE_PKT(i_pak);
12563                 }
12564                 return (error);
12565         }
12566         if ((uio == NULL) && (i_pak == NULL)) {
12567                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12568                 return (EINVAL);
12569         }
12570         user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12571         atomic_add_int(&inp->total_sends, 1);
12572         if (uio) {
12573                 if (uio->uio_resid < 0) {
12574                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12575                         return (EINVAL);
12576                 }
12577                 sndlen = uio->uio_resid;
12578         } else {
12579                 top = SCTP_HEADER_TO_CHAIN(i_pak);
12580                 sndlen = SCTP_HEADER_LEN(i_pak);
12581         }
12582         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12583             addr,
12584             sndlen);
12585         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12586             (inp->sctp_socket->so_qlimit)) {
12587                 /* The listener can NOT send */
12588                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12589                 error = ENOTCONN;
12590                 goto out_unlocked;
12591         }
12592         /**
12593          * Pre-screen address, if one is given the sin-len
12594          * must be set correctly!
12595          */
12596         if (addr) {
12597                 union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12598
12599                 switch (raddr->sa.sa_family) {
12600 #if defined(INET)
12601                 case AF_INET:
12602                         if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12603                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12604                                 error = EINVAL;
12605                                 goto out_unlocked;
12606                         }
12607                         port = raddr->sin.sin_port;
12608                         break;
12609 #endif
12610 #if defined(INET6)
12611                 case AF_INET6:
12612                         if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12613                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12614                                 error = EINVAL;
12615                                 goto out_unlocked;
12616                         }
12617                         port = raddr->sin6.sin6_port;
12618                         break;
12619 #endif
12620                 default:
12621                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT);
12622                         error = EAFNOSUPPORT;
12623                         goto out_unlocked;
12624                 }
12625         } else
12626                 port = 0;
12627
12628         if (srcv) {
12629                 sinfo_flags = srcv->sinfo_flags;
12630                 sinfo_assoc_id = srcv->sinfo_assoc_id;
12631                 if (INVALID_SINFO_FLAG(sinfo_flags) ||
12632                     PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12633                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12634                         error = EINVAL;
12635                         goto out_unlocked;
12636                 }
12637                 if (srcv->sinfo_flags)
12638                         SCTP_STAT_INCR(sctps_sends_with_flags);
12639         } else {
12640                 sinfo_flags = inp->def_send.sinfo_flags;
12641                 sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12642         }
12643         if (sinfo_flags & SCTP_SENDALL) {
12644                 /* its a sendall */
12645                 error = sctp_sendall(inp, uio, top, srcv);
12646                 top = NULL;
12647                 goto out_unlocked;
12648         }
12649         if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12650                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12651                 error = EINVAL;
12652                 goto out_unlocked;
12653         }
12654         /* now we must find the assoc */
12655         if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12656             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12657                 SCTP_INP_RLOCK(inp);
12658                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12659                 if (stcb) {
12660                         SCTP_TCB_LOCK(stcb);
12661                         hold_tcblock = 1;
12662                 }
12663                 SCTP_INP_RUNLOCK(inp);
12664         } else if (sinfo_assoc_id) {
12665                 stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 0);
12666         } else if (addr) {
12667                 /*-
12668                  * Since we did not use findep we must
12669                  * increment it, and if we don't find a tcb
12670                  * decrement it.
12671                  */
12672                 SCTP_INP_WLOCK(inp);
12673                 SCTP_INP_INCR_REF(inp);
12674                 SCTP_INP_WUNLOCK(inp);
12675                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12676                 if (stcb == NULL) {
12677                         SCTP_INP_WLOCK(inp);
12678                         SCTP_INP_DECR_REF(inp);
12679                         SCTP_INP_WUNLOCK(inp);
12680                 } else {
12681                         hold_tcblock = 1;
12682                 }
12683         }
12684         if ((stcb == NULL) && (addr)) {
12685                 /* Possible implicit send? */
12686                 SCTP_ASOC_CREATE_LOCK(inp);
12687                 create_lock_applied = 1;
12688                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12689                     (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12690                         /* Should I really unlock ? */
12691                         SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12692                         error = EINVAL;
12693                         goto out_unlocked;
12694
12695                 }
12696                 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12697                     (addr->sa_family == AF_INET6)) {
12698                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12699                         error = EINVAL;
12700                         goto out_unlocked;
12701                 }
12702                 SCTP_INP_WLOCK(inp);
12703                 SCTP_INP_INCR_REF(inp);
12704                 SCTP_INP_WUNLOCK(inp);
12705                 /* With the lock applied look again */
12706                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12707                 if ((stcb == NULL) && (control != NULL) && (port > 0)) {
12708                         stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
12709                 }
12710                 if (stcb == NULL) {
12711                         SCTP_INP_WLOCK(inp);
12712                         SCTP_INP_DECR_REF(inp);
12713                         SCTP_INP_WUNLOCK(inp);
12714                 } else {
12715                         hold_tcblock = 1;
12716                 }
12717                 if (error) {
12718                         goto out_unlocked;
12719                 }
12720                 if (t_inp != inp) {
12721                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12722                         error = ENOTCONN;
12723                         goto out_unlocked;
12724                 }
12725         }
12726         if (stcb == NULL) {
12727                 if (addr == NULL) {
12728                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12729                         error = ENOENT;
12730                         goto out_unlocked;
12731                 } else {
12732                         /* We must go ahead and start the INIT process */
12733                         uint32_t vrf_id;
12734
12735                         if ((sinfo_flags & SCTP_ABORT) ||
12736                             ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12737                                 /*-
12738                                  * User asks to abort a non-existant assoc,
12739                                  * or EOF a non-existant assoc with no data
12740                                  */
12741                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12742                                 error = ENOENT;
12743                                 goto out_unlocked;
12744                         }
12745                         /* get an asoc/stcb struct */
12746                         vrf_id = inp->def_vrf_id;
12747 #ifdef INVARIANTS
12748                         if (create_lock_applied == 0) {
12749                                 panic("Error, should hold create lock and I don't?");
12750                         }
12751 #endif
12752                         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12753                             p
12754                             );
12755                         if (stcb == NULL) {
12756                                 /* Error is setup for us in the call */
12757                                 goto out_unlocked;
12758                         }
12759                         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
12760                                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
12761                                 /*
12762                                  * Set the connected flag so we can queue
12763                                  * data
12764                                  */
12765                                 soisconnecting(so);
12766                         }
12767                         hold_tcblock = 1;
12768                         if (create_lock_applied) {
12769                                 SCTP_ASOC_CREATE_UNLOCK(inp);
12770                                 create_lock_applied = 0;
12771                         } else {
12772                                 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12773                         }
12774                         /*
12775                          * Turn on queue only flag to prevent data from
12776                          * being sent
12777                          */
12778                         queue_only = 1;
12779                         asoc = &stcb->asoc;
12780                         SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12781                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12782
12783                         /* initialize authentication params for the assoc */
12784                         sctp_initialize_auth_params(inp, stcb);
12785
12786                         if (control) {
12787                                 if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
12788                                         sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_7);
12789                                         hold_tcblock = 0;
12790                                         stcb = NULL;
12791                                         goto out_unlocked;
12792                                 }
12793                         }
12794                         /* out with the INIT */
12795                         queue_only_for_init = 1;
12796                         /*-
12797                          * we may want to dig in after this call and adjust the MTU
12798                          * value. It defaulted to 1500 (constant) but the ro
12799                          * structure may now have an update and thus we may need to
12800                          * change it BEFORE we append the message.
12801                          */
12802                 }
12803         } else
12804                 asoc = &stcb->asoc;
12805         if (srcv == NULL)
12806                 srcv = (struct sctp_sndrcvinfo *)&asoc->def_send;
12807         if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
12808                 if (addr)
12809                         net = sctp_findnet(stcb, addr);
12810                 else
12811                         net = NULL;
12812                 if ((net == NULL) ||
12813                     ((port != 0) && (port != stcb->rport))) {
12814                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12815                         error = EINVAL;
12816                         goto out_unlocked;
12817                 }
12818         } else {
12819                 if (stcb->asoc.alternate) {
12820                         net = stcb->asoc.alternate;
12821                 } else {
12822                         net = stcb->asoc.primary_destination;
12823                 }
12824         }
12825         atomic_add_int(&stcb->total_sends, 1);
12826         /* Keep the stcb from being freed under our feet */
12827         atomic_add_int(&asoc->refcnt, 1);
12828         free_cnt_applied = 1;
12829
12830         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12831                 if (sndlen > asoc->smallest_mtu) {
12832                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12833                         error = EMSGSIZE;
12834                         goto out_unlocked;
12835                 }
12836         }
12837         if ((SCTP_SO_IS_NBIO(so)
12838             || (flags & MSG_NBIO)
12839             )) {
12840                 non_blocking = 1;
12841         }
12842         /* would we block? */
12843         if (non_blocking) {
12844                 if (hold_tcblock == 0) {
12845                         SCTP_TCB_LOCK(stcb);
12846                         hold_tcblock = 1;
12847                 }
12848                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12849                 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12850                     (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12851                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12852                         if (sndlen > SCTP_SB_LIMIT_SND(so))
12853                                 error = EMSGSIZE;
12854                         else
12855                                 error = EWOULDBLOCK;
12856                         goto out_unlocked;
12857                 }
12858                 stcb->asoc.sb_send_resv += sndlen;
12859                 SCTP_TCB_UNLOCK(stcb);
12860                 hold_tcblock = 0;
12861         } else {
12862                 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12863         }
12864         local_soresv = sndlen;
12865         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12866                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12867                 error = ECONNRESET;
12868                 goto out_unlocked;
12869         }
12870         if (create_lock_applied) {
12871                 SCTP_ASOC_CREATE_UNLOCK(inp);
12872                 create_lock_applied = 0;
12873         }
12874         if (asoc->stream_reset_outstanding) {
12875                 /*
12876                  * Can't queue any data while stream reset is underway.
12877                  */
12878                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12879                 error = EAGAIN;
12880                 goto out_unlocked;
12881         }
12882         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12883             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12884                 queue_only = 1;
12885         }
12886         /* we are now done with all control */
12887         if (control) {
12888                 sctp_m_freem(control);
12889                 control = NULL;
12890         }
12891         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12892             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12893             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12894             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12895                 if (srcv->sinfo_flags & SCTP_ABORT) {
12896                         ;
12897                 } else {
12898                         SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12899                         error = ECONNRESET;
12900                         goto out_unlocked;
12901                 }
12902         }
12903         /* Ok, we will attempt a msgsnd :> */
12904         if (p) {
12905                 p->td_ru.ru_msgsnd++;
12906         }
12907         /* Are we aborting? */
12908         if (srcv->sinfo_flags & SCTP_ABORT) {
12909                 struct mbuf *mm;
12910                 int tot_demand, tot_out = 0, max_out;
12911
12912                 SCTP_STAT_INCR(sctps_sends_with_abort);
12913                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12914                     (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12915                         /* It has to be up before we abort */
12916                         /* how big is the user initiated abort? */
12917                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12918                         error = EINVAL;
12919                         goto out;
12920                 }
12921                 if (hold_tcblock) {
12922                         SCTP_TCB_UNLOCK(stcb);
12923                         hold_tcblock = 0;
12924                 }
12925                 if (top) {
12926                         struct mbuf *cntm = NULL;
12927
12928                         mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12929                         if (sndlen != 0) {
12930                                 cntm = top;
12931                                 while (cntm) {
12932                                         tot_out += SCTP_BUF_LEN(cntm);
12933                                         cntm = SCTP_BUF_NEXT(cntm);
12934                                 }
12935                         }
12936                         tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12937                 } else {
12938                         /* Must fit in a MTU */
12939                         tot_out = sndlen;
12940                         tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12941                         if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12942                                 /* To big */
12943                                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12944                                 error = EMSGSIZE;
12945                                 goto out;
12946                         }
12947                         mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12948                 }
12949                 if (mm == NULL) {
12950                         SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12951                         error = ENOMEM;
12952                         goto out;
12953                 }
12954                 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12955                 max_out -= sizeof(struct sctp_abort_msg);
12956                 if (tot_out > max_out) {
12957                         tot_out = max_out;
12958                 }
12959                 if (mm) {
12960                         struct sctp_paramhdr *ph;
12961
12962                         /* now move forward the data pointer */
12963                         ph = mtod(mm, struct sctp_paramhdr *);
12964                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12965                         ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12966                         ph++;
12967                         SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12968                         if (top == NULL) {
12969                                 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12970                                 if (error) {
12971                                         /*-
12972                                          * Here if we can't get his data we
12973                                          * still abort we just don't get to
12974                                          * send the users note :-0
12975                                          */
12976                                         sctp_m_freem(mm);
12977                                         mm = NULL;
12978                                 }
12979                         } else {
12980                                 if (sndlen != 0) {
12981                                         SCTP_BUF_NEXT(mm) = top;
12982                                 }
12983                         }
12984                 }
12985                 if (hold_tcblock == 0) {
12986                         SCTP_TCB_LOCK(stcb);
12987                         hold_tcblock = 1;
12988                 }
12989                 atomic_add_int(&stcb->asoc.refcnt, -1);
12990                 free_cnt_applied = 0;
12991                 /* release this lock, otherwise we hang on ourselves */
12992                 sctp_abort_an_association(stcb->sctp_ep, stcb,
12993                     SCTP_RESPONSE_TO_USER_REQ,
12994                     mm, SCTP_SO_LOCKED);
12995                 /* now relock the stcb so everything is sane */
12996                 hold_tcblock = 0;
12997                 stcb = NULL;
12998                 /*
12999                  * In this case top is already chained to mm avoid double
13000                  * free, since we free it below if top != NULL and driver
13001                  * would free it after sending the packet out
13002                  */
13003                 if (sndlen != 0) {
13004                         top = NULL;
13005                 }
13006                 goto out_unlocked;
13007         }
13008         /* Calculate the maximum we can send */
13009         inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13010         if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13011                 if (non_blocking) {
13012                         /* we already checked for non-blocking above. */
13013                         max_len = sndlen;
13014                 } else {
13015                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13016                 }
13017         } else {
13018                 max_len = 0;
13019         }
13020         if (hold_tcblock) {
13021                 SCTP_TCB_UNLOCK(stcb);
13022                 hold_tcblock = 0;
13023         }
13024         /* Is the stream no. valid? */
13025         if (srcv->sinfo_stream >= asoc->streamoutcnt) {
13026                 /* Invalid stream number */
13027                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13028                 error = EINVAL;
13029                 goto out_unlocked;
13030         }
13031         if (asoc->strmout == NULL) {
13032                 /* huh? software error */
13033                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
13034                 error = EFAULT;
13035                 goto out_unlocked;
13036         }
13037         /* Unless E_EOR mode is on, we must make a send FIT in one call. */
13038         if ((user_marks_eor == 0) &&
13039             (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
13040                 /* It will NEVER fit */
13041                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13042                 error = EMSGSIZE;
13043                 goto out_unlocked;
13044         }
13045         if ((uio == NULL) && user_marks_eor) {
13046                 /*-
13047                  * We do not support eeor mode for
13048                  * sending with mbuf chains (like sendfile).
13049                  */
13050                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13051                 error = EINVAL;
13052                 goto out_unlocked;
13053         }
13054         if (user_marks_eor) {
13055                 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
13056         } else {
13057                 /*-
13058                  * For non-eeor the whole message must fit in
13059                  * the socket send buffer.
13060                  */
13061                 local_add_more = sndlen;
13062         }
13063         len = 0;
13064         if (non_blocking) {
13065                 goto skip_preblock;
13066         }
13067         if (((max_len <= local_add_more) &&
13068             (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
13069             (max_len == 0) ||
13070             ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13071                 /* No room right now ! */
13072                 SOCKBUF_LOCK(&so->so_snd);
13073                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13074                 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
13075                     ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13076                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
13077                             (unsigned int)SCTP_SB_LIMIT_SND(so),
13078                             inqueue_bytes,
13079                             local_add_more,
13080                             stcb->asoc.stream_queue_cnt,
13081                             stcb->asoc.chunks_on_out_queue,
13082                             SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
13083                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13084                                 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
13085                         }
13086                         be.error = 0;
13087                         stcb->block_entry = &be;
13088                         error = sbwait(&so->so_snd);
13089                         stcb->block_entry = NULL;
13090                         if (error || so->so_error || be.error) {
13091                                 if (error == 0) {
13092                                         if (so->so_error)
13093                                                 error = so->so_error;
13094                                         if (be.error) {
13095                                                 error = be.error;
13096                                         }
13097                                 }
13098                                 SOCKBUF_UNLOCK(&so->so_snd);
13099                                 goto out_unlocked;
13100                         }
13101                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13102                                 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13103                                     so, asoc, stcb->asoc.total_output_queue_size);
13104                         }
13105                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13106                                 goto out_unlocked;
13107                         }
13108                         inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13109                 }
13110                 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13111                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13112                 } else {
13113                         max_len = 0;
13114                 }
13115                 SOCKBUF_UNLOCK(&so->so_snd);
13116         }
13117 skip_preblock:
13118         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13119                 goto out_unlocked;
13120         }
13121         /*
13122          * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13123          * case NOTE: uio will be null when top/mbuf is passed
13124          */
13125         if (sndlen == 0) {
13126                 if (srcv->sinfo_flags & SCTP_EOF) {
13127                         got_all_of_the_send = 1;
13128                         goto dataless_eof;
13129                 } else {
13130                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13131                         error = EINVAL;
13132                         goto out;
13133                 }
13134         }
13135         if (top == NULL) {
13136                 struct sctp_stream_queue_pending *sp;
13137                 struct sctp_stream_out *strm;
13138                 uint32_t sndout;
13139
13140                 SCTP_TCB_SEND_LOCK(stcb);
13141                 if ((asoc->stream_locked) &&
13142                     (asoc->stream_locked_on != srcv->sinfo_stream)) {
13143                         SCTP_TCB_SEND_UNLOCK(stcb);
13144                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13145                         error = EINVAL;
13146                         goto out;
13147                 }
13148                 SCTP_TCB_SEND_UNLOCK(stcb);
13149
13150                 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
13151                 if (strm->last_msg_incomplete == 0) {
13152         do_a_copy_in:
13153                         sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
13154                         if ((sp == NULL) || (error)) {
13155                                 goto out;
13156                         }
13157                         SCTP_TCB_SEND_LOCK(stcb);
13158                         if (sp->msg_is_complete) {
13159                                 strm->last_msg_incomplete = 0;
13160                                 asoc->stream_locked = 0;
13161                         } else {
13162                                 /*
13163                                  * Just got locked to this guy in case of an
13164                                  * interrupt.
13165                                  */
13166                                 strm->last_msg_incomplete = 1;
13167                                 asoc->stream_locked = 1;
13168                                 asoc->stream_locked_on = srcv->sinfo_stream;
13169                                 sp->sender_all_done = 0;
13170                         }
13171                         sctp_snd_sb_alloc(stcb, sp->length);
13172                         atomic_add_int(&asoc->stream_queue_cnt, 1);
13173                         if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
13174                                 sp->strseq = strm->next_sequence_sent;
13175                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
13176                                         sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
13177                                             (uintptr_t) stcb, sp->length,
13178                                             (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
13179                                 }
13180                                 strm->next_sequence_sent++;
13181                         } else {
13182                                 SCTP_STAT_INCR(sctps_sends_with_unord);
13183                         }
13184                         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13185                         stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1);
13186                         SCTP_TCB_SEND_UNLOCK(stcb);
13187                 } else {
13188                         SCTP_TCB_SEND_LOCK(stcb);
13189                         sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13190                         SCTP_TCB_SEND_UNLOCK(stcb);
13191                         if (sp == NULL) {
13192                                 /* ???? Huh ??? last msg is gone */
13193 #ifdef INVARIANTS
13194                                 panic("Warning: Last msg marked incomplete, yet nothing left?");
13195 #else
13196                                 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13197                                 strm->last_msg_incomplete = 0;
13198 #endif
13199                                 goto do_a_copy_in;
13200
13201                         }
13202                 }
13203                 while (uio->uio_resid > 0) {
13204                         /* How much room do we have? */
13205                         struct mbuf *new_tail, *mm;
13206
13207                         if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13208                                 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
13209                         else
13210                                 max_len = 0;
13211
13212                         if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13213                             (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13214                             (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
13215                                 sndout = 0;
13216                                 new_tail = NULL;
13217                                 if (hold_tcblock) {
13218                                         SCTP_TCB_UNLOCK(stcb);
13219                                         hold_tcblock = 0;
13220                                 }
13221                                 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
13222                                 if ((mm == NULL) || error) {
13223                                         if (mm) {
13224                                                 sctp_m_freem(mm);
13225                                         }
13226                                         goto out;
13227                                 }
13228                                 /* Update the mbuf and count */
13229                                 SCTP_TCB_SEND_LOCK(stcb);
13230                                 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13231                                         /*
13232                                          * we need to get out. Peer probably
13233                                          * aborted.
13234                                          */
13235                                         sctp_m_freem(mm);
13236                                         if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13237                                                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13238                                                 error = ECONNRESET;
13239                                         }
13240                                         SCTP_TCB_SEND_UNLOCK(stcb);
13241                                         goto out;
13242                                 }
13243                                 if (sp->tail_mbuf) {
13244                                         /* tack it to the end */
13245                                         SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13246                                         sp->tail_mbuf = new_tail;
13247                                 } else {
13248                                         /* A stolen mbuf */
13249                                         sp->data = mm;
13250                                         sp->tail_mbuf = new_tail;
13251                                 }
13252                                 sctp_snd_sb_alloc(stcb, sndout);
13253                                 atomic_add_int(&sp->length, sndout);
13254                                 len += sndout;
13255
13256                                 /* Did we reach EOR? */
13257                                 if ((uio->uio_resid == 0) &&
13258                                     ((user_marks_eor == 0) ||
13259                                     (srcv->sinfo_flags & SCTP_EOF) ||
13260                                     (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13261                                         sp->msg_is_complete = 1;
13262                                 } else {
13263                                         sp->msg_is_complete = 0;
13264                                 }
13265                                 SCTP_TCB_SEND_UNLOCK(stcb);
13266                         }
13267                         if (uio->uio_resid == 0) {
13268                                 /* got it all? */
13269                                 continue;
13270                         }
13271                         /* PR-SCTP? */
13272                         if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13273                                 /*
13274                                  * This is ugly but we must assure locking
13275                                  * order
13276                                  */
13277                                 if (hold_tcblock == 0) {
13278                                         SCTP_TCB_LOCK(stcb);
13279                                         hold_tcblock = 1;
13280                                 }
13281                                 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13282                                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13283                                 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13284                                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13285                                 else
13286                                         max_len = 0;
13287                                 if (max_len > 0) {
13288                                         continue;
13289                                 }
13290                                 SCTP_TCB_UNLOCK(stcb);
13291                                 hold_tcblock = 0;
13292                         }
13293                         /* wait for space now */
13294                         if (non_blocking) {
13295                                 /* Non-blocking io in place out */
13296                                 goto skip_out_eof;
13297                         }
13298                         /* What about the INIT, send it maybe */
13299                         if (queue_only_for_init) {
13300                                 if (hold_tcblock == 0) {
13301                                         SCTP_TCB_LOCK(stcb);
13302                                         hold_tcblock = 1;
13303                                 }
13304                                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13305                                         /* a collision took us forward? */
13306                                         queue_only = 0;
13307                                 } else {
13308                                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13309                                         SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13310                                         queue_only = 1;
13311                                 }
13312                         }
13313                         if ((net->flight_size > net->cwnd) &&
13314                             (asoc->sctp_cmt_on_off == 0)) {
13315                                 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13316                                 queue_only = 1;
13317                         } else if (asoc->ifp_had_enobuf) {
13318                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
13319                                 if (net->flight_size > (2 * net->mtu)) {
13320                                         queue_only = 1;
13321                                 }
13322                                 asoc->ifp_had_enobuf = 0;
13323                         }
13324                         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13325                             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13326                         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13327                             (stcb->asoc.total_flight > 0) &&
13328                             (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13329                             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13330
13331                                 /*-
13332                                  * Ok, Nagle is set on and we have data outstanding.
13333                                  * Don't send anything and let SACKs drive out the
13334                                  * data unless wen have a "full" segment to send.
13335                                  */
13336                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13337                                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13338                                 }
13339                                 SCTP_STAT_INCR(sctps_naglequeued);
13340                                 nagle_applies = 1;
13341                         } else {
13342                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13343                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13344                                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13345                                 }
13346                                 SCTP_STAT_INCR(sctps_naglesent);
13347                                 nagle_applies = 0;
13348                         }
13349                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13350
13351                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13352                                     nagle_applies, un_sent);
13353                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13354                                     stcb->asoc.total_flight,
13355                                     stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13356                         }
13357                         if (queue_only_for_init)
13358                                 queue_only_for_init = 0;
13359                         if ((queue_only == 0) && (nagle_applies == 0)) {
13360                                 /*-
13361                                  * need to start chunk output
13362                                  * before blocking.. note that if
13363                                  * a lock is already applied, then
13364                                  * the input via the net is happening
13365                                  * and I don't need to start output :-D
13366                                  */
13367                                 if (hold_tcblock == 0) {
13368                                         if (SCTP_TCB_TRYLOCK(stcb)) {
13369                                                 hold_tcblock = 1;
13370                                                 sctp_chunk_output(inp,
13371                                                     stcb,
13372                                                     SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13373                                         }
13374                                 } else {
13375                                         sctp_chunk_output(inp,
13376                                             stcb,
13377                                             SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13378                                 }
13379                                 if (hold_tcblock == 1) {
13380                                         SCTP_TCB_UNLOCK(stcb);
13381                                         hold_tcblock = 0;
13382                                 }
13383                         }
13384                         SOCKBUF_LOCK(&so->so_snd);
13385                         /*-
13386                          * This is a bit strange, but I think it will
13387                          * work. The total_output_queue_size is locked and
13388                          * protected by the TCB_LOCK, which we just released.
13389                          * There is a race that can occur between releasing it
13390                          * above, and me getting the socket lock, where sacks
13391                          * come in but we have not put the SB_WAIT on the
13392                          * so_snd buffer to get the wakeup. After the LOCK
13393                          * is applied the sack_processing will also need to
13394                          * LOCK the so->so_snd to do the actual sowwakeup(). So
13395                          * once we have the socket buffer lock if we recheck the
13396                          * size we KNOW we will get to sleep safely with the
13397                          * wakeup flag in place.
13398                          */
13399                         if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13400                             min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13401                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13402                                         sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13403                                             so, asoc, uio->uio_resid);
13404                                 }
13405                                 be.error = 0;
13406                                 stcb->block_entry = &be;
13407                                 error = sbwait(&so->so_snd);
13408                                 stcb->block_entry = NULL;
13409
13410                                 if (error || so->so_error || be.error) {
13411                                         if (error == 0) {
13412                                                 if (so->so_error)
13413                                                         error = so->so_error;
13414                                                 if (be.error) {
13415                                                         error = be.error;
13416                                                 }
13417                                         }
13418                                         SOCKBUF_UNLOCK(&so->so_snd);
13419                                         goto out_unlocked;
13420                                 }
13421                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13422                                         sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13423                                             so, asoc, stcb->asoc.total_output_queue_size);
13424                                 }
13425                         }
13426                         SOCKBUF_UNLOCK(&so->so_snd);
13427                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13428                                 goto out_unlocked;
13429                         }
13430                 }
13431                 SCTP_TCB_SEND_LOCK(stcb);
13432                 if (sp) {
13433                         if (sp->msg_is_complete == 0) {
13434                                 strm->last_msg_incomplete = 1;
13435                                 asoc->stream_locked = 1;
13436                                 asoc->stream_locked_on = srcv->sinfo_stream;
13437                         } else {
13438                                 sp->sender_all_done = 1;
13439                                 strm->last_msg_incomplete = 0;
13440                                 asoc->stream_locked = 0;
13441                         }
13442                 } else {
13443                         SCTP_PRINTF("Huh no sp TSNH?\n");
13444                         strm->last_msg_incomplete = 0;
13445                         asoc->stream_locked = 0;
13446                 }
13447                 SCTP_TCB_SEND_UNLOCK(stcb);
13448                 if (uio->uio_resid == 0) {
13449                         got_all_of_the_send = 1;
13450                 }
13451         } else {
13452                 /* We send in a 0, since we do NOT have any locks */
13453                 error = sctp_msg_append(stcb, net, top, srcv, 0);
13454                 top = NULL;
13455                 if (srcv->sinfo_flags & SCTP_EOF) {
13456                         /*
13457                          * This should only happen for Panda for the mbuf
13458                          * send case, which does NOT yet support EEOR mode.
13459                          * Thus, we can just set this flag to do the proper
13460                          * EOF handling.
13461                          */
13462                         got_all_of_the_send = 1;
13463                 }
13464         }
13465         if (error) {
13466                 goto out;
13467         }
13468 dataless_eof:
13469         /* EOF thing ? */
13470         if ((srcv->sinfo_flags & SCTP_EOF) &&
13471             (got_all_of_the_send == 1) &&
13472             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13473                 int cnt;
13474
13475                 SCTP_STAT_INCR(sctps_sends_with_eof);
13476                 error = 0;
13477                 if (hold_tcblock == 0) {
13478                         SCTP_TCB_LOCK(stcb);
13479                         hold_tcblock = 1;
13480                 }
13481                 cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED);
13482                 if (TAILQ_EMPTY(&asoc->send_queue) &&
13483                     TAILQ_EMPTY(&asoc->sent_queue) &&
13484                     (cnt == 0)) {
13485                         if (asoc->locked_on_sending) {
13486                                 goto abort_anyway;
13487                         }
13488                         /* there is nothing queued to send, so I'm done... */
13489                         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13490                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13491                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13492                                 struct sctp_nets *netp;
13493
13494                                 if (stcb->asoc.alternate) {
13495                                         netp = stcb->asoc.alternate;
13496                                 } else {
13497                                         netp = stcb->asoc.primary_destination;
13498                                 }
13499                                 /* only send SHUTDOWN the first time through */
13500                                 sctp_send_shutdown(stcb, netp);
13501                                 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13502                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13503                                 }
13504                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13505                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13506                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13507                                     netp);
13508                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13509                                     asoc->primary_destination);
13510                         }
13511                 } else {
13512                         /*-
13513                          * we still got (or just got) data to send, so set
13514                          * SHUTDOWN_PENDING
13515                          */
13516                         /*-
13517                          * XXX sockets draft says that SCTP_EOF should be
13518                          * sent with no data.  currently, we will allow user
13519                          * data to be sent first and move to
13520                          * SHUTDOWN-PENDING
13521                          */
13522                         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13523                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13524                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13525                                 if (hold_tcblock == 0) {
13526                                         SCTP_TCB_LOCK(stcb);
13527                                         hold_tcblock = 1;
13528                                 }
13529                                 if (asoc->locked_on_sending) {
13530                                         /* Locked to send out the data */
13531                                         struct sctp_stream_queue_pending *sp;
13532
13533                                         sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13534                                         if (sp) {
13535                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
13536                                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13537                                         }
13538                                 }
13539                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13540                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
13541                                     TAILQ_EMPTY(&asoc->sent_queue) &&
13542                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13543                         abort_anyway:
13544                                         if (free_cnt_applied) {
13545                                                 atomic_add_int(&stcb->asoc.refcnt, -1);
13546                                                 free_cnt_applied = 0;
13547                                         }
13548                                         sctp_abort_an_association(stcb->sctp_ep, stcb,
13549                                             SCTP_RESPONSE_TO_USER_REQ,
13550                                             NULL, SCTP_SO_LOCKED);
13551                                         /*
13552                                          * now relock the stcb so everything
13553                                          * is sane
13554                                          */
13555                                         hold_tcblock = 0;
13556                                         stcb = NULL;
13557                                         goto out;
13558                                 }
13559                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13560                                     asoc->primary_destination);
13561                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13562                         }
13563                 }
13564         }
13565 skip_out_eof:
13566         if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13567                 some_on_control = 1;
13568         }
13569         if (queue_only_for_init) {
13570                 if (hold_tcblock == 0) {
13571                         SCTP_TCB_LOCK(stcb);
13572                         hold_tcblock = 1;
13573                 }
13574                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13575                         /* a collision took us forward? */
13576                         queue_only = 0;
13577                 } else {
13578                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13579                         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13580                         queue_only = 1;
13581                 }
13582         }
13583         if ((net->flight_size > net->cwnd) &&
13584             (stcb->asoc.sctp_cmt_on_off == 0)) {
13585                 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13586                 queue_only = 1;
13587         } else if (asoc->ifp_had_enobuf) {
13588                 SCTP_STAT_INCR(sctps_ifnomemqueued);
13589                 if (net->flight_size > (2 * net->mtu)) {
13590                         queue_only = 1;
13591                 }
13592                 asoc->ifp_had_enobuf = 0;
13593         }
13594         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13595             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13596         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13597             (stcb->asoc.total_flight > 0) &&
13598             (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13599             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13600                 /*-
13601                  * Ok, Nagle is set on and we have data outstanding.
13602                  * Don't send anything and let SACKs drive out the
13603                  * data unless wen have a "full" segment to send.
13604                  */
13605                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13606                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13607                 }
13608                 SCTP_STAT_INCR(sctps_naglequeued);
13609                 nagle_applies = 1;
13610         } else {
13611                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13612                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13613                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13614                 }
13615                 SCTP_STAT_INCR(sctps_naglesent);
13616                 nagle_applies = 0;
13617         }
13618         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13619                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13620                     nagle_applies, un_sent);
13621                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13622                     stcb->asoc.total_flight,
13623                     stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13624         }
13625         if (queue_only_for_init)
13626                 queue_only_for_init = 0;
13627         if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13628                 /* we can attempt to send too. */
13629                 if (hold_tcblock == 0) {
13630                         /*
13631                          * If there is activity recv'ing sacks no need to
13632                          * send
13633                          */
13634                         if (SCTP_TCB_TRYLOCK(stcb)) {
13635                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13636                                 hold_tcblock = 1;
13637                         }
13638                 } else {
13639                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13640                 }
13641         } else if ((queue_only == 0) &&
13642                     (stcb->asoc.peers_rwnd == 0) &&
13643             (stcb->asoc.total_flight == 0)) {
13644                 /* We get to have a probe outstanding */
13645                 if (hold_tcblock == 0) {
13646                         hold_tcblock = 1;
13647                         SCTP_TCB_LOCK(stcb);
13648                 }
13649                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13650         } else if (some_on_control) {
13651                 int num_out, reason, frag_point;
13652
13653                 /* Here we do control only */
13654                 if (hold_tcblock == 0) {
13655                         hold_tcblock = 1;
13656                         SCTP_TCB_LOCK(stcb);
13657                 }
13658                 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13659                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13660                     &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13661         }
13662         SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13663             queue_only, stcb->asoc.peers_rwnd, un_sent,
13664             stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13665             stcb->asoc.total_output_queue_size, error);
13666
13667 out:
13668 out_unlocked:
13669
13670         if (local_soresv && stcb) {
13671                 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13672                 local_soresv = 0;
13673         }
13674         if (create_lock_applied) {
13675                 SCTP_ASOC_CREATE_UNLOCK(inp);
13676                 create_lock_applied = 0;
13677         }
13678         if ((stcb) && hold_tcblock) {
13679                 SCTP_TCB_UNLOCK(stcb);
13680         }
13681         if (stcb && free_cnt_applied) {
13682                 atomic_add_int(&stcb->asoc.refcnt, -1);
13683         }
13684 #ifdef INVARIANTS
13685         if (stcb) {
13686                 if (mtx_owned(&stcb->tcb_mtx)) {
13687                         panic("Leaving with tcb mtx owned?");
13688                 }
13689                 if (mtx_owned(&stcb->tcb_send_mtx)) {
13690                         panic("Leaving with tcb send mtx owned?");
13691                 }
13692         }
13693 #endif
13694 #ifdef INVARIANTS
13695         if (inp) {
13696                 sctp_validate_no_locks(inp);
13697         } else {
13698                 printf("Warning - inp is NULL so cant validate locks\n");
13699         }
13700 #endif
13701         if (top) {
13702                 sctp_m_freem(top);
13703         }
13704         if (control) {
13705                 sctp_m_freem(control);
13706         }
13707         return (error);
13708 }
13709
13710
13711 /*
13712  * generate an AUTHentication chunk, if required
13713  */
13714 struct mbuf *
13715 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13716     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13717     struct sctp_tcb *stcb, uint8_t chunk)
13718 {
13719         struct mbuf *m_auth;
13720         struct sctp_auth_chunk *auth;
13721         int chunk_len;
13722
13723         if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13724             (stcb == NULL))
13725                 return (m);
13726
13727         /* sysctl disabled auth? */
13728         if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13729                 return (m);
13730
13731         /* peer doesn't do auth... */
13732         if (!stcb->asoc.peer_supports_auth) {
13733                 return (m);
13734         }
13735         /* does the requested chunk require auth? */
13736         if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13737                 return (m);
13738         }
13739         m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13740         if (m_auth == NULL) {
13741                 /* no mbuf's */
13742                 return (m);
13743         }
13744         /* reserve some space if this will be the first mbuf */
13745         if (m == NULL)
13746                 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13747         /* fill in the AUTH chunk details */
13748         auth = mtod(m_auth, struct sctp_auth_chunk *);
13749         bzero(auth, sizeof(*auth));
13750         auth->ch.chunk_type = SCTP_AUTHENTICATION;
13751         auth->ch.chunk_flags = 0;
13752         chunk_len = sizeof(*auth) +
13753             sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13754         auth->ch.chunk_length = htons(chunk_len);
13755         auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13756         /* key id and hmac digest will be computed and filled in upon send */
13757
13758         /* save the offset where the auth was inserted into the chain */
13759         if (m != NULL) {
13760                 struct mbuf *cn;
13761
13762                 *offset = 0;
13763                 cn = m;
13764                 while (cn) {
13765                         *offset += SCTP_BUF_LEN(cn);
13766                         cn = SCTP_BUF_NEXT(cn);
13767                 }
13768         } else
13769                 *offset = 0;
13770
13771         /* update length and return pointer to the auth chunk */
13772         SCTP_BUF_LEN(m_auth) = chunk_len;
13773         m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13774         if (auth_ret != NULL)
13775                 *auth_ret = auth;
13776
13777         return (m);
13778 }
13779
13780 #ifdef INET6
13781 int
13782 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13783 {
13784         struct nd_prefix *pfx = NULL;
13785         struct nd_pfxrouter *pfxrtr = NULL;
13786         struct sockaddr_in6 gw6;
13787
13788         if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13789                 return (0);
13790
13791         /* get prefix entry of address */
13792         LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13793                 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13794                         continue;
13795                 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13796                     &src6->sin6_addr, &pfx->ndpr_mask))
13797                         break;
13798         }
13799         /* no prefix entry in the prefix list */
13800         if (pfx == NULL) {
13801                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13802                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13803                 return (0);
13804         }
13805         SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13806         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13807
13808         /* search installed gateway from prefix entry */
13809         for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13810             pfxrtr->pfr_next) {
13811                 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13812                 gw6.sin6_family = AF_INET6;
13813                 gw6.sin6_len = sizeof(struct sockaddr_in6);
13814                 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13815                     sizeof(struct in6_addr));
13816                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13817                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13818                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13819                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13820                 if (sctp_cmpaddr((struct sockaddr *)&gw6,
13821                     ro->ro_rt->rt_gateway)) {
13822                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13823                         return (1);
13824                 }
13825         }
13826         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13827         return (0);
13828 }
13829
13830 #endif
13831
13832 int
13833 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13834 {
13835 #ifdef INET
13836         struct sockaddr_in *sin, *mask;
13837         struct ifaddr *ifa;
13838         struct in_addr srcnetaddr, gwnetaddr;
13839
13840         if (ro == NULL || ro->ro_rt == NULL ||
13841             sifa->address.sa.sa_family != AF_INET) {
13842                 return (0);
13843         }
13844         ifa = (struct ifaddr *)sifa->ifa;
13845         mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13846         sin = (struct sockaddr_in *)&sifa->address.sin;
13847         srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13848         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13849         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13850         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13851
13852         sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13853         gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13854         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13855         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13856         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13857         if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13858                 return (1);
13859         }
13860 #endif
13861         return (0);
13862 }