]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/sctp_pcb.c
- most all includes (#include <>) migrate to the sctp_os_bsd.h file
[FreeBSD/FreeBSD.git] / sys / netinet / sctp_pcb.c
1 /*-
2  * Copyright (c) 2001-2007, 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_pcb.c,v 1.38 2005/03/06 16:04:18 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_pcb.h>
40 #include <netinet/sctputil.h>
41 #include <netinet/sctp.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctp_asconf.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_timer.h>
46
47
48 #ifdef SCTP_DEBUG
49 uint32_t sctp_debug_on = 0;
50
51 #endif                          /* SCTP_DEBUG */
52
53
54 extern int sctp_pcbtblsize;
55 extern int sctp_hashtblsize;
56 extern int sctp_chunkscale;
57
58 struct sctp_epinfo sctppcbinfo;
59
60 /* FIX: we don't handle multiple link local scopes */
61 /* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
62 int
63 SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b)
64 {
65         struct in6_addr tmp_a, tmp_b;
66
67         /* use a copy of a and b */
68         tmp_a = *a;
69         tmp_b = *b;
70         in6_clearscope(&tmp_a);
71         in6_clearscope(&tmp_b);
72         return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
73 }
74
75
76 void
77 sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
78 {
79         /*
80          * We really don't need to lock this, but I will just because it
81          * does not hurt.
82          */
83         SCTP_INP_INFO_RLOCK();
84         spcb->ep_count = sctppcbinfo.ipi_count_ep;
85         spcb->asoc_count = sctppcbinfo.ipi_count_asoc;
86         spcb->laddr_count = sctppcbinfo.ipi_count_laddr;
87         spcb->raddr_count = sctppcbinfo.ipi_count_raddr;
88         spcb->chk_count = sctppcbinfo.ipi_count_chunk;
89         spcb->readq_count = sctppcbinfo.ipi_count_readq;
90         spcb->stream_oque = sctppcbinfo.ipi_count_strmoq;
91         spcb->free_chunks = sctppcbinfo.ipi_free_chunks;
92
93         SCTP_INP_INFO_RUNLOCK();
94 }
95
96
97 /*
98  * Notes on locks for FreeBSD 5 and up. All association lookups that have a
99  * definte ep, the INP structure is assumed to be locked for reading. If we
100  * need to go find the INP (ususally when a **inp is passed) then we must
101  * lock the INFO structure first and if needed lock the INP too. Note that if
102  * we lock it we must
103  *
104  */
105
106
107 /*
108  * Given a endpoint, look and find in its association list any association
109  * with the "to" address given. This can be a "from" address, too, for
110  * inbound packets. For outbound packets it is a true "to" address.
111  */
112
113 static struct sctp_tcb *
114 sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
115     struct sockaddr *to, struct sctp_nets **netp)
116 {
117         /**** ASSUMSES THE CALLER holds the INP_INFO_RLOCK */
118
119         /*
120          * Note for this module care must be taken when observing what to is
121          * for. In most of the rest of the code the TO field represents my
122          * peer and the FROM field represents my address. For this module it
123          * is reversed of that.
124          */
125         /*
126          * If we support the TCP model, then we must now dig through to see
127          * if we can find our endpoint in the list of tcp ep's.
128          */
129         uint16_t lport, rport;
130         struct sctppcbhead *ephead;
131         struct sctp_inpcb *inp;
132         struct sctp_laddr *laddr;
133         struct sctp_tcb *stcb;
134         struct sctp_nets *net;
135
136         if ((to == NULL) || (from == NULL)) {
137                 return (NULL);
138         }
139         if (to->sa_family == AF_INET && from->sa_family == AF_INET) {
140                 lport = ((struct sockaddr_in *)to)->sin_port;
141                 rport = ((struct sockaddr_in *)from)->sin_port;
142         } else if (to->sa_family == AF_INET6 && from->sa_family == AF_INET6) {
143                 lport = ((struct sockaddr_in6 *)to)->sin6_port;
144                 rport = ((struct sockaddr_in6 *)from)->sin6_port;
145         } else {
146                 return NULL;
147         }
148         ephead = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR(
149             (lport + rport), sctppcbinfo.hashtcpmark)];
150         /*
151          * Ok now for each of the guys in this bucket we must look and see:
152          * - Does the remote port match. - Does there single association's
153          * addresses match this address (to). If so we update p_ep to point
154          * to this ep and return the tcb from it.
155          */
156         LIST_FOREACH(inp, ephead, sctp_hash) {
157                 if (lport != inp->sctp_lport) {
158                         continue;
159                 }
160                 SCTP_INP_RLOCK(inp);
161                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
162                         SCTP_INP_RUNLOCK(inp);
163                         continue;
164                 }
165                 /* check to see if the ep has one of the addresses */
166                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
167                         /* We are NOT bound all, so look further */
168                         int match = 0;
169
170                         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
171
172                                 if (laddr->ifa == NULL) {
173 #ifdef SCTP_DEBUG
174                                         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
175                                                 printf("An ounce of prevention is worth a pound of cure\n");
176                                         }
177 #endif
178                                         continue;
179                                 }
180                                 if (laddr->ifa->ifa_addr == NULL) {
181 #ifdef SCTP_DEBUG
182                                         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
183                                                 printf("ifa with a NULL address\n");
184                                         }
185 #endif
186                                         continue;
187                                 }
188                                 if (laddr->ifa->ifa_addr->sa_family ==
189                                     to->sa_family) {
190                                         /* see if it matches */
191                                         struct sockaddr_in *intf_addr, *sin;
192
193                                         intf_addr = (struct sockaddr_in *)
194                                             laddr->ifa->ifa_addr;
195                                         sin = (struct sockaddr_in *)to;
196                                         if (from->sa_family == AF_INET) {
197                                                 if (sin->sin_addr.s_addr ==
198                                                     intf_addr->sin_addr.s_addr) {
199                                                         match = 1;
200                                                         break;
201                                                 }
202                                         } else {
203                                                 struct sockaddr_in6 *intf_addr6;
204                                                 struct sockaddr_in6 *sin6;
205
206                                                 sin6 = (struct sockaddr_in6 *)
207                                                     to;
208                                                 intf_addr6 = (struct sockaddr_in6 *)
209                                                     laddr->ifa->ifa_addr;
210
211                                                 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
212                                                     &intf_addr6->sin6_addr)) {
213                                                         match = 1;
214                                                         break;
215                                                 }
216                                         }
217                                 }
218                         }
219                         if (match == 0) {
220                                 /* This endpoint does not have this address */
221                                 SCTP_INP_RUNLOCK(inp);
222                                 continue;
223                         }
224                 }
225                 /*
226                  * Ok if we hit here the ep has the address, does it hold
227                  * the tcb?
228                  */
229
230                 stcb = LIST_FIRST(&inp->sctp_asoc_list);
231                 if (stcb == NULL) {
232                         SCTP_INP_RUNLOCK(inp);
233                         continue;
234                 }
235                 SCTP_TCB_LOCK(stcb);
236                 if (stcb->rport != rport) {
237                         /* remote port does not match. */
238                         SCTP_TCB_UNLOCK(stcb);
239                         SCTP_INP_RUNLOCK(inp);
240                         continue;
241                 }
242                 /* Does this TCB have a matching address? */
243                 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
244
245                         if (net->ro._l_addr.sa.sa_family != from->sa_family) {
246                                 /* not the same family, can't be a match */
247                                 continue;
248                         }
249                         if (from->sa_family == AF_INET) {
250                                 struct sockaddr_in *sin, *rsin;
251
252                                 sin = (struct sockaddr_in *)&net->ro._l_addr;
253                                 rsin = (struct sockaddr_in *)from;
254                                 if (sin->sin_addr.s_addr ==
255                                     rsin->sin_addr.s_addr) {
256                                         /* found it */
257                                         if (netp != NULL) {
258                                                 *netp = net;
259                                         }
260                                         /* Update the endpoint pointer */
261                                         *inp_p = inp;
262                                         SCTP_INP_RUNLOCK(inp);
263                                         return (stcb);
264                                 }
265                         } else {
266                                 struct sockaddr_in6 *sin6, *rsin6;
267
268                                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
269                                 rsin6 = (struct sockaddr_in6 *)from;
270                                 if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
271                                     &rsin6->sin6_addr)) {
272                                         /* found it */
273                                         if (netp != NULL) {
274                                                 *netp = net;
275                                         }
276                                         /* Update the endpoint pointer */
277                                         *inp_p = inp;
278                                         SCTP_INP_RUNLOCK(inp);
279                                         return (stcb);
280                                 }
281                         }
282                 }
283                 SCTP_TCB_UNLOCK(stcb);
284                 SCTP_INP_RUNLOCK(inp);
285         }
286         return (NULL);
287 }
288
289 /*
290  * rules for use
291  *
292  * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
293  * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
294  * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
295  * NULL.
296  */
297
298 struct sctp_tcb *
299 sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
300     struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
301 {
302         struct sctpasochead *head;
303         struct sctp_inpcb *inp;
304         struct sctp_tcb *stcb;
305         struct sctp_nets *net;
306         uint16_t rport;
307
308         inp = *inp_p;
309         if (remote->sa_family == AF_INET) {
310                 rport = (((struct sockaddr_in *)remote)->sin_port);
311         } else if (remote->sa_family == AF_INET6) {
312                 rport = (((struct sockaddr_in6 *)remote)->sin6_port);
313         } else {
314                 return (NULL);
315         }
316         if (locked_tcb) {
317                 /*
318                  * UN-lock so we can do proper locking here this occurs when
319                  * called from load_addresses_from_init.
320                  */
321                 SCTP_TCB_UNLOCK(locked_tcb);
322         }
323         SCTP_INP_INFO_RLOCK();
324         if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
325                 /*
326                  * Now either this guy is our listener or it's the
327                  * connector. If it is the one that issued the connect, then
328                  * it's only chance is to be the first TCB in the list. If
329                  * it is the acceptor, then do the special_lookup to hash
330                  * and find the real inp.
331                  */
332                 if ((inp->sctp_socket) && (inp->sctp_socket->so_qlimit)) {
333                         /* to is peer addr, from is my addr */
334                         stcb = sctp_tcb_special_locate(inp_p, remote, local,
335                             netp);
336                         if ((stcb != NULL) && (locked_tcb == NULL)) {
337                                 /* we have a locked tcb, lower refcount */
338                                 SCTP_INP_WLOCK(inp);
339                                 SCTP_INP_DECR_REF(inp);
340                                 SCTP_INP_WUNLOCK(inp);
341                         }
342                         if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
343                                 SCTP_INP_RLOCK(locked_tcb->sctp_ep);
344                                 SCTP_TCB_LOCK(locked_tcb);
345                                 SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
346                         }
347                         SCTP_INP_INFO_RUNLOCK();
348                         return (stcb);
349                 } else {
350                         SCTP_INP_WLOCK(inp);
351                         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
352                                 goto null_return;
353                         }
354                         stcb = LIST_FIRST(&inp->sctp_asoc_list);
355                         if (stcb == NULL) {
356                                 goto null_return;
357                         }
358                         SCTP_TCB_LOCK(stcb);
359                         if (stcb->rport != rport) {
360                                 /* remote port does not match. */
361                                 SCTP_TCB_UNLOCK(stcb);
362                                 goto null_return;
363                         }
364                         /* now look at the list of remote addresses */
365                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
366 #ifdef INVARIANTS
367                                 if (net == (TAILQ_NEXT(net, sctp_next))) {
368                                         panic("Corrupt net list");
369                                 }
370 #endif
371                                 if (net->ro._l_addr.sa.sa_family !=
372                                     remote->sa_family) {
373                                         /* not the same family */
374                                         continue;
375                                 }
376                                 if (remote->sa_family == AF_INET) {
377                                         struct sockaddr_in *sin, *rsin;
378
379                                         sin = (struct sockaddr_in *)
380                                             &net->ro._l_addr;
381                                         rsin = (struct sockaddr_in *)remote;
382                                         if (sin->sin_addr.s_addr ==
383                                             rsin->sin_addr.s_addr) {
384                                                 /* found it */
385                                                 if (netp != NULL) {
386                                                         *netp = net;
387                                                 }
388                                                 if (locked_tcb == NULL) {
389                                                         SCTP_INP_DECR_REF(inp);
390                                                 } else if (locked_tcb != stcb) {
391                                                         SCTP_TCB_LOCK(locked_tcb);
392                                                 }
393                                                 SCTP_INP_WUNLOCK(inp);
394                                                 SCTP_INP_INFO_RUNLOCK();
395                                                 return (stcb);
396                                         }
397                                 } else if (remote->sa_family == AF_INET6) {
398                                         struct sockaddr_in6 *sin6, *rsin6;
399
400                                         sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
401                                         rsin6 = (struct sockaddr_in6 *)remote;
402                                         if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
403                                             &rsin6->sin6_addr)) {
404                                                 /* found it */
405                                                 if (netp != NULL) {
406                                                         *netp = net;
407                                                 }
408                                                 if (locked_tcb == NULL) {
409                                                         SCTP_INP_DECR_REF(inp);
410                                                 } else if (locked_tcb != stcb) {
411                                                         SCTP_TCB_LOCK(locked_tcb);
412                                                 }
413                                                 SCTP_INP_WUNLOCK(inp);
414                                                 SCTP_INP_INFO_RUNLOCK();
415                                                 return (stcb);
416                                         }
417                                 }
418                         }
419                         SCTP_TCB_UNLOCK(stcb);
420                 }
421         } else {
422                 SCTP_INP_WLOCK(inp);
423                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
424                         goto null_return;
425                 }
426                 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
427                     inp->sctp_hashmark)];
428                 if (head == NULL) {
429                         goto null_return;
430                 }
431                 LIST_FOREACH(stcb, head, sctp_tcbhash) {
432                         if (stcb->rport != rport) {
433                                 /* remote port does not match */
434                                 continue;
435                         }
436                         /* now look at the list of remote addresses */
437                         SCTP_TCB_LOCK(stcb);
438                         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
439 #ifdef INVARIANTS
440                                 if (net == (TAILQ_NEXT(net, sctp_next))) {
441                                         panic("Corrupt net list");
442                                 }
443 #endif
444                                 if (net->ro._l_addr.sa.sa_family !=
445                                     remote->sa_family) {
446                                         /* not the same family */
447                                         continue;
448                                 }
449                                 if (remote->sa_family == AF_INET) {
450                                         struct sockaddr_in *sin, *rsin;
451
452                                         sin = (struct sockaddr_in *)
453                                             &net->ro._l_addr;
454                                         rsin = (struct sockaddr_in *)remote;
455                                         if (sin->sin_addr.s_addr ==
456                                             rsin->sin_addr.s_addr) {
457                                                 /* found it */
458                                                 if (netp != NULL) {
459                                                         *netp = net;
460                                                 }
461                                                 if (locked_tcb == NULL) {
462                                                         SCTP_INP_DECR_REF(inp);
463                                                 } else if (locked_tcb != stcb) {
464                                                         SCTP_TCB_LOCK(locked_tcb);
465                                                 }
466                                                 SCTP_INP_WUNLOCK(inp);
467                                                 SCTP_INP_INFO_RUNLOCK();
468                                                 return (stcb);
469                                         }
470                                 } else if (remote->sa_family == AF_INET6) {
471                                         struct sockaddr_in6 *sin6, *rsin6;
472
473                                         sin6 = (struct sockaddr_in6 *)
474                                             &net->ro._l_addr;
475                                         rsin6 = (struct sockaddr_in6 *)remote;
476                                         if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
477                                             &rsin6->sin6_addr)) {
478                                                 /* found it */
479                                                 if (netp != NULL) {
480                                                         *netp = net;
481                                                 }
482                                                 if (locked_tcb == NULL) {
483                                                         SCTP_INP_DECR_REF(inp);
484                                                 } else if (locked_tcb != stcb) {
485                                                         SCTP_TCB_LOCK(locked_tcb);
486                                                 }
487                                                 SCTP_INP_WUNLOCK(inp);
488                                                 SCTP_INP_INFO_RUNLOCK();
489                                                 return (stcb);
490                                         }
491                                 }
492                         }
493                         SCTP_TCB_UNLOCK(stcb);
494                 }
495         }
496 null_return:
497         /* clean up for returning null */
498         if (locked_tcb) {
499                 SCTP_TCB_LOCK(locked_tcb);
500         }
501         SCTP_INP_WUNLOCK(inp);
502         SCTP_INP_INFO_RUNLOCK();
503         /* not found */
504         return (NULL);
505 }
506
507 /*
508  * Find an association for a specific endpoint using the association id given
509  * out in the COMM_UP notification
510  */
511
512 struct sctp_tcb *
513 sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
514 {
515         /*
516          * Use my the assoc_id to find a endpoint
517          */
518         struct sctpasochead *head;
519         struct sctp_tcb *stcb;
520         uint32_t id;
521
522         if (asoc_id == 0 || inp == NULL) {
523                 return (NULL);
524         }
525         SCTP_INP_INFO_RLOCK();
526         id = (uint32_t) asoc_id;
527         head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(id,
528             sctppcbinfo.hashasocmark)];
529         if (head == NULL) {
530                 /* invalid id TSNH */
531                 SCTP_INP_INFO_RUNLOCK();
532                 return (NULL);
533         }
534         LIST_FOREACH(stcb, head, sctp_asocs) {
535                 SCTP_INP_RLOCK(stcb->sctp_ep);
536                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
537                         SCTP_INP_RUNLOCK(stcb->sctp_ep);
538                         SCTP_INP_INFO_RUNLOCK();
539                         return (NULL);
540                 }
541                 if (stcb->asoc.assoc_id == id) {
542                         /* candidate */
543                         if (inp != stcb->sctp_ep) {
544                                 /*
545                                  * some other guy has the same id active (id
546                                  * collision ??).
547                                  */
548                                 SCTP_INP_RUNLOCK(stcb->sctp_ep);
549                                 continue;
550                         }
551                         if (want_lock) {
552                                 SCTP_TCB_LOCK(stcb);
553                         }
554                         SCTP_INP_RUNLOCK(stcb->sctp_ep);
555                         SCTP_INP_INFO_RUNLOCK();
556                         return (stcb);
557                 }
558                 SCTP_INP_RUNLOCK(stcb->sctp_ep);
559         }
560         /* Ok if we missed here, lets try the restart hash */
561         head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(id, sctppcbinfo.hashrestartmark)];
562         if (head == NULL) {
563                 /* invalid id TSNH */
564                 SCTP_INP_INFO_RUNLOCK();
565                 return (NULL);
566         }
567         LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
568                 SCTP_INP_RLOCK(stcb->sctp_ep);
569                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
570                         SCTP_INP_RUNLOCK(stcb->sctp_ep);
571                         SCTP_INP_INFO_RUNLOCK();
572                         return (NULL);
573                 }
574                 SCTP_TCB_LOCK(stcb);
575                 SCTP_INP_RUNLOCK(stcb->sctp_ep);
576                 if (stcb->asoc.assoc_id == id) {
577                         /* candidate */
578                         if (inp != stcb->sctp_ep) {
579                                 /*
580                                  * some other guy has the same id active (id
581                                  * collision ??).
582                                  */
583                                 SCTP_TCB_UNLOCK(stcb);
584                                 continue;
585                         }
586                         SCTP_INP_INFO_RUNLOCK();
587                         return (stcb);
588                 }
589                 SCTP_TCB_UNLOCK(stcb);
590         }
591         SCTP_INP_INFO_RUNLOCK();
592         return (NULL);
593 }
594
595
596 static struct sctp_inpcb *
597 sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
598     uint16_t lport)
599 {
600         struct sctp_inpcb *inp;
601         struct sockaddr_in *sin;
602         struct sockaddr_in6 *sin6;
603         struct sctp_laddr *laddr;
604
605         /*
606          * Endpoing probe expects that the INP_INFO is locked.
607          */
608         if (nam->sa_family == AF_INET) {
609                 sin = (struct sockaddr_in *)nam;
610                 sin6 = NULL;
611         } else if (nam->sa_family == AF_INET6) {
612                 sin6 = (struct sockaddr_in6 *)nam;
613                 sin = NULL;
614         } else {
615                 /* unsupported family */
616                 return (NULL);
617         }
618         if (head == NULL)
619                 return (NULL);
620         LIST_FOREACH(inp, head, sctp_hash) {
621                 SCTP_INP_RLOCK(inp);
622                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
623                         SCTP_INP_RUNLOCK(inp);
624                         continue;
625                 }
626                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
627                     (inp->sctp_lport == lport)) {
628                         /* got it */
629                         if ((nam->sa_family == AF_INET) &&
630                             (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
631                             SCTP_IPV6_V6ONLY(inp)) {
632                                 /* IPv4 on a IPv6 socket with ONLY IPv6 set */
633                                 SCTP_INP_RUNLOCK(inp);
634                                 continue;
635                         }
636                         /* A V6 address and the endpoint is NOT bound V6 */
637                         if (nam->sa_family == AF_INET6 &&
638                             (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
639                                 SCTP_INP_RUNLOCK(inp);
640                                 continue;
641                         }
642                         SCTP_INP_RUNLOCK(inp);
643                         return (inp);
644                 }
645                 SCTP_INP_RUNLOCK(inp);
646         }
647
648         if ((nam->sa_family == AF_INET) &&
649             (sin->sin_addr.s_addr == INADDR_ANY)) {
650                 /* Can't hunt for one that has no address specified */
651                 return (NULL);
652         } else if ((nam->sa_family == AF_INET6) &&
653             (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
654                 /* Can't hunt for one that has no address specified */
655                 return (NULL);
656         }
657         /*
658          * ok, not bound to all so see if we can find a EP bound to this
659          * address.
660          */
661         LIST_FOREACH(inp, head, sctp_hash) {
662                 SCTP_INP_RLOCK(inp);
663                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
664                         SCTP_INP_RUNLOCK(inp);
665                         continue;
666                 }
667                 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
668                         SCTP_INP_RUNLOCK(inp);
669                         continue;
670                 }
671                 /*
672                  * Ok this could be a likely candidate, look at all of its
673                  * addresses
674                  */
675                 if (inp->sctp_lport != lport) {
676                         SCTP_INP_RUNLOCK(inp);
677                         continue;
678                 }
679                 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
680                         if (laddr->ifa == NULL) {
681 #ifdef SCTP_DEBUG
682                                 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
683                                         printf("An ounce of prevention is worth a pound of cure\n");
684                                 }
685 #endif
686                                 continue;
687                         }
688 #ifdef SCTP_DEBUG
689                         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
690                                 printf("Ok laddr->ifa:%p is possible, ",
691                                     laddr->ifa);
692                         }
693 #endif
694                         if (laddr->ifa->ifa_addr == NULL) {
695 #ifdef SCTP_DEBUG
696                                 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
697                                         printf("Huh IFA as an ifa_addr=NULL, ");
698                                 }
699 #endif
700                                 continue;
701                         }
702                         if (laddr->ifa->ifa_addr->sa_family == nam->sa_family) {
703                                 /* possible, see if it matches */
704                                 struct sockaddr_in *intf_addr;
705
706                                 intf_addr = (struct sockaddr_in *)
707                                     laddr->ifa->ifa_addr;
708                                 if (nam->sa_family == AF_INET) {
709                                         if (sin->sin_addr.s_addr ==
710                                             intf_addr->sin_addr.s_addr) {
711                                                 SCTP_INP_RUNLOCK(inp);
712                                                 return (inp);
713                                         }
714                                 } else if (nam->sa_family == AF_INET6) {
715                                         struct sockaddr_in6 *intf_addr6;
716
717                                         intf_addr6 = (struct sockaddr_in6 *)
718                                             laddr->ifa->ifa_addr;
719                                         if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
720                                             &intf_addr6->sin6_addr)) {
721                                                 SCTP_INP_RUNLOCK(inp);
722                                                 return (inp);
723                                         }
724                                 }
725                         }
726                 }
727                 SCTP_INP_RUNLOCK(inp);
728         }
729         return (NULL);
730 }
731
732
733 struct sctp_inpcb *
734 sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock)
735 {
736         /*
737          * First we check the hash table to see if someone has this port
738          * bound with just the port.
739          */
740         struct sctp_inpcb *inp;
741         struct sctppcbhead *head;
742         struct sockaddr_in *sin;
743         struct sockaddr_in6 *sin6;
744         int lport;
745
746         if (nam->sa_family == AF_INET) {
747                 sin = (struct sockaddr_in *)nam;
748                 lport = ((struct sockaddr_in *)nam)->sin_port;
749         } else if (nam->sa_family == AF_INET6) {
750                 sin6 = (struct sockaddr_in6 *)nam;
751                 lport = ((struct sockaddr_in6 *)nam)->sin6_port;
752         } else {
753                 /* unsupported family */
754                 return (NULL);
755         }
756         /*
757          * I could cheat here and just cast to one of the types but we will
758          * do it right. It also provides the check against an Unsupported
759          * type too.
760          */
761         /* Find the head of the ALLADDR chain */
762         if (have_lock == 0) {
763                 SCTP_INP_INFO_RLOCK();
764
765         }
766         head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
767             sctppcbinfo.hashmark)];
768         inp = sctp_endpoint_probe(nam, head, lport);
769
770         /*
771          * If the TCP model exists it could be that the main listening
772          * endpoint is gone but there exists a connected socket for this guy
773          * yet. If so we can return the first one that we find. This may NOT
774          * be the correct one but the sctp_findassociation_ep_addr has
775          * further code to look at all TCP models.
776          */
777         if (inp == NULL && find_tcp_pool) {
778                 unsigned int i;
779
780                 for (i = 0; i < sctppcbinfo.hashtblsize; i++) {
781                         /*
782                          * This is real gross, but we do NOT have a remote
783                          * port at this point depending on who is calling.
784                          * We must therefore look for ANY one that matches
785                          * our local port :/
786                          */
787                         head = &sctppcbinfo.sctp_tcpephash[i];
788                         if (LIST_FIRST(head)) {
789                                 inp = sctp_endpoint_probe(nam, head, lport);
790                                 if (inp) {
791                                         /* Found one */
792                                         break;
793                                 }
794                         }
795                 }
796         }
797         if (inp) {
798                 SCTP_INP_INCR_REF(inp);
799         }
800         if (have_lock == 0) {
801                 SCTP_INP_INFO_RUNLOCK();
802         }
803         return (inp);
804 }
805
806 /*
807  * Find an association for an endpoint with the pointer to whom you want to
808  * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
809  * need to change the *to to some other struct like a mbuf...
810  */
811 struct sctp_tcb *
812 sctp_findassociation_addr_sa(struct sockaddr *to, struct sockaddr *from,
813     struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool)
814 {
815         struct sctp_inpcb *inp;
816         struct sctp_tcb *retval;
817
818         SCTP_INP_INFO_RLOCK();
819         if (find_tcp_pool) {
820                 if (inp_p != NULL) {
821                         retval = sctp_tcb_special_locate(inp_p, from, to, netp);
822                 } else {
823                         retval = sctp_tcb_special_locate(&inp, from, to, netp);
824                 }
825                 if (retval != NULL) {
826                         SCTP_INP_INFO_RUNLOCK();
827                         return (retval);
828                 }
829         }
830         inp = sctp_pcb_findep(to, 0, 1);
831         if (inp_p != NULL) {
832                 *inp_p = inp;
833         }
834         SCTP_INP_INFO_RUNLOCK();
835
836         if (inp == NULL) {
837                 return (NULL);
838         }
839         /*
840          * ok, we have an endpoint, now lets find the assoc for it (if any)
841          * we now place the source address or from in the to of the find
842          * endpoint call. Since in reality this chain is used from the
843          * inbound packet side.
844          */
845         if (inp_p != NULL) {
846                 retval = sctp_findassociation_ep_addr(inp_p, from, netp, to, NULL);
847         } else {
848                 retval = sctp_findassociation_ep_addr(&inp, from, netp, to, NULL);
849         }
850         return retval;
851 }
852
853
854 /*
855  * This routine will grub through the mbuf that is a INIT or INIT-ACK and
856  * find all addresses that the sender has specified in any address list. Each
857  * address will be used to lookup the TCB and see if one exits.
858  */
859 static struct sctp_tcb *
860 sctp_findassociation_special_addr(struct mbuf *m, int iphlen, int offset,
861     struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
862     struct sockaddr *dest)
863 {
864         struct sockaddr_in sin4;
865         struct sockaddr_in6 sin6;
866         struct sctp_paramhdr *phdr, parm_buf;
867         struct sctp_tcb *retval;
868         uint32_t ptype, plen;
869
870         memset(&sin4, 0, sizeof(sin4));
871         memset(&sin6, 0, sizeof(sin6));
872         sin4.sin_len = sizeof(sin4);
873         sin4.sin_family = AF_INET;
874         sin4.sin_port = sh->src_port;
875         sin6.sin6_len = sizeof(sin6);
876         sin6.sin6_family = AF_INET6;
877         sin6.sin6_port = sh->src_port;
878
879         retval = NULL;
880         offset += sizeof(struct sctp_init_chunk);
881
882         phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
883         while (phdr != NULL) {
884                 /* now we must see if we want the parameter */
885                 ptype = ntohs(phdr->param_type);
886                 plen = ntohs(phdr->param_length);
887                 if (plen == 0) {
888                         break;
889                 }
890                 if (ptype == SCTP_IPV4_ADDRESS &&
891                     plen == sizeof(struct sctp_ipv4addr_param)) {
892                         /* Get the rest of the address */
893                         struct sctp_ipv4addr_param ip4_parm, *p4;
894
895                         phdr = sctp_get_next_param(m, offset,
896                             (struct sctp_paramhdr *)&ip4_parm, plen);
897                         if (phdr == NULL) {
898                                 return (NULL);
899                         }
900                         p4 = (struct sctp_ipv4addr_param *)phdr;
901                         memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
902                         /* look it up */
903                         retval = sctp_findassociation_ep_addr(inp_p,
904                             (struct sockaddr *)&sin4, netp, dest, NULL);
905                         if (retval != NULL) {
906                                 return (retval);
907                         }
908                 } else if (ptype == SCTP_IPV6_ADDRESS &&
909                     plen == sizeof(struct sctp_ipv6addr_param)) {
910                         /* Get the rest of the address */
911                         struct sctp_ipv6addr_param ip6_parm, *p6;
912
913                         phdr = sctp_get_next_param(m, offset,
914                             (struct sctp_paramhdr *)&ip6_parm, plen);
915                         if (phdr == NULL) {
916                                 return (NULL);
917                         }
918                         p6 = (struct sctp_ipv6addr_param *)phdr;
919                         memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
920                         /* look it up */
921                         retval = sctp_findassociation_ep_addr(inp_p,
922                             (struct sockaddr *)&sin6, netp, dest, NULL);
923                         if (retval != NULL) {
924                                 return (retval);
925                         }
926                 }
927                 offset += SCTP_SIZE32(plen);
928                 phdr = sctp_get_next_param(m, offset, &parm_buf,
929                     sizeof(parm_buf));
930         }
931         return (NULL);
932 }
933
934
935 static struct sctp_tcb *
936 sctp_findassoc_by_vtag(struct sockaddr *from, uint32_t vtag,
937     struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
938     uint16_t lport, int skip_src_check)
939 {
940         /*
941          * Use my vtag to hash. If we find it we then verify the source addr
942          * is in the assoc. If all goes well we save a bit on rec of a
943          * packet.
944          */
945         struct sctpasochead *head;
946         struct sctp_nets *net;
947         struct sctp_tcb *stcb;
948
949         *netp = NULL;
950         *inp_p = NULL;
951         SCTP_INP_INFO_RLOCK();
952         head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(vtag,
953             sctppcbinfo.hashasocmark)];
954         if (head == NULL) {
955                 /* invalid vtag */
956                 SCTP_INP_INFO_RUNLOCK();
957                 return (NULL);
958         }
959         LIST_FOREACH(stcb, head, sctp_asocs) {
960                 SCTP_INP_RLOCK(stcb->sctp_ep);
961                 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
962                         SCTP_INP_RUNLOCK(stcb->sctp_ep);
963                         SCTP_INP_INFO_RUNLOCK();
964                         return (NULL);
965                 }
966                 SCTP_TCB_LOCK(stcb);
967                 SCTP_INP_RUNLOCK(stcb->sctp_ep);
968                 if (stcb->asoc.my_vtag == vtag) {
969                         /* candidate */
970                         if (stcb->rport != rport) {
971                                 /*
972                                  * we could remove this if vtags are unique
973                                  * across the system.
974                                  */
975                                 SCTP_TCB_UNLOCK(stcb);
976                                 continue;
977                         }
978                         if (stcb->sctp_ep->sctp_lport != lport) {
979                                 /*
980                                  * we could remove this if vtags are unique
981                                  * across the system.
982                                  */
983                                 SCTP_TCB_UNLOCK(stcb);
984                                 continue;
985                         }
986                         if (skip_src_check) {
987                                 *netp = NULL;   /* unknown */
988                                 *inp_p = stcb->sctp_ep;
989                                 SCTP_INP_INFO_RUNLOCK();
990                                 return (stcb);
991                         }
992                         net = sctp_findnet(stcb, from);
993                         if (net) {
994                                 /* yep its him. */
995                                 *netp = net;
996                                 SCTP_STAT_INCR(sctps_vtagexpress);
997                                 *inp_p = stcb->sctp_ep;
998                                 SCTP_INP_INFO_RUNLOCK();
999                                 return (stcb);
1000                         } else {
1001                                 /*
1002                                  * not him, this should only happen in rare
1003                                  * cases so I peg it.
1004                                  */
1005                                 SCTP_STAT_INCR(sctps_vtagbogus);
1006                         }
1007                 }
1008                 SCTP_TCB_UNLOCK(stcb);
1009         }
1010         SCTP_INP_INFO_RUNLOCK();
1011         return (NULL);
1012 }
1013
1014 /*
1015  * Find an association with the pointer to the inbound IP packet. This can be
1016  * a IPv4 or IPv6 packet.
1017  */
1018 struct sctp_tcb *
1019 sctp_findassociation_addr(struct mbuf *m, int iphlen, int offset,
1020     struct sctphdr *sh, struct sctp_chunkhdr *ch,
1021     struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1022 {
1023         int find_tcp_pool;
1024         struct ip *iph;
1025         struct sctp_tcb *retval;
1026         struct sockaddr_storage to_store, from_store;
1027         struct sockaddr *to = (struct sockaddr *)&to_store;
1028         struct sockaddr *from = (struct sockaddr *)&from_store;
1029         struct sctp_inpcb *inp;
1030
1031
1032         iph = mtod(m, struct ip *);
1033         if (iph->ip_v == IPVERSION) {
1034                 /* its IPv4 */
1035                 struct sockaddr_in *from4;
1036
1037                 from4 = (struct sockaddr_in *)&from_store;
1038                 bzero(from4, sizeof(*from4));
1039                 from4->sin_family = AF_INET;
1040                 from4->sin_len = sizeof(struct sockaddr_in);
1041                 from4->sin_addr.s_addr = iph->ip_src.s_addr;
1042                 from4->sin_port = sh->src_port;
1043         } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1044                 /* its IPv6 */
1045                 struct ip6_hdr *ip6;
1046                 struct sockaddr_in6 *from6;
1047
1048                 ip6 = mtod(m, struct ip6_hdr *);
1049                 from6 = (struct sockaddr_in6 *)&from_store;
1050                 bzero(from6, sizeof(*from6));
1051                 from6->sin6_family = AF_INET6;
1052                 from6->sin6_len = sizeof(struct sockaddr_in6);
1053                 from6->sin6_addr = ip6->ip6_src;
1054                 from6->sin6_port = sh->src_port;
1055                 /* Get the scopes in properly to the sin6 addr's */
1056                 /* we probably don't need these operations */
1057                 (void)sa6_recoverscope(from6);
1058                 sa6_embedscope(from6, ip6_use_defzone);
1059         } else {
1060                 /* Currently not supported. */
1061                 return (NULL);
1062         }
1063         if (sh->v_tag) {
1064                 /* we only go down this path if vtag is non-zero */
1065                 retval = sctp_findassoc_by_vtag(from, ntohl(sh->v_tag),
1066                     inp_p, netp, sh->src_port, sh->dest_port, 0);
1067                 if (retval) {
1068                         return (retval);
1069                 }
1070         }
1071         if (iph->ip_v == IPVERSION) {
1072                 /* its IPv4 */
1073                 struct sockaddr_in *to4;
1074
1075                 to4 = (struct sockaddr_in *)&to_store;
1076                 bzero(to4, sizeof(*to4));
1077                 to4->sin_family = AF_INET;
1078                 to4->sin_len = sizeof(struct sockaddr_in);
1079                 to4->sin_addr.s_addr = iph->ip_dst.s_addr;
1080                 to4->sin_port = sh->dest_port;
1081         } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1082                 /* its IPv6 */
1083                 struct ip6_hdr *ip6;
1084                 struct sockaddr_in6 *to6;
1085
1086                 ip6 = mtod(m, struct ip6_hdr *);
1087                 to6 = (struct sockaddr_in6 *)&to_store;
1088                 bzero(to6, sizeof(*to6));
1089                 to6->sin6_family = AF_INET6;
1090                 to6->sin6_len = sizeof(struct sockaddr_in6);
1091                 to6->sin6_addr = ip6->ip6_dst;
1092                 to6->sin6_port = sh->dest_port;
1093                 /* Get the scopes in properly to the sin6 addr's */
1094                 /* we probably don't need these operations */
1095                 (void)sa6_recoverscope(to6);
1096                 sa6_embedscope(to6, ip6_use_defzone);
1097         }
1098         find_tcp_pool = 0;
1099         /*
1100          * FIX FIX?, I think we only need to look in the TCP pool if its an
1101          * INIT or COOKIE-ECHO, We really don't need to find it that way if
1102          * its a INIT-ACK or COOKIE_ACK since these in bot one-2-one and
1103          * one-2-N would be in the main pool anyway.
1104          */
1105         if ((ch->chunk_type != SCTP_INITIATION) &&
1106             (ch->chunk_type != SCTP_INITIATION_ACK) &&
1107             (ch->chunk_type != SCTP_COOKIE_ACK) &&
1108             (ch->chunk_type != SCTP_COOKIE_ECHO)) {
1109                 /* Other chunk types go to the tcp pool. */
1110                 find_tcp_pool = 1;
1111         }
1112         if (inp_p) {
1113                 retval = sctp_findassociation_addr_sa(to, from, inp_p, netp,
1114                     find_tcp_pool);
1115                 inp = *inp_p;
1116         } else {
1117                 retval = sctp_findassociation_addr_sa(to, from, &inp, netp,
1118                     find_tcp_pool);
1119         }
1120 #ifdef SCTP_DEBUG
1121         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1122                 printf("retval:%p inp:%p\n", retval, inp);
1123         }
1124 #endif
1125         if (retval == NULL && inp) {
1126                 /* Found a EP but not this address */
1127                 if ((ch->chunk_type == SCTP_INITIATION) ||
1128                     (ch->chunk_type == SCTP_INITIATION_ACK)) {
1129                         /*
1130                          * special hook, we do NOT return linp or an
1131                          * association that is linked to an existing
1132                          * association that is under the TCP pool (i.e. no
1133                          * listener exists). The endpoint finding routine
1134                          * will always find a listner before examining the
1135                          * TCP pool.
1136                          */
1137                         if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
1138                                 if (inp_p) {
1139                                         *inp_p = NULL;
1140                                 }
1141                                 return (NULL);
1142                         }
1143                         retval = sctp_findassociation_special_addr(m, iphlen,
1144                             offset, sh, &inp, netp, to);
1145                         if (inp_p != NULL) {
1146                                 *inp_p = inp;
1147                         }
1148                 }
1149         }
1150 #ifdef SCTP_DEBUG
1151         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1152                 printf("retval is %p\n", retval);
1153         }
1154 #endif
1155         return (retval);
1156 }
1157
1158 /*
1159  * lookup an association by an ASCONF lookup address.
1160  * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
1161  */
1162 struct sctp_tcb *
1163 sctp_findassociation_ep_asconf(struct mbuf *m, int iphlen, int offset,
1164     struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp)
1165 {
1166         struct sctp_tcb *stcb;
1167         struct sockaddr_in *sin;
1168         struct sockaddr_in6 *sin6;
1169         struct sockaddr_storage local_store, remote_store;
1170         struct ip *iph;
1171         struct sctp_paramhdr parm_buf, *phdr;
1172         int ptype;
1173         int zero_address = 0;
1174
1175
1176         memset(&local_store, 0, sizeof(local_store));
1177         memset(&remote_store, 0, sizeof(remote_store));
1178
1179         /* First get the destination address setup too. */
1180         iph = mtod(m, struct ip *);
1181         if (iph->ip_v == IPVERSION) {
1182                 /* its IPv4 */
1183                 sin = (struct sockaddr_in *)&local_store;
1184                 sin->sin_family = AF_INET;
1185                 sin->sin_len = sizeof(*sin);
1186                 sin->sin_port = sh->dest_port;
1187                 sin->sin_addr.s_addr = iph->ip_dst.s_addr;
1188         } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
1189                 /* its IPv6 */
1190                 struct ip6_hdr *ip6;
1191
1192                 ip6 = mtod(m, struct ip6_hdr *);
1193                 sin6 = (struct sockaddr_in6 *)&local_store;
1194                 sin6->sin6_family = AF_INET6;
1195                 sin6->sin6_len = sizeof(*sin6);
1196                 sin6->sin6_port = sh->dest_port;
1197                 sin6->sin6_addr = ip6->ip6_dst;
1198         } else {
1199                 return NULL;
1200         }
1201
1202         phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
1203             &parm_buf, sizeof(struct sctp_paramhdr));
1204         if (phdr == NULL) {
1205 #ifdef SCTP_DEBUG
1206                 if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1207                         printf("findassociation_ep_asconf: failed to get asconf lookup addr\n");
1208                 }
1209 #endif                          /* SCTP_DEBUG */
1210                 return NULL;
1211         }
1212         ptype = (int)((uint32_t) ntohs(phdr->param_type));
1213         /* get the correlation address */
1214         if (ptype == SCTP_IPV6_ADDRESS) {
1215                 /* ipv6 address param */
1216                 struct sctp_ipv6addr_param *p6, p6_buf;
1217
1218                 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
1219                         return NULL;
1220                 }
1221                 p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
1222                     offset + sizeof(struct sctp_asconf_chunk),
1223                     &p6_buf.ph, sizeof(*p6));
1224                 if (p6 == NULL) {
1225 #ifdef SCTP_DEBUG
1226                         if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1227                                 printf("findassociation_ep_asconf: failed to get asconf v6 lookup addr\n");
1228                         }
1229 #endif                          /* SCTP_DEBUG */
1230                         return (NULL);
1231                 }
1232                 sin6 = (struct sockaddr_in6 *)&remote_store;
1233                 sin6->sin6_family = AF_INET6;
1234                 sin6->sin6_len = sizeof(*sin6);
1235                 sin6->sin6_port = sh->src_port;
1236                 memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
1237                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1238                         zero_address = 1;
1239         } else if (ptype == SCTP_IPV4_ADDRESS) {
1240                 /* ipv4 address param */
1241                 struct sctp_ipv4addr_param *p4, p4_buf;
1242
1243                 if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
1244                         return NULL;
1245                 }
1246                 p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
1247                     offset + sizeof(struct sctp_asconf_chunk),
1248                     &p4_buf.ph, sizeof(*p4));
1249                 if (p4 == NULL) {
1250 #ifdef SCTP_DEBUG
1251                         if (sctp_debug_on & SCTP_DEBUG_INPUT3) {
1252                                 printf("findassociation_ep_asconf: failed to get asconf v4 lookup addr\n");
1253                         }
1254 #endif                          /* SCTP_DEBUG */
1255                         return (NULL);
1256                 }
1257                 sin = (struct sockaddr_in *)&remote_store;
1258                 sin->sin_family = AF_INET;
1259                 sin->sin_len = sizeof(*sin);
1260                 sin->sin_port = sh->src_port;
1261                 memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
1262                 if (sin->sin_addr.s_addr == INADDR_ANY)
1263                         zero_address = 1;
1264         } else {
1265                 /* invalid address param type */
1266                 return NULL;
1267         }
1268
1269         if (zero_address) {
1270                 stcb = sctp_findassoc_by_vtag(NULL, ntohl(sh->v_tag), inp_p,
1271                     netp, sh->src_port, sh->dest_port, 1);
1272                 /*
1273                  * printf("findassociation_ep_asconf: zero lookup address
1274                  * finds stcb 0x%x\n", (uint32_t)stcb);
1275                  */
1276         } else {
1277                 stcb = sctp_findassociation_ep_addr(inp_p,
1278                     (struct sockaddr *)&remote_store, netp,
1279                     (struct sockaddr *)&local_store, NULL);
1280         }
1281         return (stcb);
1282 }
1283
1284
1285 extern int sctp_max_burst_default;
1286
1287 extern unsigned int sctp_delayed_sack_time_default;
1288 extern unsigned int sctp_heartbeat_interval_default;
1289 extern unsigned int sctp_pmtu_raise_time_default;
1290 extern unsigned int sctp_shutdown_guard_time_default;
1291 extern unsigned int sctp_secret_lifetime_default;
1292
1293 extern unsigned int sctp_rto_max_default;
1294 extern unsigned int sctp_rto_min_default;
1295 extern unsigned int sctp_rto_initial_default;
1296 extern unsigned int sctp_init_rto_max_default;
1297 extern unsigned int sctp_valid_cookie_life_default;
1298 extern unsigned int sctp_init_rtx_max_default;
1299 extern unsigned int sctp_assoc_rtx_max_default;
1300 extern unsigned int sctp_path_rtx_max_default;
1301 extern unsigned int sctp_nr_outgoing_streams_default;
1302
1303 /*
1304  * allocate a sctp_inpcb and setup a temporary binding to a port/all
1305  * addresses. This way if we don't get a bind we by default pick a ephemeral
1306  * port with all addresses bound.
1307  */
1308 int
1309 sctp_inpcb_alloc(struct socket *so)
1310 {
1311         /*
1312          * we get called when a new endpoint starts up. We need to allocate
1313          * the sctp_inpcb structure from the zone and init it. Mark it as
1314          * unbound and find a port that we can use as an ephemeral with
1315          * INADDR_ANY. If the user binds later no problem we can then add in
1316          * the specific addresses. And setup the default parameters for the
1317          * EP.
1318          */
1319         int i, error;
1320         struct sctp_inpcb *inp;
1321
1322         struct sctp_pcb *m;
1323         struct timeval time;
1324         sctp_sharedkey_t *null_key;
1325
1326         error = 0;
1327
1328         SCTP_INP_INFO_WLOCK();
1329         inp = (struct sctp_inpcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_ep);
1330         if (inp == NULL) {
1331                 printf("Out of SCTP-INPCB structures - no resources\n");
1332                 SCTP_INP_INFO_WUNLOCK();
1333                 return (ENOBUFS);
1334         }
1335         /* zap it */
1336         bzero(inp, sizeof(*inp));
1337
1338         /* bump generations */
1339         /* setup socket pointers */
1340         inp->sctp_socket = so;
1341         inp->ip_inp.inp.inp_socket = so;
1342
1343         inp->partial_delivery_point = so->so_rcv.sb_hiwat >> SCTP_PARTIAL_DELIVERY_SHIFT;
1344         inp->sctp_frag_point = SCTP_DEFAULT_MAXSEGMENT;
1345
1346 #ifdef IPSEC
1347         {
1348                 struct inpcbpolicy *pcb_sp = NULL;
1349
1350                 error = ipsec_init_pcbpolicy(so, &pcb_sp);
1351                 /* Arrange to share the policy */
1352                 inp->ip_inp.inp.inp_sp = pcb_sp;
1353                 ((struct in6pcb *)(&inp->ip_inp.inp))->in6p_sp = pcb_sp;
1354         }
1355         if (error != 0) {
1356                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1357                 SCTP_INP_INFO_WUNLOCK();
1358                 return error;
1359         }
1360 #endif                          /* IPSEC */
1361         SCTP_INCR_EP_COUNT();
1362         inp->ip_inp.inp.inp_ip_ttl = ip_defttl;
1363         SCTP_INP_INFO_WUNLOCK();
1364
1365         so->so_pcb = (caddr_t)inp;
1366
1367         if ((so->so_type == SOCK_DGRAM) ||
1368             (so->so_type == SOCK_SEQPACKET)) {
1369                 /* UDP style socket */
1370                 inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
1371                     SCTP_PCB_FLAGS_UNBOUND);
1372                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1373                 /* Be sure it is NON-BLOCKING IO for UDP */
1374                 /* so->so_state |= SS_NBIO; */
1375         } else if (so->so_type == SOCK_STREAM) {
1376                 /* TCP style socket */
1377                 inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
1378                     SCTP_PCB_FLAGS_UNBOUND);
1379                 sctp_feature_on(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT);
1380                 /* Be sure we have blocking IO by default */
1381                 so->so_state &= ~SS_NBIO;
1382         } else {
1383                 /*
1384                  * unsupported socket type (RAW, etc)- in case we missed it
1385                  * in protosw
1386                  */
1387                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1388                 return (EOPNOTSUPP);
1389         }
1390         inp->sctp_tcbhash = SCTP_HASH_INIT(sctp_pcbtblsize,
1391             &inp->sctp_hashmark);
1392         if (inp->sctp_tcbhash == NULL) {
1393                 printf("Out of SCTP-INPCB->hashinit - no resources\n");
1394                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
1395                 return (ENOBUFS);
1396         }
1397         SCTP_INP_INFO_WLOCK();
1398         SCTP_INP_LOCK_INIT(inp);
1399         SCTP_INP_READ_INIT(inp);
1400         SCTP_ASOC_CREATE_LOCK_INIT(inp);
1401         /* lock the new ep */
1402         SCTP_INP_WLOCK(inp);
1403
1404         /* add it to the info area */
1405         LIST_INSERT_HEAD(&sctppcbinfo.listhead, inp, sctp_list);
1406         SCTP_INP_INFO_WUNLOCK();
1407
1408         TAILQ_INIT(&inp->read_queue);
1409         LIST_INIT(&inp->sctp_addr_list);
1410         LIST_INIT(&inp->sctp_asoc_list);
1411
1412 #ifdef SCTP_TRACK_FREED_ASOCS
1413         /* TEMP CODE */
1414         LIST_INIT(&inp->sctp_asoc_free_list);
1415 #endif
1416         /* Init the timer structure for signature change */
1417         SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
1418         inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
1419
1420         /* now init the actual endpoint default data */
1421         m = &inp->sctp_ep;
1422
1423         /* setup the base timeout information */
1424         m->sctp_timeoutticks[SCTP_TIMER_SEND] = SEC_TO_TICKS(SCTP_SEND_SEC);    /* needed ? */
1425         m->sctp_timeoutticks[SCTP_TIMER_INIT] = SEC_TO_TICKS(SCTP_INIT_SEC);    /* needed ? */
1426         m->sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(sctp_delayed_sack_time_default);
1427         m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = MSEC_TO_TICKS(sctp_heartbeat_interval_default);
1428         m->sctp_timeoutticks[SCTP_TIMER_PMTU] = SEC_TO_TICKS(sctp_pmtu_raise_time_default);
1429         m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = SEC_TO_TICKS(sctp_shutdown_guard_time_default);
1430         m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = SEC_TO_TICKS(sctp_secret_lifetime_default);
1431         /* all max/min max are in ms */
1432         m->sctp_maxrto = sctp_rto_max_default;
1433         m->sctp_minrto = sctp_rto_min_default;
1434         m->initial_rto = sctp_rto_initial_default;
1435         m->initial_init_rto_max = sctp_init_rto_max_default;
1436
1437         m->max_open_streams_intome = MAX_SCTP_STREAMS;
1438
1439         m->max_init_times = sctp_init_rtx_max_default;
1440         m->max_send_times = sctp_assoc_rtx_max_default;
1441         m->def_net_failure = sctp_path_rtx_max_default;
1442         m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
1443         m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
1444         m->max_burst = sctp_max_burst_default;
1445         /* number of streams to pre-open on a association */
1446         m->pre_open_stream_count = sctp_nr_outgoing_streams_default;
1447
1448         /* Add adaptation cookie */
1449         m->adaptation_layer_indicator = 0x504C5253;
1450
1451         /* seed random number generator */
1452         m->random_counter = 1;
1453         m->store_at = SCTP_SIGNATURE_SIZE;
1454         SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
1455         sctp_fill_random_store(m);
1456
1457         /* Minimum cookie size */
1458         m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
1459             sizeof(struct sctp_state_cookie);
1460         m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
1461
1462         /* Setup the initial secret */
1463         SCTP_GETTIME_TIMEVAL(&time);
1464         m->time_of_secret_change = time.tv_sec;
1465
1466         for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
1467                 m->secret_key[0][i] = sctp_select_initial_TSN(m);
1468         }
1469         sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
1470
1471         /* How long is a cookie good for ? */
1472         m->def_cookie_life = sctp_valid_cookie_life_default;
1473
1474         /*
1475          * Initialize authentication parameters
1476          */
1477         m->local_hmacs = sctp_default_supported_hmaclist();
1478         m->local_auth_chunks = sctp_alloc_chunklist();
1479         sctp_auth_set_default_chunks(m->local_auth_chunks);
1480         LIST_INIT(&m->shared_keys);
1481         /* add default NULL key as key id 0 */
1482         null_key = sctp_alloc_sharedkey();
1483         sctp_insert_sharedkey(&m->shared_keys, null_key);
1484         SCTP_INP_WUNLOCK(inp);
1485 #ifdef SCTP_LOG_CLOSING
1486         sctp_log_closing(inp, NULL, 12);
1487 #endif
1488         return (error);
1489 }
1490
1491
1492 void
1493 sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
1494     struct sctp_tcb *stcb)
1495 {
1496         struct sctp_nets *net;
1497         uint16_t lport, rport;
1498         struct sctppcbhead *head;
1499         struct sctp_laddr *laddr, *oladdr;
1500
1501         SCTP_TCB_UNLOCK(stcb);
1502         SCTP_INP_INFO_WLOCK();
1503         SCTP_INP_WLOCK(old_inp);
1504         SCTP_INP_WLOCK(new_inp);
1505         SCTP_TCB_LOCK(stcb);
1506
1507
1508         new_inp->sctp_ep.time_of_secret_change =
1509             old_inp->sctp_ep.time_of_secret_change;
1510         memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
1511             sizeof(old_inp->sctp_ep.secret_key));
1512         new_inp->sctp_ep.current_secret_number =
1513             old_inp->sctp_ep.current_secret_number;
1514         new_inp->sctp_ep.last_secret_number =
1515             old_inp->sctp_ep.last_secret_number;
1516         new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
1517
1518         /* make it so new data pours into the new socket */
1519         stcb->sctp_socket = new_inp->sctp_socket;
1520         stcb->sctp_ep = new_inp;
1521
1522         /* Copy the port across */
1523         lport = new_inp->sctp_lport = old_inp->sctp_lport;
1524         rport = stcb->rport;
1525         /* Pull the tcb from the old association */
1526         LIST_REMOVE(stcb, sctp_tcbhash);
1527         LIST_REMOVE(stcb, sctp_tcblist);
1528
1529         /* Now insert the new_inp into the TCP connected hash */
1530         head = &sctppcbinfo.sctp_tcpephash[SCTP_PCBHASH_ALLADDR((lport + rport),
1531             sctppcbinfo.hashtcpmark)];
1532
1533         LIST_INSERT_HEAD(head, new_inp, sctp_hash);
1534         /* Its safe to access */
1535         new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
1536
1537         /* Now move the tcb into the endpoint list */
1538         LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
1539         /*
1540          * Question, do we even need to worry about the ep-hash since we
1541          * only have one connection? Probably not :> so lets get rid of it
1542          * and not suck up any kernel memory in that.
1543          */
1544
1545         /* Ok. Let's restart timer. */
1546         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1547                 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
1548                     stcb, net);
1549         }
1550
1551         SCTP_INP_INFO_WUNLOCK();
1552         if (new_inp->sctp_tcbhash != NULL) {
1553                 SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
1554                 new_inp->sctp_tcbhash = NULL;
1555         }
1556         if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1557                 /* Subset bound, so copy in the laddr list from the old_inp */
1558                 LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
1559                         laddr = (struct sctp_laddr *)SCTP_ZONE_GET(
1560                             sctppcbinfo.ipi_zone_laddr);
1561                         if (laddr == NULL) {
1562                                 /*
1563                                  * Gak, what can we do? This assoc is really
1564                                  * HOSED. We probably should send an abort
1565                                  * here.
1566                                  */
1567 #ifdef SCTP_DEBUG
1568                                 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1569                                         printf("Association hosed in TCP model, out of laddr memory\n");
1570                                 }
1571 #endif                          /* SCTP_DEBUG */
1572                                 continue;
1573                         }
1574                         SCTP_INCR_LADDR_COUNT();
1575                         bzero(laddr, sizeof(*laddr));
1576                         laddr->ifa = oladdr->ifa;
1577                         LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
1578                             sctp_nxt_addr);
1579                         new_inp->laddr_count++;
1580                 }
1581         }
1582         /*
1583          * Now any running timers need to be adjusted since we really don't
1584          * care if they are running or not just blast in the new_inp into
1585          * all of them.
1586          */
1587
1588         stcb->asoc.hb_timer.ep = (void *)new_inp;
1589         stcb->asoc.dack_timer.ep = (void *)new_inp;
1590         stcb->asoc.asconf_timer.ep = (void *)new_inp;
1591         stcb->asoc.strreset_timer.ep = (void *)new_inp;
1592         stcb->asoc.shut_guard_timer.ep = (void *)new_inp;
1593         stcb->asoc.autoclose_timer.ep = (void *)new_inp;
1594         stcb->asoc.delayed_event_timer.ep = (void *)new_inp;
1595         /* now what about the nets? */
1596         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1597                 net->pmtu_timer.ep = (void *)new_inp;
1598                 net->rxt_timer.ep = (void *)new_inp;
1599                 net->fr_timer.ep = (void *)new_inp;
1600         }
1601         SCTP_INP_WUNLOCK(new_inp);
1602         SCTP_INP_WUNLOCK(old_inp);
1603 }
1604
1605 static int
1606 sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport)
1607 {
1608         struct sctppcbhead *head;
1609         struct sctp_inpcb *t_inp;
1610
1611         head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1612             sctppcbinfo.hashmark)];
1613
1614         LIST_FOREACH(t_inp, head, sctp_hash) {
1615                 if (t_inp->sctp_lport != lport) {
1616                         continue;
1617                 }
1618                 /* This one is in use. */
1619                 /* check the v6/v4 binding issue */
1620                 if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1621                     SCTP_IPV6_V6ONLY(t_inp)) {
1622                         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1623                                 /* collision in V6 space */
1624                                 return (1);
1625                         } else {
1626                                 /* inp is BOUND_V4 no conflict */
1627                                 continue;
1628                         }
1629                 } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1630                         /* t_inp is bound v4 and v6, conflict always */
1631                         return (1);
1632                 } else {
1633                         /* t_inp is bound only V4 */
1634                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1635                             SCTP_IPV6_V6ONLY(t_inp)) {
1636                                 /* no conflict */
1637                                 continue;
1638                         }
1639                         /* else fall through to conflict */
1640                 }
1641                 return (1);
1642         }
1643         return (0);
1644 }
1645
1646
1647
1648 int
1649 sctp_inpcb_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
1650 {
1651         /* bind a ep to a socket address */
1652         struct sctppcbhead *head;
1653         struct sctp_inpcb *inp, *inp_tmp;
1654         struct inpcb *ip_inp;
1655         int bindall;
1656         uint16_t lport;
1657         int error;
1658
1659         lport = 0;
1660         error = 0;
1661         bindall = 1;
1662         inp = (struct sctp_inpcb *)so->so_pcb;
1663         ip_inp = (struct inpcb *)so->so_pcb;
1664 #ifdef SCTP_DEBUG
1665         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1666                 if (addr) {
1667                         printf("Bind called port:%d\n",
1668                             ntohs(((struct sockaddr_in *)addr)->sin_port));
1669                         printf("Addr :");
1670                         sctp_print_address(addr);
1671                 }
1672         }
1673 #endif                          /* SCTP_DEBUG */
1674         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
1675                 /* already did a bind, subsequent binds NOT allowed ! */
1676                 return (EINVAL);
1677         }
1678         if (addr != NULL) {
1679                 if (addr->sa_family == AF_INET) {
1680                         struct sockaddr_in *sin;
1681
1682                         /* IPV6_V6ONLY socket? */
1683                         if (SCTP_IPV6_V6ONLY(ip_inp)) {
1684                                 return (EINVAL);
1685                         }
1686                         if (addr->sa_len != sizeof(*sin))
1687                                 return (EINVAL);
1688
1689                         sin = (struct sockaddr_in *)addr;
1690                         lport = sin->sin_port;
1691
1692                         if (sin->sin_addr.s_addr != INADDR_ANY) {
1693                                 bindall = 0;
1694                         }
1695                 } else if (addr->sa_family == AF_INET6) {
1696                         /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
1697                         struct sockaddr_in6 *sin6;
1698
1699                         sin6 = (struct sockaddr_in6 *)addr;
1700
1701                         if (addr->sa_len != sizeof(*sin6))
1702                                 return (EINVAL);
1703
1704                         lport = sin6->sin6_port;
1705                         if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1706                                 bindall = 0;
1707                                 /* KAME hack: embed scopeid */
1708                                 if (sa6_embedscope(sin6, ip6_use_defzone) != 0)
1709                                         return (EINVAL);
1710                         }
1711                         /* this must be cleared for ifa_ifwithaddr() */
1712                         sin6->sin6_scope_id = 0;
1713                 } else {
1714                         return (EAFNOSUPPORT);
1715                 }
1716         }
1717         SCTP_INP_INFO_WLOCK();
1718         SCTP_INP_WLOCK(inp);
1719         /* increase our count due to the unlock we do */
1720         SCTP_INP_INCR_REF(inp);
1721         if (lport) {
1722                 /*
1723                  * Did the caller specify a port? if so we must see if a ep
1724                  * already has this one bound.
1725                  */
1726                 /* got to be root to get at low ports */
1727                 if (ntohs(lport) < IPPORT_RESERVED) {
1728                         if (p && (error =
1729                             priv_check(p,
1730                             PRIV_NETINET_RESERVEDPORT)
1731                             )) {
1732                                 SCTP_INP_DECR_REF(inp);
1733                                 SCTP_INP_WUNLOCK(inp);
1734                                 SCTP_INP_INFO_WUNLOCK();
1735                                 return (error);
1736                         }
1737                 }
1738                 if (p == NULL) {
1739                         SCTP_INP_DECR_REF(inp);
1740                         SCTP_INP_WUNLOCK(inp);
1741                         SCTP_INP_INFO_WUNLOCK();
1742                         return (error);
1743                 }
1744                 SCTP_INP_WUNLOCK(inp);
1745                 inp_tmp = sctp_pcb_findep(addr, 0, 1);
1746                 if (inp_tmp != NULL) {
1747                         /*
1748                          * lock guy returned and lower count note that we
1749                          * are not bound so inp_tmp should NEVER be inp. And
1750                          * it is this inp (inp_tmp) that gets the reference
1751                          * bump, so we must lower it.
1752                          */
1753                         SCTP_INP_DECR_REF(inp_tmp);
1754                         SCTP_INP_DECR_REF(inp);
1755                         /* unlock info */
1756                         SCTP_INP_INFO_WUNLOCK();
1757                         return (EADDRNOTAVAIL);
1758                 }
1759                 SCTP_INP_WLOCK(inp);
1760                 if (bindall) {
1761                         /* verify that no lport is not used by a singleton */
1762                         if (sctp_isport_inuse(inp, lport)) {
1763                                 /* Sorry someone already has this one bound */
1764                                 SCTP_INP_DECR_REF(inp);
1765                                 SCTP_INP_WUNLOCK(inp);
1766                                 SCTP_INP_INFO_WUNLOCK();
1767                                 return (EADDRNOTAVAIL);
1768                         }
1769                 }
1770         } else {
1771                 /*
1772                  * get any port but lets make sure no one has any address
1773                  * with this port bound
1774                  */
1775
1776                 /*
1777                  * setup the inp to the top (I could use the union but this
1778                  * is just as easy
1779                  */
1780                 uint32_t port_guess;
1781                 uint16_t port_attempt;
1782                 int not_done = 1;
1783
1784                 while (not_done) {
1785                         port_guess = sctp_select_initial_TSN(&inp->sctp_ep);
1786                         port_attempt = (port_guess & 0x0000ffff);
1787                         if (port_attempt == 0) {
1788                                 goto next_half;
1789                         }
1790                         if (port_attempt < IPPORT_RESERVED) {
1791                                 port_attempt += IPPORT_RESERVED;
1792                         }
1793                         if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1794                                 /* got a port we can use */
1795                                 not_done = 0;
1796                                 continue;
1797                         }
1798                         /* try upper half */
1799         next_half:
1800                         port_attempt = ((port_guess >> 16) & 0x0000ffff);
1801                         if (port_attempt == 0) {
1802                                 goto last_try;
1803                         }
1804                         if (port_attempt < IPPORT_RESERVED) {
1805                                 port_attempt += IPPORT_RESERVED;
1806                         }
1807                         if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1808                                 /* got a port we can use */
1809                                 not_done = 0;
1810                                 continue;
1811                         }
1812                         /* try two half's added together */
1813         last_try:
1814                         port_attempt = (((port_guess >> 16) & 0x0000ffff) +
1815                             (port_guess & 0x0000ffff));
1816                         if (port_attempt == 0) {
1817                                 /* get a new random number */
1818                                 continue;
1819                         }
1820                         if (port_attempt < IPPORT_RESERVED) {
1821                                 port_attempt += IPPORT_RESERVED;
1822                         }
1823                         if (sctp_isport_inuse(inp, htons(port_attempt)) == 0) {
1824                                 /* got a port we can use */
1825                                 not_done = 0;
1826                                 continue;
1827                         }
1828                 }
1829                 /* we don't get out of the loop until we have a port */
1830                 lport = htons(port_attempt);
1831         }
1832         SCTP_INP_DECR_REF(inp);
1833         if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
1834             SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
1835                 /*
1836                  * this really should not happen. The guy did a non-blocking
1837                  * bind and then did a close at the same time.
1838                  */
1839                 SCTP_INP_WUNLOCK(inp);
1840                 SCTP_INP_INFO_WUNLOCK();
1841                 return (EINVAL);
1842         }
1843         /* ok we look clear to give out this port, so lets setup the binding */
1844         if (bindall) {
1845                 /* binding to all addresses, so just set in the proper flags */
1846                 inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
1847                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
1848                 /* set the automatic addr changes from kernel flag */
1849                 if (sctp_auto_asconf == 0) {
1850                         sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1851                 } else {
1852                         sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1853                 }
1854         } else {
1855                 /*
1856                  * bind specific, make sure flags is off and add a new
1857                  * address structure to the sctp_addr_list inside the ep
1858                  * structure.
1859                  * 
1860                  * We will need to allocate one and insert it at the head. The
1861                  * socketopt call can just insert new addresses in there as
1862                  * well. It will also have to do the embed scope kame hack
1863                  * too (before adding).
1864                  */
1865                 struct ifaddr *ifa;
1866                 struct sockaddr_storage store_sa;
1867
1868                 memset(&store_sa, 0, sizeof(store_sa));
1869                 if (addr->sa_family == AF_INET) {
1870                         struct sockaddr_in *sin;
1871
1872                         sin = (struct sockaddr_in *)&store_sa;
1873                         memcpy(sin, addr, sizeof(struct sockaddr_in));
1874                         sin->sin_port = 0;
1875                 } else if (addr->sa_family == AF_INET6) {
1876                         struct sockaddr_in6 *sin6;
1877
1878                         sin6 = (struct sockaddr_in6 *)&store_sa;
1879                         memcpy(sin6, addr, sizeof(struct sockaddr_in6));
1880                         sin6->sin6_port = 0;
1881                 }
1882                 /*
1883                  * first find the interface with the bound address need to
1884                  * zero out the port to find the address! yuck! can't do
1885                  * this earlier since need port for sctp_pcb_findep()
1886                  */
1887                 ifa = sctp_find_ifa_by_addr((struct sockaddr *)&store_sa);
1888                 if (ifa == NULL) {
1889                         /* Can't find an interface with that address */
1890                         SCTP_INP_WUNLOCK(inp);
1891                         SCTP_INP_INFO_WUNLOCK();
1892                         return (EADDRNOTAVAIL);
1893                 }
1894                 if (addr->sa_family == AF_INET6) {
1895                         struct in6_ifaddr *ifa6;
1896
1897                         ifa6 = (struct in6_ifaddr *)ifa;
1898                         /*
1899                          * allow binding of deprecated addresses as per RFC
1900                          * 2462 and ipng discussion
1901                          */
1902                         if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
1903                             IN6_IFF_ANYCAST |
1904                             IN6_IFF_NOTREADY)) {
1905                                 /* Can't bind a non-existent addr. */
1906                                 SCTP_INP_WUNLOCK(inp);
1907                                 SCTP_INP_INFO_WUNLOCK();
1908                                 return (EINVAL);
1909                         }
1910                 }
1911                 /* we're not bound all */
1912                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
1913                 /* set the automatic addr changes from kernel flag */
1914                 if (sctp_auto_asconf == 0) {
1915                         sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1916                 } else {
1917                         sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
1918                 }
1919                 /* allow bindx() to send ASCONF's for binding changes */
1920                 sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
1921                 /* add this address to the endpoint list */
1922                 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
1923                 if (error != 0) {
1924                         SCTP_INP_WUNLOCK(inp);
1925                         SCTP_INP_INFO_WUNLOCK();
1926                         return (error);
1927                 }
1928                 inp->laddr_count++;
1929         }
1930         /* find the bucket */
1931         head = &sctppcbinfo.sctp_ephash[SCTP_PCBHASH_ALLADDR(lport,
1932             sctppcbinfo.hashmark)];
1933         /* put it in the bucket */
1934         LIST_INSERT_HEAD(head, inp, sctp_hash);
1935 #ifdef SCTP_DEBUG
1936         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1937                 printf("Main hash to bind at head:%p, bound port:%d\n", head, ntohs(lport));
1938         }
1939 #endif
1940         /* set in the port */
1941         inp->sctp_lport = lport;
1942
1943         /* turn off just the unbound flag */
1944         inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
1945         SCTP_INP_WUNLOCK(inp);
1946         SCTP_INP_INFO_WUNLOCK();
1947         return (0);
1948 }
1949
1950
1951 static void
1952 sctp_iterator_inp_being_freed(struct sctp_inpcb *inp, struct sctp_inpcb *inp_next)
1953 {
1954         struct sctp_iterator *it;
1955
1956         /*
1957          * We enter with the only the ITERATOR_LOCK in place and a write
1958          * lock on the inp_info stuff.
1959          */
1960
1961         /*
1962          * Go through all iterators, we must do this since it is possible
1963          * that some iterator does NOT have the lock, but is waiting for it.
1964          * And the one that had the lock has either moved in the last
1965          * iteration or we just cleared it above. We need to find all of
1966          * those guys. The list of iterators should never be very big
1967          * though.
1968          */
1969         LIST_FOREACH(it, &sctppcbinfo.iteratorhead, sctp_nxt_itr) {
1970                 if (it == inp->inp_starting_point_for_iterator)
1971                         /* skip this guy, he's special */
1972                         continue;
1973                 if (it->inp == inp) {
1974                         /*
1975                          * This is tricky and we DON'T lock the iterator.
1976                          * Reason is he's running but waiting for me since
1977                          * inp->inp_starting_point_for_iterator has the lock
1978                          * on me (the guy above we skipped). This tells us
1979                          * its is not running but waiting for
1980                          * inp->inp_starting_point_for_iterator to be
1981                          * released by the guy that does have our INP in a
1982                          * lock.
1983                          */
1984                         if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1985                                 it->inp = NULL;
1986                                 it->stcb = NULL;
1987                         } else {
1988                                 /* set him up to do the next guy not me */
1989                                 it->inp = inp_next;
1990                                 it->stcb = NULL;
1991                         }
1992                 }
1993         }
1994         it = inp->inp_starting_point_for_iterator;
1995         if (it) {
1996                 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1997                         it->inp = NULL;
1998                 } else {
1999                         it->inp = inp_next;
2000                 }
2001                 it->stcb = NULL;
2002         }
2003 }
2004
2005 /* release sctp_inpcb unbind the port */
2006 void
2007 sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
2008 {
2009         /*
2010          * Here we free a endpoint. We must find it (if it is in the Hash
2011          * table) and remove it from there. Then we must also find it in the
2012          * overall list and remove it from there. After all removals are
2013          * complete then any timer has to be stopped. Then start the actual
2014          * freeing. a) Any local lists. b) Any associations. c) The hash of
2015          * all associations. d) finally the ep itself.
2016          */
2017         struct sctp_pcb *m;
2018         struct sctp_inpcb *inp_save;
2019         struct sctp_tcb *asoc, *nasoc;
2020         struct sctp_laddr *laddr, *nladdr;
2021         struct inpcb *ip_pcb;
2022         struct socket *so;
2023
2024         struct sctp_queued_to_read *sq;
2025
2026         int cnt;
2027         sctp_sharedkey_t *shared_key;
2028
2029
2030 #ifdef SCTP_LOG_CLOSING
2031         sctp_log_closing(inp, NULL, 0);
2032 #endif
2033
2034         SCTP_ITERATOR_LOCK();
2035         so = inp->sctp_socket;
2036         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2037                 /* been here before.. eeks.. get out of here */
2038                 printf("This conflict in free SHOULD not be happening!\n");
2039                 SCTP_ITERATOR_UNLOCK();
2040 #ifdef SCTP_LOG_CLOSING
2041                 sctp_log_closing(inp, NULL, 1);
2042 #endif
2043                 return;
2044         }
2045         SCTP_ASOC_CREATE_LOCK(inp);
2046         SCTP_INP_INFO_WLOCK();
2047
2048         SCTP_INP_WLOCK(inp);
2049         /*
2050          * First time through we have the socket lock, after that no more.
2051          */
2052         if (from == 1) {
2053                 /*
2054                  * Once we are in we can remove the flag from = 1 is only
2055                  * passed from the actual closing routines that are called
2056                  * via the sockets layer.
2057                  */
2058                 inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
2059         }
2060         sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
2061             SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
2062
2063         if (inp->control) {
2064                 sctp_m_freem(inp->control);
2065                 inp->control = NULL;
2066         }
2067         if (inp->pkt) {
2068                 sctp_m_freem(inp->pkt);
2069                 inp->pkt = NULL;
2070         }
2071         m = &inp->sctp_ep;
2072         ip_pcb = &inp->ip_inp.inp;      /* we could just cast the main pointer
2073                                          * here but I will be nice :> (i.e.
2074                                          * ip_pcb = ep;) */
2075         if (immediate == 0) {
2076                 int cnt_in_sd;
2077
2078                 cnt_in_sd = 0;
2079                 for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2080                     asoc = nasoc) {
2081                         nasoc = LIST_NEXT(asoc, sctp_tcblist);
2082                         if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2083                                 /* Skip guys being freed */
2084                                 asoc->sctp_socket = NULL;
2085                                 cnt_in_sd++;
2086                                 continue;
2087                         }
2088                         if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_WAIT) ||
2089                             (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_COOKIE_ECHOED)) {
2090                                 /* Just abandon things in the front states */
2091                                 if (asoc->asoc.total_output_queue_size == 0) {
2092                                         sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_2);
2093                                         continue;
2094                                 }
2095                         }
2096                         SCTP_TCB_LOCK(asoc);
2097                         /* Disconnect the socket please */
2098                         asoc->sctp_socket = NULL;
2099                         asoc->asoc.state |= SCTP_STATE_CLOSED_SOCKET;
2100                         if ((asoc->asoc.size_on_reasm_queue > 0) ||
2101                             (asoc->asoc.control_pdapi) ||
2102                             (asoc->asoc.size_on_all_streams > 0) ||
2103                             (so && (so->so_rcv.sb_cc > 0))
2104                             ) {
2105                                 /* Left with Data unread */
2106                                 struct mbuf *op_err;
2107
2108                                 op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2109                                     0, M_DONTWAIT, 1, MT_DATA);
2110                                 if (op_err) {
2111                                         /* Fill in the user initiated abort */
2112                                         struct sctp_paramhdr *ph;
2113                                         uint32_t *ippp;
2114
2115                                         SCTP_BUF_LEN(op_err) =
2116                                             sizeof(struct sctp_paramhdr) + sizeof(uint32_t);
2117                                         ph = mtod(op_err,
2118                                             struct sctp_paramhdr *);
2119                                         ph->param_type = htons(
2120                                             SCTP_CAUSE_USER_INITIATED_ABT);
2121                                         ph->param_length = htons(SCTP_BUF_LEN(op_err));
2122                                         ippp = (uint32_t *) (ph + 1);
2123                                         *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_3);
2124                                 }
2125                                 asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
2126                                 sctp_send_abort_tcb(asoc, op_err);
2127                                 SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2128                                 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2129                                     (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2130                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2131                                 }
2132                                 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4);
2133                                 continue;
2134                         } else if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2135                                     TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2136                                     (asoc->asoc.stream_queue_cnt == 0)
2137                             ) {
2138                                 if (asoc->asoc.locked_on_sending) {
2139                                         goto abort_anyway;
2140                                 }
2141                                 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_SENT) &&
2142                                     (SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
2143                                         /*
2144                                          * there is nothing queued to send,
2145                                          * so I send shutdown
2146                                          */
2147                                         sctp_send_shutdown(asoc, asoc->asoc.primary_destination);
2148                                         asoc->asoc.state = SCTP_STATE_SHUTDOWN_SENT;
2149                                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2150                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, asoc->sctp_ep, asoc,
2151                                             asoc->asoc.primary_destination);
2152                                         sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2153                                             asoc->asoc.primary_destination);
2154                                         sctp_chunk_output(inp, asoc, SCTP_OUTPUT_FROM_SHUT_TMR);
2155                                 }
2156                         } else {
2157                                 /* mark into shutdown pending */
2158                                 struct sctp_stream_queue_pending *sp;
2159
2160                                 asoc->asoc.state |= SCTP_STATE_SHUTDOWN_PENDING;
2161                                 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, asoc->sctp_ep, asoc,
2162                                     asoc->asoc.primary_destination);
2163                                 if (asoc->asoc.locked_on_sending) {
2164                                         sp = TAILQ_LAST(&((asoc->asoc.locked_on_sending)->outqueue),
2165                                             sctp_streamhead);
2166                                         if (sp == NULL) {
2167                                                 printf("Error, sp is NULL, locked on sending is %p strm:%d\n",
2168                                                     asoc->asoc.locked_on_sending,
2169                                                     asoc->asoc.locked_on_sending->stream_no);
2170                                         } else {
2171                                                 if ((sp->length == 0) && (sp->msg_is_complete == 0))
2172                                                         asoc->asoc.state |= SCTP_STATE_PARTIAL_MSG_LEFT;
2173                                         }
2174                                 }
2175                                 if (TAILQ_EMPTY(&asoc->asoc.send_queue) &&
2176                                     TAILQ_EMPTY(&asoc->asoc.sent_queue) &&
2177                                     (asoc->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
2178                                         struct mbuf *op_err;
2179
2180                         abort_anyway:
2181                                         op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2182                                             0, M_DONTWAIT, 1, MT_DATA);
2183                                         if (op_err) {
2184                                                 /*
2185                                                  * Fill in the user
2186                                                  * initiated abort
2187                                                  */
2188                                                 struct sctp_paramhdr *ph;
2189                                                 uint32_t *ippp;
2190
2191                                                 SCTP_BUF_LEN(op_err) =
2192                                                     (sizeof(struct sctp_paramhdr) +
2193                                                     sizeof(uint32_t));
2194                                                 ph = mtod(op_err,
2195                                                     struct sctp_paramhdr *);
2196                                                 ph->param_type = htons(
2197                                                     SCTP_CAUSE_USER_INITIATED_ABT);
2198                                                 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2199                                                 ippp = (uint32_t *) (ph + 1);
2200                                                 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_5);
2201                                         }
2202                                         asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
2203                                         sctp_send_abort_tcb(asoc, op_err);
2204                                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2205                                         if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2206                                             (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2207                                                 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2208                                         }
2209                                         sctp_free_assoc(inp, asoc, SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_6);
2210                                         continue;
2211                                 }
2212                         }
2213                         cnt_in_sd++;
2214                         SCTP_TCB_UNLOCK(asoc);
2215                 }
2216                 /* now is there some left in our SHUTDOWN state? */
2217                 if (cnt_in_sd) {
2218                         SCTP_INP_WUNLOCK(inp);
2219                         SCTP_ASOC_CREATE_UNLOCK(inp);
2220                         SCTP_INP_INFO_WUNLOCK();
2221                         SCTP_ITERATOR_UNLOCK();
2222 #ifdef SCTP_LOG_CLOSING
2223                         sctp_log_closing(inp, NULL, 2);
2224 #endif
2225                         return;
2226                 }
2227         }
2228         inp->sctp_socket = NULL;
2229         if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) !=
2230             SCTP_PCB_FLAGS_UNBOUND) {
2231                 /*
2232                  * ok, this guy has been bound. It's port is somewhere in
2233                  * the sctppcbinfo hash table. Remove it!
2234                  */
2235                 LIST_REMOVE(inp, sctp_hash);
2236                 inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
2237         }
2238         /*
2239          * If there is a timer running to kill us, forget it, since it may
2240          * have a contest on the INP lock.. which would cause us to die ...
2241          */
2242         cnt = 0;
2243         for ((asoc = LIST_FIRST(&inp->sctp_asoc_list)); asoc != NULL;
2244             asoc = nasoc) {
2245                 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2246                 if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2247                         cnt++;
2248                         continue;
2249                 }
2250                 /* Free associations that are NOT killing us */
2251                 SCTP_TCB_LOCK(asoc);
2252                 if ((SCTP_GET_STATE(&asoc->asoc) != SCTP_STATE_COOKIE_WAIT) &&
2253                     ((asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
2254                         struct mbuf *op_err;
2255                         uint32_t *ippp;
2256
2257                         op_err = sctp_get_mbuf_for_msg((sizeof(struct sctp_paramhdr) + sizeof(uint32_t)),
2258                             0, M_DONTWAIT, 1, MT_DATA);
2259                         if (op_err) {
2260                                 /* Fill in the user initiated abort */
2261                                 struct sctp_paramhdr *ph;
2262
2263                                 SCTP_BUF_LEN(op_err) = (sizeof(struct sctp_paramhdr) +
2264                                     sizeof(uint32_t));
2265                                 ph = mtod(op_err, struct sctp_paramhdr *);
2266                                 ph->param_type = htons(
2267                                     SCTP_CAUSE_USER_INITIATED_ABT);
2268                                 ph->param_length = htons(SCTP_BUF_LEN(op_err));
2269                                 ippp = (uint32_t *) (ph + 1);
2270                                 *ippp = htonl(SCTP_FROM_SCTP_PCB + SCTP_LOC_7);
2271
2272                         }
2273                         asoc->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
2274                         sctp_send_abort_tcb(asoc, op_err);
2275                         SCTP_STAT_INCR_COUNTER32(sctps_aborted);
2276                 } else if (asoc->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2277                         cnt++;
2278                         SCTP_TCB_UNLOCK(asoc);
2279                         continue;
2280                 }
2281                 if ((SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_OPEN) ||
2282                     (SCTP_GET_STATE(&asoc->asoc) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
2283                         SCTP_STAT_DECR_GAUGE32(sctps_currestab);
2284                 }
2285                 sctp_free_assoc(inp, asoc, SCTP_PCBFREE_FORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_8);
2286         }
2287         if (cnt) {
2288                 /* Ok we have someone out there that will kill us */
2289                 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2290                 SCTP_INP_WUNLOCK(inp);
2291                 SCTP_ASOC_CREATE_UNLOCK(inp);
2292                 SCTP_INP_INFO_WUNLOCK();
2293                 SCTP_ITERATOR_UNLOCK();
2294 #ifdef SCTP_LOG_CLOSING
2295                 sctp_log_closing(inp, NULL, 3);
2296 #endif
2297                 return;
2298         }
2299         if ((inp->refcount) || (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
2300                 SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2301                 sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
2302                 SCTP_INP_WUNLOCK(inp);
2303                 SCTP_ASOC_CREATE_UNLOCK(inp);
2304                 SCTP_INP_INFO_WUNLOCK();
2305                 SCTP_ITERATOR_UNLOCK();
2306 #ifdef SCTP_LOG_CLOSING
2307                 sctp_log_closing(inp, NULL, 4);
2308 #endif
2309                 return;
2310         }
2311         SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2312         inp->sctp_ep.signature_change.type = 0;
2313         inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
2314
2315 #ifdef SCTP_LOG_CLOSING
2316         sctp_log_closing(inp, NULL, 5);
2317 #endif
2318
2319         SCTP_OS_TIMER_STOP(&inp->sctp_ep.signature_change.timer);
2320         inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NONE;
2321         /* Clear the read queue */
2322         while ((sq = TAILQ_FIRST(&inp->read_queue)) != NULL) {
2323                 TAILQ_REMOVE(&inp->read_queue, sq, next);
2324                 sctp_free_remote_addr(sq->whoFrom);
2325                 if (so)
2326                         so->so_rcv.sb_cc -= sq->length;
2327                 if (sq->data) {
2328                         sctp_m_freem(sq->data);
2329                         sq->data = NULL;
2330                 }
2331                 /*
2332                  * no need to free the net count, since at this point all
2333                  * assoc's are gone.
2334                  */
2335                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
2336                 SCTP_DECR_READQ_COUNT();
2337         }
2338         /* Now the sctp_pcb things */
2339         /*
2340          * free each asoc if it is not already closed/free. we can't use the
2341          * macro here since le_next will get freed as part of the
2342          * sctp_free_assoc() call.
2343          */
2344         cnt = 0;
2345         if (so) {
2346 #ifdef IPSEC
2347                 ipsec4_delete_pcbpolicy(ip_pcb);
2348 #endif                          /* IPSEC */
2349
2350                 /* Unlocks not needed since the socket is gone now */
2351         }
2352         if (ip_pcb->inp_options) {
2353                 (void)sctp_m_free(ip_pcb->inp_options);
2354                 ip_pcb->inp_options = 0;
2355         }
2356         if (ip_pcb->inp_moptions) {
2357                 ip_freemoptions(ip_pcb->inp_moptions);
2358                 ip_pcb->inp_moptions = 0;
2359         }
2360 #ifdef INET6
2361         if (ip_pcb->inp_vflag & INP_IPV6) {
2362                 struct in6pcb *in6p;
2363
2364                 in6p = (struct in6pcb *)inp;
2365                 ip6_freepcbopts(in6p->in6p_outputopts);
2366         }
2367 #endif                          /* INET6 */
2368         ip_pcb->inp_vflag = 0;
2369         /* free up authentication fields */
2370         if (inp->sctp_ep.local_auth_chunks != NULL)
2371                 sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
2372         if (inp->sctp_ep.local_hmacs != NULL)
2373                 sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
2374
2375         shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2376         while (shared_key) {
2377                 LIST_REMOVE(shared_key, next);
2378                 sctp_free_sharedkey(shared_key);
2379                 shared_key = LIST_FIRST(&inp->sctp_ep.shared_keys);
2380         }
2381
2382         inp_save = LIST_NEXT(inp, sctp_list);
2383         LIST_REMOVE(inp, sctp_list);
2384
2385         /* fix any iterators only after out of the list */
2386         sctp_iterator_inp_being_freed(inp, inp_save);
2387         /*
2388          * if we have an address list the following will free the list of
2389          * ifaddr's that are set into this ep. Again macro limitations here,
2390          * since the LIST_FOREACH could be a bad idea.
2391          */
2392         for ((laddr = LIST_FIRST(&inp->sctp_addr_list)); laddr != NULL;
2393             laddr = nladdr) {
2394                 nladdr = LIST_NEXT(laddr, sctp_nxt_addr);
2395                 LIST_REMOVE(laddr, sctp_nxt_addr);
2396                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
2397                 SCTP_DECR_LADDR_COUNT();
2398         }
2399
2400 #ifdef SCTP_TRACK_FREED_ASOCS
2401         /* TEMP CODE */
2402         for ((asoc = LIST_FIRST(&inp->sctp_asoc_free_list)); asoc != NULL;
2403             asoc = nasoc) {
2404                 nasoc = LIST_NEXT(asoc, sctp_tcblist);
2405                 LIST_REMOVE(asoc, sctp_tcblist);
2406                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, asoc);
2407                 SCTP_DECR_ASOC_COUNT();
2408         }
2409         /* *** END TEMP CODE *** */
2410 #endif
2411         /* Now lets see about freeing the EP hash table. */
2412         if (inp->sctp_tcbhash != NULL) {
2413                 SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2414                 inp->sctp_tcbhash = NULL;
2415         }
2416         /* Now we must put the ep memory back into the zone pool */
2417         SCTP_INP_LOCK_DESTROY(inp);
2418         SCTP_INP_READ_DESTROY(inp);
2419         SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
2420         SCTP_INP_INFO_WUNLOCK();
2421
2422         SCTP_ITERATOR_UNLOCK();
2423
2424         SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_ep, inp);
2425         SCTP_DECR_EP_COUNT();
2426
2427 }
2428
2429
2430 struct sctp_nets *
2431 sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
2432 {
2433         struct sctp_nets *net;
2434
2435         /* locate the address */
2436         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2437                 if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
2438                         return (net);
2439         }
2440         return (NULL);
2441 }
2442
2443
2444 /*
2445  * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
2446  * when a ASCONF arrives that adds it. It will also initialize all the cwnd
2447  * stats of stuff.
2448  */
2449 int
2450 sctp_is_address_on_local_host(struct sockaddr *addr)
2451 {
2452         struct ifnet *ifn;
2453         struct ifaddr *ifa;
2454
2455         TAILQ_FOREACH(ifn, &ifnet, if_list) {
2456                 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
2457                         if (addr->sa_family == ifa->ifa_addr->sa_family) {
2458                                 /* same family */
2459                                 if (addr->sa_family == AF_INET) {
2460                                         struct sockaddr_in *sin, *sin_c;
2461
2462                                         sin = (struct sockaddr_in *)addr;
2463                                         sin_c = (struct sockaddr_in *)
2464                                             ifa->ifa_addr;
2465                                         if (sin->sin_addr.s_addr ==
2466                                             sin_c->sin_addr.s_addr) {
2467                                                 /*
2468                                                  * we are on the same
2469                                                  * machine
2470                                                  */
2471                                                 return (1);
2472                                         }
2473                                 } else if (addr->sa_family == AF_INET6) {
2474                                         struct sockaddr_in6 *sin6, *sin_c6;
2475
2476                                         sin6 = (struct sockaddr_in6 *)addr;
2477                                         sin_c6 = (struct sockaddr_in6 *)
2478                                             ifa->ifa_addr;
2479                                         if (SCTP6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
2480                                             &sin_c6->sin6_addr)) {
2481                                                 /*
2482                                                  * we are on the same
2483                                                  * machine
2484                                                  */
2485                                                 return (1);
2486                                         }
2487                                 }
2488                         }
2489                 }
2490         }
2491         return (0);
2492 }
2493
2494 int
2495 sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
2496     int set_scope, int from)
2497 {
2498         /*
2499          * The following is redundant to the same lines in the
2500          * sctp_aloc_assoc() but is needed since other's call the add
2501          * address function
2502          */
2503         struct sctp_nets *net, *netfirst;
2504         int addr_inscope;
2505
2506 #ifdef SCTP_DEBUG
2507         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2508                 printf("Adding an address (from:%d) to the peer: ", from);
2509                 sctp_print_address(newaddr);
2510         }
2511 #endif
2512
2513         netfirst = sctp_findnet(stcb, newaddr);
2514         if (netfirst) {
2515                 /*
2516                  * Lie and return ok, we don't want to make the association
2517                  * go away for this behavior. It will happen in the TCP
2518                  * model in a connected socket. It does not reach the hash
2519                  * table until after the association is built so it can't be
2520                  * found. Mark as reachable, since the initial creation will
2521                  * have been cleared and the NOT_IN_ASSOC flag will have
2522                  * been added... and we don't want to end up removing it
2523                  * back out.
2524                  */
2525                 if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
2526                         netfirst->dest_state = (SCTP_ADDR_REACHABLE |
2527                             SCTP_ADDR_UNCONFIRMED);
2528                 } else {
2529                         netfirst->dest_state = SCTP_ADDR_REACHABLE;
2530                 }
2531
2532                 return (0);
2533         }
2534         addr_inscope = 1;
2535         if (newaddr->sa_family == AF_INET) {
2536                 struct sockaddr_in *sin;
2537
2538                 sin = (struct sockaddr_in *)newaddr;
2539                 if (sin->sin_addr.s_addr == 0) {
2540                         /* Invalid address */
2541                         return (-1);
2542                 }
2543                 /* zero out the bzero area */
2544                 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
2545
2546                 /* assure len is set */
2547                 sin->sin_len = sizeof(struct sockaddr_in);
2548                 if (set_scope) {
2549 #ifdef SCTP_DONT_DO_PRIVADDR_SCOPE
2550                         stcb->ipv4_local_scope = 1;
2551 #else
2552                         if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
2553                                 stcb->asoc.ipv4_local_scope = 1;
2554                         }
2555 #endif                          /* SCTP_DONT_DO_PRIVADDR_SCOPE */
2556
2557                         if (sctp_is_address_on_local_host(newaddr)) {
2558                                 stcb->asoc.loopback_scope = 1;
2559                                 stcb->asoc.ipv4_local_scope = 1;
2560                                 stcb->asoc.local_scope = 1;
2561                                 stcb->asoc.site_scope = 1;
2562                         }
2563                 } else {
2564                         if (from == SCTP_ADDR_IS_CONFIRMED) {
2565                                 /* From connectx */
2566                                 if (sctp_is_address_on_local_host(newaddr)) {
2567                                         stcb->asoc.loopback_scope = 1;
2568                                         stcb->asoc.ipv4_local_scope = 1;
2569                                         stcb->asoc.local_scope = 1;
2570                                         stcb->asoc.site_scope = 1;
2571                                 }
2572                         }
2573                         /* Validate the address is in scope */
2574                         if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
2575                             (stcb->asoc.ipv4_local_scope == 0)) {
2576                                 addr_inscope = 0;
2577                         }
2578                 }
2579         } else if (newaddr->sa_family == AF_INET6) {
2580                 struct sockaddr_in6 *sin6;
2581
2582                 sin6 = (struct sockaddr_in6 *)newaddr;
2583                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
2584                         /* Invalid address */
2585                         return (-1);
2586                 }
2587                 /* assure len is set */
2588                 sin6->sin6_len = sizeof(struct sockaddr_in6);
2589                 if (set_scope) {
2590                         if (sctp_is_address_on_local_host(newaddr)) {
2591                                 stcb->asoc.loopback_scope = 1;
2592                                 stcb->asoc.local_scope = 1;
2593                                 stcb->asoc.ipv4_local_scope = 1;
2594                                 stcb->asoc.site_scope = 1;
2595                         } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
2596                                 /*
2597                                  * If the new destination is a LINK_LOCAL we
2598                                  * must have common site scope. Don't set
2599                                  * the local scope since we may not share
2600                                  * all links, only loopback can do this.
2601                                  * Links on the local network would also be
2602                                  * on our private network for v4 too.
2603                                  */
2604                                 stcb->asoc.ipv4_local_scope = 1;
2605                                 stcb->asoc.site_scope = 1;
2606                         } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
2607                                 /*
2608                                  * If the new destination is SITE_LOCAL then
2609                                  * we must have site scope in common.
2610                                  */
2611                                 stcb->asoc.site_scope = 1;
2612                         }
2613                 } else {
2614                         if (from == SCTP_ADDR_IS_CONFIRMED) {
2615                                 /* From connectx so we check for localhost. */
2616                                 if (sctp_is_address_on_local_host(newaddr)) {
2617                                         stcb->asoc.loopback_scope = 1;
2618                                         stcb->asoc.ipv4_local_scope = 1;
2619                                         stcb->asoc.local_scope = 1;
2620                                         stcb->asoc.site_scope = 1;
2621                                 }
2622                         }
2623                         /* Validate the address is in scope */
2624                         if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
2625                             (stcb->asoc.loopback_scope == 0)) {
2626                                 addr_inscope = 0;
2627                         } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
2628                             (stcb->asoc.local_scope == 0)) {
2629                                 addr_inscope = 0;
2630                         } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
2631                             (stcb->asoc.site_scope == 0)) {
2632                                 addr_inscope = 0;
2633                         }
2634                 }
2635         } else {
2636                 /* not supported family type */
2637                 return (-1);
2638         }
2639         net = (struct sctp_nets *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_net);
2640         if (net == NULL) {
2641                 return (-1);
2642         }
2643         SCTP_INCR_RADDR_COUNT();
2644         bzero(net, sizeof(*net));
2645         memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
2646         if (newaddr->sa_family == AF_INET) {
2647                 ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
2648         } else if (newaddr->sa_family == AF_INET6) {
2649                 ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
2650         }
2651         net->addr_is_local = sctp_is_address_on_local_host(newaddr);
2652         net->failure_threshold = stcb->asoc.def_net_failure;
2653         if (addr_inscope == 0) {
2654                 net->dest_state = (SCTP_ADDR_REACHABLE |
2655                     SCTP_ADDR_OUT_OF_SCOPE);
2656         } else {
2657                 if (from == SCTP_ADDR_IS_CONFIRMED)
2658                         /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
2659                         net->dest_state = SCTP_ADDR_REACHABLE;
2660                 else
2661                         net->dest_state = SCTP_ADDR_REACHABLE |
2662                             SCTP_ADDR_UNCONFIRMED;
2663         }
2664         net->RTO = stcb->asoc.initial_rto;
2665         stcb->asoc.numnets++;
2666         *(&net->ref_count) = 1;
2667         net->tos_flowlabel = 0;
2668 #ifdef AF_INET
2669         if (newaddr->sa_family == AF_INET)
2670                 net->tos_flowlabel = stcb->asoc.default_tos;
2671 #endif
2672 #ifdef AF_INET6
2673         if (newaddr->sa_family == AF_INET6)
2674                 net->tos_flowlabel = stcb->asoc.default_flowlabel;
2675 #endif
2676         /* Init the timer structure */
2677         SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
2678         SCTP_OS_TIMER_INIT(&net->fr_timer.timer);
2679         SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
2680
2681         /* Now generate a route for this guy */
2682         /* KAME hack: embed scopeid */
2683         if (newaddr->sa_family == AF_INET6) {
2684                 struct sockaddr_in6 *sin6;
2685
2686                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2687                 (void)sa6_embedscope(sin6, ip6_use_defzone);
2688                 sin6->sin6_scope_id = 0;
2689         }
2690         rtalloc_ign((struct route *)&net->ro, 0UL);
2691         if (newaddr->sa_family == AF_INET6) {
2692                 struct sockaddr_in6 *sin6;
2693
2694                 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
2695                 (void)sa6_recoverscope(sin6);
2696         }
2697         if ((net->ro.ro_rt) &&
2698             (net->ro.ro_rt->rt_ifp)) {
2699                 net->mtu = net->ro.ro_rt->rt_ifp->if_mtu;
2700                 if (from == SCTP_ALLOC_ASOC) {
2701                         stcb->asoc.smallest_mtu = net->mtu;
2702                 }
2703                 /* start things off to match mtu of interface please. */
2704                 net->ro.ro_rt->rt_rmx.rmx_mtu = net->ro.ro_rt->rt_ifp->if_mtu;
2705         } else {
2706                 net->mtu = stcb->asoc.smallest_mtu;
2707         }
2708
2709         if (stcb->asoc.smallest_mtu > net->mtu) {
2710                 stcb->asoc.smallest_mtu = net->mtu;
2711         }
2712         /*
2713          * We take the max of the burst limit times a MTU or the
2714          * INITIAL_CWND. We then limit this to 4 MTU's of sending.
2715          */
2716         net->cwnd = min((net->mtu * 4), max((2 * net->mtu), SCTP_INITIAL_CWND));
2717
2718         /* we always get at LEAST 2 MTU's */
2719         if (net->cwnd < (2 * net->mtu)) {
2720                 net->cwnd = 2 * net->mtu;
2721         }
2722         net->ssthresh = stcb->asoc.peers_rwnd;
2723
2724 #if defined(SCTP_CWND_MONITOR) || defined(SCTP_CWND_LOGGING)
2725         sctp_log_cwnd(stcb, net, 0, SCTP_CWND_INITIALIZATION);
2726 #endif
2727
2728         /*
2729          * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
2730          * of assoc (2005/06/27, iyengar@cis.udel.edu)
2731          */
2732         net->find_pseudo_cumack = 1;
2733         net->find_rtx_pseudo_cumack = 1;
2734         net->src_addr_selected = 0;
2735         netfirst = TAILQ_FIRST(&stcb->asoc.nets);
2736         if (net->ro.ro_rt == NULL) {
2737                 /* Since we have no route put it at the back */
2738                 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
2739         } else if (netfirst == NULL) {
2740                 /* We are the first one in the pool. */
2741                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2742         } else if (netfirst->ro.ro_rt == NULL) {
2743                 /*
2744                  * First one has NO route. Place this one ahead of the first
2745                  * one.
2746                  */
2747                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2748         } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
2749                 /*
2750                  * This one has a different interface than the one at the
2751                  * top of the list. Place it ahead.
2752                  */
2753                 TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
2754         } else {
2755                 /*
2756                  * Ok we have the same interface as the first one. Move
2757                  * forward until we find either a) one with a NULL route...
2758                  * insert ahead of that b) one with a different ifp.. insert
2759                  * after that. c) end of the list.. insert at the tail.
2760                  */
2761                 struct sctp_nets *netlook;
2762
2763                 do {
2764                         netlook = TAILQ_NEXT(netfirst, sctp_next);
2765                         if (netlook == NULL) {
2766                                 /* End of the list */
2767                                 TAILQ_INSERT_TAIL(&stcb->asoc.nets, net,
2768                                     sctp_next);
2769                                 break;
2770                         } else if (netlook->ro.ro_rt == NULL) {
2771                                 /* next one has NO route */
2772                                 TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
2773                                 break;
2774                         } else if (netlook->ro.ro_rt->rt_ifp !=
2775                             net->ro.ro_rt->rt_ifp) {
2776                                 TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
2777                                     net, sctp_next);
2778                                 break;
2779                         }
2780                         /* Shift forward */
2781                         netfirst = netlook;
2782                 } while (netlook != NULL);
2783         }
2784
2785         /* got to have a primary set */
2786         if (stcb->asoc.primary_destination == 0) {
2787                 stcb->asoc.primary_destination = net;
2788         } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
2789                     (net->ro.ro_rt) &&
2790             ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
2791                 /* No route to current primary adopt new primary */
2792                 stcb->asoc.primary_destination = net;
2793         }
2794         sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, stcb->sctp_ep, stcb,
2795             net);
2796         /* Validate primary is first */
2797         net = TAILQ_FIRST(&stcb->asoc.nets);
2798         if ((net != stcb->asoc.primary_destination) &&
2799             (stcb->asoc.primary_destination)) {
2800                 /*
2801                  * first one on the list is NOT the primary sctp_cmpaddr()
2802                  * is much more efficent if the primary is the first on the
2803                  * list, make it so.
2804                  */
2805                 TAILQ_REMOVE(&stcb->asoc.nets,
2806                     stcb->asoc.primary_destination, sctp_next);
2807                 TAILQ_INSERT_HEAD(&stcb->asoc.nets,
2808                     stcb->asoc.primary_destination, sctp_next);
2809         }
2810         return (0);
2811 }
2812
2813
2814 /*
2815  * allocate an association and add it to the endpoint. The caller must be
2816  * careful to add all additional addresses once they are know right away or
2817  * else the assoc will be may experience a blackout scenario.
2818  */
2819 struct sctp_tcb *
2820 sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
2821     int for_a_init, int *error, uint32_t override_tag)
2822 {
2823         struct sctp_tcb *stcb;
2824         struct sctp_association *asoc;
2825         struct sctpasochead *head;
2826         uint16_t rport;
2827         int err;
2828
2829         /*
2830          * Assumption made here: Caller has done a
2831          * sctp_findassociation_ep_addr(ep, addr's); to make sure the
2832          * address does not exist already.
2833          */
2834         if (sctppcbinfo.ipi_count_asoc >= SCTP_MAX_NUM_OF_ASOC) {
2835                 /* Hit max assoc, sorry no more */
2836                 *error = ENOBUFS;
2837                 return (NULL);
2838         }
2839         SCTP_INP_RLOCK(inp);
2840         if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2841                 /*
2842                  * If its in the TCP pool, its NOT allowed to create an
2843                  * association. The parent listener needs to call
2844                  * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
2845                  * off, or connected one does this.. its an error.
2846                  */
2847                 SCTP_INP_RUNLOCK(inp);
2848                 *error = EINVAL;
2849                 return (NULL);
2850         }
2851 #ifdef SCTP_DEBUG
2852         if (sctp_debug_on & SCTP_DEBUG_PCB3) {
2853                 printf("Allocate an association for peer:");
2854                 if (firstaddr)
2855                         sctp_print_address(firstaddr);
2856                 else
2857                         printf("None\n");
2858                 printf("Port:%d\n",
2859                     ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
2860         }
2861 #endif                          /* SCTP_DEBUG */
2862         if (firstaddr->sa_family == AF_INET) {
2863                 struct sockaddr_in *sin;
2864
2865                 sin = (struct sockaddr_in *)firstaddr;
2866                 if ((sin->sin_port == 0) || (sin->sin_addr.s_addr == 0)) {
2867                         /* Invalid address */
2868                         SCTP_INP_RUNLOCK(inp);
2869                         *error = EINVAL;
2870                         return (NULL);
2871                 }
2872                 rport = sin->sin_port;
2873         } else if (firstaddr->sa_family == AF_INET6) {
2874                 struct sockaddr_in6 *sin6;
2875
2876                 sin6 = (struct sockaddr_in6 *)firstaddr;
2877                 if ((sin6->sin6_port == 0) ||
2878                     (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))) {
2879                         /* Invalid address */
2880                         SCTP_INP_RUNLOCK(inp);
2881                         *error = EINVAL;
2882                         return (NULL);
2883                 }
2884                 rport = sin6->sin6_port;
2885         } else {
2886                 /* not supported family type */
2887                 SCTP_INP_RUNLOCK(inp);
2888                 *error = EINVAL;
2889                 return (NULL);
2890         }
2891         SCTP_INP_RUNLOCK(inp);
2892         if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
2893                 /*
2894                  * If you have not performed a bind, then we need to do the
2895                  * ephemerial bind for you.
2896                  */
2897                 if ((err = sctp_inpcb_bind(inp->sctp_socket,
2898                     (struct sockaddr *)NULL,
2899                     (struct thread *)NULL
2900                     ))) {
2901                         /* bind error, probably perm */
2902                         *error = err;
2903                         return (NULL);
2904                 }
2905         }
2906         stcb = (struct sctp_tcb *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_asoc);
2907         if (stcb == NULL) {
2908                 /* out of memory? */
2909                 *error = ENOMEM;
2910                 return (NULL);
2911         }
2912         SCTP_INCR_ASOC_COUNT();
2913
2914         bzero(stcb, sizeof(*stcb));
2915         asoc = &stcb->asoc;
2916         SCTP_TCB_LOCK_INIT(stcb);
2917         SCTP_TCB_SEND_LOCK_INIT(stcb);
2918         /* setup back pointer's */
2919         stcb->sctp_ep = inp;
2920         stcb->sctp_socket = inp->sctp_socket;
2921         if ((err = sctp_init_asoc(inp, asoc, for_a_init, override_tag))) {
2922                 /* failed */
2923                 SCTP_TCB_LOCK_DESTROY(stcb);
2924                 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
2925                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2926                 SCTP_DECR_ASOC_COUNT();
2927                 *error = err;
2928                 return (NULL);
2929         }
2930         /* and the port */
2931         stcb->rport = rport;
2932         SCTP_INP_INFO_WLOCK();
2933         SCTP_INP_WLOCK(inp);
2934         if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
2935                 /* inpcb freed while alloc going on */
2936                 SCTP_TCB_LOCK_DESTROY(stcb);
2937                 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
2938                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2939                 SCTP_INP_WUNLOCK(inp);
2940                 SCTP_INP_INFO_WUNLOCK();
2941                 SCTP_DECR_ASOC_COUNT();
2942                 *error = EINVAL;
2943                 return (NULL);
2944         }
2945         SCTP_TCB_LOCK(stcb);
2946
2947         /* now that my_vtag is set, add it to the hash */
2948         head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag,
2949             sctppcbinfo.hashasocmark)];
2950         /* put it in the bucket in the vtag hash of assoc's for the system */
2951         LIST_INSERT_HEAD(head, stcb, sctp_asocs);
2952         SCTP_INP_INFO_WUNLOCK();
2953
2954         if ((err = sctp_add_remote_addr(stcb, firstaddr, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC))) {
2955                 /* failure.. memory error? */
2956                 if (asoc->strmout)
2957                         SCTP_FREE(asoc->strmout);
2958                 if (asoc->mapping_array)
2959                         SCTP_FREE(asoc->mapping_array);
2960
2961                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
2962                 SCTP_DECR_ASOC_COUNT();
2963                 SCTP_TCB_LOCK_DESTROY(stcb);
2964                 SCTP_TCB_SEND_LOCK_DESTROY(stcb);
2965                 *error = ENOBUFS;
2966                 return (NULL);
2967         }
2968         /* Init all the timers */
2969         SCTP_OS_TIMER_INIT(&asoc->hb_timer.timer);
2970         SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
2971         SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
2972         SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
2973         SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
2974         SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
2975         SCTP_OS_TIMER_INIT(&asoc->delayed_event_timer.timer);
2976
2977         LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
2978         /* now file the port under the hash as well */
2979         if (inp->sctp_tcbhash != NULL) {
2980                 head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
2981                     inp->sctp_hashmark)];
2982                 LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
2983         }
2984         SCTP_INP_WUNLOCK(inp);
2985 #ifdef SCTP_DEBUG
2986         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
2987                 printf("Association %p now allocated\n", stcb);
2988         }
2989 #endif
2990         return (stcb);
2991 }
2992
2993
2994 void
2995 sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
2996 {
2997         struct sctp_association *asoc;
2998
2999         asoc = &stcb->asoc;
3000         asoc->numnets--;
3001         TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3002         sctp_free_remote_addr(net);
3003         if (net == asoc->primary_destination) {
3004                 /* Reset primary */
3005                 struct sctp_nets *lnet;
3006
3007                 lnet = TAILQ_FIRST(&asoc->nets);
3008                 /* Try to find a confirmed primary */
3009                 asoc->primary_destination = sctp_find_alternate_net(stcb, lnet,
3010                     0);
3011         }
3012         if (net == asoc->last_data_chunk_from) {
3013                 /* Reset primary */
3014                 asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
3015         }
3016         if (net == asoc->last_control_chunk_from) {
3017                 /* Clear net */
3018                 asoc->last_control_chunk_from = NULL;
3019         }
3020 /*      if (net == asoc->asconf_last_sent_to) {*/
3021         /* Reset primary */
3022 /*              asoc->asconf_last_sent_to = TAILQ_FIRST(&asoc->nets);*/
3023 /*      }*/
3024 }
3025
3026 /*
3027  * remove a remote endpoint address from an association, it will fail if the
3028  * address does not exist.
3029  */
3030 int
3031 sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
3032 {
3033         /*
3034          * Here we need to remove a remote address. This is quite simple, we
3035          * first find it in the list of address for the association
3036          * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
3037          * on that item. Note we do not allow it to be removed if there are
3038          * no other addresses.
3039          */
3040         struct sctp_association *asoc;
3041         struct sctp_nets *net, *net_tmp;
3042
3043         asoc = &stcb->asoc;
3044
3045         /* locate the address */
3046         for (net = TAILQ_FIRST(&asoc->nets); net != NULL; net = net_tmp) {
3047                 net_tmp = TAILQ_NEXT(net, sctp_next);
3048                 if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
3049                         continue;
3050                 }
3051                 if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
3052                     remaddr)) {
3053                         /* we found the guy */
3054                         if (asoc->numnets < 2) {
3055                                 /* Must have at LEAST two remote addresses */
3056                                 return (-1);
3057                         } else {
3058                                 sctp_remove_net(stcb, net);
3059                                 return (0);
3060                         }
3061                 }
3062         }
3063         /* not found. */
3064         return (-2);
3065 }
3066
3067
3068 void
3069 sctp_add_vtag_to_timewait(struct sctp_inpcb *inp, uint32_t tag, uint32_t time)
3070 {
3071         struct sctpvtaghead *chain;
3072         struct sctp_tagblock *twait_block;
3073         struct timeval now;
3074         int set, i;
3075
3076         SCTP_GETTIME_TIMEVAL(&now);
3077         chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
3078         set = 0;
3079         if (!LIST_EMPTY(chain)) {
3080                 /* Block(s) present, lets find space, and expire on the fly */
3081                 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
3082                         for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
3083                                 if ((twait_block->vtag_block[i].v_tag == 0) &&
3084                                     !set) {
3085                                         twait_block->vtag_block[i].tv_sec_at_expire =
3086                                             now.tv_sec + time;
3087                                         twait_block->vtag_block[i].v_tag = tag;
3088                                         set = 1;
3089                                 } else if ((twait_block->vtag_block[i].v_tag) &&
3090                                             ((long)twait_block->vtag_block[i].tv_sec_at_expire >
3091                                     now.tv_sec)) {
3092                                         /* Audit expires this guy */
3093                                         twait_block->vtag_block[i].tv_sec_at_expire = 0;
3094                                         twait_block->vtag_block[i].v_tag = 0;
3095                                         if (set == 0) {
3096                                                 /* Reuse it for my new tag */
3097                                                 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec + SCTP_TIME_WAIT;
3098                                                 twait_block->vtag_block[0].v_tag = tag;
3099                                                 set = 1;
3100                                         }
3101                                 }
3102                         }
3103                         if (set) {
3104                                 /*
3105                                  * We only do up to the block where we can
3106                                  * place our tag for audits
3107                                  */
3108                                 break;
3109                         }
3110                 }
3111         }
3112         /* Need to add a new block to chain */
3113         if (!set) {
3114                 SCTP_MALLOC(twait_block, struct sctp_tagblock *,
3115                     sizeof(struct sctp_tagblock), "TimeWait");
3116                 if (twait_block == NULL) {
3117                         return;
3118                 }
3119                 memset(twait_block, 0, sizeof(struct sctp_tagblock));
3120                 LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
3121                 twait_block->vtag_block[0].tv_sec_at_expire = now.tv_sec +
3122                     SCTP_TIME_WAIT;
3123                 twait_block->vtag_block[0].v_tag = tag;
3124         }
3125 }
3126
3127
3128 static void
3129 sctp_iterator_asoc_being_freed(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
3130 {
3131         struct sctp_iterator *it;
3132
3133         /*
3134          * Unlock the tcb lock we do this so we avoid a dead lock scenario
3135          * where the iterator is waiting on the TCB lock and the TCB lock is
3136          * waiting on the iterator lock.
3137          */
3138         it = stcb->asoc.stcb_starting_point_for_iterator;
3139         if (it == NULL) {
3140                 return;
3141         }
3142         if (it->inp != stcb->sctp_ep) {
3143                 /* hmm, focused on the wrong one? */
3144                 return;
3145         }
3146         if (it->stcb != stcb) {
3147                 return;
3148         }
3149         it->stcb = LIST_NEXT(stcb, sctp_tcblist);
3150         if (it->stcb == NULL) {
3151                 /* done with all asoc's in this assoc */
3152                 if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3153                         it->inp = NULL;
3154                 } else {
3155                         it->inp = LIST_NEXT(inp, sctp_list);
3156                 }
3157         }
3158 }
3159
3160 /*
3161  * Free the association after un-hashing the remote port.
3162  */
3163 int
3164 sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
3165 {
3166         int i;
3167         struct sctp_association *asoc;
3168         struct sctp_nets *net, *prev;
3169         struct sctp_laddr *laddr;
3170         struct sctp_tmit_chunk *chk;
3171         struct sctp_asconf_addr *aparam;
3172         struct sctp_stream_reset_list *liste;
3173         struct sctp_queued_to_read *sq;
3174         struct sctp_stream_queue_pending *sp;
3175         sctp_sharedkey_t *shared_key;
3176         struct socket *so;
3177         int ccnt = 0;
3178         int cnt = 0;
3179
3180         /* first, lets purge the entry from the hash table. */
3181
3182 #ifdef SCTP_LOG_CLOSING
3183         sctp_log_closing(inp, stcb, 6);
3184 #endif
3185         if (stcb->asoc.state == 0) {
3186 #ifdef SCTP_LOG_CLOSING
3187                 sctp_log_closing(inp, NULL, 7);
3188 #endif
3189                 /* there is no asoc, really TSNH :-0 */
3190                 return (1);
3191         }
3192         /* TEMP CODE */
3193         if (stcb->freed_from_where == 0) {
3194                 /* Only record the first place free happened from */
3195                 stcb->freed_from_where = from_location;
3196         }
3197         /* TEMP CODE */
3198
3199         asoc = &stcb->asoc;
3200         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3201             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3202                 /* nothing around */
3203                 so = NULL;
3204         else
3205                 so = inp->sctp_socket;
3206
3207         /*
3208          * We used timer based freeing if a reader or writer is in the way.
3209          * So we first check if we are actually being called from a timer,
3210          * if so we abort early if a reader or writer is still in the way.
3211          */
3212         if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
3213             (from_inpcbfree == SCTP_NORMAL_PROC)) {
3214                 /*
3215                  * is it the timer driving us? if so are the reader/writers
3216                  * gone?
3217                  */
3218                 if (stcb->asoc.refcnt) {
3219                         /* nope, reader or writer in the way */
3220                         sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3221                         /* no asoc destroyed */
3222                         SCTP_TCB_UNLOCK(stcb);
3223 #ifdef SCTP_LOG_CLOSING
3224                         sctp_log_closing(inp, stcb, 8);
3225 #endif
3226                         return (0);
3227                 }
3228         }
3229         /* now clean up any other timers */
3230         SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3231         SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3232         SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3233         SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3234         SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3235         SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3236         SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3237
3238         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3239                 SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3240                 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3241                 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3242         }
3243         /* Now the read queue needs to be cleaned up (only once) */
3244         cnt = 0;
3245         if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
3246                 SCTP_INP_READ_LOCK(inp);
3247                 TAILQ_FOREACH(sq, &inp->read_queue, next) {
3248                         if (sq->stcb == stcb) {
3249                                 sq->do_not_ref_stcb = 1;
3250                                 sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
3251                                 /*
3252                                  * If there is no end, there never will be
3253                                  * now.
3254                                  */
3255                                 if (sq->end_added == 0) {
3256                                         /* Held for PD-API clear that. */
3257                                         sq->pdapi_aborted = 1;
3258                                         sq->held_length = 0;
3259                                         if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3260                                                 /*
3261                                                  * Need to add a PD-API
3262                                                  * aborted indication.
3263                                                  * Setting the control_pdapi
3264                                                  * assures that it will be
3265                                                  * added right after this
3266                                                  * msg.
3267                                                  */
3268                                                 stcb->asoc.control_pdapi = sq;
3269                                                 sctp_notify_partial_delivery_indication(stcb,
3270                                                     SCTP_PARTIAL_DELIVERY_ABORTED, 1);
3271                                                 stcb->asoc.control_pdapi = NULL;
3272                                         }
3273                                 }
3274                                 /* Add an end to wake them */
3275                                 sq->end_added = 1;
3276                                 cnt++;
3277                         }
3278                 }
3279                 SCTP_INP_READ_UNLOCK(inp);
3280                 if (stcb->block_entry) {
3281                         cnt++;
3282                         stcb->block_entry->error = ECONNRESET;
3283                         stcb->block_entry = NULL;
3284                 }
3285         }
3286         stcb->asoc.state |= SCTP_STATE_ABOUT_TO_BE_FREED;
3287         if ((from_inpcbfree != SCTP_PCBFREE_FORCE) && (stcb->asoc.refcnt)) {
3288                 /*
3289                  * reader or writer in the way, we have hopefully given him
3290                  * something to chew on above.
3291                  */
3292                 sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3293                 SCTP_TCB_UNLOCK(stcb);
3294                 if (so) {
3295                         SCTP_INP_RLOCK(inp);
3296                         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3297                             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3298                                 /* nothing around */
3299                                 so = NULL;
3300                         if (so) {
3301                                 /* Wake any reader/writers */
3302                                 sctp_sorwakeup(inp, so);
3303                                 sctp_sowwakeup(inp, so);
3304                         }
3305                         SCTP_INP_RUNLOCK(inp);
3306
3307                 }
3308 #ifdef SCTP_LOG_CLOSING
3309                 sctp_log_closing(inp, stcb, 9);
3310 #endif
3311                 /* no asoc destroyed */
3312                 return (0);
3313         }
3314 #ifdef SCTP_LOG_CLOSING
3315         sctp_log_closing(inp, stcb, 10);
3316 #endif
3317         /*
3318          * When I reach here, no others want to kill the assoc yet.. and I
3319          * own the lock. Now its possible an abort comes in when I do the
3320          * lock exchange below to grab all the locks to do the final take
3321          * out. to prevent this we increment the count, which will start a
3322          * timer and blow out above thus assuring us that we hold exclusive
3323          * killing of the asoc. Note that after getting back the TCB lock we
3324          * will go ahead and increment the counter back up and stop any
3325          * timer a passing stranger may have started :-S
3326          */
3327         if (from_inpcbfree == SCTP_NORMAL_PROC) {
3328                 atomic_add_int(&stcb->asoc.refcnt, 1);
3329
3330                 SCTP_TCB_UNLOCK(stcb);
3331
3332                 SCTP_ITERATOR_LOCK();
3333                 SCTP_INP_INFO_WLOCK();
3334                 SCTP_INP_WLOCK(inp);
3335                 SCTP_TCB_LOCK(stcb);
3336         }
3337         /* Double check the GONE flag */
3338         if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
3339             (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
3340                 /* nothing around */
3341                 so = NULL;
3342
3343         if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3344             (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3345                 /*
3346                  * For TCP type we need special handling when we are
3347                  * connected. We also include the peel'ed off ones to.
3348                  */
3349                 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3350                         inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
3351                         inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
3352                         if (so) {
3353                                 SOCK_LOCK(so);
3354                                 if (so->so_rcv.sb_cc == 0) {
3355                                         so->so_state &= ~(SS_ISCONNECTING |
3356                                             SS_ISDISCONNECTING |
3357                                             SS_ISCONFIRMING |
3358                                             SS_ISCONNECTED);
3359                                 }
3360                                 SOCK_UNLOCK(so);
3361                                 sctp_sowwakeup(inp, so);
3362                                 sctp_sorwakeup(inp, so);
3363                                 wakeup(&so->so_timeo);
3364                         }
3365                 }
3366         }
3367         /*
3368          * Make it invalid too, that way if its about to run it will abort
3369          * and return.
3370          */
3371         sctp_iterator_asoc_being_freed(inp, stcb);
3372         /* re-increment the lock */
3373         if (from_inpcbfree == SCTP_NORMAL_PROC) {
3374                 atomic_add_int(&stcb->asoc.refcnt, -1);
3375         }
3376         asoc->state = 0;
3377         if (inp->sctp_tcbhash) {
3378                 LIST_REMOVE(stcb, sctp_tcbhash);
3379         }
3380         if (stcb->asoc.in_restart_hash) {
3381                 LIST_REMOVE(stcb, sctp_tcbrestarhash);
3382         }
3383         /* Now lets remove it from the list of ALL associations in the EP */
3384         LIST_REMOVE(stcb, sctp_tcblist);
3385         if (from_inpcbfree == SCTP_NORMAL_PROC) {
3386                 SCTP_INP_INCR_REF(inp);
3387                 SCTP_INP_WUNLOCK(inp);
3388                 SCTP_ITERATOR_UNLOCK();
3389         }
3390         /* pull from vtag hash */
3391         LIST_REMOVE(stcb, sctp_asocs);
3392         sctp_add_vtag_to_timewait(inp, asoc->my_vtag, SCTP_TIME_WAIT);
3393
3394
3395         /*
3396          * Now restop the timers to be sure - this is paranoia at is finest!
3397          */
3398         SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3399         SCTP_OS_TIMER_STOP(&asoc->hb_timer.timer);
3400         SCTP_OS_TIMER_STOP(&asoc->dack_timer.timer);
3401         SCTP_OS_TIMER_STOP(&asoc->strreset_timer.timer);
3402         SCTP_OS_TIMER_STOP(&asoc->asconf_timer.timer);
3403         SCTP_OS_TIMER_STOP(&asoc->shut_guard_timer.timer);
3404         SCTP_OS_TIMER_STOP(&asoc->autoclose_timer.timer);
3405         SCTP_OS_TIMER_STOP(&asoc->delayed_event_timer.timer);
3406
3407         TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
3408                 SCTP_OS_TIMER_STOP(&net->fr_timer.timer);
3409                 SCTP_OS_TIMER_STOP(&net->rxt_timer.timer);
3410                 SCTP_OS_TIMER_STOP(&net->pmtu_timer.timer);
3411         }
3412
3413         asoc->strreset_timer.type = SCTP_TIMER_TYPE_NONE;
3414         prev = NULL;
3415         /*
3416          * The chunk lists and such SHOULD be empty but we check them just
3417          * in case.
3418          */
3419         /* anything on the wheel needs to be removed */
3420         for (i = 0; i < asoc->streamoutcnt; i++) {
3421                 struct sctp_stream_out *outs;
3422
3423                 outs = &asoc->strmout[i];
3424                 /* now clean up any chunks here */
3425                 sp = TAILQ_FIRST(&outs->outqueue);
3426                 while (sp) {
3427                         TAILQ_REMOVE(&outs->outqueue, sp, next);
3428                         if (sp->data) {
3429                                 sctp_m_freem(sp->data);
3430                                 sp->data = NULL;
3431                                 sp->tail_mbuf = NULL;
3432                         }
3433                         sctp_free_remote_addr(sp->net);
3434                         sctp_free_spbufspace(stcb, asoc, sp);
3435                         /* Free the zone stuff  */
3436                         SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3437                         SCTP_DECR_STRMOQ_COUNT();
3438                         sp = TAILQ_FIRST(&outs->outqueue);
3439                 }
3440         }
3441
3442         while ((sp = TAILQ_FIRST(&asoc->free_strmoq)) != NULL) {
3443                 TAILQ_REMOVE(&asoc->free_strmoq, sp, next);
3444                 if (sp->data) {
3445                         sctp_m_freem(sp->data);
3446                         sp->data = NULL;
3447                         sp->tail_mbuf = NULL;
3448                 }
3449                 /* Free the zone stuff  */
3450                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_strmoq, sp);
3451                 SCTP_DECR_STRMOQ_COUNT();
3452                 atomic_add_int(&sctppcbinfo.ipi_free_strmoq, -1);
3453         }
3454
3455         while ((liste = TAILQ_FIRST(&asoc->resetHead)) != NULL) {
3456                 TAILQ_REMOVE(&asoc->resetHead, liste, next_resp);
3457                 SCTP_FREE(liste);
3458         }
3459
3460         sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3461         while (sq) {
3462                 TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
3463                 if (sq->data) {
3464                         sctp_m_freem(sq->data);
3465                         sq->data = NULL;
3466                 }
3467                 sctp_free_remote_addr(sq->whoFrom);
3468                 sq->whoFrom = NULL;
3469                 sq->stcb = NULL;
3470                 /* Free the ctl entry */
3471                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, sq);
3472                 SCTP_DECR_READQ_COUNT();
3473                 sq = TAILQ_FIRST(&asoc->pending_reply_queue);
3474         }
3475
3476         chk = TAILQ_FIRST(&asoc->free_chunks);
3477         while (chk) {
3478                 TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
3479                 if (chk->data) {
3480                         sctp_m_freem(chk->data);
3481                         chk->data = NULL;
3482                 }
3483                 ccnt++;
3484                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3485                 SCTP_DECR_CHK_COUNT();
3486                 atomic_subtract_int(&sctppcbinfo.ipi_free_chunks, 1);
3487                 asoc->free_chunk_cnt--;
3488                 chk = TAILQ_FIRST(&asoc->free_chunks);
3489         }
3490         /* pending send queue SHOULD be empty */
3491         if (!TAILQ_EMPTY(&asoc->send_queue)) {
3492                 chk = TAILQ_FIRST(&asoc->send_queue);
3493                 while (chk) {
3494                         TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
3495                         if (chk->data) {
3496                                 sctp_m_freem(chk->data);
3497                                 chk->data = NULL;
3498                         }
3499                         ccnt++;
3500                         sctp_free_remote_addr(chk->whoTo);
3501                         SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3502                         SCTP_DECR_CHK_COUNT();
3503                         chk = TAILQ_FIRST(&asoc->send_queue);
3504                 }
3505         }
3506 /*
3507   if(ccnt) {
3508   printf("Freed %d from send_queue\n", ccnt);
3509   ccnt = 0;
3510   }
3511 */
3512         /* sent queue SHOULD be empty */
3513         if (!TAILQ_EMPTY(&asoc->sent_queue)) {
3514                 chk = TAILQ_FIRST(&asoc->sent_queue);
3515                 while (chk) {
3516                         TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
3517                         if (chk->data) {
3518                                 sctp_m_freem(chk->data);
3519                                 chk->data = NULL;
3520                         }
3521                         ccnt++;
3522                         sctp_free_remote_addr(chk->whoTo);
3523                         SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3524                         SCTP_DECR_CHK_COUNT();
3525                         chk = TAILQ_FIRST(&asoc->sent_queue);
3526                 }
3527         }
3528 /*
3529   if(ccnt) {
3530   printf("Freed %d from sent_queue\n", ccnt);
3531   ccnt = 0;
3532   }
3533 */
3534         /* control queue MAY not be empty */
3535         if (!TAILQ_EMPTY(&asoc->control_send_queue)) {
3536                 chk = TAILQ_FIRST(&asoc->control_send_queue);
3537                 while (chk) {
3538                         TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
3539                         if (chk->data) {
3540                                 sctp_m_freem(chk->data);
3541                                 chk->data = NULL;
3542                         }
3543                         ccnt++;
3544                         sctp_free_remote_addr(chk->whoTo);
3545                         SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3546                         SCTP_DECR_CHK_COUNT();
3547                         chk = TAILQ_FIRST(&asoc->control_send_queue);
3548                 }
3549         }
3550 /*
3551   if(ccnt) {
3552   printf("Freed %d from ctrl_queue\n", ccnt);
3553   ccnt = 0;
3554   }
3555 */
3556         if (!TAILQ_EMPTY(&asoc->reasmqueue)) {
3557                 chk = TAILQ_FIRST(&asoc->reasmqueue);
3558                 while (chk) {
3559                         TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
3560                         if (chk->data) {
3561                                 sctp_m_freem(chk->data);
3562                                 chk->data = NULL;
3563                         }
3564                         sctp_free_remote_addr(chk->whoTo);
3565                         ccnt++;
3566                         SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, chk);
3567                         SCTP_DECR_CHK_COUNT();
3568                         chk = TAILQ_FIRST(&asoc->reasmqueue);
3569                 }
3570         }
3571 /*
3572   if(ccnt) {
3573   printf("Freed %d from reasm_queue\n", ccnt);
3574   ccnt = 0;
3575   }
3576 */
3577         if (asoc->mapping_array) {
3578                 SCTP_FREE(asoc->mapping_array);
3579                 asoc->mapping_array = NULL;
3580         }
3581         /* the stream outs */
3582         if (asoc->strmout) {
3583                 SCTP_FREE(asoc->strmout);
3584                 asoc->strmout = NULL;
3585         }
3586         asoc->streamoutcnt = 0;
3587         if (asoc->strmin) {
3588                 struct sctp_queued_to_read *ctl;
3589                 int i;
3590
3591                 for (i = 0; i < asoc->streamincnt; i++) {
3592                         if (!TAILQ_EMPTY(&asoc->strmin[i].inqueue)) {
3593                                 /* We have somethings on the streamin queue */
3594                                 ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3595                                 while (ctl) {
3596                                         TAILQ_REMOVE(&asoc->strmin[i].inqueue,
3597                                             ctl, next);
3598                                         sctp_free_remote_addr(ctl->whoFrom);
3599                                         if (ctl->data) {
3600                                                 sctp_m_freem(ctl->data);
3601                                                 ctl->data = NULL;
3602                                         }
3603                                         /*
3604                                          * We don't free the address here
3605                                          * since all the net's were freed
3606                                          * above.
3607                                          */
3608                                         SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
3609                                         SCTP_DECR_READQ_COUNT();
3610                                         ctl = TAILQ_FIRST(&asoc->strmin[i].inqueue);
3611                                 }
3612                         }
3613                 }
3614                 SCTP_FREE(asoc->strmin);
3615                 asoc->strmin = NULL;
3616         }
3617         asoc->streamincnt = 0;
3618         while (!TAILQ_EMPTY(&asoc->nets)) {
3619                 net = TAILQ_FIRST(&asoc->nets);
3620                 /* pull from list */
3621                 if ((sctppcbinfo.ipi_count_raddr == 0) || (prev == net)) {
3622 #ifdef INVARIANTS
3623                         panic("no net's left alloc'ed, or list points to itself");
3624 #endif
3625                         break;
3626                 }
3627                 prev = net;
3628                 TAILQ_REMOVE(&asoc->nets, net, sctp_next);
3629                 sctp_free_remote_addr(net);
3630         }
3631
3632         /* local addresses, if any */
3633         while (!LIST_EMPTY(&asoc->sctp_local_addr_list)) {
3634                 laddr = LIST_FIRST(&asoc->sctp_local_addr_list);
3635                 LIST_REMOVE(laddr, sctp_nxt_addr);
3636                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
3637                 SCTP_DECR_LADDR_COUNT();
3638         }
3639         /* pending asconf (address) parameters */
3640         while (!TAILQ_EMPTY(&asoc->asconf_queue)) {
3641                 aparam = TAILQ_FIRST(&asoc->asconf_queue);
3642                 TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
3643                 SCTP_FREE(aparam);
3644         }
3645         if (asoc->last_asconf_ack_sent != NULL) {
3646                 sctp_m_freem(asoc->last_asconf_ack_sent);
3647                 asoc->last_asconf_ack_sent = NULL;
3648         }
3649         /* clean up auth stuff */
3650         if (asoc->local_hmacs)
3651                 sctp_free_hmaclist(asoc->local_hmacs);
3652         if (asoc->peer_hmacs)
3653                 sctp_free_hmaclist(asoc->peer_hmacs);
3654
3655         if (asoc->local_auth_chunks)
3656                 sctp_free_chunklist(asoc->local_auth_chunks);
3657         if (asoc->peer_auth_chunks)
3658                 sctp_free_chunklist(asoc->peer_auth_chunks);
3659
3660         sctp_free_authinfo(&asoc->authinfo);
3661
3662         shared_key = LIST_FIRST(&asoc->shared_keys);
3663         while (shared_key) {
3664                 LIST_REMOVE(shared_key, next);
3665                 sctp_free_sharedkey(shared_key);
3666                 shared_key = LIST_FIRST(&asoc->shared_keys);
3667         }
3668
3669         /* Insert new items here :> */
3670
3671         /* Get rid of LOCK */
3672         SCTP_TCB_LOCK_DESTROY(stcb);
3673         SCTP_TCB_SEND_LOCK_DESTROY(stcb);
3674         if (from_inpcbfree == SCTP_NORMAL_PROC) {
3675                 SCTP_INP_INFO_WUNLOCK();
3676                 SCTP_INP_RLOCK(inp);
3677         }
3678 #ifdef SCTP_TRACK_FREED_ASOCS
3679         if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3680                 /* now clean up the tasoc itself */
3681                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3682                 SCTP_DECR_ASOC_COUNT();
3683         } else {
3684                 LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
3685         }
3686 #else
3687         SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_asoc, stcb);
3688         SCTP_DECR_ASOC_COUNT();
3689 #endif
3690         if (from_inpcbfree == SCTP_NORMAL_PROC) {
3691                 if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3692                         /*
3693                          * If its NOT the inp_free calling us AND sctp_close
3694                          * as been called, we call back...
3695                          */
3696                         SCTP_INP_RUNLOCK(inp);
3697                         /*
3698                          * This will start the kill timer (if we are the
3699                          * lastone) since we hold an increment yet. But this
3700                          * is the only safe way to do this since otherwise
3701                          * if the socket closes at the same time we are here
3702                          * we might collide in the cleanup.
3703                          */
3704                         sctp_inpcb_free(inp, 0, 0);
3705                         SCTP_INP_DECR_REF(inp);
3706                         goto out_of;
3707                 } else {
3708                         /* The socket is still open. */
3709                         SCTP_INP_DECR_REF(inp);
3710                 }
3711         }
3712         if (from_inpcbfree == SCTP_NORMAL_PROC) {
3713                 SCTP_INP_RUNLOCK(inp);
3714         }
3715 out_of:
3716         /* destroyed the asoc */
3717 #ifdef SCTP_LOG_CLOSING
3718         sctp_log_closing(inp, NULL, 11);
3719 #endif
3720         return (1);
3721 }
3722
3723
3724
3725 /*
3726  * determine if a destination is "reachable" based upon the addresses bound
3727  * to the current endpoint (e.g. only v4 or v6 currently bound)
3728  */
3729 /*
3730  * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
3731  * assoc level v4/v6 flags, as the assoc *may* not have the same address
3732  * types bound as its endpoint
3733  */
3734 int
3735 sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
3736 {
3737         struct sctp_inpcb *inp;
3738         int answer;
3739
3740         /*
3741          * No locks here, the TCB, in all cases is already locked and an
3742          * assoc is up. There is either a INP lock by the caller applied (in
3743          * asconf case when deleting an address) or NOT in the HB case,
3744          * however if HB then the INP increment is up and the INP will not
3745          * be removed (on top of the fact that we have a TCB lock). So we
3746          * only want to read the sctp_flags, which is either bound-all or
3747          * not.. no protection needed since once an assoc is up you can't be
3748          * changing your binding.
3749          */
3750         inp = stcb->sctp_ep;
3751         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3752                 /* if bound all, destination is not restricted */
3753                 /*
3754                  * RRS: Question during lock work: Is this correct? If you
3755                  * are bound-all you still might need to obey the V4--V6
3756                  * flags??? IMO this bound-all stuff needs to be removed!
3757                  */
3758                 return (1);
3759         }
3760         /* NOTE: all "scope" checks are done when local addresses are added */
3761         if (destaddr->sa_family == AF_INET6) {
3762                 answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
3763         } else if (destaddr->sa_family == AF_INET) {
3764                 answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
3765         } else {
3766                 /* invalid family, so it's unreachable */
3767                 answer = 0;
3768         }
3769         return (answer);
3770 }
3771
3772 /*
3773  * update the inp_vflags on an endpoint
3774  */
3775 static void
3776 sctp_update_ep_vflag(struct sctp_inpcb *inp)
3777 {
3778         struct sctp_laddr *laddr;
3779
3780         /* first clear the flag */
3781         inp->ip_inp.inp.inp_vflag = 0;
3782         /* set the flag based on addresses on the ep list */
3783         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3784                 if (laddr->ifa == NULL) {
3785 #ifdef SCTP_DEBUG
3786                         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3787                                 printf("An ounce of prevention is worth a pound of cure\n");
3788                         }
3789 #endif                          /* SCTP_DEBUG */
3790                         continue;
3791                 }
3792                 if (laddr->ifa->ifa_addr) {
3793                         continue;
3794                 }
3795                 if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
3796                         inp->ip_inp.inp.inp_vflag |= INP_IPV6;
3797                 } else if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
3798                         inp->ip_inp.inp.inp_vflag |= INP_IPV4;
3799                 }
3800         }
3801 }
3802
3803 /*
3804  * Add the address to the endpoint local address list There is nothing to be
3805  * done if we are bound to all addresses
3806  */
3807 int
3808 sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3809 {
3810         struct sctp_laddr *laddr;
3811         int fnd, error;
3812
3813         fnd = 0;
3814
3815         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3816                 /* You are already bound to all. You have it already */
3817                 return (0);
3818         }
3819         if (ifa->ifa_addr->sa_family == AF_INET6) {
3820                 struct in6_ifaddr *ifa6;
3821
3822                 ifa6 = (struct in6_ifaddr *)ifa;
3823                 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3824                     IN6_IFF_DEPRECATED | IN6_IFF_ANYCAST | IN6_IFF_NOTREADY))
3825                         /* Can't bind a non-existent addr. */
3826                         return (-1);
3827         }
3828         /* first, is it already present? */
3829         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3830                 if (laddr->ifa == ifa) {
3831                         fnd = 1;
3832                         break;
3833                 }
3834         }
3835
3836         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd == 0)) {
3837                 /* Not bound to all */
3838                 error = sctp_insert_laddr(&inp->sctp_addr_list, ifa);
3839                 if (error != 0)
3840                         return (error);
3841                 inp->laddr_count++;
3842                 /* update inp_vflag flags */
3843                 if (ifa->ifa_addr->sa_family == AF_INET6) {
3844                         inp->ip_inp.inp.inp_vflag |= INP_IPV6;
3845                 } else if (ifa->ifa_addr->sa_family == AF_INET) {
3846                         inp->ip_inp.inp.inp_vflag |= INP_IPV4;
3847                 }
3848         }
3849         return (0);
3850 }
3851
3852
3853 /*
3854  * select a new (hopefully reachable) destination net (should only be used
3855  * when we deleted an ep addr that is the only usable source address to reach
3856  * the destination net)
3857  */
3858 static void
3859 sctp_select_primary_destination(struct sctp_tcb *stcb)
3860 {
3861         struct sctp_nets *net;
3862
3863         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3864                 /* for now, we'll just pick the first reachable one we find */
3865                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
3866                         continue;
3867                 if (sctp_destination_is_reachable(stcb,
3868                     (struct sockaddr *)&net->ro._l_addr)) {
3869                         /* found a reachable destination */
3870                         stcb->asoc.primary_destination = net;
3871                 }
3872         }
3873         /* I can't there from here! ...we're gonna die shortly... */
3874 }
3875
3876
3877 /*
3878  * Delete the address from the endpoint local address list There is nothing
3879  * to be done if we are bound to all addresses
3880  */
3881 int
3882 sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct ifaddr *ifa)
3883 {
3884         struct sctp_laddr *laddr;
3885         int fnd;
3886
3887         fnd = 0;
3888         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3889                 /* You are already bound to all. You have it already */
3890                 return (EINVAL);
3891         }
3892         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
3893                 if (laddr->ifa == ifa) {
3894                         fnd = 1;
3895                         break;
3896                 }
3897         }
3898         if (fnd && (inp->laddr_count < 2)) {
3899                 /* can't delete unless there are at LEAST 2 addresses */
3900                 return (-1);
3901         }
3902         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) && (fnd)) {
3903                 /*
3904                  * clean up any use of this address go through our
3905                  * associations and clear any last_used_address that match
3906                  * this one for each assoc, see if a new primary_destination
3907                  * is needed
3908                  */
3909                 struct sctp_tcb *stcb;
3910
3911                 /* clean up "next_addr_touse" */
3912                 if (inp->next_addr_touse == laddr)
3913                         /* delete this address */
3914                         inp->next_addr_touse = NULL;
3915
3916                 /* clean up "last_used_address" */
3917                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3918                         if (stcb->asoc.last_used_address == laddr)
3919                                 /* delete this address */
3920                                 stcb->asoc.last_used_address = NULL;
3921                 }               /* for each tcb */
3922
3923                 /* remove it from the ep list */
3924                 sctp_remove_laddr(laddr);
3925                 inp->laddr_count--;
3926                 /* update inp_vflag flags */
3927                 sctp_update_ep_vflag(inp);
3928                 /* select a new primary destination if needed */
3929                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
3930                         /*
3931                          * presume caller (sctp_asconf.c) already owns INP
3932                          * lock
3933                          */
3934                         SCTP_TCB_LOCK(stcb);
3935                         if (sctp_destination_is_reachable(stcb,
3936                             (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr) == 0) {
3937                                 sctp_select_primary_destination(stcb);
3938                         }
3939                         SCTP_TCB_UNLOCK(stcb);
3940                 }               /* for each tcb */
3941         }
3942         return (0);
3943 }
3944
3945 /*
3946  * Add the addr to the TCB local address list For the BOUNDALL or dynamic
3947  * case, this is a "pending" address list (eg. addresses waiting for an
3948  * ASCONF-ACK response) For the subset binding, static case, this is a
3949  * "valid" address list
3950  */
3951 int
3952 sctp_add_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
3953 {
3954         struct sctp_inpcb *inp;
3955         struct sctp_laddr *laddr;
3956         int error;
3957
3958         /*
3959          * Assumes TCP is locked.. and possiblye the INP. May need to
3960          * confirm/fix that if we need it and is not the case.
3961          */
3962         inp = stcb->sctp_ep;
3963         if (ifa->ifa_addr->sa_family == AF_INET6) {
3964                 struct in6_ifaddr *ifa6;
3965
3966                 ifa6 = (struct in6_ifaddr *)ifa;
3967                 if (ifa6->ia6_flags & (IN6_IFF_DETACHED |
3968                 /* IN6_IFF_DEPRECATED | */
3969                     IN6_IFF_ANYCAST |
3970                     IN6_IFF_NOTREADY))
3971                         /* Can't bind a non-existent addr. */
3972                         return (-1);
3973         }
3974         /* does the address already exist? */
3975         LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
3976                 if (laddr->ifa == ifa) {
3977                         return (-1);
3978                 }
3979         }
3980
3981         /* add to the list */
3982         error = sctp_insert_laddr(&stcb->asoc.sctp_local_addr_list, ifa);
3983         if (error != 0)
3984                 return (error);
3985         return (0);
3986 }
3987
3988 /*
3989  * insert an laddr entry with the given ifa for the desired list
3990  */
3991 int
3992 sctp_insert_laddr(struct sctpladdr *list, struct ifaddr *ifa)
3993 {
3994         struct sctp_laddr *laddr;
3995
3996         laddr = (struct sctp_laddr *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_laddr);
3997         if (laddr == NULL) {
3998                 /* out of memory? */
3999                 return (EINVAL);
4000         }
4001         SCTP_INCR_LADDR_COUNT();
4002         bzero(laddr, sizeof(*laddr));
4003         laddr->ifa = ifa;
4004         /* insert it */
4005         LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
4006
4007         return (0);
4008 }
4009
4010 /*
4011  * Remove an laddr entry from the local address list (on an assoc)
4012  */
4013 void
4014 sctp_remove_laddr(struct sctp_laddr *laddr)
4015 {
4016
4017         /* remove from the list */
4018         LIST_REMOVE(laddr, sctp_nxt_addr);
4019         SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_laddr, laddr);
4020         SCTP_DECR_LADDR_COUNT();
4021 }
4022
4023 /*
4024  * Remove an address from the TCB local address list
4025  */
4026 int
4027 sctp_del_local_addr_assoc(struct sctp_tcb *stcb, struct ifaddr *ifa)
4028 {
4029         struct sctp_inpcb *inp;
4030         struct sctp_laddr *laddr;
4031
4032         /*
4033          * This is called by asconf work. It is assumed that a) The TCB is
4034          * locked and b) The INP is locked. This is true in as much as I can
4035          * trace through the entry asconf code where I did these locks.
4036          * Again, the ASCONF code is a bit different in that it does lock
4037          * the INP during its work often times. This must be since we don't
4038          * want other proc's looking up things while what they are looking
4039          * up is changing :-D
4040          */
4041
4042         inp = stcb->sctp_ep;
4043         /* if subset bound and don't allow ASCONF's, can't delete last */
4044         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4045             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
4046                 if (stcb->asoc.numnets < 2) {
4047                         /* can't delete last address */
4048                         return (-1);
4049                 }
4050         }
4051         LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
4052                 /* remove the address if it exists */
4053                 if (laddr->ifa == NULL)
4054                         continue;
4055                 if (laddr->ifa == ifa) {
4056                         sctp_remove_laddr(laddr);
4057                         return (0);
4058                 }
4059         }
4060
4061         /* address not found! */
4062         return (-1);
4063 }
4064
4065 /*
4066  * Remove an address from the TCB local address list lookup using a sockaddr
4067  * addr
4068  */
4069 int
4070 sctp_del_local_addr_assoc_sa(struct sctp_tcb *stcb, struct sockaddr *sa)
4071 {
4072         struct sctp_inpcb *inp;
4073         struct sctp_laddr *laddr;
4074         struct sockaddr *l_sa;
4075
4076         /*
4077          * This function I find does not seem to have a caller. As such we
4078          * NEED TO DELETE this code. If we do find a caller, the caller MUST
4079          * have locked the TCB at the least and probably the INP as well.
4080          */
4081         inp = stcb->sctp_ep;
4082         /* if subset bound and don't allow ASCONF's, can't delete last */
4083         if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
4084             (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF) == 0)) {
4085                 if (stcb->asoc.numnets < 2) {
4086                         /* can't delete last address */
4087                         return (-1);
4088                 }
4089         }
4090         LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list, sctp_nxt_addr) {
4091                 /* make sure the address exists */
4092                 if (laddr->ifa == NULL)
4093                         continue;
4094                 if (laddr->ifa->ifa_addr == NULL)
4095                         continue;
4096
4097                 l_sa = laddr->ifa->ifa_addr;
4098                 if (l_sa->sa_family == AF_INET6) {
4099                         /* IPv6 address */
4100                         struct sockaddr_in6 *sin1, *sin2;
4101
4102                         sin1 = (struct sockaddr_in6 *)l_sa;
4103                         sin2 = (struct sockaddr_in6 *)sa;
4104                         if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4105                             sizeof(struct in6_addr)) == 0) {
4106                                 /* matched */
4107                                 sctp_remove_laddr(laddr);
4108                                 return (0);
4109                         }
4110                 } else if (l_sa->sa_family == AF_INET) {
4111                         /* IPv4 address */
4112                         struct sockaddr_in *sin1, *sin2;
4113
4114                         sin1 = (struct sockaddr_in *)l_sa;
4115                         sin2 = (struct sockaddr_in *)sa;
4116                         if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
4117                                 /* matched */
4118                                 sctp_remove_laddr(laddr);
4119                                 return (0);
4120                         }
4121                 } else {
4122                         /* invalid family */
4123                         return (-1);
4124                 }
4125         }                       /* end foreach */
4126         /* address not found! */
4127         return (-1);
4128 }
4129
4130 static char sctp_pcb_initialized = 0;
4131
4132 /*
4133  * Temporarily remove for __APPLE__ until we use the Tiger equivalents
4134  */
4135 /* sysctl */
4136 static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
4137 static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
4138
4139 void
4140 sctp_pcb_init()
4141 {
4142         /*
4143          * SCTP initialization for the PCB structures should be called by
4144          * the sctp_init() funciton.
4145          */
4146         int i;
4147
4148         if (sctp_pcb_initialized != 0) {
4149                 /* error I was called twice */
4150                 return;
4151         }
4152         sctp_pcb_initialized = 1;
4153
4154         bzero(&sctpstat, sizeof(struct sctpstat));
4155
4156         /* init the empty list of (All) Endpoints */
4157         LIST_INIT(&sctppcbinfo.listhead);
4158
4159         /* init the iterator head */
4160         LIST_INIT(&sctppcbinfo.iteratorhead);
4161
4162         /* init the hash table of endpoints */
4163         TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &sctp_hashtblsize);
4164         TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &sctp_pcbtblsize);
4165         TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &sctp_chunkscale);
4166         sctppcbinfo.sctp_asochash = SCTP_HASH_INIT((sctp_hashtblsize * 31),
4167             &sctppcbinfo.hashasocmark);
4168         sctppcbinfo.sctp_ephash = SCTP_HASH_INIT(sctp_hashtblsize,
4169             &sctppcbinfo.hashmark);
4170         sctppcbinfo.sctp_tcpephash = SCTP_HASH_INIT(sctp_hashtblsize,
4171             &sctppcbinfo.hashtcpmark);
4172         sctppcbinfo.hashtblsize = sctp_hashtblsize;
4173
4174         /* init the small hash table we use to track restarted asoc's */
4175         sctppcbinfo.sctp_restarthash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE,
4176             &sctppcbinfo.hashrestartmark);
4177
4178         /* init the zones */
4179         /*
4180          * FIX ME: Should check for NULL returns, but if it does fail we are
4181          * doomed to panic anyways... add later maybe.
4182          */
4183         SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_ep, "sctp_ep",
4184             sizeof(struct sctp_inpcb), maxsockets);
4185
4186         SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_asoc, "sctp_asoc",
4187             sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
4188
4189         SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_laddr, "sctp_laddr",
4190             sizeof(struct sctp_laddr),
4191             (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4192
4193         SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_net, "sctp_raddr",
4194             sizeof(struct sctp_nets),
4195             (sctp_max_number_of_assoc * sctp_scale_up_for_address));
4196
4197         SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_chunk, "sctp_chunk",
4198             sizeof(struct sctp_tmit_chunk),
4199             (sctp_max_number_of_assoc * sctp_chunkscale));
4200
4201         SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_readq, "sctp_readq",
4202             sizeof(struct sctp_queued_to_read),
4203             (sctp_max_number_of_assoc * sctp_chunkscale));
4204
4205         SCTP_ZONE_INIT(sctppcbinfo.ipi_zone_strmoq, "sctp_stream_msg_out",
4206             sizeof(struct sctp_stream_queue_pending),
4207             (sctp_max_number_of_assoc * sctp_chunkscale));
4208
4209         /* Master Lock INIT for info structure */
4210         SCTP_INP_INFO_LOCK_INIT();
4211         SCTP_STATLOG_INIT_LOCK();
4212         SCTP_ITERATOR_LOCK_INIT();
4213
4214         SCTP_IPI_COUNT_INIT();
4215         SCTP_IPI_ADDR_INIT();
4216         LIST_INIT(&sctppcbinfo.addr_wq);
4217
4218         /* not sure if we need all the counts */
4219         sctppcbinfo.ipi_count_ep = 0;
4220         /* assoc/tcb zone info */
4221         sctppcbinfo.ipi_count_asoc = 0;
4222         /* local addrlist zone info */
4223         sctppcbinfo.ipi_count_laddr = 0;
4224         /* remote addrlist zone info */
4225         sctppcbinfo.ipi_count_raddr = 0;
4226         /* chunk info */
4227         sctppcbinfo.ipi_count_chunk = 0;
4228
4229         /* socket queue zone info */
4230         sctppcbinfo.ipi_count_readq = 0;
4231
4232         /* stream out queue cont */
4233         sctppcbinfo.ipi_count_strmoq = 0;
4234
4235         sctppcbinfo.ipi_free_strmoq = 0;
4236         sctppcbinfo.ipi_free_chunks = 0;
4237
4238         SCTP_OS_TIMER_INIT(&sctppcbinfo.addr_wq_timer.timer);
4239
4240         /* Init the TIMEWAIT list */
4241         for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
4242                 LIST_INIT(&sctppcbinfo.vtag_timewait[i]);
4243         }
4244
4245 }
4246
4247
4248 int
4249 sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
4250     int iphlen, int offset, int limit, struct sctphdr *sh,
4251     struct sockaddr *altsa)
4252 {
4253         /*
4254          * grub through the INIT pulling addresses and loading them to the
4255          * nets structure in the asoc. The from address in the mbuf should
4256          * also be loaded (if it is not already). This routine can be called
4257          * with either INIT or INIT-ACK's as long as the m points to the IP
4258          * packet and the offset points to the beginning of the parameters.
4259          */
4260         struct sctp_inpcb *inp, *l_inp;
4261         struct sctp_nets *net, *net_tmp;
4262         struct ip *iph;
4263         struct sctp_paramhdr *phdr, parm_buf;
4264         struct sctp_tcb *stcb_tmp;
4265         uint16_t ptype, plen;
4266         struct sockaddr *sa;
4267         struct sockaddr_storage dest_store;
4268         struct sockaddr *local_sa = (struct sockaddr *)&dest_store;
4269         struct sockaddr_in sin;
4270         struct sockaddr_in6 sin6;
4271         uint8_t store[384];
4272         struct sctp_auth_random *random = NULL;
4273         uint16_t random_len = 0;
4274         struct sctp_auth_hmac_algo *hmacs = NULL;
4275         uint16_t hmacs_len = 0;
4276         struct sctp_auth_chunk_list *chunks = NULL;
4277         uint16_t num_chunks = 0;
4278         sctp_key_t *new_key;
4279         uint32_t keylen;
4280         int got_random = 0, got_hmacs = 0, got_chklist = 0;
4281
4282         /* First get the destination address setup too. */
4283         memset(&sin, 0, sizeof(sin));
4284         memset(&sin6, 0, sizeof(sin6));
4285
4286         sin.sin_family = AF_INET;
4287         sin.sin_len = sizeof(sin);
4288         sin.sin_port = stcb->rport;
4289
4290         sin6.sin6_family = AF_INET6;
4291         sin6.sin6_len = sizeof(struct sockaddr_in6);
4292         sin6.sin6_port = stcb->rport;
4293         if (altsa == NULL) {
4294                 iph = mtod(m, struct ip *);
4295                 if (iph->ip_v == IPVERSION) {
4296                         /* its IPv4 */
4297                         struct sockaddr_in *sin_2;
4298
4299                         sin_2 = (struct sockaddr_in *)(local_sa);
4300                         memset(sin_2, 0, sizeof(sin));
4301                         sin_2->sin_family = AF_INET;
4302                         sin_2->sin_len = sizeof(sin);
4303                         sin_2->sin_port = sh->dest_port;
4304                         sin_2->sin_addr.s_addr = iph->ip_dst.s_addr;
4305                         sin.sin_addr = iph->ip_src;
4306                         sa = (struct sockaddr *)&sin;
4307                 } else if (iph->ip_v == (IPV6_VERSION >> 4)) {
4308                         /* its IPv6 */
4309                         struct ip6_hdr *ip6;
4310                         struct sockaddr_in6 *sin6_2;
4311
4312                         ip6 = mtod(m, struct ip6_hdr *);
4313                         sin6_2 = (struct sockaddr_in6 *)(local_sa);
4314                         memset(sin6_2, 0, sizeof(sin6));
4315                         sin6_2->sin6_family = AF_INET6;
4316                         sin6_2->sin6_len = sizeof(struct sockaddr_in6);
4317                         sin6_2->sin6_port = sh->dest_port;
4318                         sin6.sin6_addr = ip6->ip6_src;
4319                         sa = (struct sockaddr *)&sin6;
4320                 } else {
4321                         sa = NULL;
4322                 }
4323         } else {
4324                 /*
4325                  * For cookies we use the src address NOT from the packet
4326                  * but from the original INIT
4327                  */
4328                 sa = altsa;
4329         }
4330         /* Turn off ECN until we get through all params */
4331         stcb->asoc.ecn_allowed = 0;
4332         TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4333                 /* mark all addresses that we have currently on the list */
4334                 net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
4335         }
4336         /* does the source address already exist? if so skip it */
4337         l_inp = inp = stcb->sctp_ep;
4338
4339         atomic_add_int(&stcb->asoc.refcnt, 1);
4340         stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, local_sa, stcb);
4341         atomic_add_int(&stcb->asoc.refcnt, -1);
4342
4343         if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
4344                 /* we must add the source address */
4345                 /* no scope set here since we have a tcb already. */
4346                 if ((sa->sa_family == AF_INET) &&
4347                     (stcb->asoc.ipv4_addr_legal)) {
4348                         if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
4349                                 return (-1);
4350                         }
4351                 } else if ((sa->sa_family == AF_INET6) &&
4352                     (stcb->asoc.ipv6_addr_legal)) {
4353                         if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
4354                                 return (-2);
4355                         }
4356                 }
4357         } else {
4358                 if (net_tmp != NULL && stcb_tmp == stcb) {
4359                         net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
4360                 } else if (stcb_tmp != stcb) {
4361                         /* It belongs to another association? */
4362                         SCTP_TCB_UNLOCK(stcb_tmp);
4363                         return (-3);
4364                 }
4365         }
4366         if (stcb->asoc.state == 0) {
4367                 /* the assoc was freed? */
4368                 return (-4);
4369         }
4370         /* now we must go through each of the params. */
4371         phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
4372         while (phdr) {
4373                 ptype = ntohs(phdr->param_type);
4374                 plen = ntohs(phdr->param_length);
4375                 /*
4376                  * printf("ptype => %0x, plen => %d\n", (uint32_t)ptype,
4377                  * (int)plen);
4378                  */
4379                 if (offset + plen > limit) {
4380                         break;
4381                 }
4382                 if (plen == 0) {
4383                         break;
4384                 }
4385                 if (ptype == SCTP_IPV4_ADDRESS) {
4386                         if (stcb->asoc.ipv4_addr_legal) {
4387                                 struct sctp_ipv4addr_param *p4, p4_buf;
4388
4389                                 /* ok get the v4 address and check/add */
4390                                 phdr = sctp_get_next_param(m, offset,
4391                                     (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
4392                                 if (plen != sizeof(struct sctp_ipv4addr_param) ||
4393                                     phdr == NULL) {
4394                                         return (-5);
4395                                 }
4396                                 p4 = (struct sctp_ipv4addr_param *)phdr;
4397                                 sin.sin_addr.s_addr = p4->addr;
4398                                 sa = (struct sockaddr *)&sin;
4399                                 inp = stcb->sctp_ep;
4400                                 atomic_add_int(&stcb->asoc.refcnt, 1);
4401                                 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4402                                     local_sa, stcb);
4403                                 atomic_add_int(&stcb->asoc.refcnt, -1);
4404
4405                                 if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
4406                                     inp == NULL) {
4407                                         /* we must add the source address */
4408                                         /*
4409                                          * no scope set since we have a tcb
4410                                          * already
4411                                          */
4412
4413                                         /*
4414                                          * we must validate the state again
4415                                          * here
4416                                          */
4417                                         if (stcb->asoc.state == 0) {
4418                                                 /* the assoc was freed? */
4419                                                 return (-7);
4420                                         }
4421                                         if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
4422                                                 return (-8);
4423                                         }
4424                                 } else if (stcb_tmp == stcb) {
4425                                         if (stcb->asoc.state == 0) {
4426                                                 /* the assoc was freed? */
4427                                                 return (-10);
4428                                         }
4429                                         if (net != NULL) {
4430                                                 /* clear flag */
4431                                                 net->dest_state &=
4432                                                     ~SCTP_ADDR_NOT_IN_ASSOC;
4433                                         }
4434                                 } else {
4435                                         /*
4436                                          * strange, address is in another
4437                                          * assoc? straighten out locks.
4438                                          */
4439                                         if (stcb->asoc.state == 0) {
4440                                                 /* the assoc was freed? */
4441                                                 return (-12);
4442                                         }
4443                                         return (-13);
4444                                 }
4445                         }
4446                 } else if (ptype == SCTP_IPV6_ADDRESS) {
4447                         if (stcb->asoc.ipv6_addr_legal) {
4448                                 /* ok get the v6 address and check/add */
4449                                 struct sctp_ipv6addr_param *p6, p6_buf;
4450
4451                                 phdr = sctp_get_next_param(m, offset,
4452                                     (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
4453                                 if (plen != sizeof(struct sctp_ipv6addr_param) ||
4454                                     phdr == NULL) {
4455                                         return (-14);
4456                                 }
4457                                 p6 = (struct sctp_ipv6addr_param *)phdr;
4458                                 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
4459                                     sizeof(p6->addr));
4460                                 sa = (struct sockaddr *)&sin6;
4461                                 inp = stcb->sctp_ep;
4462                                 atomic_add_int(&stcb->asoc.refcnt, 1);
4463                                 stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
4464                                     local_sa, stcb);
4465                                 atomic_add_int(&stcb->asoc.refcnt, -1);
4466                                 if (stcb_tmp == NULL && (inp == stcb->sctp_ep ||
4467                                     inp == NULL)) {
4468                                         /*
4469                                          * we must validate the state again
4470                                          * here
4471                                          */
4472                                         if (stcb->asoc.state == 0) {
4473                                                 /* the assoc was freed? */
4474                                                 return (-16);
4475                                         }
4476                                         /*
4477                                          * we must add the address, no scope
4478                                          * set
4479                                          */
4480                                         if (sctp_add_remote_addr(stcb, sa, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
4481                                                 return (-17);
4482                                         }
4483                                 } else if (stcb_tmp == stcb) {
4484                                         /*
4485                                          * we must validate the state again
4486                                          * here
4487                                          */
4488                                         if (stcb->asoc.state == 0) {
4489                                                 /* the assoc was freed? */
4490                                                 return (-19);
4491                                         }
4492                                         if (net != NULL) {
4493                                                 /* clear flag */
4494                                                 net->dest_state &=
4495                                                     ~SCTP_ADDR_NOT_IN_ASSOC;
4496                                         }
4497                                 } else {
4498                                         /*
4499                                          * strange, address is in another
4500                                          * assoc? straighten out locks.
4501                                          */
4502                                         if (stcb->asoc.state == 0) {
4503                                                 /* the assoc was freed? */
4504                                                 return (-21);
4505                                         }
4506                                         return (-22);
4507                                 }
4508                         }
4509                 } else if (ptype == SCTP_ECN_CAPABLE) {
4510                         stcb->asoc.ecn_allowed = 1;
4511                 } else if (ptype == SCTP_ULP_ADAPTATION) {
4512                         if (stcb->asoc.state != SCTP_STATE_OPEN) {
4513                                 struct sctp_adaptation_layer_indication ai,
4514                                                                 *aip;
4515
4516                                 phdr = sctp_get_next_param(m, offset,
4517                                     (struct sctp_paramhdr *)&ai, sizeof(ai));
4518                                 aip = (struct sctp_adaptation_layer_indication *)phdr;
4519                                 sctp_ulp_notify(SCTP_NOTIFY_ADAPTATION_INDICATION,
4520                                     stcb, ntohl(aip->indication), NULL);
4521                         }
4522                 } else if (ptype == SCTP_SET_PRIM_ADDR) {
4523                         struct sctp_asconf_addr_param lstore, *fee;
4524                         struct sctp_asconf_addrv4_param *fii;
4525                         int lptype;
4526                         struct sockaddr *lsa = NULL;
4527
4528                         stcb->asoc.peer_supports_asconf = 1;
4529                         if (plen > sizeof(lstore)) {
4530                                 return (-23);
4531                         }
4532                         phdr = sctp_get_next_param(m, offset,
4533                             (struct sctp_paramhdr *)&lstore, plen);
4534                         if (phdr == NULL) {
4535                                 return (-24);
4536                         }
4537                         fee = (struct sctp_asconf_addr_param *)phdr;
4538                         lptype = ntohs(fee->addrp.ph.param_type);
4539                         if (lptype == SCTP_IPV4_ADDRESS) {
4540                                 if (plen !=
4541                                     sizeof(struct sctp_asconf_addrv4_param)) {
4542                                         printf("Sizeof setprim in init/init ack not %d but %d - ignored\n",
4543                                             (int)sizeof(struct sctp_asconf_addrv4_param),
4544                                             plen);
4545                                 } else {
4546                                         fii = (struct sctp_asconf_addrv4_param *)fee;
4547                                         sin.sin_addr.s_addr = fii->addrp.addr;
4548                                         lsa = (struct sockaddr *)&sin;
4549                                 }
4550                         } else if (lptype == SCTP_IPV6_ADDRESS) {
4551                                 if (plen !=
4552                                     sizeof(struct sctp_asconf_addr_param)) {
4553                                         printf("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
4554                                             (int)sizeof(struct sctp_asconf_addr_param),
4555                                             plen);
4556                                 } else {
4557                                         memcpy(sin6.sin6_addr.s6_addr,
4558                                             fee->addrp.addr,
4559                                             sizeof(fee->addrp.addr));
4560                                         lsa = (struct sockaddr *)&sin6;
4561                                 }
4562                         }
4563                         if (lsa) {
4564                                 sctp_set_primary_addr(stcb, sa, NULL);
4565                         }
4566                 } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
4567                         /* Peer supports pr-sctp */
4568                         stcb->asoc.peer_supports_prsctp = 1;
4569                 } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
4570                         /* A supported extension chunk */
4571                         struct sctp_supported_chunk_types_param *pr_supported;
4572                         uint8_t local_store[128];
4573                         int num_ent, i;
4574
4575                         phdr = sctp_get_next_param(m, offset,
4576                             (struct sctp_paramhdr *)&local_store, plen);
4577                         if (phdr == NULL) {
4578                                 return (-25);
4579                         }
4580                         stcb->asoc.peer_supports_asconf = 0;
4581                         stcb->asoc.peer_supports_prsctp = 0;
4582                         stcb->asoc.peer_supports_pktdrop = 0;
4583                         stcb->asoc.peer_supports_strreset = 0;
4584                         stcb->asoc.peer_supports_auth = 0;
4585                         pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
4586                         num_ent = plen - sizeof(struct sctp_paramhdr);
4587                         for (i = 0; i < num_ent; i++) {
4588                                 switch (pr_supported->chunk_types[i]) {
4589                                 case SCTP_ASCONF:
4590                                 case SCTP_ASCONF_ACK:
4591                                         stcb->asoc.peer_supports_asconf = 1;
4592                                         break;
4593                                 case SCTP_FORWARD_CUM_TSN:
4594                                         stcb->asoc.peer_supports_prsctp = 1;
4595                                         break;
4596                                 case SCTP_PACKET_DROPPED:
4597                                         stcb->asoc.peer_supports_pktdrop = 1;
4598                                         break;
4599                                 case SCTP_STREAM_RESET:
4600                                         stcb->asoc.peer_supports_strreset = 1;
4601                                         break;
4602                                 case SCTP_AUTHENTICATION:
4603                                         stcb->asoc.peer_supports_auth = 1;
4604                                         break;
4605                                 default:
4606                                         /* one I have not learned yet */
4607                                         break;
4608
4609                                 }
4610                         }
4611                 } else if (ptype == SCTP_ECN_NONCE_SUPPORTED) {
4612                         /* Peer supports ECN-nonce */
4613                         stcb->asoc.peer_supports_ecn_nonce = 1;
4614                         stcb->asoc.ecn_nonce_allowed = 1;
4615                 } else if (ptype == SCTP_RANDOM) {
4616                         if (plen > sizeof(store))
4617                                 break;
4618                         if (got_random) {
4619                                 /* already processed a RANDOM */
4620                                 goto next_param;
4621                         }
4622                         phdr = sctp_get_next_param(m, offset,
4623                             (struct sctp_paramhdr *)store,
4624                             plen);
4625                         if (phdr == NULL)
4626                                 return (-26);
4627                         random = (struct sctp_auth_random *)phdr;
4628                         random_len = plen - sizeof(*random);
4629                         /* enforce the random length */
4630                         if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
4631 #ifdef SCTP_DEBUG
4632                                 if (sctp_debug_on & SCTP_DEBUG_AUTH1)
4633                                         printf("SCTP: invalid RANDOM len\n");
4634 #endif
4635                                 return (-27);
4636                         }
4637                         got_random = 1;
4638                 } else if (ptype == SCTP_HMAC_LIST) {
4639                         int num_hmacs;
4640                         int i;
4641
4642                         if (plen > sizeof(store))
4643                                 break;
4644                         if (got_hmacs) {
4645                                 /* already processed a HMAC list */
4646                                 goto next_param;
4647                         }
4648                         phdr = sctp_get_next_param(m, offset,
4649                             (struct sctp_paramhdr *)store,
4650                             plen);
4651                         if (phdr == NULL)
4652                                 return (-28);
4653                         hmacs = (struct sctp_auth_hmac_algo *)phdr;
4654                         hmacs_len = plen - sizeof(*hmacs);
4655                         num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
4656                         /* validate the hmac list */
4657                         if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
4658                                 return (-29);
4659                         }
4660                         if (stcb->asoc.peer_hmacs != NULL)
4661                                 sctp_free_hmaclist(stcb->asoc.peer_hmacs);
4662                         stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
4663                         if (stcb->asoc.peer_hmacs != NULL) {
4664                                 for (i = 0; i < num_hmacs; i++) {
4665                                         sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
4666                                             ntohs(hmacs->hmac_ids[i]));
4667                                 }
4668                         }
4669                         got_hmacs = 1;
4670                 } else if (ptype == SCTP_CHUNK_LIST) {
4671                         int i;
4672
4673                         if (plen > sizeof(store))
4674                                 break;
4675                         if (got_chklist) {
4676                                 /* already processed a Chunks list */
4677                                 goto next_param;
4678                         }
4679                         phdr = sctp_get_next_param(m, offset,
4680                             (struct sctp_paramhdr *)store,
4681                             plen);
4682                         if (phdr == NULL)
4683                                 return (-30);
4684                         chunks = (struct sctp_auth_chunk_list *)phdr;
4685                         num_chunks = plen - sizeof(*chunks);
4686                         if (stcb->asoc.peer_auth_chunks != NULL)
4687                                 sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
4688                         else
4689                                 stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
4690                         for (i = 0; i < num_chunks; i++) {
4691                                 sctp_auth_add_chunk(chunks->chunk_types[i],
4692                                     stcb->asoc.peer_auth_chunks);
4693                         }
4694                         got_chklist = 1;
4695                 } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
4696                             (ptype == SCTP_STATE_COOKIE) ||
4697                             (ptype == SCTP_UNRECOG_PARAM) ||
4698                             (ptype == SCTP_COOKIE_PRESERVE) ||
4699                             (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
4700                             (ptype == SCTP_ADD_IP_ADDRESS) ||
4701                             (ptype == SCTP_DEL_IP_ADDRESS) ||
4702                             (ptype == SCTP_ERROR_CAUSE_IND) ||
4703                     (ptype == SCTP_SUCCESS_REPORT)) {
4704                          /* don't care */ ;
4705                 } else {
4706                         if ((ptype & 0x8000) == 0x0000) {
4707                                 /*
4708                                  * must stop processing the rest of the
4709                                  * param's. Any report bits were handled
4710                                  * with the call to
4711                                  * sctp_arethere_unrecognized_parameters()
4712                                  * when the INIT or INIT-ACK was first seen.
4713                                  */
4714                                 break;
4715                         }
4716                 }
4717 next_param:
4718                 offset += SCTP_SIZE32(plen);
4719                 if (offset >= limit) {
4720                         break;
4721                 }
4722                 phdr = sctp_get_next_param(m, offset, &parm_buf,
4723                     sizeof(parm_buf));
4724         }
4725         /* Now check to see if we need to purge any addresses */
4726         for (net = TAILQ_FIRST(&stcb->asoc.nets); net != NULL; net = net_tmp) {
4727                 net_tmp = TAILQ_NEXT(net, sctp_next);
4728                 if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
4729                     SCTP_ADDR_NOT_IN_ASSOC) {
4730                         /* This address has been removed from the asoc */
4731                         /* remove and free it */
4732                         stcb->asoc.numnets--;
4733                         TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
4734                         sctp_free_remote_addr(net);
4735                         if (net == stcb->asoc.primary_destination) {
4736                                 stcb->asoc.primary_destination = NULL;
4737                                 sctp_select_primary_destination(stcb);
4738                         }
4739                 }
4740         }
4741         /* validate authentication required parameters */
4742         if (got_random && got_hmacs) {
4743                 stcb->asoc.peer_supports_auth = 1;
4744         } else {
4745                 stcb->asoc.peer_supports_auth = 0;
4746         }
4747         if (!sctp_asconf_auth_nochk && stcb->asoc.peer_supports_asconf &&
4748             !stcb->asoc.peer_supports_auth) {
4749                 return (-31);
4750         }
4751         /* concatenate the full random key */
4752         keylen = random_len + num_chunks + hmacs_len;
4753         new_key = sctp_alloc_key(keylen);
4754         if (new_key != NULL) {
4755                 /* copy in the RANDOM */
4756                 if (random != NULL)
4757                         bcopy(random->random_data, new_key->key, random_len);
4758                 /* append in the AUTH chunks */
4759                 if (chunks != NULL)
4760                         bcopy(chunks->chunk_types, new_key->key + random_len,
4761                             num_chunks);
4762                 /* append in the HMACs */
4763                 if (hmacs != NULL)
4764                         bcopy(hmacs->hmac_ids, new_key->key + random_len + num_chunks,
4765                             hmacs_len);
4766         } else {
4767                 return (-32);
4768         }
4769         if (stcb->asoc.authinfo.peer_random != NULL)
4770                 sctp_free_key(stcb->asoc.authinfo.peer_random);
4771         stcb->asoc.authinfo.peer_random = new_key;
4772 #ifdef SCTP_AUTH_DRAFT_04
4773         /* don't include the chunks and hmacs for draft -04 */
4774         stcb->asoc.authinfo.peer_random->keylen = random_len;
4775 #endif
4776         sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
4777         sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
4778
4779         return (0);
4780 }
4781
4782 int
4783 sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
4784     struct sctp_nets *net)
4785 {
4786         /* make sure the requested primary address exists in the assoc */
4787         if (net == NULL && sa)
4788                 net = sctp_findnet(stcb, sa);
4789
4790         if (net == NULL) {
4791                 /* didn't find the requested primary address! */
4792                 return (-1);
4793         } else {
4794                 /* set the primary address */
4795                 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
4796                         /* Must be confirmed */
4797                         return (-1);
4798                 }
4799                 stcb->asoc.primary_destination = net;
4800                 net->dest_state &= ~SCTP_ADDR_WAS_PRIMARY;
4801                 net = TAILQ_FIRST(&stcb->asoc.nets);
4802                 if (net != stcb->asoc.primary_destination) {
4803                         /*
4804                          * first one on the list is NOT the primary
4805                          * sctp_cmpaddr() is much more efficent if the
4806                          * primary is the first on the list, make it so.
4807                          */
4808                         TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
4809                         TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
4810                 }
4811                 return (0);
4812         }
4813 }
4814
4815
4816 int
4817 sctp_is_vtag_good(struct sctp_inpcb *inp, uint32_t tag, struct timeval *now)
4818 {
4819         /*
4820          * This function serves two purposes. It will see if a TAG can be
4821          * re-used and return 1 for yes it is ok and 0 for don't use that
4822          * tag. A secondary function it will do is purge out old tags that
4823          * can be removed.
4824          */
4825         struct sctpasochead *head;
4826         struct sctpvtaghead *chain;
4827         struct sctp_tagblock *twait_block;
4828         struct sctp_tcb *stcb;
4829         int i;
4830
4831         SCTP_INP_INFO_WLOCK();
4832         chain = &sctppcbinfo.vtag_timewait[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
4833         /* First is the vtag in use ? */
4834
4835         head = &sctppcbinfo.sctp_asochash[SCTP_PCBHASH_ASOC(tag,
4836             sctppcbinfo.hashasocmark)];
4837         if (head == NULL) {
4838                 goto check_restart;
4839         }
4840         LIST_FOREACH(stcb, head, sctp_asocs) {
4841
4842                 if (stcb->asoc.my_vtag == tag) {
4843                         /*
4844                          * We should remove this if and return 0 always if
4845                          * we want vtags unique across all endpoints. For
4846                          * now within a endpoint is ok.
4847                          */
4848                         if (inp == stcb->sctp_ep) {
4849                                 /* bad tag, in use */
4850                                 SCTP_INP_INFO_WUNLOCK();
4851                                 return (0);
4852                         }
4853                 }
4854         }
4855 check_restart:
4856         /* Now lets check the restart hash */
4857         head = &sctppcbinfo.sctp_restarthash[SCTP_PCBHASH_ASOC(tag,
4858             sctppcbinfo.hashrestartmark)];
4859         if (head == NULL) {
4860                 goto check_time_wait;
4861         }
4862         LIST_FOREACH(stcb, head, sctp_tcbrestarhash) {
4863                 if (stcb->asoc.assoc_id == tag) {
4864                         /* candidate */
4865                         if (inp == stcb->sctp_ep) {
4866                                 /* bad tag, in use */
4867                                 SCTP_INP_INFO_WUNLOCK();
4868                                 return (0);
4869                         }
4870                 }
4871         }
4872 check_time_wait:
4873         /* Now what about timed wait ? */
4874         if (!LIST_EMPTY(chain)) {
4875                 /*
4876                  * Block(s) are present, lets see if we have this tag in the
4877                  * list
4878                  */
4879                 LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
4880                         for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
4881                                 if (twait_block->vtag_block[i].v_tag == 0) {
4882                                         /* not used */
4883                                         continue;
4884                                 } else if ((long)twait_block->vtag_block[i].tv_sec_at_expire >
4885                                     now->tv_sec) {
4886                                         /* Audit expires this guy */
4887                                         twait_block->vtag_block[i].tv_sec_at_expire = 0;
4888                                         twait_block->vtag_block[i].v_tag = 0;
4889                                 } else if (twait_block->vtag_block[i].v_tag ==
4890                                     tag) {
4891                                         /* Bad tag, sorry :< */
4892                                         SCTP_INP_INFO_WUNLOCK();
4893                                         return (0);
4894                                 }
4895                         }
4896                 }
4897         }
4898         /* Not found, ok to use the tag */
4899         SCTP_INP_INFO_WUNLOCK();
4900         return (1);
4901 }
4902
4903
4904 /*
4905  * Delete the address from the endpoint local address list Lookup using a
4906  * sockaddr address (ie. not an ifaddr)
4907  */
4908 int
4909 sctp_del_local_addr_ep_sa(struct sctp_inpcb *inp, struct sockaddr *sa)
4910 {
4911         struct sctp_laddr *laddr;
4912         struct sockaddr *l_sa;
4913         int found = 0;
4914
4915         /*
4916          * Here is another function I cannot find a caller for. As such we
4917          * SHOULD delete it if we have no users. If we find a user that user
4918          * MUST have the INP locked.
4919          * 
4920          */
4921
4922         if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4923                 /* You are already bound to all. You have it already */
4924                 return (EINVAL);
4925         }
4926         LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4927                 /* make sure the address exists */
4928                 if (laddr->ifa == NULL)
4929                         continue;
4930                 if (laddr->ifa->ifa_addr == NULL)
4931                         continue;
4932
4933                 l_sa = laddr->ifa->ifa_addr;
4934                 if (l_sa->sa_family == AF_INET6) {
4935                         /* IPv6 address */
4936                         struct sockaddr_in6 *sin1, *sin2;
4937
4938                         sin1 = (struct sockaddr_in6 *)l_sa;
4939                         sin2 = (struct sockaddr_in6 *)sa;
4940                         if (memcmp(&sin1->sin6_addr, &sin2->sin6_addr,
4941                             sizeof(struct in6_addr)) == 0) {
4942                                 /* matched */
4943                                 found = 1;
4944                                 break;
4945                         }
4946                 } else if (l_sa->sa_family == AF_INET) {
4947                         /* IPv4 address */
4948                         struct sockaddr_in *sin1, *sin2;
4949
4950                         sin1 = (struct sockaddr_in *)l_sa;
4951                         sin2 = (struct sockaddr_in *)sa;
4952                         if (sin1->sin_addr.s_addr == sin2->sin_addr.s_addr) {
4953                                 /* matched */
4954                                 found = 1;
4955                                 break;
4956                         }
4957                 } else {
4958                         /* invalid family */
4959                         return (-1);
4960                 }
4961         }
4962
4963         if (found && inp->laddr_count < 2) {
4964                 /* can't delete unless there are at LEAST 2 addresses */
4965                 return (-1);
4966         }
4967         if (found && (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
4968                 /*
4969                  * remove it from the ep list, this should NOT be done until
4970                  * its really gone from the interface list and we won't be
4971                  * receiving more of these. Probably right away. If we do
4972                  * allow a removal of an address from an association
4973                  * (sub-set bind) than this should NOT be called until the
4974                  * all ASCONF come back from this association.
4975                  */
4976                 sctp_remove_laddr(laddr);
4977                 return (0);
4978         } else {
4979                 return (-1);
4980         }
4981 }
4982
4983 static sctp_assoc_t reneged_asoc_ids[256];
4984 static uint8_t reneged_at = 0;
4985
4986 extern int sctp_do_drain;
4987
4988 static void
4989 sctp_drain_mbufs(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4990 {
4991         /*
4992          * We must hunt this association for MBUF's past the cumack (i.e.
4993          * out of order data that we can renege on).
4994          */
4995         struct sctp_association *asoc;
4996         struct sctp_tmit_chunk *chk, *nchk;
4997         uint32_t cumulative_tsn_p1, tsn;
4998         struct sctp_queued_to_read *ctl, *nctl;
4999         int cnt, strmat, gap;
5000
5001         /* We look for anything larger than the cum-ack + 1 */
5002
5003         SCTP_STAT_INCR(sctps_protocol_drain_calls);
5004         if (sctp_do_drain == 0) {
5005                 return;
5006         }
5007         asoc = &stcb->asoc;
5008         if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
5009                 /* none we can reneg on. */
5010                 return;
5011         }
5012         SCTP_STAT_INCR(sctps_protocol_drains_done);
5013         cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
5014         cnt = 0;
5015         /* First look in the re-assembly queue */
5016         chk = TAILQ_FIRST(&asoc->reasmqueue);
5017         while (chk) {
5018                 /* Get the next one */
5019                 nchk = TAILQ_NEXT(chk, sctp_next);
5020                 if (compare_with_wrap(chk->rec.data.TSN_seq,
5021                     cumulative_tsn_p1, MAX_TSN)) {
5022                         /* Yep it is above cum-ack */
5023                         cnt++;
5024                         tsn = chk->rec.data.TSN_seq;
5025                         if (tsn >= asoc->mapping_array_base_tsn) {
5026                                 gap = tsn - asoc->mapping_array_base_tsn;
5027                         } else {
5028                                 gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5029                                     tsn + 1;
5030                         }
5031                         asoc->size_on_reasm_queue = sctp_sbspace_sub(asoc->size_on_reasm_queue, chk->send_size);
5032                         sctp_ucount_decr(asoc->cnt_on_reasm_queue);
5033                         SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
5034                         TAILQ_REMOVE(&asoc->reasmqueue, chk, sctp_next);
5035                         if (chk->data) {
5036                                 sctp_m_freem(chk->data);
5037                                 chk->data = NULL;
5038                         }
5039                         sctp_free_remote_addr(chk->whoTo);
5040                         sctp_free_a_chunk(stcb, chk);
5041                 }
5042                 chk = nchk;
5043         }
5044         /* Ok that was fun, now we will drain all the inbound streams? */
5045         for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
5046                 ctl = TAILQ_FIRST(&asoc->strmin[strmat].inqueue);
5047                 while (ctl) {
5048                         nctl = TAILQ_NEXT(ctl, next);
5049                         if (compare_with_wrap(ctl->sinfo_tsn,
5050                             cumulative_tsn_p1, MAX_TSN)) {
5051                                 /* Yep it is above cum-ack */
5052                                 cnt++;
5053                                 tsn = ctl->sinfo_tsn;
5054                                 if (tsn >= asoc->mapping_array_base_tsn) {
5055                                         gap = tsn -
5056                                             asoc->mapping_array_base_tsn;
5057                                 } else {
5058                                         gap = (MAX_TSN -
5059                                             asoc->mapping_array_base_tsn) +
5060                                             tsn + 1;
5061                                 }
5062                                 asoc->size_on_all_streams = sctp_sbspace_sub(asoc->size_on_all_streams, ctl->length);
5063                                 sctp_ucount_decr(asoc->cnt_on_all_streams);
5064
5065                                 SCTP_UNSET_TSN_PRESENT(asoc->mapping_array,
5066                                     gap);
5067                                 TAILQ_REMOVE(&asoc->strmin[strmat].inqueue,
5068                                     ctl, next);
5069                                 if (ctl->data) {
5070                                         sctp_m_freem(ctl->data);
5071                                         ctl->data = NULL;
5072                                 }
5073                                 sctp_free_remote_addr(ctl->whoFrom);
5074                                 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_readq, ctl);
5075                                 SCTP_DECR_READQ_COUNT();
5076                         }
5077                         ctl = nctl;
5078                 }
5079         }
5080         /*
5081          * Question, should we go through the delivery queue? The only
5082          * reason things are on here is the app not reading OR a p-d-api up.
5083          * An attacker COULD send enough in to initiate the PD-API and then
5084          * send a bunch of stuff to other streams... these would wind up on
5085          * the delivery queue.. and then we would not get to them. But in
5086          * order to do this I then have to back-track and un-deliver
5087          * sequence numbers in streams.. el-yucko. I think for now we will
5088          * NOT look at the delivery queue and leave it to be something to
5089          * consider later. An alternative would be to abort the P-D-API with
5090          * a notification and then deliver the data.... Or another method
5091          * might be to keep track of how many times the situation occurs and
5092          * if we see a possible attack underway just abort the association.
5093          */
5094 #ifdef SCTP_DEBUG
5095         if (sctp_debug_on & SCTP_DEBUG_PCB1) {
5096                 if (cnt) {
5097                         printf("Freed %d chunks from reneg harvest\n", cnt);
5098                 }
5099         }
5100 #endif                          /* SCTP_DEBUG */
5101         if (cnt) {
5102                 /*
5103                  * Now do we need to find a new
5104                  * asoc->highest_tsn_inside_map?
5105                  */
5106                 if (asoc->highest_tsn_inside_map >= asoc->mapping_array_base_tsn) {
5107                         gap = asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn;
5108                 } else {
5109                         gap = (MAX_TSN - asoc->mapping_array_base_tsn) +
5110                             asoc->highest_tsn_inside_map + 1;
5111                 }
5112                 if (gap >= (asoc->mapping_array_size << 3)) {
5113                         /*
5114                          * Something bad happened or cum-ack and high were
5115                          * behind the base, but if so earlier checks should
5116                          * have found NO data... wierd... we will start at
5117                          * end of mapping array.
5118                          */
5119                         printf("Gap was larger than array?? %d set to max:%d maparraymax:%x\n",
5120                             (int)gap,
5121                             (int)(asoc->mapping_array_size << 3),
5122                             (int)asoc->highest_tsn_inside_map);
5123                         gap = asoc->mapping_array_size << 3;
5124                 }
5125                 while (gap > 0) {
5126                         if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
5127                                 /* found the new highest */
5128                                 asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn + gap;
5129                                 break;
5130                         }
5131                         gap--;
5132                 }
5133                 if (gap == 0) {
5134                         /* Nothing left in map */
5135                         memset(asoc->mapping_array, 0, asoc->mapping_array_size);
5136                         asoc->mapping_array_base_tsn = asoc->cumulative_tsn + 1;
5137                         asoc->highest_tsn_inside_map = asoc->cumulative_tsn;
5138                 }
5139                 asoc->last_revoke_count = cnt;
5140                 SCTP_OS_TIMER_STOP(&stcb->asoc.dack_timer.timer);
5141                 sctp_send_sack(stcb);
5142                 reneged_asoc_ids[reneged_at] = sctp_get_associd(stcb);
5143                 reneged_at++;
5144         }
5145         /*
5146          * Another issue, in un-setting the TSN's in the mapping array we
5147          * DID NOT adjust the higest_tsn marker.  This will cause one of two
5148          * things to occur. It may cause us to do extra work in checking for
5149          * our mapping array movement. More importantly it may cause us to
5150          * SACK every datagram. This may not be a bad thing though since we
5151          * will recover once we get our cum-ack above and all this stuff we
5152          * dumped recovered.
5153          */
5154 }
5155
5156 void
5157 sctp_drain()
5158 {
5159         /*
5160          * We must walk the PCB lists for ALL associations here. The system
5161          * is LOW on MBUF's and needs help. This is where reneging will
5162          * occur. We really hope this does NOT happen!
5163          */
5164         struct sctp_inpcb *inp;
5165         struct sctp_tcb *stcb;
5166
5167         SCTP_INP_INFO_RLOCK();
5168         LIST_FOREACH(inp, &sctppcbinfo.listhead, sctp_list) {
5169                 /* For each endpoint */
5170                 SCTP_INP_RLOCK(inp);
5171                 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
5172                         /* For each association */
5173                         SCTP_TCB_LOCK(stcb);
5174                         sctp_drain_mbufs(inp, stcb);
5175                         SCTP_TCB_UNLOCK(stcb);
5176                 }
5177                 SCTP_INP_RUNLOCK(inp);
5178         }
5179         SCTP_INP_INFO_RUNLOCK();
5180 }
5181
5182 /*
5183  * start a new iterator
5184  * iterates through all endpoints and associations based on the pcb_state
5185  * flags and asoc_state.  "af" (mandatory) is executed for all matching
5186  * assocs and "ef" (optional) is executed when the iterator completes.
5187  * "inpf" (optional) is executed for each new endpoint as it is being
5188  * iterated through.
5189  */
5190 int
5191 sctp_initiate_iterator(inp_func inpf, asoc_func af, uint32_t pcb_state,
5192     uint32_t pcb_features, uint32_t asoc_state, void *argp, uint32_t argi,
5193     end_func ef, struct sctp_inpcb *s_inp, uint8_t chunk_output_off)
5194 {
5195         struct sctp_iterator *it = NULL;
5196
5197         if (af == NULL) {
5198                 return (-1);
5199         }
5200         SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
5201             "Iterator");
5202         if (it == NULL) {
5203                 return (ENOMEM);
5204         }
5205         memset(it, 0, sizeof(*it));
5206         it->function_assoc = af;
5207         it->function_inp = inpf;
5208         it->function_atend = ef;
5209         it->pointer = argp;
5210         it->val = argi;
5211         it->pcb_flags = pcb_state;
5212         it->pcb_features = pcb_features;
5213         it->asoc_state = asoc_state;
5214         it->no_chunk_output = chunk_output_off;
5215         if (s_inp) {
5216                 it->inp = s_inp;
5217                 it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
5218         } else {
5219                 SCTP_INP_INFO_RLOCK();
5220                 it->inp = LIST_FIRST(&sctppcbinfo.listhead);
5221                 SCTP_INP_INFO_RUNLOCK();
5222                 it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
5223
5224         }
5225         /* Init the timer */
5226         SCTP_OS_TIMER_INIT(&it->tmr.timer);
5227         /* add to the list of all iterators */
5228         SCTP_INP_INFO_WLOCK();
5229         LIST_INSERT_HEAD(&sctppcbinfo.iteratorhead, it, sctp_nxt_itr);
5230         SCTP_INP_INFO_WUNLOCK();
5231         sctp_timer_start(SCTP_TIMER_TYPE_ITERATOR, (struct sctp_inpcb *)it,
5232             NULL, NULL);
5233         return (0);
5234 }