]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/netinet/sctp_output.c
MFC r235416:
[FreeBSD/stable/9.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 <netinet/udp_var.h>
57 #include <machine/in_cksum.h>
58
59
60
61 #define SCTP_MAX_GAPS_INARRAY 4
62 struct sack_track {
63         uint8_t right_edge;     /* mergable on the right edge */
64         uint8_t left_edge;      /* mergable on the left edge */
65         uint8_t num_entries;
66         uint8_t spare;
67         struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
68 };
69
70 struct sack_track sack_array[256] = {
71         {0, 0, 0, 0,            /* 0x00 */
72                 {{0, 0},
73                 {0, 0},
74                 {0, 0},
75                 {0, 0}
76                 }
77         },
78         {1, 0, 1, 0,            /* 0x01 */
79                 {{0, 0},
80                 {0, 0},
81                 {0, 0},
82                 {0, 0}
83                 }
84         },
85         {0, 0, 1, 0,            /* 0x02 */
86                 {{1, 1},
87                 {0, 0},
88                 {0, 0},
89                 {0, 0}
90                 }
91         },
92         {1, 0, 1, 0,            /* 0x03 */
93                 {{0, 1},
94                 {0, 0},
95                 {0, 0},
96                 {0, 0}
97                 }
98         },
99         {0, 0, 1, 0,            /* 0x04 */
100                 {{2, 2},
101                 {0, 0},
102                 {0, 0},
103                 {0, 0}
104                 }
105         },
106         {1, 0, 2, 0,            /* 0x05 */
107                 {{0, 0},
108                 {2, 2},
109                 {0, 0},
110                 {0, 0}
111                 }
112         },
113         {0, 0, 1, 0,            /* 0x06 */
114                 {{1, 2},
115                 {0, 0},
116                 {0, 0},
117                 {0, 0}
118                 }
119         },
120         {1, 0, 1, 0,            /* 0x07 */
121                 {{0, 2},
122                 {0, 0},
123                 {0, 0},
124                 {0, 0}
125                 }
126         },
127         {0, 0, 1, 0,            /* 0x08 */
128                 {{3, 3},
129                 {0, 0},
130                 {0, 0},
131                 {0, 0}
132                 }
133         },
134         {1, 0, 2, 0,            /* 0x09 */
135                 {{0, 0},
136                 {3, 3},
137                 {0, 0},
138                 {0, 0}
139                 }
140         },
141         {0, 0, 2, 0,            /* 0x0a */
142                 {{1, 1},
143                 {3, 3},
144                 {0, 0},
145                 {0, 0}
146                 }
147         },
148         {1, 0, 2, 0,            /* 0x0b */
149                 {{0, 1},
150                 {3, 3},
151                 {0, 0},
152                 {0, 0}
153                 }
154         },
155         {0, 0, 1, 0,            /* 0x0c */
156                 {{2, 3},
157                 {0, 0},
158                 {0, 0},
159                 {0, 0}
160                 }
161         },
162         {1, 0, 2, 0,            /* 0x0d */
163                 {{0, 0},
164                 {2, 3},
165                 {0, 0},
166                 {0, 0}
167                 }
168         },
169         {0, 0, 1, 0,            /* 0x0e */
170                 {{1, 3},
171                 {0, 0},
172                 {0, 0},
173                 {0, 0}
174                 }
175         },
176         {1, 0, 1, 0,            /* 0x0f */
177                 {{0, 3},
178                 {0, 0},
179                 {0, 0},
180                 {0, 0}
181                 }
182         },
183         {0, 0, 1, 0,            /* 0x10 */
184                 {{4, 4},
185                 {0, 0},
186                 {0, 0},
187                 {0, 0}
188                 }
189         },
190         {1, 0, 2, 0,            /* 0x11 */
191                 {{0, 0},
192                 {4, 4},
193                 {0, 0},
194                 {0, 0}
195                 }
196         },
197         {0, 0, 2, 0,            /* 0x12 */
198                 {{1, 1},
199                 {4, 4},
200                 {0, 0},
201                 {0, 0}
202                 }
203         },
204         {1, 0, 2, 0,            /* 0x13 */
205                 {{0, 1},
206                 {4, 4},
207                 {0, 0},
208                 {0, 0}
209                 }
210         },
211         {0, 0, 2, 0,            /* 0x14 */
212                 {{2, 2},
213                 {4, 4},
214                 {0, 0},
215                 {0, 0}
216                 }
217         },
218         {1, 0, 3, 0,            /* 0x15 */
219                 {{0, 0},
220                 {2, 2},
221                 {4, 4},
222                 {0, 0}
223                 }
224         },
225         {0, 0, 2, 0,            /* 0x16 */
226                 {{1, 2},
227                 {4, 4},
228                 {0, 0},
229                 {0, 0}
230                 }
231         },
232         {1, 0, 2, 0,            /* 0x17 */
233                 {{0, 2},
234                 {4, 4},
235                 {0, 0},
236                 {0, 0}
237                 }
238         },
239         {0, 0, 1, 0,            /* 0x18 */
240                 {{3, 4},
241                 {0, 0},
242                 {0, 0},
243                 {0, 0}
244                 }
245         },
246         {1, 0, 2, 0,            /* 0x19 */
247                 {{0, 0},
248                 {3, 4},
249                 {0, 0},
250                 {0, 0}
251                 }
252         },
253         {0, 0, 2, 0,            /* 0x1a */
254                 {{1, 1},
255                 {3, 4},
256                 {0, 0},
257                 {0, 0}
258                 }
259         },
260         {1, 0, 2, 0,            /* 0x1b */
261                 {{0, 1},
262                 {3, 4},
263                 {0, 0},
264                 {0, 0}
265                 }
266         },
267         {0, 0, 1, 0,            /* 0x1c */
268                 {{2, 4},
269                 {0, 0},
270                 {0, 0},
271                 {0, 0}
272                 }
273         },
274         {1, 0, 2, 0,            /* 0x1d */
275                 {{0, 0},
276                 {2, 4},
277                 {0, 0},
278                 {0, 0}
279                 }
280         },
281         {0, 0, 1, 0,            /* 0x1e */
282                 {{1, 4},
283                 {0, 0},
284                 {0, 0},
285                 {0, 0}
286                 }
287         },
288         {1, 0, 1, 0,            /* 0x1f */
289                 {{0, 4},
290                 {0, 0},
291                 {0, 0},
292                 {0, 0}
293                 }
294         },
295         {0, 0, 1, 0,            /* 0x20 */
296                 {{5, 5},
297                 {0, 0},
298                 {0, 0},
299                 {0, 0}
300                 }
301         },
302         {1, 0, 2, 0,            /* 0x21 */
303                 {{0, 0},
304                 {5, 5},
305                 {0, 0},
306                 {0, 0}
307                 }
308         },
309         {0, 0, 2, 0,            /* 0x22 */
310                 {{1, 1},
311                 {5, 5},
312                 {0, 0},
313                 {0, 0}
314                 }
315         },
316         {1, 0, 2, 0,            /* 0x23 */
317                 {{0, 1},
318                 {5, 5},
319                 {0, 0},
320                 {0, 0}
321                 }
322         },
323         {0, 0, 2, 0,            /* 0x24 */
324                 {{2, 2},
325                 {5, 5},
326                 {0, 0},
327                 {0, 0}
328                 }
329         },
330         {1, 0, 3, 0,            /* 0x25 */
331                 {{0, 0},
332                 {2, 2},
333                 {5, 5},
334                 {0, 0}
335                 }
336         },
337         {0, 0, 2, 0,            /* 0x26 */
338                 {{1, 2},
339                 {5, 5},
340                 {0, 0},
341                 {0, 0}
342                 }
343         },
344         {1, 0, 2, 0,            /* 0x27 */
345                 {{0, 2},
346                 {5, 5},
347                 {0, 0},
348                 {0, 0}
349                 }
350         },
351         {0, 0, 2, 0,            /* 0x28 */
352                 {{3, 3},
353                 {5, 5},
354                 {0, 0},
355                 {0, 0}
356                 }
357         },
358         {1, 0, 3, 0,            /* 0x29 */
359                 {{0, 0},
360                 {3, 3},
361                 {5, 5},
362                 {0, 0}
363                 }
364         },
365         {0, 0, 3, 0,            /* 0x2a */
366                 {{1, 1},
367                 {3, 3},
368                 {5, 5},
369                 {0, 0}
370                 }
371         },
372         {1, 0, 3, 0,            /* 0x2b */
373                 {{0, 1},
374                 {3, 3},
375                 {5, 5},
376                 {0, 0}
377                 }
378         },
379         {0, 0, 2, 0,            /* 0x2c */
380                 {{2, 3},
381                 {5, 5},
382                 {0, 0},
383                 {0, 0}
384                 }
385         },
386         {1, 0, 3, 0,            /* 0x2d */
387                 {{0, 0},
388                 {2, 3},
389                 {5, 5},
390                 {0, 0}
391                 }
392         },
393         {0, 0, 2, 0,            /* 0x2e */
394                 {{1, 3},
395                 {5, 5},
396                 {0, 0},
397                 {0, 0}
398                 }
399         },
400         {1, 0, 2, 0,            /* 0x2f */
401                 {{0, 3},
402                 {5, 5},
403                 {0, 0},
404                 {0, 0}
405                 }
406         },
407         {0, 0, 1, 0,            /* 0x30 */
408                 {{4, 5},
409                 {0, 0},
410                 {0, 0},
411                 {0, 0}
412                 }
413         },
414         {1, 0, 2, 0,            /* 0x31 */
415                 {{0, 0},
416                 {4, 5},
417                 {0, 0},
418                 {0, 0}
419                 }
420         },
421         {0, 0, 2, 0,            /* 0x32 */
422                 {{1, 1},
423                 {4, 5},
424                 {0, 0},
425                 {0, 0}
426                 }
427         },
428         {1, 0, 2, 0,            /* 0x33 */
429                 {{0, 1},
430                 {4, 5},
431                 {0, 0},
432                 {0, 0}
433                 }
434         },
435         {0, 0, 2, 0,            /* 0x34 */
436                 {{2, 2},
437                 {4, 5},
438                 {0, 0},
439                 {0, 0}
440                 }
441         },
442         {1, 0, 3, 0,            /* 0x35 */
443                 {{0, 0},
444                 {2, 2},
445                 {4, 5},
446                 {0, 0}
447                 }
448         },
449         {0, 0, 2, 0,            /* 0x36 */
450                 {{1, 2},
451                 {4, 5},
452                 {0, 0},
453                 {0, 0}
454                 }
455         },
456         {1, 0, 2, 0,            /* 0x37 */
457                 {{0, 2},
458                 {4, 5},
459                 {0, 0},
460                 {0, 0}
461                 }
462         },
463         {0, 0, 1, 0,            /* 0x38 */
464                 {{3, 5},
465                 {0, 0},
466                 {0, 0},
467                 {0, 0}
468                 }
469         },
470         {1, 0, 2, 0,            /* 0x39 */
471                 {{0, 0},
472                 {3, 5},
473                 {0, 0},
474                 {0, 0}
475                 }
476         },
477         {0, 0, 2, 0,            /* 0x3a */
478                 {{1, 1},
479                 {3, 5},
480                 {0, 0},
481                 {0, 0}
482                 }
483         },
484         {1, 0, 2, 0,            /* 0x3b */
485                 {{0, 1},
486                 {3, 5},
487                 {0, 0},
488                 {0, 0}
489                 }
490         },
491         {0, 0, 1, 0,            /* 0x3c */
492                 {{2, 5},
493                 {0, 0},
494                 {0, 0},
495                 {0, 0}
496                 }
497         },
498         {1, 0, 2, 0,            /* 0x3d */
499                 {{0, 0},
500                 {2, 5},
501                 {0, 0},
502                 {0, 0}
503                 }
504         },
505         {0, 0, 1, 0,            /* 0x3e */
506                 {{1, 5},
507                 {0, 0},
508                 {0, 0},
509                 {0, 0}
510                 }
511         },
512         {1, 0, 1, 0,            /* 0x3f */
513                 {{0, 5},
514                 {0, 0},
515                 {0, 0},
516                 {0, 0}
517                 }
518         },
519         {0, 0, 1, 0,            /* 0x40 */
520                 {{6, 6},
521                 {0, 0},
522                 {0, 0},
523                 {0, 0}
524                 }
525         },
526         {1, 0, 2, 0,            /* 0x41 */
527                 {{0, 0},
528                 {6, 6},
529                 {0, 0},
530                 {0, 0}
531                 }
532         },
533         {0, 0, 2, 0,            /* 0x42 */
534                 {{1, 1},
535                 {6, 6},
536                 {0, 0},
537                 {0, 0}
538                 }
539         },
540         {1, 0, 2, 0,            /* 0x43 */
541                 {{0, 1},
542                 {6, 6},
543                 {0, 0},
544                 {0, 0}
545                 }
546         },
547         {0, 0, 2, 0,            /* 0x44 */
548                 {{2, 2},
549                 {6, 6},
550                 {0, 0},
551                 {0, 0}
552                 }
553         },
554         {1, 0, 3, 0,            /* 0x45 */
555                 {{0, 0},
556                 {2, 2},
557                 {6, 6},
558                 {0, 0}
559                 }
560         },
561         {0, 0, 2, 0,            /* 0x46 */
562                 {{1, 2},
563                 {6, 6},
564                 {0, 0},
565                 {0, 0}
566                 }
567         },
568         {1, 0, 2, 0,            /* 0x47 */
569                 {{0, 2},
570                 {6, 6},
571                 {0, 0},
572                 {0, 0}
573                 }
574         },
575         {0, 0, 2, 0,            /* 0x48 */
576                 {{3, 3},
577                 {6, 6},
578                 {0, 0},
579                 {0, 0}
580                 }
581         },
582         {1, 0, 3, 0,            /* 0x49 */
583                 {{0, 0},
584                 {3, 3},
585                 {6, 6},
586                 {0, 0}
587                 }
588         },
589         {0, 0, 3, 0,            /* 0x4a */
590                 {{1, 1},
591                 {3, 3},
592                 {6, 6},
593                 {0, 0}
594                 }
595         },
596         {1, 0, 3, 0,            /* 0x4b */
597                 {{0, 1},
598                 {3, 3},
599                 {6, 6},
600                 {0, 0}
601                 }
602         },
603         {0, 0, 2, 0,            /* 0x4c */
604                 {{2, 3},
605                 {6, 6},
606                 {0, 0},
607                 {0, 0}
608                 }
609         },
610         {1, 0, 3, 0,            /* 0x4d */
611                 {{0, 0},
612                 {2, 3},
613                 {6, 6},
614                 {0, 0}
615                 }
616         },
617         {0, 0, 2, 0,            /* 0x4e */
618                 {{1, 3},
619                 {6, 6},
620                 {0, 0},
621                 {0, 0}
622                 }
623         },
624         {1, 0, 2, 0,            /* 0x4f */
625                 {{0, 3},
626                 {6, 6},
627                 {0, 0},
628                 {0, 0}
629                 }
630         },
631         {0, 0, 2, 0,            /* 0x50 */
632                 {{4, 4},
633                 {6, 6},
634                 {0, 0},
635                 {0, 0}
636                 }
637         },
638         {1, 0, 3, 0,            /* 0x51 */
639                 {{0, 0},
640                 {4, 4},
641                 {6, 6},
642                 {0, 0}
643                 }
644         },
645         {0, 0, 3, 0,            /* 0x52 */
646                 {{1, 1},
647                 {4, 4},
648                 {6, 6},
649                 {0, 0}
650                 }
651         },
652         {1, 0, 3, 0,            /* 0x53 */
653                 {{0, 1},
654                 {4, 4},
655                 {6, 6},
656                 {0, 0}
657                 }
658         },
659         {0, 0, 3, 0,            /* 0x54 */
660                 {{2, 2},
661                 {4, 4},
662                 {6, 6},
663                 {0, 0}
664                 }
665         },
666         {1, 0, 4, 0,            /* 0x55 */
667                 {{0, 0},
668                 {2, 2},
669                 {4, 4},
670                 {6, 6}
671                 }
672         },
673         {0, 0, 3, 0,            /* 0x56 */
674                 {{1, 2},
675                 {4, 4},
676                 {6, 6},
677                 {0, 0}
678                 }
679         },
680         {1, 0, 3, 0,            /* 0x57 */
681                 {{0, 2},
682                 {4, 4},
683                 {6, 6},
684                 {0, 0}
685                 }
686         },
687         {0, 0, 2, 0,            /* 0x58 */
688                 {{3, 4},
689                 {6, 6},
690                 {0, 0},
691                 {0, 0}
692                 }
693         },
694         {1, 0, 3, 0,            /* 0x59 */
695                 {{0, 0},
696                 {3, 4},
697                 {6, 6},
698                 {0, 0}
699                 }
700         },
701         {0, 0, 3, 0,            /* 0x5a */
702                 {{1, 1},
703                 {3, 4},
704                 {6, 6},
705                 {0, 0}
706                 }
707         },
708         {1, 0, 3, 0,            /* 0x5b */
709                 {{0, 1},
710                 {3, 4},
711                 {6, 6},
712                 {0, 0}
713                 }
714         },
715         {0, 0, 2, 0,            /* 0x5c */
716                 {{2, 4},
717                 {6, 6},
718                 {0, 0},
719                 {0, 0}
720                 }
721         },
722         {1, 0, 3, 0,            /* 0x5d */
723                 {{0, 0},
724                 {2, 4},
725                 {6, 6},
726                 {0, 0}
727                 }
728         },
729         {0, 0, 2, 0,            /* 0x5e */
730                 {{1, 4},
731                 {6, 6},
732                 {0, 0},
733                 {0, 0}
734                 }
735         },
736         {1, 0, 2, 0,            /* 0x5f */
737                 {{0, 4},
738                 {6, 6},
739                 {0, 0},
740                 {0, 0}
741                 }
742         },
743         {0, 0, 1, 0,            /* 0x60 */
744                 {{5, 6},
745                 {0, 0},
746                 {0, 0},
747                 {0, 0}
748                 }
749         },
750         {1, 0, 2, 0,            /* 0x61 */
751                 {{0, 0},
752                 {5, 6},
753                 {0, 0},
754                 {0, 0}
755                 }
756         },
757         {0, 0, 2, 0,            /* 0x62 */
758                 {{1, 1},
759                 {5, 6},
760                 {0, 0},
761                 {0, 0}
762                 }
763         },
764         {1, 0, 2, 0,            /* 0x63 */
765                 {{0, 1},
766                 {5, 6},
767                 {0, 0},
768                 {0, 0}
769                 }
770         },
771         {0, 0, 2, 0,            /* 0x64 */
772                 {{2, 2},
773                 {5, 6},
774                 {0, 0},
775                 {0, 0}
776                 }
777         },
778         {1, 0, 3, 0,            /* 0x65 */
779                 {{0, 0},
780                 {2, 2},
781                 {5, 6},
782                 {0, 0}
783                 }
784         },
785         {0, 0, 2, 0,            /* 0x66 */
786                 {{1, 2},
787                 {5, 6},
788                 {0, 0},
789                 {0, 0}
790                 }
791         },
792         {1, 0, 2, 0,            /* 0x67 */
793                 {{0, 2},
794                 {5, 6},
795                 {0, 0},
796                 {0, 0}
797                 }
798         },
799         {0, 0, 2, 0,            /* 0x68 */
800                 {{3, 3},
801                 {5, 6},
802                 {0, 0},
803                 {0, 0}
804                 }
805         },
806         {1, 0, 3, 0,            /* 0x69 */
807                 {{0, 0},
808                 {3, 3},
809                 {5, 6},
810                 {0, 0}
811                 }
812         },
813         {0, 0, 3, 0,            /* 0x6a */
814                 {{1, 1},
815                 {3, 3},
816                 {5, 6},
817                 {0, 0}
818                 }
819         },
820         {1, 0, 3, 0,            /* 0x6b */
821                 {{0, 1},
822                 {3, 3},
823                 {5, 6},
824                 {0, 0}
825                 }
826         },
827         {0, 0, 2, 0,            /* 0x6c */
828                 {{2, 3},
829                 {5, 6},
830                 {0, 0},
831                 {0, 0}
832                 }
833         },
834         {1, 0, 3, 0,            /* 0x6d */
835                 {{0, 0},
836                 {2, 3},
837                 {5, 6},
838                 {0, 0}
839                 }
840         },
841         {0, 0, 2, 0,            /* 0x6e */
842                 {{1, 3},
843                 {5, 6},
844                 {0, 0},
845                 {0, 0}
846                 }
847         },
848         {1, 0, 2, 0,            /* 0x6f */
849                 {{0, 3},
850                 {5, 6},
851                 {0, 0},
852                 {0, 0}
853                 }
854         },
855         {0, 0, 1, 0,            /* 0x70 */
856                 {{4, 6},
857                 {0, 0},
858                 {0, 0},
859                 {0, 0}
860                 }
861         },
862         {1, 0, 2, 0,            /* 0x71 */
863                 {{0, 0},
864                 {4, 6},
865                 {0, 0},
866                 {0, 0}
867                 }
868         },
869         {0, 0, 2, 0,            /* 0x72 */
870                 {{1, 1},
871                 {4, 6},
872                 {0, 0},
873                 {0, 0}
874                 }
875         },
876         {1, 0, 2, 0,            /* 0x73 */
877                 {{0, 1},
878                 {4, 6},
879                 {0, 0},
880                 {0, 0}
881                 }
882         },
883         {0, 0, 2, 0,            /* 0x74 */
884                 {{2, 2},
885                 {4, 6},
886                 {0, 0},
887                 {0, 0}
888                 }
889         },
890         {1, 0, 3, 0,            /* 0x75 */
891                 {{0, 0},
892                 {2, 2},
893                 {4, 6},
894                 {0, 0}
895                 }
896         },
897         {0, 0, 2, 0,            /* 0x76 */
898                 {{1, 2},
899                 {4, 6},
900                 {0, 0},
901                 {0, 0}
902                 }
903         },
904         {1, 0, 2, 0,            /* 0x77 */
905                 {{0, 2},
906                 {4, 6},
907                 {0, 0},
908                 {0, 0}
909                 }
910         },
911         {0, 0, 1, 0,            /* 0x78 */
912                 {{3, 6},
913                 {0, 0},
914                 {0, 0},
915                 {0, 0}
916                 }
917         },
918         {1, 0, 2, 0,            /* 0x79 */
919                 {{0, 0},
920                 {3, 6},
921                 {0, 0},
922                 {0, 0}
923                 }
924         },
925         {0, 0, 2, 0,            /* 0x7a */
926                 {{1, 1},
927                 {3, 6},
928                 {0, 0},
929                 {0, 0}
930                 }
931         },
932         {1, 0, 2, 0,            /* 0x7b */
933                 {{0, 1},
934                 {3, 6},
935                 {0, 0},
936                 {0, 0}
937                 }
938         },
939         {0, 0, 1, 0,            /* 0x7c */
940                 {{2, 6},
941                 {0, 0},
942                 {0, 0},
943                 {0, 0}
944                 }
945         },
946         {1, 0, 2, 0,            /* 0x7d */
947                 {{0, 0},
948                 {2, 6},
949                 {0, 0},
950                 {0, 0}
951                 }
952         },
953         {0, 0, 1, 0,            /* 0x7e */
954                 {{1, 6},
955                 {0, 0},
956                 {0, 0},
957                 {0, 0}
958                 }
959         },
960         {1, 0, 1, 0,            /* 0x7f */
961                 {{0, 6},
962                 {0, 0},
963                 {0, 0},
964                 {0, 0}
965                 }
966         },
967         {0, 1, 1, 0,            /* 0x80 */
968                 {{7, 7},
969                 {0, 0},
970                 {0, 0},
971                 {0, 0}
972                 }
973         },
974         {1, 1, 2, 0,            /* 0x81 */
975                 {{0, 0},
976                 {7, 7},
977                 {0, 0},
978                 {0, 0}
979                 }
980         },
981         {0, 1, 2, 0,            /* 0x82 */
982                 {{1, 1},
983                 {7, 7},
984                 {0, 0},
985                 {0, 0}
986                 }
987         },
988         {1, 1, 2, 0,            /* 0x83 */
989                 {{0, 1},
990                 {7, 7},
991                 {0, 0},
992                 {0, 0}
993                 }
994         },
995         {0, 1, 2, 0,            /* 0x84 */
996                 {{2, 2},
997                 {7, 7},
998                 {0, 0},
999                 {0, 0}
1000                 }
1001         },
1002         {1, 1, 3, 0,            /* 0x85 */
1003                 {{0, 0},
1004                 {2, 2},
1005                 {7, 7},
1006                 {0, 0}
1007                 }
1008         },
1009         {0, 1, 2, 0,            /* 0x86 */
1010                 {{1, 2},
1011                 {7, 7},
1012                 {0, 0},
1013                 {0, 0}
1014                 }
1015         },
1016         {1, 1, 2, 0,            /* 0x87 */
1017                 {{0, 2},
1018                 {7, 7},
1019                 {0, 0},
1020                 {0, 0}
1021                 }
1022         },
1023         {0, 1, 2, 0,            /* 0x88 */
1024                 {{3, 3},
1025                 {7, 7},
1026                 {0, 0},
1027                 {0, 0}
1028                 }
1029         },
1030         {1, 1, 3, 0,            /* 0x89 */
1031                 {{0, 0},
1032                 {3, 3},
1033                 {7, 7},
1034                 {0, 0}
1035                 }
1036         },
1037         {0, 1, 3, 0,            /* 0x8a */
1038                 {{1, 1},
1039                 {3, 3},
1040                 {7, 7},
1041                 {0, 0}
1042                 }
1043         },
1044         {1, 1, 3, 0,            /* 0x8b */
1045                 {{0, 1},
1046                 {3, 3},
1047                 {7, 7},
1048                 {0, 0}
1049                 }
1050         },
1051         {0, 1, 2, 0,            /* 0x8c */
1052                 {{2, 3},
1053                 {7, 7},
1054                 {0, 0},
1055                 {0, 0}
1056                 }
1057         },
1058         {1, 1, 3, 0,            /* 0x8d */
1059                 {{0, 0},
1060                 {2, 3},
1061                 {7, 7},
1062                 {0, 0}
1063                 }
1064         },
1065         {0, 1, 2, 0,            /* 0x8e */
1066                 {{1, 3},
1067                 {7, 7},
1068                 {0, 0},
1069                 {0, 0}
1070                 }
1071         },
1072         {1, 1, 2, 0,            /* 0x8f */
1073                 {{0, 3},
1074                 {7, 7},
1075                 {0, 0},
1076                 {0, 0}
1077                 }
1078         },
1079         {0, 1, 2, 0,            /* 0x90 */
1080                 {{4, 4},
1081                 {7, 7},
1082                 {0, 0},
1083                 {0, 0}
1084                 }
1085         },
1086         {1, 1, 3, 0,            /* 0x91 */
1087                 {{0, 0},
1088                 {4, 4},
1089                 {7, 7},
1090                 {0, 0}
1091                 }
1092         },
1093         {0, 1, 3, 0,            /* 0x92 */
1094                 {{1, 1},
1095                 {4, 4},
1096                 {7, 7},
1097                 {0, 0}
1098                 }
1099         },
1100         {1, 1, 3, 0,            /* 0x93 */
1101                 {{0, 1},
1102                 {4, 4},
1103                 {7, 7},
1104                 {0, 0}
1105                 }
1106         },
1107         {0, 1, 3, 0,            /* 0x94 */
1108                 {{2, 2},
1109                 {4, 4},
1110                 {7, 7},
1111                 {0, 0}
1112                 }
1113         },
1114         {1, 1, 4, 0,            /* 0x95 */
1115                 {{0, 0},
1116                 {2, 2},
1117                 {4, 4},
1118                 {7, 7}
1119                 }
1120         },
1121         {0, 1, 3, 0,            /* 0x96 */
1122                 {{1, 2},
1123                 {4, 4},
1124                 {7, 7},
1125                 {0, 0}
1126                 }
1127         },
1128         {1, 1, 3, 0,            /* 0x97 */
1129                 {{0, 2},
1130                 {4, 4},
1131                 {7, 7},
1132                 {0, 0}
1133                 }
1134         },
1135         {0, 1, 2, 0,            /* 0x98 */
1136                 {{3, 4},
1137                 {7, 7},
1138                 {0, 0},
1139                 {0, 0}
1140                 }
1141         },
1142         {1, 1, 3, 0,            /* 0x99 */
1143                 {{0, 0},
1144                 {3, 4},
1145                 {7, 7},
1146                 {0, 0}
1147                 }
1148         },
1149         {0, 1, 3, 0,            /* 0x9a */
1150                 {{1, 1},
1151                 {3, 4},
1152                 {7, 7},
1153                 {0, 0}
1154                 }
1155         },
1156         {1, 1, 3, 0,            /* 0x9b */
1157                 {{0, 1},
1158                 {3, 4},
1159                 {7, 7},
1160                 {0, 0}
1161                 }
1162         },
1163         {0, 1, 2, 0,            /* 0x9c */
1164                 {{2, 4},
1165                 {7, 7},
1166                 {0, 0},
1167                 {0, 0}
1168                 }
1169         },
1170         {1, 1, 3, 0,            /* 0x9d */
1171                 {{0, 0},
1172                 {2, 4},
1173                 {7, 7},
1174                 {0, 0}
1175                 }
1176         },
1177         {0, 1, 2, 0,            /* 0x9e */
1178                 {{1, 4},
1179                 {7, 7},
1180                 {0, 0},
1181                 {0, 0}
1182                 }
1183         },
1184         {1, 1, 2, 0,            /* 0x9f */
1185                 {{0, 4},
1186                 {7, 7},
1187                 {0, 0},
1188                 {0, 0}
1189                 }
1190         },
1191         {0, 1, 2, 0,            /* 0xa0 */
1192                 {{5, 5},
1193                 {7, 7},
1194                 {0, 0},
1195                 {0, 0}
1196                 }
1197         },
1198         {1, 1, 3, 0,            /* 0xa1 */
1199                 {{0, 0},
1200                 {5, 5},
1201                 {7, 7},
1202                 {0, 0}
1203                 }
1204         },
1205         {0, 1, 3, 0,            /* 0xa2 */
1206                 {{1, 1},
1207                 {5, 5},
1208                 {7, 7},
1209                 {0, 0}
1210                 }
1211         },
1212         {1, 1, 3, 0,            /* 0xa3 */
1213                 {{0, 1},
1214                 {5, 5},
1215                 {7, 7},
1216                 {0, 0}
1217                 }
1218         },
1219         {0, 1, 3, 0,            /* 0xa4 */
1220                 {{2, 2},
1221                 {5, 5},
1222                 {7, 7},
1223                 {0, 0}
1224                 }
1225         },
1226         {1, 1, 4, 0,            /* 0xa5 */
1227                 {{0, 0},
1228                 {2, 2},
1229                 {5, 5},
1230                 {7, 7}
1231                 }
1232         },
1233         {0, 1, 3, 0,            /* 0xa6 */
1234                 {{1, 2},
1235                 {5, 5},
1236                 {7, 7},
1237                 {0, 0}
1238                 }
1239         },
1240         {1, 1, 3, 0,            /* 0xa7 */
1241                 {{0, 2},
1242                 {5, 5},
1243                 {7, 7},
1244                 {0, 0}
1245                 }
1246         },
1247         {0, 1, 3, 0,            /* 0xa8 */
1248                 {{3, 3},
1249                 {5, 5},
1250                 {7, 7},
1251                 {0, 0}
1252                 }
1253         },
1254         {1, 1, 4, 0,            /* 0xa9 */
1255                 {{0, 0},
1256                 {3, 3},
1257                 {5, 5},
1258                 {7, 7}
1259                 }
1260         },
1261         {0, 1, 4, 0,            /* 0xaa */
1262                 {{1, 1},
1263                 {3, 3},
1264                 {5, 5},
1265                 {7, 7}
1266                 }
1267         },
1268         {1, 1, 4, 0,            /* 0xab */
1269                 {{0, 1},
1270                 {3, 3},
1271                 {5, 5},
1272                 {7, 7}
1273                 }
1274         },
1275         {0, 1, 3, 0,            /* 0xac */
1276                 {{2, 3},
1277                 {5, 5},
1278                 {7, 7},
1279                 {0, 0}
1280                 }
1281         },
1282         {1, 1, 4, 0,            /* 0xad */
1283                 {{0, 0},
1284                 {2, 3},
1285                 {5, 5},
1286                 {7, 7}
1287                 }
1288         },
1289         {0, 1, 3, 0,            /* 0xae */
1290                 {{1, 3},
1291                 {5, 5},
1292                 {7, 7},
1293                 {0, 0}
1294                 }
1295         },
1296         {1, 1, 3, 0,            /* 0xaf */
1297                 {{0, 3},
1298                 {5, 5},
1299                 {7, 7},
1300                 {0, 0}
1301                 }
1302         },
1303         {0, 1, 2, 0,            /* 0xb0 */
1304                 {{4, 5},
1305                 {7, 7},
1306                 {0, 0},
1307                 {0, 0}
1308                 }
1309         },
1310         {1, 1, 3, 0,            /* 0xb1 */
1311                 {{0, 0},
1312                 {4, 5},
1313                 {7, 7},
1314                 {0, 0}
1315                 }
1316         },
1317         {0, 1, 3, 0,            /* 0xb2 */
1318                 {{1, 1},
1319                 {4, 5},
1320                 {7, 7},
1321                 {0, 0}
1322                 }
1323         },
1324         {1, 1, 3, 0,            /* 0xb3 */
1325                 {{0, 1},
1326                 {4, 5},
1327                 {7, 7},
1328                 {0, 0}
1329                 }
1330         },
1331         {0, 1, 3, 0,            /* 0xb4 */
1332                 {{2, 2},
1333                 {4, 5},
1334                 {7, 7},
1335                 {0, 0}
1336                 }
1337         },
1338         {1, 1, 4, 0,            /* 0xb5 */
1339                 {{0, 0},
1340                 {2, 2},
1341                 {4, 5},
1342                 {7, 7}
1343                 }
1344         },
1345         {0, 1, 3, 0,            /* 0xb6 */
1346                 {{1, 2},
1347                 {4, 5},
1348                 {7, 7},
1349                 {0, 0}
1350                 }
1351         },
1352         {1, 1, 3, 0,            /* 0xb7 */
1353                 {{0, 2},
1354                 {4, 5},
1355                 {7, 7},
1356                 {0, 0}
1357                 }
1358         },
1359         {0, 1, 2, 0,            /* 0xb8 */
1360                 {{3, 5},
1361                 {7, 7},
1362                 {0, 0},
1363                 {0, 0}
1364                 }
1365         },
1366         {1, 1, 3, 0,            /* 0xb9 */
1367                 {{0, 0},
1368                 {3, 5},
1369                 {7, 7},
1370                 {0, 0}
1371                 }
1372         },
1373         {0, 1, 3, 0,            /* 0xba */
1374                 {{1, 1},
1375                 {3, 5},
1376                 {7, 7},
1377                 {0, 0}
1378                 }
1379         },
1380         {1, 1, 3, 0,            /* 0xbb */
1381                 {{0, 1},
1382                 {3, 5},
1383                 {7, 7},
1384                 {0, 0}
1385                 }
1386         },
1387         {0, 1, 2, 0,            /* 0xbc */
1388                 {{2, 5},
1389                 {7, 7},
1390                 {0, 0},
1391                 {0, 0}
1392                 }
1393         },
1394         {1, 1, 3, 0,            /* 0xbd */
1395                 {{0, 0},
1396                 {2, 5},
1397                 {7, 7},
1398                 {0, 0}
1399                 }
1400         },
1401         {0, 1, 2, 0,            /* 0xbe */
1402                 {{1, 5},
1403                 {7, 7},
1404                 {0, 0},
1405                 {0, 0}
1406                 }
1407         },
1408         {1, 1, 2, 0,            /* 0xbf */
1409                 {{0, 5},
1410                 {7, 7},
1411                 {0, 0},
1412                 {0, 0}
1413                 }
1414         },
1415         {0, 1, 1, 0,            /* 0xc0 */
1416                 {{6, 7},
1417                 {0, 0},
1418                 {0, 0},
1419                 {0, 0}
1420                 }
1421         },
1422         {1, 1, 2, 0,            /* 0xc1 */
1423                 {{0, 0},
1424                 {6, 7},
1425                 {0, 0},
1426                 {0, 0}
1427                 }
1428         },
1429         {0, 1, 2, 0,            /* 0xc2 */
1430                 {{1, 1},
1431                 {6, 7},
1432                 {0, 0},
1433                 {0, 0}
1434                 }
1435         },
1436         {1, 1, 2, 0,            /* 0xc3 */
1437                 {{0, 1},
1438                 {6, 7},
1439                 {0, 0},
1440                 {0, 0}
1441                 }
1442         },
1443         {0, 1, 2, 0,            /* 0xc4 */
1444                 {{2, 2},
1445                 {6, 7},
1446                 {0, 0},
1447                 {0, 0}
1448                 }
1449         },
1450         {1, 1, 3, 0,            /* 0xc5 */
1451                 {{0, 0},
1452                 {2, 2},
1453                 {6, 7},
1454                 {0, 0}
1455                 }
1456         },
1457         {0, 1, 2, 0,            /* 0xc6 */
1458                 {{1, 2},
1459                 {6, 7},
1460                 {0, 0},
1461                 {0, 0}
1462                 }
1463         },
1464         {1, 1, 2, 0,            /* 0xc7 */
1465                 {{0, 2},
1466                 {6, 7},
1467                 {0, 0},
1468                 {0, 0}
1469                 }
1470         },
1471         {0, 1, 2, 0,            /* 0xc8 */
1472                 {{3, 3},
1473                 {6, 7},
1474                 {0, 0},
1475                 {0, 0}
1476                 }
1477         },
1478         {1, 1, 3, 0,            /* 0xc9 */
1479                 {{0, 0},
1480                 {3, 3},
1481                 {6, 7},
1482                 {0, 0}
1483                 }
1484         },
1485         {0, 1, 3, 0,            /* 0xca */
1486                 {{1, 1},
1487                 {3, 3},
1488                 {6, 7},
1489                 {0, 0}
1490                 }
1491         },
1492         {1, 1, 3, 0,            /* 0xcb */
1493                 {{0, 1},
1494                 {3, 3},
1495                 {6, 7},
1496                 {0, 0}
1497                 }
1498         },
1499         {0, 1, 2, 0,            /* 0xcc */
1500                 {{2, 3},
1501                 {6, 7},
1502                 {0, 0},
1503                 {0, 0}
1504                 }
1505         },
1506         {1, 1, 3, 0,            /* 0xcd */
1507                 {{0, 0},
1508                 {2, 3},
1509                 {6, 7},
1510                 {0, 0}
1511                 }
1512         },
1513         {0, 1, 2, 0,            /* 0xce */
1514                 {{1, 3},
1515                 {6, 7},
1516                 {0, 0},
1517                 {0, 0}
1518                 }
1519         },
1520         {1, 1, 2, 0,            /* 0xcf */
1521                 {{0, 3},
1522                 {6, 7},
1523                 {0, 0},
1524                 {0, 0}
1525                 }
1526         },
1527         {0, 1, 2, 0,            /* 0xd0 */
1528                 {{4, 4},
1529                 {6, 7},
1530                 {0, 0},
1531                 {0, 0}
1532                 }
1533         },
1534         {1, 1, 3, 0,            /* 0xd1 */
1535                 {{0, 0},
1536                 {4, 4},
1537                 {6, 7},
1538                 {0, 0}
1539                 }
1540         },
1541         {0, 1, 3, 0,            /* 0xd2 */
1542                 {{1, 1},
1543                 {4, 4},
1544                 {6, 7},
1545                 {0, 0}
1546                 }
1547         },
1548         {1, 1, 3, 0,            /* 0xd3 */
1549                 {{0, 1},
1550                 {4, 4},
1551                 {6, 7},
1552                 {0, 0}
1553                 }
1554         },
1555         {0, 1, 3, 0,            /* 0xd4 */
1556                 {{2, 2},
1557                 {4, 4},
1558                 {6, 7},
1559                 {0, 0}
1560                 }
1561         },
1562         {1, 1, 4, 0,            /* 0xd5 */
1563                 {{0, 0},
1564                 {2, 2},
1565                 {4, 4},
1566                 {6, 7}
1567                 }
1568         },
1569         {0, 1, 3, 0,            /* 0xd6 */
1570                 {{1, 2},
1571                 {4, 4},
1572                 {6, 7},
1573                 {0, 0}
1574                 }
1575         },
1576         {1, 1, 3, 0,            /* 0xd7 */
1577                 {{0, 2},
1578                 {4, 4},
1579                 {6, 7},
1580                 {0, 0}
1581                 }
1582         },
1583         {0, 1, 2, 0,            /* 0xd8 */
1584                 {{3, 4},
1585                 {6, 7},
1586                 {0, 0},
1587                 {0, 0}
1588                 }
1589         },
1590         {1, 1, 3, 0,            /* 0xd9 */
1591                 {{0, 0},
1592                 {3, 4},
1593                 {6, 7},
1594                 {0, 0}
1595                 }
1596         },
1597         {0, 1, 3, 0,            /* 0xda */
1598                 {{1, 1},
1599                 {3, 4},
1600                 {6, 7},
1601                 {0, 0}
1602                 }
1603         },
1604         {1, 1, 3, 0,            /* 0xdb */
1605                 {{0, 1},
1606                 {3, 4},
1607                 {6, 7},
1608                 {0, 0}
1609                 }
1610         },
1611         {0, 1, 2, 0,            /* 0xdc */
1612                 {{2, 4},
1613                 {6, 7},
1614                 {0, 0},
1615                 {0, 0}
1616                 }
1617         },
1618         {1, 1, 3, 0,            /* 0xdd */
1619                 {{0, 0},
1620                 {2, 4},
1621                 {6, 7},
1622                 {0, 0}
1623                 }
1624         },
1625         {0, 1, 2, 0,            /* 0xde */
1626                 {{1, 4},
1627                 {6, 7},
1628                 {0, 0},
1629                 {0, 0}
1630                 }
1631         },
1632         {1, 1, 2, 0,            /* 0xdf */
1633                 {{0, 4},
1634                 {6, 7},
1635                 {0, 0},
1636                 {0, 0}
1637                 }
1638         },
1639         {0, 1, 1, 0,            /* 0xe0 */
1640                 {{5, 7},
1641                 {0, 0},
1642                 {0, 0},
1643                 {0, 0}
1644                 }
1645         },
1646         {1, 1, 2, 0,            /* 0xe1 */
1647                 {{0, 0},
1648                 {5, 7},
1649                 {0, 0},
1650                 {0, 0}
1651                 }
1652         },
1653         {0, 1, 2, 0,            /* 0xe2 */
1654                 {{1, 1},
1655                 {5, 7},
1656                 {0, 0},
1657                 {0, 0}
1658                 }
1659         },
1660         {1, 1, 2, 0,            /* 0xe3 */
1661                 {{0, 1},
1662                 {5, 7},
1663                 {0, 0},
1664                 {0, 0}
1665                 }
1666         },
1667         {0, 1, 2, 0,            /* 0xe4 */
1668                 {{2, 2},
1669                 {5, 7},
1670                 {0, 0},
1671                 {0, 0}
1672                 }
1673         },
1674         {1, 1, 3, 0,            /* 0xe5 */
1675                 {{0, 0},
1676                 {2, 2},
1677                 {5, 7},
1678                 {0, 0}
1679                 }
1680         },
1681         {0, 1, 2, 0,            /* 0xe6 */
1682                 {{1, 2},
1683                 {5, 7},
1684                 {0, 0},
1685                 {0, 0}
1686                 }
1687         },
1688         {1, 1, 2, 0,            /* 0xe7 */
1689                 {{0, 2},
1690                 {5, 7},
1691                 {0, 0},
1692                 {0, 0}
1693                 }
1694         },
1695         {0, 1, 2, 0,            /* 0xe8 */
1696                 {{3, 3},
1697                 {5, 7},
1698                 {0, 0},
1699                 {0, 0}
1700                 }
1701         },
1702         {1, 1, 3, 0,            /* 0xe9 */
1703                 {{0, 0},
1704                 {3, 3},
1705                 {5, 7},
1706                 {0, 0}
1707                 }
1708         },
1709         {0, 1, 3, 0,            /* 0xea */
1710                 {{1, 1},
1711                 {3, 3},
1712                 {5, 7},
1713                 {0, 0}
1714                 }
1715         },
1716         {1, 1, 3, 0,            /* 0xeb */
1717                 {{0, 1},
1718                 {3, 3},
1719                 {5, 7},
1720                 {0, 0}
1721                 }
1722         },
1723         {0, 1, 2, 0,            /* 0xec */
1724                 {{2, 3},
1725                 {5, 7},
1726                 {0, 0},
1727                 {0, 0}
1728                 }
1729         },
1730         {1, 1, 3, 0,            /* 0xed */
1731                 {{0, 0},
1732                 {2, 3},
1733                 {5, 7},
1734                 {0, 0}
1735                 }
1736         },
1737         {0, 1, 2, 0,            /* 0xee */
1738                 {{1, 3},
1739                 {5, 7},
1740                 {0, 0},
1741                 {0, 0}
1742                 }
1743         },
1744         {1, 1, 2, 0,            /* 0xef */
1745                 {{0, 3},
1746                 {5, 7},
1747                 {0, 0},
1748                 {0, 0}
1749                 }
1750         },
1751         {0, 1, 1, 0,            /* 0xf0 */
1752                 {{4, 7},
1753                 {0, 0},
1754                 {0, 0},
1755                 {0, 0}
1756                 }
1757         },
1758         {1, 1, 2, 0,            /* 0xf1 */
1759                 {{0, 0},
1760                 {4, 7},
1761                 {0, 0},
1762                 {0, 0}
1763                 }
1764         },
1765         {0, 1, 2, 0,            /* 0xf2 */
1766                 {{1, 1},
1767                 {4, 7},
1768                 {0, 0},
1769                 {0, 0}
1770                 }
1771         },
1772         {1, 1, 2, 0,            /* 0xf3 */
1773                 {{0, 1},
1774                 {4, 7},
1775                 {0, 0},
1776                 {0, 0}
1777                 }
1778         },
1779         {0, 1, 2, 0,            /* 0xf4 */
1780                 {{2, 2},
1781                 {4, 7},
1782                 {0, 0},
1783                 {0, 0}
1784                 }
1785         },
1786         {1, 1, 3, 0,            /* 0xf5 */
1787                 {{0, 0},
1788                 {2, 2},
1789                 {4, 7},
1790                 {0, 0}
1791                 }
1792         },
1793         {0, 1, 2, 0,            /* 0xf6 */
1794                 {{1, 2},
1795                 {4, 7},
1796                 {0, 0},
1797                 {0, 0}
1798                 }
1799         },
1800         {1, 1, 2, 0,            /* 0xf7 */
1801                 {{0, 2},
1802                 {4, 7},
1803                 {0, 0},
1804                 {0, 0}
1805                 }
1806         },
1807         {0, 1, 1, 0,            /* 0xf8 */
1808                 {{3, 7},
1809                 {0, 0},
1810                 {0, 0},
1811                 {0, 0}
1812                 }
1813         },
1814         {1, 1, 2, 0,            /* 0xf9 */
1815                 {{0, 0},
1816                 {3, 7},
1817                 {0, 0},
1818                 {0, 0}
1819                 }
1820         },
1821         {0, 1, 2, 0,            /* 0xfa */
1822                 {{1, 1},
1823                 {3, 7},
1824                 {0, 0},
1825                 {0, 0}
1826                 }
1827         },
1828         {1, 1, 2, 0,            /* 0xfb */
1829                 {{0, 1},
1830                 {3, 7},
1831                 {0, 0},
1832                 {0, 0}
1833                 }
1834         },
1835         {0, 1, 1, 0,            /* 0xfc */
1836                 {{2, 7},
1837                 {0, 0},
1838                 {0, 0},
1839                 {0, 0}
1840                 }
1841         },
1842         {1, 1, 2, 0,            /* 0xfd */
1843                 {{0, 0},
1844                 {2, 7},
1845                 {0, 0},
1846                 {0, 0}
1847                 }
1848         },
1849         {0, 1, 1, 0,            /* 0xfe */
1850                 {{1, 7},
1851                 {0, 0},
1852                 {0, 0},
1853                 {0, 0}
1854                 }
1855         },
1856         {1, 1, 1, 0,            /* 0xff */
1857                 {{0, 7},
1858                 {0, 0},
1859                 {0, 0},
1860                 {0, 0}
1861                 }
1862         }
1863 };
1864
1865
1866 int
1867 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1868     int ipv4_addr_legal,
1869     int ipv6_addr_legal,
1870     int loopback_scope,
1871     int ipv4_local_scope,
1872     int local_scope SCTP_UNUSED,/* XXX */
1873     int site_scope,
1874     int do_update)
1875 {
1876         if ((loopback_scope == 0) &&
1877             (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1878                 /*
1879                  * skip loopback if not in scope *
1880                  */
1881                 return (0);
1882         }
1883         switch (ifa->address.sa.sa_family) {
1884 #ifdef INET
1885         case AF_INET:
1886                 if (ipv4_addr_legal) {
1887                         struct sockaddr_in *sin;
1888
1889                         sin = (struct sockaddr_in *)&ifa->address.sin;
1890                         if (sin->sin_addr.s_addr == 0) {
1891                                 /* not in scope , unspecified */
1892                                 return (0);
1893                         }
1894                         if ((ipv4_local_scope == 0) &&
1895                             (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1896                                 /* private address not in scope */
1897                                 return (0);
1898                         }
1899                 } else {
1900                         return (0);
1901                 }
1902                 break;
1903 #endif
1904 #ifdef INET6
1905         case AF_INET6:
1906                 if (ipv6_addr_legal) {
1907                         struct sockaddr_in6 *sin6;
1908
1909                         /*
1910                          * Must update the flags,  bummer, which means any
1911                          * IFA locks must now be applied HERE <->
1912                          */
1913                         if (do_update) {
1914                                 sctp_gather_internal_ifa_flags(ifa);
1915                         }
1916                         if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1917                                 return (0);
1918                         }
1919                         /* ok to use deprecated addresses? */
1920                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1921                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1922                                 /* skip unspecifed addresses */
1923                                 return (0);
1924                         }
1925                         if (    /* (local_scope == 0) && */
1926                             (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1927                                 return (0);
1928                         }
1929                         if ((site_scope == 0) &&
1930                             (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1931                                 return (0);
1932                         }
1933                 } else {
1934                         return (0);
1935                 }
1936                 break;
1937 #endif
1938         default:
1939                 return (0);
1940         }
1941         return (1);
1942 }
1943
1944 static struct mbuf *
1945 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1946 {
1947         struct sctp_paramhdr *parmh;
1948         struct mbuf *mret;
1949         int len;
1950
1951         switch (ifa->address.sa.sa_family) {
1952 #ifdef INET
1953         case AF_INET:
1954                 len = sizeof(struct sctp_ipv4addr_param);
1955                 break;
1956 #endif
1957 #ifdef INET6
1958         case AF_INET6:
1959                 len = sizeof(struct sctp_ipv6addr_param);
1960                 break;
1961 #endif
1962         default:
1963                 return (m);
1964         }
1965         if (M_TRAILINGSPACE(m) >= len) {
1966                 /* easy side we just drop it on the end */
1967                 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1968                 mret = m;
1969         } else {
1970                 /* Need more space */
1971                 mret = m;
1972                 while (SCTP_BUF_NEXT(mret) != NULL) {
1973                         mret = SCTP_BUF_NEXT(mret);
1974                 }
1975                 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1976                 if (SCTP_BUF_NEXT(mret) == NULL) {
1977                         /* We are hosed, can't add more addresses */
1978                         return (m);
1979                 }
1980                 mret = SCTP_BUF_NEXT(mret);
1981                 parmh = mtod(mret, struct sctp_paramhdr *);
1982         }
1983         /* now add the parameter */
1984         switch (ifa->address.sa.sa_family) {
1985 #ifdef INET
1986         case AF_INET:
1987                 {
1988                         struct sctp_ipv4addr_param *ipv4p;
1989                         struct sockaddr_in *sin;
1990
1991                         sin = (struct sockaddr_in *)&ifa->address.sin;
1992                         ipv4p = (struct sctp_ipv4addr_param *)parmh;
1993                         parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1994                         parmh->param_length = htons(len);
1995                         ipv4p->addr = sin->sin_addr.s_addr;
1996                         SCTP_BUF_LEN(mret) += len;
1997                         break;
1998                 }
1999 #endif
2000 #ifdef INET6
2001         case AF_INET6:
2002                 {
2003                         struct sctp_ipv6addr_param *ipv6p;
2004                         struct sockaddr_in6 *sin6;
2005
2006                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
2007                         ipv6p = (struct sctp_ipv6addr_param *)parmh;
2008                         parmh->param_type = htons(SCTP_IPV6_ADDRESS);
2009                         parmh->param_length = htons(len);
2010                         memcpy(ipv6p->addr, &sin6->sin6_addr,
2011                             sizeof(ipv6p->addr));
2012                         /* clear embedded scope in the address */
2013                         in6_clearscope((struct in6_addr *)ipv6p->addr);
2014                         SCTP_BUF_LEN(mret) += len;
2015                         break;
2016                 }
2017 #endif
2018         default:
2019                 return (m);
2020         }
2021         return (mret);
2022 }
2023
2024
2025 struct mbuf *
2026 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2027     struct sctp_scoping *scope,
2028     struct mbuf *m_at, int cnt_inits_to)
2029 {
2030         struct sctp_vrf *vrf = NULL;
2031         int cnt, limit_out = 0, total_count;
2032         uint32_t vrf_id;
2033
2034         vrf_id = inp->def_vrf_id;
2035         SCTP_IPI_ADDR_RLOCK();
2036         vrf = sctp_find_vrf(vrf_id);
2037         if (vrf == NULL) {
2038                 SCTP_IPI_ADDR_RUNLOCK();
2039                 return (m_at);
2040         }
2041         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2042                 struct sctp_ifa *sctp_ifap;
2043                 struct sctp_ifn *sctp_ifnp;
2044
2045                 cnt = cnt_inits_to;
2046                 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2047                         limit_out = 1;
2048                         cnt = SCTP_ADDRESS_LIMIT;
2049                         goto skip_count;
2050                 }
2051                 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2052                         if ((scope->loopback_scope == 0) &&
2053                             SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2054                                 /*
2055                                  * Skip loopback devices if loopback_scope
2056                                  * not set
2057                                  */
2058                                 continue;
2059                         }
2060                         LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2061                                 if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2062                                         continue;
2063                                 }
2064                                 if (sctp_is_address_in_scope(sctp_ifap,
2065                                     scope->ipv4_addr_legal,
2066                                     scope->ipv6_addr_legal,
2067                                     scope->loopback_scope,
2068                                     scope->ipv4_local_scope,
2069                                     scope->local_scope,
2070                                     scope->site_scope, 1) == 0) {
2071                                         continue;
2072                                 }
2073                                 cnt++;
2074                                 if (cnt > SCTP_ADDRESS_LIMIT) {
2075                                         break;
2076                                 }
2077                         }
2078                         if (cnt > SCTP_ADDRESS_LIMIT) {
2079                                 break;
2080                         }
2081                 }
2082 skip_count:
2083                 if (cnt > 1) {
2084                         total_count = 0;
2085                         LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2086                                 cnt = 0;
2087                                 if ((scope->loopback_scope == 0) &&
2088                                     SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2089                                         /*
2090                                          * Skip loopback devices if
2091                                          * loopback_scope not set
2092                                          */
2093                                         continue;
2094                                 }
2095                                 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2096                                         if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2097                                                 continue;
2098                                         }
2099                                         if (sctp_is_address_in_scope(sctp_ifap,
2100                                             scope->ipv4_addr_legal,
2101                                             scope->ipv6_addr_legal,
2102                                             scope->loopback_scope,
2103                                             scope->ipv4_local_scope,
2104                                             scope->local_scope,
2105                                             scope->site_scope, 0) == 0) {
2106                                                 continue;
2107                                         }
2108                                         m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2109                                         if (limit_out) {
2110                                                 cnt++;
2111                                                 total_count++;
2112                                                 if (cnt >= 2) {
2113                                                         /*
2114                                                          * two from each
2115                                                          * address
2116                                                          */
2117                                                         break;
2118                                                 }
2119                                                 if (total_count > SCTP_ADDRESS_LIMIT) {
2120                                                         /* No more addresses */
2121                                                         break;
2122                                                 }
2123                                         }
2124                                 }
2125                         }
2126                 }
2127         } else {
2128                 struct sctp_laddr *laddr;
2129
2130                 cnt = cnt_inits_to;
2131                 /* First, how many ? */
2132                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2133                         if (laddr->ifa == NULL) {
2134                                 continue;
2135                         }
2136                         if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2137                                 /*
2138                                  * Address being deleted by the system, dont
2139                                  * list.
2140                                  */
2141                                 continue;
2142                         if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2143                                 /*
2144                                  * Address being deleted on this ep don't
2145                                  * list.
2146                                  */
2147                                 continue;
2148                         }
2149                         if (sctp_is_address_in_scope(laddr->ifa,
2150                             scope->ipv4_addr_legal,
2151                             scope->ipv6_addr_legal,
2152                             scope->loopback_scope,
2153                             scope->ipv4_local_scope,
2154                             scope->local_scope,
2155                             scope->site_scope, 1) == 0) {
2156                                 continue;
2157                         }
2158                         cnt++;
2159                 }
2160                 /*
2161                  * To get through a NAT we only list addresses if we have
2162                  * more than one. That way if you just bind a single address
2163                  * we let the source of the init dictate our address.
2164                  */
2165                 if (cnt > 1) {
2166                         cnt = cnt_inits_to;
2167                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2168                                 if (laddr->ifa == NULL) {
2169                                         continue;
2170                                 }
2171                                 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2172                                         continue;
2173                                 }
2174                                 if (sctp_is_address_in_scope(laddr->ifa,
2175                                     scope->ipv4_addr_legal,
2176                                     scope->ipv6_addr_legal,
2177                                     scope->loopback_scope,
2178                                     scope->ipv4_local_scope,
2179                                     scope->local_scope,
2180                                     scope->site_scope, 0) == 0) {
2181                                         continue;
2182                                 }
2183                                 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2184                                 cnt++;
2185                                 if (cnt >= SCTP_ADDRESS_LIMIT) {
2186                                         break;
2187                                 }
2188                         }
2189                 }
2190         }
2191         SCTP_IPI_ADDR_RUNLOCK();
2192         return (m_at);
2193 }
2194
2195 static struct sctp_ifa *
2196 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2197     uint8_t dest_is_loop,
2198     uint8_t dest_is_priv,
2199     sa_family_t fam)
2200 {
2201         uint8_t dest_is_global = 0;
2202
2203         /* dest_is_priv is true if destination is a private address */
2204         /* dest_is_loop is true if destination is a loopback addresses */
2205
2206         /**
2207          * Here we determine if its a preferred address. A preferred address
2208          * means it is the same scope or higher scope then the destination.
2209          * L = loopback, P = private, G = global
2210          * -----------------------------------------
2211          *    src    |  dest | result
2212          *  ----------------------------------------
2213          *     L     |    L  |    yes
2214          *  -----------------------------------------
2215          *     P     |    L  |    yes-v4 no-v6
2216          *  -----------------------------------------
2217          *     G     |    L  |    yes-v4 no-v6
2218          *  -----------------------------------------
2219          *     L     |    P  |    no
2220          *  -----------------------------------------
2221          *     P     |    P  |    yes
2222          *  -----------------------------------------
2223          *     G     |    P  |    no
2224          *   -----------------------------------------
2225          *     L     |    G  |    no
2226          *   -----------------------------------------
2227          *     P     |    G  |    no
2228          *    -----------------------------------------
2229          *     G     |    G  |    yes
2230          *    -----------------------------------------
2231          */
2232
2233         if (ifa->address.sa.sa_family != fam) {
2234                 /* forget mis-matched family */
2235                 return (NULL);
2236         }
2237         if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2238                 dest_is_global = 1;
2239         }
2240         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2241         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2242         /* Ok the address may be ok */
2243 #ifdef INET6
2244         if (fam == AF_INET6) {
2245                 /* ok to use deprecated addresses? no lets not! */
2246                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2247                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2248                         return (NULL);
2249                 }
2250                 if (ifa->src_is_priv && !ifa->src_is_loop) {
2251                         if (dest_is_loop) {
2252                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2253                                 return (NULL);
2254                         }
2255                 }
2256                 if (ifa->src_is_glob) {
2257                         if (dest_is_loop) {
2258                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2259                                 return (NULL);
2260                         }
2261                 }
2262         }
2263 #endif
2264         /*
2265          * Now that we know what is what, implement or table this could in
2266          * theory be done slicker (it used to be), but this is
2267          * straightforward and easier to validate :-)
2268          */
2269         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2270             ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2271         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2272             dest_is_loop, dest_is_priv, dest_is_global);
2273
2274         if ((ifa->src_is_loop) && (dest_is_priv)) {
2275                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2276                 return (NULL);
2277         }
2278         if ((ifa->src_is_glob) && (dest_is_priv)) {
2279                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2280                 return (NULL);
2281         }
2282         if ((ifa->src_is_loop) && (dest_is_global)) {
2283                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2284                 return (NULL);
2285         }
2286         if ((ifa->src_is_priv) && (dest_is_global)) {
2287                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2288                 return (NULL);
2289         }
2290         SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2291         /* its a preferred address */
2292         return (ifa);
2293 }
2294
2295 static struct sctp_ifa *
2296 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2297     uint8_t dest_is_loop,
2298     uint8_t dest_is_priv,
2299     sa_family_t fam)
2300 {
2301         uint8_t dest_is_global = 0;
2302
2303         /**
2304          * Here we determine if its a acceptable address. A acceptable
2305          * address means it is the same scope or higher scope but we can
2306          * allow for NAT which means its ok to have a global dest and a
2307          * private src.
2308          *
2309          * L = loopback, P = private, G = global
2310          * -----------------------------------------
2311          *  src    |  dest | result
2312          * -----------------------------------------
2313          *   L     |   L   |    yes
2314          *  -----------------------------------------
2315          *   P     |   L   |    yes-v4 no-v6
2316          *  -----------------------------------------
2317          *   G     |   L   |    yes
2318          * -----------------------------------------
2319          *   L     |   P   |    no
2320          * -----------------------------------------
2321          *   P     |   P   |    yes
2322          * -----------------------------------------
2323          *   G     |   P   |    yes - May not work
2324          * -----------------------------------------
2325          *   L     |   G   |    no
2326          * -----------------------------------------
2327          *   P     |   G   |    yes - May not work
2328          * -----------------------------------------
2329          *   G     |   G   |    yes
2330          * -----------------------------------------
2331          */
2332
2333         if (ifa->address.sa.sa_family != fam) {
2334                 /* forget non matching family */
2335                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2336                     ifa->address.sa.sa_family, fam);
2337                 return (NULL);
2338         }
2339         /* Ok the address may be ok */
2340         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2341         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2342             dest_is_loop, dest_is_priv);
2343         if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2344                 dest_is_global = 1;
2345         }
2346 #ifdef INET6
2347         if (fam == AF_INET6) {
2348                 /* ok to use deprecated addresses? */
2349                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2350                         return (NULL);
2351                 }
2352                 if (ifa->src_is_priv) {
2353                         /* Special case, linklocal to loop */
2354                         if (dest_is_loop)
2355                                 return (NULL);
2356                 }
2357         }
2358 #endif
2359         /*
2360          * Now that we know what is what, implement our table. This could in
2361          * theory be done slicker (it used to be), but this is
2362          * straightforward and easier to validate :-)
2363          */
2364         SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2365             ifa->src_is_loop,
2366             dest_is_priv);
2367         if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2368                 return (NULL);
2369         }
2370         SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2371             ifa->src_is_loop,
2372             dest_is_global);
2373         if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2374                 return (NULL);
2375         }
2376         SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2377         /* its an acceptable address */
2378         return (ifa);
2379 }
2380
2381 int
2382 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2383 {
2384         struct sctp_laddr *laddr;
2385
2386         if (stcb == NULL) {
2387                 /* There are no restrictions, no TCB :-) */
2388                 return (0);
2389         }
2390         LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2391                 if (laddr->ifa == NULL) {
2392                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2393                             __FUNCTION__);
2394                         continue;
2395                 }
2396                 if (laddr->ifa == ifa) {
2397                         /* Yes it is on the list */
2398                         return (1);
2399                 }
2400         }
2401         return (0);
2402 }
2403
2404
2405 int
2406 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2407 {
2408         struct sctp_laddr *laddr;
2409
2410         if (ifa == NULL)
2411                 return (0);
2412         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2413                 if (laddr->ifa == NULL) {
2414                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2415                             __FUNCTION__);
2416                         continue;
2417                 }
2418                 if ((laddr->ifa == ifa) && laddr->action == 0)
2419                         /* same pointer */
2420                         return (1);
2421         }
2422         return (0);
2423 }
2424
2425
2426
2427 static struct sctp_ifa *
2428 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2429     sctp_route_t * ro,
2430     uint32_t vrf_id,
2431     int non_asoc_addr_ok,
2432     uint8_t dest_is_priv,
2433     uint8_t dest_is_loop,
2434     sa_family_t fam)
2435 {
2436         struct sctp_laddr *laddr, *starting_point;
2437         void *ifn;
2438         int resettotop = 0;
2439         struct sctp_ifn *sctp_ifn;
2440         struct sctp_ifa *sctp_ifa, *sifa;
2441         struct sctp_vrf *vrf;
2442         uint32_t ifn_index;
2443
2444         vrf = sctp_find_vrf(vrf_id);
2445         if (vrf == NULL)
2446                 return (NULL);
2447
2448         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2449         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2450         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2451         /*
2452          * first question, is the ifn we will emit on in our list, if so, we
2453          * want such an address. Note that we first looked for a preferred
2454          * address.
2455          */
2456         if (sctp_ifn) {
2457                 /* is a preferred one on the interface we route out? */
2458                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2459                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2460                             (non_asoc_addr_ok == 0))
2461                                 continue;
2462                         sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2463                             dest_is_loop,
2464                             dest_is_priv, fam);
2465                         if (sifa == NULL)
2466                                 continue;
2467                         if (sctp_is_addr_in_ep(inp, sifa)) {
2468                                 atomic_add_int(&sifa->refcount, 1);
2469                                 return (sifa);
2470                         }
2471                 }
2472         }
2473         /*
2474          * ok, now we now need to find one on the list of the addresses. We
2475          * can't get one on the emitting interface so let's find first a
2476          * preferred one. If not that an acceptable one otherwise... we
2477          * return NULL.
2478          */
2479         starting_point = inp->next_addr_touse;
2480 once_again:
2481         if (inp->next_addr_touse == NULL) {
2482                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2483                 resettotop = 1;
2484         }
2485         for (laddr = inp->next_addr_touse; laddr;
2486             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2487                 if (laddr->ifa == NULL) {
2488                         /* address has been removed */
2489                         continue;
2490                 }
2491                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2492                         /* address is being deleted */
2493                         continue;
2494                 }
2495                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2496                     dest_is_priv, fam);
2497                 if (sifa == NULL)
2498                         continue;
2499                 atomic_add_int(&sifa->refcount, 1);
2500                 return (sifa);
2501         }
2502         if (resettotop == 0) {
2503                 inp->next_addr_touse = NULL;
2504                 goto once_again;
2505         }
2506         inp->next_addr_touse = starting_point;
2507         resettotop = 0;
2508 once_again_too:
2509         if (inp->next_addr_touse == NULL) {
2510                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2511                 resettotop = 1;
2512         }
2513         /* ok, what about an acceptable address in the inp */
2514         for (laddr = inp->next_addr_touse; laddr;
2515             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2516                 if (laddr->ifa == NULL) {
2517                         /* address has been removed */
2518                         continue;
2519                 }
2520                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2521                         /* address is being deleted */
2522                         continue;
2523                 }
2524                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2525                     dest_is_priv, fam);
2526                 if (sifa == NULL)
2527                         continue;
2528                 atomic_add_int(&sifa->refcount, 1);
2529                 return (sifa);
2530         }
2531         if (resettotop == 0) {
2532                 inp->next_addr_touse = NULL;
2533                 goto once_again_too;
2534         }
2535         /*
2536          * no address bound can be a source for the destination we are in
2537          * trouble
2538          */
2539         return (NULL);
2540 }
2541
2542
2543
2544 static struct sctp_ifa *
2545 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2546     struct sctp_tcb *stcb,
2547     sctp_route_t * ro,
2548     uint32_t vrf_id,
2549     uint8_t dest_is_priv,
2550     uint8_t dest_is_loop,
2551     int non_asoc_addr_ok,
2552     sa_family_t fam)
2553 {
2554         struct sctp_laddr *laddr, *starting_point;
2555         void *ifn;
2556         struct sctp_ifn *sctp_ifn;
2557         struct sctp_ifa *sctp_ifa, *sifa;
2558         uint8_t start_at_beginning = 0;
2559         struct sctp_vrf *vrf;
2560         uint32_t ifn_index;
2561
2562         /*
2563          * first question, is the ifn we will emit on in our list, if so, we
2564          * want that one.
2565          */
2566         vrf = sctp_find_vrf(vrf_id);
2567         if (vrf == NULL)
2568                 return (NULL);
2569
2570         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2571         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2572         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2573
2574         /*
2575          * first question, is the ifn we will emit on in our list?  If so,
2576          * we want that one. First we look for a preferred. Second, we go
2577          * for an acceptable.
2578          */
2579         if (sctp_ifn) {
2580                 /* first try for a preferred address on the ep */
2581                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2582                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2583                                 continue;
2584                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2585                                 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2586                                 if (sifa == NULL)
2587                                         continue;
2588                                 if (((non_asoc_addr_ok == 0) &&
2589                                     (sctp_is_addr_restricted(stcb, sifa))) ||
2590                                     (non_asoc_addr_ok &&
2591                                     (sctp_is_addr_restricted(stcb, sifa)) &&
2592                                     (!sctp_is_addr_pending(stcb, sifa)))) {
2593                                         /* on the no-no list */
2594                                         continue;
2595                                 }
2596                                 atomic_add_int(&sifa->refcount, 1);
2597                                 return (sifa);
2598                         }
2599                 }
2600                 /* next try for an acceptable address on the ep */
2601                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2602                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2603                                 continue;
2604                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2605                                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2606                                 if (sifa == NULL)
2607                                         continue;
2608                                 if (((non_asoc_addr_ok == 0) &&
2609                                     (sctp_is_addr_restricted(stcb, sifa))) ||
2610                                     (non_asoc_addr_ok &&
2611                                     (sctp_is_addr_restricted(stcb, sifa)) &&
2612                                     (!sctp_is_addr_pending(stcb, sifa)))) {
2613                                         /* on the no-no list */
2614                                         continue;
2615                                 }
2616                                 atomic_add_int(&sifa->refcount, 1);
2617                                 return (sifa);
2618                         }
2619                 }
2620
2621         }
2622         /*
2623          * if we can't find one like that then we must look at all addresses
2624          * bound to pick one at first preferable then secondly acceptable.
2625          */
2626         starting_point = stcb->asoc.last_used_address;
2627 sctp_from_the_top:
2628         if (stcb->asoc.last_used_address == NULL) {
2629                 start_at_beginning = 1;
2630                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2631         }
2632         /* search beginning with the last used address */
2633         for (laddr = stcb->asoc.last_used_address; laddr;
2634             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2635                 if (laddr->ifa == NULL) {
2636                         /* address has been removed */
2637                         continue;
2638                 }
2639                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2640                         /* address is being deleted */
2641                         continue;
2642                 }
2643                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2644                 if (sifa == NULL)
2645                         continue;
2646                 if (((non_asoc_addr_ok == 0) &&
2647                     (sctp_is_addr_restricted(stcb, sifa))) ||
2648                     (non_asoc_addr_ok &&
2649                     (sctp_is_addr_restricted(stcb, sifa)) &&
2650                     (!sctp_is_addr_pending(stcb, sifa)))) {
2651                         /* on the no-no list */
2652                         continue;
2653                 }
2654                 stcb->asoc.last_used_address = laddr;
2655                 atomic_add_int(&sifa->refcount, 1);
2656                 return (sifa);
2657         }
2658         if (start_at_beginning == 0) {
2659                 stcb->asoc.last_used_address = NULL;
2660                 goto sctp_from_the_top;
2661         }
2662         /* now try for any higher scope than the destination */
2663         stcb->asoc.last_used_address = starting_point;
2664         start_at_beginning = 0;
2665 sctp_from_the_top2:
2666         if (stcb->asoc.last_used_address == NULL) {
2667                 start_at_beginning = 1;
2668                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2669         }
2670         /* search beginning with the last used address */
2671         for (laddr = stcb->asoc.last_used_address; laddr;
2672             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2673                 if (laddr->ifa == NULL) {
2674                         /* address has been removed */
2675                         continue;
2676                 }
2677                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2678                         /* address is being deleted */
2679                         continue;
2680                 }
2681                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2682                     dest_is_priv, fam);
2683                 if (sifa == NULL)
2684                         continue;
2685                 if (((non_asoc_addr_ok == 0) &&
2686                     (sctp_is_addr_restricted(stcb, sifa))) ||
2687                     (non_asoc_addr_ok &&
2688                     (sctp_is_addr_restricted(stcb, sifa)) &&
2689                     (!sctp_is_addr_pending(stcb, sifa)))) {
2690                         /* on the no-no list */
2691                         continue;
2692                 }
2693                 stcb->asoc.last_used_address = laddr;
2694                 atomic_add_int(&sifa->refcount, 1);
2695                 return (sifa);
2696         }
2697         if (start_at_beginning == 0) {
2698                 stcb->asoc.last_used_address = NULL;
2699                 goto sctp_from_the_top2;
2700         }
2701         return (NULL);
2702 }
2703
2704 static struct sctp_ifa *
2705 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2706     struct sctp_tcb *stcb,
2707     int non_asoc_addr_ok,
2708     uint8_t dest_is_loop,
2709     uint8_t dest_is_priv,
2710     int addr_wanted,
2711     sa_family_t fam,
2712     sctp_route_t * ro
2713 )
2714 {
2715         struct sctp_ifa *ifa, *sifa;
2716         int num_eligible_addr = 0;
2717
2718 #ifdef INET6
2719         struct sockaddr_in6 sin6, lsa6;
2720
2721         if (fam == AF_INET6) {
2722                 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2723                 (void)sa6_recoverscope(&sin6);
2724         }
2725 #endif                          /* INET6 */
2726         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2727                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2728                     (non_asoc_addr_ok == 0))
2729                         continue;
2730                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2731                     dest_is_priv, fam);
2732                 if (sifa == NULL)
2733                         continue;
2734 #ifdef INET6
2735                 if (fam == AF_INET6 &&
2736                     dest_is_loop &&
2737                     sifa->src_is_loop && sifa->src_is_priv) {
2738                         /*
2739                          * don't allow fe80::1 to be a src on loop ::1, we
2740                          * don't list it to the peer so we will get an
2741                          * abort.
2742                          */
2743                         continue;
2744                 }
2745                 if (fam == AF_INET6 &&
2746                     IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2747                     IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2748                         /*
2749                          * link-local <-> link-local must belong to the same
2750                          * scope.
2751                          */
2752                         memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2753                         (void)sa6_recoverscope(&lsa6);
2754                         if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2755                                 continue;
2756                         }
2757                 }
2758 #endif                          /* INET6 */
2759
2760                 /*
2761                  * Check if the IPv6 address matches to next-hop. In the
2762                  * mobile case, old IPv6 address may be not deleted from the
2763                  * interface. Then, the interface has previous and new
2764                  * addresses.  We should use one corresponding to the
2765                  * next-hop.  (by micchie)
2766                  */
2767 #ifdef INET6
2768                 if (stcb && fam == AF_INET6 &&
2769                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2770                         if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2771                             == 0) {
2772                                 continue;
2773                         }
2774                 }
2775 #endif
2776 #ifdef INET
2777                 /* Avoid topologically incorrect IPv4 address */
2778                 if (stcb && fam == AF_INET &&
2779                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2780                         if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2781                                 continue;
2782                         }
2783                 }
2784 #endif
2785                 if (stcb) {
2786                         if (sctp_is_address_in_scope(ifa,
2787                             stcb->asoc.ipv4_addr_legal,
2788                             stcb->asoc.ipv6_addr_legal,
2789                             stcb->asoc.loopback_scope,
2790                             stcb->asoc.ipv4_local_scope,
2791                             stcb->asoc.local_scope,
2792                             stcb->asoc.site_scope, 0) == 0) {
2793                                 continue;
2794                         }
2795                         if (((non_asoc_addr_ok == 0) &&
2796                             (sctp_is_addr_restricted(stcb, sifa))) ||
2797                             (non_asoc_addr_ok &&
2798                             (sctp_is_addr_restricted(stcb, sifa)) &&
2799                             (!sctp_is_addr_pending(stcb, sifa)))) {
2800                                 /*
2801                                  * It is restricted for some reason..
2802                                  * probably not yet added.
2803                                  */
2804                                 continue;
2805                         }
2806                 }
2807                 if (num_eligible_addr >= addr_wanted) {
2808                         return (sifa);
2809                 }
2810                 num_eligible_addr++;
2811         }
2812         return (NULL);
2813 }
2814
2815
2816 static int
2817 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2818     struct sctp_tcb *stcb,
2819     int non_asoc_addr_ok,
2820     uint8_t dest_is_loop,
2821     uint8_t dest_is_priv,
2822     sa_family_t fam)
2823 {
2824         struct sctp_ifa *ifa, *sifa;
2825         int num_eligible_addr = 0;
2826
2827         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2828                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2829                     (non_asoc_addr_ok == 0)) {
2830                         continue;
2831                 }
2832                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2833                     dest_is_priv, fam);
2834                 if (sifa == NULL) {
2835                         continue;
2836                 }
2837                 if (stcb) {
2838                         if (sctp_is_address_in_scope(ifa,
2839                             stcb->asoc.ipv4_addr_legal,
2840                             stcb->asoc.ipv6_addr_legal,
2841                             stcb->asoc.loopback_scope,
2842                             stcb->asoc.ipv4_local_scope,
2843                             stcb->asoc.local_scope,
2844                             stcb->asoc.site_scope, 0) == 0) {
2845                                 continue;
2846                         }
2847                         if (((non_asoc_addr_ok == 0) &&
2848                             (sctp_is_addr_restricted(stcb, sifa))) ||
2849                             (non_asoc_addr_ok &&
2850                             (sctp_is_addr_restricted(stcb, sifa)) &&
2851                             (!sctp_is_addr_pending(stcb, sifa)))) {
2852                                 /*
2853                                  * It is restricted for some reason..
2854                                  * probably not yet added.
2855                                  */
2856                                 continue;
2857                         }
2858                 }
2859                 num_eligible_addr++;
2860         }
2861         return (num_eligible_addr);
2862 }
2863
2864 static struct sctp_ifa *
2865 sctp_choose_boundall(struct sctp_tcb *stcb,
2866     struct sctp_nets *net,
2867     sctp_route_t * ro,
2868     uint32_t vrf_id,
2869     uint8_t dest_is_priv,
2870     uint8_t dest_is_loop,
2871     int non_asoc_addr_ok,
2872     sa_family_t fam)
2873 {
2874         int cur_addr_num = 0, num_preferred = 0;
2875         void *ifn;
2876         struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2877         struct sctp_ifa *sctp_ifa, *sifa;
2878         uint32_t ifn_index;
2879         struct sctp_vrf *vrf;
2880
2881 #ifdef INET
2882         int retried = 0;
2883
2884 #endif
2885
2886         /*-
2887          * For boundall we can use any address in the association.
2888          * If non_asoc_addr_ok is set we can use any address (at least in
2889          * theory). So we look for preferred addresses first. If we find one,
2890          * we use it. Otherwise we next try to get an address on the
2891          * interface, which we should be able to do (unless non_asoc_addr_ok
2892          * is false and we are routed out that way). In these cases where we
2893          * can't use the address of the interface we go through all the
2894          * ifn's looking for an address we can use and fill that in. Punting
2895          * means we send back address 0, which will probably cause problems
2896          * actually since then IP will fill in the address of the route ifn,
2897          * which means we probably already rejected it.. i.e. here comes an
2898          * abort :-<.
2899          */
2900         vrf = sctp_find_vrf(vrf_id);
2901         if (vrf == NULL)
2902                 return (NULL);
2903
2904         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2905         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2906         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
2907         emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2908         if (sctp_ifn == NULL) {
2909                 /* ?? We don't have this guy ?? */
2910                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2911                 goto bound_all_plan_b;
2912         }
2913         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2914             ifn_index, sctp_ifn->ifn_name);
2915
2916         if (net) {
2917                 cur_addr_num = net->indx_of_eligible_next_to_use;
2918         }
2919         num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2920             stcb,
2921             non_asoc_addr_ok,
2922             dest_is_loop,
2923             dest_is_priv, fam);
2924         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2925             num_preferred, sctp_ifn->ifn_name);
2926         if (num_preferred == 0) {
2927                 /*
2928                  * no eligible addresses, we must use some other interface
2929                  * address if we can find one.
2930                  */
2931                 goto bound_all_plan_b;
2932         }
2933         /*
2934          * Ok we have num_eligible_addr set with how many we can use, this
2935          * may vary from call to call due to addresses being deprecated
2936          * etc..
2937          */
2938         if (cur_addr_num >= num_preferred) {
2939                 cur_addr_num = 0;
2940         }
2941         /*
2942          * select the nth address from the list (where cur_addr_num is the
2943          * nth) and 0 is the first one, 1 is the second one etc...
2944          */
2945         SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2946
2947         sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2948             dest_is_priv, cur_addr_num, fam, ro);
2949
2950         /* if sctp_ifa is NULL something changed??, fall to plan b. */
2951         if (sctp_ifa) {
2952                 atomic_add_int(&sctp_ifa->refcount, 1);
2953                 if (net) {
2954                         /* save off where the next one we will want */
2955                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2956                 }
2957                 return (sctp_ifa);
2958         }
2959         /*
2960          * plan_b: Look at all interfaces and find a preferred address. If
2961          * no preferred fall through to plan_c.
2962          */
2963 bound_all_plan_b:
2964         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2965         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2966                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2967                     sctp_ifn->ifn_name);
2968                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2969                         /* wrong base scope */
2970                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2971                         continue;
2972                 }
2973                 if ((sctp_ifn == looked_at) && looked_at) {
2974                         /* already looked at this guy */
2975                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2976                         continue;
2977                 }
2978                 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2979                     dest_is_loop, dest_is_priv, fam);
2980                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2981                     "Found ifn:%p %d preferred source addresses\n",
2982                     ifn, num_preferred);
2983                 if (num_preferred == 0) {
2984                         /* None on this interface. */
2985                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2986                         continue;
2987                 }
2988                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2989                     "num preferred:%d on interface:%p cur_addr_num:%d\n",
2990                     num_preferred, sctp_ifn, cur_addr_num);
2991
2992                 /*
2993                  * Ok we have num_eligible_addr set with how many we can
2994                  * use, this may vary from call to call due to addresses
2995                  * being deprecated etc..
2996                  */
2997                 if (cur_addr_num >= num_preferred) {
2998                         cur_addr_num = 0;
2999                 }
3000                 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
3001                     dest_is_priv, cur_addr_num, fam, ro);
3002                 if (sifa == NULL)
3003                         continue;
3004                 if (net) {
3005                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3006                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3007                             cur_addr_num);
3008                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3009                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3010                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3011                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3012                 }
3013                 atomic_add_int(&sifa->refcount, 1);
3014                 return (sifa);
3015         }
3016 #ifdef INET
3017 again_with_private_addresses_allowed:
3018 #endif
3019         /* plan_c: do we have an acceptable address on the emit interface */
3020         sifa = NULL;
3021         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
3022         if (emit_ifn == NULL) {
3023                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
3024                 goto plan_d;
3025         }
3026         LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3027                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", sctp_ifa);
3028                 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3029                     (non_asoc_addr_ok == 0)) {
3030                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
3031                         continue;
3032                 }
3033                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3034                     dest_is_priv, fam);
3035                 if (sifa == NULL) {
3036                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3037                         continue;
3038                 }
3039                 if (stcb) {
3040                         if (sctp_is_address_in_scope(sifa,
3041                             stcb->asoc.ipv4_addr_legal,
3042                             stcb->asoc.ipv6_addr_legal,
3043                             stcb->asoc.loopback_scope,
3044                             stcb->asoc.ipv4_local_scope,
3045                             stcb->asoc.local_scope,
3046                             stcb->asoc.site_scope, 0) == 0) {
3047                                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3048                                 sifa = NULL;
3049                                 continue;
3050                         }
3051                         if (((non_asoc_addr_ok == 0) &&
3052                             (sctp_is_addr_restricted(stcb, sifa))) ||
3053                             (non_asoc_addr_ok &&
3054                             (sctp_is_addr_restricted(stcb, sifa)) &&
3055                             (!sctp_is_addr_pending(stcb, sifa)))) {
3056                                 /*
3057                                  * It is restricted for some reason..
3058                                  * probably not yet added.
3059                                  */
3060                                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its resticted\n");
3061                                 sifa = NULL;
3062                                 continue;
3063                         }
3064                 } else {
3065                         SCTP_PRINTF("Stcb is null - no print\n");
3066                 }
3067                 atomic_add_int(&sifa->refcount, 1);
3068                 goto out;
3069         }
3070 plan_d:
3071         /*
3072          * plan_d: We are in trouble. No preferred address on the emit
3073          * interface. And not even a preferred address on all interfaces. Go
3074          * out and see if we can find an acceptable address somewhere
3075          * amongst all interfaces.
3076          */
3077         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", looked_at);
3078         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3079                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3080                         /* wrong base scope */
3081                         continue;
3082                 }
3083                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3084                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3085                             (non_asoc_addr_ok == 0))
3086                                 continue;
3087                         sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3088                             dest_is_loop,
3089                             dest_is_priv, fam);
3090                         if (sifa == NULL)
3091                                 continue;
3092                         if (stcb) {
3093                                 if (sctp_is_address_in_scope(sifa,
3094                                     stcb->asoc.ipv4_addr_legal,
3095                                     stcb->asoc.ipv6_addr_legal,
3096                                     stcb->asoc.loopback_scope,
3097                                     stcb->asoc.ipv4_local_scope,
3098                                     stcb->asoc.local_scope,
3099                                     stcb->asoc.site_scope, 0) == 0) {
3100                                         sifa = NULL;
3101                                         continue;
3102                                 }
3103                                 if (((non_asoc_addr_ok == 0) &&
3104                                     (sctp_is_addr_restricted(stcb, sifa))) ||
3105                                     (non_asoc_addr_ok &&
3106                                     (sctp_is_addr_restricted(stcb, sifa)) &&
3107                                     (!sctp_is_addr_pending(stcb, sifa)))) {
3108                                         /*
3109                                          * It is restricted for some
3110                                          * reason.. probably not yet added.
3111                                          */
3112                                         sifa = NULL;
3113                                         continue;
3114                                 }
3115                         }
3116                         goto out;
3117                 }
3118         }
3119 #ifdef INET
3120         if ((retried == 0) && (stcb->asoc.ipv4_local_scope == 0)) {
3121                 stcb->asoc.ipv4_local_scope = 1;
3122                 retried = 1;
3123                 goto again_with_private_addresses_allowed;
3124         } else if (retried == 1) {
3125                 stcb->asoc.ipv4_local_scope = 0;
3126         }
3127 #endif
3128 out:
3129 #ifdef INET
3130         if (sifa) {
3131                 if (retried == 1) {
3132                         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3133                                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3134                                         /* wrong base scope */
3135                                         continue;
3136                                 }
3137                                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3138                                         struct sctp_ifa *tmp_sifa;
3139
3140                                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3141                                             (non_asoc_addr_ok == 0))
3142                                                 continue;
3143                                         tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3144                                             dest_is_loop,
3145                                             dest_is_priv, fam);
3146                                         if (tmp_sifa == NULL) {
3147                                                 continue;
3148                                         }
3149                                         if (tmp_sifa == sifa) {
3150                                                 continue;
3151                                         }
3152                                         if (stcb) {
3153                                                 if (sctp_is_address_in_scope(tmp_sifa,
3154                                                     stcb->asoc.ipv4_addr_legal,
3155                                                     stcb->asoc.ipv6_addr_legal,
3156                                                     stcb->asoc.loopback_scope,
3157                                                     stcb->asoc.ipv4_local_scope,
3158                                                     stcb->asoc.local_scope,
3159                                                     stcb->asoc.site_scope, 0) == 0) {
3160                                                         continue;
3161                                                 }
3162                                                 if (((non_asoc_addr_ok == 0) &&
3163                                                     (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3164                                                     (non_asoc_addr_ok &&
3165                                                     (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3166                                                     (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3167                                                         /*
3168                                                          * It is restricted
3169                                                          * for some reason..
3170                                                          * probably not yet
3171                                                          * added.
3172                                                          */
3173                                                         continue;
3174                                                 }
3175                                         }
3176                                         if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3177                                             (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3178                                                 sctp_add_local_addr_restricted(stcb, tmp_sifa);
3179                                         }
3180                                 }
3181                         }
3182                 }
3183                 atomic_add_int(&sifa->refcount, 1);
3184         }
3185 #endif
3186         return (sifa);
3187 }
3188
3189
3190
3191 /* tcb may be NULL */
3192 struct sctp_ifa *
3193 sctp_source_address_selection(struct sctp_inpcb *inp,
3194     struct sctp_tcb *stcb,
3195     sctp_route_t * ro,
3196     struct sctp_nets *net,
3197     int non_asoc_addr_ok, uint32_t vrf_id)
3198 {
3199         struct sctp_ifa *answer;
3200         uint8_t dest_is_priv, dest_is_loop;
3201         sa_family_t fam;
3202
3203 #ifdef INET
3204         struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3205
3206 #endif
3207 #ifdef INET6
3208         struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3209
3210 #endif
3211
3212         /**
3213          * Rules: - Find the route if needed, cache if I can. - Look at
3214          * interface address in route, Is it in the bound list. If so we
3215          * have the best source. - If not we must rotate amongst the
3216          * addresses.
3217          *
3218          * Cavets and issues
3219          *
3220          * Do we need to pay attention to scope. We can have a private address
3221          * or a global address we are sourcing or sending to. So if we draw
3222          * it out
3223          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3224          * For V4
3225          * ------------------------------------------
3226          *      source     *      dest  *  result
3227          * -----------------------------------------
3228          * <a>  Private    *    Global  *  NAT
3229          * -----------------------------------------
3230          * <b>  Private    *    Private *  No problem
3231          * -----------------------------------------
3232          * <c>  Global     *    Private *  Huh, How will this work?
3233          * -----------------------------------------
3234          * <d>  Global     *    Global  *  No Problem
3235          *------------------------------------------
3236          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3237          * For V6
3238          *------------------------------------------
3239          *      source     *      dest  *  result
3240          * -----------------------------------------
3241          * <a>  Linklocal  *    Global  *
3242          * -----------------------------------------
3243          * <b>  Linklocal  * Linklocal  *  No problem
3244          * -----------------------------------------
3245          * <c>  Global     * Linklocal  *  Huh, How will this work?
3246          * -----------------------------------------
3247          * <d>  Global     *    Global  *  No Problem
3248          *------------------------------------------
3249          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3250          *
3251          * And then we add to that what happens if there are multiple addresses
3252          * assigned to an interface. Remember the ifa on a ifn is a linked
3253          * list of addresses. So one interface can have more than one IP
3254          * address. What happens if we have both a private and a global
3255          * address? Do we then use context of destination to sort out which
3256          * one is best? And what about NAT's sending P->G may get you a NAT
3257          * translation, or should you select the G thats on the interface in
3258          * preference.
3259          *
3260          * Decisions:
3261          *
3262          * - count the number of addresses on the interface.
3263          * - if it is one, no problem except case <c>.
3264          *   For <a> we will assume a NAT out there.
3265          * - if there are more than one, then we need to worry about scope P
3266          *   or G. We should prefer G -> G and P -> P if possible.
3267          *   Then as a secondary fall back to mixed types G->P being a last
3268          *   ditch one.
3269          * - The above all works for bound all, but bound specific we need to
3270          *   use the same concept but instead only consider the bound
3271          *   addresses. If the bound set is NOT assigned to the interface then
3272          *   we must use rotation amongst the bound addresses..
3273          */
3274         if (ro->ro_rt == NULL) {
3275                 /*
3276                  * Need a route to cache.
3277                  */
3278                 SCTP_RTALLOC(ro, vrf_id);
3279         }
3280         if (ro->ro_rt == NULL) {
3281                 return (NULL);
3282         }
3283         fam = ro->ro_dst.sa_family;
3284         dest_is_priv = dest_is_loop = 0;
3285         /* Setup our scopes for the destination */
3286         switch (fam) {
3287 #ifdef INET
3288         case AF_INET:
3289                 /* Scope based on outbound address */
3290                 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3291                         dest_is_loop = 1;
3292                         if (net != NULL) {
3293                                 /* mark it as local */
3294                                 net->addr_is_local = 1;
3295                         }
3296                 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3297                         dest_is_priv = 1;
3298                 }
3299                 break;
3300 #endif
3301 #ifdef INET6
3302         case AF_INET6:
3303                 /* Scope based on outbound address */
3304                 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3305                     SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3306                         /*
3307                          * If the address is a loopback address, which
3308                          * consists of "::1" OR "fe80::1%lo0", we are
3309                          * loopback scope. But we don't use dest_is_priv
3310                          * (link local addresses).
3311                          */
3312                         dest_is_loop = 1;
3313                         if (net != NULL) {
3314                                 /* mark it as local */
3315                                 net->addr_is_local = 1;
3316                         }
3317                 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3318                         dest_is_priv = 1;
3319                 }
3320                 break;
3321 #endif
3322         }
3323         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3324         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3325         SCTP_IPI_ADDR_RLOCK();
3326         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3327                 /*
3328                  * Bound all case
3329                  */
3330                 answer = sctp_choose_boundall(stcb, net, ro, vrf_id,
3331                     dest_is_priv, dest_is_loop,
3332                     non_asoc_addr_ok, fam);
3333                 SCTP_IPI_ADDR_RUNLOCK();
3334                 return (answer);
3335         }
3336         /*
3337          * Subset bound case
3338          */
3339         if (stcb) {
3340                 answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
3341                     vrf_id, dest_is_priv,
3342                     dest_is_loop,
3343                     non_asoc_addr_ok, fam);
3344         } else {
3345                 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3346                     non_asoc_addr_ok,
3347                     dest_is_priv,
3348                     dest_is_loop, fam);
3349         }
3350         SCTP_IPI_ADDR_RUNLOCK();
3351         return (answer);
3352 }
3353
3354 static int
3355 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
3356 {
3357         struct cmsghdr cmh;
3358         int tlen, at, found;
3359         struct sctp_sndinfo sndinfo;
3360         struct sctp_prinfo prinfo;
3361         struct sctp_authinfo authinfo;
3362
3363         tlen = SCTP_BUF_LEN(control);
3364         at = 0;
3365         found = 0;
3366         /*
3367          * Independent of how many mbufs, find the c_type inside the control
3368          * structure and copy out the data.
3369          */
3370         while (at < tlen) {
3371                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3372                         /* There is not enough room for one more. */
3373                         return (found);
3374                 }
3375                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3376                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3377                         /* We dont't have a complete CMSG header. */
3378                         return (found);
3379                 }
3380                 if (((int)cmh.cmsg_len + at) > tlen) {
3381                         /* We don't have the complete CMSG. */
3382                         return (found);
3383                 }
3384                 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3385                     ((c_type == cmh.cmsg_type) ||
3386                     ((c_type == SCTP_SNDRCV) &&
3387                     ((cmh.cmsg_type == SCTP_SNDINFO) ||
3388                     (cmh.cmsg_type == SCTP_PRINFO) ||
3389                     (cmh.cmsg_type == SCTP_AUTHINFO))))) {
3390                         if (c_type == cmh.cmsg_type) {
3391                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3392                                         return (found);
3393                                 }
3394                                 /* It is exactly what we want. Copy it out. */
3395                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), cpsize, (caddr_t)data);
3396                                 return (1);
3397                         } else {
3398                                 struct sctp_sndrcvinfo *sndrcvinfo;
3399
3400                                 sndrcvinfo = (struct sctp_sndrcvinfo *)data;
3401                                 if (found == 0) {
3402                                         if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
3403                                                 return (found);
3404                                         }
3405                                         memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
3406                                 }
3407                                 switch (cmh.cmsg_type) {
3408                                 case SCTP_SNDINFO:
3409                                         if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_sndinfo)) {
3410                                                 return (found);
3411                                         }
3412                                         m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
3413                                         sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
3414                                         sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
3415                                         sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
3416                                         sndrcvinfo->sinfo_context = sndinfo.snd_context;
3417                                         sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
3418                                         break;
3419                                 case SCTP_PRINFO:
3420                                         if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_prinfo)) {
3421                                                 return (found);
3422                                         }
3423                                         m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
3424                                         sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
3425                                         sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
3426                                         break;
3427                                 case SCTP_AUTHINFO:
3428                                         if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_authinfo)) {
3429                                                 return (found);
3430                                         }
3431                                         m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
3432                                         sndrcvinfo->sinfo_keynumber_valid = 1;
3433                                         sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber;
3434                                         break;
3435                                 default:
3436                                         return (found);
3437                                 }
3438                                 found = 1;
3439                         }
3440                 }
3441                 at += CMSG_ALIGN(cmh.cmsg_len);
3442         }
3443         return (found);
3444 }
3445
3446 static int
3447 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
3448 {
3449         struct cmsghdr cmh;
3450         int tlen, at;
3451         struct sctp_initmsg initmsg;
3452
3453 #ifdef INET
3454         struct sockaddr_in sin;
3455
3456 #endif
3457 #ifdef INET6
3458         struct sockaddr_in6 sin6;
3459
3460 #endif
3461
3462         tlen = SCTP_BUF_LEN(control);
3463         at = 0;
3464         while (at < tlen) {
3465                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3466                         /* There is not enough room for one more. */
3467                         *error = EINVAL;
3468                         return (1);
3469                 }
3470                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3471                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3472                         /* We dont't have a complete CMSG header. */
3473                         *error = EINVAL;
3474                         return (1);
3475                 }
3476                 if (((int)cmh.cmsg_len + at) > tlen) {
3477                         /* We don't have the complete CMSG. */
3478                         *error = EINVAL;
3479                         return (1);
3480                 }
3481                 if (cmh.cmsg_level == IPPROTO_SCTP) {
3482                         switch (cmh.cmsg_type) {
3483                         case SCTP_INIT:
3484                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct sctp_initmsg)) {
3485                                         *error = EINVAL;
3486                                         return (1);
3487                                 }
3488                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
3489                                 if (initmsg.sinit_max_attempts)
3490                                         stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
3491                                 if (initmsg.sinit_num_ostreams)
3492                                         stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
3493                                 if (initmsg.sinit_max_instreams)
3494                                         stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
3495                                 if (initmsg.sinit_max_init_timeo)
3496                                         stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
3497                                 if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
3498                                         struct sctp_stream_out *tmp_str;
3499                                         unsigned int i;
3500
3501                                         /* Default is NOT correct */
3502                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
3503                                             stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
3504                                         SCTP_TCB_UNLOCK(stcb);
3505                                         SCTP_MALLOC(tmp_str,
3506                                             struct sctp_stream_out *,
3507                                             (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
3508                                             SCTP_M_STRMO);
3509                                         SCTP_TCB_LOCK(stcb);
3510                                         if (tmp_str != NULL) {
3511                                                 SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
3512                                                 stcb->asoc.strmout = tmp_str;
3513                                                 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
3514                                         } else {
3515                                                 stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
3516                                         }
3517                                         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3518                                                 stcb->asoc.strmout[i].next_sequence_sent = 0;
3519                                                 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3520                                                 stcb->asoc.strmout[i].stream_no = i;
3521                                                 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3522                                                 stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
3523                                         }
3524                                 }
3525                                 break;
3526 #ifdef INET
3527                         case SCTP_DSTADDRV4:
3528                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) {
3529                                         *error = EINVAL;
3530                                         return (1);
3531                                 }
3532                                 memset(&sin, 0, sizeof(struct sockaddr_in));
3533                                 sin.sin_family = AF_INET;
3534                                 sin.sin_len = sizeof(struct sockaddr_in);
3535                                 sin.sin_port = stcb->rport;
3536                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3537                                 if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3538                                     (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3539                                     IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3540                                         *error = EINVAL;
3541                                         return (1);
3542                                 }
3543                                 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL,
3544                                     SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3545                                         *error = ENOBUFS;
3546                                         return (1);
3547                                 }
3548                                 break;
3549 #endif
3550 #ifdef INET6
3551                         case SCTP_DSTADDRV6:
3552                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) {
3553                                         *error = EINVAL;
3554                                         return (1);
3555                                 }
3556                                 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3557                                 sin6.sin6_family = AF_INET6;
3558                                 sin6.sin6_len = sizeof(struct sockaddr_in6);
3559                                 sin6.sin6_port = stcb->rport;
3560                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3561                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
3562                                     IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
3563                                         *error = EINVAL;
3564                                         return (1);
3565                                 }
3566 #ifdef INET
3567                                 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3568                                         in6_sin6_2_sin(&sin, &sin6);
3569                                         if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3570                                             (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3571                                             IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3572                                                 *error = EINVAL;
3573                                                 return (1);
3574                                         }
3575                                         if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL,
3576                                             SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3577                                                 *error = ENOBUFS;
3578                                                 return (1);
3579                                         }
3580                                 } else
3581 #endif
3582                                         if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL,
3583                                     SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3584                                         *error = ENOBUFS;
3585                                         return (1);
3586                                 }
3587                                 break;
3588 #endif
3589                         default:
3590                                 break;
3591                         }
3592                 }
3593                 at += CMSG_ALIGN(cmh.cmsg_len);
3594         }
3595         return (0);
3596 }
3597
3598 static struct sctp_tcb *
3599 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
3600     in_port_t port,
3601     struct mbuf *control,
3602     struct sctp_nets **net_p,
3603     int *error)
3604 {
3605         struct cmsghdr cmh;
3606         int tlen, at;
3607         struct sctp_tcb *stcb;
3608         struct sockaddr *addr;
3609
3610 #ifdef INET
3611         struct sockaddr_in sin;
3612
3613 #endif
3614 #ifdef INET6
3615         struct sockaddr_in6 sin6;
3616
3617 #endif
3618
3619         tlen = SCTP_BUF_LEN(control);
3620         at = 0;
3621         while (at < tlen) {
3622                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3623                         /* There is not enough room for one more. */
3624                         *error = EINVAL;
3625                         return (NULL);
3626                 }
3627                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3628                 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(struct cmsghdr))) {
3629                         /* We dont't have a complete CMSG header. */
3630                         *error = EINVAL;
3631                         return (NULL);
3632                 }
3633                 if (((int)cmh.cmsg_len + at) > tlen) {
3634                         /* We don't have the complete CMSG. */
3635                         *error = EINVAL;
3636                         return (NULL);
3637                 }
3638                 if (cmh.cmsg_level == IPPROTO_SCTP) {
3639                         switch (cmh.cmsg_type) {
3640 #ifdef INET
3641                         case SCTP_DSTADDRV4:
3642                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in_addr)) {
3643                                         *error = EINVAL;
3644                                         return (NULL);
3645                                 }
3646                                 memset(&sin, 0, sizeof(struct sockaddr_in));
3647                                 sin.sin_family = AF_INET;
3648                                 sin.sin_len = sizeof(struct sockaddr_in);
3649                                 sin.sin_port = port;
3650                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3651                                 addr = (struct sockaddr *)&sin;
3652                                 break;
3653 #endif
3654 #ifdef INET6
3655                         case SCTP_DSTADDRV6:
3656                                 if ((size_t)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < sizeof(struct in6_addr)) {
3657                                         *error = EINVAL;
3658                                         return (NULL);
3659                                 }
3660                                 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3661                                 sin6.sin6_family = AF_INET6;
3662                                 sin6.sin6_len = sizeof(struct sockaddr_in6);
3663                                 sin6.sin6_port = port;
3664                                 m_copydata(control, at + CMSG_ALIGN(sizeof(struct cmsghdr)), sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3665 #ifdef INET
3666                                 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3667                                         in6_sin6_2_sin(&sin, &sin6);
3668                                         addr = (struct sockaddr *)&sin;
3669                                 } else
3670 #endif
3671                                         addr = (struct sockaddr *)&sin6;
3672                                 break;
3673 #endif
3674                         default:
3675                                 addr = NULL;
3676                                 break;
3677                         }
3678                         if (addr) {
3679                                 stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
3680                                 if (stcb != NULL) {
3681                                         return (stcb);
3682                                 }
3683                         }
3684                 }
3685                 at += CMSG_ALIGN(cmh.cmsg_len);
3686         }
3687         return (NULL);
3688 }
3689
3690 static struct mbuf *
3691 sctp_add_cookie(struct mbuf *init, int init_offset,
3692     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3693 {
3694         struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3695         struct sctp_state_cookie *stc;
3696         struct sctp_paramhdr *ph;
3697         uint8_t *foo;
3698         int sig_offset;
3699         uint16_t cookie_sz;
3700
3701         mret = NULL;
3702         mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3703             sizeof(struct sctp_paramhdr)), 0,
3704             M_DONTWAIT, 1, MT_DATA);
3705         if (mret == NULL) {
3706                 return (NULL);
3707         }
3708         copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3709         if (copy_init == NULL) {
3710                 sctp_m_freem(mret);
3711                 return (NULL);
3712         }
3713 #ifdef SCTP_MBUF_LOGGING
3714         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3715                 struct mbuf *mat;
3716
3717                 for (mat = copy_init; mat; mat = SCTP_BUF_NEXT(mat)) {
3718                         if (SCTP_BUF_IS_EXTENDED(mat)) {
3719                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3720                         }
3721                 }
3722         }
3723 #endif
3724         copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3725             M_DONTWAIT);
3726         if (copy_initack == NULL) {
3727                 sctp_m_freem(mret);
3728                 sctp_m_freem(copy_init);
3729                 return (NULL);
3730         }
3731 #ifdef SCTP_MBUF_LOGGING
3732         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3733                 struct mbuf *mat;
3734
3735                 for (mat = copy_initack; mat; mat = SCTP_BUF_NEXT(mat)) {
3736                         if (SCTP_BUF_IS_EXTENDED(mat)) {
3737                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3738                         }
3739                 }
3740         }
3741 #endif
3742         /* easy side we just drop it on the end */
3743         ph = mtod(mret, struct sctp_paramhdr *);
3744         SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3745             sizeof(struct sctp_paramhdr);
3746         stc = (struct sctp_state_cookie *)((caddr_t)ph +
3747             sizeof(struct sctp_paramhdr));
3748         ph->param_type = htons(SCTP_STATE_COOKIE);
3749         ph->param_length = 0;   /* fill in at the end */
3750         /* Fill in the stc cookie data */
3751         memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3752
3753         /* tack the INIT and then the INIT-ACK onto the chain */
3754         cookie_sz = 0;
3755         for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3756                 cookie_sz += SCTP_BUF_LEN(m_at);
3757                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3758                         SCTP_BUF_NEXT(m_at) = copy_init;
3759                         break;
3760                 }
3761         }
3762         for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3763                 cookie_sz += SCTP_BUF_LEN(m_at);
3764                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3765                         SCTP_BUF_NEXT(m_at) = copy_initack;
3766                         break;
3767                 }
3768         }
3769         for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3770                 cookie_sz += SCTP_BUF_LEN(m_at);
3771                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3772                         break;
3773                 }
3774         }
3775         sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3776         if (sig == NULL) {
3777                 /* no space, so free the entire chain */
3778                 sctp_m_freem(mret);
3779                 return (NULL);
3780         }
3781         SCTP_BUF_LEN(sig) = 0;
3782         SCTP_BUF_NEXT(m_at) = sig;
3783         sig_offset = 0;
3784         foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3785         memset(foo, 0, SCTP_SIGNATURE_SIZE);
3786         *signature = foo;
3787         SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3788         cookie_sz += SCTP_SIGNATURE_SIZE;
3789         ph->param_length = htons(cookie_sz);
3790         return (mret);
3791 }
3792
3793
3794 static uint8_t
3795 sctp_get_ect(struct sctp_tcb *stcb)
3796 {
3797         if ((stcb != NULL) && (stcb->asoc.ecn_allowed == 1)) {
3798                 return (SCTP_ECT0_BIT);
3799         } else {
3800                 return (0);
3801         }
3802 }
3803
3804 static void
3805 sctp_handle_no_route(struct sctp_tcb *stcb,
3806     struct sctp_nets *net,
3807     int so_locked)
3808 {
3809         SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
3810
3811         if (net) {
3812                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
3813                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
3814                 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3815                         if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3816                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3817                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3818                                     stcb, 0,
3819                                     (void *)net,
3820                                     so_locked);
3821                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3822                                 net->dest_state &= ~SCTP_ADDR_PF;
3823                         }
3824                 }
3825                 if (stcb) {
3826                         if (net == stcb->asoc.primary_destination) {
3827                                 /* need a new primary */
3828                                 struct sctp_nets *alt;
3829
3830                                 alt = sctp_find_alternate_net(stcb, net, 0);
3831                                 if (alt != net) {
3832                                         if (stcb->asoc.alternate) {
3833                                                 sctp_free_remote_addr(stcb->asoc.alternate);
3834                                         }
3835                                         stcb->asoc.alternate = alt;
3836                                         atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
3837                                         if (net->ro._s_addr) {
3838                                                 sctp_free_ifa(net->ro._s_addr);
3839                                                 net->ro._s_addr = NULL;
3840                                         }
3841                                         net->src_addr_selected = 0;
3842                                 }
3843                         }
3844                 }
3845         }
3846 }
3847
3848 static int
3849 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3850     struct sctp_tcb *stcb,      /* may be NULL */
3851     struct sctp_nets *net,
3852     struct sockaddr *to,
3853     struct mbuf *m,
3854     uint32_t auth_offset,
3855     struct sctp_auth_chunk *auth,
3856     uint16_t auth_keyid,
3857     int nofragment_flag,
3858     int ecn_ok,
3859     int out_of_asoc_ok,
3860     uint16_t src_port,
3861     uint16_t dest_port,
3862     uint32_t v_tag,
3863     uint16_t port,
3864 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3865     int so_locked SCTP_UNUSED,
3866 #else
3867     int so_locked,
3868 #endif
3869     union sctp_sockstore *over_addr,
3870     struct mbuf *init
3871 )
3872 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3873 {
3874         /**
3875          * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
3876          * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
3877          * - fill in the HMAC digest of any AUTH chunk in the packet.
3878          * - calculate and fill in the SCTP checksum.
3879          * - prepend an IP address header.
3880          * - if boundall use INADDR_ANY.
3881          * - if boundspecific do source address selection.
3882          * - set fragmentation option for ipV4.
3883          * - On return from IP output, check/adjust mtu size of output
3884          *   interface and smallest_mtu size as well.
3885          */
3886         /* Will need ifdefs around this */
3887         struct mbuf *o_pak;
3888         struct mbuf *newm;
3889         struct sctphdr *sctphdr;
3890         int packet_length;
3891         int ret;
3892         uint32_t vrf_id;
3893         sctp_route_t *ro = NULL;
3894         struct udphdr *udp = NULL;
3895         uint8_t tos_value;
3896
3897 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3898         struct socket *so = NULL;
3899
3900 #endif
3901
3902         if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3903                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3904                 sctp_m_freem(m);
3905                 return (EFAULT);
3906         }
3907         if (stcb) {
3908                 vrf_id = stcb->asoc.vrf_id;
3909         } else {
3910                 vrf_id = inp->def_vrf_id;
3911         }
3912
3913         /* fill in the HMAC digest for any AUTH chunk in the packet */
3914         if ((auth != NULL) && (stcb != NULL)) {
3915                 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3916         }
3917         if (net) {
3918                 tos_value = net->dscp;
3919         } else if (stcb) {
3920                 tos_value = stcb->asoc.default_dscp;
3921         } else {
3922                 tos_value = inp->sctp_ep.default_dscp;
3923         }
3924
3925         switch (to->sa_family) {
3926 #ifdef INET
3927         case AF_INET:
3928                 {
3929                         struct ip *ip = NULL;
3930                         sctp_route_t iproute;
3931                         int len;
3932
3933                         len = sizeof(struct ip) + sizeof(struct sctphdr);
3934                         if (port) {
3935                                 len += sizeof(struct udphdr);
3936                         }
3937                         newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3938                         if (newm == NULL) {
3939                                 sctp_m_freem(m);
3940                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3941                                 return (ENOMEM);
3942                         }
3943                         SCTP_ALIGN_TO_END(newm, len);
3944                         SCTP_BUF_LEN(newm) = len;
3945                         SCTP_BUF_NEXT(newm) = m;
3946                         m = newm;
3947                         if (net != NULL) {
3948 #ifdef INVARIANTS
3949                                 if (net->flowidset == 0) {
3950                                         panic("Flow ID not set");
3951                                 }
3952 #endif
3953                                 m->m_pkthdr.flowid = net->flowid;
3954                                 m->m_flags |= M_FLOWID;
3955                         } else {
3956                                 if ((init != NULL) && (init->m_flags & M_FLOWID)) {
3957                                         m->m_pkthdr.flowid = init->m_pkthdr.flowid;
3958                                         m->m_flags |= M_FLOWID;
3959                                 }
3960                         }
3961                         packet_length = sctp_calculate_len(m);
3962                         ip = mtod(m, struct ip *);
3963                         ip->ip_v = IPVERSION;
3964                         ip->ip_hl = (sizeof(struct ip) >> 2);
3965                         if (tos_value == 0) {
3966                                 /*
3967                                  * This means especially, that it is not set
3968                                  * at the SCTP layer. So use the value from
3969                                  * the IP layer.
3970                                  */
3971                                 tos_value = inp->ip_inp.inp.inp_ip_tos;
3972                         }
3973                         tos_value &= 0xfc;
3974                         if (ecn_ok) {
3975                                 tos_value |= sctp_get_ect(stcb);
3976                         }
3977                         if ((nofragment_flag) && (port == 0)) {
3978                                 ip->ip_off = IP_DF;
3979                         } else
3980                                 ip->ip_off = 0;
3981
3982                         /* FreeBSD has a function for ip_id's */
3983                         ip->ip_id = ip_newid();
3984
3985                         ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3986                         ip->ip_len = packet_length;
3987                         ip->ip_tos = tos_value;
3988                         if (port) {
3989                                 ip->ip_p = IPPROTO_UDP;
3990                         } else {
3991                                 ip->ip_p = IPPROTO_SCTP;
3992                         }
3993                         ip->ip_sum = 0;
3994                         if (net == NULL) {
3995                                 ro = &iproute;
3996                                 memset(&iproute, 0, sizeof(iproute));
3997                                 memcpy(&ro->ro_dst, to, to->sa_len);
3998                         } else {
3999                                 ro = (sctp_route_t *) & net->ro;
4000                         }
4001                         /* Now the address selection part */
4002                         ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
4003
4004                         /* call the routine to select the src address */
4005                         if (net && out_of_asoc_ok == 0) {
4006                                 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4007                                         sctp_free_ifa(net->ro._s_addr);
4008                                         net->ro._s_addr = NULL;
4009                                         net->src_addr_selected = 0;
4010                                         if (ro->ro_rt) {
4011                                                 RTFREE(ro->ro_rt);
4012                                                 ro->ro_rt = NULL;
4013                                         }
4014                                 }
4015                                 if (net->src_addr_selected == 0) {
4016                                         /* Cache the source address */
4017                                         net->ro._s_addr = sctp_source_address_selection(inp, stcb,
4018                                             ro, net, 0,
4019                                             vrf_id);
4020                                         net->src_addr_selected = 1;
4021                                 }
4022                                 if (net->ro._s_addr == NULL) {
4023                                         /* No route to host */
4024                                         net->src_addr_selected = 0;
4025                                         sctp_handle_no_route(stcb, net, so_locked);
4026                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4027                                         sctp_m_freem(m);
4028                                         return (EHOSTUNREACH);
4029                                 }
4030                                 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
4031                         } else {
4032                                 if (over_addr == NULL) {
4033                                         struct sctp_ifa *_lsrc;
4034
4035                                         _lsrc = sctp_source_address_selection(inp, stcb, ro,
4036                                             net,
4037                                             out_of_asoc_ok,
4038                                             vrf_id);
4039                                         if (_lsrc == NULL) {
4040                                                 sctp_handle_no_route(stcb, net, so_locked);
4041                                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4042                                                 sctp_m_freem(m);
4043                                                 return (EHOSTUNREACH);
4044                                         }
4045                                         ip->ip_src = _lsrc->address.sin.sin_addr;
4046                                         sctp_free_ifa(_lsrc);
4047                                 } else {
4048                                         ip->ip_src = over_addr->sin.sin_addr;
4049                                         SCTP_RTALLOC(ro, vrf_id);
4050                                 }
4051                         }
4052                         if (port) {
4053                                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4054                                         sctp_handle_no_route(stcb, net, so_locked);
4055                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4056                                         sctp_m_freem(m);
4057                                         return (EHOSTUNREACH);
4058                                 }
4059                                 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
4060                                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4061                                 udp->uh_dport = port;
4062                                 udp->uh_ulen = htons(packet_length - sizeof(struct ip));
4063                                 if (V_udp_cksum) {
4064                                         udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
4065                                 } else {
4066                                         udp->uh_sum = 0;
4067                                 }
4068                                 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4069                         } else {
4070                                 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
4071                         }
4072
4073                         sctphdr->src_port = src_port;
4074                         sctphdr->dest_port = dest_port;
4075                         sctphdr->v_tag = v_tag;
4076                         sctphdr->checksum = 0;
4077
4078                         /*
4079                          * If source address selection fails and we find no
4080                          * route then the ip_output should fail as well with
4081                          * a NO_ROUTE_TO_HOST type error. We probably should
4082                          * catch that somewhere and abort the association
4083                          * right away (assuming this is an INIT being sent).
4084                          */
4085                         if (ro->ro_rt == NULL) {
4086                                 /*
4087                                  * src addr selection failed to find a route
4088                                  * (or valid source addr), so we can't get
4089                                  * there from here (yet)!
4090                                  */
4091                                 sctp_handle_no_route(stcb, net, so_locked);
4092                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4093                                 sctp_m_freem(m);
4094                                 return (EHOSTUNREACH);
4095                         }
4096                         if (ro != &iproute) {
4097                                 memcpy(&iproute, ro, sizeof(*ro));
4098                         }
4099                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
4100                             (uint32_t) (ntohl(ip->ip_src.s_addr)));
4101                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
4102                             (uint32_t) (ntohl(ip->ip_dst.s_addr)));
4103                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
4104                             ro->ro_rt);
4105
4106                         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4107                                 /* failed to prepend data, give up */
4108                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4109                                 sctp_m_freem(m);
4110                                 return (ENOMEM);
4111                         }
4112 #ifdef  SCTP_PACKET_LOGGING
4113                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4114                                 sctp_packet_log(m, packet_length);
4115 #endif
4116                         SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4117                         if (port) {
4118 #if defined(SCTP_WITH_NO_CSUM)
4119                                 SCTP_STAT_INCR(sctps_sendnocrc);
4120 #else
4121                                 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4122                                     (stcb) &&
4123                                     (stcb->asoc.loopback_scope))) {
4124                                         sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
4125                                         SCTP_STAT_INCR(sctps_sendswcrc);
4126                                 } else {
4127                                         SCTP_STAT_INCR(sctps_sendnocrc);
4128                                 }
4129 #endif
4130                                 if (V_udp_cksum) {
4131                                         SCTP_ENABLE_UDP_CSUM(o_pak);
4132                                 }
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);
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                                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4417                                         sctp_handle_no_route(stcb, net, so_locked);
4418                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4419                                         sctp_m_freem(m);
4420                                         return (EHOSTUNREACH);
4421                                 }
4422                                 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4423                                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4424                                 udp->uh_dport = port;
4425                                 udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
4426                                 udp->uh_sum = 0;
4427                                 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4428                         } else {
4429                                 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4430                         }
4431
4432                         sctphdr->src_port = src_port;
4433                         sctphdr->dest_port = dest_port;
4434                         sctphdr->v_tag = v_tag;
4435                         sctphdr->checksum = 0;
4436
4437                         /*
4438                          * We set the hop limit now since there is a good
4439                          * chance that our ro pointer is now filled
4440                          */
4441                         ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4442                         ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4443
4444 #ifdef SCTP_DEBUG
4445                         /* Copy to be sure something bad is not happening */
4446                         sin6->sin6_addr = ip6h->ip6_dst;
4447                         lsa6->sin6_addr = ip6h->ip6_src;
4448 #endif
4449
4450                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4451                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4452                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4453                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4454                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4455                         if (net) {
4456                                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4457                                 /*
4458                                  * preserve the port and scope for link
4459                                  * local send
4460                                  */
4461                                 prev_scope = sin6->sin6_scope_id;
4462                                 prev_port = sin6->sin6_port;
4463                         }
4464                         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4465                                 /* failed to prepend data, give up */
4466                                 sctp_m_freem(m);
4467                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4468                                 return (ENOMEM);
4469                         }
4470 #ifdef  SCTP_PACKET_LOGGING
4471                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4472                                 sctp_packet_log(m, packet_length);
4473 #endif
4474                         SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4475                         if (port) {
4476 #if defined(SCTP_WITH_NO_CSUM)
4477                                 SCTP_STAT_INCR(sctps_sendnocrc);
4478 #else
4479                                 if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4480                                     (stcb) &&
4481                                     (stcb->asoc.loopback_scope))) {
4482                                         sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4483                                         SCTP_STAT_INCR(sctps_sendswcrc);
4484                                 } else {
4485                                         SCTP_STAT_INCR(sctps_sendnocrc);
4486                                 }
4487 #endif
4488                                 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4489                                         udp->uh_sum = 0xffff;
4490                                 }
4491                         } else {
4492 #if defined(SCTP_WITH_NO_CSUM)
4493                                 SCTP_STAT_INCR(sctps_sendnocrc);
4494 #else
4495                                 m->m_pkthdr.csum_flags = CSUM_SCTP;
4496                                 m->m_pkthdr.csum_data = 0;
4497                                 SCTP_STAT_INCR(sctps_sendhwcrc);
4498 #endif
4499                         }
4500                         /* send it out. table id is taken from stcb */
4501 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4502                         if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4503                                 so = SCTP_INP_SO(inp);
4504                                 SCTP_SOCKET_UNLOCK(so, 0);
4505                         }
4506 #endif
4507                         SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4508 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4509                         if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4510                                 atomic_add_int(&stcb->asoc.refcnt, 1);
4511                                 SCTP_TCB_UNLOCK(stcb);
4512                                 SCTP_SOCKET_LOCK(so, 0);
4513                                 SCTP_TCB_LOCK(stcb);
4514                                 atomic_subtract_int(&stcb->asoc.refcnt, 1);
4515                         }
4516 #endif
4517                         if (net) {
4518                                 /* for link local this must be done */
4519                                 sin6->sin6_scope_id = prev_scope;
4520                                 sin6->sin6_port = prev_port;
4521                         }
4522                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4523                         SCTP_STAT_INCR(sctps_sendpackets);
4524                         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4525                         if (ret) {
4526                                 SCTP_STAT_INCR(sctps_senderrors);
4527                         }
4528                         if (net == NULL) {
4529                                 /* Now if we had a temp route free it */
4530                                 if (ro->ro_rt) {
4531                                         RTFREE(ro->ro_rt);
4532                                 }
4533                         } else {
4534                                 /*
4535                                  * PMTU check versus smallest asoc MTU goes
4536                                  * here
4537                                  */
4538                                 if (ro->ro_rt == NULL) {
4539                                         /* Route was freed */
4540                                         if (net->ro._s_addr &&
4541                                             net->src_addr_selected) {
4542                                                 sctp_free_ifa(net->ro._s_addr);
4543                                                 net->ro._s_addr = NULL;
4544                                         }
4545                                         net->src_addr_selected = 0;
4546                                 }
4547                                 if ((ro->ro_rt != NULL) &&
4548                                     (net->ro._s_addr)) {
4549                                         uint32_t mtu;
4550
4551                                         mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4552                                         if (mtu &&
4553                                             (stcb->asoc.smallest_mtu > mtu)) {
4554                                                 sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4555                                                 net->mtu = mtu;
4556                                                 if (net->port) {
4557                                                         net->mtu -= sizeof(struct udphdr);
4558                                                 }
4559                                         }
4560                                 } else if (ifp) {
4561                                         if (ND_IFINFO(ifp)->linkmtu &&
4562                                             (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4563                                                 sctp_mtu_size_reset(inp,
4564                                                     &stcb->asoc,
4565                                                     ND_IFINFO(ifp)->linkmtu);
4566                                         }
4567                                 }
4568                         }
4569                         return (ret);
4570                 }
4571 #endif
4572         default:
4573                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4574                     ((struct sockaddr *)to)->sa_family);
4575                 sctp_m_freem(m);
4576                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4577                 return (EFAULT);
4578         }
4579 }
4580
4581
4582 void
4583 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4584 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4585     SCTP_UNUSED
4586 #endif
4587 )
4588 {
4589         struct mbuf *m, *m_at, *mp_last;
4590         struct sctp_nets *net;
4591         struct sctp_init_chunk *init;
4592         struct sctp_supported_addr_param *sup_addr;
4593         struct sctp_adaptation_layer_indication *ali;
4594         struct sctp_ecn_supported_param *ecn;
4595         struct sctp_prsctp_supported_param *prsctp;
4596         struct sctp_supported_chunk_types_param *pr_supported;
4597         int cnt_inits_to = 0;
4598         int padval, ret;
4599         int num_ext;
4600         int p_len;
4601
4602         /* INIT's always go to the primary (and usually ONLY address) */
4603         mp_last = NULL;
4604         net = stcb->asoc.primary_destination;
4605         if (net == NULL) {
4606                 net = TAILQ_FIRST(&stcb->asoc.nets);
4607                 if (net == NULL) {
4608                         /* TSNH */
4609                         return;
4610                 }
4611                 /* we confirm any address we send an INIT to */
4612                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4613                 (void)sctp_set_primary_addr(stcb, NULL, net);
4614         } else {
4615                 /* we confirm any address we send an INIT to */
4616                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4617         }
4618         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4619 #ifdef INET6
4620         if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4621                 /*
4622                  * special hook, if we are sending to link local it will not
4623                  * show up in our private address count.
4624                  */
4625                 struct sockaddr_in6 *sin6l;
4626
4627                 sin6l = &net->ro._l_addr.sin6;
4628                 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4629                         cnt_inits_to = 1;
4630         }
4631 #endif
4632         if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4633                 /* This case should not happen */
4634                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4635                 return;
4636         }
4637         /* start the INIT timer */
4638         sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4639
4640         m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4641         if (m == NULL) {
4642                 /* No memory, INIT timer will re-attempt. */
4643                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4644                 return;
4645         }
4646         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4647         /*
4648          * assume peer supports asconf in order to be able to queue local
4649          * address changes while an INIT is in flight and before the assoc
4650          * is established.
4651          */
4652         stcb->asoc.peer_supports_asconf = 1;
4653         /* Now lets put the SCTP header in place */
4654         init = mtod(m, struct sctp_init_chunk *);
4655         /* now the chunk header */
4656         init->ch.chunk_type = SCTP_INITIATION;
4657         init->ch.chunk_flags = 0;
4658         /* fill in later from mbuf we build */
4659         init->ch.chunk_length = 0;
4660         /* place in my tag */
4661         init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4662         /* set up some of the credits. */
4663         init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4664             SCTP_MINIMAL_RWND));
4665
4666         init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4667         init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4668         init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4669         /* now the address restriction */
4670         /* XXX Should we take the address family of the socket into account? */
4671         sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4672             sizeof(*init));
4673         sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4674 #ifdef INET6
4675 #ifdef INET
4676         /* we support 2 types: IPv4/IPv6 */
4677         sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + 2 * sizeof(uint16_t));
4678         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4679         sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4680 #else
4681         /* we support 1 type: IPv6 */
4682         sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t));
4683         sup_addr->addr_type[0] = htons(SCTP_IPV6_ADDRESS);
4684         sup_addr->addr_type[1] = htons(0);      /* this is the padding */
4685 #endif
4686 #else
4687         /* we support 1 type: IPv4 */
4688         sup_addr->ph.param_length = htons(sizeof(struct sctp_paramhdr) + sizeof(uint16_t));
4689         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4690         sup_addr->addr_type[1] = htons(0);      /* this is the padding */
4691 #endif
4692         SCTP_BUF_LEN(m) += sizeof(struct sctp_supported_addr_param);
4693         /* adaptation layer indication parameter */
4694         ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(struct sctp_supported_addr_param));
4695         ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4696         ali->ph.param_length = htons(sizeof(*ali));
4697         ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4698         SCTP_BUF_LEN(m) += sizeof(*ali);
4699         ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4700
4701         if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4702                 /* Add NAT friendly parameter */
4703                 struct sctp_paramhdr *ph;
4704
4705                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4706                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4707                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4708                 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4709                 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4710         }
4711         /* now any cookie time extensions */
4712         if (stcb->asoc.cookie_preserve_req) {
4713                 struct sctp_cookie_perserve_param *cookie_preserve;
4714
4715                 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4716                 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4717                 cookie_preserve->ph.param_length = htons(
4718                     sizeof(*cookie_preserve));
4719                 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4720                 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4721                 ecn = (struct sctp_ecn_supported_param *)(
4722                     (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4723                 stcb->asoc.cookie_preserve_req = 0;
4724         }
4725         /* ECN parameter */
4726         if (stcb->asoc.ecn_allowed == 1) {
4727                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4728                 ecn->ph.param_length = htons(sizeof(*ecn));
4729                 SCTP_BUF_LEN(m) += sizeof(*ecn);
4730                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4731                     sizeof(*ecn));
4732         } else {
4733                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4734         }
4735         /* And now tell the peer we do pr-sctp */
4736         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4737         prsctp->ph.param_length = htons(sizeof(*prsctp));
4738         SCTP_BUF_LEN(m) += sizeof(*prsctp);
4739
4740         /* And now tell the peer we do all the extensions */
4741         pr_supported = (struct sctp_supported_chunk_types_param *)
4742             ((caddr_t)prsctp + sizeof(*prsctp));
4743         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4744         num_ext = 0;
4745         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4746         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4747         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4748         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4749         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4750         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4751                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4752         }
4753         if (stcb->asoc.sctp_nr_sack_on_off == 1) {
4754                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4755         }
4756         p_len = sizeof(*pr_supported) + num_ext;
4757         pr_supported->ph.param_length = htons(p_len);
4758         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4759         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4760
4761
4762         /* add authentication parameters */
4763         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4764                 struct sctp_auth_random *randp;
4765                 struct sctp_auth_hmac_algo *hmacs;
4766                 struct sctp_auth_chunk_list *chunks;
4767
4768                 /* attach RANDOM parameter, if available */
4769                 if (stcb->asoc.authinfo.random != NULL) {
4770                         randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4771                         p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4772                         /* random key already contains the header */
4773                         bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4774                         /* zero out any padding required */
4775                         bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4776                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4777                 }
4778                 /* add HMAC_ALGO parameter */
4779                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4780                 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4781                     (uint8_t *) hmacs->hmac_ids);
4782                 if (p_len > 0) {
4783                         p_len += sizeof(*hmacs);
4784                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4785                         hmacs->ph.param_length = htons(p_len);
4786                         /* zero out any padding required */
4787                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4788                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4789                 }
4790                 /* add CHUNKS parameter */
4791                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4792                 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4793                     chunks->chunk_types);
4794                 if (p_len > 0) {
4795                         p_len += sizeof(*chunks);
4796                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4797                         chunks->ph.param_length = htons(p_len);
4798                         /* zero out any padding required */
4799                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4800                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4801                 }
4802         }
4803         /* now the addresses */
4804         {
4805                 struct sctp_scoping scp;
4806
4807                 /*
4808                  * To optimize this we could put the scoping stuff into a
4809                  * structure and remove the individual uint8's from the
4810                  * assoc structure. Then we could just sifa in the address
4811                  * within the stcb. But for now this is a quick hack to get
4812                  * the address stuff teased apart.
4813                  */
4814
4815                 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4816                 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4817                 scp.loopback_scope = stcb->asoc.loopback_scope;
4818                 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4819                 scp.local_scope = stcb->asoc.local_scope;
4820                 scp.site_scope = stcb->asoc.site_scope;
4821
4822                 sctp_add_addresses_to_i_ia(inp, stcb, &scp, m, cnt_inits_to);
4823         }
4824
4825         /* calulate the size and update pkt header and chunk header */
4826         p_len = 0;
4827         for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4828                 if (SCTP_BUF_NEXT(m_at) == NULL)
4829                         mp_last = m_at;
4830                 p_len += SCTP_BUF_LEN(m_at);
4831         }
4832         init->ch.chunk_length = htons(p_len);
4833         /*
4834          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4835          * here since the timer will drive a retranmission.
4836          */
4837
4838         /* I don't expect this to execute but we will be safe here */
4839         padval = p_len % 4;
4840         if ((padval) && (mp_last)) {
4841                 /*
4842                  * The compiler worries that mp_last may not be set even
4843                  * though I think it is impossible :-> however we add
4844                  * mp_last here just in case.
4845                  */
4846                 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4847                 if (ret) {
4848                         /* Houston we have a problem, no space */
4849                         sctp_m_freem(m);
4850                         return;
4851                 }
4852         }
4853         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4854         ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4855             (struct sockaddr *)&net->ro._l_addr,
4856             m, 0, NULL, 0, 0, 0, 0,
4857             inp->sctp_lport, stcb->rport, htonl(0),
4858             net->port, so_locked, NULL, NULL);
4859         SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4860         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4861         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4862 }
4863
4864 struct mbuf *
4865 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4866     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4867 {
4868         /*
4869          * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4870          * being equal to the beginning of the params i.e. (iphlen +
4871          * sizeof(struct sctp_init_msg) parse through the parameters to the
4872          * end of the mbuf verifying that all parameters are known.
4873          * 
4874          * For unknown parameters build and return a mbuf with
4875          * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4876          * processing this chunk stop, and set *abort_processing to 1.
4877          * 
4878          * By having param_offset be pre-set to where parameters begin it is
4879          * hoped that this routine may be reused in the future by new
4880          * features.
4881          */
4882         struct sctp_paramhdr *phdr, params;
4883
4884         struct mbuf *mat, *op_err;
4885         char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4886         int at, limit, pad_needed;
4887         uint16_t ptype, plen, padded_size;
4888         int err_at;
4889
4890         *abort_processing = 0;
4891         mat = in_initpkt;
4892         err_at = 0;
4893         limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4894         at = param_offset;
4895         op_err = NULL;
4896         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4897         phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4898         while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4899                 ptype = ntohs(phdr->param_type);
4900                 plen = ntohs(phdr->param_length);
4901                 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4902                         /* wacked parameter */
4903                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4904                         goto invalid_size;
4905                 }
4906                 limit -= SCTP_SIZE32(plen);
4907                 /*-
4908                  * All parameters for all chunks that we know/understand are
4909                  * listed here. We process them other places and make
4910                  * appropriate stop actions per the upper bits. However this
4911                  * is the generic routine processor's can call to get back
4912                  * an operr.. to either incorporate (init-ack) or send.
4913                  */
4914                 padded_size = SCTP_SIZE32(plen);
4915                 switch (ptype) {
4916                         /* Param's with variable size */
4917                 case SCTP_HEARTBEAT_INFO:
4918                 case SCTP_STATE_COOKIE:
4919                 case SCTP_UNRECOG_PARAM:
4920                 case SCTP_ERROR_CAUSE_IND:
4921                         /* ok skip fwd */
4922                         at += padded_size;
4923                         break;
4924                         /* Param's with variable size within a range */
4925                 case SCTP_CHUNK_LIST:
4926                 case SCTP_SUPPORTED_CHUNK_EXT:
4927                         if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4928                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4929                                 goto invalid_size;
4930                         }
4931                         at += padded_size;
4932                         break;
4933                 case SCTP_SUPPORTED_ADDRTYPE:
4934                         if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4935                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4936                                 goto invalid_size;
4937                         }
4938                         at += padded_size;
4939                         break;
4940                 case SCTP_RANDOM:
4941                         if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4942                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4943                                 goto invalid_size;
4944                         }
4945                         at += padded_size;
4946                         break;
4947                 case SCTP_SET_PRIM_ADDR:
4948                 case SCTP_DEL_IP_ADDRESS:
4949                 case SCTP_ADD_IP_ADDRESS:
4950                         if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4951                             (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4952                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4953                                 goto invalid_size;
4954                         }
4955                         at += padded_size;
4956                         break;
4957                         /* Param's with a fixed size */
4958                 case SCTP_IPV4_ADDRESS:
4959                         if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4960                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4961                                 goto invalid_size;
4962                         }
4963                         at += padded_size;
4964                         break;
4965                 case SCTP_IPV6_ADDRESS:
4966                         if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4967                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4968                                 goto invalid_size;
4969                         }
4970                         at += padded_size;
4971                         break;
4972                 case SCTP_COOKIE_PRESERVE:
4973                         if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4974                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4975                                 goto invalid_size;
4976                         }
4977                         at += padded_size;
4978                         break;
4979                 case SCTP_HAS_NAT_SUPPORT:
4980                         *nat_friendly = 1;
4981                         /* fall through */
4982                 case SCTP_PRSCTP_SUPPORTED:
4983
4984                         if (padded_size != sizeof(struct sctp_paramhdr)) {
4985                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
4986                                 goto invalid_size;
4987                         }
4988                         at += padded_size;
4989                         break;
4990                 case SCTP_ECN_CAPABLE:
4991                         if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4992                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4993                                 goto invalid_size;
4994                         }
4995                         at += padded_size;
4996                         break;
4997                 case SCTP_ULP_ADAPTATION:
4998                         if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4999                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
5000                                 goto invalid_size;
5001                         }
5002                         at += padded_size;
5003                         break;
5004                 case SCTP_SUCCESS_REPORT:
5005                         if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
5006                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
5007                                 goto invalid_size;
5008                         }
5009                         at += padded_size;
5010                         break;
5011                 case SCTP_HOSTNAME_ADDRESS:
5012                         {
5013                                 /* We can NOT handle HOST NAME addresses!! */
5014                                 int l_len;
5015
5016                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
5017                                 *abort_processing = 1;
5018                                 if (op_err == NULL) {
5019                                         /* Ok need to try to get a mbuf */
5020 #ifdef INET6
5021                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5022 #else
5023                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5024 #endif
5025                                         l_len += plen;
5026                                         l_len += sizeof(struct sctp_paramhdr);
5027                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5028                                         if (op_err) {
5029                                                 SCTP_BUF_LEN(op_err) = 0;
5030                                                 /*
5031                                                  * pre-reserve space for ip
5032                                                  * and sctp header  and
5033                                                  * chunk hdr
5034                                                  */
5035 #ifdef INET6
5036                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5037 #else
5038                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5039 #endif
5040                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5041                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5042                                         }
5043                                 }
5044                                 if (op_err) {
5045                                         /* If we have space */
5046                                         struct sctp_paramhdr s;
5047
5048                                         if (err_at % 4) {
5049                                                 uint32_t cpthis = 0;
5050
5051                                                 pad_needed = 4 - (err_at % 4);
5052                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5053                                                 err_at += pad_needed;
5054                                         }
5055                                         s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
5056                                         s.param_length = htons(sizeof(s) + plen);
5057                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5058                                         err_at += sizeof(s);
5059                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
5060                                         if (phdr == NULL) {
5061                                                 sctp_m_freem(op_err);
5062                                                 /*
5063                                                  * we are out of memory but
5064                                                  * we still need to have a
5065                                                  * look at what to do (the
5066                                                  * system is in trouble
5067                                                  * though).
5068                                                  */
5069                                                 return (NULL);
5070                                         }
5071                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
5072                                 }
5073                                 return (op_err);
5074                                 break;
5075                         }
5076                 default:
5077                         /*
5078                          * we do not recognize the parameter figure out what
5079                          * we do.
5080                          */
5081                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
5082                         if ((ptype & 0x4000) == 0x4000) {
5083                                 /* Report bit is set?? */
5084                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
5085                                 if (op_err == NULL) {
5086                                         int l_len;
5087
5088                                         /* Ok need to try to get an mbuf */
5089 #ifdef INET6
5090                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5091 #else
5092                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5093 #endif
5094                                         l_len += plen;
5095                                         l_len += sizeof(struct sctp_paramhdr);
5096                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5097                                         if (op_err) {
5098                                                 SCTP_BUF_LEN(op_err) = 0;
5099 #ifdef INET6
5100                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5101 #else
5102                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5103 #endif
5104                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5105                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5106                                         }
5107                                 }
5108                                 if (op_err) {
5109                                         /* If we have space */
5110                                         struct sctp_paramhdr s;
5111
5112                                         if (err_at % 4) {
5113                                                 uint32_t cpthis = 0;
5114
5115                                                 pad_needed = 4 - (err_at % 4);
5116                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5117                                                 err_at += pad_needed;
5118                                         }
5119                                         s.param_type = htons(SCTP_UNRECOG_PARAM);
5120                                         s.param_length = htons(sizeof(s) + plen);
5121                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5122                                         err_at += sizeof(s);
5123                                         if (plen > sizeof(tempbuf)) {
5124                                                 plen = sizeof(tempbuf);
5125                                         }
5126                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
5127                                         if (phdr == NULL) {
5128                                                 sctp_m_freem(op_err);
5129                                                 /*
5130                                                  * we are out of memory but
5131                                                  * we still need to have a
5132                                                  * look at what to do (the
5133                                                  * system is in trouble
5134                                                  * though).
5135                                                  */
5136                                                 op_err = NULL;
5137                                                 goto more_processing;
5138                                         }
5139                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
5140                                         err_at += plen;
5141                                 }
5142                         }
5143         more_processing:
5144                         if ((ptype & 0x8000) == 0x0000) {
5145                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
5146                                 return (op_err);
5147                         } else {
5148                                 /* skip this chunk and continue processing */
5149                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
5150                                 at += SCTP_SIZE32(plen);
5151                         }
5152                         break;
5153
5154                 }
5155                 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
5156         }
5157         return (op_err);
5158 invalid_size:
5159         SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
5160         *abort_processing = 1;
5161         if ((op_err == NULL) && phdr) {
5162                 int l_len;
5163
5164 #ifdef INET6
5165                 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5166 #else
5167                 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
5168 #endif
5169                 l_len += (2 * sizeof(struct sctp_paramhdr));
5170                 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
5171                 if (op_err) {
5172                         SCTP_BUF_LEN(op_err) = 0;
5173 #ifdef INET6
5174                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5175 #else
5176                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5177 #endif
5178                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5179                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5180                 }
5181         }
5182         if ((op_err) && phdr) {
5183                 struct sctp_paramhdr s;
5184
5185                 if (err_at % 4) {
5186                         uint32_t cpthis = 0;
5187
5188                         pad_needed = 4 - (err_at % 4);
5189                         m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
5190                         err_at += pad_needed;
5191                 }
5192                 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5193                 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
5194                 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
5195                 err_at += sizeof(s);
5196                 /* Only copy back the p-hdr that caused the issue */
5197                 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
5198         }
5199         return (op_err);
5200 }
5201
5202 static int
5203 sctp_are_there_new_addresses(struct sctp_association *asoc,
5204     struct mbuf *in_initpkt, int offset)
5205 {
5206         /*
5207          * Given a INIT packet, look through the packet to verify that there
5208          * are NO new addresses. As we go through the parameters add reports
5209          * of any un-understood parameters that require an error.  Also we
5210          * must return (1) to drop the packet if we see a un-understood
5211          * parameter that tells us to drop the chunk.
5212          */
5213         struct sockaddr *sa_touse;
5214         struct sockaddr *sa;
5215         struct sctp_paramhdr *phdr, params;
5216         uint16_t ptype, plen;
5217         uint8_t fnd;
5218         struct sctp_nets *net;
5219         struct ip *iph;
5220
5221 #ifdef INET
5222         struct sockaddr_in sin4, *sa4;
5223
5224 #endif
5225 #ifdef INET6
5226         struct sockaddr_in6 sin6, *sa6;
5227         struct ip6_hdr *ip6h;
5228
5229 #endif
5230
5231 #ifdef INET
5232         memset(&sin4, 0, sizeof(sin4));
5233         sin4.sin_family = AF_INET;
5234         sin4.sin_len = sizeof(sin4);
5235 #endif
5236 #ifdef INET6
5237         memset(&sin6, 0, sizeof(sin6));
5238         sin6.sin6_family = AF_INET6;
5239         sin6.sin6_len = sizeof(sin6);
5240 #endif
5241         sa_touse = NULL;
5242         /* First what about the src address of the pkt ? */
5243         iph = mtod(in_initpkt, struct ip *);
5244         switch (iph->ip_v) {
5245 #ifdef INET
5246         case IPVERSION:
5247                 /* source addr is IPv4 */
5248                 sin4.sin_addr = iph->ip_src;
5249                 sa_touse = (struct sockaddr *)&sin4;
5250                 break;
5251 #endif
5252 #ifdef INET6
5253         case IPV6_VERSION >> 4:
5254                 /* source addr is IPv6 */
5255                 ip6h = mtod(in_initpkt, struct ip6_hdr *);
5256                 sin6.sin6_addr = ip6h->ip6_src;
5257                 sa_touse = (struct sockaddr *)&sin6;
5258                 break;
5259 #endif
5260         default:
5261                 return (1);
5262         }
5263
5264         fnd = 0;
5265         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5266                 sa = (struct sockaddr *)&net->ro._l_addr;
5267                 if (sa->sa_family == sa_touse->sa_family) {
5268 #ifdef INET
5269                         if (sa->sa_family == AF_INET) {
5270                                 sa4 = (struct sockaddr_in *)sa;
5271                                 if (sa4->sin_addr.s_addr == sin4.sin_addr.s_addr) {
5272                                         fnd = 1;
5273                                         break;
5274                                 }
5275                         }
5276 #endif
5277 #ifdef INET6
5278                         if (sa->sa_family == AF_INET6) {
5279                                 sa6 = (struct sockaddr_in6 *)sa;
5280                                 if (SCTP6_ARE_ADDR_EQUAL(sa6, &sin6)) {
5281                                         fnd = 1;
5282                                         break;
5283                                 }
5284                         }
5285 #endif
5286                 }
5287         }
5288         if (fnd == 0) {
5289                 /* New address added! no need to look futher. */
5290                 return (1);
5291         }
5292         /* Ok so far lets munge through the rest of the packet */
5293         offset += sizeof(struct sctp_init_chunk);
5294         phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5295         while (phdr) {
5296                 sa_touse = NULL;
5297                 ptype = ntohs(phdr->param_type);
5298                 plen = ntohs(phdr->param_length);
5299                 switch (ptype) {
5300 #ifdef INET
5301                 case SCTP_IPV4_ADDRESS:
5302                         {
5303                                 struct sctp_ipv4addr_param *p4, p4_buf;
5304
5305                                 phdr = sctp_get_next_param(in_initpkt, offset,
5306                                     (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5307                                 if (plen != sizeof(struct sctp_ipv4addr_param) ||
5308                                     phdr == NULL) {
5309                                         return (1);
5310                                 }
5311                                 p4 = (struct sctp_ipv4addr_param *)phdr;
5312                                 sin4.sin_addr.s_addr = p4->addr;
5313                                 sa_touse = (struct sockaddr *)&sin4;
5314                                 break;
5315                         }
5316 #endif
5317 #ifdef INET6
5318                 case SCTP_IPV6_ADDRESS:
5319                         {
5320                                 struct sctp_ipv6addr_param *p6, p6_buf;
5321
5322                                 phdr = sctp_get_next_param(in_initpkt, offset,
5323                                     (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5324                                 if (plen != sizeof(struct sctp_ipv6addr_param) ||
5325                                     phdr == NULL) {
5326                                         return (1);
5327                                 }
5328                                 p6 = (struct sctp_ipv6addr_param *)phdr;
5329                                 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5330                                     sizeof(p6->addr));
5331                                 sa_touse = (struct sockaddr *)&sin6;
5332                                 break;
5333                         }
5334 #endif
5335                 default:
5336                         sa_touse = NULL;
5337                         break;
5338                 }
5339                 if (sa_touse) {
5340                         /* ok, sa_touse points to one to check */
5341                         fnd = 0;
5342                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5343                                 sa = (struct sockaddr *)&net->ro._l_addr;
5344                                 if (sa->sa_family != sa_touse->sa_family) {
5345                                         continue;
5346                                 }
5347 #ifdef INET
5348                                 if (sa->sa_family == AF_INET) {
5349                                         sa4 = (struct sockaddr_in *)sa;
5350                                         if (sa4->sin_addr.s_addr ==
5351                                             sin4.sin_addr.s_addr) {
5352                                                 fnd = 1;
5353                                                 break;
5354                                         }
5355                                 }
5356 #endif
5357 #ifdef INET6
5358                                 if (sa->sa_family == AF_INET6) {
5359                                         sa6 = (struct sockaddr_in6 *)sa;
5360                                         if (SCTP6_ARE_ADDR_EQUAL(
5361                                             sa6, &sin6)) {
5362                                                 fnd = 1;
5363                                                 break;
5364                                         }
5365                                 }
5366 #endif
5367                         }
5368                         if (!fnd) {
5369                                 /* New addr added! no need to look further */
5370                                 return (1);
5371                         }
5372                 }
5373                 offset += SCTP_SIZE32(plen);
5374                 phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5375         }
5376         return (0);
5377 }
5378
5379 /*
5380  * Given a MBUF chain that was sent into us containing an INIT. Build a
5381  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5382  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5383  * message (i.e. the struct sctp_init_msg).
5384  */
5385 void
5386 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5387     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
5388     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
5389 {
5390         struct sctp_association *asoc;
5391         struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
5392         struct sctp_init_ack_chunk *initack;
5393         struct sctp_adaptation_layer_indication *ali;
5394         struct sctp_ecn_supported_param *ecn;
5395         struct sctp_prsctp_supported_param *prsctp;
5396         struct sctp_supported_chunk_types_param *pr_supported;
5397         union sctp_sockstore store, store1, *over_addr;
5398
5399 #ifdef INET
5400         struct sockaddr_in *sin, *to_sin;
5401
5402 #endif
5403 #ifdef INET6
5404         struct sockaddr_in6 *sin6, *to_sin6;
5405
5406 #endif
5407         struct ip *iph;
5408
5409 #ifdef INET6
5410         struct ip6_hdr *ip6;
5411
5412 #endif
5413         struct sockaddr *to;
5414         struct sctp_state_cookie stc;
5415         struct sctp_nets *net = NULL;
5416         uint8_t *signature = NULL;
5417         int cnt_inits_to = 0;
5418         uint16_t his_limit, i_want;
5419         int abort_flag, padval;
5420         int num_ext;
5421         int p_len;
5422         int nat_friendly = 0;
5423         struct socket *so;
5424
5425         if (stcb)
5426                 asoc = &stcb->asoc;
5427         else
5428                 asoc = NULL;
5429         mp_last = NULL;
5430         if ((asoc != NULL) &&
5431             (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
5432             (sctp_are_there_new_addresses(asoc, init_pkt, offset))) {
5433                 /* new addresses, out of here in non-cookie-wait states */
5434                 /*
5435                  * Send a ABORT, we don't add the new address error clause
5436                  * though we even set the T bit and copy in the 0 tag.. this
5437                  * looks no different than if no listener was present.
5438                  */
5439                 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
5440                 return;
5441         }
5442         abort_flag = 0;
5443         op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5444             (offset + sizeof(struct sctp_init_chunk)),
5445             &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
5446         if (abort_flag) {
5447 do_a_abort:
5448                 sctp_send_abort(init_pkt, iphlen, sh,
5449                     init_chk->init.initiate_tag, op_err, vrf_id, port);
5450                 return;
5451         }
5452         m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
5453         if (m == NULL) {
5454                 /* No memory, INIT timer will re-attempt. */
5455                 if (op_err)
5456                         sctp_m_freem(op_err);
5457                 return;
5458         }
5459         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5460
5461         /* the time I built cookie */
5462         (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5463
5464         /* populate any tie tags */
5465         if (asoc != NULL) {
5466                 /* unlock before tag selections */
5467                 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5468                 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5469                 stc.cookie_life = asoc->cookie_life;
5470                 net = asoc->primary_destination;
5471         } else {
5472                 stc.tie_tag_my_vtag = 0;
5473                 stc.tie_tag_peer_vtag = 0;
5474                 /* life I will award this cookie */
5475                 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5476         }
5477
5478         /* copy in the ports for later check */
5479         stc.myport = sh->dest_port;
5480         stc.peerport = sh->src_port;
5481
5482         /*
5483          * If we wanted to honor cookie life extentions, we would add to
5484          * stc.cookie_life. For now we should NOT honor any extension
5485          */
5486         stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5487         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5488                 struct inpcb *in_inp;
5489
5490                 /* Its a V6 socket */
5491                 in_inp = (struct inpcb *)inp;
5492                 stc.ipv6_addr_legal = 1;
5493                 /* Now look at the binding flag to see if V4 will be legal */
5494                 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5495                         stc.ipv4_addr_legal = 1;
5496                 } else {
5497                         /* V4 addresses are NOT legal on the association */
5498                         stc.ipv4_addr_legal = 0;
5499                 }
5500         } else {
5501                 /* Its a V4 socket, no - V6 */
5502                 stc.ipv4_addr_legal = 1;
5503                 stc.ipv6_addr_legal = 0;
5504         }
5505
5506 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5507         stc.ipv4_scope = 1;
5508 #else
5509         stc.ipv4_scope = 0;
5510 #endif
5511         /* now for scope setup */
5512         memset((caddr_t)&store, 0, sizeof(store));
5513         memset((caddr_t)&store1, 0, sizeof(store1));
5514 #ifdef INET
5515         sin = &store.sin;
5516         to_sin = &store1.sin;
5517 #endif
5518 #ifdef INET6
5519         sin6 = &store.sin6;
5520         to_sin6 = &store1.sin6;
5521 #endif
5522         iph = mtod(init_pkt, struct ip *);
5523         /* establish the to_addr's */
5524         switch (iph->ip_v) {
5525 #ifdef INET
5526         case IPVERSION:
5527                 to_sin->sin_port = sh->dest_port;
5528                 to_sin->sin_family = AF_INET;
5529                 to_sin->sin_len = sizeof(struct sockaddr_in);
5530                 to_sin->sin_addr = iph->ip_dst;
5531                 break;
5532 #endif
5533 #ifdef INET6
5534         case IPV6_VERSION >> 4:
5535                 ip6 = mtod(init_pkt, struct ip6_hdr *);
5536                 to_sin6->sin6_addr = ip6->ip6_dst;
5537                 to_sin6->sin6_scope_id = 0;
5538                 to_sin6->sin6_port = sh->dest_port;
5539                 to_sin6->sin6_family = AF_INET6;
5540                 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5541                 break;
5542 #endif
5543         default:
5544                 goto do_a_abort;
5545                 break;
5546         }
5547
5548         if (net == NULL) {
5549                 to = (struct sockaddr *)&store;
5550                 switch (iph->ip_v) {
5551 #ifdef INET
5552                 case IPVERSION:
5553                         {
5554                                 sin->sin_family = AF_INET;
5555                                 sin->sin_len = sizeof(struct sockaddr_in);
5556                                 sin->sin_port = sh->src_port;
5557                                 sin->sin_addr = iph->ip_src;
5558                                 /* lookup address */
5559                                 stc.address[0] = sin->sin_addr.s_addr;
5560                                 stc.address[1] = 0;
5561                                 stc.address[2] = 0;
5562                                 stc.address[3] = 0;
5563                                 stc.addr_type = SCTP_IPV4_ADDRESS;
5564                                 /* local from address */
5565                                 stc.laddress[0] = to_sin->sin_addr.s_addr;
5566                                 stc.laddress[1] = 0;
5567                                 stc.laddress[2] = 0;
5568                                 stc.laddress[3] = 0;
5569                                 stc.laddr_type = SCTP_IPV4_ADDRESS;
5570                                 /* scope_id is only for v6 */
5571                                 stc.scope_id = 0;
5572 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5573                                 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5574                                         stc.ipv4_scope = 1;
5575                                 }
5576 #else
5577                                 stc.ipv4_scope = 1;
5578 #endif                          /* SCTP_DONT_DO_PRIVADDR_SCOPE */
5579                                 /* Must use the address in this case */
5580                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5581                                         stc.loopback_scope = 1;
5582                                         stc.ipv4_scope = 1;
5583                                         stc.site_scope = 1;
5584                                         stc.local_scope = 0;
5585                                 }
5586                                 break;
5587                         }
5588 #endif
5589 #ifdef INET6
5590                 case IPV6_VERSION >> 4:
5591                         {
5592                                 ip6 = mtod(init_pkt, struct ip6_hdr *);
5593                                 sin6->sin6_family = AF_INET6;
5594                                 sin6->sin6_len = sizeof(struct sockaddr_in6);
5595                                 sin6->sin6_port = sh->src_port;
5596                                 sin6->sin6_addr = ip6->ip6_src;
5597                                 /* lookup address */
5598                                 memcpy(&stc.address, &sin6->sin6_addr,
5599                                     sizeof(struct in6_addr));
5600                                 sin6->sin6_scope_id = 0;
5601                                 stc.addr_type = SCTP_IPV6_ADDRESS;
5602                                 stc.scope_id = 0;
5603                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5604                                         /*
5605                                          * FIX ME: does this have scope from
5606                                          * rcvif?
5607                                          */
5608                                         (void)sa6_recoverscope(sin6);
5609                                         stc.scope_id = sin6->sin6_scope_id;
5610                                         sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5611                                         stc.loopback_scope = 1;
5612                                         stc.local_scope = 0;
5613                                         stc.site_scope = 1;
5614                                         stc.ipv4_scope = 1;
5615                                 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5616                                         /*
5617                                          * If the new destination is a
5618                                          * LINK_LOCAL we must have common
5619                                          * both site and local scope. Don't
5620                                          * set local scope though since we
5621                                          * must depend on the source to be
5622                                          * added implicitly. We cannot
5623                                          * assure just because we share one
5624                                          * link that all links are common.
5625                                          */
5626                                         stc.local_scope = 0;
5627                                         stc.site_scope = 1;
5628                                         stc.ipv4_scope = 1;
5629                                         /*
5630                                          * we start counting for the private
5631                                          * address stuff at 1. since the
5632                                          * link local we source from won't
5633                                          * show up in our scoped count.
5634                                          */
5635                                         cnt_inits_to = 1;
5636                                         /*
5637                                          * pull out the scope_id from
5638                                          * incoming pkt
5639                                          */
5640                                         /*
5641                                          * FIX ME: does this have scope from
5642                                          * rcvif?
5643                                          */
5644                                         (void)sa6_recoverscope(sin6);
5645                                         stc.scope_id = sin6->sin6_scope_id;
5646                                         sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5647                                 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5648                                         /*
5649                                          * If the new destination is
5650                                          * SITE_LOCAL then we must have site
5651                                          * scope in common.
5652                                          */
5653                                         stc.site_scope = 1;
5654                                 }
5655                                 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5656                                 stc.laddr_type = SCTP_IPV6_ADDRESS;
5657                                 break;
5658                         }
5659 #endif
5660                 default:
5661                         /* TSNH */
5662                         goto do_a_abort;
5663                         break;
5664                 }
5665         } else {
5666                 /* set the scope per the existing tcb */
5667
5668 #ifdef INET6
5669                 struct sctp_nets *lnet;
5670
5671 #endif
5672
5673                 stc.loopback_scope = asoc->loopback_scope;
5674                 stc.ipv4_scope = asoc->ipv4_local_scope;
5675                 stc.site_scope = asoc->site_scope;
5676                 stc.local_scope = asoc->local_scope;
5677 #ifdef INET6
5678                 /* Why do we not consider IPv4 LL addresses? */
5679                 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5680                         if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5681                                 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5682                                         /*
5683                                          * if we have a LL address, start
5684                                          * counting at 1.
5685                                          */
5686                                         cnt_inits_to = 1;
5687                                 }
5688                         }
5689                 }
5690 #endif
5691                 /* use the net pointer */
5692                 to = (struct sockaddr *)&net->ro._l_addr;
5693                 switch (to->sa_family) {
5694 #ifdef INET
5695                 case AF_INET:
5696                         sin = (struct sockaddr_in *)to;
5697                         stc.address[0] = sin->sin_addr.s_addr;
5698                         stc.address[1] = 0;
5699                         stc.address[2] = 0;
5700                         stc.address[3] = 0;
5701                         stc.addr_type = SCTP_IPV4_ADDRESS;
5702                         if (net->src_addr_selected == 0) {
5703                                 /*
5704                                  * strange case here, the INIT should have
5705                                  * did the selection.
5706                                  */
5707                                 net->ro._s_addr = sctp_source_address_selection(inp,
5708                                     stcb, (sctp_route_t *) & net->ro,
5709                                     net, 0, vrf_id);
5710                                 if (net->ro._s_addr == NULL)
5711                                         return;
5712
5713                                 net->src_addr_selected = 1;
5714
5715                         }
5716                         stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5717                         stc.laddress[1] = 0;
5718                         stc.laddress[2] = 0;
5719                         stc.laddress[3] = 0;
5720                         stc.laddr_type = SCTP_IPV4_ADDRESS;
5721                         /* scope_id is only for v6 */
5722                         stc.scope_id = 0;
5723                         break;
5724 #endif
5725 #ifdef INET6
5726                 case AF_INET6:
5727                         sin6 = (struct sockaddr_in6 *)to;
5728                         memcpy(&stc.address, &sin6->sin6_addr,
5729                             sizeof(struct in6_addr));
5730                         stc.addr_type = SCTP_IPV6_ADDRESS;
5731                         stc.scope_id = sin6->sin6_scope_id;
5732                         if (net->src_addr_selected == 0) {
5733                                 /*
5734                                  * strange case here, the INIT should have
5735                                  * did the selection.
5736                                  */
5737                                 net->ro._s_addr = sctp_source_address_selection(inp,
5738                                     stcb, (sctp_route_t *) & net->ro,
5739                                     net, 0, vrf_id);
5740                                 if (net->ro._s_addr == NULL)
5741                                         return;
5742
5743                                 net->src_addr_selected = 1;
5744                         }
5745                         memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5746                             sizeof(struct in6_addr));
5747                         stc.laddr_type = SCTP_IPV6_ADDRESS;
5748                         break;
5749 #endif
5750                 }
5751         }
5752         /* Now lets put the SCTP header in place */
5753         initack = mtod(m, struct sctp_init_ack_chunk *);
5754         /* Save it off for quick ref */
5755         stc.peers_vtag = init_chk->init.initiate_tag;
5756         /* who are we */
5757         memcpy(stc.identification, SCTP_VERSION_STRING,
5758             min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5759         memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
5760         /* now the chunk header */
5761         initack->ch.chunk_type = SCTP_INITIATION_ACK;
5762         initack->ch.chunk_flags = 0;
5763         /* fill in later from mbuf we build */
5764         initack->ch.chunk_length = 0;
5765         /* place in my tag */
5766         if ((asoc != NULL) &&
5767             ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5768             (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5769             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5770                 /* re-use the v-tags and init-seq here */
5771                 initack->init.initiate_tag = htonl(asoc->my_vtag);
5772                 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5773         } else {
5774                 uint32_t vtag, itsn;
5775
5776                 if (hold_inp_lock) {
5777                         SCTP_INP_INCR_REF(inp);
5778                         SCTP_INP_RUNLOCK(inp);
5779                 }
5780                 if (asoc) {
5781                         atomic_add_int(&asoc->refcnt, 1);
5782                         SCTP_TCB_UNLOCK(stcb);
5783         new_tag:
5784                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5785                         if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5786                                 /*
5787                                  * Got a duplicate vtag on some guy behind a
5788                                  * nat make sure we don't use it.
5789                                  */
5790                                 goto new_tag;
5791                         }
5792                         initack->init.initiate_tag = htonl(vtag);
5793                         /* get a TSN to use too */
5794                         itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5795                         initack->init.initial_tsn = htonl(itsn);
5796                         SCTP_TCB_LOCK(stcb);
5797                         atomic_add_int(&asoc->refcnt, -1);
5798                 } else {
5799                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5800                         initack->init.initiate_tag = htonl(vtag);
5801                         /* get a TSN to use too */
5802                         initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5803                 }
5804                 if (hold_inp_lock) {
5805                         SCTP_INP_RLOCK(inp);
5806                         SCTP_INP_DECR_REF(inp);
5807                 }
5808         }
5809         /* save away my tag to */
5810         stc.my_vtag = initack->init.initiate_tag;
5811
5812         /* set up some of the credits. */
5813         so = inp->sctp_socket;
5814         if (so == NULL) {
5815                 /* memory problem */
5816                 sctp_m_freem(m);
5817                 return;
5818         } else {
5819                 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5820         }
5821         /* set what I want */
5822         his_limit = ntohs(init_chk->init.num_inbound_streams);
5823         /* choose what I want */
5824         if (asoc != NULL) {
5825                 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5826                         i_want = asoc->streamoutcnt;
5827                 } else {
5828                         i_want = inp->sctp_ep.pre_open_stream_count;
5829                 }
5830         } else {
5831                 i_want = inp->sctp_ep.pre_open_stream_count;
5832         }
5833         if (his_limit < i_want) {
5834                 /* I Want more :< */
5835                 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5836         } else {
5837                 /* I can have what I want :> */
5838                 initack->init.num_outbound_streams = htons(i_want);
5839         }
5840         /* tell him his limt. */
5841         initack->init.num_inbound_streams =
5842             htons(inp->sctp_ep.max_open_streams_intome);
5843
5844         /* adaptation layer indication parameter */
5845         ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5846         ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5847         ali->ph.param_length = htons(sizeof(*ali));
5848         ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5849         SCTP_BUF_LEN(m) += sizeof(*ali);
5850         ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5851
5852         /* ECN parameter */
5853         if (((asoc != NULL) && (asoc->ecn_allowed == 1)) ||
5854             (inp->sctp_ecn_enable == 1)) {
5855                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5856                 ecn->ph.param_length = htons(sizeof(*ecn));
5857                 SCTP_BUF_LEN(m) += sizeof(*ecn);
5858
5859                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5860                     sizeof(*ecn));
5861         } else {
5862                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5863         }
5864         /* And now tell the peer we do  pr-sctp */
5865         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5866         prsctp->ph.param_length = htons(sizeof(*prsctp));
5867         SCTP_BUF_LEN(m) += sizeof(*prsctp);
5868         if (nat_friendly) {
5869                 /* Add NAT friendly parameter */
5870                 struct sctp_paramhdr *ph;
5871
5872                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5873                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5874                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
5875                 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5876         }
5877         /* And now tell the peer we do all the extensions */
5878         pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5879         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5880         num_ext = 0;
5881         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5882         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5883         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5884         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5885         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5886         if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5887                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5888         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5889                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5890         p_len = sizeof(*pr_supported) + num_ext;
5891         pr_supported->ph.param_length = htons(p_len);
5892         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5893         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5894
5895         /* add authentication parameters */
5896         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5897                 struct sctp_auth_random *randp;
5898                 struct sctp_auth_hmac_algo *hmacs;
5899                 struct sctp_auth_chunk_list *chunks;
5900                 uint16_t random_len;
5901
5902                 /* generate and add RANDOM parameter */
5903                 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5904                 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5905                 randp->ph.param_type = htons(SCTP_RANDOM);
5906                 p_len = sizeof(*randp) + random_len;
5907                 randp->ph.param_length = htons(p_len);
5908                 SCTP_READ_RANDOM(randp->random_data, random_len);
5909                 /* zero out any padding required */
5910                 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5911                 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5912
5913                 /* add HMAC_ALGO parameter */
5914                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5915                 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5916                     (uint8_t *) hmacs->hmac_ids);
5917                 if (p_len > 0) {
5918                         p_len += sizeof(*hmacs);
5919                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5920                         hmacs->ph.param_length = htons(p_len);
5921                         /* zero out any padding required */
5922                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5923                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5924                 }
5925                 /* add CHUNKS parameter */
5926                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5927                 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5928                     chunks->chunk_types);
5929                 if (p_len > 0) {
5930                         p_len += sizeof(*chunks);
5931                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5932                         chunks->ph.param_length = htons(p_len);
5933                         /* zero out any padding required */
5934                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5935                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5936                 }
5937         }
5938         m_at = m;
5939         /* now the addresses */
5940         {
5941                 struct sctp_scoping scp;
5942
5943                 /*
5944                  * To optimize this we could put the scoping stuff into a
5945                  * structure and remove the individual uint8's from the stc
5946                  * structure. Then we could just sifa in the address within
5947                  * the stc.. but for now this is a quick hack to get the
5948                  * address stuff teased apart.
5949                  */
5950                 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5951                 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5952                 scp.loopback_scope = stc.loopback_scope;
5953                 scp.ipv4_local_scope = stc.ipv4_scope;
5954                 scp.local_scope = stc.local_scope;
5955                 scp.site_scope = stc.site_scope;
5956                 m_at = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_at, cnt_inits_to);
5957         }
5958
5959         /* tack on the operational error if present */
5960         if (op_err) {
5961                 struct mbuf *ol;
5962                 int llen;
5963
5964                 llen = 0;
5965                 ol = op_err;
5966
5967                 while (ol) {
5968                         llen += SCTP_BUF_LEN(ol);
5969                         ol = SCTP_BUF_NEXT(ol);
5970                 }
5971                 if (llen % 4) {
5972                         /* must add a pad to the param */
5973                         uint32_t cpthis = 0;
5974                         int padlen;
5975
5976                         padlen = 4 - (llen % 4);
5977                         m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5978                 }
5979                 while (SCTP_BUF_NEXT(m_at) != NULL) {
5980                         m_at = SCTP_BUF_NEXT(m_at);
5981                 }
5982                 SCTP_BUF_NEXT(m_at) = op_err;
5983                 while (SCTP_BUF_NEXT(m_at) != NULL) {
5984                         m_at = SCTP_BUF_NEXT(m_at);
5985                 }
5986         }
5987         /* pre-calulate the size and update pkt header and chunk header */
5988         p_len = 0;
5989         for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5990                 p_len += SCTP_BUF_LEN(m_tmp);
5991                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5992                         /* m_tmp should now point to last one */
5993                         break;
5994                 }
5995         }
5996
5997         /* Now we must build a cookie */
5998         m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
5999         if (m_cookie == NULL) {
6000                 /* memory problem */
6001                 sctp_m_freem(m);
6002                 return;
6003         }
6004         /* Now append the cookie to the end and update the space/size */
6005         SCTP_BUF_NEXT(m_tmp) = m_cookie;
6006
6007         for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6008                 p_len += SCTP_BUF_LEN(m_tmp);
6009                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6010                         /* m_tmp should now point to last one */
6011                         mp_last = m_tmp;
6012                         break;
6013                 }
6014         }
6015         /*
6016          * Place in the size, but we don't include the last pad (if any) in
6017          * the INIT-ACK.
6018          */
6019         initack->ch.chunk_length = htons(p_len);
6020
6021         /*
6022          * Time to sign the cookie, we don't sign over the cookie signature
6023          * though thus we set trailer.
6024          */
6025         (void)sctp_hmac_m(SCTP_HMAC,
6026             (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
6027             SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
6028             (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
6029         /*
6030          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6031          * here since the timer will drive a retranmission.
6032          */
6033         padval = p_len % 4;
6034         if ((padval) && (mp_last)) {
6035                 /* see my previous comments on mp_last */
6036                 if (sctp_add_pad_tombuf(mp_last, (4 - padval))) {
6037                         /* Houston we have a problem, no space */
6038                         sctp_m_freem(m);
6039                         return;
6040                 }
6041         }
6042         if (stc.loopback_scope) {
6043                 over_addr = &store1;
6044         } else {
6045                 over_addr = NULL;
6046         }
6047
6048         (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
6049             0, 0,
6050             inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
6051             port, SCTP_SO_NOT_LOCKED, over_addr, init_pkt);
6052         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6053 }
6054
6055
6056 static void
6057 sctp_prune_prsctp(struct sctp_tcb *stcb,
6058     struct sctp_association *asoc,
6059     struct sctp_sndrcvinfo *srcv,
6060     int dataout)
6061 {
6062         int freed_spc = 0;
6063         struct sctp_tmit_chunk *chk, *nchk;
6064
6065         SCTP_TCB_LOCK_ASSERT(stcb);
6066         if ((asoc->peer_supports_prsctp) &&
6067             (asoc->sent_queue_cnt_removeable > 0)) {
6068                 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6069                         /*
6070                          * Look for chunks marked with the PR_SCTP flag AND
6071                          * the buffer space flag. If the one being sent is
6072                          * equal or greater priority then purge the old one
6073                          * and free some space.
6074                          */
6075                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6076                                 /*
6077                                  * This one is PR-SCTP AND buffer space
6078                                  * limited type
6079                                  */
6080                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
6081                                         /*
6082                                          * Lower numbers equates to higher
6083                                          * priority so if the one we are
6084                                          * looking at has a larger or equal
6085                                          * priority we want to drop the data
6086                                          * and NOT retransmit it.
6087                                          */
6088                                         if (chk->data) {
6089                                                 /*
6090                                                  * We release the book_size
6091                                                  * if the mbuf is here
6092                                                  */
6093                                                 int ret_spc;
6094                                                 uint8_t sent;
6095
6096                                                 if (chk->sent > SCTP_DATAGRAM_UNSENT)
6097                                                         sent = 1;
6098                                                 else
6099                                                         sent = 0;
6100                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6101                                                     sent,
6102                                                     SCTP_SO_LOCKED);
6103                                                 freed_spc += ret_spc;
6104                                                 if (freed_spc >= dataout) {
6105                                                         return;
6106                                                 }
6107                                         }       /* if chunk was present */
6108                                 }       /* if of sufficent priority */
6109                         }       /* if chunk has enabled */
6110                 }               /* tailqforeach */
6111
6112                 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
6113                         /* Here we must move to the sent queue and mark */
6114                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6115                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
6116                                         if (chk->data) {
6117                                                 /*
6118                                                  * We release the book_size
6119                                                  * if the mbuf is here
6120                                                  */
6121                                                 int ret_spc;
6122
6123                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6124                                                     0, SCTP_SO_LOCKED);
6125
6126                                                 freed_spc += ret_spc;
6127                                                 if (freed_spc >= dataout) {
6128                                                         return;
6129                                                 }
6130                                         }       /* end if chk->data */
6131                                 }       /* end if right class */
6132                         }       /* end if chk pr-sctp */
6133                 }               /* tailqforeachsafe (chk) */
6134         }                       /* if enabled in asoc */
6135 }
6136
6137 int
6138 sctp_get_frag_point(struct sctp_tcb *stcb,
6139     struct sctp_association *asoc)
6140 {
6141         int siz, ovh;
6142
6143         /*
6144          * For endpoints that have both v6 and v4 addresses we must reserve
6145          * room for the ipv6 header, for those that are only dealing with V4
6146          * we use a larger frag point.
6147          */
6148         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6149                 ovh = SCTP_MED_OVERHEAD;
6150         } else {
6151                 ovh = SCTP_MED_V4_OVERHEAD;
6152         }
6153
6154         if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
6155                 siz = asoc->smallest_mtu - ovh;
6156         else
6157                 siz = (stcb->asoc.sctp_frag_point - ovh);
6158         /*
6159          * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
6160          */
6161         /* A data chunk MUST fit in a cluster */
6162         /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
6163         /* } */
6164
6165         /* adjust for an AUTH chunk if DATA requires auth */
6166         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
6167                 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
6168
6169         if (siz % 4) {
6170                 /* make it an even word boundary please */
6171                 siz -= (siz % 4);
6172         }
6173         return (siz);
6174 }
6175
6176 static void
6177 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
6178 {
6179         sp->pr_sctp_on = 0;
6180         /*
6181          * We assume that the user wants PR_SCTP_TTL if the user provides a
6182          * positive lifetime but does not specify any PR_SCTP policy. This
6183          * is a BAD assumption and causes problems at least with the
6184          * U-Vancovers MPI folks. I will change this to be no policy means
6185          * NO PR-SCTP.
6186          */
6187         if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
6188                 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6189                 sp->pr_sctp_on = 1;
6190         } else {
6191                 return;
6192         }
6193         switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
6194         case CHUNK_FLAGS_PR_SCTP_BUF:
6195                 /*
6196                  * Time to live is a priority stored in tv_sec when doing
6197                  * the buffer drop thing.
6198                  */
6199                 sp->ts.tv_sec = sp->timetolive;
6200                 sp->ts.tv_usec = 0;
6201                 break;
6202         case CHUNK_FLAGS_PR_SCTP_TTL:
6203                 {
6204                         struct timeval tv;
6205
6206                         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6207                         tv.tv_sec = sp->timetolive / 1000;
6208                         tv.tv_usec = (sp->timetolive * 1000) % 1000000;
6209                         /*
6210                          * TODO sctp_constants.h needs alternative time
6211                          * macros when _KERNEL is undefined.
6212                          */
6213                         timevaladd(&sp->ts, &tv);
6214                 }
6215                 break;
6216         case CHUNK_FLAGS_PR_SCTP_RTX:
6217                 /*
6218                  * Time to live is a the number or retransmissions stored in
6219                  * tv_sec.
6220                  */
6221                 sp->ts.tv_sec = sp->timetolive;
6222                 sp->ts.tv_usec = 0;
6223                 break;
6224         default:
6225                 SCTPDBG(SCTP_DEBUG_USRREQ1,
6226                     "Unknown PR_SCTP policy %u.\n",
6227                     PR_SCTP_POLICY(sp->sinfo_flags));
6228                 break;
6229         }
6230 }
6231
6232 static int
6233 sctp_msg_append(struct sctp_tcb *stcb,
6234     struct sctp_nets *net,
6235     struct mbuf *m,
6236     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
6237 {
6238         int error = 0;
6239         struct mbuf *at;
6240         struct sctp_stream_queue_pending *sp = NULL;
6241         struct sctp_stream_out *strm;
6242
6243         /*
6244          * Given an mbuf chain, put it into the association send queue and
6245          * place it on the wheel
6246          */
6247         if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
6248                 /* Invalid stream number */
6249                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6250                 error = EINVAL;
6251                 goto out_now;
6252         }
6253         if ((stcb->asoc.stream_locked) &&
6254             (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
6255                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6256                 error = EINVAL;
6257                 goto out_now;
6258         }
6259         strm = &stcb->asoc.strmout[srcv->sinfo_stream];
6260         /* Now can we send this? */
6261         if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
6262             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6263             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
6264             (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
6265                 /* got data while shutting down */
6266                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
6267                 error = ECONNRESET;
6268                 goto out_now;
6269         }
6270         sctp_alloc_a_strmoq(stcb, sp);
6271         if (sp == NULL) {
6272                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6273                 error = ENOMEM;
6274                 goto out_now;
6275         }
6276         sp->sinfo_flags = srcv->sinfo_flags;
6277         sp->timetolive = srcv->sinfo_timetolive;
6278         sp->ppid = srcv->sinfo_ppid;
6279         sp->context = srcv->sinfo_context;
6280         sp->strseq = 0;
6281         if (sp->sinfo_flags & SCTP_ADDR_OVER) {
6282                 sp->net = net;
6283                 atomic_add_int(&sp->net->ref_count, 1);
6284         } else {
6285                 sp->net = NULL;
6286         }
6287         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6288         sp->stream = srcv->sinfo_stream;
6289         sp->msg_is_complete = 1;
6290         sp->sender_all_done = 1;
6291         sp->some_taken = 0;
6292         sp->data = m;
6293         sp->tail_mbuf = NULL;
6294         sctp_set_prsctp_policy(sp);
6295         /*
6296          * We could in theory (for sendall) sifa the length in, but we would
6297          * still have to hunt through the chain since we need to setup the
6298          * tail_mbuf
6299          */
6300         sp->length = 0;
6301         for (at = m; at; at = SCTP_BUF_NEXT(at)) {
6302                 if (SCTP_BUF_NEXT(at) == NULL)
6303                         sp->tail_mbuf = at;
6304                 sp->length += SCTP_BUF_LEN(at);
6305         }
6306         if (srcv->sinfo_keynumber_valid) {
6307                 sp->auth_keyid = srcv->sinfo_keynumber;
6308         } else {
6309                 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
6310         }
6311         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
6312                 sctp_auth_key_acquire(stcb, sp->auth_keyid);
6313                 sp->holds_key_ref = 1;
6314         }
6315         if (hold_stcb_lock == 0) {
6316                 SCTP_TCB_SEND_LOCK(stcb);
6317         }
6318         sctp_snd_sb_alloc(stcb, sp->length);
6319         atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
6320         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
6321         if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
6322                 sp->strseq = strm->next_sequence_sent;
6323                 strm->next_sequence_sent++;
6324         }
6325         stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp, 1);
6326         m = NULL;
6327         if (hold_stcb_lock == 0) {
6328                 SCTP_TCB_SEND_UNLOCK(stcb);
6329         }
6330 out_now:
6331         if (m) {
6332                 sctp_m_freem(m);
6333         }
6334         return (error);
6335 }
6336
6337
6338 static struct mbuf *
6339 sctp_copy_mbufchain(struct mbuf *clonechain,
6340     struct mbuf *outchain,
6341     struct mbuf **endofchain,
6342     int can_take_mbuf,
6343     int sizeofcpy,
6344     uint8_t copy_by_ref)
6345 {
6346         struct mbuf *m;
6347         struct mbuf *appendchain;
6348         caddr_t cp;
6349         int len;
6350
6351         if (endofchain == NULL) {
6352                 /* error */
6353 error_out:
6354                 if (outchain)
6355                         sctp_m_freem(outchain);
6356                 return (NULL);
6357         }
6358         if (can_take_mbuf) {
6359                 appendchain = clonechain;
6360         } else {
6361                 if (!copy_by_ref &&
6362                     (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
6363                     ) {
6364                         /* Its not in a cluster */
6365                         if (*endofchain == NULL) {
6366                                 /* lets get a mbuf cluster */
6367                                 if (outchain == NULL) {
6368                                         /* This is the general case */
6369                         new_mbuf:
6370                                         outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6371                                         if (outchain == NULL) {
6372                                                 goto error_out;
6373                                         }
6374                                         SCTP_BUF_LEN(outchain) = 0;
6375                                         *endofchain = outchain;
6376                                         /* get the prepend space */
6377                                         SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6378                                 } else {
6379                                         /*
6380                                          * We really should not get a NULL
6381                                          * in endofchain
6382                                          */
6383                                         /* find end */
6384                                         m = outchain;
6385                                         while (m) {
6386                                                 if (SCTP_BUF_NEXT(m) == NULL) {
6387                                                         *endofchain = m;
6388                                                         break;
6389                                                 }
6390                                                 m = SCTP_BUF_NEXT(m);
6391                                         }
6392                                         /* sanity */
6393                                         if (*endofchain == NULL) {
6394                                                 /*
6395                                                  * huh, TSNH XXX maybe we
6396                                                  * should panic
6397                                                  */
6398                                                 sctp_m_freem(outchain);
6399                                                 goto new_mbuf;
6400                                         }
6401                                 }
6402                                 /* get the new end of length */
6403                                 len = M_TRAILINGSPACE(*endofchain);
6404                         } else {
6405                                 /* how much is left at the end? */
6406                                 len = M_TRAILINGSPACE(*endofchain);
6407                         }
6408                         /* Find the end of the data, for appending */
6409                         cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6410
6411                         /* Now lets copy it out */
6412                         if (len >= sizeofcpy) {
6413                                 /* It all fits, copy it in */
6414                                 m_copydata(clonechain, 0, sizeofcpy, cp);
6415                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6416                         } else {
6417                                 /* fill up the end of the chain */
6418                                 if (len > 0) {
6419                                         m_copydata(clonechain, 0, len, cp);
6420                                         SCTP_BUF_LEN((*endofchain)) += len;
6421                                         /* now we need another one */
6422                                         sizeofcpy -= len;
6423                                 }
6424                                 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6425                                 if (m == NULL) {
6426                                         /* We failed */
6427                                         goto error_out;
6428                                 }
6429                                 SCTP_BUF_NEXT((*endofchain)) = m;
6430                                 *endofchain = m;
6431                                 cp = mtod((*endofchain), caddr_t);
6432                                 m_copydata(clonechain, len, sizeofcpy, cp);
6433                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6434                         }
6435                         return (outchain);
6436                 } else {
6437                         /* copy the old fashion way */
6438                         appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6439 #ifdef SCTP_MBUF_LOGGING
6440                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6441                                 struct mbuf *mat;
6442
6443                                 for (mat = appendchain; mat; mat = SCTP_BUF_NEXT(mat)) {
6444                                         if (SCTP_BUF_IS_EXTENDED(mat)) {
6445                                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6446                                         }
6447                                 }
6448                         }
6449 #endif
6450                 }
6451         }
6452         if (appendchain == NULL) {
6453                 /* error */
6454                 if (outchain)
6455                         sctp_m_freem(outchain);
6456                 return (NULL);
6457         }
6458         if (outchain) {
6459                 /* tack on to the end */
6460                 if (*endofchain != NULL) {
6461                         SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6462                 } else {
6463                         m = outchain;
6464                         while (m) {
6465                                 if (SCTP_BUF_NEXT(m) == NULL) {
6466                                         SCTP_BUF_NEXT(m) = appendchain;
6467                                         break;
6468                                 }
6469                                 m = SCTP_BUF_NEXT(m);
6470                         }
6471                 }
6472                 /*
6473                  * save off the end and update the end-chain postion
6474                  */
6475                 m = appendchain;
6476                 while (m) {
6477                         if (SCTP_BUF_NEXT(m) == NULL) {
6478                                 *endofchain = m;
6479                                 break;
6480                         }
6481                         m = SCTP_BUF_NEXT(m);
6482                 }
6483                 return (outchain);
6484         } else {
6485                 /* save off the end and update the end-chain postion */
6486                 m = appendchain;
6487                 while (m) {
6488                         if (SCTP_BUF_NEXT(m) == NULL) {
6489                                 *endofchain = m;
6490                                 break;
6491                         }
6492                         m = SCTP_BUF_NEXT(m);
6493                 }
6494                 return (appendchain);
6495         }
6496 }
6497
6498 int
6499 sctp_med_chunk_output(struct sctp_inpcb *inp,
6500     struct sctp_tcb *stcb,
6501     struct sctp_association *asoc,
6502     int *num_out,
6503     int *reason_code,
6504     int control_only, int from_where,
6505     struct timeval *now, int *now_filled, int frag_point, int so_locked
6506 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6507     SCTP_UNUSED
6508 #endif
6509 );
6510
6511 static void
6512 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6513     uint32_t val SCTP_UNUSED)
6514 {
6515         struct sctp_copy_all *ca;
6516         struct mbuf *m;
6517         int ret = 0;
6518         int added_control = 0;
6519         int un_sent, do_chunk_output = 1;
6520         struct sctp_association *asoc;
6521         struct sctp_nets *net;
6522
6523         ca = (struct sctp_copy_all *)ptr;
6524         if (ca->m == NULL) {
6525                 return;
6526         }
6527         if (ca->inp != inp) {
6528                 /* TSNH */
6529                 return;
6530         }
6531         if ((ca->m) && ca->sndlen) {
6532                 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6533                 if (m == NULL) {
6534                         /* can't copy so we are done */
6535                         ca->cnt_failed++;
6536                         return;
6537                 }
6538 #ifdef SCTP_MBUF_LOGGING
6539                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6540                         struct mbuf *mat;
6541
6542                         for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6543                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
6544                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6545                                 }
6546                         }
6547                 }
6548 #endif
6549         } else {
6550                 m = NULL;
6551         }
6552         SCTP_TCB_LOCK_ASSERT(stcb);
6553         if (stcb->asoc.alternate) {
6554                 net = stcb->asoc.alternate;
6555         } else {
6556                 net = stcb->asoc.primary_destination;
6557         }
6558         if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6559                 /* Abort this assoc with m as the user defined reason */
6560                 if (m) {
6561                         struct sctp_paramhdr *ph;
6562
6563                         SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6564                         if (m) {
6565                                 ph = mtod(m, struct sctp_paramhdr *);
6566                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6567                                 ph->param_length = htons(ca->sndlen);
6568                         }
6569                         /*
6570                          * We add one here to keep the assoc from
6571                          * dis-appearing on us.
6572                          */
6573                         atomic_add_int(&stcb->asoc.refcnt, 1);
6574                         sctp_abort_an_association(inp, stcb, 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                                                     NULL, SCTP_SO_NOT_LOCKED);
6669                                                 atomic_add_int(&stcb->asoc.refcnt, -1);
6670                                                 goto no_chunk_output;
6671                                         }
6672                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6673                                             asoc->primary_destination);
6674                                 }
6675                         }
6676
6677                 }
6678         }
6679         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6680             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6681
6682         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6683             (stcb->asoc.total_flight > 0) &&
6684             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6685             ) {
6686                 do_chunk_output = 0;
6687         }
6688         if (do_chunk_output)
6689                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6690         else if (added_control) {
6691                 int num_out = 0, reason = 0, now_filled = 0;
6692                 struct timeval now;
6693                 int frag_point;
6694
6695                 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6696                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6697                     &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6698         }
6699 no_chunk_output:
6700         if (ret) {
6701                 ca->cnt_failed++;
6702         } else {
6703                 ca->cnt_sent++;
6704         }
6705 }
6706
6707 static void
6708 sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
6709 {
6710         struct sctp_copy_all *ca;
6711
6712         ca = (struct sctp_copy_all *)ptr;
6713         /*
6714          * Do a notify here? Kacheong suggests that the notify be done at
6715          * the send time.. so you would push up a notification if any send
6716          * failed. Don't know if this is feasable since the only failures we
6717          * have is "memory" related and if you cannot get an mbuf to send
6718          * the data you surely can't get an mbuf to send up to notify the
6719          * user you can't send the data :->
6720          */
6721
6722         /* now free everything */
6723         sctp_m_freem(ca->m);
6724         SCTP_FREE(ca, SCTP_M_COPYAL);
6725 }
6726
6727
6728 #define MC_ALIGN(m, len) do {                                           \
6729         SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
6730 } while (0)
6731
6732
6733
6734 static struct mbuf *
6735 sctp_copy_out_all(struct uio *uio, int len)
6736 {
6737         struct mbuf *ret, *at;
6738         int left, willcpy, cancpy, error;
6739
6740         ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6741         if (ret == NULL) {
6742                 /* TSNH */
6743                 return (NULL);
6744         }
6745         left = len;
6746         SCTP_BUF_LEN(ret) = 0;
6747         /* save space for the data chunk header */
6748         cancpy = M_TRAILINGSPACE(ret);
6749         willcpy = min(cancpy, left);
6750         at = ret;
6751         while (left > 0) {
6752                 /* Align data to the end */
6753                 error = uiomove(mtod(at, caddr_t), willcpy, uio);
6754                 if (error) {
6755         err_out_now:
6756                         sctp_m_freem(at);
6757                         return (NULL);
6758                 }
6759                 SCTP_BUF_LEN(at) = willcpy;
6760                 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6761                 left -= willcpy;
6762                 if (left > 0) {
6763                         SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6764                         if (SCTP_BUF_NEXT(at) == NULL) {
6765                                 goto err_out_now;
6766                         }
6767                         at = SCTP_BUF_NEXT(at);
6768                         SCTP_BUF_LEN(at) = 0;
6769                         cancpy = M_TRAILINGSPACE(at);
6770                         willcpy = min(cancpy, left);
6771                 }
6772         }
6773         return (ret);
6774 }
6775
6776 static int
6777 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6778     struct sctp_sndrcvinfo *srcv)
6779 {
6780         int ret;
6781         struct sctp_copy_all *ca;
6782
6783         SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6784             SCTP_M_COPYAL);
6785         if (ca == NULL) {
6786                 sctp_m_freem(m);
6787                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6788                 return (ENOMEM);
6789         }
6790         memset(ca, 0, sizeof(struct sctp_copy_all));
6791
6792         ca->inp = inp;
6793         if (srcv) {
6794                 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6795         }
6796         /*
6797          * take off the sendall flag, it would be bad if we failed to do
6798          * this :-0
6799          */
6800         ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6801         /* get length and mbuf chain */
6802         if (uio) {
6803                 ca->sndlen = uio->uio_resid;
6804                 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6805                 if (ca->m == NULL) {
6806                         SCTP_FREE(ca, SCTP_M_COPYAL);
6807                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6808                         return (ENOMEM);
6809                 }
6810         } else {
6811                 /* Gather the length of the send */
6812                 struct mbuf *mat;
6813
6814                 mat = m;
6815                 ca->sndlen = 0;
6816                 while (m) {
6817                         ca->sndlen += SCTP_BUF_LEN(m);
6818                         m = SCTP_BUF_NEXT(m);
6819                 }
6820                 ca->m = mat;
6821         }
6822         ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6823             SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6824             SCTP_ASOC_ANY_STATE,
6825             (void *)ca, 0,
6826             sctp_sendall_completes, inp, 1);
6827         if (ret) {
6828                 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6829                 SCTP_FREE(ca, SCTP_M_COPYAL);
6830                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6831                 return (EFAULT);
6832         }
6833         return (0);
6834 }
6835
6836
6837 void
6838 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6839 {
6840         struct sctp_tmit_chunk *chk, *nchk;
6841
6842         TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6843                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6844                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6845                         if (chk->data) {
6846                                 sctp_m_freem(chk->data);
6847                                 chk->data = NULL;
6848                         }
6849                         asoc->ctrl_queue_cnt--;
6850                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6851                 }
6852         }
6853 }
6854
6855 void
6856 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6857 {
6858         struct sctp_association *asoc;
6859         struct sctp_tmit_chunk *chk, *nchk;
6860         struct sctp_asconf_chunk *acp;
6861
6862         asoc = &stcb->asoc;
6863         TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6864                 /* find SCTP_ASCONF chunk in queue */
6865                 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6866                         if (chk->data) {
6867                                 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6868                                 if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6869                                         /* Not Acked yet */
6870                                         break;
6871                                 }
6872                         }
6873                         TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6874                         if (chk->data) {
6875                                 sctp_m_freem(chk->data);
6876                                 chk->data = NULL;
6877                         }
6878                         asoc->ctrl_queue_cnt--;
6879                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6880                 }
6881         }
6882 }
6883
6884
6885 static void
6886 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6887     struct sctp_association *asoc,
6888     struct sctp_tmit_chunk **data_list,
6889     int bundle_at,
6890     struct sctp_nets *net)
6891 {
6892         int i;
6893         struct sctp_tmit_chunk *tp1;
6894
6895         for (i = 0; i < bundle_at; i++) {
6896                 /* off of the send queue */
6897                 TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
6898                 asoc->send_queue_cnt--;
6899                 if (i > 0) {
6900                         /*
6901                          * Any chunk NOT 0 you zap the time chunk 0 gets
6902                          * zapped or set based on if a RTO measurment is
6903                          * needed.
6904                          */
6905                         data_list[i]->do_rtt = 0;
6906                 }
6907                 /* record time */
6908                 data_list[i]->sent_rcv_time = net->last_sent_time;
6909                 data_list[i]->rec.data.cwnd_at_send = net->cwnd;
6910                 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6911                 if (data_list[i]->whoTo == NULL) {
6912                         data_list[i]->whoTo = net;
6913                         atomic_add_int(&net->ref_count, 1);
6914                 }
6915                 /* on to the sent queue */
6916                 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6917                 if ((tp1) && SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6918                         struct sctp_tmit_chunk *tpp;
6919
6920                         /* need to move back */
6921         back_up_more:
6922                         tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6923                         if (tpp == NULL) {
6924                                 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6925                                 goto all_done;
6926                         }
6927                         tp1 = tpp;
6928                         if (SCTP_TSN_GT(tp1->rec.data.TSN_seq, data_list[i]->rec.data.TSN_seq)) {
6929                                 goto back_up_more;
6930                         }
6931                         TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6932                 } else {
6933                         TAILQ_INSERT_TAIL(&asoc->sent_queue,
6934                             data_list[i],
6935                             sctp_next);
6936                 }
6937 all_done:
6938                 /* This does not lower until the cum-ack passes it */
6939                 asoc->sent_queue_cnt++;
6940                 if ((asoc->peers_rwnd <= 0) &&
6941                     (asoc->total_flight == 0) &&
6942                     (bundle_at == 1)) {
6943                         /* Mark the chunk as being a window probe */
6944                         SCTP_STAT_INCR(sctps_windowprobed);
6945                 }
6946 #ifdef SCTP_AUDITING_ENABLED
6947                 sctp_audit_log(0xC2, 3);
6948 #endif
6949                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6950                 data_list[i]->snd_count = 1;
6951                 data_list[i]->rec.data.chunk_was_revoked = 0;
6952                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6953                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6954                             data_list[i]->whoTo->flight_size,
6955                             data_list[i]->book_size,
6956                             (uintptr_t) data_list[i]->whoTo,
6957                             data_list[i]->rec.data.TSN_seq);
6958                 }
6959                 sctp_flight_size_increase(data_list[i]);
6960                 sctp_total_flight_increase(stcb, data_list[i]);
6961                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6962                         sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6963                             asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6964                 }
6965                 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6966                     (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6967                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6968                         /* SWS sender side engages */
6969                         asoc->peers_rwnd = 0;
6970                 }
6971         }
6972         if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
6973                 (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
6974         }
6975 }
6976
6977 static void
6978 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked
6979 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6980     SCTP_UNUSED
6981 #endif
6982 )
6983 {
6984         struct sctp_tmit_chunk *chk, *nchk;
6985
6986         TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6987                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6988                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
6989                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6990                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6991                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6992                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6993                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6994                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6995                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6996                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6997                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6998                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6999                         /* Stray chunks must be cleaned up */
7000         clean_up_anyway:
7001                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7002                         if (chk->data) {
7003                                 sctp_m_freem(chk->data);
7004                                 chk->data = NULL;
7005                         }
7006                         asoc->ctrl_queue_cnt--;
7007                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
7008                                 asoc->fwd_tsn_cnt--;
7009                         sctp_free_a_chunk(stcb, chk, so_locked);
7010                 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
7011                         /* special handling, we must look into the param */
7012                         if (chk != asoc->str_reset) {
7013                                 goto clean_up_anyway;
7014                         }
7015                 }
7016         }
7017 }
7018
7019
7020 static int
7021 sctp_can_we_split_this(struct sctp_tcb *stcb,
7022     uint32_t length,
7023     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
7024 {
7025         /*
7026          * Make a decision on if I should split a msg into multiple parts.
7027          * This is only asked of incomplete messages.
7028          */
7029         if (eeor_on) {
7030                 /*
7031                  * If we are doing EEOR we need to always send it if its the
7032                  * entire thing, since it might be all the guy is putting in
7033                  * the hopper.
7034                  */
7035                 if (goal_mtu >= length) {
7036                         /*-
7037                          * If we have data outstanding,
7038                          * we get another chance when the sack
7039                          * arrives to transmit - wait for more data
7040                          */
7041                         if (stcb->asoc.total_flight == 0) {
7042                                 /*
7043                                  * If nothing is in flight, we zero the
7044                                  * packet counter.
7045                                  */
7046                                 return (length);
7047                         }
7048                         return (0);
7049
7050                 } else {
7051                         /* You can fill the rest */
7052                         return (goal_mtu);
7053                 }
7054         }
7055         /*-
7056          * For those strange folk that make the send buffer
7057          * smaller than our fragmentation point, we can't
7058          * get a full msg in so we have to allow splitting.
7059          */
7060         if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
7061                 return (length);
7062         }
7063         if ((length <= goal_mtu) ||
7064             ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
7065                 /* Sub-optimial residual don't split in non-eeor mode. */
7066                 return (0);
7067         }
7068         /*
7069          * If we reach here length is larger than the goal_mtu. Do we wish
7070          * to split it for the sake of packet putting together?
7071          */
7072         if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
7073                 /* Its ok to split it */
7074                 return (min(goal_mtu, frag_point));
7075         }
7076         /* Nope, can't split */
7077         return (0);
7078
7079 }
7080
7081 static uint32_t
7082 sctp_move_to_outqueue(struct sctp_tcb *stcb,
7083     struct sctp_stream_out *strq,
7084     uint32_t goal_mtu,
7085     uint32_t frag_point,
7086     int *locked,
7087     int *giveup,
7088     int eeor_mode,
7089     int *bail,
7090     int so_locked
7091 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7092     SCTP_UNUSED
7093 #endif
7094 )
7095 {
7096         /* Move from the stream to the send_queue keeping track of the total */
7097         struct sctp_association *asoc;
7098         struct sctp_stream_queue_pending *sp;
7099         struct sctp_tmit_chunk *chk;
7100         struct sctp_data_chunk *dchkh;
7101         uint32_t to_move, length;
7102         uint8_t rcv_flags = 0;
7103         uint8_t some_taken;
7104         uint8_t send_lock_up = 0;
7105
7106         SCTP_TCB_LOCK_ASSERT(stcb);
7107         asoc = &stcb->asoc;
7108 one_more_time:
7109         /* sa_ignore FREED_MEMORY */
7110         sp = TAILQ_FIRST(&strq->outqueue);
7111         if (sp == NULL) {
7112                 *locked = 0;
7113                 if (send_lock_up == 0) {
7114                         SCTP_TCB_SEND_LOCK(stcb);
7115                         send_lock_up = 1;
7116                 }
7117                 sp = TAILQ_FIRST(&strq->outqueue);
7118                 if (sp) {
7119                         goto one_more_time;
7120                 }
7121                 if (strq->last_msg_incomplete) {
7122                         SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
7123                             strq->stream_no,
7124                             strq->last_msg_incomplete);
7125                         strq->last_msg_incomplete = 0;
7126                 }
7127                 to_move = 0;
7128                 if (send_lock_up) {
7129                         SCTP_TCB_SEND_UNLOCK(stcb);
7130                         send_lock_up = 0;
7131                 }
7132                 goto out_of;
7133         }
7134         if ((sp->msg_is_complete) && (sp->length == 0)) {
7135                 if (sp->sender_all_done) {
7136                         /*
7137                          * We are doing differed cleanup. Last time through
7138                          * when we took all the data the sender_all_done was
7139                          * not set.
7140                          */
7141                         if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
7142                                 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
7143                                 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7144                                     sp->sender_all_done,
7145                                     sp->length,
7146                                     sp->msg_is_complete,
7147                                     sp->put_last_out,
7148                                     send_lock_up);
7149                         }
7150                         if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
7151                                 SCTP_TCB_SEND_LOCK(stcb);
7152                                 send_lock_up = 1;
7153                         }
7154                         atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7155                         TAILQ_REMOVE(&strq->outqueue, sp, next);
7156                         stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
7157                         if (sp->net) {
7158                                 sctp_free_remote_addr(sp->net);
7159                                 sp->net = NULL;
7160                         }
7161                         if (sp->data) {
7162                                 sctp_m_freem(sp->data);
7163                                 sp->data = NULL;
7164                         }
7165                         sctp_free_a_strmoq(stcb, sp, so_locked);
7166                         /* we can't be locked to it */
7167                         *locked = 0;
7168                         stcb->asoc.locked_on_sending = NULL;
7169                         if (send_lock_up) {
7170                                 SCTP_TCB_SEND_UNLOCK(stcb);
7171                                 send_lock_up = 0;
7172                         }
7173                         /* back to get the next msg */
7174                         goto one_more_time;
7175                 } else {
7176                         /*
7177                          * sender just finished this but still holds a
7178                          * reference
7179                          */
7180                         *locked = 1;
7181                         *giveup = 1;
7182                         to_move = 0;
7183                         goto out_of;
7184                 }
7185         } else {
7186                 /* is there some to get */
7187                 if (sp->length == 0) {
7188                         /* no */
7189                         *locked = 1;
7190                         *giveup = 1;
7191                         to_move = 0;
7192                         goto out_of;
7193                 } else if (sp->discard_rest) {
7194                         if (send_lock_up == 0) {
7195                                 SCTP_TCB_SEND_LOCK(stcb);
7196                                 send_lock_up = 1;
7197                         }
7198                         /* Whack down the size */
7199                         atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
7200                         if ((stcb->sctp_socket != NULL) && \
7201                             ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7202                             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
7203                                 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
7204                         }
7205                         if (sp->data) {
7206                                 sctp_m_freem(sp->data);
7207                                 sp->data = NULL;
7208                                 sp->tail_mbuf = NULL;
7209                         }
7210                         sp->length = 0;
7211                         sp->some_taken = 1;
7212                         *locked = 1;
7213                         *giveup = 1;
7214                         to_move = 0;
7215                         goto out_of;
7216                 }
7217         }
7218         some_taken = sp->some_taken;
7219         if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
7220                 sp->msg_is_complete = 1;
7221         }
7222 re_look:
7223         length = sp->length;
7224         if (sp->msg_is_complete) {
7225                 /* The message is complete */
7226                 to_move = min(length, frag_point);
7227                 if (to_move == length) {
7228                         /* All of it fits in the MTU */
7229                         if (sp->some_taken) {
7230                                 rcv_flags |= SCTP_DATA_LAST_FRAG;
7231                                 sp->put_last_out = 1;
7232                         } else {
7233                                 rcv_flags |= SCTP_DATA_NOT_FRAG;
7234                                 sp->put_last_out = 1;
7235                         }
7236                 } else {
7237                         /* Not all of it fits, we fragment */
7238                         if (sp->some_taken == 0) {
7239                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7240                         }
7241                         sp->some_taken = 1;
7242                 }
7243         } else {
7244                 to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
7245                 if (to_move) {
7246                         /*-
7247                          * We use a snapshot of length in case it
7248                          * is expanding during the compare.
7249                          */
7250                         uint32_t llen;
7251
7252                         llen = length;
7253                         if (to_move >= llen) {
7254                                 to_move = llen;
7255                                 if (send_lock_up == 0) {
7256                                         /*-
7257                                          * We are taking all of an incomplete msg
7258                                          * thus we need a send lock.
7259                                          */
7260                                         SCTP_TCB_SEND_LOCK(stcb);
7261                                         send_lock_up = 1;
7262                                         if (sp->msg_is_complete) {
7263                                                 /*
7264                                                  * the sender finished the
7265                                                  * msg
7266                                                  */
7267                                                 goto re_look;
7268                                         }
7269                                 }
7270                         }
7271                         if (sp->some_taken == 0) {
7272                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7273                                 sp->some_taken = 1;
7274                         }
7275                 } else {
7276                         /* Nothing to take. */
7277                         if (sp->some_taken) {
7278                                 *locked = 1;
7279                         }
7280                         *giveup = 1;
7281                         to_move = 0;
7282                         goto out_of;
7283                 }
7284         }
7285
7286         /* If we reach here, we can copy out a chunk */
7287         sctp_alloc_a_chunk(stcb, chk);
7288         if (chk == NULL) {
7289                 /* No chunk memory */
7290                 *giveup = 1;
7291                 to_move = 0;
7292                 goto out_of;
7293         }
7294         /*
7295          * Setup for unordered if needed by looking at the user sent info
7296          * flags.
7297          */
7298         if (sp->sinfo_flags & SCTP_UNORDERED) {
7299                 rcv_flags |= SCTP_DATA_UNORDERED;
7300         }
7301         if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
7302             ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
7303                 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7304         }
7305         /* clear out the chunk before setting up */
7306         memset(chk, 0, sizeof(*chk));
7307         chk->rec.data.rcv_flags = rcv_flags;
7308
7309         if (to_move >= length) {
7310                 /* we think we can steal the whole thing */
7311                 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
7312                         SCTP_TCB_SEND_LOCK(stcb);
7313                         send_lock_up = 1;
7314                 }
7315                 if (to_move < sp->length) {
7316                         /* bail, it changed */
7317                         goto dont_do_it;
7318                 }
7319                 chk->data = sp->data;
7320                 chk->last_mbuf = sp->tail_mbuf;
7321                 /* register the stealing */
7322                 sp->data = sp->tail_mbuf = NULL;
7323         } else {
7324                 struct mbuf *m;
7325
7326 dont_do_it:
7327                 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
7328                 chk->last_mbuf = NULL;
7329                 if (chk->data == NULL) {
7330                         sp->some_taken = some_taken;
7331                         sctp_free_a_chunk(stcb, chk, so_locked);
7332                         *bail = 1;
7333                         to_move = 0;
7334                         goto out_of;
7335                 }
7336 #ifdef SCTP_MBUF_LOGGING
7337                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7338                         struct mbuf *mat;
7339
7340                         for (mat = chk->data; mat; mat = SCTP_BUF_NEXT(mat)) {
7341                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
7342                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7343                                 }
7344                         }
7345                 }
7346 #endif
7347                 /* Pull off the data */
7348                 m_adj(sp->data, to_move);
7349                 /* Now lets work our way down and compact it */
7350                 m = sp->data;
7351                 while (m && (SCTP_BUF_LEN(m) == 0)) {
7352                         sp->data = SCTP_BUF_NEXT(m);
7353                         SCTP_BUF_NEXT(m) = NULL;
7354                         if (sp->tail_mbuf == m) {
7355                                 /*-
7356                                  * Freeing tail? TSNH since
7357                                  * we supposedly were taking less
7358                                  * than the sp->length.
7359                                  */
7360 #ifdef INVARIANTS
7361                                 panic("Huh, freing tail? - TSNH");
7362 #else
7363                                 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
7364                                 sp->tail_mbuf = sp->data = NULL;
7365                                 sp->length = 0;
7366 #endif
7367
7368                         }
7369                         sctp_m_free(m);
7370                         m = sp->data;
7371                 }
7372         }
7373         if (SCTP_BUF_IS_EXTENDED(chk->data)) {
7374                 chk->copy_by_ref = 1;
7375         } else {
7376                 chk->copy_by_ref = 0;
7377         }
7378         /*
7379          * get last_mbuf and counts of mb useage This is ugly but hopefully
7380          * its only one mbuf.
7381          */
7382         if (chk->last_mbuf == NULL) {
7383                 chk->last_mbuf = chk->data;
7384                 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7385                         chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7386                 }
7387         }
7388         if (to_move > length) {
7389                 /*- This should not happen either
7390                  * since we always lower to_move to the size
7391                  * of sp->length if its larger.
7392                  */
7393 #ifdef INVARIANTS
7394                 panic("Huh, how can to_move be larger?");
7395 #else
7396                 SCTP_PRINTF("Huh, how can to_move be larger?\n");
7397                 sp->length = 0;
7398 #endif
7399         } else {
7400                 atomic_subtract_int(&sp->length, to_move);
7401         }
7402         if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
7403                 /* Not enough room for a chunk header, get some */
7404                 struct mbuf *m;
7405
7406                 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
7407                 if (m == NULL) {
7408                         /*
7409                          * we're in trouble here. _PREPEND below will free
7410                          * all the data if there is no leading space, so we
7411                          * must put the data back and restore.
7412                          */
7413                         if (send_lock_up == 0) {
7414                                 SCTP_TCB_SEND_LOCK(stcb);
7415                                 send_lock_up = 1;
7416                         }
7417                         if (chk->data == NULL) {
7418                                 /* unsteal the data */
7419                                 sp->data = chk->data;
7420                                 sp->tail_mbuf = chk->last_mbuf;
7421                         } else {
7422                                 struct mbuf *m_tmp;
7423
7424                                 /* reassemble the data */
7425                                 m_tmp = sp->data;
7426                                 sp->data = chk->data;
7427                                 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7428                         }
7429                         sp->some_taken = some_taken;
7430                         atomic_add_int(&sp->length, to_move);
7431                         chk->data = NULL;
7432                         *bail = 1;
7433                         sctp_free_a_chunk(stcb, chk, so_locked);
7434                         to_move = 0;
7435                         goto out_of;
7436                 } else {
7437                         SCTP_BUF_LEN(m) = 0;
7438                         SCTP_BUF_NEXT(m) = chk->data;
7439                         chk->data = m;
7440                         M_ALIGN(chk->data, 4);
7441                 }
7442         }
7443         SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7444         if (chk->data == NULL) {
7445                 /* HELP, TSNH since we assured it would not above? */
7446 #ifdef INVARIANTS
7447                 panic("prepend failes HELP?");
7448 #else
7449                 SCTP_PRINTF("prepend fails HELP?\n");
7450                 sctp_free_a_chunk(stcb, chk, so_locked);
7451 #endif
7452                 *bail = 1;
7453                 to_move = 0;
7454                 goto out_of;
7455         }
7456         sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7457         chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7458         chk->book_size_scale = 0;
7459         chk->sent = SCTP_DATAGRAM_UNSENT;
7460
7461         chk->flags = 0;
7462         chk->asoc = &stcb->asoc;
7463         chk->pad_inplace = 0;
7464         chk->no_fr_allowed = 0;
7465         chk->rec.data.stream_seq = sp->strseq;
7466         chk->rec.data.stream_number = sp->stream;
7467         chk->rec.data.payloadtype = sp->ppid;
7468         chk->rec.data.context = sp->context;
7469         chk->rec.data.doing_fast_retransmit = 0;
7470
7471         chk->rec.data.timetodrop = sp->ts;
7472         chk->flags = sp->act_flags;
7473
7474         if (sp->net) {
7475                 chk->whoTo = sp->net;
7476                 atomic_add_int(&chk->whoTo->ref_count, 1);
7477         } else
7478                 chk->whoTo = NULL;
7479
7480         if (sp->holds_key_ref) {
7481                 chk->auth_keyid = sp->auth_keyid;
7482                 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7483                 chk->holds_key_ref = 1;
7484         }
7485         chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7486         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7487                 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7488                     (uintptr_t) stcb, sp->length,
7489                     (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7490                     chk->rec.data.TSN_seq);
7491         }
7492         dchkh = mtod(chk->data, struct sctp_data_chunk *);
7493         /*
7494          * Put the rest of the things in place now. Size was done earlier in
7495          * previous loop prior to padding.
7496          */
7497
7498 #ifdef SCTP_ASOCLOG_OF_TSNS
7499         SCTP_TCB_LOCK_ASSERT(stcb);
7500         if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7501                 asoc->tsn_out_at = 0;
7502                 asoc->tsn_out_wrapped = 1;
7503         }
7504         asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7505         asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7506         asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7507         asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7508         asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7509         asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7510         asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7511         asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7512         asoc->tsn_out_at++;
7513 #endif
7514
7515         dchkh->ch.chunk_type = SCTP_DATA;
7516         dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7517         dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7518         dchkh->dp.stream_id = htons(strq->stream_no);
7519         dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7520         dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7521         dchkh->ch.chunk_length = htons(chk->send_size);
7522         /* Now advance the chk->send_size by the actual pad needed. */
7523         if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7524                 /* need a pad */
7525                 struct mbuf *lm;
7526                 int pads;
7527
7528                 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7529                 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7530                         chk->pad_inplace = 1;
7531                 }
7532                 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7533                         /* pad added an mbuf */
7534                         chk->last_mbuf = lm;
7535                 }
7536                 chk->send_size += pads;
7537         }
7538         /* We only re-set the policy if it is on */
7539         if (sp->pr_sctp_on) {
7540                 sctp_set_prsctp_policy(sp);
7541                 asoc->pr_sctp_cnt++;
7542                 chk->pr_sctp_on = 1;
7543         } else {
7544                 chk->pr_sctp_on = 0;
7545         }
7546         if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7547                 /* All done pull and kill the message */
7548                 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7549                 if (sp->put_last_out == 0) {
7550                         SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7551                         SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7552                             sp->sender_all_done,
7553                             sp->length,
7554                             sp->msg_is_complete,
7555                             sp->put_last_out,
7556                             send_lock_up);
7557                 }
7558                 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7559                         SCTP_TCB_SEND_LOCK(stcb);
7560                         send_lock_up = 1;
7561                 }
7562                 TAILQ_REMOVE(&strq->outqueue, sp, next);
7563                 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp, send_lock_up);
7564                 if (sp->net) {
7565                         sctp_free_remote_addr(sp->net);
7566                         sp->net = NULL;
7567                 }
7568                 if (sp->data) {
7569                         sctp_m_freem(sp->data);
7570                         sp->data = NULL;
7571                 }
7572                 sctp_free_a_strmoq(stcb, sp, so_locked);
7573
7574                 /* we can't be locked to it */
7575                 *locked = 0;
7576                 stcb->asoc.locked_on_sending = NULL;
7577         } else {
7578                 /* more to go, we are locked */
7579                 *locked = 1;
7580         }
7581         asoc->chunks_on_out_queue++;
7582         TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7583         asoc->send_queue_cnt++;
7584 out_of:
7585         if (send_lock_up) {
7586                 SCTP_TCB_SEND_UNLOCK(stcb);
7587         }
7588         return (to_move);
7589 }
7590
7591
7592 static void
7593 sctp_fill_outqueue(struct sctp_tcb *stcb,
7594     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now, int so_locked
7595 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7596     SCTP_UNUSED
7597 #endif
7598 )
7599 {
7600         struct sctp_association *asoc;
7601         struct sctp_stream_out *strq;
7602         int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7603         int locked, giveup;
7604
7605         SCTP_TCB_LOCK_ASSERT(stcb);
7606         asoc = &stcb->asoc;
7607         switch (net->ro._l_addr.sa.sa_family) {
7608 #ifdef INET
7609         case AF_INET:
7610                 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7611                 break;
7612 #endif
7613 #ifdef INET6
7614         case AF_INET6:
7615                 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7616                 break;
7617 #endif
7618         default:
7619                 /* TSNH */
7620                 goal_mtu = net->mtu;
7621                 break;
7622         }
7623         /* Need an allowance for the data chunk header too */
7624         goal_mtu -= sizeof(struct sctp_data_chunk);
7625
7626         /* must make even word boundary */
7627         goal_mtu &= 0xfffffffc;
7628         if (asoc->locked_on_sending) {
7629                 /* We are stuck on one stream until the message completes. */
7630                 strq = asoc->locked_on_sending;
7631                 locked = 1;
7632         } else {
7633                 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7634                 locked = 0;
7635         }
7636         while ((goal_mtu > 0) && strq) {
7637                 giveup = 0;
7638                 bail = 0;
7639                 moved_how_much = sctp_move_to_outqueue(stcb, strq, goal_mtu, frag_point, &locked,
7640                     &giveup, eeor_mode, &bail, so_locked);
7641                 if (moved_how_much)
7642                         stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, moved_how_much);
7643
7644                 if (locked) {
7645                         asoc->locked_on_sending = strq;
7646                         if ((moved_how_much == 0) || (giveup) || bail)
7647                                 /* no more to move for now */
7648                                 break;
7649                 } else {
7650                         asoc->locked_on_sending = NULL;
7651                         if ((giveup) || bail) {
7652                                 break;
7653                         }
7654                         strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7655                         if (strq == NULL) {
7656                                 break;
7657                         }
7658                 }
7659                 total_moved += moved_how_much;
7660                 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7661                 goal_mtu &= 0xfffffffc;
7662         }
7663         if (bail)
7664                 *quit_now = 1;
7665
7666         stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7667
7668         if (total_moved == 0) {
7669                 if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7670                     (net == stcb->asoc.primary_destination)) {
7671                         /* ran dry for primary network net */
7672                         SCTP_STAT_INCR(sctps_primary_randry);
7673                 } else if (stcb->asoc.sctp_cmt_on_off > 0) {
7674                         /* ran dry with CMT on */
7675                         SCTP_STAT_INCR(sctps_cmt_randry);
7676                 }
7677         }
7678 }
7679
7680 void
7681 sctp_fix_ecn_echo(struct sctp_association *asoc)
7682 {
7683         struct sctp_tmit_chunk *chk;
7684
7685         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7686                 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7687                         chk->sent = SCTP_DATAGRAM_UNSENT;
7688                 }
7689         }
7690 }
7691
7692 void
7693 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7694 {
7695         struct sctp_association *asoc;
7696         struct sctp_tmit_chunk *chk;
7697         struct sctp_stream_queue_pending *sp;
7698         unsigned int i;
7699
7700         if (net == NULL) {
7701                 return;
7702         }
7703         asoc = &stcb->asoc;
7704         for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7705                 TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7706                         if (sp->net == net) {
7707                                 sctp_free_remote_addr(sp->net);
7708                                 sp->net = NULL;
7709                         }
7710                 }
7711         }
7712         TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7713                 if (chk->whoTo == net) {
7714                         sctp_free_remote_addr(chk->whoTo);
7715                         chk->whoTo = NULL;
7716                 }
7717         }
7718 }
7719
7720 int
7721 sctp_med_chunk_output(struct sctp_inpcb *inp,
7722     struct sctp_tcb *stcb,
7723     struct sctp_association *asoc,
7724     int *num_out,
7725     int *reason_code,
7726     int control_only, int from_where,
7727     struct timeval *now, int *now_filled, int frag_point, int so_locked
7728 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7729     SCTP_UNUSED
7730 #endif
7731 )
7732 {
7733         /*
7734          * Ok this is the generic chunk service queue. we must do the
7735          * following: - Service the stream queue that is next, moving any
7736          * message (note I must get a complete message i.e. FIRST/MIDDLE and
7737          * LAST to the out queue in one pass) and assigning TSN's - Check to
7738          * see if the cwnd/rwnd allows any output, if so we go ahead and
7739          * fomulate and send the low level chunks. Making sure to combine
7740          * any control in the control chunk queue also.
7741          */
7742         struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7743         struct mbuf *outchain, *endoutchain;
7744         struct sctp_tmit_chunk *chk, *nchk;
7745
7746         /* temp arrays for unlinking */
7747         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7748         int no_fragmentflg, error;
7749         unsigned int max_rwnd_per_dest, max_send_per_dest;
7750         int one_chunk, hbflag, skip_data_for_this_net;
7751         int asconf, cookie, no_out_cnt;
7752         int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7753         unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7754         int tsns_sent = 0;
7755         uint32_t auth_offset = 0;
7756         struct sctp_auth_chunk *auth = NULL;
7757         uint16_t auth_keyid;
7758         int override_ok = 1;
7759         int skip_fill_up = 0;
7760         int data_auth_reqd = 0;
7761
7762         /*
7763          * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7764          * destination.
7765          */
7766         int quit_now = 0;
7767
7768         *num_out = 0;
7769         auth_keyid = stcb->asoc.authinfo.active_keyid;
7770
7771         if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7772             (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7773             (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7774                 eeor_mode = 1;
7775         } else {
7776                 eeor_mode = 0;
7777         }
7778         ctl_cnt = no_out_cnt = asconf = cookie = 0;
7779         /*
7780          * First lets prime the pump. For each destination, if there is room
7781          * in the flight size, attempt to pull an MTU's worth out of the
7782          * stream queues into the general send_queue
7783          */
7784 #ifdef SCTP_AUDITING_ENABLED
7785         sctp_audit_log(0xC2, 2);
7786 #endif
7787         SCTP_TCB_LOCK_ASSERT(stcb);
7788         hbflag = 0;
7789         if ((control_only) || (asoc->stream_reset_outstanding))
7790                 no_data_chunks = 1;
7791         else
7792                 no_data_chunks = 0;
7793
7794         /* Nothing to possible to send? */
7795         if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7796             (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7797             TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7798             TAILQ_EMPTY(&asoc->send_queue) &&
7799             stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
7800 nothing_to_send:
7801                 *reason_code = 9;
7802                 return (0);
7803         }
7804         if (asoc->peers_rwnd == 0) {
7805                 /* No room in peers rwnd */
7806                 *reason_code = 1;
7807                 if (asoc->total_flight > 0) {
7808                         /* we are allowed one chunk in flight */
7809                         no_data_chunks = 1;
7810                 }
7811         }
7812         if (stcb->asoc.ecn_echo_cnt_onq) {
7813                 /* Record where a sack goes, if any */
7814                 if (no_data_chunks &&
7815                     (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7816                         /* Nothing but ECNe to send - we don't do that */
7817                         goto nothing_to_send;
7818                 }
7819                 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7820                         if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7821                             (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7822                                 sack_goes_to = chk->whoTo;
7823                                 break;
7824                         }
7825                 }
7826         }
7827         max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7828         if (stcb->sctp_socket)
7829                 max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7830         else
7831                 max_send_per_dest = 0;
7832         if (no_data_chunks == 0) {
7833                 /* How many non-directed chunks are there? */
7834                 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7835                         if (chk->whoTo == NULL) {
7836                                 /*
7837                                  * We already have non-directed chunks on
7838                                  * the queue, no need to do a fill-up.
7839                                  */
7840                                 skip_fill_up = 1;
7841                                 break;
7842                         }
7843                 }
7844
7845         }
7846         if ((no_data_chunks == 0) &&
7847             (skip_fill_up == 0) &&
7848             (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7849                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7850                         /*
7851                          * This for loop we are in takes in each net, if
7852                          * its's got space in cwnd and has data sent to it
7853                          * (when CMT is off) then it calls
7854                          * sctp_fill_outqueue for the net. This gets data on
7855                          * the send queue for that network.
7856                          * 
7857                          * In sctp_fill_outqueue TSN's are assigned and data is
7858                          * copied out of the stream buffers. Note mostly
7859                          * copy by reference (we hope).
7860                          */
7861                         net->window_probe = 0;
7862                         if ((net != stcb->asoc.alternate) &&
7863                             ((net->dest_state & SCTP_ADDR_PF) ||
7864                             (!(net->dest_state & SCTP_ADDR_REACHABLE)) ||
7865                             (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
7866                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7867                                         sctp_log_cwnd(stcb, net, 1,
7868                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7869                                 }
7870                                 continue;
7871                         }
7872                         if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
7873                             (net->flight_size == 0)) {
7874                                 (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
7875                         }
7876                         if (net->flight_size >= net->cwnd) {
7877                                 /* skip this network, no room - can't fill */
7878                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7879                                         sctp_log_cwnd(stcb, net, 3,
7880                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7881                                 }
7882                                 continue;
7883                         }
7884                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7885                                 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7886                         }
7887                         sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
7888                         if (quit_now) {
7889                                 /* memory alloc failure */
7890                                 no_data_chunks = 1;
7891                                 break;
7892                         }
7893                 }
7894         }
7895         /* now service each destination and send out what we can for it */
7896         /* Nothing to send? */
7897         if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7898             TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7899             TAILQ_EMPTY(&asoc->send_queue)) {
7900                 *reason_code = 8;
7901                 return (0);
7902         }
7903         if (asoc->sctp_cmt_on_off > 0) {
7904                 /* get the last start point */
7905                 start_at = asoc->last_net_cmt_send_started;
7906                 if (start_at == NULL) {
7907                         /* null so to beginning */
7908                         start_at = TAILQ_FIRST(&asoc->nets);
7909                 } else {
7910                         start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7911                         if (start_at == NULL) {
7912                                 start_at = TAILQ_FIRST(&asoc->nets);
7913                         }
7914                 }
7915                 asoc->last_net_cmt_send_started = start_at;
7916         } else {
7917                 start_at = TAILQ_FIRST(&asoc->nets);
7918         }
7919         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7920                 if (chk->whoTo == NULL) {
7921                         if (asoc->alternate) {
7922                                 chk->whoTo = asoc->alternate;
7923                         } else {
7924                                 chk->whoTo = asoc->primary_destination;
7925                         }
7926                         atomic_add_int(&chk->whoTo->ref_count, 1);
7927                 }
7928         }
7929         old_start_at = NULL;
7930 again_one_more_time:
7931         for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7932                 /* how much can we send? */
7933                 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7934                 if (old_start_at && (old_start_at == net)) {
7935                         /* through list ocmpletely. */
7936                         break;
7937                 }
7938                 tsns_sent = 0xa;
7939                 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7940                     TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7941                     (net->flight_size >= net->cwnd)) {
7942                         /*
7943                          * Nothing on control or asconf and flight is full,
7944                          * we can skip even in the CMT case.
7945                          */
7946                         continue;
7947                 }
7948                 bundle_at = 0;
7949                 endoutchain = outchain = NULL;
7950                 no_fragmentflg = 1;
7951                 one_chunk = 0;
7952                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7953                         skip_data_for_this_net = 1;
7954                 } else {
7955                         skip_data_for_this_net = 0;
7956                 }
7957                 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7958                         /*
7959                          * if we have a route and an ifp check to see if we
7960                          * have room to send to this guy
7961                          */
7962                         struct ifnet *ifp;
7963
7964                         ifp = net->ro.ro_rt->rt_ifp;
7965                         if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7966                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
7967                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7968                                         sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7969                                 }
7970                                 continue;
7971                         }
7972                 }
7973                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7974 #ifdef INET
7975                 case AF_INET:
7976                         mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7977                         break;
7978 #endif
7979 #ifdef INET6
7980                 case AF_INET6:
7981                         mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7982                         break;
7983 #endif
7984                 default:
7985                         /* TSNH */
7986                         mtu = net->mtu;
7987                         break;
7988                 }
7989                 mx_mtu = mtu;
7990                 to_out = 0;
7991                 if (mtu > asoc->peers_rwnd) {
7992                         if (asoc->total_flight > 0) {
7993                                 /* We have a packet in flight somewhere */
7994                                 r_mtu = asoc->peers_rwnd;
7995                         } else {
7996                                 /* We are always allowed to send one MTU out */
7997                                 one_chunk = 1;
7998                                 r_mtu = mtu;
7999                         }
8000                 } else {
8001                         r_mtu = mtu;
8002                 }
8003                 /************************/
8004                 /* ASCONF transmission */
8005                 /************************/
8006                 /* Now first lets go through the asconf queue */
8007                 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
8008                         if (chk->rec.chunk_id.id != SCTP_ASCONF) {
8009                                 continue;
8010                         }
8011                         if (chk->whoTo == NULL) {
8012                                 if (asoc->alternate == NULL) {
8013                                         if (asoc->primary_destination != net) {
8014                                                 break;
8015                                         }
8016                                 } else {
8017                                         if (asoc->alternate != net) {
8018                                                 break;
8019                                         }
8020                                 }
8021                         } else {
8022                                 if (chk->whoTo != net) {
8023                                         break;
8024                                 }
8025                         }
8026                         if (chk->data == NULL) {
8027                                 break;
8028                         }
8029                         if (chk->sent != SCTP_DATAGRAM_UNSENT &&
8030                             chk->sent != SCTP_DATAGRAM_RESEND) {
8031                                 break;
8032                         }
8033                         /*
8034                          * if no AUTH is yet included and this chunk
8035                          * requires it, make sure to account for it.  We
8036                          * don't apply the size until the AUTH chunk is
8037                          * actually added below in case there is no room for
8038                          * this chunk. NOTE: we overload the use of "omtu"
8039                          * here
8040                          */
8041                         if ((auth == NULL) &&
8042                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8043                             stcb->asoc.peer_auth_chunks)) {
8044                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8045                         } else
8046                                 omtu = 0;
8047                         /* Here we do NOT factor the r_mtu */
8048                         if ((chk->send_size < (int)(mtu - omtu)) ||
8049                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8050                                 /*
8051                                  * We probably should glom the mbuf chain
8052                                  * from the chk->data for control but the
8053                                  * problem is it becomes yet one more level
8054                                  * of tracking to do if for some reason
8055                                  * output fails. Then I have got to
8056                                  * reconstruct the merged control chain.. el
8057                                  * yucko.. for now we take the easy way and
8058                                  * do the copy
8059                                  */
8060                                 /*
8061                                  * Add an AUTH chunk, if chunk requires it
8062                                  * save the offset into the chain for AUTH
8063                                  */
8064                                 if ((auth == NULL) &&
8065                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8066                                     stcb->asoc.peer_auth_chunks))) {
8067                                         outchain = sctp_add_auth_chunk(outchain,
8068                                             &endoutchain,
8069                                             &auth,
8070                                             &auth_offset,
8071                                             stcb,
8072                                             chk->rec.chunk_id.id);
8073                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8074                                 }
8075                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8076                                     (int)chk->rec.chunk_id.can_take_data,
8077                                     chk->send_size, chk->copy_by_ref);
8078                                 if (outchain == NULL) {
8079                                         *reason_code = 8;
8080                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8081                                         return (ENOMEM);
8082                                 }
8083                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8084                                 /* update our MTU size */
8085                                 if (mtu > (chk->send_size + omtu))
8086                                         mtu -= (chk->send_size + omtu);
8087                                 else
8088                                         mtu = 0;
8089                                 to_out += (chk->send_size + omtu);
8090                                 /* Do clear IP_DF ? */
8091                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8092                                         no_fragmentflg = 0;
8093                                 }
8094                                 if (chk->rec.chunk_id.can_take_data)
8095                                         chk->data = NULL;
8096                                 /*
8097                                  * set hb flag since we can use these for
8098                                  * RTO
8099                                  */
8100                                 hbflag = 1;
8101                                 asconf = 1;
8102                                 /*
8103                                  * should sysctl this: don't bundle data
8104                                  * with ASCONF since it requires AUTH
8105                                  */
8106                                 no_data_chunks = 1;
8107                                 chk->sent = SCTP_DATAGRAM_SENT;
8108                                 if (chk->whoTo == NULL) {
8109                                         chk->whoTo = net;
8110                                         atomic_add_int(&net->ref_count, 1);
8111                                 }
8112                                 chk->snd_count++;
8113                                 if (mtu == 0) {
8114                                         /*
8115                                          * Ok we are out of room but we can
8116                                          * output without effecting the
8117                                          * flight size since this little guy
8118                                          * is a control only packet.
8119                                          */
8120                                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8121                                         /*
8122                                          * do NOT clear the asconf flag as
8123                                          * it is used to do appropriate
8124                                          * source address selection.
8125                                          */
8126                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8127                                             (struct sockaddr *)&net->ro._l_addr,
8128                                             outchain, auth_offset, auth,
8129                                             stcb->asoc.authinfo.active_keyid,
8130                                             no_fragmentflg, 0, asconf,
8131                                             inp->sctp_lport, stcb->rport,
8132                                             htonl(stcb->asoc.peer_vtag),
8133                                             net->port, so_locked, NULL, NULL))) {
8134                                                 if (error == ENOBUFS) {
8135                                                         asoc->ifp_had_enobuf = 1;
8136                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
8137                                                 }
8138                                                 if (from_where == 0) {
8139                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
8140                                                 }
8141                                                 if (*now_filled == 0) {
8142                                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8143                                                         *now_filled = 1;
8144                                                         *now = net->last_sent_time;
8145                                                 } else {
8146                                                         net->last_sent_time = *now;
8147                                                 }
8148                                                 hbflag = 0;
8149                                                 /* error, could not output */
8150                                                 if (error == EHOSTUNREACH) {
8151                                                         /*
8152                                                          * Destination went
8153                                                          * unreachable
8154                                                          * during this send
8155                                                          */
8156                                                         sctp_move_chunks_from_net(stcb, net);
8157                                                 }
8158                                                 *reason_code = 7;
8159                                                 continue;
8160                                         } else
8161                                                 asoc->ifp_had_enobuf = 0;
8162                                         if (*now_filled == 0) {
8163                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8164                                                 *now_filled = 1;
8165                                                 *now = net->last_sent_time;
8166                                         } else {
8167                                                 net->last_sent_time = *now;
8168                                         }
8169                                         hbflag = 0;
8170                                         /*
8171                                          * increase the number we sent, if a
8172                                          * cookie is sent we don't tell them
8173                                          * any was sent out.
8174                                          */
8175                                         outchain = endoutchain = NULL;
8176                                         auth = NULL;
8177                                         auth_offset = 0;
8178                                         if (!no_out_cnt)
8179                                                 *num_out += ctl_cnt;
8180                                         /* recalc a clean slate and setup */
8181                                         switch (net->ro._l_addr.sa.sa_family) {
8182 #ifdef INET
8183                                         case AF_INET:
8184                                                 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8185                                                 break;
8186 #endif
8187 #ifdef INET6
8188                                         case AF_INET6:
8189                                                 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8190                                                 break;
8191 #endif
8192                                         default:
8193                                                 /* TSNH */
8194                                                 mtu = net->mtu;
8195                                                 break;
8196                                         }
8197                                         to_out = 0;
8198                                         no_fragmentflg = 1;
8199                                 }
8200                         }
8201                 }
8202                 /************************/
8203                 /* Control transmission */
8204                 /************************/
8205                 /* Now first lets go through the control queue */
8206                 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
8207                         if ((sack_goes_to) &&
8208                             (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
8209                             (chk->whoTo != sack_goes_to)) {
8210                                 /*
8211                                  * if we have a sack in queue, and we are
8212                                  * looking at an ecn echo that is NOT queued
8213                                  * to where the sack is going..
8214                                  */
8215                                 if (chk->whoTo == net) {
8216                                         /*
8217                                          * Don't transmit it to where its
8218                                          * going (current net)
8219                                          */
8220                                         continue;
8221                                 } else if (sack_goes_to == net) {
8222                                         /*
8223                                          * But do transmit it to this
8224                                          * address
8225                                          */
8226                                         goto skip_net_check;
8227                                 }
8228                         }
8229                         if (chk->whoTo == NULL) {
8230                                 if (asoc->alternate == NULL) {
8231                                         if (asoc->primary_destination != net) {
8232                                                 continue;
8233                                         }
8234                                 } else {
8235                                         if (asoc->alternate != net) {
8236                                                 continue;
8237                                         }
8238                                 }
8239                         } else {
8240                                 if (chk->whoTo != net) {
8241                                         continue;
8242                                 }
8243                         }
8244         skip_net_check:
8245                         if (chk->data == NULL) {
8246                                 continue;
8247                         }
8248                         if (chk->sent != SCTP_DATAGRAM_UNSENT) {
8249                                 /*
8250                                  * It must be unsent. Cookies and ASCONF's
8251                                  * hang around but there timers will force
8252                                  * when marked for resend.
8253                                  */
8254                                 continue;
8255                         }
8256                         /*
8257                          * if no AUTH is yet included and this chunk
8258                          * requires it, make sure to account for it.  We
8259                          * don't apply the size until the AUTH chunk is
8260                          * actually added below in case there is no room for
8261                          * this chunk. NOTE: we overload the use of "omtu"
8262                          * here
8263                          */
8264                         if ((auth == NULL) &&
8265                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8266                             stcb->asoc.peer_auth_chunks)) {
8267                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8268                         } else
8269                                 omtu = 0;
8270                         /* Here we do NOT factor the r_mtu */
8271                         if ((chk->send_size <= (int)(mtu - omtu)) ||
8272                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8273                                 /*
8274                                  * We probably should glom the mbuf chain
8275                                  * from the chk->data for control but the
8276                                  * problem is it becomes yet one more level
8277                                  * of tracking to do if for some reason
8278                                  * output fails. Then I have got to
8279                                  * reconstruct the merged control chain.. el
8280                                  * yucko.. for now we take the easy way and
8281                                  * do the copy
8282                                  */
8283                                 /*
8284                                  * Add an AUTH chunk, if chunk requires it
8285                                  * save the offset into the chain for AUTH
8286                                  */
8287                                 if ((auth == NULL) &&
8288                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8289                                     stcb->asoc.peer_auth_chunks))) {
8290                                         outchain = sctp_add_auth_chunk(outchain,
8291                                             &endoutchain,
8292                                             &auth,
8293                                             &auth_offset,
8294                                             stcb,
8295                                             chk->rec.chunk_id.id);
8296                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8297                                 }
8298                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8299                                     (int)chk->rec.chunk_id.can_take_data,
8300                                     chk->send_size, chk->copy_by_ref);
8301                                 if (outchain == NULL) {
8302                                         *reason_code = 8;
8303                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8304                                         return (ENOMEM);
8305                                 }
8306                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8307                                 /* update our MTU size */
8308                                 if (mtu > (chk->send_size + omtu))
8309                                         mtu -= (chk->send_size + omtu);
8310                                 else
8311                                         mtu = 0;
8312                                 to_out += (chk->send_size + omtu);
8313                                 /* Do clear IP_DF ? */
8314                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8315                                         no_fragmentflg = 0;
8316                                 }
8317                                 if (chk->rec.chunk_id.can_take_data)
8318                                         chk->data = NULL;
8319                                 /* Mark things to be removed, if needed */
8320                                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8321                                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
8322                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8323                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8324                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8325                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8326                                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8327                                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8328                                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8329                                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8330                                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8331                                         if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
8332                                                 hbflag = 1;
8333                                         }
8334                                         /* remove these chunks at the end */
8335                                         if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8336                                             (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
8337                                                 /* turn off the timer */
8338                                                 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8339                                                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8340                                                             inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
8341                                                 }
8342                                         }
8343                                         ctl_cnt++;
8344                                 } else {
8345                                         /*
8346                                          * Other chunks, since they have
8347                                          * timers running (i.e. COOKIE) we
8348                                          * just "trust" that it gets sent or
8349                                          * retransmitted.
8350                                          */
8351                                         ctl_cnt++;
8352                                         if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8353                                                 cookie = 1;
8354                                                 no_out_cnt = 1;
8355                                         } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8356                                                 /*
8357                                                  * Increment ecne send count
8358                                                  * here this means we may be
8359                                                  * over-zealous in our
8360                                                  * counting if the send
8361                                                  * fails, but its the best
8362                                                  * place to do it (we used
8363                                                  * to do it in the queue of
8364                                                  * the chunk, but that did
8365                                                  * not tell how many times
8366                                                  * it was sent.
8367                                                  */
8368                                                 SCTP_STAT_INCR(sctps_sendecne);
8369                                         }
8370                                         chk->sent = SCTP_DATAGRAM_SENT;
8371                                         if (chk->whoTo == NULL) {
8372                                                 chk->whoTo = net;
8373                                                 atomic_add_int(&net->ref_count, 1);
8374                                         }
8375                                         chk->snd_count++;
8376                                 }
8377                                 if (mtu == 0) {
8378                                         /*
8379                                          * Ok we are out of room but we can
8380                                          * output without effecting the
8381                                          * flight size since this little guy
8382                                          * is a control only packet.
8383                                          */
8384                                         if (asconf) {
8385                                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8386                                                 /*
8387                                                  * do NOT clear the asconf
8388                                                  * flag as it is used to do
8389                                                  * appropriate source
8390                                                  * address selection.
8391                                                  */
8392                                         }
8393                                         if (cookie) {
8394                                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8395                                                 cookie = 0;
8396                                         }
8397                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8398                                             (struct sockaddr *)&net->ro._l_addr,
8399                                             outchain,
8400                                             auth_offset, auth,
8401                                             stcb->asoc.authinfo.active_keyid,
8402                                             no_fragmentflg, 0, asconf,
8403                                             inp->sctp_lport, stcb->rport,
8404                                             htonl(stcb->asoc.peer_vtag),
8405                                             net->port, so_locked, NULL, NULL))) {
8406                                                 if (error == ENOBUFS) {
8407                                                         asoc->ifp_had_enobuf = 1;
8408                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
8409                                                 }
8410                                                 if (from_where == 0) {
8411                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
8412                                                 }
8413                                                 /* error, could not output */
8414                                                 if (hbflag) {
8415                                                         if (*now_filled == 0) {
8416                                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8417                                                                 *now_filled = 1;
8418                                                                 *now = net->last_sent_time;
8419                                                         } else {
8420                                                                 net->last_sent_time = *now;
8421                                                         }
8422                                                         hbflag = 0;
8423                                                 }
8424                                                 if (error == EHOSTUNREACH) {
8425                                                         /*
8426                                                          * Destination went
8427                                                          * unreachable
8428                                                          * during this send
8429                                                          */
8430                                                         sctp_move_chunks_from_net(stcb, net);
8431                                                 }
8432                                                 *reason_code = 7;
8433                                                 continue;
8434                                         } else
8435                                                 asoc->ifp_had_enobuf = 0;
8436                                         /* Only HB or ASCONF advances time */
8437                                         if (hbflag) {
8438                                                 if (*now_filled == 0) {
8439                                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8440                                                         *now_filled = 1;
8441                                                         *now = net->last_sent_time;
8442                                                 } else {
8443                                                         net->last_sent_time = *now;
8444                                                 }
8445                                                 hbflag = 0;
8446                                         }
8447                                         /*
8448                                          * increase the number we sent, if a
8449                                          * cookie is sent we don't tell them
8450                                          * any was sent out.
8451                                          */
8452                                         outchain = endoutchain = NULL;
8453                                         auth = NULL;
8454                                         auth_offset = 0;
8455                                         if (!no_out_cnt)
8456                                                 *num_out += ctl_cnt;
8457                                         /* recalc a clean slate and setup */
8458                                         switch (net->ro._l_addr.sa.sa_family) {
8459 #ifdef INET
8460                                         case AF_INET:
8461                                                 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8462                                                 break;
8463 #endif
8464 #ifdef INET6
8465                                         case AF_INET6:
8466                                                 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8467                                                 break;
8468 #endif
8469                                         default:
8470                                                 /* TSNH */
8471                                                 mtu = net->mtu;
8472                                                 break;
8473                                         }
8474                                         to_out = 0;
8475                                         no_fragmentflg = 1;
8476                                 }
8477                         }
8478                 }
8479                 /* JRI: if dest is in PF state, do not send data to it */
8480                 if ((asoc->sctp_cmt_on_off > 0) &&
8481                     (net != stcb->asoc.alternate) &&
8482                     (net->dest_state & SCTP_ADDR_PF)) {
8483                         goto no_data_fill;
8484                 }
8485                 if (net->flight_size >= net->cwnd) {
8486                         goto no_data_fill;
8487                 }
8488                 if ((asoc->sctp_cmt_on_off > 0) &&
8489                     (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
8490                     (net->flight_size > max_rwnd_per_dest)) {
8491                         goto no_data_fill;
8492                 }
8493                 /*
8494                  * We need a specific accounting for the usage of the send
8495                  * buffer. We also need to check the number of messages per
8496                  * net. For now, this is better than nothing and it disabled
8497                  * by default...
8498                  */
8499                 if ((asoc->sctp_cmt_on_off > 0) &&
8500                     (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
8501                     (max_send_per_dest > 0) &&
8502                     (net->flight_size > max_send_per_dest)) {
8503                         goto no_data_fill;
8504                 }
8505                 /*********************/
8506                 /* Data transmission */
8507                 /*********************/
8508                 /*
8509                  * if AUTH for DATA is required and no AUTH has been added
8510                  * yet, account for this in the mtu now... if no data can be
8511                  * bundled, this adjustment won't matter anyways since the
8512                  * packet will be going out...
8513                  */
8514                 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8515                     stcb->asoc.peer_auth_chunks);
8516                 if (data_auth_reqd && (auth == NULL)) {
8517                         mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8518                 }
8519                 /* now lets add any data within the MTU constraints */
8520                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8521                 case AF_INET:
8522                         if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8523                                 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8524                         else
8525                                 omtu = 0;
8526                         break;
8527 #ifdef INET6
8528                 case AF_INET6:
8529                         if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8530                                 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8531                         else
8532                                 omtu = 0;
8533                         break;
8534 #endif
8535                 default:
8536                         /* TSNH */
8537                         omtu = 0;
8538                         break;
8539                 }
8540                 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8541                     (skip_data_for_this_net == 0)) ||
8542                     (cookie)) {
8543                         TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
8544                                 if (no_data_chunks) {
8545                                         /* let only control go out */
8546                                         *reason_code = 1;
8547                                         break;
8548                                 }
8549                                 if (net->flight_size >= net->cwnd) {
8550                                         /* skip this net, no room for data */
8551                                         *reason_code = 2;
8552                                         break;
8553                                 }
8554                                 if ((chk->whoTo != NULL) &&
8555                                     (chk->whoTo != net)) {
8556                                         /* Don't send the chunk on this net */
8557                                         continue;
8558                                 }
8559                                 if (asoc->sctp_cmt_on_off == 0) {
8560                                         if ((asoc->alternate) &&
8561                                             (asoc->alternate != net) &&
8562                                             (chk->whoTo == NULL)) {
8563                                                 continue;
8564                                         } else if ((net != asoc->primary_destination) &&
8565                                                     (asoc->alternate == NULL) &&
8566                                             (chk->whoTo == NULL)) {
8567                                                 continue;
8568                                         }
8569                                 }
8570                                 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8571                                         /*-
8572                                          * strange, we have a chunk that is
8573                                          * to big for its destination and
8574                                          * yet no fragment ok flag.
8575                                          * Something went wrong when the
8576                                          * PMTU changed...we did not mark
8577                                          * this chunk for some reason?? I
8578                                          * will fix it here by letting IP
8579                                          * fragment it for now and printing
8580                                          * a warning. This really should not
8581                                          * happen ...
8582                                          */
8583                                         SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8584                                             chk->send_size, mtu);
8585                                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8586                                 }
8587                                 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8588                                     ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8589                                         struct sctp_data_chunk *dchkh;
8590
8591                                         dchkh = mtod(chk->data, struct sctp_data_chunk *);
8592                                         dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8593                                 }
8594                                 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8595                                     ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8596                                         /* ok we will add this one */
8597
8598                                         /*
8599                                          * Add an AUTH chunk, if chunk
8600                                          * requires it, save the offset into
8601                                          * the chain for AUTH
8602                                          */
8603                                         if (data_auth_reqd) {
8604                                                 if (auth == NULL) {
8605                                                         outchain = sctp_add_auth_chunk(outchain,
8606                                                             &endoutchain,
8607                                                             &auth,
8608                                                             &auth_offset,
8609                                                             stcb,
8610                                                             SCTP_DATA);
8611                                                         auth_keyid = chk->auth_keyid;
8612                                                         override_ok = 0;
8613                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8614                                                 } else if (override_ok) {
8615                                                         /*
8616                                                          * use this data's
8617                                                          * keyid
8618                                                          */
8619                                                         auth_keyid = chk->auth_keyid;
8620                                                         override_ok = 0;
8621                                                 } else if (auth_keyid != chk->auth_keyid) {
8622                                                         /*
8623                                                          * different keyid,
8624                                                          * so done bundling
8625                                                          */
8626                                                         break;
8627                                                 }
8628                                         }
8629                                         outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8630                                             chk->send_size, chk->copy_by_ref);
8631                                         if (outchain == NULL) {
8632                                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8633                                                 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8634                                                         sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8635                                                 }
8636                                                 *reason_code = 3;
8637                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8638                                                 return (ENOMEM);
8639                                         }
8640                                         /* upate our MTU size */
8641                                         /* Do clear IP_DF ? */
8642                                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8643                                                 no_fragmentflg = 0;
8644                                         }
8645                                         /* unsigned subtraction of mtu */
8646                                         if (mtu > chk->send_size)
8647                                                 mtu -= chk->send_size;
8648                                         else
8649                                                 mtu = 0;
8650                                         /* unsigned subtraction of r_mtu */
8651                                         if (r_mtu > chk->send_size)
8652                                                 r_mtu -= chk->send_size;
8653                                         else
8654                                                 r_mtu = 0;
8655
8656                                         to_out += chk->send_size;
8657                                         if ((to_out > mx_mtu) && no_fragmentflg) {
8658 #ifdef INVARIANTS
8659                                                 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8660 #else
8661                                                 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8662                                                     mx_mtu, to_out);
8663 #endif
8664                                         }
8665                                         chk->window_probe = 0;
8666                                         data_list[bundle_at++] = chk;
8667                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8668                                                 break;
8669                                         }
8670                                         if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8671                                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8672                                                         SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8673                                                 } else {
8674                                                         SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8675                                                 }
8676                                                 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8677                                                     ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8678                                                         /*
8679                                                          * Count number of
8680                                                          * user msg's that
8681                                                          * were fragmented
8682                                                          * we do this by
8683                                                          * counting when we
8684                                                          * see a LAST
8685                                                          * fragment only.
8686                                                          */
8687                                                         SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8688                                         }
8689                                         if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8690                                                 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8691                                                         data_list[0]->window_probe = 1;
8692                                                         net->window_probe = 1;
8693                                                 }
8694                                                 break;
8695                                         }
8696                                 } else {
8697                                         /*
8698                                          * Must be sent in order of the
8699                                          * TSN's (on a network)
8700                                          */
8701                                         break;
8702                                 }
8703                         }       /* for (chunk gather loop for this net) */
8704                 }               /* if asoc.state OPEN */
8705 no_data_fill:
8706                 /* Is there something to send for this destination? */
8707                 if (outchain) {
8708                         /* We may need to start a control timer or two */
8709                         if (asconf) {
8710                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8711                                     stcb, net);
8712                                 /*
8713                                  * do NOT clear the asconf flag as it is
8714                                  * used to do appropriate source address
8715                                  * selection.
8716                                  */
8717                         }
8718                         if (cookie) {
8719                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8720                                 cookie = 0;
8721                         }
8722                         /* must start a send timer if data is being sent */
8723                         if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8724                                 /*
8725                                  * no timer running on this destination
8726                                  * restart it.
8727                                  */
8728                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8729                         }
8730                         /* Now send it, if there is anything to send :> */
8731                         if ((error = sctp_lowlevel_chunk_output(inp,
8732                             stcb,
8733                             net,
8734                             (struct sockaddr *)&net->ro._l_addr,
8735                             outchain,
8736                             auth_offset,
8737                             auth,
8738                             auth_keyid,
8739                             no_fragmentflg,
8740                             bundle_at,
8741                             asconf,
8742                             inp->sctp_lport, stcb->rport,
8743                             htonl(stcb->asoc.peer_vtag),
8744                             net->port, so_locked, NULL, NULL))) {
8745                                 /* error, we could not output */
8746                                 if (error == ENOBUFS) {
8747                                         SCTP_STAT_INCR(sctps_lowlevelerr);
8748                                         asoc->ifp_had_enobuf = 1;
8749                                 }
8750                                 if (from_where == 0) {
8751                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
8752                                 }
8753                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8754                                 if (hbflag) {
8755                                         if (*now_filled == 0) {
8756                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8757                                                 *now_filled = 1;
8758                                                 *now = net->last_sent_time;
8759                                         } else {
8760                                                 net->last_sent_time = *now;
8761                                         }
8762                                         hbflag = 0;
8763                                 }
8764                                 if (error == EHOSTUNREACH) {
8765                                         /*
8766                                          * Destination went unreachable
8767                                          * during this send
8768                                          */
8769                                         sctp_move_chunks_from_net(stcb, net);
8770                                 }
8771                                 *reason_code = 6;
8772                                 /*-
8773                                  * I add this line to be paranoid. As far as
8774                                  * I can tell the continue, takes us back to
8775                                  * the top of the for, but just to make sure
8776                                  * I will reset these again here.
8777                                  */
8778                                 ctl_cnt = bundle_at = 0;
8779                                 continue;       /* This takes us back to the
8780                                                  * for() for the nets. */
8781                         } else {
8782                                 asoc->ifp_had_enobuf = 0;
8783                         }
8784                         endoutchain = NULL;
8785                         auth = NULL;
8786                         auth_offset = 0;
8787                         if (bundle_at || hbflag) {
8788                                 /* For data/asconf and hb set time */
8789                                 if (*now_filled == 0) {
8790                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8791                                         *now_filled = 1;
8792                                         *now = net->last_sent_time;
8793                                 } else {
8794                                         net->last_sent_time = *now;
8795                                 }
8796                         }
8797                         if (!no_out_cnt) {
8798                                 *num_out += (ctl_cnt + bundle_at);
8799                         }
8800                         if (bundle_at) {
8801                                 /* setup for a RTO measurement */
8802                                 tsns_sent = data_list[0]->rec.data.TSN_seq;
8803                                 /* fill time if not already filled */
8804                                 if (*now_filled == 0) {
8805                                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8806                                         *now_filled = 1;
8807                                         *now = asoc->time_last_sent;
8808                                 } else {
8809                                         asoc->time_last_sent = *now;
8810                                 }
8811                                 if (net->rto_needed) {
8812                                         data_list[0]->do_rtt = 1;
8813                                         net->rto_needed = 0;
8814                                 }
8815                                 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8816                                 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8817                         }
8818                         if (one_chunk) {
8819                                 break;
8820                         }
8821                 }
8822                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8823                         sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8824                 }
8825         }
8826         if (old_start_at == NULL) {
8827                 old_start_at = start_at;
8828                 start_at = TAILQ_FIRST(&asoc->nets);
8829                 if (old_start_at)
8830                         goto again_one_more_time;
8831         }
8832         /*
8833          * At the end there should be no NON timed chunks hanging on this
8834          * queue.
8835          */
8836         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8837                 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8838         }
8839         if ((*num_out == 0) && (*reason_code == 0)) {
8840                 *reason_code = 4;
8841         } else {
8842                 *reason_code = 5;
8843         }
8844         sctp_clean_up_ctl(stcb, asoc, so_locked);
8845         return (0);
8846 }
8847
8848 void
8849 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8850 {
8851         /*-
8852          * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8853          * the control chunk queue.
8854          */
8855         struct sctp_chunkhdr *hdr;
8856         struct sctp_tmit_chunk *chk;
8857         struct mbuf *mat;
8858
8859         SCTP_TCB_LOCK_ASSERT(stcb);
8860         sctp_alloc_a_chunk(stcb, chk);
8861         if (chk == NULL) {
8862                 /* no memory */
8863                 sctp_m_freem(op_err);
8864                 return;
8865         }
8866         chk->copy_by_ref = 0;
8867         SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8868         if (op_err == NULL) {
8869                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
8870                 return;
8871         }
8872         chk->send_size = 0;
8873         mat = op_err;
8874         while (mat != NULL) {
8875                 chk->send_size += SCTP_BUF_LEN(mat);
8876                 mat = SCTP_BUF_NEXT(mat);
8877         }
8878         chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8879         chk->rec.chunk_id.can_take_data = 1;
8880         chk->sent = SCTP_DATAGRAM_UNSENT;
8881         chk->snd_count = 0;
8882         chk->flags = 0;
8883         chk->asoc = &stcb->asoc;
8884         chk->data = op_err;
8885         chk->whoTo = NULL;
8886         hdr = mtod(op_err, struct sctp_chunkhdr *);
8887         hdr->chunk_type = SCTP_OPERATION_ERROR;
8888         hdr->chunk_flags = 0;
8889         hdr->chunk_length = htons(chk->send_size);
8890         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8891             chk,
8892             sctp_next);
8893         chk->asoc->ctrl_queue_cnt++;
8894 }
8895
8896 int
8897 sctp_send_cookie_echo(struct mbuf *m,
8898     int offset,
8899     struct sctp_tcb *stcb,
8900     struct sctp_nets *net)
8901 {
8902         /*-
8903          * pull out the cookie and put it at the front of the control chunk
8904          * queue.
8905          */
8906         int at;
8907         struct mbuf *cookie;
8908         struct sctp_paramhdr parm, *phdr;
8909         struct sctp_chunkhdr *hdr;
8910         struct sctp_tmit_chunk *chk;
8911         uint16_t ptype, plen;
8912
8913         /* First find the cookie in the param area */
8914         cookie = NULL;
8915         at = offset + sizeof(struct sctp_init_chunk);
8916
8917         SCTP_TCB_LOCK_ASSERT(stcb);
8918         do {
8919                 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8920                 if (phdr == NULL) {
8921                         return (-3);
8922                 }
8923                 ptype = ntohs(phdr->param_type);
8924                 plen = ntohs(phdr->param_length);
8925                 if (ptype == SCTP_STATE_COOKIE) {
8926                         int pad;
8927
8928                         /* found the cookie */
8929                         if ((pad = (plen % 4))) {
8930                                 plen += 4 - pad;
8931                         }
8932                         cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8933                         if (cookie == NULL) {
8934                                 /* No memory */
8935                                 return (-2);
8936                         }
8937 #ifdef SCTP_MBUF_LOGGING
8938                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8939                                 struct mbuf *mat;
8940
8941                                 for (mat = cookie; mat; mat = SCTP_BUF_NEXT(mat)) {
8942                                         if (SCTP_BUF_IS_EXTENDED(mat)) {
8943                                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8944                                         }
8945                                 }
8946                         }
8947 #endif
8948                         break;
8949                 }
8950                 at += SCTP_SIZE32(plen);
8951         } while (phdr);
8952         if (cookie == NULL) {
8953                 /* Did not find the cookie */
8954                 return (-3);
8955         }
8956         /* ok, we got the cookie lets change it into a cookie echo chunk */
8957
8958         /* first the change from param to cookie */
8959         hdr = mtod(cookie, struct sctp_chunkhdr *);
8960         hdr->chunk_type = SCTP_COOKIE_ECHO;
8961         hdr->chunk_flags = 0;
8962         /* get the chunk stuff now and place it in the FRONT of the queue */
8963         sctp_alloc_a_chunk(stcb, chk);
8964         if (chk == NULL) {
8965                 /* no memory */
8966                 sctp_m_freem(cookie);
8967                 return (-5);
8968         }
8969         chk->copy_by_ref = 0;
8970         chk->send_size = plen;
8971         chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8972         chk->rec.chunk_id.can_take_data = 0;
8973         chk->sent = SCTP_DATAGRAM_UNSENT;
8974         chk->snd_count = 0;
8975         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8976         chk->asoc = &stcb->asoc;
8977         chk->data = cookie;
8978         chk->whoTo = net;
8979         atomic_add_int(&chk->whoTo->ref_count, 1);
8980         TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8981         chk->asoc->ctrl_queue_cnt++;
8982         return (0);
8983 }
8984
8985 void
8986 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8987     struct mbuf *m,
8988     int offset,
8989     int chk_length,
8990     struct sctp_nets *net)
8991 {
8992         /*
8993          * take a HB request and make it into a HB ack and send it.
8994          */
8995         struct mbuf *outchain;
8996         struct sctp_chunkhdr *chdr;
8997         struct sctp_tmit_chunk *chk;
8998
8999
9000         if (net == NULL)
9001                 /* must have a net pointer */
9002                 return;
9003
9004         outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
9005         if (outchain == NULL) {
9006                 /* gak out of memory */
9007                 return;
9008         }
9009 #ifdef SCTP_MBUF_LOGGING
9010         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9011                 struct mbuf *mat;
9012
9013                 for (mat = outchain; mat; mat = SCTP_BUF_NEXT(mat)) {
9014                         if (SCTP_BUF_IS_EXTENDED(mat)) {
9015                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
9016                         }
9017                 }
9018         }
9019 #endif
9020         chdr = mtod(outchain, struct sctp_chunkhdr *);
9021         chdr->chunk_type = SCTP_HEARTBEAT_ACK;
9022         chdr->chunk_flags = 0;
9023         if (chk_length % 4) {
9024                 /* need pad */
9025                 uint32_t cpthis = 0;
9026                 int padlen;
9027
9028                 padlen = 4 - (chk_length % 4);
9029                 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
9030         }
9031         sctp_alloc_a_chunk(stcb, chk);
9032         if (chk == NULL) {
9033                 /* no memory */
9034                 sctp_m_freem(outchain);
9035                 return;
9036         }
9037         chk->copy_by_ref = 0;
9038         chk->send_size = chk_length;
9039         chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
9040         chk->rec.chunk_id.can_take_data = 1;
9041         chk->sent = SCTP_DATAGRAM_UNSENT;
9042         chk->snd_count = 0;
9043         chk->flags = 0;
9044         chk->asoc = &stcb->asoc;
9045         chk->data = outchain;
9046         chk->whoTo = net;
9047         atomic_add_int(&chk->whoTo->ref_count, 1);
9048         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9049         chk->asoc->ctrl_queue_cnt++;
9050 }
9051
9052 void
9053 sctp_send_cookie_ack(struct sctp_tcb *stcb)
9054 {
9055         /* formulate and queue a cookie-ack back to sender */
9056         struct mbuf *cookie_ack;
9057         struct sctp_chunkhdr *hdr;
9058         struct sctp_tmit_chunk *chk;
9059
9060         cookie_ack = NULL;
9061         SCTP_TCB_LOCK_ASSERT(stcb);
9062
9063         cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
9064         if (cookie_ack == NULL) {
9065                 /* no mbuf's */
9066                 return;
9067         }
9068         SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
9069         sctp_alloc_a_chunk(stcb, chk);
9070         if (chk == NULL) {
9071                 /* no memory */
9072                 sctp_m_freem(cookie_ack);
9073                 return;
9074         }
9075         chk->copy_by_ref = 0;
9076         chk->send_size = sizeof(struct sctp_chunkhdr);
9077         chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
9078         chk->rec.chunk_id.can_take_data = 1;
9079         chk->sent = SCTP_DATAGRAM_UNSENT;
9080         chk->snd_count = 0;
9081         chk->flags = 0;
9082         chk->asoc = &stcb->asoc;
9083         chk->data = cookie_ack;
9084         if (chk->asoc->last_control_chunk_from != NULL) {
9085                 chk->whoTo = chk->asoc->last_control_chunk_from;
9086                 atomic_add_int(&chk->whoTo->ref_count, 1);
9087         } else {
9088                 chk->whoTo = NULL;
9089         }
9090         hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
9091         hdr->chunk_type = SCTP_COOKIE_ACK;
9092         hdr->chunk_flags = 0;
9093         hdr->chunk_length = htons(chk->send_size);
9094         SCTP_BUF_LEN(cookie_ack) = chk->send_size;
9095         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9096         chk->asoc->ctrl_queue_cnt++;
9097         return;
9098 }
9099
9100
9101 void
9102 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
9103 {
9104         /* formulate and queue a SHUTDOWN-ACK back to the sender */
9105         struct mbuf *m_shutdown_ack;
9106         struct sctp_shutdown_ack_chunk *ack_cp;
9107         struct sctp_tmit_chunk *chk;
9108
9109         m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9110         if (m_shutdown_ack == NULL) {
9111                 /* no mbuf's */
9112                 return;
9113         }
9114         SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
9115         sctp_alloc_a_chunk(stcb, chk);
9116         if (chk == NULL) {
9117                 /* no memory */
9118                 sctp_m_freem(m_shutdown_ack);
9119                 return;
9120         }
9121         chk->copy_by_ref = 0;
9122         chk->send_size = sizeof(struct sctp_chunkhdr);
9123         chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
9124         chk->rec.chunk_id.can_take_data = 1;
9125         chk->sent = SCTP_DATAGRAM_UNSENT;
9126         chk->snd_count = 0;
9127         chk->flags = 0;
9128         chk->asoc = &stcb->asoc;
9129         chk->data = m_shutdown_ack;
9130         chk->whoTo = net;
9131         if (chk->whoTo) {
9132                 atomic_add_int(&chk->whoTo->ref_count, 1);
9133         }
9134         ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
9135         ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
9136         ack_cp->ch.chunk_flags = 0;
9137         ack_cp->ch.chunk_length = htons(chk->send_size);
9138         SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
9139         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9140         chk->asoc->ctrl_queue_cnt++;
9141         return;
9142 }
9143
9144 void
9145 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
9146 {
9147         /* formulate and queue a SHUTDOWN to the sender */
9148         struct mbuf *m_shutdown;
9149         struct sctp_shutdown_chunk *shutdown_cp;
9150         struct sctp_tmit_chunk *chk;
9151
9152         m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
9153         if (m_shutdown == NULL) {
9154                 /* no mbuf's */
9155                 return;
9156         }
9157         SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
9158         sctp_alloc_a_chunk(stcb, chk);
9159         if (chk == NULL) {
9160                 /* no memory */
9161                 sctp_m_freem(m_shutdown);
9162                 return;
9163         }
9164         chk->copy_by_ref = 0;
9165         chk->send_size = sizeof(struct sctp_shutdown_chunk);
9166         chk->rec.chunk_id.id = SCTP_SHUTDOWN;
9167         chk->rec.chunk_id.can_take_data = 1;
9168         chk->sent = SCTP_DATAGRAM_UNSENT;
9169         chk->snd_count = 0;
9170         chk->flags = 0;
9171         chk->asoc = &stcb->asoc;
9172         chk->data = m_shutdown;
9173         chk->whoTo = net;
9174         if (chk->whoTo) {
9175                 atomic_add_int(&chk->whoTo->ref_count, 1);
9176         }
9177         shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
9178         shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
9179         shutdown_cp->ch.chunk_flags = 0;
9180         shutdown_cp->ch.chunk_length = htons(chk->send_size);
9181         shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9182         SCTP_BUF_LEN(m_shutdown) = chk->send_size;
9183         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9184         chk->asoc->ctrl_queue_cnt++;
9185         return;
9186 }
9187
9188 void
9189 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
9190 {
9191         /*
9192          * formulate and queue an ASCONF to the peer. ASCONF parameters
9193          * should be queued on the assoc queue.
9194          */
9195         struct sctp_tmit_chunk *chk;
9196         struct mbuf *m_asconf;
9197         int len;
9198
9199         SCTP_TCB_LOCK_ASSERT(stcb);
9200
9201         if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
9202             (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
9203                 /* can't send a new one if there is one in flight already */
9204                 return;
9205         }
9206         /* compose an ASCONF chunk, maximum length is PMTU */
9207         m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
9208         if (m_asconf == NULL) {
9209                 return;
9210         }
9211         sctp_alloc_a_chunk(stcb, chk);
9212         if (chk == NULL) {
9213                 /* no memory */
9214                 sctp_m_freem(m_asconf);
9215                 return;
9216         }
9217         chk->copy_by_ref = 0;
9218         chk->data = m_asconf;
9219         chk->send_size = len;
9220         chk->rec.chunk_id.id = SCTP_ASCONF;
9221         chk->rec.chunk_id.can_take_data = 0;
9222         chk->sent = SCTP_DATAGRAM_UNSENT;
9223         chk->snd_count = 0;
9224         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9225         chk->asoc = &stcb->asoc;
9226         chk->whoTo = net;
9227         if (chk->whoTo) {
9228                 atomic_add_int(&chk->whoTo->ref_count, 1);
9229         }
9230         TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
9231         chk->asoc->ctrl_queue_cnt++;
9232         return;
9233 }
9234
9235 void
9236 sctp_send_asconf_ack(struct sctp_tcb *stcb)
9237 {
9238         /*
9239          * formulate and queue a asconf-ack back to sender. the asconf-ack
9240          * must be stored in the tcb.
9241          */
9242         struct sctp_tmit_chunk *chk;
9243         struct sctp_asconf_ack *ack, *latest_ack;
9244         struct mbuf *m_ack;
9245         struct sctp_nets *net = NULL;
9246
9247         SCTP_TCB_LOCK_ASSERT(stcb);
9248         /* Get the latest ASCONF-ACK */
9249         latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
9250         if (latest_ack == NULL) {
9251                 return;
9252         }
9253         if (latest_ack->last_sent_to != NULL &&
9254             latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
9255                 /* we're doing a retransmission */
9256                 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
9257                 if (net == NULL) {
9258                         /* no alternate */
9259                         if (stcb->asoc.last_control_chunk_from == NULL) {
9260                                 if (stcb->asoc.alternate) {
9261                                         net = stcb->asoc.alternate;
9262                                 } else {
9263                                         net = stcb->asoc.primary_destination;
9264                                 }
9265                         } else {
9266                                 net = stcb->asoc.last_control_chunk_from;
9267                         }
9268                 }
9269         } else {
9270                 /* normal case */
9271                 if (stcb->asoc.last_control_chunk_from == NULL) {
9272                         if (stcb->asoc.alternate) {
9273                                 net = stcb->asoc.alternate;
9274                         } else {
9275                                 net = stcb->asoc.primary_destination;
9276                         }
9277                 } else {
9278                         net = stcb->asoc.last_control_chunk_from;
9279                 }
9280         }
9281         latest_ack->last_sent_to = net;
9282
9283         TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
9284                 if (ack->data == NULL) {
9285                         continue;
9286                 }
9287                 /* copy the asconf_ack */
9288                 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
9289                 if (m_ack == NULL) {
9290                         /* couldn't copy it */
9291                         return;
9292                 }
9293 #ifdef SCTP_MBUF_LOGGING
9294                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9295                         struct mbuf *mat;
9296
9297                         for (mat = m_ack; mat; mat = SCTP_BUF_NEXT(mat)) {
9298                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
9299                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
9300                                 }
9301                         }
9302                 }
9303 #endif
9304
9305                 sctp_alloc_a_chunk(stcb, chk);
9306                 if (chk == NULL) {
9307                         /* no memory */
9308                         if (m_ack)
9309                                 sctp_m_freem(m_ack);
9310                         return;
9311                 }
9312                 chk->copy_by_ref = 0;
9313
9314                 chk->whoTo = net;
9315                 if (chk->whoTo) {
9316                         atomic_add_int(&chk->whoTo->ref_count, 1);
9317                 }
9318                 chk->data = m_ack;
9319                 chk->send_size = 0;
9320                 /* Get size */
9321                 chk->send_size = ack->len;
9322                 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
9323                 chk->rec.chunk_id.can_take_data = 1;
9324                 chk->sent = SCTP_DATAGRAM_UNSENT;
9325                 chk->snd_count = 0;
9326                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;  /* XXX */
9327                 chk->asoc = &stcb->asoc;
9328
9329                 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9330                 chk->asoc->ctrl_queue_cnt++;
9331         }
9332         return;
9333 }
9334
9335
9336 static int
9337 sctp_chunk_retransmission(struct sctp_inpcb *inp,
9338     struct sctp_tcb *stcb,
9339     struct sctp_association *asoc,
9340     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
9341 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9342     SCTP_UNUSED
9343 #endif
9344 )
9345 {
9346         /*-
9347          * send out one MTU of retransmission. If fast_retransmit is
9348          * happening we ignore the cwnd. Otherwise we obey the cwnd and
9349          * rwnd. For a Cookie or Asconf in the control chunk queue we
9350          * retransmit them by themselves.
9351          *
9352          * For data chunks we will pick out the lowest TSN's in the sent_queue
9353          * marked for resend and bundle them all together (up to a MTU of
9354          * destination). The address to send to should have been
9355          * selected/changed where the retransmission was marked (i.e. in FR
9356          * or t3-timeout routines).
9357          */
9358         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9359         struct sctp_tmit_chunk *chk, *fwd;
9360         struct mbuf *m, *endofchain;
9361         struct sctp_nets *net = NULL;
9362         uint32_t tsns_sent = 0;
9363         int no_fragmentflg, bundle_at, cnt_thru;
9364         unsigned int mtu;
9365         int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
9366         struct sctp_auth_chunk *auth = NULL;
9367         uint32_t auth_offset = 0;
9368         uint16_t auth_keyid;
9369         int override_ok = 1;
9370         int data_auth_reqd = 0;
9371         uint32_t dmtu = 0;
9372
9373         SCTP_TCB_LOCK_ASSERT(stcb);
9374         tmr_started = ctl_cnt = bundle_at = error = 0;
9375         no_fragmentflg = 1;
9376         fwd_tsn = 0;
9377         *cnt_out = 0;
9378         fwd = NULL;
9379         endofchain = m = NULL;
9380         auth_keyid = stcb->asoc.authinfo.active_keyid;
9381 #ifdef SCTP_AUDITING_ENABLED
9382         sctp_audit_log(0xC3, 1);
9383 #endif
9384         if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
9385             (TAILQ_EMPTY(&asoc->control_send_queue))) {
9386                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
9387                     asoc->sent_queue_retran_cnt);
9388                 asoc->sent_queue_cnt = 0;
9389                 asoc->sent_queue_cnt_removeable = 0;
9390                 /* send back 0/0 so we enter normal transmission */
9391                 *cnt_out = 0;
9392                 return (0);
9393         }
9394         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9395                 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
9396                     (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
9397                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
9398                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
9399                                 continue;
9400                         }
9401                         if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
9402                                 if (chk != asoc->str_reset) {
9403                                         /*
9404                                          * not eligible for retran if its
9405                                          * not ours
9406                                          */
9407                                         continue;
9408                                 }
9409                         }
9410                         ctl_cnt++;
9411                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9412                                 fwd_tsn = 1;
9413                         }
9414                         /*
9415                          * Add an AUTH chunk, if chunk requires it save the
9416                          * offset into the chain for AUTH
9417                          */
9418                         if ((auth == NULL) &&
9419                             (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9420                             stcb->asoc.peer_auth_chunks))) {
9421                                 m = sctp_add_auth_chunk(m, &endofchain,
9422                                     &auth, &auth_offset,
9423                                     stcb,
9424                                     chk->rec.chunk_id.id);
9425                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9426                         }
9427                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9428                         break;
9429                 }
9430         }
9431         one_chunk = 0;
9432         cnt_thru = 0;
9433         /* do we have control chunks to retransmit? */
9434         if (m != NULL) {
9435                 /* Start a timer no matter if we suceed or fail */
9436                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9437                         sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9438                 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9439                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9440                 chk->snd_count++;       /* update our count */
9441                 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9442                     (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9443                     auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9444                     no_fragmentflg, 0, 0,
9445                     inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9446                     chk->whoTo->port, so_locked, NULL, NULL))) {
9447                         SCTP_STAT_INCR(sctps_lowlevelerr);
9448                         return (error);
9449                 }
9450                 endofchain = NULL;
9451                 auth = NULL;
9452                 auth_offset = 0;
9453                 /*
9454                  * We don't want to mark the net->sent time here since this
9455                  * we use this for HB and retrans cannot measure RTT
9456                  */
9457                 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9458                 *cnt_out += 1;
9459                 chk->sent = SCTP_DATAGRAM_SENT;
9460                 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9461                 if (fwd_tsn == 0) {
9462                         return (0);
9463                 } else {
9464                         /* Clean up the fwd-tsn list */
9465                         sctp_clean_up_ctl(stcb, asoc, so_locked);
9466                         return (0);
9467                 }
9468         }
9469         /*
9470          * Ok, it is just data retransmission we need to do or that and a
9471          * fwd-tsn with it all.
9472          */
9473         if (TAILQ_EMPTY(&asoc->sent_queue)) {
9474                 return (SCTP_RETRAN_DONE);
9475         }
9476         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9477             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9478                 /* not yet open, resend the cookie and that is it */
9479                 return (1);
9480         }
9481 #ifdef SCTP_AUDITING_ENABLED
9482         sctp_auditing(20, inp, stcb, NULL);
9483 #endif
9484         data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9485         TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9486                 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9487                         /* No, not sent to this net or not ready for rtx */
9488                         continue;
9489                 }
9490                 if (chk->data == NULL) {
9491                         SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9492                             chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
9493                         continue;
9494                 }
9495                 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9496                     (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9497                         /* Gak, we have exceeded max unlucky retran, abort! */
9498                         SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9499                             chk->snd_count,
9500                             SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9501                         atomic_add_int(&stcb->asoc.refcnt, 1);
9502                         sctp_abort_an_association(stcb->sctp_ep, stcb, NULL, so_locked);
9503                         SCTP_TCB_LOCK(stcb);
9504                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
9505                         return (SCTP_RETRAN_EXIT);
9506                 }
9507                 /* pick up the net */
9508                 net = chk->whoTo;
9509                 switch (net->ro._l_addr.sa.sa_family) {
9510 #ifdef INET
9511                 case AF_INET:
9512                         mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9513                         break;
9514 #endif
9515 #ifdef INET6
9516                 case AF_INET6:
9517                         mtu = net->mtu - SCTP_MIN_OVERHEAD;
9518                         break;
9519 #endif
9520                 default:
9521                         /* TSNH */
9522                         mtu = net->mtu;
9523                         break;
9524                 }
9525
9526                 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9527                         /* No room in peers rwnd */
9528                         uint32_t tsn;
9529
9530                         tsn = asoc->last_acked_seq + 1;
9531                         if (tsn == chk->rec.data.TSN_seq) {
9532                                 /*
9533                                  * we make a special exception for this
9534                                  * case. The peer has no rwnd but is missing
9535                                  * the lowest chunk.. which is probably what
9536                                  * is holding up the rwnd.
9537                                  */
9538                                 goto one_chunk_around;
9539                         }
9540                         return (1);
9541                 }
9542 one_chunk_around:
9543                 if (asoc->peers_rwnd < mtu) {
9544                         one_chunk = 1;
9545                         if ((asoc->peers_rwnd == 0) &&
9546                             (asoc->total_flight == 0)) {
9547                                 chk->window_probe = 1;
9548                                 chk->whoTo->window_probe = 1;
9549                         }
9550                 }
9551 #ifdef SCTP_AUDITING_ENABLED
9552                 sctp_audit_log(0xC3, 2);
9553 #endif
9554                 bundle_at = 0;
9555                 m = NULL;
9556                 net->fast_retran_ip = 0;
9557                 if (chk->rec.data.doing_fast_retransmit == 0) {
9558                         /*
9559                          * if no FR in progress skip destination that have
9560                          * flight_size > cwnd.
9561                          */
9562                         if (net->flight_size >= net->cwnd) {
9563                                 continue;
9564                         }
9565                 } else {
9566                         /*
9567                          * Mark the destination net to have FR recovery
9568                          * limits put on it.
9569                          */
9570                         *fr_done = 1;
9571                         net->fast_retran_ip = 1;
9572                 }
9573
9574                 /*
9575                  * if no AUTH is yet included and this chunk requires it,
9576                  * make sure to account for it.  We don't apply the size
9577                  * until the AUTH chunk is actually added below in case
9578                  * there is no room for this chunk.
9579                  */
9580                 if (data_auth_reqd && (auth == NULL)) {
9581                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9582                 } else
9583                         dmtu = 0;
9584
9585                 if ((chk->send_size <= (mtu - dmtu)) ||
9586                     (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9587                         /* ok we will add this one */
9588                         if (data_auth_reqd) {
9589                                 if (auth == NULL) {
9590                                         m = sctp_add_auth_chunk(m,
9591                                             &endofchain,
9592                                             &auth,
9593                                             &auth_offset,
9594                                             stcb,
9595                                             SCTP_DATA);
9596                                         auth_keyid = chk->auth_keyid;
9597                                         override_ok = 0;
9598                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9599                                 } else if (override_ok) {
9600                                         auth_keyid = chk->auth_keyid;
9601                                         override_ok = 0;
9602                                 } else if (chk->auth_keyid != auth_keyid) {
9603                                         /* different keyid, so done bundling */
9604                                         break;
9605                                 }
9606                         }
9607                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9608                         if (m == NULL) {
9609                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9610                                 return (ENOMEM);
9611                         }
9612                         /* Do clear IP_DF ? */
9613                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9614                                 no_fragmentflg = 0;
9615                         }
9616                         /* upate our MTU size */
9617                         if (mtu > (chk->send_size + dmtu))
9618                                 mtu -= (chk->send_size + dmtu);
9619                         else
9620                                 mtu = 0;
9621                         data_list[bundle_at++] = chk;
9622                         if (one_chunk && (asoc->total_flight <= 0)) {
9623                                 SCTP_STAT_INCR(sctps_windowprobed);
9624                         }
9625                 }
9626                 if (one_chunk == 0) {
9627                         /*
9628                          * now are there anymore forward from chk to pick
9629                          * up?
9630                          */
9631                         for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
9632                                 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9633                                         /* Nope, not for retran */
9634                                         continue;
9635                                 }
9636                                 if (fwd->whoTo != net) {
9637                                         /* Nope, not the net in question */
9638                                         continue;
9639                                 }
9640                                 if (data_auth_reqd && (auth == NULL)) {
9641                                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9642                                 } else
9643                                         dmtu = 0;
9644                                 if (fwd->send_size <= (mtu - dmtu)) {
9645                                         if (data_auth_reqd) {
9646                                                 if (auth == NULL) {
9647                                                         m = sctp_add_auth_chunk(m,
9648                                                             &endofchain,
9649                                                             &auth,
9650                                                             &auth_offset,
9651                                                             stcb,
9652                                                             SCTP_DATA);
9653                                                         auth_keyid = fwd->auth_keyid;
9654                                                         override_ok = 0;
9655                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9656                                                 } else if (override_ok) {
9657                                                         auth_keyid = fwd->auth_keyid;
9658                                                         override_ok = 0;
9659                                                 } else if (fwd->auth_keyid != auth_keyid) {
9660                                                         /*
9661                                                          * different keyid,
9662                                                          * so done bundling
9663                                                          */
9664                                                         break;
9665                                                 }
9666                                         }
9667                                         m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9668                                         if (m == NULL) {
9669                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9670                                                 return (ENOMEM);
9671                                         }
9672                                         /* Do clear IP_DF ? */
9673                                         if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9674                                                 no_fragmentflg = 0;
9675                                         }
9676                                         /* upate our MTU size */
9677                                         if (mtu > (fwd->send_size + dmtu))
9678                                                 mtu -= (fwd->send_size + dmtu);
9679                                         else
9680                                                 mtu = 0;
9681                                         data_list[bundle_at++] = fwd;
9682                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9683                                                 break;
9684                                         }
9685                                 } else {
9686                                         /* can't fit so we are done */
9687                                         break;
9688                                 }
9689                         }
9690                 }
9691                 /* Is there something to send for this destination? */
9692                 if (m) {
9693                         /*
9694                          * No matter if we fail/or suceed we should start a
9695                          * timer. A failure is like a lost IP packet :-)
9696                          */
9697                         if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9698                                 /*
9699                                  * no timer running on this destination
9700                                  * restart it.
9701                                  */
9702                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9703                                 tmr_started = 1;
9704                         }
9705                         /* Now lets send it, if there is anything to send :> */
9706                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9707                             (struct sockaddr *)&net->ro._l_addr, m,
9708                             auth_offset, auth, auth_keyid,
9709                             no_fragmentflg, 0, 0,
9710                             inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9711                             net->port, so_locked, NULL, NULL))) {
9712                                 /* error, we could not output */
9713                                 SCTP_STAT_INCR(sctps_lowlevelerr);
9714                                 return (error);
9715                         }
9716                         endofchain = NULL;
9717                         auth = NULL;
9718                         auth_offset = 0;
9719                         /* For HB's */
9720                         /*
9721                          * We don't want to mark the net->sent time here
9722                          * since this we use this for HB and retrans cannot
9723                          * measure RTT
9724                          */
9725                         /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9726
9727                         /* For auto-close */
9728                         cnt_thru++;
9729                         if (*now_filled == 0) {
9730                                 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9731                                 *now = asoc->time_last_sent;
9732                                 *now_filled = 1;
9733                         } else {
9734                                 asoc->time_last_sent = *now;
9735                         }
9736                         *cnt_out += bundle_at;
9737 #ifdef SCTP_AUDITING_ENABLED
9738                         sctp_audit_log(0xC4, bundle_at);
9739 #endif
9740                         if (bundle_at) {
9741                                 tsns_sent = data_list[0]->rec.data.TSN_seq;
9742                         }
9743                         for (i = 0; i < bundle_at; i++) {
9744                                 SCTP_STAT_INCR(sctps_sendretransdata);
9745                                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9746                                 /*
9747                                  * When we have a revoked data, and we
9748                                  * retransmit it, then we clear the revoked
9749                                  * flag since this flag dictates if we
9750                                  * subtracted from the fs
9751                                  */
9752                                 if (data_list[i]->rec.data.chunk_was_revoked) {
9753                                         /* Deflate the cwnd */
9754                                         data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9755                                         data_list[i]->rec.data.chunk_was_revoked = 0;
9756                                 }
9757                                 data_list[i]->snd_count++;
9758                                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9759                                 /* record the time */
9760                                 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9761                                 if (data_list[i]->book_size_scale) {
9762                                         /*
9763                                          * need to double the book size on
9764                                          * this one
9765                                          */
9766                                         data_list[i]->book_size_scale = 0;
9767                                         /*
9768                                          * Since we double the booksize, we
9769                                          * must also double the output queue
9770                                          * size, since this get shrunk when
9771                                          * we free by this amount.
9772                                          */
9773                                         atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9774                                         data_list[i]->book_size *= 2;
9775
9776
9777                                 } else {
9778                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9779                                                 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9780                                                     asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9781                                         }
9782                                         asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9783                                             (uint32_t) (data_list[i]->send_size +
9784                                             SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9785                                 }
9786                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9787                                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9788                                             data_list[i]->whoTo->flight_size,
9789                                             data_list[i]->book_size,
9790                                             (uintptr_t) data_list[i]->whoTo,
9791                                             data_list[i]->rec.data.TSN_seq);
9792                                 }
9793                                 sctp_flight_size_increase(data_list[i]);
9794                                 sctp_total_flight_increase(stcb, data_list[i]);
9795                                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9796                                         /* SWS sender side engages */
9797                                         asoc->peers_rwnd = 0;
9798                                 }
9799                                 if ((i == 0) &&
9800                                     (data_list[i]->rec.data.doing_fast_retransmit)) {
9801                                         SCTP_STAT_INCR(sctps_sendfastretrans);
9802                                         if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9803                                             (tmr_started == 0)) {
9804                                                 /*-
9805                                                  * ok we just fast-retrans'd
9806                                                  * the lowest TSN, i.e the
9807                                                  * first on the list. In
9808                                                  * this case we want to give
9809                                                  * some more time to get a
9810                                                  * SACK back without a
9811                                                  * t3-expiring.
9812                                                  */
9813                                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9814                                                     SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9815                                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9816                                         }
9817                                 }
9818                         }
9819                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9820                                 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9821                         }
9822 #ifdef SCTP_AUDITING_ENABLED
9823                         sctp_auditing(21, inp, stcb, NULL);
9824 #endif
9825                 } else {
9826                         /* None will fit */
9827                         return (1);
9828                 }
9829                 if (asoc->sent_queue_retran_cnt <= 0) {
9830                         /* all done we have no more to retran */
9831                         asoc->sent_queue_retran_cnt = 0;
9832                         break;
9833                 }
9834                 if (one_chunk) {
9835                         /* No more room in rwnd */
9836                         return (1);
9837                 }
9838                 /* stop the for loop here. we sent out a packet */
9839                 break;
9840         }
9841         return (0);
9842 }
9843
9844 static void
9845 sctp_timer_validation(struct sctp_inpcb *inp,
9846     struct sctp_tcb *stcb,
9847     struct sctp_association *asoc)
9848 {
9849         struct sctp_nets *net;
9850
9851         /* Validate that a timer is running somewhere */
9852         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9853                 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9854                         /* Here is a timer */
9855                         return;
9856                 }
9857         }
9858         SCTP_TCB_LOCK_ASSERT(stcb);
9859         /* Gak, we did not have a timer somewhere */
9860         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9861         if (asoc->alternate) {
9862                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
9863         } else {
9864                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9865         }
9866         return;
9867 }
9868
9869 void
9870 sctp_chunk_output(struct sctp_inpcb *inp,
9871     struct sctp_tcb *stcb,
9872     int from_where,
9873     int so_locked
9874 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9875     SCTP_UNUSED
9876 #endif
9877 )
9878 {
9879         /*-
9880          * Ok this is the generic chunk service queue. we must do the
9881          * following:
9882          * - See if there are retransmits pending, if so we must
9883          *   do these first.
9884          * - Service the stream queue that is next, moving any
9885          *   message (note I must get a complete message i.e.
9886          *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9887          *   TSN's
9888          * - Check to see if the cwnd/rwnd allows any output, if so we
9889          *   go ahead and fomulate and send the low level chunks. Making sure
9890          *   to combine any control in the control chunk queue also.
9891          */
9892         struct sctp_association *asoc;
9893         struct sctp_nets *net;
9894         int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0;
9895         unsigned int burst_cnt = 0;
9896         struct timeval now;
9897         int now_filled = 0;
9898         int nagle_on;
9899         int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9900         int un_sent = 0;
9901         int fr_done;
9902         unsigned int tot_frs = 0;
9903
9904         asoc = &stcb->asoc;
9905         /* The Nagle algorithm is only applied when handling a send call. */
9906         if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9907                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9908                         nagle_on = 0;
9909                 } else {
9910                         nagle_on = 1;
9911                 }
9912         } else {
9913                 nagle_on = 0;
9914         }
9915         SCTP_TCB_LOCK_ASSERT(stcb);
9916
9917         un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9918
9919         if ((un_sent <= 0) &&
9920             (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9921             (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9922             (asoc->sent_queue_retran_cnt == 0)) {
9923                 /* Nothing to do unless there is something to be sent left */
9924                 return;
9925         }
9926         /*
9927          * Do we have something to send, data or control AND a sack timer
9928          * running, if so piggy-back the sack.
9929          */
9930         if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9931                 sctp_send_sack(stcb, so_locked);
9932                 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9933         }
9934         while (asoc->sent_queue_retran_cnt) {
9935                 /*-
9936                  * Ok, it is retransmission time only, we send out only ONE
9937                  * packet with a single call off to the retran code.
9938                  */
9939                 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9940                         /*-
9941                          * Special hook for handling cookiess discarded
9942                          * by peer that carried data. Send cookie-ack only
9943                          * and then the next call with get the retran's.
9944                          */
9945                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9946                             from_where,
9947                             &now, &now_filled, frag_point, so_locked);
9948                         return;
9949                 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9950                         /* if its not from a HB then do it */
9951                         fr_done = 0;
9952                         ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9953                         if (fr_done) {
9954                                 tot_frs++;
9955                         }
9956                 } else {
9957                         /*
9958                          * its from any other place, we don't allow retran
9959                          * output (only control)
9960                          */
9961                         ret = 1;
9962                 }
9963                 if (ret > 0) {
9964                         /* Can't send anymore */
9965                         /*-
9966                          * now lets push out control by calling med-level
9967                          * output once. this assures that we WILL send HB's
9968                          * if queued too.
9969                          */
9970                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9971                             from_where,
9972                             &now, &now_filled, frag_point, so_locked);
9973 #ifdef SCTP_AUDITING_ENABLED
9974                         sctp_auditing(8, inp, stcb, NULL);
9975 #endif
9976                         sctp_timer_validation(inp, stcb, asoc);
9977                         return;
9978                 }
9979                 if (ret < 0) {
9980                         /*-
9981                          * The count was off.. retran is not happening so do
9982                          * the normal retransmission.
9983                          */
9984 #ifdef SCTP_AUDITING_ENABLED
9985                         sctp_auditing(9, inp, stcb, NULL);
9986 #endif
9987                         if (ret == SCTP_RETRAN_EXIT) {
9988                                 return;
9989                         }
9990                         break;
9991                 }
9992                 if (from_where == SCTP_OUTPUT_FROM_T3) {
9993                         /* Only one transmission allowed out of a timeout */
9994 #ifdef SCTP_AUDITING_ENABLED
9995                         sctp_auditing(10, inp, stcb, NULL);
9996 #endif
9997                         /* Push out any control */
9998                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9999                             &now, &now_filled, frag_point, so_locked);
10000                         return;
10001                 }
10002                 if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
10003                         /* Hit FR burst limit */
10004                         return;
10005                 }
10006                 if ((num_out == 0) && (ret == 0)) {
10007                         /* No more retrans to send */
10008                         break;
10009                 }
10010         }
10011 #ifdef SCTP_AUDITING_ENABLED
10012         sctp_auditing(12, inp, stcb, NULL);
10013 #endif
10014         /* Check for bad destinations, if they exist move chunks around. */
10015         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10016                 if (!(net->dest_state & SCTP_ADDR_REACHABLE)) {
10017                         /*-
10018                          * if possible move things off of this address we
10019                          * still may send below due to the dormant state but
10020                          * we try to find an alternate address to send to
10021                          * and if we have one we move all queued data on the
10022                          * out wheel to this alternate address.
10023                          */
10024                         if (net->ref_count > 1)
10025                                 sctp_move_chunks_from_net(stcb, net);
10026                 } else {
10027                         /*-
10028                          * if ((asoc->sat_network) || (net->addr_is_local))
10029                          * { burst_limit = asoc->max_burst *
10030                          * SCTP_SAT_NETWORK_BURST_INCR; }
10031                          */
10032                         if (asoc->max_burst > 0) {
10033                                 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10034                                         if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10035                                                 /*
10036                                                  * JRS - Use the congestion
10037                                                  * control given in the
10038                                                  * congestion control module
10039                                                  */
10040                                                 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10041                                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10042                                                         sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10043                                                 }
10044                                                 SCTP_STAT_INCR(sctps_maxburstqueued);
10045                                         }
10046                                         net->fast_retran_ip = 0;
10047                                 } else {
10048                                         if (net->flight_size == 0) {
10049                                                 /*
10050                                                  * Should be decaying the
10051                                                  * cwnd here
10052                                                  */
10053                                                 ;
10054                                         }
10055                                 }
10056                         }
10057                 }
10058
10059         }
10060         burst_cnt = 0;
10061         do {
10062                 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10063                     &reason_code, 0, from_where,
10064                     &now, &now_filled, frag_point, so_locked);
10065                 if (error) {
10066                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10067                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10068                                 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10069                         }
10070                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10071                                 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10072                                 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10073                         }
10074                         break;
10075                 }
10076                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10077
10078                 tot_out += num_out;
10079                 burst_cnt++;
10080                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10081                         sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10082                         if (num_out == 0) {
10083                                 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10084                         }
10085                 }
10086                 if (nagle_on) {
10087                         /*
10088                          * When the Nagle algorithm is used, look at how
10089                          * much is unsent, then if its smaller than an MTU
10090                          * and we have data in flight we stop, except if we
10091                          * are handling a fragmented user message.
10092                          */
10093                         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
10094                             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
10095                         if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10096                             (stcb->asoc.total_flight > 0) &&
10097                             ((stcb->asoc.locked_on_sending == NULL) ||
10098                             sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
10099                                 break;
10100                         }
10101                 }
10102                 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10103                     TAILQ_EMPTY(&asoc->send_queue) &&
10104                     stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc)) {
10105                         /* Nothing left to send */
10106                         break;
10107                 }
10108                 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10109                         /* Nothing left to send */
10110                         break;
10111                 }
10112         } while (num_out &&
10113             ((asoc->max_burst == 0) ||
10114             SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10115             (burst_cnt < asoc->max_burst)));
10116
10117         if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10118                 if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10119                         SCTP_STAT_INCR(sctps_maxburstqueued);
10120                         asoc->burst_limit_applied = 1;
10121                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10122                                 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10123                         }
10124                 } else {
10125                         asoc->burst_limit_applied = 0;
10126                 }
10127         }
10128         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10129                 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10130         }
10131         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10132             tot_out);
10133
10134         /*-
10135          * Now we need to clean up the control chunk chain if a ECNE is on
10136          * it. It must be marked as UNSENT again so next call will continue
10137          * to send it until such time that we get a CWR, to remove it.
10138          */
10139         if (stcb->asoc.ecn_echo_cnt_onq)
10140                 sctp_fix_ecn_echo(asoc);
10141         return;
10142 }
10143
10144
10145 int
10146 sctp_output(
10147     struct sctp_inpcb *inp,
10148     struct mbuf *m,
10149     struct sockaddr *addr,
10150     struct mbuf *control,
10151     struct thread *p,
10152     int flags)
10153 {
10154         if (inp == NULL) {
10155                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10156                 return (EINVAL);
10157         }
10158         if (inp->sctp_socket == NULL) {
10159                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10160                 return (EINVAL);
10161         }
10162         return (sctp_sosend(inp->sctp_socket,
10163             addr,
10164             (struct uio *)NULL,
10165             m,
10166             control,
10167             flags, p
10168             ));
10169 }
10170
10171 void
10172 send_forward_tsn(struct sctp_tcb *stcb,
10173     struct sctp_association *asoc)
10174 {
10175         struct sctp_tmit_chunk *chk;
10176         struct sctp_forward_tsn_chunk *fwdtsn;
10177         uint32_t advance_peer_ack_point;
10178
10179         SCTP_TCB_LOCK_ASSERT(stcb);
10180         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10181                 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10182                         /* mark it to unsent */
10183                         chk->sent = SCTP_DATAGRAM_UNSENT;
10184                         chk->snd_count = 0;
10185                         /* Do we correct its output location? */
10186                         if (chk->whoTo) {
10187                                 sctp_free_remote_addr(chk->whoTo);
10188                                 chk->whoTo = NULL;
10189                         }
10190                         goto sctp_fill_in_rest;
10191                 }
10192         }
10193         /* Ok if we reach here we must build one */
10194         sctp_alloc_a_chunk(stcb, chk);
10195         if (chk == NULL) {
10196                 return;
10197         }
10198         asoc->fwd_tsn_cnt++;
10199         chk->copy_by_ref = 0;
10200         chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10201         chk->rec.chunk_id.can_take_data = 0;
10202         chk->asoc = asoc;
10203         chk->whoTo = NULL;
10204         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
10205         if (chk->data == NULL) {
10206                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10207                 return;
10208         }
10209         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10210         chk->sent = SCTP_DATAGRAM_UNSENT;
10211         chk->snd_count = 0;
10212         TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10213         asoc->ctrl_queue_cnt++;
10214 sctp_fill_in_rest:
10215         /*-
10216          * Here we go through and fill out the part that deals with
10217          * stream/seq of the ones we skip.
10218          */
10219         SCTP_BUF_LEN(chk->data) = 0;
10220         {
10221                 struct sctp_tmit_chunk *at, *tp1, *last;
10222                 struct sctp_strseq *strseq;
10223                 unsigned int cnt_of_space, i, ovh;
10224                 unsigned int space_needed;
10225                 unsigned int cnt_of_skipped = 0;
10226
10227                 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10228                         if (at->sent != SCTP_FORWARD_TSN_SKIP) {
10229                                 /* no more to look at */
10230                                 break;
10231                         }
10232                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10233                                 /* We don't report these */
10234                                 continue;
10235                         }
10236                         cnt_of_skipped++;
10237                 }
10238                 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10239                     (cnt_of_skipped * sizeof(struct sctp_strseq)));
10240
10241                 cnt_of_space = M_TRAILINGSPACE(chk->data);
10242
10243                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10244                         ovh = SCTP_MIN_OVERHEAD;
10245                 } else {
10246                         ovh = SCTP_MIN_V4_OVERHEAD;
10247                 }
10248                 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
10249                         /* trim to a mtu size */
10250                         cnt_of_space = asoc->smallest_mtu - ovh;
10251                 }
10252                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10253                         sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10254                             0xff, 0, cnt_of_skipped,
10255                             asoc->advanced_peer_ack_point);
10256
10257                 }
10258                 advance_peer_ack_point = asoc->advanced_peer_ack_point;
10259                 if (cnt_of_space < space_needed) {
10260                         /*-
10261                          * ok we must trim down the chunk by lowering the
10262                          * advance peer ack point.
10263                          */
10264                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10265                                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10266                                     0xff, 0xff, cnt_of_space,
10267                                     space_needed);
10268                         }
10269                         cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
10270                         cnt_of_skipped /= sizeof(struct sctp_strseq);
10271                         /*-
10272                          * Go through and find the TSN that will be the one
10273                          * we report.
10274                          */
10275                         at = TAILQ_FIRST(&asoc->sent_queue);
10276                         if (at != NULL) {
10277                                 for (i = 0; i < cnt_of_skipped; i++) {
10278                                         tp1 = TAILQ_NEXT(at, sctp_next);
10279                                         if (tp1 == NULL) {
10280                                                 break;
10281                                         }
10282                                         at = tp1;
10283                                 }
10284                         }
10285                         if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10286                                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10287                                     0xff, cnt_of_skipped, at->rec.data.TSN_seq,
10288                                     asoc->advanced_peer_ack_point);
10289                         }
10290                         last = at;
10291                         /*-
10292                          * last now points to last one I can report, update
10293                          * peer ack point
10294                          */
10295                         if (last)
10296                                 advance_peer_ack_point = last->rec.data.TSN_seq;
10297                         space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10298                             cnt_of_skipped * sizeof(struct sctp_strseq);
10299                 }
10300                 chk->send_size = space_needed;
10301                 /* Setup the chunk */
10302                 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
10303                 fwdtsn->ch.chunk_length = htons(chk->send_size);
10304                 fwdtsn->ch.chunk_flags = 0;
10305                 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
10306                 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
10307                 SCTP_BUF_LEN(chk->data) = chk->send_size;
10308                 fwdtsn++;
10309                 /*-
10310                  * Move pointer to after the fwdtsn and transfer to the
10311                  * strseq pointer.
10312                  */
10313                 strseq = (struct sctp_strseq *)fwdtsn;
10314                 /*-
10315                  * Now populate the strseq list. This is done blindly
10316                  * without pulling out duplicate stream info. This is
10317                  * inefficent but won't harm the process since the peer will
10318                  * look at these in sequence and will thus release anything.
10319                  * It could mean we exceed the PMTU and chop off some that
10320                  * we could have included.. but this is unlikely (aka 1432/4
10321                  * would mean 300+ stream seq's would have to be reported in
10322                  * one FWD-TSN. With a bit of work we can later FIX this to
10323                  * optimize and pull out duplcates.. but it does add more
10324                  * overhead. So for now... not!
10325                  */
10326                 at = TAILQ_FIRST(&asoc->sent_queue);
10327                 for (i = 0; i < cnt_of_skipped; i++) {
10328                         tp1 = TAILQ_NEXT(at, sctp_next);
10329                         if (tp1 == NULL)
10330                                 break;
10331                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10332                                 /* We don't report these */
10333                                 i--;
10334                                 at = tp1;
10335                                 continue;
10336                         }
10337                         if (at->rec.data.TSN_seq == advance_peer_ack_point) {
10338                                 at->rec.data.fwd_tsn_cnt = 0;
10339                         }
10340                         strseq->stream = ntohs(at->rec.data.stream_number);
10341                         strseq->sequence = ntohs(at->rec.data.stream_seq);
10342                         strseq++;
10343                         at = tp1;
10344                 }
10345         }
10346         return;
10347 }
10348
10349 void
10350 sctp_send_sack(struct sctp_tcb *stcb, int so_locked
10351 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10352     SCTP_UNUSED
10353 #endif
10354 )
10355 {
10356         /*-
10357          * Queue up a SACK or NR-SACK in the control queue.
10358          * We must first check to see if a SACK or NR-SACK is
10359          * somehow on the control queue.
10360          * If so, we will take and and remove the old one.
10361          */
10362         struct sctp_association *asoc;
10363         struct sctp_tmit_chunk *chk, *a_chk;
10364         struct sctp_sack_chunk *sack;
10365         struct sctp_nr_sack_chunk *nr_sack;
10366         struct sctp_gap_ack_block *gap_descriptor;
10367         struct sack_track *selector;
10368         int mergeable = 0;
10369         int offset;
10370         caddr_t limit;
10371         uint32_t *dup;
10372         int limit_reached = 0;
10373         unsigned int i, siz, j;
10374         unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10375         int num_dups = 0;
10376         int space_req;
10377         uint32_t highest_tsn;
10378         uint8_t flags;
10379         uint8_t type;
10380         uint8_t tsn_map;
10381
10382         if ((stcb->asoc.sctp_nr_sack_on_off == 1) &&
10383             (stcb->asoc.peer_supports_nr_sack == 1)) {
10384                 type = SCTP_NR_SELECTIVE_ACK;
10385         } else {
10386                 type = SCTP_SELECTIVE_ACK;
10387         }
10388         a_chk = NULL;
10389         asoc = &stcb->asoc;
10390         SCTP_TCB_LOCK_ASSERT(stcb);
10391         if (asoc->last_data_chunk_from == NULL) {
10392                 /* Hmm we never received anything */
10393                 return;
10394         }
10395         sctp_slide_mapping_arrays(stcb);
10396         sctp_set_rwnd(stcb, asoc);
10397         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10398                 if (chk->rec.chunk_id.id == type) {
10399                         /* Hmm, found a sack already on queue, remove it */
10400                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10401                         asoc->ctrl_queue_cnt--;
10402                         a_chk = chk;
10403                         if (a_chk->data) {
10404                                 sctp_m_freem(a_chk->data);
10405                                 a_chk->data = NULL;
10406                         }
10407                         if (a_chk->whoTo) {
10408                                 sctp_free_remote_addr(a_chk->whoTo);
10409                                 a_chk->whoTo = NULL;
10410                         }
10411                         break;
10412                 }
10413         }
10414         if (a_chk == NULL) {
10415                 sctp_alloc_a_chunk(stcb, a_chk);
10416                 if (a_chk == NULL) {
10417                         /* No memory so we drop the idea, and set a timer */
10418                         if (stcb->asoc.delayed_ack) {
10419                                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10420                                     stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10421                                 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10422                                     stcb->sctp_ep, stcb, NULL);
10423                         } else {
10424                                 stcb->asoc.send_sack = 1;
10425                         }
10426                         return;
10427                 }
10428                 a_chk->copy_by_ref = 0;
10429                 a_chk->rec.chunk_id.id = type;
10430                 a_chk->rec.chunk_id.can_take_data = 1;
10431         }
10432         /* Clear our pkt counts */
10433         asoc->data_pkts_seen = 0;
10434
10435         a_chk->asoc = asoc;
10436         a_chk->snd_count = 0;
10437         a_chk->send_size = 0;   /* fill in later */
10438         a_chk->sent = SCTP_DATAGRAM_UNSENT;
10439         a_chk->whoTo = NULL;
10440
10441         if ((asoc->numduptsns) ||
10442             (!(asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE))) {
10443                 /*-
10444                  * Ok, we have some duplicates or the destination for the
10445                  * sack is unreachable, lets see if we can select an
10446                  * alternate than asoc->last_data_chunk_from
10447                  */
10448                 if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) &&
10449                     (asoc->used_alt_onsack > asoc->numnets)) {
10450                         /* We used an alt last time, don't this time */
10451                         a_chk->whoTo = NULL;
10452                 } else {
10453                         asoc->used_alt_onsack++;
10454                         a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10455                 }
10456                 if (a_chk->whoTo == NULL) {
10457                         /* Nope, no alternate */
10458                         a_chk->whoTo = asoc->last_data_chunk_from;
10459                         asoc->used_alt_onsack = 0;
10460                 }
10461         } else {
10462                 /*
10463                  * No duplicates so we use the last place we received data
10464                  * from.
10465                  */
10466                 asoc->used_alt_onsack = 0;
10467                 a_chk->whoTo = asoc->last_data_chunk_from;
10468         }
10469         if (a_chk->whoTo) {
10470                 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10471         }
10472         if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10473                 highest_tsn = asoc->highest_tsn_inside_map;
10474         } else {
10475                 highest_tsn = asoc->highest_tsn_inside_nr_map;
10476         }
10477         if (highest_tsn == asoc->cumulative_tsn) {
10478                 /* no gaps */
10479                 if (type == SCTP_SELECTIVE_ACK) {
10480                         space_req = sizeof(struct sctp_sack_chunk);
10481                 } else {
10482                         space_req = sizeof(struct sctp_nr_sack_chunk);
10483                 }
10484         } else {
10485                 /* gaps get a cluster */
10486                 space_req = MCLBYTES;
10487         }
10488         /* Ok now lets formulate a MBUF with our sack */
10489         a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10490         if ((a_chk->data == NULL) ||
10491             (a_chk->whoTo == NULL)) {
10492                 /* rats, no mbuf memory */
10493                 if (a_chk->data) {
10494                         /* was a problem with the destination */
10495                         sctp_m_freem(a_chk->data);
10496                         a_chk->data = NULL;
10497                 }
10498                 sctp_free_a_chunk(stcb, a_chk, so_locked);
10499                 /* sa_ignore NO_NULL_CHK */
10500                 if (stcb->asoc.delayed_ack) {
10501                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10502                             stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10503                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10504                             stcb->sctp_ep, stcb, NULL);
10505                 } else {
10506                         stcb->asoc.send_sack = 1;
10507                 }
10508                 return;
10509         }
10510         /* ok, lets go through and fill it in */
10511         SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10512         space = M_TRAILINGSPACE(a_chk->data);
10513         if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10514                 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10515         }
10516         limit = mtod(a_chk->data, caddr_t);
10517         limit += space;
10518
10519         flags = 0;
10520
10521         if ((asoc->sctp_cmt_on_off > 0) &&
10522             SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10523                 /*-
10524                  * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10525                  * received, then set high bit to 1, else 0. Reset
10526                  * pkts_rcvd.
10527                  */
10528                 flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10529                 asoc->cmt_dac_pkts_rcvd = 0;
10530         }
10531 #ifdef SCTP_ASOCLOG_OF_TSNS
10532         stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10533         stcb->asoc.cumack_log_atsnt++;
10534         if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10535                 stcb->asoc.cumack_log_atsnt = 0;
10536         }
10537 #endif
10538         /* reset the readers interpretation */
10539         stcb->freed_by_sorcv_sincelast = 0;
10540
10541         if (type == SCTP_SELECTIVE_ACK) {
10542                 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10543                 nr_sack = NULL;
10544                 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10545                 if (highest_tsn > asoc->mapping_array_base_tsn) {
10546                         siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10547                 } else {
10548                         siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10549                 }
10550         } else {
10551                 sack = NULL;
10552                 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10553                 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10554                 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10555                         siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10556                 } else {
10557                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10558                 }
10559         }
10560
10561         if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10562                 offset = 1;
10563         } else {
10564                 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10565         }
10566         if (((type == SCTP_SELECTIVE_ACK) &&
10567             SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10568             ((type == SCTP_NR_SELECTIVE_ACK) &&
10569             SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10570                 /* we have a gap .. maybe */
10571                 for (i = 0; i < siz; i++) {
10572                         tsn_map = asoc->mapping_array[i];
10573                         if (type == SCTP_SELECTIVE_ACK) {
10574                                 tsn_map |= asoc->nr_mapping_array[i];
10575                         }
10576                         if (i == 0) {
10577                                 /*
10578                                  * Clear all bits corresponding to TSNs
10579                                  * smaller or equal to the cumulative TSN.
10580                                  */
10581                                 tsn_map &= (~0 << (1 - offset));
10582                         }
10583                         selector = &sack_array[tsn_map];
10584                         if (mergeable && selector->right_edge) {
10585                                 /*
10586                                  * Backup, left and right edges were ok to
10587                                  * merge.
10588                                  */
10589                                 num_gap_blocks--;
10590                                 gap_descriptor--;
10591                         }
10592                         if (selector->num_entries == 0)
10593                                 mergeable = 0;
10594                         else {
10595                                 for (j = 0; j < selector->num_entries; j++) {
10596                                         if (mergeable && selector->right_edge) {
10597                                                 /*
10598                                                  * do a merge by NOT setting
10599                                                  * the left side
10600                                                  */
10601                                                 mergeable = 0;
10602                                         } else {
10603                                                 /*
10604                                                  * no merge, set the left
10605                                                  * side
10606                                                  */
10607                                                 mergeable = 0;
10608                                                 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10609                                         }
10610                                         gap_descriptor->end = htons((selector->gaps[j].end + offset));
10611                                         num_gap_blocks++;
10612                                         gap_descriptor++;
10613                                         if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10614                                                 /* no more room */
10615                                                 limit_reached = 1;
10616                                                 break;
10617                                         }
10618                                 }
10619                                 if (selector->left_edge) {
10620                                         mergeable = 1;
10621                                 }
10622                         }
10623                         if (limit_reached) {
10624                                 /* Reached the limit stop */
10625                                 break;
10626                         }
10627                         offset += 8;
10628                 }
10629         }
10630         if ((type == SCTP_NR_SELECTIVE_ACK) &&
10631             (limit_reached == 0)) {
10632
10633                 mergeable = 0;
10634
10635                 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10636                         siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10637                 } else {
10638                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10639                 }
10640
10641                 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10642                         offset = 1;
10643                 } else {
10644                         offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10645                 }
10646                 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10647                         /* we have a gap .. maybe */
10648                         for (i = 0; i < siz; i++) {
10649                                 tsn_map = asoc->nr_mapping_array[i];
10650                                 if (i == 0) {
10651                                         /*
10652                                          * Clear all bits corresponding to
10653                                          * TSNs smaller or equal to the
10654                                          * cumulative TSN.
10655                                          */
10656                                         tsn_map &= (~0 << (1 - offset));
10657                                 }
10658                                 selector = &sack_array[tsn_map];
10659                                 if (mergeable && selector->right_edge) {
10660                                         /*
10661                                          * Backup, left and right edges were
10662                                          * ok to merge.
10663                                          */
10664                                         num_nr_gap_blocks--;
10665                                         gap_descriptor--;
10666                                 }
10667                                 if (selector->num_entries == 0)
10668                                         mergeable = 0;
10669                                 else {
10670                                         for (j = 0; j < selector->num_entries; j++) {
10671                                                 if (mergeable && selector->right_edge) {
10672                                                         /*
10673                                                          * do a merge by NOT
10674                                                          * setting the left
10675                                                          * side
10676                                                          */
10677                                                         mergeable = 0;
10678                                                 } else {
10679                                                         /*
10680                                                          * no merge, set the
10681                                                          * left side
10682                                                          */
10683                                                         mergeable = 0;
10684                                                         gap_descriptor->start = htons((selector->gaps[j].start + offset));
10685                                                 }
10686                                                 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10687                                                 num_nr_gap_blocks++;
10688                                                 gap_descriptor++;
10689                                                 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10690                                                         /* no more room */
10691                                                         limit_reached = 1;
10692                                                         break;
10693                                                 }
10694                                         }
10695                                         if (selector->left_edge) {
10696                                                 mergeable = 1;
10697                                         }
10698                                 }
10699                                 if (limit_reached) {
10700                                         /* Reached the limit stop */
10701                                         break;
10702                                 }
10703                                 offset += 8;
10704                         }
10705                 }
10706         }
10707         /* now we must add any dups we are going to report. */
10708         if ((limit_reached == 0) && (asoc->numduptsns)) {
10709                 dup = (uint32_t *) gap_descriptor;
10710                 for (i = 0; i < asoc->numduptsns; i++) {
10711                         *dup = htonl(asoc->dup_tsns[i]);
10712                         dup++;
10713                         num_dups++;
10714                         if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10715                                 /* no more room */
10716                                 break;
10717                         }
10718                 }
10719                 asoc->numduptsns = 0;
10720         }
10721         /*
10722          * now that the chunk is prepared queue it to the control chunk
10723          * queue.
10724          */
10725         if (type == SCTP_SELECTIVE_ACK) {
10726                 a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10727                     (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10728                     num_dups * sizeof(int32_t);
10729                 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10730                 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10731                 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10732                 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10733                 sack->sack.num_dup_tsns = htons(num_dups);
10734                 sack->ch.chunk_type = type;
10735                 sack->ch.chunk_flags = flags;
10736                 sack->ch.chunk_length = htons(a_chk->send_size);
10737         } else {
10738                 a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10739                     (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10740                     num_dups * sizeof(int32_t);
10741                 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10742                 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10743                 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10744                 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10745                 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10746                 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10747                 nr_sack->nr_sack.reserved = 0;
10748                 nr_sack->ch.chunk_type = type;
10749                 nr_sack->ch.chunk_flags = flags;
10750                 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10751         }
10752         TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10753         asoc->my_last_reported_rwnd = asoc->my_rwnd;
10754         asoc->ctrl_queue_cnt++;
10755         asoc->send_sack = 0;
10756         SCTP_STAT_INCR(sctps_sendsacks);
10757         return;
10758 }
10759
10760 void
10761 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10762 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10763     SCTP_UNUSED
10764 #endif
10765 )
10766 {
10767         struct mbuf *m_abort;
10768         struct mbuf *m_out = NULL, *m_end = NULL;
10769         struct sctp_abort_chunk *abort = NULL;
10770         int sz;
10771         uint32_t auth_offset = 0;
10772         struct sctp_auth_chunk *auth = NULL;
10773         struct sctp_nets *net;
10774
10775         /*-
10776          * Add an AUTH chunk, if chunk requires it and save the offset into
10777          * the chain for AUTH
10778          */
10779         if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10780             stcb->asoc.peer_auth_chunks)) {
10781                 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10782                     stcb, SCTP_ABORT_ASSOCIATION);
10783                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10784         }
10785         SCTP_TCB_LOCK_ASSERT(stcb);
10786         m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10787         if (m_abort == NULL) {
10788                 /* no mbuf's */
10789                 if (m_out)
10790                         sctp_m_freem(m_out);
10791                 return;
10792         }
10793         /* link in any error */
10794         SCTP_BUF_NEXT(m_abort) = operr;
10795         sz = 0;
10796         if (operr) {
10797                 struct mbuf *n;
10798
10799                 n = operr;
10800                 while (n) {
10801                         sz += SCTP_BUF_LEN(n);
10802                         n = SCTP_BUF_NEXT(n);
10803                 }
10804         }
10805         SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10806         if (m_out == NULL) {
10807                 /* NO Auth chunk prepended, so reserve space in front */
10808                 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10809                 m_out = m_abort;
10810         } else {
10811                 /* Put AUTH chunk at the front of the chain */
10812                 SCTP_BUF_NEXT(m_end) = m_abort;
10813         }
10814         if (stcb->asoc.alternate) {
10815                 net = stcb->asoc.alternate;
10816         } else {
10817                 net = stcb->asoc.primary_destination;
10818         }
10819         /* fill in the ABORT chunk */
10820         abort = mtod(m_abort, struct sctp_abort_chunk *);
10821         abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10822         abort->ch.chunk_flags = 0;
10823         abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10824
10825         (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10826             (struct sockaddr *)&net->ro._l_addr,
10827             m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
10828             stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10829             stcb->asoc.primary_destination->port, so_locked, NULL, NULL);
10830         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10831 }
10832
10833 void
10834 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10835     struct sctp_nets *net,
10836     int reflect_vtag)
10837 {
10838         /* formulate and SEND a SHUTDOWN-COMPLETE */
10839         struct mbuf *m_shutdown_comp;
10840         struct sctp_shutdown_complete_chunk *shutdown_complete;
10841         uint32_t vtag;
10842         uint8_t flags;
10843
10844         m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10845         if (m_shutdown_comp == NULL) {
10846                 /* no mbuf's */
10847                 return;
10848         }
10849         if (reflect_vtag) {
10850                 flags = SCTP_HAD_NO_TCB;
10851                 vtag = stcb->asoc.my_vtag;
10852         } else {
10853                 flags = 0;
10854                 vtag = stcb->asoc.peer_vtag;
10855         }
10856         shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10857         shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10858         shutdown_complete->ch.chunk_flags = flags;
10859         shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10860         SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10861         (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10862             (struct sockaddr *)&net->ro._l_addr,
10863             m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
10864             stcb->sctp_ep->sctp_lport, stcb->rport,
10865             htonl(vtag),
10866             net->port, SCTP_SO_NOT_LOCKED, NULL, NULL);
10867         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10868         return;
10869 }
10870
10871 void
10872 sctp_send_shutdown_complete2(struct mbuf *m, struct sctphdr *sh,
10873     uint32_t vrf_id, uint16_t port)
10874 {
10875         /* formulate and SEND a SHUTDOWN-COMPLETE */
10876         struct mbuf *o_pak;
10877         struct mbuf *mout;
10878         struct ip *iph;
10879         struct udphdr *udp = NULL;
10880         int offset_out, len, mlen;
10881         struct sctp_shutdown_complete_msg *comp_cp;
10882
10883 #ifdef INET
10884         struct ip *iph_out;
10885
10886 #endif
10887 #ifdef INET6
10888         struct ip6_hdr *ip6, *ip6_out;
10889
10890 #endif
10891
10892         iph = mtod(m, struct ip *);
10893         switch (iph->ip_v) {
10894 #ifdef INET
10895         case IPVERSION:
10896                 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10897                 break;
10898 #endif
10899 #ifdef INET6
10900         case IPV6_VERSION >> 4:
10901                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10902                 break;
10903 #endif
10904         default:
10905                 return;
10906         }
10907         if (port) {
10908                 len += sizeof(struct udphdr);
10909         }
10910         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10911         if (mout == NULL) {
10912                 return;
10913         }
10914         SCTP_BUF_RESV_UF(mout, max_linkhdr);
10915         SCTP_BUF_LEN(mout) = len;
10916         SCTP_BUF_NEXT(mout) = NULL;
10917         if (m->m_flags & M_FLOWID) {
10918                 mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
10919                 mout->m_flags |= M_FLOWID;
10920         }
10921 #ifdef INET
10922         iph_out = NULL;
10923 #endif
10924 #ifdef INET6
10925         ip6_out = NULL;
10926 #endif
10927         offset_out = 0;
10928
10929         switch (iph->ip_v) {
10930 #ifdef INET
10931         case IPVERSION:
10932                 iph_out = mtod(mout, struct ip *);
10933
10934                 /* Fill in the IP header for the ABORT */
10935                 iph_out->ip_v = IPVERSION;
10936                 iph_out->ip_hl = (sizeof(struct ip) / 4);
10937                 iph_out->ip_tos = (u_char)0;
10938                 iph_out->ip_id = 0;
10939                 iph_out->ip_off = 0;
10940                 iph_out->ip_ttl = MAXTTL;
10941                 if (port) {
10942                         iph_out->ip_p = IPPROTO_UDP;
10943                 } else {
10944                         iph_out->ip_p = IPPROTO_SCTP;
10945                 }
10946                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10947                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10948
10949                 /* let IP layer calculate this */
10950                 iph_out->ip_sum = 0;
10951                 offset_out += sizeof(*iph_out);
10952                 comp_cp = (struct sctp_shutdown_complete_msg *)(
10953                     (caddr_t)iph_out + offset_out);
10954                 break;
10955 #endif
10956 #ifdef INET6
10957         case IPV6_VERSION >> 4:
10958                 ip6 = (struct ip6_hdr *)iph;
10959                 ip6_out = mtod(mout, struct ip6_hdr *);
10960
10961                 /* Fill in the IPv6 header for the ABORT */
10962                 ip6_out->ip6_flow = ip6->ip6_flow;
10963                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10964                 if (port) {
10965                         ip6_out->ip6_nxt = IPPROTO_UDP;
10966                 } else {
10967                         ip6_out->ip6_nxt = IPPROTO_SCTP;
10968                 }
10969                 ip6_out->ip6_src = ip6->ip6_dst;
10970                 ip6_out->ip6_dst = ip6->ip6_src;
10971                 /*
10972                  * ?? The old code had both the iph len + payload, I think
10973                  * this is wrong and would never have worked
10974                  */
10975                 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10976                 offset_out += sizeof(*ip6_out);
10977                 comp_cp = (struct sctp_shutdown_complete_msg *)(
10978                     (caddr_t)ip6_out + offset_out);
10979                 break;
10980 #endif                          /* INET6 */
10981         default:
10982                 /* Currently not supported. */
10983                 sctp_m_freem(mout);
10984                 return;
10985         }
10986         if (port) {
10987                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
10988                         sctp_m_freem(mout);
10989                         return;
10990                 }
10991                 udp = (struct udphdr *)comp_cp;
10992                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10993                 udp->uh_dport = port;
10994                 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10995 #ifdef INET
10996                 if (iph_out) {
10997                         if (V_udp_cksum) {
10998                                 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10999                         } else {
11000                                 udp->uh_sum = 0;
11001                         }
11002                 }
11003 #endif
11004                 offset_out += sizeof(struct udphdr);
11005                 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
11006         }
11007         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11008                 /* no mbuf's */
11009                 sctp_m_freem(mout);
11010                 return;
11011         }
11012         /* Now copy in and fill in the ABORT tags etc. */
11013         comp_cp->sh.src_port = sh->dest_port;
11014         comp_cp->sh.dest_port = sh->src_port;
11015         comp_cp->sh.checksum = 0;
11016         comp_cp->sh.v_tag = sh->v_tag;
11017         comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
11018         comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
11019         comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
11020
11021 #ifdef INET
11022         if (iph_out != NULL) {
11023                 sctp_route_t ro;
11024                 int ret;
11025
11026                 mlen = SCTP_BUF_LEN(mout);
11027                 bzero(&ro, sizeof ro);
11028                 /* set IPv4 length */
11029                 iph_out->ip_len = mlen;
11030 #ifdef  SCTP_PACKET_LOGGING
11031                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11032                         sctp_packet_log(mout, mlen);
11033 #endif
11034                 if (port) {
11035 #if defined(SCTP_WITH_NO_CSUM)
11036                         SCTP_STAT_INCR(sctps_sendnocrc);
11037 #else
11038                         comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
11039                         SCTP_STAT_INCR(sctps_sendswcrc);
11040 #endif
11041                         if (V_udp_cksum) {
11042                                 SCTP_ENABLE_UDP_CSUM(mout);
11043                         }
11044                 } else {
11045 #if defined(SCTP_WITH_NO_CSUM)
11046                         SCTP_STAT_INCR(sctps_sendnocrc);
11047 #else
11048                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
11049                         mout->m_pkthdr.csum_data = 0;
11050                         SCTP_STAT_INCR(sctps_sendhwcrc);
11051 #endif
11052                 }
11053                 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
11054                 /* out it goes */
11055                 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
11056
11057                 /* Free the route if we got one back */
11058                 if (ro.ro_rt)
11059                         RTFREE(ro.ro_rt);
11060         }
11061 #endif
11062 #ifdef INET6
11063         if (ip6_out != NULL) {
11064                 struct route_in6 ro;
11065                 int ret;
11066                 struct ifnet *ifp = NULL;
11067
11068                 bzero(&ro, sizeof(ro));
11069                 mlen = SCTP_BUF_LEN(mout);
11070 #ifdef  SCTP_PACKET_LOGGING
11071                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11072                         sctp_packet_log(mout, mlen);
11073 #endif
11074                 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
11075                 if (port) {
11076 #if defined(SCTP_WITH_NO_CSUM)
11077                         SCTP_STAT_INCR(sctps_sendnocrc);
11078 #else
11079                         comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11080                         SCTP_STAT_INCR(sctps_sendswcrc);
11081 #endif
11082                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
11083                                 udp->uh_sum = 0xffff;
11084                         }
11085                 } else {
11086 #if defined(SCTP_WITH_NO_CSUM)
11087                         SCTP_STAT_INCR(sctps_sendnocrc);
11088 #else
11089                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
11090                         mout->m_pkthdr.csum_data = 0;
11091                         SCTP_STAT_INCR(sctps_sendhwcrc);
11092 #endif
11093                 }
11094                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
11095
11096                 /* Free the route if we got one back */
11097                 if (ro.ro_rt)
11098                         RTFREE(ro.ro_rt);
11099         }
11100 #endif
11101         SCTP_STAT_INCR(sctps_sendpackets);
11102         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11103         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11104         return;
11105
11106 }
11107
11108 void
11109 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked
11110 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
11111     SCTP_UNUSED
11112 #endif
11113 )
11114 {
11115         struct sctp_tmit_chunk *chk;
11116         struct sctp_heartbeat_chunk *hb;
11117         struct timeval now;
11118
11119         SCTP_TCB_LOCK_ASSERT(stcb);
11120         if (net == NULL) {
11121                 return;
11122         }
11123         (void)SCTP_GETTIME_TIMEVAL(&now);
11124         switch (net->ro._l_addr.sa.sa_family) {
11125 #ifdef INET
11126         case AF_INET:
11127                 break;
11128 #endif
11129 #ifdef INET6
11130         case AF_INET6:
11131                 break;
11132 #endif
11133         default:
11134                 return;
11135         }
11136         sctp_alloc_a_chunk(stcb, chk);
11137         if (chk == NULL) {
11138                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11139                 return;
11140         }
11141         chk->copy_by_ref = 0;
11142         chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11143         chk->rec.chunk_id.can_take_data = 1;
11144         chk->asoc = &stcb->asoc;
11145         chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11146
11147         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11148         if (chk->data == NULL) {
11149                 sctp_free_a_chunk(stcb, chk, so_locked);
11150                 return;
11151         }
11152         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11153         SCTP_BUF_LEN(chk->data) = chk->send_size;
11154         chk->sent = SCTP_DATAGRAM_UNSENT;
11155         chk->snd_count = 0;
11156         chk->whoTo = net;
11157         atomic_add_int(&chk->whoTo->ref_count, 1);
11158         /* Now we have a mbuf that we can fill in with the details */
11159         hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11160         memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11161         /* fill out chunk header */
11162         hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11163         hb->ch.chunk_flags = 0;
11164         hb->ch.chunk_length = htons(chk->send_size);
11165         /* Fill out hb parameter */
11166         hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11167         hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11168         hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
11169         hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11170         /* Did our user request this one, put it in */
11171         hb->heartbeat.hb_info.addr_family = net->ro._l_addr.sa.sa_family;
11172         hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
11173         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11174                 /*
11175                  * we only take from the entropy pool if the address is not
11176                  * confirmed.
11177                  */
11178                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11179                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11180         } else {
11181                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11182                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11183         }
11184         switch (net->ro._l_addr.sa.sa_family) {
11185 #ifdef INET
11186         case AF_INET:
11187                 memcpy(hb->heartbeat.hb_info.address,
11188                     &net->ro._l_addr.sin.sin_addr,
11189                     sizeof(net->ro._l_addr.sin.sin_addr));
11190                 break;
11191 #endif
11192 #ifdef INET6
11193         case AF_INET6:
11194                 memcpy(hb->heartbeat.hb_info.address,
11195                     &net->ro._l_addr.sin6.sin6_addr,
11196                     sizeof(net->ro._l_addr.sin6.sin6_addr));
11197                 break;
11198 #endif
11199         default:
11200                 return;
11201                 break;
11202         }
11203         net->hb_responded = 0;
11204         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11205         stcb->asoc.ctrl_queue_cnt++;
11206         SCTP_STAT_INCR(sctps_sendheartbeat);
11207         return;
11208 }
11209
11210 void
11211 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11212     uint32_t high_tsn)
11213 {
11214         struct sctp_association *asoc;
11215         struct sctp_ecne_chunk *ecne;
11216         struct sctp_tmit_chunk *chk;
11217
11218         if (net == NULL) {
11219                 return;
11220         }
11221         asoc = &stcb->asoc;
11222         SCTP_TCB_LOCK_ASSERT(stcb);
11223         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11224                 if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
11225                         /* found a previous ECN_ECHO update it if needed */
11226                         uint32_t cnt, ctsn;
11227
11228                         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11229                         ctsn = ntohl(ecne->tsn);
11230                         if (SCTP_TSN_GT(high_tsn, ctsn)) {
11231                                 ecne->tsn = htonl(high_tsn);
11232                                 SCTP_STAT_INCR(sctps_queue_upd_ecne);
11233                         }
11234                         cnt = ntohl(ecne->num_pkts_since_cwr);
11235                         cnt++;
11236                         ecne->num_pkts_since_cwr = htonl(cnt);
11237                         return;
11238                 }
11239         }
11240         /* nope could not find one to update so we must build one */
11241         sctp_alloc_a_chunk(stcb, chk);
11242         if (chk == NULL) {
11243                 return;
11244         }
11245         chk->copy_by_ref = 0;
11246         SCTP_STAT_INCR(sctps_queue_upd_ecne);
11247         chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11248         chk->rec.chunk_id.can_take_data = 0;
11249         chk->asoc = &stcb->asoc;
11250         chk->send_size = sizeof(struct sctp_ecne_chunk);
11251         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11252         if (chk->data == NULL) {
11253                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11254                 return;
11255         }
11256         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11257         SCTP_BUF_LEN(chk->data) = chk->send_size;
11258         chk->sent = SCTP_DATAGRAM_UNSENT;
11259         chk->snd_count = 0;
11260         chk->whoTo = net;
11261         atomic_add_int(&chk->whoTo->ref_count, 1);
11262
11263         stcb->asoc.ecn_echo_cnt_onq++;
11264         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11265         ecne->ch.chunk_type = SCTP_ECN_ECHO;
11266         ecne->ch.chunk_flags = 0;
11267         ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11268         ecne->tsn = htonl(high_tsn);
11269         ecne->num_pkts_since_cwr = htonl(1);
11270         TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11271         asoc->ctrl_queue_cnt++;
11272 }
11273
11274 void
11275 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11276     struct mbuf *m, int iphlen, int bad_crc)
11277 {
11278         struct sctp_association *asoc;
11279         struct sctp_pktdrop_chunk *drp;
11280         struct sctp_tmit_chunk *chk;
11281         uint8_t *datap;
11282         int len;
11283         int was_trunc = 0;
11284         struct ip *iph;
11285
11286 #ifdef INET6
11287         struct ip6_hdr *ip6h;
11288
11289 #endif
11290         int fullsz = 0, extra = 0;
11291         long spc;
11292         int offset;
11293         struct sctp_chunkhdr *ch, chunk_buf;
11294         unsigned int chk_length;
11295
11296         if (!stcb) {
11297                 return;
11298         }
11299         asoc = &stcb->asoc;
11300         SCTP_TCB_LOCK_ASSERT(stcb);
11301         if (asoc->peer_supports_pktdrop == 0) {
11302                 /*-
11303                  * peer must declare support before I send one.
11304                  */
11305                 return;
11306         }
11307         if (stcb->sctp_socket == NULL) {
11308                 return;
11309         }
11310         sctp_alloc_a_chunk(stcb, chk);
11311         if (chk == NULL) {
11312                 return;
11313         }
11314         chk->copy_by_ref = 0;
11315         iph = mtod(m, struct ip *);
11316         if (iph == NULL) {
11317                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11318                 return;
11319         }
11320         switch (iph->ip_v) {
11321 #ifdef INET
11322         case IPVERSION:
11323                 /* IPv4 */
11324                 len = chk->send_size = iph->ip_len;
11325                 break;
11326 #endif
11327 #ifdef INET6
11328         case IPV6_VERSION >> 4:
11329                 /* IPv6 */
11330                 ip6h = mtod(m, struct ip6_hdr *);
11331                 len = chk->send_size = htons(ip6h->ip6_plen);
11332                 break;
11333 #endif
11334         default:
11335                 return;
11336         }
11337         /* Validate that we do not have an ABORT in here. */
11338         offset = iphlen + sizeof(struct sctphdr);
11339         ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11340             sizeof(*ch), (uint8_t *) & chunk_buf);
11341         while (ch != NULL) {
11342                 chk_length = ntohs(ch->chunk_length);
11343                 if (chk_length < sizeof(*ch)) {
11344                         /* break to abort land */
11345                         break;
11346                 }
11347                 switch (ch->chunk_type) {
11348                 case SCTP_PACKET_DROPPED:
11349                 case SCTP_ABORT_ASSOCIATION:
11350                 case SCTP_INITIATION_ACK:
11351                         /**
11352                          * We don't respond with an PKT-DROP to an ABORT
11353                          * or PKT-DROP. We also do not respond to an
11354                          * INIT-ACK, because we can't know if the initiation
11355                          * tag is correct or not.
11356                          */
11357                         sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11358                         return;
11359                 default:
11360                         break;
11361                 }
11362                 offset += SCTP_SIZE32(chk_length);
11363                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11364                     sizeof(*ch), (uint8_t *) & chunk_buf);
11365         }
11366
11367         if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11368             min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11369                 /*
11370                  * only send 1 mtu worth, trim off the excess on the end.
11371                  */
11372                 fullsz = len - extra;
11373                 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11374                 was_trunc = 1;
11375         }
11376         chk->asoc = &stcb->asoc;
11377         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11378         if (chk->data == NULL) {
11379 jump_out:
11380                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11381                 return;
11382         }
11383         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11384         drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11385         if (drp == NULL) {
11386                 sctp_m_freem(chk->data);
11387                 chk->data = NULL;
11388                 goto jump_out;
11389         }
11390         chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11391             sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11392         chk->book_size_scale = 0;
11393         if (was_trunc) {
11394                 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11395                 drp->trunc_len = htons(fullsz);
11396                 /*
11397                  * Len is already adjusted to size minus overhead above take
11398                  * out the pkt_drop chunk itself from it.
11399                  */
11400                 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11401                 len = chk->send_size;
11402         } else {
11403                 /* no truncation needed */
11404                 drp->ch.chunk_flags = 0;
11405                 drp->trunc_len = htons(0);
11406         }
11407         if (bad_crc) {
11408                 drp->ch.chunk_flags |= SCTP_BADCRC;
11409         }
11410         chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11411         SCTP_BUF_LEN(chk->data) = chk->send_size;
11412         chk->sent = SCTP_DATAGRAM_UNSENT;
11413         chk->snd_count = 0;
11414         if (net) {
11415                 /* we should hit here */
11416                 chk->whoTo = net;
11417                 atomic_add_int(&chk->whoTo->ref_count, 1);
11418         } else {
11419                 chk->whoTo = NULL;
11420         }
11421         chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11422         chk->rec.chunk_id.can_take_data = 1;
11423         drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11424         drp->ch.chunk_length = htons(chk->send_size);
11425         spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11426         if (spc < 0) {
11427                 spc = 0;
11428         }
11429         drp->bottle_bw = htonl(spc);
11430         if (asoc->my_rwnd) {
11431                 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11432                     asoc->size_on_all_streams +
11433                     asoc->my_rwnd_control_len +
11434                     stcb->sctp_socket->so_rcv.sb_cc);
11435         } else {
11436                 /*-
11437                  * If my rwnd is 0, possibly from mbuf depletion as well as
11438                  * space used, tell the peer there is NO space aka onq == bw
11439                  */
11440                 drp->current_onq = htonl(spc);
11441         }
11442         drp->reserved = 0;
11443         datap = drp->data;
11444         m_copydata(m, iphlen, len, (caddr_t)datap);
11445         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11446         asoc->ctrl_queue_cnt++;
11447 }
11448
11449 void
11450 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11451 {
11452         struct sctp_association *asoc;
11453         struct sctp_cwr_chunk *cwr;
11454         struct sctp_tmit_chunk *chk;
11455
11456         asoc = &stcb->asoc;
11457         SCTP_TCB_LOCK_ASSERT(stcb);
11458         if (net == NULL) {
11459                 return;
11460         }
11461         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11462                 if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11463                         /*
11464                          * found a previous CWR queued to same destination
11465                          * update it if needed
11466                          */
11467                         uint32_t ctsn;
11468
11469                         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11470                         ctsn = ntohl(cwr->tsn);
11471                         if (SCTP_TSN_GT(high_tsn, ctsn)) {
11472                                 cwr->tsn = htonl(high_tsn);
11473                         }
11474                         if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11475                                 /* Make sure override is carried */
11476                                 cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11477                         }
11478                         return;
11479                 }
11480         }
11481         sctp_alloc_a_chunk(stcb, chk);
11482         if (chk == NULL) {
11483                 return;
11484         }
11485         chk->copy_by_ref = 0;
11486         chk->rec.chunk_id.id = SCTP_ECN_CWR;
11487         chk->rec.chunk_id.can_take_data = 1;
11488         chk->asoc = &stcb->asoc;
11489         chk->send_size = sizeof(struct sctp_cwr_chunk);
11490         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11491         if (chk->data == NULL) {
11492                 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11493                 return;
11494         }
11495         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11496         SCTP_BUF_LEN(chk->data) = chk->send_size;
11497         chk->sent = SCTP_DATAGRAM_UNSENT;
11498         chk->snd_count = 0;
11499         chk->whoTo = net;
11500         atomic_add_int(&chk->whoTo->ref_count, 1);
11501         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11502         cwr->ch.chunk_type = SCTP_ECN_CWR;
11503         cwr->ch.chunk_flags = override;
11504         cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11505         cwr->tsn = htonl(high_tsn);
11506         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11507         asoc->ctrl_queue_cnt++;
11508 }
11509
11510 void
11511 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11512     int number_entries, uint16_t * list,
11513     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11514 {
11515         int len, old_len, i;
11516         struct sctp_stream_reset_out_request *req_out;
11517         struct sctp_chunkhdr *ch;
11518
11519         ch = mtod(chk->data, struct sctp_chunkhdr *);
11520
11521
11522         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11523
11524         /* get to new offset for the param. */
11525         req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11526         /* now how long will this param be? */
11527         len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11528         req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11529         req_out->ph.param_length = htons(len);
11530         req_out->request_seq = htonl(seq);
11531         req_out->response_seq = htonl(resp_seq);
11532         req_out->send_reset_at_tsn = htonl(last_sent);
11533         if (number_entries) {
11534                 for (i = 0; i < number_entries; i++) {
11535                         req_out->list_of_streams[i] = htons(list[i]);
11536                 }
11537         }
11538         if (SCTP_SIZE32(len) > len) {
11539                 /*-
11540                  * Need to worry about the pad we may end up adding to the
11541                  * end. This is easy since the struct is either aligned to 4
11542                  * bytes or 2 bytes off.
11543                  */
11544                 req_out->list_of_streams[number_entries] = 0;
11545         }
11546         /* now fix the chunk length */
11547         ch->chunk_length = htons(len + old_len);
11548         chk->book_size = len + old_len;
11549         chk->book_size_scale = 0;
11550         chk->send_size = SCTP_SIZE32(chk->book_size);
11551         SCTP_BUF_LEN(chk->data) = chk->send_size;
11552         return;
11553 }
11554
11555
11556 void
11557 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11558     int number_entries, uint16_t * list,
11559     uint32_t seq)
11560 {
11561         int len, old_len, i;
11562         struct sctp_stream_reset_in_request *req_in;
11563         struct sctp_chunkhdr *ch;
11564
11565         ch = mtod(chk->data, struct sctp_chunkhdr *);
11566
11567
11568         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11569
11570         /* get to new offset for the param. */
11571         req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11572         /* now how long will this param be? */
11573         len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11574         req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11575         req_in->ph.param_length = htons(len);
11576         req_in->request_seq = htonl(seq);
11577         if (number_entries) {
11578                 for (i = 0; i < number_entries; i++) {
11579                         req_in->list_of_streams[i] = htons(list[i]);
11580                 }
11581         }
11582         if (SCTP_SIZE32(len) > len) {
11583                 /*-
11584                  * Need to worry about the pad we may end up adding to the
11585                  * end. This is easy since the struct is either aligned to 4
11586                  * bytes or 2 bytes off.
11587                  */
11588                 req_in->list_of_streams[number_entries] = 0;
11589         }
11590         /* now fix the chunk length */
11591         ch->chunk_length = htons(len + old_len);
11592         chk->book_size = len + old_len;
11593         chk->book_size_scale = 0;
11594         chk->send_size = SCTP_SIZE32(chk->book_size);
11595         SCTP_BUF_LEN(chk->data) = chk->send_size;
11596         return;
11597 }
11598
11599
11600 void
11601 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11602     uint32_t seq)
11603 {
11604         int len, old_len;
11605         struct sctp_stream_reset_tsn_request *req_tsn;
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         req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11615         /* now how long will this param be? */
11616         len = sizeof(struct sctp_stream_reset_tsn_request);
11617         req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11618         req_tsn->ph.param_length = htons(len);
11619         req_tsn->request_seq = htonl(seq);
11620
11621         /* now fix the chunk length */
11622         ch->chunk_length = htons(len + old_len);
11623         chk->send_size = len + old_len;
11624         chk->book_size = SCTP_SIZE32(chk->send_size);
11625         chk->book_size_scale = 0;
11626         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11627         return;
11628 }
11629
11630 void
11631 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11632     uint32_t resp_seq, uint32_t result)
11633 {
11634         int len, old_len;
11635         struct sctp_stream_reset_response *resp;
11636         struct sctp_chunkhdr *ch;
11637
11638         ch = mtod(chk->data, struct sctp_chunkhdr *);
11639
11640
11641         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11642
11643         /* get to new offset for the param. */
11644         resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11645         /* now how long will this param be? */
11646         len = sizeof(struct sctp_stream_reset_response);
11647         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11648         resp->ph.param_length = htons(len);
11649         resp->response_seq = htonl(resp_seq);
11650         resp->result = ntohl(result);
11651
11652         /* now fix the chunk length */
11653         ch->chunk_length = htons(len + old_len);
11654         chk->book_size = len + old_len;
11655         chk->book_size_scale = 0;
11656         chk->send_size = SCTP_SIZE32(chk->book_size);
11657         SCTP_BUF_LEN(chk->data) = chk->send_size;
11658         return;
11659
11660 }
11661
11662
11663 void
11664 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11665     uint32_t resp_seq, uint32_t result,
11666     uint32_t send_una, uint32_t recv_next)
11667 {
11668         int len, old_len;
11669         struct sctp_stream_reset_response_tsn *resp;
11670         struct sctp_chunkhdr *ch;
11671
11672         ch = mtod(chk->data, struct sctp_chunkhdr *);
11673
11674
11675         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11676
11677         /* get to new offset for the param. */
11678         resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11679         /* now how long will this param be? */
11680         len = sizeof(struct sctp_stream_reset_response_tsn);
11681         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11682         resp->ph.param_length = htons(len);
11683         resp->response_seq = htonl(resp_seq);
11684         resp->result = htonl(result);
11685         resp->senders_next_tsn = htonl(send_una);
11686         resp->receivers_next_tsn = htonl(recv_next);
11687
11688         /* now fix the chunk length */
11689         ch->chunk_length = htons(len + old_len);
11690         chk->book_size = len + old_len;
11691         chk->send_size = SCTP_SIZE32(chk->book_size);
11692         chk->book_size_scale = 0;
11693         SCTP_BUF_LEN(chk->data) = chk->send_size;
11694         return;
11695 }
11696
11697 static void
11698 sctp_add_an_out_stream(struct sctp_tmit_chunk *chk,
11699     uint32_t seq,
11700     uint16_t adding)
11701 {
11702         int len, old_len;
11703         struct sctp_chunkhdr *ch;
11704         struct sctp_stream_reset_add_strm *addstr;
11705
11706         ch = mtod(chk->data, struct sctp_chunkhdr *);
11707         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11708
11709         /* get to new offset for the param. */
11710         addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11711         /* now how long will this param be? */
11712         len = sizeof(struct sctp_stream_reset_add_strm);
11713
11714         /* Fill it out. */
11715         addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS);
11716         addstr->ph.param_length = htons(len);
11717         addstr->request_seq = htonl(seq);
11718         addstr->number_of_streams = htons(adding);
11719         addstr->reserved = 0;
11720
11721         /* now fix the chunk length */
11722         ch->chunk_length = htons(len + old_len);
11723         chk->send_size = len + old_len;
11724         chk->book_size = SCTP_SIZE32(chk->send_size);
11725         chk->book_size_scale = 0;
11726         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11727         return;
11728 }
11729
11730 static void
11731 sctp_add_an_in_stream(struct sctp_tmit_chunk *chk,
11732     uint32_t seq,
11733     uint16_t adding)
11734 {
11735         int len, old_len;
11736         struct sctp_chunkhdr *ch;
11737         struct sctp_stream_reset_add_strm *addstr;
11738
11739         ch = mtod(chk->data, struct sctp_chunkhdr *);
11740         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11741
11742         /* get to new offset for the param. */
11743         addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11744         /* now how long will this param be? */
11745         len = sizeof(struct sctp_stream_reset_add_strm);
11746         /* Fill it out. */
11747         addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS);
11748         addstr->ph.param_length = htons(len);
11749         addstr->request_seq = htonl(seq);
11750         addstr->number_of_streams = htons(adding);
11751         addstr->reserved = 0;
11752
11753         /* now fix the chunk length */
11754         ch->chunk_length = htons(len + old_len);
11755         chk->send_size = len + old_len;
11756         chk->book_size = SCTP_SIZE32(chk->send_size);
11757         chk->book_size_scale = 0;
11758         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11759         return;
11760 }
11761
11762
11763
11764 int
11765 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11766     int number_entries, uint16_t * list,
11767     uint8_t send_out_req,
11768     uint8_t send_in_req,
11769     uint8_t send_tsn_req,
11770     uint8_t add_stream,
11771     uint16_t adding_o,
11772     uint16_t adding_i, uint8_t peer_asked
11773 )
11774 {
11775
11776         struct sctp_association *asoc;
11777         struct sctp_tmit_chunk *chk;
11778         struct sctp_chunkhdr *ch;
11779         uint32_t seq;
11780
11781         asoc = &stcb->asoc;
11782         if (asoc->stream_reset_outstanding) {
11783                 /*-
11784                  * Already one pending, must get ACK back to clear the flag.
11785                  */
11786                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11787                 return (EBUSY);
11788         }
11789         if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11790             (add_stream == 0)) {
11791                 /* nothing to do */
11792                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11793                 return (EINVAL);
11794         }
11795         if (send_tsn_req && (send_out_req || send_in_req)) {
11796                 /* error, can't do that */
11797                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11798                 return (EINVAL);
11799         }
11800         sctp_alloc_a_chunk(stcb, chk);
11801         if (chk == NULL) {
11802                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11803                 return (ENOMEM);
11804         }
11805         chk->copy_by_ref = 0;
11806         chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11807         chk->rec.chunk_id.can_take_data = 0;
11808         chk->asoc = &stcb->asoc;
11809         chk->book_size = sizeof(struct sctp_chunkhdr);
11810         chk->send_size = SCTP_SIZE32(chk->book_size);
11811         chk->book_size_scale = 0;
11812
11813         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11814         if (chk->data == NULL) {
11815                 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11816                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11817                 return (ENOMEM);
11818         }
11819         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11820
11821         /* setup chunk parameters */
11822         chk->sent = SCTP_DATAGRAM_UNSENT;
11823         chk->snd_count = 0;
11824         if (stcb->asoc.alternate) {
11825                 chk->whoTo = stcb->asoc.alternate;
11826         } else {
11827                 chk->whoTo = stcb->asoc.primary_destination;
11828         }
11829         atomic_add_int(&chk->whoTo->ref_count, 1);
11830         ch = mtod(chk->data, struct sctp_chunkhdr *);
11831         ch->chunk_type = SCTP_STREAM_RESET;
11832         ch->chunk_flags = 0;
11833         ch->chunk_length = htons(chk->book_size);
11834         SCTP_BUF_LEN(chk->data) = chk->send_size;
11835
11836         seq = stcb->asoc.str_reset_seq_out;
11837         if (send_out_req) {
11838                 sctp_add_stream_reset_out(chk, number_entries, list,
11839                     seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1));
11840                 asoc->stream_reset_out_is_outstanding = 1;
11841                 seq++;
11842                 asoc->stream_reset_outstanding++;
11843         }
11844         if ((add_stream & 1) &&
11845             ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) {
11846                 /* Need to allocate more */
11847                 struct sctp_stream_out *oldstream;
11848                 struct sctp_stream_queue_pending *sp, *nsp;
11849                 int i;
11850
11851                 oldstream = stcb->asoc.strmout;
11852                 /* get some more */
11853                 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
11854                     ((stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out)),
11855                     SCTP_M_STRMO);
11856                 if (stcb->asoc.strmout == NULL) {
11857                         uint8_t x;
11858
11859                         stcb->asoc.strmout = oldstream;
11860                         /* Turn off the bit */
11861                         x = add_stream & 0xfe;
11862                         add_stream = x;
11863                         goto skip_stuff;
11864                 }
11865                 /*
11866                  * Ok now we proceed with copying the old out stuff and
11867                  * initializing the new stuff.
11868                  */
11869                 SCTP_TCB_SEND_LOCK(stcb);
11870                 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, 0, 1);
11871                 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11872                         TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
11873                         stcb->asoc.strmout[i].next_sequence_sent = oldstream[i].next_sequence_sent;
11874                         stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
11875                         stcb->asoc.strmout[i].stream_no = i;
11876                         stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], &oldstream[i]);
11877                         /* now anything on those queues? */
11878                         TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
11879                                 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
11880                                 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
11881                         }
11882                         /* Now move assoc pointers too */
11883                         if (stcb->asoc.last_out_stream == &oldstream[i]) {
11884                                 stcb->asoc.last_out_stream = &stcb->asoc.strmout[i];
11885                         }
11886                         if (stcb->asoc.locked_on_sending == &oldstream[i]) {
11887                                 stcb->asoc.locked_on_sending = &stcb->asoc.strmout[i];
11888                         }
11889                 }
11890                 /* now the new streams */
11891                 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc, 1);
11892                 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) {
11893                         stcb->asoc.strmout[i].next_sequence_sent = 0x0;
11894                         TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
11895                         stcb->asoc.strmout[i].stream_no = i;
11896                         stcb->asoc.strmout[i].last_msg_incomplete = 0;
11897                         stcb->asoc.ss_functions.sctp_ss_init_stream(&stcb->asoc.strmout[i], NULL);
11898                 }
11899                 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o;
11900                 SCTP_FREE(oldstream, SCTP_M_STRMO);
11901                 SCTP_TCB_SEND_UNLOCK(stcb);
11902         }
11903 skip_stuff:
11904         if ((add_stream & 1) && (adding_o > 0)) {
11905                 asoc->strm_pending_add_size = adding_o;
11906                 asoc->peer_req_out = peer_asked;
11907                 sctp_add_an_out_stream(chk, seq, adding_o);
11908                 seq++;
11909                 asoc->stream_reset_outstanding++;
11910         }
11911         if ((add_stream & 2) && (adding_i > 0)) {
11912                 sctp_add_an_in_stream(chk, seq, adding_i);
11913                 seq++;
11914                 asoc->stream_reset_outstanding++;
11915         }
11916         if (send_in_req) {
11917                 sctp_add_stream_reset_in(chk, number_entries, list, seq);
11918                 seq++;
11919                 asoc->stream_reset_outstanding++;
11920         }
11921         if (send_tsn_req) {
11922                 sctp_add_stream_reset_tsn(chk, seq);
11923                 asoc->stream_reset_outstanding++;
11924         }
11925         asoc->str_reset = chk;
11926         /* insert the chunk for sending */
11927         TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11928             chk,
11929             sctp_next);
11930         asoc->ctrl_queue_cnt++;
11931         sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11932         return (0);
11933 }
11934
11935 void
11936 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11937     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11938 {
11939         /*-
11940          * Formulate the abort message, and send it back down.
11941          */
11942         struct mbuf *o_pak;
11943         struct mbuf *mout;
11944         struct sctp_abort_msg *abm;
11945         struct ip *iph;
11946         struct udphdr *udp;
11947         int iphlen_out, len;
11948
11949 #ifdef INET
11950         struct ip *iph_out;
11951
11952 #endif
11953 #ifdef INET6
11954         struct ip6_hdr *ip6, *ip6_out;
11955
11956 #endif
11957
11958         /* don't respond to ABORT with ABORT */
11959         if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11960                 if (err_cause)
11961                         sctp_m_freem(err_cause);
11962                 return;
11963         }
11964         iph = mtod(m, struct ip *);
11965         switch (iph->ip_v) {
11966 #ifdef INET
11967         case IPVERSION:
11968                 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11969                 break;
11970 #endif
11971 #ifdef INET6
11972         case IPV6_VERSION >> 4:
11973                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11974                 break;
11975 #endif
11976         default:
11977                 if (err_cause) {
11978                         sctp_m_freem(err_cause);
11979                 }
11980                 return;
11981         }
11982         if (port) {
11983                 len += sizeof(struct udphdr);
11984         }
11985         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11986         if (mout == NULL) {
11987                 if (err_cause) {
11988                         sctp_m_freem(err_cause);
11989                 }
11990                 return;
11991         }
11992         SCTP_BUF_RESV_UF(mout, max_linkhdr);
11993         SCTP_BUF_LEN(mout) = len;
11994         SCTP_BUF_NEXT(mout) = err_cause;
11995         if (m->m_flags & M_FLOWID) {
11996                 mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
11997                 mout->m_flags |= M_FLOWID;
11998         }
11999 #ifdef INET
12000         iph_out = NULL;
12001 #endif
12002 #ifdef INET6
12003         ip6_out = NULL;
12004 #endif
12005         switch (iph->ip_v) {
12006 #ifdef INET
12007         case IPVERSION:
12008                 iph_out = mtod(mout, struct ip *);
12009
12010                 /* Fill in the IP header for the ABORT */
12011                 iph_out->ip_v = IPVERSION;
12012                 iph_out->ip_hl = (sizeof(struct ip) / 4);
12013                 iph_out->ip_tos = (u_char)0;
12014                 iph_out->ip_id = 0;
12015                 iph_out->ip_off = 0;
12016                 iph_out->ip_ttl = MAXTTL;
12017                 if (port) {
12018                         iph_out->ip_p = IPPROTO_UDP;
12019                 } else {
12020                         iph_out->ip_p = IPPROTO_SCTP;
12021                 }
12022                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12023                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12024                 /* let IP layer calculate this */
12025                 iph_out->ip_sum = 0;
12026
12027                 iphlen_out = sizeof(*iph_out);
12028                 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
12029                 break;
12030 #endif
12031 #ifdef INET6
12032         case IPV6_VERSION >> 4:
12033                 ip6 = (struct ip6_hdr *)iph;
12034                 ip6_out = mtod(mout, struct ip6_hdr *);
12035
12036                 /* Fill in the IP6 header for the ABORT */
12037                 ip6_out->ip6_flow = ip6->ip6_flow;
12038                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
12039                 if (port) {
12040                         ip6_out->ip6_nxt = IPPROTO_UDP;
12041                 } else {
12042                         ip6_out->ip6_nxt = IPPROTO_SCTP;
12043                 }
12044                 ip6_out->ip6_src = ip6->ip6_dst;
12045                 ip6_out->ip6_dst = ip6->ip6_src;
12046
12047                 iphlen_out = sizeof(*ip6_out);
12048                 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
12049                 break;
12050 #endif                          /* INET6 */
12051         default:
12052                 /* Currently not supported */
12053                 sctp_m_freem(mout);
12054                 return;
12055         }
12056
12057         udp = (struct udphdr *)abm;
12058         if (port) {
12059                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
12060                         sctp_m_freem(mout);
12061                         return;
12062                 }
12063                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12064                 udp->uh_dport = port;
12065                 /* set udp->uh_ulen later */
12066                 udp->uh_sum = 0;
12067                 iphlen_out += sizeof(struct udphdr);
12068                 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
12069         }
12070         abm->sh.src_port = sh->dest_port;
12071         abm->sh.dest_port = sh->src_port;
12072         abm->sh.checksum = 0;
12073         if (vtag == 0) {
12074                 abm->sh.v_tag = sh->v_tag;
12075                 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
12076         } else {
12077                 abm->sh.v_tag = htonl(vtag);
12078                 abm->msg.ch.chunk_flags = 0;
12079         }
12080         abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
12081
12082         if (err_cause) {
12083                 struct mbuf *m_tmp = err_cause;
12084                 int err_len = 0;
12085
12086                 /* get length of the err_cause chain */
12087                 while (m_tmp != NULL) {
12088                         err_len += SCTP_BUF_LEN(m_tmp);
12089                         m_tmp = SCTP_BUF_NEXT(m_tmp);
12090                 }
12091                 len = SCTP_BUF_LEN(mout) + err_len;
12092                 if (err_len % 4) {
12093                         /* need pad at end of chunk */
12094                         uint32_t cpthis = 0;
12095                         int padlen;
12096
12097                         padlen = 4 - (len % 4);
12098                         m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12099                         len += padlen;
12100                 }
12101                 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
12102         } else {
12103                 len = SCTP_BUF_LEN(mout);
12104                 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
12105         }
12106
12107         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12108                 /* no mbuf's */
12109                 sctp_m_freem(mout);
12110                 return;
12111         }
12112 #ifdef INET
12113         if (iph_out != NULL) {
12114                 sctp_route_t ro;
12115                 int ret;
12116
12117                 /* zap the stack pointer to the route */
12118                 bzero(&ro, sizeof ro);
12119                 if (port) {
12120                         udp->uh_ulen = htons(len - sizeof(struct ip));
12121                         if (V_udp_cksum) {
12122                                 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12123                         } else {
12124                                 udp->uh_sum = 0;
12125                         }
12126                 }
12127                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
12128                 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
12129                 /* set IPv4 length */
12130                 iph_out->ip_len = len;
12131                 /* out it goes */
12132 #ifdef  SCTP_PACKET_LOGGING
12133                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12134                         sctp_packet_log(mout, len);
12135 #endif
12136                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12137                 if (port) {
12138 #if defined(SCTP_WITH_NO_CSUM)
12139                         SCTP_STAT_INCR(sctps_sendnocrc);
12140 #else
12141                         abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
12142                         SCTP_STAT_INCR(sctps_sendswcrc);
12143 #endif
12144                         if (V_udp_cksum) {
12145                                 SCTP_ENABLE_UDP_CSUM(o_pak);
12146                         }
12147                 } else {
12148 #if defined(SCTP_WITH_NO_CSUM)
12149                         SCTP_STAT_INCR(sctps_sendnocrc);
12150 #else
12151                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12152                         mout->m_pkthdr.csum_data = 0;
12153                         SCTP_STAT_INCR(sctps_sendhwcrc);
12154 #endif
12155                 }
12156                 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12157
12158                 /* Free the route if we got one back */
12159                 if (ro.ro_rt)
12160                         RTFREE(ro.ro_rt);
12161         }
12162 #endif
12163 #ifdef INET6
12164         if (ip6_out != NULL) {
12165                 struct route_in6 ro;
12166                 int ret;
12167                 struct ifnet *ifp = NULL;
12168
12169                 /* zap the stack pointer to the route */
12170                 bzero(&ro, sizeof(ro));
12171                 if (port) {
12172                         udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12173                 }
12174                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
12175                 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
12176                 ip6_out->ip6_plen = len - sizeof(*ip6_out);
12177 #ifdef  SCTP_PACKET_LOGGING
12178                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12179                         sctp_packet_log(mout, len);
12180 #endif
12181                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12182                 if (port) {
12183 #if defined(SCTP_WITH_NO_CSUM)
12184                         SCTP_STAT_INCR(sctps_sendnocrc);
12185 #else
12186                         abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12187                         SCTP_STAT_INCR(sctps_sendswcrc);
12188 #endif
12189                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12190                                 udp->uh_sum = 0xffff;
12191                         }
12192                 } else {
12193 #if defined(SCTP_WITH_NO_CSUM)
12194                         SCTP_STAT_INCR(sctps_sendnocrc);
12195 #else
12196                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12197                         mout->m_pkthdr.csum_data = 0;
12198                         SCTP_STAT_INCR(sctps_sendhwcrc);
12199 #endif
12200                 }
12201                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12202
12203                 /* Free the route if we got one back */
12204                 if (ro.ro_rt)
12205                         RTFREE(ro.ro_rt);
12206         }
12207 #endif
12208         SCTP_STAT_INCR(sctps_sendpackets);
12209         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12210         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12211 }
12212
12213 void
12214 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
12215     uint32_t vrf_id, uint16_t port)
12216 {
12217         struct mbuf *o_pak;
12218         struct sctphdr *sh, *sh_out;
12219         struct sctp_chunkhdr *ch;
12220         struct ip *iph;
12221         struct udphdr *udp = NULL;
12222         struct mbuf *mout;
12223         int iphlen_out, len;
12224
12225 #ifdef INET
12226         struct ip *iph_out;
12227
12228 #endif
12229 #ifdef INET6
12230         struct ip6_hdr *ip6, *ip6_out;
12231
12232 #endif
12233
12234         iph = mtod(m, struct ip *);
12235         sh = (struct sctphdr *)((caddr_t)iph + iphlen);
12236         switch (iph->ip_v) {
12237 #ifdef INET
12238         case IPVERSION:
12239                 len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
12240                 break;
12241 #endif
12242 #ifdef INET6
12243         case IPV6_VERSION >> 4:
12244                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
12245                 break;
12246 #endif
12247         default:
12248                 if (scm) {
12249                         sctp_m_freem(scm);
12250                 }
12251                 return;
12252         }
12253         if (port) {
12254                 len += sizeof(struct udphdr);
12255         }
12256         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
12257         if (mout == NULL) {
12258                 if (scm) {
12259                         sctp_m_freem(scm);
12260                 }
12261                 return;
12262         }
12263         SCTP_BUF_RESV_UF(mout, max_linkhdr);
12264         SCTP_BUF_LEN(mout) = len;
12265         SCTP_BUF_NEXT(mout) = scm;
12266         if (m->m_flags & M_FLOWID) {
12267                 mout->m_pkthdr.flowid = m->m_pkthdr.flowid;
12268                 mout->m_flags |= M_FLOWID;
12269         }
12270 #ifdef INET
12271         iph_out = NULL;
12272 #endif
12273 #ifdef INET6
12274         ip6_out = NULL;
12275 #endif
12276         switch (iph->ip_v) {
12277 #ifdef INET
12278         case IPVERSION:
12279                 iph_out = mtod(mout, struct ip *);
12280
12281                 /* Fill in the IP header for the ABORT */
12282                 iph_out->ip_v = IPVERSION;
12283                 iph_out->ip_hl = (sizeof(struct ip) / 4);
12284                 iph_out->ip_tos = (u_char)0;
12285                 iph_out->ip_id = 0;
12286                 iph_out->ip_off = 0;
12287                 iph_out->ip_ttl = MAXTTL;
12288                 if (port) {
12289                         iph_out->ip_p = IPPROTO_UDP;
12290                 } else {
12291                         iph_out->ip_p = IPPROTO_SCTP;
12292                 }
12293                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12294                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12295                 /* let IP layer calculate this */
12296                 iph_out->ip_sum = 0;
12297
12298                 iphlen_out = sizeof(struct ip);
12299                 sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
12300                 break;
12301 #endif
12302 #ifdef INET6
12303         case IPV6_VERSION >> 4:
12304                 ip6 = (struct ip6_hdr *)iph;
12305                 ip6_out = mtod(mout, struct ip6_hdr *);
12306
12307                 /* Fill in the IP6 header for the ABORT */
12308                 ip6_out->ip6_flow = ip6->ip6_flow;
12309                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
12310                 if (port) {
12311                         ip6_out->ip6_nxt = IPPROTO_UDP;
12312                 } else {
12313                         ip6_out->ip6_nxt = IPPROTO_SCTP;
12314                 }
12315                 ip6_out->ip6_src = ip6->ip6_dst;
12316                 ip6_out->ip6_dst = ip6->ip6_src;
12317
12318                 iphlen_out = sizeof(struct ip6_hdr);
12319                 sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
12320                 break;
12321 #endif                          /* INET6 */
12322         default:
12323                 /* Currently not supported */
12324                 sctp_m_freem(mout);
12325                 return;
12326         }
12327
12328         udp = (struct udphdr *)sh_out;
12329         if (port) {
12330                 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
12331                         sctp_m_freem(mout);
12332                         return;
12333                 }
12334                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12335                 udp->uh_dport = port;
12336                 /* set udp->uh_ulen later */
12337                 udp->uh_sum = 0;
12338                 iphlen_out += sizeof(struct udphdr);
12339                 sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
12340         }
12341         sh_out->src_port = sh->dest_port;
12342         sh_out->dest_port = sh->src_port;
12343         sh_out->v_tag = vtag;
12344         sh_out->checksum = 0;
12345
12346         ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
12347         ch->chunk_type = SCTP_OPERATION_ERROR;
12348         ch->chunk_flags = 0;
12349
12350         if (scm) {
12351                 struct mbuf *m_tmp = scm;
12352                 int cause_len = 0;
12353
12354                 /* get length of the err_cause chain */
12355                 while (m_tmp != NULL) {
12356                         cause_len += SCTP_BUF_LEN(m_tmp);
12357                         m_tmp = SCTP_BUF_NEXT(m_tmp);
12358                 }
12359                 len = SCTP_BUF_LEN(mout) + cause_len;
12360                 if (cause_len % 4) {
12361                         /* need pad at end of chunk */
12362                         uint32_t cpthis = 0;
12363                         int padlen;
12364
12365                         padlen = 4 - (len % 4);
12366                         m_copyback(mout, len, padlen, (caddr_t)&cpthis);
12367                         len += padlen;
12368                 }
12369                 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
12370         } else {
12371                 len = SCTP_BUF_LEN(mout);
12372                 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
12373         }
12374
12375         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12376                 /* no mbuf's */
12377                 sctp_m_freem(mout);
12378                 return;
12379         }
12380 #ifdef INET
12381         if (iph_out != NULL) {
12382                 sctp_route_t ro;
12383                 int ret;
12384
12385                 /* zap the stack pointer to the route */
12386                 bzero(&ro, sizeof ro);
12387                 if (port) {
12388                         udp->uh_ulen = htons(len - sizeof(struct ip));
12389                         if (V_udp_cksum) {
12390                                 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12391                         } else {
12392                                 udp->uh_sum = 0;
12393                         }
12394                 }
12395                 /* set IPv4 length */
12396                 iph_out->ip_len = len;
12397                 /* out it goes */
12398 #ifdef  SCTP_PACKET_LOGGING
12399                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12400                         sctp_packet_log(mout, len);
12401 #endif
12402                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12403                 if (port) {
12404 #if defined(SCTP_WITH_NO_CSUM)
12405                         SCTP_STAT_INCR(sctps_sendnocrc);
12406 #else
12407                         sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
12408                         SCTP_STAT_INCR(sctps_sendswcrc);
12409 #endif
12410                         if (V_udp_cksum) {
12411                                 SCTP_ENABLE_UDP_CSUM(o_pak);
12412                         }
12413                 } else {
12414 #if defined(SCTP_WITH_NO_CSUM)
12415                         SCTP_STAT_INCR(sctps_sendnocrc);
12416 #else
12417                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12418                         mout->m_pkthdr.csum_data = 0;
12419                         SCTP_STAT_INCR(sctps_sendhwcrc);
12420 #endif
12421                 }
12422                 SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12423
12424                 /* Free the route if we got one back */
12425                 if (ro.ro_rt)
12426                         RTFREE(ro.ro_rt);
12427         }
12428 #endif
12429 #ifdef INET6
12430         if (ip6_out != NULL) {
12431                 struct route_in6 ro;
12432                 int ret;
12433                 struct ifnet *ifp = NULL;
12434
12435                 /* zap the stack pointer to the route */
12436                 bzero(&ro, sizeof(ro));
12437                 if (port) {
12438                         udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
12439                 }
12440                 ip6_out->ip6_plen = len - sizeof(*ip6_out);
12441 #ifdef  SCTP_PACKET_LOGGING
12442                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12443                         sctp_packet_log(mout, len);
12444 #endif
12445                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
12446                 if (port) {
12447 #if defined(SCTP_WITH_NO_CSUM)
12448                         SCTP_STAT_INCR(sctps_sendnocrc);
12449 #else
12450                         sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12451                         SCTP_STAT_INCR(sctps_sendswcrc);
12452 #endif
12453                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12454                                 udp->uh_sum = 0xffff;
12455                         }
12456                 } else {
12457 #if defined(SCTP_WITH_NO_CSUM)
12458                         SCTP_STAT_INCR(sctps_sendnocrc);
12459 #else
12460                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
12461                         mout->m_pkthdr.csum_data = 0;
12462                         SCTP_STAT_INCR(sctps_sendhwcrc);
12463 #endif
12464                 }
12465                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, NULL, vrf_id);
12466
12467                 /* Free the route if we got one back */
12468                 if (ro.ro_rt)
12469                         RTFREE(ro.ro_rt);
12470         }
12471 #endif
12472         SCTP_STAT_INCR(sctps_sendpackets);
12473         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12474         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12475 }
12476
12477 static struct mbuf *
12478 sctp_copy_resume(struct uio *uio,
12479     int max_send_len,
12480     int user_marks_eor,
12481     int *error,
12482     uint32_t * sndout,
12483     struct mbuf **new_tail)
12484 {
12485         struct mbuf *m;
12486
12487         m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12488             (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12489         if (m == NULL) {
12490                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12491                 *error = ENOMEM;
12492         } else {
12493                 *sndout = m_length(m, NULL);
12494                 *new_tail = m_last(m);
12495         }
12496         return (m);
12497 }
12498
12499 static int
12500 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12501     struct uio *uio,
12502     int resv_upfront)
12503 {
12504         int left;
12505
12506         left = sp->length;
12507         sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
12508             resv_upfront, 0);
12509         if (sp->data == NULL) {
12510                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12511                 return (ENOMEM);
12512         }
12513         sp->tail_mbuf = m_last(sp->data);
12514         return (0);
12515 }
12516
12517
12518
12519 static struct sctp_stream_queue_pending *
12520 sctp_copy_it_in(struct sctp_tcb *stcb,
12521     struct sctp_association *asoc,
12522     struct sctp_sndrcvinfo *srcv,
12523     struct uio *uio,
12524     struct sctp_nets *net,
12525     int max_send_len,
12526     int user_marks_eor,
12527     int *error)
12528 {
12529         /*-
12530          * This routine must be very careful in its work. Protocol
12531          * processing is up and running so care must be taken to spl...()
12532          * when you need to do something that may effect the stcb/asoc. The
12533          * sb is locked however. When data is copied the protocol processing
12534          * should be enabled since this is a slower operation...
12535          */
12536         struct sctp_stream_queue_pending *sp = NULL;
12537         int resv_in_first;
12538
12539         *error = 0;
12540         /* Now can we send this? */
12541         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12542             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12543             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12544             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12545                 /* got data while shutting down */
12546                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12547                 *error = ECONNRESET;
12548                 goto out_now;
12549         }
12550         sctp_alloc_a_strmoq(stcb, sp);
12551         if (sp == NULL) {
12552                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12553                 *error = ENOMEM;
12554                 goto out_now;
12555         }
12556         sp->act_flags = 0;
12557         sp->sender_all_done = 0;
12558         sp->sinfo_flags = srcv->sinfo_flags;
12559         sp->timetolive = srcv->sinfo_timetolive;
12560         sp->ppid = srcv->sinfo_ppid;
12561         sp->context = srcv->sinfo_context;
12562         sp->strseq = 0;
12563         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12564
12565         sp->stream = srcv->sinfo_stream;
12566         sp->length = min(uio->uio_resid, max_send_len);
12567         if ((sp->length == (uint32_t) uio->uio_resid) &&
12568             ((user_marks_eor == 0) ||
12569             (srcv->sinfo_flags & SCTP_EOF) ||
12570             (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12571                 sp->msg_is_complete = 1;
12572         } else {
12573                 sp->msg_is_complete = 0;
12574         }
12575         sp->sender_all_done = 0;
12576         sp->some_taken = 0;
12577         sp->put_last_out = 0;
12578         resv_in_first = sizeof(struct sctp_data_chunk);
12579         sp->data = sp->tail_mbuf = NULL;
12580         if (sp->length == 0) {
12581                 *error = 0;
12582                 goto skip_copy;
12583         }
12584         if (srcv->sinfo_keynumber_valid) {
12585                 sp->auth_keyid = srcv->sinfo_keynumber;
12586         } else {
12587                 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12588         }
12589         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12590                 sctp_auth_key_acquire(stcb, sp->auth_keyid);
12591                 sp->holds_key_ref = 1;
12592         }
12593         *error = sctp_copy_one(sp, uio, resv_in_first);
12594 skip_copy:
12595         if (*error) {
12596                 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12597                 sp = NULL;
12598         } else {
12599                 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12600                         sp->net = net;
12601                         atomic_add_int(&sp->net->ref_count, 1);
12602                 } else {
12603                         sp->net = NULL;
12604                 }
12605                 sctp_set_prsctp_policy(sp);
12606         }
12607 out_now:
12608         return (sp);
12609 }
12610
12611
12612 int
12613 sctp_sosend(struct socket *so,
12614     struct sockaddr *addr,
12615     struct uio *uio,
12616     struct mbuf *top,
12617     struct mbuf *control,
12618     int flags,
12619     struct thread *p
12620 )
12621 {
12622         int error, use_sndinfo = 0;
12623         struct sctp_sndrcvinfo sndrcvninfo;
12624         struct sockaddr *addr_to_use;
12625
12626 #if defined(INET) && defined(INET6)
12627         struct sockaddr_in sin;
12628
12629 #endif
12630
12631         if (control) {
12632                 /* process cmsg snd/rcv info (maybe a assoc-id) */
12633                 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control,
12634                     sizeof(sndrcvninfo))) {
12635                         /* got one */
12636                         use_sndinfo = 1;
12637                 }
12638         }
12639         addr_to_use = addr;
12640 #if defined(INET) && defined(INET6)
12641         if ((addr) && (addr->sa_family == AF_INET6)) {
12642                 struct sockaddr_in6 *sin6;
12643
12644                 sin6 = (struct sockaddr_in6 *)addr;
12645                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12646                         in6_sin6_2_sin(&sin, sin6);
12647                         addr_to_use = (struct sockaddr *)&sin;
12648                 }
12649         }
12650 #endif
12651         error = sctp_lower_sosend(so, addr_to_use, uio, top,
12652             control,
12653             flags,
12654             use_sndinfo ? &sndrcvninfo : NULL
12655             ,p
12656             );
12657         return (error);
12658 }
12659
12660
12661 int
12662 sctp_lower_sosend(struct socket *so,
12663     struct sockaddr *addr,
12664     struct uio *uio,
12665     struct mbuf *i_pak,
12666     struct mbuf *control,
12667     int flags,
12668     struct sctp_sndrcvinfo *srcv
12669     ,
12670     struct thread *p
12671 )
12672 {
12673         unsigned int sndlen = 0, max_len;
12674         int error, len;
12675         struct mbuf *top = NULL;
12676         int queue_only = 0, queue_only_for_init = 0;
12677         int free_cnt_applied = 0;
12678         int un_sent;
12679         int now_filled = 0;
12680         unsigned int inqueue_bytes = 0;
12681         struct sctp_block_entry be;
12682         struct sctp_inpcb *inp;
12683         struct sctp_tcb *stcb = NULL;
12684         struct timeval now;
12685         struct sctp_nets *net;
12686         struct sctp_association *asoc;
12687         struct sctp_inpcb *t_inp;
12688         int user_marks_eor;
12689         int create_lock_applied = 0;
12690         int nagle_applies = 0;
12691         int some_on_control = 0;
12692         int got_all_of_the_send = 0;
12693         int hold_tcblock = 0;
12694         int non_blocking = 0;
12695         uint32_t local_add_more, local_soresv = 0;
12696         uint16_t port;
12697         uint16_t sinfo_flags;
12698         sctp_assoc_t sinfo_assoc_id;
12699
12700         error = 0;
12701         net = NULL;
12702         stcb = NULL;
12703         asoc = NULL;
12704
12705         t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12706         if (inp == NULL) {
12707                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12708                 error = EINVAL;
12709                 if (i_pak) {
12710                         SCTP_RELEASE_PKT(i_pak);
12711                 }
12712                 return (error);
12713         }
12714         if ((uio == NULL) && (i_pak == NULL)) {
12715                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12716                 return (EINVAL);
12717         }
12718         user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12719         atomic_add_int(&inp->total_sends, 1);
12720         if (uio) {
12721                 if (uio->uio_resid < 0) {
12722                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12723                         return (EINVAL);
12724                 }
12725                 sndlen = uio->uio_resid;
12726         } else {
12727                 top = SCTP_HEADER_TO_CHAIN(i_pak);
12728                 sndlen = SCTP_HEADER_LEN(i_pak);
12729         }
12730         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12731             addr,
12732             sndlen);
12733         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12734             (inp->sctp_socket->so_qlimit)) {
12735                 /* The listener can NOT send */
12736                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12737                 error = ENOTCONN;
12738                 goto out_unlocked;
12739         }
12740         /**
12741          * Pre-screen address, if one is given the sin-len
12742          * must be set correctly!
12743          */
12744         if (addr) {
12745                 union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12746
12747                 switch (raddr->sa.sa_family) {
12748 #if defined(INET)
12749                 case AF_INET:
12750                         if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12751                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12752                                 error = EINVAL;
12753                                 goto out_unlocked;
12754                         }
12755                         port = raddr->sin.sin_port;
12756                         break;
12757 #endif
12758 #if defined(INET6)
12759                 case AF_INET6:
12760                         if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12761                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12762                                 error = EINVAL;
12763                                 goto out_unlocked;
12764                         }
12765                         port = raddr->sin6.sin6_port;
12766                         break;
12767 #endif
12768                 default:
12769                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT);
12770                         error = EAFNOSUPPORT;
12771                         goto out_unlocked;
12772                 }
12773         } else
12774                 port = 0;
12775
12776         if (srcv) {
12777                 sinfo_flags = srcv->sinfo_flags;
12778                 sinfo_assoc_id = srcv->sinfo_assoc_id;
12779                 if (INVALID_SINFO_FLAG(sinfo_flags) ||
12780                     PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12781                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12782                         error = EINVAL;
12783                         goto out_unlocked;
12784                 }
12785                 if (srcv->sinfo_flags)
12786                         SCTP_STAT_INCR(sctps_sends_with_flags);
12787         } else {
12788                 sinfo_flags = inp->def_send.sinfo_flags;
12789                 sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12790         }
12791         if (sinfo_flags & SCTP_SENDALL) {
12792                 /* its a sendall */
12793                 error = sctp_sendall(inp, uio, top, srcv);
12794                 top = NULL;
12795                 goto out_unlocked;
12796         }
12797         if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12798                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12799                 error = EINVAL;
12800                 goto out_unlocked;
12801         }
12802         /* now we must find the assoc */
12803         if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12804             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12805                 SCTP_INP_RLOCK(inp);
12806                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12807                 if (stcb) {
12808                         SCTP_TCB_LOCK(stcb);
12809                         hold_tcblock = 1;
12810                 }
12811                 SCTP_INP_RUNLOCK(inp);
12812         } else if (sinfo_assoc_id) {
12813                 stcb = sctp_findassociation_ep_asocid(inp, sinfo_assoc_id, 0);
12814         } else if (addr) {
12815                 /*-
12816                  * Since we did not use findep we must
12817                  * increment it, and if we don't find a tcb
12818                  * decrement it.
12819                  */
12820                 SCTP_INP_WLOCK(inp);
12821                 SCTP_INP_INCR_REF(inp);
12822                 SCTP_INP_WUNLOCK(inp);
12823                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12824                 if (stcb == NULL) {
12825                         SCTP_INP_WLOCK(inp);
12826                         SCTP_INP_DECR_REF(inp);
12827                         SCTP_INP_WUNLOCK(inp);
12828                 } else {
12829                         hold_tcblock = 1;
12830                 }
12831         }
12832         if ((stcb == NULL) && (addr)) {
12833                 /* Possible implicit send? */
12834                 SCTP_ASOC_CREATE_LOCK(inp);
12835                 create_lock_applied = 1;
12836                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12837                     (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12838                         /* Should I really unlock ? */
12839                         SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12840                         error = EINVAL;
12841                         goto out_unlocked;
12842
12843                 }
12844                 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12845                     (addr->sa_family == AF_INET6)) {
12846                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12847                         error = EINVAL;
12848                         goto out_unlocked;
12849                 }
12850                 SCTP_INP_WLOCK(inp);
12851                 SCTP_INP_INCR_REF(inp);
12852                 SCTP_INP_WUNLOCK(inp);
12853                 /* With the lock applied look again */
12854                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12855                 if ((stcb == NULL) && (control != NULL) && (port > 0)) {
12856                         stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
12857                 }
12858                 if (stcb == NULL) {
12859                         SCTP_INP_WLOCK(inp);
12860                         SCTP_INP_DECR_REF(inp);
12861                         SCTP_INP_WUNLOCK(inp);
12862                 } else {
12863                         hold_tcblock = 1;
12864                 }
12865                 if (error) {
12866                         goto out_unlocked;
12867                 }
12868                 if (t_inp != inp) {
12869                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12870                         error = ENOTCONN;
12871                         goto out_unlocked;
12872                 }
12873         }
12874         if (stcb == NULL) {
12875                 if (addr == NULL) {
12876                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12877                         error = ENOENT;
12878                         goto out_unlocked;
12879                 } else {
12880                         /* We must go ahead and start the INIT process */
12881                         uint32_t vrf_id;
12882
12883                         if ((sinfo_flags & SCTP_ABORT) ||
12884                             ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12885                                 /*-
12886                                  * User asks to abort a non-existant assoc,
12887                                  * or EOF a non-existant assoc with no data
12888                                  */
12889                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12890                                 error = ENOENT;
12891                                 goto out_unlocked;
12892                         }
12893                         /* get an asoc/stcb struct */
12894                         vrf_id = inp->def_vrf_id;
12895 #ifdef INVARIANTS
12896                         if (create_lock_applied == 0) {
12897                                 panic("Error, should hold create lock and I don't?");
12898                         }
12899 #endif
12900                         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12901                             p
12902                             );
12903                         if (stcb == NULL) {
12904                                 /* Error is setup for us in the call */
12905                                 goto out_unlocked;
12906                         }
12907                         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
12908                                 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
12909                                 /*
12910                                  * Set the connected flag so we can queue
12911                                  * data
12912                                  */
12913                                 soisconnecting(so);
12914                         }
12915                         hold_tcblock = 1;
12916                         if (create_lock_applied) {
12917                                 SCTP_ASOC_CREATE_UNLOCK(inp);
12918                                 create_lock_applied = 0;
12919                         } else {
12920                                 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12921                         }
12922                         /*
12923                          * Turn on queue only flag to prevent data from
12924                          * being sent
12925                          */
12926                         queue_only = 1;
12927                         asoc = &stcb->asoc;
12928                         SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12929                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12930
12931                         /* initialize authentication params for the assoc */
12932                         sctp_initialize_auth_params(inp, stcb);
12933
12934                         if (control) {
12935                                 if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
12936                                         sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_7);
12937                                         hold_tcblock = 0;
12938                                         stcb = NULL;
12939                                         goto out_unlocked;
12940                                 }
12941                         }
12942                         /* out with the INIT */
12943                         queue_only_for_init = 1;
12944                         /*-
12945                          * we may want to dig in after this call and adjust the MTU
12946                          * value. It defaulted to 1500 (constant) but the ro
12947                          * structure may now have an update and thus we may need to
12948                          * change it BEFORE we append the message.
12949                          */
12950                 }
12951         } else
12952                 asoc = &stcb->asoc;
12953         if (srcv == NULL)
12954                 srcv = (struct sctp_sndrcvinfo *)&asoc->def_send;
12955         if (srcv->sinfo_flags & SCTP_ADDR_OVER) {
12956                 if (addr)
12957                         net = sctp_findnet(stcb, addr);
12958                 else
12959                         net = NULL;
12960                 if ((net == NULL) ||
12961                     ((port != 0) && (port != stcb->rport))) {
12962                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12963                         error = EINVAL;
12964                         goto out_unlocked;
12965                 }
12966         } else {
12967                 if (stcb->asoc.alternate) {
12968                         net = stcb->asoc.alternate;
12969                 } else {
12970                         net = stcb->asoc.primary_destination;
12971                 }
12972         }
12973         atomic_add_int(&stcb->total_sends, 1);
12974         /* Keep the stcb from being freed under our feet */
12975         atomic_add_int(&asoc->refcnt, 1);
12976         free_cnt_applied = 1;
12977
12978         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12979                 if (sndlen > asoc->smallest_mtu) {
12980                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12981                         error = EMSGSIZE;
12982                         goto out_unlocked;
12983                 }
12984         }
12985         if (SCTP_SO_IS_NBIO(so)
12986             || (flags & MSG_NBIO)
12987             ) {
12988                 non_blocking = 1;
12989         }
12990         /* would we block? */
12991         if (non_blocking) {
12992                 if (hold_tcblock == 0) {
12993                         SCTP_TCB_LOCK(stcb);
12994                         hold_tcblock = 1;
12995                 }
12996                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12997                 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12998                     (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12999                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
13000                         if (sndlen > SCTP_SB_LIMIT_SND(so))
13001                                 error = EMSGSIZE;
13002                         else
13003                                 error = EWOULDBLOCK;
13004                         goto out_unlocked;
13005                 }
13006                 stcb->asoc.sb_send_resv += sndlen;
13007                 SCTP_TCB_UNLOCK(stcb);
13008                 hold_tcblock = 0;
13009         } else {
13010                 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
13011         }
13012         local_soresv = sndlen;
13013         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13014                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13015                 error = ECONNRESET;
13016                 goto out_unlocked;
13017         }
13018         if (create_lock_applied) {
13019                 SCTP_ASOC_CREATE_UNLOCK(inp);
13020                 create_lock_applied = 0;
13021         }
13022         if (asoc->stream_reset_outstanding) {
13023                 /*
13024                  * Can't queue any data while stream reset is underway.
13025                  */
13026                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
13027                 error = EAGAIN;
13028                 goto out_unlocked;
13029         }
13030         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
13031             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
13032                 queue_only = 1;
13033         }
13034         /* we are now done with all control */
13035         if (control) {
13036                 sctp_m_freem(control);
13037                 control = NULL;
13038         }
13039         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
13040             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13041             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13042             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13043                 if (srcv->sinfo_flags & SCTP_ABORT) {
13044                         ;
13045                 } else {
13046                         SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13047                         error = ECONNRESET;
13048                         goto out_unlocked;
13049                 }
13050         }
13051         /* Ok, we will attempt a msgsnd :> */
13052         if (p) {
13053                 p->td_ru.ru_msgsnd++;
13054         }
13055         /* Are we aborting? */
13056         if (srcv->sinfo_flags & SCTP_ABORT) {
13057                 struct mbuf *mm;
13058                 int tot_demand, tot_out = 0, max_out;
13059
13060                 SCTP_STAT_INCR(sctps_sends_with_abort);
13061                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
13062                     (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
13063                         /* It has to be up before we abort */
13064                         /* how big is the user initiated abort? */
13065                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13066                         error = EINVAL;
13067                         goto out;
13068                 }
13069                 if (hold_tcblock) {
13070                         SCTP_TCB_UNLOCK(stcb);
13071                         hold_tcblock = 0;
13072                 }
13073                 if (top) {
13074                         struct mbuf *cntm = NULL;
13075
13076                         mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_WAIT, 1, MT_DATA);
13077                         if (sndlen != 0) {
13078                                 for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
13079                                         tot_out += SCTP_BUF_LEN(cntm);
13080                                 }
13081                         }
13082                 } else {
13083                         /* Must fit in a MTU */
13084                         tot_out = sndlen;
13085                         tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
13086                         if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
13087                                 /* To big */
13088                                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13089                                 error = EMSGSIZE;
13090                                 goto out;
13091                         }
13092                         mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
13093                 }
13094                 if (mm == NULL) {
13095                         SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13096                         error = ENOMEM;
13097                         goto out;
13098                 }
13099                 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
13100                 max_out -= sizeof(struct sctp_abort_msg);
13101                 if (tot_out > max_out) {
13102                         tot_out = max_out;
13103                 }
13104                 if (mm) {
13105                         struct sctp_paramhdr *ph;
13106
13107                         /* now move forward the data pointer */
13108                         ph = mtod(mm, struct sctp_paramhdr *);
13109                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
13110                         ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
13111                         ph++;
13112                         SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
13113                         if (top == NULL) {
13114                                 error = uiomove((caddr_t)ph, (int)tot_out, uio);
13115                                 if (error) {
13116                                         /*-
13117                                          * Here if we can't get his data we
13118                                          * still abort we just don't get to
13119                                          * send the users note :-0
13120                                          */
13121                                         sctp_m_freem(mm);
13122                                         mm = NULL;
13123                                 }
13124                         } else {
13125                                 if (sndlen != 0) {
13126                                         SCTP_BUF_NEXT(mm) = top;
13127                                 }
13128                         }
13129                 }
13130                 if (hold_tcblock == 0) {
13131                         SCTP_TCB_LOCK(stcb);
13132                 }
13133                 atomic_add_int(&stcb->asoc.refcnt, -1);
13134                 free_cnt_applied = 0;
13135                 /* release this lock, otherwise we hang on ourselves */
13136                 sctp_abort_an_association(stcb->sctp_ep, stcb, mm, SCTP_SO_LOCKED);
13137                 /* now relock the stcb so everything is sane */
13138                 hold_tcblock = 0;
13139                 stcb = NULL;
13140                 /*
13141                  * In this case top is already chained to mm avoid double
13142                  * free, since we free it below if top != NULL and driver
13143                  * would free it after sending the packet out
13144                  */
13145                 if (sndlen != 0) {
13146                         top = NULL;
13147                 }
13148                 goto out_unlocked;
13149         }
13150         /* Calculate the maximum we can send */
13151         inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13152         if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13153                 if (non_blocking) {
13154                         /* we already checked for non-blocking above. */
13155                         max_len = sndlen;
13156                 } else {
13157                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13158                 }
13159         } else {
13160                 max_len = 0;
13161         }
13162         if (hold_tcblock) {
13163                 SCTP_TCB_UNLOCK(stcb);
13164                 hold_tcblock = 0;
13165         }
13166         /* Is the stream no. valid? */
13167         if (srcv->sinfo_stream >= asoc->streamoutcnt) {
13168                 /* Invalid stream number */
13169                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13170                 error = EINVAL;
13171                 goto out_unlocked;
13172         }
13173         if (asoc->strmout == NULL) {
13174                 /* huh? software error */
13175                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
13176                 error = EFAULT;
13177                 goto out_unlocked;
13178         }
13179         /* Unless E_EOR mode is on, we must make a send FIT in one call. */
13180         if ((user_marks_eor == 0) &&
13181             (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
13182                 /* It will NEVER fit */
13183                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
13184                 error = EMSGSIZE;
13185                 goto out_unlocked;
13186         }
13187         if ((uio == NULL) && user_marks_eor) {
13188                 /*-
13189                  * We do not support eeor mode for
13190                  * sending with mbuf chains (like sendfile).
13191                  */
13192                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13193                 error = EINVAL;
13194                 goto out_unlocked;
13195         }
13196         if (user_marks_eor) {
13197                 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
13198         } else {
13199                 /*-
13200                  * For non-eeor the whole message must fit in
13201                  * the socket send buffer.
13202                  */
13203                 local_add_more = sndlen;
13204         }
13205         len = 0;
13206         if (non_blocking) {
13207                 goto skip_preblock;
13208         }
13209         if (((max_len <= local_add_more) &&
13210             (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
13211             (max_len == 0) ||
13212             ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13213                 /* No room right now ! */
13214                 SOCKBUF_LOCK(&so->so_snd);
13215                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13216                 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
13217                     ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
13218                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
13219                             (unsigned int)SCTP_SB_LIMIT_SND(so),
13220                             inqueue_bytes,
13221                             local_add_more,
13222                             stcb->asoc.stream_queue_cnt,
13223                             stcb->asoc.chunks_on_out_queue,
13224                             SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
13225                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13226                                 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
13227                         }
13228                         be.error = 0;
13229                         stcb->block_entry = &be;
13230                         error = sbwait(&so->so_snd);
13231                         stcb->block_entry = NULL;
13232                         if (error || so->so_error || be.error) {
13233                                 if (error == 0) {
13234                                         if (so->so_error)
13235                                                 error = so->so_error;
13236                                         if (be.error) {
13237                                                 error = be.error;
13238                                         }
13239                                 }
13240                                 SOCKBUF_UNLOCK(&so->so_snd);
13241                                 goto out_unlocked;
13242                         }
13243                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13244                                 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13245                                     asoc, stcb->asoc.total_output_queue_size);
13246                         }
13247                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13248                                 goto out_unlocked;
13249                         }
13250                         inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13251                 }
13252                 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13253                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13254                 } else {
13255                         max_len = 0;
13256                 }
13257                 SOCKBUF_UNLOCK(&so->so_snd);
13258         }
13259 skip_preblock:
13260         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13261                 goto out_unlocked;
13262         }
13263         /*
13264          * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13265          * case NOTE: uio will be null when top/mbuf is passed
13266          */
13267         if (sndlen == 0) {
13268                 if (srcv->sinfo_flags & SCTP_EOF) {
13269                         got_all_of_the_send = 1;
13270                         goto dataless_eof;
13271                 } else {
13272                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13273                         error = EINVAL;
13274                         goto out;
13275                 }
13276         }
13277         if (top == NULL) {
13278                 struct sctp_stream_queue_pending *sp;
13279                 struct sctp_stream_out *strm;
13280                 uint32_t sndout;
13281
13282                 SCTP_TCB_SEND_LOCK(stcb);
13283                 if ((asoc->stream_locked) &&
13284                     (asoc->stream_locked_on != srcv->sinfo_stream)) {
13285                         SCTP_TCB_SEND_UNLOCK(stcb);
13286                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13287                         error = EINVAL;
13288                         goto out;
13289                 }
13290                 SCTP_TCB_SEND_UNLOCK(stcb);
13291
13292                 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
13293                 if (strm->last_msg_incomplete == 0) {
13294         do_a_copy_in:
13295                         sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error);
13296                         if ((sp == NULL) || (error)) {
13297                                 goto out;
13298                         }
13299                         SCTP_TCB_SEND_LOCK(stcb);
13300                         if (sp->msg_is_complete) {
13301                                 strm->last_msg_incomplete = 0;
13302                                 asoc->stream_locked = 0;
13303                         } else {
13304                                 /*
13305                                  * Just got locked to this guy in case of an
13306                                  * interrupt.
13307                                  */
13308                                 strm->last_msg_incomplete = 1;
13309                                 asoc->stream_locked = 1;
13310                                 asoc->stream_locked_on = srcv->sinfo_stream;
13311                                 sp->sender_all_done = 0;
13312                         }
13313                         sctp_snd_sb_alloc(stcb, sp->length);
13314                         atomic_add_int(&asoc->stream_queue_cnt, 1);
13315                         if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
13316                                 sp->strseq = strm->next_sequence_sent;
13317                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
13318                                         sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
13319                                             (uintptr_t) stcb, sp->length,
13320                                             (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
13321                                 }
13322                                 strm->next_sequence_sent++;
13323                         } else {
13324                                 SCTP_STAT_INCR(sctps_sends_with_unord);
13325                         }
13326                         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13327                         stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp, 1);
13328                         SCTP_TCB_SEND_UNLOCK(stcb);
13329                 } else {
13330                         SCTP_TCB_SEND_LOCK(stcb);
13331                         sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13332                         SCTP_TCB_SEND_UNLOCK(stcb);
13333                         if (sp == NULL) {
13334                                 /* ???? Huh ??? last msg is gone */
13335 #ifdef INVARIANTS
13336                                 panic("Warning: Last msg marked incomplete, yet nothing left?");
13337 #else
13338                                 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13339                                 strm->last_msg_incomplete = 0;
13340 #endif
13341                                 goto do_a_copy_in;
13342
13343                         }
13344                 }
13345                 while (uio->uio_resid > 0) {
13346                         /* How much room do we have? */
13347                         struct mbuf *new_tail, *mm;
13348
13349                         if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13350                                 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
13351                         else
13352                                 max_len = 0;
13353
13354                         if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13355                             (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13356                             (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
13357                                 sndout = 0;
13358                                 new_tail = NULL;
13359                                 if (hold_tcblock) {
13360                                         SCTP_TCB_UNLOCK(stcb);
13361                                         hold_tcblock = 0;
13362                                 }
13363                                 mm = sctp_copy_resume(uio, max_len, user_marks_eor, &error, &sndout, &new_tail);
13364                                 if ((mm == NULL) || error) {
13365                                         if (mm) {
13366                                                 sctp_m_freem(mm);
13367                                         }
13368                                         goto out;
13369                                 }
13370                                 /* Update the mbuf and count */
13371                                 SCTP_TCB_SEND_LOCK(stcb);
13372                                 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13373                                         /*
13374                                          * we need to get out. Peer probably
13375                                          * aborted.
13376                                          */
13377                                         sctp_m_freem(mm);
13378                                         if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13379                                                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13380                                                 error = ECONNRESET;
13381                                         }
13382                                         SCTP_TCB_SEND_UNLOCK(stcb);
13383                                         goto out;
13384                                 }
13385                                 if (sp->tail_mbuf) {
13386                                         /* tack it to the end */
13387                                         SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13388                                         sp->tail_mbuf = new_tail;
13389                                 } else {
13390                                         /* A stolen mbuf */
13391                                         sp->data = mm;
13392                                         sp->tail_mbuf = new_tail;
13393                                 }
13394                                 sctp_snd_sb_alloc(stcb, sndout);
13395                                 atomic_add_int(&sp->length, sndout);
13396                                 len += sndout;
13397
13398                                 /* Did we reach EOR? */
13399                                 if ((uio->uio_resid == 0) &&
13400                                     ((user_marks_eor == 0) ||
13401                                     (srcv->sinfo_flags & SCTP_EOF) ||
13402                                     (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13403                                         sp->msg_is_complete = 1;
13404                                 } else {
13405                                         sp->msg_is_complete = 0;
13406                                 }
13407                                 SCTP_TCB_SEND_UNLOCK(stcb);
13408                         }
13409                         if (uio->uio_resid == 0) {
13410                                 /* got it all? */
13411                                 continue;
13412                         }
13413                         /* PR-SCTP? */
13414                         if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13415                                 /*
13416                                  * This is ugly but we must assure locking
13417                                  * order
13418                                  */
13419                                 if (hold_tcblock == 0) {
13420                                         SCTP_TCB_LOCK(stcb);
13421                                         hold_tcblock = 1;
13422                                 }
13423                                 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13424                                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13425                                 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13426                                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13427                                 else
13428                                         max_len = 0;
13429                                 if (max_len > 0) {
13430                                         continue;
13431                                 }
13432                                 SCTP_TCB_UNLOCK(stcb);
13433                                 hold_tcblock = 0;
13434                         }
13435                         /* wait for space now */
13436                         if (non_blocking) {
13437                                 /* Non-blocking io in place out */
13438                                 goto skip_out_eof;
13439                         }
13440                         /* What about the INIT, send it maybe */
13441                         if (queue_only_for_init) {
13442                                 if (hold_tcblock == 0) {
13443                                         SCTP_TCB_LOCK(stcb);
13444                                         hold_tcblock = 1;
13445                                 }
13446                                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13447                                         /* a collision took us forward? */
13448                                         queue_only = 0;
13449                                 } else {
13450                                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13451                                         SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13452                                         queue_only = 1;
13453                                 }
13454                         }
13455                         if ((net->flight_size > net->cwnd) &&
13456                             (asoc->sctp_cmt_on_off == 0)) {
13457                                 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13458                                 queue_only = 1;
13459                         } else if (asoc->ifp_had_enobuf) {
13460                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
13461                                 if (net->flight_size > (2 * net->mtu)) {
13462                                         queue_only = 1;
13463                                 }
13464                                 asoc->ifp_had_enobuf = 0;
13465                         }
13466                         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13467                             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13468                         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13469                             (stcb->asoc.total_flight > 0) &&
13470                             (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13471                             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13472
13473                                 /*-
13474                                  * Ok, Nagle is set on and we have data outstanding.
13475                                  * Don't send anything and let SACKs drive out the
13476                                  * data unless wen have a "full" segment to send.
13477                                  */
13478                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13479                                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13480                                 }
13481                                 SCTP_STAT_INCR(sctps_naglequeued);
13482                                 nagle_applies = 1;
13483                         } else {
13484                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13485                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13486                                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13487                                 }
13488                                 SCTP_STAT_INCR(sctps_naglesent);
13489                                 nagle_applies = 0;
13490                         }
13491                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13492
13493                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13494                                     nagle_applies, un_sent);
13495                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13496                                     stcb->asoc.total_flight,
13497                                     stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13498                         }
13499                         if (queue_only_for_init)
13500                                 queue_only_for_init = 0;
13501                         if ((queue_only == 0) && (nagle_applies == 0)) {
13502                                 /*-
13503                                  * need to start chunk output
13504                                  * before blocking.. note that if
13505                                  * a lock is already applied, then
13506                                  * the input via the net is happening
13507                                  * and I don't need to start output :-D
13508                                  */
13509                                 if (hold_tcblock == 0) {
13510                                         if (SCTP_TCB_TRYLOCK(stcb)) {
13511                                                 hold_tcblock = 1;
13512                                                 sctp_chunk_output(inp,
13513                                                     stcb,
13514                                                     SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13515                                         }
13516                                 } else {
13517                                         sctp_chunk_output(inp,
13518                                             stcb,
13519                                             SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13520                                 }
13521                                 if (hold_tcblock == 1) {
13522                                         SCTP_TCB_UNLOCK(stcb);
13523                                         hold_tcblock = 0;
13524                                 }
13525                         }
13526                         SOCKBUF_LOCK(&so->so_snd);
13527                         /*-
13528                          * This is a bit strange, but I think it will
13529                          * work. The total_output_queue_size is locked and
13530                          * protected by the TCB_LOCK, which we just released.
13531                          * There is a race that can occur between releasing it
13532                          * above, and me getting the socket lock, where sacks
13533                          * come in but we have not put the SB_WAIT on the
13534                          * so_snd buffer to get the wakeup. After the LOCK
13535                          * is applied the sack_processing will also need to
13536                          * LOCK the so->so_snd to do the actual sowwakeup(). So
13537                          * once we have the socket buffer lock if we recheck the
13538                          * size we KNOW we will get to sleep safely with the
13539                          * wakeup flag in place.
13540                          */
13541                         if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13542                             min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13543                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13544                                         sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13545                                             asoc, uio->uio_resid);
13546                                 }
13547                                 be.error = 0;
13548                                 stcb->block_entry = &be;
13549                                 error = sbwait(&so->so_snd);
13550                                 stcb->block_entry = NULL;
13551
13552                                 if (error || so->so_error || be.error) {
13553                                         if (error == 0) {
13554                                                 if (so->so_error)
13555                                                         error = so->so_error;
13556                                                 if (be.error) {
13557                                                         error = be.error;
13558                                                 }
13559                                         }
13560                                         SOCKBUF_UNLOCK(&so->so_snd);
13561                                         goto out_unlocked;
13562                                 }
13563                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13564                                         sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13565                                             asoc, stcb->asoc.total_output_queue_size);
13566                                 }
13567                         }
13568                         SOCKBUF_UNLOCK(&so->so_snd);
13569                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13570                                 goto out_unlocked;
13571                         }
13572                 }
13573                 SCTP_TCB_SEND_LOCK(stcb);
13574                 if (sp) {
13575                         if (sp->msg_is_complete == 0) {
13576                                 strm->last_msg_incomplete = 1;
13577                                 asoc->stream_locked = 1;
13578                                 asoc->stream_locked_on = srcv->sinfo_stream;
13579                         } else {
13580                                 sp->sender_all_done = 1;
13581                                 strm->last_msg_incomplete = 0;
13582                                 asoc->stream_locked = 0;
13583                         }
13584                 } else {
13585                         SCTP_PRINTF("Huh no sp TSNH?\n");
13586                         strm->last_msg_incomplete = 0;
13587                         asoc->stream_locked = 0;
13588                 }
13589                 SCTP_TCB_SEND_UNLOCK(stcb);
13590                 if (uio->uio_resid == 0) {
13591                         got_all_of_the_send = 1;
13592                 }
13593         } else {
13594                 /* We send in a 0, since we do NOT have any locks */
13595                 error = sctp_msg_append(stcb, net, top, srcv, 0);
13596                 top = NULL;
13597                 if (srcv->sinfo_flags & SCTP_EOF) {
13598                         /*
13599                          * This should only happen for Panda for the mbuf
13600                          * send case, which does NOT yet support EEOR mode.
13601                          * Thus, we can just set this flag to do the proper
13602                          * EOF handling.
13603                          */
13604                         got_all_of_the_send = 1;
13605                 }
13606         }
13607         if (error) {
13608                 goto out;
13609         }
13610 dataless_eof:
13611         /* EOF thing ? */
13612         if ((srcv->sinfo_flags & SCTP_EOF) &&
13613             (got_all_of_the_send == 1) &&
13614             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13615                 int cnt;
13616
13617                 SCTP_STAT_INCR(sctps_sends_with_eof);
13618                 error = 0;
13619                 if (hold_tcblock == 0) {
13620                         SCTP_TCB_LOCK(stcb);
13621                         hold_tcblock = 1;
13622                 }
13623                 cnt = sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED);
13624                 if (TAILQ_EMPTY(&asoc->send_queue) &&
13625                     TAILQ_EMPTY(&asoc->sent_queue) &&
13626                     (cnt == 0)) {
13627                         if (asoc->locked_on_sending) {
13628                                 goto abort_anyway;
13629                         }
13630                         /* there is nothing queued to send, so I'm done... */
13631                         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13632                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13633                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13634                                 struct sctp_nets *netp;
13635
13636                                 if (stcb->asoc.alternate) {
13637                                         netp = stcb->asoc.alternate;
13638                                 } else {
13639                                         netp = stcb->asoc.primary_destination;
13640                                 }
13641                                 /* only send SHUTDOWN the first time through */
13642                                 sctp_send_shutdown(stcb, netp);
13643                                 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13644                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13645                                 }
13646                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13647                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13648                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13649                                     netp);
13650                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13651                                     asoc->primary_destination);
13652                         }
13653                 } else {
13654                         /*-
13655                          * we still got (or just got) data to send, so set
13656                          * SHUTDOWN_PENDING
13657                          */
13658                         /*-
13659                          * XXX sockets draft says that SCTP_EOF should be
13660                          * sent with no data.  currently, we will allow user
13661                          * data to be sent first and move to
13662                          * SHUTDOWN-PENDING
13663                          */
13664                         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13665                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13666                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13667                                 if (hold_tcblock == 0) {
13668                                         SCTP_TCB_LOCK(stcb);
13669                                         hold_tcblock = 1;
13670                                 }
13671                                 if (asoc->locked_on_sending) {
13672                                         /* Locked to send out the data */
13673                                         struct sctp_stream_queue_pending *sp;
13674
13675                                         sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13676                                         if (sp) {
13677                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
13678                                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13679                                         }
13680                                 }
13681                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13682                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
13683                                     TAILQ_EMPTY(&asoc->sent_queue) &&
13684                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13685                         abort_anyway:
13686                                         if (free_cnt_applied) {
13687                                                 atomic_add_int(&stcb->asoc.refcnt, -1);
13688                                                 free_cnt_applied = 0;
13689                                         }
13690                                         sctp_abort_an_association(stcb->sctp_ep, stcb,
13691                                             NULL, SCTP_SO_LOCKED);
13692                                         /*
13693                                          * now relock the stcb so everything
13694                                          * is sane
13695                                          */
13696                                         hold_tcblock = 0;
13697                                         stcb = NULL;
13698                                         goto out;
13699                                 }
13700                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13701                                     asoc->primary_destination);
13702                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13703                         }
13704                 }
13705         }
13706 skip_out_eof:
13707         if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13708                 some_on_control = 1;
13709         }
13710         if (queue_only_for_init) {
13711                 if (hold_tcblock == 0) {
13712                         SCTP_TCB_LOCK(stcb);
13713                         hold_tcblock = 1;
13714                 }
13715                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13716                         /* a collision took us forward? */
13717                         queue_only = 0;
13718                 } else {
13719                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13720                         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13721                         queue_only = 1;
13722                 }
13723         }
13724         if ((net->flight_size > net->cwnd) &&
13725             (stcb->asoc.sctp_cmt_on_off == 0)) {
13726                 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13727                 queue_only = 1;
13728         } else if (asoc->ifp_had_enobuf) {
13729                 SCTP_STAT_INCR(sctps_ifnomemqueued);
13730                 if (net->flight_size > (2 * net->mtu)) {
13731                         queue_only = 1;
13732                 }
13733                 asoc->ifp_had_enobuf = 0;
13734         }
13735         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13736             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13737         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13738             (stcb->asoc.total_flight > 0) &&
13739             (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13740             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13741                 /*-
13742                  * Ok, Nagle is set on and we have data outstanding.
13743                  * Don't send anything and let SACKs drive out the
13744                  * data unless wen have a "full" segment to send.
13745                  */
13746                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13747                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13748                 }
13749                 SCTP_STAT_INCR(sctps_naglequeued);
13750                 nagle_applies = 1;
13751         } else {
13752                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13753                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13754                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13755                 }
13756                 SCTP_STAT_INCR(sctps_naglesent);
13757                 nagle_applies = 0;
13758         }
13759         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13760                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13761                     nagle_applies, un_sent);
13762                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13763                     stcb->asoc.total_flight,
13764                     stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13765         }
13766         if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13767                 /* we can attempt to send too. */
13768                 if (hold_tcblock == 0) {
13769                         /*
13770                          * If there is activity recv'ing sacks no need to
13771                          * send
13772                          */
13773                         if (SCTP_TCB_TRYLOCK(stcb)) {
13774                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13775                                 hold_tcblock = 1;
13776                         }
13777                 } else {
13778                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13779                 }
13780         } else if ((queue_only == 0) &&
13781                     (stcb->asoc.peers_rwnd == 0) &&
13782             (stcb->asoc.total_flight == 0)) {
13783                 /* We get to have a probe outstanding */
13784                 if (hold_tcblock == 0) {
13785                         hold_tcblock = 1;
13786                         SCTP_TCB_LOCK(stcb);
13787                 }
13788                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13789         } else if (some_on_control) {
13790                 int num_out, reason, frag_point;
13791
13792                 /* Here we do control only */
13793                 if (hold_tcblock == 0) {
13794                         hold_tcblock = 1;
13795                         SCTP_TCB_LOCK(stcb);
13796                 }
13797                 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13798                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13799                     &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13800         }
13801         SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13802             queue_only, stcb->asoc.peers_rwnd, un_sent,
13803             stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13804             stcb->asoc.total_output_queue_size, error);
13805
13806 out:
13807 out_unlocked:
13808
13809         if (local_soresv && stcb) {
13810                 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13811         }
13812         if (create_lock_applied) {
13813                 SCTP_ASOC_CREATE_UNLOCK(inp);
13814         }
13815         if ((stcb) && hold_tcblock) {
13816                 SCTP_TCB_UNLOCK(stcb);
13817         }
13818         if (stcb && free_cnt_applied) {
13819                 atomic_add_int(&stcb->asoc.refcnt, -1);
13820         }
13821 #ifdef INVARIANTS
13822         if (stcb) {
13823                 if (mtx_owned(&stcb->tcb_mtx)) {
13824                         panic("Leaving with tcb mtx owned?");
13825                 }
13826                 if (mtx_owned(&stcb->tcb_send_mtx)) {
13827                         panic("Leaving with tcb send mtx owned?");
13828                 }
13829         }
13830 #endif
13831 #ifdef INVARIANTS
13832         if (inp) {
13833                 sctp_validate_no_locks(inp);
13834         } else {
13835                 SCTP_PRINTF("Warning - inp is NULL so cant validate locks\n");
13836         }
13837 #endif
13838         if (top) {
13839                 sctp_m_freem(top);
13840         }
13841         if (control) {
13842                 sctp_m_freem(control);
13843         }
13844         return (error);
13845 }
13846
13847
13848 /*
13849  * generate an AUTHentication chunk, if required
13850  */
13851 struct mbuf *
13852 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13853     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13854     struct sctp_tcb *stcb, uint8_t chunk)
13855 {
13856         struct mbuf *m_auth;
13857         struct sctp_auth_chunk *auth;
13858         int chunk_len;
13859         struct mbuf *cn;
13860
13861         if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13862             (stcb == NULL))
13863                 return (m);
13864
13865         /* sysctl disabled auth? */
13866         if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13867                 return (m);
13868
13869         /* peer doesn't do auth... */
13870         if (!stcb->asoc.peer_supports_auth) {
13871                 return (m);
13872         }
13873         /* does the requested chunk require auth? */
13874         if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13875                 return (m);
13876         }
13877         m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13878         if (m_auth == NULL) {
13879                 /* no mbuf's */
13880                 return (m);
13881         }
13882         /* reserve some space if this will be the first mbuf */
13883         if (m == NULL)
13884                 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13885         /* fill in the AUTH chunk details */
13886         auth = mtod(m_auth, struct sctp_auth_chunk *);
13887         bzero(auth, sizeof(*auth));
13888         auth->ch.chunk_type = SCTP_AUTHENTICATION;
13889         auth->ch.chunk_flags = 0;
13890         chunk_len = sizeof(*auth) +
13891             sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13892         auth->ch.chunk_length = htons(chunk_len);
13893         auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13894         /* key id and hmac digest will be computed and filled in upon send */
13895
13896         /* save the offset where the auth was inserted into the chain */
13897         *offset = 0;
13898         for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
13899                 *offset += SCTP_BUF_LEN(cn);
13900         }
13901
13902         /* update length and return pointer to the auth chunk */
13903         SCTP_BUF_LEN(m_auth) = chunk_len;
13904         m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13905         if (auth_ret != NULL)
13906                 *auth_ret = auth;
13907
13908         return (m);
13909 }
13910
13911 #ifdef INET6
13912 int
13913 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13914 {
13915         struct nd_prefix *pfx = NULL;
13916         struct nd_pfxrouter *pfxrtr = NULL;
13917         struct sockaddr_in6 gw6;
13918
13919         if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13920                 return (0);
13921
13922         /* get prefix entry of address */
13923         LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13924                 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13925                         continue;
13926                 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13927                     &src6->sin6_addr, &pfx->ndpr_mask))
13928                         break;
13929         }
13930         /* no prefix entry in the prefix list */
13931         if (pfx == NULL) {
13932                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13933                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13934                 return (0);
13935         }
13936         SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13937         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13938
13939         /* search installed gateway from prefix entry */
13940         LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) {
13941                 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13942                 gw6.sin6_family = AF_INET6;
13943                 gw6.sin6_len = sizeof(struct sockaddr_in6);
13944                 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13945                     sizeof(struct in6_addr));
13946                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13947                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13948                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13949                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13950                 if (sctp_cmpaddr((struct sockaddr *)&gw6,
13951                     ro->ro_rt->rt_gateway)) {
13952                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13953                         return (1);
13954                 }
13955         }
13956         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13957         return (0);
13958 }
13959
13960 #endif
13961
13962 int
13963 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13964 {
13965 #ifdef INET
13966         struct sockaddr_in *sin, *mask;
13967         struct ifaddr *ifa;
13968         struct in_addr srcnetaddr, gwnetaddr;
13969
13970         if (ro == NULL || ro->ro_rt == NULL ||
13971             sifa->address.sa.sa_family != AF_INET) {
13972                 return (0);
13973         }
13974         ifa = (struct ifaddr *)sifa->ifa;
13975         mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13976         sin = (struct sockaddr_in *)&sifa->address.sin;
13977         srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13978         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13979         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13980         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13981
13982         sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13983         gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13984         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13985         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13986         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13987         if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13988                 return (1);
13989         }
13990 #endif
13991         return (0);
13992 }