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