]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/netinet/sctp_output.c
MFC 211944:
[FreeBSD/stable/8.git] / sys / netinet / sctp_output.c
1 /*-
2  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * a) Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  *
10  * b) Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *   the documentation and/or other materials provided with the distribution.
13  *
14  * c) Neither the name of Cisco Systems, Inc. nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <netinet/sctp_os.h>
37 #include <sys/proc.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_uio.h>
45 #include <netinet/sctputil.h>
46 #include <netinet/sctp_auth.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_indata.h>
50 #include <netinet/sctp_bsd_addr.h>
51 #include <netinet/sctp_input.h>
52 #include <netinet/sctp_crc32.h>
53 #include <netinet/udp.h>
54 #include <machine/in_cksum.h>
55
56
57
58 #define SCTP_MAX_GAPS_INARRAY 4
59 struct sack_track {
60         uint8_t right_edge;     /* mergable on the right edge */
61         uint8_t left_edge;      /* mergable on the left edge */
62         uint8_t num_entries;
63         uint8_t spare;
64         struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
65 };
66
67 struct sack_track sack_array[256] = {
68         {0, 0, 0, 0,            /* 0x00 */
69                 {{0, 0},
70                 {0, 0},
71                 {0, 0},
72                 {0, 0}
73                 }
74         },
75         {1, 0, 1, 0,            /* 0x01 */
76                 {{0, 0},
77                 {0, 0},
78                 {0, 0},
79                 {0, 0}
80                 }
81         },
82         {0, 0, 1, 0,            /* 0x02 */
83                 {{1, 1},
84                 {0, 0},
85                 {0, 0},
86                 {0, 0}
87                 }
88         },
89         {1, 0, 1, 0,            /* 0x03 */
90                 {{0, 1},
91                 {0, 0},
92                 {0, 0},
93                 {0, 0}
94                 }
95         },
96         {0, 0, 1, 0,            /* 0x04 */
97                 {{2, 2},
98                 {0, 0},
99                 {0, 0},
100                 {0, 0}
101                 }
102         },
103         {1, 0, 2, 0,            /* 0x05 */
104                 {{0, 0},
105                 {2, 2},
106                 {0, 0},
107                 {0, 0}
108                 }
109         },
110         {0, 0, 1, 0,            /* 0x06 */
111                 {{1, 2},
112                 {0, 0},
113                 {0, 0},
114                 {0, 0}
115                 }
116         },
117         {1, 0, 1, 0,            /* 0x07 */
118                 {{0, 2},
119                 {0, 0},
120                 {0, 0},
121                 {0, 0}
122                 }
123         },
124         {0, 0, 1, 0,            /* 0x08 */
125                 {{3, 3},
126                 {0, 0},
127                 {0, 0},
128                 {0, 0}
129                 }
130         },
131         {1, 0, 2, 0,            /* 0x09 */
132                 {{0, 0},
133                 {3, 3},
134                 {0, 0},
135                 {0, 0}
136                 }
137         },
138         {0, 0, 2, 0,            /* 0x0a */
139                 {{1, 1},
140                 {3, 3},
141                 {0, 0},
142                 {0, 0}
143                 }
144         },
145         {1, 0, 2, 0,            /* 0x0b */
146                 {{0, 1},
147                 {3, 3},
148                 {0, 0},
149                 {0, 0}
150                 }
151         },
152         {0, 0, 1, 0,            /* 0x0c */
153                 {{2, 3},
154                 {0, 0},
155                 {0, 0},
156                 {0, 0}
157                 }
158         },
159         {1, 0, 2, 0,            /* 0x0d */
160                 {{0, 0},
161                 {2, 3},
162                 {0, 0},
163                 {0, 0}
164                 }
165         },
166         {0, 0, 1, 0,            /* 0x0e */
167                 {{1, 3},
168                 {0, 0},
169                 {0, 0},
170                 {0, 0}
171                 }
172         },
173         {1, 0, 1, 0,            /* 0x0f */
174                 {{0, 3},
175                 {0, 0},
176                 {0, 0},
177                 {0, 0}
178                 }
179         },
180         {0, 0, 1, 0,            /* 0x10 */
181                 {{4, 4},
182                 {0, 0},
183                 {0, 0},
184                 {0, 0}
185                 }
186         },
187         {1, 0, 2, 0,            /* 0x11 */
188                 {{0, 0},
189                 {4, 4},
190                 {0, 0},
191                 {0, 0}
192                 }
193         },
194         {0, 0, 2, 0,            /* 0x12 */
195                 {{1, 1},
196                 {4, 4},
197                 {0, 0},
198                 {0, 0}
199                 }
200         },
201         {1, 0, 2, 0,            /* 0x13 */
202                 {{0, 1},
203                 {4, 4},
204                 {0, 0},
205                 {0, 0}
206                 }
207         },
208         {0, 0, 2, 0,            /* 0x14 */
209                 {{2, 2},
210                 {4, 4},
211                 {0, 0},
212                 {0, 0}
213                 }
214         },
215         {1, 0, 3, 0,            /* 0x15 */
216                 {{0, 0},
217                 {2, 2},
218                 {4, 4},
219                 {0, 0}
220                 }
221         },
222         {0, 0, 2, 0,            /* 0x16 */
223                 {{1, 2},
224                 {4, 4},
225                 {0, 0},
226                 {0, 0}
227                 }
228         },
229         {1, 0, 2, 0,            /* 0x17 */
230                 {{0, 2},
231                 {4, 4},
232                 {0, 0},
233                 {0, 0}
234                 }
235         },
236         {0, 0, 1, 0,            /* 0x18 */
237                 {{3, 4},
238                 {0, 0},
239                 {0, 0},
240                 {0, 0}
241                 }
242         },
243         {1, 0, 2, 0,            /* 0x19 */
244                 {{0, 0},
245                 {3, 4},
246                 {0, 0},
247                 {0, 0}
248                 }
249         },
250         {0, 0, 2, 0,            /* 0x1a */
251                 {{1, 1},
252                 {3, 4},
253                 {0, 0},
254                 {0, 0}
255                 }
256         },
257         {1, 0, 2, 0,            /* 0x1b */
258                 {{0, 1},
259                 {3, 4},
260                 {0, 0},
261                 {0, 0}
262                 }
263         },
264         {0, 0, 1, 0,            /* 0x1c */
265                 {{2, 4},
266                 {0, 0},
267                 {0, 0},
268                 {0, 0}
269                 }
270         },
271         {1, 0, 2, 0,            /* 0x1d */
272                 {{0, 0},
273                 {2, 4},
274                 {0, 0},
275                 {0, 0}
276                 }
277         },
278         {0, 0, 1, 0,            /* 0x1e */
279                 {{1, 4},
280                 {0, 0},
281                 {0, 0},
282                 {0, 0}
283                 }
284         },
285         {1, 0, 1, 0,            /* 0x1f */
286                 {{0, 4},
287                 {0, 0},
288                 {0, 0},
289                 {0, 0}
290                 }
291         },
292         {0, 0, 1, 0,            /* 0x20 */
293                 {{5, 5},
294                 {0, 0},
295                 {0, 0},
296                 {0, 0}
297                 }
298         },
299         {1, 0, 2, 0,            /* 0x21 */
300                 {{0, 0},
301                 {5, 5},
302                 {0, 0},
303                 {0, 0}
304                 }
305         },
306         {0, 0, 2, 0,            /* 0x22 */
307                 {{1, 1},
308                 {5, 5},
309                 {0, 0},
310                 {0, 0}
311                 }
312         },
313         {1, 0, 2, 0,            /* 0x23 */
314                 {{0, 1},
315                 {5, 5},
316                 {0, 0},
317                 {0, 0}
318                 }
319         },
320         {0, 0, 2, 0,            /* 0x24 */
321                 {{2, 2},
322                 {5, 5},
323                 {0, 0},
324                 {0, 0}
325                 }
326         },
327         {1, 0, 3, 0,            /* 0x25 */
328                 {{0, 0},
329                 {2, 2},
330                 {5, 5},
331                 {0, 0}
332                 }
333         },
334         {0, 0, 2, 0,            /* 0x26 */
335                 {{1, 2},
336                 {5, 5},
337                 {0, 0},
338                 {0, 0}
339                 }
340         },
341         {1, 0, 2, 0,            /* 0x27 */
342                 {{0, 2},
343                 {5, 5},
344                 {0, 0},
345                 {0, 0}
346                 }
347         },
348         {0, 0, 2, 0,            /* 0x28 */
349                 {{3, 3},
350                 {5, 5},
351                 {0, 0},
352                 {0, 0}
353                 }
354         },
355         {1, 0, 3, 0,            /* 0x29 */
356                 {{0, 0},
357                 {3, 3},
358                 {5, 5},
359                 {0, 0}
360                 }
361         },
362         {0, 0, 3, 0,            /* 0x2a */
363                 {{1, 1},
364                 {3, 3},
365                 {5, 5},
366                 {0, 0}
367                 }
368         },
369         {1, 0, 3, 0,            /* 0x2b */
370                 {{0, 1},
371                 {3, 3},
372                 {5, 5},
373                 {0, 0}
374                 }
375         },
376         {0, 0, 2, 0,            /* 0x2c */
377                 {{2, 3},
378                 {5, 5},
379                 {0, 0},
380                 {0, 0}
381                 }
382         },
383         {1, 0, 3, 0,            /* 0x2d */
384                 {{0, 0},
385                 {2, 3},
386                 {5, 5},
387                 {0, 0}
388                 }
389         },
390         {0, 0, 2, 0,            /* 0x2e */
391                 {{1, 3},
392                 {5, 5},
393                 {0, 0},
394                 {0, 0}
395                 }
396         },
397         {1, 0, 2, 0,            /* 0x2f */
398                 {{0, 3},
399                 {5, 5},
400                 {0, 0},
401                 {0, 0}
402                 }
403         },
404         {0, 0, 1, 0,            /* 0x30 */
405                 {{4, 5},
406                 {0, 0},
407                 {0, 0},
408                 {0, 0}
409                 }
410         },
411         {1, 0, 2, 0,            /* 0x31 */
412                 {{0, 0},
413                 {4, 5},
414                 {0, 0},
415                 {0, 0}
416                 }
417         },
418         {0, 0, 2, 0,            /* 0x32 */
419                 {{1, 1},
420                 {4, 5},
421                 {0, 0},
422                 {0, 0}
423                 }
424         },
425         {1, 0, 2, 0,            /* 0x33 */
426                 {{0, 1},
427                 {4, 5},
428                 {0, 0},
429                 {0, 0}
430                 }
431         },
432         {0, 0, 2, 0,            /* 0x34 */
433                 {{2, 2},
434                 {4, 5},
435                 {0, 0},
436                 {0, 0}
437                 }
438         },
439         {1, 0, 3, 0,            /* 0x35 */
440                 {{0, 0},
441                 {2, 2},
442                 {4, 5},
443                 {0, 0}
444                 }
445         },
446         {0, 0, 2, 0,            /* 0x36 */
447                 {{1, 2},
448                 {4, 5},
449                 {0, 0},
450                 {0, 0}
451                 }
452         },
453         {1, 0, 2, 0,            /* 0x37 */
454                 {{0, 2},
455                 {4, 5},
456                 {0, 0},
457                 {0, 0}
458                 }
459         },
460         {0, 0, 1, 0,            /* 0x38 */
461                 {{3, 5},
462                 {0, 0},
463                 {0, 0},
464                 {0, 0}
465                 }
466         },
467         {1, 0, 2, 0,            /* 0x39 */
468                 {{0, 0},
469                 {3, 5},
470                 {0, 0},
471                 {0, 0}
472                 }
473         },
474         {0, 0, 2, 0,            /* 0x3a */
475                 {{1, 1},
476                 {3, 5},
477                 {0, 0},
478                 {0, 0}
479                 }
480         },
481         {1, 0, 2, 0,            /* 0x3b */
482                 {{0, 1},
483                 {3, 5},
484                 {0, 0},
485                 {0, 0}
486                 }
487         },
488         {0, 0, 1, 0,            /* 0x3c */
489                 {{2, 5},
490                 {0, 0},
491                 {0, 0},
492                 {0, 0}
493                 }
494         },
495         {1, 0, 2, 0,            /* 0x3d */
496                 {{0, 0},
497                 {2, 5},
498                 {0, 0},
499                 {0, 0}
500                 }
501         },
502         {0, 0, 1, 0,            /* 0x3e */
503                 {{1, 5},
504                 {0, 0},
505                 {0, 0},
506                 {0, 0}
507                 }
508         },
509         {1, 0, 1, 0,            /* 0x3f */
510                 {{0, 5},
511                 {0, 0},
512                 {0, 0},
513                 {0, 0}
514                 }
515         },
516         {0, 0, 1, 0,            /* 0x40 */
517                 {{6, 6},
518                 {0, 0},
519                 {0, 0},
520                 {0, 0}
521                 }
522         },
523         {1, 0, 2, 0,            /* 0x41 */
524                 {{0, 0},
525                 {6, 6},
526                 {0, 0},
527                 {0, 0}
528                 }
529         },
530         {0, 0, 2, 0,            /* 0x42 */
531                 {{1, 1},
532                 {6, 6},
533                 {0, 0},
534                 {0, 0}
535                 }
536         },
537         {1, 0, 2, 0,            /* 0x43 */
538                 {{0, 1},
539                 {6, 6},
540                 {0, 0},
541                 {0, 0}
542                 }
543         },
544         {0, 0, 2, 0,            /* 0x44 */
545                 {{2, 2},
546                 {6, 6},
547                 {0, 0},
548                 {0, 0}
549                 }
550         },
551         {1, 0, 3, 0,            /* 0x45 */
552                 {{0, 0},
553                 {2, 2},
554                 {6, 6},
555                 {0, 0}
556                 }
557         },
558         {0, 0, 2, 0,            /* 0x46 */
559                 {{1, 2},
560                 {6, 6},
561                 {0, 0},
562                 {0, 0}
563                 }
564         },
565         {1, 0, 2, 0,            /* 0x47 */
566                 {{0, 2},
567                 {6, 6},
568                 {0, 0},
569                 {0, 0}
570                 }
571         },
572         {0, 0, 2, 0,            /* 0x48 */
573                 {{3, 3},
574                 {6, 6},
575                 {0, 0},
576                 {0, 0}
577                 }
578         },
579         {1, 0, 3, 0,            /* 0x49 */
580                 {{0, 0},
581                 {3, 3},
582                 {6, 6},
583                 {0, 0}
584                 }
585         },
586         {0, 0, 3, 0,            /* 0x4a */
587                 {{1, 1},
588                 {3, 3},
589                 {6, 6},
590                 {0, 0}
591                 }
592         },
593         {1, 0, 3, 0,            /* 0x4b */
594                 {{0, 1},
595                 {3, 3},
596                 {6, 6},
597                 {0, 0}
598                 }
599         },
600         {0, 0, 2, 0,            /* 0x4c */
601                 {{2, 3},
602                 {6, 6},
603                 {0, 0},
604                 {0, 0}
605                 }
606         },
607         {1, 0, 3, 0,            /* 0x4d */
608                 {{0, 0},
609                 {2, 3},
610                 {6, 6},
611                 {0, 0}
612                 }
613         },
614         {0, 0, 2, 0,            /* 0x4e */
615                 {{1, 3},
616                 {6, 6},
617                 {0, 0},
618                 {0, 0}
619                 }
620         },
621         {1, 0, 2, 0,            /* 0x4f */
622                 {{0, 3},
623                 {6, 6},
624                 {0, 0},
625                 {0, 0}
626                 }
627         },
628         {0, 0, 2, 0,            /* 0x50 */
629                 {{4, 4},
630                 {6, 6},
631                 {0, 0},
632                 {0, 0}
633                 }
634         },
635         {1, 0, 3, 0,            /* 0x51 */
636                 {{0, 0},
637                 {4, 4},
638                 {6, 6},
639                 {0, 0}
640                 }
641         },
642         {0, 0, 3, 0,            /* 0x52 */
643                 {{1, 1},
644                 {4, 4},
645                 {6, 6},
646                 {0, 0}
647                 }
648         },
649         {1, 0, 3, 0,            /* 0x53 */
650                 {{0, 1},
651                 {4, 4},
652                 {6, 6},
653                 {0, 0}
654                 }
655         },
656         {0, 0, 3, 0,            /* 0x54 */
657                 {{2, 2},
658                 {4, 4},
659                 {6, 6},
660                 {0, 0}
661                 }
662         },
663         {1, 0, 4, 0,            /* 0x55 */
664                 {{0, 0},
665                 {2, 2},
666                 {4, 4},
667                 {6, 6}
668                 }
669         },
670         {0, 0, 3, 0,            /* 0x56 */
671                 {{1, 2},
672                 {4, 4},
673                 {6, 6},
674                 {0, 0}
675                 }
676         },
677         {1, 0, 3, 0,            /* 0x57 */
678                 {{0, 2},
679                 {4, 4},
680                 {6, 6},
681                 {0, 0}
682                 }
683         },
684         {0, 0, 2, 0,            /* 0x58 */
685                 {{3, 4},
686                 {6, 6},
687                 {0, 0},
688                 {0, 0}
689                 }
690         },
691         {1, 0, 3, 0,            /* 0x59 */
692                 {{0, 0},
693                 {3, 4},
694                 {6, 6},
695                 {0, 0}
696                 }
697         },
698         {0, 0, 3, 0,            /* 0x5a */
699                 {{1, 1},
700                 {3, 4},
701                 {6, 6},
702                 {0, 0}
703                 }
704         },
705         {1, 0, 3, 0,            /* 0x5b */
706                 {{0, 1},
707                 {3, 4},
708                 {6, 6},
709                 {0, 0}
710                 }
711         },
712         {0, 0, 2, 0,            /* 0x5c */
713                 {{2, 4},
714                 {6, 6},
715                 {0, 0},
716                 {0, 0}
717                 }
718         },
719         {1, 0, 3, 0,            /* 0x5d */
720                 {{0, 0},
721                 {2, 4},
722                 {6, 6},
723                 {0, 0}
724                 }
725         },
726         {0, 0, 2, 0,            /* 0x5e */
727                 {{1, 4},
728                 {6, 6},
729                 {0, 0},
730                 {0, 0}
731                 }
732         },
733         {1, 0, 2, 0,            /* 0x5f */
734                 {{0, 4},
735                 {6, 6},
736                 {0, 0},
737                 {0, 0}
738                 }
739         },
740         {0, 0, 1, 0,            /* 0x60 */
741                 {{5, 6},
742                 {0, 0},
743                 {0, 0},
744                 {0, 0}
745                 }
746         },
747         {1, 0, 2, 0,            /* 0x61 */
748                 {{0, 0},
749                 {5, 6},
750                 {0, 0},
751                 {0, 0}
752                 }
753         },
754         {0, 0, 2, 0,            /* 0x62 */
755                 {{1, 1},
756                 {5, 6},
757                 {0, 0},
758                 {0, 0}
759                 }
760         },
761         {1, 0, 2, 0,            /* 0x63 */
762                 {{0, 1},
763                 {5, 6},
764                 {0, 0},
765                 {0, 0}
766                 }
767         },
768         {0, 0, 2, 0,            /* 0x64 */
769                 {{2, 2},
770                 {5, 6},
771                 {0, 0},
772                 {0, 0}
773                 }
774         },
775         {1, 0, 3, 0,            /* 0x65 */
776                 {{0, 0},
777                 {2, 2},
778                 {5, 6},
779                 {0, 0}
780                 }
781         },
782         {0, 0, 2, 0,            /* 0x66 */
783                 {{1, 2},
784                 {5, 6},
785                 {0, 0},
786                 {0, 0}
787                 }
788         },
789         {1, 0, 2, 0,            /* 0x67 */
790                 {{0, 2},
791                 {5, 6},
792                 {0, 0},
793                 {0, 0}
794                 }
795         },
796         {0, 0, 2, 0,            /* 0x68 */
797                 {{3, 3},
798                 {5, 6},
799                 {0, 0},
800                 {0, 0}
801                 }
802         },
803         {1, 0, 3, 0,            /* 0x69 */
804                 {{0, 0},
805                 {3, 3},
806                 {5, 6},
807                 {0, 0}
808                 }
809         },
810         {0, 0, 3, 0,            /* 0x6a */
811                 {{1, 1},
812                 {3, 3},
813                 {5, 6},
814                 {0, 0}
815                 }
816         },
817         {1, 0, 3, 0,            /* 0x6b */
818                 {{0, 1},
819                 {3, 3},
820                 {5, 6},
821                 {0, 0}
822                 }
823         },
824         {0, 0, 2, 0,            /* 0x6c */
825                 {{2, 3},
826                 {5, 6},
827                 {0, 0},
828                 {0, 0}
829                 }
830         },
831         {1, 0, 3, 0,            /* 0x6d */
832                 {{0, 0},
833                 {2, 3},
834                 {5, 6},
835                 {0, 0}
836                 }
837         },
838         {0, 0, 2, 0,            /* 0x6e */
839                 {{1, 3},
840                 {5, 6},
841                 {0, 0},
842                 {0, 0}
843                 }
844         },
845         {1, 0, 2, 0,            /* 0x6f */
846                 {{0, 3},
847                 {5, 6},
848                 {0, 0},
849                 {0, 0}
850                 }
851         },
852         {0, 0, 1, 0,            /* 0x70 */
853                 {{4, 6},
854                 {0, 0},
855                 {0, 0},
856                 {0, 0}
857                 }
858         },
859         {1, 0, 2, 0,            /* 0x71 */
860                 {{0, 0},
861                 {4, 6},
862                 {0, 0},
863                 {0, 0}
864                 }
865         },
866         {0, 0, 2, 0,            /* 0x72 */
867                 {{1, 1},
868                 {4, 6},
869                 {0, 0},
870                 {0, 0}
871                 }
872         },
873         {1, 0, 2, 0,            /* 0x73 */
874                 {{0, 1},
875                 {4, 6},
876                 {0, 0},
877                 {0, 0}
878                 }
879         },
880         {0, 0, 2, 0,            /* 0x74 */
881                 {{2, 2},
882                 {4, 6},
883                 {0, 0},
884                 {0, 0}
885                 }
886         },
887         {1, 0, 3, 0,            /* 0x75 */
888                 {{0, 0},
889                 {2, 2},
890                 {4, 6},
891                 {0, 0}
892                 }
893         },
894         {0, 0, 2, 0,            /* 0x76 */
895                 {{1, 2},
896                 {4, 6},
897                 {0, 0},
898                 {0, 0}
899                 }
900         },
901         {1, 0, 2, 0,            /* 0x77 */
902                 {{0, 2},
903                 {4, 6},
904                 {0, 0},
905                 {0, 0}
906                 }
907         },
908         {0, 0, 1, 0,            /* 0x78 */
909                 {{3, 6},
910                 {0, 0},
911                 {0, 0},
912                 {0, 0}
913                 }
914         },
915         {1, 0, 2, 0,            /* 0x79 */
916                 {{0, 0},
917                 {3, 6},
918                 {0, 0},
919                 {0, 0}
920                 }
921         },
922         {0, 0, 2, 0,            /* 0x7a */
923                 {{1, 1},
924                 {3, 6},
925                 {0, 0},
926                 {0, 0}
927                 }
928         },
929         {1, 0, 2, 0,            /* 0x7b */
930                 {{0, 1},
931                 {3, 6},
932                 {0, 0},
933                 {0, 0}
934                 }
935         },
936         {0, 0, 1, 0,            /* 0x7c */
937                 {{2, 6},
938                 {0, 0},
939                 {0, 0},
940                 {0, 0}
941                 }
942         },
943         {1, 0, 2, 0,            /* 0x7d */
944                 {{0, 0},
945                 {2, 6},
946                 {0, 0},
947                 {0, 0}
948                 }
949         },
950         {0, 0, 1, 0,            /* 0x7e */
951                 {{1, 6},
952                 {0, 0},
953                 {0, 0},
954                 {0, 0}
955                 }
956         },
957         {1, 0, 1, 0,            /* 0x7f */
958                 {{0, 6},
959                 {0, 0},
960                 {0, 0},
961                 {0, 0}
962                 }
963         },
964         {0, 1, 1, 0,            /* 0x80 */
965                 {{7, 7},
966                 {0, 0},
967                 {0, 0},
968                 {0, 0}
969                 }
970         },
971         {1, 1, 2, 0,            /* 0x81 */
972                 {{0, 0},
973                 {7, 7},
974                 {0, 0},
975                 {0, 0}
976                 }
977         },
978         {0, 1, 2, 0,            /* 0x82 */
979                 {{1, 1},
980                 {7, 7},
981                 {0, 0},
982                 {0, 0}
983                 }
984         },
985         {1, 1, 2, 0,            /* 0x83 */
986                 {{0, 1},
987                 {7, 7},
988                 {0, 0},
989                 {0, 0}
990                 }
991         },
992         {0, 1, 2, 0,            /* 0x84 */
993                 {{2, 2},
994                 {7, 7},
995                 {0, 0},
996                 {0, 0}
997                 }
998         },
999         {1, 1, 3, 0,            /* 0x85 */
1000                 {{0, 0},
1001                 {2, 2},
1002                 {7, 7},
1003                 {0, 0}
1004                 }
1005         },
1006         {0, 1, 2, 0,            /* 0x86 */
1007                 {{1, 2},
1008                 {7, 7},
1009                 {0, 0},
1010                 {0, 0}
1011                 }
1012         },
1013         {1, 1, 2, 0,            /* 0x87 */
1014                 {{0, 2},
1015                 {7, 7},
1016                 {0, 0},
1017                 {0, 0}
1018                 }
1019         },
1020         {0, 1, 2, 0,            /* 0x88 */
1021                 {{3, 3},
1022                 {7, 7},
1023                 {0, 0},
1024                 {0, 0}
1025                 }
1026         },
1027         {1, 1, 3, 0,            /* 0x89 */
1028                 {{0, 0},
1029                 {3, 3},
1030                 {7, 7},
1031                 {0, 0}
1032                 }
1033         },
1034         {0, 1, 3, 0,            /* 0x8a */
1035                 {{1, 1},
1036                 {3, 3},
1037                 {7, 7},
1038                 {0, 0}
1039                 }
1040         },
1041         {1, 1, 3, 0,            /* 0x8b */
1042                 {{0, 1},
1043                 {3, 3},
1044                 {7, 7},
1045                 {0, 0}
1046                 }
1047         },
1048         {0, 1, 2, 0,            /* 0x8c */
1049                 {{2, 3},
1050                 {7, 7},
1051                 {0, 0},
1052                 {0, 0}
1053                 }
1054         },
1055         {1, 1, 3, 0,            /* 0x8d */
1056                 {{0, 0},
1057                 {2, 3},
1058                 {7, 7},
1059                 {0, 0}
1060                 }
1061         },
1062         {0, 1, 2, 0,            /* 0x8e */
1063                 {{1, 3},
1064                 {7, 7},
1065                 {0, 0},
1066                 {0, 0}
1067                 }
1068         },
1069         {1, 1, 2, 0,            /* 0x8f */
1070                 {{0, 3},
1071                 {7, 7},
1072                 {0, 0},
1073                 {0, 0}
1074                 }
1075         },
1076         {0, 1, 2, 0,            /* 0x90 */
1077                 {{4, 4},
1078                 {7, 7},
1079                 {0, 0},
1080                 {0, 0}
1081                 }
1082         },
1083         {1, 1, 3, 0,            /* 0x91 */
1084                 {{0, 0},
1085                 {4, 4},
1086                 {7, 7},
1087                 {0, 0}
1088                 }
1089         },
1090         {0, 1, 3, 0,            /* 0x92 */
1091                 {{1, 1},
1092                 {4, 4},
1093                 {7, 7},
1094                 {0, 0}
1095                 }
1096         },
1097         {1, 1, 3, 0,            /* 0x93 */
1098                 {{0, 1},
1099                 {4, 4},
1100                 {7, 7},
1101                 {0, 0}
1102                 }
1103         },
1104         {0, 1, 3, 0,            /* 0x94 */
1105                 {{2, 2},
1106                 {4, 4},
1107                 {7, 7},
1108                 {0, 0}
1109                 }
1110         },
1111         {1, 1, 4, 0,            /* 0x95 */
1112                 {{0, 0},
1113                 {2, 2},
1114                 {4, 4},
1115                 {7, 7}
1116                 }
1117         },
1118         {0, 1, 3, 0,            /* 0x96 */
1119                 {{1, 2},
1120                 {4, 4},
1121                 {7, 7},
1122                 {0, 0}
1123                 }
1124         },
1125         {1, 1, 3, 0,            /* 0x97 */
1126                 {{0, 2},
1127                 {4, 4},
1128                 {7, 7},
1129                 {0, 0}
1130                 }
1131         },
1132         {0, 1, 2, 0,            /* 0x98 */
1133                 {{3, 4},
1134                 {7, 7},
1135                 {0, 0},
1136                 {0, 0}
1137                 }
1138         },
1139         {1, 1, 3, 0,            /* 0x99 */
1140                 {{0, 0},
1141                 {3, 4},
1142                 {7, 7},
1143                 {0, 0}
1144                 }
1145         },
1146         {0, 1, 3, 0,            /* 0x9a */
1147                 {{1, 1},
1148                 {3, 4},
1149                 {7, 7},
1150                 {0, 0}
1151                 }
1152         },
1153         {1, 1, 3, 0,            /* 0x9b */
1154                 {{0, 1},
1155                 {3, 4},
1156                 {7, 7},
1157                 {0, 0}
1158                 }
1159         },
1160         {0, 1, 2, 0,            /* 0x9c */
1161                 {{2, 4},
1162                 {7, 7},
1163                 {0, 0},
1164                 {0, 0}
1165                 }
1166         },
1167         {1, 1, 3, 0,            /* 0x9d */
1168                 {{0, 0},
1169                 {2, 4},
1170                 {7, 7},
1171                 {0, 0}
1172                 }
1173         },
1174         {0, 1, 2, 0,            /* 0x9e */
1175                 {{1, 4},
1176                 {7, 7},
1177                 {0, 0},
1178                 {0, 0}
1179                 }
1180         },
1181         {1, 1, 2, 0,            /* 0x9f */
1182                 {{0, 4},
1183                 {7, 7},
1184                 {0, 0},
1185                 {0, 0}
1186                 }
1187         },
1188         {0, 1, 2, 0,            /* 0xa0 */
1189                 {{5, 5},
1190                 {7, 7},
1191                 {0, 0},
1192                 {0, 0}
1193                 }
1194         },
1195         {1, 1, 3, 0,            /* 0xa1 */
1196                 {{0, 0},
1197                 {5, 5},
1198                 {7, 7},
1199                 {0, 0}
1200                 }
1201         },
1202         {0, 1, 3, 0,            /* 0xa2 */
1203                 {{1, 1},
1204                 {5, 5},
1205                 {7, 7},
1206                 {0, 0}
1207                 }
1208         },
1209         {1, 1, 3, 0,            /* 0xa3 */
1210                 {{0, 1},
1211                 {5, 5},
1212                 {7, 7},
1213                 {0, 0}
1214                 }
1215         },
1216         {0, 1, 3, 0,            /* 0xa4 */
1217                 {{2, 2},
1218                 {5, 5},
1219                 {7, 7},
1220                 {0, 0}
1221                 }
1222         },
1223         {1, 1, 4, 0,            /* 0xa5 */
1224                 {{0, 0},
1225                 {2, 2},
1226                 {5, 5},
1227                 {7, 7}
1228                 }
1229         },
1230         {0, 1, 3, 0,            /* 0xa6 */
1231                 {{1, 2},
1232                 {5, 5},
1233                 {7, 7},
1234                 {0, 0}
1235                 }
1236         },
1237         {1, 1, 3, 0,            /* 0xa7 */
1238                 {{0, 2},
1239                 {5, 5},
1240                 {7, 7},
1241                 {0, 0}
1242                 }
1243         },
1244         {0, 1, 3, 0,            /* 0xa8 */
1245                 {{3, 3},
1246                 {5, 5},
1247                 {7, 7},
1248                 {0, 0}
1249                 }
1250         },
1251         {1, 1, 4, 0,            /* 0xa9 */
1252                 {{0, 0},
1253                 {3, 3},
1254                 {5, 5},
1255                 {7, 7}
1256                 }
1257         },
1258         {0, 1, 4, 0,            /* 0xaa */
1259                 {{1, 1},
1260                 {3, 3},
1261                 {5, 5},
1262                 {7, 7}
1263                 }
1264         },
1265         {1, 1, 4, 0,            /* 0xab */
1266                 {{0, 1},
1267                 {3, 3},
1268                 {5, 5},
1269                 {7, 7}
1270                 }
1271         },
1272         {0, 1, 3, 0,            /* 0xac */
1273                 {{2, 3},
1274                 {5, 5},
1275                 {7, 7},
1276                 {0, 0}
1277                 }
1278         },
1279         {1, 1, 4, 0,            /* 0xad */
1280                 {{0, 0},
1281                 {2, 3},
1282                 {5, 5},
1283                 {7, 7}
1284                 }
1285         },
1286         {0, 1, 3, 0,            /* 0xae */
1287                 {{1, 3},
1288                 {5, 5},
1289                 {7, 7},
1290                 {0, 0}
1291                 }
1292         },
1293         {1, 1, 3, 0,            /* 0xaf */
1294                 {{0, 3},
1295                 {5, 5},
1296                 {7, 7},
1297                 {0, 0}
1298                 }
1299         },
1300         {0, 1, 2, 0,            /* 0xb0 */
1301                 {{4, 5},
1302                 {7, 7},
1303                 {0, 0},
1304                 {0, 0}
1305                 }
1306         },
1307         {1, 1, 3, 0,            /* 0xb1 */
1308                 {{0, 0},
1309                 {4, 5},
1310                 {7, 7},
1311                 {0, 0}
1312                 }
1313         },
1314         {0, 1, 3, 0,            /* 0xb2 */
1315                 {{1, 1},
1316                 {4, 5},
1317                 {7, 7},
1318                 {0, 0}
1319                 }
1320         },
1321         {1, 1, 3, 0,            /* 0xb3 */
1322                 {{0, 1},
1323                 {4, 5},
1324                 {7, 7},
1325                 {0, 0}
1326                 }
1327         },
1328         {0, 1, 3, 0,            /* 0xb4 */
1329                 {{2, 2},
1330                 {4, 5},
1331                 {7, 7},
1332                 {0, 0}
1333                 }
1334         },
1335         {1, 1, 4, 0,            /* 0xb5 */
1336                 {{0, 0},
1337                 {2, 2},
1338                 {4, 5},
1339                 {7, 7}
1340                 }
1341         },
1342         {0, 1, 3, 0,            /* 0xb6 */
1343                 {{1, 2},
1344                 {4, 5},
1345                 {7, 7},
1346                 {0, 0}
1347                 }
1348         },
1349         {1, 1, 3, 0,            /* 0xb7 */
1350                 {{0, 2},
1351                 {4, 5},
1352                 {7, 7},
1353                 {0, 0}
1354                 }
1355         },
1356         {0, 1, 2, 0,            /* 0xb8 */
1357                 {{3, 5},
1358                 {7, 7},
1359                 {0, 0},
1360                 {0, 0}
1361                 }
1362         },
1363         {1, 1, 3, 0,            /* 0xb9 */
1364                 {{0, 0},
1365                 {3, 5},
1366                 {7, 7},
1367                 {0, 0}
1368                 }
1369         },
1370         {0, 1, 3, 0,            /* 0xba */
1371                 {{1, 1},
1372                 {3, 5},
1373                 {7, 7},
1374                 {0, 0}
1375                 }
1376         },
1377         {1, 1, 3, 0,            /* 0xbb */
1378                 {{0, 1},
1379                 {3, 5},
1380                 {7, 7},
1381                 {0, 0}
1382                 }
1383         },
1384         {0, 1, 2, 0,            /* 0xbc */
1385                 {{2, 5},
1386                 {7, 7},
1387                 {0, 0},
1388                 {0, 0}
1389                 }
1390         },
1391         {1, 1, 3, 0,            /* 0xbd */
1392                 {{0, 0},
1393                 {2, 5},
1394                 {7, 7},
1395                 {0, 0}
1396                 }
1397         },
1398         {0, 1, 2, 0,            /* 0xbe */
1399                 {{1, 5},
1400                 {7, 7},
1401                 {0, 0},
1402                 {0, 0}
1403                 }
1404         },
1405         {1, 1, 2, 0,            /* 0xbf */
1406                 {{0, 5},
1407                 {7, 7},
1408                 {0, 0},
1409                 {0, 0}
1410                 }
1411         },
1412         {0, 1, 1, 0,            /* 0xc0 */
1413                 {{6, 7},
1414                 {0, 0},
1415                 {0, 0},
1416                 {0, 0}
1417                 }
1418         },
1419         {1, 1, 2, 0,            /* 0xc1 */
1420                 {{0, 0},
1421                 {6, 7},
1422                 {0, 0},
1423                 {0, 0}
1424                 }
1425         },
1426         {0, 1, 2, 0,            /* 0xc2 */
1427                 {{1, 1},
1428                 {6, 7},
1429                 {0, 0},
1430                 {0, 0}
1431                 }
1432         },
1433         {1, 1, 2, 0,            /* 0xc3 */
1434                 {{0, 1},
1435                 {6, 7},
1436                 {0, 0},
1437                 {0, 0}
1438                 }
1439         },
1440         {0, 1, 2, 0,            /* 0xc4 */
1441                 {{2, 2},
1442                 {6, 7},
1443                 {0, 0},
1444                 {0, 0}
1445                 }
1446         },
1447         {1, 1, 3, 0,            /* 0xc5 */
1448                 {{0, 0},
1449                 {2, 2},
1450                 {6, 7},
1451                 {0, 0}
1452                 }
1453         },
1454         {0, 1, 2, 0,            /* 0xc6 */
1455                 {{1, 2},
1456                 {6, 7},
1457                 {0, 0},
1458                 {0, 0}
1459                 }
1460         },
1461         {1, 1, 2, 0,            /* 0xc7 */
1462                 {{0, 2},
1463                 {6, 7},
1464                 {0, 0},
1465                 {0, 0}
1466                 }
1467         },
1468         {0, 1, 2, 0,            /* 0xc8 */
1469                 {{3, 3},
1470                 {6, 7},
1471                 {0, 0},
1472                 {0, 0}
1473                 }
1474         },
1475         {1, 1, 3, 0,            /* 0xc9 */
1476                 {{0, 0},
1477                 {3, 3},
1478                 {6, 7},
1479                 {0, 0}
1480                 }
1481         },
1482         {0, 1, 3, 0,            /* 0xca */
1483                 {{1, 1},
1484                 {3, 3},
1485                 {6, 7},
1486                 {0, 0}
1487                 }
1488         },
1489         {1, 1, 3, 0,            /* 0xcb */
1490                 {{0, 1},
1491                 {3, 3},
1492                 {6, 7},
1493                 {0, 0}
1494                 }
1495         },
1496         {0, 1, 2, 0,            /* 0xcc */
1497                 {{2, 3},
1498                 {6, 7},
1499                 {0, 0},
1500                 {0, 0}
1501                 }
1502         },
1503         {1, 1, 3, 0,            /* 0xcd */
1504                 {{0, 0},
1505                 {2, 3},
1506                 {6, 7},
1507                 {0, 0}
1508                 }
1509         },
1510         {0, 1, 2, 0,            /* 0xce */
1511                 {{1, 3},
1512                 {6, 7},
1513                 {0, 0},
1514                 {0, 0}
1515                 }
1516         },
1517         {1, 1, 2, 0,            /* 0xcf */
1518                 {{0, 3},
1519                 {6, 7},
1520                 {0, 0},
1521                 {0, 0}
1522                 }
1523         },
1524         {0, 1, 2, 0,            /* 0xd0 */
1525                 {{4, 4},
1526                 {6, 7},
1527                 {0, 0},
1528                 {0, 0}
1529                 }
1530         },
1531         {1, 1, 3, 0,            /* 0xd1 */
1532                 {{0, 0},
1533                 {4, 4},
1534                 {6, 7},
1535                 {0, 0}
1536                 }
1537         },
1538         {0, 1, 3, 0,            /* 0xd2 */
1539                 {{1, 1},
1540                 {4, 4},
1541                 {6, 7},
1542                 {0, 0}
1543                 }
1544         },
1545         {1, 1, 3, 0,            /* 0xd3 */
1546                 {{0, 1},
1547                 {4, 4},
1548                 {6, 7},
1549                 {0, 0}
1550                 }
1551         },
1552         {0, 1, 3, 0,            /* 0xd4 */
1553                 {{2, 2},
1554                 {4, 4},
1555                 {6, 7},
1556                 {0, 0}
1557                 }
1558         },
1559         {1, 1, 4, 0,            /* 0xd5 */
1560                 {{0, 0},
1561                 {2, 2},
1562                 {4, 4},
1563                 {6, 7}
1564                 }
1565         },
1566         {0, 1, 3, 0,            /* 0xd6 */
1567                 {{1, 2},
1568                 {4, 4},
1569                 {6, 7},
1570                 {0, 0}
1571                 }
1572         },
1573         {1, 1, 3, 0,            /* 0xd7 */
1574                 {{0, 2},
1575                 {4, 4},
1576                 {6, 7},
1577                 {0, 0}
1578                 }
1579         },
1580         {0, 1, 2, 0,            /* 0xd8 */
1581                 {{3, 4},
1582                 {6, 7},
1583                 {0, 0},
1584                 {0, 0}
1585                 }
1586         },
1587         {1, 1, 3, 0,            /* 0xd9 */
1588                 {{0, 0},
1589                 {3, 4},
1590                 {6, 7},
1591                 {0, 0}
1592                 }
1593         },
1594         {0, 1, 3, 0,            /* 0xda */
1595                 {{1, 1},
1596                 {3, 4},
1597                 {6, 7},
1598                 {0, 0}
1599                 }
1600         },
1601         {1, 1, 3, 0,            /* 0xdb */
1602                 {{0, 1},
1603                 {3, 4},
1604                 {6, 7},
1605                 {0, 0}
1606                 }
1607         },
1608         {0, 1, 2, 0,            /* 0xdc */
1609                 {{2, 4},
1610                 {6, 7},
1611                 {0, 0},
1612                 {0, 0}
1613                 }
1614         },
1615         {1, 1, 3, 0,            /* 0xdd */
1616                 {{0, 0},
1617                 {2, 4},
1618                 {6, 7},
1619                 {0, 0}
1620                 }
1621         },
1622         {0, 1, 2, 0,            /* 0xde */
1623                 {{1, 4},
1624                 {6, 7},
1625                 {0, 0},
1626                 {0, 0}
1627                 }
1628         },
1629         {1, 1, 2, 0,            /* 0xdf */
1630                 {{0, 4},
1631                 {6, 7},
1632                 {0, 0},
1633                 {0, 0}
1634                 }
1635         },
1636         {0, 1, 1, 0,            /* 0xe0 */
1637                 {{5, 7},
1638                 {0, 0},
1639                 {0, 0},
1640                 {0, 0}
1641                 }
1642         },
1643         {1, 1, 2, 0,            /* 0xe1 */
1644                 {{0, 0},
1645                 {5, 7},
1646                 {0, 0},
1647                 {0, 0}
1648                 }
1649         },
1650         {0, 1, 2, 0,            /* 0xe2 */
1651                 {{1, 1},
1652                 {5, 7},
1653                 {0, 0},
1654                 {0, 0}
1655                 }
1656         },
1657         {1, 1, 2, 0,            /* 0xe3 */
1658                 {{0, 1},
1659                 {5, 7},
1660                 {0, 0},
1661                 {0, 0}
1662                 }
1663         },
1664         {0, 1, 2, 0,            /* 0xe4 */
1665                 {{2, 2},
1666                 {5, 7},
1667                 {0, 0},
1668                 {0, 0}
1669                 }
1670         },
1671         {1, 1, 3, 0,            /* 0xe5 */
1672                 {{0, 0},
1673                 {2, 2},
1674                 {5, 7},
1675                 {0, 0}
1676                 }
1677         },
1678         {0, 1, 2, 0,            /* 0xe6 */
1679                 {{1, 2},
1680                 {5, 7},
1681                 {0, 0},
1682                 {0, 0}
1683                 }
1684         },
1685         {1, 1, 2, 0,            /* 0xe7 */
1686                 {{0, 2},
1687                 {5, 7},
1688                 {0, 0},
1689                 {0, 0}
1690                 }
1691         },
1692         {0, 1, 2, 0,            /* 0xe8 */
1693                 {{3, 3},
1694                 {5, 7},
1695                 {0, 0},
1696                 {0, 0}
1697                 }
1698         },
1699         {1, 1, 3, 0,            /* 0xe9 */
1700                 {{0, 0},
1701                 {3, 3},
1702                 {5, 7},
1703                 {0, 0}
1704                 }
1705         },
1706         {0, 1, 3, 0,            /* 0xea */
1707                 {{1, 1},
1708                 {3, 3},
1709                 {5, 7},
1710                 {0, 0}
1711                 }
1712         },
1713         {1, 1, 3, 0,            /* 0xeb */
1714                 {{0, 1},
1715                 {3, 3},
1716                 {5, 7},
1717                 {0, 0}
1718                 }
1719         },
1720         {0, 1, 2, 0,            /* 0xec */
1721                 {{2, 3},
1722                 {5, 7},
1723                 {0, 0},
1724                 {0, 0}
1725                 }
1726         },
1727         {1, 1, 3, 0,            /* 0xed */
1728                 {{0, 0},
1729                 {2, 3},
1730                 {5, 7},
1731                 {0, 0}
1732                 }
1733         },
1734         {0, 1, 2, 0,            /* 0xee */
1735                 {{1, 3},
1736                 {5, 7},
1737                 {0, 0},
1738                 {0, 0}
1739                 }
1740         },
1741         {1, 1, 2, 0,            /* 0xef */
1742                 {{0, 3},
1743                 {5, 7},
1744                 {0, 0},
1745                 {0, 0}
1746                 }
1747         },
1748         {0, 1, 1, 0,            /* 0xf0 */
1749                 {{4, 7},
1750                 {0, 0},
1751                 {0, 0},
1752                 {0, 0}
1753                 }
1754         },
1755         {1, 1, 2, 0,            /* 0xf1 */
1756                 {{0, 0},
1757                 {4, 7},
1758                 {0, 0},
1759                 {0, 0}
1760                 }
1761         },
1762         {0, 1, 2, 0,            /* 0xf2 */
1763                 {{1, 1},
1764                 {4, 7},
1765                 {0, 0},
1766                 {0, 0}
1767                 }
1768         },
1769         {1, 1, 2, 0,            /* 0xf3 */
1770                 {{0, 1},
1771                 {4, 7},
1772                 {0, 0},
1773                 {0, 0}
1774                 }
1775         },
1776         {0, 1, 2, 0,            /* 0xf4 */
1777                 {{2, 2},
1778                 {4, 7},
1779                 {0, 0},
1780                 {0, 0}
1781                 }
1782         },
1783         {1, 1, 3, 0,            /* 0xf5 */
1784                 {{0, 0},
1785                 {2, 2},
1786                 {4, 7},
1787                 {0, 0}
1788                 }
1789         },
1790         {0, 1, 2, 0,            /* 0xf6 */
1791                 {{1, 2},
1792                 {4, 7},
1793                 {0, 0},
1794                 {0, 0}
1795                 }
1796         },
1797         {1, 1, 2, 0,            /* 0xf7 */
1798                 {{0, 2},
1799                 {4, 7},
1800                 {0, 0},
1801                 {0, 0}
1802                 }
1803         },
1804         {0, 1, 1, 0,            /* 0xf8 */
1805                 {{3, 7},
1806                 {0, 0},
1807                 {0, 0},
1808                 {0, 0}
1809                 }
1810         },
1811         {1, 1, 2, 0,            /* 0xf9 */
1812                 {{0, 0},
1813                 {3, 7},
1814                 {0, 0},
1815                 {0, 0}
1816                 }
1817         },
1818         {0, 1, 2, 0,            /* 0xfa */
1819                 {{1, 1},
1820                 {3, 7},
1821                 {0, 0},
1822                 {0, 0}
1823                 }
1824         },
1825         {1, 1, 2, 0,            /* 0xfb */
1826                 {{0, 1},
1827                 {3, 7},
1828                 {0, 0},
1829                 {0, 0}
1830                 }
1831         },
1832         {0, 1, 1, 0,            /* 0xfc */
1833                 {{2, 7},
1834                 {0, 0},
1835                 {0, 0},
1836                 {0, 0}
1837                 }
1838         },
1839         {1, 1, 2, 0,            /* 0xfd */
1840                 {{0, 0},
1841                 {2, 7},
1842                 {0, 0},
1843                 {0, 0}
1844                 }
1845         },
1846         {0, 1, 1, 0,            /* 0xfe */
1847                 {{1, 7},
1848                 {0, 0},
1849                 {0, 0},
1850                 {0, 0}
1851                 }
1852         },
1853         {1, 1, 1, 0,            /* 0xff */
1854                 {{0, 7},
1855                 {0, 0},
1856                 {0, 0},
1857                 {0, 0}
1858                 }
1859         }
1860 };
1861
1862
1863 int
1864 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1865     int ipv4_addr_legal,
1866     int ipv6_addr_legal,
1867     int loopback_scope,
1868     int ipv4_local_scope,
1869     int local_scope,
1870     int site_scope,
1871     int do_update)
1872 {
1873         if ((loopback_scope == 0) &&
1874             (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1875                 /*
1876                  * skip loopback if not in scope *
1877                  */
1878                 return (0);
1879         }
1880         switch (ifa->address.sa.sa_family) {
1881         case AF_INET:
1882                 if (ipv4_addr_legal) {
1883                         struct sockaddr_in *sin;
1884
1885                         sin = (struct sockaddr_in *)&ifa->address.sin;
1886                         if (sin->sin_addr.s_addr == 0) {
1887                                 /* not in scope , unspecified */
1888                                 return (0);
1889                         }
1890                         if ((ipv4_local_scope == 0) &&
1891                             (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1892                                 /* private address not in scope */
1893                                 return (0);
1894                         }
1895                 } else {
1896                         return (0);
1897                 }
1898                 break;
1899 #ifdef INET6
1900         case AF_INET6:
1901                 if (ipv6_addr_legal) {
1902                         struct sockaddr_in6 *sin6;
1903
1904                         /*
1905                          * Must update the flags,  bummer, which means any
1906                          * IFA locks must now be applied HERE <->
1907                          */
1908                         if (do_update) {
1909                                 sctp_gather_internal_ifa_flags(ifa);
1910                         }
1911                         if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1912                                 return (0);
1913                         }
1914                         /* ok to use deprecated addresses? */
1915                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1916                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1917                                 /* skip unspecifed addresses */
1918                                 return (0);
1919                         }
1920                         if (    /* (local_scope == 0) && */
1921                             (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1922                                 return (0);
1923                         }
1924                         if ((site_scope == 0) &&
1925                             (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1926                                 return (0);
1927                         }
1928                 } else {
1929                         return (0);
1930                 }
1931                 break;
1932 #endif
1933         default:
1934                 return (0);
1935         }
1936         return (1);
1937 }
1938
1939 static struct mbuf *
1940 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
1941 {
1942         struct sctp_paramhdr *parmh;
1943         struct mbuf *mret;
1944         int len;
1945
1946         if (ifa->address.sa.sa_family == AF_INET) {
1947                 len = sizeof(struct sctp_ipv4addr_param);
1948         } else if (ifa->address.sa.sa_family == AF_INET6) {
1949                 len = sizeof(struct sctp_ipv6addr_param);
1950         } else {
1951                 /* unknown type */
1952                 return (m);
1953         }
1954         if (M_TRAILINGSPACE(m) >= len) {
1955                 /* easy side we just drop it on the end */
1956                 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1957                 mret = m;
1958         } else {
1959                 /* Need more space */
1960                 mret = m;
1961                 while (SCTP_BUF_NEXT(mret) != NULL) {
1962                         mret = SCTP_BUF_NEXT(mret);
1963                 }
1964                 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
1965                 if (SCTP_BUF_NEXT(mret) == NULL) {
1966                         /* We are hosed, can't add more addresses */
1967                         return (m);
1968                 }
1969                 mret = SCTP_BUF_NEXT(mret);
1970                 parmh = mtod(mret, struct sctp_paramhdr *);
1971         }
1972         /* now add the parameter */
1973         switch (ifa->address.sa.sa_family) {
1974         case AF_INET:
1975                 {
1976                         struct sctp_ipv4addr_param *ipv4p;
1977                         struct sockaddr_in *sin;
1978
1979                         sin = (struct sockaddr_in *)&ifa->address.sin;
1980                         ipv4p = (struct sctp_ipv4addr_param *)parmh;
1981                         parmh->param_type = htons(SCTP_IPV4_ADDRESS);
1982                         parmh->param_length = htons(len);
1983                         ipv4p->addr = sin->sin_addr.s_addr;
1984                         SCTP_BUF_LEN(mret) += len;
1985                         break;
1986                 }
1987 #ifdef INET6
1988         case AF_INET6:
1989                 {
1990                         struct sctp_ipv6addr_param *ipv6p;
1991                         struct sockaddr_in6 *sin6;
1992
1993                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
1994                         ipv6p = (struct sctp_ipv6addr_param *)parmh;
1995                         parmh->param_type = htons(SCTP_IPV6_ADDRESS);
1996                         parmh->param_length = htons(len);
1997                         memcpy(ipv6p->addr, &sin6->sin6_addr,
1998                             sizeof(ipv6p->addr));
1999                         /* clear embedded scope in the address */
2000                         in6_clearscope((struct in6_addr *)ipv6p->addr);
2001                         SCTP_BUF_LEN(mret) += len;
2002                         break;
2003                 }
2004 #endif
2005         default:
2006                 return (m);
2007         }
2008         return (mret);
2009 }
2010
2011
2012 struct mbuf *
2013 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
2014     struct mbuf *m_at, int cnt_inits_to)
2015 {
2016         struct sctp_vrf *vrf = NULL;
2017         int cnt, limit_out = 0, total_count;
2018         uint32_t vrf_id;
2019
2020         vrf_id = inp->def_vrf_id;
2021         SCTP_IPI_ADDR_RLOCK();
2022         vrf = sctp_find_vrf(vrf_id);
2023         if (vrf == NULL) {
2024                 SCTP_IPI_ADDR_RUNLOCK();
2025                 return (m_at);
2026         }
2027         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2028                 struct sctp_ifa *sctp_ifap;
2029                 struct sctp_ifn *sctp_ifnp;
2030
2031                 cnt = cnt_inits_to;
2032                 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2033                         limit_out = 1;
2034                         cnt = SCTP_ADDRESS_LIMIT;
2035                         goto skip_count;
2036                 }
2037                 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2038                         if ((scope->loopback_scope == 0) &&
2039                             SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2040                                 /*
2041                                  * Skip loopback devices if loopback_scope
2042                                  * not set
2043                                  */
2044                                 continue;
2045                         }
2046                         LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2047                                 if (sctp_is_address_in_scope(sctp_ifap,
2048                                     scope->ipv4_addr_legal,
2049                                     scope->ipv6_addr_legal,
2050                                     scope->loopback_scope,
2051                                     scope->ipv4_local_scope,
2052                                     scope->local_scope,
2053                                     scope->site_scope, 1) == 0) {
2054                                         continue;
2055                                 }
2056                                 cnt++;
2057                                 if (cnt > SCTP_ADDRESS_LIMIT) {
2058                                         break;
2059                                 }
2060                         }
2061                         if (cnt > SCTP_ADDRESS_LIMIT) {
2062                                 break;
2063                         }
2064                 }
2065 skip_count:
2066                 if (cnt > 1) {
2067                         total_count = 0;
2068                         LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2069                                 cnt = 0;
2070                                 if ((scope->loopback_scope == 0) &&
2071                                     SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2072                                         /*
2073                                          * Skip loopback devices if
2074                                          * loopback_scope not set
2075                                          */
2076                                         continue;
2077                                 }
2078                                 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2079                                         if (sctp_is_address_in_scope(sctp_ifap,
2080                                             scope->ipv4_addr_legal,
2081                                             scope->ipv6_addr_legal,
2082                                             scope->loopback_scope,
2083                                             scope->ipv4_local_scope,
2084                                             scope->local_scope,
2085                                             scope->site_scope, 0) == 0) {
2086                                                 continue;
2087                                         }
2088                                         m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
2089                                         if (limit_out) {
2090                                                 cnt++;
2091                                                 total_count++;
2092                                                 if (cnt >= 2) {
2093                                                         /*
2094                                                          * two from each
2095                                                          * address
2096                                                          */
2097                                                         break;
2098                                                 }
2099                                                 if (total_count > SCTP_ADDRESS_LIMIT) {
2100                                                         /* No more addresses */
2101                                                         break;
2102                                                 }
2103                                         }
2104                                 }
2105                         }
2106                 }
2107         } else {
2108                 struct sctp_laddr *laddr;
2109
2110                 cnt = cnt_inits_to;
2111                 /* First, how many ? */
2112                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2113                         if (laddr->ifa == NULL) {
2114                                 continue;
2115                         }
2116                         if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2117                                 /*
2118                                  * Address being deleted by the system, dont
2119                                  * list.
2120                                  */
2121                                 continue;
2122                         if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2123                                 /*
2124                                  * Address being deleted on this ep don't
2125                                  * list.
2126                                  */
2127                                 continue;
2128                         }
2129                         if (sctp_is_address_in_scope(laddr->ifa,
2130                             scope->ipv4_addr_legal,
2131                             scope->ipv6_addr_legal,
2132                             scope->loopback_scope,
2133                             scope->ipv4_local_scope,
2134                             scope->local_scope,
2135                             scope->site_scope, 1) == 0) {
2136                                 continue;
2137                         }
2138                         cnt++;
2139                 }
2140                 if (cnt > SCTP_ADDRESS_LIMIT) {
2141                         limit_out = 1;
2142                 }
2143                 /*
2144                  * To get through a NAT we only list addresses if we have
2145                  * more than one. That way if you just bind a single address
2146                  * we let the source of the init dictate our address.
2147                  */
2148                 if (cnt > 1) {
2149                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2150                                 cnt = 0;
2151                                 if (laddr->ifa == NULL) {
2152                                         continue;
2153                                 }
2154                                 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2155                                         continue;
2156
2157                                 if (sctp_is_address_in_scope(laddr->ifa,
2158                                     scope->ipv4_addr_legal,
2159                                     scope->ipv6_addr_legal,
2160                                     scope->loopback_scope,
2161                                     scope->ipv4_local_scope,
2162                                     scope->local_scope,
2163                                     scope->site_scope, 0) == 0) {
2164                                         continue;
2165                                 }
2166                                 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
2167                                 cnt++;
2168                                 if (cnt >= SCTP_ADDRESS_LIMIT) {
2169                                         break;
2170                                 }
2171                         }
2172                 }
2173         }
2174         SCTP_IPI_ADDR_RUNLOCK();
2175         return (m_at);
2176 }
2177
2178 static struct sctp_ifa *
2179 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2180     uint8_t dest_is_loop,
2181     uint8_t dest_is_priv,
2182     sa_family_t fam)
2183 {
2184         uint8_t dest_is_global = 0;
2185
2186         /* dest_is_priv is true if destination is a private address */
2187         /* dest_is_loop is true if destination is a loopback addresses */
2188
2189         /**
2190          * Here we determine if its a preferred address. A preferred address
2191          * means it is the same scope or higher scope then the destination.
2192          * L = loopback, P = private, G = global
2193          * -----------------------------------------
2194          *    src    |  dest | result
2195          *  ----------------------------------------
2196          *     L     |    L  |    yes
2197          *  -----------------------------------------
2198          *     P     |    L  |    yes-v4 no-v6
2199          *  -----------------------------------------
2200          *     G     |    L  |    yes-v4 no-v6
2201          *  -----------------------------------------
2202          *     L     |    P  |    no
2203          *  -----------------------------------------
2204          *     P     |    P  |    yes
2205          *  -----------------------------------------
2206          *     G     |    P  |    no
2207          *   -----------------------------------------
2208          *     L     |    G  |    no
2209          *   -----------------------------------------
2210          *     P     |    G  |    no
2211          *    -----------------------------------------
2212          *     G     |    G  |    yes
2213          *    -----------------------------------------
2214          */
2215
2216         if (ifa->address.sa.sa_family != fam) {
2217                 /* forget mis-matched family */
2218                 return (NULL);
2219         }
2220         if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2221                 dest_is_global = 1;
2222         }
2223         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2224         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2225         /* Ok the address may be ok */
2226         if (fam == AF_INET6) {
2227                 /* ok to use deprecated addresses? no lets not! */
2228                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2229                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2230                         return (NULL);
2231                 }
2232                 if (ifa->src_is_priv && !ifa->src_is_loop) {
2233                         if (dest_is_loop) {
2234                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2235                                 return (NULL);
2236                         }
2237                 }
2238                 if (ifa->src_is_glob) {
2239                         if (dest_is_loop) {
2240                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2241                                 return (NULL);
2242                         }
2243                 }
2244         }
2245         /*
2246          * Now that we know what is what, implement or table this could in
2247          * theory be done slicker (it used to be), but this is
2248          * straightforward and easier to validate :-)
2249          */
2250         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2251             ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2252         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2253             dest_is_loop, dest_is_priv, dest_is_global);
2254
2255         if ((ifa->src_is_loop) && (dest_is_priv)) {
2256                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2257                 return (NULL);
2258         }
2259         if ((ifa->src_is_glob) && (dest_is_priv)) {
2260                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2261                 return (NULL);
2262         }
2263         if ((ifa->src_is_loop) && (dest_is_global)) {
2264                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2265                 return (NULL);
2266         }
2267         if ((ifa->src_is_priv) && (dest_is_global)) {
2268                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2269                 return (NULL);
2270         }
2271         SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2272         /* its a preferred address */
2273         return (ifa);
2274 }
2275
2276 static struct sctp_ifa *
2277 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2278     uint8_t dest_is_loop,
2279     uint8_t dest_is_priv,
2280     sa_family_t fam)
2281 {
2282         uint8_t dest_is_global = 0;
2283
2284         /*
2285          * Here we determine if its a acceptable address. A acceptable
2286          * address means it is the same scope or higher scope but we can
2287          * allow for NAT which means its ok to have a global dest and a
2288          * private src.
2289          * 
2290          * L = loopback, P = private, G = global
2291          * ----------------------------------------- src    |  dest | result
2292          * ----------------------------------------- L     |   L   |    yes
2293          * ----------------------------------------- P     |   L   |
2294          * yes-v4 no-v6 ----------------------------------------- G     |
2295          * L   |    yes ----------------------------------------- L     |
2296          * P   |    no ----------------------------------------- P     |   P
2297          * |    yes ----------------------------------------- G     |   P
2298          * |    yes - May not work -----------------------------------------
2299          * L     |   G   |    no ----------------------------------------- P
2300          * |   G   |    yes - May not work
2301          * ----------------------------------------- G     |   G   |    yes
2302          * -----------------------------------------
2303          */
2304
2305         if (ifa->address.sa.sa_family != fam) {
2306                 /* forget non matching family */
2307                 return (NULL);
2308         }
2309         /* Ok the address may be ok */
2310         if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2311                 dest_is_global = 1;
2312         }
2313         if (fam == AF_INET6) {
2314                 /* ok to use deprecated addresses? */
2315                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2316                         return (NULL);
2317                 }
2318                 if (ifa->src_is_priv) {
2319                         /* Special case, linklocal to loop */
2320                         if (dest_is_loop)
2321                                 return (NULL);
2322                 }
2323         }
2324         /*
2325          * Now that we know what is what, implement our table. This could in
2326          * theory be done slicker (it used to be), but this is
2327          * straightforward and easier to validate :-)
2328          */
2329         if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2330                 return (NULL);
2331         }
2332         if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2333                 return (NULL);
2334         }
2335         /* its an acceptable address */
2336         return (ifa);
2337 }
2338
2339 int
2340 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2341 {
2342         struct sctp_laddr *laddr;
2343
2344         if (stcb == NULL) {
2345                 /* There are no restrictions, no TCB :-) */
2346                 return (0);
2347         }
2348         LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2349                 if (laddr->ifa == NULL) {
2350                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2351                             __FUNCTION__);
2352                         continue;
2353                 }
2354                 if (laddr->ifa == ifa) {
2355                         /* Yes it is on the list */
2356                         return (1);
2357                 }
2358         }
2359         return (0);
2360 }
2361
2362
2363 int
2364 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2365 {
2366         struct sctp_laddr *laddr;
2367
2368         if (ifa == NULL)
2369                 return (0);
2370         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2371                 if (laddr->ifa == NULL) {
2372                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2373                             __FUNCTION__);
2374                         continue;
2375                 }
2376                 if ((laddr->ifa == ifa) && laddr->action == 0)
2377                         /* same pointer */
2378                         return (1);
2379         }
2380         return (0);
2381 }
2382
2383
2384
2385 static struct sctp_ifa *
2386 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2387     sctp_route_t * ro,
2388     uint32_t vrf_id,
2389     int non_asoc_addr_ok,
2390     uint8_t dest_is_priv,
2391     uint8_t dest_is_loop,
2392     sa_family_t fam)
2393 {
2394         struct sctp_laddr *laddr, *starting_point;
2395         void *ifn;
2396         int resettotop = 0;
2397         struct sctp_ifn *sctp_ifn;
2398         struct sctp_ifa *sctp_ifa, *sifa;
2399         struct sctp_vrf *vrf;
2400         uint32_t ifn_index;
2401
2402         vrf = sctp_find_vrf(vrf_id);
2403         if (vrf == NULL)
2404                 return (NULL);
2405
2406         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2407         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2408         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2409         /*
2410          * first question, is the ifn we will emit on in our list, if so, we
2411          * want such an address. Note that we first looked for a preferred
2412          * address.
2413          */
2414         if (sctp_ifn) {
2415                 /* is a preferred one on the interface we route out? */
2416                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2417                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2418                             (non_asoc_addr_ok == 0))
2419                                 continue;
2420                         sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2421                             dest_is_loop,
2422                             dest_is_priv, fam);
2423                         if (sifa == NULL)
2424                                 continue;
2425                         if (sctp_is_addr_in_ep(inp, sifa)) {
2426                                 atomic_add_int(&sifa->refcount, 1);
2427                                 return (sifa);
2428                         }
2429                 }
2430         }
2431         /*
2432          * ok, now we now need to find one on the list of the addresses. We
2433          * can't get one on the emitting interface so let's find first a
2434          * preferred one. If not that an acceptable one otherwise... we
2435          * return NULL.
2436          */
2437         starting_point = inp->next_addr_touse;
2438 once_again:
2439         if (inp->next_addr_touse == NULL) {
2440                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2441                 resettotop = 1;
2442         }
2443         for (laddr = inp->next_addr_touse; laddr;
2444             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2445                 if (laddr->ifa == NULL) {
2446                         /* address has been removed */
2447                         continue;
2448                 }
2449                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2450                         /* address is being deleted */
2451                         continue;
2452                 }
2453                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2454                     dest_is_priv, fam);
2455                 if (sifa == NULL)
2456                         continue;
2457                 atomic_add_int(&sifa->refcount, 1);
2458                 return (sifa);
2459         }
2460         if (resettotop == 0) {
2461                 inp->next_addr_touse = NULL;
2462                 goto once_again;
2463         }
2464         inp->next_addr_touse = starting_point;
2465         resettotop = 0;
2466 once_again_too:
2467         if (inp->next_addr_touse == NULL) {
2468                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2469                 resettotop = 1;
2470         }
2471         /* ok, what about an acceptable address in the inp */
2472         for (laddr = inp->next_addr_touse; laddr;
2473             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2474                 if (laddr->ifa == NULL) {
2475                         /* address has been removed */
2476                         continue;
2477                 }
2478                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2479                         /* address is being deleted */
2480                         continue;
2481                 }
2482                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2483                     dest_is_priv, fam);
2484                 if (sifa == NULL)
2485                         continue;
2486                 atomic_add_int(&sifa->refcount, 1);
2487                 return (sifa);
2488         }
2489         if (resettotop == 0) {
2490                 inp->next_addr_touse = NULL;
2491                 goto once_again_too;
2492         }
2493         /*
2494          * no address bound can be a source for the destination we are in
2495          * trouble
2496          */
2497         return (NULL);
2498 }
2499
2500
2501
2502 static struct sctp_ifa *
2503 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2504     struct sctp_tcb *stcb,
2505     struct sctp_nets *net,
2506     sctp_route_t * ro,
2507     uint32_t vrf_id,
2508     uint8_t dest_is_priv,
2509     uint8_t dest_is_loop,
2510     int non_asoc_addr_ok,
2511     sa_family_t fam)
2512 {
2513         struct sctp_laddr *laddr, *starting_point;
2514         void *ifn;
2515         struct sctp_ifn *sctp_ifn;
2516         struct sctp_ifa *sctp_ifa, *sifa;
2517         uint8_t start_at_beginning = 0;
2518         struct sctp_vrf *vrf;
2519         uint32_t ifn_index;
2520
2521         /*
2522          * first question, is the ifn we will emit on in our list, if so, we
2523          * want that one.
2524          */
2525         vrf = sctp_find_vrf(vrf_id);
2526         if (vrf == NULL)
2527                 return (NULL);
2528
2529         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2530         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2531         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2532
2533         /*
2534          * first question, is the ifn we will emit on in our list?  If so,
2535          * we want that one. First we look for a preferred. Second, we go
2536          * for an acceptable.
2537          */
2538         if (sctp_ifn) {
2539                 /* first try for a preferred address on the ep */
2540                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2541                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2542                                 continue;
2543                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2544                                 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2545                                 if (sifa == NULL)
2546                                         continue;
2547                                 if (((non_asoc_addr_ok == 0) &&
2548                                     (sctp_is_addr_restricted(stcb, sifa))) ||
2549                                     (non_asoc_addr_ok &&
2550                                     (sctp_is_addr_restricted(stcb, sifa)) &&
2551                                     (!sctp_is_addr_pending(stcb, sifa)))) {
2552                                         /* on the no-no list */
2553                                         continue;
2554                                 }
2555                                 atomic_add_int(&sifa->refcount, 1);
2556                                 return (sifa);
2557                         }
2558                 }
2559                 /* next try for an acceptable address on the ep */
2560                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2561                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2562                                 continue;
2563                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2564                                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2565                                 if (sifa == NULL)
2566                                         continue;
2567                                 if (((non_asoc_addr_ok == 0) &&
2568                                     (sctp_is_addr_restricted(stcb, sifa))) ||
2569                                     (non_asoc_addr_ok &&
2570                                     (sctp_is_addr_restricted(stcb, sifa)) &&
2571                                     (!sctp_is_addr_pending(stcb, sifa)))) {
2572                                         /* on the no-no list */
2573                                         continue;
2574                                 }
2575                                 atomic_add_int(&sifa->refcount, 1);
2576                                 return (sifa);
2577                         }
2578                 }
2579
2580         }
2581         /*
2582          * if we can't find one like that then we must look at all addresses
2583          * bound to pick one at first preferable then secondly acceptable.
2584          */
2585         starting_point = stcb->asoc.last_used_address;
2586 sctp_from_the_top:
2587         if (stcb->asoc.last_used_address == NULL) {
2588                 start_at_beginning = 1;
2589                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2590         }
2591         /* search beginning with the last used address */
2592         for (laddr = stcb->asoc.last_used_address; laddr;
2593             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2594                 if (laddr->ifa == NULL) {
2595                         /* address has been removed */
2596                         continue;
2597                 }
2598                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2599                         /* address is being deleted */
2600                         continue;
2601                 }
2602                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2603                 if (sifa == NULL)
2604                         continue;
2605                 if (((non_asoc_addr_ok == 0) &&
2606                     (sctp_is_addr_restricted(stcb, sifa))) ||
2607                     (non_asoc_addr_ok &&
2608                     (sctp_is_addr_restricted(stcb, sifa)) &&
2609                     (!sctp_is_addr_pending(stcb, sifa)))) {
2610                         /* on the no-no list */
2611                         continue;
2612                 }
2613                 stcb->asoc.last_used_address = laddr;
2614                 atomic_add_int(&sifa->refcount, 1);
2615                 return (sifa);
2616         }
2617         if (start_at_beginning == 0) {
2618                 stcb->asoc.last_used_address = NULL;
2619                 goto sctp_from_the_top;
2620         }
2621         /* now try for any higher scope than the destination */
2622         stcb->asoc.last_used_address = starting_point;
2623         start_at_beginning = 0;
2624 sctp_from_the_top2:
2625         if (stcb->asoc.last_used_address == NULL) {
2626                 start_at_beginning = 1;
2627                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2628         }
2629         /* search beginning with the last used address */
2630         for (laddr = stcb->asoc.last_used_address; laddr;
2631             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2632                 if (laddr->ifa == NULL) {
2633                         /* address has been removed */
2634                         continue;
2635                 }
2636                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2637                         /* address is being deleted */
2638                         continue;
2639                 }
2640                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2641                     dest_is_priv, fam);
2642                 if (sifa == NULL)
2643                         continue;
2644                 if (((non_asoc_addr_ok == 0) &&
2645                     (sctp_is_addr_restricted(stcb, sifa))) ||
2646                     (non_asoc_addr_ok &&
2647                     (sctp_is_addr_restricted(stcb, sifa)) &&
2648                     (!sctp_is_addr_pending(stcb, sifa)))) {
2649                         /* on the no-no list */
2650                         continue;
2651                 }
2652                 stcb->asoc.last_used_address = laddr;
2653                 atomic_add_int(&sifa->refcount, 1);
2654                 return (sifa);
2655         }
2656         if (start_at_beginning == 0) {
2657                 stcb->asoc.last_used_address = NULL;
2658                 goto sctp_from_the_top2;
2659         }
2660         return (NULL);
2661 }
2662
2663 static struct sctp_ifa *
2664 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2665     struct sctp_tcb *stcb,
2666     int non_asoc_addr_ok,
2667     uint8_t dest_is_loop,
2668     uint8_t dest_is_priv,
2669     int addr_wanted,
2670     sa_family_t fam,
2671     sctp_route_t * ro
2672 )
2673 {
2674         struct sctp_ifa *ifa, *sifa;
2675         int num_eligible_addr = 0;
2676
2677 #ifdef INET6
2678         struct sockaddr_in6 sin6, lsa6;
2679
2680         if (fam == AF_INET6) {
2681                 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2682                 (void)sa6_recoverscope(&sin6);
2683         }
2684 #endif                          /* INET6 */
2685         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2686                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2687                     (non_asoc_addr_ok == 0))
2688                         continue;
2689                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2690                     dest_is_priv, fam);
2691                 if (sifa == NULL)
2692                         continue;
2693 #ifdef INET6
2694                 if (fam == AF_INET6 &&
2695                     dest_is_loop &&
2696                     sifa->src_is_loop && sifa->src_is_priv) {
2697                         /*
2698                          * don't allow fe80::1 to be a src on loop ::1, we
2699                          * don't list it to the peer so we will get an
2700                          * abort.
2701                          */
2702                         continue;
2703                 }
2704                 if (fam == AF_INET6 &&
2705                     IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2706                     IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2707                         /*
2708                          * link-local <-> link-local must belong to the same
2709                          * scope.
2710                          */
2711                         memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2712                         (void)sa6_recoverscope(&lsa6);
2713                         if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2714                                 continue;
2715                         }
2716                 }
2717 #endif                          /* INET6 */
2718
2719                 /*
2720                  * Check if the IPv6 address matches to next-hop. In the
2721                  * mobile case, old IPv6 address may be not deleted from the
2722                  * interface. Then, the interface has previous and new
2723                  * addresses.  We should use one corresponding to the
2724                  * next-hop.  (by micchie)
2725                  */
2726 #ifdef INET6
2727                 if (stcb && fam == AF_INET6 &&
2728                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2729                         if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
2730                             == 0) {
2731                                 continue;
2732                         }
2733                 }
2734 #endif
2735                 /* Avoid topologically incorrect IPv4 address */
2736                 if (stcb && fam == AF_INET &&
2737                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2738                         if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2739                                 continue;
2740                         }
2741                 }
2742                 if (stcb) {
2743                         if (sctp_is_address_in_scope(ifa,
2744                             stcb->asoc.ipv4_addr_legal,
2745                             stcb->asoc.ipv6_addr_legal,
2746                             stcb->asoc.loopback_scope,
2747                             stcb->asoc.ipv4_local_scope,
2748                             stcb->asoc.local_scope,
2749                             stcb->asoc.site_scope, 0) == 0) {
2750                                 continue;
2751                         }
2752                         if (((non_asoc_addr_ok == 0) &&
2753                             (sctp_is_addr_restricted(stcb, sifa))) ||
2754                             (non_asoc_addr_ok &&
2755                             (sctp_is_addr_restricted(stcb, sifa)) &&
2756                             (!sctp_is_addr_pending(stcb, sifa)))) {
2757                                 /*
2758                                  * It is restricted for some reason..
2759                                  * probably not yet added.
2760                                  */
2761                                 continue;
2762                         }
2763                 }
2764                 if (num_eligible_addr >= addr_wanted) {
2765                         return (sifa);
2766                 }
2767                 num_eligible_addr++;
2768         }
2769         return (NULL);
2770 }
2771
2772
2773 static int
2774 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2775     struct sctp_tcb *stcb,
2776     int non_asoc_addr_ok,
2777     uint8_t dest_is_loop,
2778     uint8_t dest_is_priv,
2779     sa_family_t fam)
2780 {
2781         struct sctp_ifa *ifa, *sifa;
2782         int num_eligible_addr = 0;
2783
2784         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2785                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2786                     (non_asoc_addr_ok == 0)) {
2787                         continue;
2788                 }
2789                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2790                     dest_is_priv, fam);
2791                 if (sifa == NULL) {
2792                         continue;
2793                 }
2794                 if (stcb) {
2795                         if (sctp_is_address_in_scope(ifa,
2796                             stcb->asoc.ipv4_addr_legal,
2797                             stcb->asoc.ipv6_addr_legal,
2798                             stcb->asoc.loopback_scope,
2799                             stcb->asoc.ipv4_local_scope,
2800                             stcb->asoc.local_scope,
2801                             stcb->asoc.site_scope, 0) == 0) {
2802                                 continue;
2803                         }
2804                         if (((non_asoc_addr_ok == 0) &&
2805                             (sctp_is_addr_restricted(stcb, sifa))) ||
2806                             (non_asoc_addr_ok &&
2807                             (sctp_is_addr_restricted(stcb, sifa)) &&
2808                             (!sctp_is_addr_pending(stcb, sifa)))) {
2809                                 /*
2810                                  * It is restricted for some reason..
2811                                  * probably not yet added.
2812                                  */
2813                                 continue;
2814                         }
2815                 }
2816                 num_eligible_addr++;
2817         }
2818         return (num_eligible_addr);
2819 }
2820
2821 static struct sctp_ifa *
2822 sctp_choose_boundall(struct sctp_inpcb *inp,
2823     struct sctp_tcb *stcb,
2824     struct sctp_nets *net,
2825     sctp_route_t * ro,
2826     uint32_t vrf_id,
2827     uint8_t dest_is_priv,
2828     uint8_t dest_is_loop,
2829     int non_asoc_addr_ok,
2830     sa_family_t fam)
2831 {
2832         int cur_addr_num = 0, num_preferred = 0;
2833         void *ifn;
2834         struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2835         struct sctp_ifa *sctp_ifa, *sifa;
2836         uint32_t ifn_index;
2837         struct sctp_vrf *vrf;
2838
2839         /*-
2840          * For boundall we can use any address in the association.
2841          * If non_asoc_addr_ok is set we can use any address (at least in
2842          * theory). So we look for preferred addresses first. If we find one,
2843          * we use it. Otherwise we next try to get an address on the
2844          * interface, which we should be able to do (unless non_asoc_addr_ok
2845          * is false and we are routed out that way). In these cases where we
2846          * can't use the address of the interface we go through all the
2847          * ifn's looking for an address we can use and fill that in. Punting
2848          * means we send back address 0, which will probably cause problems
2849          * actually since then IP will fill in the address of the route ifn,
2850          * which means we probably already rejected it.. i.e. here comes an
2851          * abort :-<.
2852          */
2853         vrf = sctp_find_vrf(vrf_id);
2854         if (vrf == NULL)
2855                 return (NULL);
2856
2857         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2858         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2859         emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2860         if (sctp_ifn == NULL) {
2861                 /* ?? We don't have this guy ?? */
2862                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2863                 goto bound_all_plan_b;
2864         }
2865         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2866             ifn_index, sctp_ifn->ifn_name);
2867
2868         if (net) {
2869                 cur_addr_num = net->indx_of_eligible_next_to_use;
2870         }
2871         num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2872             stcb,
2873             non_asoc_addr_ok,
2874             dest_is_loop,
2875             dest_is_priv, fam);
2876         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
2877             num_preferred, sctp_ifn->ifn_name);
2878         if (num_preferred == 0) {
2879                 /*
2880                  * no eligible addresses, we must use some other interface
2881                  * address if we can find one.
2882                  */
2883                 goto bound_all_plan_b;
2884         }
2885         /*
2886          * Ok we have num_eligible_addr set with how many we can use, this
2887          * may vary from call to call due to addresses being deprecated
2888          * etc..
2889          */
2890         if (cur_addr_num >= num_preferred) {
2891                 cur_addr_num = 0;
2892         }
2893         /*
2894          * select the nth address from the list (where cur_addr_num is the
2895          * nth) and 0 is the first one, 1 is the second one etc...
2896          */
2897         SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
2898
2899         sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2900             dest_is_priv, cur_addr_num, fam, ro);
2901
2902         /* if sctp_ifa is NULL something changed??, fall to plan b. */
2903         if (sctp_ifa) {
2904                 atomic_add_int(&sctp_ifa->refcount, 1);
2905                 if (net) {
2906                         /* save off where the next one we will want */
2907                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2908                 }
2909                 return (sctp_ifa);
2910         }
2911         /*
2912          * plan_b: Look at all interfaces and find a preferred address. If
2913          * no preferred fall through to plan_c.
2914          */
2915 bound_all_plan_b:
2916         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
2917         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
2918                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
2919                     sctp_ifn->ifn_name);
2920                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
2921                         /* wrong base scope */
2922                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
2923                         continue;
2924                 }
2925                 if ((sctp_ifn == looked_at) && looked_at) {
2926                         /* already looked at this guy */
2927                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
2928                         continue;
2929                 }
2930                 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
2931                     dest_is_loop, dest_is_priv, fam);
2932                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2933                     "Found ifn:%p %d preferred source addresses\n",
2934                     ifn, num_preferred);
2935                 if (num_preferred == 0) {
2936                         /* None on this interface. */
2937                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
2938                         continue;
2939                 }
2940                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
2941                     "num preferred:%d on interface:%p cur_addr_num:%d\n",
2942                     num_preferred, sctp_ifn, cur_addr_num);
2943
2944                 /*
2945                  * Ok we have num_eligible_addr set with how many we can
2946                  * use, this may vary from call to call due to addresses
2947                  * being deprecated etc..
2948                  */
2949                 if (cur_addr_num >= num_preferred) {
2950                         cur_addr_num = 0;
2951                 }
2952                 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
2953                     dest_is_priv, cur_addr_num, fam, ro);
2954                 if (sifa == NULL)
2955                         continue;
2956                 if (net) {
2957                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
2958                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
2959                             cur_addr_num);
2960                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
2961                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
2962                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
2963                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
2964                 }
2965                 atomic_add_int(&sifa->refcount, 1);
2966                 return (sifa);
2967
2968         }
2969
2970         /* plan_c: do we have an acceptable address on the emit interface */
2971         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
2972         if (emit_ifn == NULL) {
2973                 goto plan_d;
2974         }
2975         LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
2976                 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2977                     (non_asoc_addr_ok == 0))
2978                         continue;
2979                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
2980                     dest_is_priv, fam);
2981                 if (sifa == NULL)
2982                         continue;
2983                 if (stcb) {
2984                         if (sctp_is_address_in_scope(sifa,
2985                             stcb->asoc.ipv4_addr_legal,
2986                             stcb->asoc.ipv6_addr_legal,
2987                             stcb->asoc.loopback_scope,
2988                             stcb->asoc.ipv4_local_scope,
2989                             stcb->asoc.local_scope,
2990                             stcb->asoc.site_scope, 0) == 0) {
2991                                 continue;
2992                         }
2993                         if (((non_asoc_addr_ok == 0) &&
2994                             (sctp_is_addr_restricted(stcb, sifa))) ||
2995                             (non_asoc_addr_ok &&
2996                             (sctp_is_addr_restricted(stcb, sifa)) &&
2997                             (!sctp_is_addr_pending(stcb, sifa)))) {
2998                                 /*
2999                                  * It is restricted for some reason..
3000                                  * probably not yet added.
3001                                  */
3002                                 continue;
3003                         }
3004                 }
3005                 atomic_add_int(&sifa->refcount, 1);
3006                 return (sifa);
3007         }
3008 plan_d:
3009         /*
3010          * plan_d: We are in trouble. No preferred address on the emit
3011          * interface. And not even a preferred address on all interfaces. Go
3012          * out and see if we can find an acceptable address somewhere
3013          * amongst all interfaces.
3014          */
3015         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
3016         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3017                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3018                         /* wrong base scope */
3019                         continue;
3020                 }
3021                 if ((sctp_ifn == looked_at) && looked_at)
3022                         /* already looked at this guy */
3023                         continue;
3024
3025                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3026                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3027                             (non_asoc_addr_ok == 0))
3028                                 continue;
3029                         sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3030                             dest_is_loop,
3031                             dest_is_priv, fam);
3032                         if (sifa == NULL)
3033                                 continue;
3034                         if (stcb) {
3035                                 if (sctp_is_address_in_scope(sifa,
3036                                     stcb->asoc.ipv4_addr_legal,
3037                                     stcb->asoc.ipv6_addr_legal,
3038                                     stcb->asoc.loopback_scope,
3039                                     stcb->asoc.ipv4_local_scope,
3040                                     stcb->asoc.local_scope,
3041                                     stcb->asoc.site_scope, 0) == 0) {
3042                                         continue;
3043                                 }
3044                                 if (((non_asoc_addr_ok == 0) &&
3045                                     (sctp_is_addr_restricted(stcb, sifa))) ||
3046                                     (non_asoc_addr_ok &&
3047                                     (sctp_is_addr_restricted(stcb, sifa)) &&
3048                                     (!sctp_is_addr_pending(stcb, sifa)))) {
3049                                         /*
3050                                          * It is restricted for some
3051                                          * reason.. probably not yet added.
3052                                          */
3053                                         continue;
3054                                 }
3055                         }
3056                         atomic_add_int(&sifa->refcount, 1);
3057                         return (sifa);
3058                 }
3059         }
3060         /*
3061          * Ok we can find NO address to source from that is not on our
3062          * restricted list and non_asoc_address is NOT ok, or it is on our
3063          * restricted list. We can't source to it :-(
3064          */
3065         return (NULL);
3066 }
3067
3068
3069
3070 /* tcb may be NULL */
3071 struct sctp_ifa *
3072 sctp_source_address_selection(struct sctp_inpcb *inp,
3073     struct sctp_tcb *stcb,
3074     sctp_route_t * ro,
3075     struct sctp_nets *net,
3076     int non_asoc_addr_ok, uint32_t vrf_id)
3077 {
3078         struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3079
3080 #ifdef INET6
3081         struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3082
3083 #endif
3084         struct sctp_ifa *answer;
3085         uint8_t dest_is_priv, dest_is_loop;
3086         sa_family_t fam;
3087
3088         /*-
3089          * Rules: - Find the route if needed, cache if I can. - Look at
3090          * interface address in route, Is it in the bound list. If so we
3091          * have the best source. - If not we must rotate amongst the
3092          * addresses.
3093          *
3094          * Cavets and issues
3095          *
3096          * Do we need to pay attention to scope. We can have a private address
3097          * or a global address we are sourcing or sending to. So if we draw
3098          * it out
3099          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3100          * For V4
3101          * ------------------------------------------
3102          *      source     *      dest  *  result
3103          * -----------------------------------------
3104          * <a>  Private    *    Global  *  NAT
3105          * -----------------------------------------
3106          * <b>  Private    *    Private *  No problem
3107          * -----------------------------------------
3108          * <c>  Global     *    Private *  Huh, How will this work?
3109          * -----------------------------------------
3110          * <d>  Global     *    Global  *  No Problem
3111          *------------------------------------------
3112          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3113          * For V6
3114          *------------------------------------------
3115          *      source     *      dest  *  result
3116          * -----------------------------------------
3117          * <a>  Linklocal  *    Global  *
3118          * -----------------------------------------
3119          * <b>  Linklocal  * Linklocal  *  No problem
3120          * -----------------------------------------
3121          * <c>  Global     * Linklocal  *  Huh, How will this work?
3122          * -----------------------------------------
3123          * <d>  Global     *    Global  *  No Problem
3124          *------------------------------------------
3125          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3126          *
3127          * And then we add to that what happens if there are multiple addresses
3128          * assigned to an interface. Remember the ifa on a ifn is a linked
3129          * list of addresses. So one interface can have more than one IP
3130          * address. What happens if we have both a private and a global
3131          * address? Do we then use context of destination to sort out which
3132          * one is best? And what about NAT's sending P->G may get you a NAT
3133          * translation, or should you select the G thats on the interface in
3134          * preference.
3135          *
3136          * Decisions:
3137          *
3138          * - count the number of addresses on the interface.
3139          * - if it is one, no problem except case <c>.
3140          *   For <a> we will assume a NAT out there.
3141          * - if there are more than one, then we need to worry about scope P
3142          *   or G. We should prefer G -> G and P -> P if possible.
3143          *   Then as a secondary fall back to mixed types G->P being a last
3144          *   ditch one.
3145          * - The above all works for bound all, but bound specific we need to
3146          *   use the same concept but instead only consider the bound
3147          *   addresses. If the bound set is NOT assigned to the interface then
3148          *   we must use rotation amongst the bound addresses..
3149          */
3150         if (ro->ro_rt == NULL) {
3151                 /*
3152                  * Need a route to cache.
3153                  */
3154                 SCTP_RTALLOC(ro, vrf_id);
3155         }
3156         if (ro->ro_rt == NULL) {
3157                 return (NULL);
3158         }
3159         fam = to->sin_family;
3160         dest_is_priv = dest_is_loop = 0;
3161         /* Setup our scopes for the destination */
3162         switch (fam) {
3163         case AF_INET:
3164                 /* Scope based on outbound address */
3165                 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3166                         dest_is_loop = 1;
3167                         if (net != NULL) {
3168                                 /* mark it as local */
3169                                 net->addr_is_local = 1;
3170                         }
3171                 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3172                         dest_is_priv = 1;
3173                 }
3174                 break;
3175 #ifdef INET6
3176         case AF_INET6:
3177                 /* Scope based on outbound address */
3178                 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3179                     SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3180                         /*
3181                          * If the address is a loopback address, which
3182                          * consists of "::1" OR "fe80::1%lo0", we are
3183                          * loopback scope. But we don't use dest_is_priv
3184                          * (link local addresses).
3185                          */
3186                         dest_is_loop = 1;
3187                         if (net != NULL) {
3188                                 /* mark it as local */
3189                                 net->addr_is_local = 1;
3190                         }
3191                 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3192                         dest_is_priv = 1;
3193                 }
3194                 break;
3195 #endif
3196         }
3197         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3198         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
3199         SCTP_IPI_ADDR_RLOCK();
3200         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3201                 /*
3202                  * Bound all case
3203                  */
3204                 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3205                     dest_is_priv, dest_is_loop,
3206                     non_asoc_addr_ok, fam);
3207                 SCTP_IPI_ADDR_RUNLOCK();
3208                 return (answer);
3209         }
3210         /*
3211          * Subset bound case
3212          */
3213         if (stcb) {
3214                 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
3215                     vrf_id, dest_is_priv,
3216                     dest_is_loop,
3217                     non_asoc_addr_ok, fam);
3218         } else {
3219                 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3220                     non_asoc_addr_ok,
3221                     dest_is_priv,
3222                     dest_is_loop, fam);
3223         }
3224         SCTP_IPI_ADDR_RUNLOCK();
3225         return (answer);
3226 }
3227
3228 static int
3229 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
3230 {
3231         struct cmsghdr cmh;
3232         int tlen, at;
3233
3234         tlen = SCTP_BUF_LEN(control);
3235         at = 0;
3236         /*
3237          * Independent of how many mbufs, find the c_type inside the control
3238          * structure and copy out the data.
3239          */
3240         while (at < tlen) {
3241                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
3242                         /* not enough room for one more we are done. */
3243                         return (0);
3244                 }
3245                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
3246                 if (((int)cmh.cmsg_len + at) > tlen) {
3247                         /*
3248                          * this is real messed up since there is not enough
3249                          * data here to cover the cmsg header. We are done.
3250                          */
3251                         return (0);
3252                 }
3253                 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3254                     (c_type == cmh.cmsg_type)) {
3255                         /* found the one we want, copy it out */
3256                         at += CMSG_ALIGN(sizeof(struct cmsghdr));
3257                         if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
3258                                 /*
3259                                  * space of cmsg_len after header not big
3260                                  * enough
3261                                  */
3262                                 return (0);
3263                         }
3264                         m_copydata(control, at, cpsize, data);
3265                         return (1);
3266                 } else {
3267                         at += CMSG_ALIGN(cmh.cmsg_len);
3268                         if (cmh.cmsg_len == 0) {
3269                                 break;
3270                         }
3271                 }
3272         }
3273         /* not found */
3274         return (0);
3275 }
3276
3277 static struct mbuf *
3278 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
3279     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
3280 {
3281         struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3282         struct sctp_state_cookie *stc;
3283         struct sctp_paramhdr *ph;
3284         uint8_t *foo;
3285         int sig_offset;
3286         uint16_t cookie_sz;
3287
3288         mret = NULL;
3289         mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3290             sizeof(struct sctp_paramhdr)), 0,
3291             M_DONTWAIT, 1, MT_DATA);
3292         if (mret == NULL) {
3293                 return (NULL);
3294         }
3295         copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
3296         if (copy_init == NULL) {
3297                 sctp_m_freem(mret);
3298                 return (NULL);
3299         }
3300 #ifdef SCTP_MBUF_LOGGING
3301         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3302                 struct mbuf *mat;
3303
3304                 mat = copy_init;
3305                 while (mat) {
3306                         if (SCTP_BUF_IS_EXTENDED(mat)) {
3307                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3308                         }
3309                         mat = SCTP_BUF_NEXT(mat);
3310                 }
3311         }
3312 #endif
3313         copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3314             M_DONTWAIT);
3315         if (copy_initack == NULL) {
3316                 sctp_m_freem(mret);
3317                 sctp_m_freem(copy_init);
3318                 return (NULL);
3319         }
3320 #ifdef SCTP_MBUF_LOGGING
3321         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3322                 struct mbuf *mat;
3323
3324                 mat = copy_initack;
3325                 while (mat) {
3326                         if (SCTP_BUF_IS_EXTENDED(mat)) {
3327                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
3328                         }
3329                         mat = SCTP_BUF_NEXT(mat);
3330                 }
3331         }
3332 #endif
3333         /* easy side we just drop it on the end */
3334         ph = mtod(mret, struct sctp_paramhdr *);
3335         SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3336             sizeof(struct sctp_paramhdr);
3337         stc = (struct sctp_state_cookie *)((caddr_t)ph +
3338             sizeof(struct sctp_paramhdr));
3339         ph->param_type = htons(SCTP_STATE_COOKIE);
3340         ph->param_length = 0;   /* fill in at the end */
3341         /* Fill in the stc cookie data */
3342         memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3343
3344         /* tack the INIT and then the INIT-ACK onto the chain */
3345         cookie_sz = 0;
3346         m_at = mret;
3347         for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3348                 cookie_sz += SCTP_BUF_LEN(m_at);
3349                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3350                         SCTP_BUF_NEXT(m_at) = copy_init;
3351                         break;
3352                 }
3353         }
3354
3355         for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3356                 cookie_sz += SCTP_BUF_LEN(m_at);
3357                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3358                         SCTP_BUF_NEXT(m_at) = copy_initack;
3359                         break;
3360                 }
3361         }
3362
3363         for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3364                 cookie_sz += SCTP_BUF_LEN(m_at);
3365                 if (SCTP_BUF_NEXT(m_at) == NULL) {
3366                         break;
3367                 }
3368         }
3369         sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
3370         if (sig == NULL) {
3371                 /* no space, so free the entire chain */
3372                 sctp_m_freem(mret);
3373                 return (NULL);
3374         }
3375         SCTP_BUF_LEN(sig) = 0;
3376         SCTP_BUF_NEXT(m_at) = sig;
3377         sig_offset = 0;
3378         foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
3379         memset(foo, 0, SCTP_SIGNATURE_SIZE);
3380         *signature = foo;
3381         SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
3382         cookie_sz += SCTP_SIGNATURE_SIZE;
3383         ph->param_length = htons(cookie_sz);
3384         return (mret);
3385 }
3386
3387
3388 static uint8_t
3389 sctp_get_ect(struct sctp_tcb *stcb,
3390     struct sctp_tmit_chunk *chk)
3391 {
3392         uint8_t this_random;
3393
3394         /* Huh? */
3395         if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
3396                 return (0);
3397
3398         if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
3399                 /* no nonce, always return ECT0 */
3400                 return (SCTP_ECT0_BIT);
3401
3402         if (stcb->asoc.peer_supports_ecn_nonce == 0) {
3403                 /* Peer does NOT support it, so we send a ECT0 only */
3404                 return (SCTP_ECT0_BIT);
3405         }
3406         if (chk == NULL)
3407                 return (SCTP_ECT0_BIT);
3408
3409         if ((stcb->asoc.hb_random_idx > 3) ||
3410             ((stcb->asoc.hb_random_idx == 3) &&
3411             (stcb->asoc.hb_ect_randombit > 7))) {
3412                 uint32_t rndval;
3413
3414 warp_drive_sa:
3415                 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
3416                 memcpy(stcb->asoc.hb_random_values, &rndval,
3417                     sizeof(stcb->asoc.hb_random_values));
3418                 this_random = stcb->asoc.hb_random_values[0];
3419                 stcb->asoc.hb_random_idx = 0;
3420                 stcb->asoc.hb_ect_randombit = 0;
3421         } else {
3422                 if (stcb->asoc.hb_ect_randombit > 7) {
3423                         stcb->asoc.hb_ect_randombit = 0;
3424                         stcb->asoc.hb_random_idx++;
3425                         if (stcb->asoc.hb_random_idx > 3) {
3426                                 goto warp_drive_sa;
3427                         }
3428                 }
3429                 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
3430         }
3431         if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
3432                 if (chk != NULL)
3433                         /* ECN Nonce stuff */
3434                         chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
3435                 stcb->asoc.hb_ect_randombit++;
3436                 return (SCTP_ECT1_BIT);
3437         } else {
3438                 stcb->asoc.hb_ect_randombit++;
3439                 return (SCTP_ECT0_BIT);
3440         }
3441 }
3442
3443 static int
3444 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3445     struct sctp_tcb *stcb,      /* may be NULL */
3446     struct sctp_nets *net,
3447     struct sockaddr *to,
3448     struct mbuf *m,
3449     uint32_t auth_offset,
3450     struct sctp_auth_chunk *auth,
3451     uint16_t auth_keyid,
3452     int nofragment_flag,
3453     int ecn_ok,
3454     struct sctp_tmit_chunk *chk,
3455     int out_of_asoc_ok,
3456     uint16_t src_port,
3457     uint16_t dest_port,
3458     uint32_t v_tag,
3459     uint16_t port,
3460     int so_locked,
3461 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
3462     SCTP_UNUSED
3463 #endif
3464     union sctp_sockstore *over_addr
3465 )
3466 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3467 {
3468         /*
3469          * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
3470          * header WITH an SCTPHDR but no IP header, endpoint inp and sa
3471          * structure: - fill in the HMAC digest of any AUTH chunk in the
3472          * packet. - calculate and fill in the SCTP checksum. - prepend an
3473          * IP address header. - if boundall use INADDR_ANY. - if
3474          * boundspecific do source address selection. - set fragmentation
3475          * option for ipV4. - On return from IP output, check/adjust mtu
3476          * size of output interface and smallest_mtu size as well.
3477          */
3478         /* Will need ifdefs around this */
3479         struct mbuf *o_pak;
3480         struct mbuf *newm;
3481         struct sctphdr *sctphdr;
3482         int packet_length;
3483         int ret;
3484         uint32_t vrf_id;
3485         sctp_route_t *ro = NULL;
3486         struct udphdr *udp = NULL;
3487
3488 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3489         struct socket *so = NULL;
3490
3491 #endif
3492
3493         if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
3494                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
3495                 sctp_m_freem(m);
3496                 return (EFAULT);
3497         }
3498         if (stcb) {
3499                 vrf_id = stcb->asoc.vrf_id;
3500         } else {
3501                 vrf_id = inp->def_vrf_id;
3502         }
3503
3504         /* fill in the HMAC digest for any AUTH chunk in the packet */
3505         if ((auth != NULL) && (stcb != NULL)) {
3506                 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
3507         }
3508         if (to->sa_family == AF_INET) {
3509                 struct ip *ip = NULL;
3510                 sctp_route_t iproute;
3511                 uint8_t tos_value;
3512                 int len;
3513
3514                 len = sizeof(struct ip) + sizeof(struct sctphdr);
3515                 if (port) {
3516                         len += sizeof(struct udphdr);
3517                 }
3518                 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3519                 if (newm == NULL) {
3520                         sctp_m_freem(m);
3521                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3522                         return (ENOMEM);
3523                 }
3524                 SCTP_ALIGN_TO_END(newm, len);
3525                 SCTP_BUF_LEN(newm) = len;
3526                 SCTP_BUF_NEXT(newm) = m;
3527                 m = newm;
3528                 packet_length = sctp_calculate_len(m);
3529                 ip = mtod(m, struct ip *);
3530                 ip->ip_v = IPVERSION;
3531                 ip->ip_hl = (sizeof(struct ip) >> 2);
3532                 if (net) {
3533                         tos_value = net->tos_flowlabel & 0x000000ff;
3534                 } else {
3535                         tos_value = inp->ip_inp.inp.inp_ip_tos;
3536                 }
3537                 if ((nofragment_flag) && (port == 0)) {
3538                         ip->ip_off = IP_DF;
3539                 } else
3540                         ip->ip_off = 0;
3541
3542                 /* FreeBSD has a function for ip_id's */
3543                 ip->ip_id = ip_newid();
3544
3545                 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
3546                 ip->ip_len = packet_length;
3547                 if (stcb) {
3548                         if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3549                                 /* Enable ECN */
3550                                 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
3551                         } else {
3552                                 /* No ECN */
3553                                 ip->ip_tos = (u_char)(tos_value & 0xfc);
3554                         }
3555                 } else {
3556                         /* no association at all */
3557                         ip->ip_tos = (tos_value & 0xfc);
3558                 }
3559                 if (port) {
3560                         ip->ip_p = IPPROTO_UDP;
3561                 } else {
3562                         ip->ip_p = IPPROTO_SCTP;
3563                 }
3564                 ip->ip_sum = 0;
3565                 if (net == NULL) {
3566                         ro = &iproute;
3567                         memset(&iproute, 0, sizeof(iproute));
3568                         memcpy(&ro->ro_dst, to, to->sa_len);
3569                 } else {
3570                         ro = (sctp_route_t *) & net->ro;
3571                 }
3572                 /* Now the address selection part */
3573                 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
3574
3575                 /* call the routine to select the src address */
3576                 if (net && out_of_asoc_ok == 0) {
3577                         if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3578                                 sctp_free_ifa(net->ro._s_addr);
3579                                 net->ro._s_addr = NULL;
3580                                 net->src_addr_selected = 0;
3581                                 if (ro->ro_rt) {
3582                                         RTFREE(ro->ro_rt);
3583                                         ro->ro_rt = NULL;
3584                                 }
3585                         }
3586                         if (net->src_addr_selected == 0) {
3587                                 /* Cache the source address */
3588                                 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
3589                                     ro, net, 0,
3590                                     vrf_id);
3591                                 net->src_addr_selected = 1;
3592                         }
3593                         if (net->ro._s_addr == NULL) {
3594                                 /* No route to host */
3595                                 net->src_addr_selected = 0;
3596                                 goto no_route;
3597                         }
3598                         ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
3599                 } else {
3600                         if (over_addr == NULL) {
3601                                 struct sctp_ifa *_lsrc;
3602
3603                                 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3604                                     net,
3605                                     out_of_asoc_ok,
3606                                     vrf_id);
3607                                 if (_lsrc == NULL) {
3608                                         goto no_route;
3609                                 }
3610                                 ip->ip_src = _lsrc->address.sin.sin_addr;
3611                                 sctp_free_ifa(_lsrc);
3612                         } else {
3613                                 ip->ip_src = over_addr->sin.sin_addr;
3614                                 SCTP_RTALLOC(ro, vrf_id);
3615                         }
3616                 }
3617                 if (port) {
3618                         udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
3619                         udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
3620                         udp->uh_dport = port;
3621                         udp->uh_ulen = htons(packet_length - sizeof(struct ip));
3622                         udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
3623                         sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
3624                 } else {
3625                         sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
3626                 }
3627
3628                 sctphdr->src_port = src_port;
3629                 sctphdr->dest_port = dest_port;
3630                 sctphdr->v_tag = v_tag;
3631                 sctphdr->checksum = 0;
3632
3633                 /*
3634                  * If source address selection fails and we find no route
3635                  * then the ip_output should fail as well with a
3636                  * NO_ROUTE_TO_HOST type error. We probably should catch
3637                  * that somewhere and abort the association right away
3638                  * (assuming this is an INIT being sent).
3639                  */
3640                 if ((ro->ro_rt == NULL)) {
3641                         /*
3642                          * src addr selection failed to find a route (or
3643                          * valid source addr), so we can't get there from
3644                          * here (yet)!
3645                          */
3646         no_route:
3647                         SCTPDBG(SCTP_DEBUG_OUTPUT1,
3648                             "%s: dropped packet - no valid source addr\n",
3649                             __FUNCTION__);
3650                         if (net) {
3651                                 SCTPDBG(SCTP_DEBUG_OUTPUT1,
3652                                     "Destination was ");
3653                                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
3654                                     &net->ro._l_addr.sa);
3655                                 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3656                                         if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3657                                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
3658                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3659                                                     stcb,
3660                                                     SCTP_FAILED_THRESHOLD,
3661                                                     (void *)net,
3662                                                     so_locked);
3663                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3664                                                 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
3665                                                 /*
3666                                                  * JRS 5/14/07 - If a
3667                                                  * destination is
3668                                                  * unreachable, the PF bit
3669                                                  * is turned off.  This
3670                                                  * allows an unambiguous use
3671                                                  * of the PF bit for
3672                                                  * destinations that are
3673                                                  * reachable but potentially
3674                                                  * failed. If the
3675                                                  * destination is set to the
3676                                                  * unreachable state, also
3677                                                  * set the destination to
3678                                                  * the PF state.
3679                                                  */
3680                                                 /*
3681                                                  * Add debug message here if
3682                                                  * destination is not in PF
3683                                                  * state.
3684                                                  */
3685                                                 /*
3686                                                  * Stop any running T3
3687                                                  * timers here?
3688                                                  */
3689                                                 if ((stcb->asoc.sctp_cmt_on_off == 1) &&
3690                                                     (stcb->asoc.sctp_cmt_pf > 0)) {
3691                                                         net->dest_state &= ~SCTP_ADDR_PF;
3692                                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
3693                                                             net);
3694                                                 }
3695                                         }
3696                                 }
3697                                 if (stcb) {
3698                                         if (net == stcb->asoc.primary_destination) {
3699                                                 /* need a new primary */
3700                                                 struct sctp_nets *alt;
3701
3702                                                 alt = sctp_find_alternate_net(stcb, net, 0);
3703                                                 if (alt != net) {
3704                                                         if (sctp_set_primary_addr(stcb,
3705                                                             (struct sockaddr *)NULL,
3706                                                             alt) == 0) {
3707                                                                 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
3708                                                                 if (net->ro._s_addr) {
3709                                                                         sctp_free_ifa(net->ro._s_addr);
3710                                                                         net->ro._s_addr = NULL;
3711                                                                 }
3712                                                                 net->src_addr_selected = 0;
3713                                                         }
3714                                                 }
3715                                         }
3716                                 }
3717                         }
3718                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
3719                         sctp_m_freem(m);
3720                         return (EHOSTUNREACH);
3721                 }
3722                 if (ro != &iproute) {
3723                         memcpy(&iproute, ro, sizeof(*ro));
3724                 }
3725                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
3726                     (uint32_t) (ntohl(ip->ip_src.s_addr)));
3727                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
3728                     (uint32_t) (ntohl(ip->ip_dst.s_addr)));
3729                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
3730                     ro->ro_rt);
3731
3732                 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
3733                         /* failed to prepend data, give up */
3734                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3735                         sctp_m_freem(m);
3736                         return (ENOMEM);
3737                 }
3738 #ifdef  SCTP_PACKET_LOGGING
3739                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
3740                         sctp_packet_log(m, packet_length);
3741 #endif
3742                 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
3743                 if (port) {
3744                         if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3745                             (stcb) &&
3746                             (stcb->asoc.loopback_scope))) {
3747                                 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
3748                                 SCTP_STAT_INCR(sctps_sendswcrc);
3749                         } else {
3750                                 SCTP_STAT_INCR(sctps_sendnocrc);
3751                         }
3752                         SCTP_ENABLE_UDP_CSUM(o_pak);
3753                 } else {
3754                         if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
3755                             (stcb) &&
3756                             (stcb->asoc.loopback_scope))) {
3757                                 m->m_pkthdr.csum_flags = CSUM_SCTP;
3758                                 m->m_pkthdr.csum_data = 0;
3759                                 SCTP_STAT_INCR(sctps_sendhwcrc);
3760                         } else {
3761                                 SCTP_STAT_INCR(sctps_sendnocrc);
3762                         }
3763                 }
3764                 /* send it out.  table id is taken from stcb */
3765 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3766                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3767                         so = SCTP_INP_SO(inp);
3768                         SCTP_SOCKET_UNLOCK(so, 0);
3769                 }
3770 #endif
3771                 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
3772 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
3773                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
3774                         atomic_add_int(&stcb->asoc.refcnt, 1);
3775                         SCTP_TCB_UNLOCK(stcb);
3776                         SCTP_SOCKET_LOCK(so, 0);
3777                         SCTP_TCB_LOCK(stcb);
3778                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
3779                 }
3780 #endif
3781                 SCTP_STAT_INCR(sctps_sendpackets);
3782                 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
3783                 if (ret)
3784                         SCTP_STAT_INCR(sctps_senderrors);
3785
3786                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
3787                 if (net == NULL) {
3788                         /* free tempy routes */
3789                         if (ro->ro_rt) {
3790                                 RTFREE(ro->ro_rt);
3791                                 ro->ro_rt = NULL;
3792                         }
3793                 } else {
3794                         /* PMTU check versus smallest asoc MTU goes here */
3795                         if ((ro->ro_rt != NULL) &&
3796                             (net->ro._s_addr)) {
3797                                 uint32_t mtu;
3798
3799                                 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
3800                                 if (net->port) {
3801                                         mtu -= sizeof(struct udphdr);
3802                                 }
3803                                 if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
3804 #ifdef SCTP_PRINT_FOR_B_AND_M
3805                                         SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
3806 #endif
3807                                         sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
3808                                         net->mtu = mtu;
3809                                 }
3810                         } else if (ro->ro_rt == NULL) {
3811                                 /* route was freed */
3812                                 if (net->ro._s_addr &&
3813                                     net->src_addr_selected) {
3814                                         sctp_free_ifa(net->ro._s_addr);
3815                                         net->ro._s_addr = NULL;
3816                                 }
3817                                 net->src_addr_selected = 0;
3818                         }
3819                 }
3820                 return (ret);
3821         }
3822 #ifdef INET6
3823         else if (to->sa_family == AF_INET6) {
3824                 uint32_t flowlabel;
3825                 struct ip6_hdr *ip6h;
3826                 struct route_in6 ip6route;
3827                 struct ifnet *ifp;
3828                 u_char flowTop;
3829                 uint16_t flowBottom;
3830                 u_char tosBottom, tosTop;
3831                 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
3832                 int prev_scope = 0;
3833                 struct sockaddr_in6 lsa6_storage;
3834                 int error;
3835                 u_short prev_port = 0;
3836                 int len;
3837
3838                 if (net != NULL) {
3839                         flowlabel = net->tos_flowlabel;
3840                 } else {
3841                         flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
3842                 }
3843
3844                 len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr);
3845                 if (port) {
3846                         len += sizeof(struct udphdr);
3847                 }
3848                 newm = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
3849                 if (newm == NULL) {
3850                         sctp_m_freem(m);
3851                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
3852                         return (ENOMEM);
3853                 }
3854                 SCTP_ALIGN_TO_END(newm, len);
3855                 SCTP_BUF_LEN(newm) = len;
3856                 SCTP_BUF_NEXT(newm) = m;
3857                 m = newm;
3858                 packet_length = sctp_calculate_len(m);
3859
3860                 ip6h = mtod(m, struct ip6_hdr *);
3861                 /*
3862                  * We assume here that inp_flow is in host byte order within
3863                  * the TCB!
3864                  */
3865                 flowBottom = flowlabel & 0x0000ffff;
3866                 flowTop = ((flowlabel & 0x000f0000) >> 16);
3867                 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
3868                 /* protect *sin6 from overwrite */
3869                 sin6 = (struct sockaddr_in6 *)to;
3870                 tmp = *sin6;
3871                 sin6 = &tmp;
3872
3873                 /* KAME hack: embed scopeid */
3874                 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3875                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3876                         return (EINVAL);
3877                 }
3878                 if (net == NULL) {
3879                         memset(&ip6route, 0, sizeof(ip6route));
3880                         ro = (sctp_route_t *) & ip6route;
3881                         memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
3882                 } else {
3883                         ro = (sctp_route_t *) & net->ro;
3884                 }
3885                 if (stcb != NULL) {
3886                         if ((stcb->asoc.ecn_allowed) && ecn_ok) {
3887                                 /* Enable ECN */
3888                                 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
3889                         } else {
3890                                 /* No ECN */
3891                                 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3892                         }
3893                 } else {
3894                         /* we could get no asoc if it is a O-O-T-B packet */
3895                         tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
3896                 }
3897                 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
3898                 if (port) {
3899                         ip6h->ip6_nxt = IPPROTO_UDP;
3900                 } else {
3901                         ip6h->ip6_nxt = IPPROTO_SCTP;
3902                 }
3903                 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
3904                 ip6h->ip6_dst = sin6->sin6_addr;
3905
3906                 /*
3907                  * Add SRC address selection here: we can only reuse to a
3908                  * limited degree the kame src-addr-sel, since we can try
3909                  * their selection but it may not be bound.
3910                  */
3911                 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
3912                 lsa6_tmp.sin6_family = AF_INET6;
3913                 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
3914                 lsa6 = &lsa6_tmp;
3915                 if (net && out_of_asoc_ok == 0) {
3916                         if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
3917                                 sctp_free_ifa(net->ro._s_addr);
3918                                 net->ro._s_addr = NULL;
3919                                 net->src_addr_selected = 0;
3920                                 if (ro->ro_rt) {
3921                                         RTFREE(ro->ro_rt);
3922                                         ro->ro_rt = NULL;
3923                                 }
3924                         }
3925                         if (net->src_addr_selected == 0) {
3926                                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
3927                                 /* KAME hack: embed scopeid */
3928                                 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3929                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3930                                         return (EINVAL);
3931                                 }
3932                                 /* Cache the source address */
3933                                 net->ro._s_addr = sctp_source_address_selection(inp,
3934                                     stcb,
3935                                     ro,
3936                                     net,
3937                                     0,
3938                                     vrf_id);
3939                                 (void)sa6_recoverscope(sin6);
3940                                 net->src_addr_selected = 1;
3941                         }
3942                         if (net->ro._s_addr == NULL) {
3943                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
3944                                 net->src_addr_selected = 0;
3945                                 goto no_route;
3946                         }
3947                         lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
3948                 } else {
3949                         sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
3950                         /* KAME hack: embed scopeid */
3951                         if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3952                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
3953                                 return (EINVAL);
3954                         }
3955                         if (over_addr == NULL) {
3956                                 struct sctp_ifa *_lsrc;
3957
3958                                 _lsrc = sctp_source_address_selection(inp, stcb, ro,
3959                                     net,
3960                                     out_of_asoc_ok,
3961                                     vrf_id);
3962                                 if (_lsrc == NULL) {
3963                                         goto no_route;
3964                                 }
3965                                 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
3966                                 sctp_free_ifa(_lsrc);
3967                         } else {
3968                                 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
3969                                 SCTP_RTALLOC(ro, vrf_id);
3970                         }
3971                         (void)sa6_recoverscope(sin6);
3972                 }
3973                 lsa6->sin6_port = inp->sctp_lport;
3974
3975                 if (ro->ro_rt == NULL) {
3976                         /*
3977                          * src addr selection failed to find a route (or
3978                          * valid source addr), so we can't get there from
3979                          * here!
3980                          */
3981                         goto no_route;
3982                 }
3983                 /*
3984                  * XXX: sa6 may not have a valid sin6_scope_id in the
3985                  * non-SCOPEDROUTING case.
3986                  */
3987                 bzero(&lsa6_storage, sizeof(lsa6_storage));
3988                 lsa6_storage.sin6_family = AF_INET6;
3989                 lsa6_storage.sin6_len = sizeof(lsa6_storage);
3990                 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3991                 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
3992                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
3993                         sctp_m_freem(m);
3994                         return (error);
3995                 }
3996                 /* XXX */
3997                 lsa6_storage.sin6_addr = lsa6->sin6_addr;
3998                 lsa6_storage.sin6_port = inp->sctp_lport;
3999                 lsa6 = &lsa6_storage;
4000                 ip6h->ip6_src = lsa6->sin6_addr;
4001
4002                 if (port) {
4003                         udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4004                         udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4005                         udp->uh_dport = port;
4006                         udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
4007                         udp->uh_sum = 0;
4008                         sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4009                 } else {
4010                         sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4011                 }
4012
4013                 sctphdr->src_port = src_port;
4014                 sctphdr->dest_port = dest_port;
4015                 sctphdr->v_tag = v_tag;
4016                 sctphdr->checksum = 0;
4017
4018                 /*
4019                  * We set the hop limit now since there is a good chance
4020                  * that our ro pointer is now filled
4021                  */
4022                 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4023                 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4024
4025 #ifdef SCTP_DEBUG
4026                 /* Copy to be sure something bad is not happening */
4027                 sin6->sin6_addr = ip6h->ip6_dst;
4028                 lsa6->sin6_addr = ip6h->ip6_src;
4029 #endif
4030
4031                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4032                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4033                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4034                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4035                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4036                 if (net) {
4037                         sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4038                         /* preserve the port and scope for link local send */
4039                         prev_scope = sin6->sin6_scope_id;
4040                         prev_port = sin6->sin6_port;
4041                 }
4042                 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4043                         /* failed to prepend data, give up */
4044                         sctp_m_freem(m);
4045                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4046                         return (ENOMEM);
4047                 }
4048 #ifdef  SCTP_PACKET_LOGGING
4049                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4050                         sctp_packet_log(m, packet_length);
4051 #endif
4052                 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4053                 if (port) {
4054                         if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4055                             (stcb) &&
4056                             (stcb->asoc.loopback_scope))) {
4057                                 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4058                                 SCTP_STAT_INCR(sctps_sendswcrc);
4059                         } else {
4060                                 SCTP_STAT_INCR(sctps_sendnocrc);
4061                         }
4062                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4063                                 udp->uh_sum = 0xffff;
4064                         }
4065                 } else {
4066                         if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4067                             (stcb) &&
4068                             (stcb->asoc.loopback_scope))) {
4069                                 m->m_pkthdr.csum_flags = CSUM_SCTP;
4070                                 m->m_pkthdr.csum_data = 0;
4071                                 SCTP_STAT_INCR(sctps_sendhwcrc);
4072                         } else {
4073                                 SCTP_STAT_INCR(sctps_sendnocrc);
4074                         }
4075                 }
4076                 /* send it out. table id is taken from stcb */
4077 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4078                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4079                         so = SCTP_INP_SO(inp);
4080                         SCTP_SOCKET_UNLOCK(so, 0);
4081                 }
4082 #endif
4083                 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
4084 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
4085                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4086                         atomic_add_int(&stcb->asoc.refcnt, 1);
4087                         SCTP_TCB_UNLOCK(stcb);
4088                         SCTP_SOCKET_LOCK(so, 0);
4089                         SCTP_TCB_LOCK(stcb);
4090                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
4091                 }
4092 #endif
4093                 if (net) {
4094                         /* for link local this must be done */
4095                         sin6->sin6_scope_id = prev_scope;
4096                         sin6->sin6_port = prev_port;
4097                 }
4098                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4099                 SCTP_STAT_INCR(sctps_sendpackets);
4100                 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4101                 if (ret) {
4102                         SCTP_STAT_INCR(sctps_senderrors);
4103                 }
4104                 if (net == NULL) {
4105                         /* Now if we had a temp route free it */
4106                         if (ro->ro_rt) {
4107                                 RTFREE(ro->ro_rt);
4108                         }
4109                 } else {
4110                         /* PMTU check versus smallest asoc MTU goes here */
4111                         if (ro->ro_rt == NULL) {
4112                                 /* Route was freed */
4113                                 if (net->ro._s_addr &&
4114                                     net->src_addr_selected) {
4115                                         sctp_free_ifa(net->ro._s_addr);
4116                                         net->ro._s_addr = NULL;
4117                                 }
4118                                 net->src_addr_selected = 0;
4119                         }
4120                         if ((ro->ro_rt != NULL) &&
4121                             (net->ro._s_addr)) {
4122                                 uint32_t mtu;
4123
4124                                 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4125                                 if (mtu &&
4126                                     (stcb->asoc.smallest_mtu > mtu)) {
4127 #ifdef SCTP_PRINT_FOR_B_AND_M
4128                                         SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
4129                                             mtu);
4130 #endif
4131                                         sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
4132                                         net->mtu = mtu;
4133                                         if (net->port) {
4134                                                 net->mtu -= sizeof(struct udphdr);
4135                                         }
4136                                 }
4137                         } else if (ifp) {
4138                                 if (ND_IFINFO(ifp)->linkmtu &&
4139                                     (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4140 #ifdef SCTP_PRINT_FOR_B_AND_M
4141                                         SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
4142                                             ND_IFINFO(ifp)->linkmtu);
4143 #endif
4144                                         sctp_mtu_size_reset(inp,
4145                                             &stcb->asoc,
4146                                             ND_IFINFO(ifp)->linkmtu);
4147                                 }
4148                         }
4149                 }
4150                 return (ret);
4151         }
4152 #endif
4153         else {
4154                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4155                     ((struct sockaddr *)to)->sa_family);
4156                 sctp_m_freem(m);
4157                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4158                 return (EFAULT);
4159         }
4160 }
4161
4162
4163 void
4164 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
4165 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
4166     SCTP_UNUSED
4167 #endif
4168 )
4169 {
4170         struct mbuf *m, *m_at, *mp_last;
4171         struct sctp_nets *net;
4172         struct sctp_init_chunk *init;
4173         struct sctp_supported_addr_param *sup_addr;
4174         struct sctp_adaptation_layer_indication *ali;
4175         struct sctp_ecn_supported_param *ecn;
4176         struct sctp_prsctp_supported_param *prsctp;
4177         struct sctp_ecn_nonce_supported_param *ecn_nonce;
4178         struct sctp_supported_chunk_types_param *pr_supported;
4179         int cnt_inits_to = 0;
4180         int padval, ret;
4181         int num_ext;
4182         int p_len;
4183
4184         /* INIT's always go to the primary (and usually ONLY address) */
4185         mp_last = NULL;
4186         net = stcb->asoc.primary_destination;
4187         if (net == NULL) {
4188                 net = TAILQ_FIRST(&stcb->asoc.nets);
4189                 if (net == NULL) {
4190                         /* TSNH */
4191                         return;
4192                 }
4193                 /* we confirm any address we send an INIT to */
4194                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4195                 (void)sctp_set_primary_addr(stcb, NULL, net);
4196         } else {
4197                 /* we confirm any address we send an INIT to */
4198                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4199         }
4200         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4201 #ifdef INET6
4202         if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
4203                 /*
4204                  * special hook, if we are sending to link local it will not
4205                  * show up in our private address count.
4206                  */
4207                 struct sockaddr_in6 *sin6l;
4208
4209                 sin6l = &net->ro._l_addr.sin6;
4210                 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
4211                         cnt_inits_to = 1;
4212         }
4213 #endif
4214         if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4215                 /* This case should not happen */
4216                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4217                 return;
4218         }
4219         /* start the INIT timer */
4220         sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4221
4222         m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
4223         if (m == NULL) {
4224                 /* No memory, INIT timer will re-attempt. */
4225                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4226                 return;
4227         }
4228         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
4229         /*
4230          * assume peer supports asconf in order to be able to queue local
4231          * address changes while an INIT is in flight and before the assoc
4232          * is established.
4233          */
4234         stcb->asoc.peer_supports_asconf = 1;
4235         /* Now lets put the SCTP header in place */
4236         init = mtod(m, struct sctp_init_chunk *);
4237         /* now the chunk header */
4238         init->ch.chunk_type = SCTP_INITIATION;
4239         init->ch.chunk_flags = 0;
4240         /* fill in later from mbuf we build */
4241         init->ch.chunk_length = 0;
4242         /* place in my tag */
4243         init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4244         /* set up some of the credits. */
4245         init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4246             SCTP_MINIMAL_RWND));
4247
4248         init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4249         init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4250         init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4251         /* now the address restriction */
4252         sup_addr = (struct sctp_supported_addr_param *)((caddr_t)init +
4253             sizeof(*init));
4254         sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4255 #ifdef INET6
4256         /* we support 2 types: IPv6/IPv4 */
4257         sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
4258         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4259         sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
4260 #else
4261         /* we support 1 type: IPv4 */
4262         sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
4263         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
4264         sup_addr->addr_type[1] = htons(0);      /* this is the padding */
4265 #endif
4266         SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
4267         /* adaptation layer indication parameter */
4268         ali = (struct sctp_adaptation_layer_indication *)((caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
4269         ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4270         ali->ph.param_length = htons(sizeof(*ali));
4271         ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
4272         SCTP_BUF_LEN(m) += sizeof(*ali);
4273         ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
4274
4275         if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4276                 /* Add NAT friendly parameter */
4277                 struct sctp_paramhdr *ph;
4278
4279                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4280                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4281                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
4282                 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
4283                 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ph + sizeof(*ph));
4284         }
4285         /* now any cookie time extensions */
4286         if (stcb->asoc.cookie_preserve_req) {
4287                 struct sctp_cookie_perserve_param *cookie_preserve;
4288
4289                 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
4290                 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4291                 cookie_preserve->ph.param_length = htons(
4292                     sizeof(*cookie_preserve));
4293                 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4294                 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
4295                 ecn = (struct sctp_ecn_supported_param *)(
4296                     (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
4297                 stcb->asoc.cookie_preserve_req = 0;
4298         }
4299         /* ECN parameter */
4300         if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
4301                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
4302                 ecn->ph.param_length = htons(sizeof(*ecn));
4303                 SCTP_BUF_LEN(m) += sizeof(*ecn);
4304                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
4305                     sizeof(*ecn));
4306         } else {
4307                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
4308         }
4309         /* And now tell the peer we do pr-sctp */
4310         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
4311         prsctp->ph.param_length = htons(sizeof(*prsctp));
4312         SCTP_BUF_LEN(m) += sizeof(*prsctp);
4313
4314         /* And now tell the peer we do all the extensions */
4315         pr_supported = (struct sctp_supported_chunk_types_param *)
4316             ((caddr_t)prsctp + sizeof(*prsctp));
4317         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4318         num_ext = 0;
4319         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4320         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4321         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4322         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4323         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4324         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4325                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4326         }
4327         if (stcb->asoc.sctp_nr_sack_on_off == 1) {
4328                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4329         }
4330         p_len = sizeof(*pr_supported) + num_ext;
4331         pr_supported->ph.param_length = htons(p_len);
4332         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
4333         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4334
4335
4336         /* ECN nonce: And now tell the peer we support ECN nonce */
4337         if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
4338                 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
4339                     ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
4340                 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
4341                 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
4342                 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
4343         }
4344         /* add authentication parameters */
4345         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
4346                 struct sctp_auth_random *randp;
4347                 struct sctp_auth_hmac_algo *hmacs;
4348                 struct sctp_auth_chunk_list *chunks;
4349
4350                 /* attach RANDOM parameter, if available */
4351                 if (stcb->asoc.authinfo.random != NULL) {
4352                         randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4353                         p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
4354                         /* random key already contains the header */
4355                         bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
4356                         /* zero out any padding required */
4357                         bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
4358                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4359                 }
4360                 /* add HMAC_ALGO parameter */
4361                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4362                 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
4363                     (uint8_t *) hmacs->hmac_ids);
4364                 if (p_len > 0) {
4365                         p_len += sizeof(*hmacs);
4366                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4367                         hmacs->ph.param_length = htons(p_len);
4368                         /* zero out any padding required */
4369                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
4370                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4371                 }
4372                 /* add CHUNKS parameter */
4373                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
4374                 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
4375                     chunks->chunk_types);
4376                 if (p_len > 0) {
4377                         p_len += sizeof(*chunks);
4378                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4379                         chunks->ph.param_length = htons(p_len);
4380                         /* zero out any padding required */
4381                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
4382                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
4383                 }
4384         }
4385         m_at = m;
4386         /* now the addresses */
4387         {
4388                 struct sctp_scoping scp;
4389
4390                 /*
4391                  * To optimize this we could put the scoping stuff into a
4392                  * structure and remove the individual uint8's from the
4393                  * assoc structure. Then we could just sifa in the address
4394                  * within the stcb.. but for now this is a quick hack to get
4395                  * the address stuff teased apart.
4396                  */
4397                 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
4398                 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
4399                 scp.loopback_scope = stcb->asoc.loopback_scope;
4400                 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
4401                 scp.local_scope = stcb->asoc.local_scope;
4402                 scp.site_scope = stcb->asoc.site_scope;
4403
4404                 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
4405         }
4406
4407         /* calulate the size and update pkt header and chunk header */
4408         p_len = 0;
4409         for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
4410                 if (SCTP_BUF_NEXT(m_at) == NULL)
4411                         mp_last = m_at;
4412                 p_len += SCTP_BUF_LEN(m_at);
4413         }
4414         init->ch.chunk_length = htons(p_len);
4415         /*
4416          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
4417          * here since the timer will drive a retranmission.
4418          */
4419
4420         /* I don't expect this to execute but we will be safe here */
4421         padval = p_len % 4;
4422         if ((padval) && (mp_last)) {
4423                 /*
4424                  * The compiler worries that mp_last may not be set even
4425                  * though I think it is impossible :-> however we add
4426                  * mp_last here just in case.
4427                  */
4428                 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
4429                 if (ret) {
4430                         /* Houston we have a problem, no space */
4431                         sctp_m_freem(m);
4432                         return;
4433                 }
4434                 p_len += padval;
4435         }
4436         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4437         ret = sctp_lowlevel_chunk_output(inp, stcb, net,
4438             (struct sockaddr *)&net->ro._l_addr,
4439             m, 0, NULL, 0, 0, 0, NULL, 0,
4440             inp->sctp_lport, stcb->rport, htonl(0),
4441             net->port, so_locked, NULL);
4442         SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
4443         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4444         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4445 }
4446
4447 struct mbuf *
4448 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4449     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
4450 {
4451         /*
4452          * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4453          * being equal to the beginning of the params i.e. (iphlen +
4454          * sizeof(struct sctp_init_msg) parse through the parameters to the
4455          * end of the mbuf verifying that all parameters are known.
4456          * 
4457          * For unknown parameters build and return a mbuf with
4458          * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4459          * processing this chunk stop, and set *abort_processing to 1.
4460          * 
4461          * By having param_offset be pre-set to where parameters begin it is
4462          * hoped that this routine may be reused in the future by new
4463          * features.
4464          */
4465         struct sctp_paramhdr *phdr, params;
4466
4467         struct mbuf *mat, *op_err;
4468         char tempbuf[SCTP_PARAM_BUFFER_SIZE];
4469         int at, limit, pad_needed;
4470         uint16_t ptype, plen, padded_size;
4471         int err_at;
4472
4473         *abort_processing = 0;
4474         mat = in_initpkt;
4475         err_at = 0;
4476         limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4477         at = param_offset;
4478         op_err = NULL;
4479         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4480         phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4481         while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4482                 ptype = ntohs(phdr->param_type);
4483                 plen = ntohs(phdr->param_length);
4484                 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4485                         /* wacked parameter */
4486                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4487                         goto invalid_size;
4488                 }
4489                 limit -= SCTP_SIZE32(plen);
4490                 /*-
4491                  * All parameters for all chunks that we know/understand are
4492                  * listed here. We process them other places and make
4493                  * appropriate stop actions per the upper bits. However this
4494                  * is the generic routine processor's can call to get back
4495                  * an operr.. to either incorporate (init-ack) or send.
4496                  */
4497                 padded_size = SCTP_SIZE32(plen);
4498                 switch (ptype) {
4499                         /* Param's with variable size */
4500                 case SCTP_HEARTBEAT_INFO:
4501                 case SCTP_STATE_COOKIE:
4502                 case SCTP_UNRECOG_PARAM:
4503                 case SCTP_ERROR_CAUSE_IND:
4504                         /* ok skip fwd */
4505                         at += padded_size;
4506                         break;
4507                         /* Param's with variable size within a range */
4508                 case SCTP_CHUNK_LIST:
4509                 case SCTP_SUPPORTED_CHUNK_EXT:
4510                         if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4511                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4512                                 goto invalid_size;
4513                         }
4514                         at += padded_size;
4515                         break;
4516                 case SCTP_SUPPORTED_ADDRTYPE:
4517                         if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4518                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4519                                 goto invalid_size;
4520                         }
4521                         at += padded_size;
4522                         break;
4523                 case SCTP_RANDOM:
4524                         if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4525                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4526                                 goto invalid_size;
4527                         }
4528                         at += padded_size;
4529                         break;
4530                 case SCTP_SET_PRIM_ADDR:
4531                 case SCTP_DEL_IP_ADDRESS:
4532                 case SCTP_ADD_IP_ADDRESS:
4533                         if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4534                             (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4535                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4536                                 goto invalid_size;
4537                         }
4538                         at += padded_size;
4539                         break;
4540                         /* Param's with a fixed size */
4541                 case SCTP_IPV4_ADDRESS:
4542                         if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
4543                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
4544                                 goto invalid_size;
4545                         }
4546                         at += padded_size;
4547                         break;
4548                 case SCTP_IPV6_ADDRESS:
4549                         if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
4550                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
4551                                 goto invalid_size;
4552                         }
4553                         at += padded_size;
4554                         break;
4555                 case SCTP_COOKIE_PRESERVE:
4556                         if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
4557                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
4558                                 goto invalid_size;
4559                         }
4560                         at += padded_size;
4561                         break;
4562                 case SCTP_HAS_NAT_SUPPORT:
4563                         *nat_friendly = 1;
4564                         /* fall through */
4565                 case SCTP_ECN_NONCE_SUPPORTED:
4566                 case SCTP_PRSCTP_SUPPORTED:
4567
4568                         if (padded_size != sizeof(struct sctp_paramhdr)) {
4569                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
4570                                 goto invalid_size;
4571                         }
4572                         at += padded_size;
4573                         break;
4574                 case SCTP_ECN_CAPABLE:
4575                         if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
4576                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
4577                                 goto invalid_size;
4578                         }
4579                         at += padded_size;
4580                         break;
4581                 case SCTP_ULP_ADAPTATION:
4582                         if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
4583                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
4584                                 goto invalid_size;
4585                         }
4586                         at += padded_size;
4587                         break;
4588                 case SCTP_SUCCESS_REPORT:
4589                         if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
4590                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
4591                                 goto invalid_size;
4592                         }
4593                         at += padded_size;
4594                         break;
4595                 case SCTP_HOSTNAME_ADDRESS:
4596                         {
4597                                 /* We can NOT handle HOST NAME addresses!! */
4598                                 int l_len;
4599
4600                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
4601                                 *abort_processing = 1;
4602                                 if (op_err == NULL) {
4603                                         /* Ok need to try to get a mbuf */
4604 #ifdef INET6
4605                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4606 #else
4607                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4608 #endif
4609                                         l_len += plen;
4610                                         l_len += sizeof(struct sctp_paramhdr);
4611                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4612                                         if (op_err) {
4613                                                 SCTP_BUF_LEN(op_err) = 0;
4614                                                 /*
4615                                                  * pre-reserve space for ip
4616                                                  * and sctp header  and
4617                                                  * chunk hdr
4618                                                  */
4619 #ifdef INET6
4620                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4621 #else
4622                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4623 #endif
4624                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4625                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4626                                         }
4627                                 }
4628                                 if (op_err) {
4629                                         /* If we have space */
4630                                         struct sctp_paramhdr s;
4631
4632                                         if (err_at % 4) {
4633                                                 uint32_t cpthis = 0;
4634
4635                                                 pad_needed = 4 - (err_at % 4);
4636                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4637                                                 err_at += pad_needed;
4638                                         }
4639                                         s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
4640                                         s.param_length = htons(sizeof(s) + plen);
4641                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4642                                         err_at += sizeof(s);
4643                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4644                                         if (phdr == NULL) {
4645                                                 sctp_m_freem(op_err);
4646                                                 /*
4647                                                  * we are out of memory but
4648                                                  * we still need to have a
4649                                                  * look at what to do (the
4650                                                  * system is in trouble
4651                                                  * though).
4652                                                  */
4653                                                 return (NULL);
4654                                         }
4655                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4656                                         err_at += plen;
4657                                 }
4658                                 return (op_err);
4659                                 break;
4660                         }
4661                 default:
4662                         /*
4663                          * we do not recognize the parameter figure out what
4664                          * we do.
4665                          */
4666                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
4667                         if ((ptype & 0x4000) == 0x4000) {
4668                                 /* Report bit is set?? */
4669                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
4670                                 if (op_err == NULL) {
4671                                         int l_len;
4672
4673                                         /* Ok need to try to get an mbuf */
4674 #ifdef INET6
4675                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4676 #else
4677                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4678 #endif
4679                                         l_len += plen;
4680                                         l_len += sizeof(struct sctp_paramhdr);
4681                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4682                                         if (op_err) {
4683                                                 SCTP_BUF_LEN(op_err) = 0;
4684 #ifdef INET6
4685                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4686 #else
4687                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4688 #endif
4689                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4690                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4691                                         }
4692                                 }
4693                                 if (op_err) {
4694                                         /* If we have space */
4695                                         struct sctp_paramhdr s;
4696
4697                                         if (err_at % 4) {
4698                                                 uint32_t cpthis = 0;
4699
4700                                                 pad_needed = 4 - (err_at % 4);
4701                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4702                                                 err_at += pad_needed;
4703                                         }
4704                                         s.param_type = htons(SCTP_UNRECOG_PARAM);
4705                                         s.param_length = htons(sizeof(s) + plen);
4706                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4707                                         err_at += sizeof(s);
4708                                         if (plen > sizeof(tempbuf)) {
4709                                                 plen = sizeof(tempbuf);
4710                                         }
4711                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
4712                                         if (phdr == NULL) {
4713                                                 sctp_m_freem(op_err);
4714                                                 /*
4715                                                  * we are out of memory but
4716                                                  * we still need to have a
4717                                                  * look at what to do (the
4718                                                  * system is in trouble
4719                                                  * though).
4720                                                  */
4721                                                 op_err = NULL;
4722                                                 goto more_processing;
4723                                         }
4724                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
4725                                         err_at += plen;
4726                                 }
4727                         }
4728         more_processing:
4729                         if ((ptype & 0x8000) == 0x0000) {
4730                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
4731                                 return (op_err);
4732                         } else {
4733                                 /* skip this chunk and continue processing */
4734                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
4735                                 at += SCTP_SIZE32(plen);
4736                         }
4737                         break;
4738
4739                 }
4740                 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
4741         }
4742         return (op_err);
4743 invalid_size:
4744         SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
4745         *abort_processing = 1;
4746         if ((op_err == NULL) && phdr) {
4747                 int l_len;
4748
4749 #ifdef INET6
4750                 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4751 #else
4752                 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
4753 #endif
4754                 l_len += (2 * sizeof(struct sctp_paramhdr));
4755                 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
4756                 if (op_err) {
4757                         SCTP_BUF_LEN(op_err) = 0;
4758 #ifdef INET6
4759                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
4760 #else
4761                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
4762 #endif
4763                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
4764                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
4765                 }
4766         }
4767         if ((op_err) && phdr) {
4768                 struct sctp_paramhdr s;
4769
4770                 if (err_at % 4) {
4771                         uint32_t cpthis = 0;
4772
4773                         pad_needed = 4 - (err_at % 4);
4774                         m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
4775                         err_at += pad_needed;
4776                 }
4777                 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
4778                 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
4779                 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
4780                 err_at += sizeof(s);
4781                 /* Only copy back the p-hdr that caused the issue */
4782                 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
4783         }
4784         return (op_err);
4785 }
4786
4787 static int
4788 sctp_are_there_new_addresses(struct sctp_association *asoc,
4789     struct mbuf *in_initpkt, int iphlen, int offset)
4790 {
4791         /*
4792          * Given a INIT packet, look through the packet to verify that there
4793          * are NO new addresses. As we go through the parameters add reports
4794          * of any un-understood parameters that require an error.  Also we
4795          * must return (1) to drop the packet if we see a un-understood
4796          * parameter that tells us to drop the chunk.
4797          */
4798         struct sockaddr_in sin4, *sa4;
4799
4800 #ifdef INET6
4801         struct sockaddr_in6 sin6, *sa6;
4802
4803 #endif
4804         struct sockaddr *sa_touse;
4805         struct sockaddr *sa;
4806         struct sctp_paramhdr *phdr, params;
4807         struct ip *iph;
4808
4809 #ifdef INET6
4810         struct ip6_hdr *ip6h;
4811
4812 #endif
4813         struct mbuf *mat;
4814         uint16_t ptype, plen;
4815         int err_at;
4816         uint8_t fnd;
4817         struct sctp_nets *net;
4818
4819         memset(&sin4, 0, sizeof(sin4));
4820 #ifdef INET6
4821         memset(&sin6, 0, sizeof(sin6));
4822 #endif
4823         sin4.sin_family = AF_INET;
4824         sin4.sin_len = sizeof(sin4);
4825 #ifdef INET6
4826         sin6.sin6_family = AF_INET6;
4827         sin6.sin6_len = sizeof(sin6);
4828 #endif
4829         sa_touse = NULL;
4830         /* First what about the src address of the pkt ? */
4831         iph = mtod(in_initpkt, struct ip *);
4832         switch (iph->ip_v) {
4833         case IPVERSION:
4834                 /* source addr is IPv4 */
4835                 sin4.sin_addr = iph->ip_src;
4836                 sa_touse = (struct sockaddr *)&sin4;
4837                 break;
4838 #ifdef INET6
4839         case IPV6_VERSION >> 4:
4840                 /* source addr is IPv6 */
4841                 ip6h = mtod(in_initpkt, struct ip6_hdr *);
4842                 sin6.sin6_addr = ip6h->ip6_src;
4843                 sa_touse = (struct sockaddr *)&sin6;
4844                 break;
4845 #endif
4846         default:
4847                 return (1);
4848         }
4849
4850         fnd = 0;
4851         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4852                 sa = (struct sockaddr *)&net->ro._l_addr;
4853                 if (sa->sa_family == sa_touse->sa_family) {
4854                         if (sa->sa_family == AF_INET) {
4855                                 sa4 = (struct sockaddr_in *)sa;
4856                                 if (sa4->sin_addr.s_addr ==
4857                                     sin4.sin_addr.s_addr) {
4858                                         fnd = 1;
4859                                         break;
4860                                 }
4861                         }
4862 #ifdef INET6
4863                         if (sa->sa_family == AF_INET6) {
4864                                 sa6 = (struct sockaddr_in6 *)sa;
4865                                 if (SCTP6_ARE_ADDR_EQUAL(sa6,
4866                                     &sin6)) {
4867                                         fnd = 1;
4868                                         break;
4869                                 }
4870                         }
4871 #endif
4872                 }
4873         }
4874         if (fnd == 0) {
4875                 /* New address added! no need to look futher. */
4876                 return (1);
4877         }
4878         /* Ok so far lets munge through the rest of the packet */
4879         mat = in_initpkt;
4880         err_at = 0;
4881         sa_touse = NULL;
4882         offset += sizeof(struct sctp_init_chunk);
4883         phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4884         while (phdr) {
4885                 ptype = ntohs(phdr->param_type);
4886                 plen = ntohs(phdr->param_length);
4887                 if (ptype == SCTP_IPV4_ADDRESS) {
4888                         struct sctp_ipv4addr_param *p4, p4_buf;
4889
4890                         phdr = sctp_get_next_param(mat, offset,
4891                             (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4892                         if (plen != sizeof(struct sctp_ipv4addr_param) ||
4893                             phdr == NULL) {
4894                                 return (1);
4895                         }
4896                         p4 = (struct sctp_ipv4addr_param *)phdr;
4897                         sin4.sin_addr.s_addr = p4->addr;
4898                         sa_touse = (struct sockaddr *)&sin4;
4899                 } else if (ptype == SCTP_IPV6_ADDRESS) {
4900                         struct sctp_ipv6addr_param *p6, p6_buf;
4901
4902                         phdr = sctp_get_next_param(mat, offset,
4903                             (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4904                         if (plen != sizeof(struct sctp_ipv6addr_param) ||
4905                             phdr == NULL) {
4906                                 return (1);
4907                         }
4908                         p6 = (struct sctp_ipv6addr_param *)phdr;
4909 #ifdef INET6
4910                         memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4911                             sizeof(p6->addr));
4912 #endif
4913                         sa_touse = (struct sockaddr *)&sin4;
4914                 }
4915                 if (sa_touse) {
4916                         /* ok, sa_touse points to one to check */
4917                         fnd = 0;
4918                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
4919                                 sa = (struct sockaddr *)&net->ro._l_addr;
4920                                 if (sa->sa_family != sa_touse->sa_family) {
4921                                         continue;
4922                                 }
4923                                 if (sa->sa_family == AF_INET) {
4924                                         sa4 = (struct sockaddr_in *)sa;
4925                                         if (sa4->sin_addr.s_addr ==
4926                                             sin4.sin_addr.s_addr) {
4927                                                 fnd = 1;
4928                                                 break;
4929                                         }
4930                                 }
4931 #ifdef INET6
4932                                 if (sa->sa_family == AF_INET6) {
4933                                         sa6 = (struct sockaddr_in6 *)sa;
4934                                         if (SCTP6_ARE_ADDR_EQUAL(
4935                                             sa6, &sin6)) {
4936                                                 fnd = 1;
4937                                                 break;
4938                                         }
4939                                 }
4940 #endif
4941                         }
4942                         if (!fnd) {
4943                                 /* New addr added! no need to look further */
4944                                 return (1);
4945                         }
4946                 }
4947                 offset += SCTP_SIZE32(plen);
4948                 phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
4949         }
4950         return (0);
4951 }
4952
4953 /*
4954  * Given a MBUF chain that was sent into us containing an INIT. Build a
4955  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
4956  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
4957  * message (i.e. the struct sctp_init_msg).
4958  */
4959 void
4960 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4961     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
4962     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
4963 {
4964         struct sctp_association *asoc;
4965         struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
4966         struct sctp_init_ack_chunk *initack;
4967         struct sctp_adaptation_layer_indication *ali;
4968         struct sctp_ecn_supported_param *ecn;
4969         struct sctp_prsctp_supported_param *prsctp;
4970         struct sctp_ecn_nonce_supported_param *ecn_nonce;
4971         struct sctp_supported_chunk_types_param *pr_supported;
4972         union sctp_sockstore store, store1, *over_addr;
4973         struct sockaddr_in *sin, *to_sin;
4974
4975 #ifdef INET6
4976         struct sockaddr_in6 *sin6, *to_sin6;
4977
4978 #endif
4979         struct ip *iph;
4980
4981 #ifdef INET6
4982         struct ip6_hdr *ip6;
4983
4984 #endif
4985         struct sockaddr *to;
4986         struct sctp_state_cookie stc;
4987         struct sctp_nets *net = NULL;
4988         uint8_t *signature = NULL;
4989         int cnt_inits_to = 0;
4990         uint16_t his_limit, i_want;
4991         int abort_flag, padval;
4992         int num_ext;
4993         int p_len;
4994         int nat_friendly = 0;
4995         struct socket *so;
4996
4997         if (stcb)
4998                 asoc = &stcb->asoc;
4999         else
5000                 asoc = NULL;
5001         mp_last = NULL;
5002         if ((asoc != NULL) &&
5003             (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
5004             (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
5005                 /* new addresses, out of here in non-cookie-wait states */
5006                 /*
5007                  * Send a ABORT, we don't add the new address error clause
5008                  * though we even set the T bit and copy in the 0 tag.. this
5009                  * looks no different than if no listener was present.
5010                  */
5011                 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
5012                 return;
5013         }
5014         abort_flag = 0;
5015         op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5016             (offset + sizeof(struct sctp_init_chunk)),
5017             &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
5018         if (abort_flag) {
5019 do_a_abort:
5020                 sctp_send_abort(init_pkt, iphlen, sh,
5021                     init_chk->init.initiate_tag, op_err, vrf_id, port);
5022                 return;
5023         }
5024         m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
5025         if (m == NULL) {
5026                 /* No memory, INIT timer will re-attempt. */
5027                 if (op_err)
5028                         sctp_m_freem(op_err);
5029                 return;
5030         }
5031         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_chunk);
5032
5033         /* the time I built cookie */
5034         (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
5035
5036         /* populate any tie tags */
5037         if (asoc != NULL) {
5038                 /* unlock before tag selections */
5039                 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5040                 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5041                 stc.cookie_life = asoc->cookie_life;
5042                 net = asoc->primary_destination;
5043         } else {
5044                 stc.tie_tag_my_vtag = 0;
5045                 stc.tie_tag_peer_vtag = 0;
5046                 /* life I will award this cookie */
5047                 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5048         }
5049
5050         /* copy in the ports for later check */
5051         stc.myport = sh->dest_port;
5052         stc.peerport = sh->src_port;
5053
5054         /*
5055          * If we wanted to honor cookie life extentions, we would add to
5056          * stc.cookie_life. For now we should NOT honor any extension
5057          */
5058         stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5059         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5060                 struct inpcb *in_inp;
5061
5062                 /* Its a V6 socket */
5063                 in_inp = (struct inpcb *)inp;
5064                 stc.ipv6_addr_legal = 1;
5065                 /* Now look at the binding flag to see if V4 will be legal */
5066                 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
5067                         stc.ipv4_addr_legal = 1;
5068                 } else {
5069                         /* V4 addresses are NOT legal on the association */
5070                         stc.ipv4_addr_legal = 0;
5071                 }
5072         } else {
5073                 /* Its a V4 socket, no - V6 */
5074                 stc.ipv4_addr_legal = 1;
5075                 stc.ipv6_addr_legal = 0;
5076         }
5077
5078 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
5079         stc.ipv4_scope = 1;
5080 #else
5081         stc.ipv4_scope = 0;
5082 #endif
5083         /* now for scope setup */
5084         memset((caddr_t)&store, 0, sizeof(store));
5085         memset((caddr_t)&store1, 0, sizeof(store1));
5086         sin = &store.sin;
5087         to_sin = &store1.sin;
5088 #ifdef INET6
5089         sin6 = &store.sin6;
5090         to_sin6 = &store1.sin6;
5091 #endif
5092         iph = mtod(init_pkt, struct ip *);
5093         /* establish the to_addr's */
5094         switch (iph->ip_v) {
5095         case IPVERSION:
5096                 to_sin->sin_port = sh->dest_port;
5097                 to_sin->sin_family = AF_INET;
5098                 to_sin->sin_len = sizeof(struct sockaddr_in);
5099                 to_sin->sin_addr = iph->ip_dst;
5100                 break;
5101 #ifdef INET6
5102         case IPV6_VERSION >> 4:
5103                 ip6 = mtod(init_pkt, struct ip6_hdr *);
5104                 to_sin6->sin6_addr = ip6->ip6_dst;
5105                 to_sin6->sin6_scope_id = 0;
5106                 to_sin6->sin6_port = sh->dest_port;
5107                 to_sin6->sin6_family = AF_INET6;
5108                 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
5109                 break;
5110 #endif
5111         default:
5112                 goto do_a_abort;
5113                 break;
5114         };
5115
5116         if (net == NULL) {
5117                 to = (struct sockaddr *)&store;
5118                 switch (iph->ip_v) {
5119                 case IPVERSION:
5120                         {
5121                                 sin->sin_family = AF_INET;
5122                                 sin->sin_len = sizeof(struct sockaddr_in);
5123                                 sin->sin_port = sh->src_port;
5124                                 sin->sin_addr = iph->ip_src;
5125                                 /* lookup address */
5126                                 stc.address[0] = sin->sin_addr.s_addr;
5127                                 stc.address[1] = 0;
5128                                 stc.address[2] = 0;
5129                                 stc.address[3] = 0;
5130                                 stc.addr_type = SCTP_IPV4_ADDRESS;
5131                                 /* local from address */
5132                                 stc.laddress[0] = to_sin->sin_addr.s_addr;
5133                                 stc.laddress[1] = 0;
5134                                 stc.laddress[2] = 0;
5135                                 stc.laddress[3] = 0;
5136                                 stc.laddr_type = SCTP_IPV4_ADDRESS;
5137                                 /* scope_id is only for v6 */
5138                                 stc.scope_id = 0;
5139 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
5140                                 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
5141                                         stc.ipv4_scope = 1;
5142                                 }
5143 #else
5144                                 stc.ipv4_scope = 1;
5145 #endif                          /* SCTP_DONT_DO_PRIVADDR_SCOPE */
5146                                 /* Must use the address in this case */
5147                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
5148                                         stc.loopback_scope = 1;
5149                                         stc.ipv4_scope = 1;
5150                                         stc.site_scope = 1;
5151                                         stc.local_scope = 0;
5152                                 }
5153                                 break;
5154                         }
5155 #ifdef INET6
5156                 case IPV6_VERSION >> 4:
5157                         {
5158                                 ip6 = mtod(init_pkt, struct ip6_hdr *);
5159                                 sin6->sin6_family = AF_INET6;
5160                                 sin6->sin6_len = sizeof(struct sockaddr_in6);
5161                                 sin6->sin6_port = sh->src_port;
5162                                 sin6->sin6_addr = ip6->ip6_src;
5163                                 /* lookup address */
5164                                 memcpy(&stc.address, &sin6->sin6_addr,
5165                                     sizeof(struct in6_addr));
5166                                 sin6->sin6_scope_id = 0;
5167                                 stc.addr_type = SCTP_IPV6_ADDRESS;
5168                                 stc.scope_id = 0;
5169                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
5170                                         /*
5171                                          * FIX ME: does this have scope from
5172                                          * rcvif?
5173                                          */
5174                                         (void)sa6_recoverscope(sin6);
5175                                         stc.scope_id = sin6->sin6_scope_id;
5176                                         sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5177                                         stc.loopback_scope = 1;
5178                                         stc.local_scope = 0;
5179                                         stc.site_scope = 1;
5180                                         stc.ipv4_scope = 1;
5181                                 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5182                                         /*
5183                                          * If the new destination is a
5184                                          * LINK_LOCAL we must have common
5185                                          * both site and local scope. Don't
5186                                          * set local scope though since we
5187                                          * must depend on the source to be
5188                                          * added implicitly. We cannot
5189                                          * assure just because we share one
5190                                          * link that all links are common.
5191                                          */
5192                                         stc.local_scope = 0;
5193                                         stc.site_scope = 1;
5194                                         stc.ipv4_scope = 1;
5195                                         /*
5196                                          * we start counting for the private
5197                                          * address stuff at 1. since the
5198                                          * link local we source from won't
5199                                          * show up in our scoped count.
5200                                          */
5201                                         cnt_inits_to = 1;
5202                                         /*
5203                                          * pull out the scope_id from
5204                                          * incoming pkt
5205                                          */
5206                                         /*
5207                                          * FIX ME: does this have scope from
5208                                          * rcvif?
5209                                          */
5210                                         (void)sa6_recoverscope(sin6);
5211                                         stc.scope_id = sin6->sin6_scope_id;
5212                                         sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
5213                                 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
5214                                         /*
5215                                          * If the new destination is
5216                                          * SITE_LOCAL then we must have site
5217                                          * scope in common.
5218                                          */
5219                                         stc.site_scope = 1;
5220                                 }
5221                                 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
5222                                 stc.laddr_type = SCTP_IPV6_ADDRESS;
5223                                 break;
5224                         }
5225 #endif
5226                 default:
5227                         /* TSNH */
5228                         goto do_a_abort;
5229                         break;
5230                 }
5231         } else {
5232                 /* set the scope per the existing tcb */
5233
5234 #ifdef INET6
5235                 struct sctp_nets *lnet;
5236
5237 #endif
5238
5239                 stc.loopback_scope = asoc->loopback_scope;
5240                 stc.ipv4_scope = asoc->ipv4_local_scope;
5241                 stc.site_scope = asoc->site_scope;
5242                 stc.local_scope = asoc->local_scope;
5243 #ifdef INET6
5244                 /* Why do we not consider IPv4 LL addresses? */
5245                 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5246                         if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5247                                 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5248                                         /*
5249                                          * if we have a LL address, start
5250                                          * counting at 1.
5251                                          */
5252                                         cnt_inits_to = 1;
5253                                 }
5254                         }
5255                 }
5256 #endif
5257                 /* use the net pointer */
5258                 to = (struct sockaddr *)&net->ro._l_addr;
5259                 switch (to->sa_family) {
5260                 case AF_INET:
5261                         sin = (struct sockaddr_in *)to;
5262                         stc.address[0] = sin->sin_addr.s_addr;
5263                         stc.address[1] = 0;
5264                         stc.address[2] = 0;
5265                         stc.address[3] = 0;
5266                         stc.addr_type = SCTP_IPV4_ADDRESS;
5267                         if (net->src_addr_selected == 0) {
5268                                 /*
5269                                  * strange case here, the INIT should have
5270                                  * did the selection.
5271                                  */
5272                                 net->ro._s_addr = sctp_source_address_selection(inp,
5273                                     stcb, (sctp_route_t *) & net->ro,
5274                                     net, 0, vrf_id);
5275                                 if (net->ro._s_addr == NULL)
5276                                         return;
5277
5278                                 net->src_addr_selected = 1;
5279
5280                         }
5281                         stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5282                         stc.laddress[1] = 0;
5283                         stc.laddress[2] = 0;
5284                         stc.laddress[3] = 0;
5285                         stc.laddr_type = SCTP_IPV4_ADDRESS;
5286                         break;
5287 #ifdef INET6
5288                 case AF_INET6:
5289                         sin6 = (struct sockaddr_in6 *)to;
5290                         memcpy(&stc.address, &sin6->sin6_addr,
5291                             sizeof(struct in6_addr));
5292                         stc.addr_type = SCTP_IPV6_ADDRESS;
5293                         if (net->src_addr_selected == 0) {
5294                                 /*
5295                                  * strange case here, the INIT should have
5296                                  * did the selection.
5297                                  */
5298                                 net->ro._s_addr = sctp_source_address_selection(inp,
5299                                     stcb, (sctp_route_t *) & net->ro,
5300                                     net, 0, vrf_id);
5301                                 if (net->ro._s_addr == NULL)
5302                                         return;
5303
5304                                 net->src_addr_selected = 1;
5305                         }
5306                         memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5307                             sizeof(struct in6_addr));
5308                         stc.laddr_type = SCTP_IPV6_ADDRESS;
5309                         break;
5310 #endif
5311                 }
5312         }
5313         /* Now lets put the SCTP header in place */
5314         initack = mtod(m, struct sctp_init_ack_chunk *);
5315         /* Save it off for quick ref */
5316         stc.peers_vtag = init_chk->init.initiate_tag;
5317         /* who are we */
5318         memcpy(stc.identification, SCTP_VERSION_STRING,
5319             min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5320         /* now the chunk header */
5321         initack->ch.chunk_type = SCTP_INITIATION_ACK;
5322         initack->ch.chunk_flags = 0;
5323         /* fill in later from mbuf we build */
5324         initack->ch.chunk_length = 0;
5325         /* place in my tag */
5326         if ((asoc != NULL) &&
5327             ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
5328             (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
5329             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
5330                 /* re-use the v-tags and init-seq here */
5331                 initack->init.initiate_tag = htonl(asoc->my_vtag);
5332                 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5333         } else {
5334                 uint32_t vtag, itsn;
5335
5336                 if (hold_inp_lock) {
5337                         SCTP_INP_INCR_REF(inp);
5338                         SCTP_INP_RUNLOCK(inp);
5339                 }
5340                 if (asoc) {
5341                         atomic_add_int(&asoc->refcnt, 1);
5342                         SCTP_TCB_UNLOCK(stcb);
5343         new_tag:
5344                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5345                         if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5346                                 /*
5347                                  * Got a duplicate vtag on some guy behind a
5348                                  * nat make sure we don't use it.
5349                                  */
5350                                 goto new_tag;
5351                         }
5352                         initack->init.initiate_tag = htonl(vtag);
5353                         /* get a TSN to use too */
5354                         itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5355                         initack->init.initial_tsn = htonl(itsn);
5356                         SCTP_TCB_LOCK(stcb);
5357                         atomic_add_int(&asoc->refcnt, -1);
5358                 } else {
5359                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5360                         initack->init.initiate_tag = htonl(vtag);
5361                         /* get a TSN to use too */
5362                         initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5363                 }
5364                 if (hold_inp_lock) {
5365                         SCTP_INP_RLOCK(inp);
5366                         SCTP_INP_DECR_REF(inp);
5367                 }
5368         }
5369         /* save away my tag to */
5370         stc.my_vtag = initack->init.initiate_tag;
5371
5372         /* set up some of the credits. */
5373         so = inp->sctp_socket;
5374         if (so == NULL) {
5375                 /* memory problem */
5376                 sctp_m_freem(m);
5377                 return;
5378         } else {
5379                 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5380         }
5381         /* set what I want */
5382         his_limit = ntohs(init_chk->init.num_inbound_streams);
5383         /* choose what I want */
5384         if (asoc != NULL) {
5385                 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
5386                         i_want = asoc->streamoutcnt;
5387                 } else {
5388                         i_want = inp->sctp_ep.pre_open_stream_count;
5389                 }
5390         } else {
5391                 i_want = inp->sctp_ep.pre_open_stream_count;
5392         }
5393         if (his_limit < i_want) {
5394                 /* I Want more :< */
5395                 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5396         } else {
5397                 /* I can have what I want :> */
5398                 initack->init.num_outbound_streams = htons(i_want);
5399         }
5400         /* tell him his limt. */
5401         initack->init.num_inbound_streams =
5402             htons(inp->sctp_ep.max_open_streams_intome);
5403
5404         /* adaptation layer indication parameter */
5405         ali = (struct sctp_adaptation_layer_indication *)((caddr_t)initack + sizeof(*initack));
5406         ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5407         ali->ph.param_length = htons(sizeof(*ali));
5408         ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
5409         SCTP_BUF_LEN(m) += sizeof(*ali);
5410         ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali + sizeof(*ali));
5411
5412         /* ECN parameter */
5413         if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
5414                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
5415                 ecn->ph.param_length = htons(sizeof(*ecn));
5416                 SCTP_BUF_LEN(m) += sizeof(*ecn);
5417
5418                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
5419                     sizeof(*ecn));
5420         } else {
5421                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
5422         }
5423         /* And now tell the peer we do  pr-sctp */
5424         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
5425         prsctp->ph.param_length = htons(sizeof(*prsctp));
5426         SCTP_BUF_LEN(m) += sizeof(*prsctp);
5427         if (nat_friendly) {
5428                 /* Add NAT friendly parameter */
5429                 struct sctp_paramhdr *ph;
5430
5431                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5432                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5433                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
5434                 SCTP_BUF_LEN(m) += sizeof(struct sctp_paramhdr);
5435         }
5436         /* And now tell the peer we do all the extensions */
5437         pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5438         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5439         num_ext = 0;
5440         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5441         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5442         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5443         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5444         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5445         if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
5446                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5447         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
5448                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5449         p_len = sizeof(*pr_supported) + num_ext;
5450         pr_supported->ph.param_length = htons(p_len);
5451         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
5452         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5453
5454         /* ECN nonce: And now tell the peer we support ECN nonce */
5455         if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
5456                 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
5457                     ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
5458                 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
5459                 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
5460                 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
5461         }
5462         /* add authentication parameters */
5463         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
5464                 struct sctp_auth_random *randp;
5465                 struct sctp_auth_hmac_algo *hmacs;
5466                 struct sctp_auth_chunk_list *chunks;
5467                 uint16_t random_len;
5468
5469                 /* generate and add RANDOM parameter */
5470                 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5471                 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5472                 randp->ph.param_type = htons(SCTP_RANDOM);
5473                 p_len = sizeof(*randp) + random_len;
5474                 randp->ph.param_length = htons(p_len);
5475                 SCTP_READ_RANDOM(randp->random_data, random_len);
5476                 /* zero out any padding required */
5477                 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
5478                 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5479
5480                 /* add HMAC_ALGO parameter */
5481                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5482                 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5483                     (uint8_t *) hmacs->hmac_ids);
5484                 if (p_len > 0) {
5485                         p_len += sizeof(*hmacs);
5486                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5487                         hmacs->ph.param_length = htons(p_len);
5488                         /* zero out any padding required */
5489                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
5490                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5491                 }
5492                 /* add CHUNKS parameter */
5493                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
5494                 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
5495                     chunks->chunk_types);
5496                 if (p_len > 0) {
5497                         p_len += sizeof(*chunks);
5498                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5499                         chunks->ph.param_length = htons(p_len);
5500                         /* zero out any padding required */
5501                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
5502                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
5503                 }
5504         }
5505         m_at = m;
5506         /* now the addresses */
5507         {
5508                 struct sctp_scoping scp;
5509
5510                 /*
5511                  * To optimize this we could put the scoping stuff into a
5512                  * structure and remove the individual uint8's from the stc
5513                  * structure. Then we could just sifa in the address within
5514                  * the stc.. but for now this is a quick hack to get the
5515                  * address stuff teased apart.
5516                  */
5517                 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
5518                 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
5519                 scp.loopback_scope = stc.loopback_scope;
5520                 scp.ipv4_local_scope = stc.ipv4_scope;
5521                 scp.local_scope = stc.local_scope;
5522                 scp.site_scope = stc.site_scope;
5523                 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
5524         }
5525
5526         /* tack on the operational error if present */
5527         if (op_err) {
5528                 struct mbuf *ol;
5529                 int llen;
5530
5531                 llen = 0;
5532                 ol = op_err;
5533                 while (ol) {
5534                         llen += SCTP_BUF_LEN(ol);
5535                         ol = SCTP_BUF_NEXT(ol);
5536                 }
5537                 if (llen % 4) {
5538                         /* must add a pad to the param */
5539                         uint32_t cpthis = 0;
5540                         int padlen;
5541
5542                         padlen = 4 - (llen % 4);
5543                         m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
5544                 }
5545                 while (SCTP_BUF_NEXT(m_at) != NULL) {
5546                         m_at = SCTP_BUF_NEXT(m_at);
5547                 }
5548                 SCTP_BUF_NEXT(m_at) = op_err;
5549                 while (SCTP_BUF_NEXT(m_at) != NULL) {
5550                         m_at = SCTP_BUF_NEXT(m_at);
5551                 }
5552         }
5553         /* pre-calulate the size and update pkt header and chunk header */
5554         p_len = 0;
5555         for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5556                 p_len += SCTP_BUF_LEN(m_tmp);
5557                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5558                         /* m_tmp should now point to last one */
5559                         break;
5560                 }
5561         }
5562
5563         /* Now we must build a cookie */
5564         m_cookie = sctp_add_cookie(inp, init_pkt, offset, m, 0, &stc, &signature);
5565         if (m_cookie == NULL) {
5566                 /* memory problem */
5567                 sctp_m_freem(m);
5568                 return;
5569         }
5570         /* Now append the cookie to the end and update the space/size */
5571         SCTP_BUF_NEXT(m_tmp) = m_cookie;
5572
5573         for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
5574                 p_len += SCTP_BUF_LEN(m_tmp);
5575                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
5576                         /* m_tmp should now point to last one */
5577                         mp_last = m_tmp;
5578                         break;
5579                 }
5580         }
5581         /*
5582          * Place in the size, but we don't include the last pad (if any) in
5583          * the INIT-ACK.
5584          */
5585         initack->ch.chunk_length = htons(p_len);
5586
5587         /*
5588          * Time to sign the cookie, we don't sign over the cookie signature
5589          * though thus we set trailer.
5590          */
5591         (void)sctp_hmac_m(SCTP_HMAC,
5592             (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
5593             SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
5594             (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
5595         /*
5596          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
5597          * here since the timer will drive a retranmission.
5598          */
5599         padval = p_len % 4;
5600         if ((padval) && (mp_last)) {
5601                 /* see my previous comments on mp_last */
5602                 int ret;
5603
5604                 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
5605                 if (ret) {
5606                         /* Houston we have a problem, no space */
5607                         sctp_m_freem(m);
5608                         return;
5609                 }
5610                 p_len += padval;
5611         }
5612         if (stc.loopback_scope) {
5613                 over_addr = &store1;
5614         } else {
5615                 over_addr = NULL;
5616         }
5617
5618         (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
5619             0, NULL, 0,
5620             inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
5621             port, SCTP_SO_NOT_LOCKED, over_addr);
5622         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5623 }
5624
5625
5626 void
5627 sctp_insert_on_wheel(struct sctp_tcb *stcb,
5628     struct sctp_association *asoc,
5629     struct sctp_stream_out *strq, int holds_lock)
5630 {
5631         if (holds_lock == 0) {
5632                 SCTP_TCB_SEND_LOCK(stcb);
5633         }
5634         if ((strq->next_spoke.tqe_next == NULL) &&
5635             (strq->next_spoke.tqe_prev == NULL)) {
5636                 TAILQ_INSERT_TAIL(&asoc->out_wheel, strq, next_spoke);
5637         }
5638         if (holds_lock == 0) {
5639                 SCTP_TCB_SEND_UNLOCK(stcb);
5640         }
5641 }
5642
5643 void
5644 sctp_remove_from_wheel(struct sctp_tcb *stcb,
5645     struct sctp_association *asoc,
5646     struct sctp_stream_out *strq,
5647     int holds_lock)
5648 {
5649         /* take off and then setup so we know it is not on the wheel */
5650         if (holds_lock == 0) {
5651                 SCTP_TCB_SEND_LOCK(stcb);
5652         }
5653         if (TAILQ_EMPTY(&strq->outqueue)) {
5654                 if (asoc->last_out_stream == strq) {
5655                         asoc->last_out_stream = TAILQ_PREV(asoc->last_out_stream, sctpwheel_listhead, next_spoke);
5656                         if (asoc->last_out_stream == NULL) {
5657                                 asoc->last_out_stream = TAILQ_LAST(&asoc->out_wheel, sctpwheel_listhead);
5658                         }
5659                         if (asoc->last_out_stream == strq) {
5660                                 asoc->last_out_stream = NULL;
5661                         }
5662                 }
5663                 TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
5664                 strq->next_spoke.tqe_next = NULL;
5665                 strq->next_spoke.tqe_prev = NULL;
5666         }
5667         if (holds_lock == 0) {
5668                 SCTP_TCB_SEND_UNLOCK(stcb);
5669         }
5670 }
5671
5672 static void
5673 sctp_prune_prsctp(struct sctp_tcb *stcb,
5674     struct sctp_association *asoc,
5675     struct sctp_sndrcvinfo *srcv,
5676     int dataout)
5677 {
5678         int freed_spc = 0;
5679         struct sctp_tmit_chunk *chk, *nchk;
5680
5681         SCTP_TCB_LOCK_ASSERT(stcb);
5682         if ((asoc->peer_supports_prsctp) &&
5683             (asoc->sent_queue_cnt_removeable > 0)) {
5684                 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
5685                         /*
5686                          * Look for chunks marked with the PR_SCTP flag AND
5687                          * the buffer space flag. If the one being sent is
5688                          * equal or greater priority then purge the old one
5689                          * and free some space.
5690                          */
5691                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
5692                                 /*
5693                                  * This one is PR-SCTP AND buffer space
5694                                  * limited type
5695                                  */
5696                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5697                                         /*
5698                                          * Lower numbers equates to higher
5699                                          * priority so if the one we are
5700                                          * looking at has a larger or equal
5701                                          * priority we want to drop the data
5702                                          * and NOT retransmit it.
5703                                          */
5704                                         if (chk->data) {
5705                                                 /*
5706                                                  * We release the book_size
5707                                                  * if the mbuf is here
5708                                                  */
5709                                                 int ret_spc;
5710                                                 int cause;
5711
5712                                                 if (chk->sent > SCTP_DATAGRAM_UNSENT)
5713                                                         cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
5714                                                 else
5715                                                         cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
5716                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5717                                                     cause,
5718                                                     SCTP_SO_LOCKED);
5719                                                 freed_spc += ret_spc;
5720                                                 if (freed_spc >= dataout) {
5721                                                         return;
5722                                                 }
5723                                         }       /* if chunk was present */
5724                                 }       /* if of sufficent priority */
5725                         }       /* if chunk has enabled */
5726                 }               /* tailqforeach */
5727
5728                 chk = TAILQ_FIRST(&asoc->send_queue);
5729                 while (chk) {
5730                         nchk = TAILQ_NEXT(chk, sctp_next);
5731                         /* Here we must move to the sent queue and mark */
5732                         if (PR_SCTP_TTL_ENABLED(chk->flags)) {
5733                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
5734                                         if (chk->data) {
5735                                                 /*
5736                                                  * We release the book_size
5737                                                  * if the mbuf is here
5738                                                  */
5739                                                 int ret_spc;
5740
5741                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
5742                                                     SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
5743                                                     SCTP_SO_LOCKED);
5744
5745                                                 freed_spc += ret_spc;
5746                                                 if (freed_spc >= dataout) {
5747                                                         return;
5748                                                 }
5749                                         }       /* end if chk->data */
5750                                 }       /* end if right class */
5751                         }       /* end if chk pr-sctp */
5752                         chk = nchk;
5753                 }               /* end while (chk) */
5754         }                       /* if enabled in asoc */
5755 }
5756
5757 int
5758 sctp_get_frag_point(struct sctp_tcb *stcb,
5759     struct sctp_association *asoc)
5760 {
5761         int siz, ovh;
5762
5763         /*
5764          * For endpoints that have both v6 and v4 addresses we must reserve
5765          * room for the ipv6 header, for those that are only dealing with V4
5766          * we use a larger frag point.
5767          */
5768         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5769                 ovh = SCTP_MED_OVERHEAD;
5770         } else {
5771                 ovh = SCTP_MED_V4_OVERHEAD;
5772         }
5773
5774         if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
5775                 siz = asoc->smallest_mtu - ovh;
5776         else
5777                 siz = (stcb->asoc.sctp_frag_point - ovh);
5778         /*
5779          * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
5780          */
5781         /* A data chunk MUST fit in a cluster */
5782         /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
5783         /* } */
5784
5785         /* adjust for an AUTH chunk if DATA requires auth */
5786         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
5787                 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
5788
5789         if (siz % 4) {
5790                 /* make it an even word boundary please */
5791                 siz -= (siz % 4);
5792         }
5793         return (siz);
5794 }
5795
5796 static void
5797 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
5798 {
5799         sp->pr_sctp_on = 0;
5800         /*
5801          * We assume that the user wants PR_SCTP_TTL if the user provides a
5802          * positive lifetime but does not specify any PR_SCTP policy. This
5803          * is a BAD assumption and causes problems at least with the
5804          * U-Vancovers MPI folks. I will change this to be no policy means
5805          * NO PR-SCTP.
5806          */
5807         if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
5808                 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
5809                 sp->pr_sctp_on = 1;
5810         } else {
5811                 return;
5812         }
5813         switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
5814         case CHUNK_FLAGS_PR_SCTP_BUF:
5815                 /*
5816                  * Time to live is a priority stored in tv_sec when doing
5817                  * the buffer drop thing.
5818                  */
5819                 sp->ts.tv_sec = sp->timetolive;
5820                 sp->ts.tv_usec = 0;
5821                 break;
5822         case CHUNK_FLAGS_PR_SCTP_TTL:
5823                 {
5824                         struct timeval tv;
5825
5826                         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5827                         tv.tv_sec = sp->timetolive / 1000;
5828                         tv.tv_usec = (sp->timetolive * 1000) % 1000000;
5829                         /*
5830                          * TODO sctp_constants.h needs alternative time
5831                          * macros when _KERNEL is undefined.
5832                          */
5833                         timevaladd(&sp->ts, &tv);
5834                 }
5835                 break;
5836         case CHUNK_FLAGS_PR_SCTP_RTX:
5837                 /*
5838                  * Time to live is a the number or retransmissions stored in
5839                  * tv_sec.
5840                  */
5841                 sp->ts.tv_sec = sp->timetolive;
5842                 sp->ts.tv_usec = 0;
5843                 break;
5844         default:
5845                 SCTPDBG(SCTP_DEBUG_USRREQ1,
5846                     "Unknown PR_SCTP policy %u.\n",
5847                     PR_SCTP_POLICY(sp->sinfo_flags));
5848                 break;
5849         }
5850 }
5851
5852 static int
5853 sctp_msg_append(struct sctp_tcb *stcb,
5854     struct sctp_nets *net,
5855     struct mbuf *m,
5856     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
5857 {
5858         int error = 0, holds_lock;
5859         struct mbuf *at;
5860         struct sctp_stream_queue_pending *sp = NULL;
5861         struct sctp_stream_out *strm;
5862
5863         /*
5864          * Given an mbuf chain, put it into the association send queue and
5865          * place it on the wheel
5866          */
5867         holds_lock = hold_stcb_lock;
5868         if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
5869                 /* Invalid stream number */
5870                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5871                 error = EINVAL;
5872                 goto out_now;
5873         }
5874         if ((stcb->asoc.stream_locked) &&
5875             (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
5876                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5877                 error = EINVAL;
5878                 goto out_now;
5879         }
5880         strm = &stcb->asoc.strmout[srcv->sinfo_stream];
5881         /* Now can we send this? */
5882         if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
5883             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
5884             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
5885             (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
5886                 /* got data while shutting down */
5887                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
5888                 error = ECONNRESET;
5889                 goto out_now;
5890         }
5891         sctp_alloc_a_strmoq(stcb, sp);
5892         if (sp == NULL) {
5893                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5894                 error = ENOMEM;
5895                 goto out_now;
5896         }
5897         sp->sinfo_flags = srcv->sinfo_flags;
5898         sp->timetolive = srcv->sinfo_timetolive;
5899         sp->ppid = srcv->sinfo_ppid;
5900         sp->context = srcv->sinfo_context;
5901         sp->strseq = 0;
5902         if (sp->sinfo_flags & SCTP_ADDR_OVER) {
5903                 sp->net = net;
5904         } else {
5905                 sp->net = stcb->asoc.primary_destination;
5906         }
5907         atomic_add_int(&sp->net->ref_count, 1);
5908         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
5909         sp->stream = srcv->sinfo_stream;
5910         sp->msg_is_complete = 1;
5911         sp->sender_all_done = 1;
5912         sp->some_taken = 0;
5913         sp->data = m;
5914         sp->tail_mbuf = NULL;
5915         sp->length = 0;
5916         at = m;
5917         sctp_set_prsctp_policy(sp);
5918         /*
5919          * We could in theory (for sendall) sifa the length in, but we would
5920          * still have to hunt through the chain since we need to setup the
5921          * tail_mbuf
5922          */
5923         while (at) {
5924                 if (SCTP_BUF_NEXT(at) == NULL)
5925                         sp->tail_mbuf = at;
5926                 sp->length += SCTP_BUF_LEN(at);
5927                 at = SCTP_BUF_NEXT(at);
5928         }
5929         SCTP_TCB_SEND_LOCK(stcb);
5930         sctp_snd_sb_alloc(stcb, sp->length);
5931         atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
5932         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
5933         if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
5934                 sp->strseq = strm->next_sequence_sent;
5935                 strm->next_sequence_sent++;
5936         }
5937         if ((strm->next_spoke.tqe_next == NULL) &&
5938             (strm->next_spoke.tqe_prev == NULL)) {
5939                 /* Not on wheel, insert */
5940                 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
5941         }
5942         m = NULL;
5943         SCTP_TCB_SEND_UNLOCK(stcb);
5944 out_now:
5945         if (m) {
5946                 sctp_m_freem(m);
5947         }
5948         return (error);
5949 }
5950
5951
5952 static struct mbuf *
5953 sctp_copy_mbufchain(struct mbuf *clonechain,
5954     struct mbuf *outchain,
5955     struct mbuf **endofchain,
5956     int can_take_mbuf,
5957     int sizeofcpy,
5958     uint8_t copy_by_ref)
5959 {
5960         struct mbuf *m;
5961         struct mbuf *appendchain;
5962         caddr_t cp;
5963         int len;
5964
5965         if (endofchain == NULL) {
5966                 /* error */
5967 error_out:
5968                 if (outchain)
5969                         sctp_m_freem(outchain);
5970                 return (NULL);
5971         }
5972         if (can_take_mbuf) {
5973                 appendchain = clonechain;
5974         } else {
5975                 if (!copy_by_ref &&
5976                     (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
5977                     ) {
5978                         /* Its not in a cluster */
5979                         if (*endofchain == NULL) {
5980                                 /* lets get a mbuf cluster */
5981                                 if (outchain == NULL) {
5982                                         /* This is the general case */
5983                         new_mbuf:
5984                                         outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
5985                                         if (outchain == NULL) {
5986                                                 goto error_out;
5987                                         }
5988                                         SCTP_BUF_LEN(outchain) = 0;
5989                                         *endofchain = outchain;
5990                                         /* get the prepend space */
5991                                         SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
5992                                 } else {
5993                                         /*
5994                                          * We really should not get a NULL
5995                                          * in endofchain
5996                                          */
5997                                         /* find end */
5998                                         m = outchain;
5999                                         while (m) {
6000                                                 if (SCTP_BUF_NEXT(m) == NULL) {
6001                                                         *endofchain = m;
6002                                                         break;
6003                                                 }
6004                                                 m = SCTP_BUF_NEXT(m);
6005                                         }
6006                                         /* sanity */
6007                                         if (*endofchain == NULL) {
6008                                                 /*
6009                                                  * huh, TSNH XXX maybe we
6010                                                  * should panic
6011                                                  */
6012                                                 sctp_m_freem(outchain);
6013                                                 goto new_mbuf;
6014                                         }
6015                                 }
6016                                 /* get the new end of length */
6017                                 len = M_TRAILINGSPACE(*endofchain);
6018                         } else {
6019                                 /* how much is left at the end? */
6020                                 len = M_TRAILINGSPACE(*endofchain);
6021                         }
6022                         /* Find the end of the data, for appending */
6023                         cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6024
6025                         /* Now lets copy it out */
6026                         if (len >= sizeofcpy) {
6027                                 /* It all fits, copy it in */
6028                                 m_copydata(clonechain, 0, sizeofcpy, cp);
6029                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6030                         } else {
6031                                 /* fill up the end of the chain */
6032                                 if (len > 0) {
6033                                         m_copydata(clonechain, 0, len, cp);
6034                                         SCTP_BUF_LEN((*endofchain)) += len;
6035                                         /* now we need another one */
6036                                         sizeofcpy -= len;
6037                                 }
6038                                 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
6039                                 if (m == NULL) {
6040                                         /* We failed */
6041                                         goto error_out;
6042                                 }
6043                                 SCTP_BUF_NEXT((*endofchain)) = m;
6044                                 *endofchain = m;
6045                                 cp = mtod((*endofchain), caddr_t);
6046                                 m_copydata(clonechain, len, sizeofcpy, cp);
6047                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6048                         }
6049                         return (outchain);
6050                 } else {
6051                         /* copy the old fashion way */
6052                         appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
6053 #ifdef SCTP_MBUF_LOGGING
6054                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6055                                 struct mbuf *mat;
6056
6057                                 mat = appendchain;
6058                                 while (mat) {
6059                                         if (SCTP_BUF_IS_EXTENDED(mat)) {
6060                                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6061                                         }
6062                                         mat = SCTP_BUF_NEXT(mat);
6063                                 }
6064                         }
6065 #endif
6066                 }
6067         }
6068         if (appendchain == NULL) {
6069                 /* error */
6070                 if (outchain)
6071                         sctp_m_freem(outchain);
6072                 return (NULL);
6073         }
6074         if (outchain) {
6075                 /* tack on to the end */
6076                 if (*endofchain != NULL) {
6077                         SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6078                 } else {
6079                         m = outchain;
6080                         while (m) {
6081                                 if (SCTP_BUF_NEXT(m) == NULL) {
6082                                         SCTP_BUF_NEXT(m) = appendchain;
6083                                         break;
6084                                 }
6085                                 m = SCTP_BUF_NEXT(m);
6086                         }
6087                 }
6088                 /*
6089                  * save off the end and update the end-chain postion
6090                  */
6091                 m = appendchain;
6092                 while (m) {
6093                         if (SCTP_BUF_NEXT(m) == NULL) {
6094                                 *endofchain = m;
6095                                 break;
6096                         }
6097                         m = SCTP_BUF_NEXT(m);
6098                 }
6099                 return (outchain);
6100         } else {
6101                 /* save off the end and update the end-chain postion */
6102                 m = appendchain;
6103                 while (m) {
6104                         if (SCTP_BUF_NEXT(m) == NULL) {
6105                                 *endofchain = m;
6106                                 break;
6107                         }
6108                         m = SCTP_BUF_NEXT(m);
6109                 }
6110                 return (appendchain);
6111         }
6112 }
6113
6114 int
6115 sctp_med_chunk_output(struct sctp_inpcb *inp,
6116     struct sctp_tcb *stcb,
6117     struct sctp_association *asoc,
6118     int *num_out,
6119     int *reason_code,
6120     int control_only, int from_where,
6121     struct timeval *now, int *now_filled, int frag_point, int so_locked
6122 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
6123     SCTP_UNUSED
6124 #endif
6125 );
6126
6127 static void
6128 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6129     uint32_t val)
6130 {
6131         struct sctp_copy_all *ca;
6132         struct mbuf *m;
6133         int ret = 0;
6134         int added_control = 0;
6135         int un_sent, do_chunk_output = 1;
6136         struct sctp_association *asoc;
6137
6138         ca = (struct sctp_copy_all *)ptr;
6139         if (ca->m == NULL) {
6140                 return;
6141         }
6142         if (ca->inp != inp) {
6143                 /* TSNH */
6144                 return;
6145         }
6146         if ((ca->m) && ca->sndlen) {
6147                 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
6148                 if (m == NULL) {
6149                         /* can't copy so we are done */
6150                         ca->cnt_failed++;
6151                         return;
6152                 }
6153 #ifdef SCTP_MBUF_LOGGING
6154                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6155                         struct mbuf *mat;
6156
6157                         mat = m;
6158                         while (mat) {
6159                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
6160                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6161                                 }
6162                                 mat = SCTP_BUF_NEXT(mat);
6163                         }
6164                 }
6165 #endif
6166         } else {
6167                 m = NULL;
6168         }
6169         SCTP_TCB_LOCK_ASSERT(stcb);
6170         if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6171                 /* Abort this assoc with m as the user defined reason */
6172                 if (m) {
6173                         struct sctp_paramhdr *ph;
6174
6175                         SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
6176                         if (m) {
6177                                 ph = mtod(m, struct sctp_paramhdr *);
6178                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6179                                 ph->param_length = htons(ca->sndlen);
6180                         }
6181                         /*
6182                          * We add one here to keep the assoc from
6183                          * dis-appearing on us.
6184                          */
6185                         atomic_add_int(&stcb->asoc.refcnt, 1);
6186                         sctp_abort_an_association(inp, stcb,
6187                             SCTP_RESPONSE_TO_USER_REQ,
6188                             m, SCTP_SO_NOT_LOCKED);
6189                         /*
6190                          * sctp_abort_an_association calls sctp_free_asoc()
6191                          * free association will NOT free it since we
6192                          * incremented the refcnt .. we do this to prevent
6193                          * it being freed and things getting tricky since we
6194                          * could end up (from free_asoc) calling inpcb_free
6195                          * which would get a recursive lock call to the
6196                          * iterator lock.. But as a consequence of that the
6197                          * stcb will return to us un-locked.. since
6198                          * free_asoc returns with either no TCB or the TCB
6199                          * unlocked, we must relock.. to unlock in the
6200                          * iterator timer :-0
6201                          */
6202                         SCTP_TCB_LOCK(stcb);
6203                         atomic_add_int(&stcb->asoc.refcnt, -1);
6204                         goto no_chunk_output;
6205                 }
6206         } else {
6207                 if (m) {
6208                         ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
6209                             &ca->sndrcv, 1);
6210                 }
6211                 asoc = &stcb->asoc;
6212                 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6213                         /* shutdown this assoc */
6214                         int cnt;
6215
6216                         cnt = sctp_is_there_unsent_data(stcb);
6217
6218                         if (TAILQ_EMPTY(&asoc->send_queue) &&
6219                             TAILQ_EMPTY(&asoc->sent_queue) &&
6220                             (cnt == 0)) {
6221                                 if (asoc->locked_on_sending) {
6222                                         goto abort_anyway;
6223                                 }
6224                                 /*
6225                                  * there is nothing queued to send, so I'm
6226                                  * done...
6227                                  */
6228                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6229                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6230                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6231                                         /*
6232                                          * only send SHUTDOWN the first time
6233                                          * through
6234                                          */
6235                                         sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
6236                                         if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
6237                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6238                                         }
6239                                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
6240                                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
6241                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6242                                             asoc->primary_destination);
6243                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6244                                             asoc->primary_destination);
6245                                         added_control = 1;
6246                                         do_chunk_output = 0;
6247                                 }
6248                         } else {
6249                                 /*
6250                                  * we still got (or just got) data to send,
6251                                  * so set SHUTDOWN_PENDING
6252                                  */
6253                                 /*
6254                                  * XXX sockets draft says that SCTP_EOF
6255                                  * should be sent with no data.  currently,
6256                                  * we will allow user data to be sent first
6257                                  * and move to SHUTDOWN-PENDING
6258                                  */
6259                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
6260                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6261                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6262                                         if (asoc->locked_on_sending) {
6263                                                 /*
6264                                                  * Locked to send out the
6265                                                  * data
6266                                                  */
6267                                                 struct sctp_stream_queue_pending *sp;
6268
6269                                                 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
6270                                                 if (sp) {
6271                                                         if ((sp->length == 0) && (sp->msg_is_complete == 0))
6272                                                                 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
6273                                                 }
6274                                         }
6275                                         asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
6276                                         if (TAILQ_EMPTY(&asoc->send_queue) &&
6277                                             TAILQ_EMPTY(&asoc->sent_queue) &&
6278                                             (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6279                                 abort_anyway:
6280                                                 atomic_add_int(&stcb->asoc.refcnt, 1);
6281                                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
6282                                                     SCTP_RESPONSE_TO_USER_REQ,
6283                                                     NULL, SCTP_SO_NOT_LOCKED);
6284                                                 atomic_add_int(&stcb->asoc.refcnt, -1);
6285                                                 goto no_chunk_output;
6286                                         }
6287                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6288                                             asoc->primary_destination);
6289                                 }
6290                         }
6291
6292                 }
6293         }
6294         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6295             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
6296
6297         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6298             (stcb->asoc.total_flight > 0) &&
6299             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
6300             ) {
6301                 do_chunk_output = 0;
6302         }
6303         if (do_chunk_output)
6304                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6305         else if (added_control) {
6306                 int num_out = 0, reason = 0, now_filled = 0;
6307                 struct timeval now;
6308                 int frag_point;
6309
6310                 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
6311                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6312                     &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
6313         }
6314 no_chunk_output:
6315         if (ret) {
6316                 ca->cnt_failed++;
6317         } else {
6318                 ca->cnt_sent++;
6319         }
6320 }
6321
6322 static void
6323 sctp_sendall_completes(void *ptr, uint32_t val)
6324 {
6325         struct sctp_copy_all *ca;
6326
6327         ca = (struct sctp_copy_all *)ptr;
6328         /*
6329          * Do a notify here? Kacheong suggests that the notify be done at
6330          * the send time.. so you would push up a notification if any send
6331          * failed. Don't know if this is feasable since the only failures we
6332          * have is "memory" related and if you cannot get an mbuf to send
6333          * the data you surely can't get an mbuf to send up to notify the
6334          * user you can't send the data :->
6335          */
6336
6337         /* now free everything */
6338         sctp_m_freem(ca->m);
6339         SCTP_FREE(ca, SCTP_M_COPYAL);
6340 }
6341
6342
6343 #define MC_ALIGN(m, len) do {                                           \
6344         SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
6345 } while (0)
6346
6347
6348
6349 static struct mbuf *
6350 sctp_copy_out_all(struct uio *uio, int len)
6351 {
6352         struct mbuf *ret, *at;
6353         int left, willcpy, cancpy, error;
6354
6355         ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
6356         if (ret == NULL) {
6357                 /* TSNH */
6358                 return (NULL);
6359         }
6360         left = len;
6361         SCTP_BUF_LEN(ret) = 0;
6362         /* save space for the data chunk header */
6363         cancpy = M_TRAILINGSPACE(ret);
6364         willcpy = min(cancpy, left);
6365         at = ret;
6366         while (left > 0) {
6367                 /* Align data to the end */
6368                 error = uiomove(mtod(at, caddr_t), willcpy, uio);
6369                 if (error) {
6370         err_out_now:
6371                         sctp_m_freem(at);
6372                         return (NULL);
6373                 }
6374                 SCTP_BUF_LEN(at) = willcpy;
6375                 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6376                 left -= willcpy;
6377                 if (left > 0) {
6378                         SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
6379                         if (SCTP_BUF_NEXT(at) == NULL) {
6380                                 goto err_out_now;
6381                         }
6382                         at = SCTP_BUF_NEXT(at);
6383                         SCTP_BUF_LEN(at) = 0;
6384                         cancpy = M_TRAILINGSPACE(at);
6385                         willcpy = min(cancpy, left);
6386                 }
6387         }
6388         return (ret);
6389 }
6390
6391 static int
6392 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6393     struct sctp_sndrcvinfo *srcv)
6394 {
6395         int ret;
6396         struct sctp_copy_all *ca;
6397
6398         SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6399             SCTP_M_COPYAL);
6400         if (ca == NULL) {
6401                 sctp_m_freem(m);
6402                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6403                 return (ENOMEM);
6404         }
6405         memset(ca, 0, sizeof(struct sctp_copy_all));
6406
6407         ca->inp = inp;
6408         memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6409         /*
6410          * take off the sendall flag, it would be bad if we failed to do
6411          * this :-0
6412          */
6413         ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6414         /* get length and mbuf chain */
6415         if (uio) {
6416                 ca->sndlen = uio->uio_resid;
6417                 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6418                 if (ca->m == NULL) {
6419                         SCTP_FREE(ca, SCTP_M_COPYAL);
6420                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6421                         return (ENOMEM);
6422                 }
6423         } else {
6424                 /* Gather the length of the send */
6425                 struct mbuf *mat;
6426
6427                 mat = m;
6428                 ca->sndlen = 0;
6429                 while (m) {
6430                         ca->sndlen += SCTP_BUF_LEN(m);
6431                         m = SCTP_BUF_NEXT(m);
6432                 }
6433                 ca->m = mat;
6434         }
6435         ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6436             SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6437             SCTP_ASOC_ANY_STATE,
6438             (void *)ca, 0,
6439             sctp_sendall_completes, inp, 1);
6440         if (ret) {
6441                 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
6442                 SCTP_FREE(ca, SCTP_M_COPYAL);
6443                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6444                 return (EFAULT);
6445         }
6446         return (0);
6447 }
6448
6449
6450 void
6451 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6452 {
6453         struct sctp_tmit_chunk *chk, *nchk;
6454
6455         chk = TAILQ_FIRST(&asoc->control_send_queue);
6456         while (chk) {
6457                 nchk = TAILQ_NEXT(chk, sctp_next);
6458                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6459                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6460                         if (chk->data) {
6461                                 sctp_m_freem(chk->data);
6462                                 chk->data = NULL;
6463                         }
6464                         asoc->ctrl_queue_cnt--;
6465                         sctp_free_a_chunk(stcb, chk);
6466                 }
6467                 chk = nchk;
6468         }
6469 }
6470
6471 void
6472 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6473 {
6474         struct sctp_association *asoc;
6475         struct sctp_tmit_chunk *chk, *chk_tmp;
6476         struct sctp_asconf_chunk *acp;
6477
6478         asoc = &stcb->asoc;
6479         for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
6480             chk = chk_tmp) {
6481                 /* get next chk */
6482                 chk_tmp = TAILQ_NEXT(chk, sctp_next);
6483                 /* find SCTP_ASCONF chunk in queue */
6484                 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6485                         if (chk->data) {
6486                                 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6487                                 if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
6488                                         /* Not Acked yet */
6489                                         break;
6490                                 }
6491                         }
6492                         TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6493                         if (chk->data) {
6494                                 sctp_m_freem(chk->data);
6495                                 chk->data = NULL;
6496                         }
6497                         asoc->ctrl_queue_cnt--;
6498                         sctp_free_a_chunk(stcb, chk);
6499                 }
6500         }
6501 }
6502
6503
6504 static void
6505 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6506
6507     struct sctp_association *asoc,
6508     struct sctp_tmit_chunk **data_list,
6509     int bundle_at,
6510     struct sctp_nets *net)
6511 {
6512         int i;
6513         struct sctp_tmit_chunk *tp1;
6514
6515         for (i = 0; i < bundle_at; i++) {
6516                 /* off of the send queue */
6517                 if (i) {
6518                         /*
6519                          * Any chunk NOT 0 you zap the time chunk 0 gets
6520                          * zapped or set based on if a RTO measurment is
6521                          * needed.
6522                          */
6523                         data_list[i]->do_rtt = 0;
6524                 }
6525                 /* record time */
6526                 data_list[i]->sent_rcv_time = net->last_sent_time;
6527                 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
6528                 TAILQ_REMOVE(&asoc->send_queue,
6529                     data_list[i],
6530                     sctp_next);
6531                 /* on to the sent queue */
6532                 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6533                 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
6534                     data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
6535                         struct sctp_tmit_chunk *tpp;
6536
6537                         /* need to move back */
6538         back_up_more:
6539                         tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
6540                         if (tpp == NULL) {
6541                                 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
6542                                 goto all_done;
6543                         }
6544                         tp1 = tpp;
6545                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
6546                             data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
6547                                 goto back_up_more;
6548                         }
6549                         TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
6550                 } else {
6551                         TAILQ_INSERT_TAIL(&asoc->sent_queue,
6552                             data_list[i],
6553                             sctp_next);
6554                 }
6555 all_done:
6556                 /* This does not lower until the cum-ack passes it */
6557                 asoc->sent_queue_cnt++;
6558                 asoc->send_queue_cnt--;
6559                 if ((asoc->peers_rwnd <= 0) &&
6560                     (asoc->total_flight == 0) &&
6561                     (bundle_at == 1)) {
6562                         /* Mark the chunk as being a window probe */
6563                         SCTP_STAT_INCR(sctps_windowprobed);
6564                 }
6565 #ifdef SCTP_AUDITING_ENABLED
6566                 sctp_audit_log(0xC2, 3);
6567 #endif
6568                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
6569                 data_list[i]->snd_count = 1;
6570                 data_list[i]->rec.data.chunk_was_revoked = 0;
6571                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
6572                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
6573                             data_list[i]->whoTo->flight_size,
6574                             data_list[i]->book_size,
6575                             (uintptr_t) data_list[i]->whoTo,
6576                             data_list[i]->rec.data.TSN_seq);
6577                 }
6578                 sctp_flight_size_increase(data_list[i]);
6579                 sctp_total_flight_increase(stcb, data_list[i]);
6580                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
6581                         sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
6582                             asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
6583                 }
6584                 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
6585                     (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
6586                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
6587                         /* SWS sender side engages */
6588                         asoc->peers_rwnd = 0;
6589                 }
6590         }
6591 }
6592
6593 static void
6594 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
6595 {
6596         struct sctp_tmit_chunk *chk, *nchk;
6597
6598         for (chk = TAILQ_FIRST(&asoc->control_send_queue);
6599             chk; chk = nchk) {
6600                 nchk = TAILQ_NEXT(chk, sctp_next);
6601                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
6602                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
6603                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
6604                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
6605                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
6606                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
6607                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
6608                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
6609                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
6610                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
6611                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
6612                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
6613                         /* Stray chunks must be cleaned up */
6614         clean_up_anyway:
6615                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6616                         if (chk->data) {
6617                                 sctp_m_freem(chk->data);
6618                                 chk->data = NULL;
6619                         }
6620                         asoc->ctrl_queue_cnt--;
6621                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)
6622                                 asoc->fwd_tsn_cnt--;
6623                         sctp_free_a_chunk(stcb, chk);
6624                 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
6625                         /* special handling, we must look into the param */
6626                         if (chk != asoc->str_reset) {
6627                                 goto clean_up_anyway;
6628                         }
6629                 }
6630         }
6631 }
6632
6633
6634 static int
6635 sctp_can_we_split_this(struct sctp_tcb *stcb,
6636     uint32_t length,
6637     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
6638 {
6639         /*
6640          * Make a decision on if I should split a msg into multiple parts.
6641          * This is only asked of incomplete messages.
6642          */
6643         if (eeor_on) {
6644                 /*
6645                  * If we are doing EEOR we need to always send it if its the
6646                  * entire thing, since it might be all the guy is putting in
6647                  * the hopper.
6648                  */
6649                 if (goal_mtu >= length) {
6650                         /*-
6651                          * If we have data outstanding,
6652                          * we get another chance when the sack
6653                          * arrives to transmit - wait for more data
6654                          */
6655                         if (stcb->asoc.total_flight == 0) {
6656                                 /*
6657                                  * If nothing is in flight, we zero the
6658                                  * packet counter.
6659                                  */
6660                                 return (length);
6661                         }
6662                         return (0);
6663
6664                 } else {
6665                         /* You can fill the rest */
6666                         return (goal_mtu);
6667                 }
6668         }
6669         /*-
6670          * For those strange folk that make the send buffer
6671          * smaller than our fragmentation point, we can't
6672          * get a full msg in so we have to allow splitting.
6673          */
6674         if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
6675                 return (length);
6676         }
6677         if ((length <= goal_mtu) ||
6678             ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
6679                 /* Sub-optimial residual don't split in non-eeor mode. */
6680                 return (0);
6681         }
6682         /*
6683          * If we reach here length is larger than the goal_mtu. Do we wish
6684          * to split it for the sake of packet putting together?
6685          */
6686         if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
6687                 /* Its ok to split it */
6688                 return (min(goal_mtu, frag_point));
6689         }
6690         /* Nope, can't split */
6691         return (0);
6692
6693 }
6694
6695 static uint32_t
6696 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
6697     struct sctp_stream_out *strq,
6698     uint32_t goal_mtu,
6699     uint32_t frag_point,
6700     int *locked,
6701     int *giveup,
6702     int eeor_mode,
6703     int *bail)
6704 {
6705         /* Move from the stream to the send_queue keeping track of the total */
6706         struct sctp_association *asoc;
6707         struct sctp_stream_queue_pending *sp;
6708         struct sctp_tmit_chunk *chk;
6709         struct sctp_data_chunk *dchkh;
6710         uint32_t to_move, length;
6711         uint8_t rcv_flags = 0;
6712         uint8_t some_taken;
6713         uint8_t send_lock_up = 0;
6714
6715         SCTP_TCB_LOCK_ASSERT(stcb);
6716         asoc = &stcb->asoc;
6717 one_more_time:
6718         /* sa_ignore FREED_MEMORY */
6719         sp = TAILQ_FIRST(&strq->outqueue);
6720         if (sp == NULL) {
6721                 *locked = 0;
6722                 if (send_lock_up == 0) {
6723                         SCTP_TCB_SEND_LOCK(stcb);
6724                         send_lock_up = 1;
6725                 }
6726                 sp = TAILQ_FIRST(&strq->outqueue);
6727                 if (sp) {
6728                         goto one_more_time;
6729                 }
6730                 if (strq->last_msg_incomplete) {
6731                         SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
6732                             strq->stream_no,
6733                             strq->last_msg_incomplete);
6734                         strq->last_msg_incomplete = 0;
6735                 }
6736                 to_move = 0;
6737                 if (send_lock_up) {
6738                         SCTP_TCB_SEND_UNLOCK(stcb);
6739                         send_lock_up = 0;
6740                 }
6741                 goto out_of;
6742         }
6743         if ((sp->msg_is_complete) && (sp->length == 0)) {
6744                 if (sp->sender_all_done) {
6745                         /*
6746                          * We are doing differed cleanup. Last time through
6747                          * when we took all the data the sender_all_done was
6748                          * not set.
6749                          */
6750                         if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
6751                                 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
6752                                 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
6753                                     sp->sender_all_done,
6754                                     sp->length,
6755                                     sp->msg_is_complete,
6756                                     sp->put_last_out,
6757                                     send_lock_up);
6758                         }
6759                         if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
6760                                 SCTP_TCB_SEND_LOCK(stcb);
6761                                 send_lock_up = 1;
6762                         }
6763                         atomic_subtract_int(&asoc->stream_queue_cnt, 1);
6764                         TAILQ_REMOVE(&strq->outqueue, sp, next);
6765                         sctp_free_remote_addr(sp->net);
6766                         if (sp->data) {
6767                                 sctp_m_freem(sp->data);
6768                                 sp->data = NULL;
6769                         }
6770                         sctp_free_a_strmoq(stcb, sp);
6771                         /* we can't be locked to it */
6772                         *locked = 0;
6773                         stcb->asoc.locked_on_sending = NULL;
6774                         if (send_lock_up) {
6775                                 SCTP_TCB_SEND_UNLOCK(stcb);
6776                                 send_lock_up = 0;
6777                         }
6778                         /* back to get the next msg */
6779                         goto one_more_time;
6780                 } else {
6781                         /*
6782                          * sender just finished this but still holds a
6783                          * reference
6784                          */
6785                         *locked = 1;
6786                         *giveup = 1;
6787                         to_move = 0;
6788                         goto out_of;
6789                 }
6790         } else {
6791                 /* is there some to get */
6792                 if (sp->length == 0) {
6793                         /* no */
6794                         *locked = 1;
6795                         *giveup = 1;
6796                         to_move = 0;
6797                         goto out_of;
6798                 } else if (sp->discard_rest) {
6799                         if (send_lock_up == 0) {
6800                                 SCTP_TCB_SEND_LOCK(stcb);
6801                                 send_lock_up = 1;
6802                         }
6803                         /* Whack down the size */
6804                         atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
6805                         if ((stcb->sctp_socket != NULL) && \
6806                             ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6807                             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6808                                 atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc, sp->length);
6809                         }
6810                         if (sp->data) {
6811                                 sctp_m_freem(sp->data);
6812                                 sp->data = NULL;
6813                                 sp->tail_mbuf = NULL;
6814                         }
6815                         sp->length = 0;
6816                         sp->some_taken = 1;
6817                         *locked = 1;
6818                         *giveup = 1;
6819                         to_move = 0;
6820                         goto out_of;
6821                 }
6822         }
6823         some_taken = sp->some_taken;
6824         if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
6825                 sp->msg_is_complete = 1;
6826         }
6827 re_look:
6828         length = sp->length;
6829         if (sp->msg_is_complete) {
6830                 /* The message is complete */
6831                 to_move = min(length, frag_point);
6832                 if (to_move == length) {
6833                         /* All of it fits in the MTU */
6834                         if (sp->some_taken) {
6835                                 rcv_flags |= SCTP_DATA_LAST_FRAG;
6836                                 sp->put_last_out = 1;
6837                         } else {
6838                                 rcv_flags |= SCTP_DATA_NOT_FRAG;
6839                                 sp->put_last_out = 1;
6840                         }
6841                 } else {
6842                         /* Not all of it fits, we fragment */
6843                         if (sp->some_taken == 0) {
6844                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6845                         }
6846                         sp->some_taken = 1;
6847                 }
6848         } else {
6849                 to_move = sctp_can_we_split_this(stcb, length, goal_mtu, frag_point, eeor_mode);
6850                 if (to_move) {
6851                         /*-
6852                          * We use a snapshot of length in case it
6853                          * is expanding during the compare.
6854                          */
6855                         uint32_t llen;
6856
6857                         llen = length;
6858                         if (to_move >= llen) {
6859                                 to_move = llen;
6860                                 if (send_lock_up == 0) {
6861                                         /*-
6862                                          * We are taking all of an incomplete msg
6863                                          * thus we need a send lock.
6864                                          */
6865                                         SCTP_TCB_SEND_LOCK(stcb);
6866                                         send_lock_up = 1;
6867                                         if (sp->msg_is_complete) {
6868                                                 /*
6869                                                  * the sender finished the
6870                                                  * msg
6871                                                  */
6872                                                 goto re_look;
6873                                         }
6874                                 }
6875                         }
6876                         if (sp->some_taken == 0) {
6877                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
6878                                 sp->some_taken = 1;
6879                         }
6880                 } else {
6881                         /* Nothing to take. */
6882                         if (sp->some_taken) {
6883                                 *locked = 1;
6884                         }
6885                         *giveup = 1;
6886                         to_move = 0;
6887                         goto out_of;
6888                 }
6889         }
6890
6891         /* If we reach here, we can copy out a chunk */
6892         sctp_alloc_a_chunk(stcb, chk);
6893         if (chk == NULL) {
6894                 /* No chunk memory */
6895                 *giveup = 1;
6896                 to_move = 0;
6897                 goto out_of;
6898         }
6899         /*
6900          * Setup for unordered if needed by looking at the user sent info
6901          * flags.
6902          */
6903         if (sp->sinfo_flags & SCTP_UNORDERED) {
6904                 rcv_flags |= SCTP_DATA_UNORDERED;
6905         }
6906         if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
6907             ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
6908                 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
6909         }
6910         /* clear out the chunk before setting up */
6911         memset(chk, 0, sizeof(*chk));
6912         chk->rec.data.rcv_flags = rcv_flags;
6913
6914         if (to_move >= length) {
6915                 /* we think we can steal the whole thing */
6916                 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
6917                         SCTP_TCB_SEND_LOCK(stcb);
6918                         send_lock_up = 1;
6919                 }
6920                 if (to_move < sp->length) {
6921                         /* bail, it changed */
6922                         goto dont_do_it;
6923                 }
6924                 chk->data = sp->data;
6925                 chk->last_mbuf = sp->tail_mbuf;
6926                 /* register the stealing */
6927                 sp->data = sp->tail_mbuf = NULL;
6928         } else {
6929                 struct mbuf *m;
6930
6931 dont_do_it:
6932                 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
6933                 chk->last_mbuf = NULL;
6934                 if (chk->data == NULL) {
6935                         sp->some_taken = some_taken;
6936                         sctp_free_a_chunk(stcb, chk);
6937                         *bail = 1;
6938                         to_move = 0;
6939                         goto out_of;
6940                 }
6941 #ifdef SCTP_MBUF_LOGGING
6942                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6943                         struct mbuf *mat;
6944
6945                         mat = chk->data;
6946                         while (mat) {
6947                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
6948                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
6949                                 }
6950                                 mat = SCTP_BUF_NEXT(mat);
6951                         }
6952                 }
6953 #endif
6954                 /* Pull off the data */
6955                 m_adj(sp->data, to_move);
6956                 /* Now lets work our way down and compact it */
6957                 m = sp->data;
6958                 while (m && (SCTP_BUF_LEN(m) == 0)) {
6959                         sp->data = SCTP_BUF_NEXT(m);
6960                         SCTP_BUF_NEXT(m) = NULL;
6961                         if (sp->tail_mbuf == m) {
6962                                 /*-
6963                                  * Freeing tail? TSNH since
6964                                  * we supposedly were taking less
6965                                  * than the sp->length.
6966                                  */
6967 #ifdef INVARIANTS
6968                                 panic("Huh, freing tail? - TSNH");
6969 #else
6970                                 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
6971                                 sp->tail_mbuf = sp->data = NULL;
6972                                 sp->length = 0;
6973 #endif
6974
6975                         }
6976                         sctp_m_free(m);
6977                         m = sp->data;
6978                 }
6979         }
6980         if (SCTP_BUF_IS_EXTENDED(chk->data)) {
6981                 chk->copy_by_ref = 1;
6982         } else {
6983                 chk->copy_by_ref = 0;
6984         }
6985         /*
6986          * get last_mbuf and counts of mb useage This is ugly but hopefully
6987          * its only one mbuf.
6988          */
6989         if (chk->last_mbuf == NULL) {
6990                 chk->last_mbuf = chk->data;
6991                 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
6992                         chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
6993                 }
6994         }
6995         if (to_move > length) {
6996                 /*- This should not happen either
6997                  * since we always lower to_move to the size
6998                  * of sp->length if its larger.
6999                  */
7000 #ifdef INVARIANTS
7001                 panic("Huh, how can to_move be larger?");
7002 #else
7003                 SCTP_PRINTF("Huh, how can to_move be larger?\n");
7004                 sp->length = 0;
7005 #endif
7006         } else {
7007                 atomic_subtract_int(&sp->length, to_move);
7008         }
7009         if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
7010                 /* Not enough room for a chunk header, get some */
7011                 struct mbuf *m;
7012
7013                 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
7014                 if (m == NULL) {
7015                         /*
7016                          * we're in trouble here. _PREPEND below will free
7017                          * all the data if there is no leading space, so we
7018                          * must put the data back and restore.
7019                          */
7020                         if (send_lock_up == 0) {
7021                                 SCTP_TCB_SEND_LOCK(stcb);
7022                                 send_lock_up = 1;
7023                         }
7024                         if (chk->data == NULL) {
7025                                 /* unsteal the data */
7026                                 sp->data = chk->data;
7027                                 sp->tail_mbuf = chk->last_mbuf;
7028                         } else {
7029                                 struct mbuf *m_tmp;
7030
7031                                 /* reassemble the data */
7032                                 m_tmp = sp->data;
7033                                 sp->data = chk->data;
7034                                 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7035                         }
7036                         sp->some_taken = some_taken;
7037                         atomic_add_int(&sp->length, to_move);
7038                         chk->data = NULL;
7039                         *bail = 1;
7040                         sctp_free_a_chunk(stcb, chk);
7041                         to_move = 0;
7042                         goto out_of;
7043                 } else {
7044                         SCTP_BUF_LEN(m) = 0;
7045                         SCTP_BUF_NEXT(m) = chk->data;
7046                         chk->data = m;
7047                         M_ALIGN(chk->data, 4);
7048                 }
7049         }
7050         SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
7051         if (chk->data == NULL) {
7052                 /* HELP, TSNH since we assured it would not above? */
7053 #ifdef INVARIANTS
7054                 panic("prepend failes HELP?");
7055 #else
7056                 SCTP_PRINTF("prepend fails HELP?\n");
7057                 sctp_free_a_chunk(stcb, chk);
7058 #endif
7059                 *bail = 1;
7060                 to_move = 0;
7061                 goto out_of;
7062         }
7063         sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
7064         chk->book_size = chk->send_size = (to_move + sizeof(struct sctp_data_chunk));
7065         chk->book_size_scale = 0;
7066         chk->sent = SCTP_DATAGRAM_UNSENT;
7067
7068         chk->flags = 0;
7069         chk->asoc = &stcb->asoc;
7070         chk->pad_inplace = 0;
7071         chk->no_fr_allowed = 0;
7072         chk->rec.data.stream_seq = sp->strseq;
7073         chk->rec.data.stream_number = sp->stream;
7074         chk->rec.data.payloadtype = sp->ppid;
7075         chk->rec.data.context = sp->context;
7076         chk->rec.data.doing_fast_retransmit = 0;
7077         chk->rec.data.ect_nonce = 0;    /* ECN Nonce */
7078
7079         chk->rec.data.timetodrop = sp->ts;
7080         chk->flags = sp->act_flags;
7081
7082         chk->whoTo = net;
7083         atomic_add_int(&chk->whoTo->ref_count, 1);
7084
7085         if (sp->holds_key_ref) {
7086                 chk->auth_keyid = sp->auth_keyid;
7087                 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7088                 chk->holds_key_ref = 1;
7089         }
7090         chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
7091         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7092                 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7093                     (uintptr_t) stcb, sp->length,
7094                     (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
7095                     chk->rec.data.TSN_seq);
7096         }
7097         dchkh = mtod(chk->data, struct sctp_data_chunk *);
7098         /*
7099          * Put the rest of the things in place now. Size was done earlier in
7100          * previous loop prior to padding.
7101          */
7102
7103 #ifdef SCTP_ASOCLOG_OF_TSNS
7104         SCTP_TCB_LOCK_ASSERT(stcb);
7105         if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7106                 asoc->tsn_out_at = 0;
7107                 asoc->tsn_out_wrapped = 1;
7108         }
7109         asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
7110         asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
7111         asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
7112         asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7113         asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7114         asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7115         asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7116         asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7117         asoc->tsn_out_at++;
7118 #endif
7119
7120         dchkh->ch.chunk_type = SCTP_DATA;
7121         dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7122         dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
7123         dchkh->dp.stream_id = htons(strq->stream_no);
7124         dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
7125         dchkh->dp.protocol_id = chk->rec.data.payloadtype;
7126         dchkh->ch.chunk_length = htons(chk->send_size);
7127         /* Now advance the chk->send_size by the actual pad needed. */
7128         if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7129                 /* need a pad */
7130                 struct mbuf *lm;
7131                 int pads;
7132
7133                 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7134                 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
7135                         chk->pad_inplace = 1;
7136                 }
7137                 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
7138                         /* pad added an mbuf */
7139                         chk->last_mbuf = lm;
7140                 }
7141                 chk->send_size += pads;
7142         }
7143         /* We only re-set the policy if it is on */
7144         if (sp->pr_sctp_on) {
7145                 sctp_set_prsctp_policy(sp);
7146                 asoc->pr_sctp_cnt++;
7147                 chk->pr_sctp_on = 1;
7148         } else {
7149                 chk->pr_sctp_on = 0;
7150         }
7151         if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7152                 /* All done pull and kill the message */
7153                 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7154                 if (sp->put_last_out == 0) {
7155                         SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7156                         SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
7157                             sp->sender_all_done,
7158                             sp->length,
7159                             sp->msg_is_complete,
7160                             sp->put_last_out,
7161                             send_lock_up);
7162                 }
7163                 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
7164                         SCTP_TCB_SEND_LOCK(stcb);
7165                         send_lock_up = 1;
7166                 }
7167                 TAILQ_REMOVE(&strq->outqueue, sp, next);
7168                 sctp_free_remote_addr(sp->net);
7169                 if (sp->data) {
7170                         sctp_m_freem(sp->data);
7171                         sp->data = NULL;
7172                 }
7173                 sctp_free_a_strmoq(stcb, sp);
7174
7175                 /* we can't be locked to it */
7176                 *locked = 0;
7177                 stcb->asoc.locked_on_sending = NULL;
7178         } else {
7179                 /* more to go, we are locked */
7180                 *locked = 1;
7181         }
7182         asoc->chunks_on_out_queue++;
7183         TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7184         asoc->send_queue_cnt++;
7185 out_of:
7186         if (send_lock_up) {
7187                 SCTP_TCB_SEND_UNLOCK(stcb);
7188                 send_lock_up = 0;
7189         }
7190         return (to_move);
7191 }
7192
7193
7194 static struct sctp_stream_out *
7195 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
7196 {
7197         struct sctp_stream_out *strq;
7198
7199         /* Find the next stream to use */
7200         if (asoc->last_out_stream == NULL) {
7201                 strq = TAILQ_FIRST(&asoc->out_wheel);
7202         } else {
7203                 strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
7204                 if (strq == NULL) {
7205                         strq = TAILQ_FIRST(&asoc->out_wheel);
7206                 }
7207         }
7208         return (strq);
7209 }
7210
7211
7212 static void
7213 sctp_fill_outqueue(struct sctp_tcb *stcb,
7214     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
7215 {
7216         struct sctp_association *asoc;
7217         struct sctp_stream_out *strq, *strqn;
7218         int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
7219         int locked, giveup;
7220         struct sctp_stream_queue_pending *sp;
7221
7222         SCTP_TCB_LOCK_ASSERT(stcb);
7223         asoc = &stcb->asoc;
7224 #ifdef INET6
7225         if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
7226                 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7227         } else {
7228                 /* ?? not sure what else to do */
7229                 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7230         }
7231 #else
7232         goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
7233 #endif
7234         /* Need an allowance for the data chunk header too */
7235         goal_mtu -= sizeof(struct sctp_data_chunk);
7236
7237         /* must make even word boundary */
7238         goal_mtu &= 0xfffffffc;
7239         if (asoc->locked_on_sending) {
7240                 /* We are stuck on one stream until the message completes. */
7241                 strqn = strq = asoc->locked_on_sending;
7242                 locked = 1;
7243         } else {
7244                 strqn = strq = sctp_select_a_stream(stcb, asoc);
7245                 locked = 0;
7246         }
7247
7248         while ((goal_mtu > 0) && strq) {
7249                 sp = TAILQ_FIRST(&strq->outqueue);
7250                 /*
7251                  * If CMT is off, we must validate that the stream in
7252                  * question has the first item pointed towards are network
7253                  * destionation requested by the caller. Note that if we
7254                  * turn out to be locked to a stream (assigning TSN's then
7255                  * we must stop, since we cannot look for another stream
7256                  * with data to send to that destination). In CMT's case, by
7257                  * skipping this check, we will send one data packet towards
7258                  * the requested net.
7259                  */
7260                 if (sp == NULL) {
7261                         break;
7262                 }
7263                 if ((sp->net != net) &&
7264                     (asoc->sctp_cmt_on_off == 0)) {
7265                         /* none for this network */
7266                         if (locked) {
7267                                 break;
7268                         } else {
7269                                 strq = sctp_select_a_stream(stcb, asoc);
7270                                 if (strq == NULL)
7271                                         /* none left */
7272                                         break;
7273                                 if (strqn == strq) {
7274                                         /* I have circled */
7275                                         break;
7276                                 }
7277                                 continue;
7278                         }
7279                 }
7280                 giveup = 0;
7281                 bail = 0;
7282                 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
7283                     &giveup, eeor_mode, &bail);
7284                 if (moved_how_much)
7285                         asoc->last_out_stream = strq;
7286
7287                 if (locked) {
7288                         asoc->locked_on_sending = strq;
7289                         if ((moved_how_much == 0) || (giveup) || bail)
7290                                 /* no more to move for now */
7291                                 break;
7292                 } else {
7293                         asoc->locked_on_sending = NULL;
7294                         if (TAILQ_EMPTY(&strq->outqueue)) {
7295                                 if (strq == strqn) {
7296                                         /* Must move start to next one */
7297                                         strqn = TAILQ_NEXT(strq, next_spoke);
7298                                         if (strqn == NULL) {
7299                                                 strqn = TAILQ_FIRST(&asoc->out_wheel);
7300                                                 if (strqn == NULL) {
7301                                                         break;
7302                                                 }
7303                                         }
7304                                 }
7305                                 sctp_remove_from_wheel(stcb, asoc, strq, 0);
7306                         }
7307                         if ((giveup) || bail) {
7308                                 break;
7309                         }
7310                         strq = sctp_select_a_stream(stcb, asoc);
7311                         if (strq == NULL) {
7312                                 break;
7313                         }
7314                 }
7315                 total_moved += moved_how_much;
7316                 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
7317                 goal_mtu &= 0xfffffffc;
7318         }
7319         if (bail)
7320                 *quit_now = 1;
7321
7322         if (total_moved == 0) {
7323                 if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7324                     (net == stcb->asoc.primary_destination)) {
7325                         /* ran dry for primary network net */
7326                         SCTP_STAT_INCR(sctps_primary_randry);
7327                 } else if (stcb->asoc.sctp_cmt_on_off == 1) {
7328                         /* ran dry with CMT on */
7329                         SCTP_STAT_INCR(sctps_cmt_randry);
7330                 }
7331         }
7332 }
7333
7334 void
7335 sctp_fix_ecn_echo(struct sctp_association *asoc)
7336 {
7337         struct sctp_tmit_chunk *chk;
7338
7339         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7340                 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7341                         chk->sent = SCTP_DATAGRAM_UNSENT;
7342                 }
7343         }
7344 }
7345
7346 static void
7347 sctp_move_to_an_alt(struct sctp_tcb *stcb,
7348     struct sctp_association *asoc,
7349     struct sctp_nets *net)
7350 {
7351         struct sctp_tmit_chunk *chk;
7352         struct sctp_nets *a_net;
7353
7354         SCTP_TCB_LOCK_ASSERT(stcb);
7355         /*
7356          * JRS 5/14/07 - If CMT PF is turned on, find an alternate
7357          * destination using the PF algorithm for finding alternate
7358          * destinations.
7359          */
7360         if ((asoc->sctp_cmt_on_off == 1) &&
7361             (asoc->sctp_cmt_pf > 0)) {
7362                 a_net = sctp_find_alternate_net(stcb, net, 2);
7363         } else {
7364                 a_net = sctp_find_alternate_net(stcb, net, 0);
7365         }
7366         if ((a_net != net) &&
7367             ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
7368                 /*
7369                  * We only proceed if a valid alternate is found that is not
7370                  * this one and is reachable. Here we must move all chunks
7371                  * queued in the send queue off of the destination address
7372                  * to our alternate.
7373                  */
7374                 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7375                         if (chk->whoTo == net) {
7376                                 /* Move the chunk to our alternate */
7377                                 sctp_free_remote_addr(chk->whoTo);
7378                                 chk->whoTo = a_net;
7379                                 atomic_add_int(&a_net->ref_count, 1);
7380                         }
7381                 }
7382         }
7383 }
7384
7385 int
7386 sctp_med_chunk_output(struct sctp_inpcb *inp,
7387     struct sctp_tcb *stcb,
7388     struct sctp_association *asoc,
7389     int *num_out,
7390     int *reason_code,
7391     int control_only, int from_where,
7392     struct timeval *now, int *now_filled, int frag_point, int so_locked
7393 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7394     SCTP_UNUSED
7395 #endif
7396 )
7397 {
7398         /*
7399          * Ok this is the generic chunk service queue. we must do the
7400          * following: - Service the stream queue that is next, moving any
7401          * message (note I must get a complete message i.e. FIRST/MIDDLE and
7402          * LAST to the out queue in one pass) and assigning TSN's - Check to
7403          * see if the cwnd/rwnd allows any output, if so we go ahead and
7404          * fomulate and send the low level chunks. Making sure to combine
7405          * any control in the control chunk queue also.
7406          */
7407         struct sctp_nets *net, *start_at, *old_start_at = NULL;
7408         struct mbuf *outchain, *endoutchain;
7409         struct sctp_tmit_chunk *chk, *nchk;
7410
7411         /* temp arrays for unlinking */
7412         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7413         int no_fragmentflg, error;
7414         unsigned int max_rwnd_per_dest;
7415         int one_chunk, hbflag, skip_data_for_this_net;
7416         int asconf, cookie, no_out_cnt;
7417         int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7418         unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7419         int tsns_sent = 0;
7420         uint32_t auth_offset = 0;
7421         struct sctp_auth_chunk *auth = NULL;
7422         uint16_t auth_keyid;
7423         int override_ok = 1;
7424         int data_auth_reqd = 0;
7425
7426         /*
7427          * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7428          * destination.
7429          */
7430         int pf_hbflag = 0;
7431         int quit_now = 0;
7432
7433         *num_out = 0;
7434         auth_keyid = stcb->asoc.authinfo.active_keyid;
7435
7436         if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7437             (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
7438             (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7439                 eeor_mode = 1;
7440         } else {
7441                 eeor_mode = 0;
7442         }
7443         ctl_cnt = no_out_cnt = asconf = cookie = 0;
7444         /*
7445          * First lets prime the pump. For each destination, if there is room
7446          * in the flight size, attempt to pull an MTU's worth out of the
7447          * stream queues into the general send_queue
7448          */
7449 #ifdef SCTP_AUDITING_ENABLED
7450         sctp_audit_log(0xC2, 2);
7451 #endif
7452         SCTP_TCB_LOCK_ASSERT(stcb);
7453         hbflag = 0;
7454         if ((control_only) || (asoc->stream_reset_outstanding))
7455                 no_data_chunks = 1;
7456         else
7457                 no_data_chunks = 0;
7458
7459         /* Nothing to possible to send? */
7460         if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7461             TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7462             TAILQ_EMPTY(&asoc->send_queue) &&
7463             TAILQ_EMPTY(&asoc->out_wheel)) {
7464                 *reason_code = 9;
7465                 return (0);
7466         }
7467         if (asoc->peers_rwnd == 0) {
7468                 /* No room in peers rwnd */
7469                 *reason_code = 1;
7470                 if (asoc->total_flight > 0) {
7471                         /* we are allowed one chunk in flight */
7472                         no_data_chunks = 1;
7473                 }
7474         }
7475         max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7476         if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
7477                 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7478                         /*
7479                          * This for loop we are in takes in each net, if
7480                          * its's got space in cwnd and has data sent to it
7481                          * (when CMT is off) then it calls
7482                          * sctp_fill_outqueue for the net. This gets data on
7483                          * the send queue for that network.
7484                          * 
7485                          * In sctp_fill_outqueue TSN's are assigned and data is
7486                          * copied out of the stream buffers. Note mostly
7487                          * copy by reference (we hope).
7488                          */
7489                         net->window_probe = 0;
7490                         if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
7491                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7492                                         sctp_log_cwnd(stcb, net, 1,
7493                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7494                                 }
7495                                 continue;
7496                         }
7497                         if ((asoc->sctp_cmt_on_off == 0) &&
7498                             (net->ref_count < 2)) {
7499                                 /* nothing can be in queue for this guy */
7500                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7501                                         sctp_log_cwnd(stcb, net, 2,
7502                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7503                                 }
7504                                 continue;
7505                         }
7506                         if (net->flight_size >= net->cwnd) {
7507                                 /* skip this network, no room - can't fill */
7508                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7509                                         sctp_log_cwnd(stcb, net, 3,
7510                                             SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7511                                 }
7512                                 continue;
7513                         }
7514                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7515                                 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7516                         }
7517                         sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
7518                         if (quit_now) {
7519                                 /* memory alloc failure */
7520                                 no_data_chunks = 1;
7521                                 break;
7522                         }
7523                 }
7524         }
7525         /* now service each destination and send out what we can for it */
7526         /* Nothing to send? */
7527         if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7528             (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7529             (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
7530                 *reason_code = 8;
7531                 return (0);
7532         }
7533         if (asoc->sctp_cmt_on_off == 1) {
7534                 /* get the last start point */
7535                 start_at = asoc->last_net_cmt_send_started;
7536                 if (start_at == NULL) {
7537                         /* null so to beginning */
7538                         start_at = TAILQ_FIRST(&asoc->nets);
7539                 } else {
7540                         start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7541                         if (start_at == NULL) {
7542                                 start_at = TAILQ_FIRST(&asoc->nets);
7543                         }
7544                 }
7545                 asoc->last_net_cmt_send_started = start_at;
7546         } else {
7547                 start_at = TAILQ_FIRST(&asoc->nets);
7548         }
7549         old_start_at = NULL;
7550 again_one_more_time:
7551         for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7552                 /* how much can we send? */
7553                 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7554                 if (old_start_at && (old_start_at == net)) {
7555                         /* through list ocmpletely. */
7556                         break;
7557                 }
7558                 tsns_sent = 0xa;
7559                 if ((asoc->sctp_cmt_on_off == 0) && (net->ref_count < 2)) {
7560                         /*
7561                          * Ref-count of 1 so we cannot have data or control
7562                          * queued to this address. Skip it (non-CMT).
7563                          */
7564                         continue;
7565                 }
7566                 if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
7567                     (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
7568                     (net->flight_size >= net->cwnd)) {
7569                         /*
7570                          * Nothing on control or asconf and flight is full,
7571                          * we can skip even in the CMT case.
7572                          */
7573                         continue;
7574                 }
7575                 ctl_cnt = bundle_at = 0;
7576                 endoutchain = outchain = NULL;
7577                 no_fragmentflg = 1;
7578                 one_chunk = 0;
7579                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7580                         skip_data_for_this_net = 1;
7581                 } else {
7582                         skip_data_for_this_net = 0;
7583                 }
7584                 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
7585                         /*
7586                          * if we have a route and an ifp check to see if we
7587                          * have room to send to this guy
7588                          */
7589                         struct ifnet *ifp;
7590
7591                         ifp = net->ro.ro_rt->rt_ifp;
7592                         if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
7593                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
7594                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
7595                                         sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
7596                                 }
7597                                 continue;
7598                         }
7599                 }
7600                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7601                 case AF_INET:
7602                         mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
7603                         break;
7604 #ifdef INET6
7605                 case AF_INET6:
7606                         mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
7607                         break;
7608 #endif
7609                 default:
7610                         /* TSNH */
7611                         mtu = net->mtu;
7612                         break;
7613                 }
7614                 mx_mtu = mtu;
7615                 to_out = 0;
7616                 if (mtu > asoc->peers_rwnd) {
7617                         if (asoc->total_flight > 0) {
7618                                 /* We have a packet in flight somewhere */
7619                                 r_mtu = asoc->peers_rwnd;
7620                         } else {
7621                                 /* We are always allowed to send one MTU out */
7622                                 one_chunk = 1;
7623                                 r_mtu = mtu;
7624                         }
7625                 } else {
7626                         r_mtu = mtu;
7627                 }
7628                 /************************/
7629                 /* ASCONF transmission */
7630                 /************************/
7631                 /* Now first lets go through the asconf queue */
7632                 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
7633                     chk; chk = nchk) {
7634                         nchk = TAILQ_NEXT(chk, sctp_next);
7635                         if (chk->rec.chunk_id.id != SCTP_ASCONF) {
7636                                 continue;
7637                         }
7638                         if (chk->whoTo != net) {
7639                                 /*
7640                                  * No, not sent to the network we are
7641                                  * looking at
7642                                  */
7643                                 break;
7644                         }
7645                         if (chk->data == NULL) {
7646                                 break;
7647                         }
7648                         if (chk->sent != SCTP_DATAGRAM_UNSENT &&
7649                             chk->sent != SCTP_DATAGRAM_RESEND) {
7650                                 break;
7651                         }
7652                         /*
7653                          * if no AUTH is yet included and this chunk
7654                          * requires it, make sure to account for it.  We
7655                          * don't apply the size until the AUTH chunk is
7656                          * actually added below in case there is no room for
7657                          * this chunk. NOTE: we overload the use of "omtu"
7658                          * here
7659                          */
7660                         if ((auth == NULL) &&
7661                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7662                             stcb->asoc.peer_auth_chunks)) {
7663                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7664                         } else
7665                                 omtu = 0;
7666                         /* Here we do NOT factor the r_mtu */
7667                         if ((chk->send_size < (int)(mtu - omtu)) ||
7668                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7669                                 /*
7670                                  * We probably should glom the mbuf chain
7671                                  * from the chk->data for control but the
7672                                  * problem is it becomes yet one more level
7673                                  * of tracking to do if for some reason
7674                                  * output fails. Then I have got to
7675                                  * reconstruct the merged control chain.. el
7676                                  * yucko.. for now we take the easy way and
7677                                  * do the copy
7678                                  */
7679                                 /*
7680                                  * Add an AUTH chunk, if chunk requires it
7681                                  * save the offset into the chain for AUTH
7682                                  */
7683                                 if ((auth == NULL) &&
7684                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7685                                     stcb->asoc.peer_auth_chunks))) {
7686                                         outchain = sctp_add_auth_chunk(outchain,
7687                                             &endoutchain,
7688                                             &auth,
7689                                             &auth_offset,
7690                                             stcb,
7691                                             chk->rec.chunk_id.id);
7692                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7693                                 }
7694                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7695                                     (int)chk->rec.chunk_id.can_take_data,
7696                                     chk->send_size, chk->copy_by_ref);
7697                                 if (outchain == NULL) {
7698                                         *reason_code = 8;
7699                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7700                                         return (ENOMEM);
7701                                 }
7702                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7703                                 /* update our MTU size */
7704                                 if (mtu > (chk->send_size + omtu))
7705                                         mtu -= (chk->send_size + omtu);
7706                                 else
7707                                         mtu = 0;
7708                                 to_out += (chk->send_size + omtu);
7709                                 /* Do clear IP_DF ? */
7710                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7711                                         no_fragmentflg = 0;
7712                                 }
7713                                 if (chk->rec.chunk_id.can_take_data)
7714                                         chk->data = NULL;
7715                                 /*
7716                                  * set hb flag since we can use these for
7717                                  * RTO
7718                                  */
7719                                 hbflag = 1;
7720                                 asconf = 1;
7721                                 /*
7722                                  * should sysctl this: don't bundle data
7723                                  * with ASCONF since it requires AUTH
7724                                  */
7725                                 no_data_chunks = 1;
7726                                 chk->sent = SCTP_DATAGRAM_SENT;
7727                                 chk->snd_count++;
7728                                 if (mtu == 0) {
7729                                         /*
7730                                          * Ok we are out of room but we can
7731                                          * output without effecting the
7732                                          * flight size since this little guy
7733                                          * is a control only packet.
7734                                          */
7735                                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7736                                         /*
7737                                          * do NOT clear the asconf flag as
7738                                          * it is used to do appropriate
7739                                          * source address selection.
7740                                          */
7741                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7742                                             (struct sockaddr *)&net->ro._l_addr,
7743                                             outchain, auth_offset, auth,
7744                                             stcb->asoc.authinfo.active_keyid,
7745                                             no_fragmentflg, 0, NULL, asconf,
7746                                             inp->sctp_lport, stcb->rport,
7747                                             htonl(stcb->asoc.peer_vtag),
7748                                             net->port, so_locked, NULL))) {
7749                                                 if (error == ENOBUFS) {
7750                                                         asoc->ifp_had_enobuf = 1;
7751                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
7752                                                 }
7753                                                 if (from_where == 0) {
7754                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
7755                                                 }
7756                                                 if (*now_filled == 0) {
7757                                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7758                                                         *now_filled = 1;
7759                                                         *now = net->last_sent_time;
7760                                                 } else {
7761                                                         net->last_sent_time = *now;
7762                                                 }
7763                                                 hbflag = 0;
7764                                                 /* error, could not output */
7765                                                 if (error == EHOSTUNREACH) {
7766                                                         /*
7767                                                          * Destination went
7768                                                          * unreachable
7769                                                          * during this send
7770                                                          */
7771                                                         sctp_move_to_an_alt(stcb, asoc, net);
7772                                                 }
7773                                                 *reason_code = 7;
7774                                                 continue;
7775                                         } else
7776                                                 asoc->ifp_had_enobuf = 0;
7777                                         if (*now_filled == 0) {
7778                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7779                                                 *now_filled = 1;
7780                                                 *now = net->last_sent_time;
7781                                         } else {
7782                                                 net->last_sent_time = *now;
7783                                         }
7784                                         hbflag = 0;
7785                                         /*
7786                                          * increase the number we sent, if a
7787                                          * cookie is sent we don't tell them
7788                                          * any was sent out.
7789                                          */
7790                                         outchain = endoutchain = NULL;
7791                                         auth = NULL;
7792                                         auth_offset = 0;
7793                                         if (!no_out_cnt)
7794                                                 *num_out += ctl_cnt;
7795                                         /* recalc a clean slate and setup */
7796                                         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7797                                                 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
7798                                         } else {
7799                                                 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
7800                                         }
7801                                         to_out = 0;
7802                                         no_fragmentflg = 1;
7803                                 }
7804                         }
7805                 }
7806                 /************************/
7807                 /* Control transmission */
7808                 /************************/
7809                 /* Now first lets go through the control queue */
7810                 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
7811                     chk; chk = nchk) {
7812                         nchk = TAILQ_NEXT(chk, sctp_next);
7813                         if (chk->whoTo != net) {
7814                                 /*
7815                                  * No, not sent to the network we are
7816                                  * looking at
7817                                  */
7818                                 continue;
7819                         }
7820                         if (chk->data == NULL) {
7821                                 continue;
7822                         }
7823                         if (chk->sent != SCTP_DATAGRAM_UNSENT) {
7824                                 /*
7825                                  * It must be unsent. Cookies and ASCONF's
7826                                  * hang around but there timers will force
7827                                  * when marked for resend.
7828                                  */
7829                                 continue;
7830                         }
7831                         /*
7832                          * if no AUTH is yet included and this chunk
7833                          * requires it, make sure to account for it.  We
7834                          * don't apply the size until the AUTH chunk is
7835                          * actually added below in case there is no room for
7836                          * this chunk. NOTE: we overload the use of "omtu"
7837                          * here
7838                          */
7839                         if ((auth == NULL) &&
7840                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7841                             stcb->asoc.peer_auth_chunks)) {
7842                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7843                         } else
7844                                 omtu = 0;
7845                         /* Here we do NOT factor the r_mtu */
7846                         if ((chk->send_size <= (int)(mtu - omtu)) ||
7847                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
7848                                 /*
7849                                  * We probably should glom the mbuf chain
7850                                  * from the chk->data for control but the
7851                                  * problem is it becomes yet one more level
7852                                  * of tracking to do if for some reason
7853                                  * output fails. Then I have got to
7854                                  * reconstruct the merged control chain.. el
7855                                  * yucko.. for now we take the easy way and
7856                                  * do the copy
7857                                  */
7858                                 /*
7859                                  * Add an AUTH chunk, if chunk requires it
7860                                  * save the offset into the chain for AUTH
7861                                  */
7862                                 if ((auth == NULL) &&
7863                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
7864                                     stcb->asoc.peer_auth_chunks))) {
7865                                         outchain = sctp_add_auth_chunk(outchain,
7866                                             &endoutchain,
7867                                             &auth,
7868                                             &auth_offset,
7869                                             stcb,
7870                                             chk->rec.chunk_id.id);
7871                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7872                                 }
7873                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
7874                                     (int)chk->rec.chunk_id.can_take_data,
7875                                     chk->send_size, chk->copy_by_ref);
7876                                 if (outchain == NULL) {
7877                                         *reason_code = 8;
7878                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7879                                         return (ENOMEM);
7880                                 }
7881                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7882                                 /* update our MTU size */
7883                                 if (mtu > (chk->send_size + omtu))
7884                                         mtu -= (chk->send_size + omtu);
7885                                 else
7886                                         mtu = 0;
7887                                 to_out += (chk->send_size + omtu);
7888                                 /* Do clear IP_DF ? */
7889                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
7890                                         no_fragmentflg = 0;
7891                                 }
7892                                 if (chk->rec.chunk_id.can_take_data)
7893                                         chk->data = NULL;
7894                                 /* Mark things to be removed, if needed */
7895                                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7896                                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
7897                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7898                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7899                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7900                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7901                                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7902                                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7903                                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7904                                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7905                                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7906
7907                                         if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
7908                                                 hbflag = 1;
7909                                                 /*
7910                                                  * JRS 5/14/07 - Set the
7911                                                  * flag to say a heartbeat
7912                                                  * is being sent.
7913                                                  */
7914                                                 pf_hbflag = 1;
7915                                         }
7916                                         /* remove these chunks at the end */
7917                                         if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7918                                             (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7919                                                 /* turn off the timer */
7920                                                 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
7921                                                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
7922                                                             inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
7923                                                 }
7924                                         }
7925                                         ctl_cnt++;
7926                                 } else {
7927                                         /*
7928                                          * Other chunks, since they have
7929                                          * timers running (i.e. COOKIE) we
7930                                          * just "trust" that it gets sent or
7931                                          * retransmitted.
7932                                          */
7933                                         ctl_cnt++;
7934                                         if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7935                                                 cookie = 1;
7936                                                 no_out_cnt = 1;
7937                                         }
7938                                         chk->sent = SCTP_DATAGRAM_SENT;
7939                                         chk->snd_count++;
7940                                 }
7941                                 if (mtu == 0) {
7942                                         /*
7943                                          * Ok we are out of room but we can
7944                                          * output without effecting the
7945                                          * flight size since this little guy
7946                                          * is a control only packet.
7947                                          */
7948                                         if (asconf) {
7949                                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
7950                                                 /*
7951                                                  * do NOT clear the asconf
7952                                                  * flag as it is used to do
7953                                                  * appropriate source
7954                                                  * address selection.
7955                                                  */
7956                                         }
7957                                         if (cookie) {
7958                                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
7959                                                 cookie = 0;
7960                                         }
7961                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
7962                                             (struct sockaddr *)&net->ro._l_addr,
7963                                             outchain,
7964                                             auth_offset, auth,
7965                                             stcb->asoc.authinfo.active_keyid,
7966                                             no_fragmentflg, 0, NULL, asconf,
7967                                             inp->sctp_lport, stcb->rport,
7968                                             htonl(stcb->asoc.peer_vtag),
7969                                             net->port, so_locked, NULL))) {
7970                                                 if (error == ENOBUFS) {
7971                                                         asoc->ifp_had_enobuf = 1;
7972                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
7973                                                 }
7974                                                 if (from_where == 0) {
7975                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
7976                                                 }
7977                                                 /* error, could not output */
7978                                                 if (hbflag) {
7979                                                         if (*now_filled == 0) {
7980                                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
7981                                                                 *now_filled = 1;
7982                                                                 *now = net->last_sent_time;
7983                                                         } else {
7984                                                                 net->last_sent_time = *now;
7985                                                         }
7986                                                         hbflag = 0;
7987                                                 }
7988                                                 if (error == EHOSTUNREACH) {
7989                                                         /*
7990                                                          * Destination went
7991                                                          * unreachable
7992                                                          * during this send
7993                                                          */
7994                                                         sctp_move_to_an_alt(stcb, asoc, net);
7995                                                 }
7996                                                 *reason_code = 7;
7997                                                 continue;
7998                                         } else
7999                                                 asoc->ifp_had_enobuf = 0;
8000                                         /* Only HB or ASCONF advances time */
8001                                         if (hbflag) {
8002                                                 if (*now_filled == 0) {
8003                                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8004                                                         *now_filled = 1;
8005                                                         *now = net->last_sent_time;
8006                                                 } else {
8007                                                         net->last_sent_time = *now;
8008                                                 }
8009                                                 hbflag = 0;
8010                                         }
8011                                         /*
8012                                          * increase the number we sent, if a
8013                                          * cookie is sent we don't tell them
8014                                          * any was sent out.
8015                                          */
8016                                         outchain = endoutchain = NULL;
8017                                         auth = NULL;
8018                                         auth_offset = 0;
8019                                         if (!no_out_cnt)
8020                                                 *num_out += ctl_cnt;
8021                                         /* recalc a clean slate and setup */
8022                                         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
8023                                                 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
8024                                         } else {
8025                                                 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
8026                                         }
8027                                         to_out = 0;
8028                                         no_fragmentflg = 1;
8029                                 }
8030                         }
8031                 }
8032                 /* JRI: if dest is in PF state, do not send data to it */
8033                 if ((asoc->sctp_cmt_on_off == 1) &&
8034                     (asoc->sctp_cmt_pf > 0) &&
8035                     (net->dest_state & SCTP_ADDR_PF)) {
8036                         goto no_data_fill;
8037                 }
8038                 if (net->flight_size >= net->cwnd) {
8039                         goto no_data_fill;
8040                 }
8041                 if ((asoc->sctp_cmt_on_off == 1) &&
8042                     (net->flight_size > max_rwnd_per_dest)) {
8043                         goto no_data_fill;
8044                 }
8045                 /*********************/
8046                 /* Data transmission */
8047                 /*********************/
8048                 /*
8049                  * if AUTH for DATA is required and no AUTH has been added
8050                  * yet, account for this in the mtu now... if no data can be
8051                  * bundled, this adjustment won't matter anyways since the
8052                  * packet will be going out...
8053                  */
8054                 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8055                     stcb->asoc.peer_auth_chunks);
8056                 if (data_auth_reqd && (auth == NULL)) {
8057                         mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8058                 }
8059                 /* now lets add any data within the MTU constraints */
8060                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8061                 case AF_INET:
8062                         if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
8063                                 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
8064                         else
8065                                 omtu = 0;
8066                         break;
8067 #ifdef INET6
8068                 case AF_INET6:
8069                         if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
8070                                 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
8071                         else
8072                                 omtu = 0;
8073                         break;
8074 #endif
8075                 default:
8076                         /* TSNH */
8077                         omtu = 0;
8078                         break;
8079                 }
8080                 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) &&
8081                     (skip_data_for_this_net == 0)) ||
8082                     (cookie)) {
8083                         for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
8084                                 if (no_data_chunks) {
8085                                         /* let only control go out */
8086                                         *reason_code = 1;
8087                                         break;
8088                                 }
8089                                 if (net->flight_size >= net->cwnd) {
8090                                         /* skip this net, no room for data */
8091                                         *reason_code = 2;
8092                                         break;
8093                                 }
8094                                 nchk = TAILQ_NEXT(chk, sctp_next);
8095                                 if (asoc->sctp_cmt_on_off == 1) {
8096                                         if (chk->whoTo != net) {
8097                                                 /*
8098                                                  * For CMT, steal the data
8099                                                  * to this network if its
8100                                                  * not set here.
8101                                                  */
8102                                                 sctp_free_remote_addr(chk->whoTo);
8103                                                 chk->whoTo = net;
8104                                                 atomic_add_int(&chk->whoTo->ref_count, 1);
8105                                         }
8106                                 } else if (chk->whoTo != net) {
8107                                         /* No, not sent to this net */
8108                                         continue;
8109                                 }
8110                                 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8111                                         /*-
8112                                          * strange, we have a chunk that is
8113                                          * to big for its destination and
8114                                          * yet no fragment ok flag.
8115                                          * Something went wrong when the
8116                                          * PMTU changed...we did not mark
8117                                          * this chunk for some reason?? I
8118                                          * will fix it here by letting IP
8119                                          * fragment it for now and printing
8120                                          * a warning. This really should not
8121                                          * happen ...
8122                                          */
8123                                         SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8124                                             chk->send_size, mtu);
8125                                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8126                                 }
8127                                 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8128                                     ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) == SCTP_STATE_SHUTDOWN_PENDING)) {
8129                                         struct sctp_data_chunk *dchkh;
8130
8131                                         dchkh = mtod(chk->data, struct sctp_data_chunk *);
8132                                         dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8133                                 }
8134                                 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8135                                     ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8136                                         /* ok we will add this one */
8137
8138                                         /*
8139                                          * Add an AUTH chunk, if chunk
8140                                          * requires it, save the offset into
8141                                          * the chain for AUTH
8142                                          */
8143                                         if (data_auth_reqd) {
8144                                                 if (auth == NULL) {
8145                                                         outchain = sctp_add_auth_chunk(outchain,
8146                                                             &endoutchain,
8147                                                             &auth,
8148                                                             &auth_offset,
8149                                                             stcb,
8150                                                             SCTP_DATA);
8151                                                         auth_keyid = chk->auth_keyid;
8152                                                         override_ok = 0;
8153                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8154                                                 } else if (override_ok) {
8155                                                         /*
8156                                                          * use this data's
8157                                                          * keyid
8158                                                          */
8159                                                         auth_keyid = chk->auth_keyid;
8160                                                         override_ok = 0;
8161                                                 } else if (auth_keyid != chk->auth_keyid) {
8162                                                         /*
8163                                                          * different keyid,
8164                                                          * so done bundling
8165                                                          */
8166                                                         break;
8167                                                 }
8168                                         }
8169                                         outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8170                                             chk->send_size, chk->copy_by_ref);
8171                                         if (outchain == NULL) {
8172                                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8173                                                 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8174                                                         sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8175                                                 }
8176                                                 *reason_code = 3;
8177                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8178                                                 return (ENOMEM);
8179                                         }
8180                                         /* upate our MTU size */
8181                                         /* Do clear IP_DF ? */
8182                                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8183                                                 no_fragmentflg = 0;
8184                                         }
8185                                         /* unsigned subtraction of mtu */
8186                                         if (mtu > chk->send_size)
8187                                                 mtu -= chk->send_size;
8188                                         else
8189                                                 mtu = 0;
8190                                         /* unsigned subtraction of r_mtu */
8191                                         if (r_mtu > chk->send_size)
8192                                                 r_mtu -= chk->send_size;
8193                                         else
8194                                                 r_mtu = 0;
8195
8196                                         to_out += chk->send_size;
8197                                         if ((to_out > mx_mtu) && no_fragmentflg) {
8198 #ifdef INVARIANTS
8199                                                 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8200 #else
8201                                                 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8202                                                     mx_mtu, to_out);
8203 #endif
8204                                         }
8205                                         chk->window_probe = 0;
8206                                         data_list[bundle_at++] = chk;
8207                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8208                                                 mtu = 0;
8209                                                 break;
8210                                         }
8211                                         if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8212                                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8213                                                         SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8214                                                 } else {
8215                                                         SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8216                                                 }
8217                                                 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8218                                                     ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8219                                                         /*
8220                                                          * Count number of
8221                                                          * user msg's that
8222                                                          * were fragmented
8223                                                          * we do this by
8224                                                          * counting when we
8225                                                          * see a LAST
8226                                                          * fragment only.
8227                                                          */
8228                                                         SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8229                                         }
8230                                         if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8231                                                 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8232                                                         data_list[0]->window_probe = 1;
8233                                                         net->window_probe = 1;
8234                                                 }
8235                                                 break;
8236                                         }
8237                                 } else {
8238                                         /*
8239                                          * Must be sent in order of the
8240                                          * TSN's (on a network)
8241                                          */
8242                                         break;
8243                                 }
8244                         }       /* for (chunk gather loop for this net) */
8245                 }               /* if asoc.state OPEN */
8246 no_data_fill:
8247                 /* Is there something to send for this destination? */
8248                 if (outchain) {
8249                         /* We may need to start a control timer or two */
8250                         if (asconf) {
8251                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8252                                     stcb, net);
8253                                 /*
8254                                  * do NOT clear the asconf flag as it is
8255                                  * used to do appropriate source address
8256                                  * selection.
8257                                  */
8258                         }
8259                         if (cookie) {
8260                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8261                                 cookie = 0;
8262                         }
8263                         /* must start a send timer if data is being sent */
8264                         if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8265                                 /*
8266                                  * no timer running on this destination
8267                                  * restart it.
8268                                  */
8269                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8270                         } else if ((asoc->sctp_cmt_on_off == 1) &&
8271                                     (asoc->sctp_cmt_pf > 0) &&
8272                                     pf_hbflag &&
8273                                     ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
8274                             (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8275                                 /*
8276                                  * JRS 5/14/07 - If a HB has been sent to a
8277                                  * PF destination and no T3 timer is
8278                                  * currently running, start the T3 timer to
8279                                  * track the HBs that were sent.
8280                                  */
8281                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8282                         }
8283                         /* Now send it, if there is anything to send :> */
8284                         if ((error = sctp_lowlevel_chunk_output(inp,
8285                             stcb,
8286                             net,
8287                             (struct sockaddr *)&net->ro._l_addr,
8288                             outchain,
8289                             auth_offset,
8290                             auth,
8291                             auth_keyid,
8292                             no_fragmentflg,
8293                             bundle_at,
8294                             data_list[0],
8295                             asconf,
8296                             inp->sctp_lport, stcb->rport,
8297                             htonl(stcb->asoc.peer_vtag),
8298                             net->port, so_locked, NULL))) {
8299                                 /* error, we could not output */
8300                                 if (error == ENOBUFS) {
8301                                         SCTP_STAT_INCR(sctps_lowlevelerr);
8302                                         asoc->ifp_had_enobuf = 1;
8303                                 }
8304                                 if (from_where == 0) {
8305                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
8306                                 }
8307                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8308                                 if (hbflag) {
8309                                         if (*now_filled == 0) {
8310                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8311                                                 *now_filled = 1;
8312                                                 *now = net->last_sent_time;
8313                                         } else {
8314                                                 net->last_sent_time = *now;
8315                                         }
8316                                         hbflag = 0;
8317                                 }
8318                                 if (error == EHOSTUNREACH) {
8319                                         /*
8320                                          * Destination went unreachable
8321                                          * during this send
8322                                          */
8323                                         sctp_move_to_an_alt(stcb, asoc, net);
8324                                 }
8325                                 *reason_code = 6;
8326                                 /*-
8327                                  * I add this line to be paranoid. As far as
8328                                  * I can tell the continue, takes us back to
8329                                  * the top of the for, but just to make sure
8330                                  * I will reset these again here.
8331                                  */
8332                                 ctl_cnt = bundle_at = 0;
8333                                 continue;       /* This takes us back to the
8334                                                  * for() for the nets. */
8335                         } else {
8336                                 asoc->ifp_had_enobuf = 0;
8337                         }
8338                         outchain = endoutchain = NULL;
8339                         auth = NULL;
8340                         auth_offset = 0;
8341                         if (bundle_at || hbflag) {
8342                                 /* For data/asconf and hb set time */
8343                                 if (*now_filled == 0) {
8344                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
8345                                         *now_filled = 1;
8346                                         *now = net->last_sent_time;
8347                                 } else {
8348                                         net->last_sent_time = *now;
8349                                 }
8350                         }
8351                         if (!no_out_cnt) {
8352                                 *num_out += (ctl_cnt + bundle_at);
8353                         }
8354                         if (bundle_at) {
8355                                 /* setup for a RTO measurement */
8356                                 tsns_sent = data_list[0]->rec.data.TSN_seq;
8357                                 /* fill time if not already filled */
8358                                 if (*now_filled == 0) {
8359                                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8360                                         *now_filled = 1;
8361                                         *now = asoc->time_last_sent;
8362                                 } else {
8363                                         asoc->time_last_sent = *now;
8364                                 }
8365                                 data_list[0]->do_rtt = 1;
8366                                 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8367                                 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8368                                 if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
8369                                         if (net->flight_size < net->cwnd) {
8370                                                 /* start or restart it */
8371                                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8372                                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8373                                                             SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
8374                                                 }
8375                                                 SCTP_STAT_INCR(sctps_earlyfrstrout);
8376                                                 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
8377                                         } else {
8378                                                 /* stop it if its running */
8379                                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
8380                                                         SCTP_STAT_INCR(sctps_earlyfrstpout);
8381                                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
8382                                                             SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
8383                                                 }
8384                                         }
8385                                 }
8386                         }
8387                         if (one_chunk) {
8388                                 break;
8389                         }
8390                 }
8391                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8392                         sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8393                 }
8394         }
8395         if (old_start_at == NULL) {
8396                 old_start_at = start_at;
8397                 start_at = TAILQ_FIRST(&asoc->nets);
8398                 if (old_start_at)
8399                         goto again_one_more_time;
8400         }
8401         /*
8402          * At the end there should be no NON timed chunks hanging on this
8403          * queue.
8404          */
8405         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8406                 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8407         }
8408         if ((*num_out == 0) && (*reason_code == 0)) {
8409                 *reason_code = 4;
8410         } else {
8411                 *reason_code = 5;
8412         }
8413         sctp_clean_up_ctl(stcb, asoc);
8414         return (0);
8415 }
8416
8417 void
8418 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8419 {
8420         /*-
8421          * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8422          * the control chunk queue.
8423          */
8424         struct sctp_chunkhdr *hdr;
8425         struct sctp_tmit_chunk *chk;
8426         struct mbuf *mat;
8427
8428         SCTP_TCB_LOCK_ASSERT(stcb);
8429         sctp_alloc_a_chunk(stcb, chk);
8430         if (chk == NULL) {
8431                 /* no memory */
8432                 sctp_m_freem(op_err);
8433                 return;
8434         }
8435         chk->copy_by_ref = 0;
8436         SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
8437         if (op_err == NULL) {
8438                 sctp_free_a_chunk(stcb, chk);
8439                 return;
8440         }
8441         chk->send_size = 0;
8442         mat = op_err;
8443         while (mat != NULL) {
8444                 chk->send_size += SCTP_BUF_LEN(mat);
8445                 mat = SCTP_BUF_NEXT(mat);
8446         }
8447         chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8448         chk->rec.chunk_id.can_take_data = 1;
8449         chk->sent = SCTP_DATAGRAM_UNSENT;
8450         chk->snd_count = 0;
8451         chk->flags = 0;
8452         chk->asoc = &stcb->asoc;
8453         chk->data = op_err;
8454         chk->whoTo = chk->asoc->primary_destination;
8455         atomic_add_int(&chk->whoTo->ref_count, 1);
8456         hdr = mtod(op_err, struct sctp_chunkhdr *);
8457         hdr->chunk_type = SCTP_OPERATION_ERROR;
8458         hdr->chunk_flags = 0;
8459         hdr->chunk_length = htons(chk->send_size);
8460         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
8461             chk,
8462             sctp_next);
8463         chk->asoc->ctrl_queue_cnt++;
8464 }
8465
8466 int
8467 sctp_send_cookie_echo(struct mbuf *m,
8468     int offset,
8469     struct sctp_tcb *stcb,
8470     struct sctp_nets *net)
8471 {
8472         /*-
8473          * pull out the cookie and put it at the front of the control chunk
8474          * queue.
8475          */
8476         int at;
8477         struct mbuf *cookie;
8478         struct sctp_paramhdr parm, *phdr;
8479         struct sctp_chunkhdr *hdr;
8480         struct sctp_tmit_chunk *chk;
8481         uint16_t ptype, plen;
8482
8483         /* First find the cookie in the param area */
8484         cookie = NULL;
8485         at = offset + sizeof(struct sctp_init_chunk);
8486
8487         SCTP_TCB_LOCK_ASSERT(stcb);
8488         do {
8489                 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
8490                 if (phdr == NULL) {
8491                         return (-3);
8492                 }
8493                 ptype = ntohs(phdr->param_type);
8494                 plen = ntohs(phdr->param_length);
8495                 if (ptype == SCTP_STATE_COOKIE) {
8496                         int pad;
8497
8498                         /* found the cookie */
8499                         if ((pad = (plen % 4))) {
8500                                 plen += 4 - pad;
8501                         }
8502                         cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
8503                         if (cookie == NULL) {
8504                                 /* No memory */
8505                                 return (-2);
8506                         }
8507 #ifdef SCTP_MBUF_LOGGING
8508                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8509                                 struct mbuf *mat;
8510
8511                                 mat = cookie;
8512                                 while (mat) {
8513                                         if (SCTP_BUF_IS_EXTENDED(mat)) {
8514                                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8515                                         }
8516                                         mat = SCTP_BUF_NEXT(mat);
8517                                 }
8518                         }
8519 #endif
8520                         break;
8521                 }
8522                 at += SCTP_SIZE32(plen);
8523         } while (phdr);
8524         if (cookie == NULL) {
8525                 /* Did not find the cookie */
8526                 return (-3);
8527         }
8528         /* ok, we got the cookie lets change it into a cookie echo chunk */
8529
8530         /* first the change from param to cookie */
8531         hdr = mtod(cookie, struct sctp_chunkhdr *);
8532         hdr->chunk_type = SCTP_COOKIE_ECHO;
8533         hdr->chunk_flags = 0;
8534         /* get the chunk stuff now and place it in the FRONT of the queue */
8535         sctp_alloc_a_chunk(stcb, chk);
8536         if (chk == NULL) {
8537                 /* no memory */
8538                 sctp_m_freem(cookie);
8539                 return (-5);
8540         }
8541         chk->copy_by_ref = 0;
8542         chk->send_size = plen;
8543         chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8544         chk->rec.chunk_id.can_take_data = 0;
8545         chk->sent = SCTP_DATAGRAM_UNSENT;
8546         chk->snd_count = 0;
8547         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8548         chk->asoc = &stcb->asoc;
8549         chk->data = cookie;
8550         chk->whoTo = chk->asoc->primary_destination;
8551         atomic_add_int(&chk->whoTo->ref_count, 1);
8552         TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
8553         chk->asoc->ctrl_queue_cnt++;
8554         return (0);
8555 }
8556
8557 void
8558 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
8559     struct mbuf *m,
8560     int offset,
8561     int chk_length,
8562     struct sctp_nets *net)
8563 {
8564         /*
8565          * take a HB request and make it into a HB ack and send it.
8566          */
8567         struct mbuf *outchain;
8568         struct sctp_chunkhdr *chdr;
8569         struct sctp_tmit_chunk *chk;
8570
8571
8572         if (net == NULL)
8573                 /* must have a net pointer */
8574                 return;
8575
8576         outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
8577         if (outchain == NULL) {
8578                 /* gak out of memory */
8579                 return;
8580         }
8581 #ifdef SCTP_MBUF_LOGGING
8582         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8583                 struct mbuf *mat;
8584
8585                 mat = outchain;
8586                 while (mat) {
8587                         if (SCTP_BUF_IS_EXTENDED(mat)) {
8588                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8589                         }
8590                         mat = SCTP_BUF_NEXT(mat);
8591                 }
8592         }
8593 #endif
8594         chdr = mtod(outchain, struct sctp_chunkhdr *);
8595         chdr->chunk_type = SCTP_HEARTBEAT_ACK;
8596         chdr->chunk_flags = 0;
8597         if (chk_length % 4) {
8598                 /* need pad */
8599                 uint32_t cpthis = 0;
8600                 int padlen;
8601
8602                 padlen = 4 - (chk_length % 4);
8603                 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
8604         }
8605         sctp_alloc_a_chunk(stcb, chk);
8606         if (chk == NULL) {
8607                 /* no memory */
8608                 sctp_m_freem(outchain);
8609                 return;
8610         }
8611         chk->copy_by_ref = 0;
8612         chk->send_size = chk_length;
8613         chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
8614         chk->rec.chunk_id.can_take_data = 1;
8615         chk->sent = SCTP_DATAGRAM_UNSENT;
8616         chk->snd_count = 0;
8617         chk->flags = 0;
8618         chk->asoc = &stcb->asoc;
8619         chk->data = outchain;
8620         chk->whoTo = net;
8621         atomic_add_int(&chk->whoTo->ref_count, 1);
8622         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8623         chk->asoc->ctrl_queue_cnt++;
8624 }
8625
8626 void
8627 sctp_send_cookie_ack(struct sctp_tcb *stcb)
8628 {
8629         /* formulate and queue a cookie-ack back to sender */
8630         struct mbuf *cookie_ack;
8631         struct sctp_chunkhdr *hdr;
8632         struct sctp_tmit_chunk *chk;
8633
8634         cookie_ack = NULL;
8635         SCTP_TCB_LOCK_ASSERT(stcb);
8636
8637         cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
8638         if (cookie_ack == NULL) {
8639                 /* no mbuf's */
8640                 return;
8641         }
8642         SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
8643         sctp_alloc_a_chunk(stcb, chk);
8644         if (chk == NULL) {
8645                 /* no memory */
8646                 sctp_m_freem(cookie_ack);
8647                 return;
8648         }
8649         chk->copy_by_ref = 0;
8650         chk->send_size = sizeof(struct sctp_chunkhdr);
8651         chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
8652         chk->rec.chunk_id.can_take_data = 1;
8653         chk->sent = SCTP_DATAGRAM_UNSENT;
8654         chk->snd_count = 0;
8655         chk->flags = 0;
8656         chk->asoc = &stcb->asoc;
8657         chk->data = cookie_ack;
8658         if (chk->asoc->last_control_chunk_from != NULL) {
8659                 chk->whoTo = chk->asoc->last_control_chunk_from;
8660         } else {
8661                 chk->whoTo = chk->asoc->primary_destination;
8662         }
8663         atomic_add_int(&chk->whoTo->ref_count, 1);
8664         hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
8665         hdr->chunk_type = SCTP_COOKIE_ACK;
8666         hdr->chunk_flags = 0;
8667         hdr->chunk_length = htons(chk->send_size);
8668         SCTP_BUF_LEN(cookie_ack) = chk->send_size;
8669         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8670         chk->asoc->ctrl_queue_cnt++;
8671         return;
8672 }
8673
8674
8675 void
8676 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
8677 {
8678         /* formulate and queue a SHUTDOWN-ACK back to the sender */
8679         struct mbuf *m_shutdown_ack;
8680         struct sctp_shutdown_ack_chunk *ack_cp;
8681         struct sctp_tmit_chunk *chk;
8682
8683         m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8684         if (m_shutdown_ack == NULL) {
8685                 /* no mbuf's */
8686                 return;
8687         }
8688         SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
8689         sctp_alloc_a_chunk(stcb, chk);
8690         if (chk == NULL) {
8691                 /* no memory */
8692                 sctp_m_freem(m_shutdown_ack);
8693                 return;
8694         }
8695         chk->copy_by_ref = 0;
8696         chk->send_size = sizeof(struct sctp_chunkhdr);
8697         chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
8698         chk->rec.chunk_id.can_take_data = 1;
8699         chk->sent = SCTP_DATAGRAM_UNSENT;
8700         chk->snd_count = 0;
8701         chk->flags = 0;
8702         chk->asoc = &stcb->asoc;
8703         chk->data = m_shutdown_ack;
8704         chk->whoTo = net;
8705         atomic_add_int(&net->ref_count, 1);
8706
8707         ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
8708         ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
8709         ack_cp->ch.chunk_flags = 0;
8710         ack_cp->ch.chunk_length = htons(chk->send_size);
8711         SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
8712         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8713         chk->asoc->ctrl_queue_cnt++;
8714         return;
8715 }
8716
8717 void
8718 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
8719 {
8720         /* formulate and queue a SHUTDOWN to the sender */
8721         struct mbuf *m_shutdown;
8722         struct sctp_shutdown_chunk *shutdown_cp;
8723         struct sctp_tmit_chunk *chk;
8724
8725         m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
8726         if (m_shutdown == NULL) {
8727                 /* no mbuf's */
8728                 return;
8729         }
8730         SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
8731         sctp_alloc_a_chunk(stcb, chk);
8732         if (chk == NULL) {
8733                 /* no memory */
8734                 sctp_m_freem(m_shutdown);
8735                 return;
8736         }
8737         chk->copy_by_ref = 0;
8738         chk->send_size = sizeof(struct sctp_shutdown_chunk);
8739         chk->rec.chunk_id.id = SCTP_SHUTDOWN;
8740         chk->rec.chunk_id.can_take_data = 1;
8741         chk->sent = SCTP_DATAGRAM_UNSENT;
8742         chk->snd_count = 0;
8743         chk->flags = 0;
8744         chk->asoc = &stcb->asoc;
8745         chk->data = m_shutdown;
8746         chk->whoTo = net;
8747         atomic_add_int(&net->ref_count, 1);
8748
8749         shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
8750         shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
8751         shutdown_cp->ch.chunk_flags = 0;
8752         shutdown_cp->ch.chunk_length = htons(chk->send_size);
8753         shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
8754         SCTP_BUF_LEN(m_shutdown) = chk->send_size;
8755         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8756         chk->asoc->ctrl_queue_cnt++;
8757         return;
8758 }
8759
8760 void
8761 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
8762 {
8763         /*
8764          * formulate and queue an ASCONF to the peer. ASCONF parameters
8765          * should be queued on the assoc queue.
8766          */
8767         struct sctp_tmit_chunk *chk;
8768         struct mbuf *m_asconf;
8769         int len;
8770
8771         SCTP_TCB_LOCK_ASSERT(stcb);
8772
8773         if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
8774             (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
8775                 /* can't send a new one if there is one in flight already */
8776                 return;
8777         }
8778         /* compose an ASCONF chunk, maximum length is PMTU */
8779         m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
8780         if (m_asconf == NULL) {
8781                 return;
8782         }
8783         sctp_alloc_a_chunk(stcb, chk);
8784         if (chk == NULL) {
8785                 /* no memory */
8786                 sctp_m_freem(m_asconf);
8787                 return;
8788         }
8789         chk->copy_by_ref = 0;
8790         chk->data = m_asconf;
8791         chk->send_size = len;
8792         chk->rec.chunk_id.id = SCTP_ASCONF;
8793         chk->rec.chunk_id.can_take_data = 0;
8794         chk->sent = SCTP_DATAGRAM_UNSENT;
8795         chk->snd_count = 0;
8796         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8797         chk->asoc = &stcb->asoc;
8798         chk->whoTo = net;
8799         atomic_add_int(&chk->whoTo->ref_count, 1);
8800         TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
8801         chk->asoc->ctrl_queue_cnt++;
8802         return;
8803 }
8804
8805 void
8806 sctp_send_asconf_ack(struct sctp_tcb *stcb)
8807 {
8808         /*
8809          * formulate and queue a asconf-ack back to sender. the asconf-ack
8810          * must be stored in the tcb.
8811          */
8812         struct sctp_tmit_chunk *chk;
8813         struct sctp_asconf_ack *ack, *latest_ack;
8814         struct mbuf *m_ack, *m;
8815         struct sctp_nets *net = NULL;
8816
8817         SCTP_TCB_LOCK_ASSERT(stcb);
8818         /* Get the latest ASCONF-ACK */
8819         latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
8820         if (latest_ack == NULL) {
8821                 return;
8822         }
8823         if (latest_ack->last_sent_to != NULL &&
8824             latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
8825                 /* we're doing a retransmission */
8826                 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
8827                 if (net == NULL) {
8828                         /* no alternate */
8829                         if (stcb->asoc.last_control_chunk_from == NULL)
8830                                 net = stcb->asoc.primary_destination;
8831                         else
8832                                 net = stcb->asoc.last_control_chunk_from;
8833                 }
8834         } else {
8835                 /* normal case */
8836                 if (stcb->asoc.last_control_chunk_from == NULL)
8837                         net = stcb->asoc.primary_destination;
8838                 else
8839                         net = stcb->asoc.last_control_chunk_from;
8840         }
8841         latest_ack->last_sent_to = net;
8842
8843         TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
8844                 if (ack->data == NULL) {
8845                         continue;
8846                 }
8847                 /* copy the asconf_ack */
8848                 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
8849                 if (m_ack == NULL) {
8850                         /* couldn't copy it */
8851                         return;
8852                 }
8853 #ifdef SCTP_MBUF_LOGGING
8854                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8855                         struct mbuf *mat;
8856
8857                         mat = m_ack;
8858                         while (mat) {
8859                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
8860                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8861                                 }
8862                                 mat = SCTP_BUF_NEXT(mat);
8863                         }
8864                 }
8865 #endif
8866
8867                 sctp_alloc_a_chunk(stcb, chk);
8868                 if (chk == NULL) {
8869                         /* no memory */
8870                         if (m_ack)
8871                                 sctp_m_freem(m_ack);
8872                         return;
8873                 }
8874                 chk->copy_by_ref = 0;
8875
8876                 chk->whoTo = net;
8877                 chk->data = m_ack;
8878                 chk->send_size = 0;
8879                 /* Get size */
8880                 m = m_ack;
8881                 chk->send_size = ack->len;
8882                 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
8883                 chk->rec.chunk_id.can_take_data = 1;
8884                 chk->sent = SCTP_DATAGRAM_UNSENT;
8885                 chk->snd_count = 0;
8886                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;  /* XXX */
8887                 chk->asoc = &stcb->asoc;
8888                 atomic_add_int(&chk->whoTo->ref_count, 1);
8889
8890                 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8891                 chk->asoc->ctrl_queue_cnt++;
8892         }
8893         return;
8894 }
8895
8896
8897 static int
8898 sctp_chunk_retransmission(struct sctp_inpcb *inp,
8899     struct sctp_tcb *stcb,
8900     struct sctp_association *asoc,
8901     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
8902 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
8903     SCTP_UNUSED
8904 #endif
8905 )
8906 {
8907         /*-
8908          * send out one MTU of retransmission. If fast_retransmit is
8909          * happening we ignore the cwnd. Otherwise we obey the cwnd and
8910          * rwnd. For a Cookie or Asconf in the control chunk queue we
8911          * retransmit them by themselves.
8912          *
8913          * For data chunks we will pick out the lowest TSN's in the sent_queue
8914          * marked for resend and bundle them all together (up to a MTU of
8915          * destination). The address to send to should have been
8916          * selected/changed where the retransmission was marked (i.e. in FR
8917          * or t3-timeout routines).
8918          */
8919         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8920         struct sctp_tmit_chunk *chk, *fwd;
8921         struct mbuf *m, *endofchain;
8922         struct sctp_nets *net = NULL;
8923         uint32_t tsns_sent = 0;
8924         int no_fragmentflg, bundle_at, cnt_thru;
8925         unsigned int mtu;
8926         int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
8927         struct sctp_auth_chunk *auth = NULL;
8928         uint32_t auth_offset = 0;
8929         uint16_t auth_keyid;
8930         int override_ok = 1;
8931         int data_auth_reqd = 0;
8932         uint32_t dmtu = 0;
8933
8934         SCTP_TCB_LOCK_ASSERT(stcb);
8935         tmr_started = ctl_cnt = bundle_at = error = 0;
8936         no_fragmentflg = 1;
8937         fwd_tsn = 0;
8938         *cnt_out = 0;
8939         fwd = NULL;
8940         endofchain = m = NULL;
8941         auth_keyid = stcb->asoc.authinfo.active_keyid;
8942 #ifdef SCTP_AUDITING_ENABLED
8943         sctp_audit_log(0xC3, 1);
8944 #endif
8945         if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
8946             (TAILQ_EMPTY(&asoc->control_send_queue))) {
8947                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
8948                     asoc->sent_queue_retran_cnt);
8949                 asoc->sent_queue_cnt = 0;
8950                 asoc->sent_queue_cnt_removeable = 0;
8951                 /* send back 0/0 so we enter normal transmission */
8952                 *cnt_out = 0;
8953                 return (0);
8954         }
8955         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8956                 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
8957                     (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
8958                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
8959                         if (chk->sent != SCTP_DATAGRAM_RESEND) {
8960                                 continue;
8961                         }
8962                         if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8963                                 if (chk != asoc->str_reset) {
8964                                         /*
8965                                          * not eligible for retran if its
8966                                          * not ours
8967                                          */
8968                                         continue;
8969                                 }
8970                         }
8971                         ctl_cnt++;
8972                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
8973                                 fwd_tsn = 1;
8974                                 fwd = chk;
8975                         }
8976                         /*
8977                          * Add an AUTH chunk, if chunk requires it save the
8978                          * offset into the chain for AUTH
8979                          */
8980                         if ((auth == NULL) &&
8981                             (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8982                             stcb->asoc.peer_auth_chunks))) {
8983                                 m = sctp_add_auth_chunk(m, &endofchain,
8984                                     &auth, &auth_offset,
8985                                     stcb,
8986                                     chk->rec.chunk_id.id);
8987                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8988                         }
8989                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
8990                         break;
8991                 }
8992         }
8993         one_chunk = 0;
8994         cnt_thru = 0;
8995         /* do we have control chunks to retransmit? */
8996         if (m != NULL) {
8997                 /* Start a timer no matter if we suceed or fail */
8998                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8999                         sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9000                 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9001                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9002                 chk->snd_count++;       /* update our count */
9003                 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9004                     (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9005                     auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9006                     no_fragmentflg, 0, NULL, 0,
9007                     inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9008                     chk->whoTo->port, so_locked, NULL))) {
9009                         SCTP_STAT_INCR(sctps_lowlevelerr);
9010                         return (error);
9011                 }
9012                 m = endofchain = NULL;
9013                 auth = NULL;
9014                 auth_offset = 0;
9015                 /*
9016                  * We don't want to mark the net->sent time here since this
9017                  * we use this for HB and retrans cannot measure RTT
9018                  */
9019                 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9020                 *cnt_out += 1;
9021                 chk->sent = SCTP_DATAGRAM_SENT;
9022                 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9023                 if (fwd_tsn == 0) {
9024                         return (0);
9025                 } else {
9026                         /* Clean up the fwd-tsn list */
9027                         sctp_clean_up_ctl(stcb, asoc);
9028                         return (0);
9029                 }
9030         }
9031         /*
9032          * Ok, it is just data retransmission we need to do or that and a
9033          * fwd-tsn with it all.
9034          */
9035         if (TAILQ_EMPTY(&asoc->sent_queue)) {
9036                 return (SCTP_RETRAN_DONE);
9037         }
9038         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
9039             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
9040                 /* not yet open, resend the cookie and that is it */
9041                 return (1);
9042         }
9043 #ifdef SCTP_AUDITING_ENABLED
9044         sctp_auditing(20, inp, stcb, NULL);
9045 #endif
9046         data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9047         TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9048                 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9049                         /* No, not sent to this net or not ready for rtx */
9050                         continue;
9051                 }
9052                 if (chk->data == NULL) {
9053                         printf("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9054                             chk->rec.data.TSN_seq, chk->snd_count, chk->sent);
9055                         continue;
9056                 }
9057                 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9058                     (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9059                         /* Gak, we have exceeded max unlucky retran, abort! */
9060                         SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
9061                             chk->snd_count,
9062                             SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
9063                         atomic_add_int(&stcb->asoc.refcnt, 1);
9064                         sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
9065                         SCTP_TCB_LOCK(stcb);
9066                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
9067                         return (SCTP_RETRAN_EXIT);
9068                 }
9069                 /* pick up the net */
9070                 net = chk->whoTo;
9071                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9072                         mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9073                 } else {
9074                         mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9075                 }
9076
9077                 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9078                         /* No room in peers rwnd */
9079                         uint32_t tsn;
9080
9081                         tsn = asoc->last_acked_seq + 1;
9082                         if (tsn == chk->rec.data.TSN_seq) {
9083                                 /*
9084                                  * we make a special exception for this
9085                                  * case. The peer has no rwnd but is missing
9086                                  * the lowest chunk.. which is probably what
9087                                  * is holding up the rwnd.
9088                                  */
9089                                 goto one_chunk_around;
9090                         }
9091                         return (1);
9092                 }
9093 one_chunk_around:
9094                 if (asoc->peers_rwnd < mtu) {
9095                         one_chunk = 1;
9096                         if ((asoc->peers_rwnd == 0) &&
9097                             (asoc->total_flight == 0)) {
9098                                 chk->window_probe = 1;
9099                                 chk->whoTo->window_probe = 1;
9100                         }
9101                 }
9102 #ifdef SCTP_AUDITING_ENABLED
9103                 sctp_audit_log(0xC3, 2);
9104 #endif
9105                 bundle_at = 0;
9106                 m = NULL;
9107                 net->fast_retran_ip = 0;
9108                 if (chk->rec.data.doing_fast_retransmit == 0) {
9109                         /*
9110                          * if no FR in progress skip destination that have
9111                          * flight_size > cwnd.
9112                          */
9113                         if (net->flight_size >= net->cwnd) {
9114                                 continue;
9115                         }
9116                 } else {
9117                         /*
9118                          * Mark the destination net to have FR recovery
9119                          * limits put on it.
9120                          */
9121                         *fr_done = 1;
9122                         net->fast_retran_ip = 1;
9123                 }
9124
9125                 /*
9126                  * if no AUTH is yet included and this chunk requires it,
9127                  * make sure to account for it.  We don't apply the size
9128                  * until the AUTH chunk is actually added below in case
9129                  * there is no room for this chunk.
9130                  */
9131                 if (data_auth_reqd && (auth == NULL)) {
9132                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9133                 } else
9134                         dmtu = 0;
9135
9136                 if ((chk->send_size <= (mtu - dmtu)) ||
9137                     (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9138                         /* ok we will add this one */
9139                         if (data_auth_reqd) {
9140                                 if (auth == NULL) {
9141                                         m = sctp_add_auth_chunk(m,
9142                                             &endofchain,
9143                                             &auth,
9144                                             &auth_offset,
9145                                             stcb,
9146                                             SCTP_DATA);
9147                                         auth_keyid = chk->auth_keyid;
9148                                         override_ok = 0;
9149                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9150                                 } else if (override_ok) {
9151                                         auth_keyid = chk->auth_keyid;
9152                                         override_ok = 0;
9153                                 } else if (chk->auth_keyid != auth_keyid) {
9154                                         /* different keyid, so done bundling */
9155                                         break;
9156                                 }
9157                         }
9158                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9159                         if (m == NULL) {
9160                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9161                                 return (ENOMEM);
9162                         }
9163                         /* Do clear IP_DF ? */
9164                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9165                                 no_fragmentflg = 0;
9166                         }
9167                         /* upate our MTU size */
9168                         if (mtu > (chk->send_size + dmtu))
9169                                 mtu -= (chk->send_size + dmtu);
9170                         else
9171                                 mtu = 0;
9172                         data_list[bundle_at++] = chk;
9173                         if (one_chunk && (asoc->total_flight <= 0)) {
9174                                 SCTP_STAT_INCR(sctps_windowprobed);
9175                         }
9176                 }
9177                 if (one_chunk == 0) {
9178                         /*
9179                          * now are there anymore forward from chk to pick
9180                          * up?
9181                          */
9182                         fwd = TAILQ_NEXT(chk, sctp_next);
9183                         while (fwd) {
9184                                 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9185                                         /* Nope, not for retran */
9186                                         fwd = TAILQ_NEXT(fwd, sctp_next);
9187                                         continue;
9188                                 }
9189                                 if (fwd->whoTo != net) {
9190                                         /* Nope, not the net in question */
9191                                         fwd = TAILQ_NEXT(fwd, sctp_next);
9192                                         continue;
9193                                 }
9194                                 if (data_auth_reqd && (auth == NULL)) {
9195                                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9196                                 } else
9197                                         dmtu = 0;
9198                                 if (fwd->send_size <= (mtu - dmtu)) {
9199                                         if (data_auth_reqd) {
9200                                                 if (auth == NULL) {
9201                                                         m = sctp_add_auth_chunk(m,
9202                                                             &endofchain,
9203                                                             &auth,
9204                                                             &auth_offset,
9205                                                             stcb,
9206                                                             SCTP_DATA);
9207                                                         auth_keyid = fwd->auth_keyid;
9208                                                         override_ok = 0;
9209                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9210                                                 } else if (override_ok) {
9211                                                         auth_keyid = fwd->auth_keyid;
9212                                                         override_ok = 0;
9213                                                 } else if (fwd->auth_keyid != auth_keyid) {
9214                                                         /*
9215                                                          * different keyid,
9216                                                          * so done bundling
9217                                                          */
9218                                                         break;
9219                                                 }
9220                                         }
9221                                         m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9222                                         if (m == NULL) {
9223                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9224                                                 return (ENOMEM);
9225                                         }
9226                                         /* Do clear IP_DF ? */
9227                                         if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9228                                                 no_fragmentflg = 0;
9229                                         }
9230                                         /* upate our MTU size */
9231                                         if (mtu > (fwd->send_size + dmtu))
9232                                                 mtu -= (fwd->send_size + dmtu);
9233                                         else
9234                                                 mtu = 0;
9235                                         data_list[bundle_at++] = fwd;
9236                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9237                                                 break;
9238                                         }
9239                                         fwd = TAILQ_NEXT(fwd, sctp_next);
9240                                 } else {
9241                                         /* can't fit so we are done */
9242                                         break;
9243                                 }
9244                         }
9245                 }
9246                 /* Is there something to send for this destination? */
9247                 if (m) {
9248                         /*
9249                          * No matter if we fail/or suceed we should start a
9250                          * timer. A failure is like a lost IP packet :-)
9251                          */
9252                         if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9253                                 /*
9254                                  * no timer running on this destination
9255                                  * restart it.
9256                                  */
9257                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9258                                 tmr_started = 1;
9259                         }
9260                         /* Now lets send it, if there is anything to send :> */
9261                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9262                             (struct sockaddr *)&net->ro._l_addr, m,
9263                             auth_offset, auth, auth_keyid,
9264                             no_fragmentflg, 0, NULL, 0,
9265                             inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9266                             net->port, so_locked, NULL))) {
9267                                 /* error, we could not output */
9268                                 SCTP_STAT_INCR(sctps_lowlevelerr);
9269                                 return (error);
9270                         }
9271                         m = endofchain = NULL;
9272                         auth = NULL;
9273                         auth_offset = 0;
9274                         /* For HB's */
9275                         /*
9276                          * We don't want to mark the net->sent time here
9277                          * since this we use this for HB and retrans cannot
9278                          * measure RTT
9279                          */
9280                         /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9281
9282                         /* For auto-close */
9283                         cnt_thru++;
9284                         if (*now_filled == 0) {
9285                                 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9286                                 *now = asoc->time_last_sent;
9287                                 *now_filled = 1;
9288                         } else {
9289                                 asoc->time_last_sent = *now;
9290                         }
9291                         *cnt_out += bundle_at;
9292 #ifdef SCTP_AUDITING_ENABLED
9293                         sctp_audit_log(0xC4, bundle_at);
9294 #endif
9295                         if (bundle_at) {
9296                                 tsns_sent = data_list[0]->rec.data.TSN_seq;
9297                         }
9298                         for (i = 0; i < bundle_at; i++) {
9299                                 SCTP_STAT_INCR(sctps_sendretransdata);
9300                                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9301                                 /*
9302                                  * When we have a revoked data, and we
9303                                  * retransmit it, then we clear the revoked
9304                                  * flag since this flag dictates if we
9305                                  * subtracted from the fs
9306                                  */
9307                                 if (data_list[i]->rec.data.chunk_was_revoked) {
9308                                         /* Deflate the cwnd */
9309                                         data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9310                                         data_list[i]->rec.data.chunk_was_revoked = 0;
9311                                 }
9312                                 data_list[i]->snd_count++;
9313                                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9314                                 /* record the time */
9315                                 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9316                                 if (data_list[i]->book_size_scale) {
9317                                         /*
9318                                          * need to double the book size on
9319                                          * this one
9320                                          */
9321                                         data_list[i]->book_size_scale = 0;
9322                                         /*
9323                                          * Since we double the booksize, we
9324                                          * must also double the output queue
9325                                          * size, since this get shrunk when
9326                                          * we free by this amount.
9327                                          */
9328                                         atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9329                                         data_list[i]->book_size *= 2;
9330
9331
9332                                 } else {
9333                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9334                                                 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9335                                                     asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9336                                         }
9337                                         asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9338                                             (uint32_t) (data_list[i]->send_size +
9339                                             SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9340                                 }
9341                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9342                                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9343                                             data_list[i]->whoTo->flight_size,
9344                                             data_list[i]->book_size,
9345                                             (uintptr_t) data_list[i]->whoTo,
9346                                             data_list[i]->rec.data.TSN_seq);
9347                                 }
9348                                 sctp_flight_size_increase(data_list[i]);
9349                                 sctp_total_flight_increase(stcb, data_list[i]);
9350                                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9351                                         /* SWS sender side engages */
9352                                         asoc->peers_rwnd = 0;
9353                                 }
9354                                 if ((i == 0) &&
9355                                     (data_list[i]->rec.data.doing_fast_retransmit)) {
9356                                         SCTP_STAT_INCR(sctps_sendfastretrans);
9357                                         if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9358                                             (tmr_started == 0)) {
9359                                                 /*-
9360                                                  * ok we just fast-retrans'd
9361                                                  * the lowest TSN, i.e the
9362                                                  * first on the list. In
9363                                                  * this case we want to give
9364                                                  * some more time to get a
9365                                                  * SACK back without a
9366                                                  * t3-expiring.
9367                                                  */
9368                                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9369                                                     SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
9370                                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9371                                         }
9372                                 }
9373                         }
9374                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9375                                 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9376                         }
9377 #ifdef SCTP_AUDITING_ENABLED
9378                         sctp_auditing(21, inp, stcb, NULL);
9379 #endif
9380                 } else {
9381                         /* None will fit */
9382                         return (1);
9383                 }
9384                 if (asoc->sent_queue_retran_cnt <= 0) {
9385                         /* all done we have no more to retran */
9386                         asoc->sent_queue_retran_cnt = 0;
9387                         break;
9388                 }
9389                 if (one_chunk) {
9390                         /* No more room in rwnd */
9391                         return (1);
9392                 }
9393                 /* stop the for loop here. we sent out a packet */
9394                 break;
9395         }
9396         return (0);
9397 }
9398
9399
9400 static int
9401 sctp_timer_validation(struct sctp_inpcb *inp,
9402     struct sctp_tcb *stcb,
9403     struct sctp_association *asoc,
9404     int ret)
9405 {
9406         struct sctp_nets *net;
9407
9408         /* Validate that a timer is running somewhere */
9409         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9410                 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9411                         /* Here is a timer */
9412                         return (ret);
9413                 }
9414         }
9415         SCTP_TCB_LOCK_ASSERT(stcb);
9416         /* Gak, we did not have a timer somewhere */
9417         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9418         sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9419         return (ret);
9420 }
9421
9422 void
9423 sctp_chunk_output(struct sctp_inpcb *inp,
9424     struct sctp_tcb *stcb,
9425     int from_where,
9426     int so_locked
9427 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9428     SCTP_UNUSED
9429 #endif
9430 )
9431 {
9432         /*-
9433          * Ok this is the generic chunk service queue. we must do the
9434          * following:
9435          * - See if there are retransmits pending, if so we must
9436          *   do these first.
9437          * - Service the stream queue that is next, moving any
9438          *   message (note I must get a complete message i.e.
9439          *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9440          *   TSN's
9441          * - Check to see if the cwnd/rwnd allows any output, if so we
9442          *   go ahead and fomulate and send the low level chunks. Making sure
9443          *   to combine any control in the control chunk queue also.
9444          */
9445         struct sctp_association *asoc;
9446         struct sctp_nets *net;
9447         int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
9448             burst_cnt = 0, burst_limit = 0;
9449         struct timeval now;
9450         int now_filled = 0;
9451         int nagle_on = 0;
9452         int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
9453         int un_sent = 0;
9454         int fr_done, tot_frs = 0;
9455
9456         asoc = &stcb->asoc;
9457         if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9458                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9459                         nagle_on = 0;
9460                 } else {
9461                         nagle_on = 1;
9462                 }
9463         }
9464         SCTP_TCB_LOCK_ASSERT(stcb);
9465
9466         un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9467
9468         if ((un_sent <= 0) &&
9469             (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9470             (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9471             (asoc->sent_queue_retran_cnt == 0)) {
9472                 /* Nothing to do unless there is something to be sent left */
9473                 return;
9474         }
9475         /*
9476          * Do we have something to send, data or control AND a sack timer
9477          * running, if so piggy-back the sack.
9478          */
9479         if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9480                 sctp_send_sack(stcb);
9481                 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
9482         }
9483         while (asoc->sent_queue_retran_cnt) {
9484                 /*-
9485                  * Ok, it is retransmission time only, we send out only ONE
9486                  * packet with a single call off to the retran code.
9487                  */
9488                 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9489                         /*-
9490                          * Special hook for handling cookiess discarded
9491                          * by peer that carried data. Send cookie-ack only
9492                          * and then the next call with get the retran's.
9493                          */
9494                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9495                             from_where,
9496                             &now, &now_filled, frag_point, so_locked);
9497                         return;
9498                 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9499                         /* if its not from a HB then do it */
9500                         fr_done = 0;
9501                         ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9502                         if (fr_done) {
9503                                 tot_frs++;
9504                         }
9505                 } else {
9506                         /*
9507                          * its from any other place, we don't allow retran
9508                          * output (only control)
9509                          */
9510                         ret = 1;
9511                 }
9512                 if (ret > 0) {
9513                         /* Can't send anymore */
9514                         /*-
9515                          * now lets push out control by calling med-level
9516                          * output once. this assures that we WILL send HB's
9517                          * if queued too.
9518                          */
9519                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9520                             from_where,
9521                             &now, &now_filled, frag_point, so_locked);
9522 #ifdef SCTP_AUDITING_ENABLED
9523                         sctp_auditing(8, inp, stcb, NULL);
9524 #endif
9525                         (void)sctp_timer_validation(inp, stcb, asoc, ret);
9526                         return;
9527                 }
9528                 if (ret < 0) {
9529                         /*-
9530                          * The count was off.. retran is not happening so do
9531                          * the normal retransmission.
9532                          */
9533 #ifdef SCTP_AUDITING_ENABLED
9534                         sctp_auditing(9, inp, stcb, NULL);
9535 #endif
9536                         if (ret == SCTP_RETRAN_EXIT) {
9537                                 return;
9538                         }
9539                         break;
9540                 }
9541                 if (from_where == SCTP_OUTPUT_FROM_T3) {
9542                         /* Only one transmission allowed out of a timeout */
9543 #ifdef SCTP_AUDITING_ENABLED
9544                         sctp_auditing(10, inp, stcb, NULL);
9545 #endif
9546                         /* Push out any control */
9547                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
9548                             &now, &now_filled, frag_point, so_locked);
9549                         return;
9550                 }
9551                 if (tot_frs > asoc->max_burst) {
9552                         /* Hit FR burst limit */
9553                         return;
9554                 }
9555                 if ((num_out == 0) && (ret == 0)) {
9556
9557                         /* No more retrans to send */
9558                         break;
9559                 }
9560         }
9561 #ifdef SCTP_AUDITING_ENABLED
9562         sctp_auditing(12, inp, stcb, NULL);
9563 #endif
9564         /* Check for bad destinations, if they exist move chunks around. */
9565         burst_limit = asoc->max_burst;
9566         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9567                 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
9568                     SCTP_ADDR_NOT_REACHABLE) {
9569                         /*-
9570                          * if possible move things off of this address we
9571                          * still may send below due to the dormant state but
9572                          * we try to find an alternate address to send to
9573                          * and if we have one we move all queued data on the
9574                          * out wheel to this alternate address.
9575                          */
9576                         if (net->ref_count > 1)
9577                                 sctp_move_to_an_alt(stcb, asoc, net);
9578                 } else if ((asoc->sctp_cmt_on_off == 1) &&
9579                             (asoc->sctp_cmt_pf > 0) &&
9580                     ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
9581                         /*
9582                          * JRS 5/14/07 - If CMT PF is on and the current
9583                          * destination is in PF state, move all queued data
9584                          * to an alternate desination.
9585                          */
9586                         if (net->ref_count > 1)
9587                                 sctp_move_to_an_alt(stcb, asoc, net);
9588                 } else {
9589                         /*-
9590                          * if ((asoc->sat_network) || (net->addr_is_local))
9591                          * { burst_limit = asoc->max_burst *
9592                          * SCTP_SAT_NETWORK_BURST_INCR; }
9593                          */
9594                         if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
9595                                 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
9596                                         /*
9597                                          * JRS - Use the congestion control
9598                                          * given in the congestion control
9599                                          * module
9600                                          */
9601                                         asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
9602                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9603                                                 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
9604                                         }
9605                                         SCTP_STAT_INCR(sctps_maxburstqueued);
9606                                 }
9607                                 net->fast_retran_ip = 0;
9608                         } else {
9609                                 if (net->flight_size == 0) {
9610                                         /* Should be decaying the cwnd here */
9611                                         ;
9612                                 }
9613                         }
9614                 }
9615
9616         }
9617         burst_cnt = 0;
9618         do {
9619                 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
9620                     &reason_code, 0, from_where,
9621                     &now, &now_filled, frag_point, so_locked);
9622                 if (error) {
9623                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
9624                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9625                                 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
9626                         }
9627                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9628                                 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
9629                                 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
9630                         }
9631                         break;
9632                 }
9633                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
9634
9635                 tot_out += num_out;
9636                 burst_cnt++;
9637                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9638                         sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
9639                         if (num_out == 0) {
9640                                 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
9641                         }
9642                 }
9643                 if (nagle_on) {
9644                         /*-
9645                          * When nagle is on, we look at how much is un_sent, then
9646                          * if its smaller than an MTU and we have data in
9647                          * flight we stop.
9648                          */
9649                         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
9650                             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
9651                         if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
9652                             (stcb->asoc.total_flight > 0)) {
9653                                 break;
9654                         }
9655                 }
9656                 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9657                     TAILQ_EMPTY(&asoc->send_queue) &&
9658                     TAILQ_EMPTY(&asoc->out_wheel)) {
9659                         /* Nothing left to send */
9660                         break;
9661                 }
9662                 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
9663                         /* Nothing left to send */
9664                         break;
9665                 }
9666         } while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
9667             (burst_cnt < burst_limit)));
9668
9669         if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
9670                 if (burst_cnt >= burst_limit) {
9671                         SCTP_STAT_INCR(sctps_maxburstqueued);
9672                         asoc->burst_limit_applied = 1;
9673                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9674                                 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
9675                         }
9676                 } else {
9677                         asoc->burst_limit_applied = 0;
9678                 }
9679         }
9680         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9681                 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
9682         }
9683         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
9684             tot_out);
9685
9686         /*-
9687          * Now we need to clean up the control chunk chain if a ECNE is on
9688          * it. It must be marked as UNSENT again so next call will continue
9689          * to send it until such time that we get a CWR, to remove it.
9690          */
9691         if (stcb->asoc.ecn_echo_cnt_onq)
9692                 sctp_fix_ecn_echo(asoc);
9693         return;
9694 }
9695
9696
9697 int
9698 sctp_output(inp, m, addr, control, p, flags)
9699         struct sctp_inpcb *inp;
9700         struct mbuf *m;
9701         struct sockaddr *addr;
9702         struct mbuf *control;
9703         struct thread *p;
9704         int flags;
9705 {
9706         if (inp == NULL) {
9707                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9708                 return (EINVAL);
9709         }
9710         if (inp->sctp_socket == NULL) {
9711                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
9712                 return (EINVAL);
9713         }
9714         return (sctp_sosend(inp->sctp_socket,
9715             addr,
9716             (struct uio *)NULL,
9717             m,
9718             control,
9719             flags, p
9720             ));
9721 }
9722
9723 void
9724 send_forward_tsn(struct sctp_tcb *stcb,
9725     struct sctp_association *asoc)
9726 {
9727         struct sctp_tmit_chunk *chk;
9728         struct sctp_forward_tsn_chunk *fwdtsn;
9729         uint32_t advance_peer_ack_point;
9730
9731         SCTP_TCB_LOCK_ASSERT(stcb);
9732         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9733                 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9734                         /* mark it to unsent */
9735                         chk->sent = SCTP_DATAGRAM_UNSENT;
9736                         chk->snd_count = 0;
9737                         /* Do we correct its output location? */
9738                         if (chk->whoTo != asoc->primary_destination) {
9739                                 sctp_free_remote_addr(chk->whoTo);
9740                                 chk->whoTo = asoc->primary_destination;
9741                                 atomic_add_int(&chk->whoTo->ref_count, 1);
9742                         }
9743                         goto sctp_fill_in_rest;
9744                 }
9745         }
9746         /* Ok if we reach here we must build one */
9747         sctp_alloc_a_chunk(stcb, chk);
9748         if (chk == NULL) {
9749                 return;
9750         }
9751         asoc->fwd_tsn_cnt++;
9752         chk->copy_by_ref = 0;
9753         chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
9754         chk->rec.chunk_id.can_take_data = 0;
9755         chk->asoc = asoc;
9756         chk->whoTo = NULL;
9757         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
9758         if (chk->data == NULL) {
9759                 sctp_free_a_chunk(stcb, chk);
9760                 return;
9761         }
9762         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
9763         chk->sent = SCTP_DATAGRAM_UNSENT;
9764         chk->snd_count = 0;
9765         chk->whoTo = asoc->primary_destination;
9766         atomic_add_int(&chk->whoTo->ref_count, 1);
9767         TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
9768         asoc->ctrl_queue_cnt++;
9769 sctp_fill_in_rest:
9770         /*-
9771          * Here we go through and fill out the part that deals with
9772          * stream/seq of the ones we skip.
9773          */
9774         SCTP_BUF_LEN(chk->data) = 0;
9775         {
9776                 struct sctp_tmit_chunk *at, *tp1, *last;
9777                 struct sctp_strseq *strseq;
9778                 unsigned int cnt_of_space, i, ovh;
9779                 unsigned int space_needed;
9780                 unsigned int cnt_of_skipped = 0;
9781
9782                 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
9783                         if (at->sent != SCTP_FORWARD_TSN_SKIP) {
9784                                 /* no more to look at */
9785                                 break;
9786                         }
9787                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9788                                 /* We don't report these */
9789                                 continue;
9790                         }
9791                         cnt_of_skipped++;
9792                 }
9793                 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
9794                     (cnt_of_skipped * sizeof(struct sctp_strseq)));
9795
9796                 cnt_of_space = M_TRAILINGSPACE(chk->data);
9797
9798                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9799                         ovh = SCTP_MIN_OVERHEAD;
9800                 } else {
9801                         ovh = SCTP_MIN_V4_OVERHEAD;
9802                 }
9803                 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
9804                         /* trim to a mtu size */
9805                         cnt_of_space = asoc->smallest_mtu - ovh;
9806                 }
9807                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9808                         sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9809                             0xff, 0, cnt_of_skipped,
9810                             asoc->advanced_peer_ack_point);
9811
9812                 }
9813                 advance_peer_ack_point = asoc->advanced_peer_ack_point;
9814                 if (cnt_of_space < space_needed) {
9815                         /*-
9816                          * ok we must trim down the chunk by lowering the
9817                          * advance peer ack point.
9818                          */
9819                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9820                                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9821                                     0xff, 0xff, cnt_of_space,
9822                                     space_needed);
9823                         }
9824                         cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
9825                         cnt_of_skipped /= sizeof(struct sctp_strseq);
9826                         /*-
9827                          * Go through and find the TSN that will be the one
9828                          * we report.
9829                          */
9830                         at = TAILQ_FIRST(&asoc->sent_queue);
9831                         for (i = 0; i < cnt_of_skipped; i++) {
9832                                 tp1 = TAILQ_NEXT(at, sctp_next);
9833                                 at = tp1;
9834                         }
9835                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
9836                                 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
9837                                     0xff, cnt_of_skipped, at->rec.data.TSN_seq,
9838                                     asoc->advanced_peer_ack_point);
9839                         }
9840                         last = at;
9841                         /*-
9842                          * last now points to last one I can report, update
9843                          * peer ack point
9844                          */
9845                         advance_peer_ack_point = last->rec.data.TSN_seq;
9846                         space_needed = sizeof(struct sctp_forward_tsn_chunk) +
9847                             cnt_of_skipped * sizeof(struct sctp_strseq);
9848                 }
9849                 chk->send_size = space_needed;
9850                 /* Setup the chunk */
9851                 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
9852                 fwdtsn->ch.chunk_length = htons(chk->send_size);
9853                 fwdtsn->ch.chunk_flags = 0;
9854                 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
9855                 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
9856                 SCTP_BUF_LEN(chk->data) = chk->send_size;
9857                 fwdtsn++;
9858                 /*-
9859                  * Move pointer to after the fwdtsn and transfer to the
9860                  * strseq pointer.
9861                  */
9862                 strseq = (struct sctp_strseq *)fwdtsn;
9863                 /*-
9864                  * Now populate the strseq list. This is done blindly
9865                  * without pulling out duplicate stream info. This is
9866                  * inefficent but won't harm the process since the peer will
9867                  * look at these in sequence and will thus release anything.
9868                  * It could mean we exceed the PMTU and chop off some that
9869                  * we could have included.. but this is unlikely (aka 1432/4
9870                  * would mean 300+ stream seq's would have to be reported in
9871                  * one FWD-TSN. With a bit of work we can later FIX this to
9872                  * optimize and pull out duplcates.. but it does add more
9873                  * overhead. So for now... not!
9874                  */
9875                 at = TAILQ_FIRST(&asoc->sent_queue);
9876                 for (i = 0; i < cnt_of_skipped; i++) {
9877                         tp1 = TAILQ_NEXT(at, sctp_next);
9878                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
9879                                 /* We don't report these */
9880                                 i--;
9881                                 at = tp1;
9882                                 continue;
9883                         }
9884                         if (at->rec.data.TSN_seq == advance_peer_ack_point) {
9885                                 at->rec.data.fwd_tsn_cnt = 0;
9886                         }
9887                         strseq->stream = ntohs(at->rec.data.stream_number);
9888                         strseq->sequence = ntohs(at->rec.data.stream_seq);
9889                         strseq++;
9890                         at = tp1;
9891                 }
9892         }
9893         return;
9894
9895 }
9896
9897 void
9898 sctp_send_sack(struct sctp_tcb *stcb)
9899 {
9900         /*-
9901          * Queue up a SACK or NR-SACK in the control queue.
9902          * We must first check to see if a SACK or NR-SACK is
9903          * somehow on the control queue.
9904          * If so, we will take and and remove the old one.
9905          */
9906         struct sctp_association *asoc;
9907         struct sctp_tmit_chunk *chk, *a_chk;
9908         struct sctp_sack_chunk *sack;
9909         struct sctp_nr_sack_chunk *nr_sack;
9910         struct sctp_gap_ack_block *gap_descriptor;
9911         struct sack_track *selector;
9912         int mergeable = 0;
9913         int offset;
9914         caddr_t limit;
9915         uint32_t *dup;
9916         int limit_reached = 0;
9917         unsigned int i, sel_start, siz, j, starting_index;
9918         unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
9919         int num_dups = 0;
9920         int space_req;
9921         uint32_t highest_tsn;
9922         uint8_t flags;
9923         uint8_t type;
9924
9925         if ((stcb->asoc.sctp_nr_sack_on_off == 1) &&
9926             (stcb->asoc.peer_supports_nr_sack == 1)) {
9927                 type = SCTP_NR_SELECTIVE_ACK;
9928         } else {
9929                 type = SCTP_SELECTIVE_ACK;
9930         }
9931         a_chk = NULL;
9932         asoc = &stcb->asoc;
9933         SCTP_TCB_LOCK_ASSERT(stcb);
9934         if (asoc->last_data_chunk_from == NULL) {
9935                 /* Hmm we never received anything */
9936                 return;
9937         }
9938         sctp_slide_mapping_arrays(stcb);
9939         sctp_set_rwnd(stcb, asoc);
9940         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9941                 if (chk->rec.chunk_id.id == type) {
9942                         /* Hmm, found a sack already on queue, remove it */
9943                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
9944                         asoc->ctrl_queue_cnt++;
9945                         a_chk = chk;
9946                         if (a_chk->data) {
9947                                 sctp_m_freem(a_chk->data);
9948                                 a_chk->data = NULL;
9949                         }
9950                         sctp_free_remote_addr(a_chk->whoTo);
9951                         a_chk->whoTo = NULL;
9952                         break;
9953                 }
9954         }
9955         if (a_chk == NULL) {
9956                 sctp_alloc_a_chunk(stcb, a_chk);
9957                 if (a_chk == NULL) {
9958                         /* No memory so we drop the idea, and set a timer */
9959                         if (stcb->asoc.delayed_ack) {
9960                                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9961                                     stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
9962                                 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
9963                                     stcb->sctp_ep, stcb, NULL);
9964                         } else {
9965                                 stcb->asoc.send_sack = 1;
9966                         }
9967                         return;
9968                 }
9969                 a_chk->copy_by_ref = 0;
9970                 a_chk->rec.chunk_id.id = type;
9971                 a_chk->rec.chunk_id.can_take_data = 1;
9972         }
9973         /* Clear our pkt counts */
9974         asoc->data_pkts_seen = 0;
9975
9976         a_chk->asoc = asoc;
9977         a_chk->snd_count = 0;
9978         a_chk->send_size = 0;   /* fill in later */
9979         a_chk->sent = SCTP_DATAGRAM_UNSENT;
9980         a_chk->whoTo = NULL;
9981
9982         if ((asoc->numduptsns) ||
9983             (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
9984             ) {
9985                 /*-
9986                  * Ok, we have some duplicates or the destination for the
9987                  * sack is unreachable, lets see if we can select an
9988                  * alternate than asoc->last_data_chunk_from
9989                  */
9990                 if ((!(asoc->last_data_chunk_from->dest_state &
9991                     SCTP_ADDR_NOT_REACHABLE)) &&
9992                     (asoc->used_alt_onsack > asoc->numnets)) {
9993                         /* We used an alt last time, don't this time */
9994                         a_chk->whoTo = NULL;
9995                 } else {
9996                         asoc->used_alt_onsack++;
9997                         a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
9998                 }
9999                 if (a_chk->whoTo == NULL) {
10000                         /* Nope, no alternate */
10001                         a_chk->whoTo = asoc->last_data_chunk_from;
10002                         asoc->used_alt_onsack = 0;
10003                 }
10004         } else {
10005                 /*
10006                  * No duplicates so we use the last place we received data
10007                  * from.
10008                  */
10009                 asoc->used_alt_onsack = 0;
10010                 a_chk->whoTo = asoc->last_data_chunk_from;
10011         }
10012         if (a_chk->whoTo) {
10013                 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10014         }
10015         if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map, MAX_TSN)) {
10016                 highest_tsn = asoc->highest_tsn_inside_map;
10017         } else {
10018                 highest_tsn = asoc->highest_tsn_inside_nr_map;
10019         }
10020         if (highest_tsn == asoc->cumulative_tsn) {
10021                 /* no gaps */
10022                 if (type == SCTP_SELECTIVE_ACK) {
10023                         space_req = sizeof(struct sctp_sack_chunk);
10024                 } else {
10025                         space_req = sizeof(struct sctp_nr_sack_chunk);
10026                 }
10027         } else {
10028                 /* gaps get a cluster */
10029                 space_req = MCLBYTES;
10030         }
10031         /* Ok now lets formulate a MBUF with our sack */
10032         a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
10033         if ((a_chk->data == NULL) ||
10034             (a_chk->whoTo == NULL)) {
10035                 /* rats, no mbuf memory */
10036                 if (a_chk->data) {
10037                         /* was a problem with the destination */
10038                         sctp_m_freem(a_chk->data);
10039                         a_chk->data = NULL;
10040                 }
10041                 sctp_free_a_chunk(stcb, a_chk);
10042                 /* sa_ignore NO_NULL_CHK */
10043                 if (stcb->asoc.delayed_ack) {
10044                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10045                             stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
10046                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10047                             stcb->sctp_ep, stcb, NULL);
10048                 } else {
10049                         stcb->asoc.send_sack = 1;
10050                 }
10051                 return;
10052         }
10053         /* ok, lets go through and fill it in */
10054         SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10055         space = M_TRAILINGSPACE(a_chk->data);
10056         if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10057                 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10058         }
10059         limit = mtod(a_chk->data, caddr_t);
10060         limit += space;
10061
10062         /* 0x01 is used by nonce for ecn */
10063         if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
10064             (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
10065             (asoc->peer_supports_ecn_nonce))
10066                 flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
10067         else
10068                 flags = 0;
10069
10070         if ((asoc->sctp_cmt_on_off == 1) &&
10071             SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10072                 /*-
10073                  * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10074                  * received, then set high bit to 1, else 0. Reset
10075                  * pkts_rcvd.
10076                  */
10077                 flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10078                 asoc->cmt_dac_pkts_rcvd = 0;
10079         }
10080 #ifdef SCTP_ASOCLOG_OF_TSNS
10081         stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10082         stcb->asoc.cumack_log_atsnt++;
10083         if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10084                 stcb->asoc.cumack_log_atsnt = 0;
10085         }
10086 #endif
10087         /* reset the readers interpretation */
10088         stcb->freed_by_sorcv_sincelast = 0;
10089
10090         if (type == SCTP_SELECTIVE_ACK) {
10091                 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10092                 nr_sack = NULL;
10093                 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10094                 if (highest_tsn > asoc->mapping_array_base_tsn) {
10095                         siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10096                 } else {
10097                         siz = (((MAX_TSN - highest_tsn) + 1) + highest_tsn + 7) / 8;
10098                 }
10099         } else {
10100                 sack = NULL;
10101                 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10102                 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10103                 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10104                         siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10105                 } else {
10106                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10107                 }
10108         }
10109
10110         if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10111                 offset = 1;
10112                 /*-
10113                  * The base TSN is intialized to be the first TSN the peer
10114                  * will send us. If the cum-ack is behind this then when they
10115                  * send us the next in sequence it will mark the base_tsn bit.
10116                  * Thus we need to use the very first selector and the offset
10117                  * is 1. Our table is built for this case.
10118                  */
10119                 starting_index = 0;
10120                 sel_start = 0;
10121         } else {
10122                 /*-
10123                  * we skip the first selector  when the cum-ack is at or above the
10124                  * mapping array base. This is because the bits at the base or above
10125                  * are turned on and our first selector in the table assumes they are
10126                  * off. We thus will use the second selector (first is 0). We use
10127                  * the reverse of our macro to fix the offset, in bits, that our
10128                  * table is at. Note that this method assumes that the cum-tsn is
10129                  * within the first bit, i.e. its value is 0-7 which means the
10130                  * result to our offset will be either a 0 - -7. If the cumack
10131                  * is NOT in the first byte (0) (which it should be since we did
10132                  * a mapping array slide above) then we need to calculate the starting
10133                  * index i.e. which byte of the mapping array we should start at. We
10134                  * do this by dividing by 8 and pushing the remainder (mod) into offset.
10135                  * then we multiply the offset to be negative, since we need a negative
10136                  * offset into the selector table.
10137                  */
10138                 SCTP_CALC_TSN_TO_GAP(offset, asoc->cumulative_tsn, asoc->mapping_array_base_tsn);
10139                 if (offset > 7) {
10140                         starting_index = offset / 8;
10141                         offset = offset % 8;
10142                         printf("Strange starting index is %d offset:%d (not 0/x)\n",
10143                             starting_index, offset);
10144                 } else {
10145                         starting_index = 0;
10146                 }
10147                 /* We need a negative offset in our table */
10148                 offset *= -1;
10149                 sel_start = 1;
10150         }
10151         if (((type == SCTP_SELECTIVE_ACK) &&
10152             compare_with_wrap(highest_tsn, asoc->cumulative_tsn, MAX_TSN)) ||
10153             ((type == SCTP_NR_SELECTIVE_ACK) &&
10154             compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN))) {
10155                 /* we have a gap .. maybe */
10156                 for (i = starting_index; i < siz; i++) {
10157                         if (type == SCTP_SELECTIVE_ACK) {
10158                                 selector = &sack_array[asoc->mapping_array[i] | asoc->nr_mapping_array[i]];
10159                         } else {
10160                                 selector = &sack_array[asoc->mapping_array[i]];
10161                         }
10162                         if (mergeable && selector->right_edge) {
10163                                 /*
10164                                  * Backup, left and right edges were ok to
10165                                  * merge.
10166                                  */
10167                                 num_gap_blocks--;
10168                                 gap_descriptor--;
10169                         }
10170                         if (selector->num_entries == 0)
10171                                 mergeable = 0;
10172                         else {
10173                                 for (j = sel_start; j < selector->num_entries; j++) {
10174                                         if (mergeable && selector->right_edge) {
10175                                                 /*
10176                                                  * do a merge by NOT setting
10177                                                  * the left side
10178                                                  */
10179                                                 mergeable = 0;
10180                                         } else {
10181                                                 /*
10182                                                  * no merge, set the left
10183                                                  * side
10184                                                  */
10185                                                 mergeable = 0;
10186                                                 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10187                                         }
10188                                         gap_descriptor->end = htons((selector->gaps[j].end + offset));
10189                                         num_gap_blocks++;
10190                                         gap_descriptor++;
10191                                         if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10192                                                 /* no more room */
10193                                                 limit_reached = 1;
10194                                                 break;
10195                                         }
10196                                 }
10197                                 if (selector->left_edge) {
10198                                         mergeable = 1;
10199                                 }
10200                         }
10201                         if (limit_reached) {
10202                                 /* Reached the limit stop */
10203                                 break;
10204                         }
10205                         sel_start = 0;
10206                         offset += 8;
10207                 }
10208         }
10209         if ((type == SCTP_NR_SELECTIVE_ACK) &&
10210             (limit_reached == 0)) {
10211
10212                 mergeable = 0;
10213
10214                 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn)
10215                         siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10216                 else
10217                         siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10218
10219                 if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
10220                         offset = 1;
10221                         /*-
10222                         * cum-ack behind the mapping array, so we start and use all
10223                         * entries.
10224                         */
10225                         sel_start = 0;
10226                 } else {
10227                         offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10228                         /*-
10229                         * we skip the first one when the cum-ack is at or above the
10230                         * mapping array base. Note this only works if
10231                         */
10232                         sel_start = 1;
10233                 }
10234                 if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
10235                         /* we have a gap .. maybe */
10236                         for (i = 0; i < siz; i++) {
10237                                 selector = &sack_array[asoc->nr_mapping_array[i]];
10238                                 if (mergeable && selector->right_edge) {
10239                                         /*
10240                                          * Backup, left and right edges were
10241                                          * ok to merge.
10242                                          */
10243                                         num_nr_gap_blocks--;
10244                                         gap_descriptor--;
10245                                 }
10246                                 if (selector->num_entries == 0)
10247                                         mergeable = 0;
10248                                 else {
10249                                         for (j = sel_start; j < selector->num_entries; j++) {
10250                                                 if (mergeable && selector->right_edge) {
10251                                                         /*
10252                                                          * do a merge by NOT
10253                                                          * setting the left
10254                                                          * side
10255                                                          */
10256                                                         mergeable = 0;
10257                                                 } else {
10258                                                         /*
10259                                                          * no merge, set the
10260                                                          * left side
10261                                                          */
10262                                                         mergeable = 0;
10263                                                         gap_descriptor->start = htons((selector->gaps[j].start + offset));
10264                                                 }
10265                                                 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10266                                                 num_nr_gap_blocks++;
10267                                                 gap_descriptor++;
10268                                                 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10269                                                         /* no more room */
10270                                                         limit_reached = 1;
10271                                                         break;
10272                                                 }
10273                                         }
10274                                         if (selector->left_edge) {
10275                                                 mergeable = 1;
10276                                         }
10277                                 }
10278                                 if (limit_reached) {
10279                                         /* Reached the limit stop */
10280                                         break;
10281                                 }
10282                                 sel_start = 0;
10283                                 offset += 8;
10284                         }
10285                 }
10286         }
10287         /* now we must add any dups we are going to report. */
10288         if ((limit_reached == 0) && (asoc->numduptsns)) {
10289                 dup = (uint32_t *) gap_descriptor;
10290                 for (i = 0; i < asoc->numduptsns; i++) {
10291                         *dup = htonl(asoc->dup_tsns[i]);
10292                         dup++;
10293                         num_dups++;
10294                         if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10295                                 /* no more room */
10296                                 break;
10297                         }
10298                 }
10299                 asoc->numduptsns = 0;
10300         }
10301         /*
10302          * now that the chunk is prepared queue it to the control chunk
10303          * queue.
10304          */
10305         if (type == SCTP_SELECTIVE_ACK) {
10306                 a_chk->send_size = sizeof(struct sctp_sack_chunk) +
10307                     (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10308                     num_dups * sizeof(int32_t);
10309                 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10310                 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10311                 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10312                 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10313                 sack->sack.num_dup_tsns = htons(num_dups);
10314                 sack->ch.chunk_type = type;
10315                 sack->ch.chunk_flags = flags;
10316                 sack->ch.chunk_length = htons(a_chk->send_size);
10317         } else {
10318                 a_chk->send_size = sizeof(struct sctp_nr_sack_chunk) +
10319                     (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10320                     num_dups * sizeof(int32_t);
10321                 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10322                 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10323                 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10324                 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10325                 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10326                 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10327                 nr_sack->nr_sack.reserved = 0;
10328                 nr_sack->ch.chunk_type = type;
10329                 nr_sack->ch.chunk_flags = flags;
10330                 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10331         }
10332         TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10333         asoc->my_last_reported_rwnd = asoc->my_rwnd;
10334         asoc->ctrl_queue_cnt++;
10335         asoc->send_sack = 0;
10336         SCTP_STAT_INCR(sctps_sendsacks);
10337         return;
10338 }
10339
10340 void
10341 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
10342 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10343     SCTP_UNUSED
10344 #endif
10345 )
10346 {
10347         struct mbuf *m_abort;
10348         struct mbuf *m_out = NULL, *m_end = NULL;
10349         struct sctp_abort_chunk *abort = NULL;
10350         int sz;
10351         uint32_t auth_offset = 0;
10352         struct sctp_auth_chunk *auth = NULL;
10353
10354         /*-
10355          * Add an AUTH chunk, if chunk requires it and save the offset into
10356          * the chain for AUTH
10357          */
10358         if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10359             stcb->asoc.peer_auth_chunks)) {
10360                 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
10361                     stcb, SCTP_ABORT_ASSOCIATION);
10362                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10363         }
10364         SCTP_TCB_LOCK_ASSERT(stcb);
10365         m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10366         if (m_abort == NULL) {
10367                 /* no mbuf's */
10368                 if (m_out)
10369                         sctp_m_freem(m_out);
10370                 return;
10371         }
10372         /* link in any error */
10373         SCTP_BUF_NEXT(m_abort) = operr;
10374         sz = 0;
10375         if (operr) {
10376                 struct mbuf *n;
10377
10378                 n = operr;
10379                 while (n) {
10380                         sz += SCTP_BUF_LEN(n);
10381                         n = SCTP_BUF_NEXT(n);
10382                 }
10383         }
10384         SCTP_BUF_LEN(m_abort) = sizeof(*abort);
10385         if (m_out == NULL) {
10386                 /* NO Auth chunk prepended, so reserve space in front */
10387                 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10388                 m_out = m_abort;
10389         } else {
10390                 /* Put AUTH chunk at the front of the chain */
10391                 SCTP_BUF_NEXT(m_end) = m_abort;
10392         }
10393
10394         /* fill in the ABORT chunk */
10395         abort = mtod(m_abort, struct sctp_abort_chunk *);
10396         abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10397         abort->ch.chunk_flags = 0;
10398         abort->ch.chunk_length = htons(sizeof(*abort) + sz);
10399
10400         (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
10401             stcb->asoc.primary_destination,
10402             (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
10403             m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0,
10404             stcb->sctp_ep->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10405             stcb->asoc.primary_destination->port, so_locked, NULL);
10406         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10407 }
10408
10409 void
10410 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10411     struct sctp_nets *net,
10412     int reflect_vtag)
10413 {
10414         /* formulate and SEND a SHUTDOWN-COMPLETE */
10415         struct mbuf *m_shutdown_comp;
10416         struct sctp_shutdown_complete_chunk *shutdown_complete;
10417         uint32_t vtag;
10418         uint8_t flags;
10419
10420         m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10421         if (m_shutdown_comp == NULL) {
10422                 /* no mbuf's */
10423                 return;
10424         }
10425         if (reflect_vtag) {
10426                 flags = SCTP_HAD_NO_TCB;
10427                 vtag = stcb->asoc.my_vtag;
10428         } else {
10429                 flags = 0;
10430                 vtag = stcb->asoc.peer_vtag;
10431         }
10432         shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10433         shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10434         shutdown_complete->ch.chunk_flags = flags;
10435         shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10436         SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10437         (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10438             (struct sockaddr *)&net->ro._l_addr,
10439             m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0,
10440             stcb->sctp_ep->sctp_lport, stcb->rport,
10441             htonl(vtag),
10442             net->port, SCTP_SO_NOT_LOCKED, NULL);
10443         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10444         return;
10445 }
10446
10447 void
10448 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
10449     uint32_t vrf_id, uint16_t port)
10450 {
10451         /* formulate and SEND a SHUTDOWN-COMPLETE */
10452         struct mbuf *o_pak;
10453         struct mbuf *mout;
10454         struct ip *iph, *iph_out;
10455         struct udphdr *udp = NULL;
10456
10457 #ifdef INET6
10458         struct ip6_hdr *ip6, *ip6_out;
10459
10460 #endif
10461         int offset_out, len, mlen;
10462         struct sctp_shutdown_complete_msg *comp_cp;
10463
10464         iph = mtod(m, struct ip *);
10465         switch (iph->ip_v) {
10466         case IPVERSION:
10467                 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
10468                 break;
10469 #ifdef INET6
10470         case IPV6_VERSION >> 4:
10471                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
10472                 break;
10473 #endif
10474         default:
10475                 return;
10476         }
10477         if (port) {
10478                 len += sizeof(struct udphdr);
10479         }
10480         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
10481         if (mout == NULL) {
10482                 return;
10483         }
10484         SCTP_BUF_RESV_UF(mout, max_linkhdr);
10485         SCTP_BUF_LEN(mout) = len;
10486         SCTP_BUF_NEXT(mout) = NULL;
10487         iph_out = NULL;
10488 #ifdef INET6
10489         ip6_out = NULL;
10490 #endif
10491         offset_out = 0;
10492
10493         switch (iph->ip_v) {
10494         case IPVERSION:
10495                 iph_out = mtod(mout, struct ip *);
10496
10497                 /* Fill in the IP header for the ABORT */
10498                 iph_out->ip_v = IPVERSION;
10499                 iph_out->ip_hl = (sizeof(struct ip) / 4);
10500                 iph_out->ip_tos = (u_char)0;
10501                 iph_out->ip_id = 0;
10502                 iph_out->ip_off = 0;
10503                 iph_out->ip_ttl = MAXTTL;
10504                 if (port) {
10505                         iph_out->ip_p = IPPROTO_UDP;
10506                 } else {
10507                         iph_out->ip_p = IPPROTO_SCTP;
10508                 }
10509                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
10510                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
10511
10512                 /* let IP layer calculate this */
10513                 iph_out->ip_sum = 0;
10514                 offset_out += sizeof(*iph_out);
10515                 comp_cp = (struct sctp_shutdown_complete_msg *)(
10516                     (caddr_t)iph_out + offset_out);
10517                 break;
10518 #ifdef INET6
10519         case IPV6_VERSION >> 4:
10520                 ip6 = (struct ip6_hdr *)iph;
10521                 ip6_out = mtod(mout, struct ip6_hdr *);
10522
10523                 /* Fill in the IPv6 header for the ABORT */
10524                 ip6_out->ip6_flow = ip6->ip6_flow;
10525                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
10526                 if (port) {
10527                         ip6_out->ip6_nxt = IPPROTO_UDP;
10528                 } else {
10529                         ip6_out->ip6_nxt = IPPROTO_SCTP;
10530                 }
10531                 ip6_out->ip6_src = ip6->ip6_dst;
10532                 ip6_out->ip6_dst = ip6->ip6_src;
10533                 /*
10534                  * ?? The old code had both the iph len + payload, I think
10535                  * this is wrong and would never have worked
10536                  */
10537                 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
10538                 offset_out += sizeof(*ip6_out);
10539                 comp_cp = (struct sctp_shutdown_complete_msg *)(
10540                     (caddr_t)ip6_out + offset_out);
10541                 break;
10542 #endif                          /* INET6 */
10543         default:
10544                 /* Currently not supported. */
10545                 sctp_m_freem(mout);
10546                 return;
10547         }
10548         if (port) {
10549                 udp = (struct udphdr *)comp_cp;
10550                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
10551                 udp->uh_dport = port;
10552                 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
10553                 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
10554                 offset_out += sizeof(struct udphdr);
10555                 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
10556         }
10557         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
10558                 /* no mbuf's */
10559                 sctp_m_freem(mout);
10560                 return;
10561         }
10562         /* Now copy in and fill in the ABORT tags etc. */
10563         comp_cp->sh.src_port = sh->dest_port;
10564         comp_cp->sh.dest_port = sh->src_port;
10565         comp_cp->sh.checksum = 0;
10566         comp_cp->sh.v_tag = sh->v_tag;
10567         comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
10568         comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10569         comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10570
10571         if (iph_out != NULL) {
10572                 sctp_route_t ro;
10573                 int ret;
10574                 struct sctp_tcb *stcb = NULL;
10575
10576                 mlen = SCTP_BUF_LEN(mout);
10577                 bzero(&ro, sizeof ro);
10578                 /* set IPv4 length */
10579                 iph_out->ip_len = mlen;
10580 #ifdef  SCTP_PACKET_LOGGING
10581                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10582                         sctp_packet_log(mout, mlen);
10583 #endif
10584                 if (port) {
10585                         comp_cp->sh.checksum = sctp_calculate_cksum(mout, offset_out);
10586                         SCTP_STAT_INCR(sctps_sendswcrc);
10587                         SCTP_ENABLE_UDP_CSUM(mout);
10588                 } else {
10589                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
10590                         mout->m_pkthdr.csum_data = 0;
10591                         SCTP_STAT_INCR(sctps_sendhwcrc);
10592                 }
10593                 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10594                 /* out it goes */
10595                 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
10596
10597                 /* Free the route if we got one back */
10598                 if (ro.ro_rt)
10599                         RTFREE(ro.ro_rt);
10600         }
10601 #ifdef INET6
10602         if (ip6_out != NULL) {
10603                 struct route_in6 ro;
10604                 int ret;
10605                 struct sctp_tcb *stcb = NULL;
10606                 struct ifnet *ifp = NULL;
10607
10608                 bzero(&ro, sizeof(ro));
10609                 mlen = SCTP_BUF_LEN(mout);
10610 #ifdef  SCTP_PACKET_LOGGING
10611                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
10612                         sctp_packet_log(mout, mlen);
10613 #endif
10614                 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
10615                 if (port) {
10616                         if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
10617                             (stcb) &&
10618                             (stcb->asoc.loopback_scope))) {
10619                                 comp_cp->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
10620                                 SCTP_STAT_INCR(sctps_sendswcrc);
10621                         } else {
10622                                 SCTP_STAT_INCR(sctps_sendnocrc);
10623                         }
10624                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), mlen - sizeof(struct ip6_hdr))) == 0) {
10625                                 udp->uh_sum = 0xffff;
10626                         }
10627                 } else {
10628                         if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
10629                             (stcb) &&
10630                             (stcb->asoc.loopback_scope))) {
10631                                 mout->m_pkthdr.csum_flags = CSUM_SCTP;
10632                                 mout->m_pkthdr.csum_data = 0;
10633                                 SCTP_STAT_INCR(sctps_sendhwcrc);
10634                         } else {
10635                                 SCTP_STAT_INCR(sctps_sendnocrc);
10636                         }
10637                 }
10638                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
10639
10640                 /* Free the route if we got one back */
10641                 if (ro.ro_rt)
10642                         RTFREE(ro.ro_rt);
10643         }
10644 #endif
10645         SCTP_STAT_INCR(sctps_sendpackets);
10646         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
10647         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10648         return;
10649
10650 }
10651
10652 static struct sctp_nets *
10653 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
10654 {
10655         struct sctp_nets *net, *hnet;
10656         int ms_goneby, highest_ms, state_overide = 0;
10657
10658         (void)SCTP_GETTIME_TIMEVAL(now);
10659         highest_ms = 0;
10660         hnet = NULL;
10661         SCTP_TCB_LOCK_ASSERT(stcb);
10662         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
10663                 if (
10664                     ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
10665                     (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
10666                     ) {
10667                         /*
10668                          * Skip this guy from consideration if HB is off AND
10669                          * its confirmed
10670                          */
10671                         continue;
10672                 }
10673                 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
10674                         /* skip this dest net from consideration */
10675                         continue;
10676                 }
10677                 if (net->last_sent_time.tv_sec) {
10678                         /* Sent to so we subtract */
10679                         ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
10680                 } else
10681                         /* Never been sent to */
10682                         ms_goneby = 0x7fffffff;
10683                 /*-
10684                  * When the address state is unconfirmed but still
10685                  * considered reachable, we HB at a higher rate. Once it
10686                  * goes confirmed OR reaches the "unreachable" state, thenw
10687                  * we cut it back to HB at a more normal pace.
10688                  */
10689                 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
10690                         state_overide = 1;
10691                 } else {
10692                         state_overide = 0;
10693                 }
10694
10695                 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
10696                     (ms_goneby > highest_ms)) {
10697                         highest_ms = ms_goneby;
10698                         hnet = net;
10699                 }
10700         }
10701         if (hnet &&
10702             ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
10703                 state_overide = 1;
10704         } else {
10705                 state_overide = 0;
10706         }
10707
10708         if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
10709                 /*-
10710                  * Found the one with longest delay bounds OR it is
10711                  * unconfirmed and still not marked unreachable.
10712                  */
10713                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
10714 #ifdef SCTP_DEBUG
10715                 if (hnet) {
10716                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
10717                             (struct sockaddr *)&hnet->ro._l_addr);
10718                 } else {
10719                         SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
10720                 }
10721 #endif
10722                 /* update the timer now */
10723                 hnet->last_sent_time = *now;
10724                 return (hnet);
10725         }
10726         /* Nothing to HB */
10727         return (NULL);
10728 }
10729
10730 int
10731 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
10732 {
10733         struct sctp_tmit_chunk *chk;
10734         struct sctp_nets *net;
10735         struct sctp_heartbeat_chunk *hb;
10736         struct timeval now;
10737         struct sockaddr_in *sin;
10738         struct sockaddr_in6 *sin6;
10739
10740         SCTP_TCB_LOCK_ASSERT(stcb);
10741         if (user_req == 0) {
10742                 net = sctp_select_hb_destination(stcb, &now);
10743                 if (net == NULL) {
10744                         /*-
10745                          * All our busy none to send to, just start the
10746                          * timer again.
10747                          */
10748                         if (stcb->asoc.state == 0) {
10749                                 return (0);
10750                         }
10751                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
10752                             stcb->sctp_ep,
10753                             stcb,
10754                             net);
10755                         return (0);
10756                 }
10757         } else {
10758                 net = u_net;
10759                 if (net == NULL) {
10760                         return (0);
10761                 }
10762                 (void)SCTP_GETTIME_TIMEVAL(&now);
10763         }
10764         sin = (struct sockaddr_in *)&net->ro._l_addr;
10765         if (sin->sin_family != AF_INET) {
10766                 if (sin->sin_family != AF_INET6) {
10767                         /* huh */
10768                         return (0);
10769                 }
10770         }
10771         sctp_alloc_a_chunk(stcb, chk);
10772         if (chk == NULL) {
10773                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
10774                 return (0);
10775         }
10776         chk->copy_by_ref = 0;
10777         chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
10778         chk->rec.chunk_id.can_take_data = 1;
10779         chk->asoc = &stcb->asoc;
10780         chk->send_size = sizeof(struct sctp_heartbeat_chunk);
10781
10782         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10783         if (chk->data == NULL) {
10784                 sctp_free_a_chunk(stcb, chk);
10785                 return (0);
10786         }
10787         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10788         SCTP_BUF_LEN(chk->data) = chk->send_size;
10789         chk->sent = SCTP_DATAGRAM_UNSENT;
10790         chk->snd_count = 0;
10791         chk->whoTo = net;
10792         atomic_add_int(&chk->whoTo->ref_count, 1);
10793         /* Now we have a mbuf that we can fill in with the details */
10794         hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
10795         memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
10796         /* fill out chunk header */
10797         hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
10798         hb->ch.chunk_flags = 0;
10799         hb->ch.chunk_length = htons(chk->send_size);
10800         /* Fill out hb parameter */
10801         hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
10802         hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
10803         hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
10804         hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
10805         /* Did our user request this one, put it in */
10806         hb->heartbeat.hb_info.user_req = user_req;
10807         hb->heartbeat.hb_info.addr_family = sin->sin_family;
10808         hb->heartbeat.hb_info.addr_len = sin->sin_len;
10809         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
10810                 /*
10811                  * we only take from the entropy pool if the address is not
10812                  * confirmed.
10813                  */
10814                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10815                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
10816         } else {
10817                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
10818                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
10819         }
10820         if (sin->sin_family == AF_INET) {
10821                 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
10822         } else if (sin->sin_family == AF_INET6) {
10823                 /* We leave the scope the way it is in our lookup table. */
10824                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
10825                 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
10826         } else {
10827                 /* huh compiler bug */
10828                 return (0);
10829         }
10830
10831         /*
10832          * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
10833          * PF-heartbeats.  Because of this, threshold management is done by
10834          * the t3 timer handler, and does not need to be done upon the send
10835          * of a PF-heartbeat. If CMT PF is on and the destination to which a
10836          * heartbeat is being sent is in PF state, do NOT do threshold
10837          * management.
10838          */
10839         if ((stcb->asoc.sctp_cmt_pf == 0) ||
10840             ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
10841                 /* ok we have a destination that needs a beat */
10842                 /* lets do the theshold management Qiaobing style */
10843                 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
10844                     stcb->asoc.max_send_times)) {
10845                         /*-
10846                          * we have lost the association, in a way this is
10847                          * quite bad since we really are one less time since
10848                          * we really did not send yet. This is the down side
10849                          * to the Q's style as defined in the RFC and not my
10850                          * alternate style defined in the RFC.
10851                          */
10852                         if (chk->data != NULL) {
10853                                 sctp_m_freem(chk->data);
10854                                 chk->data = NULL;
10855                         }
10856                         /*
10857                          * Here we do NOT use the macro since the
10858                          * association is now gone.
10859                          */
10860                         if (chk->whoTo) {
10861                                 sctp_free_remote_addr(chk->whoTo);
10862                                 chk->whoTo = NULL;
10863                         }
10864                         sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
10865                         return (-1);
10866                 }
10867         }
10868         net->hb_responded = 0;
10869         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10870         stcb->asoc.ctrl_queue_cnt++;
10871         SCTP_STAT_INCR(sctps_sendheartbeat);
10872         /*-
10873          * Call directly med level routine to put out the chunk. It will
10874          * always tumble out control chunks aka HB but it may even tumble
10875          * out data too.
10876          */
10877         return (1);
10878 }
10879
10880 void
10881 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
10882     uint32_t high_tsn)
10883 {
10884         struct sctp_association *asoc;
10885         struct sctp_ecne_chunk *ecne;
10886         struct sctp_tmit_chunk *chk;
10887
10888         asoc = &stcb->asoc;
10889         SCTP_TCB_LOCK_ASSERT(stcb);
10890         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10891                 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
10892                         /* found a previous ECN_ECHO update it if needed */
10893                         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10894                         ecne->tsn = htonl(high_tsn);
10895                         return;
10896                 }
10897         }
10898         /* nope could not find one to update so we must build one */
10899         sctp_alloc_a_chunk(stcb, chk);
10900         if (chk == NULL) {
10901                 return;
10902         }
10903         chk->copy_by_ref = 0;
10904         SCTP_STAT_INCR(sctps_sendecne);
10905         chk->rec.chunk_id.id = SCTP_ECN_ECHO;
10906         chk->rec.chunk_id.can_take_data = 0;
10907         chk->asoc = &stcb->asoc;
10908         chk->send_size = sizeof(struct sctp_ecne_chunk);
10909         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
10910         if (chk->data == NULL) {
10911                 sctp_free_a_chunk(stcb, chk);
10912                 return;
10913         }
10914         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10915         SCTP_BUF_LEN(chk->data) = chk->send_size;
10916         chk->sent = SCTP_DATAGRAM_UNSENT;
10917         chk->snd_count = 0;
10918         chk->whoTo = net;
10919         atomic_add_int(&chk->whoTo->ref_count, 1);
10920         stcb->asoc.ecn_echo_cnt_onq++;
10921         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
10922         ecne->ch.chunk_type = SCTP_ECN_ECHO;
10923         ecne->ch.chunk_flags = 0;
10924         ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
10925         ecne->tsn = htonl(high_tsn);
10926         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
10927         asoc->ctrl_queue_cnt++;
10928 }
10929
10930 void
10931 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
10932     struct mbuf *m, int iphlen, int bad_crc)
10933 {
10934         struct sctp_association *asoc;
10935         struct sctp_pktdrop_chunk *drp;
10936         struct sctp_tmit_chunk *chk;
10937         uint8_t *datap;
10938         int len;
10939         int was_trunc = 0;
10940         struct ip *iph;
10941
10942 #ifdef INET6
10943         struct ip6_hdr *ip6h;
10944
10945 #endif
10946         int fullsz = 0, extra = 0;
10947         long spc;
10948         int offset;
10949         struct sctp_chunkhdr *ch, chunk_buf;
10950         unsigned int chk_length;
10951
10952         if (!stcb) {
10953                 return;
10954         }
10955         asoc = &stcb->asoc;
10956         SCTP_TCB_LOCK_ASSERT(stcb);
10957         if (asoc->peer_supports_pktdrop == 0) {
10958                 /*-
10959                  * peer must declare support before I send one.
10960                  */
10961                 return;
10962         }
10963         if (stcb->sctp_socket == NULL) {
10964                 return;
10965         }
10966         sctp_alloc_a_chunk(stcb, chk);
10967         if (chk == NULL) {
10968                 return;
10969         }
10970         chk->copy_by_ref = 0;
10971         iph = mtod(m, struct ip *);
10972         if (iph == NULL) {
10973                 sctp_free_a_chunk(stcb, chk);
10974                 return;
10975         }
10976         switch (iph->ip_v) {
10977         case IPVERSION:
10978                 /* IPv4 */
10979                 len = chk->send_size = iph->ip_len;
10980                 break;
10981 #ifdef INET6
10982         case IPV6_VERSION >> 4:
10983                 /* IPv6 */
10984                 ip6h = mtod(m, struct ip6_hdr *);
10985                 len = chk->send_size = htons(ip6h->ip6_plen);
10986                 break;
10987 #endif
10988         default:
10989                 return;
10990         }
10991         /* Validate that we do not have an ABORT in here. */
10992         offset = iphlen + sizeof(struct sctphdr);
10993         ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
10994             sizeof(*ch), (uint8_t *) & chunk_buf);
10995         while (ch != NULL) {
10996                 chk_length = ntohs(ch->chunk_length);
10997                 if (chk_length < sizeof(*ch)) {
10998                         /* break to abort land */
10999                         break;
11000                 }
11001                 switch (ch->chunk_type) {
11002                 case SCTP_PACKET_DROPPED:
11003                 case SCTP_ABORT_ASSOCIATION:
11004                         /*-
11005                          * we don't respond with an PKT-DROP to an ABORT
11006                          * or PKT-DROP
11007                          */
11008                         sctp_free_a_chunk(stcb, chk);
11009                         return;
11010                 default:
11011                         break;
11012                 }
11013                 offset += SCTP_SIZE32(chk_length);
11014                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11015                     sizeof(*ch), (uint8_t *) & chunk_buf);
11016         }
11017
11018         if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11019             min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11020                 /*
11021                  * only send 1 mtu worth, trim off the excess on the end.
11022                  */
11023                 fullsz = len - extra;
11024                 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11025                 was_trunc = 1;
11026         }
11027         chk->asoc = &stcb->asoc;
11028         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11029         if (chk->data == NULL) {
11030 jump_out:
11031                 sctp_free_a_chunk(stcb, chk);
11032                 return;
11033         }
11034         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11035         drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11036         if (drp == NULL) {
11037                 sctp_m_freem(chk->data);
11038                 chk->data = NULL;
11039                 goto jump_out;
11040         }
11041         chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11042             sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11043         chk->book_size_scale = 0;
11044         if (was_trunc) {
11045                 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11046                 drp->trunc_len = htons(fullsz);
11047                 /*
11048                  * Len is already adjusted to size minus overhead above take
11049                  * out the pkt_drop chunk itself from it.
11050                  */
11051                 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
11052                 len = chk->send_size;
11053         } else {
11054                 /* no truncation needed */
11055                 drp->ch.chunk_flags = 0;
11056                 drp->trunc_len = htons(0);
11057         }
11058         if (bad_crc) {
11059                 drp->ch.chunk_flags |= SCTP_BADCRC;
11060         }
11061         chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11062         SCTP_BUF_LEN(chk->data) = chk->send_size;
11063         chk->sent = SCTP_DATAGRAM_UNSENT;
11064         chk->snd_count = 0;
11065         if (net) {
11066                 /* we should hit here */
11067                 chk->whoTo = net;
11068         } else {
11069                 chk->whoTo = asoc->primary_destination;
11070         }
11071         atomic_add_int(&chk->whoTo->ref_count, 1);
11072         chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11073         chk->rec.chunk_id.can_take_data = 1;
11074         drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11075         drp->ch.chunk_length = htons(chk->send_size);
11076         spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11077         if (spc < 0) {
11078                 spc = 0;
11079         }
11080         drp->bottle_bw = htonl(spc);
11081         if (asoc->my_rwnd) {
11082                 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11083                     asoc->size_on_all_streams +
11084                     asoc->my_rwnd_control_len +
11085                     stcb->sctp_socket->so_rcv.sb_cc);
11086         } else {
11087                 /*-
11088                  * If my rwnd is 0, possibly from mbuf depletion as well as
11089                  * space used, tell the peer there is NO space aka onq == bw
11090                  */
11091                 drp->current_onq = htonl(spc);
11092         }
11093         drp->reserved = 0;
11094         datap = drp->data;
11095         m_copydata(m, iphlen, len, (caddr_t)datap);
11096         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11097         asoc->ctrl_queue_cnt++;
11098 }
11099
11100 void
11101 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
11102 {
11103         struct sctp_association *asoc;
11104         struct sctp_cwr_chunk *cwr;
11105         struct sctp_tmit_chunk *chk;
11106
11107         asoc = &stcb->asoc;
11108         SCTP_TCB_LOCK_ASSERT(stcb);
11109         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11110                 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
11111                         /* found a previous ECN_CWR update it if needed */
11112                         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11113                         if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
11114                             MAX_TSN)) {
11115                                 cwr->tsn = htonl(high_tsn);
11116                         }
11117                         return;
11118                 }
11119         }
11120         /* nope could not find one to update so we must build one */
11121         sctp_alloc_a_chunk(stcb, chk);
11122         if (chk == NULL) {
11123                 return;
11124         }
11125         chk->copy_by_ref = 0;
11126         chk->rec.chunk_id.id = SCTP_ECN_CWR;
11127         chk->rec.chunk_id.can_take_data = 1;
11128         chk->asoc = &stcb->asoc;
11129         chk->send_size = sizeof(struct sctp_cwr_chunk);
11130         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
11131         if (chk->data == NULL) {
11132                 sctp_free_a_chunk(stcb, chk);
11133                 return;
11134         }
11135         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11136         SCTP_BUF_LEN(chk->data) = chk->send_size;
11137         chk->sent = SCTP_DATAGRAM_UNSENT;
11138         chk->snd_count = 0;
11139         chk->whoTo = net;
11140         atomic_add_int(&chk->whoTo->ref_count, 1);
11141         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11142         cwr->ch.chunk_type = SCTP_ECN_CWR;
11143         cwr->ch.chunk_flags = 0;
11144         cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11145         cwr->tsn = htonl(high_tsn);
11146         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11147         asoc->ctrl_queue_cnt++;
11148 }
11149
11150 void
11151 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
11152     int number_entries, uint16_t * list,
11153     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11154 {
11155         int len, old_len, i;
11156         struct sctp_stream_reset_out_request *req_out;
11157         struct sctp_chunkhdr *ch;
11158
11159         ch = mtod(chk->data, struct sctp_chunkhdr *);
11160
11161
11162         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11163
11164         /* get to new offset for the param. */
11165         req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11166         /* now how long will this param be? */
11167         len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11168         req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11169         req_out->ph.param_length = htons(len);
11170         req_out->request_seq = htonl(seq);
11171         req_out->response_seq = htonl(resp_seq);
11172         req_out->send_reset_at_tsn = htonl(last_sent);
11173         if (number_entries) {
11174                 for (i = 0; i < number_entries; i++) {
11175                         req_out->list_of_streams[i] = htons(list[i]);
11176                 }
11177         }
11178         if (SCTP_SIZE32(len) > len) {
11179                 /*-
11180                  * Need to worry about the pad we may end up adding to the
11181                  * end. This is easy since the struct is either aligned to 4
11182                  * bytes or 2 bytes off.
11183                  */
11184                 req_out->list_of_streams[number_entries] = 0;
11185         }
11186         /* now fix the chunk length */
11187         ch->chunk_length = htons(len + old_len);
11188         chk->book_size = len + old_len;
11189         chk->book_size_scale = 0;
11190         chk->send_size = SCTP_SIZE32(chk->book_size);
11191         SCTP_BUF_LEN(chk->data) = chk->send_size;
11192         return;
11193 }
11194
11195
11196 void
11197 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11198     int number_entries, uint16_t * list,
11199     uint32_t seq)
11200 {
11201         int len, old_len, i;
11202         struct sctp_stream_reset_in_request *req_in;
11203         struct sctp_chunkhdr *ch;
11204
11205         ch = mtod(chk->data, struct sctp_chunkhdr *);
11206
11207
11208         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11209
11210         /* get to new offset for the param. */
11211         req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11212         /* now how long will this param be? */
11213         len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11214         req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11215         req_in->ph.param_length = htons(len);
11216         req_in->request_seq = htonl(seq);
11217         if (number_entries) {
11218                 for (i = 0; i < number_entries; i++) {
11219                         req_in->list_of_streams[i] = htons(list[i]);
11220                 }
11221         }
11222         if (SCTP_SIZE32(len) > len) {
11223                 /*-
11224                  * Need to worry about the pad we may end up adding to the
11225                  * end. This is easy since the struct is either aligned to 4
11226                  * bytes or 2 bytes off.
11227                  */
11228                 req_in->list_of_streams[number_entries] = 0;
11229         }
11230         /* now fix the chunk length */
11231         ch->chunk_length = htons(len + old_len);
11232         chk->book_size = len + old_len;
11233         chk->book_size_scale = 0;
11234         chk->send_size = SCTP_SIZE32(chk->book_size);
11235         SCTP_BUF_LEN(chk->data) = chk->send_size;
11236         return;
11237 }
11238
11239
11240 void
11241 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11242     uint32_t seq)
11243 {
11244         int len, old_len;
11245         struct sctp_stream_reset_tsn_request *req_tsn;
11246         struct sctp_chunkhdr *ch;
11247
11248         ch = mtod(chk->data, struct sctp_chunkhdr *);
11249
11250
11251         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11252
11253         /* get to new offset for the param. */
11254         req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11255         /* now how long will this param be? */
11256         len = sizeof(struct sctp_stream_reset_tsn_request);
11257         req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11258         req_tsn->ph.param_length = htons(len);
11259         req_tsn->request_seq = htonl(seq);
11260
11261         /* now fix the chunk length */
11262         ch->chunk_length = htons(len + old_len);
11263         chk->send_size = len + old_len;
11264         chk->book_size = SCTP_SIZE32(chk->send_size);
11265         chk->book_size_scale = 0;
11266         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11267         return;
11268 }
11269
11270 void
11271 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11272     uint32_t resp_seq, uint32_t result)
11273 {
11274         int len, old_len;
11275         struct sctp_stream_reset_response *resp;
11276         struct sctp_chunkhdr *ch;
11277
11278         ch = mtod(chk->data, struct sctp_chunkhdr *);
11279
11280
11281         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11282
11283         /* get to new offset for the param. */
11284         resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11285         /* now how long will this param be? */
11286         len = sizeof(struct sctp_stream_reset_response);
11287         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11288         resp->ph.param_length = htons(len);
11289         resp->response_seq = htonl(resp_seq);
11290         resp->result = ntohl(result);
11291
11292         /* now fix the chunk length */
11293         ch->chunk_length = htons(len + old_len);
11294         chk->book_size = len + old_len;
11295         chk->book_size_scale = 0;
11296         chk->send_size = SCTP_SIZE32(chk->book_size);
11297         SCTP_BUF_LEN(chk->data) = chk->send_size;
11298         return;
11299
11300 }
11301
11302
11303 void
11304 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11305     uint32_t resp_seq, uint32_t result,
11306     uint32_t send_una, uint32_t recv_next)
11307 {
11308         int len, old_len;
11309         struct sctp_stream_reset_response_tsn *resp;
11310         struct sctp_chunkhdr *ch;
11311
11312         ch = mtod(chk->data, struct sctp_chunkhdr *);
11313
11314
11315         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11316
11317         /* get to new offset for the param. */
11318         resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11319         /* now how long will this param be? */
11320         len = sizeof(struct sctp_stream_reset_response_tsn);
11321         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11322         resp->ph.param_length = htons(len);
11323         resp->response_seq = htonl(resp_seq);
11324         resp->result = htonl(result);
11325         resp->senders_next_tsn = htonl(send_una);
11326         resp->receivers_next_tsn = htonl(recv_next);
11327
11328         /* now fix the chunk length */
11329         ch->chunk_length = htons(len + old_len);
11330         chk->book_size = len + old_len;
11331         chk->send_size = SCTP_SIZE32(chk->book_size);
11332         chk->book_size_scale = 0;
11333         SCTP_BUF_LEN(chk->data) = chk->send_size;
11334         return;
11335 }
11336
11337 static void
11338 sctp_add_a_stream(struct sctp_tmit_chunk *chk,
11339     uint32_t seq,
11340     uint16_t adding)
11341 {
11342         int len, old_len;
11343         struct sctp_chunkhdr *ch;
11344         struct sctp_stream_reset_add_strm *addstr;
11345
11346         ch = mtod(chk->data, struct sctp_chunkhdr *);
11347         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11348
11349         /* get to new offset for the param. */
11350         addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11351         /* now how long will this param be? */
11352         len = sizeof(struct sctp_stream_reset_add_strm);
11353
11354         /* Fill it out. */
11355         addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_STREAMS);
11356         addstr->ph.param_length = htons(len);
11357         addstr->request_seq = htonl(seq);
11358         addstr->number_of_streams = htons(adding);
11359         addstr->reserved = 0;
11360
11361         /* now fix the chunk length */
11362         ch->chunk_length = htons(len + old_len);
11363         chk->send_size = len + old_len;
11364         chk->book_size = SCTP_SIZE32(chk->send_size);
11365         chk->book_size_scale = 0;
11366         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11367         return;
11368 }
11369
11370 int
11371 sctp_send_str_reset_req(struct sctp_tcb *stcb,
11372     int number_entries, uint16_t * list,
11373     uint8_t send_out_req,
11374     uint32_t resp_seq,
11375     uint8_t send_in_req,
11376     uint8_t send_tsn_req,
11377     uint8_t add_stream,
11378     uint16_t adding
11379 )
11380 {
11381
11382         struct sctp_association *asoc;
11383         struct sctp_tmit_chunk *chk;
11384         struct sctp_chunkhdr *ch;
11385         uint32_t seq;
11386
11387         asoc = &stcb->asoc;
11388         if (asoc->stream_reset_outstanding) {
11389                 /*-
11390                  * Already one pending, must get ACK back to clear the flag.
11391                  */
11392                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
11393                 return (EBUSY);
11394         }
11395         if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0) &&
11396             (add_stream == 0)) {
11397                 /* nothing to do */
11398                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11399                 return (EINVAL);
11400         }
11401         if (send_tsn_req && (send_out_req || send_in_req)) {
11402                 /* error, can't do that */
11403                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11404                 return (EINVAL);
11405         }
11406         sctp_alloc_a_chunk(stcb, chk);
11407         if (chk == NULL) {
11408                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11409                 return (ENOMEM);
11410         }
11411         chk->copy_by_ref = 0;
11412         chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11413         chk->rec.chunk_id.can_take_data = 0;
11414         chk->asoc = &stcb->asoc;
11415         chk->book_size = sizeof(struct sctp_chunkhdr);
11416         chk->send_size = SCTP_SIZE32(chk->book_size);
11417         chk->book_size_scale = 0;
11418
11419         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11420         if (chk->data == NULL) {
11421                 sctp_free_a_chunk(stcb, chk);
11422                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11423                 return (ENOMEM);
11424         }
11425         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11426
11427         /* setup chunk parameters */
11428         chk->sent = SCTP_DATAGRAM_UNSENT;
11429         chk->snd_count = 0;
11430         chk->whoTo = asoc->primary_destination;
11431         atomic_add_int(&chk->whoTo->ref_count, 1);
11432
11433         ch = mtod(chk->data, struct sctp_chunkhdr *);
11434         ch->chunk_type = SCTP_STREAM_RESET;
11435         ch->chunk_flags = 0;
11436         ch->chunk_length = htons(chk->book_size);
11437         SCTP_BUF_LEN(chk->data) = chk->send_size;
11438
11439         seq = stcb->asoc.str_reset_seq_out;
11440         if (send_out_req) {
11441                 sctp_add_stream_reset_out(chk, number_entries, list,
11442                     seq, resp_seq, (stcb->asoc.sending_seq - 1));
11443                 asoc->stream_reset_out_is_outstanding = 1;
11444                 seq++;
11445                 asoc->stream_reset_outstanding++;
11446         }
11447         if (add_stream) {
11448                 sctp_add_a_stream(chk, seq, adding);
11449                 seq++;
11450                 asoc->stream_reset_outstanding++;
11451         }
11452         if (send_in_req) {
11453                 sctp_add_stream_reset_in(chk, number_entries, list, seq);
11454                 asoc->stream_reset_outstanding++;
11455         }
11456         if (send_tsn_req) {
11457                 sctp_add_stream_reset_tsn(chk, seq);
11458                 asoc->stream_reset_outstanding++;
11459         }
11460         asoc->str_reset = chk;
11461
11462         /* insert the chunk for sending */
11463         TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11464             chk,
11465             sctp_next);
11466         asoc->ctrl_queue_cnt++;
11467         sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
11468         return (0);
11469 }
11470
11471 void
11472 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
11473     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
11474 {
11475         /*-
11476          * Formulate the abort message, and send it back down.
11477          */
11478         struct mbuf *o_pak;
11479         struct mbuf *mout;
11480         struct sctp_abort_msg *abm;
11481         struct ip *iph, *iph_out;
11482         struct udphdr *udp;
11483
11484 #ifdef INET6
11485         struct ip6_hdr *ip6, *ip6_out;
11486
11487 #endif
11488         int iphlen_out, len;
11489
11490         /* don't respond to ABORT with ABORT */
11491         if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
11492                 if (err_cause)
11493                         sctp_m_freem(err_cause);
11494                 return;
11495         }
11496         iph = mtod(m, struct ip *);
11497         switch (iph->ip_v) {
11498         case IPVERSION:
11499                 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
11500                 break;
11501 #ifdef INET6
11502         case IPV6_VERSION >> 4:
11503                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
11504                 break;
11505 #endif
11506         default:
11507                 if (err_cause) {
11508                         sctp_m_freem(err_cause);
11509                 }
11510                 return;
11511         }
11512         if (port) {
11513                 len += sizeof(struct udphdr);
11514         }
11515         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11516         if (mout == NULL) {
11517                 if (err_cause) {
11518                         sctp_m_freem(err_cause);
11519                 }
11520                 return;
11521         }
11522         SCTP_BUF_RESV_UF(mout, max_linkhdr);
11523         SCTP_BUF_LEN(mout) = len;
11524         SCTP_BUF_NEXT(mout) = err_cause;
11525         iph_out = NULL;
11526 #ifdef INET6
11527         ip6_out = NULL;
11528 #endif
11529         switch (iph->ip_v) {
11530         case IPVERSION:
11531                 iph_out = mtod(mout, struct ip *);
11532
11533                 /* Fill in the IP header for the ABORT */
11534                 iph_out->ip_v = IPVERSION;
11535                 iph_out->ip_hl = (sizeof(struct ip) / 4);
11536                 iph_out->ip_tos = (u_char)0;
11537                 iph_out->ip_id = 0;
11538                 iph_out->ip_off = 0;
11539                 iph_out->ip_ttl = MAXTTL;
11540                 if (port) {
11541                         iph_out->ip_p = IPPROTO_UDP;
11542                 } else {
11543                         iph_out->ip_p = IPPROTO_SCTP;
11544                 }
11545                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11546                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11547                 /* let IP layer calculate this */
11548                 iph_out->ip_sum = 0;
11549
11550                 iphlen_out = sizeof(*iph_out);
11551                 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
11552                 break;
11553 #ifdef INET6
11554         case IPV6_VERSION >> 4:
11555                 ip6 = (struct ip6_hdr *)iph;
11556                 ip6_out = mtod(mout, struct ip6_hdr *);
11557
11558                 /* Fill in the IP6 header for the ABORT */
11559                 ip6_out->ip6_flow = ip6->ip6_flow;
11560                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11561                 if (port) {
11562                         ip6_out->ip6_nxt = IPPROTO_UDP;
11563                 } else {
11564                         ip6_out->ip6_nxt = IPPROTO_SCTP;
11565                 }
11566                 ip6_out->ip6_src = ip6->ip6_dst;
11567                 ip6_out->ip6_dst = ip6->ip6_src;
11568
11569                 iphlen_out = sizeof(*ip6_out);
11570                 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
11571                 break;
11572 #endif                          /* INET6 */
11573         default:
11574                 /* Currently not supported */
11575                 sctp_m_freem(mout);
11576                 return;
11577         }
11578
11579         udp = (struct udphdr *)abm;
11580         if (port) {
11581                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11582                 udp->uh_dport = port;
11583                 /* set udp->uh_ulen later */
11584                 udp->uh_sum = 0;
11585                 iphlen_out += sizeof(struct udphdr);
11586                 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
11587         }
11588         abm->sh.src_port = sh->dest_port;
11589         abm->sh.dest_port = sh->src_port;
11590         abm->sh.checksum = 0;
11591         if (vtag == 0) {
11592                 abm->sh.v_tag = sh->v_tag;
11593                 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
11594         } else {
11595                 abm->sh.v_tag = htonl(vtag);
11596                 abm->msg.ch.chunk_flags = 0;
11597         }
11598         abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11599
11600         if (err_cause) {
11601                 struct mbuf *m_tmp = err_cause;
11602                 int err_len = 0;
11603
11604                 /* get length of the err_cause chain */
11605                 while (m_tmp != NULL) {
11606                         err_len += SCTP_BUF_LEN(m_tmp);
11607                         m_tmp = SCTP_BUF_NEXT(m_tmp);
11608                 }
11609                 len = SCTP_BUF_LEN(mout) + err_len;
11610                 if (err_len % 4) {
11611                         /* need pad at end of chunk */
11612                         uint32_t cpthis = 0;
11613                         int padlen;
11614
11615                         padlen = 4 - (len % 4);
11616                         m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11617                         len += padlen;
11618                 }
11619                 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
11620         } else {
11621                 len = SCTP_BUF_LEN(mout);
11622                 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
11623         }
11624
11625         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11626                 /* no mbuf's */
11627                 sctp_m_freem(mout);
11628                 return;
11629         }
11630         if (iph_out != NULL) {
11631                 sctp_route_t ro;
11632                 struct sctp_tcb *stcb = NULL;
11633                 int ret;
11634
11635                 /* zap the stack pointer to the route */
11636                 bzero(&ro, sizeof ro);
11637                 if (port) {
11638                         udp->uh_ulen = htons(len - sizeof(struct ip));
11639                         udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11640                 }
11641                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
11642                 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
11643                 /* set IPv4 length */
11644                 iph_out->ip_len = len;
11645                 /* out it goes */
11646 #ifdef  SCTP_PACKET_LOGGING
11647                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11648                         sctp_packet_log(mout, len);
11649 #endif
11650                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11651                 if (port) {
11652                         abm->sh.checksum = sctp_calculate_cksum(mout, iphlen_out);
11653                         SCTP_STAT_INCR(sctps_sendswcrc);
11654                         SCTP_ENABLE_UDP_CSUM(o_pak);
11655                 } else {
11656                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
11657                         mout->m_pkthdr.csum_data = 0;
11658                         SCTP_STAT_INCR(sctps_sendhwcrc);
11659                 }
11660                 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11661
11662                 /* Free the route if we got one back */
11663                 if (ro.ro_rt)
11664                         RTFREE(ro.ro_rt);
11665         }
11666 #ifdef INET6
11667         if (ip6_out != NULL) {
11668                 struct route_in6 ro;
11669                 int ret;
11670                 struct sctp_tcb *stcb = NULL;
11671                 struct ifnet *ifp = NULL;
11672
11673                 /* zap the stack pointer to the route */
11674                 bzero(&ro, sizeof(ro));
11675                 if (port) {
11676                         udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11677                 }
11678                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
11679                 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
11680                 ip6_out->ip6_plen = len - sizeof(*ip6_out);
11681 #ifdef  SCTP_PACKET_LOGGING
11682                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11683                         sctp_packet_log(mout, len);
11684 #endif
11685                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11686                 if (port) {
11687                         if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11688                             (stcb) &&
11689                             (stcb->asoc.loopback_scope))) {
11690                                 abm->sh.checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11691                                 SCTP_STAT_INCR(sctps_sendswcrc);
11692                         } else {
11693                                 SCTP_STAT_INCR(sctps_sendnocrc);
11694                         }
11695                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11696                                 udp->uh_sum = 0xffff;
11697                         }
11698                 } else {
11699                         if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11700                             (stcb) &&
11701                             (stcb->asoc.loopback_scope))) {
11702                                 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11703                                 mout->m_pkthdr.csum_data = 0;
11704                                 SCTP_STAT_INCR(sctps_sendhwcrc);
11705                         } else {
11706                                 SCTP_STAT_INCR(sctps_sendnocrc);
11707                         }
11708                 }
11709                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11710
11711                 /* Free the route if we got one back */
11712                 if (ro.ro_rt)
11713                         RTFREE(ro.ro_rt);
11714         }
11715 #endif
11716         SCTP_STAT_INCR(sctps_sendpackets);
11717         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11718         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11719 }
11720
11721 void
11722 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
11723     uint32_t vrf_id, uint16_t port)
11724 {
11725         struct mbuf *o_pak;
11726         struct sctphdr *sh, *sh_out;
11727         struct sctp_chunkhdr *ch;
11728         struct ip *iph, *iph_out;
11729         struct udphdr *udp = NULL;
11730         struct mbuf *mout;
11731
11732 #ifdef INET6
11733         struct ip6_hdr *ip6, *ip6_out;
11734
11735 #endif
11736         int iphlen_out, len;
11737
11738         iph = mtod(m, struct ip *);
11739         sh = (struct sctphdr *)((caddr_t)iph + iphlen);
11740         switch (iph->ip_v) {
11741         case IPVERSION:
11742                 len = (sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11743                 break;
11744 #ifdef INET6
11745         case IPV6_VERSION >> 4:
11746                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr));
11747                 break;
11748 #endif
11749         default:
11750                 if (scm) {
11751                         sctp_m_freem(scm);
11752                 }
11753                 return;
11754         }
11755         if (port) {
11756                 len += sizeof(struct udphdr);
11757         }
11758         mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_DONTWAIT, 1, MT_DATA);
11759         if (mout == NULL) {
11760                 if (scm) {
11761                         sctp_m_freem(scm);
11762                 }
11763                 return;
11764         }
11765         SCTP_BUF_RESV_UF(mout, max_linkhdr);
11766         SCTP_BUF_LEN(mout) = len;
11767         SCTP_BUF_NEXT(mout) = scm;
11768         iph_out = NULL;
11769 #ifdef INET6
11770         ip6_out = NULL;
11771 #endif
11772         switch (iph->ip_v) {
11773         case IPVERSION:
11774                 iph_out = mtod(mout, struct ip *);
11775
11776                 /* Fill in the IP header for the ABORT */
11777                 iph_out->ip_v = IPVERSION;
11778                 iph_out->ip_hl = (sizeof(struct ip) / 4);
11779                 iph_out->ip_tos = (u_char)0;
11780                 iph_out->ip_id = 0;
11781                 iph_out->ip_off = 0;
11782                 iph_out->ip_ttl = MAXTTL;
11783                 if (port) {
11784                         iph_out->ip_p = IPPROTO_UDP;
11785                 } else {
11786                         iph_out->ip_p = IPPROTO_SCTP;
11787                 }
11788                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
11789                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
11790                 /* let IP layer calculate this */
11791                 iph_out->ip_sum = 0;
11792
11793                 iphlen_out = sizeof(struct ip);
11794                 sh_out = (struct sctphdr *)((caddr_t)iph_out + iphlen_out);
11795                 break;
11796 #ifdef INET6
11797         case IPV6_VERSION >> 4:
11798                 ip6 = (struct ip6_hdr *)iph;
11799                 ip6_out = mtod(mout, struct ip6_hdr *);
11800
11801                 /* Fill in the IP6 header for the ABORT */
11802                 ip6_out->ip6_flow = ip6->ip6_flow;
11803                 ip6_out->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11804                 if (port) {
11805                         ip6_out->ip6_nxt = IPPROTO_UDP;
11806                 } else {
11807                         ip6_out->ip6_nxt = IPPROTO_SCTP;
11808                 }
11809                 ip6_out->ip6_src = ip6->ip6_dst;
11810                 ip6_out->ip6_dst = ip6->ip6_src;
11811
11812                 iphlen_out = sizeof(struct ip6_hdr);
11813                 sh_out = (struct sctphdr *)((caddr_t)ip6_out + iphlen_out);
11814                 break;
11815 #endif                          /* INET6 */
11816         default:
11817                 /* Currently not supported */
11818                 sctp_m_freem(mout);
11819                 return;
11820         }
11821
11822         udp = (struct udphdr *)sh_out;
11823         if (port) {
11824                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11825                 udp->uh_dport = port;
11826                 /* set udp->uh_ulen later */
11827                 udp->uh_sum = 0;
11828                 iphlen_out += sizeof(struct udphdr);
11829                 sh_out = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
11830         }
11831         sh_out->src_port = sh->dest_port;
11832         sh_out->dest_port = sh->src_port;
11833         sh_out->v_tag = vtag;
11834         sh_out->checksum = 0;
11835
11836         ch = (struct sctp_chunkhdr *)((caddr_t)sh_out + sizeof(struct sctphdr));
11837         ch->chunk_type = SCTP_OPERATION_ERROR;
11838         ch->chunk_flags = 0;
11839
11840         if (scm) {
11841                 struct mbuf *m_tmp = scm;
11842                 int cause_len = 0;
11843
11844                 /* get length of the err_cause chain */
11845                 while (m_tmp != NULL) {
11846                         cause_len += SCTP_BUF_LEN(m_tmp);
11847                         m_tmp = SCTP_BUF_NEXT(m_tmp);
11848                 }
11849                 len = SCTP_BUF_LEN(mout) + cause_len;
11850                 if (cause_len % 4) {
11851                         /* need pad at end of chunk */
11852                         uint32_t cpthis = 0;
11853                         int padlen;
11854
11855                         padlen = 4 - (len % 4);
11856                         m_copyback(mout, len, padlen, (caddr_t)&cpthis);
11857                         len += padlen;
11858                 }
11859                 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr) + cause_len);
11860         } else {
11861                 len = SCTP_BUF_LEN(mout);
11862                 ch->chunk_length = htons(sizeof(struct sctp_chunkhdr));
11863         }
11864
11865         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11866                 /* no mbuf's */
11867                 sctp_m_freem(mout);
11868                 return;
11869         }
11870         if (iph_out != NULL) {
11871                 sctp_route_t ro;
11872                 struct sctp_tcb *stcb = NULL;
11873                 int ret;
11874
11875                 /* zap the stack pointer to the route */
11876                 bzero(&ro, sizeof ro);
11877                 if (port) {
11878                         udp->uh_ulen = htons(len - sizeof(struct ip));
11879                         udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11880                 }
11881                 /* set IPv4 length */
11882                 iph_out->ip_len = len;
11883                 /* out it goes */
11884 #ifdef  SCTP_PACKET_LOGGING
11885                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11886                         sctp_packet_log(mout, len);
11887 #endif
11888                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11889                 if (port) {
11890                         sh_out->checksum = sctp_calculate_cksum(mout, iphlen_out);
11891                         SCTP_STAT_INCR(sctps_sendswcrc);
11892                         SCTP_ENABLE_UDP_CSUM(o_pak);
11893                 } else {
11894                         mout->m_pkthdr.csum_flags = CSUM_SCTP;
11895                         mout->m_pkthdr.csum_data = 0;
11896                         SCTP_STAT_INCR(sctps_sendhwcrc);
11897                 }
11898                 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
11899
11900                 /* Free the route if we got one back */
11901                 if (ro.ro_rt)
11902                         RTFREE(ro.ro_rt);
11903         }
11904 #ifdef INET6
11905         if (ip6_out != NULL) {
11906                 struct route_in6 ro;
11907                 int ret;
11908                 struct sctp_tcb *stcb = NULL;
11909                 struct ifnet *ifp = NULL;
11910
11911                 /* zap the stack pointer to the route */
11912                 bzero(&ro, sizeof(ro));
11913                 if (port) {
11914                         udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
11915                 }
11916                 ip6_out->ip6_plen = len - sizeof(*ip6_out);
11917 #ifdef  SCTP_PACKET_LOGGING
11918                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
11919                         sctp_packet_log(mout, len);
11920 #endif
11921                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11922                 if (port) {
11923                         if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11924                             (stcb) &&
11925                             (stcb->asoc.loopback_scope))) {
11926                                 sh_out->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11927                                 SCTP_STAT_INCR(sctps_sendswcrc);
11928                         } else {
11929                                 SCTP_STAT_INCR(sctps_sendnocrc);
11930                         }
11931                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11932                                 udp->uh_sum = 0xffff;
11933                         }
11934                 } else {
11935                         if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
11936                             (stcb) &&
11937                             (stcb->asoc.loopback_scope))) {
11938                                 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11939                                 mout->m_pkthdr.csum_data = 0;
11940                                 SCTP_STAT_INCR(sctps_sendhwcrc);
11941                         } else {
11942                                 SCTP_STAT_INCR(sctps_sendnocrc);
11943                         }
11944                 }
11945                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
11946
11947                 /* Free the route if we got one back */
11948                 if (ro.ro_rt)
11949                         RTFREE(ro.ro_rt);
11950         }
11951 #endif
11952         SCTP_STAT_INCR(sctps_sendpackets);
11953         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11954         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11955 }
11956
11957 static struct mbuf *
11958 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
11959     struct uio *uio,
11960     struct sctp_sndrcvinfo *srcv,
11961     int max_send_len,
11962     int user_marks_eor,
11963     int *error,
11964     uint32_t * sndout,
11965     struct mbuf **new_tail)
11966 {
11967         struct mbuf *m;
11968
11969         m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
11970             (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
11971         if (m == NULL) {
11972                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11973                 *error = ENOMEM;
11974         } else {
11975                 *sndout = m_length(m, NULL);
11976                 *new_tail = m_last(m);
11977         }
11978         return (m);
11979 }
11980
11981 static int
11982 sctp_copy_one(struct sctp_stream_queue_pending *sp,
11983     struct uio *uio,
11984     int resv_upfront)
11985 {
11986         int left;
11987
11988         left = sp->length;
11989         sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
11990             resv_upfront, 0);
11991         if (sp->data == NULL) {
11992                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11993                 return (ENOMEM);
11994         }
11995         sp->tail_mbuf = m_last(sp->data);
11996         return (0);
11997 }
11998
11999
12000
12001 static struct sctp_stream_queue_pending *
12002 sctp_copy_it_in(struct sctp_tcb *stcb,
12003     struct sctp_association *asoc,
12004     struct sctp_sndrcvinfo *srcv,
12005     struct uio *uio,
12006     struct sctp_nets *net,
12007     int max_send_len,
12008     int user_marks_eor,
12009     int *error,
12010     int non_blocking)
12011 {
12012         /*-
12013          * This routine must be very careful in its work. Protocol
12014          * processing is up and running so care must be taken to spl...()
12015          * when you need to do something that may effect the stcb/asoc. The
12016          * sb is locked however. When data is copied the protocol processing
12017          * should be enabled since this is a slower operation...
12018          */
12019         struct sctp_stream_queue_pending *sp = NULL;
12020         int resv_in_first;
12021
12022         *error = 0;
12023         /* Now can we send this? */
12024         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12025             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12026             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12027             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12028                 /* got data while shutting down */
12029                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12030                 *error = ECONNRESET;
12031                 goto out_now;
12032         }
12033         sctp_alloc_a_strmoq(stcb, sp);
12034         if (sp == NULL) {
12035                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12036                 *error = ENOMEM;
12037                 goto out_now;
12038         }
12039         sp->act_flags = 0;
12040         sp->sender_all_done = 0;
12041         sp->sinfo_flags = srcv->sinfo_flags;
12042         sp->timetolive = srcv->sinfo_timetolive;
12043         sp->ppid = srcv->sinfo_ppid;
12044         sp->context = srcv->sinfo_context;
12045         sp->strseq = 0;
12046         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12047
12048         sp->stream = srcv->sinfo_stream;
12049         sp->length = min(uio->uio_resid, max_send_len);
12050         if ((sp->length == (uint32_t) uio->uio_resid) &&
12051             ((user_marks_eor == 0) ||
12052             (srcv->sinfo_flags & SCTP_EOF) ||
12053             (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12054                 sp->msg_is_complete = 1;
12055         } else {
12056                 sp->msg_is_complete = 0;
12057         }
12058         sp->sender_all_done = 0;
12059         sp->some_taken = 0;
12060         sp->put_last_out = 0;
12061         resv_in_first = sizeof(struct sctp_data_chunk);
12062         sp->data = sp->tail_mbuf = NULL;
12063         if (sp->length == 0) {
12064                 *error = 0;
12065                 goto skip_copy;
12066         }
12067         sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12068         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12069                 sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
12070                 sp->holds_key_ref = 1;
12071         }
12072         *error = sctp_copy_one(sp, uio, resv_in_first);
12073 skip_copy:
12074         if (*error) {
12075                 sctp_free_a_strmoq(stcb, sp);
12076                 sp = NULL;
12077         } else {
12078                 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12079                         sp->net = net;
12080                 } else {
12081                         sp->net = asoc->primary_destination;
12082                 }
12083                 atomic_add_int(&sp->net->ref_count, 1);
12084                 sctp_set_prsctp_policy(sp);
12085         }
12086 out_now:
12087         return (sp);
12088 }
12089
12090
12091 int
12092 sctp_sosend(struct socket *so,
12093     struct sockaddr *addr,
12094     struct uio *uio,
12095     struct mbuf *top,
12096     struct mbuf *control,
12097     int flags,
12098     struct thread *p
12099 )
12100 {
12101         struct sctp_inpcb *inp;
12102         int error, use_rcvinfo = 0;
12103         struct sctp_sndrcvinfo srcv;
12104         struct sockaddr *addr_to_use;
12105
12106 #if defined(INET) && defined(INET6)
12107         struct sockaddr_in sin;
12108
12109 #endif
12110
12111         inp = (struct sctp_inpcb *)so->so_pcb;
12112         if (control) {
12113                 /* process cmsg snd/rcv info (maybe a assoc-id) */
12114                 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
12115                     sizeof(srcv))) {
12116                         /* got one */
12117                         use_rcvinfo = 1;
12118                 }
12119         }
12120         addr_to_use = addr;
12121 #if defined(INET) && defined(INET6)
12122         if ((addr) && (addr->sa_family == AF_INET6)) {
12123                 struct sockaddr_in6 *sin6;
12124
12125                 sin6 = (struct sockaddr_in6 *)addr;
12126                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12127                         in6_sin6_2_sin(&sin, sin6);
12128                         addr_to_use = (struct sockaddr *)&sin;
12129                 }
12130         }
12131 #endif
12132         error = sctp_lower_sosend(so, addr_to_use, uio, top,
12133             control,
12134             flags,
12135             use_rcvinfo, &srcv
12136             ,p
12137             );
12138         return (error);
12139 }
12140
12141
12142 int
12143 sctp_lower_sosend(struct socket *so,
12144     struct sockaddr *addr,
12145     struct uio *uio,
12146     struct mbuf *i_pak,
12147     struct mbuf *control,
12148     int flags,
12149     int use_rcvinfo,
12150     struct sctp_sndrcvinfo *srcv
12151     ,
12152     struct thread *p
12153 )
12154 {
12155         unsigned int sndlen = 0, max_len;
12156         int error, len;
12157         struct mbuf *top = NULL;
12158         int queue_only = 0, queue_only_for_init = 0;
12159         int free_cnt_applied = 0;
12160         int un_sent = 0;
12161         int now_filled = 0;
12162         unsigned int inqueue_bytes = 0;
12163         struct sctp_block_entry be;
12164         struct sctp_inpcb *inp;
12165         struct sctp_tcb *stcb = NULL;
12166         struct timeval now;
12167         struct sctp_nets *net;
12168         struct sctp_association *asoc;
12169         struct sctp_inpcb *t_inp;
12170         int user_marks_eor;
12171         int create_lock_applied = 0;
12172         int nagle_applies = 0;
12173         int some_on_control = 0;
12174         int got_all_of_the_send = 0;
12175         int hold_tcblock = 0;
12176         int non_blocking = 0;
12177         int temp_flags = 0;
12178         uint32_t local_add_more, local_soresv = 0;
12179
12180         error = 0;
12181         net = NULL;
12182         stcb = NULL;
12183         asoc = NULL;
12184
12185         t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12186         if (inp == NULL) {
12187                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12188                 error = EINVAL;
12189                 if (i_pak) {
12190                         SCTP_RELEASE_PKT(i_pak);
12191                 }
12192                 return (error);
12193         }
12194         if ((uio == NULL) && (i_pak == NULL)) {
12195                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12196                 return (EINVAL);
12197         }
12198         user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12199         atomic_add_int(&inp->total_sends, 1);
12200         if (uio) {
12201                 if (uio->uio_resid < 0) {
12202                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12203                         return (EINVAL);
12204                 }
12205                 sndlen = uio->uio_resid;
12206         } else {
12207                 top = SCTP_HEADER_TO_CHAIN(i_pak);
12208                 sndlen = SCTP_HEADER_LEN(i_pak);
12209         }
12210         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
12211             addr,
12212             sndlen);
12213         /**
12214          * Pre-screen address, if one is given the sin-len
12215          * must be set correctly!
12216          */
12217         if (addr) {
12218                 switch (addr->sa_family) {
12219 #if defined(INET)
12220                 case AF_INET:
12221                         if (addr->sa_len != sizeof(struct sockaddr_in)) {
12222                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12223                                 error = EINVAL;
12224                                 goto out_unlocked;
12225                         }
12226                         break;
12227 #endif
12228 #if defined(INET6)
12229                 case AF_INET6:
12230                         if (addr->sa_len != sizeof(struct sockaddr_in6)) {
12231                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12232                                 error = EINVAL;
12233                                 goto out_unlocked;
12234                         }
12235                         break;
12236 #endif
12237                 default:
12238                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAFNOSUPPORT);
12239                         error = EAFNOSUPPORT;
12240                         goto out_unlocked;
12241                 }
12242         }
12243         hold_tcblock = 0;
12244
12245         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12246             (inp->sctp_socket->so_qlimit)) {
12247                 /* The listener can NOT send */
12248                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12249                 error = ENOTCONN;
12250                 goto out_unlocked;
12251         }
12252         if ((use_rcvinfo) && srcv) {
12253                 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
12254                     PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
12255                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12256                         error = EINVAL;
12257                         goto out_unlocked;
12258                 }
12259                 if (srcv->sinfo_flags)
12260                         SCTP_STAT_INCR(sctps_sends_with_flags);
12261
12262                 if (srcv->sinfo_flags & SCTP_SENDALL) {
12263                         /* its a sendall */
12264                         error = sctp_sendall(inp, uio, top, srcv);
12265                         top = NULL;
12266                         goto out_unlocked;
12267                 }
12268         }
12269         /* now we must find the assoc */
12270         if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12271             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12272                 SCTP_INP_RLOCK(inp);
12273                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12274                 if (stcb == NULL) {
12275                         SCTP_INP_RUNLOCK(inp);
12276                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12277                         error = ENOTCONN;
12278                         goto out_unlocked;
12279                 }
12280                 SCTP_TCB_LOCK(stcb);
12281                 hold_tcblock = 1;
12282                 SCTP_INP_RUNLOCK(inp);
12283                 if (addr) {
12284                         /* Must locate the net structure if addr given */
12285                         net = sctp_findnet(stcb, addr);
12286                         if (net) {
12287                                 /* validate port was 0 or correct */
12288                                 struct sockaddr_in *sin;
12289
12290                                 sin = (struct sockaddr_in *)addr;
12291                                 if ((sin->sin_port != 0) &&
12292                                     (sin->sin_port != stcb->rport)) {
12293                                         net = NULL;
12294                                 }
12295                         }
12296                         temp_flags |= SCTP_ADDR_OVER;
12297                 } else
12298                         net = stcb->asoc.primary_destination;
12299                 if (addr && (net == NULL)) {
12300                         /* Could not find address, was it legal */
12301                         if (addr->sa_family == AF_INET) {
12302                                 struct sockaddr_in *sin;
12303
12304                                 sin = (struct sockaddr_in *)addr;
12305                                 if (sin->sin_addr.s_addr == 0) {
12306                                         if ((sin->sin_port == 0) ||
12307                                             (sin->sin_port == stcb->rport)) {
12308                                                 net = stcb->asoc.primary_destination;
12309                                         }
12310                                 }
12311                         } else {
12312                                 struct sockaddr_in6 *sin6;
12313
12314                                 sin6 = (struct sockaddr_in6 *)addr;
12315                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
12316                                         if ((sin6->sin6_port == 0) ||
12317                                             (sin6->sin6_port == stcb->rport)) {
12318                                                 net = stcb->asoc.primary_destination;
12319                                         }
12320                                 }
12321                         }
12322                 }
12323                 if (net == NULL) {
12324                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12325                         error = EINVAL;
12326                         goto out_unlocked;
12327                 }
12328         } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
12329                 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
12330                 if (stcb) {
12331                         if (addr)
12332                                 /*
12333                                  * Must locate the net structure if addr
12334                                  * given
12335                                  */
12336                                 net = sctp_findnet(stcb, addr);
12337                         else
12338                                 net = stcb->asoc.primary_destination;
12339                         if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
12340                             ((net == NULL) || (addr == NULL))) {
12341                                 struct sockaddr_in *sin;
12342
12343                                 if (addr == NULL) {
12344                                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12345                                         error = EINVAL;
12346                                         goto out_unlocked;
12347                                 }
12348                                 sin = (struct sockaddr_in *)addr;
12349                                 /* Validate port is 0 or correct */
12350                                 if ((sin->sin_port != 0) &&
12351                                     (sin->sin_port != stcb->rport)) {
12352                                         net = NULL;
12353                                 }
12354                         }
12355                 }
12356                 hold_tcblock = 0;
12357         } else if (addr) {
12358                 /*-
12359                  * Since we did not use findep we must
12360                  * increment it, and if we don't find a tcb
12361                  * decrement it.
12362                  */
12363                 SCTP_INP_WLOCK(inp);
12364                 SCTP_INP_INCR_REF(inp);
12365                 SCTP_INP_WUNLOCK(inp);
12366                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12367                 if (stcb == NULL) {
12368                         SCTP_INP_WLOCK(inp);
12369                         SCTP_INP_DECR_REF(inp);
12370                         SCTP_INP_WUNLOCK(inp);
12371                 } else {
12372                         hold_tcblock = 1;
12373                 }
12374         }
12375         if ((stcb == NULL) && (addr)) {
12376                 /* Possible implicit send? */
12377                 SCTP_ASOC_CREATE_LOCK(inp);
12378                 create_lock_applied = 1;
12379                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12380                     (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12381                         /* Should I really unlock ? */
12382                         SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12383                         error = EINVAL;
12384                         goto out_unlocked;
12385
12386                 }
12387                 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12388                     (addr->sa_family == AF_INET6)) {
12389                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12390                         error = EINVAL;
12391                         goto out_unlocked;
12392                 }
12393                 SCTP_INP_WLOCK(inp);
12394                 SCTP_INP_INCR_REF(inp);
12395                 SCTP_INP_WUNLOCK(inp);
12396                 /* With the lock applied look again */
12397                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12398                 if (stcb == NULL) {
12399                         SCTP_INP_WLOCK(inp);
12400                         SCTP_INP_DECR_REF(inp);
12401                         SCTP_INP_WUNLOCK(inp);
12402                 } else {
12403                         hold_tcblock = 1;
12404                 }
12405                 if (t_inp != inp) {
12406                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12407                         error = ENOTCONN;
12408                         goto out_unlocked;
12409                 }
12410         }
12411         if (stcb == NULL) {
12412                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
12413                     (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12414                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
12415                         error = ENOTCONN;
12416                         goto out_unlocked;
12417                 }
12418                 if (addr == NULL) {
12419                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12420                         error = ENOENT;
12421                         goto out_unlocked;
12422                 } else {
12423                         /*
12424                          * UDP style, we must go ahead and start the INIT
12425                          * process
12426                          */
12427                         uint32_t vrf_id;
12428
12429                         if ((use_rcvinfo) && (srcv) &&
12430                             ((srcv->sinfo_flags & SCTP_ABORT) ||
12431                             ((srcv->sinfo_flags & SCTP_EOF) &&
12432                             (sndlen == 0)))) {
12433                                 /*-
12434                                  * User asks to abort a non-existant assoc,
12435                                  * or EOF a non-existant assoc with no data
12436                                  */
12437                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
12438                                 error = ENOENT;
12439                                 goto out_unlocked;
12440                         }
12441                         /* get an asoc/stcb struct */
12442                         vrf_id = inp->def_vrf_id;
12443 #ifdef INVARIANTS
12444                         if (create_lock_applied == 0) {
12445                                 panic("Error, should hold create lock and I don't?");
12446                         }
12447 #endif
12448                         stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
12449                             p
12450                             );
12451                         if (stcb == NULL) {
12452                                 /* Error is setup for us in the call */
12453                                 goto out_unlocked;
12454                         }
12455                         if (create_lock_applied) {
12456                                 SCTP_ASOC_CREATE_UNLOCK(inp);
12457                                 create_lock_applied = 0;
12458                         } else {
12459                                 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
12460                         }
12461                         /*
12462                          * Turn on queue only flag to prevent data from
12463                          * being sent
12464                          */
12465                         queue_only = 1;
12466                         asoc = &stcb->asoc;
12467                         SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
12468                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
12469
12470                         /* initialize authentication params for the assoc */
12471                         sctp_initialize_auth_params(inp, stcb);
12472
12473                         if (control) {
12474                                 /*
12475                                  * see if a init structure exists in cmsg
12476                                  * headers
12477                                  */
12478                                 struct sctp_initmsg initm;
12479                                 int i;
12480
12481                                 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
12482                                     sizeof(initm))) {
12483                                         /*
12484                                          * we have an INIT override of the
12485                                          * default
12486                                          */
12487                                         if (initm.sinit_max_attempts)
12488                                                 asoc->max_init_times = initm.sinit_max_attempts;
12489                                         if (initm.sinit_num_ostreams)
12490                                                 asoc->pre_open_streams = initm.sinit_num_ostreams;
12491                                         if (initm.sinit_max_instreams)
12492                                                 asoc->max_inbound_streams = initm.sinit_max_instreams;
12493                                         if (initm.sinit_max_init_timeo)
12494                                                 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
12495                                         if (asoc->streamoutcnt < asoc->pre_open_streams) {
12496                                                 struct sctp_stream_out *tmp_str;
12497                                                 int had_lock = 0;
12498
12499                                                 /* Default is NOT correct */
12500                                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
12501                                                     asoc->streamoutcnt, asoc->pre_open_streams);
12502                                                 /*
12503                                                  * What happens if this
12504                                                  * fails? we panic ...
12505                                                  */
12506
12507                                                 if (hold_tcblock) {
12508                                                         had_lock = 1;
12509                                                         SCTP_TCB_UNLOCK(stcb);
12510                                                 }
12511                                                 SCTP_MALLOC(tmp_str,
12512                                                     struct sctp_stream_out *,
12513                                                     (asoc->pre_open_streams *
12514                                                     sizeof(struct sctp_stream_out)),
12515                                                     SCTP_M_STRMO);
12516                                                 if (had_lock) {
12517                                                         SCTP_TCB_LOCK(stcb);
12518                                                 }
12519                                                 if (tmp_str != NULL) {
12520                                                         SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
12521                                                         asoc->strmout = tmp_str;
12522                                                         asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams;
12523                                                 } else {
12524                                                         asoc->pre_open_streams = asoc->streamoutcnt;
12525                                                 }
12526                                                 for (i = 0; i < asoc->streamoutcnt; i++) {
12527                                                         /*-
12528                                                          * inbound side must be set
12529                                                          * to 0xffff, also NOTE when
12530                                                          * we get the INIT-ACK back
12531                                                          * (for INIT sender) we MUST
12532                                                          * reduce the count
12533                                                          * (streamoutcnt) but first
12534                                                          * check if we sent to any
12535                                                          * of the upper streams that
12536                                                          * were dropped (if some
12537                                                          * were). Those that were
12538                                                          * dropped must be notified
12539                                                          * to the upper layer as
12540                                                          * failed to send.
12541                                                          */
12542                                                         asoc->strmout[i].next_sequence_sent = 0x0;
12543                                                         TAILQ_INIT(&asoc->strmout[i].outqueue);
12544                                                         asoc->strmout[i].stream_no = i;
12545                                                         asoc->strmout[i].last_msg_incomplete = 0;
12546                                                         asoc->strmout[i].next_spoke.tqe_next = 0;
12547                                                         asoc->strmout[i].next_spoke.tqe_prev = 0;
12548                                                 }
12549                                         }
12550                                 }
12551                         }
12552                         hold_tcblock = 1;
12553                         /* out with the INIT */
12554                         queue_only_for_init = 1;
12555                         /*-
12556                          * we may want to dig in after this call and adjust the MTU
12557                          * value. It defaulted to 1500 (constant) but the ro
12558                          * structure may now have an update and thus we may need to
12559                          * change it BEFORE we append the message.
12560                          */
12561                         net = stcb->asoc.primary_destination;
12562                         asoc = &stcb->asoc;
12563                 }
12564         }
12565         if ((SCTP_SO_IS_NBIO(so)
12566             || (flags & MSG_NBIO)
12567             )) {
12568                 non_blocking = 1;
12569         }
12570         asoc = &stcb->asoc;
12571         atomic_add_int(&stcb->total_sends, 1);
12572
12573         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12574                 if (sndlen > asoc->smallest_mtu) {
12575                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12576                         error = EMSGSIZE;
12577                         goto out_unlocked;
12578                 }
12579         }
12580         /* would we block? */
12581         if (non_blocking) {
12582                 if (hold_tcblock == 0) {
12583                         SCTP_TCB_LOCK(stcb);
12584                         hold_tcblock = 1;
12585                 }
12586                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12587                 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
12588                     (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12589                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
12590                         if (sndlen > SCTP_SB_LIMIT_SND(so))
12591                                 error = EMSGSIZE;
12592                         else
12593                                 error = EWOULDBLOCK;
12594                         goto out_unlocked;
12595                 }
12596                 stcb->asoc.sb_send_resv += sndlen;
12597                 SCTP_TCB_UNLOCK(stcb);
12598                 hold_tcblock = 0;
12599         } else {
12600                 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
12601         }
12602         local_soresv = sndlen;
12603         /* Keep the stcb from being freed under our feet */
12604         if (free_cnt_applied) {
12605 #ifdef INVARIANTS
12606                 panic("refcnt already incremented");
12607 #else
12608                 printf("refcnt:1 already incremented?\n");
12609 #endif
12610         } else {
12611                 atomic_add_int(&stcb->asoc.refcnt, 1);
12612                 free_cnt_applied = 1;
12613         }
12614         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12615                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12616                 error = ECONNRESET;
12617                 goto out_unlocked;
12618         }
12619         if (create_lock_applied) {
12620                 SCTP_ASOC_CREATE_UNLOCK(inp);
12621                 create_lock_applied = 0;
12622         }
12623         if (asoc->stream_reset_outstanding) {
12624                 /*
12625                  * Can't queue any data while stream reset is underway.
12626                  */
12627                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
12628                 error = EAGAIN;
12629                 goto out_unlocked;
12630         }
12631         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12632             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12633                 queue_only = 1;
12634         }
12635         if ((use_rcvinfo == 0) || (srcv == NULL)) {
12636                 /* Grab the default stuff from the asoc */
12637                 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
12638         }
12639         /* we are now done with all control */
12640         if (control) {
12641                 sctp_m_freem(control);
12642                 control = NULL;
12643         }
12644         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
12645             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
12646             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
12647             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
12648                 if ((use_rcvinfo) &&
12649                     (srcv->sinfo_flags & SCTP_ABORT)) {
12650                         ;
12651                 } else {
12652                         SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
12653                         error = ECONNRESET;
12654                         goto out_unlocked;
12655                 }
12656         }
12657         /* Ok, we will attempt a msgsnd :> */
12658         if (p) {
12659                 p->td_ru.ru_msgsnd++;
12660         }
12661         if (stcb) {
12662                 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
12663                         net = stcb->asoc.primary_destination;
12664                 }
12665         }
12666         if (net == NULL) {
12667                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12668                 error = EINVAL;
12669                 goto out_unlocked;
12670         }
12671         if ((net->flight_size > net->cwnd) &&
12672             (asoc->sctp_cmt_on_off == 0)) {
12673                 /*-
12674                  * CMT: Added check for CMT above. net above is the primary
12675                  * dest. If CMT is ON, sender should always attempt to send
12676                  * with the output routine sctp_fill_outqueue() that loops
12677                  * through all destination addresses. Therefore, if CMT is
12678                  * ON, queue_only is NOT set to 1 here, so that
12679                  * sctp_chunk_output() can be called below.
12680                  */
12681                 queue_only = 1;
12682         } else if (asoc->ifp_had_enobuf) {
12683                 SCTP_STAT_INCR(sctps_ifnomemqueued);
12684                 if (net->flight_size > (net->mtu * 2))
12685                         queue_only = 1;
12686                 asoc->ifp_had_enobuf = 0;
12687         } else {
12688                 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
12689                     (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
12690         }
12691         /* Are we aborting? */
12692         if (srcv->sinfo_flags & SCTP_ABORT) {
12693                 struct mbuf *mm;
12694                 int tot_demand, tot_out = 0, max_out;
12695
12696                 SCTP_STAT_INCR(sctps_sends_with_abort);
12697                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
12698                     (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
12699                         /* It has to be up before we abort */
12700                         /* how big is the user initiated abort? */
12701                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12702                         error = EINVAL;
12703                         goto out;
12704                 }
12705                 if (hold_tcblock) {
12706                         SCTP_TCB_UNLOCK(stcb);
12707                         hold_tcblock = 0;
12708                 }
12709                 if (top) {
12710                         struct mbuf *cntm = NULL;
12711
12712                         mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
12713                         if (sndlen != 0) {
12714                                 cntm = top;
12715                                 while (cntm) {
12716                                         tot_out += SCTP_BUF_LEN(cntm);
12717                                         cntm = SCTP_BUF_NEXT(cntm);
12718                                 }
12719                         }
12720                         tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12721                 } else {
12722                         /* Must fit in a MTU */
12723                         tot_out = sndlen;
12724                         tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12725                         if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12726                                 /* To big */
12727                                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12728                                 error = EMSGSIZE;
12729                                 goto out;
12730                         }
12731                         mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
12732                 }
12733                 if (mm == NULL) {
12734                         SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12735                         error = ENOMEM;
12736                         goto out;
12737                 }
12738                 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12739                 max_out -= sizeof(struct sctp_abort_msg);
12740                 if (tot_out > max_out) {
12741                         tot_out = max_out;
12742                 }
12743                 if (mm) {
12744                         struct sctp_paramhdr *ph;
12745
12746                         /* now move forward the data pointer */
12747                         ph = mtod(mm, struct sctp_paramhdr *);
12748                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12749                         ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
12750                         ph++;
12751                         SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
12752                         if (top == NULL) {
12753                                 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12754                                 if (error) {
12755                                         /*-
12756                                          * Here if we can't get his data we
12757                                          * still abort we just don't get to
12758                                          * send the users note :-0
12759                                          */
12760                                         sctp_m_freem(mm);
12761                                         mm = NULL;
12762                                 }
12763                         } else {
12764                                 if (sndlen != 0) {
12765                                         SCTP_BUF_NEXT(mm) = top;
12766                                 }
12767                         }
12768                 }
12769                 if (hold_tcblock == 0) {
12770                         SCTP_TCB_LOCK(stcb);
12771                         hold_tcblock = 1;
12772                 }
12773                 atomic_add_int(&stcb->asoc.refcnt, -1);
12774                 free_cnt_applied = 0;
12775                 /* release this lock, otherwise we hang on ourselves */
12776                 sctp_abort_an_association(stcb->sctp_ep, stcb,
12777                     SCTP_RESPONSE_TO_USER_REQ,
12778                     mm, SCTP_SO_LOCKED);
12779                 /* now relock the stcb so everything is sane */
12780                 hold_tcblock = 0;
12781                 stcb = NULL;
12782                 /*
12783                  * In this case top is already chained to mm avoid double
12784                  * free, since we free it below if top != NULL and driver
12785                  * would free it after sending the packet out
12786                  */
12787                 if (sndlen != 0) {
12788                         top = NULL;
12789                 }
12790                 goto out_unlocked;
12791         }
12792         /* Calculate the maximum we can send */
12793         inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12794         if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12795                 if (non_blocking) {
12796                         /* we already checked for non-blocking above. */
12797                         max_len = sndlen;
12798                 } else {
12799                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12800                 }
12801         } else {
12802                 max_len = 0;
12803         }
12804         if (hold_tcblock) {
12805                 SCTP_TCB_UNLOCK(stcb);
12806                 hold_tcblock = 0;
12807         }
12808         /* Is the stream no. valid? */
12809         if (srcv->sinfo_stream >= asoc->streamoutcnt) {
12810                 /* Invalid stream number */
12811                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12812                 error = EINVAL;
12813                 goto out_unlocked;
12814         }
12815         if (asoc->strmout == NULL) {
12816                 /* huh? software error */
12817                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12818                 error = EFAULT;
12819                 goto out_unlocked;
12820         }
12821         /* Unless E_EOR mode is on, we must make a send FIT in one call. */
12822         if ((user_marks_eor == 0) &&
12823             (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12824                 /* It will NEVER fit */
12825                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
12826                 error = EMSGSIZE;
12827                 goto out_unlocked;
12828         }
12829         if ((uio == NULL) && user_marks_eor) {
12830                 /*-
12831                  * We do not support eeor mode for
12832                  * sending with mbuf chains (like sendfile).
12833                  */
12834                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12835                 error = EINVAL;
12836                 goto out_unlocked;
12837         }
12838         if (user_marks_eor) {
12839                 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12840         } else {
12841                 /*-
12842                  * For non-eeor the whole message must fit in
12843                  * the socket send buffer.
12844                  */
12845                 local_add_more = sndlen;
12846         }
12847         len = 0;
12848         if (non_blocking) {
12849                 goto skip_preblock;
12850         }
12851         if (((max_len <= local_add_more) &&
12852             (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12853             (max_len == 0) ||
12854             ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12855                 /* No room right now ! */
12856                 SOCKBUF_LOCK(&so->so_snd);
12857                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12858                 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12859                     ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12860                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
12861                             (unsigned int)SCTP_SB_LIMIT_SND(so),
12862                             inqueue_bytes,
12863                             local_add_more,
12864                             stcb->asoc.stream_queue_cnt,
12865                             stcb->asoc.chunks_on_out_queue,
12866                             SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12867                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12868                                 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
12869                         }
12870                         be.error = 0;
12871                         stcb->block_entry = &be;
12872                         error = sbwait(&so->so_snd);
12873                         stcb->block_entry = NULL;
12874                         if (error || so->so_error || be.error) {
12875                                 if (error == 0) {
12876                                         if (so->so_error)
12877                                                 error = so->so_error;
12878                                         if (be.error) {
12879                                                 error = be.error;
12880                                         }
12881                                 }
12882                                 SOCKBUF_UNLOCK(&so->so_snd);
12883                                 goto out_unlocked;
12884                         }
12885                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12886                                 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
12887                                     so, asoc, stcb->asoc.total_output_queue_size);
12888                         }
12889                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12890                                 goto out_unlocked;
12891                         }
12892                         inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12893                 }
12894                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
12895                 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12896                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12897                 } else {
12898                         max_len = 0;
12899                 }
12900                 SOCKBUF_UNLOCK(&so->so_snd);
12901         }
12902 skip_preblock:
12903         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
12904                 goto out_unlocked;
12905         }
12906         /*
12907          * sndlen covers for mbuf case uio_resid covers for the non-mbuf
12908          * case NOTE: uio will be null when top/mbuf is passed
12909          */
12910         if (sndlen == 0) {
12911                 if (srcv->sinfo_flags & SCTP_EOF) {
12912                         got_all_of_the_send = 1;
12913                         goto dataless_eof;
12914                 } else {
12915                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12916                         error = EINVAL;
12917                         goto out;
12918                 }
12919         }
12920         if (top == NULL) {
12921                 struct sctp_stream_queue_pending *sp;
12922                 struct sctp_stream_out *strm;
12923                 uint32_t sndout, initial_out;
12924
12925                 initial_out = uio->uio_resid;
12926
12927                 SCTP_TCB_SEND_LOCK(stcb);
12928                 if ((asoc->stream_locked) &&
12929                     (asoc->stream_locked_on != srcv->sinfo_stream)) {
12930                         SCTP_TCB_SEND_UNLOCK(stcb);
12931                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12932                         error = EINVAL;
12933                         goto out;
12934                 }
12935                 SCTP_TCB_SEND_UNLOCK(stcb);
12936
12937                 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
12938                 if (strm->last_msg_incomplete == 0) {
12939         do_a_copy_in:
12940                         sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
12941                         if ((sp == NULL) || (error)) {
12942                                 goto out;
12943                         }
12944                         SCTP_TCB_SEND_LOCK(stcb);
12945                         if (sp->msg_is_complete) {
12946                                 strm->last_msg_incomplete = 0;
12947                                 asoc->stream_locked = 0;
12948                         } else {
12949                                 /*
12950                                  * Just got locked to this guy in case of an
12951                                  * interrupt.
12952                                  */
12953                                 strm->last_msg_incomplete = 1;
12954                                 asoc->stream_locked = 1;
12955                                 asoc->stream_locked_on = srcv->sinfo_stream;
12956                                 sp->sender_all_done = 0;
12957                         }
12958                         sctp_snd_sb_alloc(stcb, sp->length);
12959                         atomic_add_int(&asoc->stream_queue_cnt, 1);
12960                         if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
12961                                 sp->strseq = strm->next_sequence_sent;
12962                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
12963                                         sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
12964                                             (uintptr_t) stcb, sp->length,
12965                                             (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
12966                                 }
12967                                 strm->next_sequence_sent++;
12968                         } else {
12969                                 SCTP_STAT_INCR(sctps_sends_with_unord);
12970                         }
12971                         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
12972                         if ((strm->next_spoke.tqe_next == NULL) &&
12973                             (strm->next_spoke.tqe_prev == NULL)) {
12974                                 /* Not on wheel, insert */
12975                                 sctp_insert_on_wheel(stcb, asoc, strm, 1);
12976                         }
12977                         SCTP_TCB_SEND_UNLOCK(stcb);
12978                 } else {
12979                         SCTP_TCB_SEND_LOCK(stcb);
12980                         sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
12981                         SCTP_TCB_SEND_UNLOCK(stcb);
12982                         if (sp == NULL) {
12983                                 /* ???? Huh ??? last msg is gone */
12984 #ifdef INVARIANTS
12985                                 panic("Warning: Last msg marked incomplete, yet nothing left?");
12986 #else
12987                                 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
12988                                 strm->last_msg_incomplete = 0;
12989 #endif
12990                                 goto do_a_copy_in;
12991
12992                         }
12993                 }
12994                 while (uio->uio_resid > 0) {
12995                         /* How much room do we have? */
12996                         struct mbuf *new_tail, *mm;
12997
12998                         if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
12999                                 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
13000                         else
13001                                 max_len = 0;
13002
13003                         if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13004                             (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13005                             (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
13006                                 sndout = 0;
13007                                 new_tail = NULL;
13008                                 if (hold_tcblock) {
13009                                         SCTP_TCB_UNLOCK(stcb);
13010                                         hold_tcblock = 0;
13011                                 }
13012                                 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
13013                                 if ((mm == NULL) || error) {
13014                                         if (mm) {
13015                                                 sctp_m_freem(mm);
13016                                         }
13017                                         goto out;
13018                                 }
13019                                 /* Update the mbuf and count */
13020                                 SCTP_TCB_SEND_LOCK(stcb);
13021                                 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13022                                         /*
13023                                          * we need to get out. Peer probably
13024                                          * aborted.
13025                                          */
13026                                         sctp_m_freem(mm);
13027                                         if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
13028                                                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13029                                                 error = ECONNRESET;
13030                                         }
13031                                         SCTP_TCB_SEND_UNLOCK(stcb);
13032                                         goto out;
13033                                 }
13034                                 if (sp->tail_mbuf) {
13035                                         /* tack it to the end */
13036                                         SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13037                                         sp->tail_mbuf = new_tail;
13038                                 } else {
13039                                         /* A stolen mbuf */
13040                                         sp->data = mm;
13041                                         sp->tail_mbuf = new_tail;
13042                                 }
13043                                 sctp_snd_sb_alloc(stcb, sndout);
13044                                 atomic_add_int(&sp->length, sndout);
13045                                 len += sndout;
13046
13047                                 /* Did we reach EOR? */
13048                                 if ((uio->uio_resid == 0) &&
13049                                     ((user_marks_eor == 0) ||
13050                                     (srcv->sinfo_flags & SCTP_EOF) ||
13051                                     (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13052                                         sp->msg_is_complete = 1;
13053                                 } else {
13054                                         sp->msg_is_complete = 0;
13055                                 }
13056                                 SCTP_TCB_SEND_UNLOCK(stcb);
13057                         }
13058                         if (uio->uio_resid == 0) {
13059                                 /* got it all? */
13060                                 continue;
13061                         }
13062                         /* PR-SCTP? */
13063                         if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
13064                                 /*
13065                                  * This is ugly but we must assure locking
13066                                  * order
13067                                  */
13068                                 if (hold_tcblock == 0) {
13069                                         SCTP_TCB_LOCK(stcb);
13070                                         hold_tcblock = 1;
13071                                 }
13072                                 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
13073                                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
13074                                 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
13075                                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13076                                 else
13077                                         max_len = 0;
13078                                 if (max_len > 0) {
13079                                         continue;
13080                                 }
13081                                 SCTP_TCB_UNLOCK(stcb);
13082                                 hold_tcblock = 0;
13083                         }
13084                         /* wait for space now */
13085                         if (non_blocking) {
13086                                 /* Non-blocking io in place out */
13087                                 goto skip_out_eof;
13088                         }
13089                         if ((net->flight_size > net->cwnd) &&
13090                             (asoc->sctp_cmt_on_off == 0)) {
13091                                 queue_only = 1;
13092                         } else if (asoc->ifp_had_enobuf) {
13093                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
13094                                 if (net->flight_size > (net->mtu * 2)) {
13095                                         queue_only = 1;
13096                                 } else {
13097                                         queue_only = 0;
13098                                 }
13099                                 asoc->ifp_had_enobuf = 0;
13100                                 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13101                                     (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13102                         } else {
13103                                 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13104                                     (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13105                                 if (net->flight_size > net->cwnd) {
13106                                         queue_only = 1;
13107                                         SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13108                                 } else {
13109                                         queue_only = 0;
13110                                 }
13111                         }
13112                         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13113                             (stcb->asoc.total_flight > 0) &&
13114                             (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13115                             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13116
13117                                 /*-
13118                                  * Ok, Nagle is set on and we have data outstanding.
13119                                  * Don't send anything and let SACKs drive out the
13120                                  * data unless wen have a "full" segment to send.
13121                                  */
13122                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13123                                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13124                                 }
13125                                 SCTP_STAT_INCR(sctps_naglequeued);
13126                                 nagle_applies = 1;
13127                         } else {
13128                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13129                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13130                                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13131                                 }
13132                                 SCTP_STAT_INCR(sctps_naglesent);
13133                                 nagle_applies = 0;
13134                         }
13135                         /* What about the INIT, send it maybe */
13136                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13137
13138                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13139                                     nagle_applies, un_sent);
13140                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
13141                                     stcb->asoc.total_flight,
13142                                     stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
13143                         }
13144                         if (queue_only_for_init) {
13145                                 if (hold_tcblock == 0) {
13146                                         SCTP_TCB_LOCK(stcb);
13147                                         hold_tcblock = 1;
13148                                 }
13149                                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13150                                         /* a collision took us forward? */
13151                                         queue_only_for_init = 0;
13152                                         queue_only = 0;
13153                                 } else {
13154                                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13155                                         SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
13156                                         queue_only_for_init = 0;
13157                                         queue_only = 1;
13158                                 }
13159                         }
13160                         if ((queue_only == 0) && (nagle_applies == 0)) {
13161                                 /*-
13162                                  * need to start chunk output
13163                                  * before blocking.. note that if
13164                                  * a lock is already applied, then
13165                                  * the input via the net is happening
13166                                  * and I don't need to start output :-D
13167                                  */
13168                                 if (hold_tcblock == 0) {
13169                                         if (SCTP_TCB_TRYLOCK(stcb)) {
13170                                                 hold_tcblock = 1;
13171                                                 sctp_chunk_output(inp,
13172                                                     stcb,
13173                                                     SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13174                                         }
13175                                 } else {
13176                                         sctp_chunk_output(inp,
13177                                             stcb,
13178                                             SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13179                                 }
13180                                 if (hold_tcblock == 1) {
13181                                         SCTP_TCB_UNLOCK(stcb);
13182                                         hold_tcblock = 0;
13183                                 }
13184                         }
13185                         SOCKBUF_LOCK(&so->so_snd);
13186                         /*-
13187                          * This is a bit strange, but I think it will
13188                          * work. The total_output_queue_size is locked and
13189                          * protected by the TCB_LOCK, which we just released.
13190                          * There is a race that can occur between releasing it
13191                          * above, and me getting the socket lock, where sacks
13192                          * come in but we have not put the SB_WAIT on the
13193                          * so_snd buffer to get the wakeup. After the LOCK
13194                          * is applied the sack_processing will also need to
13195                          * LOCK the so->so_snd to do the actual sowwakeup(). So
13196                          * once we have the socket buffer lock if we recheck the
13197                          * size we KNOW we will get to sleep safely with the
13198                          * wakeup flag in place.
13199                          */
13200                         if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
13201                             min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13202                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13203                                         sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13204                                             so, asoc, uio->uio_resid);
13205                                 }
13206                                 be.error = 0;
13207                                 stcb->block_entry = &be;
13208                                 error = sbwait(&so->so_snd);
13209                                 stcb->block_entry = NULL;
13210
13211                                 if (error || so->so_error || be.error) {
13212                                         if (error == 0) {
13213                                                 if (so->so_error)
13214                                                         error = so->so_error;
13215                                                 if (be.error) {
13216                                                         error = be.error;
13217                                                 }
13218                                         }
13219                                         SOCKBUF_UNLOCK(&so->so_snd);
13220                                         goto out_unlocked;
13221                                 }
13222                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13223                                         sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13224                                             so, asoc, stcb->asoc.total_output_queue_size);
13225                                 }
13226                         }
13227                         SOCKBUF_UNLOCK(&so->so_snd);
13228                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
13229                                 goto out_unlocked;
13230                         }
13231                 }
13232                 SCTP_TCB_SEND_LOCK(stcb);
13233                 if (sp) {
13234                         if (sp->msg_is_complete == 0) {
13235                                 strm->last_msg_incomplete = 1;
13236                                 asoc->stream_locked = 1;
13237                                 asoc->stream_locked_on = srcv->sinfo_stream;
13238                         } else {
13239                                 sp->sender_all_done = 1;
13240                                 strm->last_msg_incomplete = 0;
13241                                 asoc->stream_locked = 0;
13242                         }
13243                 } else {
13244                         SCTP_PRINTF("Huh no sp TSNH?\n");
13245                         strm->last_msg_incomplete = 0;
13246                         asoc->stream_locked = 0;
13247                 }
13248                 SCTP_TCB_SEND_UNLOCK(stcb);
13249                 if (uio->uio_resid == 0) {
13250                         got_all_of_the_send = 1;
13251                 }
13252         } else if (top) {
13253                 /* We send in a 0, since we do NOT have any locks */
13254                 error = sctp_msg_append(stcb, net, top, srcv, 0);
13255                 top = NULL;
13256                 if (srcv->sinfo_flags & SCTP_EOF) {
13257                         /*
13258                          * This should only happen for Panda for the mbuf
13259                          * send case, which does NOT yet support EEOR mode.
13260                          * Thus, we can just set this flag to do the proper
13261                          * EOF handling.
13262                          */
13263                         got_all_of_the_send = 1;
13264                 }
13265         }
13266         if (error) {
13267                 goto out;
13268         }
13269 dataless_eof:
13270         /* EOF thing ? */
13271         if ((srcv->sinfo_flags & SCTP_EOF) &&
13272             (got_all_of_the_send == 1) &&
13273             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13274                 int cnt;
13275
13276                 SCTP_STAT_INCR(sctps_sends_with_eof);
13277                 error = 0;
13278                 if (hold_tcblock == 0) {
13279                         SCTP_TCB_LOCK(stcb);
13280                         hold_tcblock = 1;
13281                 }
13282                 cnt = sctp_is_there_unsent_data(stcb);
13283                 if (TAILQ_EMPTY(&asoc->send_queue) &&
13284                     TAILQ_EMPTY(&asoc->sent_queue) &&
13285                     (cnt == 0)) {
13286                         if (asoc->locked_on_sending) {
13287                                 goto abort_anyway;
13288                         }
13289                         /* there is nothing queued to send, so I'm done... */
13290                         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13291                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13292                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13293                                 /* only send SHUTDOWN the first time through */
13294                                 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
13295                                 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
13296                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13297                                 }
13298                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
13299                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
13300                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13301                                     asoc->primary_destination);
13302                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13303                                     asoc->primary_destination);
13304                         }
13305                 } else {
13306                         /*-
13307                          * we still got (or just got) data to send, so set
13308                          * SHUTDOWN_PENDING
13309                          */
13310                         /*-
13311                          * XXX sockets draft says that SCTP_EOF should be
13312                          * sent with no data.  currently, we will allow user
13313                          * data to be sent first and move to
13314                          * SHUTDOWN-PENDING
13315                          */
13316                         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
13317                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13318                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13319                                 if (hold_tcblock == 0) {
13320                                         SCTP_TCB_LOCK(stcb);
13321                                         hold_tcblock = 1;
13322                                 }
13323                                 if (asoc->locked_on_sending) {
13324                                         /* Locked to send out the data */
13325                                         struct sctp_stream_queue_pending *sp;
13326
13327                                         sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
13328                                         if (sp) {
13329                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
13330                                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
13331                                         }
13332                                 }
13333                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
13334                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
13335                                     TAILQ_EMPTY(&asoc->sent_queue) &&
13336                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13337                         abort_anyway:
13338                                         if (free_cnt_applied) {
13339                                                 atomic_add_int(&stcb->asoc.refcnt, -1);
13340                                                 free_cnt_applied = 0;
13341                                         }
13342                                         sctp_abort_an_association(stcb->sctp_ep, stcb,
13343                                             SCTP_RESPONSE_TO_USER_REQ,
13344                                             NULL, SCTP_SO_LOCKED);
13345                                         /*
13346                                          * now relock the stcb so everything
13347                                          * is sane
13348                                          */
13349                                         hold_tcblock = 0;
13350                                         stcb = NULL;
13351                                         goto out;
13352                                 }
13353                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13354                                     asoc->primary_destination);
13355                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13356                         }
13357                 }
13358         }
13359 skip_out_eof:
13360         if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
13361                 some_on_control = 1;
13362         }
13363         if ((net->flight_size > net->cwnd) &&
13364             (stcb->asoc.sctp_cmt_on_off == 0)) {
13365                 queue_only = 1;
13366         } else if (asoc->ifp_had_enobuf) {
13367                 SCTP_STAT_INCR(sctps_ifnomemqueued);
13368                 if (net->flight_size > (net->mtu * 2)) {
13369                         queue_only = 1;
13370                 } else {
13371                         queue_only = 0;
13372                 }
13373                 asoc->ifp_had_enobuf = 0;
13374                 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13375                     (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13376         } else {
13377                 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
13378                     (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
13379                 if (net->flight_size > net->cwnd) {
13380                         queue_only = 1;
13381                         SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13382                 } else {
13383                         queue_only = 0;
13384                 }
13385         }
13386         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13387             (stcb->asoc.total_flight > 0) &&
13388             (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13389             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
13390                 /*-
13391                  * Ok, Nagle is set on and we have data outstanding.
13392                  * Don't send anything and let SACKs drive out the
13393                  * data unless wen have a "full" segment to send.
13394                  */
13395                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13396                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13397                 }
13398                 SCTP_STAT_INCR(sctps_naglequeued);
13399                 nagle_applies = 1;
13400         } else {
13401                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13402                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13403                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13404                 }
13405                 SCTP_STAT_INCR(sctps_naglesent);
13406                 nagle_applies = 0;
13407         }
13408         if (queue_only_for_init) {
13409                 if (hold_tcblock == 0) {
13410                         SCTP_TCB_LOCK(stcb);
13411                         hold_tcblock = 1;
13412                 }
13413                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
13414                         /* a collision took us forward? */
13415                         queue_only_for_init = 0;
13416                         queue_only = 0;
13417                 } else {
13418                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13419                         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
13420                         queue_only_for_init = 0;
13421                         queue_only = 1;
13422                 }
13423         }
13424         if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
13425                 /* we can attempt to send too. */
13426                 if (hold_tcblock == 0) {
13427                         /*
13428                          * If there is activity recv'ing sacks no need to
13429                          * send
13430                          */
13431                         if (SCTP_TCB_TRYLOCK(stcb)) {
13432                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13433                                 hold_tcblock = 1;
13434                         }
13435                 } else {
13436                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13437                 }
13438         } else if ((queue_only == 0) &&
13439                     (stcb->asoc.peers_rwnd == 0) &&
13440             (stcb->asoc.total_flight == 0)) {
13441                 /* We get to have a probe outstanding */
13442                 if (hold_tcblock == 0) {
13443                         hold_tcblock = 1;
13444                         SCTP_TCB_LOCK(stcb);
13445                 }
13446                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13447         } else if (some_on_control) {
13448                 int num_out, reason, frag_point;
13449
13450                 /* Here we do control only */
13451                 if (hold_tcblock == 0) {
13452                         hold_tcblock = 1;
13453                         SCTP_TCB_LOCK(stcb);
13454                 }
13455                 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
13456                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
13457                     &reason, 1, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
13458         }
13459         SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13460             queue_only, stcb->asoc.peers_rwnd, un_sent,
13461             stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
13462             stcb->asoc.total_output_queue_size, error);
13463
13464 out:
13465 out_unlocked:
13466
13467         if (local_soresv && stcb) {
13468                 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
13469                 local_soresv = 0;
13470         }
13471         if (create_lock_applied) {
13472                 SCTP_ASOC_CREATE_UNLOCK(inp);
13473                 create_lock_applied = 0;
13474         }
13475         if ((stcb) && hold_tcblock) {
13476                 SCTP_TCB_UNLOCK(stcb);
13477         }
13478         if (stcb && free_cnt_applied) {
13479                 atomic_add_int(&stcb->asoc.refcnt, -1);
13480         }
13481 #ifdef INVARIANTS
13482         if (stcb) {
13483                 if (mtx_owned(&stcb->tcb_mtx)) {
13484                         panic("Leaving with tcb mtx owned?");
13485                 }
13486                 if (mtx_owned(&stcb->tcb_send_mtx)) {
13487                         panic("Leaving with tcb send mtx owned?");
13488                 }
13489         }
13490 #endif
13491 #ifdef INVARIANTS
13492         if (inp) {
13493                 sctp_validate_no_locks(inp);
13494         } else {
13495                 printf("Warning - inp is NULL so cant validate locks\n");
13496         }
13497 #endif
13498         if (top) {
13499                 sctp_m_freem(top);
13500         }
13501         if (control) {
13502                 sctp_m_freem(control);
13503         }
13504         return (error);
13505 }
13506
13507
13508 /*
13509  * generate an AUTHentication chunk, if required
13510  */
13511 struct mbuf *
13512 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13513     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
13514     struct sctp_tcb *stcb, uint8_t chunk)
13515 {
13516         struct mbuf *m_auth;
13517         struct sctp_auth_chunk *auth;
13518         int chunk_len;
13519
13520         if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13521             (stcb == NULL))
13522                 return (m);
13523
13524         /* sysctl disabled auth? */
13525         if (SCTP_BASE_SYSCTL(sctp_auth_disable))
13526                 return (m);
13527
13528         /* peer doesn't do auth... */
13529         if (!stcb->asoc.peer_supports_auth) {
13530                 return (m);
13531         }
13532         /* does the requested chunk require auth? */
13533         if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13534                 return (m);
13535         }
13536         m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
13537         if (m_auth == NULL) {
13538                 /* no mbuf's */
13539                 return (m);
13540         }
13541         /* reserve some space if this will be the first mbuf */
13542         if (m == NULL)
13543                 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13544         /* fill in the AUTH chunk details */
13545         auth = mtod(m_auth, struct sctp_auth_chunk *);
13546         bzero(auth, sizeof(*auth));
13547         auth->ch.chunk_type = SCTP_AUTHENTICATION;
13548         auth->ch.chunk_flags = 0;
13549         chunk_len = sizeof(*auth) +
13550             sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13551         auth->ch.chunk_length = htons(chunk_len);
13552         auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13553         /* key id and hmac digest will be computed and filled in upon send */
13554
13555         /* save the offset where the auth was inserted into the chain */
13556         if (m != NULL) {
13557                 struct mbuf *cn;
13558
13559                 *offset = 0;
13560                 cn = m;
13561                 while (cn) {
13562                         *offset += SCTP_BUF_LEN(cn);
13563                         cn = SCTP_BUF_NEXT(cn);
13564                 }
13565         } else
13566                 *offset = 0;
13567
13568         /* update length and return pointer to the auth chunk */
13569         SCTP_BUF_LEN(m_auth) = chunk_len;
13570         m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13571         if (auth_ret != NULL)
13572                 *auth_ret = auth;
13573
13574         return (m);
13575 }
13576
13577 #ifdef INET6
13578 int
13579 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
13580 {
13581         struct nd_prefix *pfx = NULL;
13582         struct nd_pfxrouter *pfxrtr = NULL;
13583         struct sockaddr_in6 gw6;
13584
13585         if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
13586                 return (0);
13587
13588         /* get prefix entry of address */
13589         LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13590                 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13591                         continue;
13592                 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13593                     &src6->sin6_addr, &pfx->ndpr_mask))
13594                         break;
13595         }
13596         /* no prefix entry in the prefix list */
13597         if (pfx == NULL) {
13598                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13599                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13600                 return (0);
13601         }
13602         SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13603         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13604
13605         /* search installed gateway from prefix entry */
13606         for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
13607             pfxrtr->pfr_next) {
13608                 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13609                 gw6.sin6_family = AF_INET6;
13610                 gw6.sin6_len = sizeof(struct sockaddr_in6);
13611                 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13612                     sizeof(struct in6_addr));
13613                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13614                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13615                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13616                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13617                 if (sctp_cmpaddr((struct sockaddr *)&gw6,
13618                     ro->ro_rt->rt_gateway)) {
13619                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13620                         return (1);
13621                 }
13622         }
13623         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13624         return (0);
13625 }
13626
13627 #endif
13628
13629 int
13630 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
13631 {
13632         struct sockaddr_in *sin, *mask;
13633         struct ifaddr *ifa;
13634         struct in_addr srcnetaddr, gwnetaddr;
13635
13636         if (ro == NULL || ro->ro_rt == NULL ||
13637             sifa->address.sa.sa_family != AF_INET) {
13638                 return (0);
13639         }
13640         ifa = (struct ifaddr *)sifa->ifa;
13641         mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13642         sin = (struct sockaddr_in *)&sifa->address.sin;
13643         srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13644         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
13645         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13646         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
13647
13648         sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
13649         gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13650         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
13651         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
13652         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
13653         if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13654                 return (1);
13655         }
13656         return (0);
13657 }