]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/netinet/sctp_output.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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/udp.h>
53 #include <machine/in_cksum.h>
54
55
56
57 #define SCTP_MAX_GAPS_INARRAY 4
58 struct sack_track {
59         uint8_t right_edge;     /* mergable on the right edge */
60         uint8_t left_edge;      /* mergable on the left edge */
61         uint8_t num_entries;
62         uint8_t spare;
63         struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
64 };
65
66 struct sack_track sack_array[256] = {
67         {0, 0, 0, 0,            /* 0x00 */
68                 {{0, 0},
69                 {0, 0},
70                 {0, 0},
71                 {0, 0}
72                 }
73         },
74         {1, 0, 1, 0,            /* 0x01 */
75                 {{0, 0},
76                 {0, 0},
77                 {0, 0},
78                 {0, 0}
79                 }
80         },
81         {0, 0, 1, 0,            /* 0x02 */
82                 {{1, 1},
83                 {0, 0},
84                 {0, 0},
85                 {0, 0}
86                 }
87         },
88         {1, 0, 1, 0,            /* 0x03 */
89                 {{0, 1},
90                 {0, 0},
91                 {0, 0},
92                 {0, 0}
93                 }
94         },
95         {0, 0, 1, 0,            /* 0x04 */
96                 {{2, 2},
97                 {0, 0},
98                 {0, 0},
99                 {0, 0}
100                 }
101         },
102         {1, 0, 2, 0,            /* 0x05 */
103                 {{0, 0},
104                 {2, 2},
105                 {0, 0},
106                 {0, 0}
107                 }
108         },
109         {0, 0, 1, 0,            /* 0x06 */
110                 {{1, 2},
111                 {0, 0},
112                 {0, 0},
113                 {0, 0}
114                 }
115         },
116         {1, 0, 1, 0,            /* 0x07 */
117                 {{0, 2},
118                 {0, 0},
119                 {0, 0},
120                 {0, 0}
121                 }
122         },
123         {0, 0, 1, 0,            /* 0x08 */
124                 {{3, 3},
125                 {0, 0},
126                 {0, 0},
127                 {0, 0}
128                 }
129         },
130         {1, 0, 2, 0,            /* 0x09 */
131                 {{0, 0},
132                 {3, 3},
133                 {0, 0},
134                 {0, 0}
135                 }
136         },
137         {0, 0, 2, 0,            /* 0x0a */
138                 {{1, 1},
139                 {3, 3},
140                 {0, 0},
141                 {0, 0}
142                 }
143         },
144         {1, 0, 2, 0,            /* 0x0b */
145                 {{0, 1},
146                 {3, 3},
147                 {0, 0},
148                 {0, 0}
149                 }
150         },
151         {0, 0, 1, 0,            /* 0x0c */
152                 {{2, 3},
153                 {0, 0},
154                 {0, 0},
155                 {0, 0}
156                 }
157         },
158         {1, 0, 2, 0,            /* 0x0d */
159                 {{0, 0},
160                 {2, 3},
161                 {0, 0},
162                 {0, 0}
163                 }
164         },
165         {0, 0, 1, 0,            /* 0x0e */
166                 {{1, 3},
167                 {0, 0},
168                 {0, 0},
169                 {0, 0}
170                 }
171         },
172         {1, 0, 1, 0,            /* 0x0f */
173                 {{0, 3},
174                 {0, 0},
175                 {0, 0},
176                 {0, 0}
177                 }
178         },
179         {0, 0, 1, 0,            /* 0x10 */
180                 {{4, 4},
181                 {0, 0},
182                 {0, 0},
183                 {0, 0}
184                 }
185         },
186         {1, 0, 2, 0,            /* 0x11 */
187                 {{0, 0},
188                 {4, 4},
189                 {0, 0},
190                 {0, 0}
191                 }
192         },
193         {0, 0, 2, 0,            /* 0x12 */
194                 {{1, 1},
195                 {4, 4},
196                 {0, 0},
197                 {0, 0}
198                 }
199         },
200         {1, 0, 2, 0,            /* 0x13 */
201                 {{0, 1},
202                 {4, 4},
203                 {0, 0},
204                 {0, 0}
205                 }
206         },
207         {0, 0, 2, 0,            /* 0x14 */
208                 {{2, 2},
209                 {4, 4},
210                 {0, 0},
211                 {0, 0}
212                 }
213         },
214         {1, 0, 3, 0,            /* 0x15 */
215                 {{0, 0},
216                 {2, 2},
217                 {4, 4},
218                 {0, 0}
219                 }
220         },
221         {0, 0, 2, 0,            /* 0x16 */
222                 {{1, 2},
223                 {4, 4},
224                 {0, 0},
225                 {0, 0}
226                 }
227         },
228         {1, 0, 2, 0,            /* 0x17 */
229                 {{0, 2},
230                 {4, 4},
231                 {0, 0},
232                 {0, 0}
233                 }
234         },
235         {0, 0, 1, 0,            /* 0x18 */
236                 {{3, 4},
237                 {0, 0},
238                 {0, 0},
239                 {0, 0}
240                 }
241         },
242         {1, 0, 2, 0,            /* 0x19 */
243                 {{0, 0},
244                 {3, 4},
245                 {0, 0},
246                 {0, 0}
247                 }
248         },
249         {0, 0, 2, 0,            /* 0x1a */
250                 {{1, 1},
251                 {3, 4},
252                 {0, 0},
253                 {0, 0}
254                 }
255         },
256         {1, 0, 2, 0,            /* 0x1b */
257                 {{0, 1},
258                 {3, 4},
259                 {0, 0},
260                 {0, 0}
261                 }
262         },
263         {0, 0, 1, 0,            /* 0x1c */
264                 {{2, 4},
265                 {0, 0},
266                 {0, 0},
267                 {0, 0}
268                 }
269         },
270         {1, 0, 2, 0,            /* 0x1d */
271                 {{0, 0},
272                 {2, 4},
273                 {0, 0},
274                 {0, 0}
275                 }
276         },
277         {0, 0, 1, 0,            /* 0x1e */
278                 {{1, 4},
279                 {0, 0},
280                 {0, 0},
281                 {0, 0}
282                 }
283         },
284         {1, 0, 1, 0,            /* 0x1f */
285                 {{0, 4},
286                 {0, 0},
287                 {0, 0},
288                 {0, 0}
289                 }
290         },
291         {0, 0, 1, 0,            /* 0x20 */
292                 {{5, 5},
293                 {0, 0},
294                 {0, 0},
295                 {0, 0}
296                 }
297         },
298         {1, 0, 2, 0,            /* 0x21 */
299                 {{0, 0},
300                 {5, 5},
301                 {0, 0},
302                 {0, 0}
303                 }
304         },
305         {0, 0, 2, 0,            /* 0x22 */
306                 {{1, 1},
307                 {5, 5},
308                 {0, 0},
309                 {0, 0}
310                 }
311         },
312         {1, 0, 2, 0,            /* 0x23 */
313                 {{0, 1},
314                 {5, 5},
315                 {0, 0},
316                 {0, 0}
317                 }
318         },
319         {0, 0, 2, 0,            /* 0x24 */
320                 {{2, 2},
321                 {5, 5},
322                 {0, 0},
323                 {0, 0}
324                 }
325         },
326         {1, 0, 3, 0,            /* 0x25 */
327                 {{0, 0},
328                 {2, 2},
329                 {5, 5},
330                 {0, 0}
331                 }
332         },
333         {0, 0, 2, 0,            /* 0x26 */
334                 {{1, 2},
335                 {5, 5},
336                 {0, 0},
337                 {0, 0}
338                 }
339         },
340         {1, 0, 2, 0,            /* 0x27 */
341                 {{0, 2},
342                 {5, 5},
343                 {0, 0},
344                 {0, 0}
345                 }
346         },
347         {0, 0, 2, 0,            /* 0x28 */
348                 {{3, 3},
349                 {5, 5},
350                 {0, 0},
351                 {0, 0}
352                 }
353         },
354         {1, 0, 3, 0,            /* 0x29 */
355                 {{0, 0},
356                 {3, 3},
357                 {5, 5},
358                 {0, 0}
359                 }
360         },
361         {0, 0, 3, 0,            /* 0x2a */
362                 {{1, 1},
363                 {3, 3},
364                 {5, 5},
365                 {0, 0}
366                 }
367         },
368         {1, 0, 3, 0,            /* 0x2b */
369                 {{0, 1},
370                 {3, 3},
371                 {5, 5},
372                 {0, 0}
373                 }
374         },
375         {0, 0, 2, 0,            /* 0x2c */
376                 {{2, 3},
377                 {5, 5},
378                 {0, 0},
379                 {0, 0}
380                 }
381         },
382         {1, 0, 3, 0,            /* 0x2d */
383                 {{0, 0},
384                 {2, 3},
385                 {5, 5},
386                 {0, 0}
387                 }
388         },
389         {0, 0, 2, 0,            /* 0x2e */
390                 {{1, 3},
391                 {5, 5},
392                 {0, 0},
393                 {0, 0}
394                 }
395         },
396         {1, 0, 2, 0,            /* 0x2f */
397                 {{0, 3},
398                 {5, 5},
399                 {0, 0},
400                 {0, 0}
401                 }
402         },
403         {0, 0, 1, 0,            /* 0x30 */
404                 {{4, 5},
405                 {0, 0},
406                 {0, 0},
407                 {0, 0}
408                 }
409         },
410         {1, 0, 2, 0,            /* 0x31 */
411                 {{0, 0},
412                 {4, 5},
413                 {0, 0},
414                 {0, 0}
415                 }
416         },
417         {0, 0, 2, 0,            /* 0x32 */
418                 {{1, 1},
419                 {4, 5},
420                 {0, 0},
421                 {0, 0}
422                 }
423         },
424         {1, 0, 2, 0,            /* 0x33 */
425                 {{0, 1},
426                 {4, 5},
427                 {0, 0},
428                 {0, 0}
429                 }
430         },
431         {0, 0, 2, 0,            /* 0x34 */
432                 {{2, 2},
433                 {4, 5},
434                 {0, 0},
435                 {0, 0}
436                 }
437         },
438         {1, 0, 3, 0,            /* 0x35 */
439                 {{0, 0},
440                 {2, 2},
441                 {4, 5},
442                 {0, 0}
443                 }
444         },
445         {0, 0, 2, 0,            /* 0x36 */
446                 {{1, 2},
447                 {4, 5},
448                 {0, 0},
449                 {0, 0}
450                 }
451         },
452         {1, 0, 2, 0,            /* 0x37 */
453                 {{0, 2},
454                 {4, 5},
455                 {0, 0},
456                 {0, 0}
457                 }
458         },
459         {0, 0, 1, 0,            /* 0x38 */
460                 {{3, 5},
461                 {0, 0},
462                 {0, 0},
463                 {0, 0}
464                 }
465         },
466         {1, 0, 2, 0,            /* 0x39 */
467                 {{0, 0},
468                 {3, 5},
469                 {0, 0},
470                 {0, 0}
471                 }
472         },
473         {0, 0, 2, 0,            /* 0x3a */
474                 {{1, 1},
475                 {3, 5},
476                 {0, 0},
477                 {0, 0}
478                 }
479         },
480         {1, 0, 2, 0,            /* 0x3b */
481                 {{0, 1},
482                 {3, 5},
483                 {0, 0},
484                 {0, 0}
485                 }
486         },
487         {0, 0, 1, 0,            /* 0x3c */
488                 {{2, 5},
489                 {0, 0},
490                 {0, 0},
491                 {0, 0}
492                 }
493         },
494         {1, 0, 2, 0,            /* 0x3d */
495                 {{0, 0},
496                 {2, 5},
497                 {0, 0},
498                 {0, 0}
499                 }
500         },
501         {0, 0, 1, 0,            /* 0x3e */
502                 {{1, 5},
503                 {0, 0},
504                 {0, 0},
505                 {0, 0}
506                 }
507         },
508         {1, 0, 1, 0,            /* 0x3f */
509                 {{0, 5},
510                 {0, 0},
511                 {0, 0},
512                 {0, 0}
513                 }
514         },
515         {0, 0, 1, 0,            /* 0x40 */
516                 {{6, 6},
517                 {0, 0},
518                 {0, 0},
519                 {0, 0}
520                 }
521         },
522         {1, 0, 2, 0,            /* 0x41 */
523                 {{0, 0},
524                 {6, 6},
525                 {0, 0},
526                 {0, 0}
527                 }
528         },
529         {0, 0, 2, 0,            /* 0x42 */
530                 {{1, 1},
531                 {6, 6},
532                 {0, 0},
533                 {0, 0}
534                 }
535         },
536         {1, 0, 2, 0,            /* 0x43 */
537                 {{0, 1},
538                 {6, 6},
539                 {0, 0},
540                 {0, 0}
541                 }
542         },
543         {0, 0, 2, 0,            /* 0x44 */
544                 {{2, 2},
545                 {6, 6},
546                 {0, 0},
547                 {0, 0}
548                 }
549         },
550         {1, 0, 3, 0,            /* 0x45 */
551                 {{0, 0},
552                 {2, 2},
553                 {6, 6},
554                 {0, 0}
555                 }
556         },
557         {0, 0, 2, 0,            /* 0x46 */
558                 {{1, 2},
559                 {6, 6},
560                 {0, 0},
561                 {0, 0}
562                 }
563         },
564         {1, 0, 2, 0,            /* 0x47 */
565                 {{0, 2},
566                 {6, 6},
567                 {0, 0},
568                 {0, 0}
569                 }
570         },
571         {0, 0, 2, 0,            /* 0x48 */
572                 {{3, 3},
573                 {6, 6},
574                 {0, 0},
575                 {0, 0}
576                 }
577         },
578         {1, 0, 3, 0,            /* 0x49 */
579                 {{0, 0},
580                 {3, 3},
581                 {6, 6},
582                 {0, 0}
583                 }
584         },
585         {0, 0, 3, 0,            /* 0x4a */
586                 {{1, 1},
587                 {3, 3},
588                 {6, 6},
589                 {0, 0}
590                 }
591         },
592         {1, 0, 3, 0,            /* 0x4b */
593                 {{0, 1},
594                 {3, 3},
595                 {6, 6},
596                 {0, 0}
597                 }
598         },
599         {0, 0, 2, 0,            /* 0x4c */
600                 {{2, 3},
601                 {6, 6},
602                 {0, 0},
603                 {0, 0}
604                 }
605         },
606         {1, 0, 3, 0,            /* 0x4d */
607                 {{0, 0},
608                 {2, 3},
609                 {6, 6},
610                 {0, 0}
611                 }
612         },
613         {0, 0, 2, 0,            /* 0x4e */
614                 {{1, 3},
615                 {6, 6},
616                 {0, 0},
617                 {0, 0}
618                 }
619         },
620         {1, 0, 2, 0,            /* 0x4f */
621                 {{0, 3},
622                 {6, 6},
623                 {0, 0},
624                 {0, 0}
625                 }
626         },
627         {0, 0, 2, 0,            /* 0x50 */
628                 {{4, 4},
629                 {6, 6},
630                 {0, 0},
631                 {0, 0}
632                 }
633         },
634         {1, 0, 3, 0,            /* 0x51 */
635                 {{0, 0},
636                 {4, 4},
637                 {6, 6},
638                 {0, 0}
639                 }
640         },
641         {0, 0, 3, 0,            /* 0x52 */
642                 {{1, 1},
643                 {4, 4},
644                 {6, 6},
645                 {0, 0}
646                 }
647         },
648         {1, 0, 3, 0,            /* 0x53 */
649                 {{0, 1},
650                 {4, 4},
651                 {6, 6},
652                 {0, 0}
653                 }
654         },
655         {0, 0, 3, 0,            /* 0x54 */
656                 {{2, 2},
657                 {4, 4},
658                 {6, 6},
659                 {0, 0}
660                 }
661         },
662         {1, 0, 4, 0,            /* 0x55 */
663                 {{0, 0},
664                 {2, 2},
665                 {4, 4},
666                 {6, 6}
667                 }
668         },
669         {0, 0, 3, 0,            /* 0x56 */
670                 {{1, 2},
671                 {4, 4},
672                 {6, 6},
673                 {0, 0}
674                 }
675         },
676         {1, 0, 3, 0,            /* 0x57 */
677                 {{0, 2},
678                 {4, 4},
679                 {6, 6},
680                 {0, 0}
681                 }
682         },
683         {0, 0, 2, 0,            /* 0x58 */
684                 {{3, 4},
685                 {6, 6},
686                 {0, 0},
687                 {0, 0}
688                 }
689         },
690         {1, 0, 3, 0,            /* 0x59 */
691                 {{0, 0},
692                 {3, 4},
693                 {6, 6},
694                 {0, 0}
695                 }
696         },
697         {0, 0, 3, 0,            /* 0x5a */
698                 {{1, 1},
699                 {3, 4},
700                 {6, 6},
701                 {0, 0}
702                 }
703         },
704         {1, 0, 3, 0,            /* 0x5b */
705                 {{0, 1},
706                 {3, 4},
707                 {6, 6},
708                 {0, 0}
709                 }
710         },
711         {0, 0, 2, 0,            /* 0x5c */
712                 {{2, 4},
713                 {6, 6},
714                 {0, 0},
715                 {0, 0}
716                 }
717         },
718         {1, 0, 3, 0,            /* 0x5d */
719                 {{0, 0},
720                 {2, 4},
721                 {6, 6},
722                 {0, 0}
723                 }
724         },
725         {0, 0, 2, 0,            /* 0x5e */
726                 {{1, 4},
727                 {6, 6},
728                 {0, 0},
729                 {0, 0}
730                 }
731         },
732         {1, 0, 2, 0,            /* 0x5f */
733                 {{0, 4},
734                 {6, 6},
735                 {0, 0},
736                 {0, 0}
737                 }
738         },
739         {0, 0, 1, 0,            /* 0x60 */
740                 {{5, 6},
741                 {0, 0},
742                 {0, 0},
743                 {0, 0}
744                 }
745         },
746         {1, 0, 2, 0,            /* 0x61 */
747                 {{0, 0},
748                 {5, 6},
749                 {0, 0},
750                 {0, 0}
751                 }
752         },
753         {0, 0, 2, 0,            /* 0x62 */
754                 {{1, 1},
755                 {5, 6},
756                 {0, 0},
757                 {0, 0}
758                 }
759         },
760         {1, 0, 2, 0,            /* 0x63 */
761                 {{0, 1},
762                 {5, 6},
763                 {0, 0},
764                 {0, 0}
765                 }
766         },
767         {0, 0, 2, 0,            /* 0x64 */
768                 {{2, 2},
769                 {5, 6},
770                 {0, 0},
771                 {0, 0}
772                 }
773         },
774         {1, 0, 3, 0,            /* 0x65 */
775                 {{0, 0},
776                 {2, 2},
777                 {5, 6},
778                 {0, 0}
779                 }
780         },
781         {0, 0, 2, 0,            /* 0x66 */
782                 {{1, 2},
783                 {5, 6},
784                 {0, 0},
785                 {0, 0}
786                 }
787         },
788         {1, 0, 2, 0,            /* 0x67 */
789                 {{0, 2},
790                 {5, 6},
791                 {0, 0},
792                 {0, 0}
793                 }
794         },
795         {0, 0, 2, 0,            /* 0x68 */
796                 {{3, 3},
797                 {5, 6},
798                 {0, 0},
799                 {0, 0}
800                 }
801         },
802         {1, 0, 3, 0,            /* 0x69 */
803                 {{0, 0},
804                 {3, 3},
805                 {5, 6},
806                 {0, 0}
807                 }
808         },
809         {0, 0, 3, 0,            /* 0x6a */
810                 {{1, 1},
811                 {3, 3},
812                 {5, 6},
813                 {0, 0}
814                 }
815         },
816         {1, 0, 3, 0,            /* 0x6b */
817                 {{0, 1},
818                 {3, 3},
819                 {5, 6},
820                 {0, 0}
821                 }
822         },
823         {0, 0, 2, 0,            /* 0x6c */
824                 {{2, 3},
825                 {5, 6},
826                 {0, 0},
827                 {0, 0}
828                 }
829         },
830         {1, 0, 3, 0,            /* 0x6d */
831                 {{0, 0},
832                 {2, 3},
833                 {5, 6},
834                 {0, 0}
835                 }
836         },
837         {0, 0, 2, 0,            /* 0x6e */
838                 {{1, 3},
839                 {5, 6},
840                 {0, 0},
841                 {0, 0}
842                 }
843         },
844         {1, 0, 2, 0,            /* 0x6f */
845                 {{0, 3},
846                 {5, 6},
847                 {0, 0},
848                 {0, 0}
849                 }
850         },
851         {0, 0, 1, 0,            /* 0x70 */
852                 {{4, 6},
853                 {0, 0},
854                 {0, 0},
855                 {0, 0}
856                 }
857         },
858         {1, 0, 2, 0,            /* 0x71 */
859                 {{0, 0},
860                 {4, 6},
861                 {0, 0},
862                 {0, 0}
863                 }
864         },
865         {0, 0, 2, 0,            /* 0x72 */
866                 {{1, 1},
867                 {4, 6},
868                 {0, 0},
869                 {0, 0}
870                 }
871         },
872         {1, 0, 2, 0,            /* 0x73 */
873                 {{0, 1},
874                 {4, 6},
875                 {0, 0},
876                 {0, 0}
877                 }
878         },
879         {0, 0, 2, 0,            /* 0x74 */
880                 {{2, 2},
881                 {4, 6},
882                 {0, 0},
883                 {0, 0}
884                 }
885         },
886         {1, 0, 3, 0,            /* 0x75 */
887                 {{0, 0},
888                 {2, 2},
889                 {4, 6},
890                 {0, 0}
891                 }
892         },
893         {0, 0, 2, 0,            /* 0x76 */
894                 {{1, 2},
895                 {4, 6},
896                 {0, 0},
897                 {0, 0}
898                 }
899         },
900         {1, 0, 2, 0,            /* 0x77 */
901                 {{0, 2},
902                 {4, 6},
903                 {0, 0},
904                 {0, 0}
905                 }
906         },
907         {0, 0, 1, 0,            /* 0x78 */
908                 {{3, 6},
909                 {0, 0},
910                 {0, 0},
911                 {0, 0}
912                 }
913         },
914         {1, 0, 2, 0,            /* 0x79 */
915                 {{0, 0},
916                 {3, 6},
917                 {0, 0},
918                 {0, 0}
919                 }
920         },
921         {0, 0, 2, 0,            /* 0x7a */
922                 {{1, 1},
923                 {3, 6},
924                 {0, 0},
925                 {0, 0}
926                 }
927         },
928         {1, 0, 2, 0,            /* 0x7b */
929                 {{0, 1},
930                 {3, 6},
931                 {0, 0},
932                 {0, 0}
933                 }
934         },
935         {0, 0, 1, 0,            /* 0x7c */
936                 {{2, 6},
937                 {0, 0},
938                 {0, 0},
939                 {0, 0}
940                 }
941         },
942         {1, 0, 2, 0,            /* 0x7d */
943                 {{0, 0},
944                 {2, 6},
945                 {0, 0},
946                 {0, 0}
947                 }
948         },
949         {0, 0, 1, 0,            /* 0x7e */
950                 {{1, 6},
951                 {0, 0},
952                 {0, 0},
953                 {0, 0}
954                 }
955         },
956         {1, 0, 1, 0,            /* 0x7f */
957                 {{0, 6},
958                 {0, 0},
959                 {0, 0},
960                 {0, 0}
961                 }
962         },
963         {0, 1, 1, 0,            /* 0x80 */
964                 {{7, 7},
965                 {0, 0},
966                 {0, 0},
967                 {0, 0}
968                 }
969         },
970         {1, 1, 2, 0,            /* 0x81 */
971                 {{0, 0},
972                 {7, 7},
973                 {0, 0},
974                 {0, 0}
975                 }
976         },
977         {0, 1, 2, 0,            /* 0x82 */
978                 {{1, 1},
979                 {7, 7},
980                 {0, 0},
981                 {0, 0}
982                 }
983         },
984         {1, 1, 2, 0,            /* 0x83 */
985                 {{0, 1},
986                 {7, 7},
987                 {0, 0},
988                 {0, 0}
989                 }
990         },
991         {0, 1, 2, 0,            /* 0x84 */
992                 {{2, 2},
993                 {7, 7},
994                 {0, 0},
995                 {0, 0}
996                 }
997         },
998         {1, 1, 3, 0,            /* 0x85 */
999                 {{0, 0},
1000                 {2, 2},
1001                 {7, 7},
1002                 {0, 0}
1003                 }
1004         },
1005         {0, 1, 2, 0,            /* 0x86 */
1006                 {{1, 2},
1007                 {7, 7},
1008                 {0, 0},
1009                 {0, 0}
1010                 }
1011         },
1012         {1, 1, 2, 0,            /* 0x87 */
1013                 {{0, 2},
1014                 {7, 7},
1015                 {0, 0},
1016                 {0, 0}
1017                 }
1018         },
1019         {0, 1, 2, 0,            /* 0x88 */
1020                 {{3, 3},
1021                 {7, 7},
1022                 {0, 0},
1023                 {0, 0}
1024                 }
1025         },
1026         {1, 1, 3, 0,            /* 0x89 */
1027                 {{0, 0},
1028                 {3, 3},
1029                 {7, 7},
1030                 {0, 0}
1031                 }
1032         },
1033         {0, 1, 3, 0,            /* 0x8a */
1034                 {{1, 1},
1035                 {3, 3},
1036                 {7, 7},
1037                 {0, 0}
1038                 }
1039         },
1040         {1, 1, 3, 0,            /* 0x8b */
1041                 {{0, 1},
1042                 {3, 3},
1043                 {7, 7},
1044                 {0, 0}
1045                 }
1046         },
1047         {0, 1, 2, 0,            /* 0x8c */
1048                 {{2, 3},
1049                 {7, 7},
1050                 {0, 0},
1051                 {0, 0}
1052                 }
1053         },
1054         {1, 1, 3, 0,            /* 0x8d */
1055                 {{0, 0},
1056                 {2, 3},
1057                 {7, 7},
1058                 {0, 0}
1059                 }
1060         },
1061         {0, 1, 2, 0,            /* 0x8e */
1062                 {{1, 3},
1063                 {7, 7},
1064                 {0, 0},
1065                 {0, 0}
1066                 }
1067         },
1068         {1, 1, 2, 0,            /* 0x8f */
1069                 {{0, 3},
1070                 {7, 7},
1071                 {0, 0},
1072                 {0, 0}
1073                 }
1074         },
1075         {0, 1, 2, 0,            /* 0x90 */
1076                 {{4, 4},
1077                 {7, 7},
1078                 {0, 0},
1079                 {0, 0}
1080                 }
1081         },
1082         {1, 1, 3, 0,            /* 0x91 */
1083                 {{0, 0},
1084                 {4, 4},
1085                 {7, 7},
1086                 {0, 0}
1087                 }
1088         },
1089         {0, 1, 3, 0,            /* 0x92 */
1090                 {{1, 1},
1091                 {4, 4},
1092                 {7, 7},
1093                 {0, 0}
1094                 }
1095         },
1096         {1, 1, 3, 0,            /* 0x93 */
1097                 {{0, 1},
1098                 {4, 4},
1099                 {7, 7},
1100                 {0, 0}
1101                 }
1102         },
1103         {0, 1, 3, 0,            /* 0x94 */
1104                 {{2, 2},
1105                 {4, 4},
1106                 {7, 7},
1107                 {0, 0}
1108                 }
1109         },
1110         {1, 1, 4, 0,            /* 0x95 */
1111                 {{0, 0},
1112                 {2, 2},
1113                 {4, 4},
1114                 {7, 7}
1115                 }
1116         },
1117         {0, 1, 3, 0,            /* 0x96 */
1118                 {{1, 2},
1119                 {4, 4},
1120                 {7, 7},
1121                 {0, 0}
1122                 }
1123         },
1124         {1, 1, 3, 0,            /* 0x97 */
1125                 {{0, 2},
1126                 {4, 4},
1127                 {7, 7},
1128                 {0, 0}
1129                 }
1130         },
1131         {0, 1, 2, 0,            /* 0x98 */
1132                 {{3, 4},
1133                 {7, 7},
1134                 {0, 0},
1135                 {0, 0}
1136                 }
1137         },
1138         {1, 1, 3, 0,            /* 0x99 */
1139                 {{0, 0},
1140                 {3, 4},
1141                 {7, 7},
1142                 {0, 0}
1143                 }
1144         },
1145         {0, 1, 3, 0,            /* 0x9a */
1146                 {{1, 1},
1147                 {3, 4},
1148                 {7, 7},
1149                 {0, 0}
1150                 }
1151         },
1152         {1, 1, 3, 0,            /* 0x9b */
1153                 {{0, 1},
1154                 {3, 4},
1155                 {7, 7},
1156                 {0, 0}
1157                 }
1158         },
1159         {0, 1, 2, 0,            /* 0x9c */
1160                 {{2, 4},
1161                 {7, 7},
1162                 {0, 0},
1163                 {0, 0}
1164                 }
1165         },
1166         {1, 1, 3, 0,            /* 0x9d */
1167                 {{0, 0},
1168                 {2, 4},
1169                 {7, 7},
1170                 {0, 0}
1171                 }
1172         },
1173         {0, 1, 2, 0,            /* 0x9e */
1174                 {{1, 4},
1175                 {7, 7},
1176                 {0, 0},
1177                 {0, 0}
1178                 }
1179         },
1180         {1, 1, 2, 0,            /* 0x9f */
1181                 {{0, 4},
1182                 {7, 7},
1183                 {0, 0},
1184                 {0, 0}
1185                 }
1186         },
1187         {0, 1, 2, 0,            /* 0xa0 */
1188                 {{5, 5},
1189                 {7, 7},
1190                 {0, 0},
1191                 {0, 0}
1192                 }
1193         },
1194         {1, 1, 3, 0,            /* 0xa1 */
1195                 {{0, 0},
1196                 {5, 5},
1197                 {7, 7},
1198                 {0, 0}
1199                 }
1200         },
1201         {0, 1, 3, 0,            /* 0xa2 */
1202                 {{1, 1},
1203                 {5, 5},
1204                 {7, 7},
1205                 {0, 0}
1206                 }
1207         },
1208         {1, 1, 3, 0,            /* 0xa3 */
1209                 {{0, 1},
1210                 {5, 5},
1211                 {7, 7},
1212                 {0, 0}
1213                 }
1214         },
1215         {0, 1, 3, 0,            /* 0xa4 */
1216                 {{2, 2},
1217                 {5, 5},
1218                 {7, 7},
1219                 {0, 0}
1220                 }
1221         },
1222         {1, 1, 4, 0,            /* 0xa5 */
1223                 {{0, 0},
1224                 {2, 2},
1225                 {5, 5},
1226                 {7, 7}
1227                 }
1228         },
1229         {0, 1, 3, 0,            /* 0xa6 */
1230                 {{1, 2},
1231                 {5, 5},
1232                 {7, 7},
1233                 {0, 0}
1234                 }
1235         },
1236         {1, 1, 3, 0,            /* 0xa7 */
1237                 {{0, 2},
1238                 {5, 5},
1239                 {7, 7},
1240                 {0, 0}
1241                 }
1242         },
1243         {0, 1, 3, 0,            /* 0xa8 */
1244                 {{3, 3},
1245                 {5, 5},
1246                 {7, 7},
1247                 {0, 0}
1248                 }
1249         },
1250         {1, 1, 4, 0,            /* 0xa9 */
1251                 {{0, 0},
1252                 {3, 3},
1253                 {5, 5},
1254                 {7, 7}
1255                 }
1256         },
1257         {0, 1, 4, 0,            /* 0xaa */
1258                 {{1, 1},
1259                 {3, 3},
1260                 {5, 5},
1261                 {7, 7}
1262                 }
1263         },
1264         {1, 1, 4, 0,            /* 0xab */
1265                 {{0, 1},
1266                 {3, 3},
1267                 {5, 5},
1268                 {7, 7}
1269                 }
1270         },
1271         {0, 1, 3, 0,            /* 0xac */
1272                 {{2, 3},
1273                 {5, 5},
1274                 {7, 7},
1275                 {0, 0}
1276                 }
1277         },
1278         {1, 1, 4, 0,            /* 0xad */
1279                 {{0, 0},
1280                 {2, 3},
1281                 {5, 5},
1282                 {7, 7}
1283                 }
1284         },
1285         {0, 1, 3, 0,            /* 0xae */
1286                 {{1, 3},
1287                 {5, 5},
1288                 {7, 7},
1289                 {0, 0}
1290                 }
1291         },
1292         {1, 1, 3, 0,            /* 0xaf */
1293                 {{0, 3},
1294                 {5, 5},
1295                 {7, 7},
1296                 {0, 0}
1297                 }
1298         },
1299         {0, 1, 2, 0,            /* 0xb0 */
1300                 {{4, 5},
1301                 {7, 7},
1302                 {0, 0},
1303                 {0, 0}
1304                 }
1305         },
1306         {1, 1, 3, 0,            /* 0xb1 */
1307                 {{0, 0},
1308                 {4, 5},
1309                 {7, 7},
1310                 {0, 0}
1311                 }
1312         },
1313         {0, 1, 3, 0,            /* 0xb2 */
1314                 {{1, 1},
1315                 {4, 5},
1316                 {7, 7},
1317                 {0, 0}
1318                 }
1319         },
1320         {1, 1, 3, 0,            /* 0xb3 */
1321                 {{0, 1},
1322                 {4, 5},
1323                 {7, 7},
1324                 {0, 0}
1325                 }
1326         },
1327         {0, 1, 3, 0,            /* 0xb4 */
1328                 {{2, 2},
1329                 {4, 5},
1330                 {7, 7},
1331                 {0, 0}
1332                 }
1333         },
1334         {1, 1, 4, 0,            /* 0xb5 */
1335                 {{0, 0},
1336                 {2, 2},
1337                 {4, 5},
1338                 {7, 7}
1339                 }
1340         },
1341         {0, 1, 3, 0,            /* 0xb6 */
1342                 {{1, 2},
1343                 {4, 5},
1344                 {7, 7},
1345                 {0, 0}
1346                 }
1347         },
1348         {1, 1, 3, 0,            /* 0xb7 */
1349                 {{0, 2},
1350                 {4, 5},
1351                 {7, 7},
1352                 {0, 0}
1353                 }
1354         },
1355         {0, 1, 2, 0,            /* 0xb8 */
1356                 {{3, 5},
1357                 {7, 7},
1358                 {0, 0},
1359                 {0, 0}
1360                 }
1361         },
1362         {1, 1, 3, 0,            /* 0xb9 */
1363                 {{0, 0},
1364                 {3, 5},
1365                 {7, 7},
1366                 {0, 0}
1367                 }
1368         },
1369         {0, 1, 3, 0,            /* 0xba */
1370                 {{1, 1},
1371                 {3, 5},
1372                 {7, 7},
1373                 {0, 0}
1374                 }
1375         },
1376         {1, 1, 3, 0,            /* 0xbb */
1377                 {{0, 1},
1378                 {3, 5},
1379                 {7, 7},
1380                 {0, 0}
1381                 }
1382         },
1383         {0, 1, 2, 0,            /* 0xbc */
1384                 {{2, 5},
1385                 {7, 7},
1386                 {0, 0},
1387                 {0, 0}
1388                 }
1389         },
1390         {1, 1, 3, 0,            /* 0xbd */
1391                 {{0, 0},
1392                 {2, 5},
1393                 {7, 7},
1394                 {0, 0}
1395                 }
1396         },
1397         {0, 1, 2, 0,            /* 0xbe */
1398                 {{1, 5},
1399                 {7, 7},
1400                 {0, 0},
1401                 {0, 0}
1402                 }
1403         },
1404         {1, 1, 2, 0,            /* 0xbf */
1405                 {{0, 5},
1406                 {7, 7},
1407                 {0, 0},
1408                 {0, 0}
1409                 }
1410         },
1411         {0, 1, 1, 0,            /* 0xc0 */
1412                 {{6, 7},
1413                 {0, 0},
1414                 {0, 0},
1415                 {0, 0}
1416                 }
1417         },
1418         {1, 1, 2, 0,            /* 0xc1 */
1419                 {{0, 0},
1420                 {6, 7},
1421                 {0, 0},
1422                 {0, 0}
1423                 }
1424         },
1425         {0, 1, 2, 0,            /* 0xc2 */
1426                 {{1, 1},
1427                 {6, 7},
1428                 {0, 0},
1429                 {0, 0}
1430                 }
1431         },
1432         {1, 1, 2, 0,            /* 0xc3 */
1433                 {{0, 1},
1434                 {6, 7},
1435                 {0, 0},
1436                 {0, 0}
1437                 }
1438         },
1439         {0, 1, 2, 0,            /* 0xc4 */
1440                 {{2, 2},
1441                 {6, 7},
1442                 {0, 0},
1443                 {0, 0}
1444                 }
1445         },
1446         {1, 1, 3, 0,            /* 0xc5 */
1447                 {{0, 0},
1448                 {2, 2},
1449                 {6, 7},
1450                 {0, 0}
1451                 }
1452         },
1453         {0, 1, 2, 0,            /* 0xc6 */
1454                 {{1, 2},
1455                 {6, 7},
1456                 {0, 0},
1457                 {0, 0}
1458                 }
1459         },
1460         {1, 1, 2, 0,            /* 0xc7 */
1461                 {{0, 2},
1462                 {6, 7},
1463                 {0, 0},
1464                 {0, 0}
1465                 }
1466         },
1467         {0, 1, 2, 0,            /* 0xc8 */
1468                 {{3, 3},
1469                 {6, 7},
1470                 {0, 0},
1471                 {0, 0}
1472                 }
1473         },
1474         {1, 1, 3, 0,            /* 0xc9 */
1475                 {{0, 0},
1476                 {3, 3},
1477                 {6, 7},
1478                 {0, 0}
1479                 }
1480         },
1481         {0, 1, 3, 0,            /* 0xca */
1482                 {{1, 1},
1483                 {3, 3},
1484                 {6, 7},
1485                 {0, 0}
1486                 }
1487         },
1488         {1, 1, 3, 0,            /* 0xcb */
1489                 {{0, 1},
1490                 {3, 3},
1491                 {6, 7},
1492                 {0, 0}
1493                 }
1494         },
1495         {0, 1, 2, 0,            /* 0xcc */
1496                 {{2, 3},
1497                 {6, 7},
1498                 {0, 0},
1499                 {0, 0}
1500                 }
1501         },
1502         {1, 1, 3, 0,            /* 0xcd */
1503                 {{0, 0},
1504                 {2, 3},
1505                 {6, 7},
1506                 {0, 0}
1507                 }
1508         },
1509         {0, 1, 2, 0,            /* 0xce */
1510                 {{1, 3},
1511                 {6, 7},
1512                 {0, 0},
1513                 {0, 0}
1514                 }
1515         },
1516         {1, 1, 2, 0,            /* 0xcf */
1517                 {{0, 3},
1518                 {6, 7},
1519                 {0, 0},
1520                 {0, 0}
1521                 }
1522         },
1523         {0, 1, 2, 0,            /* 0xd0 */
1524                 {{4, 4},
1525                 {6, 7},
1526                 {0, 0},
1527                 {0, 0}
1528                 }
1529         },
1530         {1, 1, 3, 0,            /* 0xd1 */
1531                 {{0, 0},
1532                 {4, 4},
1533                 {6, 7},
1534                 {0, 0}
1535                 }
1536         },
1537         {0, 1, 3, 0,            /* 0xd2 */
1538                 {{1, 1},
1539                 {4, 4},
1540                 {6, 7},
1541                 {0, 0}
1542                 }
1543         },
1544         {1, 1, 3, 0,            /* 0xd3 */
1545                 {{0, 1},
1546                 {4, 4},
1547                 {6, 7},
1548                 {0, 0}
1549                 }
1550         },
1551         {0, 1, 3, 0,            /* 0xd4 */
1552                 {{2, 2},
1553                 {4, 4},
1554                 {6, 7},
1555                 {0, 0}
1556                 }
1557         },
1558         {1, 1, 4, 0,            /* 0xd5 */
1559                 {{0, 0},
1560                 {2, 2},
1561                 {4, 4},
1562                 {6, 7}
1563                 }
1564         },
1565         {0, 1, 3, 0,            /* 0xd6 */
1566                 {{1, 2},
1567                 {4, 4},
1568                 {6, 7},
1569                 {0, 0}
1570                 }
1571         },
1572         {1, 1, 3, 0,            /* 0xd7 */
1573                 {{0, 2},
1574                 {4, 4},
1575                 {6, 7},
1576                 {0, 0}
1577                 }
1578         },
1579         {0, 1, 2, 0,            /* 0xd8 */
1580                 {{3, 4},
1581                 {6, 7},
1582                 {0, 0},
1583                 {0, 0}
1584                 }
1585         },
1586         {1, 1, 3, 0,            /* 0xd9 */
1587                 {{0, 0},
1588                 {3, 4},
1589                 {6, 7},
1590                 {0, 0}
1591                 }
1592         },
1593         {0, 1, 3, 0,            /* 0xda */
1594                 {{1, 1},
1595                 {3, 4},
1596                 {6, 7},
1597                 {0, 0}
1598                 }
1599         },
1600         {1, 1, 3, 0,            /* 0xdb */
1601                 {{0, 1},
1602                 {3, 4},
1603                 {6, 7},
1604                 {0, 0}
1605                 }
1606         },
1607         {0, 1, 2, 0,            /* 0xdc */
1608                 {{2, 4},
1609                 {6, 7},
1610                 {0, 0},
1611                 {0, 0}
1612                 }
1613         },
1614         {1, 1, 3, 0,            /* 0xdd */
1615                 {{0, 0},
1616                 {2, 4},
1617                 {6, 7},
1618                 {0, 0}
1619                 }
1620         },
1621         {0, 1, 2, 0,            /* 0xde */
1622                 {{1, 4},
1623                 {6, 7},
1624                 {0, 0},
1625                 {0, 0}
1626                 }
1627         },
1628         {1, 1, 2, 0,            /* 0xdf */
1629                 {{0, 4},
1630                 {6, 7},
1631                 {0, 0},
1632                 {0, 0}
1633                 }
1634         },
1635         {0, 1, 1, 0,            /* 0xe0 */
1636                 {{5, 7},
1637                 {0, 0},
1638                 {0, 0},
1639                 {0, 0}
1640                 }
1641         },
1642         {1, 1, 2, 0,            /* 0xe1 */
1643                 {{0, 0},
1644                 {5, 7},
1645                 {0, 0},
1646                 {0, 0}
1647                 }
1648         },
1649         {0, 1, 2, 0,            /* 0xe2 */
1650                 {{1, 1},
1651                 {5, 7},
1652                 {0, 0},
1653                 {0, 0}
1654                 }
1655         },
1656         {1, 1, 2, 0,            /* 0xe3 */
1657                 {{0, 1},
1658                 {5, 7},
1659                 {0, 0},
1660                 {0, 0}
1661                 }
1662         },
1663         {0, 1, 2, 0,            /* 0xe4 */
1664                 {{2, 2},
1665                 {5, 7},
1666                 {0, 0},
1667                 {0, 0}
1668                 }
1669         },
1670         {1, 1, 3, 0,            /* 0xe5 */
1671                 {{0, 0},
1672                 {2, 2},
1673                 {5, 7},
1674                 {0, 0}
1675                 }
1676         },
1677         {0, 1, 2, 0,            /* 0xe6 */
1678                 {{1, 2},
1679                 {5, 7},
1680                 {0, 0},
1681                 {0, 0}
1682                 }
1683         },
1684         {1, 1, 2, 0,            /* 0xe7 */
1685                 {{0, 2},
1686                 {5, 7},
1687                 {0, 0},
1688                 {0, 0}
1689                 }
1690         },
1691         {0, 1, 2, 0,            /* 0xe8 */
1692                 {{3, 3},
1693                 {5, 7},
1694                 {0, 0},
1695                 {0, 0}
1696                 }
1697         },
1698         {1, 1, 3, 0,            /* 0xe9 */
1699                 {{0, 0},
1700                 {3, 3},
1701                 {5, 7},
1702                 {0, 0}
1703                 }
1704         },
1705         {0, 1, 3, 0,            /* 0xea */
1706                 {{1, 1},
1707                 {3, 3},
1708                 {5, 7},
1709                 {0, 0}
1710                 }
1711         },
1712         {1, 1, 3, 0,            /* 0xeb */
1713                 {{0, 1},
1714                 {3, 3},
1715                 {5, 7},
1716                 {0, 0}
1717                 }
1718         },
1719         {0, 1, 2, 0,            /* 0xec */
1720                 {{2, 3},
1721                 {5, 7},
1722                 {0, 0},
1723                 {0, 0}
1724                 }
1725         },
1726         {1, 1, 3, 0,            /* 0xed */
1727                 {{0, 0},
1728                 {2, 3},
1729                 {5, 7},
1730                 {0, 0}
1731                 }
1732         },
1733         {0, 1, 2, 0,            /* 0xee */
1734                 {{1, 3},
1735                 {5, 7},
1736                 {0, 0},
1737                 {0, 0}
1738                 }
1739         },
1740         {1, 1, 2, 0,            /* 0xef */
1741                 {{0, 3},
1742                 {5, 7},
1743                 {0, 0},
1744                 {0, 0}
1745                 }
1746         },
1747         {0, 1, 1, 0,            /* 0xf0 */
1748                 {{4, 7},
1749                 {0, 0},
1750                 {0, 0},
1751                 {0, 0}
1752                 }
1753         },
1754         {1, 1, 2, 0,            /* 0xf1 */
1755                 {{0, 0},
1756                 {4, 7},
1757                 {0, 0},
1758                 {0, 0}
1759                 }
1760         },
1761         {0, 1, 2, 0,            /* 0xf2 */
1762                 {{1, 1},
1763                 {4, 7},
1764                 {0, 0},
1765                 {0, 0}
1766                 }
1767         },
1768         {1, 1, 2, 0,            /* 0xf3 */
1769                 {{0, 1},
1770                 {4, 7},
1771                 {0, 0},
1772                 {0, 0}
1773                 }
1774         },
1775         {0, 1, 2, 0,            /* 0xf4 */
1776                 {{2, 2},
1777                 {4, 7},
1778                 {0, 0},
1779                 {0, 0}
1780                 }
1781         },
1782         {1, 1, 3, 0,            /* 0xf5 */
1783                 {{0, 0},
1784                 {2, 2},
1785                 {4, 7},
1786                 {0, 0}
1787                 }
1788         },
1789         {0, 1, 2, 0,            /* 0xf6 */
1790                 {{1, 2},
1791                 {4, 7},
1792                 {0, 0},
1793                 {0, 0}
1794                 }
1795         },
1796         {1, 1, 2, 0,            /* 0xf7 */
1797                 {{0, 2},
1798                 {4, 7},
1799                 {0, 0},
1800                 {0, 0}
1801                 }
1802         },
1803         {0, 1, 1, 0,            /* 0xf8 */
1804                 {{3, 7},
1805                 {0, 0},
1806                 {0, 0},
1807                 {0, 0}
1808                 }
1809         },
1810         {1, 1, 2, 0,            /* 0xf9 */
1811                 {{0, 0},
1812                 {3, 7},
1813                 {0, 0},
1814                 {0, 0}
1815                 }
1816         },
1817         {0, 1, 2, 0,            /* 0xfa */
1818                 {{1, 1},
1819                 {3, 7},
1820                 {0, 0},
1821                 {0, 0}
1822                 }
1823         },
1824         {1, 1, 2, 0,            /* 0xfb */
1825                 {{0, 1},
1826                 {3, 7},
1827                 {0, 0},
1828                 {0, 0}
1829                 }
1830         },
1831         {0, 1, 1, 0,            /* 0xfc */
1832                 {{2, 7},
1833                 {0, 0},
1834                 {0, 0},
1835                 {0, 0}
1836                 }
1837         },
1838         {1, 1, 2, 0,            /* 0xfd */
1839                 {{0, 0},
1840                 {2, 7},
1841                 {0, 0},
1842                 {0, 0}
1843                 }
1844         },
1845         {0, 1, 1, 0,            /* 0xfe */
1846                 {{1, 7},
1847                 {0, 0},
1848                 {0, 0},
1849                 {0, 0}
1850                 }
1851         },
1852         {1, 1, 1, 0,            /* 0xff */
1853                 {{0, 7},
1854                 {0, 0},
1855                 {0, 0},
1856                 {0, 0}
1857                 }
1858         }
1859 };
1860
1861 /* EY  below are nr_sacks version of the preceeding two data structures, identical except their names */
1862 #define SCTP_MAX_NR_GAPS_INARRAY 4
1863 struct nr_sack_track {
1864         uint8_t right_edge;     /* mergable on the right edge */
1865         uint8_t left_edge;      /* mergable on the left edge */
1866         uint8_t num_entries;
1867         uint8_t spare;
1868         struct sctp_nr_gap_ack_block nr_gaps[SCTP_MAX_NR_GAPS_INARRAY];
1869 };
1870
1871 struct nr_sack_track nr_sack_array[256] = {
1872         {0, 0, 0, 0,            /* 0x00 */
1873                 {{0, 0},
1874                 {0, 0},
1875                 {0, 0},
1876                 {0, 0}
1877                 }
1878         },
1879         {1, 0, 1, 0,            /* 0x01 */
1880                 {{0, 0},
1881                 {0, 0},
1882                 {0, 0},
1883                 {0, 0}
1884                 }
1885         },
1886         {0, 0, 1, 0,            /* 0x02 */
1887                 {{1, 1},
1888                 {0, 0},
1889                 {0, 0},
1890                 {0, 0}
1891                 }
1892         },
1893         {1, 0, 1, 0,            /* 0x03 */
1894                 {{0, 1},
1895                 {0, 0},
1896                 {0, 0},
1897                 {0, 0}
1898                 }
1899         },
1900         {0, 0, 1, 0,            /* 0x04 */
1901                 {{2, 2},
1902                 {0, 0},
1903                 {0, 0},
1904                 {0, 0}
1905                 }
1906         },
1907         {1, 0, 2, 0,            /* 0x05 */
1908                 {{0, 0},
1909                 {2, 2},
1910                 {0, 0},
1911                 {0, 0}
1912                 }
1913         },
1914         {0, 0, 1, 0,            /* 0x06 */
1915                 {{1, 2},
1916                 {0, 0},
1917                 {0, 0},
1918                 {0, 0}
1919                 }
1920         },
1921         {1, 0, 1, 0,            /* 0x07 */
1922                 {{0, 2},
1923                 {0, 0},
1924                 {0, 0},
1925                 {0, 0}
1926                 }
1927         },
1928         {0, 0, 1, 0,            /* 0x08 */
1929                 {{3, 3},
1930                 {0, 0},
1931                 {0, 0},
1932                 {0, 0}
1933                 }
1934         },
1935         {1, 0, 2, 0,            /* 0x09 */
1936                 {{0, 0},
1937                 {3, 3},
1938                 {0, 0},
1939                 {0, 0}
1940                 }
1941         },
1942         {0, 0, 2, 0,            /* 0x0a */
1943                 {{1, 1},
1944                 {3, 3},
1945                 {0, 0},
1946                 {0, 0}
1947                 }
1948         },
1949         {1, 0, 2, 0,            /* 0x0b */
1950                 {{0, 1},
1951                 {3, 3},
1952                 {0, 0},
1953                 {0, 0}
1954                 }
1955         },
1956         {0, 0, 1, 0,            /* 0x0c */
1957                 {{2, 3},
1958                 {0, 0},
1959                 {0, 0},
1960                 {0, 0}
1961                 }
1962         },
1963         {1, 0, 2, 0,            /* 0x0d */
1964                 {{0, 0},
1965                 {2, 3},
1966                 {0, 0},
1967                 {0, 0}
1968                 }
1969         },
1970         {0, 0, 1, 0,            /* 0x0e */
1971                 {{1, 3},
1972                 {0, 0},
1973                 {0, 0},
1974                 {0, 0}
1975                 }
1976         },
1977         {1, 0, 1, 0,            /* 0x0f */
1978                 {{0, 3},
1979                 {0, 0},
1980                 {0, 0},
1981                 {0, 0}
1982                 }
1983         },
1984         {0, 0, 1, 0,            /* 0x10 */
1985                 {{4, 4},
1986                 {0, 0},
1987                 {0, 0},
1988                 {0, 0}
1989                 }
1990         },
1991         {1, 0, 2, 0,            /* 0x11 */
1992                 {{0, 0},
1993                 {4, 4},
1994                 {0, 0},
1995                 {0, 0}
1996                 }
1997         },
1998         {0, 0, 2, 0,            /* 0x12 */
1999                 {{1, 1},
2000                 {4, 4},
2001                 {0, 0},
2002                 {0, 0}
2003                 }
2004         },
2005         {1, 0, 2, 0,            /* 0x13 */
2006                 {{0, 1},
2007                 {4, 4},
2008                 {0, 0},
2009                 {0, 0}
2010                 }
2011         },
2012         {0, 0, 2, 0,            /* 0x14 */
2013                 {{2, 2},
2014                 {4, 4},
2015                 {0, 0},
2016                 {0, 0}
2017                 }
2018         },
2019         {1, 0, 3, 0,            /* 0x15 */
2020                 {{0, 0},
2021                 {2, 2},
2022                 {4, 4},
2023                 {0, 0}
2024                 }
2025         },
2026         {0, 0, 2, 0,            /* 0x16 */
2027                 {{1, 2},
2028                 {4, 4},
2029                 {0, 0},
2030                 {0, 0}
2031                 }
2032         },
2033         {1, 0, 2, 0,            /* 0x17 */
2034                 {{0, 2},
2035                 {4, 4},
2036                 {0, 0},
2037                 {0, 0}
2038                 }
2039         },
2040         {0, 0, 1, 0,            /* 0x18 */
2041                 {{3, 4},
2042                 {0, 0},
2043                 {0, 0},
2044                 {0, 0}
2045                 }
2046         },
2047         {1, 0, 2, 0,            /* 0x19 */
2048                 {{0, 0},
2049                 {3, 4},
2050                 {0, 0},
2051                 {0, 0}
2052                 }
2053         },
2054         {0, 0, 2, 0,            /* 0x1a */
2055                 {{1, 1},
2056                 {3, 4},
2057                 {0, 0},
2058                 {0, 0}
2059                 }
2060         },
2061         {1, 0, 2, 0,            /* 0x1b */
2062                 {{0, 1},
2063                 {3, 4},
2064                 {0, 0},
2065                 {0, 0}
2066                 }
2067         },
2068         {0, 0, 1, 0,            /* 0x1c */
2069                 {{2, 4},
2070                 {0, 0},
2071                 {0, 0},
2072                 {0, 0}
2073                 }
2074         },
2075         {1, 0, 2, 0,            /* 0x1d */
2076                 {{0, 0},
2077                 {2, 4},
2078                 {0, 0},
2079                 {0, 0}
2080                 }
2081         },
2082         {0, 0, 1, 0,            /* 0x1e */
2083                 {{1, 4},
2084                 {0, 0},
2085                 {0, 0},
2086                 {0, 0}
2087                 }
2088         },
2089         {1, 0, 1, 0,            /* 0x1f */
2090                 {{0, 4},
2091                 {0, 0},
2092                 {0, 0},
2093                 {0, 0}
2094                 }
2095         },
2096         {0, 0, 1, 0,            /* 0x20 */
2097                 {{5, 5},
2098                 {0, 0},
2099                 {0, 0},
2100                 {0, 0}
2101                 }
2102         },
2103         {1, 0, 2, 0,            /* 0x21 */
2104                 {{0, 0},
2105                 {5, 5},
2106                 {0, 0},
2107                 {0, 0}
2108                 }
2109         },
2110         {0, 0, 2, 0,            /* 0x22 */
2111                 {{1, 1},
2112                 {5, 5},
2113                 {0, 0},
2114                 {0, 0}
2115                 }
2116         },
2117         {1, 0, 2, 0,            /* 0x23 */
2118                 {{0, 1},
2119                 {5, 5},
2120                 {0, 0},
2121                 {0, 0}
2122                 }
2123         },
2124         {0, 0, 2, 0,            /* 0x24 */
2125                 {{2, 2},
2126                 {5, 5},
2127                 {0, 0},
2128                 {0, 0}
2129                 }
2130         },
2131         {1, 0, 3, 0,            /* 0x25 */
2132                 {{0, 0},
2133                 {2, 2},
2134                 {5, 5},
2135                 {0, 0}
2136                 }
2137         },
2138         {0, 0, 2, 0,            /* 0x26 */
2139                 {{1, 2},
2140                 {5, 5},
2141                 {0, 0},
2142                 {0, 0}
2143                 }
2144         },
2145         {1, 0, 2, 0,            /* 0x27 */
2146                 {{0, 2},
2147                 {5, 5},
2148                 {0, 0},
2149                 {0, 0}
2150                 }
2151         },
2152         {0, 0, 2, 0,            /* 0x28 */
2153                 {{3, 3},
2154                 {5, 5},
2155                 {0, 0},
2156                 {0, 0}
2157                 }
2158         },
2159         {1, 0, 3, 0,            /* 0x29 */
2160                 {{0, 0},
2161                 {3, 3},
2162                 {5, 5},
2163                 {0, 0}
2164                 }
2165         },
2166         {0, 0, 3, 0,            /* 0x2a */
2167                 {{1, 1},
2168                 {3, 3},
2169                 {5, 5},
2170                 {0, 0}
2171                 }
2172         },
2173         {1, 0, 3, 0,            /* 0x2b */
2174                 {{0, 1},
2175                 {3, 3},
2176                 {5, 5},
2177                 {0, 0}
2178                 }
2179         },
2180         {0, 0, 2, 0,            /* 0x2c */
2181                 {{2, 3},
2182                 {5, 5},
2183                 {0, 0},
2184                 {0, 0}
2185                 }
2186         },
2187         {1, 0, 3, 0,            /* 0x2d */
2188                 {{0, 0},
2189                 {2, 3},
2190                 {5, 5},
2191                 {0, 0}
2192                 }
2193         },
2194         {0, 0, 2, 0,            /* 0x2e */
2195                 {{1, 3},
2196                 {5, 5},
2197                 {0, 0},
2198                 {0, 0}
2199                 }
2200         },
2201         {1, 0, 2, 0,            /* 0x2f */
2202                 {{0, 3},
2203                 {5, 5},
2204                 {0, 0},
2205                 {0, 0}
2206                 }
2207         },
2208         {0, 0, 1, 0,            /* 0x30 */
2209                 {{4, 5},
2210                 {0, 0},
2211                 {0, 0},
2212                 {0, 0}
2213                 }
2214         },
2215         {1, 0, 2, 0,            /* 0x31 */
2216                 {{0, 0},
2217                 {4, 5},
2218                 {0, 0},
2219                 {0, 0}
2220                 }
2221         },
2222         {0, 0, 2, 0,            /* 0x32 */
2223                 {{1, 1},
2224                 {4, 5},
2225                 {0, 0},
2226                 {0, 0}
2227                 }
2228         },
2229         {1, 0, 2, 0,            /* 0x33 */
2230                 {{0, 1},
2231                 {4, 5},
2232                 {0, 0},
2233                 {0, 0}
2234                 }
2235         },
2236         {0, 0, 2, 0,            /* 0x34 */
2237                 {{2, 2},
2238                 {4, 5},
2239                 {0, 0},
2240                 {0, 0}
2241                 }
2242         },
2243         {1, 0, 3, 0,            /* 0x35 */
2244                 {{0, 0},
2245                 {2, 2},
2246                 {4, 5},
2247                 {0, 0}
2248                 }
2249         },
2250         {0, 0, 2, 0,            /* 0x36 */
2251                 {{1, 2},
2252                 {4, 5},
2253                 {0, 0},
2254                 {0, 0}
2255                 }
2256         },
2257         {1, 0, 2, 0,            /* 0x37 */
2258                 {{0, 2},
2259                 {4, 5},
2260                 {0, 0},
2261                 {0, 0}
2262                 }
2263         },
2264         {0, 0, 1, 0,            /* 0x38 */
2265                 {{3, 5},
2266                 {0, 0},
2267                 {0, 0},
2268                 {0, 0}
2269                 }
2270         },
2271         {1, 0, 2, 0,            /* 0x39 */
2272                 {{0, 0},
2273                 {3, 5},
2274                 {0, 0},
2275                 {0, 0}
2276                 }
2277         },
2278         {0, 0, 2, 0,            /* 0x3a */
2279                 {{1, 1},
2280                 {3, 5},
2281                 {0, 0},
2282                 {0, 0}
2283                 }
2284         },
2285         {1, 0, 2, 0,            /* 0x3b */
2286                 {{0, 1},
2287                 {3, 5},
2288                 {0, 0},
2289                 {0, 0}
2290                 }
2291         },
2292         {0, 0, 1, 0,            /* 0x3c */
2293                 {{2, 5},
2294                 {0, 0},
2295                 {0, 0},
2296                 {0, 0}
2297                 }
2298         },
2299         {1, 0, 2, 0,            /* 0x3d */
2300                 {{0, 0},
2301                 {2, 5},
2302                 {0, 0},
2303                 {0, 0}
2304                 }
2305         },
2306         {0, 0, 1, 0,            /* 0x3e */
2307                 {{1, 5},
2308                 {0, 0},
2309                 {0, 0},
2310                 {0, 0}
2311                 }
2312         },
2313         {1, 0, 1, 0,            /* 0x3f */
2314                 {{0, 5},
2315                 {0, 0},
2316                 {0, 0},
2317                 {0, 0}
2318                 }
2319         },
2320         {0, 0, 1, 0,            /* 0x40 */
2321                 {{6, 6},
2322                 {0, 0},
2323                 {0, 0},
2324                 {0, 0}
2325                 }
2326         },
2327         {1, 0, 2, 0,            /* 0x41 */
2328                 {{0, 0},
2329                 {6, 6},
2330                 {0, 0},
2331                 {0, 0}
2332                 }
2333         },
2334         {0, 0, 2, 0,            /* 0x42 */
2335                 {{1, 1},
2336                 {6, 6},
2337                 {0, 0},
2338                 {0, 0}
2339                 }
2340         },
2341         {1, 0, 2, 0,            /* 0x43 */
2342                 {{0, 1},
2343                 {6, 6},
2344                 {0, 0},
2345                 {0, 0}
2346                 }
2347         },
2348         {0, 0, 2, 0,            /* 0x44 */
2349                 {{2, 2},
2350                 {6, 6},
2351                 {0, 0},
2352                 {0, 0}
2353                 }
2354         },
2355         {1, 0, 3, 0,            /* 0x45 */
2356                 {{0, 0},
2357                 {2, 2},
2358                 {6, 6},
2359                 {0, 0}
2360                 }
2361         },
2362         {0, 0, 2, 0,            /* 0x46 */
2363                 {{1, 2},
2364                 {6, 6},
2365                 {0, 0},
2366                 {0, 0}
2367                 }
2368         },
2369         {1, 0, 2, 0,            /* 0x47 */
2370                 {{0, 2},
2371                 {6, 6},
2372                 {0, 0},
2373                 {0, 0}
2374                 }
2375         },
2376         {0, 0, 2, 0,            /* 0x48 */
2377                 {{3, 3},
2378                 {6, 6},
2379                 {0, 0},
2380                 {0, 0}
2381                 }
2382         },
2383         {1, 0, 3, 0,            /* 0x49 */
2384                 {{0, 0},
2385                 {3, 3},
2386                 {6, 6},
2387                 {0, 0}
2388                 }
2389         },
2390         {0, 0, 3, 0,            /* 0x4a */
2391                 {{1, 1},
2392                 {3, 3},
2393                 {6, 6},
2394                 {0, 0}
2395                 }
2396         },
2397         {1, 0, 3, 0,            /* 0x4b */
2398                 {{0, 1},
2399                 {3, 3},
2400                 {6, 6},
2401                 {0, 0}
2402                 }
2403         },
2404         {0, 0, 2, 0,            /* 0x4c */
2405                 {{2, 3},
2406                 {6, 6},
2407                 {0, 0},
2408                 {0, 0}
2409                 }
2410         },
2411         {1, 0, 3, 0,            /* 0x4d */
2412                 {{0, 0},
2413                 {2, 3},
2414                 {6, 6},
2415                 {0, 0}
2416                 }
2417         },
2418         {0, 0, 2, 0,            /* 0x4e */
2419                 {{1, 3},
2420                 {6, 6},
2421                 {0, 0},
2422                 {0, 0}
2423                 }
2424         },
2425         {1, 0, 2, 0,            /* 0x4f */
2426                 {{0, 3},
2427                 {6, 6},
2428                 {0, 0},
2429                 {0, 0}
2430                 }
2431         },
2432         {0, 0, 2, 0,            /* 0x50 */
2433                 {{4, 4},
2434                 {6, 6},
2435                 {0, 0},
2436                 {0, 0}
2437                 }
2438         },
2439         {1, 0, 3, 0,            /* 0x51 */
2440                 {{0, 0},
2441                 {4, 4},
2442                 {6, 6},
2443                 {0, 0}
2444                 }
2445         },
2446         {0, 0, 3, 0,            /* 0x52 */
2447                 {{1, 1},
2448                 {4, 4},
2449                 {6, 6},
2450                 {0, 0}
2451                 }
2452         },
2453         {1, 0, 3, 0,            /* 0x53 */
2454                 {{0, 1},
2455                 {4, 4},
2456                 {6, 6},
2457                 {0, 0}
2458                 }
2459         },
2460         {0, 0, 3, 0,            /* 0x54 */
2461                 {{2, 2},
2462                 {4, 4},
2463                 {6, 6},
2464                 {0, 0}
2465                 }
2466         },
2467         {1, 0, 4, 0,            /* 0x55 */
2468                 {{0, 0},
2469                 {2, 2},
2470                 {4, 4},
2471                 {6, 6}
2472                 }
2473         },
2474         {0, 0, 3, 0,            /* 0x56 */
2475                 {{1, 2},
2476                 {4, 4},
2477                 {6, 6},
2478                 {0, 0}
2479                 }
2480         },
2481         {1, 0, 3, 0,            /* 0x57 */
2482                 {{0, 2},
2483                 {4, 4},
2484                 {6, 6},
2485                 {0, 0}
2486                 }
2487         },
2488         {0, 0, 2, 0,            /* 0x58 */
2489                 {{3, 4},
2490                 {6, 6},
2491                 {0, 0},
2492                 {0, 0}
2493                 }
2494         },
2495         {1, 0, 3, 0,            /* 0x59 */
2496                 {{0, 0},
2497                 {3, 4},
2498                 {6, 6},
2499                 {0, 0}
2500                 }
2501         },
2502         {0, 0, 3, 0,            /* 0x5a */
2503                 {{1, 1},
2504                 {3, 4},
2505                 {6, 6},
2506                 {0, 0}
2507                 }
2508         },
2509         {1, 0, 3, 0,            /* 0x5b */
2510                 {{0, 1},
2511                 {3, 4},
2512                 {6, 6},
2513                 {0, 0}
2514                 }
2515         },
2516         {0, 0, 2, 0,            /* 0x5c */
2517                 {{2, 4},
2518                 {6, 6},
2519                 {0, 0},
2520                 {0, 0}
2521                 }
2522         },
2523         {1, 0, 3, 0,            /* 0x5d */
2524                 {{0, 0},
2525                 {2, 4},
2526                 {6, 6},
2527                 {0, 0}
2528                 }
2529         },
2530         {0, 0, 2, 0,            /* 0x5e */
2531                 {{1, 4},
2532                 {6, 6},
2533                 {0, 0},
2534                 {0, 0}
2535                 }
2536         },
2537         {1, 0, 2, 0,            /* 0x5f */
2538                 {{0, 4},
2539                 {6, 6},
2540                 {0, 0},
2541                 {0, 0}
2542                 }
2543         },
2544         {0, 0, 1, 0,            /* 0x60 */
2545                 {{5, 6},
2546                 {0, 0},
2547                 {0, 0},
2548                 {0, 0}
2549                 }
2550         },
2551         {1, 0, 2, 0,            /* 0x61 */
2552                 {{0, 0},
2553                 {5, 6},
2554                 {0, 0},
2555                 {0, 0}
2556                 }
2557         },
2558         {0, 0, 2, 0,            /* 0x62 */
2559                 {{1, 1},
2560                 {5, 6},
2561                 {0, 0},
2562                 {0, 0}
2563                 }
2564         },
2565         {1, 0, 2, 0,            /* 0x63 */
2566                 {{0, 1},
2567                 {5, 6},
2568                 {0, 0},
2569                 {0, 0}
2570                 }
2571         },
2572         {0, 0, 2, 0,            /* 0x64 */
2573                 {{2, 2},
2574                 {5, 6},
2575                 {0, 0},
2576                 {0, 0}
2577                 }
2578         },
2579         {1, 0, 3, 0,            /* 0x65 */
2580                 {{0, 0},
2581                 {2, 2},
2582                 {5, 6},
2583                 {0, 0}
2584                 }
2585         },
2586         {0, 0, 2, 0,            /* 0x66 */
2587                 {{1, 2},
2588                 {5, 6},
2589                 {0, 0},
2590                 {0, 0}
2591                 }
2592         },
2593         {1, 0, 2, 0,            /* 0x67 */
2594                 {{0, 2},
2595                 {5, 6},
2596                 {0, 0},
2597                 {0, 0}
2598                 }
2599         },
2600         {0, 0, 2, 0,            /* 0x68 */
2601                 {{3, 3},
2602                 {5, 6},
2603                 {0, 0},
2604                 {0, 0}
2605                 }
2606         },
2607         {1, 0, 3, 0,            /* 0x69 */
2608                 {{0, 0},
2609                 {3, 3},
2610                 {5, 6},
2611                 {0, 0}
2612                 }
2613         },
2614         {0, 0, 3, 0,            /* 0x6a */
2615                 {{1, 1},
2616                 {3, 3},
2617                 {5, 6},
2618                 {0, 0}
2619                 }
2620         },
2621         {1, 0, 3, 0,            /* 0x6b */
2622                 {{0, 1},
2623                 {3, 3},
2624                 {5, 6},
2625                 {0, 0}
2626                 }
2627         },
2628         {0, 0, 2, 0,            /* 0x6c */
2629                 {{2, 3},
2630                 {5, 6},
2631                 {0, 0},
2632                 {0, 0}
2633                 }
2634         },
2635         {1, 0, 3, 0,            /* 0x6d */
2636                 {{0, 0},
2637                 {2, 3},
2638                 {5, 6},
2639                 {0, 0}
2640                 }
2641         },
2642         {0, 0, 2, 0,            /* 0x6e */
2643                 {{1, 3},
2644                 {5, 6},
2645                 {0, 0},
2646                 {0, 0}
2647                 }
2648         },
2649         {1, 0, 2, 0,            /* 0x6f */
2650                 {{0, 3},
2651                 {5, 6},
2652                 {0, 0},
2653                 {0, 0}
2654                 }
2655         },
2656         {0, 0, 1, 0,            /* 0x70 */
2657                 {{4, 6},
2658                 {0, 0},
2659                 {0, 0},
2660                 {0, 0}
2661                 }
2662         },
2663         {1, 0, 2, 0,            /* 0x71 */
2664                 {{0, 0},
2665                 {4, 6},
2666                 {0, 0},
2667                 {0, 0}
2668                 }
2669         },
2670         {0, 0, 2, 0,            /* 0x72 */
2671                 {{1, 1},
2672                 {4, 6},
2673                 {0, 0},
2674                 {0, 0}
2675                 }
2676         },
2677         {1, 0, 2, 0,            /* 0x73 */
2678                 {{0, 1},
2679                 {4, 6},
2680                 {0, 0},
2681                 {0, 0}
2682                 }
2683         },
2684         {0, 0, 2, 0,            /* 0x74 */
2685                 {{2, 2},
2686                 {4, 6},
2687                 {0, 0},
2688                 {0, 0}
2689                 }
2690         },
2691         {1, 0, 3, 0,            /* 0x75 */
2692                 {{0, 0},
2693                 {2, 2},
2694                 {4, 6},
2695                 {0, 0}
2696                 }
2697         },
2698         {0, 0, 2, 0,            /* 0x76 */
2699                 {{1, 2},
2700                 {4, 6},
2701                 {0, 0},
2702                 {0, 0}
2703                 }
2704         },
2705         {1, 0, 2, 0,            /* 0x77 */
2706                 {{0, 2},
2707                 {4, 6},
2708                 {0, 0},
2709                 {0, 0}
2710                 }
2711         },
2712         {0, 0, 1, 0,            /* 0x78 */
2713                 {{3, 6},
2714                 {0, 0},
2715                 {0, 0},
2716                 {0, 0}
2717                 }
2718         },
2719         {1, 0, 2, 0,            /* 0x79 */
2720                 {{0, 0},
2721                 {3, 6},
2722                 {0, 0},
2723                 {0, 0}
2724                 }
2725         },
2726         {0, 0, 2, 0,            /* 0x7a */
2727                 {{1, 1},
2728                 {3, 6},
2729                 {0, 0},
2730                 {0, 0}
2731                 }
2732         },
2733         {1, 0, 2, 0,            /* 0x7b */
2734                 {{0, 1},
2735                 {3, 6},
2736                 {0, 0},
2737                 {0, 0}
2738                 }
2739         },
2740         {0, 0, 1, 0,            /* 0x7c */
2741                 {{2, 6},
2742                 {0, 0},
2743                 {0, 0},
2744                 {0, 0}
2745                 }
2746         },
2747         {1, 0, 2, 0,            /* 0x7d */
2748                 {{0, 0},
2749                 {2, 6},
2750                 {0, 0},
2751                 {0, 0}
2752                 }
2753         },
2754         {0, 0, 1, 0,            /* 0x7e */
2755                 {{1, 6},
2756                 {0, 0},
2757                 {0, 0},
2758                 {0, 0}
2759                 }
2760         },
2761         {1, 0, 1, 0,            /* 0x7f */
2762                 {{0, 6},
2763                 {0, 0},
2764                 {0, 0},
2765                 {0, 0}
2766                 }
2767         },
2768         {0, 1, 1, 0,            /* 0x80 */
2769                 {{7, 7},
2770                 {0, 0},
2771                 {0, 0},
2772                 {0, 0}
2773                 }
2774         },
2775         {1, 1, 2, 0,            /* 0x81 */
2776                 {{0, 0},
2777                 {7, 7},
2778                 {0, 0},
2779                 {0, 0}
2780                 }
2781         },
2782         {0, 1, 2, 0,            /* 0x82 */
2783                 {{1, 1},
2784                 {7, 7},
2785                 {0, 0},
2786                 {0, 0}
2787                 }
2788         },
2789         {1, 1, 2, 0,            /* 0x83 */
2790                 {{0, 1},
2791                 {7, 7},
2792                 {0, 0},
2793                 {0, 0}
2794                 }
2795         },
2796         {0, 1, 2, 0,            /* 0x84 */
2797                 {{2, 2},
2798                 {7, 7},
2799                 {0, 0},
2800                 {0, 0}
2801                 }
2802         },
2803         {1, 1, 3, 0,            /* 0x85 */
2804                 {{0, 0},
2805                 {2, 2},
2806                 {7, 7},
2807                 {0, 0}
2808                 }
2809         },
2810         {0, 1, 2, 0,            /* 0x86 */
2811                 {{1, 2},
2812                 {7, 7},
2813                 {0, 0},
2814                 {0, 0}
2815                 }
2816         },
2817         {1, 1, 2, 0,            /* 0x87 */
2818                 {{0, 2},
2819                 {7, 7},
2820                 {0, 0},
2821                 {0, 0}
2822                 }
2823         },
2824         {0, 1, 2, 0,            /* 0x88 */
2825                 {{3, 3},
2826                 {7, 7},
2827                 {0, 0},
2828                 {0, 0}
2829                 }
2830         },
2831         {1, 1, 3, 0,            /* 0x89 */
2832                 {{0, 0},
2833                 {3, 3},
2834                 {7, 7},
2835                 {0, 0}
2836                 }
2837         },
2838         {0, 1, 3, 0,            /* 0x8a */
2839                 {{1, 1},
2840                 {3, 3},
2841                 {7, 7},
2842                 {0, 0}
2843                 }
2844         },
2845         {1, 1, 3, 0,            /* 0x8b */
2846                 {{0, 1},
2847                 {3, 3},
2848                 {7, 7},
2849                 {0, 0}
2850                 }
2851         },
2852         {0, 1, 2, 0,            /* 0x8c */
2853                 {{2, 3},
2854                 {7, 7},
2855                 {0, 0},
2856                 {0, 0}
2857                 }
2858         },
2859         {1, 1, 3, 0,            /* 0x8d */
2860                 {{0, 0},
2861                 {2, 3},
2862                 {7, 7},
2863                 {0, 0}
2864                 }
2865         },
2866         {0, 1, 2, 0,            /* 0x8e */
2867                 {{1, 3},
2868                 {7, 7},
2869                 {0, 0},
2870                 {0, 0}
2871                 }
2872         },
2873         {1, 1, 2, 0,            /* 0x8f */
2874                 {{0, 3},
2875                 {7, 7},
2876                 {0, 0},
2877                 {0, 0}
2878                 }
2879         },
2880         {0, 1, 2, 0,            /* 0x90 */
2881                 {{4, 4},
2882                 {7, 7},
2883                 {0, 0},
2884                 {0, 0}
2885                 }
2886         },
2887         {1, 1, 3, 0,            /* 0x91 */
2888                 {{0, 0},
2889                 {4, 4},
2890                 {7, 7},
2891                 {0, 0}
2892                 }
2893         },
2894         {0, 1, 3, 0,            /* 0x92 */
2895                 {{1, 1},
2896                 {4, 4},
2897                 {7, 7},
2898                 {0, 0}
2899                 }
2900         },
2901         {1, 1, 3, 0,            /* 0x93 */
2902                 {{0, 1},
2903                 {4, 4},
2904                 {7, 7},
2905                 {0, 0}
2906                 }
2907         },
2908         {0, 1, 3, 0,            /* 0x94 */
2909                 {{2, 2},
2910                 {4, 4},
2911                 {7, 7},
2912                 {0, 0}
2913                 }
2914         },
2915         {1, 1, 4, 0,            /* 0x95 */
2916                 {{0, 0},
2917                 {2, 2},
2918                 {4, 4},
2919                 {7, 7}
2920                 }
2921         },
2922         {0, 1, 3, 0,            /* 0x96 */
2923                 {{1, 2},
2924                 {4, 4},
2925                 {7, 7},
2926                 {0, 0}
2927                 }
2928         },
2929         {1, 1, 3, 0,            /* 0x97 */
2930                 {{0, 2},
2931                 {4, 4},
2932                 {7, 7},
2933                 {0, 0}
2934                 }
2935         },
2936         {0, 1, 2, 0,            /* 0x98 */
2937                 {{3, 4},
2938                 {7, 7},
2939                 {0, 0},
2940                 {0, 0}
2941                 }
2942         },
2943         {1, 1, 3, 0,            /* 0x99 */
2944                 {{0, 0},
2945                 {3, 4},
2946                 {7, 7},
2947                 {0, 0}
2948                 }
2949         },
2950         {0, 1, 3, 0,            /* 0x9a */
2951                 {{1, 1},
2952                 {3, 4},
2953                 {7, 7},
2954                 {0, 0}
2955                 }
2956         },
2957         {1, 1, 3, 0,            /* 0x9b */
2958                 {{0, 1},
2959                 {3, 4},
2960                 {7, 7},
2961                 {0, 0}
2962                 }
2963         },
2964         {0, 1, 2, 0,            /* 0x9c */
2965                 {{2, 4},
2966                 {7, 7},
2967                 {0, 0},
2968                 {0, 0}
2969                 }
2970         },
2971         {1, 1, 3, 0,            /* 0x9d */
2972                 {{0, 0},
2973                 {2, 4},
2974                 {7, 7},
2975                 {0, 0}
2976                 }
2977         },
2978         {0, 1, 2, 0,            /* 0x9e */
2979                 {{1, 4},
2980                 {7, 7},
2981                 {0, 0},
2982                 {0, 0}
2983                 }
2984         },
2985         {1, 1, 2, 0,            /* 0x9f */
2986                 {{0, 4},
2987                 {7, 7},
2988                 {0, 0},
2989                 {0, 0}
2990                 }
2991         },
2992         {0, 1, 2, 0,            /* 0xa0 */
2993                 {{5, 5},
2994                 {7, 7},
2995                 {0, 0},
2996                 {0, 0}
2997                 }
2998         },
2999         {1, 1, 3, 0,            /* 0xa1 */
3000                 {{0, 0},
3001                 {5, 5},
3002                 {7, 7},
3003                 {0, 0}
3004                 }
3005         },
3006         {0, 1, 3, 0,            /* 0xa2 */
3007                 {{1, 1},
3008                 {5, 5},
3009                 {7, 7},
3010                 {0, 0}
3011                 }
3012         },
3013         {1, 1, 3, 0,            /* 0xa3 */
3014                 {{0, 1},
3015                 {5, 5},
3016                 {7, 7},
3017                 {0, 0}
3018                 }
3019         },
3020         {0, 1, 3, 0,            /* 0xa4 */
3021                 {{2, 2},
3022                 {5, 5},
3023                 {7, 7},
3024                 {0, 0}
3025                 }
3026         },
3027         {1, 1, 4, 0,            /* 0xa5 */
3028                 {{0, 0},
3029                 {2, 2},
3030                 {5, 5},
3031                 {7, 7}
3032                 }
3033         },
3034         {0, 1, 3, 0,            /* 0xa6 */
3035                 {{1, 2},
3036                 {5, 5},
3037                 {7, 7},
3038                 {0, 0}
3039                 }
3040         },
3041         {1, 1, 3, 0,            /* 0xa7 */
3042                 {{0, 2},
3043                 {5, 5},
3044                 {7, 7},
3045                 {0, 0}
3046                 }
3047         },
3048         {0, 1, 3, 0,            /* 0xa8 */
3049                 {{3, 3},
3050                 {5, 5},
3051                 {7, 7},
3052                 {0, 0}
3053                 }
3054         },
3055         {1, 1, 4, 0,            /* 0xa9 */
3056                 {{0, 0},
3057                 {3, 3},
3058                 {5, 5},
3059                 {7, 7}
3060                 }
3061         },
3062         {0, 1, 4, 0,            /* 0xaa */
3063                 {{1, 1},
3064                 {3, 3},
3065                 {5, 5},
3066                 {7, 7}
3067                 }
3068         },
3069         {1, 1, 4, 0,            /* 0xab */
3070                 {{0, 1},
3071                 {3, 3},
3072                 {5, 5},
3073                 {7, 7}
3074                 }
3075         },
3076         {0, 1, 3, 0,            /* 0xac */
3077                 {{2, 3},
3078                 {5, 5},
3079                 {7, 7},
3080                 {0, 0}
3081                 }
3082         },
3083         {1, 1, 4, 0,            /* 0xad */
3084                 {{0, 0},
3085                 {2, 3},
3086                 {5, 5},
3087                 {7, 7}
3088                 }
3089         },
3090         {0, 1, 3, 0,            /* 0xae */
3091                 {{1, 3},
3092                 {5, 5},
3093                 {7, 7},
3094                 {0, 0}
3095                 }
3096         },
3097         {1, 1, 3, 0,            /* 0xaf */
3098                 {{0, 3},
3099                 {5, 5},
3100                 {7, 7},
3101                 {0, 0}
3102                 }
3103         },
3104         {0, 1, 2, 0,            /* 0xb0 */
3105                 {{4, 5},
3106                 {7, 7},
3107                 {0, 0},
3108                 {0, 0}
3109                 }
3110         },
3111         {1, 1, 3, 0,            /* 0xb1 */
3112                 {{0, 0},
3113                 {4, 5},
3114                 {7, 7},
3115                 {0, 0}
3116                 }
3117         },
3118         {0, 1, 3, 0,            /* 0xb2 */
3119                 {{1, 1},
3120                 {4, 5},
3121                 {7, 7},
3122                 {0, 0}
3123                 }
3124         },
3125         {1, 1, 3, 0,            /* 0xb3 */
3126                 {{0, 1},
3127                 {4, 5},
3128                 {7, 7},
3129                 {0, 0}
3130                 }
3131         },
3132         {0, 1, 3, 0,            /* 0xb4 */
3133                 {{2, 2},
3134                 {4, 5},
3135                 {7, 7},
3136                 {0, 0}
3137                 }
3138         },
3139         {1, 1, 4, 0,            /* 0xb5 */
3140                 {{0, 0},
3141                 {2, 2},
3142                 {4, 5},
3143                 {7, 7}
3144                 }
3145         },
3146         {0, 1, 3, 0,            /* 0xb6 */
3147                 {{1, 2},
3148                 {4, 5},
3149                 {7, 7},
3150                 {0, 0}
3151                 }
3152         },
3153         {1, 1, 3, 0,            /* 0xb7 */
3154                 {{0, 2},
3155                 {4, 5},
3156                 {7, 7},
3157                 {0, 0}
3158                 }
3159         },
3160         {0, 1, 2, 0,            /* 0xb8 */
3161                 {{3, 5},
3162                 {7, 7},
3163                 {0, 0},
3164                 {0, 0}
3165                 }
3166         },
3167         {1, 1, 3, 0,            /* 0xb9 */
3168                 {{0, 0},
3169                 {3, 5},
3170                 {7, 7},
3171                 {0, 0}
3172                 }
3173         },
3174         {0, 1, 3, 0,            /* 0xba */
3175                 {{1, 1},
3176                 {3, 5},
3177                 {7, 7},
3178                 {0, 0}
3179                 }
3180         },
3181         {1, 1, 3, 0,            /* 0xbb */
3182                 {{0, 1},
3183                 {3, 5},
3184                 {7, 7},
3185                 {0, 0}
3186                 }
3187         },
3188         {0, 1, 2, 0,            /* 0xbc */
3189                 {{2, 5},
3190                 {7, 7},
3191                 {0, 0},
3192                 {0, 0}
3193                 }
3194         },
3195         {1, 1, 3, 0,            /* 0xbd */
3196                 {{0, 0},
3197                 {2, 5},
3198                 {7, 7},
3199                 {0, 0}
3200                 }
3201         },
3202         {0, 1, 2, 0,            /* 0xbe */
3203                 {{1, 5},
3204                 {7, 7},
3205                 {0, 0},
3206                 {0, 0}
3207                 }
3208         },
3209         {1, 1, 2, 0,            /* 0xbf */
3210                 {{0, 5},
3211                 {7, 7},
3212                 {0, 0},
3213                 {0, 0}
3214                 }
3215         },
3216         {0, 1, 1, 0,            /* 0xc0 */
3217                 {{6, 7},
3218                 {0, 0},
3219                 {0, 0},
3220                 {0, 0}
3221                 }
3222         },
3223         {1, 1, 2, 0,            /* 0xc1 */
3224                 {{0, 0},
3225                 {6, 7},
3226                 {0, 0},
3227                 {0, 0}
3228                 }
3229         },
3230         {0, 1, 2, 0,            /* 0xc2 */
3231                 {{1, 1},
3232                 {6, 7},
3233                 {0, 0},
3234                 {0, 0}
3235                 }
3236         },
3237         {1, 1, 2, 0,            /* 0xc3 */
3238                 {{0, 1},
3239                 {6, 7},
3240                 {0, 0},
3241                 {0, 0}
3242                 }
3243         },
3244         {0, 1, 2, 0,            /* 0xc4 */
3245                 {{2, 2},
3246                 {6, 7},
3247                 {0, 0},
3248                 {0, 0}
3249                 }
3250         },
3251         {1, 1, 3, 0,            /* 0xc5 */
3252                 {{0, 0},
3253                 {2, 2},
3254                 {6, 7},
3255                 {0, 0}
3256                 }
3257         },
3258         {0, 1, 2, 0,            /* 0xc6 */
3259                 {{1, 2},
3260                 {6, 7},
3261                 {0, 0},
3262                 {0, 0}
3263                 }
3264         },
3265         {1, 1, 2, 0,            /* 0xc7 */
3266                 {{0, 2},
3267                 {6, 7},
3268                 {0, 0},
3269                 {0, 0}
3270                 }
3271         },
3272         {0, 1, 2, 0,            /* 0xc8 */
3273                 {{3, 3},
3274                 {6, 7},
3275                 {0, 0},
3276                 {0, 0}
3277                 }
3278         },
3279         {1, 1, 3, 0,            /* 0xc9 */
3280                 {{0, 0},
3281                 {3, 3},
3282                 {6, 7},
3283                 {0, 0}
3284                 }
3285         },
3286         {0, 1, 3, 0,            /* 0xca */
3287                 {{1, 1},
3288                 {3, 3},
3289                 {6, 7},
3290                 {0, 0}
3291                 }
3292         },
3293         {1, 1, 3, 0,            /* 0xcb */
3294                 {{0, 1},
3295                 {3, 3},
3296                 {6, 7},
3297                 {0, 0}
3298                 }
3299         },
3300         {0, 1, 2, 0,            /* 0xcc */
3301                 {{2, 3},
3302                 {6, 7},
3303                 {0, 0},
3304                 {0, 0}
3305                 }
3306         },
3307         {1, 1, 3, 0,            /* 0xcd */
3308                 {{0, 0},
3309                 {2, 3},
3310                 {6, 7},
3311                 {0, 0}
3312                 }
3313         },
3314         {0, 1, 2, 0,            /* 0xce */
3315                 {{1, 3},
3316                 {6, 7},
3317                 {0, 0},
3318                 {0, 0}
3319                 }
3320         },
3321         {1, 1, 2, 0,            /* 0xcf */
3322                 {{0, 3},
3323                 {6, 7},
3324                 {0, 0},
3325                 {0, 0}
3326                 }
3327         },
3328         {0, 1, 2, 0,            /* 0xd0 */
3329                 {{4, 4},
3330                 {6, 7},
3331                 {0, 0},
3332                 {0, 0}
3333                 }
3334         },
3335         {1, 1, 3, 0,            /* 0xd1 */
3336                 {{0, 0},
3337                 {4, 4},
3338                 {6, 7},
3339                 {0, 0}
3340                 }
3341         },
3342         {0, 1, 3, 0,            /* 0xd2 */
3343                 {{1, 1},
3344                 {4, 4},
3345                 {6, 7},
3346                 {0, 0}
3347                 }
3348         },
3349         {1, 1, 3, 0,            /* 0xd3 */
3350                 {{0, 1},
3351                 {4, 4},
3352                 {6, 7},
3353                 {0, 0}
3354                 }
3355         },
3356         {0, 1, 3, 0,            /* 0xd4 */
3357                 {{2, 2},
3358                 {4, 4},
3359                 {6, 7},
3360                 {0, 0}
3361                 }
3362         },
3363         {1, 1, 4, 0,            /* 0xd5 */
3364                 {{0, 0},
3365                 {2, 2},
3366                 {4, 4},
3367                 {6, 7}
3368                 }
3369         },
3370         {0, 1, 3, 0,            /* 0xd6 */
3371                 {{1, 2},
3372                 {4, 4},
3373                 {6, 7},
3374                 {0, 0}
3375                 }
3376         },
3377         {1, 1, 3, 0,            /* 0xd7 */
3378                 {{0, 2},
3379                 {4, 4},
3380                 {6, 7},
3381                 {0, 0}
3382                 }
3383         },
3384         {0, 1, 2, 0,            /* 0xd8 */
3385                 {{3, 4},
3386                 {6, 7},
3387                 {0, 0},
3388                 {0, 0}
3389                 }
3390         },
3391         {1, 1, 3, 0,            /* 0xd9 */
3392                 {{0, 0},
3393                 {3, 4},
3394                 {6, 7},
3395                 {0, 0}
3396                 }
3397         },
3398         {0, 1, 3, 0,            /* 0xda */
3399                 {{1, 1},
3400                 {3, 4},
3401                 {6, 7},
3402                 {0, 0}
3403                 }
3404         },
3405         {1, 1, 3, 0,            /* 0xdb */
3406                 {{0, 1},
3407                 {3, 4},
3408                 {6, 7},
3409                 {0, 0}
3410                 }
3411         },
3412         {0, 1, 2, 0,            /* 0xdc */
3413                 {{2, 4},
3414                 {6, 7},
3415                 {0, 0},
3416                 {0, 0}
3417                 }
3418         },
3419         {1, 1, 3, 0,            /* 0xdd */
3420                 {{0, 0},
3421                 {2, 4},
3422                 {6, 7},
3423                 {0, 0}
3424                 }
3425         },
3426         {0, 1, 2, 0,            /* 0xde */
3427                 {{1, 4},
3428                 {6, 7},
3429                 {0, 0},
3430                 {0, 0}
3431                 }
3432         },
3433         {1, 1, 2, 0,            /* 0xdf */
3434                 {{0, 4},
3435                 {6, 7},
3436                 {0, 0},
3437                 {0, 0}
3438                 }
3439         },
3440         {0, 1, 1, 0,            /* 0xe0 */
3441                 {{5, 7},
3442                 {0, 0},
3443                 {0, 0},
3444                 {0, 0}
3445                 }
3446         },
3447         {1, 1, 2, 0,            /* 0xe1 */
3448                 {{0, 0},
3449                 {5, 7},
3450                 {0, 0},
3451                 {0, 0}
3452                 }
3453         },
3454         {0, 1, 2, 0,            /* 0xe2 */
3455                 {{1, 1},
3456                 {5, 7},
3457                 {0, 0},
3458                 {0, 0}
3459                 }
3460         },
3461         {1, 1, 2, 0,            /* 0xe3 */
3462                 {{0, 1},
3463                 {5, 7},
3464                 {0, 0},
3465                 {0, 0}
3466                 }
3467         },
3468         {0, 1, 2, 0,            /* 0xe4 */
3469                 {{2, 2},
3470                 {5, 7},
3471                 {0, 0},
3472                 {0, 0}
3473                 }
3474         },
3475         {1, 1, 3, 0,            /* 0xe5 */
3476                 {{0, 0},
3477                 {2, 2},
3478                 {5, 7},
3479                 {0, 0}
3480                 }
3481         },
3482         {0, 1, 2, 0,            /* 0xe6 */
3483                 {{1, 2},
3484                 {5, 7},
3485                 {0, 0},
3486                 {0, 0}
3487                 }
3488         },
3489         {1, 1, 2, 0,            /* 0xe7 */
3490                 {{0, 2},
3491                 {5, 7},
3492                 {0, 0},
3493                 {0, 0}
3494                 }
3495         },
3496         {0, 1, 2, 0,            /* 0xe8 */
3497                 {{3, 3},
3498                 {5, 7},
3499                 {0, 0},
3500                 {0, 0}
3501                 }
3502         },
3503         {1, 1, 3, 0,            /* 0xe9 */
3504                 {{0, 0},
3505                 {3, 3},
3506                 {5, 7},
3507                 {0, 0}
3508                 }
3509         },
3510         {0, 1, 3, 0,            /* 0xea */
3511                 {{1, 1},
3512                 {3, 3},
3513                 {5, 7},
3514                 {0, 0}
3515                 }
3516         },
3517         {1, 1, 3, 0,            /* 0xeb */
3518                 {{0, 1},
3519                 {3, 3},
3520                 {5, 7},
3521                 {0, 0}
3522                 }
3523         },
3524         {0, 1, 2, 0,            /* 0xec */
3525                 {{2, 3},
3526                 {5, 7},
3527                 {0, 0},
3528                 {0, 0}
3529                 }
3530         },
3531         {1, 1, 3, 0,            /* 0xed */
3532                 {{0, 0},
3533                 {2, 3},
3534                 {5, 7},
3535                 {0, 0}
3536                 }
3537         },
3538         {0, 1, 2, 0,            /* 0xee */
3539                 {{1, 3},
3540                 {5, 7},
3541                 {0, 0},
3542                 {0, 0}
3543                 }
3544         },
3545         {1, 1, 2, 0,            /* 0xef */
3546                 {{0, 3},
3547                 {5, 7},
3548                 {0, 0},
3549                 {0, 0}
3550                 }
3551         },
3552         {0, 1, 1, 0,            /* 0xf0 */
3553                 {{4, 7},
3554                 {0, 0},
3555                 {0, 0},
3556                 {0, 0}
3557                 }
3558         },
3559         {1, 1, 2, 0,            /* 0xf1 */
3560                 {{0, 0},
3561                 {4, 7},
3562                 {0, 0},
3563                 {0, 0}
3564                 }
3565         },
3566         {0, 1, 2, 0,            /* 0xf2 */
3567                 {{1, 1},
3568                 {4, 7},
3569                 {0, 0},
3570                 {0, 0}
3571                 }
3572         },
3573         {1, 1, 2, 0,            /* 0xf3 */
3574                 {{0, 1},
3575                 {4, 7},
3576                 {0, 0},
3577                 {0, 0}
3578                 }
3579         },
3580         {0, 1, 2, 0,            /* 0xf4 */
3581                 {{2, 2},
3582                 {4, 7},
3583                 {0, 0},
3584                 {0, 0}
3585                 }
3586         },
3587         {1, 1, 3, 0,            /* 0xf5 */
3588                 {{0, 0},
3589                 {2, 2},
3590                 {4, 7},
3591                 {0, 0}
3592                 }
3593         },
3594         {0, 1, 2, 0,            /* 0xf6 */
3595                 {{1, 2},
3596                 {4, 7},
3597                 {0, 0},
3598                 {0, 0}
3599                 }
3600         },
3601         {1, 1, 2, 0,            /* 0xf7 */
3602                 {{0, 2},
3603                 {4, 7},
3604                 {0, 0},
3605                 {0, 0}
3606                 }
3607         },
3608         {0, 1, 1, 0,            /* 0xf8 */
3609                 {{3, 7},
3610                 {0, 0},
3611                 {0, 0},
3612                 {0, 0}
3613                 }
3614         },
3615         {1, 1, 2, 0,            /* 0xf9 */
3616                 {{0, 0},
3617                 {3, 7},
3618                 {0, 0},
3619                 {0, 0}
3620                 }
3621         },
3622         {0, 1, 2, 0,            /* 0xfa */
3623                 {{1, 1},
3624                 {3, 7},
3625                 {0, 0},
3626                 {0, 0}
3627                 }
3628         },
3629         {1, 1, 2, 0,            /* 0xfb */
3630                 {{0, 1},
3631                 {3, 7},
3632                 {0, 0},
3633                 {0, 0}
3634                 }
3635         },
3636         {0, 1, 1, 0,            /* 0xfc */
3637                 {{2, 7},
3638                 {0, 0},
3639                 {0, 0},
3640                 {0, 0}
3641                 }
3642         },
3643         {1, 1, 2, 0,            /* 0xfd */
3644                 {{0, 0},
3645                 {2, 7},
3646                 {0, 0},
3647                 {0, 0}
3648                 }
3649         },
3650         {0, 1, 1, 0,            /* 0xfe */
3651                 {{1, 7},
3652                 {0, 0},
3653                 {0, 0},
3654                 {0, 0}
3655                 }
3656         },
3657         {1, 1, 1, 0,            /* 0xff */
3658                 {{0, 7},
3659                 {0, 0},
3660                 {0, 0},
3661                 {0, 0}
3662                 }
3663         }
3664 };
3665
3666
3667
3668 int
3669 sctp_is_address_in_scope(struct sctp_ifa *ifa,
3670     int ipv4_addr_legal,
3671     int ipv6_addr_legal,
3672     int loopback_scope,
3673     int ipv4_local_scope,
3674     int local_scope,
3675     int site_scope,
3676     int do_update)
3677 {
3678         if ((loopback_scope == 0) &&
3679             (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
3680                 /*
3681                  * skip loopback if not in scope *
3682                  */
3683                 return (0);
3684         }
3685         switch (ifa->address.sa.sa_family) {
3686         case AF_INET:
3687                 if (ipv4_addr_legal) {
3688                         struct sockaddr_in *sin;
3689
3690                         sin = (struct sockaddr_in *)&ifa->address.sin;
3691                         if (sin->sin_addr.s_addr == 0) {
3692                                 /* not in scope , unspecified */
3693                                 return (0);
3694                         }
3695                         if ((ipv4_local_scope == 0) &&
3696                             (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
3697                                 /* private address not in scope */
3698                                 return (0);
3699                         }
3700                 } else {
3701                         return (0);
3702                 }
3703                 break;
3704 #ifdef INET6
3705         case AF_INET6:
3706                 if (ipv6_addr_legal) {
3707                         struct sockaddr_in6 *sin6;
3708
3709                         /*
3710                          * Must update the flags,  bummer, which means any
3711                          * IFA locks must now be applied HERE <->
3712                          */
3713                         if (do_update) {
3714                                 sctp_gather_internal_ifa_flags(ifa);
3715                         }
3716                         if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3717                                 return (0);
3718                         }
3719                         /* ok to use deprecated addresses? */
3720                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
3721                         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3722                                 /* skip unspecifed addresses */
3723                                 return (0);
3724                         }
3725                         if (    /* (local_scope == 0) && */
3726                             (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
3727                                 return (0);
3728                         }
3729                         if ((site_scope == 0) &&
3730                             (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
3731                                 return (0);
3732                         }
3733                 } else {
3734                         return (0);
3735                 }
3736                 break;
3737 #endif
3738         default:
3739                 return (0);
3740         }
3741         return (1);
3742 }
3743
3744 static struct mbuf *
3745 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa)
3746 {
3747         struct sctp_paramhdr *parmh;
3748         struct mbuf *mret;
3749         int len;
3750
3751         if (ifa->address.sa.sa_family == AF_INET) {
3752                 len = sizeof(struct sctp_ipv4addr_param);
3753         } else if (ifa->address.sa.sa_family == AF_INET6) {
3754                 len = sizeof(struct sctp_ipv6addr_param);
3755         } else {
3756                 /* unknown type */
3757                 return (m);
3758         }
3759         if (M_TRAILINGSPACE(m) >= len) {
3760                 /* easy side we just drop it on the end */
3761                 parmh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
3762                 mret = m;
3763         } else {
3764                 /* Need more space */
3765                 mret = m;
3766                 while (SCTP_BUF_NEXT(mret) != NULL) {
3767                         mret = SCTP_BUF_NEXT(mret);
3768                 }
3769                 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(len, 0, M_DONTWAIT, 1, MT_DATA);
3770                 if (SCTP_BUF_NEXT(mret) == NULL) {
3771                         /* We are hosed, can't add more addresses */
3772                         return (m);
3773                 }
3774                 mret = SCTP_BUF_NEXT(mret);
3775                 parmh = mtod(mret, struct sctp_paramhdr *);
3776         }
3777         /* now add the parameter */
3778         switch (ifa->address.sa.sa_family) {
3779         case AF_INET:
3780                 {
3781                         struct sctp_ipv4addr_param *ipv4p;
3782                         struct sockaddr_in *sin;
3783
3784                         sin = (struct sockaddr_in *)&ifa->address.sin;
3785                         ipv4p = (struct sctp_ipv4addr_param *)parmh;
3786                         parmh->param_type = htons(SCTP_IPV4_ADDRESS);
3787                         parmh->param_length = htons(len);
3788                         ipv4p->addr = sin->sin_addr.s_addr;
3789                         SCTP_BUF_LEN(mret) += len;
3790                         break;
3791                 }
3792 #ifdef INET6
3793         case AF_INET6:
3794                 {
3795                         struct sctp_ipv6addr_param *ipv6p;
3796                         struct sockaddr_in6 *sin6;
3797
3798                         sin6 = (struct sockaddr_in6 *)&ifa->address.sin6;
3799                         ipv6p = (struct sctp_ipv6addr_param *)parmh;
3800                         parmh->param_type = htons(SCTP_IPV6_ADDRESS);
3801                         parmh->param_length = htons(len);
3802                         memcpy(ipv6p->addr, &sin6->sin6_addr,
3803                             sizeof(ipv6p->addr));
3804                         /* clear embedded scope in the address */
3805                         in6_clearscope((struct in6_addr *)ipv6p->addr);
3806                         SCTP_BUF_LEN(mret) += len;
3807                         break;
3808                 }
3809 #endif
3810         default:
3811                 return (m);
3812         }
3813         return (mret);
3814 }
3815
3816
3817 struct mbuf *
3818 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_scoping *scope,
3819     struct mbuf *m_at, int cnt_inits_to)
3820 {
3821         struct sctp_vrf *vrf = NULL;
3822         int cnt, limit_out = 0, total_count;
3823         uint32_t vrf_id;
3824
3825         vrf_id = inp->def_vrf_id;
3826         SCTP_IPI_ADDR_RLOCK();
3827         vrf = sctp_find_vrf(vrf_id);
3828         if (vrf == NULL) {
3829                 SCTP_IPI_ADDR_RUNLOCK();
3830                 return (m_at);
3831         }
3832         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3833                 struct sctp_ifa *sctp_ifap;
3834                 struct sctp_ifn *sctp_ifnp;
3835
3836                 cnt = cnt_inits_to;
3837                 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
3838                         limit_out = 1;
3839                         cnt = SCTP_ADDRESS_LIMIT;
3840                         goto skip_count;
3841                 }
3842                 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3843                         if ((scope->loopback_scope == 0) &&
3844                             SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
3845                                 /*
3846                                  * Skip loopback devices if loopback_scope
3847                                  * not set
3848                                  */
3849                                 continue;
3850                         }
3851                         LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3852                                 if (sctp_is_address_in_scope(sctp_ifap,
3853                                     scope->ipv4_addr_legal,
3854                                     scope->ipv6_addr_legal,
3855                                     scope->loopback_scope,
3856                                     scope->ipv4_local_scope,
3857                                     scope->local_scope,
3858                                     scope->site_scope, 1) == 0) {
3859                                         continue;
3860                                 }
3861                                 cnt++;
3862                                 if (cnt > SCTP_ADDRESS_LIMIT) {
3863                                         break;
3864                                 }
3865                         }
3866                         if (cnt > SCTP_ADDRESS_LIMIT) {
3867                                 break;
3868                         }
3869                 }
3870 skip_count:
3871                 if (cnt > 1) {
3872                         total_count = 0;
3873                         LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
3874                                 cnt = 0;
3875                                 if ((scope->loopback_scope == 0) &&
3876                                     SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
3877                                         /*
3878                                          * Skip loopback devices if
3879                                          * loopback_scope not set
3880                                          */
3881                                         continue;
3882                                 }
3883                                 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
3884                                         if (sctp_is_address_in_scope(sctp_ifap,
3885                                             scope->ipv4_addr_legal,
3886                                             scope->ipv6_addr_legal,
3887                                             scope->loopback_scope,
3888                                             scope->ipv4_local_scope,
3889                                             scope->local_scope,
3890                                             scope->site_scope, 0) == 0) {
3891                                                 continue;
3892                                         }
3893                                         m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap);
3894                                         if (limit_out) {
3895                                                 cnt++;
3896                                                 total_count++;
3897                                                 if (cnt >= 2) {
3898                                                         /*
3899                                                          * two from each
3900                                                          * address
3901                                                          */
3902                                                         break;
3903                                                 }
3904                                                 if (total_count > SCTP_ADDRESS_LIMIT) {
3905                                                         /* No more addresses */
3906                                                         break;
3907                                                 }
3908                                         }
3909                                 }
3910                         }
3911                 }
3912         } else {
3913                 struct sctp_laddr *laddr;
3914
3915                 cnt = cnt_inits_to;
3916                 /* First, how many ? */
3917                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3918                         if (laddr->ifa == NULL) {
3919                                 continue;
3920                         }
3921                         if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3922                                 /*
3923                                  * Address being deleted by the system, dont
3924                                  * list.
3925                                  */
3926                                 continue;
3927                         if (laddr->action == SCTP_DEL_IP_ADDRESS) {
3928                                 /*
3929                                  * Address being deleted on this ep don't
3930                                  * list.
3931                                  */
3932                                 continue;
3933                         }
3934                         if (sctp_is_address_in_scope(laddr->ifa,
3935                             scope->ipv4_addr_legal,
3936                             scope->ipv6_addr_legal,
3937                             scope->loopback_scope,
3938                             scope->ipv4_local_scope,
3939                             scope->local_scope,
3940                             scope->site_scope, 1) == 0) {
3941                                 continue;
3942                         }
3943                         cnt++;
3944                 }
3945                 if (cnt > SCTP_ADDRESS_LIMIT) {
3946                         limit_out = 1;
3947                 }
3948                 /*
3949                  * To get through a NAT we only list addresses if we have
3950                  * more than one. That way if you just bind a single address
3951                  * we let the source of the init dictate our address.
3952                  */
3953                 if (cnt > 1) {
3954                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3955                                 cnt = 0;
3956                                 if (laddr->ifa == NULL) {
3957                                         continue;
3958                                 }
3959                                 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
3960                                         continue;
3961
3962                                 if (sctp_is_address_in_scope(laddr->ifa,
3963                                     scope->ipv4_addr_legal,
3964                                     scope->ipv6_addr_legal,
3965                                     scope->loopback_scope,
3966                                     scope->ipv4_local_scope,
3967                                     scope->local_scope,
3968                                     scope->site_scope, 0) == 0) {
3969                                         continue;
3970                                 }
3971                                 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa);
3972                                 cnt++;
3973                                 if (cnt >= SCTP_ADDRESS_LIMIT) {
3974                                         break;
3975                                 }
3976                         }
3977                 }
3978         }
3979         SCTP_IPI_ADDR_RUNLOCK();
3980         return (m_at);
3981 }
3982
3983 static struct sctp_ifa *
3984 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
3985     uint8_t dest_is_loop,
3986     uint8_t dest_is_priv,
3987     sa_family_t fam)
3988 {
3989         uint8_t dest_is_global = 0;
3990
3991         /* dest_is_priv is true if destination is a private address */
3992         /* dest_is_loop is true if destination is a loopback addresses */
3993
3994         /*
3995          * Here we determine if its a preferred address. A preferred address
3996          * means it is the same scope or higher scope then the destination.
3997          * L = loopback, P = private, G = global
3998          * ----------------------------------------- src    |  dest | result
3999          * ---------------------------------------- L     |    L  |    yes
4000          * ----------------------------------------- P     |    L  |
4001          * yes-v4 no-v6 ----------------------------------------- G     |
4002          * L  |    yes-v4 no-v6 ----------------------------------------- L
4003          * |    P  |    no ----------------------------------------- P     |
4004          * P  |    yes ----------------------------------------- G     |
4005          * P  |    no ----------------------------------------- L     |    G
4006          * |    no ----------------------------------------- P     |    G  |
4007          * no ----------------------------------------- G     |    G  |
4008          * yes -----------------------------------------
4009          */
4010
4011         if (ifa->address.sa.sa_family != fam) {
4012                 /* forget mis-matched family */
4013                 return (NULL);
4014         }
4015         if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
4016                 dest_is_global = 1;
4017         }
4018         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
4019         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
4020         /* Ok the address may be ok */
4021         if (fam == AF_INET6) {
4022                 /* ok to use deprecated addresses? no lets not! */
4023                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4024                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
4025                         return (NULL);
4026                 }
4027                 if (ifa->src_is_priv && !ifa->src_is_loop) {
4028                         if (dest_is_loop) {
4029                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
4030                                 return (NULL);
4031                         }
4032                 }
4033                 if (ifa->src_is_glob) {
4034                         if (dest_is_loop) {
4035                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
4036                                 return (NULL);
4037                         }
4038                 }
4039         }
4040         /*
4041          * Now that we know what is what, implement or table this could in
4042          * theory be done slicker (it used to be), but this is
4043          * straightforward and easier to validate :-)
4044          */
4045         SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
4046             ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
4047         SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
4048             dest_is_loop, dest_is_priv, dest_is_global);
4049
4050         if ((ifa->src_is_loop) && (dest_is_priv)) {
4051                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
4052                 return (NULL);
4053         }
4054         if ((ifa->src_is_glob) && (dest_is_priv)) {
4055                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
4056                 return (NULL);
4057         }
4058         if ((ifa->src_is_loop) && (dest_is_global)) {
4059                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
4060                 return (NULL);
4061         }
4062         if ((ifa->src_is_priv) && (dest_is_global)) {
4063                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
4064                 return (NULL);
4065         }
4066         SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
4067         /* its a preferred address */
4068         return (ifa);
4069 }
4070
4071 static struct sctp_ifa *
4072 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
4073     uint8_t dest_is_loop,
4074     uint8_t dest_is_priv,
4075     sa_family_t fam)
4076 {
4077         uint8_t dest_is_global = 0;
4078
4079
4080         /*
4081          * Here we determine if its a acceptable address. A acceptable
4082          * address means it is the same scope or higher scope but we can
4083          * allow for NAT which means its ok to have a global dest and a
4084          * private src.
4085          * 
4086          * L = loopback, P = private, G = global
4087          * ----------------------------------------- src    |  dest | result
4088          * ----------------------------------------- L     |   L   |    yes
4089          * ----------------------------------------- P     |   L   |
4090          * yes-v4 no-v6 ----------------------------------------- G     |
4091          * L   |    yes ----------------------------------------- L     |
4092          * P   |    no ----------------------------------------- P     |   P
4093          * |    yes ----------------------------------------- G     |   P
4094          * |    yes - May not work -----------------------------------------
4095          * L     |   G   |    no ----------------------------------------- P
4096          * |   G   |    yes - May not work
4097          * ----------------------------------------- G     |   G   |    yes
4098          * -----------------------------------------
4099          */
4100
4101         if (ifa->address.sa.sa_family != fam) {
4102                 /* forget non matching family */
4103                 return (NULL);
4104         }
4105         /* Ok the address may be ok */
4106         if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
4107                 dest_is_global = 1;
4108         }
4109         if (fam == AF_INET6) {
4110                 /* ok to use deprecated addresses? */
4111                 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
4112                         return (NULL);
4113                 }
4114                 if (ifa->src_is_priv) {
4115                         /* Special case, linklocal to loop */
4116                         if (dest_is_loop)
4117                                 return (NULL);
4118                 }
4119         }
4120         /*
4121          * Now that we know what is what, implement our table. This could in
4122          * theory be done slicker (it used to be), but this is
4123          * straightforward and easier to validate :-)
4124          */
4125         if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
4126                 return (NULL);
4127         }
4128         if ((ifa->src_is_loop == 1) && (dest_is_global)) {
4129                 return (NULL);
4130         }
4131         /* its an acceptable address */
4132         return (ifa);
4133 }
4134
4135 int
4136 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
4137 {
4138         struct sctp_laddr *laddr;
4139
4140         if (stcb == NULL) {
4141                 /* There are no restrictions, no TCB :-) */
4142                 return (0);
4143         }
4144         LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
4145                 if (laddr->ifa == NULL) {
4146                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4147                             __FUNCTION__);
4148                         continue;
4149                 }
4150                 if (laddr->ifa == ifa) {
4151                         /* Yes it is on the list */
4152                         return (1);
4153                 }
4154         }
4155         return (0);
4156 }
4157
4158
4159 int
4160 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
4161 {
4162         struct sctp_laddr *laddr;
4163
4164         if (ifa == NULL)
4165                 return (0);
4166         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4167                 if (laddr->ifa == NULL) {
4168                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
4169                             __FUNCTION__);
4170                         continue;
4171                 }
4172                 if ((laddr->ifa == ifa) && laddr->action == 0)
4173                         /* same pointer */
4174                         return (1);
4175         }
4176         return (0);
4177 }
4178
4179
4180
4181 static struct sctp_ifa *
4182 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
4183     sctp_route_t * ro,
4184     uint32_t vrf_id,
4185     int non_asoc_addr_ok,
4186     uint8_t dest_is_priv,
4187     uint8_t dest_is_loop,
4188     sa_family_t fam)
4189 {
4190         struct sctp_laddr *laddr, *starting_point;
4191         void *ifn;
4192         int resettotop = 0;
4193         struct sctp_ifn *sctp_ifn;
4194         struct sctp_ifa *sctp_ifa, *sifa;
4195         struct sctp_vrf *vrf;
4196         uint32_t ifn_index;
4197
4198         vrf = sctp_find_vrf(vrf_id);
4199         if (vrf == NULL)
4200                 return (NULL);
4201
4202         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4203         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
4204         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
4205         /*
4206          * first question, is the ifn we will emit on in our list, if so, we
4207          * want such an address. Note that we first looked for a preferred
4208          * address.
4209          */
4210         if (sctp_ifn) {
4211                 /* is a preferred one on the interface we route out? */
4212                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4213                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4214                             (non_asoc_addr_ok == 0))
4215                                 continue;
4216                         sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
4217                             dest_is_loop,
4218                             dest_is_priv, fam);
4219                         if (sifa == NULL)
4220                                 continue;
4221                         if (sctp_is_addr_in_ep(inp, sifa)) {
4222                                 atomic_add_int(&sifa->refcount, 1);
4223                                 return (sifa);
4224                         }
4225                 }
4226         }
4227         /*
4228          * ok, now we now need to find one on the list of the addresses. We
4229          * can't get one on the emitting interface so let's find first a
4230          * preferred one. If not that an acceptable one otherwise... we
4231          * return NULL.
4232          */
4233         starting_point = inp->next_addr_touse;
4234 once_again:
4235         if (inp->next_addr_touse == NULL) {
4236                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
4237                 resettotop = 1;
4238         }
4239         for (laddr = inp->next_addr_touse; laddr;
4240             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4241                 if (laddr->ifa == NULL) {
4242                         /* address has been removed */
4243                         continue;
4244                 }
4245                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4246                         /* address is being deleted */
4247                         continue;
4248                 }
4249                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
4250                     dest_is_priv, fam);
4251                 if (sifa == NULL)
4252                         continue;
4253                 atomic_add_int(&sifa->refcount, 1);
4254                 return (sifa);
4255         }
4256         if (resettotop == 0) {
4257                 inp->next_addr_touse = NULL;
4258                 goto once_again;
4259         }
4260         inp->next_addr_touse = starting_point;
4261         resettotop = 0;
4262 once_again_too:
4263         if (inp->next_addr_touse == NULL) {
4264                 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
4265                 resettotop = 1;
4266         }
4267         /* ok, what about an acceptable address in the inp */
4268         for (laddr = inp->next_addr_touse; laddr;
4269             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4270                 if (laddr->ifa == NULL) {
4271                         /* address has been removed */
4272                         continue;
4273                 }
4274                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4275                         /* address is being deleted */
4276                         continue;
4277                 }
4278                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
4279                     dest_is_priv, fam);
4280                 if (sifa == NULL)
4281                         continue;
4282                 atomic_add_int(&sifa->refcount, 1);
4283                 return (sifa);
4284         }
4285         if (resettotop == 0) {
4286                 inp->next_addr_touse = NULL;
4287                 goto once_again_too;
4288         }
4289         /*
4290          * no address bound can be a source for the destination we are in
4291          * trouble
4292          */
4293         return (NULL);
4294 }
4295
4296
4297
4298 static struct sctp_ifa *
4299 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
4300     struct sctp_tcb *stcb,
4301     struct sctp_nets *net,
4302     sctp_route_t * ro,
4303     uint32_t vrf_id,
4304     uint8_t dest_is_priv,
4305     uint8_t dest_is_loop,
4306     int non_asoc_addr_ok,
4307     sa_family_t fam)
4308 {
4309         struct sctp_laddr *laddr, *starting_point;
4310         void *ifn;
4311         struct sctp_ifn *sctp_ifn;
4312         struct sctp_ifa *sctp_ifa, *sifa;
4313         uint8_t start_at_beginning = 0;
4314         struct sctp_vrf *vrf;
4315         uint32_t ifn_index;
4316
4317         /*
4318          * first question, is the ifn we will emit on in our list, if so, we
4319          * want that one.
4320          */
4321         vrf = sctp_find_vrf(vrf_id);
4322         if (vrf == NULL)
4323                 return (NULL);
4324
4325         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4326         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
4327         sctp_ifn = sctp_find_ifn(ifn, ifn_index);
4328
4329         /*
4330          * first question, is the ifn we will emit on in our list?  If so,
4331          * we want that one. First we look for a preferred. Second, we go
4332          * for an acceptable.
4333          */
4334         if (sctp_ifn) {
4335                 /* first try for a preferred address on the ep */
4336                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4337                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
4338                                 continue;
4339                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
4340                                 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
4341                                 if (sifa == NULL)
4342                                         continue;
4343                                 if (((non_asoc_addr_ok == 0) &&
4344                                     (sctp_is_addr_restricted(stcb, sifa))) ||
4345                                     (non_asoc_addr_ok &&
4346                                     (sctp_is_addr_restricted(stcb, sifa)) &&
4347                                     (!sctp_is_addr_pending(stcb, sifa)))) {
4348                                         /* on the no-no list */
4349                                         continue;
4350                                 }
4351                                 atomic_add_int(&sifa->refcount, 1);
4352                                 return (sifa);
4353                         }
4354                 }
4355                 /* next try for an acceptable address on the ep */
4356                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4357                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
4358                                 continue;
4359                         if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
4360                                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
4361                                 if (sifa == NULL)
4362                                         continue;
4363                                 if (((non_asoc_addr_ok == 0) &&
4364                                     (sctp_is_addr_restricted(stcb, sifa))) ||
4365                                     (non_asoc_addr_ok &&
4366                                     (sctp_is_addr_restricted(stcb, sifa)) &&
4367                                     (!sctp_is_addr_pending(stcb, sifa)))) {
4368                                         /* on the no-no list */
4369                                         continue;
4370                                 }
4371                                 atomic_add_int(&sifa->refcount, 1);
4372                                 return (sifa);
4373                         }
4374                 }
4375
4376         }
4377         /*
4378          * if we can't find one like that then we must look at all addresses
4379          * bound to pick one at first preferable then secondly acceptable.
4380          */
4381         starting_point = stcb->asoc.last_used_address;
4382 sctp_from_the_top:
4383         if (stcb->asoc.last_used_address == NULL) {
4384                 start_at_beginning = 1;
4385                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
4386         }
4387         /* search beginning with the last used address */
4388         for (laddr = stcb->asoc.last_used_address; laddr;
4389             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4390                 if (laddr->ifa == NULL) {
4391                         /* address has been removed */
4392                         continue;
4393                 }
4394                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4395                         /* address is being deleted */
4396                         continue;
4397                 }
4398                 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
4399                 if (sifa == NULL)
4400                         continue;
4401                 if (((non_asoc_addr_ok == 0) &&
4402                     (sctp_is_addr_restricted(stcb, sifa))) ||
4403                     (non_asoc_addr_ok &&
4404                     (sctp_is_addr_restricted(stcb, sifa)) &&
4405                     (!sctp_is_addr_pending(stcb, sifa)))) {
4406                         /* on the no-no list */
4407                         continue;
4408                 }
4409                 stcb->asoc.last_used_address = laddr;
4410                 atomic_add_int(&sifa->refcount, 1);
4411                 return (sifa);
4412         }
4413         if (start_at_beginning == 0) {
4414                 stcb->asoc.last_used_address = NULL;
4415                 goto sctp_from_the_top;
4416         }
4417         /* now try for any higher scope than the destination */
4418         stcb->asoc.last_used_address = starting_point;
4419         start_at_beginning = 0;
4420 sctp_from_the_top2:
4421         if (stcb->asoc.last_used_address == NULL) {
4422                 start_at_beginning = 1;
4423                 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
4424         }
4425         /* search beginning with the last used address */
4426         for (laddr = stcb->asoc.last_used_address; laddr;
4427             laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
4428                 if (laddr->ifa == NULL) {
4429                         /* address has been removed */
4430                         continue;
4431                 }
4432                 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
4433                         /* address is being deleted */
4434                         continue;
4435                 }
4436                 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
4437                     dest_is_priv, fam);
4438                 if (sifa == NULL)
4439                         continue;
4440                 if (((non_asoc_addr_ok == 0) &&
4441                     (sctp_is_addr_restricted(stcb, sifa))) ||
4442                     (non_asoc_addr_ok &&
4443                     (sctp_is_addr_restricted(stcb, sifa)) &&
4444                     (!sctp_is_addr_pending(stcb, sifa)))) {
4445                         /* on the no-no list */
4446                         continue;
4447                 }
4448                 stcb->asoc.last_used_address = laddr;
4449                 atomic_add_int(&sifa->refcount, 1);
4450                 return (sifa);
4451         }
4452         if (start_at_beginning == 0) {
4453                 stcb->asoc.last_used_address = NULL;
4454                 goto sctp_from_the_top2;
4455         }
4456         return (NULL);
4457 }
4458
4459 static struct sctp_ifa *
4460 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
4461     struct sctp_tcb *stcb,
4462     int non_asoc_addr_ok,
4463     uint8_t dest_is_loop,
4464     uint8_t dest_is_priv,
4465     int addr_wanted,
4466     sa_family_t fam,
4467     sctp_route_t * ro
4468 )
4469 {
4470         struct sctp_ifa *ifa, *sifa;
4471         int num_eligible_addr = 0;
4472
4473 #ifdef INET6
4474         struct sockaddr_in6 sin6, lsa6;
4475
4476         if (fam == AF_INET6) {
4477                 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
4478                 (void)sa6_recoverscope(&sin6);
4479         }
4480 #endif                          /* INET6 */
4481         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
4482                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4483                     (non_asoc_addr_ok == 0))
4484                         continue;
4485                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
4486                     dest_is_priv, fam);
4487                 if (sifa == NULL)
4488                         continue;
4489 #ifdef INET6
4490                 if (fam == AF_INET6 &&
4491                     dest_is_loop &&
4492                     sifa->src_is_loop && sifa->src_is_priv) {
4493                         /*
4494                          * don't allow fe80::1 to be a src on loop ::1, we
4495                          * don't list it to the peer so we will get an
4496                          * abort.
4497                          */
4498                         continue;
4499                 }
4500                 if (fam == AF_INET6 &&
4501                     IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
4502                     IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
4503                         /*
4504                          * link-local <-> link-local must belong to the same
4505                          * scope.
4506                          */
4507                         memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
4508                         (void)sa6_recoverscope(&lsa6);
4509                         if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
4510                                 continue;
4511                         }
4512                 }
4513 #endif                          /* INET6 */
4514
4515                 /*
4516                  * Check if the IPv6 address matches to next-hop. In the
4517                  * mobile case, old IPv6 address may be not deleted from the
4518                  * interface. Then, the interface has previous and new
4519                  * addresses.  We should use one corresponding to the
4520                  * next-hop.  (by micchie)
4521                  */
4522 #ifdef INET6
4523                 if (stcb && fam == AF_INET6 &&
4524                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
4525                         if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro)
4526                             == 0) {
4527                                 continue;
4528                         }
4529                 }
4530 #endif
4531                 /* Avoid topologically incorrect IPv4 address */
4532                 if (stcb && fam == AF_INET &&
4533                     sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
4534                         if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
4535                                 continue;
4536                         }
4537                 }
4538                 if (stcb) {
4539                         if (((non_asoc_addr_ok == 0) &&
4540                             (sctp_is_addr_restricted(stcb, sifa))) ||
4541                             (non_asoc_addr_ok &&
4542                             (sctp_is_addr_restricted(stcb, sifa)) &&
4543                             (!sctp_is_addr_pending(stcb, sifa)))) {
4544                                 /*
4545                                  * It is restricted for some reason..
4546                                  * probably not yet added.
4547                                  */
4548                                 continue;
4549                         }
4550                 }
4551                 if (num_eligible_addr >= addr_wanted) {
4552                         return (sifa);
4553                 }
4554                 num_eligible_addr++;
4555         }
4556         return (NULL);
4557 }
4558
4559
4560 static int
4561 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
4562     struct sctp_tcb *stcb,
4563     int non_asoc_addr_ok,
4564     uint8_t dest_is_loop,
4565     uint8_t dest_is_priv,
4566     sa_family_t fam)
4567 {
4568         struct sctp_ifa *ifa, *sifa;
4569         int num_eligible_addr = 0;
4570
4571         LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
4572                 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4573                     (non_asoc_addr_ok == 0)) {
4574                         continue;
4575                 }
4576                 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
4577                     dest_is_priv, fam);
4578                 if (sifa == NULL) {
4579                         continue;
4580                 }
4581                 if (stcb) {
4582                         if (((non_asoc_addr_ok == 0) &&
4583                             (sctp_is_addr_restricted(stcb, sifa))) ||
4584                             (non_asoc_addr_ok &&
4585                             (sctp_is_addr_restricted(stcb, sifa)) &&
4586                             (!sctp_is_addr_pending(stcb, sifa)))) {
4587                                 /*
4588                                  * It is restricted for some reason..
4589                                  * probably not yet added.
4590                                  */
4591                                 continue;
4592                         }
4593                 }
4594                 num_eligible_addr++;
4595         }
4596         return (num_eligible_addr);
4597 }
4598
4599 static struct sctp_ifa *
4600 sctp_choose_boundall(struct sctp_inpcb *inp,
4601     struct sctp_tcb *stcb,
4602     struct sctp_nets *net,
4603     sctp_route_t * ro,
4604     uint32_t vrf_id,
4605     uint8_t dest_is_priv,
4606     uint8_t dest_is_loop,
4607     int non_asoc_addr_ok,
4608     sa_family_t fam)
4609 {
4610         int cur_addr_num = 0, num_preferred = 0;
4611         void *ifn;
4612         struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
4613         struct sctp_ifa *sctp_ifa, *sifa;
4614         uint32_t ifn_index;
4615         struct sctp_vrf *vrf;
4616
4617         /*-
4618          * For boundall we can use any address in the association.
4619          * If non_asoc_addr_ok is set we can use any address (at least in
4620          * theory). So we look for preferred addresses first. If we find one,
4621          * we use it. Otherwise we next try to get an address on the
4622          * interface, which we should be able to do (unless non_asoc_addr_ok
4623          * is false and we are routed out that way). In these cases where we
4624          * can't use the address of the interface we go through all the
4625          * ifn's looking for an address we can use and fill that in. Punting
4626          * means we send back address 0, which will probably cause problems
4627          * actually since then IP will fill in the address of the route ifn,
4628          * which means we probably already rejected it.. i.e. here comes an
4629          * abort :-<.
4630          */
4631         vrf = sctp_find_vrf(vrf_id);
4632         if (vrf == NULL)
4633                 return (NULL);
4634
4635         ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4636         ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
4637         emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
4638         if (sctp_ifn == NULL) {
4639                 /* ?? We don't have this guy ?? */
4640                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
4641                 goto bound_all_plan_b;
4642         }
4643         SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
4644             ifn_index, sctp_ifn->ifn_name);
4645
4646         if (net) {
4647                 cur_addr_num = net->indx_of_eligible_next_to_use;
4648         }
4649         num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
4650             stcb,
4651             non_asoc_addr_ok,
4652             dest_is_loop,
4653             dest_is_priv, fam);
4654         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
4655             num_preferred, sctp_ifn->ifn_name);
4656         if (num_preferred == 0) {
4657                 /*
4658                  * no eligible addresses, we must use some other interface
4659                  * address if we can find one.
4660                  */
4661                 goto bound_all_plan_b;
4662         }
4663         /*
4664          * Ok we have num_eligible_addr set with how many we can use, this
4665          * may vary from call to call due to addresses being deprecated
4666          * etc..
4667          */
4668         if (cur_addr_num >= num_preferred) {
4669                 cur_addr_num = 0;
4670         }
4671         /*
4672          * select the nth address from the list (where cur_addr_num is the
4673          * nth) and 0 is the first one, 1 is the second one etc...
4674          */
4675         SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
4676
4677         sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
4678             dest_is_priv, cur_addr_num, fam, ro);
4679
4680         /* if sctp_ifa is NULL something changed??, fall to plan b. */
4681         if (sctp_ifa) {
4682                 atomic_add_int(&sctp_ifa->refcount, 1);
4683                 if (net) {
4684                         /* save off where the next one we will want */
4685                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
4686                 }
4687                 return (sctp_ifa);
4688         }
4689         /*
4690          * plan_b: Look at all interfaces and find a preferred address. If
4691          * no preferred fall through to plan_c.
4692          */
4693 bound_all_plan_b:
4694         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
4695         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
4696                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
4697                     sctp_ifn->ifn_name);
4698                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
4699                         /* wrong base scope */
4700                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
4701                         continue;
4702                 }
4703                 if ((sctp_ifn == looked_at) && looked_at) {
4704                         /* already looked at this guy */
4705                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
4706                         continue;
4707                 }
4708                 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, stcb, non_asoc_addr_ok,
4709                     dest_is_loop, dest_is_priv, fam);
4710                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
4711                     "Found ifn:%p %d preferred source addresses\n",
4712                     ifn, num_preferred);
4713                 if (num_preferred == 0) {
4714                         /* None on this interface. */
4715                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefered -- skipping to next\n");
4716                         continue;
4717                 }
4718                 SCTPDBG(SCTP_DEBUG_OUTPUT2,
4719                     "num preferred:%d on interface:%p cur_addr_num:%d\n",
4720                     num_preferred, sctp_ifn, cur_addr_num);
4721
4722                 /*
4723                  * Ok we have num_eligible_addr set with how many we can
4724                  * use, this may vary from call to call due to addresses
4725                  * being deprecated etc..
4726                  */
4727                 if (cur_addr_num >= num_preferred) {
4728                         cur_addr_num = 0;
4729                 }
4730                 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, stcb, non_asoc_addr_ok, dest_is_loop,
4731                     dest_is_priv, cur_addr_num, fam, ro);
4732                 if (sifa == NULL)
4733                         continue;
4734                 if (net) {
4735                         net->indx_of_eligible_next_to_use = cur_addr_num + 1;
4736                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
4737                             cur_addr_num);
4738                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
4739                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
4740                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
4741                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
4742                 }
4743                 atomic_add_int(&sifa->refcount, 1);
4744                 return (sifa);
4745
4746         }
4747
4748         /* plan_c: do we have an acceptable address on the emit interface */
4749         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
4750         if (emit_ifn == NULL) {
4751                 goto plan_d;
4752         }
4753         LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
4754                 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4755                     (non_asoc_addr_ok == 0))
4756                         continue;
4757                 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
4758                     dest_is_priv, fam);
4759                 if (sifa == NULL)
4760                         continue;
4761                 if (stcb) {
4762                         if (((non_asoc_addr_ok == 0) &&
4763                             (sctp_is_addr_restricted(stcb, sifa))) ||
4764                             (non_asoc_addr_ok &&
4765                             (sctp_is_addr_restricted(stcb, sifa)) &&
4766                             (!sctp_is_addr_pending(stcb, sifa)))) {
4767                                 /*
4768                                  * It is restricted for some reason..
4769                                  * probably not yet added.
4770                                  */
4771                                 continue;
4772                         }
4773                 }
4774                 atomic_add_int(&sifa->refcount, 1);
4775                 return (sifa);
4776         }
4777 plan_d:
4778         /*
4779          * plan_d: We are in trouble. No preferred address on the emit
4780          * interface. And not even a preferred address on all interfaces. Go
4781          * out and see if we can find an acceptable address somewhere
4782          * amongst all interfaces.
4783          */
4784         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D\n");
4785         LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
4786                 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
4787                         /* wrong base scope */
4788                         continue;
4789                 }
4790                 if ((sctp_ifn == looked_at) && looked_at)
4791                         /* already looked at this guy */
4792                         continue;
4793
4794                 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
4795                         if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
4796                             (non_asoc_addr_ok == 0))
4797                                 continue;
4798                         sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
4799                             dest_is_loop,
4800                             dest_is_priv, fam);
4801                         if (sifa == NULL)
4802                                 continue;
4803                         if (stcb) {
4804                                 if (((non_asoc_addr_ok == 0) &&
4805                                     (sctp_is_addr_restricted(stcb, sifa))) ||
4806                                     (non_asoc_addr_ok &&
4807                                     (sctp_is_addr_restricted(stcb, sifa)) &&
4808                                     (!sctp_is_addr_pending(stcb, sifa)))) {
4809                                         /*
4810                                          * It is restricted for some
4811                                          * reason.. probably not yet added.
4812                                          */
4813                                         continue;
4814                                 }
4815                         }
4816                         atomic_add_int(&sifa->refcount, 1);
4817                         return (sifa);
4818                 }
4819         }
4820         /*
4821          * Ok we can find NO address to source from that is not on our
4822          * restricted list and non_asoc_address is NOT ok, or it is on our
4823          * restricted list. We can't source to it :-(
4824          */
4825         return (NULL);
4826 }
4827
4828
4829
4830 /* tcb may be NULL */
4831 struct sctp_ifa *
4832 sctp_source_address_selection(struct sctp_inpcb *inp,
4833     struct sctp_tcb *stcb,
4834     sctp_route_t * ro,
4835     struct sctp_nets *net,
4836     int non_asoc_addr_ok, uint32_t vrf_id)
4837 {
4838         struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
4839
4840 #ifdef INET6
4841         struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
4842
4843 #endif
4844         struct sctp_ifa *answer;
4845         uint8_t dest_is_priv, dest_is_loop;
4846         sa_family_t fam;
4847
4848         /*-
4849          * Rules: - Find the route if needed, cache if I can. - Look at
4850          * interface address in route, Is it in the bound list. If so we
4851          * have the best source. - If not we must rotate amongst the
4852          * addresses.
4853          *
4854          * Cavets and issues
4855          *
4856          * Do we need to pay attention to scope. We can have a private address
4857          * or a global address we are sourcing or sending to. So if we draw
4858          * it out
4859          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
4860          * For V4
4861          *------------------------------------------
4862          *      source     *      dest  *  result
4863          * -----------------------------------------
4864          * <a>  Private    *    Global  *  NAT
4865          * -----------------------------------------
4866          * <b>  Private    *    Private *  No problem
4867          * -----------------------------------------
4868          * <c>  Global     *    Private *  Huh, How will this work?
4869          * -----------------------------------------
4870          * <d>  Global     *    Global  *  No Problem
4871          *------------------------------------------
4872          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
4873          * For V6
4874          *------------------------------------------
4875          *      source     *      dest  *  result
4876          * -----------------------------------------
4877          * <a>  Linklocal  *    Global  *
4878          * -----------------------------------------
4879          * <b>  Linklocal  * Linklocal  *  No problem
4880          * -----------------------------------------
4881          * <c>  Global     * Linklocal  *  Huh, How will this work?
4882          * -----------------------------------------
4883          * <d>  Global     *    Global  *  No Problem
4884          *------------------------------------------
4885          * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
4886          *
4887          * And then we add to that what happens if there are multiple addresses
4888          * assigned to an interface. Remember the ifa on a ifn is a linked
4889          * list of addresses. So one interface can have more than one IP
4890          * address. What happens if we have both a private and a global
4891          * address? Do we then use context of destination to sort out which
4892          * one is best? And what about NAT's sending P->G may get you a NAT
4893          * translation, or should you select the G thats on the interface in
4894          * preference.
4895          *
4896          * Decisions:
4897          *
4898          * - count the number of addresses on the interface.
4899          * - if it is one, no problem except case <c>.
4900          *   For <a> we will assume a NAT out there.
4901          * - if there are more than one, then we need to worry about scope P
4902          *   or G. We should prefer G -> G and P -> P if possible.
4903          *   Then as a secondary fall back to mixed types G->P being a last
4904          *   ditch one.
4905          * - The above all works for bound all, but bound specific we need to
4906          *   use the same concept but instead only consider the bound
4907          *   addresses. If the bound set is NOT assigned to the interface then
4908          *   we must use rotation amongst the bound addresses..
4909          */
4910         if (ro->ro_rt == NULL) {
4911                 /*
4912                  * Need a route to cache.
4913                  */
4914                 SCTP_RTALLOC(ro, vrf_id);
4915         }
4916         if (ro->ro_rt == NULL) {
4917                 return (NULL);
4918         }
4919         fam = to->sin_family;
4920         dest_is_priv = dest_is_loop = 0;
4921         /* Setup our scopes for the destination */
4922         switch (fam) {
4923         case AF_INET:
4924                 /* Scope based on outbound address */
4925                 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
4926                         dest_is_loop = 1;
4927                         if (net != NULL) {
4928                                 /* mark it as local */
4929                                 net->addr_is_local = 1;
4930                         }
4931                 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
4932                         dest_is_priv = 1;
4933                 }
4934                 break;
4935 #ifdef INET6
4936         case AF_INET6:
4937                 /* Scope based on outbound address */
4938                 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
4939                     SCTP_ROUTE_IS_REAL_LOOP(ro)) {
4940                         /*
4941                          * If the address is a loopback address, which
4942                          * consists of "::1" OR "fe80::1%lo0", we are
4943                          * loopback scope. But we don't use dest_is_priv
4944                          * (link local addresses).
4945                          */
4946                         dest_is_loop = 1;
4947                         if (net != NULL) {
4948                                 /* mark it as local */
4949                                 net->addr_is_local = 1;
4950                         }
4951                 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
4952                         dest_is_priv = 1;
4953                 }
4954                 break;
4955 #endif
4956         }
4957         SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
4958         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)to);
4959         SCTP_IPI_ADDR_RLOCK();
4960         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4961                 /*
4962                  * Bound all case
4963                  */
4964                 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
4965                     dest_is_priv, dest_is_loop,
4966                     non_asoc_addr_ok, fam);
4967                 SCTP_IPI_ADDR_RUNLOCK();
4968                 return (answer);
4969         }
4970         /*
4971          * Subset bound case
4972          */
4973         if (stcb) {
4974                 answer = sctp_choose_boundspecific_stcb(inp, stcb, net, ro,
4975                     vrf_id, dest_is_priv,
4976                     dest_is_loop,
4977                     non_asoc_addr_ok, fam);
4978         } else {
4979                 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
4980                     non_asoc_addr_ok,
4981                     dest_is_priv,
4982                     dest_is_loop, fam);
4983         }
4984         SCTP_IPI_ADDR_RUNLOCK();
4985         return (answer);
4986 }
4987
4988 static int
4989 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, int cpsize)
4990 {
4991         struct cmsghdr cmh;
4992         int tlen, at;
4993
4994         tlen = SCTP_BUF_LEN(control);
4995         at = 0;
4996         /*
4997          * Independent of how many mbufs, find the c_type inside the control
4998          * structure and copy out the data.
4999          */
5000         while (at < tlen) {
5001                 if ((tlen - at) < (int)CMSG_ALIGN(sizeof(cmh))) {
5002                         /* not enough room for one more we are done. */
5003                         return (0);
5004                 }
5005                 m_copydata(control, at, sizeof(cmh), (caddr_t)&cmh);
5006                 if (((int)cmh.cmsg_len + at) > tlen) {
5007                         /*
5008                          * this is real messed up since there is not enough
5009                          * data here to cover the cmsg header. We are done.
5010                          */
5011                         return (0);
5012                 }
5013                 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
5014                     (c_type == cmh.cmsg_type)) {
5015                         /* found the one we want, copy it out */
5016                         at += CMSG_ALIGN(sizeof(struct cmsghdr));
5017                         if ((int)(cmh.cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr))) < cpsize) {
5018                                 /*
5019                                  * space of cmsg_len after header not big
5020                                  * enough
5021                                  */
5022                                 return (0);
5023                         }
5024                         m_copydata(control, at, cpsize, data);
5025                         return (1);
5026                 } else {
5027                         at += CMSG_ALIGN(cmh.cmsg_len);
5028                         if (cmh.cmsg_len == 0) {
5029                                 break;
5030                         }
5031                 }
5032         }
5033         /* not found */
5034         return (0);
5035 }
5036
5037 static struct mbuf *
5038 sctp_add_cookie(struct sctp_inpcb *inp, struct mbuf *init, int init_offset,
5039     struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t ** signature)
5040 {
5041         struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
5042         struct sctp_state_cookie *stc;
5043         struct sctp_paramhdr *ph;
5044         uint8_t *foo;
5045         int sig_offset;
5046         uint16_t cookie_sz;
5047
5048         mret = NULL;
5049         mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
5050             sizeof(struct sctp_paramhdr)), 0,
5051             M_DONTWAIT, 1, MT_DATA);
5052         if (mret == NULL) {
5053                 return (NULL);
5054         }
5055         copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_DONTWAIT);
5056         if (copy_init == NULL) {
5057                 sctp_m_freem(mret);
5058                 return (NULL);
5059         }
5060 #ifdef SCTP_MBUF_LOGGING
5061         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5062                 struct mbuf *mat;
5063
5064                 mat = copy_init;
5065                 while (mat) {
5066                         if (SCTP_BUF_IS_EXTENDED(mat)) {
5067                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5068                         }
5069                         mat = SCTP_BUF_NEXT(mat);
5070                 }
5071         }
5072 #endif
5073         copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
5074             M_DONTWAIT);
5075         if (copy_initack == NULL) {
5076                 sctp_m_freem(mret);
5077                 sctp_m_freem(copy_init);
5078                 return (NULL);
5079         }
5080 #ifdef SCTP_MBUF_LOGGING
5081         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
5082                 struct mbuf *mat;
5083
5084                 mat = copy_initack;
5085                 while (mat) {
5086                         if (SCTP_BUF_IS_EXTENDED(mat)) {
5087                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
5088                         }
5089                         mat = SCTP_BUF_NEXT(mat);
5090                 }
5091         }
5092 #endif
5093         /* easy side we just drop it on the end */
5094         ph = mtod(mret, struct sctp_paramhdr *);
5095         SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
5096             sizeof(struct sctp_paramhdr);
5097         stc = (struct sctp_state_cookie *)((caddr_t)ph +
5098             sizeof(struct sctp_paramhdr));
5099         ph->param_type = htons(SCTP_STATE_COOKIE);
5100         ph->param_length = 0;   /* fill in at the end */
5101         /* Fill in the stc cookie data */
5102         memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
5103
5104         /* tack the INIT and then the INIT-ACK onto the chain */
5105         cookie_sz = 0;
5106         m_at = mret;
5107         for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
5108                 cookie_sz += SCTP_BUF_LEN(m_at);
5109                 if (SCTP_BUF_NEXT(m_at) == NULL) {
5110                         SCTP_BUF_NEXT(m_at) = copy_init;
5111                         break;
5112                 }
5113         }
5114
5115         for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
5116                 cookie_sz += SCTP_BUF_LEN(m_at);
5117                 if (SCTP_BUF_NEXT(m_at) == NULL) {
5118                         SCTP_BUF_NEXT(m_at) = copy_initack;
5119                         break;
5120                 }
5121         }
5122
5123         for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
5124                 cookie_sz += SCTP_BUF_LEN(m_at);
5125                 if (SCTP_BUF_NEXT(m_at) == NULL) {
5126                         break;
5127                 }
5128         }
5129         sig = sctp_get_mbuf_for_msg(SCTP_SECRET_SIZE, 0, M_DONTWAIT, 1, MT_DATA);
5130         if (sig == NULL) {
5131                 /* no space, so free the entire chain */
5132                 sctp_m_freem(mret);
5133                 return (NULL);
5134         }
5135         SCTP_BUF_LEN(sig) = 0;
5136         SCTP_BUF_NEXT(m_at) = sig;
5137         sig_offset = 0;
5138         foo = (uint8_t *) (mtod(sig, caddr_t)+sig_offset);
5139         memset(foo, 0, SCTP_SIGNATURE_SIZE);
5140         *signature = foo;
5141         SCTP_BUF_LEN(sig) += SCTP_SIGNATURE_SIZE;
5142         cookie_sz += SCTP_SIGNATURE_SIZE;
5143         ph->param_length = htons(cookie_sz);
5144         return (mret);
5145 }
5146
5147
5148 static uint8_t
5149 sctp_get_ect(struct sctp_tcb *stcb,
5150     struct sctp_tmit_chunk *chk)
5151 {
5152         uint8_t this_random;
5153
5154         /* Huh? */
5155         if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 0)
5156                 return (0);
5157
5158         if (SCTP_BASE_SYSCTL(sctp_ecn_nonce) == 0)
5159                 /* no nonce, always return ECT0 */
5160                 return (SCTP_ECT0_BIT);
5161
5162         if (stcb->asoc.peer_supports_ecn_nonce == 0) {
5163                 /* Peer does NOT support it, so we send a ECT0 only */
5164                 return (SCTP_ECT0_BIT);
5165         }
5166         if (chk == NULL)
5167                 return (SCTP_ECT0_BIT);
5168
5169         if ((stcb->asoc.hb_random_idx > 3) ||
5170             ((stcb->asoc.hb_random_idx == 3) &&
5171             (stcb->asoc.hb_ect_randombit > 7))) {
5172                 uint32_t rndval;
5173
5174 warp_drive_sa:
5175                 rndval = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
5176                 memcpy(stcb->asoc.hb_random_values, &rndval,
5177                     sizeof(stcb->asoc.hb_random_values));
5178                 this_random = stcb->asoc.hb_random_values[0];
5179                 stcb->asoc.hb_random_idx = 0;
5180                 stcb->asoc.hb_ect_randombit = 0;
5181         } else {
5182                 if (stcb->asoc.hb_ect_randombit > 7) {
5183                         stcb->asoc.hb_ect_randombit = 0;
5184                         stcb->asoc.hb_random_idx++;
5185                         if (stcb->asoc.hb_random_idx > 3) {
5186                                 goto warp_drive_sa;
5187                         }
5188                 }
5189                 this_random = stcb->asoc.hb_random_values[stcb->asoc.hb_random_idx];
5190         }
5191         if ((this_random >> stcb->asoc.hb_ect_randombit) & 0x01) {
5192                 if (chk != NULL)
5193                         /* ECN Nonce stuff */
5194                         chk->rec.data.ect_nonce = SCTP_ECT1_BIT;
5195                 stcb->asoc.hb_ect_randombit++;
5196                 return (SCTP_ECT1_BIT);
5197         } else {
5198                 stcb->asoc.hb_ect_randombit++;
5199                 return (SCTP_ECT0_BIT);
5200         }
5201 }
5202
5203 static int
5204 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
5205     struct sctp_tcb *stcb,      /* may be NULL */
5206     struct sctp_nets *net,
5207     struct sockaddr *to,
5208     struct mbuf *m,
5209     uint32_t auth_offset,
5210     struct sctp_auth_chunk *auth,
5211     uint16_t auth_keyid,
5212     int nofragment_flag,
5213     int ecn_ok,
5214     struct sctp_tmit_chunk *chk,
5215     int out_of_asoc_ok,
5216     uint16_t port,
5217     int so_locked,
5218 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5219     SCTP_UNUSED
5220 #endif
5221     union sctp_sockstore *over_addr
5222 )
5223 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
5224 {
5225         /*
5226          * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet
5227          * header WITH an SCTPHDR but no IP header, endpoint inp and sa
5228          * structure: - fill in the HMAC digest of any AUTH chunk in the
5229          * packet. - calculate and fill in the SCTP checksum. - prepend an
5230          * IP address header. - if boundall use INADDR_ANY. - if
5231          * boundspecific do source address selection. - set fragmentation
5232          * option for ipV4. - On return from IP output, check/adjust mtu
5233          * size of output interface and smallest_mtu size as well.
5234          */
5235         /* Will need ifdefs around this */
5236         struct mbuf *o_pak;
5237         struct mbuf *newm;
5238         struct sctphdr *sctphdr;
5239         int packet_length;
5240         uint32_t csum;
5241         int ret;
5242         uint32_t vrf_id;
5243         sctp_route_t *ro = NULL;
5244         struct udphdr *udp = NULL;
5245
5246 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5247         struct socket *so = NULL;
5248
5249 #endif
5250
5251         if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
5252                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
5253                 sctp_m_freem(m);
5254                 return (EFAULT);
5255         }
5256         if (stcb) {
5257                 vrf_id = stcb->asoc.vrf_id;
5258         } else {
5259                 vrf_id = inp->def_vrf_id;
5260         }
5261
5262         /* fill in the HMAC digest for any AUTH chunk in the packet */
5263         if ((auth != NULL) && (stcb != NULL)) {
5264                 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
5265         }
5266         /* Calculate the csum and fill in the length of the packet */
5267         sctphdr = mtod(m, struct sctphdr *);
5268         if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
5269             (stcb) &&
5270             (to->sa_family == AF_INET) &&
5271             (stcb->asoc.loopback_scope)) {
5272                 sctphdr->checksum = 0;
5273                 /*
5274                  * This can probably now be taken out since my audit shows
5275                  * no more bad pktlen's coming in. But we will wait a while
5276                  * yet.
5277                  */
5278                 packet_length = sctp_calculate_len(m);
5279         } else {
5280                 sctphdr->checksum = 0;
5281                 csum = sctp_calculate_sum(m, &packet_length, 0);
5282                 sctphdr->checksum = csum;
5283         }
5284
5285         if (to->sa_family == AF_INET) {
5286                 struct ip *ip = NULL;
5287                 sctp_route_t iproute;
5288                 uint8_t tos_value;
5289
5290                 if (port) {
5291                         newm = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
5292                 } else {
5293                         newm = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
5294                 }
5295                 if (newm == NULL) {
5296                         sctp_m_freem(m);
5297                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5298                         return (ENOMEM);
5299                 }
5300                 if (port) {
5301                         SCTP_ALIGN_TO_END(newm, sizeof(struct ip) + sizeof(struct udphdr));
5302                         SCTP_BUF_LEN(newm) = sizeof(struct ip) + sizeof(struct udphdr);
5303                         packet_length += sizeof(struct ip) + sizeof(struct udphdr);
5304                 } else {
5305                         SCTP_ALIGN_TO_END(newm, sizeof(struct ip));
5306                         SCTP_BUF_LEN(newm) = sizeof(struct ip);
5307                         packet_length += sizeof(struct ip);
5308                 }
5309                 SCTP_BUF_NEXT(newm) = m;
5310                 m = newm;
5311                 ip = mtod(m, struct ip *);
5312                 ip->ip_v = IPVERSION;
5313                 ip->ip_hl = (sizeof(struct ip) >> 2);
5314                 if (net) {
5315                         tos_value = net->tos_flowlabel & 0x000000ff;
5316                 } else {
5317                         tos_value = inp->ip_inp.inp.inp_ip_tos;
5318                 }
5319                 if ((nofragment_flag) && (port == 0)) {
5320 #if defined(WITH_CONVERT_IP_OFF) || defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
5321                         ip->ip_off = IP_DF;
5322 #else
5323                         ip->ip_off = htons(IP_DF);
5324 #endif
5325                 } else
5326                         ip->ip_off = 0;
5327
5328                 /* FreeBSD has a function for ip_id's */
5329                 ip->ip_id = ip_newid();
5330
5331                 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
5332                 ip->ip_len = packet_length;
5333                 if (stcb) {
5334                         if ((stcb->asoc.ecn_allowed) && ecn_ok) {
5335                                 /* Enable ECN */
5336                                 ip->ip_tos = ((u_char)(tos_value & 0xfc) | sctp_get_ect(stcb, chk));
5337                         } else {
5338                                 /* No ECN */
5339                                 ip->ip_tos = (u_char)(tos_value & 0xfc);
5340                         }
5341                 } else {
5342                         /* no association at all */
5343                         ip->ip_tos = (tos_value & 0xfc);
5344                 }
5345                 if (port) {
5346                         ip->ip_p = IPPROTO_UDP;
5347                 } else {
5348                         ip->ip_p = IPPROTO_SCTP;
5349                 }
5350                 ip->ip_sum = 0;
5351                 if (net == NULL) {
5352                         ro = &iproute;
5353                         memset(&iproute, 0, sizeof(iproute));
5354                         memcpy(&ro->ro_dst, to, to->sa_len);
5355                 } else {
5356                         ro = (sctp_route_t *) & net->ro;
5357                 }
5358                 /* Now the address selection part */
5359                 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
5360
5361                 /* call the routine to select the src address */
5362                 if (net && out_of_asoc_ok == 0) {
5363                         if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
5364                                 sctp_free_ifa(net->ro._s_addr);
5365                                 net->ro._s_addr = NULL;
5366                                 net->src_addr_selected = 0;
5367                                 if (ro->ro_rt) {
5368                                         RTFREE(ro->ro_rt);
5369                                         ro->ro_rt = NULL;
5370                                 }
5371                         }
5372                         if (net->src_addr_selected == 0) {
5373                                 /* Cache the source address */
5374                                 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
5375                                     ro, net, 0,
5376                                     vrf_id);
5377                                 net->src_addr_selected = 1;
5378                         }
5379                         if (net->ro._s_addr == NULL) {
5380                                 /* No route to host */
5381                                 net->src_addr_selected = 0;
5382                                 goto no_route;
5383                         }
5384                         ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
5385                 } else {
5386                         if (over_addr == NULL) {
5387                                 struct sctp_ifa *_lsrc;
5388
5389                                 _lsrc = sctp_source_address_selection(inp, stcb, ro,
5390                                     net,
5391                                     out_of_asoc_ok,
5392                                     vrf_id);
5393                                 if (_lsrc == NULL) {
5394                                         goto no_route;
5395                                 }
5396                                 ip->ip_src = _lsrc->address.sin.sin_addr;
5397                                 sctp_free_ifa(_lsrc);
5398                         } else {
5399                                 ip->ip_src = over_addr->sin.sin_addr;
5400                                 SCTP_RTALLOC((&ro->ro_rt), vrf_id);
5401                         }
5402                 }
5403                 if (port) {
5404                         udp = (struct udphdr *)(ip + 1);
5405                         udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
5406                         udp->uh_dport = port;
5407                         udp->uh_ulen = htons(packet_length - sizeof(struct ip));
5408                         udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
5409                 }
5410                 /*
5411                  * If source address selection fails and we find no route
5412                  * then the ip_output should fail as well with a
5413                  * NO_ROUTE_TO_HOST type error. We probably should catch
5414                  * that somewhere and abort the association right away
5415                  * (assuming this is an INIT being sent).
5416                  */
5417                 if ((ro->ro_rt == NULL)) {
5418                         /*
5419                          * src addr selection failed to find a route (or
5420                          * valid source addr), so we can't get there from
5421                          * here (yet)!
5422                          */
5423         no_route:
5424                         SCTPDBG(SCTP_DEBUG_OUTPUT1,
5425                             "%s: dropped packet - no valid source addr\n",
5426                             __FUNCTION__);
5427                         if (net) {
5428                                 SCTPDBG(SCTP_DEBUG_OUTPUT1,
5429                                     "Destination was ");
5430                                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1,
5431                                     &net->ro._l_addr.sa);
5432                                 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
5433                                         if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
5434                                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", net);
5435                                                 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
5436                                                     stcb,
5437                                                     SCTP_FAILED_THRESHOLD,
5438                                                     (void *)net,
5439                                                     so_locked);
5440                                                 net->dest_state &= ~SCTP_ADDR_REACHABLE;
5441                                                 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
5442                                                 /*
5443                                                  * JRS 5/14/07 - If a
5444                                                  * destination is
5445                                                  * unreachable, the PF bit
5446                                                  * is turned off.  This
5447                                                  * allows an unambiguous use
5448                                                  * of the PF bit for
5449                                                  * destinations that are
5450                                                  * reachable but potentially
5451                                                  * failed. If the
5452                                                  * destination is set to the
5453                                                  * unreachable state, also
5454                                                  * set the destination to
5455                                                  * the PF state.
5456                                                  */
5457                                                 /*
5458                                                  * Add debug message here if
5459                                                  * destination is not in PF
5460                                                  * state.
5461                                                  */
5462                                                 /*
5463                                                  * Stop any running T3
5464                                                  * timers here?
5465                                                  */
5466                                                 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
5467                                                         net->dest_state &= ~SCTP_ADDR_PF;
5468                                                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination %p moved from PF to unreachable.\n",
5469                                                             net);
5470                                                 }
5471                                         }
5472                                 }
5473                                 if (stcb) {
5474                                         if (net == stcb->asoc.primary_destination) {
5475                                                 /* need a new primary */
5476                                                 struct sctp_nets *alt;
5477
5478                                                 alt = sctp_find_alternate_net(stcb, net, 0);
5479                                                 if (alt != net) {
5480                                                         if (sctp_set_primary_addr(stcb,
5481                                                             (struct sockaddr *)NULL,
5482                                                             alt) == 0) {
5483                                                                 net->dest_state |= SCTP_ADDR_WAS_PRIMARY;
5484                                                                 if (net->ro._s_addr) {
5485                                                                         sctp_free_ifa(net->ro._s_addr);
5486                                                                         net->ro._s_addr = NULL;
5487                                                                 }
5488                                                                 net->src_addr_selected = 0;
5489                                                         }
5490                                                 }
5491                                         }
5492                                 }
5493                         }
5494                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
5495                         sctp_m_freem(m);
5496                         return (EHOSTUNREACH);
5497                 }
5498                 if (ro != &iproute) {
5499                         memcpy(&iproute, ro, sizeof(*ro));
5500                 }
5501                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
5502                     (uint32_t) (ntohl(ip->ip_src.s_addr)));
5503                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
5504                     (uint32_t) (ntohl(ip->ip_dst.s_addr)));
5505                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
5506                     ro->ro_rt);
5507
5508                 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
5509                         /* failed to prepend data, give up */
5510                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5511                         sctp_m_freem(m);
5512                         return (ENOMEM);
5513                 }
5514 #ifdef  SCTP_PACKET_LOGGING
5515                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5516                         sctp_packet_log(m, packet_length);
5517 #endif
5518                 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
5519                 if (port) {
5520                         SCTP_ENABLE_UDP_CSUM(o_pak);
5521                 }
5522                 /* send it out.  table id is taken from stcb */
5523 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5524                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5525                         so = SCTP_INP_SO(inp);
5526                         SCTP_SOCKET_UNLOCK(so, 0);
5527                 }
5528 #endif
5529                 SCTP_IP_OUTPUT(ret, o_pak, ro, stcb, vrf_id);
5530 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5531                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5532                         atomic_add_int(&stcb->asoc.refcnt, 1);
5533                         SCTP_TCB_UNLOCK(stcb);
5534                         SCTP_SOCKET_LOCK(so, 0);
5535                         SCTP_TCB_LOCK(stcb);
5536                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
5537                 }
5538 #endif
5539                 SCTP_STAT_INCR(sctps_sendpackets);
5540                 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
5541                 if (ret)
5542                         SCTP_STAT_INCR(sctps_senderrors);
5543
5544                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
5545                 if (net == NULL) {
5546                         /* free tempy routes */
5547                         if (ro->ro_rt) {
5548                                 RTFREE(ro->ro_rt);
5549                                 ro->ro_rt = NULL;
5550                         }
5551                 } else {
5552                         /* PMTU check versus smallest asoc MTU goes here */
5553                         if ((ro->ro_rt != NULL) &&
5554                             (net->ro._s_addr)) {
5555                                 uint32_t mtu;
5556
5557                                 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
5558                                 if (net->port) {
5559                                         mtu -= sizeof(struct udphdr);
5560                                 }
5561                                 if (mtu && (stcb->asoc.smallest_mtu > mtu)) {
5562 #ifdef SCTP_PRINT_FOR_B_AND_M
5563                                         SCTP_PRINTF("sctp_mtu_size_reset called after ip_output mtu-change:%d\n", mtu);
5564 #endif
5565                                         sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
5566                                         net->mtu = mtu;
5567                                 }
5568                         } else if (ro->ro_rt == NULL) {
5569                                 /* route was freed */
5570                                 if (net->ro._s_addr &&
5571                                     net->src_addr_selected) {
5572                                         sctp_free_ifa(net->ro._s_addr);
5573                                         net->ro._s_addr = NULL;
5574                                 }
5575                                 net->src_addr_selected = 0;
5576                         }
5577                 }
5578                 return (ret);
5579         }
5580 #ifdef INET6
5581         else if (to->sa_family == AF_INET6) {
5582                 uint32_t flowlabel;
5583                 struct ip6_hdr *ip6h;
5584                 struct route_in6 ip6route;
5585                 struct ifnet *ifp;
5586                 u_char flowTop;
5587                 uint16_t flowBottom;
5588                 u_char tosBottom, tosTop;
5589                 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
5590                 int prev_scope = 0;
5591                 struct sockaddr_in6 lsa6_storage;
5592                 int error;
5593                 u_short prev_port = 0;
5594
5595                 if (net != NULL) {
5596                         flowlabel = net->tos_flowlabel;
5597                 } else {
5598                         flowlabel = ((struct in6pcb *)inp)->in6p_flowinfo;
5599                 }
5600
5601                 if (port) {
5602                         newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
5603                 } else {
5604                         newm = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
5605                 }
5606                 if (newm == NULL) {
5607                         sctp_m_freem(m);
5608                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5609                         return (ENOMEM);
5610                 }
5611                 if (port) {
5612                         SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
5613                         SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr) + sizeof(struct udphdr);
5614                         packet_length += sizeof(struct ip6_hdr) + sizeof(struct udphdr);
5615                 } else {
5616                         SCTP_ALIGN_TO_END(newm, sizeof(struct ip6_hdr));
5617                         SCTP_BUF_LEN(newm) = sizeof(struct ip6_hdr);
5618                         packet_length += sizeof(struct ip6_hdr);
5619                 }
5620                 SCTP_BUF_NEXT(newm) = m;
5621                 m = newm;
5622
5623                 ip6h = mtod(m, struct ip6_hdr *);
5624                 /*
5625                  * We assume here that inp_flow is in host byte order within
5626                  * the TCB!
5627                  */
5628                 flowBottom = flowlabel & 0x0000ffff;
5629                 flowTop = ((flowlabel & 0x000f0000) >> 16);
5630                 tosTop = (((flowlabel & 0xf0) >> 4) | IPV6_VERSION);
5631                 /* protect *sin6 from overwrite */
5632                 sin6 = (struct sockaddr_in6 *)to;
5633                 tmp = *sin6;
5634                 sin6 = &tmp;
5635
5636                 /* KAME hack: embed scopeid */
5637                 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
5638                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5639                         return (EINVAL);
5640                 }
5641                 if (net == NULL) {
5642                         memset(&ip6route, 0, sizeof(ip6route));
5643                         ro = (sctp_route_t *) & ip6route;
5644                         memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
5645                 } else {
5646                         ro = (sctp_route_t *) & net->ro;
5647                 }
5648                 if (stcb != NULL) {
5649                         if ((stcb->asoc.ecn_allowed) && ecn_ok) {
5650                                 /* Enable ECN */
5651                                 tosBottom = (((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) | sctp_get_ect(stcb, chk)) << 4);
5652                         } else {
5653                                 /* No ECN */
5654                                 tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
5655                         }
5656                 } else {
5657                         /* we could get no asoc if it is a O-O-T-B packet */
5658                         tosBottom = ((((struct in6pcb *)inp)->in6p_flowinfo & 0x0c) << 4);
5659                 }
5660                 ip6h->ip6_flow = htonl(((tosTop << 24) | ((tosBottom | flowTop) << 16) | flowBottom));
5661                 if (port) {
5662                         ip6h->ip6_nxt = IPPROTO_UDP;
5663                 } else {
5664                         ip6h->ip6_nxt = IPPROTO_SCTP;
5665                 }
5666                 ip6h->ip6_plen = (packet_length - sizeof(struct ip6_hdr));
5667                 ip6h->ip6_dst = sin6->sin6_addr;
5668
5669                 /*
5670                  * Add SRC address selection here: we can only reuse to a
5671                  * limited degree the kame src-addr-sel, since we can try
5672                  * their selection but it may not be bound.
5673                  */
5674                 bzero(&lsa6_tmp, sizeof(lsa6_tmp));
5675                 lsa6_tmp.sin6_family = AF_INET6;
5676                 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
5677                 lsa6 = &lsa6_tmp;
5678                 if (net && out_of_asoc_ok == 0) {
5679                         if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
5680                                 sctp_free_ifa(net->ro._s_addr);
5681                                 net->ro._s_addr = NULL;
5682                                 net->src_addr_selected = 0;
5683                                 if (ro->ro_rt) {
5684                                         RTFREE(ro->ro_rt);
5685                                         ro->ro_rt = NULL;
5686                                 }
5687                         }
5688                         if (net->src_addr_selected == 0) {
5689                                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
5690                                 /* KAME hack: embed scopeid */
5691                                 if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
5692                                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5693                                         return (EINVAL);
5694                                 }
5695                                 /* Cache the source address */
5696                                 net->ro._s_addr = sctp_source_address_selection(inp,
5697                                     stcb,
5698                                     ro,
5699                                     net,
5700                                     0,
5701                                     vrf_id);
5702                                 (void)sa6_recoverscope(sin6);
5703                                 net->src_addr_selected = 1;
5704                         }
5705                         if (net->ro._s_addr == NULL) {
5706                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
5707                                 net->src_addr_selected = 0;
5708                                 goto no_route;
5709                         }
5710                         lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
5711                 } else {
5712                         sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
5713                         /* KAME hack: embed scopeid */
5714                         if (sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone)) != 0) {
5715                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
5716                                 return (EINVAL);
5717                         }
5718                         if (over_addr == NULL) {
5719                                 struct sctp_ifa *_lsrc;
5720
5721                                 _lsrc = sctp_source_address_selection(inp, stcb, ro,
5722                                     net,
5723                                     out_of_asoc_ok,
5724                                     vrf_id);
5725                                 if (_lsrc == NULL) {
5726                                         goto no_route;
5727                                 }
5728                                 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
5729                                 sctp_free_ifa(_lsrc);
5730                         } else {
5731                                 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
5732                                 SCTP_RTALLOC((&ro->ro_rt), vrf_id);
5733                         }
5734                         (void)sa6_recoverscope(sin6);
5735                 }
5736                 lsa6->sin6_port = inp->sctp_lport;
5737
5738                 if (ro->ro_rt == NULL) {
5739                         /*
5740                          * src addr selection failed to find a route (or
5741                          * valid source addr), so we can't get there from
5742                          * here!
5743                          */
5744                         goto no_route;
5745                 }
5746                 /*
5747                  * XXX: sa6 may not have a valid sin6_scope_id in the
5748                  * non-SCOPEDROUTING case.
5749                  */
5750                 bzero(&lsa6_storage, sizeof(lsa6_storage));
5751                 lsa6_storage.sin6_family = AF_INET6;
5752                 lsa6_storage.sin6_len = sizeof(lsa6_storage);
5753                 lsa6_storage.sin6_addr = lsa6->sin6_addr;
5754                 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
5755                         SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
5756                         sctp_m_freem(m);
5757                         return (error);
5758                 }
5759                 /* XXX */
5760                 lsa6_storage.sin6_addr = lsa6->sin6_addr;
5761                 lsa6_storage.sin6_port = inp->sctp_lport;
5762                 lsa6 = &lsa6_storage;
5763                 ip6h->ip6_src = lsa6->sin6_addr;
5764
5765                 if (port) {
5766                         udp = (struct udphdr *)(ip6h + 1);
5767                         udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
5768                         udp->uh_dport = port;
5769                         udp->uh_ulen = htons(packet_length - sizeof(struct ip6_hdr));
5770                         udp->uh_sum = 0;
5771                 }
5772                 /*
5773                  * We set the hop limit now since there is a good chance
5774                  * that our ro pointer is now filled
5775                  */
5776                 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
5777                 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
5778
5779 #ifdef SCTP_DEBUG
5780                 /* Copy to be sure something bad is not happening */
5781                 sin6->sin6_addr = ip6h->ip6_dst;
5782                 lsa6->sin6_addr = ip6h->ip6_src;
5783 #endif
5784
5785                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
5786                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
5787                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
5788                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
5789                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
5790                 if (net) {
5791                         sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
5792                         /* preserve the port and scope for link local send */
5793                         prev_scope = sin6->sin6_scope_id;
5794                         prev_port = sin6->sin6_port;
5795                 }
5796                 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
5797                         /* failed to prepend data, give up */
5798                         sctp_m_freem(m);
5799                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5800                         return (ENOMEM);
5801                 }
5802 #ifdef  SCTP_PACKET_LOGGING
5803                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
5804                         sctp_packet_log(m, packet_length);
5805 #endif
5806                 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
5807                 if (port) {
5808                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
5809                                 udp->uh_sum = 0xffff;
5810                         }
5811                 }
5812                 /* send it out. table id is taken from stcb */
5813 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5814                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5815                         so = SCTP_INP_SO(inp);
5816                         SCTP_SOCKET_UNLOCK(so, 0);
5817                 }
5818 #endif
5819                 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, stcb, vrf_id);
5820 #if defined (__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
5821                 if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
5822                         atomic_add_int(&stcb->asoc.refcnt, 1);
5823                         SCTP_TCB_UNLOCK(stcb);
5824                         SCTP_SOCKET_LOCK(so, 0);
5825                         SCTP_TCB_LOCK(stcb);
5826                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
5827                 }
5828 #endif
5829                 if (net) {
5830                         /* for link local this must be done */
5831                         sin6->sin6_scope_id = prev_scope;
5832                         sin6->sin6_port = prev_port;
5833                 }
5834                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
5835                 SCTP_STAT_INCR(sctps_sendpackets);
5836                 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
5837                 if (ret) {
5838                         SCTP_STAT_INCR(sctps_senderrors);
5839                 }
5840                 if (net == NULL) {
5841                         /* Now if we had a temp route free it */
5842                         if (ro->ro_rt) {
5843                                 RTFREE(ro->ro_rt);
5844                         }
5845                 } else {
5846                         /* PMTU check versus smallest asoc MTU goes here */
5847                         if (ro->ro_rt == NULL) {
5848                                 /* Route was freed */
5849                                 if (net->ro._s_addr &&
5850                                     net->src_addr_selected) {
5851                                         sctp_free_ifa(net->ro._s_addr);
5852                                         net->ro._s_addr = NULL;
5853                                 }
5854                                 net->src_addr_selected = 0;
5855                         }
5856                         if ((ro->ro_rt != NULL) &&
5857                             (net->ro._s_addr)) {
5858                                 uint32_t mtu;
5859
5860                                 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
5861                                 if (mtu &&
5862                                     (stcb->asoc.smallest_mtu > mtu)) {
5863 #ifdef SCTP_PRINT_FOR_B_AND_M
5864                                         SCTP_PRINTF("sctp_mtu_size_reset called after ip6_output mtu-change:%d\n",
5865                                             mtu);
5866 #endif
5867                                         sctp_mtu_size_reset(inp, &stcb->asoc, mtu);
5868                                         net->mtu = mtu;
5869                                         if (net->port) {
5870                                                 net->mtu -= sizeof(struct udphdr);
5871                                         }
5872                                 }
5873                         } else if (ifp) {
5874                                 if (ND_IFINFO(ifp)->linkmtu &&
5875                                     (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
5876 #ifdef SCTP_PRINT_FOR_B_AND_M
5877                                         SCTP_PRINTF("sctp_mtu_size_reset called via ifp ND_IFINFO() linkmtu:%d\n",
5878                                             ND_IFINFO(ifp)->linkmtu);
5879 #endif
5880                                         sctp_mtu_size_reset(inp,
5881                                             &stcb->asoc,
5882                                             ND_IFINFO(ifp)->linkmtu);
5883                                 }
5884                         }
5885                 }
5886                 return (ret);
5887         }
5888 #endif
5889         else {
5890                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
5891                     ((struct sockaddr *)to)->sa_family);
5892                 sctp_m_freem(m);
5893                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
5894                 return (EFAULT);
5895         }
5896 }
5897
5898
5899 void
5900 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked
5901 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
5902     SCTP_UNUSED
5903 #endif
5904 )
5905 {
5906         struct mbuf *m, *m_at, *mp_last;
5907         struct sctp_nets *net;
5908         struct sctp_init_msg *initm;
5909         struct sctp_supported_addr_param *sup_addr;
5910         struct sctp_ecn_supported_param *ecn;
5911         struct sctp_prsctp_supported_param *prsctp;
5912         struct sctp_ecn_nonce_supported_param *ecn_nonce;
5913         struct sctp_supported_chunk_types_param *pr_supported;
5914         int cnt_inits_to = 0;
5915         int padval, ret;
5916         int num_ext;
5917         int p_len;
5918
5919         /* INIT's always go to the primary (and usually ONLY address) */
5920         mp_last = NULL;
5921         net = stcb->asoc.primary_destination;
5922         if (net == NULL) {
5923                 net = TAILQ_FIRST(&stcb->asoc.nets);
5924                 if (net == NULL) {
5925                         /* TSNH */
5926                         return;
5927                 }
5928                 /* we confirm any address we send an INIT to */
5929                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
5930                 (void)sctp_set_primary_addr(stcb, NULL, net);
5931         } else {
5932                 /* we confirm any address we send an INIT to */
5933                 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
5934         }
5935         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
5936 #ifdef INET6
5937         if (((struct sockaddr *)&(net->ro._l_addr))->sa_family == AF_INET6) {
5938                 /*
5939                  * special hook, if we are sending to link local it will not
5940                  * show up in our private address count.
5941                  */
5942                 struct sockaddr_in6 *sin6l;
5943
5944                 sin6l = &net->ro._l_addr.sin6;
5945                 if (IN6_IS_ADDR_LINKLOCAL(&sin6l->sin6_addr))
5946                         cnt_inits_to = 1;
5947         }
5948 #endif
5949         if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5950                 /* This case should not happen */
5951                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
5952                 return;
5953         }
5954         /* start the INIT timer */
5955         sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
5956
5957         m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_DONTWAIT, 1, MT_DATA);
5958         if (m == NULL) {
5959                 /* No memory, INIT timer will re-attempt. */
5960                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
5961                 return;
5962         }
5963         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
5964         /*
5965          * assume peer supports asconf in order to be able to queue local
5966          * address changes while an INIT is in flight and before the assoc
5967          * is established.
5968          */
5969         stcb->asoc.peer_supports_asconf = 1;
5970         /* Now lets put the SCTP header in place */
5971         initm = mtod(m, struct sctp_init_msg *);
5972         initm->sh.src_port = inp->sctp_lport;
5973         initm->sh.dest_port = stcb->rport;
5974         initm->sh.v_tag = 0;
5975         initm->sh.checksum = 0; /* calculate later */
5976         /* now the chunk header */
5977         initm->msg.ch.chunk_type = SCTP_INITIATION;
5978         initm->msg.ch.chunk_flags = 0;
5979         /* fill in later from mbuf we build */
5980         initm->msg.ch.chunk_length = 0;
5981         /* place in my tag */
5982         initm->msg.init.initiate_tag = htonl(stcb->asoc.my_vtag);
5983         /* set up some of the credits. */
5984         initm->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(inp->sctp_socket),
5985             SCTP_MINIMAL_RWND));
5986
5987         initm->msg.init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
5988         initm->msg.init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
5989         initm->msg.init.initial_tsn = htonl(stcb->asoc.init_seq_number);
5990         /* now the address restriction */
5991         sup_addr = (struct sctp_supported_addr_param *)((caddr_t)initm +
5992             sizeof(*initm));
5993         sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
5994 #ifdef INET6
5995         /* we support 2 types: IPv6/IPv4 */
5996         sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint16_t));
5997         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
5998         sup_addr->addr_type[1] = htons(SCTP_IPV6_ADDRESS);
5999 #else
6000         /* we support 1 type: IPv4 */
6001         sup_addr->ph.param_length = htons(sizeof(*sup_addr) + sizeof(uint8_t));
6002         sup_addr->addr_type[0] = htons(SCTP_IPV4_ADDRESS);
6003         sup_addr->addr_type[1] = htons(0);      /* this is the padding */
6004 #endif
6005         SCTP_BUF_LEN(m) += sizeof(*sup_addr) + sizeof(uint16_t);
6006
6007         if (inp->sctp_ep.adaptation_layer_indicator) {
6008                 struct sctp_adaptation_layer_indication *ali;
6009
6010                 ali = (struct sctp_adaptation_layer_indication *)(
6011                     (caddr_t)sup_addr + sizeof(*sup_addr) + sizeof(uint16_t));
6012                 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
6013                 ali->ph.param_length = htons(sizeof(*ali));
6014                 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
6015                 SCTP_BUF_LEN(m) += sizeof(*ali);
6016                 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
6017                     sizeof(*ali));
6018         } else {
6019                 ecn = (struct sctp_ecn_supported_param *)((caddr_t)sup_addr +
6020                     sizeof(*sup_addr) + sizeof(uint16_t));
6021         }
6022
6023         /* now any cookie time extensions */
6024         if (stcb->asoc.cookie_preserve_req) {
6025                 struct sctp_cookie_perserve_param *cookie_preserve;
6026
6027                 cookie_preserve = (struct sctp_cookie_perserve_param *)(ecn);
6028                 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
6029                 cookie_preserve->ph.param_length = htons(
6030                     sizeof(*cookie_preserve));
6031                 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
6032                 SCTP_BUF_LEN(m) += sizeof(*cookie_preserve);
6033                 ecn = (struct sctp_ecn_supported_param *)(
6034                     (caddr_t)cookie_preserve + sizeof(*cookie_preserve));
6035                 stcb->asoc.cookie_preserve_req = 0;
6036         }
6037         /* ECN parameter */
6038         if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
6039                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
6040                 ecn->ph.param_length = htons(sizeof(*ecn));
6041                 SCTP_BUF_LEN(m) += sizeof(*ecn);
6042                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
6043                     sizeof(*ecn));
6044         } else {
6045                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
6046         }
6047         /* And now tell the peer we do pr-sctp */
6048         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
6049         prsctp->ph.param_length = htons(sizeof(*prsctp));
6050         SCTP_BUF_LEN(m) += sizeof(*prsctp);
6051
6052         /* And now tell the peer we do all the extensions */
6053         pr_supported = (struct sctp_supported_chunk_types_param *)
6054             ((caddr_t)prsctp + sizeof(*prsctp));
6055         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
6056         num_ext = 0;
6057         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
6058         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
6059         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
6060         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
6061         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
6062         if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
6063                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
6064         /*
6065          * EY  if the initiator supports nr_sacks, need to report that to
6066          * responder in INIT chunk
6067          */
6068         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
6069                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
6070         p_len = sizeof(*pr_supported) + num_ext;
6071         pr_supported->ph.param_length = htons(p_len);
6072         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
6073         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6074
6075         /* ECN nonce: And now tell the peer we support ECN nonce */
6076         if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
6077                 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
6078                     ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
6079                 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
6080                 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
6081                 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
6082         }
6083         if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
6084                 /* Add NAT friendly parameter */
6085                 struct sctp_paramhdr *ph;
6086
6087                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6088                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
6089                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
6090                 SCTP_BUF_LEN(m) += sizeof(sizeof(struct sctp_paramhdr));
6091         }
6092         /* add authentication parameters */
6093         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
6094                 struct sctp_auth_random *randp;
6095                 struct sctp_auth_hmac_algo *hmacs;
6096                 struct sctp_auth_chunk_list *chunks;
6097
6098                 /* attach RANDOM parameter, if available */
6099                 if (stcb->asoc.authinfo.random != NULL) {
6100                         randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6101                         p_len = sizeof(*randp) + stcb->asoc.authinfo.random_len;
6102 #ifdef SCTP_AUTH_DRAFT_04
6103                         randp->ph.param_type = htons(SCTP_RANDOM);
6104                         randp->ph.param_length = htons(p_len);
6105                         bcopy(stcb->asoc.authinfo.random->key,
6106                             randp->random_data,
6107                             stcb->asoc.authinfo.random_len);
6108 #else
6109                         /* random key already contains the header */
6110                         bcopy(stcb->asoc.authinfo.random->key, randp, p_len);
6111 #endif
6112                         /* zero out any padding required */
6113                         bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
6114                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6115                 }
6116                 /* add HMAC_ALGO parameter */
6117                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6118                 p_len = sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
6119                     (uint8_t *) hmacs->hmac_ids);
6120                 if (p_len > 0) {
6121                         p_len += sizeof(*hmacs);
6122                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
6123                         hmacs->ph.param_length = htons(p_len);
6124                         /* zero out any padding required */
6125                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
6126                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6127                 }
6128                 /* add CHUNKS parameter */
6129                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
6130                 p_len = sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks,
6131                     chunks->chunk_types);
6132                 if (p_len > 0) {
6133                         p_len += sizeof(*chunks);
6134                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
6135                         chunks->ph.param_length = htons(p_len);
6136                         /* zero out any padding required */
6137                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
6138                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
6139                 }
6140         }
6141         m_at = m;
6142         /* now the addresses */
6143         {
6144                 struct sctp_scoping scp;
6145
6146                 /*
6147                  * To optimize this we could put the scoping stuff into a
6148                  * structure and remove the individual uint8's from the
6149                  * assoc structure. Then we could just sifa in the address
6150                  * within the stcb.. but for now this is a quick hack to get
6151                  * the address stuff teased apart.
6152                  */
6153                 scp.ipv4_addr_legal = stcb->asoc.ipv4_addr_legal;
6154                 scp.ipv6_addr_legal = stcb->asoc.ipv6_addr_legal;
6155                 scp.loopback_scope = stcb->asoc.loopback_scope;
6156                 scp.ipv4_local_scope = stcb->asoc.ipv4_local_scope;
6157                 scp.local_scope = stcb->asoc.local_scope;
6158                 scp.site_scope = stcb->asoc.site_scope;
6159
6160                 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
6161         }
6162
6163         /* calulate the size and update pkt header and chunk header */
6164         p_len = 0;
6165         for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
6166                 if (SCTP_BUF_NEXT(m_at) == NULL)
6167                         mp_last = m_at;
6168                 p_len += SCTP_BUF_LEN(m_at);
6169         }
6170         initm->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
6171         /*
6172          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6173          * here since the timer will drive a retranmission.
6174          */
6175
6176         /* I don't expect this to execute but we will be safe here */
6177         padval = p_len % 4;
6178         if ((padval) && (mp_last)) {
6179                 /*
6180                  * The compiler worries that mp_last may not be set even
6181                  * though I think it is impossible :-> however we add
6182                  * mp_last here just in case.
6183                  */
6184                 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
6185                 if (ret) {
6186                         /* Houston we have a problem, no space */
6187                         sctp_m_freem(m);
6188                         return;
6189                 }
6190                 p_len += padval;
6191         }
6192         SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
6193         ret = sctp_lowlevel_chunk_output(inp, stcb, net,
6194             (struct sockaddr *)&net->ro._l_addr,
6195             m, 0, NULL, 0, 0, 0, NULL, 0, net->port, so_locked, NULL);
6196         SCTPDBG(SCTP_DEBUG_OUTPUT4, "lowlevel_output - %d\n", ret);
6197         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6198         sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
6199         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
6200 }
6201
6202 struct mbuf *
6203 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
6204     int param_offset, int *abort_processing, struct sctp_chunkhdr *cp, int *nat_friendly)
6205 {
6206         /*
6207          * Given a mbuf containing an INIT or INIT-ACK with the param_offset
6208          * being equal to the beginning of the params i.e. (iphlen +
6209          * sizeof(struct sctp_init_msg) parse through the parameters to the
6210          * end of the mbuf verifying that all parameters are known.
6211          * 
6212          * For unknown parameters build and return a mbuf with
6213          * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
6214          * processing this chunk stop, and set *abort_processing to 1.
6215          * 
6216          * By having param_offset be pre-set to where parameters begin it is
6217          * hoped that this routine may be reused in the future by new
6218          * features.
6219          */
6220         struct sctp_paramhdr *phdr, params;
6221
6222         struct mbuf *mat, *op_err;
6223         char tempbuf[SCTP_PARAM_BUFFER_SIZE];
6224         int at, limit, pad_needed;
6225         uint16_t ptype, plen, padded_size;
6226         int err_at;
6227
6228         *abort_processing = 0;
6229         mat = in_initpkt;
6230         err_at = 0;
6231         limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
6232         at = param_offset;
6233         op_err = NULL;
6234         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
6235         phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
6236         while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
6237                 ptype = ntohs(phdr->param_type);
6238                 plen = ntohs(phdr->param_length);
6239                 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
6240                         /* wacked parameter */
6241                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
6242                         goto invalid_size;
6243                 }
6244                 limit -= SCTP_SIZE32(plen);
6245                 /*-
6246                  * All parameters for all chunks that we know/understand are
6247                  * listed here. We process them other places and make
6248                  * appropriate stop actions per the upper bits. However this
6249                  * is the generic routine processor's can call to get back
6250                  * an operr.. to either incorporate (init-ack) or send.
6251                  */
6252                 padded_size = SCTP_SIZE32(plen);
6253                 switch (ptype) {
6254                         /* Param's with variable size */
6255                 case SCTP_HEARTBEAT_INFO:
6256                 case SCTP_STATE_COOKIE:
6257                 case SCTP_UNRECOG_PARAM:
6258                 case SCTP_ERROR_CAUSE_IND:
6259                         /* ok skip fwd */
6260                         at += padded_size;
6261                         break;
6262                         /* Param's with variable size within a range */
6263                 case SCTP_CHUNK_LIST:
6264                 case SCTP_SUPPORTED_CHUNK_EXT:
6265                         if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
6266                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
6267                                 goto invalid_size;
6268                         }
6269                         at += padded_size;
6270                         break;
6271                 case SCTP_SUPPORTED_ADDRTYPE:
6272                         if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
6273                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
6274                                 goto invalid_size;
6275                         }
6276                         at += padded_size;
6277                         break;
6278                 case SCTP_RANDOM:
6279                         if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
6280                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
6281                                 goto invalid_size;
6282                         }
6283                         at += padded_size;
6284                         break;
6285                 case SCTP_SET_PRIM_ADDR:
6286                 case SCTP_DEL_IP_ADDRESS:
6287                 case SCTP_ADD_IP_ADDRESS:
6288                         if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
6289                             (padded_size != sizeof(struct sctp_asconf_addr_param))) {
6290                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
6291                                 goto invalid_size;
6292                         }
6293                         at += padded_size;
6294                         break;
6295                         /* Param's with a fixed size */
6296                 case SCTP_IPV4_ADDRESS:
6297                         if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
6298                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
6299                                 goto invalid_size;
6300                         }
6301                         at += padded_size;
6302                         break;
6303                 case SCTP_IPV6_ADDRESS:
6304                         if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
6305                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
6306                                 goto invalid_size;
6307                         }
6308                         at += padded_size;
6309                         break;
6310                 case SCTP_COOKIE_PRESERVE:
6311                         if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
6312                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
6313                                 goto invalid_size;
6314                         }
6315                         at += padded_size;
6316                         break;
6317                 case SCTP_HAS_NAT_SUPPORT:
6318                         *nat_friendly = 1;
6319                         /* fall through */
6320                 case SCTP_ECN_NONCE_SUPPORTED:
6321                 case SCTP_PRSCTP_SUPPORTED:
6322
6323                         if (padded_size != sizeof(struct sctp_paramhdr)) {
6324                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecnnonce/prsctp/nat support %d\n", plen);
6325                                 goto invalid_size;
6326                         }
6327                         at += padded_size;
6328                         break;
6329                 case SCTP_ECN_CAPABLE:
6330                         if (padded_size != sizeof(struct sctp_ecn_supported_param)) {
6331                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
6332                                 goto invalid_size;
6333                         }
6334                         at += padded_size;
6335                         break;
6336                 case SCTP_ULP_ADAPTATION:
6337                         if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
6338                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
6339                                 goto invalid_size;
6340                         }
6341                         at += padded_size;
6342                         break;
6343                 case SCTP_SUCCESS_REPORT:
6344                         if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
6345                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
6346                                 goto invalid_size;
6347                         }
6348                         at += padded_size;
6349                         break;
6350                 case SCTP_HOSTNAME_ADDRESS:
6351                         {
6352                                 /* We can NOT handle HOST NAME addresses!! */
6353                                 int l_len;
6354
6355                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
6356                                 *abort_processing = 1;
6357                                 if (op_err == NULL) {
6358                                         /* Ok need to try to get a mbuf */
6359 #ifdef INET6
6360                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6361 #else
6362                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6363 #endif
6364                                         l_len += plen;
6365                                         l_len += sizeof(struct sctp_paramhdr);
6366                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
6367                                         if (op_err) {
6368                                                 SCTP_BUF_LEN(op_err) = 0;
6369                                                 /*
6370                                                  * pre-reserve space for ip
6371                                                  * and sctp header  and
6372                                                  * chunk hdr
6373                                                  */
6374 #ifdef INET6
6375                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
6376 #else
6377                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
6378 #endif
6379                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
6380                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
6381                                         }
6382                                 }
6383                                 if (op_err) {
6384                                         /* If we have space */
6385                                         struct sctp_paramhdr s;
6386
6387                                         if (err_at % 4) {
6388                                                 uint32_t cpthis = 0;
6389
6390                                                 pad_needed = 4 - (err_at % 4);
6391                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
6392                                                 err_at += pad_needed;
6393                                         }
6394                                         s.param_type = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
6395                                         s.param_length = htons(sizeof(s) + plen);
6396                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
6397                                         err_at += sizeof(s);
6398                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
6399                                         if (phdr == NULL) {
6400                                                 sctp_m_freem(op_err);
6401                                                 /*
6402                                                  * we are out of memory but
6403                                                  * we still need to have a
6404                                                  * look at what to do (the
6405                                                  * system is in trouble
6406                                                  * though).
6407                                                  */
6408                                                 return (NULL);
6409                                         }
6410                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
6411                                         err_at += plen;
6412                                 }
6413                                 return (op_err);
6414                                 break;
6415                         }
6416                 default:
6417                         /*
6418                          * we do not recognize the parameter figure out what
6419                          * we do.
6420                          */
6421                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
6422                         if ((ptype & 0x4000) == 0x4000) {
6423                                 /* Report bit is set?? */
6424                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
6425                                 if (op_err == NULL) {
6426                                         int l_len;
6427
6428                                         /* Ok need to try to get an mbuf */
6429 #ifdef INET6
6430                                         l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6431 #else
6432                                         l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6433 #endif
6434                                         l_len += plen;
6435                                         l_len += sizeof(struct sctp_paramhdr);
6436                                         op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
6437                                         if (op_err) {
6438                                                 SCTP_BUF_LEN(op_err) = 0;
6439 #ifdef INET6
6440                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
6441 #else
6442                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
6443 #endif
6444                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
6445                                                 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
6446                                         }
6447                                 }
6448                                 if (op_err) {
6449                                         /* If we have space */
6450                                         struct sctp_paramhdr s;
6451
6452                                         if (err_at % 4) {
6453                                                 uint32_t cpthis = 0;
6454
6455                                                 pad_needed = 4 - (err_at % 4);
6456                                                 m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
6457                                                 err_at += pad_needed;
6458                                         }
6459                                         s.param_type = htons(SCTP_UNRECOG_PARAM);
6460                                         s.param_length = htons(sizeof(s) + plen);
6461                                         m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
6462                                         err_at += sizeof(s);
6463                                         if (plen > sizeof(tempbuf)) {
6464                                                 plen = sizeof(tempbuf);
6465                                         }
6466                                         phdr = sctp_get_next_param(mat, at, (struct sctp_paramhdr *)tempbuf, min(sizeof(tempbuf), plen));
6467                                         if (phdr == NULL) {
6468                                                 sctp_m_freem(op_err);
6469                                                 /*
6470                                                  * we are out of memory but
6471                                                  * we still need to have a
6472                                                  * look at what to do (the
6473                                                  * system is in trouble
6474                                                  * though).
6475                                                  */
6476                                                 op_err = NULL;
6477                                                 goto more_processing;
6478                                         }
6479                                         m_copyback(op_err, err_at, plen, (caddr_t)phdr);
6480                                         err_at += plen;
6481                                 }
6482                         }
6483         more_processing:
6484                         if ((ptype & 0x8000) == 0x0000) {
6485                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
6486                                 return (op_err);
6487                         } else {
6488                                 /* skip this chunk and continue processing */
6489                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
6490                                 at += SCTP_SIZE32(plen);
6491                         }
6492                         break;
6493
6494                 }
6495                 phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
6496         }
6497         return (op_err);
6498 invalid_size:
6499         SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
6500         *abort_processing = 1;
6501         if ((op_err == NULL) && phdr) {
6502                 int l_len;
6503
6504 #ifdef INET6
6505                 l_len = sizeof(struct ip6_hdr) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6506 #else
6507                 l_len = sizeof(struct ip) + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
6508 #endif
6509                 l_len += (2 * sizeof(struct sctp_paramhdr));
6510                 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_DONTWAIT, 1, MT_DATA);
6511                 if (op_err) {
6512                         SCTP_BUF_LEN(op_err) = 0;
6513 #ifdef INET6
6514                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
6515 #else
6516                         SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
6517 #endif
6518                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
6519                         SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
6520                 }
6521         }
6522         if ((op_err) && phdr) {
6523                 struct sctp_paramhdr s;
6524
6525                 if (err_at % 4) {
6526                         uint32_t cpthis = 0;
6527
6528                         pad_needed = 4 - (err_at % 4);
6529                         m_copyback(op_err, err_at, pad_needed, (caddr_t)&cpthis);
6530                         err_at += pad_needed;
6531                 }
6532                 s.param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
6533                 s.param_length = htons(sizeof(s) + sizeof(struct sctp_paramhdr));
6534                 m_copyback(op_err, err_at, sizeof(s), (caddr_t)&s);
6535                 err_at += sizeof(s);
6536                 /* Only copy back the p-hdr that caused the issue */
6537                 m_copyback(op_err, err_at, sizeof(struct sctp_paramhdr), (caddr_t)phdr);
6538         }
6539         return (op_err);
6540 }
6541
6542 static int
6543 sctp_are_there_new_addresses(struct sctp_association *asoc,
6544     struct mbuf *in_initpkt, int iphlen, int offset)
6545 {
6546         /*
6547          * Given a INIT packet, look through the packet to verify that there
6548          * are NO new addresses. As we go through the parameters add reports
6549          * of any un-understood parameters that require an error.  Also we
6550          * must return (1) to drop the packet if we see a un-understood
6551          * parameter that tells us to drop the chunk.
6552          */
6553         struct sockaddr_in sin4, *sa4;
6554
6555 #ifdef INET6
6556         struct sockaddr_in6 sin6, *sa6;
6557
6558 #endif
6559         struct sockaddr *sa_touse;
6560         struct sockaddr *sa;
6561         struct sctp_paramhdr *phdr, params;
6562         struct ip *iph;
6563
6564 #ifdef INET6
6565         struct ip6_hdr *ip6h;
6566
6567 #endif
6568         struct mbuf *mat;
6569         uint16_t ptype, plen;
6570         int err_at;
6571         uint8_t fnd;
6572         struct sctp_nets *net;
6573
6574         memset(&sin4, 0, sizeof(sin4));
6575 #ifdef INET6
6576         memset(&sin6, 0, sizeof(sin6));
6577 #endif
6578         sin4.sin_family = AF_INET;
6579         sin4.sin_len = sizeof(sin4);
6580 #ifdef INET6
6581         sin6.sin6_family = AF_INET6;
6582         sin6.sin6_len = sizeof(sin6);
6583 #endif
6584         sa_touse = NULL;
6585         /* First what about the src address of the pkt ? */
6586         iph = mtod(in_initpkt, struct ip *);
6587         switch (iph->ip_v) {
6588         case IPVERSION:
6589                 /* source addr is IPv4 */
6590                 sin4.sin_addr = iph->ip_src;
6591                 sa_touse = (struct sockaddr *)&sin4;
6592                 break;
6593 #ifdef INET6
6594         case IPV6_VERSION >> 4:
6595                 /* source addr is IPv6 */
6596                 ip6h = mtod(in_initpkt, struct ip6_hdr *);
6597                 sin6.sin6_addr = ip6h->ip6_src;
6598                 sa_touse = (struct sockaddr *)&sin6;
6599                 break;
6600 #endif
6601         default:
6602                 return (1);
6603         }
6604
6605         fnd = 0;
6606         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6607                 sa = (struct sockaddr *)&net->ro._l_addr;
6608                 if (sa->sa_family == sa_touse->sa_family) {
6609                         if (sa->sa_family == AF_INET) {
6610                                 sa4 = (struct sockaddr_in *)sa;
6611                                 if (sa4->sin_addr.s_addr ==
6612                                     sin4.sin_addr.s_addr) {
6613                                         fnd = 1;
6614                                         break;
6615                                 }
6616                         }
6617 #ifdef INET6
6618                         if (sa->sa_family == AF_INET6) {
6619                                 sa6 = (struct sockaddr_in6 *)sa;
6620                                 if (SCTP6_ARE_ADDR_EQUAL(sa6,
6621                                     &sin6)) {
6622                                         fnd = 1;
6623                                         break;
6624                                 }
6625                         }
6626 #endif
6627                 }
6628         }
6629         if (fnd == 0) {
6630                 /* New address added! no need to look futher. */
6631                 return (1);
6632         }
6633         /* Ok so far lets munge through the rest of the packet */
6634         mat = in_initpkt;
6635         err_at = 0;
6636         sa_touse = NULL;
6637         offset += sizeof(struct sctp_init_chunk);
6638         phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
6639         while (phdr) {
6640                 ptype = ntohs(phdr->param_type);
6641                 plen = ntohs(phdr->param_length);
6642                 if (ptype == SCTP_IPV4_ADDRESS) {
6643                         struct sctp_ipv4addr_param *p4, p4_buf;
6644
6645                         phdr = sctp_get_next_param(mat, offset,
6646                             (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
6647                         if (plen != sizeof(struct sctp_ipv4addr_param) ||
6648                             phdr == NULL) {
6649                                 return (1);
6650                         }
6651                         p4 = (struct sctp_ipv4addr_param *)phdr;
6652                         sin4.sin_addr.s_addr = p4->addr;
6653                         sa_touse = (struct sockaddr *)&sin4;
6654                 } else if (ptype == SCTP_IPV6_ADDRESS) {
6655                         struct sctp_ipv6addr_param *p6, p6_buf;
6656
6657                         phdr = sctp_get_next_param(mat, offset,
6658                             (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
6659                         if (plen != sizeof(struct sctp_ipv6addr_param) ||
6660                             phdr == NULL) {
6661                                 return (1);
6662                         }
6663                         p6 = (struct sctp_ipv6addr_param *)phdr;
6664 #ifdef INET6
6665                         memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
6666                             sizeof(p6->addr));
6667 #endif
6668                         sa_touse = (struct sockaddr *)&sin4;
6669                 }
6670                 if (sa_touse) {
6671                         /* ok, sa_touse points to one to check */
6672                         fnd = 0;
6673                         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
6674                                 sa = (struct sockaddr *)&net->ro._l_addr;
6675                                 if (sa->sa_family != sa_touse->sa_family) {
6676                                         continue;
6677                                 }
6678                                 if (sa->sa_family == AF_INET) {
6679                                         sa4 = (struct sockaddr_in *)sa;
6680                                         if (sa4->sin_addr.s_addr ==
6681                                             sin4.sin_addr.s_addr) {
6682                                                 fnd = 1;
6683                                                 break;
6684                                         }
6685                                 }
6686 #ifdef INET6
6687                                 if (sa->sa_family == AF_INET6) {
6688                                         sa6 = (struct sockaddr_in6 *)sa;
6689                                         if (SCTP6_ARE_ADDR_EQUAL(
6690                                             sa6, &sin6)) {
6691                                                 fnd = 1;
6692                                                 break;
6693                                         }
6694                                 }
6695 #endif
6696                         }
6697                         if (!fnd) {
6698                                 /* New addr added! no need to look further */
6699                                 return (1);
6700                         }
6701                 }
6702                 offset += SCTP_SIZE32(plen);
6703                 phdr = sctp_get_next_param(mat, offset, &params, sizeof(params));
6704         }
6705         return (0);
6706 }
6707
6708 /*
6709  * Given a MBUF chain that was sent into us containing an INIT. Build a
6710  * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
6711  * a pullup to include IPv6/4header, SCTP header and initial part of INIT
6712  * message (i.e. the struct sctp_init_msg).
6713  */
6714 void
6715 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
6716     struct mbuf *init_pkt, int iphlen, int offset, struct sctphdr *sh,
6717     struct sctp_init_chunk *init_chk, uint32_t vrf_id, uint16_t port, int hold_inp_lock)
6718 {
6719         struct sctp_association *asoc;
6720         struct mbuf *m, *m_at, *m_tmp, *m_cookie, *op_err, *mp_last;
6721         struct sctp_init_msg *initackm_out;
6722         struct sctp_ecn_supported_param *ecn;
6723         struct sctp_prsctp_supported_param *prsctp;
6724         struct sctp_ecn_nonce_supported_param *ecn_nonce;
6725         struct sctp_supported_chunk_types_param *pr_supported;
6726         union sctp_sockstore store, store1, *over_addr;
6727         struct sockaddr_in *sin, *to_sin;
6728
6729 #ifdef INET6
6730         struct sockaddr_in6 *sin6, *to_sin6;
6731
6732 #endif
6733         struct ip *iph;
6734
6735 #ifdef INET6
6736         struct ip6_hdr *ip6;
6737
6738 #endif
6739         struct sockaddr *to;
6740         struct sctp_state_cookie stc;
6741         struct sctp_nets *net = NULL;
6742         uint8_t *signature = NULL;
6743         int cnt_inits_to = 0;
6744         uint16_t his_limit, i_want;
6745         int abort_flag, padval;
6746         int num_ext;
6747         int p_len;
6748         int nat_friendly = 0;
6749         struct socket *so;
6750
6751         if (stcb)
6752                 asoc = &stcb->asoc;
6753         else
6754                 asoc = NULL;
6755         mp_last = NULL;
6756         if ((asoc != NULL) &&
6757             (SCTP_GET_STATE(asoc) != SCTP_STATE_COOKIE_WAIT) &&
6758             (sctp_are_there_new_addresses(asoc, init_pkt, iphlen, offset))) {
6759                 /* new addresses, out of here in non-cookie-wait states */
6760                 /*
6761                  * Send a ABORT, we don't add the new address error clause
6762                  * though we even set the T bit and copy in the 0 tag.. this
6763                  * looks no different than if no listener was present.
6764                  */
6765                 sctp_send_abort(init_pkt, iphlen, sh, 0, NULL, vrf_id, port);
6766                 return;
6767         }
6768         abort_flag = 0;
6769         op_err = sctp_arethere_unrecognized_parameters(init_pkt,
6770             (offset + sizeof(struct sctp_init_chunk)),
6771             &abort_flag, (struct sctp_chunkhdr *)init_chk, &nat_friendly);
6772         if (abort_flag) {
6773 do_a_abort:
6774                 sctp_send_abort(init_pkt, iphlen, sh,
6775                     init_chk->init.initiate_tag, op_err, vrf_id, port);
6776                 return;
6777         }
6778         m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
6779         if (m == NULL) {
6780                 /* No memory, INIT timer will re-attempt. */
6781                 if (op_err)
6782                         sctp_m_freem(op_err);
6783                 return;
6784         }
6785         SCTP_BUF_LEN(m) = sizeof(struct sctp_init_msg);
6786
6787         /* the time I built cookie */
6788         (void)SCTP_GETTIME_TIMEVAL(&stc.time_entered);
6789
6790         /* populate any tie tags */
6791         if (asoc != NULL) {
6792                 /* unlock before tag selections */
6793                 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
6794                 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
6795                 stc.cookie_life = asoc->cookie_life;
6796                 net = asoc->primary_destination;
6797         } else {
6798                 stc.tie_tag_my_vtag = 0;
6799                 stc.tie_tag_peer_vtag = 0;
6800                 /* life I will award this cookie */
6801                 stc.cookie_life = inp->sctp_ep.def_cookie_life;
6802         }
6803
6804         /* copy in the ports for later check */
6805         stc.myport = sh->dest_port;
6806         stc.peerport = sh->src_port;
6807
6808         /*
6809          * If we wanted to honor cookie life extentions, we would add to
6810          * stc.cookie_life. For now we should NOT honor any extension
6811          */
6812         stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
6813         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6814                 struct inpcb *in_inp;
6815
6816                 /* Its a V6 socket */
6817                 in_inp = (struct inpcb *)inp;
6818                 stc.ipv6_addr_legal = 1;
6819                 /* Now look at the binding flag to see if V4 will be legal */
6820                 if (SCTP_IPV6_V6ONLY(in_inp) == 0) {
6821                         stc.ipv4_addr_legal = 1;
6822                 } else {
6823                         /* V4 addresses are NOT legal on the association */
6824                         stc.ipv4_addr_legal = 0;
6825                 }
6826         } else {
6827                 /* Its a V4 socket, no - V6 */
6828                 stc.ipv4_addr_legal = 1;
6829                 stc.ipv6_addr_legal = 0;
6830         }
6831
6832 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
6833         stc.ipv4_scope = 1;
6834 #else
6835         stc.ipv4_scope = 0;
6836 #endif
6837         /* now for scope setup */
6838         memset((caddr_t)&store, 0, sizeof(store));
6839         memset((caddr_t)&store1, 0, sizeof(store1));
6840         sin = &store.sin;
6841         to_sin = &store1.sin;
6842 #ifdef INET6
6843         sin6 = &store.sin6;
6844         to_sin6 = &store1.sin6;
6845 #endif
6846         iph = mtod(init_pkt, struct ip *);
6847         /* establish the to_addr's */
6848         switch (iph->ip_v) {
6849         case IPVERSION:
6850                 to_sin->sin_port = sh->dest_port;
6851                 to_sin->sin_family = AF_INET;
6852                 to_sin->sin_len = sizeof(struct sockaddr_in);
6853                 to_sin->sin_addr = iph->ip_dst;
6854                 break;
6855 #ifdef INET6
6856         case IPV6_VERSION >> 4:
6857                 ip6 = mtod(init_pkt, struct ip6_hdr *);
6858                 to_sin6->sin6_addr = ip6->ip6_dst;
6859                 to_sin6->sin6_scope_id = 0;
6860                 to_sin6->sin6_port = sh->dest_port;
6861                 to_sin6->sin6_family = AF_INET6;
6862                 to_sin6->sin6_len = sizeof(struct sockaddr_in6);
6863                 break;
6864 #endif
6865         default:
6866                 goto do_a_abort;
6867                 break;
6868         };
6869
6870         if (net == NULL) {
6871                 to = (struct sockaddr *)&store;
6872                 switch (iph->ip_v) {
6873                 case IPVERSION:
6874                         {
6875                                 sin->sin_family = AF_INET;
6876                                 sin->sin_len = sizeof(struct sockaddr_in);
6877                                 sin->sin_port = sh->src_port;
6878                                 sin->sin_addr = iph->ip_src;
6879                                 /* lookup address */
6880                                 stc.address[0] = sin->sin_addr.s_addr;
6881                                 stc.address[1] = 0;
6882                                 stc.address[2] = 0;
6883                                 stc.address[3] = 0;
6884                                 stc.addr_type = SCTP_IPV4_ADDRESS;
6885                                 /* local from address */
6886                                 stc.laddress[0] = to_sin->sin_addr.s_addr;
6887                                 stc.laddress[1] = 0;
6888                                 stc.laddress[2] = 0;
6889                                 stc.laddress[3] = 0;
6890                                 stc.laddr_type = SCTP_IPV4_ADDRESS;
6891                                 /* scope_id is only for v6 */
6892                                 stc.scope_id = 0;
6893 #ifndef SCTP_DONT_DO_PRIVADDR_SCOPE
6894                                 if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
6895                                         stc.ipv4_scope = 1;
6896                                 }
6897 #else
6898                                 stc.ipv4_scope = 1;
6899 #endif                          /* SCTP_DONT_DO_PRIVADDR_SCOPE */
6900                                 /* Must use the address in this case */
6901                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin, vrf_id)) {
6902                                         stc.loopback_scope = 1;
6903                                         stc.ipv4_scope = 1;
6904                                         stc.site_scope = 1;
6905                                         stc.local_scope = 0;
6906                                 }
6907                                 break;
6908                         }
6909 #ifdef INET6
6910                 case IPV6_VERSION >> 4:
6911                         {
6912                                 ip6 = mtod(init_pkt, struct ip6_hdr *);
6913                                 sin6->sin6_family = AF_INET6;
6914                                 sin6->sin6_len = sizeof(struct sockaddr_in6);
6915                                 sin6->sin6_port = sh->src_port;
6916                                 sin6->sin6_addr = ip6->ip6_src;
6917                                 /* lookup address */
6918                                 memcpy(&stc.address, &sin6->sin6_addr,
6919                                     sizeof(struct in6_addr));
6920                                 sin6->sin6_scope_id = 0;
6921                                 stc.addr_type = SCTP_IPV6_ADDRESS;
6922                                 stc.scope_id = 0;
6923                                 if (sctp_is_address_on_local_host((struct sockaddr *)sin6, vrf_id)) {
6924                                         /*
6925                                          * FIX ME: does this have scope from
6926                                          * rcvif?
6927                                          */
6928                                         (void)sa6_recoverscope(sin6);
6929                                         stc.scope_id = sin6->sin6_scope_id;
6930                                         sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
6931                                         stc.loopback_scope = 1;
6932                                         stc.local_scope = 0;
6933                                         stc.site_scope = 1;
6934                                         stc.ipv4_scope = 1;
6935                                 } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
6936                                         /*
6937                                          * If the new destination is a
6938                                          * LINK_LOCAL we must have common
6939                                          * both site and local scope. Don't
6940                                          * set local scope though since we
6941                                          * must depend on the source to be
6942                                          * added implicitly. We cannot
6943                                          * assure just because we share one
6944                                          * link that all links are common.
6945                                          */
6946                                         stc.local_scope = 0;
6947                                         stc.site_scope = 1;
6948                                         stc.ipv4_scope = 1;
6949                                         /*
6950                                          * we start counting for the private
6951                                          * address stuff at 1. since the
6952                                          * link local we source from won't
6953                                          * show up in our scoped count.
6954                                          */
6955                                         cnt_inits_to = 1;
6956                                         /*
6957                                          * pull out the scope_id from
6958                                          * incoming pkt
6959                                          */
6960                                         /*
6961                                          * FIX ME: does this have scope from
6962                                          * rcvif?
6963                                          */
6964                                         (void)sa6_recoverscope(sin6);
6965                                         stc.scope_id = sin6->sin6_scope_id;
6966                                         sa6_embedscope(sin6, MODULE_GLOBAL(MOD_INET6, ip6_use_defzone));
6967                                 } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
6968                                         /*
6969                                          * If the new destination is
6970                                          * SITE_LOCAL then we must have site
6971                                          * scope in common.
6972                                          */
6973                                         stc.site_scope = 1;
6974                                 }
6975                                 memcpy(&stc.laddress, &to_sin6->sin6_addr, sizeof(struct in6_addr));
6976                                 stc.laddr_type = SCTP_IPV6_ADDRESS;
6977                                 break;
6978                         }
6979 #endif
6980                 default:
6981                         /* TSNH */
6982                         goto do_a_abort;
6983                         break;
6984                 }
6985         } else {
6986                 /* set the scope per the existing tcb */
6987
6988 #ifdef INET6
6989                 struct sctp_nets *lnet;
6990
6991 #endif
6992
6993                 stc.loopback_scope = asoc->loopback_scope;
6994                 stc.ipv4_scope = asoc->ipv4_local_scope;
6995                 stc.site_scope = asoc->site_scope;
6996                 stc.local_scope = asoc->local_scope;
6997 #ifdef INET6
6998                 /* Why do we not consider IPv4 LL addresses? */
6999                 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
7000                         if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
7001                                 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
7002                                         /*
7003                                          * if we have a LL address, start
7004                                          * counting at 1.
7005                                          */
7006                                         cnt_inits_to = 1;
7007                                 }
7008                         }
7009                 }
7010 #endif
7011                 /* use the net pointer */
7012                 to = (struct sockaddr *)&net->ro._l_addr;
7013                 switch (to->sa_family) {
7014                 case AF_INET:
7015                         sin = (struct sockaddr_in *)to;
7016                         stc.address[0] = sin->sin_addr.s_addr;
7017                         stc.address[1] = 0;
7018                         stc.address[2] = 0;
7019                         stc.address[3] = 0;
7020                         stc.addr_type = SCTP_IPV4_ADDRESS;
7021                         if (net->src_addr_selected == 0) {
7022                                 /*
7023                                  * strange case here, the INIT should have
7024                                  * did the selection.
7025                                  */
7026                                 net->ro._s_addr = sctp_source_address_selection(inp,
7027                                     stcb, (sctp_route_t *) & net->ro,
7028                                     net, 0, vrf_id);
7029                                 if (net->ro._s_addr == NULL)
7030                                         return;
7031
7032                                 net->src_addr_selected = 1;
7033
7034                         }
7035                         stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
7036                         stc.laddress[1] = 0;
7037                         stc.laddress[2] = 0;
7038                         stc.laddress[3] = 0;
7039                         stc.laddr_type = SCTP_IPV4_ADDRESS;
7040                         break;
7041 #ifdef INET6
7042                 case AF_INET6:
7043                         sin6 = (struct sockaddr_in6 *)to;
7044                         memcpy(&stc.address, &sin6->sin6_addr,
7045                             sizeof(struct in6_addr));
7046                         stc.addr_type = SCTP_IPV6_ADDRESS;
7047                         if (net->src_addr_selected == 0) {
7048                                 /*
7049                                  * strange case here, the INIT should have
7050                                  * did the selection.
7051                                  */
7052                                 net->ro._s_addr = sctp_source_address_selection(inp,
7053                                     stcb, (sctp_route_t *) & net->ro,
7054                                     net, 0, vrf_id);
7055                                 if (net->ro._s_addr == NULL)
7056                                         return;
7057
7058                                 net->src_addr_selected = 1;
7059                         }
7060                         memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
7061                             sizeof(struct in6_addr));
7062                         stc.laddr_type = SCTP_IPV6_ADDRESS;
7063                         break;
7064 #endif
7065                 }
7066         }
7067         /* Now lets put the SCTP header in place */
7068         initackm_out = mtod(m, struct sctp_init_msg *);
7069         initackm_out->sh.src_port = inp->sctp_lport;
7070         initackm_out->sh.dest_port = sh->src_port;
7071         initackm_out->sh.v_tag = init_chk->init.initiate_tag;
7072         /* Save it off for quick ref */
7073         stc.peers_vtag = init_chk->init.initiate_tag;
7074         initackm_out->sh.checksum = 0;  /* calculate later */
7075         /* who are we */
7076         memcpy(stc.identification, SCTP_VERSION_STRING,
7077             min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
7078         /* now the chunk header */
7079         initackm_out->msg.ch.chunk_type = SCTP_INITIATION_ACK;
7080         initackm_out->msg.ch.chunk_flags = 0;
7081         /* fill in later from mbuf we build */
7082         initackm_out->msg.ch.chunk_length = 0;
7083         /* place in my tag */
7084         if ((asoc != NULL) &&
7085             ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
7086             (SCTP_GET_STATE(asoc) == SCTP_STATE_INUSE) ||
7087             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED))) {
7088                 /* re-use the v-tags and init-seq here */
7089                 initackm_out->msg.init.initiate_tag = htonl(asoc->my_vtag);
7090                 initackm_out->msg.init.initial_tsn = htonl(asoc->init_seq_number);
7091         } else {
7092                 uint32_t vtag, itsn;
7093
7094                 if (hold_inp_lock) {
7095                         SCTP_INP_INCR_REF(inp);
7096                         SCTP_INP_RUNLOCK(inp);
7097                 }
7098                 if (asoc) {
7099                         atomic_add_int(&asoc->refcnt, 1);
7100                         SCTP_TCB_UNLOCK(stcb);
7101         new_tag:
7102                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
7103                         if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
7104                                 /*
7105                                  * Got a duplicate vtag on some guy behind a
7106                                  * nat make sure we don't use it.
7107                                  */
7108                                 goto new_tag;
7109                         }
7110                         initackm_out->msg.init.initiate_tag = htonl(vtag);
7111                         /* get a TSN to use too */
7112                         itsn = sctp_select_initial_TSN(&inp->sctp_ep);
7113                         initackm_out->msg.init.initial_tsn = htonl(itsn);
7114                         SCTP_TCB_LOCK(stcb);
7115                         atomic_add_int(&asoc->refcnt, -1);
7116                 } else {
7117                         vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
7118                         initackm_out->msg.init.initiate_tag = htonl(vtag);
7119                         /* get a TSN to use too */
7120                         initackm_out->msg.init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
7121                 }
7122                 if (hold_inp_lock) {
7123                         SCTP_INP_RLOCK(inp);
7124                         SCTP_INP_DECR_REF(inp);
7125                 }
7126         }
7127         /* save away my tag to */
7128         stc.my_vtag = initackm_out->msg.init.initiate_tag;
7129
7130         /* set up some of the credits. */
7131         so = inp->sctp_socket;
7132         if (so == NULL) {
7133                 /* memory problem */
7134                 sctp_m_freem(m);
7135                 return;
7136         } else {
7137                 initackm_out->msg.init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
7138         }
7139         /* set what I want */
7140         his_limit = ntohs(init_chk->init.num_inbound_streams);
7141         /* choose what I want */
7142         if (asoc != NULL) {
7143                 if (asoc->streamoutcnt > inp->sctp_ep.pre_open_stream_count) {
7144                         i_want = asoc->streamoutcnt;
7145                 } else {
7146                         i_want = inp->sctp_ep.pre_open_stream_count;
7147                 }
7148         } else {
7149                 i_want = inp->sctp_ep.pre_open_stream_count;
7150         }
7151         if (his_limit < i_want) {
7152                 /* I Want more :< */
7153                 initackm_out->msg.init.num_outbound_streams = init_chk->init.num_inbound_streams;
7154         } else {
7155                 /* I can have what I want :> */
7156                 initackm_out->msg.init.num_outbound_streams = htons(i_want);
7157         }
7158         /* tell him his limt. */
7159         initackm_out->msg.init.num_inbound_streams =
7160             htons(inp->sctp_ep.max_open_streams_intome);
7161         /* setup the ECN pointer */
7162
7163         if (inp->sctp_ep.adaptation_layer_indicator) {
7164                 struct sctp_adaptation_layer_indication *ali;
7165
7166                 ali = (struct sctp_adaptation_layer_indication *)(
7167                     (caddr_t)initackm_out + sizeof(*initackm_out));
7168                 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
7169                 ali->ph.param_length = htons(sizeof(*ali));
7170                 ali->indication = ntohl(inp->sctp_ep.adaptation_layer_indicator);
7171                 SCTP_BUF_LEN(m) += sizeof(*ali);
7172                 ecn = (struct sctp_ecn_supported_param *)((caddr_t)ali +
7173                     sizeof(*ali));
7174         } else {
7175                 ecn = (struct sctp_ecn_supported_param *)(
7176                     (caddr_t)initackm_out + sizeof(*initackm_out));
7177         }
7178
7179         /* ECN parameter */
7180         if (SCTP_BASE_SYSCTL(sctp_ecn_enable) == 1) {
7181                 ecn->ph.param_type = htons(SCTP_ECN_CAPABLE);
7182                 ecn->ph.param_length = htons(sizeof(*ecn));
7183                 SCTP_BUF_LEN(m) += sizeof(*ecn);
7184
7185                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn +
7186                     sizeof(*ecn));
7187         } else {
7188                 prsctp = (struct sctp_prsctp_supported_param *)((caddr_t)ecn);
7189         }
7190         /* And now tell the peer we do  pr-sctp */
7191         prsctp->ph.param_type = htons(SCTP_PRSCTP_SUPPORTED);
7192         prsctp->ph.param_length = htons(sizeof(*prsctp));
7193         SCTP_BUF_LEN(m) += sizeof(*prsctp);
7194         if (nat_friendly) {
7195                 /* Add NAT friendly parameter */
7196                 struct sctp_paramhdr *ph;
7197
7198                 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7199                 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
7200                 ph->param_length = htons(sizeof(struct sctp_paramhdr));
7201                 SCTP_BUF_LEN(m) += sizeof(sizeof(struct sctp_paramhdr));
7202         }
7203         /* And now tell the peer we do all the extensions */
7204         pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7205         pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
7206         num_ext = 0;
7207         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
7208         pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
7209         pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
7210         pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
7211         pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
7212         if (!SCTP_BASE_SYSCTL(sctp_auth_disable))
7213                 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
7214         /*
7215          * EY  if the sysctl variable is set, tell the assoc. initiator that
7216          * we do nr_sack
7217          */
7218         if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off))
7219                 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
7220         p_len = sizeof(*pr_supported) + num_ext;
7221         pr_supported->ph.param_length = htons(p_len);
7222         bzero((caddr_t)pr_supported + p_len, SCTP_SIZE32(p_len) - p_len);
7223         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7224
7225         /* ECN nonce: And now tell the peer we support ECN nonce */
7226         if (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) {
7227                 ecn_nonce = (struct sctp_ecn_nonce_supported_param *)
7228                     ((caddr_t)pr_supported + SCTP_SIZE32(p_len));
7229                 ecn_nonce->ph.param_type = htons(SCTP_ECN_NONCE_SUPPORTED);
7230                 ecn_nonce->ph.param_length = htons(sizeof(*ecn_nonce));
7231                 SCTP_BUF_LEN(m) += sizeof(*ecn_nonce);
7232         }
7233         /* add authentication parameters */
7234         if (!SCTP_BASE_SYSCTL(sctp_auth_disable)) {
7235                 struct sctp_auth_random *randp;
7236                 struct sctp_auth_hmac_algo *hmacs;
7237                 struct sctp_auth_chunk_list *chunks;
7238                 uint16_t random_len;
7239
7240                 /* generate and add RANDOM parameter */
7241                 random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
7242                 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7243                 randp->ph.param_type = htons(SCTP_RANDOM);
7244                 p_len = sizeof(*randp) + random_len;
7245                 randp->ph.param_length = htons(p_len);
7246                 SCTP_READ_RANDOM(randp->random_data, random_len);
7247                 /* zero out any padding required */
7248                 bzero((caddr_t)randp + p_len, SCTP_SIZE32(p_len) - p_len);
7249                 SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7250
7251                 /* add HMAC_ALGO parameter */
7252                 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7253                 p_len = sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
7254                     (uint8_t *) hmacs->hmac_ids);
7255                 if (p_len > 0) {
7256                         p_len += sizeof(*hmacs);
7257                         hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
7258                         hmacs->ph.param_length = htons(p_len);
7259                         /* zero out any padding required */
7260                         bzero((caddr_t)hmacs + p_len, SCTP_SIZE32(p_len) - p_len);
7261                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7262                 }
7263                 /* add CHUNKS parameter */
7264                 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+SCTP_BUF_LEN(m));
7265                 p_len = sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
7266                     chunks->chunk_types);
7267                 if (p_len > 0) {
7268                         p_len += sizeof(*chunks);
7269                         chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
7270                         chunks->ph.param_length = htons(p_len);
7271                         /* zero out any padding required */
7272                         bzero((caddr_t)chunks + p_len, SCTP_SIZE32(p_len) - p_len);
7273                         SCTP_BUF_LEN(m) += SCTP_SIZE32(p_len);
7274                 }
7275         }
7276         m_at = m;
7277         /* now the addresses */
7278         {
7279                 struct sctp_scoping scp;
7280
7281                 /*
7282                  * To optimize this we could put the scoping stuff into a
7283                  * structure and remove the individual uint8's from the stc
7284                  * structure. Then we could just sifa in the address within
7285                  * the stc.. but for now this is a quick hack to get the
7286                  * address stuff teased apart.
7287                  */
7288                 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
7289                 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
7290                 scp.loopback_scope = stc.loopback_scope;
7291                 scp.ipv4_local_scope = stc.ipv4_scope;
7292                 scp.local_scope = stc.local_scope;
7293                 scp.site_scope = stc.site_scope;
7294                 m_at = sctp_add_addresses_to_i_ia(inp, &scp, m_at, cnt_inits_to);
7295         }
7296
7297         /* tack on the operational error if present */
7298         if (op_err) {
7299                 struct mbuf *ol;
7300                 int llen;
7301
7302                 llen = 0;
7303                 ol = op_err;
7304                 while (ol) {
7305                         llen += SCTP_BUF_LEN(ol);
7306                         ol = SCTP_BUF_NEXT(ol);
7307                 }
7308                 if (llen % 4) {
7309                         /* must add a pad to the param */
7310                         uint32_t cpthis = 0;
7311                         int padlen;
7312
7313                         padlen = 4 - (llen % 4);
7314                         m_copyback(op_err, llen, padlen, (caddr_t)&cpthis);
7315                 }
7316                 while (SCTP_BUF_NEXT(m_at) != NULL) {
7317                         m_at = SCTP_BUF_NEXT(m_at);
7318                 }
7319                 SCTP_BUF_NEXT(m_at) = op_err;
7320                 while (SCTP_BUF_NEXT(m_at) != NULL) {
7321                         m_at = SCTP_BUF_NEXT(m_at);
7322                 }
7323         }
7324         /* pre-calulate the size and update pkt header and chunk header */
7325         p_len = 0;
7326         for (m_tmp = m; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
7327                 p_len += SCTP_BUF_LEN(m_tmp);
7328                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
7329                         /* m_tmp should now point to last one */
7330                         break;
7331                 }
7332         }
7333
7334         /* Now we must build a cookie */
7335         m_cookie = sctp_add_cookie(inp, init_pkt, offset, m,
7336             sizeof(struct sctphdr), &stc, &signature);
7337         if (m_cookie == NULL) {
7338                 /* memory problem */
7339                 sctp_m_freem(m);
7340                 return;
7341         }
7342         /* Now append the cookie to the end and update the space/size */
7343         SCTP_BUF_NEXT(m_tmp) = m_cookie;
7344
7345         for (m_tmp = m_cookie; m_tmp; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
7346                 p_len += SCTP_BUF_LEN(m_tmp);
7347                 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
7348                         /* m_tmp should now point to last one */
7349                         mp_last = m_tmp;
7350                         break;
7351                 }
7352         }
7353         /*
7354          * Place in the size, but we don't include the last pad (if any) in
7355          * the INIT-ACK.
7356          */
7357         initackm_out->msg.ch.chunk_length = htons((p_len - sizeof(struct sctphdr)));
7358
7359         /*
7360          * Time to sign the cookie, we don't sign over the cookie signature
7361          * though thus we set trailer.
7362          */
7363         (void)sctp_hmac_m(SCTP_HMAC,
7364             (uint8_t *) inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
7365             SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
7366             (uint8_t *) signature, SCTP_SIGNATURE_SIZE);
7367         /*
7368          * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
7369          * here since the timer will drive a retranmission.
7370          */
7371         padval = p_len % 4;
7372         if ((padval) && (mp_last)) {
7373                 /* see my previous comments on mp_last */
7374                 int ret;
7375
7376                 ret = sctp_add_pad_tombuf(mp_last, (4 - padval));
7377                 if (ret) {
7378                         /* Houston we have a problem, no space */
7379                         sctp_m_freem(m);
7380                         return;
7381                 }
7382                 p_len += padval;
7383         }
7384         if (stc.loopback_scope) {
7385                 over_addr = &store1;
7386         } else {
7387                 over_addr = NULL;
7388
7389         }
7390
7391         (void)sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
7392             0, NULL, 0, port, SCTP_SO_NOT_LOCKED, over_addr);
7393         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
7394 }
7395
7396
7397 void
7398 sctp_insert_on_wheel(struct sctp_tcb *stcb,
7399     struct sctp_association *asoc,
7400     struct sctp_stream_out *strq, int holds_lock)
7401 {
7402         struct sctp_stream_out *stre, *strn;
7403
7404         if (holds_lock == 0) {
7405                 SCTP_TCB_SEND_LOCK(stcb);
7406         }
7407         if ((strq->next_spoke.tqe_next) ||
7408             (strq->next_spoke.tqe_prev)) {
7409                 /* already on wheel */
7410                 goto outof_here;
7411         }
7412         stre = TAILQ_FIRST(&asoc->out_wheel);
7413         if (stre == NULL) {
7414                 /* only one on wheel */
7415                 TAILQ_INSERT_HEAD(&asoc->out_wheel, strq, next_spoke);
7416                 goto outof_here;
7417         }
7418         for (; stre; stre = strn) {
7419                 strn = TAILQ_NEXT(stre, next_spoke);
7420                 if (stre->stream_no > strq->stream_no) {
7421                         TAILQ_INSERT_BEFORE(stre, strq, next_spoke);
7422                         goto outof_here;
7423                 } else if (stre->stream_no == strq->stream_no) {
7424                         /* huh, should not happen */
7425                         goto outof_here;
7426                 } else if (strn == NULL) {
7427                         /* next one is null */
7428                         TAILQ_INSERT_AFTER(&asoc->out_wheel, stre, strq,
7429                             next_spoke);
7430                 }
7431         }
7432 outof_here:
7433         if (holds_lock == 0) {
7434                 SCTP_TCB_SEND_UNLOCK(stcb);
7435         }
7436 }
7437
7438 static void
7439 sctp_remove_from_wheel(struct sctp_tcb *stcb,
7440     struct sctp_association *asoc,
7441     struct sctp_stream_out *strq)
7442 {
7443         /* take off and then setup so we know it is not on the wheel */
7444         SCTP_TCB_SEND_LOCK(stcb);
7445         if (TAILQ_FIRST(&strq->outqueue)) {
7446                 /* more was added */
7447                 SCTP_TCB_SEND_UNLOCK(stcb);
7448                 return;
7449         }
7450         TAILQ_REMOVE(&asoc->out_wheel, strq, next_spoke);
7451         strq->next_spoke.tqe_next = NULL;
7452         strq->next_spoke.tqe_prev = NULL;
7453         SCTP_TCB_SEND_UNLOCK(stcb);
7454 }
7455
7456 static void
7457 sctp_prune_prsctp(struct sctp_tcb *stcb,
7458     struct sctp_association *asoc,
7459     struct sctp_sndrcvinfo *srcv,
7460     int dataout)
7461 {
7462         int freed_spc = 0;
7463         struct sctp_tmit_chunk *chk, *nchk;
7464
7465         SCTP_TCB_LOCK_ASSERT(stcb);
7466         if ((asoc->peer_supports_prsctp) &&
7467             (asoc->sent_queue_cnt_removeable > 0)) {
7468                 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
7469                         /*
7470                          * Look for chunks marked with the PR_SCTP flag AND
7471                          * the buffer space flag. If the one being sent is
7472                          * equal or greater priority then purge the old one
7473                          * and free some space.
7474                          */
7475                         if (PR_SCTP_BUF_ENABLED(chk->flags)) {
7476                                 /*
7477                                  * This one is PR-SCTP AND buffer space
7478                                  * limited type
7479                                  */
7480                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
7481                                         /*
7482                                          * Lower numbers equates to higher
7483                                          * priority so if the one we are
7484                                          * looking at has a larger or equal
7485                                          * priority we want to drop the data
7486                                          * and NOT retransmit it.
7487                                          */
7488                                         if (chk->data) {
7489                                                 /*
7490                                                  * We release the book_size
7491                                                  * if the mbuf is here
7492                                                  */
7493                                                 int ret_spc;
7494                                                 int cause;
7495
7496                                                 if (chk->sent > SCTP_DATAGRAM_UNSENT)
7497                                                         cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_SENT;
7498                                                 else
7499                                                         cause = SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT;
7500                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
7501                                                     cause,
7502                                                     &asoc->sent_queue, SCTP_SO_LOCKED);
7503                                                 freed_spc += ret_spc;
7504                                                 if (freed_spc >= dataout) {
7505                                                         return;
7506                                                 }
7507                                         }       /* if chunk was present */
7508                                 }       /* if of sufficent priority */
7509                         }       /* if chunk has enabled */
7510                 }               /* tailqforeach */
7511
7512                 chk = TAILQ_FIRST(&asoc->send_queue);
7513                 while (chk) {
7514                         nchk = TAILQ_NEXT(chk, sctp_next);
7515                         /* Here we must move to the sent queue and mark */
7516                         if (PR_SCTP_TTL_ENABLED(chk->flags)) {
7517                                 if (chk->rec.data.timetodrop.tv_sec >= (long)srcv->sinfo_timetolive) {
7518                                         if (chk->data) {
7519                                                 /*
7520                                                  * We release the book_size
7521                                                  * if the mbuf is here
7522                                                  */
7523                                                 int ret_spc;
7524
7525                                                 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
7526                                                     SCTP_RESPONSE_TO_USER_REQ | SCTP_NOTIFY_DATAGRAM_UNSENT,
7527                                                     &asoc->send_queue, SCTP_SO_LOCKED);
7528
7529                                                 freed_spc += ret_spc;
7530                                                 if (freed_spc >= dataout) {
7531                                                         return;
7532                                                 }
7533                                         }       /* end if chk->data */
7534                                 }       /* end if right class */
7535                         }       /* end if chk pr-sctp */
7536                         chk = nchk;
7537                 }               /* end while (chk) */
7538         }                       /* if enabled in asoc */
7539 }
7540
7541 int
7542 sctp_get_frag_point(struct sctp_tcb *stcb,
7543     struct sctp_association *asoc)
7544 {
7545         int siz, ovh;
7546
7547         /*
7548          * For endpoints that have both v6 and v4 addresses we must reserve
7549          * room for the ipv6 header, for those that are only dealing with V4
7550          * we use a larger frag point.
7551          */
7552         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
7553                 ovh = SCTP_MED_OVERHEAD;
7554         } else {
7555                 ovh = SCTP_MED_V4_OVERHEAD;
7556         }
7557
7558         if (stcb->asoc.sctp_frag_point > asoc->smallest_mtu)
7559                 siz = asoc->smallest_mtu - ovh;
7560         else
7561                 siz = (stcb->asoc.sctp_frag_point - ovh);
7562         /*
7563          * if (siz > (MCLBYTES-sizeof(struct sctp_data_chunk))) {
7564          */
7565         /* A data chunk MUST fit in a cluster */
7566         /* siz = (MCLBYTES - sizeof(struct sctp_data_chunk)); */
7567         /* } */
7568
7569         /* adjust for an AUTH chunk if DATA requires auth */
7570         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks))
7571                 siz -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
7572
7573         if (siz % 4) {
7574                 /* make it an even word boundary please */
7575                 siz -= (siz % 4);
7576         }
7577         return (siz);
7578 }
7579
7580 static void
7581 sctp_set_prsctp_policy(struct sctp_tcb *stcb,
7582     struct sctp_stream_queue_pending *sp)
7583 {
7584         sp->pr_sctp_on = 0;
7585         if (stcb->asoc.peer_supports_prsctp) {
7586                 /*
7587                  * We assume that the user wants PR_SCTP_TTL if the user
7588                  * provides a positive lifetime but does not specify any
7589                  * PR_SCTP policy. This is a BAD assumption and causes
7590                  * problems at least with the U-Vancovers MPI folks. I will
7591                  * change this to be no policy means NO PR-SCTP.
7592                  */
7593                 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
7594                         sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
7595                         sp->pr_sctp_on = 1;
7596                 } else {
7597                         return;
7598                 }
7599                 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
7600                 case CHUNK_FLAGS_PR_SCTP_BUF:
7601                         /*
7602                          * Time to live is a priority stored in tv_sec when
7603                          * doing the buffer drop thing.
7604                          */
7605                         sp->ts.tv_sec = sp->timetolive;
7606                         sp->ts.tv_usec = 0;
7607                         break;
7608                 case CHUNK_FLAGS_PR_SCTP_TTL:
7609                         {
7610                                 struct timeval tv;
7611
7612                                 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
7613                                 tv.tv_sec = sp->timetolive / 1000;
7614                                 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
7615                                 /*
7616                                  * TODO sctp_constants.h needs alternative
7617                                  * time macros when _KERNEL is undefined.
7618                                  */
7619                                 timevaladd(&sp->ts, &tv);
7620                         }
7621                         break;
7622                 case CHUNK_FLAGS_PR_SCTP_RTX:
7623                         /*
7624                          * Time to live is a the number or retransmissions
7625                          * stored in tv_sec.
7626                          */
7627                         sp->ts.tv_sec = sp->timetolive;
7628                         sp->ts.tv_usec = 0;
7629                         break;
7630                 default:
7631                         SCTPDBG(SCTP_DEBUG_USRREQ1,
7632                             "Unknown PR_SCTP policy %u.\n",
7633                             PR_SCTP_POLICY(sp->sinfo_flags));
7634                         break;
7635                 }
7636         }
7637 }
7638
7639 static int
7640 sctp_msg_append(struct sctp_tcb *stcb,
7641     struct sctp_nets *net,
7642     struct mbuf *m,
7643     struct sctp_sndrcvinfo *srcv, int hold_stcb_lock)
7644 {
7645         int error = 0, holds_lock;
7646         struct mbuf *at;
7647         struct sctp_stream_queue_pending *sp = NULL;
7648         struct sctp_stream_out *strm;
7649
7650         /*
7651          * Given an mbuf chain, put it into the association send queue and
7652          * place it on the wheel
7653          */
7654         holds_lock = hold_stcb_lock;
7655         if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
7656                 /* Invalid stream number */
7657                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
7658                 error = EINVAL;
7659                 goto out_now;
7660         }
7661         if ((stcb->asoc.stream_locked) &&
7662             (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
7663                 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
7664                 error = EINVAL;
7665                 goto out_now;
7666         }
7667         strm = &stcb->asoc.strmout[srcv->sinfo_stream];
7668         /* Now can we send this? */
7669         if ((SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
7670             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
7671             (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7672             (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
7673                 /* got data while shutting down */
7674                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
7675                 error = ECONNRESET;
7676                 goto out_now;
7677         }
7678         sctp_alloc_a_strmoq(stcb, sp);
7679         if (sp == NULL) {
7680                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7681                 error = ENOMEM;
7682                 goto out_now;
7683         }
7684         sp->sinfo_flags = srcv->sinfo_flags;
7685         sp->timetolive = srcv->sinfo_timetolive;
7686         sp->ppid = srcv->sinfo_ppid;
7687         sp->context = srcv->sinfo_context;
7688         sp->strseq = 0;
7689         if (sp->sinfo_flags & SCTP_ADDR_OVER) {
7690                 sp->net = net;
7691                 sp->addr_over = 1;
7692         } else {
7693                 sp->net = stcb->asoc.primary_destination;
7694                 sp->addr_over = 0;
7695         }
7696         atomic_add_int(&sp->net->ref_count, 1);
7697         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
7698         sp->stream = srcv->sinfo_stream;
7699         sp->msg_is_complete = 1;
7700         sp->sender_all_done = 1;
7701         sp->some_taken = 0;
7702         sp->data = m;
7703         sp->tail_mbuf = NULL;
7704         sp->length = 0;
7705         at = m;
7706         sctp_set_prsctp_policy(stcb, sp);
7707         /*
7708          * We could in theory (for sendall) sifa the length in, but we would
7709          * still have to hunt through the chain since we need to setup the
7710          * tail_mbuf
7711          */
7712         while (at) {
7713                 if (SCTP_BUF_NEXT(at) == NULL)
7714                         sp->tail_mbuf = at;
7715                 sp->length += SCTP_BUF_LEN(at);
7716                 at = SCTP_BUF_NEXT(at);
7717         }
7718         SCTP_TCB_SEND_LOCK(stcb);
7719         sctp_snd_sb_alloc(stcb, sp->length);
7720         atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
7721         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
7722         if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
7723                 sp->strseq = strm->next_sequence_sent;
7724                 strm->next_sequence_sent++;
7725         }
7726         if ((strm->next_spoke.tqe_next == NULL) &&
7727             (strm->next_spoke.tqe_prev == NULL)) {
7728                 /* Not on wheel, insert */
7729                 sctp_insert_on_wheel(stcb, &stcb->asoc, strm, 1);
7730         }
7731         m = NULL;
7732         SCTP_TCB_SEND_UNLOCK(stcb);
7733 out_now:
7734         if (m) {
7735                 sctp_m_freem(m);
7736         }
7737         return (error);
7738 }
7739
7740
7741 static struct mbuf *
7742 sctp_copy_mbufchain(struct mbuf *clonechain,
7743     struct mbuf *outchain,
7744     struct mbuf **endofchain,
7745     int can_take_mbuf,
7746     int sizeofcpy,
7747     uint8_t copy_by_ref)
7748 {
7749         struct mbuf *m;
7750         struct mbuf *appendchain;
7751         caddr_t cp;
7752         int len;
7753
7754         if (endofchain == NULL) {
7755                 /* error */
7756 error_out:
7757                 if (outchain)
7758                         sctp_m_freem(outchain);
7759                 return (NULL);
7760         }
7761         if (can_take_mbuf) {
7762                 appendchain = clonechain;
7763         } else {
7764                 if (!copy_by_ref &&
7765                     (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))
7766                     ) {
7767                         /* Its not in a cluster */
7768                         if (*endofchain == NULL) {
7769                                 /* lets get a mbuf cluster */
7770                                 if (outchain == NULL) {
7771                                         /* This is the general case */
7772                         new_mbuf:
7773                                         outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
7774                                         if (outchain == NULL) {
7775                                                 goto error_out;
7776                                         }
7777                                         SCTP_BUF_LEN(outchain) = 0;
7778                                         *endofchain = outchain;
7779                                         /* get the prepend space */
7780                                         SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
7781                                 } else {
7782                                         /*
7783                                          * We really should not get a NULL
7784                                          * in endofchain
7785                                          */
7786                                         /* find end */
7787                                         m = outchain;
7788                                         while (m) {
7789                                                 if (SCTP_BUF_NEXT(m) == NULL) {
7790                                                         *endofchain = m;
7791                                                         break;
7792                                                 }
7793                                                 m = SCTP_BUF_NEXT(m);
7794                                         }
7795                                         /* sanity */
7796                                         if (*endofchain == NULL) {
7797                                                 /*
7798                                                  * huh, TSNH XXX maybe we
7799                                                  * should panic
7800                                                  */
7801                                                 sctp_m_freem(outchain);
7802                                                 goto new_mbuf;
7803                                         }
7804                                 }
7805                                 /* get the new end of length */
7806                                 len = M_TRAILINGSPACE(*endofchain);
7807                         } else {
7808                                 /* how much is left at the end? */
7809                                 len = M_TRAILINGSPACE(*endofchain);
7810                         }
7811                         /* Find the end of the data, for appending */
7812                         cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
7813
7814                         /* Now lets copy it out */
7815                         if (len >= sizeofcpy) {
7816                                 /* It all fits, copy it in */
7817                                 m_copydata(clonechain, 0, sizeofcpy, cp);
7818                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
7819                         } else {
7820                                 /* fill up the end of the chain */
7821                                 if (len > 0) {
7822                                         m_copydata(clonechain, 0, len, cp);
7823                                         SCTP_BUF_LEN((*endofchain)) += len;
7824                                         /* now we need another one */
7825                                         sizeofcpy -= len;
7826                                 }
7827                                 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_HEADER);
7828                                 if (m == NULL) {
7829                                         /* We failed */
7830                                         goto error_out;
7831                                 }
7832                                 SCTP_BUF_NEXT((*endofchain)) = m;
7833                                 *endofchain = m;
7834                                 cp = mtod((*endofchain), caddr_t);
7835                                 m_copydata(clonechain, len, sizeofcpy, cp);
7836                                 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
7837                         }
7838                         return (outchain);
7839                 } else {
7840                         /* copy the old fashion way */
7841                         appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_DONTWAIT);
7842 #ifdef SCTP_MBUF_LOGGING
7843                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7844                                 struct mbuf *mat;
7845
7846                                 mat = appendchain;
7847                                 while (mat) {
7848                                         if (SCTP_BUF_IS_EXTENDED(mat)) {
7849                                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7850                                         }
7851                                         mat = SCTP_BUF_NEXT(mat);
7852                                 }
7853                         }
7854 #endif
7855                 }
7856         }
7857         if (appendchain == NULL) {
7858                 /* error */
7859                 if (outchain)
7860                         sctp_m_freem(outchain);
7861                 return (NULL);
7862         }
7863         if (outchain) {
7864                 /* tack on to the end */
7865                 if (*endofchain != NULL) {
7866                         SCTP_BUF_NEXT(((*endofchain))) = appendchain;
7867                 } else {
7868                         m = outchain;
7869                         while (m) {
7870                                 if (SCTP_BUF_NEXT(m) == NULL) {
7871                                         SCTP_BUF_NEXT(m) = appendchain;
7872                                         break;
7873                                 }
7874                                 m = SCTP_BUF_NEXT(m);
7875                         }
7876                 }
7877                 /*
7878                  * save off the end and update the end-chain postion
7879                  */
7880                 m = appendchain;
7881                 while (m) {
7882                         if (SCTP_BUF_NEXT(m) == NULL) {
7883                                 *endofchain = m;
7884                                 break;
7885                         }
7886                         m = SCTP_BUF_NEXT(m);
7887                 }
7888                 return (outchain);
7889         } else {
7890                 /* save off the end and update the end-chain postion */
7891                 m = appendchain;
7892                 while (m) {
7893                         if (SCTP_BUF_NEXT(m) == NULL) {
7894                                 *endofchain = m;
7895                                 break;
7896                         }
7897                         m = SCTP_BUF_NEXT(m);
7898                 }
7899                 return (appendchain);
7900         }
7901 }
7902
7903 int
7904 sctp_med_chunk_output(struct sctp_inpcb *inp,
7905     struct sctp_tcb *stcb,
7906     struct sctp_association *asoc,
7907     int *num_out,
7908     int *reason_code,
7909     int control_only, int *cwnd_full, int from_where,
7910     struct timeval *now, int *now_filled, int frag_point, int so_locked
7911 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
7912     SCTP_UNUSED
7913 #endif
7914 );
7915
7916 static void
7917 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
7918     uint32_t val)
7919 {
7920         struct sctp_copy_all *ca;
7921         struct mbuf *m;
7922         int ret = 0;
7923         int added_control = 0;
7924         int un_sent, do_chunk_output = 1;
7925         struct sctp_association *asoc;
7926
7927         ca = (struct sctp_copy_all *)ptr;
7928         if (ca->m == NULL) {
7929                 return;
7930         }
7931         if (ca->inp != inp) {
7932                 /* TSNH */
7933                 return;
7934         }
7935         if ((ca->m) && ca->sndlen) {
7936                 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_DONTWAIT);
7937                 if (m == NULL) {
7938                         /* can't copy so we are done */
7939                         ca->cnt_failed++;
7940                         return;
7941                 }
7942 #ifdef SCTP_MBUF_LOGGING
7943                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7944                         struct mbuf *mat;
7945
7946                         mat = m;
7947                         while (mat) {
7948                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
7949                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
7950                                 }
7951                                 mat = SCTP_BUF_NEXT(mat);
7952                         }
7953                 }
7954 #endif
7955         } else {
7956                 m = NULL;
7957         }
7958         SCTP_TCB_LOCK_ASSERT(stcb);
7959         if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
7960                 /* Abort this assoc with m as the user defined reason */
7961                 if (m) {
7962                         struct sctp_paramhdr *ph;
7963
7964                         SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_DONTWAIT);
7965                         if (m) {
7966                                 ph = mtod(m, struct sctp_paramhdr *);
7967                                 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
7968                                 ph->param_length = htons(ca->sndlen);
7969                         }
7970                         /*
7971                          * We add one here to keep the assoc from
7972                          * dis-appearing on us.
7973                          */
7974                         atomic_add_int(&stcb->asoc.refcnt, 1);
7975                         sctp_abort_an_association(inp, stcb,
7976                             SCTP_RESPONSE_TO_USER_REQ,
7977                             m, SCTP_SO_NOT_LOCKED);
7978                         /*
7979                          * sctp_abort_an_association calls sctp_free_asoc()
7980                          * free association will NOT free it since we
7981                          * incremented the refcnt .. we do this to prevent
7982                          * it being freed and things getting tricky since we
7983                          * could end up (from free_asoc) calling inpcb_free
7984                          * which would get a recursive lock call to the
7985                          * iterator lock.. But as a consequence of that the
7986                          * stcb will return to us un-locked.. since
7987                          * free_asoc returns with either no TCB or the TCB
7988                          * unlocked, we must relock.. to unlock in the
7989                          * iterator timer :-0
7990                          */
7991                         SCTP_TCB_LOCK(stcb);
7992                         atomic_add_int(&stcb->asoc.refcnt, -1);
7993                         goto no_chunk_output;
7994                 }
7995         } else {
7996                 if (m) {
7997                         ret = sctp_msg_append(stcb, stcb->asoc.primary_destination, m,
7998                             &ca->sndrcv, 1);
7999                 }
8000                 asoc = &stcb->asoc;
8001                 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
8002                         /* shutdown this assoc */
8003                         int cnt;
8004
8005                         cnt = sctp_is_there_unsent_data(stcb);
8006
8007                         if (TAILQ_EMPTY(&asoc->send_queue) &&
8008                             TAILQ_EMPTY(&asoc->sent_queue) &&
8009                             (cnt == 0)) {
8010                                 if (asoc->locked_on_sending) {
8011                                         goto abort_anyway;
8012                                 }
8013                                 /*
8014                                  * there is nothing queued to send, so I'm
8015                                  * done...
8016                                  */
8017                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
8018                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
8019                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
8020                                         /*
8021                                          * only send SHUTDOWN the first time
8022                                          * through
8023                                          */
8024                                         sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
8025                                         if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
8026                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
8027                                         }
8028                                         SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
8029                                         SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
8030                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
8031                                             asoc->primary_destination);
8032                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
8033                                             asoc->primary_destination);
8034                                         added_control = 1;
8035                                         do_chunk_output = 0;
8036                                 }
8037                         } else {
8038                                 /*
8039                                  * we still got (or just got) data to send,
8040                                  * so set SHUTDOWN_PENDING
8041                                  */
8042                                 /*
8043                                  * XXX sockets draft says that SCTP_EOF
8044                                  * should be sent with no data.  currently,
8045                                  * we will allow user data to be sent first
8046                                  * and move to SHUTDOWN-PENDING
8047                                  */
8048                                 if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
8049                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
8050                                     (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
8051                                         if (asoc->locked_on_sending) {
8052                                                 /*
8053                                                  * Locked to send out the
8054                                                  * data
8055                                                  */
8056                                                 struct sctp_stream_queue_pending *sp;
8057
8058                                                 sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
8059                                                 if (sp) {
8060                                                         if ((sp->length == 0) && (sp->msg_is_complete == 0))
8061                                                                 asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
8062                                                 }
8063                                         }
8064                                         asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
8065                                         if (TAILQ_EMPTY(&asoc->send_queue) &&
8066                                             TAILQ_EMPTY(&asoc->sent_queue) &&
8067                                             (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
8068                                 abort_anyway:
8069                                                 atomic_add_int(&stcb->asoc.refcnt, 1);
8070                                                 sctp_abort_an_association(stcb->sctp_ep, stcb,
8071                                                     SCTP_RESPONSE_TO_USER_REQ,
8072                                                     NULL, SCTP_SO_NOT_LOCKED);
8073                                                 atomic_add_int(&stcb->asoc.refcnt, -1);
8074                                                 goto no_chunk_output;
8075                                         }
8076                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
8077                                             asoc->primary_destination);
8078                                 }
8079                         }
8080
8081                 }
8082         }
8083         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
8084             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
8085
8086         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
8087             (stcb->asoc.total_flight > 0) &&
8088             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))
8089             ) {
8090                 do_chunk_output = 0;
8091         }
8092         if (do_chunk_output)
8093                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
8094         else if (added_control) {
8095                 int num_out = 0, reason = 0, cwnd_full = 0, now_filled = 0;
8096                 struct timeval now;
8097                 int frag_point;
8098
8099                 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
8100                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
8101                     &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_NOT_LOCKED);
8102         }
8103 no_chunk_output:
8104         if (ret) {
8105                 ca->cnt_failed++;
8106         } else {
8107                 ca->cnt_sent++;
8108         }
8109 }
8110
8111 static void
8112 sctp_sendall_completes(void *ptr, uint32_t val)
8113 {
8114         struct sctp_copy_all *ca;
8115
8116         ca = (struct sctp_copy_all *)ptr;
8117         /*
8118          * Do a notify here? Kacheong suggests that the notify be done at
8119          * the send time.. so you would push up a notification if any send
8120          * failed. Don't know if this is feasable since the only failures we
8121          * have is "memory" related and if you cannot get an mbuf to send
8122          * the data you surely can't get an mbuf to send up to notify the
8123          * user you can't send the data :->
8124          */
8125
8126         /* now free everything */
8127         sctp_m_freem(ca->m);
8128         SCTP_FREE(ca, SCTP_M_COPYAL);
8129 }
8130
8131
8132 #define MC_ALIGN(m, len) do {                                           \
8133         SCTP_BUF_RESV_UF(m, ((MCLBYTES - (len)) & ~(sizeof(long) - 1)); \
8134 } while (0)
8135
8136
8137
8138 static struct mbuf *
8139 sctp_copy_out_all(struct uio *uio, int len)
8140 {
8141         struct mbuf *ret, *at;
8142         int left, willcpy, cancpy, error;
8143
8144         ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAIT, 1, MT_DATA);
8145         if (ret == NULL) {
8146                 /* TSNH */
8147                 return (NULL);
8148         }
8149         left = len;
8150         SCTP_BUF_LEN(ret) = 0;
8151         /* save space for the data chunk header */
8152         cancpy = M_TRAILINGSPACE(ret);
8153         willcpy = min(cancpy, left);
8154         at = ret;
8155         while (left > 0) {
8156                 /* Align data to the end */
8157                 error = uiomove(mtod(at, caddr_t), willcpy, uio);
8158                 if (error) {
8159         err_out_now:
8160                         sctp_m_freem(at);
8161                         return (NULL);
8162                 }
8163                 SCTP_BUF_LEN(at) = willcpy;
8164                 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
8165                 left -= willcpy;
8166                 if (left > 0) {
8167                         SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg(left, 0, M_WAIT, 1, MT_DATA);
8168                         if (SCTP_BUF_NEXT(at) == NULL) {
8169                                 goto err_out_now;
8170                         }
8171                         at = SCTP_BUF_NEXT(at);
8172                         SCTP_BUF_LEN(at) = 0;
8173                         cancpy = M_TRAILINGSPACE(at);
8174                         willcpy = min(cancpy, left);
8175                 }
8176         }
8177         return (ret);
8178 }
8179
8180 static int
8181 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
8182     struct sctp_sndrcvinfo *srcv)
8183 {
8184         int ret;
8185         struct sctp_copy_all *ca;
8186
8187         SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
8188             SCTP_M_COPYAL);
8189         if (ca == NULL) {
8190                 sctp_m_freem(m);
8191                 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8192                 return (ENOMEM);
8193         }
8194         memset(ca, 0, sizeof(struct sctp_copy_all));
8195
8196         ca->inp = inp;
8197         memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
8198         /*
8199          * take off the sendall flag, it would be bad if we failed to do
8200          * this :-0
8201          */
8202         ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
8203         /* get length and mbuf chain */
8204         if (uio) {
8205                 ca->sndlen = uio->uio_resid;
8206                 ca->m = sctp_copy_out_all(uio, ca->sndlen);
8207                 if (ca->m == NULL) {
8208                         SCTP_FREE(ca, SCTP_M_COPYAL);
8209                         SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8210                         return (ENOMEM);
8211                 }
8212         } else {
8213                 /* Gather the length of the send */
8214                 struct mbuf *mat;
8215
8216                 mat = m;
8217                 ca->sndlen = 0;
8218                 while (m) {
8219                         ca->sndlen += SCTP_BUF_LEN(m);
8220                         m = SCTP_BUF_NEXT(m);
8221                 }
8222                 ca->m = mat;
8223         }
8224         ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
8225             SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
8226             SCTP_ASOC_ANY_STATE,
8227             (void *)ca, 0,
8228             sctp_sendall_completes, inp, 1);
8229         if (ret) {
8230                 SCTP_PRINTF("Failed to initiate iterator for sendall\n");
8231                 SCTP_FREE(ca, SCTP_M_COPYAL);
8232                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
8233                 return (EFAULT);
8234         }
8235         return (0);
8236 }
8237
8238
8239 void
8240 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
8241 {
8242         struct sctp_tmit_chunk *chk, *nchk;
8243
8244         chk = TAILQ_FIRST(&asoc->control_send_queue);
8245         while (chk) {
8246                 nchk = TAILQ_NEXT(chk, sctp_next);
8247                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8248                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
8249                         if (chk->data) {
8250                                 sctp_m_freem(chk->data);
8251                                 chk->data = NULL;
8252                         }
8253                         asoc->ctrl_queue_cnt--;
8254                         sctp_free_a_chunk(stcb, chk);
8255                 }
8256                 chk = nchk;
8257         }
8258 }
8259
8260 void
8261 sctp_toss_old_asconf(struct sctp_tcb *stcb)
8262 {
8263         struct sctp_association *asoc;
8264         struct sctp_tmit_chunk *chk, *chk_tmp;
8265         struct sctp_asconf_chunk *acp;
8266
8267         asoc = &stcb->asoc;
8268         for (chk = TAILQ_FIRST(&asoc->asconf_send_queue); chk != NULL;
8269             chk = chk_tmp) {
8270                 /* get next chk */
8271                 chk_tmp = TAILQ_NEXT(chk, sctp_next);
8272                 /* find SCTP_ASCONF chunk in queue */
8273                 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
8274                         if (chk->data) {
8275                                 acp = mtod(chk->data, struct sctp_asconf_chunk *);
8276                                 if (compare_with_wrap(ntohl(acp->serial_number), stcb->asoc.asconf_seq_out_acked, MAX_SEQ)) {
8277                                         /* Not Acked yet */
8278                                         break;
8279                                 }
8280                         }
8281                         TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
8282                         if (chk->data) {
8283                                 sctp_m_freem(chk->data);
8284                                 chk->data = NULL;
8285                         }
8286                         asoc->ctrl_queue_cnt--;
8287                         sctp_free_a_chunk(stcb, chk);
8288                 }
8289         }
8290 }
8291
8292
8293 static void
8294 sctp_clean_up_datalist(struct sctp_tcb *stcb,
8295
8296     struct sctp_association *asoc,
8297     struct sctp_tmit_chunk **data_list,
8298     int bundle_at,
8299     struct sctp_nets *net)
8300 {
8301         int i;
8302         struct sctp_tmit_chunk *tp1;
8303
8304         for (i = 0; i < bundle_at; i++) {
8305                 /* off of the send queue */
8306                 if (i) {
8307                         /*
8308                          * Any chunk NOT 0 you zap the time chunk 0 gets
8309                          * zapped or set based on if a RTO measurment is
8310                          * needed.
8311                          */
8312                         data_list[i]->do_rtt = 0;
8313                 }
8314                 /* record time */
8315                 data_list[i]->sent_rcv_time = net->last_sent_time;
8316                 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.TSN_seq;
8317                 TAILQ_REMOVE(&asoc->send_queue,
8318                     data_list[i],
8319                     sctp_next);
8320                 /* on to the sent queue */
8321                 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
8322                 if ((tp1) && (compare_with_wrap(tp1->rec.data.TSN_seq,
8323                     data_list[i]->rec.data.TSN_seq, MAX_TSN))) {
8324                         struct sctp_tmit_chunk *tpp;
8325
8326                         /* need to move back */
8327         back_up_more:
8328                         tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
8329                         if (tpp == NULL) {
8330                                 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
8331                                 goto all_done;
8332                         }
8333                         tp1 = tpp;
8334                         if (compare_with_wrap(tp1->rec.data.TSN_seq,
8335                             data_list[i]->rec.data.TSN_seq, MAX_TSN)) {
8336                                 goto back_up_more;
8337                         }
8338                         TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
8339                 } else {
8340                         TAILQ_INSERT_TAIL(&asoc->sent_queue,
8341                             data_list[i],
8342                             sctp_next);
8343                 }
8344 all_done:
8345                 /* This does not lower until the cum-ack passes it */
8346                 asoc->sent_queue_cnt++;
8347                 asoc->send_queue_cnt--;
8348                 if ((asoc->peers_rwnd <= 0) &&
8349                     (asoc->total_flight == 0) &&
8350                     (bundle_at == 1)) {
8351                         /* Mark the chunk as being a window probe */
8352                         SCTP_STAT_INCR(sctps_windowprobed);
8353                 }
8354 #ifdef SCTP_AUDITING_ENABLED
8355                 sctp_audit_log(0xC2, 3);
8356 #endif
8357                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
8358                 data_list[i]->snd_count = 1;
8359                 data_list[i]->rec.data.chunk_was_revoked = 0;
8360                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
8361                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
8362                             data_list[i]->whoTo->flight_size,
8363                             data_list[i]->book_size,
8364                             (uintptr_t) data_list[i]->whoTo,
8365                             data_list[i]->rec.data.TSN_seq);
8366                 }
8367                 sctp_flight_size_increase(data_list[i]);
8368                 sctp_total_flight_increase(stcb, data_list[i]);
8369                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
8370                         sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
8371                             asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
8372                 }
8373                 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
8374                     (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
8375                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
8376                         /* SWS sender side engages */
8377                         asoc->peers_rwnd = 0;
8378                 }
8379         }
8380 }
8381
8382 static void
8383 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc)
8384 {
8385         struct sctp_tmit_chunk *chk, *nchk;
8386
8387         for (chk = TAILQ_FIRST(&asoc->control_send_queue);
8388             chk; chk = nchk) {
8389                 nchk = TAILQ_NEXT(chk, sctp_next);
8390                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8391                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
8392                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8393                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8394                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8395                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8396                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8397                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8398                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8399                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8400                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8401                         /* Stray chunks must be cleaned up */
8402         clean_up_anyway:
8403                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
8404                         if (chk->data) {
8405                                 sctp_m_freem(chk->data);
8406                                 chk->data = NULL;
8407                         }
8408                         asoc->ctrl_queue_cnt--;
8409                         sctp_free_a_chunk(stcb, chk);
8410                 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
8411                         /* special handling, we must look into the param */
8412                         if (chk != asoc->str_reset) {
8413                                 goto clean_up_anyway;
8414                         }
8415                 }
8416         }
8417 }
8418
8419
8420 static int
8421 sctp_can_we_split_this(struct sctp_tcb *stcb,
8422     uint32_t length,
8423     uint32_t goal_mtu, uint32_t frag_point, int eeor_on)
8424 {
8425         /*
8426          * Make a decision on if I should split a msg into multiple parts.
8427          * This is only asked of incomplete messages.
8428          */
8429         if (eeor_on) {
8430                 /*
8431                  * If we are doing EEOR we need to always send it if its the
8432                  * entire thing, since it might be all the guy is putting in
8433                  * the hopper.
8434                  */
8435                 if (goal_mtu >= length) {
8436                         /*-
8437                          * If we have data outstanding,
8438                          * we get another chance when the sack
8439                          * arrives to transmit - wait for more data
8440                          */
8441                         if (stcb->asoc.total_flight == 0) {
8442                                 /*
8443                                  * If nothing is in flight, we zero the
8444                                  * packet counter.
8445                                  */
8446                                 return (length);
8447                         }
8448                         return (0);
8449
8450                 } else {
8451                         /* You can fill the rest */
8452                         return (goal_mtu);
8453                 }
8454         }
8455         /*-
8456          * For those strange folk that make the send buffer
8457          * smaller than our fragmentation point, we can't
8458          * get a full msg in so we have to allow splitting.
8459          */
8460         if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
8461                 return (length);
8462         }
8463         if ((length <= goal_mtu) ||
8464             ((length - goal_mtu) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
8465                 /* Sub-optimial residual don't split in non-eeor mode. */
8466                 return (0);
8467         }
8468         /*
8469          * If we reach here length is larger than the goal_mtu. Do we wish
8470          * to split it for the sake of packet putting together?
8471          */
8472         if (goal_mtu >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
8473                 /* Its ok to split it */
8474                 return (min(goal_mtu, frag_point));
8475         }
8476         /* Nope, can't split */
8477         return (0);
8478
8479 }
8480
8481 static uint32_t
8482 sctp_move_to_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
8483     struct sctp_stream_out *strq,
8484     uint32_t goal_mtu,
8485     uint32_t frag_point,
8486     int *locked,
8487     int *giveup,
8488     int eeor_mode,
8489     int *bail)
8490 {
8491         /* Move from the stream to the send_queue keeping track of the total */
8492         struct sctp_association *asoc;
8493         struct sctp_stream_queue_pending *sp;
8494         struct sctp_tmit_chunk *chk;
8495         struct sctp_data_chunk *dchkh;
8496         uint32_t to_move, length;
8497         uint8_t rcv_flags = 0;
8498         uint8_t some_taken;
8499         uint8_t send_lock_up = 0;
8500
8501         SCTP_TCB_LOCK_ASSERT(stcb);
8502         asoc = &stcb->asoc;
8503 one_more_time:
8504         /* sa_ignore FREED_MEMORY */
8505         sp = TAILQ_FIRST(&strq->outqueue);
8506         if (sp == NULL) {
8507                 *locked = 0;
8508                 if (send_lock_up == 0) {
8509                         SCTP_TCB_SEND_LOCK(stcb);
8510                         send_lock_up = 1;
8511                 }
8512                 sp = TAILQ_FIRST(&strq->outqueue);
8513                 if (sp) {
8514                         goto one_more_time;
8515                 }
8516                 if (strq->last_msg_incomplete) {
8517                         SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
8518                             strq->stream_no,
8519                             strq->last_msg_incomplete);
8520                         strq->last_msg_incomplete = 0;
8521                 }
8522                 to_move = 0;
8523                 if (send_lock_up) {
8524                         SCTP_TCB_SEND_UNLOCK(stcb);
8525                         send_lock_up = 0;
8526                 }
8527                 goto out_of;
8528         }
8529         if ((sp->msg_is_complete) && (sp->length == 0)) {
8530                 if (sp->sender_all_done) {
8531                         /*
8532                          * We are doing differed cleanup. Last time through
8533                          * when we took all the data the sender_all_done was
8534                          * not set.
8535                          */
8536                         if (sp->put_last_out == 0) {
8537                                 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
8538                                 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
8539                                     sp->sender_all_done,
8540                                     sp->length,
8541                                     sp->msg_is_complete,
8542                                     sp->put_last_out,
8543                                     send_lock_up);
8544                         }
8545                         if ((TAILQ_NEXT(sp, next) == NULL) && (send_lock_up == 0)) {
8546                                 SCTP_TCB_SEND_LOCK(stcb);
8547                                 send_lock_up = 1;
8548                         }
8549                         atomic_subtract_int(&asoc->stream_queue_cnt, 1);
8550                         TAILQ_REMOVE(&strq->outqueue, sp, next);
8551                         sctp_free_remote_addr(sp->net);
8552                         if (sp->data) {
8553                                 sctp_m_freem(sp->data);
8554                                 sp->data = NULL;
8555                         }
8556                         sctp_free_a_strmoq(stcb, sp);
8557
8558                         /* we can't be locked to it */
8559                         *locked = 0;
8560                         stcb->asoc.locked_on_sending = NULL;
8561                         if (send_lock_up) {
8562                                 SCTP_TCB_SEND_UNLOCK(stcb);
8563                                 send_lock_up = 0;
8564                         }
8565                         /* back to get the next msg */
8566                         goto one_more_time;
8567                 } else {
8568                         /*
8569                          * sender just finished this but still holds a
8570                          * reference
8571                          */
8572                         *locked = 1;
8573                         *giveup = 1;
8574                         to_move = 0;
8575                         goto out_of;
8576                 }
8577         } else {
8578                 /* is there some to get */
8579                 if (sp->length == 0) {
8580                         /* no */
8581                         *locked = 1;
8582                         *giveup = 1;
8583                         to_move = 0;
8584                         goto out_of;
8585                 }
8586         }
8587         some_taken = sp->some_taken;
8588         if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
8589                 sp->msg_is_complete = 1;
8590         }
8591 re_look:
8592         length = sp->length;
8593         if (sp->msg_is_complete) {
8594                 /* The message is complete */
8595                 to_move = min(length, frag_point);
8596                 if (to_move == length) {
8597                         /* All of it fits in the MTU */
8598                         if (sp->some_taken) {
8599                                 rcv_flags |= SCTP_DATA_LAST_FRAG;
8600                                 sp->put_last_out = 1;
8601                         } else {
8602                                 rcv_flags |= SCTP_DATA_NOT_FRAG;
8603                                 sp->put_last_out = 1;
8604                         }
8605                 } else {
8606                         /* Not all of it fits, we fragment */
8607                         if (sp->some_taken == 0) {
8608                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
8609                         }
8610                         sp->some_taken = 1;
8611                 }
8612         } else {
8613                 to_move = sctp_can_we_split_this(stcb, length, goal_mtu,
8614                     frag_point, eeor_mode);
8615                 if (to_move) {
8616                         /*-
8617                          * We use a snapshot of length in case it
8618                          * is expanding during the compare.
8619                          */
8620                         uint32_t llen;
8621
8622                         llen = length;
8623                         if (to_move >= llen) {
8624                                 to_move = llen;
8625                                 if (send_lock_up == 0) {
8626                                         /*-
8627                                          * We are taking all of an incomplete msg
8628                                          * thus we need a send lock.
8629                                          */
8630                                         SCTP_TCB_SEND_LOCK(stcb);
8631                                         send_lock_up = 1;
8632                                         if (sp->msg_is_complete) {
8633                                                 /*
8634                                                  * the sender finished the
8635                                                  * msg
8636                                                  */
8637                                                 goto re_look;
8638                                         }
8639                                 }
8640                         }
8641                         if (sp->some_taken == 0) {
8642                                 rcv_flags |= SCTP_DATA_FIRST_FRAG;
8643                                 sp->some_taken = 1;
8644                         }
8645                 } else {
8646                         /* Nothing to take. */
8647                         if (sp->some_taken) {
8648                                 *locked = 1;
8649                         }
8650                         *giveup = 1;
8651                         to_move = 0;
8652                         goto out_of;
8653                 }
8654         }
8655
8656         /* If we reach here, we can copy out a chunk */
8657         sctp_alloc_a_chunk(stcb, chk);
8658         if (chk == NULL) {
8659                 /* No chunk memory */
8660                 *giveup = 1;
8661                 to_move = 0;
8662                 goto out_of;
8663         }
8664         /*
8665          * Setup for unordered if needed by looking at the user sent info
8666          * flags.
8667          */
8668         if (sp->sinfo_flags & SCTP_UNORDERED) {
8669                 rcv_flags |= SCTP_DATA_UNORDERED;
8670         }
8671         if ((SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) && ((sp->sinfo_flags & SCTP_EOF) == SCTP_EOF)) ||
8672             ((sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) == SCTP_SACK_IMMEDIATELY)) {
8673                 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8674         }
8675         /* clear out the chunk before setting up */
8676         memset(chk, 0, sizeof(*chk));
8677         chk->rec.data.rcv_flags = rcv_flags;
8678
8679         if (to_move >= length) {
8680                 /* we think we can steal the whole thing */
8681                 if ((sp->sender_all_done == 0) && (send_lock_up == 0)) {
8682                         SCTP_TCB_SEND_LOCK(stcb);
8683                         send_lock_up = 1;
8684                 }
8685                 if (to_move < sp->length) {
8686                         /* bail, it changed */
8687                         goto dont_do_it;
8688                 }
8689                 chk->data = sp->data;
8690                 chk->last_mbuf = sp->tail_mbuf;
8691                 /* register the stealing */
8692                 sp->data = sp->tail_mbuf = NULL;
8693         } else {
8694                 struct mbuf *m;
8695
8696 dont_do_it:
8697                 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_DONTWAIT);
8698                 chk->last_mbuf = NULL;
8699                 if (chk->data == NULL) {
8700                         sp->some_taken = some_taken;
8701                         sctp_free_a_chunk(stcb, chk);
8702                         *bail = 1;
8703                         to_move = 0;
8704                         goto out_of;
8705                 }
8706 #ifdef SCTP_MBUF_LOGGING
8707                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8708                         struct mbuf *mat;
8709
8710                         mat = chk->data;
8711                         while (mat) {
8712                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
8713                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
8714                                 }
8715                                 mat = SCTP_BUF_NEXT(mat);
8716                         }
8717                 }
8718 #endif
8719                 /* Pull off the data */
8720                 m_adj(sp->data, to_move);
8721                 /* Now lets work our way down and compact it */
8722                 m = sp->data;
8723                 while (m && (SCTP_BUF_LEN(m) == 0)) {
8724                         sp->data = SCTP_BUF_NEXT(m);
8725                         SCTP_BUF_NEXT(m) = NULL;
8726                         if (sp->tail_mbuf == m) {
8727                                 /*-
8728                                  * Freeing tail? TSNH since
8729                                  * we supposedly were taking less
8730                                  * than the sp->length.
8731                                  */
8732 #ifdef INVARIANTS
8733                                 panic("Huh, freing tail? - TSNH");
8734 #else
8735                                 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
8736                                 sp->tail_mbuf = sp->data = NULL;
8737                                 sp->length = 0;
8738 #endif
8739
8740                         }
8741                         sctp_m_free(m);
8742                         m = sp->data;
8743                 }
8744         }
8745         if (SCTP_BUF_IS_EXTENDED(chk->data)) {
8746                 chk->copy_by_ref = 1;
8747         } else {
8748                 chk->copy_by_ref = 0;
8749         }
8750         /*
8751          * get last_mbuf and counts of mb useage This is ugly but hopefully
8752          * its only one mbuf.
8753          */
8754         if (chk->last_mbuf == NULL) {
8755                 chk->last_mbuf = chk->data;
8756                 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
8757                         chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
8758                 }
8759         }
8760         if (to_move > length) {
8761                 /*- This should not happen either
8762                  * since we always lower to_move to the size
8763                  * of sp->length if its larger.
8764                  */
8765 #ifdef INVARIANTS
8766                 panic("Huh, how can to_move be larger?");
8767 #else
8768                 SCTP_PRINTF("Huh, how can to_move be larger?\n");
8769                 sp->length = 0;
8770 #endif
8771         } else {
8772                 atomic_subtract_int(&sp->length, to_move);
8773         }
8774         if (M_LEADINGSPACE(chk->data) < (int)sizeof(struct sctp_data_chunk)) {
8775                 /* Not enough room for a chunk header, get some */
8776                 struct mbuf *m;
8777
8778                 m = sctp_get_mbuf_for_msg(1, 0, M_DONTWAIT, 0, MT_DATA);
8779                 if (m == NULL) {
8780                         /*
8781                          * we're in trouble here. _PREPEND below will free
8782                          * all the data if there is no leading space, so we
8783                          * must put the data back and restore.
8784                          */
8785                         if (send_lock_up == 0) {
8786                                 SCTP_TCB_SEND_LOCK(stcb);
8787                                 send_lock_up = 1;
8788                         }
8789                         if (chk->data == NULL) {
8790                                 /* unsteal the data */
8791                                 sp->data = chk->data;
8792                                 sp->tail_mbuf = chk->last_mbuf;
8793                         } else {
8794                                 struct mbuf *m_tmp;
8795
8796                                 /* reassemble the data */
8797                                 m_tmp = sp->data;
8798                                 sp->data = chk->data;
8799                                 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
8800                         }
8801                         sp->some_taken = some_taken;
8802                         atomic_add_int(&sp->length, to_move);
8803                         chk->data = NULL;
8804                         *bail = 1;
8805                         sctp_free_a_chunk(stcb, chk);
8806                         to_move = 0;
8807                         goto out_of;
8808                 } else {
8809                         SCTP_BUF_LEN(m) = 0;
8810                         SCTP_BUF_NEXT(m) = chk->data;
8811                         chk->data = m;
8812                         M_ALIGN(chk->data, 4);
8813                 }
8814         }
8815         SCTP_BUF_PREPEND(chk->data, sizeof(struct sctp_data_chunk), M_DONTWAIT);
8816         if (chk->data == NULL) {
8817                 /* HELP, TSNH since we assured it would not above? */
8818 #ifdef INVARIANTS
8819                 panic("prepend failes HELP?");
8820 #else
8821                 SCTP_PRINTF("prepend fails HELP?\n");
8822                 sctp_free_a_chunk(stcb, chk);
8823 #endif
8824                 *bail = 1;
8825                 to_move = 0;
8826                 goto out_of;
8827         }
8828         sctp_snd_sb_alloc(stcb, sizeof(struct sctp_data_chunk));
8829         chk->book_size = chk->send_size = (to_move +
8830             sizeof(struct sctp_data_chunk));
8831         chk->book_size_scale = 0;
8832         chk->sent = SCTP_DATAGRAM_UNSENT;
8833
8834         chk->flags = 0;
8835         chk->asoc = &stcb->asoc;
8836         chk->pad_inplace = 0;
8837         chk->no_fr_allowed = 0;
8838         chk->rec.data.stream_seq = sp->strseq;
8839         chk->rec.data.stream_number = sp->stream;
8840         chk->rec.data.payloadtype = sp->ppid;
8841         chk->rec.data.context = sp->context;
8842         chk->rec.data.doing_fast_retransmit = 0;
8843         chk->rec.data.ect_nonce = 0;    /* ECN Nonce */
8844
8845         chk->rec.data.timetodrop = sp->ts;
8846         chk->flags = sp->act_flags;
8847         chk->addr_over = sp->addr_over;
8848
8849         chk->whoTo = net;
8850         atomic_add_int(&chk->whoTo->ref_count, 1);
8851
8852         if (sp->holds_key_ref) {
8853                 chk->auth_keyid = sp->auth_keyid;
8854                 sctp_auth_key_acquire(stcb, chk->auth_keyid);
8855                 chk->holds_key_ref = 1;
8856         }
8857         chk->rec.data.TSN_seq = atomic_fetchadd_int(&asoc->sending_seq, 1);
8858         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
8859                 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
8860                     (uintptr_t) stcb, sp->length,
8861                     (uint32_t) ((chk->rec.data.stream_number << 16) | chk->rec.data.stream_seq),
8862                     chk->rec.data.TSN_seq);
8863         }
8864         dchkh = mtod(chk->data, struct sctp_data_chunk *);
8865         /*
8866          * Put the rest of the things in place now. Size was done earlier in
8867          * previous loop prior to padding.
8868          */
8869
8870 #ifdef SCTP_ASOCLOG_OF_TSNS
8871         SCTP_TCB_LOCK_ASSERT(stcb);
8872         if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
8873                 asoc->tsn_out_at = 0;
8874                 asoc->tsn_out_wrapped = 1;
8875         }
8876         asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.TSN_seq;
8877         asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.stream_number;
8878         asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.stream_seq;
8879         asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
8880         asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
8881         asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
8882         asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
8883         asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
8884         asoc->tsn_out_at++;
8885 #endif
8886
8887         dchkh->ch.chunk_type = SCTP_DATA;
8888         dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
8889         dchkh->dp.tsn = htonl(chk->rec.data.TSN_seq);
8890         dchkh->dp.stream_id = htons(strq->stream_no);
8891         dchkh->dp.stream_sequence = htons(chk->rec.data.stream_seq);
8892         dchkh->dp.protocol_id = chk->rec.data.payloadtype;
8893         dchkh->ch.chunk_length = htons(chk->send_size);
8894         /* Now advance the chk->send_size by the actual pad needed. */
8895         if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
8896                 /* need a pad */
8897                 struct mbuf *lm;
8898                 int pads;
8899
8900                 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
8901                 if (sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf) == 0) {
8902                         chk->pad_inplace = 1;
8903                 }
8904                 if ((lm = SCTP_BUF_NEXT(chk->last_mbuf)) != NULL) {
8905                         /* pad added an mbuf */
8906                         chk->last_mbuf = lm;
8907                 }
8908                 chk->send_size += pads;
8909         }
8910         /* We only re-set the policy if it is on */
8911         if (sp->pr_sctp_on) {
8912                 sctp_set_prsctp_policy(stcb, sp);
8913                 asoc->pr_sctp_cnt++;
8914                 chk->pr_sctp_on = 1;
8915         } else {
8916                 chk->pr_sctp_on = 0;
8917         }
8918         if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
8919                 /* All done pull and kill the message */
8920                 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
8921                 if (sp->put_last_out == 0) {
8922                         SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
8923                         SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d send_lock:%d\n",
8924                             sp->sender_all_done,
8925                             sp->length,
8926                             sp->msg_is_complete,
8927                             sp->put_last_out,
8928                             send_lock_up);
8929                 }
8930                 if ((send_lock_up == 0) && (TAILQ_NEXT(sp, next) == NULL)) {
8931                         SCTP_TCB_SEND_LOCK(stcb);
8932                         send_lock_up = 1;
8933                 }
8934                 TAILQ_REMOVE(&strq->outqueue, sp, next);
8935                 sctp_free_remote_addr(sp->net);
8936                 if (sp->data) {
8937                         sctp_m_freem(sp->data);
8938                         sp->data = NULL;
8939                 }
8940                 sctp_free_a_strmoq(stcb, sp);
8941
8942                 /* we can't be locked to it */
8943                 *locked = 0;
8944                 stcb->asoc.locked_on_sending = NULL;
8945         } else {
8946                 /* more to go, we are locked */
8947                 *locked = 1;
8948         }
8949         asoc->chunks_on_out_queue++;
8950         TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
8951         asoc->send_queue_cnt++;
8952 out_of:
8953         if (send_lock_up) {
8954                 SCTP_TCB_SEND_UNLOCK(stcb);
8955                 send_lock_up = 0;
8956         }
8957         return (to_move);
8958 }
8959
8960
8961 static struct sctp_stream_out *
8962 sctp_select_a_stream(struct sctp_tcb *stcb, struct sctp_association *asoc)
8963 {
8964         struct sctp_stream_out *strq;
8965
8966         /* Find the next stream to use */
8967         if (asoc->last_out_stream == NULL) {
8968                 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
8969                 if (asoc->last_out_stream == NULL) {
8970                         /* huh nothing on the wheel, TSNH */
8971                         return (NULL);
8972                 }
8973                 goto done_it;
8974         }
8975         strq = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
8976 done_it:
8977         if (strq == NULL) {
8978                 strq = asoc->last_out_stream = TAILQ_FIRST(&asoc->out_wheel);
8979         }
8980         /* Save off the last stream */
8981         asoc->last_out_stream = strq;
8982         return (strq);
8983
8984 }
8985
8986
8987 static void
8988 sctp_fill_outqueue(struct sctp_tcb *stcb,
8989     struct sctp_nets *net, int frag_point, int eeor_mode, int *quit_now)
8990 {
8991         struct sctp_association *asoc;
8992         struct sctp_stream_out *strq, *strqn, *strqt;
8993         int goal_mtu, moved_how_much, total_moved = 0, bail = 0;
8994         int locked, giveup;
8995         struct sctp_stream_queue_pending *sp;
8996
8997         SCTP_TCB_LOCK_ASSERT(stcb);
8998         asoc = &stcb->asoc;
8999 #ifdef INET6
9000         if (net->ro._l_addr.sin6.sin6_family == AF_INET6) {
9001                 goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
9002         } else {
9003                 /* ?? not sure what else to do */
9004                 goal_mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9005         }
9006 #else
9007         goal_mtu = net->mtu - SCTP_MIN_OVERHEAD;
9008 #endif
9009         /* Need an allowance for the data chunk header too */
9010         goal_mtu -= sizeof(struct sctp_data_chunk);
9011
9012         /* must make even word boundary */
9013         goal_mtu &= 0xfffffffc;
9014         if (asoc->locked_on_sending) {
9015                 /* We are stuck on one stream until the message completes. */
9016                 strqn = strq = asoc->locked_on_sending;
9017                 locked = 1;
9018         } else {
9019                 strqn = strq = sctp_select_a_stream(stcb, asoc);
9020                 locked = 0;
9021         }
9022
9023         while ((goal_mtu > 0) && strq) {
9024                 sp = TAILQ_FIRST(&strq->outqueue);
9025                 /*
9026                  * If CMT is off, we must validate that the stream in
9027                  * question has the first item pointed towards are network
9028                  * destionation requested by the caller. Note that if we
9029                  * turn out to be locked to a stream (assigning TSN's then
9030                  * we must stop, since we cannot look for another stream
9031                  * with data to send to that destination). In CMT's case, by
9032                  * skipping this check, we will send one data packet towards
9033                  * the requested net.
9034                  */
9035                 if (sp == NULL) {
9036                         break;
9037                 }
9038                 if ((sp->net != net) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
9039                         /* none for this network */
9040                         if (locked) {
9041                                 break;
9042                         } else {
9043                                 strq = sctp_select_a_stream(stcb, asoc);
9044                                 if (strq == NULL)
9045                                         /* none left */
9046                                         break;
9047                                 if (strqn == strq) {
9048                                         /* I have circled */
9049                                         break;
9050                                 }
9051                                 continue;
9052                         }
9053                 }
9054                 giveup = 0;
9055                 bail = 0;
9056                 moved_how_much = sctp_move_to_outqueue(stcb, net, strq, goal_mtu, frag_point, &locked,
9057                     &giveup, eeor_mode, &bail);
9058                 asoc->last_out_stream = strq;
9059                 if (locked) {
9060                         asoc->locked_on_sending = strq;
9061                         if ((moved_how_much == 0) || (giveup) || bail)
9062                                 /* no more to move for now */
9063                                 break;
9064                 } else {
9065                         asoc->locked_on_sending = NULL;
9066                         strqt = sctp_select_a_stream(stcb, asoc);
9067                         if (TAILQ_FIRST(&strq->outqueue) == NULL) {
9068                                 if (strq == strqn) {
9069                                         /* Must move start to next one */
9070                                         strqn = TAILQ_NEXT(asoc->last_out_stream, next_spoke);
9071                                         if (strqn == NULL) {
9072                                                 strqn = TAILQ_FIRST(&asoc->out_wheel);
9073                                                 if (strqn == NULL) {
9074                                                         break;
9075                                                 }
9076                                         }
9077                                 }
9078                                 sctp_remove_from_wheel(stcb, asoc, strq);
9079                         }
9080                         if ((giveup) || bail) {
9081                                 break;
9082                         }
9083                         strq = strqt;
9084                         if (strq == NULL) {
9085                                 break;
9086                         }
9087                 }
9088                 total_moved += moved_how_much;
9089                 goal_mtu -= (moved_how_much + sizeof(struct sctp_data_chunk));
9090                 goal_mtu &= 0xfffffffc;
9091         }
9092         if (bail)
9093                 *quit_now = 1;
9094
9095         if (total_moved == 0) {
9096                 if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) &&
9097                     (net == stcb->asoc.primary_destination)) {
9098                         /* ran dry for primary network net */
9099                         SCTP_STAT_INCR(sctps_primary_randry);
9100                 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
9101                         /* ran dry with CMT on */
9102                         SCTP_STAT_INCR(sctps_cmt_randry);
9103                 }
9104         }
9105 }
9106
9107 void
9108 sctp_fix_ecn_echo(struct sctp_association *asoc)
9109 {
9110         struct sctp_tmit_chunk *chk;
9111
9112         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9113                 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
9114                         chk->sent = SCTP_DATAGRAM_UNSENT;
9115                 }
9116         }
9117 }
9118
9119 static void
9120 sctp_move_to_an_alt(struct sctp_tcb *stcb,
9121     struct sctp_association *asoc,
9122     struct sctp_nets *net)
9123 {
9124         struct sctp_tmit_chunk *chk;
9125         struct sctp_nets *a_net;
9126
9127         SCTP_TCB_LOCK_ASSERT(stcb);
9128         /*
9129          * JRS 5/14/07 - If CMT PF is turned on, find an alternate
9130          * destination using the PF algorithm for finding alternate
9131          * destinations.
9132          */
9133         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_pf)) {
9134                 a_net = sctp_find_alternate_net(stcb, net, 2);
9135         } else {
9136                 a_net = sctp_find_alternate_net(stcb, net, 0);
9137         }
9138         if ((a_net != net) &&
9139             ((a_net->dest_state & SCTP_ADDR_REACHABLE) == SCTP_ADDR_REACHABLE)) {
9140                 /*
9141                  * We only proceed if a valid alternate is found that is not
9142                  * this one and is reachable. Here we must move all chunks
9143                  * queued in the send queue off of the destination address
9144                  * to our alternate.
9145                  */
9146                 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
9147                         if (chk->whoTo == net) {
9148                                 /* Move the chunk to our alternate */
9149                                 sctp_free_remote_addr(chk->whoTo);
9150                                 chk->whoTo = a_net;
9151                                 atomic_add_int(&a_net->ref_count, 1);
9152                         }
9153                 }
9154         }
9155 }
9156
9157 int
9158 sctp_med_chunk_output(struct sctp_inpcb *inp,
9159     struct sctp_tcb *stcb,
9160     struct sctp_association *asoc,
9161     int *num_out,
9162     int *reason_code,
9163     int control_only, int *cwnd_full, int from_where,
9164     struct timeval *now, int *now_filled, int frag_point, int so_locked
9165 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
9166     SCTP_UNUSED
9167 #endif
9168 )
9169 {
9170         /*
9171          * Ok this is the generic chunk service queue. we must do the
9172          * following: - Service the stream queue that is next, moving any
9173          * message (note I must get a complete message i.e. FIRST/MIDDLE and
9174          * LAST to the out queue in one pass) and assigning TSN's - Check to
9175          * see if the cwnd/rwnd allows any output, if so we go ahead and
9176          * fomulate and send the low level chunks. Making sure to combine
9177          * any control in the control chunk queue also.
9178          */
9179         struct sctp_nets *net;
9180         struct mbuf *outchain, *endoutchain;
9181         struct sctp_tmit_chunk *chk, *nchk;
9182         struct sctphdr *shdr;
9183
9184         /* temp arrays for unlinking */
9185         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9186         int no_fragmentflg, error;
9187         int one_chunk, hbflag, skip_data_for_this_net;
9188         int asconf, cookie, no_out_cnt;
9189         int bundle_at, ctl_cnt, no_data_chunks, cwnd_full_ind, eeor_mode;
9190         unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
9191         struct sctp_nets *start_at, *old_startat = NULL, *send_start_at;
9192         int tsns_sent = 0;
9193         uint32_t auth_offset = 0;
9194         struct sctp_auth_chunk *auth = NULL;
9195         uint16_t auth_keyid = 0;
9196         int data_auth_reqd = 0;
9197
9198         /*
9199          * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
9200          * destination.
9201          */
9202         int pf_hbflag = 0;
9203         int quit_now = 0;
9204
9205         *num_out = 0;
9206         cwnd_full_ind = 0;
9207
9208         if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
9209             (asoc->state & SCTP_STATE_SHUTDOWN_RECEIVED) ||
9210             (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
9211                 eeor_mode = 1;
9212         } else {
9213                 eeor_mode = 0;
9214         }
9215         ctl_cnt = no_out_cnt = asconf = cookie = 0;
9216         /*
9217          * First lets prime the pump. For each destination, if there is room
9218          * in the flight size, attempt to pull an MTU's worth out of the
9219          * stream queues into the general send_queue
9220          */
9221 #ifdef SCTP_AUDITING_ENABLED
9222         sctp_audit_log(0xC2, 2);
9223 #endif
9224         SCTP_TCB_LOCK_ASSERT(stcb);
9225         hbflag = 0;
9226         if ((control_only) || (asoc->stream_reset_outstanding))
9227                 no_data_chunks = 1;
9228         else
9229                 no_data_chunks = 0;
9230
9231         /* Nothing to possible to send? */
9232         if (TAILQ_EMPTY(&asoc->control_send_queue) &&
9233             TAILQ_EMPTY(&asoc->asconf_send_queue) &&
9234             TAILQ_EMPTY(&asoc->send_queue) &&
9235             TAILQ_EMPTY(&asoc->out_wheel)) {
9236                 *reason_code = 9;
9237                 return (0);
9238         }
9239         if (asoc->peers_rwnd == 0) {
9240                 /* No room in peers rwnd */
9241                 *cwnd_full = 1;
9242                 *reason_code = 1;
9243                 if (asoc->total_flight > 0) {
9244                         /* we are allowed one chunk in flight */
9245                         no_data_chunks = 1;
9246                 }
9247         }
9248         if ((no_data_chunks == 0) && (!TAILQ_EMPTY(&asoc->out_wheel))) {
9249                 if (SCTP_BASE_SYSCTL(sctp_cmt_on_off)) {
9250                         /*
9251                          * for CMT we start at the next one past the one we
9252                          * last added data to.
9253                          */
9254                         if (TAILQ_FIRST(&asoc->send_queue) != NULL) {
9255                                 goto skip_the_fill_from_streams;
9256                         }
9257                         if (asoc->last_net_data_came_from) {
9258                                 net = TAILQ_NEXT(asoc->last_net_data_came_from, sctp_next);
9259                                 if (net == NULL) {
9260                                         net = TAILQ_FIRST(&asoc->nets);
9261                                 }
9262                         } else {
9263                                 /* back to start */
9264                                 net = TAILQ_FIRST(&asoc->nets);
9265                         }
9266
9267                         /*
9268                          * JRI-TODO: CMT-MPI. Simply set the first
9269                          * destination (net) to be optimized for the next
9270                          * message to be pulled out of the outwheel. 1. peek
9271                          * at outwheel 2. If large message, set net =
9272                          * highest_cwnd 3. If small message, set net =
9273                          * lowest rtt
9274                          */
9275                 } else {
9276                         net = asoc->primary_destination;
9277                         if (net == NULL) {
9278                                 /* TSNH */
9279                                 net = TAILQ_FIRST(&asoc->nets);
9280                         }
9281                 }
9282                 start_at = net;
9283
9284 one_more_time:
9285                 for (; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
9286                         net->window_probe = 0;
9287                         if (old_startat && (old_startat == net)) {
9288                                 break;
9289                         }
9290                         /*
9291                          * JRI: if dest is unreachable or unconfirmed, do
9292                          * not send data to it
9293                          */
9294                         if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) || (net->dest_state & SCTP_ADDR_UNCONFIRMED)) {
9295                                 continue;
9296                         }
9297                         /*
9298                          * JRI: if dest is in PF state, do not send data to
9299                          * it
9300                          */
9301                         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
9302                             SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
9303                             (net->dest_state & SCTP_ADDR_PF)) {
9304                                 continue;
9305                         }
9306                         if ((SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0) && (net->ref_count < 2)) {
9307                                 /* nothing can be in queue for this guy */
9308                                 continue;
9309                         }
9310                         if (net->flight_size >= net->cwnd) {
9311                                 /* skip this network, no room */
9312                                 cwnd_full_ind++;
9313                                 continue;
9314                         }
9315                         /*
9316                          * JRI : this for loop we are in takes in each net,
9317                          * if its's got space in cwnd and has data sent to
9318                          * it (when CMT is off) then it calls
9319                          * sctp_fill_outqueue for the net. This gets data on
9320                          * the send queue for that network.
9321                          * 
9322                          * In sctp_fill_outqueue TSN's are assigned and data is
9323                          * copied out of the stream buffers. Note mostly
9324                          * copy by reference (we hope).
9325                          */
9326                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9327                                 sctp_log_cwnd(stcb, net, 0, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
9328                         }
9329                         sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now);
9330                         if (quit_now) {
9331                                 /* memory alloc failure */
9332                                 no_data_chunks = 1;
9333                                 goto skip_the_fill_from_streams;
9334                         }
9335                 }
9336                 if (start_at != TAILQ_FIRST(&asoc->nets)) {
9337                         /* got to pick up the beginning stuff. */
9338                         old_startat = start_at;
9339                         start_at = net = TAILQ_FIRST(&asoc->nets);
9340                         if (old_startat)
9341                                 goto one_more_time;
9342                 }
9343         }
9344 skip_the_fill_from_streams:
9345         *cwnd_full = cwnd_full_ind;
9346
9347         /* now service each destination and send out what we can for it */
9348         /* Nothing to send? */
9349         if ((TAILQ_FIRST(&asoc->control_send_queue) == NULL) &&
9350             (TAILQ_FIRST(&asoc->asconf_send_queue) == NULL) &&
9351             (TAILQ_FIRST(&asoc->send_queue) == NULL)) {
9352                 *reason_code = 8;
9353                 return (0);
9354         }
9355         if (no_data_chunks) {
9356                 chk = TAILQ_FIRST(&asoc->asconf_send_queue);
9357                 if (chk == NULL)
9358                         chk = TAILQ_FIRST(&asoc->control_send_queue);
9359         } else {
9360                 chk = TAILQ_FIRST(&asoc->send_queue);
9361         }
9362         if (chk) {
9363                 send_start_at = chk->whoTo;
9364         } else {
9365                 send_start_at = TAILQ_FIRST(&asoc->nets);
9366         }
9367         old_startat = NULL;
9368 again_one_more_time:
9369         for (net = send_start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
9370                 /* how much can we send? */
9371                 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
9372                 if (old_startat && (old_startat == net)) {
9373                         /* through list ocmpletely. */
9374                         break;
9375                 }
9376                 tsns_sent = 0;
9377                 if (net->ref_count < 2) {
9378                         /*
9379                          * Ref-count of 1 so we cannot have data or control
9380                          * queued to this address. Skip it.
9381                          */
9382                         continue;
9383                 }
9384                 ctl_cnt = bundle_at = 0;
9385                 endoutchain = outchain = NULL;
9386                 no_fragmentflg = 1;
9387                 one_chunk = 0;
9388                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
9389                         skip_data_for_this_net = 1;
9390                 } else {
9391                         skip_data_for_this_net = 0;
9392                 }
9393                 if ((net->ro.ro_rt) && (net->ro.ro_rt->rt_ifp)) {
9394                         /*
9395                          * if we have a route and an ifp check to see if we
9396                          * have room to send to this guy
9397                          */
9398                         struct ifnet *ifp;
9399
9400                         ifp = net->ro.ro_rt->rt_ifp;
9401                         if ((ifp->if_snd.ifq_len + 2) >= ifp->if_snd.ifq_maxlen) {
9402                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
9403                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
9404                                         sctp_log_maxburst(stcb, net, ifp->if_snd.ifq_len, ifp->if_snd.ifq_maxlen, SCTP_MAX_IFP_APPLIED);
9405                                 }
9406                                 continue;
9407                         }
9408                 }
9409                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
9410                 case AF_INET:
9411                         mtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
9412                         break;
9413 #ifdef INET6
9414                 case AF_INET6:
9415                         mtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
9416                         break;
9417 #endif
9418                 default:
9419                         /* TSNH */
9420                         mtu = net->mtu;
9421                         break;
9422                 }
9423                 mx_mtu = mtu;
9424                 to_out = 0;
9425                 if (mtu > asoc->peers_rwnd) {
9426                         if (asoc->total_flight > 0) {
9427                                 /* We have a packet in flight somewhere */
9428                                 r_mtu = asoc->peers_rwnd;
9429                         } else {
9430                                 /* We are always allowed to send one MTU out */
9431                                 one_chunk = 1;
9432                                 r_mtu = mtu;
9433                         }
9434                 } else {
9435                         r_mtu = mtu;
9436                 }
9437                 /************************/
9438                 /* ASCONF transmission */
9439                 /************************/
9440                 /* Now first lets go through the asconf queue */
9441                 for (chk = TAILQ_FIRST(&asoc->asconf_send_queue);
9442                     chk; chk = nchk) {
9443                         nchk = TAILQ_NEXT(chk, sctp_next);
9444                         if (chk->rec.chunk_id.id != SCTP_ASCONF) {
9445                                 continue;
9446                         }
9447                         if (chk->whoTo != net) {
9448                                 /*
9449                                  * No, not sent to the network we are
9450                                  * looking at
9451                                  */
9452                                 break;
9453                         }
9454                         if (chk->data == NULL) {
9455                                 break;
9456                         }
9457                         if (chk->sent != SCTP_DATAGRAM_UNSENT &&
9458                             chk->sent != SCTP_DATAGRAM_RESEND) {
9459                                 break;
9460                         }
9461                         /*
9462                          * if no AUTH is yet included and this chunk
9463                          * requires it, make sure to account for it.  We
9464                          * don't apply the size until the AUTH chunk is
9465                          * actually added below in case there is no room for
9466                          * this chunk. NOTE: we overload the use of "omtu"
9467                          * here
9468                          */
9469                         if ((auth == NULL) &&
9470                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9471                             stcb->asoc.peer_auth_chunks)) {
9472                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9473                         } else
9474                                 omtu = 0;
9475                         /* Here we do NOT factor the r_mtu */
9476                         if ((chk->send_size < (int)(mtu - omtu)) ||
9477                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9478                                 /*
9479                                  * We probably should glom the mbuf chain
9480                                  * from the chk->data for control but the
9481                                  * problem is it becomes yet one more level
9482                                  * of tracking to do if for some reason
9483                                  * output fails. Then I have got to
9484                                  * reconstruct the merged control chain.. el
9485                                  * yucko.. for now we take the easy way and
9486                                  * do the copy
9487                                  */
9488                                 /*
9489                                  * Add an AUTH chunk, if chunk requires it
9490                                  * save the offset into the chain for AUTH
9491                                  */
9492                                 if ((auth == NULL) &&
9493                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9494                                     stcb->asoc.peer_auth_chunks))) {
9495                                         outchain = sctp_add_auth_chunk(outchain,
9496                                             &endoutchain,
9497                                             &auth,
9498                                             &auth_offset,
9499                                             stcb,
9500                                             chk->rec.chunk_id.id);
9501                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9502                                 }
9503                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
9504                                     (int)chk->rec.chunk_id.can_take_data,
9505                                     chk->send_size, chk->copy_by_ref);
9506                                 if (outchain == NULL) {
9507                                         *reason_code = 8;
9508                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9509                                         return (ENOMEM);
9510                                 }
9511                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9512                                 /* update our MTU size */
9513                                 if (mtu > (chk->send_size + omtu))
9514                                         mtu -= (chk->send_size + omtu);
9515                                 else
9516                                         mtu = 0;
9517                                 to_out += (chk->send_size + omtu);
9518                                 /* Do clear IP_DF ? */
9519                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9520                                         no_fragmentflg = 0;
9521                                 }
9522                                 if (chk->rec.chunk_id.can_take_data)
9523                                         chk->data = NULL;
9524                                 /*
9525                                  * set hb flag since we can use these for
9526                                  * RTO
9527                                  */
9528                                 hbflag = 1;
9529                                 asconf = 1;
9530                                 /*
9531                                  * should sysctl this: don't bundle data
9532                                  * with ASCONF since it requires AUTH
9533                                  */
9534                                 no_data_chunks = 1;
9535                                 chk->sent = SCTP_DATAGRAM_SENT;
9536                                 chk->snd_count++;
9537                                 if (mtu == 0) {
9538                                         /*
9539                                          * Ok we are out of room but we can
9540                                          * output without effecting the
9541                                          * flight size since this little guy
9542                                          * is a control only packet.
9543                                          */
9544                                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
9545                                         /*
9546                                          * do NOT clear the asconf flag as
9547                                          * it is used to do appropriate
9548                                          * source address selection.
9549                                          */
9550                                         SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
9551                                         if (outchain == NULL) {
9552                                                 /* no memory */
9553                                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
9554                                                 error = ENOBUFS;
9555                                                 *reason_code = 7;
9556                                                 continue;
9557                                         }
9558                                         shdr = mtod(outchain, struct sctphdr *);
9559                                         shdr->src_port = inp->sctp_lport;
9560                                         shdr->dest_port = stcb->rport;
9561                                         shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9562                                         shdr->checksum = 0;
9563                                         auth_offset += sizeof(struct sctphdr);
9564                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9565                                             (struct sockaddr *)&net->ro._l_addr,
9566                                             outchain, auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9567                                             no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
9568                                                 if (error == ENOBUFS) {
9569                                                         asoc->ifp_had_enobuf = 1;
9570                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
9571                                                 }
9572                                                 if (from_where == 0) {
9573                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
9574                                                 }
9575                                                 if (*now_filled == 0) {
9576                                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9577                                                         *now_filled = 1;
9578                                                         *now = net->last_sent_time;
9579                                                 } else {
9580                                                         net->last_sent_time = *now;
9581                                                 }
9582                                                 hbflag = 0;
9583                                                 /* error, could not output */
9584                                                 if (error == EHOSTUNREACH) {
9585                                                         /*
9586                                                          * Destination went
9587                                                          * unreachable
9588                                                          * during this send
9589                                                          */
9590                                                         sctp_move_to_an_alt(stcb, asoc, net);
9591                                                 }
9592                                                 *reason_code = 7;
9593                                                 continue;
9594                                         } else
9595                                                 asoc->ifp_had_enobuf = 0;
9596                                         if (*now_filled == 0) {
9597                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9598                                                 *now_filled = 1;
9599                                                 *now = net->last_sent_time;
9600                                         } else {
9601                                                 net->last_sent_time = *now;
9602                                         }
9603                                         hbflag = 0;
9604                                         /*
9605                                          * increase the number we sent, if a
9606                                          * cookie is sent we don't tell them
9607                                          * any was sent out.
9608                                          */
9609                                         outchain = endoutchain = NULL;
9610                                         auth = NULL;
9611                                         auth_offset = 0;
9612                                         if (!no_out_cnt)
9613                                                 *num_out += ctl_cnt;
9614                                         /* recalc a clean slate and setup */
9615                                         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9616                                                 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9617                                         } else {
9618                                                 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
9619                                         }
9620                                         to_out = 0;
9621                                         no_fragmentflg = 1;
9622                                 }
9623                         }
9624                 }
9625                 /************************/
9626                 /* Control transmission */
9627                 /************************/
9628                 /* Now first lets go through the control queue */
9629                 for (chk = TAILQ_FIRST(&asoc->control_send_queue);
9630                     chk; chk = nchk) {
9631                         nchk = TAILQ_NEXT(chk, sctp_next);
9632                         if (chk->whoTo != net) {
9633                                 /*
9634                                  * No, not sent to the network we are
9635                                  * looking at
9636                                  */
9637                                 continue;
9638                         }
9639                         if (chk->data == NULL) {
9640                                 continue;
9641                         }
9642                         if (chk->sent != SCTP_DATAGRAM_UNSENT) {
9643                                 /*
9644                                  * It must be unsent. Cookies and ASCONF's
9645                                  * hang around but there timers will force
9646                                  * when marked for resend.
9647                                  */
9648                                 continue;
9649                         }
9650                         /*
9651                          * if no AUTH is yet included and this chunk
9652                          * requires it, make sure to account for it.  We
9653                          * don't apply the size until the AUTH chunk is
9654                          * actually added below in case there is no room for
9655                          * this chunk. NOTE: we overload the use of "omtu"
9656                          * here
9657                          */
9658                         if ((auth == NULL) &&
9659                             sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9660                             stcb->asoc.peer_auth_chunks)) {
9661                                 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9662                         } else
9663                                 omtu = 0;
9664                         /* Here we do NOT factor the r_mtu */
9665                         if ((chk->send_size < (int)(mtu - omtu)) ||
9666                             (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9667                                 /*
9668                                  * We probably should glom the mbuf chain
9669                                  * from the chk->data for control but the
9670                                  * problem is it becomes yet one more level
9671                                  * of tracking to do if for some reason
9672                                  * output fails. Then I have got to
9673                                  * reconstruct the merged control chain.. el
9674                                  * yucko.. for now we take the easy way and
9675                                  * do the copy
9676                                  */
9677                                 /*
9678                                  * Add an AUTH chunk, if chunk requires it
9679                                  * save the offset into the chain for AUTH
9680                                  */
9681                                 if ((auth == NULL) &&
9682                                     (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9683                                     stcb->asoc.peer_auth_chunks))) {
9684                                         outchain = sctp_add_auth_chunk(outchain,
9685                                             &endoutchain,
9686                                             &auth,
9687                                             &auth_offset,
9688                                             stcb,
9689                                             chk->rec.chunk_id.id);
9690                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9691                                 }
9692                                 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
9693                                     (int)chk->rec.chunk_id.can_take_data,
9694                                     chk->send_size, chk->copy_by_ref);
9695                                 if (outchain == NULL) {
9696                                         *reason_code = 8;
9697                                         SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9698                                         return (ENOMEM);
9699                                 }
9700                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9701                                 /* update our MTU size */
9702                                 if (mtu > (chk->send_size + omtu))
9703                                         mtu -= (chk->send_size + omtu);
9704                                 else
9705                                         mtu = 0;
9706                                 to_out += (chk->send_size + omtu);
9707                                 /* Do clear IP_DF ? */
9708                                 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9709                                         no_fragmentflg = 0;
9710                                 }
9711                                 if (chk->rec.chunk_id.can_take_data)
9712                                         chk->data = NULL;
9713                                 /* Mark things to be removed, if needed */
9714                                 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
9715                                     (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) ||  /* EY */
9716                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
9717                                     (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
9718                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
9719                                     (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
9720                                     (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
9721                                     (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
9722                                     (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
9723                                     (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
9724                                     (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
9725
9726                                         if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
9727                                                 hbflag = 1;
9728                                                 /*
9729                                                  * JRS 5/14/07 - Set the
9730                                                  * flag to say a heartbeat
9731                                                  * is being sent.
9732                                                  */
9733                                                 pf_hbflag = 1;
9734                                         }
9735                                         /* remove these chunks at the end */
9736                                         if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
9737                                                 /* turn off the timer */
9738                                                 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9739                                                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9740                                                             inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
9741                                                 }
9742                                         }
9743                                         /*
9744                                          * EY -Nr-sack version of the above
9745                                          * if statement
9746                                          */
9747                                         if ((SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack) &&
9748                                             (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {  /* EY !?! */
9749                                                 /* turn off the timer */
9750                                                 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9751                                                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9752                                                             inp, stcb, net, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
9753                                                 }
9754                                         }
9755                                         ctl_cnt++;
9756                                 } else {
9757                                         /*
9758                                          * Other chunks, since they have
9759                                          * timers running (i.e. COOKIE) we
9760                                          * just "trust" that it gets sent or
9761                                          * retransmitted.
9762                                          */
9763                                         ctl_cnt++;
9764                                         if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9765                                                 cookie = 1;
9766                                                 no_out_cnt = 1;
9767                                         }
9768                                         chk->sent = SCTP_DATAGRAM_SENT;
9769                                         chk->snd_count++;
9770                                 }
9771                                 if (mtu == 0) {
9772                                         /*
9773                                          * Ok we are out of room but we can
9774                                          * output without effecting the
9775                                          * flight size since this little guy
9776                                          * is a control only packet.
9777                                          */
9778                                         if (asconf) {
9779                                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
9780                                                 /*
9781                                                  * do NOT clear the asconf
9782                                                  * flag as it is used to do
9783                                                  * appropriate source
9784                                                  * address selection.
9785                                                  */
9786                                         }
9787                                         if (cookie) {
9788                                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
9789                                                 cookie = 0;
9790                                         }
9791                                         SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
9792                                         if (outchain == NULL) {
9793                                                 /* no memory */
9794                                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
9795                                                 error = ENOBUFS;
9796                                                 goto error_out_again;
9797                                         }
9798                                         shdr = mtod(outchain, struct sctphdr *);
9799                                         shdr->src_port = inp->sctp_lport;
9800                                         shdr->dest_port = stcb->rport;
9801                                         shdr->v_tag = htonl(stcb->asoc.peer_vtag);
9802                                         shdr->checksum = 0;
9803                                         auth_offset += sizeof(struct sctphdr);
9804                                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9805                                             (struct sockaddr *)&net->ro._l_addr,
9806                                             outchain,
9807                                             auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9808                                             no_fragmentflg, 0, NULL, asconf, net->port, so_locked, NULL))) {
9809                                                 if (error == ENOBUFS) {
9810                                                         asoc->ifp_had_enobuf = 1;
9811                                                         SCTP_STAT_INCR(sctps_lowlevelerr);
9812                                                 }
9813                                                 if (from_where == 0) {
9814                                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
9815                                                 }
9816                                 error_out_again:
9817                                                 /* error, could not output */
9818                                                 if (hbflag) {
9819                                                         if (*now_filled == 0) {
9820                                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9821                                                                 *now_filled = 1;
9822                                                                 *now = net->last_sent_time;
9823                                                         } else {
9824                                                                 net->last_sent_time = *now;
9825                                                         }
9826                                                         hbflag = 0;
9827                                                 }
9828                                                 if (error == EHOSTUNREACH) {
9829                                                         /*
9830                                                          * Destination went
9831                                                          * unreachable
9832                                                          * during this send
9833                                                          */
9834                                                         sctp_move_to_an_alt(stcb, asoc, net);
9835                                                 }
9836                                                 *reason_code = 7;
9837                                                 continue;
9838                                         } else
9839                                                 asoc->ifp_had_enobuf = 0;
9840                                         /* Only HB or ASCONF advances time */
9841                                         if (hbflag) {
9842                                                 if (*now_filled == 0) {
9843                                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
9844                                                         *now_filled = 1;
9845                                                         *now = net->last_sent_time;
9846                                                 } else {
9847                                                         net->last_sent_time = *now;
9848                                                 }
9849                                                 hbflag = 0;
9850                                         }
9851                                         /*
9852                                          * increase the number we sent, if a
9853                                          * cookie is sent we don't tell them
9854                                          * any was sent out.
9855                                          */
9856                                         outchain = endoutchain = NULL;
9857                                         auth = NULL;
9858                                         auth_offset = 0;
9859                                         if (!no_out_cnt)
9860                                                 *num_out += ctl_cnt;
9861                                         /* recalc a clean slate and setup */
9862                                         if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
9863                                                 mtu = (net->mtu - SCTP_MIN_OVERHEAD);
9864                                         } else {
9865                                                 mtu = (net->mtu - SCTP_MIN_V4_OVERHEAD);
9866                                         }
9867                                         to_out = 0;
9868                                         no_fragmentflg = 1;
9869                                 }
9870                         }
9871                 }
9872                 /*********************/
9873                 /* Data transmission */
9874                 /*********************/
9875                 /*
9876                  * if AUTH for DATA is required and no AUTH has been added
9877                  * yet, account for this in the mtu now... if no data can be
9878                  * bundled, this adjustment won't matter anyways since the
9879                  * packet will be going out...
9880                  */
9881                 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
9882                     stcb->asoc.peer_auth_chunks);
9883                 if (data_auth_reqd && (auth == NULL)) {
9884                         mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9885                 }
9886                 /* now lets add any data within the MTU constraints */
9887                 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
9888                 case AF_INET:
9889                         if (net->mtu > (sizeof(struct ip) + sizeof(struct sctphdr)))
9890                                 omtu = net->mtu - (sizeof(struct ip) + sizeof(struct sctphdr));
9891                         else
9892                                 omtu = 0;
9893                         break;
9894 #ifdef INET6
9895                 case AF_INET6:
9896                         if (net->mtu > (sizeof(struct ip6_hdr) + sizeof(struct sctphdr)))
9897                                 omtu = net->mtu - (sizeof(struct ip6_hdr) + sizeof(struct sctphdr));
9898                         else
9899                                 omtu = 0;
9900                         break;
9901 #endif
9902                 default:
9903                         /* TSNH */
9904                         omtu = 0;
9905                         break;
9906                 }
9907                 if ((((asoc->state & SCTP_STATE_OPEN) == SCTP_STATE_OPEN) && (skip_data_for_this_net == 0)) ||
9908                     (cookie)) {
9909                         for (chk = TAILQ_FIRST(&asoc->send_queue); chk; chk = nchk) {
9910                                 if (no_data_chunks) {
9911                                         /* let only control go out */
9912                                         *reason_code = 1;
9913                                         break;
9914                                 }
9915                                 if (net->flight_size >= net->cwnd) {
9916                                         /* skip this net, no room for data */
9917                                         *reason_code = 2;
9918                                         break;
9919                                 }
9920                                 nchk = TAILQ_NEXT(chk, sctp_next);
9921                                 if (chk->whoTo != net) {
9922                                         /* No, not sent to this net */
9923                                         continue;
9924                                 }
9925                                 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
9926                                         /*-
9927                                          * strange, we have a chunk that is
9928                                          * to big for its destination and
9929                                          * yet no fragment ok flag.
9930                                          * Something went wrong when the
9931                                          * PMTU changed...we did not mark
9932                                          * this chunk for some reason?? I
9933                                          * will fix it here by letting IP
9934                                          * fragment it for now and printing
9935                                          * a warning. This really should not
9936                                          * happen ...
9937                                          */
9938                                         SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
9939                                             chk->send_size, mtu);
9940                                         chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
9941                                 }
9942                                 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
9943                                     ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
9944                                         /* ok we will add this one */
9945
9946                                         /*
9947                                          * Add an AUTH chunk, if chunk
9948                                          * requires it, save the offset into
9949                                          * the chain for AUTH
9950                                          */
9951                                         if (data_auth_reqd) {
9952                                                 if (auth == NULL) {
9953                                                         outchain = sctp_add_auth_chunk(outchain,
9954                                                             &endoutchain,
9955                                                             &auth,
9956                                                             &auth_offset,
9957                                                             stcb,
9958                                                             SCTP_DATA);
9959                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9960                                                         auth_keyid = chk->auth_keyid;
9961                                                 } else if (auth_keyid != chk->auth_keyid) {
9962                                                         /*
9963                                                          * different keyid,
9964                                                          * so done bundling
9965                                                          */
9966                                                         break;
9967                                                 }
9968                                         }
9969                                         outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
9970                                             chk->send_size, chk->copy_by_ref);
9971                                         if (outchain == NULL) {
9972                                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
9973                                                 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9974                                                         sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9975                                                 }
9976                                                 *reason_code = 3;
9977                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9978                                                 return (ENOMEM);
9979                                         }
9980                                         /* upate our MTU size */
9981                                         /* Do clear IP_DF ? */
9982                                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9983                                                 no_fragmentflg = 0;
9984                                         }
9985                                         /* unsigned subtraction of mtu */
9986                                         if (mtu > chk->send_size)
9987                                                 mtu -= chk->send_size;
9988                                         else
9989                                                 mtu = 0;
9990                                         /* unsigned subtraction of r_mtu */
9991                                         if (r_mtu > chk->send_size)
9992                                                 r_mtu -= chk->send_size;
9993                                         else
9994                                                 r_mtu = 0;
9995
9996                                         to_out += chk->send_size;
9997                                         if ((to_out > mx_mtu) && no_fragmentflg) {
9998 #ifdef INVARIANTS
9999                                                 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
10000 #else
10001                                                 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
10002                                                     mx_mtu, to_out);
10003 #endif
10004                                         }
10005                                         chk->window_probe = 0;
10006                                         data_list[bundle_at++] = chk;
10007                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
10008                                                 mtu = 0;
10009                                                 break;
10010                                         }
10011                                         if (chk->sent == SCTP_DATAGRAM_UNSENT) {
10012                                                 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
10013                                                         SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
10014                                                 } else {
10015                                                         SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
10016                                                 }
10017                                                 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
10018                                                     ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
10019                                                         /*
10020                                                          * Count number of
10021                                                          * user msg's that
10022                                                          * were fragmented
10023                                                          * we do this by
10024                                                          * counting when we
10025                                                          * see a LAST
10026                                                          * fragment only.
10027                                                          */
10028                                                         SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
10029                                         }
10030                                         if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
10031                                                 if (one_chunk) {
10032                                                         data_list[0]->window_probe = 1;
10033                                                         net->window_probe = 1;
10034                                                 }
10035                                                 break;
10036                                         }
10037                                 } else {
10038                                         /*
10039                                          * Must be sent in order of the
10040                                          * TSN's (on a network)
10041                                          */
10042                                         break;
10043                                 }
10044                         }       /* for (chunk gather loop for this net) */
10045                 }               /* if asoc.state OPEN */
10046                 /* Is there something to send for this destination? */
10047                 if (outchain) {
10048                         /* We may need to start a control timer or two */
10049                         if (asconf) {
10050                                 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
10051                                     stcb, net);
10052                                 /*
10053                                  * do NOT clear the asconf flag as it is
10054                                  * used to do appropriate source address
10055                                  * selection.
10056                                  */
10057                         }
10058                         if (cookie) {
10059                                 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
10060                                 cookie = 0;
10061                         }
10062                         /* must start a send timer if data is being sent */
10063                         if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
10064                                 /*
10065                                  * no timer running on this destination
10066                                  * restart it.
10067                                  */
10068                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
10069                         } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
10070                                     SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
10071                                     pf_hbflag &&
10072                                     ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF) &&
10073                             (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
10074                                 /*
10075                                  * JRS 5/14/07 - If a HB has been sent to a
10076                                  * PF destination and no T3 timer is
10077                                  * currently running, start the T3 timer to
10078                                  * track the HBs that were sent.
10079                                  */
10080                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
10081                         }
10082                         /* Now send it, if there is anything to send :> */
10083                         SCTP_BUF_PREPEND(outchain, sizeof(struct sctphdr), M_DONTWAIT);
10084                         if (outchain == NULL) {
10085                                 /* out of mbufs */
10086                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
10087                                 error = ENOBUFS;
10088                                 goto errored_send;
10089                         }
10090                         shdr = mtod(outchain, struct sctphdr *);
10091                         shdr->src_port = inp->sctp_lport;
10092                         shdr->dest_port = stcb->rport;
10093                         shdr->v_tag = htonl(stcb->asoc.peer_vtag);
10094                         shdr->checksum = 0;
10095                         auth_offset += sizeof(struct sctphdr);
10096                         /*
10097                          * if data auth isn't needed, use the assoc active
10098                          * key
10099                          */
10100                         if (!data_auth_reqd)
10101                                 auth_keyid = stcb->asoc.authinfo.active_keyid;
10102                         if ((error = sctp_lowlevel_chunk_output(inp,
10103                             stcb,
10104                             net,
10105                             (struct sockaddr *)&net->ro._l_addr,
10106                             outchain,
10107                             auth_offset,
10108                             auth,
10109                             auth_keyid,
10110                             no_fragmentflg,
10111                             bundle_at,
10112                             data_list[0],
10113                             asconf, net->port, so_locked, NULL))) {
10114                                 /* error, we could not output */
10115                                 if (error == ENOBUFS) {
10116                                         SCTP_STAT_INCR(sctps_lowlevelerr);
10117                                         asoc->ifp_had_enobuf = 1;
10118                                 }
10119                                 if (from_where == 0) {
10120                                         SCTP_STAT_INCR(sctps_lowlevelerrusr);
10121                                 }
10122                 errored_send:
10123                                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10124                                 if (hbflag) {
10125                                         if (*now_filled == 0) {
10126                                                 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
10127                                                 *now_filled = 1;
10128                                                 *now = net->last_sent_time;
10129                                         } else {
10130                                                 net->last_sent_time = *now;
10131                                         }
10132                                         hbflag = 0;
10133                                 }
10134                                 if (error == EHOSTUNREACH) {
10135                                         /*
10136                                          * Destination went unreachable
10137                                          * during this send
10138                                          */
10139                                         sctp_move_to_an_alt(stcb, asoc, net);
10140                                 }
10141                                 *reason_code = 6;
10142                                 /*-
10143                                  * I add this line to be paranoid. As far as
10144                                  * I can tell the continue, takes us back to
10145                                  * the top of the for, but just to make sure
10146                                  * I will reset these again here.
10147                                  */
10148                                 ctl_cnt = bundle_at = 0;
10149                                 continue;       /* This takes us back to the
10150                                                  * for() for the nets. */
10151                         } else {
10152                                 asoc->ifp_had_enobuf = 0;
10153                         }
10154                         outchain = endoutchain = NULL;
10155                         auth = NULL;
10156                         auth_offset = 0;
10157                         if (bundle_at || hbflag) {
10158                                 /* For data/asconf and hb set time */
10159                                 if (*now_filled == 0) {
10160                                         (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
10161                                         *now_filled = 1;
10162                                         *now = net->last_sent_time;
10163                                 } else {
10164                                         net->last_sent_time = *now;
10165                                 }
10166                         }
10167                         if (!no_out_cnt) {
10168                                 *num_out += (ctl_cnt + bundle_at);
10169                         }
10170                         if (bundle_at) {
10171                                 /* setup for a RTO measurement */
10172                                 tsns_sent = data_list[0]->rec.data.TSN_seq;
10173                                 /* fill time if not already filled */
10174                                 if (*now_filled == 0) {
10175                                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
10176                                         *now_filled = 1;
10177                                         *now = asoc->time_last_sent;
10178                                 } else {
10179                                         asoc->time_last_sent = *now;
10180                                 }
10181                                 data_list[0]->do_rtt = 1;
10182                                 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
10183                                 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
10184                                 if (SCTP_BASE_SYSCTL(sctp_early_fr)) {
10185                                         if (net->flight_size < net->cwnd) {
10186                                                 /* start or restart it */
10187                                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
10188                                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
10189                                                             SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
10190                                                 }
10191                                                 SCTP_STAT_INCR(sctps_earlyfrstrout);
10192                                                 sctp_timer_start(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net);
10193                                         } else {
10194                                                 /* stop it if its running */
10195                                                 if (SCTP_OS_TIMER_PENDING(&net->fr_timer.timer)) {
10196                                                         SCTP_STAT_INCR(sctps_earlyfrstpout);
10197                                                         sctp_timer_stop(SCTP_TIMER_TYPE_EARLYFR, inp, stcb, net,
10198                                                             SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
10199                                                 }
10200                                         }
10201                                 }
10202                         }
10203                         if (one_chunk) {
10204                                 break;
10205                         }
10206                 }
10207                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10208                         sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
10209                 }
10210         }
10211         if (old_startat == NULL) {
10212                 old_startat = send_start_at;
10213                 send_start_at = TAILQ_FIRST(&asoc->nets);
10214                 if (old_startat)
10215                         goto again_one_more_time;
10216         }
10217         /*
10218          * At the end there should be no NON timed chunks hanging on this
10219          * queue.
10220          */
10221         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10222                 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
10223         }
10224         if ((*num_out == 0) && (*reason_code == 0)) {
10225                 *reason_code = 4;
10226         } else {
10227                 *reason_code = 5;
10228         }
10229         sctp_clean_up_ctl(stcb, asoc);
10230         return (0);
10231 }
10232
10233 void
10234 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
10235 {
10236         /*-
10237          * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
10238          * the control chunk queue.
10239          */
10240         struct sctp_chunkhdr *hdr;
10241         struct sctp_tmit_chunk *chk;
10242         struct mbuf *mat;
10243
10244         SCTP_TCB_LOCK_ASSERT(stcb);
10245         sctp_alloc_a_chunk(stcb, chk);
10246         if (chk == NULL) {
10247                 /* no memory */
10248                 sctp_m_freem(op_err);
10249                 return;
10250         }
10251         chk->copy_by_ref = 0;
10252         SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_DONTWAIT);
10253         if (op_err == NULL) {
10254                 sctp_free_a_chunk(stcb, chk);
10255                 return;
10256         }
10257         chk->send_size = 0;
10258         mat = op_err;
10259         while (mat != NULL) {
10260                 chk->send_size += SCTP_BUF_LEN(mat);
10261                 mat = SCTP_BUF_NEXT(mat);
10262         }
10263         chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
10264         chk->rec.chunk_id.can_take_data = 1;
10265         chk->sent = SCTP_DATAGRAM_UNSENT;
10266         chk->snd_count = 0;
10267         chk->flags = 0;
10268         chk->asoc = &stcb->asoc;
10269         chk->data = op_err;
10270         chk->whoTo = chk->asoc->primary_destination;
10271         atomic_add_int(&chk->whoTo->ref_count, 1);
10272         hdr = mtod(op_err, struct sctp_chunkhdr *);
10273         hdr->chunk_type = SCTP_OPERATION_ERROR;
10274         hdr->chunk_flags = 0;
10275         hdr->chunk_length = htons(chk->send_size);
10276         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue,
10277             chk,
10278             sctp_next);
10279         chk->asoc->ctrl_queue_cnt++;
10280 }
10281
10282 int
10283 sctp_send_cookie_echo(struct mbuf *m,
10284     int offset,
10285     struct sctp_tcb *stcb,
10286     struct sctp_nets *net)
10287 {
10288         /*-
10289          * pull out the cookie and put it at the front of the control chunk
10290          * queue.
10291          */
10292         int at;
10293         struct mbuf *cookie;
10294         struct sctp_paramhdr parm, *phdr;
10295         struct sctp_chunkhdr *hdr;
10296         struct sctp_tmit_chunk *chk;
10297         uint16_t ptype, plen;
10298
10299         /* First find the cookie in the param area */
10300         cookie = NULL;
10301         at = offset + sizeof(struct sctp_init_chunk);
10302
10303         SCTP_TCB_LOCK_ASSERT(stcb);
10304         do {
10305                 phdr = sctp_get_next_param(m, at, &parm, sizeof(parm));
10306                 if (phdr == NULL) {
10307                         return (-3);
10308                 }
10309                 ptype = ntohs(phdr->param_type);
10310                 plen = ntohs(phdr->param_length);
10311                 if (ptype == SCTP_STATE_COOKIE) {
10312                         int pad;
10313
10314                         /* found the cookie */
10315                         if ((pad = (plen % 4))) {
10316                                 plen += 4 - pad;
10317                         }
10318                         cookie = SCTP_M_COPYM(m, at, plen, M_DONTWAIT);
10319                         if (cookie == NULL) {
10320                                 /* No memory */
10321                                 return (-2);
10322                         }
10323 #ifdef SCTP_MBUF_LOGGING
10324                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10325                                 struct mbuf *mat;
10326
10327                                 mat = cookie;
10328                                 while (mat) {
10329                                         if (SCTP_BUF_IS_EXTENDED(mat)) {
10330                                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10331                                         }
10332                                         mat = SCTP_BUF_NEXT(mat);
10333                                 }
10334                         }
10335 #endif
10336                         break;
10337                 }
10338                 at += SCTP_SIZE32(plen);
10339         } while (phdr);
10340         if (cookie == NULL) {
10341                 /* Did not find the cookie */
10342                 return (-3);
10343         }
10344         /* ok, we got the cookie lets change it into a cookie echo chunk */
10345
10346         /* first the change from param to cookie */
10347         hdr = mtod(cookie, struct sctp_chunkhdr *);
10348         hdr->chunk_type = SCTP_COOKIE_ECHO;
10349         hdr->chunk_flags = 0;
10350         /* get the chunk stuff now and place it in the FRONT of the queue */
10351         sctp_alloc_a_chunk(stcb, chk);
10352         if (chk == NULL) {
10353                 /* no memory */
10354                 sctp_m_freem(cookie);
10355                 return (-5);
10356         }
10357         chk->copy_by_ref = 0;
10358         chk->send_size = plen;
10359         chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
10360         chk->rec.chunk_id.can_take_data = 0;
10361         chk->sent = SCTP_DATAGRAM_UNSENT;
10362         chk->snd_count = 0;
10363         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
10364         chk->asoc = &stcb->asoc;
10365         chk->data = cookie;
10366         chk->whoTo = chk->asoc->primary_destination;
10367         atomic_add_int(&chk->whoTo->ref_count, 1);
10368         TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
10369         chk->asoc->ctrl_queue_cnt++;
10370         return (0);
10371 }
10372
10373 void
10374 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
10375     struct mbuf *m,
10376     int offset,
10377     int chk_length,
10378     struct sctp_nets *net)
10379 {
10380         /*
10381          * take a HB request and make it into a HB ack and send it.
10382          */
10383         struct mbuf *outchain;
10384         struct sctp_chunkhdr *chdr;
10385         struct sctp_tmit_chunk *chk;
10386
10387
10388         if (net == NULL)
10389                 /* must have a net pointer */
10390                 return;
10391
10392         outchain = SCTP_M_COPYM(m, offset, chk_length, M_DONTWAIT);
10393         if (outchain == NULL) {
10394                 /* gak out of memory */
10395                 return;
10396         }
10397 #ifdef SCTP_MBUF_LOGGING
10398         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10399                 struct mbuf *mat;
10400
10401                 mat = outchain;
10402                 while (mat) {
10403                         if (SCTP_BUF_IS_EXTENDED(mat)) {
10404                                 sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10405                         }
10406                         mat = SCTP_BUF_NEXT(mat);
10407                 }
10408         }
10409 #endif
10410         chdr = mtod(outchain, struct sctp_chunkhdr *);
10411         chdr->chunk_type = SCTP_HEARTBEAT_ACK;
10412         chdr->chunk_flags = 0;
10413         if (chk_length % 4) {
10414                 /* need pad */
10415                 uint32_t cpthis = 0;
10416                 int padlen;
10417
10418                 padlen = 4 - (chk_length % 4);
10419                 m_copyback(outchain, chk_length, padlen, (caddr_t)&cpthis);
10420         }
10421         sctp_alloc_a_chunk(stcb, chk);
10422         if (chk == NULL) {
10423                 /* no memory */
10424                 sctp_m_freem(outchain);
10425                 return;
10426         }
10427         chk->copy_by_ref = 0;
10428         chk->send_size = chk_length;
10429         chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
10430         chk->rec.chunk_id.can_take_data = 1;
10431         chk->sent = SCTP_DATAGRAM_UNSENT;
10432         chk->snd_count = 0;
10433         chk->flags = 0;
10434         chk->asoc = &stcb->asoc;
10435         chk->data = outchain;
10436         chk->whoTo = net;
10437         atomic_add_int(&chk->whoTo->ref_count, 1);
10438         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10439         chk->asoc->ctrl_queue_cnt++;
10440 }
10441
10442 void
10443 sctp_send_cookie_ack(struct sctp_tcb *stcb)
10444 {
10445         /* formulate and queue a cookie-ack back to sender */
10446         struct mbuf *cookie_ack;
10447         struct sctp_chunkhdr *hdr;
10448         struct sctp_tmit_chunk *chk;
10449
10450         cookie_ack = NULL;
10451         SCTP_TCB_LOCK_ASSERT(stcb);
10452
10453         cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_DONTWAIT, 1, MT_HEADER);
10454         if (cookie_ack == NULL) {
10455                 /* no mbuf's */
10456                 return;
10457         }
10458         SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
10459         sctp_alloc_a_chunk(stcb, chk);
10460         if (chk == NULL) {
10461                 /* no memory */
10462                 sctp_m_freem(cookie_ack);
10463                 return;
10464         }
10465         chk->copy_by_ref = 0;
10466         chk->send_size = sizeof(struct sctp_chunkhdr);
10467         chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
10468         chk->rec.chunk_id.can_take_data = 1;
10469         chk->sent = SCTP_DATAGRAM_UNSENT;
10470         chk->snd_count = 0;
10471         chk->flags = 0;
10472         chk->asoc = &stcb->asoc;
10473         chk->data = cookie_ack;
10474         if (chk->asoc->last_control_chunk_from != NULL) {
10475                 chk->whoTo = chk->asoc->last_control_chunk_from;
10476         } else {
10477                 chk->whoTo = chk->asoc->primary_destination;
10478         }
10479         atomic_add_int(&chk->whoTo->ref_count, 1);
10480         hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
10481         hdr->chunk_type = SCTP_COOKIE_ACK;
10482         hdr->chunk_flags = 0;
10483         hdr->chunk_length = htons(chk->send_size);
10484         SCTP_BUF_LEN(cookie_ack) = chk->send_size;
10485         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10486         chk->asoc->ctrl_queue_cnt++;
10487         return;
10488 }
10489
10490
10491 void
10492 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
10493 {
10494         /* formulate and queue a SHUTDOWN-ACK back to the sender */
10495         struct mbuf *m_shutdown_ack;
10496         struct sctp_shutdown_ack_chunk *ack_cp;
10497         struct sctp_tmit_chunk *chk;
10498
10499         m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10500         if (m_shutdown_ack == NULL) {
10501                 /* no mbuf's */
10502                 return;
10503         }
10504         SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
10505         sctp_alloc_a_chunk(stcb, chk);
10506         if (chk == NULL) {
10507                 /* no memory */
10508                 sctp_m_freem(m_shutdown_ack);
10509                 return;
10510         }
10511         chk->copy_by_ref = 0;
10512         chk->send_size = sizeof(struct sctp_chunkhdr);
10513         chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
10514         chk->rec.chunk_id.can_take_data = 1;
10515         chk->sent = SCTP_DATAGRAM_UNSENT;
10516         chk->snd_count = 0;
10517         chk->flags = 0;
10518         chk->asoc = &stcb->asoc;
10519         chk->data = m_shutdown_ack;
10520         chk->whoTo = net;
10521         atomic_add_int(&net->ref_count, 1);
10522
10523         ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
10524         ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
10525         ack_cp->ch.chunk_flags = 0;
10526         ack_cp->ch.chunk_length = htons(chk->send_size);
10527         SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
10528         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10529         chk->asoc->ctrl_queue_cnt++;
10530         return;
10531 }
10532
10533 void
10534 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
10535 {
10536         /* formulate and queue a SHUTDOWN to the sender */
10537         struct mbuf *m_shutdown;
10538         struct sctp_shutdown_chunk *shutdown_cp;
10539         struct sctp_tmit_chunk *chk;
10540
10541         m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
10542         if (m_shutdown == NULL) {
10543                 /* no mbuf's */
10544                 return;
10545         }
10546         SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
10547         sctp_alloc_a_chunk(stcb, chk);
10548         if (chk == NULL) {
10549                 /* no memory */
10550                 sctp_m_freem(m_shutdown);
10551                 return;
10552         }
10553         chk->copy_by_ref = 0;
10554         chk->send_size = sizeof(struct sctp_shutdown_chunk);
10555         chk->rec.chunk_id.id = SCTP_SHUTDOWN;
10556         chk->rec.chunk_id.can_take_data = 1;
10557         chk->sent = SCTP_DATAGRAM_UNSENT;
10558         chk->snd_count = 0;
10559         chk->flags = 0;
10560         chk->asoc = &stcb->asoc;
10561         chk->data = m_shutdown;
10562         chk->whoTo = net;
10563         atomic_add_int(&net->ref_count, 1);
10564
10565         shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
10566         shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
10567         shutdown_cp->ch.chunk_flags = 0;
10568         shutdown_cp->ch.chunk_length = htons(chk->send_size);
10569         shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
10570         SCTP_BUF_LEN(m_shutdown) = chk->send_size;
10571         TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10572         chk->asoc->ctrl_queue_cnt++;
10573         return;
10574 }
10575
10576 void
10577 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
10578 {
10579         /*
10580          * formulate and queue an ASCONF to the peer. ASCONF parameters
10581          * should be queued on the assoc queue.
10582          */
10583         struct sctp_tmit_chunk *chk;
10584         struct mbuf *m_asconf;
10585         int len;
10586
10587         SCTP_TCB_LOCK_ASSERT(stcb);
10588
10589         if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
10590             (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
10591                 /* can't send a new one if there is one in flight already */
10592                 return;
10593         }
10594         /* compose an ASCONF chunk, maximum length is PMTU */
10595         m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
10596         if (m_asconf == NULL) {
10597                 return;
10598         }
10599         sctp_alloc_a_chunk(stcb, chk);
10600         if (chk == NULL) {
10601                 /* no memory */
10602                 sctp_m_freem(m_asconf);
10603                 return;
10604         }
10605         chk->copy_by_ref = 0;
10606         chk->data = m_asconf;
10607         chk->send_size = len;
10608         chk->rec.chunk_id.id = SCTP_ASCONF;
10609         chk->rec.chunk_id.can_take_data = 0;
10610         chk->sent = SCTP_DATAGRAM_UNSENT;
10611         chk->snd_count = 0;
10612         chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
10613         chk->asoc = &stcb->asoc;
10614         chk->whoTo = net;
10615         atomic_add_int(&chk->whoTo->ref_count, 1);
10616         TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
10617         chk->asoc->ctrl_queue_cnt++;
10618         return;
10619 }
10620
10621 void
10622 sctp_send_asconf_ack(struct sctp_tcb *stcb)
10623 {
10624         /*
10625          * formulate and queue a asconf-ack back to sender. the asconf-ack
10626          * must be stored in the tcb.
10627          */
10628         struct sctp_tmit_chunk *chk;
10629         struct sctp_asconf_ack *ack, *latest_ack;
10630         struct mbuf *m_ack, *m;
10631         struct sctp_nets *net = NULL;
10632
10633         SCTP_TCB_LOCK_ASSERT(stcb);
10634         /* Get the latest ASCONF-ACK */
10635         latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
10636         if (latest_ack == NULL) {
10637                 return;
10638         }
10639         if (latest_ack->last_sent_to != NULL &&
10640             latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
10641                 /* we're doing a retransmission */
10642                 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
10643                 if (net == NULL) {
10644                         /* no alternate */
10645                         if (stcb->asoc.last_control_chunk_from == NULL)
10646                                 net = stcb->asoc.primary_destination;
10647                         else
10648                                 net = stcb->asoc.last_control_chunk_from;
10649                 }
10650         } else {
10651                 /* normal case */
10652                 if (stcb->asoc.last_control_chunk_from == NULL)
10653                         net = stcb->asoc.primary_destination;
10654                 else
10655                         net = stcb->asoc.last_control_chunk_from;
10656         }
10657         latest_ack->last_sent_to = net;
10658
10659         TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
10660                 if (ack->data == NULL) {
10661                         continue;
10662                 }
10663                 /* copy the asconf_ack */
10664                 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_DONTWAIT);
10665                 if (m_ack == NULL) {
10666                         /* couldn't copy it */
10667                         return;
10668                 }
10669 #ifdef SCTP_MBUF_LOGGING
10670                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10671                         struct mbuf *mat;
10672
10673                         mat = m_ack;
10674                         while (mat) {
10675                                 if (SCTP_BUF_IS_EXTENDED(mat)) {
10676                                         sctp_log_mb(mat, SCTP_MBUF_ICOPY);
10677                                 }
10678                                 mat = SCTP_BUF_NEXT(mat);
10679                         }
10680                 }
10681 #endif
10682
10683                 sctp_alloc_a_chunk(stcb, chk);
10684                 if (chk == NULL) {
10685                         /* no memory */
10686                         if (m_ack)
10687                                 sctp_m_freem(m_ack);
10688                         return;
10689                 }
10690                 chk->copy_by_ref = 0;
10691
10692                 chk->whoTo = net;
10693                 chk->data = m_ack;
10694                 chk->send_size = 0;
10695                 /* Get size */
10696                 m = m_ack;
10697                 chk->send_size = ack->len;
10698                 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
10699                 chk->rec.chunk_id.can_take_data = 1;
10700                 chk->sent = SCTP_DATAGRAM_UNSENT;
10701                 chk->snd_count = 0;
10702                 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;  /* XXX */
10703                 chk->asoc = &stcb->asoc;
10704                 atomic_add_int(&chk->whoTo->ref_count, 1);
10705
10706                 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10707                 chk->asoc->ctrl_queue_cnt++;
10708         }
10709         return;
10710 }
10711
10712
10713 static int
10714 sctp_chunk_retransmission(struct sctp_inpcb *inp,
10715     struct sctp_tcb *stcb,
10716     struct sctp_association *asoc,
10717     int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked
10718 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
10719     SCTP_UNUSED
10720 #endif
10721 )
10722 {
10723         /*-
10724          * send out one MTU of retransmission. If fast_retransmit is
10725          * happening we ignore the cwnd. Otherwise we obey the cwnd and
10726          * rwnd. For a Cookie or Asconf in the control chunk queue we
10727          * retransmit them by themselves.
10728          *
10729          * For data chunks we will pick out the lowest TSN's in the sent_queue
10730          * marked for resend and bundle them all together (up to a MTU of
10731          * destination). The address to send to should have been
10732          * selected/changed where the retransmission was marked (i.e. in FR
10733          * or t3-timeout routines).
10734          */
10735         struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
10736         struct sctp_tmit_chunk *chk, *fwd;
10737         struct mbuf *m, *endofchain;
10738         struct sctphdr *shdr;
10739         struct sctp_nets *net = NULL;
10740         uint32_t tsns_sent = 0;
10741         int no_fragmentflg, bundle_at, cnt_thru;
10742         unsigned int mtu;
10743         int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
10744         struct sctp_auth_chunk *auth = NULL;
10745         uint32_t auth_offset = 0;
10746         uint16_t auth_keyid = 0;
10747         int data_auth_reqd = 0;
10748         uint32_t dmtu = 0;
10749
10750         SCTP_TCB_LOCK_ASSERT(stcb);
10751         tmr_started = ctl_cnt = bundle_at = error = 0;
10752         no_fragmentflg = 1;
10753         fwd_tsn = 0;
10754         *cnt_out = 0;
10755         fwd = NULL;
10756         endofchain = m = NULL;
10757 #ifdef SCTP_AUDITING_ENABLED
10758         sctp_audit_log(0xC3, 1);
10759 #endif
10760         if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
10761             (TAILQ_EMPTY(&asoc->control_send_queue))) {
10762                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
10763                     asoc->sent_queue_retran_cnt);
10764                 asoc->sent_queue_cnt = 0;
10765                 asoc->sent_queue_cnt_removeable = 0;
10766                 /* send back 0/0 so we enter normal transmission */
10767                 *cnt_out = 0;
10768                 return (0);
10769         }
10770         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10771                 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
10772                     (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
10773                     (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
10774                         if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
10775                                 if (chk != asoc->str_reset) {
10776                                         /*
10777                                          * not eligible for retran if its
10778                                          * not ours
10779                                          */
10780                                         continue;
10781                                 }
10782                         }
10783                         ctl_cnt++;
10784                         if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10785                                 fwd_tsn = 1;
10786                                 fwd = chk;
10787                         }
10788                         /*
10789                          * Add an AUTH chunk, if chunk requires it save the
10790                          * offset into the chain for AUTH
10791                          */
10792                         if ((auth == NULL) &&
10793                             (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
10794                             stcb->asoc.peer_auth_chunks))) {
10795                                 m = sctp_add_auth_chunk(m, &endofchain,
10796                                     &auth, &auth_offset,
10797                                     stcb,
10798                                     chk->rec.chunk_id.id);
10799                                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10800                         }
10801                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
10802                         break;
10803                 }
10804         }
10805         one_chunk = 0;
10806         cnt_thru = 0;
10807         /* do we have control chunks to retransmit? */
10808         if (m != NULL) {
10809                 /* Start a timer no matter if we suceed or fail */
10810                 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
10811                         sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
10812                 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
10813                         sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
10814
10815                 SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
10816                 if (m == NULL) {
10817                         SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
10818                         return (ENOBUFS);
10819                 }
10820                 shdr = mtod(m, struct sctphdr *);
10821                 shdr->src_port = inp->sctp_lport;
10822                 shdr->dest_port = stcb->rport;
10823                 shdr->v_tag = htonl(stcb->asoc.peer_vtag);
10824                 shdr->checksum = 0;
10825                 auth_offset += sizeof(struct sctphdr);
10826                 chk->snd_count++;       /* update our count */
10827
10828                 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
10829                     (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
10830                     auth_offset, auth, stcb->asoc.authinfo.active_keyid,
10831                     no_fragmentflg, 0, NULL, 0, chk->whoTo->port, so_locked, NULL))) {
10832                         SCTP_STAT_INCR(sctps_lowlevelerr);
10833                         return (error);
10834                 }
10835                 m = endofchain = NULL;
10836                 auth = NULL;
10837                 auth_offset = 0;
10838                 /*
10839                  * We don't want to mark the net->sent time here since this
10840                  * we use this for HB and retrans cannot measure RTT
10841                  */
10842                 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
10843                 *cnt_out += 1;
10844                 chk->sent = SCTP_DATAGRAM_SENT;
10845                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
10846                 if (fwd_tsn == 0) {
10847                         return (0);
10848                 } else {
10849                         /* Clean up the fwd-tsn list */
10850                         sctp_clean_up_ctl(stcb, asoc);
10851                         return (0);
10852                 }
10853         }
10854         /*
10855          * Ok, it is just data retransmission we need to do or that and a
10856          * fwd-tsn with it all.
10857          */
10858         if (TAILQ_EMPTY(&asoc->sent_queue)) {
10859                 return (SCTP_RETRAN_DONE);
10860         }
10861         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED) ||
10862             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT)) {
10863                 /* not yet open, resend the cookie and that is it */
10864                 return (1);
10865         }
10866 #ifdef SCTP_AUDITING_ENABLED
10867         sctp_auditing(20, inp, stcb, NULL);
10868 #endif
10869         data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
10870         TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
10871                 if (chk->sent != SCTP_DATAGRAM_RESEND) {
10872                         /* No, not sent to this net or not ready for rtx */
10873                         continue;
10874                 }
10875                 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
10876                     (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
10877                         /* Gak, we have exceeded max unlucky retran, abort! */
10878                         SCTP_PRINTF("Gak, chk->snd_count:%d >= max:%d - send abort\n",
10879                             chk->snd_count,
10880                             SCTP_BASE_SYSCTL(sctp_max_retran_chunk));
10881                         atomic_add_int(&stcb->asoc.refcnt, 1);
10882                         sctp_abort_an_association(stcb->sctp_ep, stcb, 0, NULL, so_locked);
10883                         SCTP_TCB_LOCK(stcb);
10884                         atomic_subtract_int(&stcb->asoc.refcnt, 1);
10885                         return (SCTP_RETRAN_EXIT);
10886                 }
10887                 /* pick up the net */
10888                 net = chk->whoTo;
10889                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10890                         mtu = (net->mtu - SCTP_MIN_OVERHEAD);
10891                 } else {
10892                         mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
10893                 }
10894
10895                 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
10896                         /* No room in peers rwnd */
10897                         uint32_t tsn;
10898
10899                         tsn = asoc->last_acked_seq + 1;
10900                         if (tsn == chk->rec.data.TSN_seq) {
10901                                 /*
10902                                  * we make a special exception for this
10903                                  * case. The peer has no rwnd but is missing
10904                                  * the lowest chunk.. which is probably what
10905                                  * is holding up the rwnd.
10906                                  */
10907                                 goto one_chunk_around;
10908                         }
10909                         return (1);
10910                 }
10911 one_chunk_around:
10912                 if (asoc->peers_rwnd < mtu) {
10913                         one_chunk = 1;
10914                         if ((asoc->peers_rwnd == 0) &&
10915                             (asoc->total_flight == 0)) {
10916                                 chk->window_probe = 1;
10917                                 chk->whoTo->window_probe = 1;
10918                         }
10919                 }
10920 #ifdef SCTP_AUDITING_ENABLED
10921                 sctp_audit_log(0xC3, 2);
10922 #endif
10923                 bundle_at = 0;
10924                 m = NULL;
10925                 net->fast_retran_ip = 0;
10926                 if (chk->rec.data.doing_fast_retransmit == 0) {
10927                         /*
10928                          * if no FR in progress skip destination that have
10929                          * flight_size > cwnd.
10930                          */
10931                         if (net->flight_size >= net->cwnd) {
10932                                 continue;
10933                         }
10934                 } else {
10935                         /*
10936                          * Mark the destination net to have FR recovery
10937                          * limits put on it.
10938                          */
10939                         *fr_done = 1;
10940                         net->fast_retran_ip = 1;
10941                 }
10942
10943                 /*
10944                  * if no AUTH is yet included and this chunk requires it,
10945                  * make sure to account for it.  We don't apply the size
10946                  * until the AUTH chunk is actually added below in case
10947                  * there is no room for this chunk.
10948                  */
10949                 if (data_auth_reqd && (auth == NULL)) {
10950                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
10951                 } else
10952                         dmtu = 0;
10953
10954                 if ((chk->send_size <= (mtu - dmtu)) ||
10955                     (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
10956                         /* ok we will add this one */
10957                         if (data_auth_reqd) {
10958                                 if (auth == NULL) {
10959                                         m = sctp_add_auth_chunk(m,
10960                                             &endofchain,
10961                                             &auth,
10962                                             &auth_offset,
10963                                             stcb,
10964                                             SCTP_DATA);
10965                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10966                                         auth_keyid = chk->auth_keyid;
10967                                 } else if (chk->auth_keyid != auth_keyid) {
10968                                         /* different keyid, so done bundling */
10969                                         break;
10970                                 }
10971                         }
10972                         m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
10973                         if (m == NULL) {
10974                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10975                                 return (ENOMEM);
10976                         }
10977                         /* Do clear IP_DF ? */
10978                         if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
10979                                 no_fragmentflg = 0;
10980                         }
10981                         /* upate our MTU size */
10982                         if (mtu > (chk->send_size + dmtu))
10983                                 mtu -= (chk->send_size + dmtu);
10984                         else
10985                                 mtu = 0;
10986                         data_list[bundle_at++] = chk;
10987                         if (one_chunk && (asoc->total_flight <= 0)) {
10988                                 SCTP_STAT_INCR(sctps_windowprobed);
10989                         }
10990                 }
10991                 if (one_chunk == 0) {
10992                         /*
10993                          * now are there anymore forward from chk to pick
10994                          * up?
10995                          */
10996                         fwd = TAILQ_NEXT(chk, sctp_next);
10997                         while (fwd) {
10998                                 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
10999                                         /* Nope, not for retran */
11000                                         fwd = TAILQ_NEXT(fwd, sctp_next);
11001                                         continue;
11002                                 }
11003                                 if (fwd->whoTo != net) {
11004                                         /* Nope, not the net in question */
11005                                         fwd = TAILQ_NEXT(fwd, sctp_next);
11006                                         continue;
11007                                 }
11008                                 if (data_auth_reqd && (auth == NULL)) {
11009                                         dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
11010                                 } else
11011                                         dmtu = 0;
11012                                 if (fwd->send_size <= (mtu - dmtu)) {
11013                                         if (data_auth_reqd) {
11014                                                 if (auth == NULL) {
11015                                                         m = sctp_add_auth_chunk(m,
11016                                                             &endofchain,
11017                                                             &auth,
11018                                                             &auth_offset,
11019                                                             stcb,
11020                                                             SCTP_DATA);
11021                                                         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11022                                                         auth_keyid = fwd->auth_keyid;
11023                                                 } else if (fwd->auth_keyid != auth_keyid) {
11024                                                         /*
11025                                                          * different keyid,
11026                                                          * so done bundling
11027                                                          */
11028                                                         break;
11029                                                 }
11030                                         }
11031                                         m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
11032                                         if (m == NULL) {
11033                                                 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11034                                                 return (ENOMEM);
11035                                         }
11036                                         /* Do clear IP_DF ? */
11037                                         if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
11038                                                 no_fragmentflg = 0;
11039                                         }
11040                                         /* upate our MTU size */
11041                                         if (mtu > (fwd->send_size + dmtu))
11042                                                 mtu -= (fwd->send_size + dmtu);
11043                                         else
11044                                                 mtu = 0;
11045                                         data_list[bundle_at++] = fwd;
11046                                         if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
11047                                                 break;
11048                                         }
11049                                         fwd = TAILQ_NEXT(fwd, sctp_next);
11050                                 } else {
11051                                         /* can't fit so we are done */
11052                                         break;
11053                                 }
11054                         }
11055                 }
11056                 /* Is there something to send for this destination? */
11057                 if (m) {
11058                         /*
11059                          * No matter if we fail/or suceed we should start a
11060                          * timer. A failure is like a lost IP packet :-)
11061                          */
11062                         if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
11063                                 /*
11064                                  * no timer running on this destination
11065                                  * restart it.
11066                                  */
11067                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
11068                                 tmr_started = 1;
11069                         }
11070                         SCTP_BUF_PREPEND(m, sizeof(struct sctphdr), M_DONTWAIT);
11071                         if (m == NULL) {
11072                                 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
11073                                 return (ENOBUFS);
11074                         }
11075                         shdr = mtod(m, struct sctphdr *);
11076                         shdr->src_port = inp->sctp_lport;
11077                         shdr->dest_port = stcb->rport;
11078                         shdr->v_tag = htonl(stcb->asoc.peer_vtag);
11079                         shdr->checksum = 0;
11080                         auth_offset += sizeof(struct sctphdr);
11081                         /*
11082                          * if doing DATA auth, use the data chunk(s) key id,
11083                          * otherwise use the assoc's active key id
11084                          */
11085                         if (!data_auth_reqd)
11086                                 auth_keyid = stcb->asoc.authinfo.active_keyid;
11087                         /* Now lets send it, if there is anything to send :> */
11088                         if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
11089                             (struct sockaddr *)&net->ro._l_addr, m,
11090                             auth_offset, auth, auth_keyid,
11091                             no_fragmentflg, 0, NULL, 0, net->port, so_locked, NULL))) {
11092                                 /* error, we could not output */
11093                                 SCTP_STAT_INCR(sctps_lowlevelerr);
11094                                 return (error);
11095                         }
11096                         m = endofchain = NULL;
11097                         auth = NULL;
11098                         auth_offset = 0;
11099                         /* For HB's */
11100                         /*
11101                          * We don't want to mark the net->sent time here
11102                          * since this we use this for HB and retrans cannot
11103                          * measure RTT
11104                          */
11105                         /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
11106
11107                         /* For auto-close */
11108                         cnt_thru++;
11109                         if (*now_filled == 0) {
11110                                 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
11111                                 *now = asoc->time_last_sent;
11112                                 *now_filled = 1;
11113                         } else {
11114                                 asoc->time_last_sent = *now;
11115                         }
11116                         *cnt_out += bundle_at;
11117 #ifdef SCTP_AUDITING_ENABLED
11118                         sctp_audit_log(0xC4, bundle_at);
11119 #endif
11120                         if (bundle_at) {
11121                                 tsns_sent = data_list[0]->rec.data.TSN_seq;
11122                         }
11123                         for (i = 0; i < bundle_at; i++) {
11124                                 SCTP_STAT_INCR(sctps_sendretransdata);
11125                                 data_list[i]->sent = SCTP_DATAGRAM_SENT;
11126                                 /*
11127                                  * When we have a revoked data, and we
11128                                  * retransmit it, then we clear the revoked
11129                                  * flag since this flag dictates if we
11130                                  * subtracted from the fs
11131                                  */
11132                                 if (data_list[i]->rec.data.chunk_was_revoked) {
11133                                         /* Deflate the cwnd */
11134                                         data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
11135                                         data_list[i]->rec.data.chunk_was_revoked = 0;
11136                                 }
11137                                 data_list[i]->snd_count++;
11138                                 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
11139                                 /* record the time */
11140                                 data_list[i]->sent_rcv_time = asoc->time_last_sent;
11141                                 if (data_list[i]->book_size_scale) {
11142                                         /*
11143                                          * need to double the book size on
11144                                          * this one
11145                                          */
11146                                         data_list[i]->book_size_scale = 0;
11147                                         /*
11148                                          * Since we double the booksize, we
11149                                          * must also double the output queue
11150                                          * size, since this get shrunk when
11151                                          * we free by this amount.
11152                                          */
11153                                         atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
11154                                         data_list[i]->book_size *= 2;
11155
11156
11157                                 } else {
11158                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
11159                                                 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
11160                                                     asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
11161                                         }
11162                                         asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
11163                                             (uint32_t) (data_list[i]->send_size +
11164                                             SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
11165                                 }
11166                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
11167                                         sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
11168                                             data_list[i]->whoTo->flight_size,
11169                                             data_list[i]->book_size,
11170                                             (uintptr_t) data_list[i]->whoTo,
11171                                             data_list[i]->rec.data.TSN_seq);
11172                                 }
11173                                 sctp_flight_size_increase(data_list[i]);
11174                                 sctp_total_flight_increase(stcb, data_list[i]);
11175                                 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
11176                                         /* SWS sender side engages */
11177                                         asoc->peers_rwnd = 0;
11178                                 }
11179                                 if ((i == 0) &&
11180                                     (data_list[i]->rec.data.doing_fast_retransmit)) {
11181                                         SCTP_STAT_INCR(sctps_sendfastretrans);
11182                                         if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
11183                                             (tmr_started == 0)) {
11184                                                 /*-
11185                                                  * ok we just fast-retrans'd
11186                                                  * the lowest TSN, i.e the
11187                                                  * first on the list. In
11188                                                  * this case we want to give
11189                                                  * some more time to get a
11190                                                  * SACK back without a
11191                                                  * t3-expiring.
11192                                                  */
11193                                                 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
11194                                                     SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
11195                                                 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
11196                                         }
11197                                 }
11198                         }
11199                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11200                                 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
11201                         }
11202 #ifdef SCTP_AUDITING_ENABLED
11203                         sctp_auditing(21, inp, stcb, NULL);
11204 #endif
11205                 } else {
11206                         /* None will fit */
11207                         return (1);
11208                 }
11209                 if (asoc->sent_queue_retran_cnt <= 0) {
11210                         /* all done we have no more to retran */
11211                         asoc->sent_queue_retran_cnt = 0;
11212                         break;
11213                 }
11214                 if (one_chunk) {
11215                         /* No more room in rwnd */
11216                         return (1);
11217                 }
11218                 /* stop the for loop here. we sent out a packet */
11219                 break;
11220         }
11221         return (0);
11222 }
11223
11224
11225 static int
11226 sctp_timer_validation(struct sctp_inpcb *inp,
11227     struct sctp_tcb *stcb,
11228     struct sctp_association *asoc,
11229     int ret)
11230 {
11231         struct sctp_nets *net;
11232
11233         /* Validate that a timer is running somewhere */
11234         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
11235                 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
11236                         /* Here is a timer */
11237                         return (ret);
11238                 }
11239         }
11240         SCTP_TCB_LOCK_ASSERT(stcb);
11241         /* Gak, we did not have a timer somewhere */
11242         SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
11243         sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
11244         return (ret);
11245 }
11246
11247 void
11248 sctp_chunk_output(struct sctp_inpcb *inp,
11249     struct sctp_tcb *stcb,
11250     int from_where,
11251     int so_locked
11252 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
11253     SCTP_UNUSED
11254 #endif
11255 )
11256 {
11257         /*-
11258          * Ok this is the generic chunk service queue. we must do the
11259          * following:
11260          * - See if there are retransmits pending, if so we must
11261          *   do these first.
11262          * - Service the stream queue that is next, moving any
11263          *   message (note I must get a complete message i.e.
11264          *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
11265          *   TSN's
11266          * - Check to see if the cwnd/rwnd allows any output, if so we
11267          *   go ahead and fomulate and send the low level chunks. Making sure
11268          *   to combine any control in the control chunk queue also.
11269          */
11270         struct sctp_association *asoc;
11271         struct sctp_nets *net;
11272         int error = 0, num_out = 0, tot_out = 0, ret = 0, reason_code = 0,
11273             burst_cnt = 0, burst_limit = 0;
11274         struct timeval now;
11275         int now_filled = 0;
11276         int cwnd_full = 0;
11277         int nagle_on = 0;
11278         int frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
11279         int un_sent = 0;
11280         int fr_done, tot_frs = 0;
11281
11282         asoc = &stcb->asoc;
11283         if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
11284                 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
11285                         nagle_on = 0;
11286                 } else {
11287                         nagle_on = 1;
11288                 }
11289         }
11290         SCTP_TCB_LOCK_ASSERT(stcb);
11291
11292         un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
11293
11294         if ((un_sent <= 0) &&
11295             (TAILQ_EMPTY(&asoc->control_send_queue)) &&
11296             (asoc->sent_queue_retran_cnt == 0)) {
11297                 /* Nothing to do unless there is something to be sent left */
11298                 return;
11299         }
11300         /*
11301          * Do we have something to send, data or control AND a sack timer
11302          * running, if so piggy-back the sack.
11303          */
11304         if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
11305                 /*
11306                  * EY if nr_sacks used then send an nr-sack , a sack
11307                  * otherwise
11308                  */
11309                 if (SCTP_BASE_SYSCTL(sctp_nr_sack_on_off) && asoc->peer_supports_nr_sack)
11310                         sctp_send_nr_sack(stcb);
11311                 else
11312                         sctp_send_sack(stcb);
11313                 (void)SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
11314         }
11315         while (asoc->sent_queue_retran_cnt) {
11316                 /*-
11317                  * Ok, it is retransmission time only, we send out only ONE
11318                  * packet with a single call off to the retran code.
11319                  */
11320                 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
11321                         /*-
11322                          * Special hook for handling cookiess discarded
11323                          * by peer that carried data. Send cookie-ack only
11324                          * and then the next call with get the retran's.
11325                          */
11326                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
11327                             &cwnd_full, from_where,
11328                             &now, &now_filled, frag_point, so_locked);
11329                         return;
11330                 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
11331                         /* if its not from a HB then do it */
11332                         fr_done = 0;
11333                         ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
11334                         if (fr_done) {
11335                                 tot_frs++;
11336                         }
11337                 } else {
11338                         /*
11339                          * its from any other place, we don't allow retran
11340                          * output (only control)
11341                          */
11342                         ret = 1;
11343                 }
11344                 if (ret > 0) {
11345                         /* Can't send anymore */
11346                         /*-
11347                          * now lets push out control by calling med-level
11348                          * output once. this assures that we WILL send HB's
11349                          * if queued too.
11350                          */
11351                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
11352                             &cwnd_full, from_where,
11353                             &now, &now_filled, frag_point, so_locked);
11354 #ifdef SCTP_AUDITING_ENABLED
11355                         sctp_auditing(8, inp, stcb, NULL);
11356 #endif
11357                         (void)sctp_timer_validation(inp, stcb, asoc, ret);
11358                         return;
11359                 }
11360                 if (ret < 0) {
11361                         /*-
11362                          * The count was off.. retran is not happening so do
11363                          * the normal retransmission.
11364                          */
11365 #ifdef SCTP_AUDITING_ENABLED
11366                         sctp_auditing(9, inp, stcb, NULL);
11367 #endif
11368                         if (ret == SCTP_RETRAN_EXIT) {
11369                                 return;
11370                         }
11371                         break;
11372                 }
11373                 if (from_where == SCTP_OUTPUT_FROM_T3) {
11374                         /* Only one transmission allowed out of a timeout */
11375 #ifdef SCTP_AUDITING_ENABLED
11376                         sctp_auditing(10, inp, stcb, NULL);
11377 #endif
11378                         /* Push out any control */
11379                         (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, &cwnd_full, from_where,
11380                             &now, &now_filled, frag_point, so_locked);
11381                         return;
11382                 }
11383                 if (tot_frs > asoc->max_burst) {
11384                         /* Hit FR burst limit */
11385                         return;
11386                 }
11387                 if ((num_out == 0) && (ret == 0)) {
11388
11389                         /* No more retrans to send */
11390                         break;
11391                 }
11392         }
11393 #ifdef SCTP_AUDITING_ENABLED
11394         sctp_auditing(12, inp, stcb, NULL);
11395 #endif
11396         /* Check for bad destinations, if they exist move chunks around. */
11397         burst_limit = asoc->max_burst;
11398         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
11399                 if ((net->dest_state & SCTP_ADDR_NOT_REACHABLE) ==
11400                     SCTP_ADDR_NOT_REACHABLE) {
11401                         /*-
11402                          * if possible move things off of this address we
11403                          * still may send below due to the dormant state but
11404                          * we try to find an alternate address to send to
11405                          * and if we have one we move all queued data on the
11406                          * out wheel to this alternate address.
11407                          */
11408                         if (net->ref_count > 1)
11409                                 sctp_move_to_an_alt(stcb, asoc, net);
11410                 } else if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) &&
11411                             SCTP_BASE_SYSCTL(sctp_cmt_pf) &&
11412                     ((net->dest_state & SCTP_ADDR_PF) == SCTP_ADDR_PF)) {
11413                         /*
11414                          * JRS 5/14/07 - If CMT PF is on and the current
11415                          * destination is in PF state, move all queued data
11416                          * to an alternate desination.
11417                          */
11418                         if (net->ref_count > 1)
11419                                 sctp_move_to_an_alt(stcb, asoc, net);
11420                 } else {
11421                         /*-
11422                          * if ((asoc->sat_network) || (net->addr_is_local))
11423                          * { burst_limit = asoc->max_burst *
11424                          * SCTP_SAT_NETWORK_BURST_INCR; }
11425                          */
11426                         if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
11427                                 if ((net->flight_size + (burst_limit * net->mtu)) < net->cwnd) {
11428                                         /*
11429                                          * JRS - Use the congestion control
11430                                          * given in the congestion control
11431                                          * module
11432                                          */
11433                                         asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, burst_limit);
11434                                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11435                                                 sctp_log_maxburst(stcb, net, 0, burst_limit, SCTP_MAX_BURST_APPLIED);
11436                                         }
11437                                         SCTP_STAT_INCR(sctps_maxburstqueued);
11438                                 }
11439                                 net->fast_retran_ip = 0;
11440                         } else {
11441                                 if (net->flight_size == 0) {
11442                                         /* Should be decaying the cwnd here */
11443                                         ;
11444                                 }
11445                         }
11446                 }
11447
11448         }
11449         burst_cnt = 0;
11450         cwnd_full = 0;
11451         do {
11452                 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
11453                     &reason_code, 0, &cwnd_full, from_where,
11454                     &now, &now_filled, frag_point, so_locked);
11455                 if (error) {
11456                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
11457                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11458                                 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
11459                         }
11460                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11461                                 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
11462                                 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
11463                         }
11464                         break;
11465                 }
11466                 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
11467
11468                 tot_out += num_out;
11469                 burst_cnt++;
11470                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11471                         sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
11472                         if (num_out == 0) {
11473                                 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
11474                         }
11475                 }
11476                 if (nagle_on) {
11477                         /*-
11478                          * When nagle is on, we look at how much is un_sent, then
11479                          * if its smaller than an MTU and we have data in
11480                          * flight we stop.
11481                          */
11482                         un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
11483                             (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
11484                         if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
11485                             (stcb->asoc.total_flight > 0)) {
11486                                 break;
11487                         }
11488                 }
11489                 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
11490                     TAILQ_EMPTY(&asoc->send_queue) &&
11491                     TAILQ_EMPTY(&asoc->out_wheel)) {
11492                         /* Nothing left to send */
11493                         break;
11494                 }
11495                 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
11496                         /* Nothing left to send */
11497                         break;
11498                 }
11499         } while (num_out && (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
11500             (burst_cnt < burst_limit)));
11501
11502         if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
11503                 if (burst_cnt >= burst_limit) {
11504                         SCTP_STAT_INCR(sctps_maxburstqueued);
11505                         asoc->burst_limit_applied = 1;
11506                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
11507                                 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
11508                         }
11509                 } else {
11510                         asoc->burst_limit_applied = 0;
11511                 }
11512         }
11513         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
11514                 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
11515         }
11516         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
11517             tot_out);
11518
11519         /*-
11520          * Now we need to clean up the control chunk chain if a ECNE is on
11521          * it. It must be marked as UNSENT again so next call will continue
11522          * to send it until such time that we get a CWR, to remove it.
11523          */
11524         if (stcb->asoc.ecn_echo_cnt_onq)
11525                 sctp_fix_ecn_echo(asoc);
11526         return;
11527 }
11528
11529
11530 int
11531 sctp_output(inp, m, addr, control, p, flags)
11532         struct sctp_inpcb *inp;
11533         struct mbuf *m;
11534         struct sockaddr *addr;
11535         struct mbuf *control;
11536         struct thread *p;
11537         int flags;
11538 {
11539         if (inp == NULL) {
11540                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11541                 return (EINVAL);
11542         }
11543         if (inp->sctp_socket == NULL) {
11544                 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
11545                 return (EINVAL);
11546         }
11547         return (sctp_sosend(inp->sctp_socket,
11548             addr,
11549             (struct uio *)NULL,
11550             m,
11551             control,
11552             flags, p
11553             ));
11554 }
11555
11556 void
11557 send_forward_tsn(struct sctp_tcb *stcb,
11558     struct sctp_association *asoc)
11559 {
11560         struct sctp_tmit_chunk *chk;
11561         struct sctp_forward_tsn_chunk *fwdtsn;
11562
11563         SCTP_TCB_LOCK_ASSERT(stcb);
11564         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11565                 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
11566                         /* mark it to unsent */
11567                         chk->sent = SCTP_DATAGRAM_UNSENT;
11568                         chk->snd_count = 0;
11569                         /* Do we correct its output location? */
11570                         if (chk->whoTo != asoc->primary_destination) {
11571                                 sctp_free_remote_addr(chk->whoTo);
11572                                 chk->whoTo = asoc->primary_destination;
11573                                 atomic_add_int(&chk->whoTo->ref_count, 1);
11574                         }
11575                         goto sctp_fill_in_rest;
11576                 }
11577         }
11578         /* Ok if we reach here we must build one */
11579         sctp_alloc_a_chunk(stcb, chk);
11580         if (chk == NULL) {
11581                 return;
11582         }
11583         chk->copy_by_ref = 0;
11584         chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
11585         chk->rec.chunk_id.can_take_data = 0;
11586         chk->asoc = asoc;
11587         chk->whoTo = NULL;
11588
11589         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
11590         if (chk->data == NULL) {
11591                 sctp_free_a_chunk(stcb, chk);
11592                 return;
11593         }
11594         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11595         chk->sent = SCTP_DATAGRAM_UNSENT;
11596         chk->snd_count = 0;
11597         chk->whoTo = asoc->primary_destination;
11598         atomic_add_int(&chk->whoTo->ref_count, 1);
11599         TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
11600         asoc->ctrl_queue_cnt++;
11601 sctp_fill_in_rest:
11602         /*-
11603          * Here we go through and fill out the part that deals with
11604          * stream/seq of the ones we skip.
11605          */
11606         SCTP_BUF_LEN(chk->data) = 0;
11607         {
11608                 struct sctp_tmit_chunk *at, *tp1, *last;
11609                 struct sctp_strseq *strseq;
11610                 unsigned int cnt_of_space, i, ovh;
11611                 unsigned int space_needed;
11612                 unsigned int cnt_of_skipped = 0;
11613
11614                 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
11615                         if (at->sent != SCTP_FORWARD_TSN_SKIP) {
11616                                 /* no more to look at */
11617                                 break;
11618                         }
11619                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
11620                                 /* We don't report these */
11621                                 continue;
11622                         }
11623                         cnt_of_skipped++;
11624                 }
11625                 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
11626                     (cnt_of_skipped * sizeof(struct sctp_strseq)));
11627
11628                 cnt_of_space = M_TRAILINGSPACE(chk->data);
11629
11630                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
11631                         ovh = SCTP_MIN_OVERHEAD;
11632                 } else {
11633                         ovh = SCTP_MIN_V4_OVERHEAD;
11634                 }
11635                 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
11636                         /* trim to a mtu size */
11637                         cnt_of_space = asoc->smallest_mtu - ovh;
11638                 }
11639                 if (cnt_of_space < space_needed) {
11640                         /*-
11641                          * ok we must trim down the chunk by lowering the
11642                          * advance peer ack point.
11643                          */
11644                         cnt_of_skipped = (cnt_of_space -
11645                             ((sizeof(struct sctp_forward_tsn_chunk)) /
11646                             sizeof(struct sctp_strseq)));
11647                         /*-
11648                          * Go through and find the TSN that will be the one
11649                          * we report.
11650                          */
11651                         at = TAILQ_FIRST(&asoc->sent_queue);
11652                         for (i = 0; i < cnt_of_skipped; i++) {
11653                                 tp1 = TAILQ_NEXT(at, sctp_next);
11654                                 at = tp1;
11655                         }
11656                         last = at;
11657                         /*-
11658                          * last now points to last one I can report, update
11659                          * peer ack point
11660                          */
11661                         asoc->advanced_peer_ack_point = last->rec.data.TSN_seq;
11662                         space_needed -= (cnt_of_skipped * sizeof(struct sctp_strseq));
11663                 }
11664                 chk->send_size = space_needed;
11665                 /* Setup the chunk */
11666                 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
11667                 fwdtsn->ch.chunk_length = htons(chk->send_size);
11668                 fwdtsn->ch.chunk_flags = 0;
11669                 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
11670                 fwdtsn->new_cumulative_tsn = htonl(asoc->advanced_peer_ack_point);
11671                 chk->send_size = (sizeof(struct sctp_forward_tsn_chunk) +
11672                     (cnt_of_skipped * sizeof(struct sctp_strseq)));
11673                 SCTP_BUF_LEN(chk->data) = chk->send_size;
11674                 fwdtsn++;
11675                 /*-
11676                  * Move pointer to after the fwdtsn and transfer to the
11677                  * strseq pointer.
11678                  */
11679                 strseq = (struct sctp_strseq *)fwdtsn;
11680                 /*-
11681                  * Now populate the strseq list. This is done blindly
11682                  * without pulling out duplicate stream info. This is
11683                  * inefficent but won't harm the process since the peer will
11684                  * look at these in sequence and will thus release anything.
11685                  * It could mean we exceed the PMTU and chop off some that
11686                  * we could have included.. but this is unlikely (aka 1432/4
11687                  * would mean 300+ stream seq's would have to be reported in
11688                  * one FWD-TSN. With a bit of work we can later FIX this to
11689                  * optimize and pull out duplcates.. but it does add more
11690                  * overhead. So for now... not!
11691                  */
11692                 at = TAILQ_FIRST(&asoc->sent_queue);
11693                 for (i = 0; i < cnt_of_skipped; i++) {
11694                         tp1 = TAILQ_NEXT(at, sctp_next);
11695                         if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
11696                                 /* We don't report these */
11697                                 i--;
11698                                 at = tp1;
11699                                 continue;
11700                         }
11701                         strseq->stream = ntohs(at->rec.data.stream_number);
11702                         strseq->sequence = ntohs(at->rec.data.stream_seq);
11703                         strseq++;
11704                         at = tp1;
11705                 }
11706         }
11707         return;
11708
11709 }
11710
11711 void
11712 sctp_send_sack(struct sctp_tcb *stcb)
11713 {
11714         /*-
11715          * Queue up a SACK in the control queue. We must first check to see
11716          * if a SACK is somehow on the control queue. If so, we will take
11717          * and and remove the old one.
11718          */
11719         struct sctp_association *asoc;
11720         struct sctp_tmit_chunk *chk, *a_chk;
11721         struct sctp_sack_chunk *sack;
11722         struct sctp_gap_ack_block *gap_descriptor;
11723         struct sack_track *selector;
11724         int mergeable = 0;
11725         int offset;
11726         caddr_t limit;
11727         uint32_t *dup;
11728         int limit_reached = 0;
11729         unsigned int i, jstart, siz, j;
11730         unsigned int num_gap_blocks = 0, space;
11731         int num_dups = 0;
11732         int space_req;
11733
11734         a_chk = NULL;
11735         asoc = &stcb->asoc;
11736         SCTP_TCB_LOCK_ASSERT(stcb);
11737         if (asoc->last_data_chunk_from == NULL) {
11738                 /* Hmm we never received anything */
11739                 return;
11740         }
11741         sctp_set_rwnd(stcb, asoc);
11742         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11743                 if (chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) {
11744                         /* Hmm, found a sack already on queue, remove it */
11745                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
11746                         asoc->ctrl_queue_cnt++;
11747                         a_chk = chk;
11748                         if (a_chk->data) {
11749                                 sctp_m_freem(a_chk->data);
11750                                 a_chk->data = NULL;
11751                         }
11752                         sctp_free_remote_addr(a_chk->whoTo);
11753                         a_chk->whoTo = NULL;
11754                         break;
11755                 }
11756         }
11757         if (a_chk == NULL) {
11758                 sctp_alloc_a_chunk(stcb, a_chk);
11759                 if (a_chk == NULL) {
11760                         /* No memory so we drop the idea, and set a timer */
11761                         if (stcb->asoc.delayed_ack) {
11762                                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
11763                                     stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
11764                                 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
11765                                     stcb->sctp_ep, stcb, NULL);
11766                         } else {
11767                                 stcb->asoc.send_sack = 1;
11768                         }
11769                         return;
11770                 }
11771                 a_chk->copy_by_ref = 0;
11772                 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
11773                 a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK;
11774                 a_chk->rec.chunk_id.can_take_data = 1;
11775         }
11776         /* Clear our pkt counts */
11777         asoc->data_pkts_seen = 0;
11778
11779         a_chk->asoc = asoc;
11780         a_chk->snd_count = 0;
11781         a_chk->send_size = 0;   /* fill in later */
11782         a_chk->sent = SCTP_DATAGRAM_UNSENT;
11783         a_chk->whoTo = NULL;
11784
11785         if ((asoc->numduptsns) ||
11786             (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
11787             ) {
11788                 /*-
11789                  * Ok, we have some duplicates or the destination for the
11790                  * sack is unreachable, lets see if we can select an
11791                  * alternate than asoc->last_data_chunk_from
11792                  */
11793                 if ((!(asoc->last_data_chunk_from->dest_state &
11794                     SCTP_ADDR_NOT_REACHABLE)) &&
11795                     (asoc->used_alt_onsack > asoc->numnets)) {
11796                         /* We used an alt last time, don't this time */
11797                         a_chk->whoTo = NULL;
11798                 } else {
11799                         asoc->used_alt_onsack++;
11800                         a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
11801                 }
11802                 if (a_chk->whoTo == NULL) {
11803                         /* Nope, no alternate */
11804                         a_chk->whoTo = asoc->last_data_chunk_from;
11805                         asoc->used_alt_onsack = 0;
11806                 }
11807         } else {
11808                 /*
11809                  * No duplicates so we use the last place we received data
11810                  * from.
11811                  */
11812                 asoc->used_alt_onsack = 0;
11813                 a_chk->whoTo = asoc->last_data_chunk_from;
11814         }
11815         if (a_chk->whoTo) {
11816                 atomic_add_int(&a_chk->whoTo->ref_count, 1);
11817         }
11818         if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
11819                 /* no gaps */
11820                 space_req = sizeof(struct sctp_sack_chunk);
11821         } else {
11822                 /* gaps get a cluster */
11823                 space_req = MCLBYTES;
11824         }
11825         /* Ok now lets formulate a MBUF with our sack */
11826         a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
11827         if ((a_chk->data == NULL) ||
11828             (a_chk->whoTo == NULL)) {
11829                 /* rats, no mbuf memory */
11830                 if (a_chk->data) {
11831                         /* was a problem with the destination */
11832                         sctp_m_freem(a_chk->data);
11833                         a_chk->data = NULL;
11834                 }
11835                 sctp_free_a_chunk(stcb, a_chk);
11836                 /* sa_ignore NO_NULL_CHK */
11837                 if (stcb->asoc.delayed_ack) {
11838                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
11839                             stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
11840                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
11841                             stcb->sctp_ep, stcb, NULL);
11842                 } else {
11843                         stcb->asoc.send_sack = 1;
11844                 }
11845                 return;
11846         }
11847         /* ok, lets go through and fill it in */
11848         SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
11849         space = M_TRAILINGSPACE(a_chk->data);
11850         if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
11851                 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
11852         }
11853         limit = mtod(a_chk->data, caddr_t);
11854         limit += space;
11855
11856         sack = mtod(a_chk->data, struct sctp_sack_chunk *);
11857         sack->ch.chunk_type = SCTP_SELECTIVE_ACK;
11858         /* 0x01 is used by nonce for ecn */
11859         if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
11860             (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
11861             (asoc->peer_supports_ecn_nonce))
11862                 sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
11863         else
11864                 sack->ch.chunk_flags = 0;
11865
11866         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
11867                 /*-
11868                  * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
11869                  * received, then set high bit to 1, else 0. Reset
11870                  * pkts_rcvd.
11871                  */
11872                 sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
11873                 asoc->cmt_dac_pkts_rcvd = 0;
11874         }
11875 #ifdef SCTP_ASOCLOG_OF_TSNS
11876         stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
11877         stcb->asoc.cumack_log_atsnt++;
11878         if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
11879                 stcb->asoc.cumack_log_atsnt = 0;
11880         }
11881 #endif
11882         sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
11883         sack->sack.a_rwnd = htonl(asoc->my_rwnd);
11884         asoc->my_last_reported_rwnd = asoc->my_rwnd;
11885
11886         /* reset the readers interpretation */
11887         stcb->freed_by_sorcv_sincelast = 0;
11888
11889         gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
11890
11891         siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
11892         if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
11893                 offset = 1;
11894                 /*-
11895                  * cum-ack behind the mapping array, so we start and use all
11896                  * entries.
11897                  */
11898                 jstart = 0;
11899         } else {
11900                 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
11901                 /*-
11902                  * we skip the first one when the cum-ack is at or above the
11903                  * mapping array base. Note this only works if
11904                  */
11905                 jstart = 1;
11906         }
11907         if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
11908                 /* we have a gap .. maybe */
11909                 for (i = 0; i < siz; i++) {
11910                         selector = &sack_array[asoc->mapping_array[i]];
11911                         if (mergeable && selector->right_edge) {
11912                                 /*
11913                                  * Backup, left and right edges were ok to
11914                                  * merge.
11915                                  */
11916                                 num_gap_blocks--;
11917                                 gap_descriptor--;
11918                         }
11919                         if (selector->num_entries == 0)
11920                                 mergeable = 0;
11921                         else {
11922                                 for (j = jstart; j < selector->num_entries; j++) {
11923                                         if (mergeable && selector->right_edge) {
11924                                                 /*
11925                                                  * do a merge by NOT setting
11926                                                  * the left side
11927                                                  */
11928                                                 mergeable = 0;
11929                                         } else {
11930                                                 /*
11931                                                  * no merge, set the left
11932                                                  * side
11933                                                  */
11934                                                 mergeable = 0;
11935                                                 gap_descriptor->start = htons((selector->gaps[j].start + offset));
11936                                         }
11937                                         gap_descriptor->end = htons((selector->gaps[j].end + offset));
11938                                         num_gap_blocks++;
11939                                         gap_descriptor++;
11940                                         if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
11941                                                 /* no more room */
11942                                                 limit_reached = 1;
11943                                                 break;
11944                                         }
11945                                 }
11946                                 if (selector->left_edge) {
11947                                         mergeable = 1;
11948                                 }
11949                         }
11950                         if (limit_reached) {
11951                                 /* Reached the limit stop */
11952                                 break;
11953                         }
11954                         jstart = 0;
11955                         offset += 8;
11956                 }
11957                 if (num_gap_blocks == 0) {
11958                         /*
11959                          * slide not yet happened, and somehow we got called
11960                          * to send a sack. Cumack needs to move up.
11961                          */
11962                         int abort_flag = 0;
11963
11964                         asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
11965                         sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
11966                         sctp_sack_check(stcb, 0, 0, &abort_flag);
11967                 }
11968         }
11969         /* now we must add any dups we are going to report. */
11970         if ((limit_reached == 0) && (asoc->numduptsns)) {
11971                 dup = (uint32_t *) gap_descriptor;
11972                 for (i = 0; i < asoc->numduptsns; i++) {
11973                         *dup = htonl(asoc->dup_tsns[i]);
11974                         dup++;
11975                         num_dups++;
11976                         if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
11977                                 /* no more room */
11978                                 break;
11979                         }
11980                 }
11981                 asoc->numduptsns = 0;
11982         }
11983         /*
11984          * now that the chunk is prepared queue it to the control chunk
11985          * queue.
11986          */
11987         a_chk->send_size = (sizeof(struct sctp_sack_chunk) +
11988             (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
11989             (num_dups * sizeof(int32_t)));
11990         SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
11991         sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
11992         sack->sack.num_dup_tsns = htons(num_dups);
11993         sack->ch.chunk_length = htons(a_chk->send_size);
11994         TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
11995         asoc->ctrl_queue_cnt++;
11996         asoc->send_sack = 0;
11997         SCTP_STAT_INCR(sctps_sendsacks);
11998         return;
11999 }
12000
12001 /* EY - This method will replace sctp_send_sack method if nr_sacks negotiated*/
12002 void
12003 sctp_send_nr_sack(struct sctp_tcb *stcb)
12004 {
12005         /*-
12006          * Queue up an NR-SACK in the control queue. We must first check to see
12007          * if an NR-SACK is somehow on the control queue. If so, we will take
12008          * and and remove the old one.
12009          */
12010         struct sctp_association *asoc;
12011         struct sctp_tmit_chunk *chk, *a_chk;
12012
12013         struct sctp_nr_sack_chunk *nr_sack;
12014
12015         struct sctp_gap_ack_block *gap_descriptor;
12016         struct sctp_nr_gap_ack_block *nr_gap_descriptor;
12017
12018         struct sack_track *selector;
12019         struct nr_sack_track *nr_selector;
12020
12021         /* EY do we need nr_mergeable, NO */
12022         int mergeable = 0;
12023         int offset;
12024         caddr_t limit;
12025         uint32_t *dup;
12026         int limit_reached = 0;
12027         unsigned int i, jstart, siz, j;
12028         unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
12029         int num_dups = 0;
12030         int space_req;
12031         unsigned int reserved = 0;
12032
12033         a_chk = NULL;
12034         asoc = &stcb->asoc;
12035         SCTP_TCB_LOCK_ASSERT(stcb);
12036         if (asoc->last_data_chunk_from == NULL) {
12037                 /* Hmm we never received anything */
12038                 return;
12039         }
12040         sctp_set_rwnd(stcb, asoc);
12041         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
12042                 if (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) {
12043                         /* Hmm, found a sack already on queue, remove it */
12044                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
12045                         asoc->ctrl_queue_cnt++;
12046                         a_chk = chk;
12047                         if (a_chk->data) {
12048                                 sctp_m_freem(a_chk->data);
12049                                 a_chk->data = NULL;
12050                         }
12051                         sctp_free_remote_addr(a_chk->whoTo);
12052                         a_chk->whoTo = NULL;
12053                         break;
12054                 }
12055         }
12056         if (a_chk == NULL) {
12057                 sctp_alloc_a_chunk(stcb, a_chk);
12058                 if (a_chk == NULL) {
12059                         /* No memory so we drop the idea, and set a timer */
12060                         if (stcb->asoc.delayed_ack) {
12061                                 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
12062                                     stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
12063                                 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
12064                                     stcb->sctp_ep, stcb, NULL);
12065                         } else {
12066                                 stcb->asoc.send_sack = 1;
12067                         }
12068                         return;
12069                 }
12070                 a_chk->copy_by_ref = 0;
12071                 /* a_chk->rec.chunk_id.id = SCTP_SELECTIVE_ACK; */
12072                 a_chk->rec.chunk_id.id = SCTP_NR_SELECTIVE_ACK;
12073                 a_chk->rec.chunk_id.can_take_data = 1;
12074         }
12075         /* Clear our pkt counts */
12076         asoc->data_pkts_seen = 0;
12077
12078         a_chk->asoc = asoc;
12079         a_chk->snd_count = 0;
12080         a_chk->send_size = 0;   /* fill in later */
12081         a_chk->sent = SCTP_DATAGRAM_UNSENT;
12082         a_chk->whoTo = NULL;
12083
12084         if ((asoc->numduptsns) ||
12085             (asoc->last_data_chunk_from->dest_state & SCTP_ADDR_NOT_REACHABLE)
12086             ) {
12087                 /*-
12088                  * Ok, we have some duplicates or the destination for the
12089                  * sack is unreachable, lets see if we can select an
12090                  * alternate than asoc->last_data_chunk_from
12091                  */
12092                 if ((!(asoc->last_data_chunk_from->dest_state &
12093                     SCTP_ADDR_NOT_REACHABLE)) &&
12094                     (asoc->used_alt_onsack > asoc->numnets)) {
12095                         /* We used an alt last time, don't this time */
12096                         a_chk->whoTo = NULL;
12097                 } else {
12098                         asoc->used_alt_onsack++;
12099                         a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
12100                 }
12101                 if (a_chk->whoTo == NULL) {
12102                         /* Nope, no alternate */
12103                         a_chk->whoTo = asoc->last_data_chunk_from;
12104                         asoc->used_alt_onsack = 0;
12105                 }
12106         } else {
12107                 /*
12108                  * No duplicates so we use the last place we received data
12109                  * from.
12110                  */
12111                 asoc->used_alt_onsack = 0;
12112                 a_chk->whoTo = asoc->last_data_chunk_from;
12113         }
12114         if (a_chk->whoTo) {
12115                 atomic_add_int(&a_chk->whoTo->ref_count, 1);
12116         }
12117         if (asoc->highest_tsn_inside_map == asoc->cumulative_tsn) {
12118                 /* no gaps */
12119                 space_req = sizeof(struct sctp_nr_sack_chunk);
12120         } else {
12121                 /* EY - what is this about? */
12122                 /* gaps get a cluster */
12123                 space_req = MCLBYTES;
12124         }
12125         /* Ok now lets formulate a MBUF with our sack */
12126         a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_DONTWAIT, 1, MT_DATA);
12127         if ((a_chk->data == NULL) ||
12128             (a_chk->whoTo == NULL)) {
12129                 /* rats, no mbuf memory */
12130                 if (a_chk->data) {
12131                         /* was a problem with the destination */
12132                         sctp_m_freem(a_chk->data);
12133                         a_chk->data = NULL;
12134                 }
12135                 sctp_free_a_chunk(stcb, a_chk);
12136                 /* sa_ignore NO_NULL_CHK */
12137                 if (stcb->asoc.delayed_ack) {
12138                         sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
12139                             stcb->sctp_ep, stcb, NULL, SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
12140                         sctp_timer_start(SCTP_TIMER_TYPE_RECV,
12141                             stcb->sctp_ep, stcb, NULL);
12142                 } else {
12143                         stcb->asoc.send_sack = 1;
12144                 }
12145                 return;
12146         }
12147         /* ok, lets go through and fill it in */
12148         SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
12149         space = M_TRAILINGSPACE(a_chk->data);
12150         if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
12151                 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
12152         }
12153         limit = mtod(a_chk->data, caddr_t);
12154         limit += space;
12155
12156         nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
12157         nr_sack->ch.chunk_type = SCTP_NR_SELECTIVE_ACK;
12158         /* EYJ */
12159         /* 0x01 is used by nonce for ecn */
12160         if ((SCTP_BASE_SYSCTL(sctp_ecn_enable)) &&
12161             (SCTP_BASE_SYSCTL(sctp_ecn_nonce)) &&
12162             (asoc->peer_supports_ecn_nonce))
12163                 nr_sack->ch.chunk_flags = (asoc->receiver_nonce_sum & SCTP_SACK_NONCE_SUM);
12164         else
12165                 nr_sack->ch.chunk_flags = 0;
12166
12167         if (SCTP_BASE_SYSCTL(sctp_cmt_on_off) && SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
12168                 /*-
12169                  * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
12170                  * received, then set high bit to 1, else 0. Reset
12171                  * pkts_rcvd.
12172                  */
12173                 /* EY - TODO: which chunk flag is used in here? -The LSB */
12174                 nr_sack->ch.chunk_flags |= (asoc->cmt_dac_pkts_rcvd << 6);
12175                 asoc->cmt_dac_pkts_rcvd = 0;
12176         }
12177         /*
12178          * EY - this is a never reneging receiver, that makes all gaps are
12179          * nr-gaps, set the All bit
12180          */
12181         if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
12182                 nr_sack->ch.chunk_flags |= SCTP_NR_SACK_ALL_BIT;
12183         }
12184 #ifdef SCTP_ASOCLOG_OF_TSNS
12185         stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
12186         stcb->asoc.cumack_log_atsnt++;
12187         if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
12188                 stcb->asoc.cumack_log_atsnt = 0;
12189         }
12190 #endif
12191         nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
12192         nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
12193         asoc->my_last_reported_rwnd = asoc->my_rwnd;
12194
12195         /* reset the readers interpretation */
12196         stcb->freed_by_sorcv_sincelast = 0;
12197
12198         gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
12199         nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
12200
12201         siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
12202         if (compare_with_wrap(asoc->mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
12203                 offset = 1;
12204                 /*-
12205                  * cum-ack behind the mapping array, so we start and use all
12206                  * entries.
12207                  */
12208                 jstart = 0;
12209         } else {
12210                 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
12211                 /*-
12212                  * we skip the first one when the cum-ack is at or above the
12213                  * mapping array base. Note this only works if
12214                  */
12215                 jstart = 1;
12216         }
12217         if (compare_with_wrap(asoc->highest_tsn_inside_map, asoc->cumulative_tsn, MAX_TSN)) {
12218                 /* we have a gap .. maybe */
12219                 for (i = 0; i < siz; i++) {
12220                         selector = &sack_array[asoc->mapping_array[i]];
12221                         if (mergeable && selector->right_edge) {
12222                                 /*
12223                                  * Backup, left and right edges were ok to
12224                                  * merge.
12225                                  */
12226                                 num_gap_blocks--;
12227                                 gap_descriptor--;
12228                         }
12229                         if (selector->num_entries == 0)
12230                                 mergeable = 0;
12231                         else {
12232                                 for (j = jstart; j < selector->num_entries; j++) {
12233                                         if (mergeable && selector->right_edge) {
12234                                                 /*
12235                                                  * do a merge by NOT setting
12236                                                  * the left side
12237                                                  */
12238                                                 mergeable = 0;
12239                                         } else {
12240                                                 /*
12241                                                  * no merge, set the left
12242                                                  * side
12243                                                  */
12244                                                 mergeable = 0;
12245                                                 gap_descriptor->start = htons((selector->gaps[j].start + offset));
12246                                         }
12247                                         gap_descriptor->end = htons((selector->gaps[j].end + offset));
12248                                         num_gap_blocks++;
12249                                         gap_descriptor++;
12250                                         if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
12251                                                 /* no more room */
12252                                                 limit_reached = 1;
12253                                                 break;
12254                                         }
12255                                 }
12256                                 if (selector->left_edge) {
12257                                         mergeable = 1;
12258                                 }
12259                         }
12260                         if (limit_reached) {
12261                                 /* Reached the limit stop */
12262                                 break;
12263                         }
12264                         jstart = 0;
12265                         offset += 8;
12266                 }
12267                 if (num_gap_blocks == 0) {
12268                         /*
12269                          * slide not yet happened, and somehow we got called
12270                          * to send a sack. Cumack needs to move up.
12271                          */
12272                         int abort_flag = 0;
12273
12274                         asoc->cumulative_tsn = asoc->highest_tsn_inside_map;
12275                         nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
12276                         sctp_sack_check(stcb, 0, 0, &abort_flag);
12277                 }
12278         }
12279         /*---------------------------------------------------------filling the nr_gap_ack blocks----------------------------------------------------*/
12280
12281         nr_gap_descriptor = (struct sctp_nr_gap_ack_block *)gap_descriptor;
12282
12283         /* EY - there will be gaps + nr_gaps if draining is possible */
12284         if (SCTP_BASE_SYSCTL(sctp_do_drain)) {
12285
12286                 mergeable = 0;
12287
12288                 siz = (((asoc->highest_tsn_inside_nr_map - asoc->nr_mapping_array_base_tsn) + 1) + 7) / 8;
12289                 if (compare_with_wrap(asoc->nr_mapping_array_base_tsn, asoc->cumulative_tsn, MAX_TSN)) {
12290                         offset = 1;
12291                         /*-
12292                         * cum-ack behind the mapping array, so we start and use all
12293                         * entries.
12294                         */
12295                         jstart = 0;
12296                 } else {
12297                         offset = asoc->nr_mapping_array_base_tsn - asoc->cumulative_tsn;
12298                         /*-
12299                         * we skip the first one when the cum-ack is at or above the
12300                         * mapping array base. Note this only works if
12301                         */
12302                         jstart = 1;
12303                 }
12304                 if (compare_with_wrap(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn, MAX_TSN)) {
12305                         /* we have a gap .. maybe */
12306                         for (i = 0; i < siz; i++) {
12307                                 nr_selector = &nr_sack_array[asoc->nr_mapping_array[i]];
12308                                 if (mergeable && nr_selector->right_edge) {
12309                                         /*
12310                                          * Backup, left and right edges were
12311                                          * ok to merge.
12312                                          */
12313                                         num_nr_gap_blocks--;
12314                                         nr_gap_descriptor--;
12315                                 }
12316                                 if (nr_selector->num_entries == 0)
12317                                         mergeable = 0;
12318                                 else {
12319                                         for (j = jstart; j < nr_selector->num_entries; j++) {
12320                                                 if (mergeable && nr_selector->right_edge) {
12321                                                         /*
12322                                                          * do a merge by NOT
12323                                                          * setting the left
12324                                                          * side
12325                                                          */
12326                                                         mergeable = 0;
12327                                                 } else {
12328                                                         /*
12329                                                          * no merge, set the
12330                                                          * left side
12331                                                          */
12332                                                         mergeable = 0;
12333                                                         nr_gap_descriptor->start = htons((nr_selector->nr_gaps[j].start + offset));
12334                                                 }
12335                                                 nr_gap_descriptor->end = htons((nr_selector->nr_gaps[j].end + offset));
12336                                                 num_nr_gap_blocks++;
12337                                                 nr_gap_descriptor++;
12338                                                 if (((caddr_t)nr_gap_descriptor + sizeof(struct sctp_nr_gap_ack_block)) > limit) {
12339                                                         /* no more room */
12340                                                         limit_reached = 1;
12341                                                         break;
12342                                                 }
12343                                         }
12344                                         if (nr_selector->left_edge) {
12345                                                 mergeable = 1;
12346                                         }
12347                                 }
12348                                 if (limit_reached) {
12349                                         /* Reached the limit stop */
12350                                         break;
12351                                 }
12352                                 jstart = 0;
12353                                 offset += 8;
12354                         }
12355                 }
12356         }
12357         /*---------------------------------------------------End of---filling the nr_gap_ack blocks----------------------------------------------------*/
12358
12359         /* now we must add any dups we are going to report. */
12360         if ((limit_reached == 0) && (asoc->numduptsns)) {
12361                 dup = (uint32_t *) nr_gap_descriptor;
12362                 for (i = 0; i < asoc->numduptsns; i++) {
12363                         *dup = htonl(asoc->dup_tsns[i]);
12364                         dup++;
12365                         num_dups++;
12366                         if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
12367                                 /* no more room */
12368                                 break;
12369                         }
12370                 }
12371                 asoc->numduptsns = 0;
12372         }
12373         /*
12374          * now that the chunk is prepared queue it to the control chunk
12375          * queue.
12376          */
12377         if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
12378                 num_nr_gap_blocks = num_gap_blocks;
12379                 num_gap_blocks = 0;
12380         }
12381         a_chk->send_size = (sizeof(struct sctp_nr_sack_chunk) +
12382             (num_gap_blocks * sizeof(struct sctp_gap_ack_block)) +
12383             (num_nr_gap_blocks * sizeof(struct sctp_nr_gap_ack_block)) +
12384             (num_dups * sizeof(int32_t)));
12385
12386         SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
12387         nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
12388         nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
12389         nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
12390         nr_sack->nr_sack.reserved = htons(reserved);
12391         nr_sack->ch.chunk_length = htons(a_chk->send_size);
12392         TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
12393         asoc->ctrl_queue_cnt++;
12394         asoc->send_sack = 0;
12395         SCTP_STAT_INCR(sctps_sendsacks);
12396         return;
12397 }
12398
12399 void
12400 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked
12401 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
12402     SCTP_UNUSED
12403 #endif
12404 )
12405 {
12406         struct mbuf *m_abort;
12407         struct mbuf *m_out = NULL, *m_end = NULL;
12408         struct sctp_abort_chunk *abort = NULL;
12409         int sz;
12410         uint32_t auth_offset = 0;
12411         struct sctp_auth_chunk *auth = NULL;
12412         struct sctphdr *shdr;
12413
12414         /*-
12415          * Add an AUTH chunk, if chunk requires it and save the offset into
12416          * the chain for AUTH
12417          */
12418         if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
12419             stcb->asoc.peer_auth_chunks)) {
12420                 m_out = sctp_add_auth_chunk(m_out, &m_end, &auth, &auth_offset,
12421                     stcb, SCTP_ABORT_ASSOCIATION);
12422                 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12423         }
12424         SCTP_TCB_LOCK_ASSERT(stcb);
12425         m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_DONTWAIT, 1, MT_HEADER);
12426         if (m_abort == NULL) {
12427                 /* no mbuf's */
12428                 if (m_out)
12429                         sctp_m_freem(m_out);
12430                 return;
12431         }
12432         /* link in any error */
12433         SCTP_BUF_NEXT(m_abort) = operr;
12434         sz = 0;
12435         if (operr) {
12436                 struct mbuf *n;
12437
12438                 n = operr;
12439                 while (n) {
12440                         sz += SCTP_BUF_LEN(n);
12441                         n = SCTP_BUF_NEXT(n);
12442                 }
12443         }
12444         SCTP_BUF_LEN(m_abort) = sizeof(*abort);
12445         if (m_out == NULL) {
12446                 /* NO Auth chunk prepended, so reserve space in front */
12447                 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
12448                 m_out = m_abort;
12449         } else {
12450                 /* Put AUTH chunk at the front of the chain */
12451                 SCTP_BUF_NEXT(m_end) = m_abort;
12452         }
12453
12454         /* fill in the ABORT chunk */
12455         abort = mtod(m_abort, struct sctp_abort_chunk *);
12456         abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
12457         abort->ch.chunk_flags = 0;
12458         abort->ch.chunk_length = htons(sizeof(*abort) + sz);
12459
12460         /* prepend and fill in the SCTP header */
12461         SCTP_BUF_PREPEND(m_out, sizeof(struct sctphdr), M_DONTWAIT);
12462         if (m_out == NULL) {
12463                 /* TSNH: no memory */
12464                 return;
12465         }
12466         shdr = mtod(m_out, struct sctphdr *);
12467         shdr->src_port = stcb->sctp_ep->sctp_lport;
12468         shdr->dest_port = stcb->rport;
12469         shdr->v_tag = htonl(stcb->asoc.peer_vtag);
12470         shdr->checksum = 0;
12471         auth_offset += sizeof(struct sctphdr);
12472
12473         (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb,
12474             stcb->asoc.primary_destination,
12475             (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr,
12476             m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, NULL, 0, stcb->asoc.primary_destination->port, so_locked, NULL);
12477         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12478 }
12479
12480 void
12481 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
12482     struct sctp_nets *net)
12483 {
12484         /* formulate and SEND a SHUTDOWN-COMPLETE */
12485         struct mbuf *m_shutdown_comp;
12486         struct sctp_shutdown_complete_msg *comp_cp;
12487
12488         m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_complete_msg), 0, M_DONTWAIT, 1, MT_HEADER);
12489         if (m_shutdown_comp == NULL) {
12490                 /* no mbuf's */
12491                 return;
12492         }
12493         comp_cp = mtod(m_shutdown_comp, struct sctp_shutdown_complete_msg *);
12494         comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
12495         comp_cp->shut_cmp.ch.chunk_flags = 0;
12496         comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
12497         comp_cp->sh.src_port = stcb->sctp_ep->sctp_lport;
12498         comp_cp->sh.dest_port = stcb->rport;
12499         comp_cp->sh.v_tag = htonl(stcb->asoc.peer_vtag);
12500         comp_cp->sh.checksum = 0;
12501
12502         SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_msg);
12503         (void)sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
12504             (struct sockaddr *)&net->ro._l_addr,
12505             m_shutdown_comp, 0, NULL, 0, 1, 0, NULL, 0, net->port, SCTP_SO_NOT_LOCKED, NULL);
12506         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12507         return;
12508 }
12509
12510 void
12511 sctp_send_shutdown_complete2(struct mbuf *m, int iphlen, struct sctphdr *sh,
12512     uint32_t vrf_id, uint16_t port)
12513 {
12514         /* formulate and SEND a SHUTDOWN-COMPLETE */
12515         struct mbuf *o_pak;
12516         struct mbuf *mout;
12517         struct ip *iph, *iph_out;
12518         struct udphdr *udp = NULL;
12519
12520 #ifdef INET6
12521         struct ip6_hdr *ip6, *ip6_out;
12522
12523 #endif
12524         int offset_out, len, mlen;
12525         struct sctp_shutdown_complete_msg *comp_cp;
12526
12527         iph = mtod(m, struct ip *);
12528         switch (iph->ip_v) {
12529         case IPVERSION:
12530                 len = (sizeof(struct ip) + sizeof(struct sctp_shutdown_complete_msg));
12531                 break;
12532 #ifdef INET6
12533         case IPV6_VERSION >> 4:
12534                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_shutdown_complete_msg));
12535                 break;
12536 #endif
12537         default:
12538                 return;
12539         }
12540         if (port) {
12541                 len += sizeof(struct udphdr);
12542         }
12543         mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
12544         if (mout == NULL) {
12545                 return;
12546         }
12547         SCTP_BUF_LEN(mout) = len;
12548         SCTP_BUF_NEXT(mout) = NULL;
12549         iph_out = NULL;
12550 #ifdef INET6
12551         ip6_out = NULL;
12552 #endif
12553         offset_out = 0;
12554
12555         switch (iph->ip_v) {
12556         case IPVERSION:
12557                 iph_out = mtod(mout, struct ip *);
12558
12559                 /* Fill in the IP header for the ABORT */
12560                 iph_out->ip_v = IPVERSION;
12561                 iph_out->ip_hl = (sizeof(struct ip) / 4);
12562                 iph_out->ip_tos = (u_char)0;
12563                 iph_out->ip_id = 0;
12564                 iph_out->ip_off = 0;
12565                 iph_out->ip_ttl = MAXTTL;
12566                 if (port) {
12567                         iph_out->ip_p = IPPROTO_UDP;
12568                 } else {
12569                         iph_out->ip_p = IPPROTO_SCTP;
12570                 }
12571                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
12572                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
12573
12574                 /* let IP layer calculate this */
12575                 iph_out->ip_sum = 0;
12576                 offset_out += sizeof(*iph_out);
12577                 comp_cp = (struct sctp_shutdown_complete_msg *)(
12578                     (caddr_t)iph_out + offset_out);
12579                 break;
12580 #ifdef INET6
12581         case IPV6_VERSION >> 4:
12582                 ip6 = (struct ip6_hdr *)iph;
12583                 ip6_out = mtod(mout, struct ip6_hdr *);
12584
12585                 /* Fill in the IPv6 header for the ABORT */
12586                 ip6_out->ip6_flow = ip6->ip6_flow;
12587                 ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
12588                 if (port) {
12589                         ip6_out->ip6_nxt = IPPROTO_UDP;
12590                 } else {
12591                         ip6_out->ip6_nxt = IPPROTO_SCTP;
12592                 }
12593                 ip6_out->ip6_src = ip6->ip6_dst;
12594                 ip6_out->ip6_dst = ip6->ip6_src;
12595                 /*
12596                  * ?? The old code had both the iph len + payload, I think
12597                  * this is wrong and would never have worked
12598                  */
12599                 ip6_out->ip6_plen = sizeof(struct sctp_shutdown_complete_msg);
12600                 offset_out += sizeof(*ip6_out);
12601                 comp_cp = (struct sctp_shutdown_complete_msg *)(
12602                     (caddr_t)ip6_out + offset_out);
12603                 break;
12604 #endif                          /* INET6 */
12605         default:
12606                 /* Currently not supported. */
12607                 return;
12608         }
12609         if (port) {
12610                 udp = (struct udphdr *)comp_cp;
12611                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
12612                 udp->uh_dport = port;
12613                 udp->uh_ulen = htons(sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr));
12614                 udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
12615                 offset_out += sizeof(struct udphdr);
12616                 comp_cp = (struct sctp_shutdown_complete_msg *)((caddr_t)comp_cp + sizeof(struct udphdr));
12617         }
12618         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
12619                 /* no mbuf's */
12620                 sctp_m_freem(mout);
12621                 return;
12622         }
12623         /* Now copy in and fill in the ABORT tags etc. */
12624         comp_cp->sh.src_port = sh->dest_port;
12625         comp_cp->sh.dest_port = sh->src_port;
12626         comp_cp->sh.checksum = 0;
12627         comp_cp->sh.v_tag = sh->v_tag;
12628         comp_cp->shut_cmp.ch.chunk_flags = SCTP_HAD_NO_TCB;
12629         comp_cp->shut_cmp.ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
12630         comp_cp->shut_cmp.ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
12631
12632         /* add checksum */
12633         comp_cp->sh.checksum = sctp_calculate_sum(mout, NULL, offset_out);
12634         if (iph_out != NULL) {
12635                 sctp_route_t ro;
12636                 int ret;
12637                 struct sctp_tcb *stcb = NULL;
12638
12639                 mlen = SCTP_BUF_LEN(mout);
12640                 bzero(&ro, sizeof ro);
12641                 /* set IPv4 length */
12642                 iph_out->ip_len = mlen;
12643 #ifdef  SCTP_PACKET_LOGGING
12644                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12645                         sctp_packet_log(mout, mlen);
12646 #endif
12647                 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
12648                 if (port) {
12649                         SCTP_ENABLE_UDP_CSUM(o_pak);
12650                 }
12651                 /* out it goes */
12652                 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
12653
12654                 /* Free the route if we got one back */
12655                 if (ro.ro_rt)
12656                         RTFREE(ro.ro_rt);
12657         }
12658 #ifdef INET6
12659         if (ip6_out != NULL) {
12660                 struct route_in6 ro;
12661                 int ret;
12662                 struct sctp_tcb *stcb = NULL;
12663                 struct ifnet *ifp = NULL;
12664
12665                 bzero(&ro, sizeof(ro));
12666                 mlen = SCTP_BUF_LEN(mout);
12667 #ifdef  SCTP_PACKET_LOGGING
12668                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
12669                         sctp_packet_log(mout, mlen);
12670 #endif
12671                 SCTP_ATTACH_CHAIN(o_pak, mout, mlen);
12672                 if (port) {
12673                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr),
12674                             sizeof(struct sctp_shutdown_complete_msg) + sizeof(struct udphdr))) == 0) {
12675                                 udp->uh_sum = 0xffff;
12676                         }
12677                 }
12678                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
12679
12680                 /* Free the route if we got one back */
12681                 if (ro.ro_rt)
12682                         RTFREE(ro.ro_rt);
12683         }
12684 #endif
12685         SCTP_STAT_INCR(sctps_sendpackets);
12686         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12687         SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12688         return;
12689
12690 }
12691
12692 static struct sctp_nets *
12693 sctp_select_hb_destination(struct sctp_tcb *stcb, struct timeval *now)
12694 {
12695         struct sctp_nets *net, *hnet;
12696         int ms_goneby, highest_ms, state_overide = 0;
12697
12698         (void)SCTP_GETTIME_TIMEVAL(now);
12699         highest_ms = 0;
12700         hnet = NULL;
12701         SCTP_TCB_LOCK_ASSERT(stcb);
12702         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
12703                 if (
12704                     ((net->dest_state & SCTP_ADDR_NOHB) && ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) ||
12705                     (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)
12706                     ) {
12707                         /*
12708                          * Skip this guy from consideration if HB is off AND
12709                          * its confirmed
12710                          */
12711                         continue;
12712                 }
12713                 if (sctp_destination_is_reachable(stcb, (struct sockaddr *)&net->ro._l_addr) == 0) {
12714                         /* skip this dest net from consideration */
12715                         continue;
12716                 }
12717                 if (net->last_sent_time.tv_sec) {
12718                         /* Sent to so we subtract */
12719                         ms_goneby = (now->tv_sec - net->last_sent_time.tv_sec) * 1000;
12720                 } else
12721                         /* Never been sent to */
12722                         ms_goneby = 0x7fffffff;
12723                 /*-
12724                  * When the address state is unconfirmed but still
12725                  * considered reachable, we HB at a higher rate. Once it
12726                  * goes confirmed OR reaches the "unreachable" state, thenw
12727                  * we cut it back to HB at a more normal pace.
12728                  */
12729                 if ((net->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED) {
12730                         state_overide = 1;
12731                 } else {
12732                         state_overide = 0;
12733                 }
12734
12735                 if ((((unsigned int)ms_goneby >= net->RTO) || (state_overide)) &&
12736                     (ms_goneby > highest_ms)) {
12737                         highest_ms = ms_goneby;
12738                         hnet = net;
12739                 }
12740         }
12741         if (hnet &&
12742             ((hnet->dest_state & (SCTP_ADDR_UNCONFIRMED | SCTP_ADDR_NOT_REACHABLE)) == SCTP_ADDR_UNCONFIRMED)) {
12743                 state_overide = 1;
12744         } else {
12745                 state_overide = 0;
12746         }
12747
12748         if (hnet && highest_ms && (((unsigned int)highest_ms >= hnet->RTO) || state_overide)) {
12749                 /*-
12750                  * Found the one with longest delay bounds OR it is
12751                  * unconfirmed and still not marked unreachable.
12752                  */
12753                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "net:%p is the hb winner -", hnet);
12754 #ifdef SCTP_DEBUG
12755                 if (hnet) {
12756                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT4,
12757                             (struct sockaddr *)&hnet->ro._l_addr);
12758                 } else {
12759                         SCTPDBG(SCTP_DEBUG_OUTPUT4, " none\n");
12760                 }
12761 #endif
12762                 /* update the timer now */
12763                 hnet->last_sent_time = *now;
12764                 return (hnet);
12765         }
12766         /* Nothing to HB */
12767         return (NULL);
12768 }
12769
12770 int
12771 sctp_send_hb(struct sctp_tcb *stcb, int user_req, struct sctp_nets *u_net)
12772 {
12773         struct sctp_tmit_chunk *chk;
12774         struct sctp_nets *net;
12775         struct sctp_heartbeat_chunk *hb;
12776         struct timeval now;
12777         struct sockaddr_in *sin;
12778         struct sockaddr_in6 *sin6;
12779
12780         SCTP_TCB_LOCK_ASSERT(stcb);
12781         if (user_req == 0) {
12782                 net = sctp_select_hb_destination(stcb, &now);
12783                 if (net == NULL) {
12784                         /*-
12785                          * All our busy none to send to, just start the
12786                          * timer again.
12787                          */
12788                         if (stcb->asoc.state == 0) {
12789                                 return (0);
12790                         }
12791                         sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT,
12792                             stcb->sctp_ep,
12793                             stcb,
12794                             net);
12795                         return (0);
12796                 }
12797         } else {
12798                 net = u_net;
12799                 if (net == NULL) {
12800                         return (0);
12801                 }
12802                 (void)SCTP_GETTIME_TIMEVAL(&now);
12803         }
12804         sin = (struct sockaddr_in *)&net->ro._l_addr;
12805         if (sin->sin_family != AF_INET) {
12806                 if (sin->sin_family != AF_INET6) {
12807                         /* huh */
12808                         return (0);
12809                 }
12810         }
12811         sctp_alloc_a_chunk(stcb, chk);
12812         if (chk == NULL) {
12813                 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
12814                 return (0);
12815         }
12816         chk->copy_by_ref = 0;
12817         chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
12818         chk->rec.chunk_id.can_take_data = 1;
12819         chk->asoc = &stcb->asoc;
12820         chk->send_size = sizeof(struct sctp_heartbeat_chunk);
12821
12822         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
12823         if (chk->data == NULL) {
12824                 sctp_free_a_chunk(stcb, chk);
12825                 return (0);
12826         }
12827         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12828         SCTP_BUF_LEN(chk->data) = chk->send_size;
12829         chk->sent = SCTP_DATAGRAM_UNSENT;
12830         chk->snd_count = 0;
12831         chk->whoTo = net;
12832         atomic_add_int(&chk->whoTo->ref_count, 1);
12833         /* Now we have a mbuf that we can fill in with the details */
12834         hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
12835         memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
12836         /* fill out chunk header */
12837         hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
12838         hb->ch.chunk_flags = 0;
12839         hb->ch.chunk_length = htons(chk->send_size);
12840         /* Fill out hb parameter */
12841         hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
12842         hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
12843         hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
12844         hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
12845         /* Did our user request this one, put it in */
12846         hb->heartbeat.hb_info.user_req = user_req;
12847         hb->heartbeat.hb_info.addr_family = sin->sin_family;
12848         hb->heartbeat.hb_info.addr_len = sin->sin_len;
12849         if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
12850                 /*
12851                  * we only take from the entropy pool if the address is not
12852                  * confirmed.
12853                  */
12854                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
12855                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
12856         } else {
12857                 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
12858                 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
12859         }
12860         if (sin->sin_family == AF_INET) {
12861                 memcpy(hb->heartbeat.hb_info.address, &sin->sin_addr, sizeof(sin->sin_addr));
12862         } else if (sin->sin_family == AF_INET6) {
12863                 /* We leave the scope the way it is in our lookup table. */
12864                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
12865                 memcpy(hb->heartbeat.hb_info.address, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
12866         } else {
12867                 /* huh compiler bug */
12868                 return (0);
12869         }
12870
12871         /*
12872          * JRS 5/14/07 - In CMT PF, the T3 timer is used to track
12873          * PF-heartbeats.  Because of this, threshold management is done by
12874          * the t3 timer handler, and does not need to be done upon the send
12875          * of a PF-heartbeat. If CMT PF is on and the destination to which a
12876          * heartbeat is being sent is in PF state, do NOT do threshold
12877          * management.
12878          */
12879         if ((SCTP_BASE_SYSCTL(sctp_cmt_pf) == 0) || ((net->dest_state & SCTP_ADDR_PF) != SCTP_ADDR_PF)) {
12880                 /* ok we have a destination that needs a beat */
12881                 /* lets do the theshold management Qiaobing style */
12882                 if (sctp_threshold_management(stcb->sctp_ep, stcb, net,
12883                     stcb->asoc.max_send_times)) {
12884                         /*-
12885                          * we have lost the association, in a way this is
12886                          * quite bad since we really are one less time since
12887                          * we really did not send yet. This is the down side
12888                          * to the Q's style as defined in the RFC and not my
12889                          * alternate style defined in the RFC.
12890                          */
12891                         if (chk->data != NULL) {
12892                                 sctp_m_freem(chk->data);
12893                                 chk->data = NULL;
12894                         }
12895                         /*
12896                          * Here we do NOT use the macro since the
12897                          * association is now gone.
12898                          */
12899                         if (chk->whoTo) {
12900                                 sctp_free_remote_addr(chk->whoTo);
12901                                 chk->whoTo = NULL;
12902                         }
12903                         sctp_free_a_chunk((struct sctp_tcb *)NULL, chk);
12904                         return (-1);
12905                 }
12906         }
12907         net->hb_responded = 0;
12908         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
12909         stcb->asoc.ctrl_queue_cnt++;
12910         SCTP_STAT_INCR(sctps_sendheartbeat);
12911         /*-
12912          * Call directly med level routine to put out the chunk. It will
12913          * always tumble out control chunks aka HB but it may even tumble
12914          * out data too.
12915          */
12916         return (1);
12917 }
12918
12919 void
12920 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
12921     uint32_t high_tsn)
12922 {
12923         struct sctp_association *asoc;
12924         struct sctp_ecne_chunk *ecne;
12925         struct sctp_tmit_chunk *chk;
12926
12927         asoc = &stcb->asoc;
12928         SCTP_TCB_LOCK_ASSERT(stcb);
12929         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
12930                 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
12931                         /* found a previous ECN_ECHO update it if needed */
12932                         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
12933                         ecne->tsn = htonl(high_tsn);
12934                         return;
12935                 }
12936         }
12937         /* nope could not find one to update so we must build one */
12938         sctp_alloc_a_chunk(stcb, chk);
12939         if (chk == NULL) {
12940                 return;
12941         }
12942         chk->copy_by_ref = 0;
12943         SCTP_STAT_INCR(sctps_sendecne);
12944         chk->rec.chunk_id.id = SCTP_ECN_ECHO;
12945         chk->rec.chunk_id.can_take_data = 0;
12946         chk->asoc = &stcb->asoc;
12947         chk->send_size = sizeof(struct sctp_ecne_chunk);
12948         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
12949         if (chk->data == NULL) {
12950                 sctp_free_a_chunk(stcb, chk);
12951                 return;
12952         }
12953         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12954         SCTP_BUF_LEN(chk->data) = chk->send_size;
12955         chk->sent = SCTP_DATAGRAM_UNSENT;
12956         chk->snd_count = 0;
12957         chk->whoTo = net;
12958         atomic_add_int(&chk->whoTo->ref_count, 1);
12959         stcb->asoc.ecn_echo_cnt_onq++;
12960         ecne = mtod(chk->data, struct sctp_ecne_chunk *);
12961         ecne->ch.chunk_type = SCTP_ECN_ECHO;
12962         ecne->ch.chunk_flags = 0;
12963         ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
12964         ecne->tsn = htonl(high_tsn);
12965         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
12966         asoc->ctrl_queue_cnt++;
12967 }
12968
12969 void
12970 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
12971     struct mbuf *m, int iphlen, int bad_crc)
12972 {
12973         struct sctp_association *asoc;
12974         struct sctp_pktdrop_chunk *drp;
12975         struct sctp_tmit_chunk *chk;
12976         uint8_t *datap;
12977         int len;
12978         int was_trunc = 0;
12979         struct ip *iph;
12980
12981 #ifdef INET6
12982         struct ip6_hdr *ip6h;
12983
12984 #endif
12985         int fullsz = 0, extra = 0;
12986         long spc;
12987         int offset;
12988         struct sctp_chunkhdr *ch, chunk_buf;
12989         unsigned int chk_length;
12990
12991         if (!stcb) {
12992                 return;
12993         }
12994         asoc = &stcb->asoc;
12995         SCTP_TCB_LOCK_ASSERT(stcb);
12996         if (asoc->peer_supports_pktdrop == 0) {
12997                 /*-
12998                  * peer must declare support before I send one.
12999                  */
13000                 return;
13001         }
13002         if (stcb->sctp_socket == NULL) {
13003                 return;
13004         }
13005         sctp_alloc_a_chunk(stcb, chk);
13006         if (chk == NULL) {
13007                 return;
13008         }
13009         chk->copy_by_ref = 0;
13010         iph = mtod(m, struct ip *);
13011         if (iph == NULL) {
13012                 sctp_free_a_chunk(stcb, chk);
13013                 return;
13014         }
13015         switch (iph->ip_v) {
13016         case IPVERSION:
13017                 /* IPv4 */
13018                 len = chk->send_size = iph->ip_len;
13019                 break;
13020 #ifdef INET6
13021         case IPV6_VERSION >> 4:
13022                 /* IPv6 */
13023                 ip6h = mtod(m, struct ip6_hdr *);
13024                 len = chk->send_size = htons(ip6h->ip6_plen);
13025                 break;
13026 #endif
13027         default:
13028                 return;
13029         }
13030         /* Validate that we do not have an ABORT in here. */
13031         offset = iphlen + sizeof(struct sctphdr);
13032         ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
13033             sizeof(*ch), (uint8_t *) & chunk_buf);
13034         while (ch != NULL) {
13035                 chk_length = ntohs(ch->chunk_length);
13036                 if (chk_length < sizeof(*ch)) {
13037                         /* break to abort land */
13038                         break;
13039                 }
13040                 switch (ch->chunk_type) {
13041                 case SCTP_PACKET_DROPPED:
13042                 case SCTP_ABORT_ASSOCIATION:
13043                         /*-
13044                          * we don't respond with an PKT-DROP to an ABORT
13045                          * or PKT-DROP
13046                          */
13047                         sctp_free_a_chunk(stcb, chk);
13048                         return;
13049                 default:
13050                         break;
13051                 }
13052                 offset += SCTP_SIZE32(chk_length);
13053                 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
13054                     sizeof(*ch), (uint8_t *) & chunk_buf);
13055         }
13056
13057         if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
13058             min(stcb->asoc.smallest_mtu, MCLBYTES)) {
13059                 /*
13060                  * only send 1 mtu worth, trim off the excess on the end.
13061                  */
13062                 fullsz = len - extra;
13063                 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
13064                 was_trunc = 1;
13065         }
13066         chk->asoc = &stcb->asoc;
13067         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
13068         if (chk->data == NULL) {
13069 jump_out:
13070                 sctp_free_a_chunk(stcb, chk);
13071                 return;
13072         }
13073         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13074         drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
13075         if (drp == NULL) {
13076                 sctp_m_freem(chk->data);
13077                 chk->data = NULL;
13078                 goto jump_out;
13079         }
13080         chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
13081             sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
13082         chk->book_size_scale = 0;
13083         if (was_trunc) {
13084                 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
13085                 drp->trunc_len = htons(fullsz);
13086                 /*
13087                  * Len is already adjusted to size minus overhead above take
13088                  * out the pkt_drop chunk itself from it.
13089                  */
13090                 chk->send_size = len - sizeof(struct sctp_pktdrop_chunk);
13091                 len = chk->send_size;
13092         } else {
13093                 /* no truncation needed */
13094                 drp->ch.chunk_flags = 0;
13095                 drp->trunc_len = htons(0);
13096         }
13097         if (bad_crc) {
13098                 drp->ch.chunk_flags |= SCTP_BADCRC;
13099         }
13100         chk->send_size += sizeof(struct sctp_pktdrop_chunk);
13101         SCTP_BUF_LEN(chk->data) = chk->send_size;
13102         chk->sent = SCTP_DATAGRAM_UNSENT;
13103         chk->snd_count = 0;
13104         if (net) {
13105                 /* we should hit here */
13106                 chk->whoTo = net;
13107         } else {
13108                 chk->whoTo = asoc->primary_destination;
13109         }
13110         atomic_add_int(&chk->whoTo->ref_count, 1);
13111         chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
13112         chk->rec.chunk_id.can_take_data = 1;
13113         drp->ch.chunk_type = SCTP_PACKET_DROPPED;
13114         drp->ch.chunk_length = htons(chk->send_size);
13115         spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
13116         if (spc < 0) {
13117                 spc = 0;
13118         }
13119         drp->bottle_bw = htonl(spc);
13120         if (asoc->my_rwnd) {
13121                 drp->current_onq = htonl(asoc->size_on_reasm_queue +
13122                     asoc->size_on_all_streams +
13123                     asoc->my_rwnd_control_len +
13124                     stcb->sctp_socket->so_rcv.sb_cc);
13125         } else {
13126                 /*-
13127                  * If my rwnd is 0, possibly from mbuf depletion as well as
13128                  * space used, tell the peer there is NO space aka onq == bw
13129                  */
13130                 drp->current_onq = htonl(spc);
13131         }
13132         drp->reserved = 0;
13133         datap = drp->data;
13134         m_copydata(m, iphlen, len, (caddr_t)datap);
13135         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
13136         asoc->ctrl_queue_cnt++;
13137 }
13138
13139 void
13140 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn)
13141 {
13142         struct sctp_association *asoc;
13143         struct sctp_cwr_chunk *cwr;
13144         struct sctp_tmit_chunk *chk;
13145
13146         asoc = &stcb->asoc;
13147         SCTP_TCB_LOCK_ASSERT(stcb);
13148         TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
13149                 if (chk->rec.chunk_id.id == SCTP_ECN_CWR) {
13150                         /* found a previous ECN_CWR update it if needed */
13151                         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
13152                         if (compare_with_wrap(high_tsn, ntohl(cwr->tsn),
13153                             MAX_TSN)) {
13154                                 cwr->tsn = htonl(high_tsn);
13155                         }
13156                         return;
13157                 }
13158         }
13159         /* nope could not find one to update so we must build one */
13160         sctp_alloc_a_chunk(stcb, chk);
13161         if (chk == NULL) {
13162                 return;
13163         }
13164         chk->copy_by_ref = 0;
13165         chk->rec.chunk_id.id = SCTP_ECN_CWR;
13166         chk->rec.chunk_id.can_take_data = 1;
13167         chk->asoc = &stcb->asoc;
13168         chk->send_size = sizeof(struct sctp_cwr_chunk);
13169         chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_DONTWAIT, 1, MT_HEADER);
13170         if (chk->data == NULL) {
13171                 sctp_free_a_chunk(stcb, chk);
13172                 return;
13173         }
13174         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13175         SCTP_BUF_LEN(chk->data) = chk->send_size;
13176         chk->sent = SCTP_DATAGRAM_UNSENT;
13177         chk->snd_count = 0;
13178         chk->whoTo = net;
13179         atomic_add_int(&chk->whoTo->ref_count, 1);
13180         cwr = mtod(chk->data, struct sctp_cwr_chunk *);
13181         cwr->ch.chunk_type = SCTP_ECN_CWR;
13182         cwr->ch.chunk_flags = 0;
13183         cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
13184         cwr->tsn = htonl(high_tsn);
13185         TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
13186         asoc->ctrl_queue_cnt++;
13187 }
13188
13189 void
13190 sctp_add_stream_reset_out(struct sctp_tmit_chunk *chk,
13191     int number_entries, uint16_t * list,
13192     uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
13193 {
13194         int len, old_len, i;
13195         struct sctp_stream_reset_out_request *req_out;
13196         struct sctp_chunkhdr *ch;
13197
13198         ch = mtod(chk->data, struct sctp_chunkhdr *);
13199
13200
13201         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13202
13203         /* get to new offset for the param. */
13204         req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
13205         /* now how long will this param be? */
13206         len = (sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
13207         req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
13208         req_out->ph.param_length = htons(len);
13209         req_out->request_seq = htonl(seq);
13210         req_out->response_seq = htonl(resp_seq);
13211         req_out->send_reset_at_tsn = htonl(last_sent);
13212         if (number_entries) {
13213                 for (i = 0; i < number_entries; i++) {
13214                         req_out->list_of_streams[i] = htons(list[i]);
13215                 }
13216         }
13217         if (SCTP_SIZE32(len) > len) {
13218                 /*-
13219                  * Need to worry about the pad we may end up adding to the
13220                  * end. This is easy since the struct is either aligned to 4
13221                  * bytes or 2 bytes off.
13222                  */
13223                 req_out->list_of_streams[number_entries] = 0;
13224         }
13225         /* now fix the chunk length */
13226         ch->chunk_length = htons(len + old_len);
13227         chk->book_size = len + old_len;
13228         chk->book_size_scale = 0;
13229         chk->send_size = SCTP_SIZE32(chk->book_size);
13230         SCTP_BUF_LEN(chk->data) = chk->send_size;
13231         return;
13232 }
13233
13234
13235 void
13236 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
13237     int number_entries, uint16_t * list,
13238     uint32_t seq)
13239 {
13240         int len, old_len, i;
13241         struct sctp_stream_reset_in_request *req_in;
13242         struct sctp_chunkhdr *ch;
13243
13244         ch = mtod(chk->data, struct sctp_chunkhdr *);
13245
13246
13247         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13248
13249         /* get to new offset for the param. */
13250         req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
13251         /* now how long will this param be? */
13252         len = (sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
13253         req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
13254         req_in->ph.param_length = htons(len);
13255         req_in->request_seq = htonl(seq);
13256         if (number_entries) {
13257                 for (i = 0; i < number_entries; i++) {
13258                         req_in->list_of_streams[i] = htons(list[i]);
13259                 }
13260         }
13261         if (SCTP_SIZE32(len) > len) {
13262                 /*-
13263                  * Need to worry about the pad we may end up adding to the
13264                  * end. This is easy since the struct is either aligned to 4
13265                  * bytes or 2 bytes off.
13266                  */
13267                 req_in->list_of_streams[number_entries] = 0;
13268         }
13269         /* now fix the chunk length */
13270         ch->chunk_length = htons(len + old_len);
13271         chk->book_size = len + old_len;
13272         chk->book_size_scale = 0;
13273         chk->send_size = SCTP_SIZE32(chk->book_size);
13274         SCTP_BUF_LEN(chk->data) = chk->send_size;
13275         return;
13276 }
13277
13278
13279 void
13280 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
13281     uint32_t seq)
13282 {
13283         int len, old_len;
13284         struct sctp_stream_reset_tsn_request *req_tsn;
13285         struct sctp_chunkhdr *ch;
13286
13287         ch = mtod(chk->data, struct sctp_chunkhdr *);
13288
13289
13290         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13291
13292         /* get to new offset for the param. */
13293         req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
13294         /* now how long will this param be? */
13295         len = sizeof(struct sctp_stream_reset_tsn_request);
13296         req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
13297         req_tsn->ph.param_length = htons(len);
13298         req_tsn->request_seq = htonl(seq);
13299
13300         /* now fix the chunk length */
13301         ch->chunk_length = htons(len + old_len);
13302         chk->send_size = len + old_len;
13303         chk->book_size = SCTP_SIZE32(chk->send_size);
13304         chk->book_size_scale = 0;
13305         SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
13306         return;
13307 }
13308
13309 void
13310 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
13311     uint32_t resp_seq, uint32_t result)
13312 {
13313         int len, old_len;
13314         struct sctp_stream_reset_response *resp;
13315         struct sctp_chunkhdr *ch;
13316
13317         ch = mtod(chk->data, struct sctp_chunkhdr *);
13318
13319
13320         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13321
13322         /* get to new offset for the param. */
13323         resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
13324         /* now how long will this param be? */
13325         len = sizeof(struct sctp_stream_reset_response);
13326         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
13327         resp->ph.param_length = htons(len);
13328         resp->response_seq = htonl(resp_seq);
13329         resp->result = ntohl(result);
13330
13331         /* now fix the chunk length */
13332         ch->chunk_length = htons(len + old_len);
13333         chk->book_size = len + old_len;
13334         chk->book_size_scale = 0;
13335         chk->send_size = SCTP_SIZE32(chk->book_size);
13336         SCTP_BUF_LEN(chk->data) = chk->send_size;
13337         return;
13338
13339 }
13340
13341
13342 void
13343 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
13344     uint32_t resp_seq, uint32_t result,
13345     uint32_t send_una, uint32_t recv_next)
13346 {
13347         int len, old_len;
13348         struct sctp_stream_reset_response_tsn *resp;
13349         struct sctp_chunkhdr *ch;
13350
13351         ch = mtod(chk->data, struct sctp_chunkhdr *);
13352
13353
13354         old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
13355
13356         /* get to new offset for the param. */
13357         resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
13358         /* now how long will this param be? */
13359         len = sizeof(struct sctp_stream_reset_response_tsn);
13360         resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
13361         resp->ph.param_length = htons(len);
13362         resp->response_seq = htonl(resp_seq);
13363         resp->result = htonl(result);
13364         resp->senders_next_tsn = htonl(send_una);
13365         resp->receivers_next_tsn = htonl(recv_next);
13366
13367         /* now fix the chunk length */
13368         ch->chunk_length = htons(len + old_len);
13369         chk->book_size = len + old_len;
13370         chk->send_size = SCTP_SIZE32(chk->book_size);
13371         chk->book_size_scale = 0;
13372         SCTP_BUF_LEN(chk->data) = chk->send_size;
13373         return;
13374 }
13375
13376
13377 int
13378 sctp_send_str_reset_req(struct sctp_tcb *stcb,
13379     int number_entries, uint16_t * list,
13380     uint8_t send_out_req, uint32_t resp_seq,
13381     uint8_t send_in_req,
13382     uint8_t send_tsn_req)
13383 {
13384
13385         struct sctp_association *asoc;
13386         struct sctp_tmit_chunk *chk;
13387         struct sctp_chunkhdr *ch;
13388         uint32_t seq;
13389
13390         asoc = &stcb->asoc;
13391         if (asoc->stream_reset_outstanding) {
13392                 /*-
13393                  * Already one pending, must get ACK back to clear the flag.
13394                  */
13395                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
13396                 return (EBUSY);
13397         }
13398         if ((send_out_req == 0) && (send_in_req == 0) && (send_tsn_req == 0)) {
13399                 /* nothing to do */
13400                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13401                 return (EINVAL);
13402         }
13403         if (send_tsn_req && (send_out_req || send_in_req)) {
13404                 /* error, can't do that */
13405                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13406                 return (EINVAL);
13407         }
13408         sctp_alloc_a_chunk(stcb, chk);
13409         if (chk == NULL) {
13410                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13411                 return (ENOMEM);
13412         }
13413         chk->copy_by_ref = 0;
13414         chk->rec.chunk_id.id = SCTP_STREAM_RESET;
13415         chk->rec.chunk_id.can_take_data = 0;
13416         chk->asoc = &stcb->asoc;
13417         chk->book_size = sizeof(struct sctp_chunkhdr);
13418         chk->send_size = SCTP_SIZE32(chk->book_size);
13419         chk->book_size_scale = 0;
13420
13421         chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_DONTWAIT, 1, MT_DATA);
13422         if (chk->data == NULL) {
13423                 sctp_free_a_chunk(stcb, chk);
13424                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13425                 return (ENOMEM);
13426         }
13427         SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13428
13429         /* setup chunk parameters */
13430         chk->sent = SCTP_DATAGRAM_UNSENT;
13431         chk->snd_count = 0;
13432         chk->whoTo = asoc->primary_destination;
13433         atomic_add_int(&chk->whoTo->ref_count, 1);
13434
13435         ch = mtod(chk->data, struct sctp_chunkhdr *);
13436         ch->chunk_type = SCTP_STREAM_RESET;
13437         ch->chunk_flags = 0;
13438         ch->chunk_length = htons(chk->book_size);
13439         SCTP_BUF_LEN(chk->data) = chk->send_size;
13440
13441         seq = stcb->asoc.str_reset_seq_out;
13442         if (send_out_req) {
13443                 sctp_add_stream_reset_out(chk, number_entries, list,
13444                     seq, resp_seq, (stcb->asoc.sending_seq - 1));
13445                 asoc->stream_reset_out_is_outstanding = 1;
13446                 seq++;
13447                 asoc->stream_reset_outstanding++;
13448         }
13449         if (send_in_req) {
13450                 sctp_add_stream_reset_in(chk, number_entries, list, seq);
13451                 asoc->stream_reset_outstanding++;
13452         }
13453         if (send_tsn_req) {
13454                 sctp_add_stream_reset_tsn(chk, seq);
13455                 asoc->stream_reset_outstanding++;
13456         }
13457         asoc->str_reset = chk;
13458
13459         /* insert the chunk for sending */
13460         TAILQ_INSERT_TAIL(&asoc->control_send_queue,
13461             chk,
13462             sctp_next);
13463         asoc->ctrl_queue_cnt++;
13464         sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
13465         return (0);
13466 }
13467
13468 void
13469 sctp_send_abort(struct mbuf *m, int iphlen, struct sctphdr *sh, uint32_t vtag,
13470     struct mbuf *err_cause, uint32_t vrf_id, uint16_t port)
13471 {
13472         /*-
13473          * Formulate the abort message, and send it back down.
13474          */
13475         struct mbuf *o_pak;
13476         struct mbuf *mout;
13477         struct sctp_abort_msg *abm;
13478         struct ip *iph, *iph_out;
13479         struct udphdr *udp;
13480
13481 #ifdef INET6
13482         struct ip6_hdr *ip6, *ip6_out;
13483
13484 #endif
13485         int iphlen_out, len;
13486
13487         /* don't respond to ABORT with ABORT */
13488         if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
13489                 if (err_cause)
13490                         sctp_m_freem(err_cause);
13491                 return;
13492         }
13493         iph = mtod(m, struct ip *);
13494         switch (iph->ip_v) {
13495         case IPVERSION:
13496                 len = (sizeof(struct ip) + sizeof(struct sctp_abort_msg));
13497                 break;
13498 #ifdef INET6
13499         case IPV6_VERSION >> 4:
13500                 len = (sizeof(struct ip6_hdr) + sizeof(struct sctp_abort_msg));
13501                 break;
13502 #endif
13503         default:
13504                 return;
13505         }
13506         if (port) {
13507                 len += sizeof(struct udphdr);
13508         }
13509         mout = sctp_get_mbuf_for_msg(len, 1, M_DONTWAIT, 1, MT_DATA);
13510         if (mout == NULL) {
13511                 if (err_cause)
13512                         sctp_m_freem(err_cause);
13513                 return;
13514         }
13515         SCTP_BUF_LEN(mout) = len;
13516         SCTP_BUF_NEXT(mout) = err_cause;
13517         iph_out = NULL;
13518 #ifdef INET6
13519         ip6_out = NULL;
13520 #endif
13521         switch (iph->ip_v) {
13522         case IPVERSION:
13523                 iph_out = mtod(mout, struct ip *);
13524
13525                 /* Fill in the IP header for the ABORT */
13526                 iph_out->ip_v = IPVERSION;
13527                 iph_out->ip_hl = (sizeof(struct ip) / 4);
13528                 iph_out->ip_tos = (u_char)0;
13529                 iph_out->ip_id = 0;
13530                 iph_out->ip_off = 0;
13531                 iph_out->ip_ttl = MAXTTL;
13532                 if (port) {
13533                         iph_out->ip_p = IPPROTO_UDP;
13534                 } else {
13535                         iph_out->ip_p = IPPROTO_SCTP;
13536                 }
13537                 iph_out->ip_src.s_addr = iph->ip_dst.s_addr;
13538                 iph_out->ip_dst.s_addr = iph->ip_src.s_addr;
13539                 /* let IP layer calculate this */
13540                 iph_out->ip_sum = 0;
13541
13542                 iphlen_out = sizeof(*iph_out);
13543                 abm = (struct sctp_abort_msg *)((caddr_t)iph_out + iphlen_out);
13544                 break;
13545 #ifdef INET6
13546         case IPV6_VERSION >> 4:
13547                 ip6 = (struct ip6_hdr *)iph;
13548                 ip6_out = mtod(mout, struct ip6_hdr *);
13549
13550                 /* Fill in the IP6 header for the ABORT */
13551                 ip6_out->ip6_flow = ip6->ip6_flow;
13552                 ip6_out->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
13553                 if (port) {
13554                         ip6_out->ip6_nxt = IPPROTO_UDP;
13555                 } else {
13556                         ip6_out->ip6_nxt = IPPROTO_SCTP;
13557                 }
13558                 ip6_out->ip6_src = ip6->ip6_dst;
13559                 ip6_out->ip6_dst = ip6->ip6_src;
13560
13561                 iphlen_out = sizeof(*ip6_out);
13562                 abm = (struct sctp_abort_msg *)((caddr_t)ip6_out + iphlen_out);
13563                 break;
13564 #endif                          /* INET6 */
13565         default:
13566                 /* Currently not supported */
13567                 if (err_cause)
13568                         sctp_m_freem(err_cause);
13569                 sctp_m_freem(mout);
13570                 return;
13571         }
13572
13573         udp = (struct udphdr *)abm;
13574         if (port) {
13575                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
13576                 udp->uh_dport = port;
13577                 /* set udp->uh_ulen later */
13578                 udp->uh_sum = 0;
13579                 iphlen_out += sizeof(struct udphdr);
13580                 abm = (struct sctp_abort_msg *)((caddr_t)abm + sizeof(struct udphdr));
13581         }
13582         abm->sh.src_port = sh->dest_port;
13583         abm->sh.dest_port = sh->src_port;
13584         abm->sh.checksum = 0;
13585         if (vtag == 0) {
13586                 abm->sh.v_tag = sh->v_tag;
13587                 abm->msg.ch.chunk_flags = SCTP_HAD_NO_TCB;
13588         } else {
13589                 abm->sh.v_tag = htonl(vtag);
13590                 abm->msg.ch.chunk_flags = 0;
13591         }
13592         abm->msg.ch.chunk_type = SCTP_ABORT_ASSOCIATION;
13593
13594         if (err_cause) {
13595                 struct mbuf *m_tmp = err_cause;
13596                 int err_len = 0;
13597
13598                 /* get length of the err_cause chain */
13599                 while (m_tmp != NULL) {
13600                         err_len += SCTP_BUF_LEN(m_tmp);
13601                         m_tmp = SCTP_BUF_NEXT(m_tmp);
13602                 }
13603                 len = SCTP_BUF_LEN(mout) + err_len;
13604                 if (err_len % 4) {
13605                         /* need pad at end of chunk */
13606                         uint32_t cpthis = 0;
13607                         int padlen;
13608
13609                         padlen = 4 - (len % 4);
13610                         m_copyback(mout, len, padlen, (caddr_t)&cpthis);
13611                         len += padlen;
13612                 }
13613                 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch) + err_len);
13614         } else {
13615                 len = SCTP_BUF_LEN(mout);
13616                 abm->msg.ch.chunk_length = htons(sizeof(abm->msg.ch));
13617         }
13618
13619         /* add checksum */
13620         abm->sh.checksum = sctp_calculate_sum(mout, NULL, iphlen_out);
13621         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
13622                 /* no mbuf's */
13623                 sctp_m_freem(mout);
13624                 return;
13625         }
13626         if (iph_out != NULL) {
13627                 sctp_route_t ro;
13628                 struct sctp_tcb *stcb = NULL;
13629                 int ret;
13630
13631                 /* zap the stack pointer to the route */
13632                 bzero(&ro, sizeof ro);
13633                 if (port) {
13634                         udp->uh_ulen = htons(len - sizeof(struct ip));
13635                         udp->uh_sum = in_pseudo(iph_out->ip_src.s_addr, iph_out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
13636                 }
13637                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip_output:\n");
13638                 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, iph_out, &abm->sh);
13639                 /* set IPv4 length */
13640                 iph_out->ip_len = len;
13641                 /* out it goes */
13642 #ifdef  SCTP_PACKET_LOGGING
13643                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13644                         sctp_packet_log(mout, len);
13645 #endif
13646                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
13647                 if (port) {
13648                         SCTP_ENABLE_UDP_CSUM(o_pak);
13649                 }
13650                 SCTP_IP_OUTPUT(ret, o_pak, &ro, stcb, vrf_id);
13651
13652                 /* Free the route if we got one back */
13653                 if (ro.ro_rt)
13654                         RTFREE(ro.ro_rt);
13655         }
13656 #ifdef INET6
13657         if (ip6_out != NULL) {
13658                 struct route_in6 ro;
13659                 int ret;
13660                 struct sctp_tcb *stcb = NULL;
13661                 struct ifnet *ifp = NULL;
13662
13663                 /* zap the stack pointer to the route */
13664                 bzero(&ro, sizeof(ro));
13665                 if (port) {
13666                         udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
13667                 }
13668                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_send_abort calling ip6_output:\n");
13669                 SCTPDBG_PKT(SCTP_DEBUG_OUTPUT2, (struct ip *)ip6_out, &abm->sh);
13670                 ip6_out->ip6_plen = len - sizeof(*ip6_out);
13671 #ifdef  SCTP_PACKET_LOGGING
13672                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13673                         sctp_packet_log(mout, len);
13674 #endif
13675                 SCTP_ATTACH_CHAIN(o_pak, mout, len);
13676                 if (port) {
13677                         if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
13678                                 udp->uh_sum = 0xffff;
13679                         }
13680                 }
13681                 SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
13682
13683                 /* Free the route if we got one back */
13684                 if (ro.ro_rt)
13685                         RTFREE(ro.ro_rt);
13686         }
13687 #endif
13688         SCTP_STAT_INCR(sctps_sendpackets);
13689         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
13690 }
13691
13692 void
13693 sctp_send_operr_to(struct mbuf *m, int iphlen, struct mbuf *scm, uint32_t vtag,
13694     uint32_t vrf_id, uint16_t port)
13695 {
13696         struct mbuf *o_pak;
13697         struct sctphdr *ihdr;
13698         int retcode;
13699         struct sctphdr *ohdr;
13700         struct sctp_chunkhdr *ophdr;
13701         struct ip *iph;
13702         struct udphdr *udp = NULL;
13703         struct mbuf *mout;
13704
13705 #ifdef INET6
13706 #ifdef SCTP_DEBUG
13707         struct sockaddr_in6 lsa6, fsa6;
13708
13709 #endif
13710 #endif
13711         uint32_t val;
13712         struct mbuf *at;
13713         int len;
13714
13715         iph = mtod(m, struct ip *);
13716         ihdr = (struct sctphdr *)((caddr_t)iph + iphlen);
13717
13718         SCTP_BUF_PREPEND(scm, (sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)), M_DONTWAIT);
13719         if (scm == NULL) {
13720                 /* can't send because we can't add a mbuf */
13721                 return;
13722         }
13723         ohdr = mtod(scm, struct sctphdr *);
13724         ohdr->src_port = ihdr->dest_port;
13725         ohdr->dest_port = ihdr->src_port;
13726         ohdr->v_tag = vtag;
13727         ohdr->checksum = 0;
13728         ophdr = (struct sctp_chunkhdr *)(ohdr + 1);
13729         ophdr->chunk_type = SCTP_OPERATION_ERROR;
13730         ophdr->chunk_flags = 0;
13731         len = 0;
13732         at = scm;
13733         while (at) {
13734                 len += SCTP_BUF_LEN(at);
13735                 at = SCTP_BUF_NEXT(at);
13736         }
13737         ophdr->chunk_length = htons(len - sizeof(struct sctphdr));
13738         if (len % 4) {
13739                 /* need padding */
13740                 uint32_t cpthis = 0;
13741                 int padlen;
13742
13743                 padlen = 4 - (len % 4);
13744                 m_copyback(scm, len, padlen, (caddr_t)&cpthis);
13745                 len += padlen;
13746         }
13747         val = sctp_calculate_sum(scm, NULL, 0);
13748 #ifdef INET6
13749         if (port) {
13750                 mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
13751         } else {
13752                 mout = sctp_get_mbuf_for_msg(sizeof(struct ip6_hdr), 1, M_DONTWAIT, 1, MT_DATA);
13753         }
13754 #else
13755         if (port) {
13756                 mout = sctp_get_mbuf_for_msg(sizeof(struct ip) + sizeof(struct udphdr), 1, M_DONTWAIT, 1, MT_DATA);
13757         } else {
13758                 mout = sctp_get_mbuf_for_msg(sizeof(struct ip), 1, M_DONTWAIT, 1, MT_DATA);
13759         }
13760 #endif
13761         if (mout == NULL) {
13762                 sctp_m_freem(scm);
13763                 return;
13764         }
13765         SCTP_BUF_NEXT(mout) = scm;
13766         if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
13767                 sctp_m_freem(mout);
13768                 return;
13769         }
13770         ohdr->checksum = val;
13771         switch (iph->ip_v) {
13772         case IPVERSION:
13773                 {
13774                         /* V4 */
13775                         struct ip *out;
13776                         sctp_route_t ro;
13777                         struct sctp_tcb *stcb = NULL;
13778
13779                         SCTP_BUF_LEN(mout) = sizeof(struct ip);
13780                         len += sizeof(struct ip);
13781                         if (port) {
13782                                 SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
13783                                 len += sizeof(struct udphdr);
13784                         }
13785                         bzero(&ro, sizeof ro);
13786                         out = mtod(mout, struct ip *);
13787                         out->ip_v = iph->ip_v;
13788                         out->ip_hl = (sizeof(struct ip) / 4);
13789                         out->ip_tos = iph->ip_tos;
13790                         out->ip_id = iph->ip_id;
13791                         out->ip_off = 0;
13792                         out->ip_ttl = MAXTTL;
13793                         if (port) {
13794                                 out->ip_p = IPPROTO_UDP;
13795                         } else {
13796                                 out->ip_p = IPPROTO_SCTP;
13797                         }
13798                         out->ip_sum = 0;
13799                         out->ip_src = iph->ip_dst;
13800                         out->ip_dst = iph->ip_src;
13801                         out->ip_len = len;
13802                         if (port) {
13803                                 udp = (struct udphdr *)(out + 1);
13804                                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
13805                                 udp->uh_dport = port;
13806                                 udp->uh_ulen = htons(len - sizeof(struct ip));
13807                                 udp->uh_sum = in_pseudo(out->ip_src.s_addr, out->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
13808                         }
13809 #ifdef  SCTP_PACKET_LOGGING
13810                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13811                                 sctp_packet_log(mout, len);
13812 #endif
13813                         SCTP_ATTACH_CHAIN(o_pak, mout, len);
13814                         if (port) {
13815                                 SCTP_ENABLE_UDP_CSUM(o_pak);
13816                         }
13817                         SCTP_IP_OUTPUT(retcode, o_pak, &ro, stcb, vrf_id);
13818
13819                         SCTP_STAT_INCR(sctps_sendpackets);
13820                         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
13821                         /* Free the route if we got one back */
13822                         if (ro.ro_rt)
13823                                 RTFREE(ro.ro_rt);
13824                         break;
13825                 }
13826 #ifdef INET6
13827         case IPV6_VERSION >> 4:
13828                 {
13829                         /* V6 */
13830                         struct route_in6 ro;
13831                         int ret;
13832                         struct sctp_tcb *stcb = NULL;
13833                         struct ifnet *ifp = NULL;
13834                         struct ip6_hdr *out6, *in6;
13835
13836                         SCTP_BUF_LEN(mout) = sizeof(struct ip6_hdr);
13837                         len += sizeof(struct ip6_hdr);
13838                         bzero(&ro, sizeof ro);
13839                         if (port) {
13840                                 SCTP_BUF_LEN(mout) += sizeof(struct udphdr);
13841                                 len += sizeof(struct udphdr);
13842                         }
13843                         in6 = mtod(m, struct ip6_hdr *);
13844                         out6 = mtod(mout, struct ip6_hdr *);
13845                         out6->ip6_flow = in6->ip6_flow;
13846                         out6->ip6_hlim = MODULE_GLOBAL(MOD_INET6, ip6_defhlim);
13847                         if (port) {
13848                                 out6->ip6_nxt = IPPROTO_UDP;
13849                         } else {
13850                                 out6->ip6_nxt = IPPROTO_SCTP;
13851                         }
13852                         out6->ip6_src = in6->ip6_dst;
13853                         out6->ip6_dst = in6->ip6_src;
13854                         out6->ip6_plen = len - sizeof(struct ip6_hdr);
13855                         if (port) {
13856                                 udp = (struct udphdr *)(out6 + 1);
13857                                 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
13858                                 udp->uh_dport = port;
13859                                 udp->uh_ulen = htons(len - sizeof(struct ip6_hdr));
13860                                 udp->uh_sum = 0;
13861                         }
13862 #ifdef SCTP_DEBUG
13863                         bzero(&lsa6, sizeof(lsa6));
13864                         lsa6.sin6_len = sizeof(lsa6);
13865                         lsa6.sin6_family = AF_INET6;
13866                         lsa6.sin6_addr = out6->ip6_src;
13867                         bzero(&fsa6, sizeof(fsa6));
13868                         fsa6.sin6_len = sizeof(fsa6);
13869                         fsa6.sin6_family = AF_INET6;
13870                         fsa6.sin6_addr = out6->ip6_dst;
13871 #endif
13872                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "sctp_operr_to calling ipv6 output:\n");
13873                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "src: ");
13874                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&lsa6);
13875                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "dst ");
13876                         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&fsa6);
13877
13878 #ifdef  SCTP_PACKET_LOGGING
13879                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
13880                                 sctp_packet_log(mout, len);
13881 #endif
13882                         SCTP_ATTACH_CHAIN(o_pak, mout, len);
13883                         if (port) {
13884                                 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
13885                                         udp->uh_sum = 0xffff;
13886                                 }
13887                         }
13888                         SCTP_IP6_OUTPUT(ret, o_pak, &ro, &ifp, stcb, vrf_id);
13889
13890                         SCTP_STAT_INCR(sctps_sendpackets);
13891                         SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
13892                         /* Free the route if we got one back */
13893                         if (ro.ro_rt)
13894                                 RTFREE(ro.ro_rt);
13895                         break;
13896                 }
13897 #endif                          /* INET6 */
13898         default:
13899                 /* TSNH */
13900                 break;
13901         }
13902 }
13903
13904 static struct mbuf *
13905 sctp_copy_resume(struct sctp_stream_queue_pending *sp,
13906     struct uio *uio,
13907     struct sctp_sndrcvinfo *srcv,
13908     int max_send_len,
13909     int user_marks_eor,
13910     int *error,
13911     uint32_t * sndout,
13912     struct mbuf **new_tail)
13913 {
13914         struct mbuf *m;
13915
13916         m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
13917             (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
13918         if (m == NULL) {
13919                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13920                 *error = ENOMEM;
13921         } else {
13922                 *sndout = m_length(m, NULL);
13923                 *new_tail = m_last(m);
13924         }
13925         return (m);
13926 }
13927
13928 static int
13929 sctp_copy_one(struct sctp_stream_queue_pending *sp,
13930     struct uio *uio,
13931     int resv_upfront)
13932 {
13933         int left;
13934
13935         left = sp->length;
13936         sp->data = m_uiotombuf(uio, M_WAITOK, sp->length,
13937             resv_upfront, 0);
13938         if (sp->data == NULL) {
13939                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13940                 return (ENOMEM);
13941         }
13942         sp->tail_mbuf = m_last(sp->data);
13943         return (0);
13944 }
13945
13946
13947
13948 static struct sctp_stream_queue_pending *
13949 sctp_copy_it_in(struct sctp_tcb *stcb,
13950     struct sctp_association *asoc,
13951     struct sctp_sndrcvinfo *srcv,
13952     struct uio *uio,
13953     struct sctp_nets *net,
13954     int max_send_len,
13955     int user_marks_eor,
13956     int *error,
13957     int non_blocking)
13958 {
13959         /*-
13960          * This routine must be very careful in its work. Protocol
13961          * processing is up and running so care must be taken to spl...()
13962          * when you need to do something that may effect the stcb/asoc. The
13963          * sb is locked however. When data is copied the protocol processing
13964          * should be enabled since this is a slower operation...
13965          */
13966         struct sctp_stream_queue_pending *sp = NULL;
13967         int resv_in_first;
13968
13969         *error = 0;
13970         /* Now can we send this? */
13971         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
13972             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13973             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13974             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13975                 /* got data while shutting down */
13976                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
13977                 *error = ECONNRESET;
13978                 goto out_now;
13979         }
13980         sctp_alloc_a_strmoq(stcb, sp);
13981         if (sp == NULL) {
13982                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13983                 *error = ENOMEM;
13984                 goto out_now;
13985         }
13986         sp->act_flags = 0;
13987         sp->sender_all_done = 0;
13988         sp->sinfo_flags = srcv->sinfo_flags;
13989         sp->timetolive = srcv->sinfo_timetolive;
13990         sp->ppid = srcv->sinfo_ppid;
13991         sp->context = srcv->sinfo_context;
13992         sp->strseq = 0;
13993         (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
13994
13995         sp->stream = srcv->sinfo_stream;
13996         sp->length = min(uio->uio_resid, max_send_len);
13997         if ((sp->length == (uint32_t) uio->uio_resid) &&
13998             ((user_marks_eor == 0) ||
13999             (srcv->sinfo_flags & SCTP_EOF) ||
14000             (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
14001                 sp->msg_is_complete = 1;
14002         } else {
14003                 sp->msg_is_complete = 0;
14004         }
14005         sp->sender_all_done = 0;
14006         sp->some_taken = 0;
14007         sp->put_last_out = 0;
14008         resv_in_first = sizeof(struct sctp_data_chunk);
14009         sp->data = sp->tail_mbuf = NULL;
14010         if (sp->length == 0) {
14011                 *error = 0;
14012                 goto skip_copy;
14013         }
14014         sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
14015         if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
14016                 sctp_auth_key_acquire(stcb, stcb->asoc.authinfo.active_keyid);
14017                 sp->holds_key_ref = 1;
14018         }
14019         *error = sctp_copy_one(sp, uio, resv_in_first);
14020 skip_copy:
14021         if (*error) {
14022                 sctp_free_a_strmoq(stcb, sp);
14023                 sp = NULL;
14024         } else {
14025                 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
14026                         sp->net = net;
14027                         sp->addr_over = 1;
14028                 } else {
14029                         sp->net = asoc->primary_destination;
14030                         sp->addr_over = 0;
14031                 }
14032                 atomic_add_int(&sp->net->ref_count, 1);
14033                 sctp_set_prsctp_policy(stcb, sp);
14034         }
14035 out_now:
14036         return (sp);
14037 }
14038
14039
14040 int
14041 sctp_sosend(struct socket *so,
14042     struct sockaddr *addr,
14043     struct uio *uio,
14044     struct mbuf *top,
14045     struct mbuf *control,
14046     int flags,
14047     struct thread *p
14048 )
14049 {
14050         struct sctp_inpcb *inp;
14051         int error, use_rcvinfo = 0;
14052         struct sctp_sndrcvinfo srcv;
14053         struct sockaddr *addr_to_use;
14054
14055 #ifdef INET6
14056         struct sockaddr_in sin;
14057
14058 #endif
14059
14060         inp = (struct sctp_inpcb *)so->so_pcb;
14061         if (control) {
14062                 /* process cmsg snd/rcv info (maybe a assoc-id) */
14063                 if (sctp_find_cmsg(SCTP_SNDRCV, (void *)&srcv, control,
14064                     sizeof(srcv))) {
14065                         /* got one */
14066                         use_rcvinfo = 1;
14067                 }
14068         }
14069         addr_to_use = addr;
14070 #ifdef INET6
14071         if ((addr) && (addr->sa_family == AF_INET6)) {
14072                 struct sockaddr_in6 *sin6;
14073
14074                 sin6 = (struct sockaddr_in6 *)addr;
14075                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
14076                         in6_sin6_2_sin(&sin, sin6);
14077                         addr_to_use = (struct sockaddr *)&sin;
14078                 }
14079         }
14080 #endif
14081         error = sctp_lower_sosend(so, addr_to_use, uio, top,
14082             control,
14083             flags,
14084             use_rcvinfo, &srcv
14085             ,p
14086             );
14087         return (error);
14088 }
14089
14090
14091 int
14092 sctp_lower_sosend(struct socket *so,
14093     struct sockaddr *addr,
14094     struct uio *uio,
14095     struct mbuf *i_pak,
14096     struct mbuf *control,
14097     int flags,
14098     int use_rcvinfo,
14099     struct sctp_sndrcvinfo *srcv
14100     ,
14101     struct thread *p
14102 )
14103 {
14104         unsigned int sndlen = 0, max_len;
14105         int error, len;
14106         struct mbuf *top = NULL;
14107
14108         int queue_only = 0, queue_only_for_init = 0;
14109         int free_cnt_applied = 0;
14110         int un_sent = 0;
14111         int now_filled = 0;
14112         unsigned int inqueue_bytes = 0;
14113         struct sctp_block_entry be;
14114         struct sctp_inpcb *inp;
14115         struct sctp_tcb *stcb = NULL;
14116         struct timeval now;
14117         struct sctp_nets *net;
14118         struct sctp_association *asoc;
14119         struct sctp_inpcb *t_inp;
14120         int user_marks_eor;
14121         int create_lock_applied = 0;
14122         int nagle_applies = 0;
14123         int some_on_control = 0;
14124         int got_all_of_the_send = 0;
14125         int hold_tcblock = 0;
14126         int non_blocking = 0;
14127         int temp_flags = 0;
14128         uint32_t local_add_more, local_soresv = 0;
14129
14130         error = 0;
14131         net = NULL;
14132         stcb = NULL;
14133         asoc = NULL;
14134
14135         t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
14136         if (inp == NULL) {
14137                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14138                 error = EFAULT;
14139                 if (i_pak) {
14140                         SCTP_RELEASE_PKT(i_pak);
14141                 }
14142                 return (error);
14143         }
14144         if ((uio == NULL) && (i_pak == NULL)) {
14145                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14146                 return (EINVAL);
14147         }
14148         user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
14149         atomic_add_int(&inp->total_sends, 1);
14150         if (uio) {
14151                 if (uio->uio_resid < 0) {
14152                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14153                         return (EINVAL);
14154                 }
14155                 sndlen = uio->uio_resid;
14156         } else {
14157                 top = SCTP_HEADER_TO_CHAIN(i_pak);
14158                 sndlen = SCTP_HEADER_LEN(i_pak);
14159         }
14160         SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %d\n",
14161             addr,
14162             sndlen);
14163         /*-
14164          * Pre-screen address, if one is given the sin-len
14165          * must be set correctly!
14166          */
14167         if (addr) {
14168                 if ((addr->sa_family == AF_INET) &&
14169                     (addr->sa_len != sizeof(struct sockaddr_in))) {
14170                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14171                         error = EINVAL;
14172                         goto out_unlocked;
14173                 } else if ((addr->sa_family == AF_INET6) &&
14174                     (addr->sa_len != sizeof(struct sockaddr_in6))) {
14175                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14176                         error = EINVAL;
14177                         goto out_unlocked;
14178                 }
14179         }
14180         hold_tcblock = 0;
14181
14182         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
14183             (inp->sctp_socket->so_qlimit)) {
14184                 /* The listener can NOT send */
14185                 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14186                 error = EFAULT;
14187                 goto out_unlocked;
14188         }
14189         if ((use_rcvinfo) && srcv) {
14190                 if (INVALID_SINFO_FLAG(srcv->sinfo_flags) ||
14191                     PR_SCTP_INVALID_POLICY(srcv->sinfo_flags)) {
14192                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14193                         error = EINVAL;
14194                         goto out_unlocked;
14195                 }
14196                 if (srcv->sinfo_flags)
14197                         SCTP_STAT_INCR(sctps_sends_with_flags);
14198
14199                 if (srcv->sinfo_flags & SCTP_SENDALL) {
14200                         /* its a sendall */
14201                         error = sctp_sendall(inp, uio, top, srcv);
14202                         top = NULL;
14203                         goto out_unlocked;
14204                 }
14205         }
14206         /* now we must find the assoc */
14207         if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
14208             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
14209                 SCTP_INP_RLOCK(inp);
14210                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
14211                 if (stcb == NULL) {
14212                         SCTP_INP_RUNLOCK(inp);
14213                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
14214                         error = ENOTCONN;
14215                         goto out_unlocked;
14216                 }
14217                 hold_tcblock = 0;
14218                 SCTP_INP_RUNLOCK(inp);
14219                 if (addr) {
14220                         /* Must locate the net structure if addr given */
14221                         net = sctp_findnet(stcb, addr);
14222                         if (net) {
14223                                 /* validate port was 0 or correct */
14224                                 struct sockaddr_in *sin;
14225
14226                                 sin = (struct sockaddr_in *)addr;
14227                                 if ((sin->sin_port != 0) &&
14228                                     (sin->sin_port != stcb->rport)) {
14229                                         net = NULL;
14230                                 }
14231                         }
14232                         temp_flags |= SCTP_ADDR_OVER;
14233                 } else
14234                         net = stcb->asoc.primary_destination;
14235                 if (addr && (net == NULL)) {
14236                         /* Could not find address, was it legal */
14237                         if (addr->sa_family == AF_INET) {
14238                                 struct sockaddr_in *sin;
14239
14240                                 sin = (struct sockaddr_in *)addr;
14241                                 if (sin->sin_addr.s_addr == 0) {
14242                                         if ((sin->sin_port == 0) ||
14243                                             (sin->sin_port == stcb->rport)) {
14244                                                 net = stcb->asoc.primary_destination;
14245                                         }
14246                                 }
14247                         } else {
14248                                 struct sockaddr_in6 *sin6;
14249
14250                                 sin6 = (struct sockaddr_in6 *)addr;
14251                                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
14252                                         if ((sin6->sin6_port == 0) ||
14253                                             (sin6->sin6_port == stcb->rport)) {
14254                                                 net = stcb->asoc.primary_destination;
14255                                         }
14256                                 }
14257                         }
14258                 }
14259                 if (net == NULL) {
14260                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14261                         error = EINVAL;
14262                         goto out_unlocked;
14263                 }
14264         } else if (use_rcvinfo && srcv && srcv->sinfo_assoc_id) {
14265                 stcb = sctp_findassociation_ep_asocid(inp, srcv->sinfo_assoc_id, 0);
14266                 if (stcb) {
14267                         if (addr)
14268                                 /*
14269                                  * Must locate the net structure if addr
14270                                  * given
14271                                  */
14272                                 net = sctp_findnet(stcb, addr);
14273                         else
14274                                 net = stcb->asoc.primary_destination;
14275                         if ((srcv->sinfo_flags & SCTP_ADDR_OVER) &&
14276                             ((net == NULL) || (addr == NULL))) {
14277                                 struct sockaddr_in *sin;
14278
14279                                 if (addr == NULL) {
14280                                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14281                                         error = EINVAL;
14282                                         goto out_unlocked;
14283                                 }
14284                                 sin = (struct sockaddr_in *)addr;
14285                                 /* Validate port is 0 or correct */
14286                                 if ((sin->sin_port != 0) &&
14287                                     (sin->sin_port != stcb->rport)) {
14288                                         net = NULL;
14289                                 }
14290                         }
14291                 }
14292                 hold_tcblock = 0;
14293         } else if (addr) {
14294                 /*-
14295                  * Since we did not use findep we must
14296                  * increment it, and if we don't find a tcb
14297                  * decrement it.
14298                  */
14299                 SCTP_INP_WLOCK(inp);
14300                 SCTP_INP_INCR_REF(inp);
14301                 SCTP_INP_WUNLOCK(inp);
14302                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
14303                 if (stcb == NULL) {
14304                         SCTP_INP_WLOCK(inp);
14305                         SCTP_INP_DECR_REF(inp);
14306                         SCTP_INP_WUNLOCK(inp);
14307                 } else {
14308                         hold_tcblock = 1;
14309                 }
14310         }
14311         if ((stcb == NULL) && (addr)) {
14312                 /* Possible implicit send? */
14313                 SCTP_ASOC_CREATE_LOCK(inp);
14314                 create_lock_applied = 1;
14315                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
14316                     (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
14317                         /* Should I really unlock ? */
14318                         SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14319                         error = EFAULT;
14320                         goto out_unlocked;
14321
14322                 }
14323                 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
14324                     (addr->sa_family == AF_INET6)) {
14325                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14326                         error = EINVAL;
14327                         goto out_unlocked;
14328                 }
14329                 SCTP_INP_WLOCK(inp);
14330                 SCTP_INP_INCR_REF(inp);
14331                 SCTP_INP_WUNLOCK(inp);
14332                 /* With the lock applied look again */
14333                 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
14334                 if (stcb == NULL) {
14335                         SCTP_INP_WLOCK(inp);
14336                         SCTP_INP_DECR_REF(inp);
14337                         SCTP_INP_WUNLOCK(inp);
14338                 } else {
14339                         hold_tcblock = 1;
14340                 }
14341                 if (t_inp != inp) {
14342                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOTCONN);
14343                         error = ENOTCONN;
14344                         goto out_unlocked;
14345                 }
14346         }
14347         if (stcb == NULL) {
14348                 if (addr == NULL) {
14349                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
14350                         error = ENOENT;
14351                         goto out_unlocked;
14352                 } else {
14353                         /*
14354                          * UDP style, we must go ahead and start the INIT
14355                          * process
14356                          */
14357                         uint32_t vrf_id;
14358
14359                         if ((use_rcvinfo) && (srcv) &&
14360                             ((srcv->sinfo_flags & SCTP_ABORT) ||
14361                             ((srcv->sinfo_flags & SCTP_EOF) &&
14362                             (sndlen == 0)))) {
14363                                 /*-
14364                                  * User asks to abort a non-existant assoc,
14365                                  * or EOF a non-existant assoc with no data
14366                                  */
14367                                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOENT);
14368                                 error = ENOENT;
14369                                 goto out_unlocked;
14370                         }
14371                         /* get an asoc/stcb struct */
14372                         vrf_id = inp->def_vrf_id;
14373 #ifdef INVARIANTS
14374                         if (create_lock_applied == 0) {
14375                                 panic("Error, should hold create lock and I don't?");
14376                         }
14377 #endif
14378                         stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0, vrf_id,
14379                             p
14380                             );
14381                         if (stcb == NULL) {
14382                                 /* Error is setup for us in the call */
14383                                 goto out_unlocked;
14384                         }
14385                         if (create_lock_applied) {
14386                                 SCTP_ASOC_CREATE_UNLOCK(inp);
14387                                 create_lock_applied = 0;
14388                         } else {
14389                                 SCTP_PRINTF("Huh-3? create lock should have been on??\n");
14390                         }
14391                         /*
14392                          * Turn on queue only flag to prevent data from
14393                          * being sent
14394                          */
14395                         queue_only = 1;
14396                         asoc = &stcb->asoc;
14397                         SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
14398                         (void)SCTP_GETTIME_TIMEVAL(&asoc->time_entered);
14399
14400                         /* initialize authentication params for the assoc */
14401                         sctp_initialize_auth_params(inp, stcb);
14402
14403                         if (control) {
14404                                 /*
14405                                  * see if a init structure exists in cmsg
14406                                  * headers
14407                                  */
14408                                 struct sctp_initmsg initm;
14409                                 int i;
14410
14411                                 if (sctp_find_cmsg(SCTP_INIT, (void *)&initm, control,
14412                                     sizeof(initm))) {
14413                                         /*
14414                                          * we have an INIT override of the
14415                                          * default
14416                                          */
14417                                         if (initm.sinit_max_attempts)
14418                                                 asoc->max_init_times = initm.sinit_max_attempts;
14419                                         if (initm.sinit_num_ostreams)
14420                                                 asoc->pre_open_streams = initm.sinit_num_ostreams;
14421                                         if (initm.sinit_max_instreams)
14422                                                 asoc->max_inbound_streams = initm.sinit_max_instreams;
14423                                         if (initm.sinit_max_init_timeo)
14424                                                 asoc->initial_init_rto_max = initm.sinit_max_init_timeo;
14425                                         if (asoc->streamoutcnt < asoc->pre_open_streams) {
14426                                                 struct sctp_stream_out *tmp_str;
14427                                                 int had_lock = 0;
14428
14429                                                 /* Default is NOT correct */
14430                                                 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, defout:%d pre_open:%d\n",
14431                                                     asoc->streamoutcnt, asoc->pre_open_streams);
14432                                                 /*
14433                                                  * What happens if this
14434                                                  * fails? we panic ...
14435                                                  */
14436
14437                                                 if (hold_tcblock) {
14438                                                         had_lock = 1;
14439                                                         SCTP_TCB_UNLOCK(stcb);
14440                                                 }
14441                                                 SCTP_MALLOC(tmp_str,
14442                                                     struct sctp_stream_out *,
14443                                                     (asoc->pre_open_streams *
14444                                                     sizeof(struct sctp_stream_out)),
14445                                                     SCTP_M_STRMO);
14446                                                 if (had_lock) {
14447                                                         SCTP_TCB_LOCK(stcb);
14448                                                 }
14449                                                 if (tmp_str != NULL) {
14450                                                         SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
14451                                                         asoc->strmout = tmp_str;
14452                                                         asoc->streamoutcnt = asoc->pre_open_streams;
14453                                                 } else {
14454                                                         asoc->pre_open_streams = asoc->streamoutcnt;
14455                                                 }
14456                                                 for (i = 0; i < asoc->streamoutcnt; i++) {
14457                                                         /*-
14458                                                          * inbound side must be set
14459                                                          * to 0xffff, also NOTE when
14460                                                          * we get the INIT-ACK back
14461                                                          * (for INIT sender) we MUST
14462                                                          * reduce the count
14463                                                          * (streamoutcnt) but first
14464                                                          * check if we sent to any
14465                                                          * of the upper streams that
14466                                                          * were dropped (if some
14467                                                          * were). Those that were
14468                                                          * dropped must be notified
14469                                                          * to the upper layer as
14470                                                          * failed to send.
14471                                                          */
14472                                                         asoc->strmout[i].next_sequence_sent = 0x0;
14473                                                         TAILQ_INIT(&asoc->strmout[i].outqueue);
14474                                                         asoc->strmout[i].stream_no = i;
14475                                                         asoc->strmout[i].last_msg_incomplete = 0;
14476                                                         asoc->strmout[i].next_spoke.tqe_next = 0;
14477                                                         asoc->strmout[i].next_spoke.tqe_prev = 0;
14478                                                 }
14479                                         }
14480                                 }
14481                         }
14482                         hold_tcblock = 1;
14483                         /* out with the INIT */
14484                         queue_only_for_init = 1;
14485                         /*-
14486                          * we may want to dig in after this call and adjust the MTU
14487                          * value. It defaulted to 1500 (constant) but the ro
14488                          * structure may now have an update and thus we may need to
14489                          * change it BEFORE we append the message.
14490                          */
14491                         net = stcb->asoc.primary_destination;
14492                         asoc = &stcb->asoc;
14493                 }
14494         }
14495         if ((SCTP_SO_IS_NBIO(so)
14496             || (flags & MSG_NBIO)
14497             )) {
14498                 non_blocking = 1;
14499         }
14500         asoc = &stcb->asoc;
14501         atomic_add_int(&stcb->total_sends, 1);
14502
14503         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
14504                 if (sndlen > asoc->smallest_mtu) {
14505                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14506                         error = EMSGSIZE;
14507                         goto out_unlocked;
14508                 }
14509         }
14510         /* would we block? */
14511         if (non_blocking) {
14512                 if (hold_tcblock == 0) {
14513                         SCTP_TCB_LOCK(stcb);
14514                         hold_tcblock = 1;
14515                 }
14516                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14517                 if ((SCTP_SB_LIMIT_SND(so) < (sndlen + inqueue_bytes + stcb->asoc.sb_send_resv)) ||
14518                     (stcb->asoc.chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14519                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EWOULDBLOCK);
14520                         if (sndlen > SCTP_SB_LIMIT_SND(so))
14521                                 error = EMSGSIZE;
14522                         else
14523                                 error = EWOULDBLOCK;
14524                         goto out_unlocked;
14525                 }
14526                 stcb->asoc.sb_send_resv += sndlen;
14527                 SCTP_TCB_UNLOCK(stcb);
14528                 hold_tcblock = 0;
14529         } else {
14530                 atomic_add_int(&stcb->asoc.sb_send_resv, sndlen);
14531         }
14532         local_soresv = sndlen;
14533         /* Keep the stcb from being freed under our feet */
14534         if (free_cnt_applied) {
14535 #ifdef INVARIANTS
14536                 panic("refcnt already incremented");
14537 #else
14538                 printf("refcnt:1 already incremented?\n");
14539 #endif
14540         } else {
14541                 atomic_add_int(&stcb->asoc.refcnt, 1);
14542                 free_cnt_applied = 1;
14543         }
14544         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14545                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14546                 error = ECONNRESET;
14547                 goto out_unlocked;
14548         }
14549         if (create_lock_applied) {
14550                 SCTP_ASOC_CREATE_UNLOCK(inp);
14551                 create_lock_applied = 0;
14552         }
14553         if (asoc->stream_reset_outstanding) {
14554                 /*
14555                  * Can't queue any data while stream reset is underway.
14556                  */
14557                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EAGAIN);
14558                 error = EAGAIN;
14559                 goto out_unlocked;
14560         }
14561         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
14562             (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
14563                 queue_only = 1;
14564         }
14565         if ((use_rcvinfo == 0) || (srcv == NULL)) {
14566                 /* Grab the default stuff from the asoc */
14567                 srcv = (struct sctp_sndrcvinfo *)&stcb->asoc.def_send;
14568         }
14569         /* we are now done with all control */
14570         if (control) {
14571                 sctp_m_freem(control);
14572                 control = NULL;
14573         }
14574         if ((SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_SENT) ||
14575             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
14576             (SCTP_GET_STATE(asoc) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
14577             (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
14578                 if ((use_rcvinfo) &&
14579                     (srcv->sinfo_flags & SCTP_ABORT)) {
14580                         ;
14581                 } else {
14582                         SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14583                         error = ECONNRESET;
14584                         goto out_unlocked;
14585                 }
14586         }
14587         /* Ok, we will attempt a msgsnd :> */
14588         if (p) {
14589                 p->td_ru.ru_msgsnd++;
14590         }
14591         if (stcb) {
14592                 if (((srcv->sinfo_flags | temp_flags) & SCTP_ADDR_OVER) == 0) {
14593                         net = stcb->asoc.primary_destination;
14594                 }
14595         }
14596         if (net == NULL) {
14597                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14598                 error = EINVAL;
14599                 goto out_unlocked;
14600         }
14601         if ((net->flight_size > net->cwnd) && (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
14602                 /*-
14603                  * CMT: Added check for CMT above. net above is the primary
14604                  * dest. If CMT is ON, sender should always attempt to send
14605                  * with the output routine sctp_fill_outqueue() that loops
14606                  * through all destination addresses. Therefore, if CMT is
14607                  * ON, queue_only is NOT set to 1 here, so that
14608                  * sctp_chunk_output() can be called below.
14609                  */
14610                 queue_only = 1;
14611         } else if (asoc->ifp_had_enobuf) {
14612                 SCTP_STAT_INCR(sctps_ifnomemqueued);
14613                 if (net->flight_size > (net->mtu * 2))
14614                         queue_only = 1;
14615                 asoc->ifp_had_enobuf = 0;
14616         } else {
14617                 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
14618                     (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
14619         }
14620         /* Are we aborting? */
14621         if (srcv->sinfo_flags & SCTP_ABORT) {
14622                 struct mbuf *mm;
14623                 int tot_demand, tot_out = 0, max_out;
14624
14625                 SCTP_STAT_INCR(sctps_sends_with_abort);
14626                 if ((SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_WAIT) ||
14627                     (SCTP_GET_STATE(asoc) == SCTP_STATE_COOKIE_ECHOED)) {
14628                         /* It has to be up before we abort */
14629                         /* how big is the user initiated abort? */
14630                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14631                         error = EINVAL;
14632                         goto out;
14633                 }
14634                 if (hold_tcblock) {
14635                         SCTP_TCB_UNLOCK(stcb);
14636                         hold_tcblock = 0;
14637                 }
14638                 if (top) {
14639                         struct mbuf *cntm = NULL;
14640
14641                         mm = sctp_get_mbuf_for_msg(1, 0, M_WAIT, 1, MT_DATA);
14642                         if (sndlen != 0) {
14643                                 cntm = top;
14644                                 while (cntm) {
14645                                         tot_out += SCTP_BUF_LEN(cntm);
14646                                         cntm = SCTP_BUF_NEXT(cntm);
14647                                 }
14648                         }
14649                         tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
14650                 } else {
14651                         /* Must fit in a MTU */
14652                         tot_out = sndlen;
14653                         tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
14654                         if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
14655                                 /* To big */
14656                                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14657                                 error = EMSGSIZE;
14658                                 goto out;
14659                         }
14660                         mm = sctp_get_mbuf_for_msg(tot_demand, 0, M_WAIT, 1, MT_DATA);
14661                 }
14662                 if (mm == NULL) {
14663                         SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
14664                         error = ENOMEM;
14665                         goto out;
14666                 }
14667                 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
14668                 max_out -= sizeof(struct sctp_abort_msg);
14669                 if (tot_out > max_out) {
14670                         tot_out = max_out;
14671                 }
14672                 if (mm) {
14673                         struct sctp_paramhdr *ph;
14674
14675                         /* now move forward the data pointer */
14676                         ph = mtod(mm, struct sctp_paramhdr *);
14677                         ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
14678                         ph->param_length = htons((sizeof(struct sctp_paramhdr) + tot_out));
14679                         ph++;
14680                         SCTP_BUF_LEN(mm) = tot_out + sizeof(struct sctp_paramhdr);
14681                         if (top == NULL) {
14682                                 error = uiomove((caddr_t)ph, (int)tot_out, uio);
14683                                 if (error) {
14684                                         /*-
14685                                          * Here if we can't get his data we
14686                                          * still abort we just don't get to
14687                                          * send the users note :-0
14688                                          */
14689                                         sctp_m_freem(mm);
14690                                         mm = NULL;
14691                                 }
14692                         } else {
14693                                 if (sndlen != 0) {
14694                                         SCTP_BUF_NEXT(mm) = top;
14695                                 }
14696                         }
14697                 }
14698                 if (hold_tcblock == 0) {
14699                         SCTP_TCB_LOCK(stcb);
14700                         hold_tcblock = 1;
14701                 }
14702                 atomic_add_int(&stcb->asoc.refcnt, -1);
14703                 free_cnt_applied = 0;
14704                 /* release this lock, otherwise we hang on ourselves */
14705                 sctp_abort_an_association(stcb->sctp_ep, stcb,
14706                     SCTP_RESPONSE_TO_USER_REQ,
14707                     mm, SCTP_SO_LOCKED);
14708                 /* now relock the stcb so everything is sane */
14709                 hold_tcblock = 0;
14710                 stcb = NULL;
14711                 /*
14712                  * In this case top is already chained to mm avoid double
14713                  * free, since we free it below if top != NULL and driver
14714                  * would free it after sending the packet out
14715                  */
14716                 if (sndlen != 0) {
14717                         top = NULL;
14718                 }
14719                 goto out_unlocked;
14720         }
14721         /* Calculate the maximum we can send */
14722         inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14723         if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14724                 if (non_blocking) {
14725                         /* we already checked for non-blocking above. */
14726                         max_len = sndlen;
14727                 } else {
14728                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14729                 }
14730         } else {
14731                 max_len = 0;
14732         }
14733         if (hold_tcblock) {
14734                 SCTP_TCB_UNLOCK(stcb);
14735                 hold_tcblock = 0;
14736         }
14737         /* Is the stream no. valid? */
14738         if (srcv->sinfo_stream >= asoc->streamoutcnt) {
14739                 /* Invalid stream number */
14740                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14741                 error = EINVAL;
14742                 goto out_unlocked;
14743         }
14744         if (asoc->strmout == NULL) {
14745                 /* huh? software error */
14746                 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
14747                 error = EFAULT;
14748                 goto out_unlocked;
14749         }
14750         /* Unless E_EOR mode is on, we must make a send FIT in one call. */
14751         if ((user_marks_eor == 0) &&
14752             (sndlen > SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
14753                 /* It will NEVER fit */
14754                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EMSGSIZE);
14755                 error = EMSGSIZE;
14756                 goto out_unlocked;
14757         }
14758         if ((uio == NULL) && user_marks_eor) {
14759                 /*-
14760                  * We do not support eeor mode for
14761                  * sending with mbuf chains (like sendfile).
14762                  */
14763                 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14764                 error = EINVAL;
14765                 goto out_unlocked;
14766         }
14767         if (user_marks_eor) {
14768                 local_add_more = min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
14769         } else {
14770                 /*-
14771                  * For non-eeor the whole message must fit in
14772                  * the socket send buffer.
14773                  */
14774                 local_add_more = sndlen;
14775         }
14776         len = 0;
14777         if (non_blocking) {
14778                 goto skip_preblock;
14779         }
14780         if (((max_len <= local_add_more) &&
14781             (SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
14782             (max_len == 0) ||
14783             ((stcb->asoc.chunks_on_out_queue + stcb->asoc.stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14784                 /* No room right now ! */
14785                 SOCKBUF_LOCK(&so->so_snd);
14786                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14787                 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
14788                     ((stcb->asoc.stream_queue_cnt + stcb->asoc.chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14789                         SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %d) || (%d+%d > %d)\n",
14790                             (unsigned int)SCTP_SB_LIMIT_SND(so),
14791                             inqueue_bytes,
14792                             local_add_more,
14793                             stcb->asoc.stream_queue_cnt,
14794                             stcb->asoc.chunks_on_out_queue,
14795                             SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
14796                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14797                                 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, so, asoc, sndlen);
14798                         }
14799                         be.error = 0;
14800                         stcb->block_entry = &be;
14801                         error = sbwait(&so->so_snd);
14802                         stcb->block_entry = NULL;
14803                         if (error || so->so_error || be.error) {
14804                                 if (error == 0) {
14805                                         if (so->so_error)
14806                                                 error = so->so_error;
14807                                         if (be.error) {
14808                                                 error = be.error;
14809                                         }
14810                                 }
14811                                 SOCKBUF_UNLOCK(&so->so_snd);
14812                                 goto out_unlocked;
14813                         }
14814                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14815                                 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
14816                                     so, asoc, stcb->asoc.total_output_queue_size);
14817                         }
14818                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14819                                 goto out_unlocked;
14820                         }
14821                         inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14822                 }
14823                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
14824                 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14825                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14826                 } else {
14827                         max_len = 0;
14828                 }
14829                 SOCKBUF_UNLOCK(&so->so_snd);
14830         }
14831 skip_preblock:
14832         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14833                 goto out_unlocked;
14834         }
14835         /*
14836          * sndlen covers for mbuf case uio_resid covers for the non-mbuf
14837          * case NOTE: uio will be null when top/mbuf is passed
14838          */
14839         if (sndlen == 0) {
14840                 if (srcv->sinfo_flags & SCTP_EOF) {
14841                         got_all_of_the_send = 1;
14842                         goto dataless_eof;
14843                 } else {
14844                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14845                         error = EINVAL;
14846                         goto out;
14847                 }
14848         }
14849         if (top == NULL) {
14850                 struct sctp_stream_queue_pending *sp;
14851                 struct sctp_stream_out *strm;
14852                 uint32_t sndout, initial_out;
14853
14854                 initial_out = uio->uio_resid;
14855
14856                 SCTP_TCB_SEND_LOCK(stcb);
14857                 if ((asoc->stream_locked) &&
14858                     (asoc->stream_locked_on != srcv->sinfo_stream)) {
14859                         SCTP_TCB_SEND_UNLOCK(stcb);
14860                         SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
14861                         error = EINVAL;
14862                         goto out;
14863                 }
14864                 SCTP_TCB_SEND_UNLOCK(stcb);
14865
14866                 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
14867                 if (strm->last_msg_incomplete == 0) {
14868         do_a_copy_in:
14869                         sp = sctp_copy_it_in(stcb, asoc, srcv, uio, net, max_len, user_marks_eor, &error, non_blocking);
14870                         if ((sp == NULL) || (error)) {
14871                                 goto out;
14872                         }
14873                         SCTP_TCB_SEND_LOCK(stcb);
14874                         if (sp->msg_is_complete) {
14875                                 strm->last_msg_incomplete = 0;
14876                                 asoc->stream_locked = 0;
14877                         } else {
14878                                 /*
14879                                  * Just got locked to this guy in case of an
14880                                  * interrupt.
14881                                  */
14882                                 strm->last_msg_incomplete = 1;
14883                                 asoc->stream_locked = 1;
14884                                 asoc->stream_locked_on = srcv->sinfo_stream;
14885                                 sp->sender_all_done = 0;
14886                         }
14887                         sctp_snd_sb_alloc(stcb, sp->length);
14888                         atomic_add_int(&asoc->stream_queue_cnt, 1);
14889                         if ((srcv->sinfo_flags & SCTP_UNORDERED) == 0) {
14890                                 sp->strseq = strm->next_sequence_sent;
14891                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_SCTP) {
14892                                         sctp_misc_ints(SCTP_STRMOUT_LOG_ASSIGN,
14893                                             (uintptr_t) stcb, sp->length,
14894                                             (uint32_t) ((srcv->sinfo_stream << 16) | sp->strseq), 0);
14895                                 }
14896                                 strm->next_sequence_sent++;
14897                         } else {
14898                                 SCTP_STAT_INCR(sctps_sends_with_unord);
14899                         }
14900                         TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
14901                         if ((strm->next_spoke.tqe_next == NULL) &&
14902                             (strm->next_spoke.tqe_prev == NULL)) {
14903                                 /* Not on wheel, insert */
14904                                 sctp_insert_on_wheel(stcb, asoc, strm, 1);
14905                         }
14906                         SCTP_TCB_SEND_UNLOCK(stcb);
14907                 } else {
14908                         SCTP_TCB_SEND_LOCK(stcb);
14909                         sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
14910                         SCTP_TCB_SEND_UNLOCK(stcb);
14911                         if (sp == NULL) {
14912                                 /* ???? Huh ??? last msg is gone */
14913 #ifdef INVARIANTS
14914                                 panic("Warning: Last msg marked incomplete, yet nothing left?");
14915 #else
14916                                 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
14917                                 strm->last_msg_incomplete = 0;
14918 #endif
14919                                 goto do_a_copy_in;
14920
14921                         }
14922                 }
14923                 while (uio->uio_resid > 0) {
14924                         /* How much room do we have? */
14925                         struct mbuf *new_tail, *mm;
14926
14927                         if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
14928                                 max_len = SCTP_SB_LIMIT_SND(so) - stcb->asoc.total_output_queue_size;
14929                         else
14930                                 max_len = 0;
14931
14932                         if ((max_len > SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
14933                             (max_len && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
14934                             (uio->uio_resid && (uio->uio_resid <= (int)max_len))) {
14935                                 sndout = 0;
14936                                 new_tail = NULL;
14937                                 if (hold_tcblock) {
14938                                         SCTP_TCB_UNLOCK(stcb);
14939                                         hold_tcblock = 0;
14940                                 }
14941                                 mm = sctp_copy_resume(sp, uio, srcv, max_len, user_marks_eor, &error, &sndout, &new_tail);
14942                                 if ((mm == NULL) || error) {
14943                                         if (mm) {
14944                                                 sctp_m_freem(mm);
14945                                         }
14946                                         goto out;
14947                                 }
14948                                 /* Update the mbuf and count */
14949                                 SCTP_TCB_SEND_LOCK(stcb);
14950                                 if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
14951                                         /*
14952                                          * we need to get out. Peer probably
14953                                          * aborted.
14954                                          */
14955                                         sctp_m_freem(mm);
14956                                         if (stcb->asoc.state & SCTP_PCB_FLAGS_WAS_ABORTED) {
14957                                                 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ECONNRESET);
14958                                                 error = ECONNRESET;
14959                                         }
14960                                         SCTP_TCB_SEND_UNLOCK(stcb);
14961                                         goto out;
14962                                 }
14963                                 if (sp->tail_mbuf) {
14964                                         /* tack it to the end */
14965                                         SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
14966                                         sp->tail_mbuf = new_tail;
14967                                 } else {
14968                                         /* A stolen mbuf */
14969                                         sp->data = mm;
14970                                         sp->tail_mbuf = new_tail;
14971                                 }
14972                                 sctp_snd_sb_alloc(stcb, sndout);
14973                                 atomic_add_int(&sp->length, sndout);
14974                                 len += sndout;
14975
14976                                 /* Did we reach EOR? */
14977                                 if ((uio->uio_resid == 0) &&
14978                                     ((user_marks_eor == 0) ||
14979                                     (srcv->sinfo_flags & SCTP_EOF) ||
14980                                     (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
14981                                         sp->msg_is_complete = 1;
14982                                 } else {
14983                                         sp->msg_is_complete = 0;
14984                                 }
14985                                 SCTP_TCB_SEND_UNLOCK(stcb);
14986                         }
14987                         if (uio->uio_resid == 0) {
14988                                 /* got it all? */
14989                                 continue;
14990                         }
14991                         /* PR-SCTP? */
14992                         if ((asoc->peer_supports_prsctp) && (asoc->sent_queue_cnt_removeable > 0)) {
14993                                 /*
14994                                  * This is ugly but we must assure locking
14995                                  * order
14996                                  */
14997                                 if (hold_tcblock == 0) {
14998                                         SCTP_TCB_LOCK(stcb);
14999                                         hold_tcblock = 1;
15000                                 }
15001                                 sctp_prune_prsctp(stcb, asoc, srcv, sndlen);
15002                                 inqueue_bytes = stcb->asoc.total_output_queue_size - (stcb->asoc.chunks_on_out_queue * sizeof(struct sctp_data_chunk));
15003                                 if (SCTP_SB_LIMIT_SND(so) > stcb->asoc.total_output_queue_size)
15004                                         max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
15005                                 else
15006                                         max_len = 0;
15007                                 if (max_len > 0) {
15008                                         continue;
15009                                 }
15010                                 SCTP_TCB_UNLOCK(stcb);
15011                                 hold_tcblock = 0;
15012                         }
15013                         /* wait for space now */
15014                         if (non_blocking) {
15015                                 /* Non-blocking io in place out */
15016                                 goto skip_out_eof;
15017                         }
15018                         if ((net->flight_size > net->cwnd) &&
15019                             (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
15020                                 queue_only = 1;
15021                         } else if (asoc->ifp_had_enobuf) {
15022                                 SCTP_STAT_INCR(sctps_ifnomemqueued);
15023                                 if (net->flight_size > (net->mtu * 2)) {
15024                                         queue_only = 1;
15025                                 } else {
15026                                         queue_only = 0;
15027                                 }
15028                                 asoc->ifp_had_enobuf = 0;
15029                                 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15030                                     (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15031                         } else {
15032                                 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15033                                     (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15034                                 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
15035                                         queue_only = 1;
15036                                         SCTP_STAT_INCR(sctps_send_burst_avoid);
15037                                 } else if (net->flight_size > net->cwnd) {
15038                                         queue_only = 1;
15039                                         SCTP_STAT_INCR(sctps_send_cwnd_avoid);
15040                                 } else {
15041                                         queue_only = 0;
15042                                 }
15043                         }
15044                         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
15045                             (stcb->asoc.total_flight > 0) &&
15046                             (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
15047                             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
15048
15049                                 /*-
15050                                  * Ok, Nagle is set on and we have data outstanding.
15051                                  * Don't send anything and let SACKs drive out the
15052                                  * data unless wen have a "full" segment to send.
15053                                  */
15054                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15055                                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
15056                                 }
15057                                 SCTP_STAT_INCR(sctps_naglequeued);
15058                                 nagle_applies = 1;
15059                         } else {
15060                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15061                                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
15062                                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
15063                                 }
15064                                 SCTP_STAT_INCR(sctps_naglesent);
15065                                 nagle_applies = 0;
15066                         }
15067                         /* What about the INIT, send it maybe */
15068                         if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15069
15070                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
15071                                     nagle_applies, un_sent);
15072                                 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, stcb->asoc.total_output_queue_size,
15073                                     stcb->asoc.total_flight,
15074                                     stcb->asoc.chunks_on_out_queue, stcb->asoc.total_flight_count);
15075                         }
15076                         if (queue_only_for_init) {
15077                                 if (hold_tcblock == 0) {
15078                                         SCTP_TCB_LOCK(stcb);
15079                                         hold_tcblock = 1;
15080                                 }
15081                                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
15082                                         /* a collision took us forward? */
15083                                         queue_only_for_init = 0;
15084                                         queue_only = 0;
15085                                 } else {
15086                                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
15087                                         SCTP_SET_STATE(asoc, SCTP_STATE_COOKIE_WAIT);
15088                                         queue_only_for_init = 0;
15089                                         queue_only = 1;
15090                                 }
15091                         }
15092                         if ((queue_only == 0) && (nagle_applies == 0)) {
15093                                 /*-
15094                                  * need to start chunk output
15095                                  * before blocking.. note that if
15096                                  * a lock is already applied, then
15097                                  * the input via the net is happening
15098                                  * and I don't need to start output :-D
15099                                  */
15100                                 if (hold_tcblock == 0) {
15101                                         if (SCTP_TCB_TRYLOCK(stcb)) {
15102                                                 hold_tcblock = 1;
15103                                                 sctp_chunk_output(inp,
15104                                                     stcb,
15105                                                     SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15106                                         }
15107                                 } else {
15108                                         sctp_chunk_output(inp,
15109                                             stcb,
15110                                             SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15111                                 }
15112                                 if (hold_tcblock == 1) {
15113                                         SCTP_TCB_UNLOCK(stcb);
15114                                         hold_tcblock = 0;
15115                                 }
15116                         }
15117                         SOCKBUF_LOCK(&so->so_snd);
15118                         /*-
15119                          * This is a bit strange, but I think it will
15120                          * work. The total_output_queue_size is locked and
15121                          * protected by the TCB_LOCK, which we just released.
15122                          * There is a race that can occur between releasing it
15123                          * above, and me getting the socket lock, where sacks
15124                          * come in but we have not put the SB_WAIT on the
15125                          * so_snd buffer to get the wakeup. After the LOCK
15126                          * is applied the sack_processing will also need to
15127                          * LOCK the so->so_snd to do the actual sowwakeup(). So
15128                          * once we have the socket buffer lock if we recheck the
15129                          * size we KNOW we will get to sleep safely with the
15130                          * wakeup flag in place.
15131                          */
15132                         if (SCTP_SB_LIMIT_SND(so) <= (stcb->asoc.total_output_queue_size +
15133                             min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
15134                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15135                                         sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
15136                                             so, asoc, uio->uio_resid);
15137                                 }
15138                                 be.error = 0;
15139                                 stcb->block_entry = &be;
15140                                 error = sbwait(&so->so_snd);
15141                                 stcb->block_entry = NULL;
15142
15143                                 if (error || so->so_error || be.error) {
15144                                         if (error == 0) {
15145                                                 if (so->so_error)
15146                                                         error = so->so_error;
15147                                                 if (be.error) {
15148                                                         error = be.error;
15149                                                 }
15150                                         }
15151                                         SOCKBUF_UNLOCK(&so->so_snd);
15152                                         goto out_unlocked;
15153                                 }
15154                                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
15155                                         sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
15156                                             so, asoc, stcb->asoc.total_output_queue_size);
15157                                 }
15158                         }
15159                         SOCKBUF_UNLOCK(&so->so_snd);
15160                         if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
15161                                 goto out_unlocked;
15162                         }
15163                 }
15164                 SCTP_TCB_SEND_LOCK(stcb);
15165                 if (sp) {
15166                         if (sp->msg_is_complete == 0) {
15167                                 strm->last_msg_incomplete = 1;
15168                                 asoc->stream_locked = 1;
15169                                 asoc->stream_locked_on = srcv->sinfo_stream;
15170                         } else {
15171                                 sp->sender_all_done = 1;
15172                                 strm->last_msg_incomplete = 0;
15173                                 asoc->stream_locked = 0;
15174                         }
15175                 } else {
15176                         SCTP_PRINTF("Huh no sp TSNH?\n");
15177                         strm->last_msg_incomplete = 0;
15178                         asoc->stream_locked = 0;
15179                 }
15180                 SCTP_TCB_SEND_UNLOCK(stcb);
15181                 if (uio->uio_resid == 0) {
15182                         got_all_of_the_send = 1;
15183                 }
15184         } else if (top) {
15185                 /* We send in a 0, since we do NOT have any locks */
15186                 error = sctp_msg_append(stcb, net, top, srcv, 0);
15187                 top = NULL;
15188                 if (srcv->sinfo_flags & SCTP_EOF) {
15189                         /*
15190                          * This should only happen for Panda for the mbuf
15191                          * send case, which does NOT yet support EEOR mode.
15192                          * Thus, we can just set this flag to do the proper
15193                          * EOF handling.
15194                          */
15195                         got_all_of_the_send = 1;
15196                 }
15197         }
15198         if (error) {
15199                 goto out;
15200         }
15201 dataless_eof:
15202         /* EOF thing ? */
15203         if ((srcv->sinfo_flags & SCTP_EOF) &&
15204             (got_all_of_the_send == 1) &&
15205             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
15206                 int cnt;
15207
15208                 SCTP_STAT_INCR(sctps_sends_with_eof);
15209                 error = 0;
15210                 if (hold_tcblock == 0) {
15211                         SCTP_TCB_LOCK(stcb);
15212                         hold_tcblock = 1;
15213                 }
15214                 cnt = sctp_is_there_unsent_data(stcb);
15215                 if (TAILQ_EMPTY(&asoc->send_queue) &&
15216                     TAILQ_EMPTY(&asoc->sent_queue) &&
15217                     (cnt == 0)) {
15218                         if (asoc->locked_on_sending) {
15219                                 goto abort_anyway;
15220                         }
15221                         /* there is nothing queued to send, so I'm done... */
15222                         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
15223                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
15224                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
15225                                 /* only send SHUTDOWN the first time through */
15226                                 sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
15227                                 if (SCTP_GET_STATE(asoc) == SCTP_STATE_OPEN) {
15228                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
15229                                 }
15230                                 SCTP_SET_STATE(asoc, SCTP_STATE_SHUTDOWN_SENT);
15231                                 SCTP_CLEAR_SUBSTATE(asoc, SCTP_STATE_SHUTDOWN_PENDING);
15232                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
15233                                     asoc->primary_destination);
15234                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
15235                                     asoc->primary_destination);
15236                         }
15237                 } else {
15238                         /*-
15239                          * we still got (or just got) data to send, so set
15240                          * SHUTDOWN_PENDING
15241                          */
15242                         /*-
15243                          * XXX sockets draft says that SCTP_EOF should be
15244                          * sent with no data.  currently, we will allow user
15245                          * data to be sent first and move to
15246                          * SHUTDOWN-PENDING
15247                          */
15248                         if ((SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
15249                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
15250                             (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
15251                                 if (hold_tcblock == 0) {
15252                                         SCTP_TCB_LOCK(stcb);
15253                                         hold_tcblock = 1;
15254                                 }
15255                                 if (asoc->locked_on_sending) {
15256                                         /* Locked to send out the data */
15257                                         struct sctp_stream_queue_pending *sp;
15258
15259                                         sp = TAILQ_LAST(&asoc->locked_on_sending->outqueue, sctp_streamhead);
15260                                         if (sp) {
15261                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
15262                                                         asoc->state |= SCTP_STATE_PARTIAL_MSG_LEFT;
15263                                         }
15264                                 }
15265                                 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
15266                                 if (TAILQ_EMPTY(&asoc->send_queue) &&
15267                                     TAILQ_EMPTY(&asoc->sent_queue) &&
15268                                     (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
15269                         abort_anyway:
15270                                         if (free_cnt_applied) {
15271                                                 atomic_add_int(&stcb->asoc.refcnt, -1);
15272                                                 free_cnt_applied = 0;
15273                                         }
15274                                         sctp_abort_an_association(stcb->sctp_ep, stcb,
15275                                             SCTP_RESPONSE_TO_USER_REQ,
15276                                             NULL, SCTP_SO_LOCKED);
15277                                         /*
15278                                          * now relock the stcb so everything
15279                                          * is sane
15280                                          */
15281                                         hold_tcblock = 0;
15282                                         stcb = NULL;
15283                                         goto out;
15284                                 }
15285                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
15286                                     asoc->primary_destination);
15287                                 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
15288                         }
15289                 }
15290         }
15291 skip_out_eof:
15292         if (!TAILQ_EMPTY(&stcb->asoc.control_send_queue)) {
15293                 some_on_control = 1;
15294         }
15295         if ((net->flight_size > net->cwnd) &&
15296             (SCTP_BASE_SYSCTL(sctp_cmt_on_off) == 0)) {
15297                 queue_only = 1;
15298         } else if (asoc->ifp_had_enobuf) {
15299                 SCTP_STAT_INCR(sctps_ifnomemqueued);
15300                 if (net->flight_size > (net->mtu * 2)) {
15301                         queue_only = 1;
15302                 } else {
15303                         queue_only = 0;
15304                 }
15305                 asoc->ifp_had_enobuf = 0;
15306                 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15307                     (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15308         } else {
15309                 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
15310                     (stcb->asoc.stream_queue_cnt * sizeof(struct sctp_data_chunk)));
15311                 if (net->flight_size > (net->mtu * stcb->asoc.max_burst)) {
15312                         queue_only = 1;
15313                         SCTP_STAT_INCR(sctps_send_burst_avoid);
15314                 } else if (net->flight_size > net->cwnd) {
15315                         queue_only = 1;
15316                         SCTP_STAT_INCR(sctps_send_cwnd_avoid);
15317                 } else {
15318                         queue_only = 0;
15319                 }
15320         }
15321         if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
15322             (stcb->asoc.total_flight > 0) &&
15323             (stcb->asoc.stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
15324             (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
15325                 /*-
15326                  * Ok, Nagle is set on and we have data outstanding.
15327                  * Don't send anything and let SACKs drive out the
15328                  * data unless wen have a "full" segment to send.
15329                  */
15330                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15331                         sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
15332                 }
15333                 SCTP_STAT_INCR(sctps_naglequeued);
15334                 nagle_applies = 1;
15335         } else {
15336                 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
15337                         if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
15338                                 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
15339                 }
15340                 SCTP_STAT_INCR(sctps_naglesent);
15341                 nagle_applies = 0;
15342         }
15343         if (queue_only_for_init) {
15344                 if (hold_tcblock == 0) {
15345                         SCTP_TCB_LOCK(stcb);
15346                         hold_tcblock = 1;
15347                 }
15348                 if (SCTP_GET_STATE(&stcb->asoc) == SCTP_STATE_OPEN) {
15349                         /* a collision took us forward? */
15350                         queue_only_for_init = 0;
15351                         queue_only = 0;
15352                 } else {
15353                         sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
15354                         SCTP_SET_STATE(&stcb->asoc, SCTP_STATE_COOKIE_WAIT);
15355                         queue_only_for_init = 0;
15356                         queue_only = 1;
15357                 }
15358         }
15359         if ((queue_only == 0) && (nagle_applies == 0) && (stcb->asoc.peers_rwnd && un_sent)) {
15360                 /* we can attempt to send too. */
15361                 if (hold_tcblock == 0) {
15362                         /*
15363                          * If there is activity recv'ing sacks no need to
15364                          * send
15365                          */
15366                         if (SCTP_TCB_TRYLOCK(stcb)) {
15367                                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15368                                 hold_tcblock = 1;
15369                         }
15370                 } else {
15371                         sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15372                 }
15373         } else if ((queue_only == 0) &&
15374                     (stcb->asoc.peers_rwnd == 0) &&
15375             (stcb->asoc.total_flight == 0)) {
15376                 /* We get to have a probe outstanding */
15377                 if (hold_tcblock == 0) {
15378                         hold_tcblock = 1;
15379                         SCTP_TCB_LOCK(stcb);
15380                 }
15381                 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
15382         } else if (some_on_control) {
15383                 int num_out, reason, cwnd_full, frag_point;
15384
15385                 /* Here we do control only */
15386                 if (hold_tcblock == 0) {
15387                         hold_tcblock = 1;
15388                         SCTP_TCB_LOCK(stcb);
15389                 }
15390                 frag_point = sctp_get_frag_point(stcb, &stcb->asoc);
15391                 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
15392                     &reason, 1, &cwnd_full, 1, &now, &now_filled, frag_point, SCTP_SO_LOCKED);
15393         }
15394         SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
15395             queue_only, stcb->asoc.peers_rwnd, un_sent,
15396             stcb->asoc.total_flight, stcb->asoc.chunks_on_out_queue,
15397             stcb->asoc.total_output_queue_size, error);
15398
15399 out:
15400 out_unlocked:
15401
15402         if (local_soresv && stcb) {
15403                 atomic_subtract_int(&stcb->asoc.sb_send_resv, sndlen);
15404                 local_soresv = 0;
15405         }
15406         if (create_lock_applied) {
15407                 SCTP_ASOC_CREATE_UNLOCK(inp);
15408                 create_lock_applied = 0;
15409         }
15410         if ((stcb) && hold_tcblock) {
15411                 SCTP_TCB_UNLOCK(stcb);
15412         }
15413         if (stcb && free_cnt_applied) {
15414                 atomic_add_int(&stcb->asoc.refcnt, -1);
15415         }
15416 #ifdef INVARIANTS
15417         if (stcb) {
15418                 if (mtx_owned(&stcb->tcb_mtx)) {
15419                         panic("Leaving with tcb mtx owned?");
15420                 }
15421                 if (mtx_owned(&stcb->tcb_send_mtx)) {
15422                         panic("Leaving with tcb send mtx owned?");
15423                 }
15424         }
15425 #endif
15426         if (top) {
15427                 sctp_m_freem(top);
15428         }
15429         if (control) {
15430                 sctp_m_freem(control);
15431         }
15432         return (error);
15433 }
15434
15435
15436 /*
15437  * generate an AUTHentication chunk, if required
15438  */
15439 struct mbuf *
15440 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
15441     struct sctp_auth_chunk **auth_ret, uint32_t * offset,
15442     struct sctp_tcb *stcb, uint8_t chunk)
15443 {
15444         struct mbuf *m_auth;
15445         struct sctp_auth_chunk *auth;
15446         int chunk_len;
15447
15448         if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
15449             (stcb == NULL))
15450                 return (m);
15451
15452         /* sysctl disabled auth? */
15453         if (SCTP_BASE_SYSCTL(sctp_auth_disable))
15454                 return (m);
15455
15456         /* peer doesn't do auth... */
15457         if (!stcb->asoc.peer_supports_auth) {
15458                 return (m);
15459         }
15460         /* does the requested chunk require auth? */
15461         if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
15462                 return (m);
15463         }
15464         m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_DONTWAIT, 1, MT_HEADER);
15465         if (m_auth == NULL) {
15466                 /* no mbuf's */
15467                 return (m);
15468         }
15469         /* reserve some space if this will be the first mbuf */
15470         if (m == NULL)
15471                 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
15472         /* fill in the AUTH chunk details */
15473         auth = mtod(m_auth, struct sctp_auth_chunk *);
15474         bzero(auth, sizeof(*auth));
15475         auth->ch.chunk_type = SCTP_AUTHENTICATION;
15476         auth->ch.chunk_flags = 0;
15477         chunk_len = sizeof(*auth) +
15478             sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
15479         auth->ch.chunk_length = htons(chunk_len);
15480         auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
15481         /* key id and hmac digest will be computed and filled in upon send */
15482
15483         /* save the offset where the auth was inserted into the chain */
15484         if (m != NULL) {
15485                 struct mbuf *cn;
15486
15487                 *offset = 0;
15488                 cn = m;
15489                 while (cn) {
15490                         *offset += SCTP_BUF_LEN(cn);
15491                         cn = SCTP_BUF_NEXT(cn);
15492                 }
15493         } else
15494                 *offset = 0;
15495
15496         /* update length and return pointer to the auth chunk */
15497         SCTP_BUF_LEN(m_auth) = chunk_len;
15498         m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
15499         if (auth_ret != NULL)
15500                 *auth_ret = auth;
15501
15502         return (m);
15503 }
15504
15505 #ifdef INET6
15506 int
15507 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t * ro)
15508 {
15509         struct nd_prefix *pfx = NULL;
15510         struct nd_pfxrouter *pfxrtr = NULL;
15511         struct sockaddr_in6 gw6;
15512
15513         if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
15514                 return (0);
15515
15516         /* get prefix entry of address */
15517         LIST_FOREACH(pfx, &MODULE_GLOBAL(MOD_INET6, nd_prefix), ndpr_entry) {
15518                 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
15519                         continue;
15520                 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
15521                     &src6->sin6_addr, &pfx->ndpr_mask))
15522                         break;
15523         }
15524         /* no prefix entry in the prefix list */
15525         if (pfx == NULL) {
15526                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
15527                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
15528                 return (0);
15529         }
15530         SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
15531         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
15532
15533         /* search installed gateway from prefix entry */
15534         for (pfxrtr = pfx->ndpr_advrtrs.lh_first; pfxrtr; pfxrtr =
15535             pfxrtr->pfr_next) {
15536                 memset(&gw6, 0, sizeof(struct sockaddr_in6));
15537                 gw6.sin6_family = AF_INET6;
15538                 gw6.sin6_len = sizeof(struct sockaddr_in6);
15539                 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
15540                     sizeof(struct in6_addr));
15541                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
15542                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
15543                 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
15544                 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
15545                 if (sctp_cmpaddr((struct sockaddr *)&gw6,
15546                     ro->ro_rt->rt_gateway)) {
15547                         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
15548                         return (1);
15549                 }
15550         }
15551         SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
15552         return (0);
15553 }
15554
15555 #endif
15556
15557 int
15558 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t * ro)
15559 {
15560         struct sockaddr_in *sin, *mask;
15561         struct ifaddr *ifa;
15562         struct in_addr srcnetaddr, gwnetaddr;
15563
15564         if (ro == NULL || ro->ro_rt == NULL ||
15565             sifa->address.sa.sa_family != AF_INET) {
15566                 return (0);
15567         }
15568         ifa = (struct ifaddr *)sifa->ifa;
15569         mask = (struct sockaddr_in *)(ifa->ifa_netmask);
15570         sin = (struct sockaddr_in *)&sifa->address.sin;
15571         srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
15572         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: src address is ");
15573         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
15574         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", srcnetaddr.s_addr);
15575
15576         sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
15577         gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
15578         SCTPDBG(SCTP_DEBUG_OUTPUT1, "match_nexthop4: nexthop is ");
15579         SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
15580         SCTPDBG(SCTP_DEBUG_OUTPUT1, "network address is %x\n", gwnetaddr.s_addr);
15581         if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
15582                 return (1);
15583         }
15584         return (0);
15585 }