Coverage Report

Created: 2025-08-03 06:44

/src/usrsctp/usrsctplib/netinet/sctp_pcb.c
Line
Count
Source (jump to first uncovered line)
1
/*-
2
 * SPDX-License-Identifier: BSD-3-Clause
3
 *
4
 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5
 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6
 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are met:
10
 *
11
 * a) Redistributions of source code must retain the above copyright notice,
12
 *    this list of conditions and the following disclaimer.
13
 *
14
 * b) Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in
16
 *    the documentation and/or other materials provided with the distribution.
17
 *
18
 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19
 *    contributors may be used to endorse or promote products derived
20
 *    from this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32
 * THE POSSIBILITY OF SUCH DAMAGE.
33
 */
34
35
#include <netinet/sctp_os.h>
36
#if defined(__FreeBSD__) && !defined(__Userspace__)
37
#include <sys/proc.h>
38
#endif
39
#include <netinet/sctp_var.h>
40
#include <netinet/sctp_sysctl.h>
41
#include <netinet/sctp_pcb.h>
42
#include <netinet/sctputil.h>
43
#include <netinet/sctp.h>
44
#include <netinet/sctp_header.h>
45
#include <netinet/sctp_asconf.h>
46
#include <netinet/sctp_output.h>
47
#include <netinet/sctp_timer.h>
48
#include <netinet/sctp_bsd_addr.h>
49
#if defined(INET) || defined(INET6)
50
#if !defined(_WIN32)
51
#include <netinet/udp.h>
52
#endif
53
#endif
54
#ifdef INET6
55
#if defined(__Userspace__)
56
#include "user_ip6_var.h"
57
#else
58
#include <netinet6/ip6_var.h>
59
#endif
60
#endif
61
#if defined(__FreeBSD__) && !defined(__Userspace__)
62
#include <sys/sched.h>
63
#include <sys/smp.h>
64
#include <sys/unistd.h>
65
#endif
66
#if defined(__Userspace__)
67
#include <user_socketvar.h>
68
#include <user_atomic.h>
69
#if !defined(_WIN32)
70
#include <netdb.h>
71
#endif
72
#endif
73
74
#if !defined(__FreeBSD__) || defined(__Userspace__)
75
struct sctp_base_info system_base_info;
76
77
#endif
78
/* FIX: we don't handle multiple link local scopes */
79
/* "scopeless" replacement IN6_ARE_ADDR_EQUAL */
80
#ifdef INET6
81
int
82
SCTP6_ARE_ADDR_EQUAL(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
83
39.1k
{
84
#ifdef SCTP_EMBEDDED_V6_SCOPE
85
#if defined(__APPLE__) && !defined(__Userspace__)
86
  struct in6_addr tmp_a, tmp_b;
87
88
  tmp_a = a->sin6_addr;
89
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
90
  if (in6_embedscope(&tmp_a, a, NULL, NULL) != 0) {
91
#else
92
  if (in6_embedscope(&tmp_a, a, NULL, NULL, NULL) != 0) {
93
#endif
94
    return (0);
95
  }
96
  tmp_b = b->sin6_addr;
97
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
98
  if (in6_embedscope(&tmp_b, b, NULL, NULL) != 0) {
99
#else
100
  if (in6_embedscope(&tmp_b, b, NULL, NULL, NULL) != 0) {
101
#endif
102
    return (0);
103
  }
104
  return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
105
#elif defined(SCTP_KAME)
106
  struct sockaddr_in6 tmp_a, tmp_b;
107
108
  memcpy(&tmp_a, a, sizeof(struct sockaddr_in6));
109
  if (sa6_embedscope(&tmp_a, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
110
    return (0);
111
  }
112
  memcpy(&tmp_b, b, sizeof(struct sockaddr_in6));
113
  if (sa6_embedscope(&tmp_b, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
114
    return (0);
115
  }
116
  return (IN6_ARE_ADDR_EQUAL(&tmp_a.sin6_addr, &tmp_b.sin6_addr));
117
#else
118
  struct in6_addr tmp_a, tmp_b;
119
120
  tmp_a = a->sin6_addr;
121
  if (in6_embedscope(&tmp_a, a) != 0) {
122
    return (0);
123
  }
124
  tmp_b = b->sin6_addr;
125
  if (in6_embedscope(&tmp_b, b) != 0) {
126
    return (0);
127
  }
128
  return (IN6_ARE_ADDR_EQUAL(&tmp_a, &tmp_b));
129
#endif
130
#else
131
39.1k
  return (IN6_ARE_ADDR_EQUAL(&(a->sin6_addr), &(b->sin6_addr)));
132
39.1k
#endif /* SCTP_EMBEDDED_V6_SCOPE */
133
39.1k
}
134
#endif
135
136
void
137
sctp_fill_pcbinfo(struct sctp_pcbinfo *spcb)
138
0
{
139
  /*
140
   * We really don't need to lock this, but I will just because it
141
   * does not hurt.
142
   */
143
0
  SCTP_INP_INFO_RLOCK();
144
0
  spcb->ep_count = SCTP_BASE_INFO(ipi_count_ep);
145
0
  spcb->asoc_count = SCTP_BASE_INFO(ipi_count_asoc);
146
0
  spcb->laddr_count = SCTP_BASE_INFO(ipi_count_laddr);
147
0
  spcb->raddr_count = SCTP_BASE_INFO(ipi_count_raddr);
148
0
  spcb->chk_count = SCTP_BASE_INFO(ipi_count_chunk);
149
0
  spcb->readq_count = SCTP_BASE_INFO(ipi_count_readq);
150
0
  spcb->stream_oque = SCTP_BASE_INFO(ipi_count_strmoq);
151
0
  spcb->free_chunks = SCTP_BASE_INFO(ipi_free_chunks);
152
0
  SCTP_INP_INFO_RUNLOCK();
153
0
}
154
155
/*-
156
 * Addresses are added to VRF's (Virtual Router's). For BSD we
157
 * have only the default VRF 0. We maintain a hash list of
158
 * VRF's. Each VRF has its own list of sctp_ifn's. Each of
159
 * these has a list of addresses. When we add a new address
160
 * to a VRF we lookup the ifn/ifn_index, if the ifn does
161
 * not exist we create it and add it to the list of IFN's
162
 * within the VRF. Once we have the sctp_ifn, we add the
163
 * address to the list. So we look something like:
164
 *
165
 * hash-vrf-table
166
 *   vrf-> ifn-> ifn -> ifn
167
 *   vrf    |
168
 *    ...   +--ifa-> ifa -> ifa
169
 *   vrf
170
 *
171
 * We keep these separate lists since the SCTP subsystem will
172
 * point to these from its source address selection nets structure.
173
 * When an address is deleted it does not happen right away on
174
 * the SCTP side, it gets scheduled. What we do when a
175
 * delete happens is immediately remove the address from
176
 * the master list and decrement the refcount. As our
177
 * addip iterator works through and frees the src address
178
 * selection pointing to the sctp_ifa, eventually the refcount
179
 * will reach 0 and we will delete it. Note that it is assumed
180
 * that any locking on system level ifn/ifa is done at the
181
 * caller of these functions and these routines will only
182
 * lock the SCTP structures as they add or delete things.
183
 *
184
 * Other notes on VRF concepts.
185
 *  - An endpoint can be in multiple VRF's
186
 *  - An association lives within a VRF and only one VRF.
187
 *  - Any incoming packet we can deduce the VRF for by
188
 *    looking at the mbuf/pak inbound (for BSD its VRF=0 :D)
189
 *  - Any downward send call or connect call must supply the
190
 *    VRF via ancillary data or via some sort of set default
191
 *    VRF socket option call (again for BSD no brainer since
192
 *    the VRF is always 0).
193
 *  - An endpoint may add multiple VRF's to it.
194
 *  - Listening sockets can accept associations in any
195
 *    of the VRF's they are in but the assoc will end up
196
 *    in only one VRF (gotten from the packet or connect/send).
197
 *
198
 */
199
200
struct sctp_vrf *
201
sctp_allocate_vrf(int vrf_id)
202
3
{
203
3
  struct sctp_vrf *vrf = NULL;
204
3
  struct sctp_vrflist *bucket;
205
206
  /* First allocate the VRF structure */
207
3
  vrf = sctp_find_vrf(vrf_id);
208
3
  if (vrf) {
209
    /* Already allocated */
210
0
    return (vrf);
211
0
  }
212
3
  SCTP_MALLOC(vrf, struct sctp_vrf *, sizeof(struct sctp_vrf),
213
3
        SCTP_M_VRF);
214
3
  if (vrf == NULL) {
215
    /* No memory */
216
0
#ifdef INVARIANTS
217
0
    panic("No memory for VRF:%d", vrf_id);
218
0
#endif
219
0
    return (NULL);
220
0
  }
221
  /* setup the VRF */
222
3
  memset(vrf, 0, sizeof(struct sctp_vrf));
223
3
  vrf->vrf_id = vrf_id;
224
3
  LIST_INIT(&vrf->ifnlist);
225
3
  vrf->total_ifa_count = 0;
226
3
  vrf->refcount = 0;
227
  /* now also setup table ids */
228
3
  SCTP_INIT_VRF_TABLEID(vrf);
229
  /* Init the HASH of addresses */
230
3
  vrf->vrf_addr_hash = SCTP_HASH_INIT(SCTP_VRF_ADDR_HASH_SIZE,
231
3
              &vrf->vrf_addr_hashmark);
232
3
  if (vrf->vrf_addr_hash == NULL) {
233
    /* No memory */
234
0
#ifdef INVARIANTS
235
0
    panic("No memory for VRF:%d", vrf_id);
236
0
#endif
237
0
    SCTP_FREE(vrf, SCTP_M_VRF);
238
0
    return (NULL);
239
0
  }
240
241
  /* Add it to the hash table */
242
3
  bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
243
3
  LIST_INSERT_HEAD(bucket, vrf, next_vrf);
244
3
  atomic_add_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
245
3
  return (vrf);
246
3
}
247
248
struct sctp_ifn *
249
sctp_find_ifn(void *ifn, uint32_t ifn_index)
250
3.30k
{
251
3.30k
  struct sctp_ifn *sctp_ifnp;
252
3.30k
  struct sctp_ifnlist *hash_ifn_head;
253
254
3.30k
  SCTP_IPI_ADDR_LOCK_ASSERT();
255
#if defined(__FreeBSD__) && !defined(__Userspace__)
256
  KASSERT(ifn != NULL, ("sctp_find_ifn(NULL, %u) called", ifn_index));
257
#endif
258
3.30k
  hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
259
9.89k
  LIST_FOREACH(sctp_ifnp, hash_ifn_head, next_bucket) {
260
9.89k
    if (sctp_ifnp->ifn_index == ifn_index &&
261
9.89k
        sctp_ifnp->ifn_p == ifn) {
262
3.29k
      break;
263
3.29k
    }
264
9.89k
  }
265
3.30k
  return (sctp_ifnp);
266
3.30k
}
267
268
struct sctp_vrf *
269
sctp_find_vrf(uint32_t vrf_id)
270
475k
{
271
475k
  struct sctp_vrflist *bucket;
272
475k
  struct sctp_vrf *liste;
273
274
475k
  bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(vrf_id & SCTP_BASE_INFO(hashvrfmark))];
275
475k
  LIST_FOREACH(liste, bucket, next_vrf) {
276
475k
    if (vrf_id == liste->vrf_id) {
277
475k
      return (liste);
278
475k
    }
279
475k
  }
280
3
  return (NULL);
281
475k
}
282
283
void
284
sctp_free_vrf(struct sctp_vrf *vrf)
285
0
{
286
0
  if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&vrf->refcount)) {
287
0
    if (vrf->vrf_addr_hash) {
288
0
      SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
289
0
      vrf->vrf_addr_hash = NULL;
290
0
    }
291
    /* We zero'd the count */
292
0
    LIST_REMOVE(vrf, next_vrf);
293
0
    SCTP_FREE(vrf, SCTP_M_VRF);
294
0
    atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_vrfs), 1);
295
0
  }
296
0
}
297
298
static void
299
sctp_free_ifn(struct sctp_ifn *sctp_ifnp)
300
0
{
301
0
  if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifnp->refcount)) {
302
    /* We zero'd the count */
303
0
    if (sctp_ifnp->vrf) {
304
0
      sctp_free_vrf(sctp_ifnp->vrf);
305
0
    }
306
0
    SCTP_FREE(sctp_ifnp, SCTP_M_IFN);
307
0
    atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
308
0
  }
309
0
}
310
311
void
312
sctp_free_ifa(struct sctp_ifa *sctp_ifap)
313
12.8k
{
314
12.8k
  if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&sctp_ifap->refcount)) {
315
    /* We zero'd the count */
316
0
    if (sctp_ifap->ifn_p) {
317
0
      sctp_free_ifn(sctp_ifap->ifn_p);
318
0
    }
319
0
    SCTP_FREE(sctp_ifap, SCTP_M_IFA);
320
0
    atomic_subtract_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
321
0
  }
322
12.8k
}
323
324
static void
325
sctp_delete_ifn(struct sctp_ifn *sctp_ifnp)
326
0
{
327
328
0
  SCTP_IPI_ADDR_WLOCK_ASSERT();
329
0
  if (sctp_find_ifn(sctp_ifnp->ifn_p, sctp_ifnp->ifn_index) == NULL) {
330
    /* Not in the list.. sorry */
331
0
    return;
332
0
  }
333
0
  LIST_REMOVE(sctp_ifnp, next_bucket);
334
0
  LIST_REMOVE(sctp_ifnp, next_ifn);
335
  /* Take away the reference, and possibly free it */
336
0
  sctp_free_ifn(sctp_ifnp);
337
0
}
338
339
/*-
340
 * Add an ifa to an ifn.
341
 * Register the interface as necessary.
342
 */
343
static void
344
sctp_add_ifa_to_ifn(struct sctp_ifn *sctp_ifnp, struct sctp_ifa *sctp_ifap)
345
0
{
346
0
  int ifa_af;
347
348
0
  SCTP_IPI_ADDR_WLOCK_ASSERT();
349
0
  LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
350
0
  sctp_ifap->ifn_p = sctp_ifnp;
351
0
  atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
352
  /* update address counts */
353
0
  sctp_ifnp->ifa_count++;
354
0
  ifa_af = sctp_ifap->address.sa.sa_family;
355
0
  switch (ifa_af) {
356
0
#ifdef INET
357
0
  case AF_INET:
358
0
    sctp_ifnp->num_v4++;
359
0
    break;
360
0
#endif
361
0
#ifdef INET6
362
0
  case AF_INET6:
363
0
    sctp_ifnp->num_v6++;
364
0
    break;
365
0
#endif
366
0
  default:
367
0
    break;
368
0
  }
369
0
  if (sctp_ifnp->ifa_count == 1) {
370
    /* register the new interface */
371
0
    sctp_ifnp->registered_af = ifa_af;
372
0
  }
373
0
}
374
375
/*-
376
 * Remove an ifa from its ifn.
377
 * If no more addresses exist, remove the ifn too. Otherwise, re-register
378
 * the interface based on the remaining address families left.
379
 */
380
static void
381
sctp_remove_ifa_from_ifn(struct sctp_ifa *sctp_ifap)
382
0
{
383
0
  SCTP_IPI_ADDR_WLOCK_ASSERT();
384
0
  LIST_REMOVE(sctp_ifap, next_ifa);
385
0
  if (sctp_ifap->ifn_p) {
386
    /* update address counts */
387
0
    sctp_ifap->ifn_p->ifa_count--;
388
0
    switch (sctp_ifap->address.sa.sa_family) {
389
0
#ifdef INET
390
0
    case AF_INET:
391
0
      sctp_ifap->ifn_p->num_v4--;
392
0
      break;
393
0
#endif
394
0
#ifdef INET6
395
0
    case AF_INET6:
396
0
      sctp_ifap->ifn_p->num_v6--;
397
0
      break;
398
0
#endif
399
0
    default:
400
0
      break;
401
0
    }
402
403
0
    if (LIST_EMPTY(&sctp_ifap->ifn_p->ifalist)) {
404
      /* remove the ifn, possibly freeing it */
405
0
      sctp_delete_ifn(sctp_ifap->ifn_p);
406
0
    } else {
407
      /* re-register address family type, if needed */
408
0
      if ((sctp_ifap->ifn_p->num_v6 == 0) &&
409
0
          (sctp_ifap->ifn_p->registered_af == AF_INET6)) {
410
0
        sctp_ifap->ifn_p->registered_af = AF_INET;
411
0
      } else if ((sctp_ifap->ifn_p->num_v4 == 0) &&
412
0
           (sctp_ifap->ifn_p->registered_af == AF_INET)) {
413
0
        sctp_ifap->ifn_p->registered_af = AF_INET6;
414
0
      }
415
      /* free the ifn refcount */
416
0
      sctp_free_ifn(sctp_ifap->ifn_p);
417
0
    }
418
0
    sctp_ifap->ifn_p = NULL;
419
0
  }
420
0
}
421
422
struct sctp_ifa *
423
sctp_add_addr_to_vrf(uint32_t vrf_id, void *ifn, uint32_t ifn_index,
424
                     uint32_t ifn_type, const char *if_name, void *ifa,
425
                     struct sockaddr *addr, uint32_t ifa_flags,
426
                     int dynamic_add)
427
9
{
428
9
  struct sctp_vrf *vrf;
429
9
  struct sctp_ifn *sctp_ifnp, *new_sctp_ifnp;
430
9
  struct sctp_ifa *sctp_ifap, *new_sctp_ifap;
431
9
  struct sctp_ifalist *hash_addr_head;
432
9
  struct sctp_ifnlist *hash_ifn_head;
433
9
  uint32_t hash_of_addr;
434
435
#ifdef SCTP_DEBUG
436
  SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: adding address: ", vrf_id);
437
  SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
438
#endif
439
9
  SCTP_MALLOC(new_sctp_ifnp, struct sctp_ifn *,
440
9
              sizeof(struct sctp_ifn), SCTP_M_IFN);
441
9
  if (new_sctp_ifnp == NULL) {
442
0
#ifdef INVARIANTS
443
0
    panic("No memory for IFN");
444
0
#endif
445
0
    return (NULL);
446
0
  }
447
9
  SCTP_MALLOC(new_sctp_ifap, struct sctp_ifa *, sizeof(struct sctp_ifa), SCTP_M_IFA);
448
9
  if (new_sctp_ifap == NULL) {
449
0
#ifdef INVARIANTS
450
0
    panic("No memory for IFA");
451
0
#endif
452
0
    SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
453
0
    return (NULL);
454
0
  }
455
456
9
  SCTP_IPI_ADDR_WLOCK();
457
9
  sctp_ifnp = sctp_find_ifn(ifn, ifn_index);
458
9
  if (sctp_ifnp) {
459
0
    vrf = sctp_ifnp->vrf;
460
9
  } else {
461
9
    vrf = sctp_find_vrf(vrf_id);
462
9
    if (vrf == NULL) {
463
0
      vrf = sctp_allocate_vrf(vrf_id);
464
0
      if (vrf == NULL) {
465
0
        SCTP_IPI_ADDR_WUNLOCK();
466
0
        SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
467
0
        SCTP_FREE(new_sctp_ifap, SCTP_M_IFA);
468
0
        return (NULL);
469
0
      }
470
0
    }
471
9
  }
472
9
  if (sctp_ifnp == NULL) {
473
    /* build one and add it, can't hold lock
474
     * until after malloc done though.
475
     */
476
9
    sctp_ifnp = new_sctp_ifnp;
477
9
    new_sctp_ifnp = NULL;
478
9
    memset(sctp_ifnp, 0, sizeof(struct sctp_ifn));
479
9
    sctp_ifnp->ifn_index = ifn_index;
480
9
    sctp_ifnp->ifn_p = ifn;
481
9
    sctp_ifnp->ifn_type = ifn_type;
482
9
    sctp_ifnp->refcount = 0;
483
9
    sctp_ifnp->vrf = vrf;
484
9
    atomic_add_int(&vrf->refcount, 1);
485
9
    sctp_ifnp->ifn_mtu = SCTP_GATHER_MTU_FROM_IFN_INFO(ifn, ifn_index);
486
9
    if (if_name != NULL) {
487
9
      SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", if_name);
488
9
    } else {
489
0
      SCTP_SNPRINTF(sctp_ifnp->ifn_name, SCTP_IFNAMSIZ, "%s", "unknown");
490
0
    }
491
9
    hash_ifn_head = &SCTP_BASE_INFO(vrf_ifn_hash)[(ifn_index & SCTP_BASE_INFO(vrf_ifn_hashmark))];
492
9
    LIST_INIT(&sctp_ifnp->ifalist);
493
9
    LIST_INSERT_HEAD(hash_ifn_head, sctp_ifnp, next_bucket);
494
9
    LIST_INSERT_HEAD(&vrf->ifnlist, sctp_ifnp, next_ifn);
495
9
    atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifns), 1);
496
9
  }
497
9
  sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
498
9
  if (sctp_ifap != NULL) {
499
    /* The address being added is already or still known. */
500
0
    if (sctp_ifap->ifn_p != NULL) {
501
0
      if (sctp_ifap->ifn_p->ifn_index == ifn_index &&
502
0
          sctp_ifap->ifn_p->ifn_p == ifn) {
503
0
        SCTPDBG(SCTP_DEBUG_PCB4,
504
0
            "Using existing ifn %s (0x%x) for ifa %p\n",
505
0
            sctp_ifap->ifn_p->ifn_name, ifn_index,
506
0
            (void *)sctp_ifap);
507
0
        if (new_sctp_ifnp == NULL) {
508
          /* Remove the created one not used. */
509
0
          sctp_delete_ifn(sctp_ifnp);
510
0
        }
511
0
        if (sctp_ifap->localifa_flags & SCTP_BEING_DELETED) {
512
          /* Switch back to active. */
513
0
          SCTPDBG(SCTP_DEBUG_PCB4,
514
0
          "Clearing deleted ifa flag\n");
515
0
          sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
516
0
          sctp_ifap->ifn_p = sctp_ifnp;
517
0
          atomic_add_int(&sctp_ifap->ifn_p->refcount, 1);
518
0
        }
519
0
      } else {
520
        /*
521
         * The last IFN gets the address, remove the
522
         * old one.
523
         */
524
0
        SCTPDBG(SCTP_DEBUG_PCB4,
525
0
            "Moving ifa %p from %s (0x%x) to %s (0x%x)\n",
526
0
            (void *)sctp_ifap,
527
0
            sctp_ifap->ifn_p->ifn_name,
528
0
            sctp_ifap->ifn_p->ifn_index, if_name,
529
0
            ifn_index);
530
        /* remove the address from the old ifn */
531
0
        sctp_remove_ifa_from_ifn(sctp_ifap);
532
        /* move the address over to the new ifn */
533
0
        sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
534
0
      }
535
0
    } else {
536
      /* Repair ifn_p, which was NULL... */
537
0
      sctp_ifap->localifa_flags = SCTP_ADDR_VALID;
538
0
      SCTPDBG(SCTP_DEBUG_PCB4,
539
0
          "Repairing ifn %p for ifa %p\n",
540
0
          (void *)sctp_ifnp, (void *)sctp_ifap);
541
0
      sctp_add_ifa_to_ifn(sctp_ifnp, sctp_ifap);
542
0
    }
543
0
    SCTP_IPI_ADDR_WUNLOCK();
544
0
    if (new_sctp_ifnp != NULL) {
545
0
      SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
546
0
    }
547
0
    SCTP_FREE(new_sctp_ifap, SCTP_M_IFA);
548
0
    return (sctp_ifap);
549
0
  }
550
9
  KASSERT(sctp_ifnp != NULL,
551
9
      ("sctp_add_addr_to_vrf: sctp_ifnp == NULL"));
552
9
  KASSERT(sctp_ifap == NULL,
553
9
      ("sctp_add_addr_to_vrf: sctp_ifap (%p) != NULL", sctp_ifap));
554
9
  sctp_ifap = new_sctp_ifap;
555
9
  memset(sctp_ifap, 0, sizeof(struct sctp_ifa));
556
9
  sctp_ifap->ifn_p = sctp_ifnp;
557
9
  atomic_add_int(&sctp_ifnp->refcount, 1);
558
9
  sctp_ifap->vrf_id = vrf_id;
559
9
  sctp_ifap->ifa = ifa;
560
#ifdef HAVE_SA_LEN
561
  memcpy(&sctp_ifap->address, addr, addr->sa_len);
562
#else
563
9
  switch (addr->sa_family) {
564
0
#ifdef INET
565
6
  case AF_INET:
566
6
    memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in));
567
6
    break;
568
0
#endif
569
0
#ifdef INET6
570
0
  case AF_INET6:
571
0
    memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_in6));
572
0
    break;
573
0
#endif
574
0
#if defined(__Userspace__)
575
3
  case AF_CONN:
576
3
    memcpy(&sctp_ifap->address, addr, sizeof(struct sockaddr_conn));
577
3
    break;
578
0
#endif
579
0
  default:
580
    /* TSNH */
581
0
    break;
582
9
  }
583
9
#endif
584
9
  sctp_ifap->localifa_flags = SCTP_ADDR_VALID | SCTP_ADDR_DEFER_USE;
585
9
  sctp_ifap->flags = ifa_flags;
586
  /* Set scope */
587
9
  switch (sctp_ifap->address.sa.sa_family) {
588
0
#ifdef INET
589
6
  case AF_INET:
590
6
  {
591
6
    struct sockaddr_in *sin;
592
593
6
    sin = &sctp_ifap->address.sin;
594
6
    if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
595
6
        (IN4_ISLOOPBACK_ADDRESS(&sin->sin_addr))) {
596
3
      sctp_ifap->src_is_loop = 1;
597
3
    }
598
6
    if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
599
3
      sctp_ifap->src_is_priv = 1;
600
3
    }
601
6
    sctp_ifnp->num_v4++;
602
6
    if (new_sctp_ifnp == NULL)
603
6
      sctp_ifnp->registered_af = AF_INET;
604
6
    break;
605
0
  }
606
0
#endif
607
0
#ifdef INET6
608
0
  case AF_INET6:
609
0
  {
610
    /* ok to use deprecated addresses? */
611
0
    struct sockaddr_in6 *sin6;
612
613
0
    sin6 = &sctp_ifap->address.sin6;
614
0
    if (SCTP_IFN_IS_IFT_LOOP(sctp_ifap->ifn_p) ||
615
0
        (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))) {
616
0
      sctp_ifap->src_is_loop = 1;
617
0
    }
618
0
    if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
619
0
      sctp_ifap->src_is_priv = 1;
620
0
    }
621
0
    sctp_ifnp->num_v6++;
622
0
    if (new_sctp_ifnp == NULL)
623
0
      sctp_ifnp->registered_af = AF_INET6;
624
0
    break;
625
0
  }
626
0
#endif
627
0
#if defined(__Userspace__)
628
3
  case AF_CONN:
629
3
    if (new_sctp_ifnp == NULL)
630
3
      sctp_ifnp->registered_af = AF_CONN;
631
3
    break;
632
0
#endif
633
0
  default:
634
0
    break;
635
9
  }
636
9
  hash_of_addr = sctp_get_ifa_hash_val(&sctp_ifap->address.sa);
637
638
9
  if ((sctp_ifap->src_is_priv == 0) &&
639
9
      (sctp_ifap->src_is_loop == 0)) {
640
3
    sctp_ifap->src_is_glob = 1;
641
3
  }
642
9
  hash_addr_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
643
9
  LIST_INSERT_HEAD(hash_addr_head, sctp_ifap, next_bucket);
644
9
  sctp_ifap->refcount = 1;
645
9
  LIST_INSERT_HEAD(&sctp_ifnp->ifalist, sctp_ifap, next_ifa);
646
9
  sctp_ifnp->ifa_count++;
647
9
  vrf->total_ifa_count++;
648
9
  atomic_add_int(&SCTP_BASE_INFO(ipi_count_ifas), 1);
649
9
  SCTP_IPI_ADDR_WUNLOCK();
650
9
  if (new_sctp_ifnp != NULL) {
651
0
    SCTP_FREE(new_sctp_ifnp, SCTP_M_IFN);
652
0
  }
653
654
9
  if (dynamic_add) {
655
    /* Bump up the refcount so that when the timer
656
     * completes it will drop back down.
657
     */
658
0
    struct sctp_laddr *wi;
659
660
0
    atomic_add_int(&sctp_ifap->refcount, 1);
661
0
    wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
662
0
    if (wi == NULL) {
663
      /*
664
       * Gak, what can we do? We have lost an address
665
       * change can you say HOSED?
666
       */
667
0
      SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
668
      /* Opps, must decrement the count */
669
0
      sctp_del_addr_from_vrf(vrf_id, addr, ifn, ifn_index);
670
0
      return (NULL);
671
0
    }
672
0
    SCTP_INCR_LADDR_COUNT();
673
0
    memset(wi, 0, sizeof(*wi));
674
0
    (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
675
0
    wi->ifa = sctp_ifap;
676
0
    wi->action = SCTP_ADD_IP_ADDRESS;
677
678
0
    SCTP_WQ_ADDR_LOCK();
679
0
    LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
680
0
    sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
681
0
         (struct sctp_inpcb *)NULL,
682
0
         (struct sctp_tcb *)NULL,
683
0
         (struct sctp_nets *)NULL);
684
0
    SCTP_WQ_ADDR_UNLOCK();
685
9
  } else {
686
    /* it's ready for use */
687
9
    sctp_ifap->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
688
9
  }
689
9
  return (sctp_ifap);
690
9
}
691
692
void
693
sctp_del_addr_from_vrf(uint32_t vrf_id, struct sockaddr *addr,
694
                       void *ifn, uint32_t ifn_index)
695
0
{
696
0
  struct sctp_vrf *vrf;
697
0
  struct sctp_ifa *sctp_ifap;
698
699
0
  SCTP_IPI_ADDR_WLOCK();
700
0
  vrf = sctp_find_vrf(vrf_id);
701
0
  if (vrf == NULL) {
702
0
    SCTPDBG(SCTP_DEBUG_PCB4, "Can't find vrf_id 0x%x\n", vrf_id);
703
0
    SCTP_IPI_ADDR_WUNLOCK();
704
0
    return;
705
0
  }
706
707
#ifdef SCTP_DEBUG
708
  SCTPDBG(SCTP_DEBUG_PCB4, "vrf_id 0x%x: deleting address:", vrf_id);
709
  SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
710
#endif
711
0
  sctp_ifap = sctp_find_ifa_by_addr(addr, vrf->vrf_id, SCTP_ADDR_LOCKED);
712
0
  if (sctp_ifap != NULL) {
713
    /* Validate the delete */
714
0
    if (sctp_ifap->ifn_p) {
715
0
      if (ifn_index != sctp_ifap->ifn_p->ifn_index ||
716
0
          ifn != sctp_ifap->ifn_p->ifn_p) {
717
0
        SCTPDBG(SCTP_DEBUG_PCB4, "ifn:%d (%p) ifname:%s - ignoring delete\n",
718
0
                sctp_ifap->ifn_p->ifn_index,
719
0
                sctp_ifap->ifn_p->ifn_p,
720
0
                sctp_ifap->ifn_p->ifn_name);
721
0
        SCTP_IPI_ADDR_WUNLOCK();
722
0
        return;
723
0
      }
724
0
    }
725
0
    SCTPDBG(SCTP_DEBUG_PCB4, "Deleting ifa %p\n", (void *)sctp_ifap);
726
0
    sctp_ifap->localifa_flags &= SCTP_ADDR_VALID;
727
    /*
728
     * We don't set the flag. This means that the structure will
729
     * hang around in EP's that have bound specific to it until
730
     * they close. This gives us TCP like behavior if someone
731
     * removes an address (or for that matter adds it right back).
732
     */
733
    /* sctp_ifap->localifa_flags |= SCTP_BEING_DELETED; */
734
0
    vrf->total_ifa_count--;
735
0
    LIST_REMOVE(sctp_ifap, next_bucket);
736
0
    sctp_remove_ifa_from_ifn(sctp_ifap);
737
0
  }
738
#ifdef SCTP_DEBUG
739
  else {
740
    SCTPDBG(SCTP_DEBUG_PCB4, "Del Addr-ifn:%d Could not find address:",
741
      ifn_index);
742
    SCTPDBG_ADDR(SCTP_DEBUG_PCB4, addr);
743
  }
744
#endif
745
746
0
  SCTP_IPI_ADDR_WUNLOCK();
747
0
  if (sctp_ifap != NULL) {
748
0
    struct sctp_laddr *wi;
749
750
0
    wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
751
0
    if (wi == NULL) {
752
      /*
753
       * Gak, what can we do? We have lost an address
754
       * change can you say HOSED?
755
       */
756
0
      SCTPDBG(SCTP_DEBUG_PCB4, "Lost an address change?\n");
757
758
      /* Oops, must decrement the count */
759
0
      sctp_free_ifa(sctp_ifap);
760
0
      return;
761
0
    }
762
0
    SCTP_INCR_LADDR_COUNT();
763
0
    memset(wi, 0, sizeof(*wi));
764
0
    (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
765
0
    wi->ifa = sctp_ifap;
766
0
    wi->action = SCTP_DEL_IP_ADDRESS;
767
0
    SCTP_WQ_ADDR_LOCK();
768
    /*
769
     * Should this really be a tailq? As it is we will process the
770
     * newest first :-0
771
     */
772
0
    LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
773
0
    sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
774
0
         (struct sctp_inpcb *)NULL,
775
0
         (struct sctp_tcb *)NULL,
776
0
         (struct sctp_nets *)NULL);
777
0
    SCTP_WQ_ADDR_UNLOCK();
778
0
  }
779
0
  return;
780
0
}
781
782
static int
783
sctp_does_stcb_own_this_addr(struct sctp_tcb *stcb, struct sockaddr *to)
784
368k
{
785
368k
  int loopback_scope;
786
368k
#if defined(INET)
787
368k
  int ipv4_local_scope, ipv4_addr_legal;
788
368k
#endif
789
368k
#if defined(INET6)
790
368k
  int local_scope, site_scope, ipv6_addr_legal;
791
368k
#endif
792
368k
#if defined(__Userspace__)
793
368k
  int conn_addr_legal;
794
368k
#endif
795
368k
  struct sctp_vrf *vrf;
796
368k
  struct sctp_ifn *sctp_ifn;
797
368k
  struct sctp_ifa *sctp_ifa;
798
799
368k
  loopback_scope = stcb->asoc.scope.loopback_scope;
800
368k
#if defined(INET)
801
368k
  ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
802
368k
  ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
803
368k
#endif
804
368k
#if defined(INET6)
805
368k
  local_scope = stcb->asoc.scope.local_scope;
806
368k
  site_scope = stcb->asoc.scope.site_scope;
807
368k
  ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
808
368k
#endif
809
368k
#if defined(__Userspace__)
810
368k
  conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
811
368k
#endif
812
813
368k
  SCTP_IPI_ADDR_RLOCK();
814
368k
  vrf = sctp_find_vrf(stcb->asoc.vrf_id);
815
368k
  if (vrf == NULL) {
816
    /* no vrf, no addresses */
817
0
    SCTP_IPI_ADDR_RUNLOCK();
818
0
    return (0);
819
0
  }
820
821
368k
  if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
822
0
    LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
823
0
      if ((loopback_scope == 0) &&
824
0
          SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
825
0
        continue;
826
0
      }
827
0
      LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
828
0
        if (sctp_is_addr_restricted(stcb, sctp_ifa) &&
829
0
            (!sctp_is_addr_pending(stcb, sctp_ifa))) {
830
          /* We allow pending addresses, where we
831
           * have sent an asconf-add to be considered
832
           * valid.
833
           */
834
0
          continue;
835
0
        }
836
0
        if (sctp_ifa->address.sa.sa_family != to->sa_family) {
837
0
          continue;
838
0
        }
839
0
        switch (sctp_ifa->address.sa.sa_family) {
840
0
#ifdef INET
841
0
        case AF_INET:
842
0
          if (ipv4_addr_legal) {
843
0
            struct sockaddr_in *sin, *rsin;
844
845
0
            sin = &sctp_ifa->address.sin;
846
0
            rsin = (struct sockaddr_in *)to;
847
0
            if ((ipv4_local_scope == 0) &&
848
0
                IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
849
0
              continue;
850
0
            }
851
#if defined(__FreeBSD__) && !defined(__Userspace__)
852
            if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
853
                                 &sin->sin_addr) != 0) {
854
              continue;
855
            }
856
#endif
857
0
            if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
858
0
              SCTP_IPI_ADDR_RUNLOCK();
859
0
              return (1);
860
0
            }
861
0
          }
862
0
          break;
863
0
#endif
864
0
#ifdef INET6
865
0
        case AF_INET6:
866
0
          if (ipv6_addr_legal) {
867
0
            struct sockaddr_in6 *sin6, *rsin6;
868
#if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
869
            struct sockaddr_in6 lsa6;
870
#endif
871
0
            sin6 = &sctp_ifa->address.sin6;
872
0
            rsin6 = (struct sockaddr_in6 *)to;
873
#if defined(__FreeBSD__) && !defined(__Userspace__)
874
            if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
875
                                 &sin6->sin6_addr) != 0) {
876
              continue;
877
            }
878
#endif
879
0
            if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
880
0
              if (local_scope == 0)
881
0
                continue;
882
#if defined(SCTP_EMBEDDED_V6_SCOPE)
883
              if (sin6->sin6_scope_id == 0) {
884
#ifdef SCTP_KAME
885
                if (sa6_recoverscope(sin6) != 0)
886
                  continue;
887
#else
888
                lsa6 = *sin6;
889
                if (in6_recoverscope(&lsa6,
890
                                     &lsa6.sin6_addr,
891
                                     NULL))
892
                  continue;
893
                sin6 = &lsa6;
894
#endif /* SCTP_KAME */
895
              }
896
#endif /* SCTP_EMBEDDED_V6_SCOPE */
897
0
            }
898
0
            if ((site_scope == 0) &&
899
0
                (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
900
0
              continue;
901
0
            }
902
0
            if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
903
0
              SCTP_IPI_ADDR_RUNLOCK();
904
0
              return (1);
905
0
            }
906
0
          }
907
0
          break;
908
0
#endif
909
0
#if defined(__Userspace__)
910
0
        case AF_CONN:
911
0
          if (conn_addr_legal) {
912
0
            struct sockaddr_conn *sconn, *rsconn;
913
914
0
            sconn = &sctp_ifa->address.sconn;
915
0
            rsconn = (struct sockaddr_conn *)to;
916
0
            if (sconn->sconn_addr == rsconn->sconn_addr) {
917
0
              SCTP_IPI_ADDR_RUNLOCK();
918
0
              return (1);
919
0
            }
920
0
          }
921
0
          break;
922
0
#endif
923
0
        default:
924
          /* TSNH */
925
0
          break;
926
0
        }
927
0
      }
928
0
    }
929
368k
  } else {
930
368k
    struct sctp_laddr *laddr;
931
932
368k
    LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list, sctp_nxt_addr) {
933
368k
      if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
934
0
        SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
935
0
        continue;
936
0
      }
937
368k
      if (sctp_is_addr_restricted(stcb, laddr->ifa) &&
938
368k
          (!sctp_is_addr_pending(stcb, laddr->ifa))) {
939
        /* We allow pending addresses, where we
940
         * have sent an asconf-add to be considered
941
         * valid.
942
         */
943
0
        continue;
944
0
      }
945
368k
      if (laddr->ifa->address.sa.sa_family != to->sa_family) {
946
0
        continue;
947
0
      }
948
368k
      switch (to->sa_family) {
949
0
#ifdef INET
950
0
      case AF_INET:
951
0
      {
952
0
        struct sockaddr_in *sin, *rsin;
953
954
0
        sin = &laddr->ifa->address.sin;
955
0
        rsin = (struct sockaddr_in *)to;
956
0
        if (sin->sin_addr.s_addr == rsin->sin_addr.s_addr) {
957
0
          SCTP_IPI_ADDR_RUNLOCK();
958
0
          return (1);
959
0
        }
960
0
        break;
961
0
      }
962
0
#endif
963
0
#ifdef INET6
964
0
      case AF_INET6:
965
0
      {
966
0
        struct sockaddr_in6 *sin6, *rsin6;
967
968
0
        sin6 = &laddr->ifa->address.sin6;
969
0
        rsin6 = (struct sockaddr_in6 *)to;
970
0
        if (SCTP6_ARE_ADDR_EQUAL(sin6, rsin6)) {
971
0
          SCTP_IPI_ADDR_RUNLOCK();
972
0
          return (1);
973
0
        }
974
0
        break;
975
0
      }
976
977
0
#endif
978
0
#if defined(__Userspace__)
979
368k
      case AF_CONN:
980
368k
      {
981
368k
        struct sockaddr_conn *sconn, *rsconn;
982
983
368k
        sconn = &laddr->ifa->address.sconn;
984
368k
        rsconn = (struct sockaddr_conn *)to;
985
368k
        if (sconn->sconn_addr == rsconn->sconn_addr) {
986
368k
          SCTP_IPI_ADDR_RUNLOCK();
987
368k
          return (1);
988
368k
        }
989
0
        break;
990
368k
      }
991
0
#endif
992
0
      default:
993
        /* TSNH */
994
0
        break;
995
368k
      }
996
368k
    }
997
368k
  }
998
0
  SCTP_IPI_ADDR_RUNLOCK();
999
0
  return (0);
1000
0
}
1001
1002
static struct sctp_tcb *
1003
sctp_tcb_special_locate(struct sctp_inpcb **inp_p, struct sockaddr *from,
1004
    struct sockaddr *to, struct sctp_nets **netp, uint32_t vrf_id)
1005
337k
{
1006
  /**** ASSUMES THE CALLER holds the INP_INFO_RLOCK */
1007
  /*
1008
   * If we support the TCP model, then we must now dig through to see
1009
   * if we can find our endpoint in the list of tcp ep's.
1010
   */
1011
337k
  uint16_t lport, rport;
1012
337k
  struct sctppcbhead *ephead;
1013
337k
  struct sctp_inpcb *inp;
1014
337k
  struct sctp_laddr *laddr;
1015
337k
  struct sctp_tcb *stcb;
1016
337k
  struct sctp_nets *net;
1017
#ifdef SCTP_MVRF
1018
  int fnd, i;
1019
#endif
1020
1021
337k
  if ((to == NULL) || (from == NULL)) {
1022
0
    return (NULL);
1023
0
  }
1024
1025
337k
  switch (to->sa_family) {
1026
0
#ifdef INET
1027
0
  case AF_INET:
1028
0
    if (from->sa_family == AF_INET) {
1029
0
      lport = ((struct sockaddr_in *)to)->sin_port;
1030
0
      rport = ((struct sockaddr_in *)from)->sin_port;
1031
0
    } else {
1032
0
      return (NULL);
1033
0
    }
1034
0
    break;
1035
0
#endif
1036
0
#ifdef INET6
1037
0
  case AF_INET6:
1038
0
    if (from->sa_family == AF_INET6) {
1039
0
      lport = ((struct sockaddr_in6 *)to)->sin6_port;
1040
0
      rport = ((struct sockaddr_in6 *)from)->sin6_port;
1041
0
    } else {
1042
0
      return (NULL);
1043
0
    }
1044
0
    break;
1045
0
#endif
1046
0
#if defined(__Userspace__)
1047
337k
  case AF_CONN:
1048
337k
    if (from->sa_family == AF_CONN) {
1049
334k
      lport = ((struct sockaddr_conn *)to)->sconn_port;
1050
334k
      rport = ((struct sockaddr_conn *)from)->sconn_port;
1051
334k
    } else {
1052
3.28k
      return (NULL);
1053
3.28k
    }
1054
334k
    break;
1055
334k
#endif
1056
334k
  default:
1057
0
    return (NULL);
1058
337k
  }
1059
334k
  ephead = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
1060
  /*
1061
   * Ok now for each of the guys in this bucket we must look and see:
1062
   * - Does the remote port match. - Does there single association's
1063
   * addresses match this address (to). If so we update p_ep to point
1064
   * to this ep and return the tcb from it.
1065
   */
1066
334k
  LIST_FOREACH(inp, ephead, sctp_hash) {
1067
0
    SCTP_INP_RLOCK(inp);
1068
0
    if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1069
0
      SCTP_INP_RUNLOCK(inp);
1070
0
      continue;
1071
0
    }
1072
0
    if (lport != inp->sctp_lport) {
1073
0
      SCTP_INP_RUNLOCK(inp);
1074
0
      continue;
1075
0
    }
1076
#if defined(__FreeBSD__) && !defined(__Userspace__)
1077
    switch (to->sa_family) {
1078
#ifdef INET
1079
    case AF_INET:
1080
    {
1081
      struct sockaddr_in *sin;
1082
1083
      sin = (struct sockaddr_in *)to;
1084
      if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1085
                           &sin->sin_addr) != 0) {
1086
        SCTP_INP_RUNLOCK(inp);
1087
        continue;
1088
      }
1089
      break;
1090
    }
1091
#endif
1092
#ifdef INET6
1093
    case AF_INET6:
1094
    {
1095
      struct sockaddr_in6 *sin6;
1096
1097
      sin6 = (struct sockaddr_in6 *)to;
1098
      if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1099
                           &sin6->sin6_addr) != 0) {
1100
        SCTP_INP_RUNLOCK(inp);
1101
        continue;
1102
      }
1103
      break;
1104
    }
1105
#endif
1106
    default:
1107
      SCTP_INP_RUNLOCK(inp);
1108
      continue;
1109
    }
1110
#endif
1111
#ifdef SCTP_MVRF
1112
    fnd = 0;
1113
    for (i = 0; i < inp->num_vrfs; i++) {
1114
      if (inp->m_vrf_ids[i] == vrf_id) {
1115
        fnd = 1;
1116
        break;
1117
      }
1118
    }
1119
    if (fnd == 0) {
1120
      SCTP_INP_RUNLOCK(inp);
1121
      continue;
1122
    }
1123
#else
1124
0
    if (inp->def_vrf_id != vrf_id) {
1125
0
      SCTP_INP_RUNLOCK(inp);
1126
0
      continue;
1127
0
    }
1128
0
#endif
1129
    /* check to see if the ep has one of the addresses */
1130
0
    if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
1131
      /* We are NOT bound all, so look further */
1132
0
      int match = 0;
1133
1134
0
      LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1135
0
        if (laddr->ifa == NULL) {
1136
0
          SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n", __func__);
1137
0
          continue;
1138
0
        }
1139
0
        if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1140
0
          SCTPDBG(SCTP_DEBUG_PCB1, "ifa being deleted\n");
1141
0
          continue;
1142
0
        }
1143
0
        if (laddr->ifa->address.sa.sa_family ==
1144
0
            to->sa_family) {
1145
          /* see if it matches */
1146
0
#ifdef INET
1147
0
          if (from->sa_family == AF_INET) {
1148
0
            struct sockaddr_in *intf_addr, *sin;
1149
1150
0
            intf_addr = &laddr->ifa->address.sin;
1151
0
            sin = (struct sockaddr_in *)to;
1152
0
            if (sin->sin_addr.s_addr ==
1153
0
                intf_addr->sin_addr.s_addr) {
1154
0
              match = 1;
1155
0
              break;
1156
0
            }
1157
0
          }
1158
0
#endif
1159
0
#ifdef INET6
1160
0
          if (from->sa_family == AF_INET6) {
1161
0
            struct sockaddr_in6 *intf_addr6;
1162
0
            struct sockaddr_in6 *sin6;
1163
1164
0
            sin6 = (struct sockaddr_in6 *)
1165
0
                to;
1166
0
            intf_addr6 = &laddr->ifa->address.sin6;
1167
1168
0
            if (SCTP6_ARE_ADDR_EQUAL(sin6,
1169
0
                intf_addr6)) {
1170
0
              match = 1;
1171
0
              break;
1172
0
            }
1173
0
          }
1174
0
#endif
1175
0
#if defined(__Userspace__)
1176
0
          if (from->sa_family == AF_CONN) {
1177
0
            struct sockaddr_conn *intf_addr, *sconn;
1178
1179
0
            intf_addr = &laddr->ifa->address.sconn;
1180
0
            sconn = (struct sockaddr_conn *)to;
1181
0
            if (sconn->sconn_addr ==
1182
0
                intf_addr->sconn_addr) {
1183
0
              match = 1;
1184
0
              break;
1185
0
            }
1186
0
          }
1187
0
#endif
1188
0
        }
1189
0
      }
1190
0
      if (match == 0) {
1191
        /* This endpoint does not have this address */
1192
0
        SCTP_INP_RUNLOCK(inp);
1193
0
        continue;
1194
0
      }
1195
0
    }
1196
    /*
1197
     * Ok if we hit here the ep has the address, does it hold
1198
     * the tcb?
1199
     */
1200
    /* XXX: Why don't we TAILQ_FOREACH through sctp_asoc_list? */
1201
0
    stcb = LIST_FIRST(&inp->sctp_asoc_list);
1202
0
    if (stcb == NULL) {
1203
0
      SCTP_INP_RUNLOCK(inp);
1204
0
      continue;
1205
0
    }
1206
0
    SCTP_TCB_LOCK(stcb);
1207
0
    if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1208
0
      SCTP_TCB_UNLOCK(stcb);
1209
0
      SCTP_INP_RUNLOCK(inp);
1210
0
      continue;
1211
0
    }
1212
0
    if (stcb->rport != rport) {
1213
      /* remote port does not match. */
1214
0
      SCTP_TCB_UNLOCK(stcb);
1215
0
      SCTP_INP_RUNLOCK(inp);
1216
0
      continue;
1217
0
    }
1218
0
    if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1219
0
      SCTP_TCB_UNLOCK(stcb);
1220
0
      SCTP_INP_RUNLOCK(inp);
1221
0
      continue;
1222
0
    }
1223
0
    if (!sctp_does_stcb_own_this_addr(stcb, to)) {
1224
0
      SCTP_TCB_UNLOCK(stcb);
1225
0
      SCTP_INP_RUNLOCK(inp);
1226
0
      continue;
1227
0
    }
1228
    /* Does this TCB have a matching address? */
1229
0
    TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1230
0
      if (net->ro._l_addr.sa.sa_family != from->sa_family) {
1231
        /* not the same family, can't be a match */
1232
0
        continue;
1233
0
      }
1234
0
      switch (from->sa_family) {
1235
0
#ifdef INET
1236
0
      case AF_INET:
1237
0
      {
1238
0
        struct sockaddr_in *sin, *rsin;
1239
1240
0
        sin = (struct sockaddr_in *)&net->ro._l_addr;
1241
0
        rsin = (struct sockaddr_in *)from;
1242
0
        if (sin->sin_addr.s_addr ==
1243
0
            rsin->sin_addr.s_addr) {
1244
          /* found it */
1245
0
          if (netp != NULL) {
1246
0
            *netp = net;
1247
0
          }
1248
          /* Update the endpoint pointer */
1249
0
          *inp_p = inp;
1250
0
          SCTP_INP_RUNLOCK(inp);
1251
0
          return (stcb);
1252
0
        }
1253
0
        break;
1254
0
      }
1255
0
#endif
1256
0
#ifdef INET6
1257
0
      case AF_INET6:
1258
0
      {
1259
0
        struct sockaddr_in6 *sin6, *rsin6;
1260
1261
0
        sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1262
0
        rsin6 = (struct sockaddr_in6 *)from;
1263
0
        if (SCTP6_ARE_ADDR_EQUAL(sin6,
1264
0
            rsin6)) {
1265
          /* found it */
1266
0
          if (netp != NULL) {
1267
0
            *netp = net;
1268
0
          }
1269
          /* Update the endpoint pointer */
1270
0
          *inp_p = inp;
1271
0
          SCTP_INP_RUNLOCK(inp);
1272
0
          return (stcb);
1273
0
        }
1274
0
        break;
1275
0
      }
1276
0
#endif
1277
0
#if defined(__Userspace__)
1278
0
      case AF_CONN:
1279
0
      {
1280
0
        struct sockaddr_conn *sconn, *rsconn;
1281
1282
0
        sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1283
0
        rsconn = (struct sockaddr_conn *)from;
1284
0
        if (sconn->sconn_addr == rsconn->sconn_addr) {
1285
          /* found it */
1286
0
          if (netp != NULL) {
1287
0
            *netp = net;
1288
0
          }
1289
          /* Update the endpoint pointer */
1290
0
          *inp_p = inp;
1291
0
          SCTP_INP_RUNLOCK(inp);
1292
0
          return (stcb);
1293
0
        }
1294
0
        break;
1295
0
      }
1296
0
#endif
1297
0
      default:
1298
        /* TSNH */
1299
0
        break;
1300
0
      }
1301
0
    }
1302
0
    SCTP_TCB_UNLOCK(stcb);
1303
0
    SCTP_INP_RUNLOCK(inp);
1304
0
  }
1305
334k
  return (NULL);
1306
334k
}
1307
1308
/*
1309
 * rules for use
1310
 *
1311
 * 1) If I return a NULL you must decrement any INP ref cnt. 2) If I find an
1312
 * stcb, both will be locked (locked_tcb and stcb) but decrement will be done
1313
 * (if locked == NULL). 3) Decrement happens on return ONLY if locked ==
1314
 * NULL.
1315
 */
1316
1317
struct sctp_tcb *
1318
sctp_findassociation_ep_addr(struct sctp_inpcb **inp_p, struct sockaddr *remote,
1319
    struct sctp_nets **netp, struct sockaddr *local, struct sctp_tcb *locked_tcb)
1320
358k
{
1321
358k
  struct sctpasochead *head;
1322
358k
  struct sctp_inpcb *inp;
1323
358k
  struct sctp_tcb *stcb = NULL;
1324
358k
  struct sctp_nets *net;
1325
358k
  uint16_t rport;
1326
1327
358k
  inp = *inp_p;
1328
358k
  switch (remote->sa_family) {
1329
0
#ifdef INET
1330
1.30k
  case AF_INET:
1331
1.30k
    rport = (((struct sockaddr_in *)remote)->sin_port);
1332
1.30k
    break;
1333
0
#endif
1334
0
#ifdef INET6
1335
1.98k
  case AF_INET6:
1336
1.98k
    rport = (((struct sockaddr_in6 *)remote)->sin6_port);
1337
1.98k
    break;
1338
0
#endif
1339
0
#if defined(__Userspace__)
1340
355k
  case AF_CONN:
1341
355k
    rport = (((struct sockaddr_conn *)remote)->sconn_port);
1342
355k
    break;
1343
0
#endif
1344
0
  default:
1345
0
    return (NULL);
1346
358k
  }
1347
358k
  if (locked_tcb) {
1348
    /*
1349
     * UN-lock so we can do proper locking here this occurs when
1350
     * called from load_addresses_from_init.
1351
     */
1352
10.6k
    atomic_add_int(&locked_tcb->asoc.refcnt, 1);
1353
10.6k
    SCTP_TCB_UNLOCK(locked_tcb);
1354
10.6k
  }
1355
358k
  SCTP_INP_INFO_RLOCK();
1356
358k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
1357
358k
      (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1358
    /*-
1359
     * Now either this guy is our listener or it's the
1360
     * connector. If it is the one that issued the connect, then
1361
     * it's only chance is to be the first TCB in the list. If
1362
     * it is the acceptor, then do the special_lookup to hash
1363
     * and find the real inp.
1364
     */
1365
358k
    if ((inp->sctp_socket) && SCTP_IS_LISTENING(inp)) {
1366
      /* to is peer addr, from is my addr */
1367
6.28k
#ifndef SCTP_MVRF
1368
6.28k
      stcb = sctp_tcb_special_locate(inp_p, remote, local,
1369
6.28k
          netp, inp->def_vrf_id);
1370
6.28k
      if ((stcb != NULL) && (locked_tcb == NULL)) {
1371
        /* we have a locked tcb, lower refcount */
1372
0
        SCTP_INP_DECR_REF(inp);
1373
0
      }
1374
6.28k
      if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1375
0
        SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1376
0
        SCTP_TCB_LOCK(locked_tcb);
1377
0
        atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1378
0
        SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1379
0
      }
1380
#else
1381
      /*-
1382
       * MVRF is tricky, we must look in every VRF
1383
       * the endpoint has.
1384
       */
1385
      int i;
1386
1387
      for (i = 0; i < inp->num_vrfs; i++) {
1388
        stcb = sctp_tcb_special_locate(inp_p, remote, local,
1389
                                       netp, inp->m_vrf_ids[i]);
1390
        if ((stcb != NULL) && (locked_tcb == NULL)) {
1391
          /* we have a locked tcb, lower refcount */
1392
          SCTP_INP_DECR_REF(inp);
1393
          break;
1394
        }
1395
        if ((locked_tcb != NULL) && (locked_tcb != stcb)) {
1396
          SCTP_INP_RLOCK(locked_tcb->sctp_ep);
1397
          SCTP_TCB_LOCK(locked_tcb);
1398
          atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1399
          SCTP_INP_RUNLOCK(locked_tcb->sctp_ep);
1400
          break;
1401
        }
1402
      }
1403
#endif
1404
6.28k
      SCTP_INP_INFO_RUNLOCK();
1405
6.28k
      return (stcb);
1406
352k
    } else {
1407
352k
      SCTP_INP_WLOCK(inp);
1408
352k
      if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1409
0
        goto null_return;
1410
0
      }
1411
352k
      stcb = LIST_FIRST(&inp->sctp_asoc_list);
1412
352k
      if (stcb == NULL) {
1413
12.8k
        goto null_return;
1414
12.8k
      }
1415
339k
      SCTP_TCB_LOCK(stcb);
1416
1417
339k
      if (stcb->rport != rport) {
1418
        /* remote port does not match. */
1419
0
        SCTP_TCB_UNLOCK(stcb);
1420
0
        goto null_return;
1421
0
      }
1422
339k
      if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1423
0
        SCTP_TCB_UNLOCK(stcb);
1424
0
        goto null_return;
1425
0
      }
1426
339k
      if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1427
0
        SCTP_TCB_UNLOCK(stcb);
1428
0
        goto null_return;
1429
0
      }
1430
      /* now look at the list of remote addresses */
1431
339k
      TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1432
339k
#ifdef INVARIANTS
1433
339k
        if (net == (TAILQ_NEXT(net, sctp_next))) {
1434
0
          panic("Corrupt net list");
1435
0
        }
1436
339k
#endif
1437
339k
        if (net->ro._l_addr.sa.sa_family !=
1438
339k
            remote->sa_family) {
1439
          /* not the same family */
1440
0
          continue;
1441
0
        }
1442
339k
        switch (remote->sa_family) {
1443
0
#ifdef INET
1444
0
        case AF_INET:
1445
0
        {
1446
0
          struct sockaddr_in *sin, *rsin;
1447
1448
0
          sin = (struct sockaddr_in *)
1449
0
              &net->ro._l_addr;
1450
0
          rsin = (struct sockaddr_in *)remote;
1451
0
          if (sin->sin_addr.s_addr ==
1452
0
              rsin->sin_addr.s_addr) {
1453
            /* found it */
1454
0
            if (netp != NULL) {
1455
0
              *netp = net;
1456
0
            }
1457
0
            if (locked_tcb == NULL) {
1458
0
              SCTP_INP_DECR_REF(inp);
1459
0
            } else if (locked_tcb != stcb) {
1460
0
              SCTP_TCB_LOCK(locked_tcb);
1461
0
            }
1462
0
            if (locked_tcb) {
1463
0
              atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1464
0
            }
1465
1466
0
            SCTP_INP_WUNLOCK(inp);
1467
0
            SCTP_INP_INFO_RUNLOCK();
1468
0
            return (stcb);
1469
0
          }
1470
0
          break;
1471
0
        }
1472
0
#endif
1473
0
#ifdef INET6
1474
0
        case AF_INET6:
1475
0
        {
1476
0
          struct sockaddr_in6 *sin6, *rsin6;
1477
1478
0
          sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1479
0
          rsin6 = (struct sockaddr_in6 *)remote;
1480
0
          if (SCTP6_ARE_ADDR_EQUAL(sin6,
1481
0
              rsin6)) {
1482
            /* found it */
1483
0
            if (netp != NULL) {
1484
0
              *netp = net;
1485
0
            }
1486
0
            if (locked_tcb == NULL) {
1487
0
              SCTP_INP_DECR_REF(inp);
1488
0
            } else if (locked_tcb != stcb) {
1489
0
              SCTP_TCB_LOCK(locked_tcb);
1490
0
            }
1491
0
            if (locked_tcb) {
1492
0
              atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1493
0
            }
1494
0
            SCTP_INP_WUNLOCK(inp);
1495
0
            SCTP_INP_INFO_RUNLOCK();
1496
0
            return (stcb);
1497
0
          }
1498
0
          break;
1499
0
        }
1500
0
#endif
1501
0
#if defined(__Userspace__)
1502
339k
        case AF_CONN:
1503
339k
        {
1504
339k
          struct sockaddr_conn *sconn, *rsconn;
1505
1506
339k
          sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1507
339k
          rsconn = (struct sockaddr_conn *)remote;
1508
339k
          if (sconn->sconn_addr == rsconn->sconn_addr) {
1509
            /* found it */
1510
339k
            if (netp != NULL) {
1511
339k
              *netp = net;
1512
339k
            }
1513
339k
            if (locked_tcb == NULL) {
1514
328k
              SCTP_INP_DECR_REF(inp);
1515
328k
            } else if (locked_tcb != stcb) {
1516
0
              SCTP_TCB_LOCK(locked_tcb);
1517
0
            }
1518
339k
            if (locked_tcb) {
1519
10.6k
              atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1520
10.6k
            }
1521
339k
            SCTP_INP_WUNLOCK(inp);
1522
339k
            SCTP_INP_INFO_RUNLOCK();
1523
339k
            return (stcb);
1524
339k
          }
1525
0
          break;
1526
339k
        }
1527
0
#endif
1528
0
        default:
1529
          /* TSNH */
1530
0
          break;
1531
339k
        }
1532
339k
      }
1533
0
      SCTP_TCB_UNLOCK(stcb);
1534
0
    }
1535
358k
  } else {
1536
0
    SCTP_INP_WLOCK(inp);
1537
0
    if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1538
0
      goto null_return;
1539
0
    }
1540
0
    head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(rport,
1541
0
                                                   inp->sctp_hashmark)];
1542
0
    LIST_FOREACH(stcb, head, sctp_tcbhash) {
1543
0
      if (stcb->rport != rport) {
1544
        /* remote port does not match */
1545
0
        continue;
1546
0
      }
1547
0
      SCTP_TCB_LOCK(stcb);
1548
0
      if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1549
0
        SCTP_TCB_UNLOCK(stcb);
1550
0
        continue;
1551
0
      }
1552
0
      if (local && !sctp_does_stcb_own_this_addr(stcb, local)) {
1553
0
        SCTP_TCB_UNLOCK(stcb);
1554
0
        continue;
1555
0
      }
1556
      /* now look at the list of remote addresses */
1557
0
      TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1558
0
#ifdef INVARIANTS
1559
0
        if (net == (TAILQ_NEXT(net, sctp_next))) {
1560
0
          panic("Corrupt net list");
1561
0
        }
1562
0
#endif
1563
0
        if (net->ro._l_addr.sa.sa_family !=
1564
0
            remote->sa_family) {
1565
          /* not the same family */
1566
0
          continue;
1567
0
        }
1568
0
        switch (remote->sa_family) {
1569
0
#ifdef INET
1570
0
        case AF_INET:
1571
0
        {
1572
0
          struct sockaddr_in *sin, *rsin;
1573
1574
0
          sin = (struct sockaddr_in *)
1575
0
              &net->ro._l_addr;
1576
0
          rsin = (struct sockaddr_in *)remote;
1577
0
          if (sin->sin_addr.s_addr ==
1578
0
              rsin->sin_addr.s_addr) {
1579
            /* found it */
1580
0
            if (netp != NULL) {
1581
0
              *netp = net;
1582
0
            }
1583
0
            if (locked_tcb == NULL) {
1584
0
              SCTP_INP_DECR_REF(inp);
1585
0
            } else if (locked_tcb != stcb) {
1586
0
              SCTP_TCB_LOCK(locked_tcb);
1587
0
            }
1588
0
            if (locked_tcb) {
1589
0
              atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1590
0
            }
1591
0
            SCTP_INP_WUNLOCK(inp);
1592
0
            SCTP_INP_INFO_RUNLOCK();
1593
0
            return (stcb);
1594
0
          }
1595
0
          break;
1596
0
        }
1597
0
#endif
1598
0
#ifdef INET6
1599
0
        case AF_INET6:
1600
0
        {
1601
0
          struct sockaddr_in6 *sin6, *rsin6;
1602
1603
0
          sin6 = (struct sockaddr_in6 *)
1604
0
              &net->ro._l_addr;
1605
0
          rsin6 = (struct sockaddr_in6 *)remote;
1606
0
          if (SCTP6_ARE_ADDR_EQUAL(sin6,
1607
0
              rsin6)) {
1608
            /* found it */
1609
0
            if (netp != NULL) {
1610
0
              *netp = net;
1611
0
            }
1612
0
            if (locked_tcb == NULL) {
1613
0
              SCTP_INP_DECR_REF(inp);
1614
0
            } else if (locked_tcb != stcb) {
1615
0
              SCTP_TCB_LOCK(locked_tcb);
1616
0
            }
1617
0
            if (locked_tcb) {
1618
0
              atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1619
0
            }
1620
0
            SCTP_INP_WUNLOCK(inp);
1621
0
            SCTP_INP_INFO_RUNLOCK();
1622
0
            return (stcb);
1623
0
          }
1624
0
          break;
1625
0
        }
1626
0
#endif
1627
0
#if defined(__Userspace__)
1628
0
        case AF_CONN:
1629
0
        {
1630
0
          struct sockaddr_conn *sconn, *rsconn;
1631
1632
0
          sconn = (struct sockaddr_conn *)&net->ro._l_addr;
1633
0
          rsconn = (struct sockaddr_conn *)remote;
1634
0
          if (sconn->sconn_addr == rsconn->sconn_addr) {
1635
            /* found it */
1636
0
            if (netp != NULL) {
1637
0
              *netp = net;
1638
0
            }
1639
0
            if (locked_tcb == NULL) {
1640
0
              SCTP_INP_DECR_REF(inp);
1641
0
            } else if (locked_tcb != stcb) {
1642
0
              SCTP_TCB_LOCK(locked_tcb);
1643
0
            }
1644
0
            if (locked_tcb) {
1645
0
              atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1646
0
            }
1647
0
            SCTP_INP_WUNLOCK(inp);
1648
0
            SCTP_INP_INFO_RUNLOCK();
1649
0
            return (stcb);
1650
0
          }
1651
0
          break;
1652
0
        }
1653
0
#endif
1654
0
        default:
1655
          /* TSNH */
1656
0
          break;
1657
0
        }
1658
0
      }
1659
0
      SCTP_TCB_UNLOCK(stcb);
1660
0
    }
1661
0
  }
1662
12.8k
null_return:
1663
  /* clean up for returning null */
1664
12.8k
  if (locked_tcb) {
1665
0
    SCTP_TCB_LOCK(locked_tcb);
1666
0
    atomic_subtract_int(&locked_tcb->asoc.refcnt, 1);
1667
0
  }
1668
12.8k
  SCTP_INP_WUNLOCK(inp);
1669
12.8k
  SCTP_INP_INFO_RUNLOCK();
1670
  /* not found */
1671
12.8k
  return (NULL);
1672
12.8k
}
1673
1674
/*
1675
 * Find an association for a specific endpoint using the association id given
1676
 * out in the COMM_UP notification
1677
 */
1678
struct sctp_tcb *
1679
sctp_findasoc_ep_asocid_locked(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1680
12.8k
{
1681
  /*
1682
   * Use my the assoc_id to find a endpoint
1683
   */
1684
12.8k
  struct sctpasochead *head;
1685
12.8k
  struct sctp_tcb *stcb;
1686
12.8k
  uint32_t id;
1687
1688
12.8k
  if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1689
0
    SCTP_PRINTF("TSNH ep_associd0\n");
1690
0
    return (NULL);
1691
0
  }
1692
12.8k
  id = (uint32_t)asoc_id;
1693
12.8k
  head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
1694
12.8k
  if (head == NULL) {
1695
    /* invalid id TSNH */
1696
0
    SCTP_PRINTF("TSNH ep_associd1\n");
1697
0
    return (NULL);
1698
0
  }
1699
12.8k
  LIST_FOREACH(stcb, head, sctp_tcbasocidhash) {
1700
0
    if (stcb->asoc.assoc_id == id) {
1701
0
      if (inp != stcb->sctp_ep) {
1702
        /*
1703
         * some other guy has the same id active (id
1704
         * collision ??).
1705
         */
1706
0
        SCTP_PRINTF("TSNH ep_associd2\n");
1707
0
        continue;
1708
0
      }
1709
0
      if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
1710
0
        continue;
1711
0
      }
1712
0
      if (want_lock) {
1713
0
        SCTP_TCB_LOCK(stcb);
1714
0
      }
1715
0
      return (stcb);
1716
0
    }
1717
0
  }
1718
12.8k
  return (NULL);
1719
12.8k
}
1720
1721
struct sctp_tcb *
1722
sctp_findassociation_ep_asocid(struct sctp_inpcb *inp, sctp_assoc_t asoc_id, int want_lock)
1723
0
{
1724
0
  struct sctp_tcb *stcb;
1725
1726
0
  SCTP_INP_RLOCK(inp);
1727
0
  stcb = sctp_findasoc_ep_asocid_locked(inp, asoc_id, want_lock);
1728
0
  SCTP_INP_RUNLOCK(inp);
1729
0
  return (stcb);
1730
0
}
1731
1732
/*
1733
 * Endpoint probe expects that the INP_INFO is locked.
1734
 */
1735
static struct sctp_inpcb *
1736
sctp_endpoint_probe(struct sockaddr *nam, struct sctppcbhead *head,
1737
        uint16_t lport, uint32_t vrf_id)
1738
344k
{
1739
344k
  struct sctp_inpcb *inp;
1740
344k
  struct sctp_laddr *laddr;
1741
344k
#ifdef INET
1742
344k
  struct sockaddr_in *sin;
1743
344k
#endif
1744
344k
#ifdef INET6
1745
344k
  struct sockaddr_in6 *sin6;
1746
344k
  struct sockaddr_in6 *intf_addr6;
1747
344k
#endif
1748
344k
#if defined(__Userspace__)
1749
344k
  struct sockaddr_conn *sconn;
1750
344k
#endif
1751
#ifdef SCTP_MVRF
1752
  int i;
1753
#endif
1754
344k
  int  fnd;
1755
1756
344k
#ifdef INET
1757
344k
  sin = NULL;
1758
344k
#endif
1759
344k
#ifdef INET6
1760
344k
  sin6 = NULL;
1761
344k
#endif
1762
344k
#if defined(__Userspace__)
1763
344k
  sconn = NULL;
1764
344k
#endif
1765
344k
  switch (nam->sa_family) {
1766
0
#ifdef INET
1767
0
  case AF_INET:
1768
0
    sin = (struct sockaddr_in *)nam;
1769
0
    break;
1770
0
#endif
1771
0
#ifdef INET6
1772
0
  case AF_INET6:
1773
0
    sin6 = (struct sockaddr_in6 *)nam;
1774
0
    break;
1775
0
#endif
1776
0
#if defined(__Userspace__)
1777
344k
  case AF_CONN:
1778
344k
    sconn = (struct sockaddr_conn *)nam;
1779
344k
    break;
1780
0
#endif
1781
0
  default:
1782
    /* unsupported family */
1783
0
    return (NULL);
1784
344k
  }
1785
1786
344k
  if (head == NULL)
1787
0
    return (NULL);
1788
1789
344k
  LIST_FOREACH(inp, head, sctp_hash) {
1790
331k
    SCTP_INP_RLOCK(inp);
1791
331k
    if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1792
0
      SCTP_INP_RUNLOCK(inp);
1793
0
      continue;
1794
0
    }
1795
331k
    if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) &&
1796
331k
        (inp->sctp_lport == lport)) {
1797
      /* got it */
1798
0
      switch (nam->sa_family) {
1799
0
#ifdef INET
1800
0
      case AF_INET:
1801
0
        if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1802
0
            SCTP_IPV6_V6ONLY(inp)) {
1803
          /* IPv4 on a IPv6 socket with ONLY IPv6 set */
1804
0
          SCTP_INP_RUNLOCK(inp);
1805
0
          continue;
1806
0
        }
1807
#if defined(__FreeBSD__) && !defined(__Userspace__)
1808
        if (prison_check_ip4(inp->ip_inp.inp.inp_cred,
1809
                             &sin->sin_addr) != 0) {
1810
          SCTP_INP_RUNLOCK(inp);
1811
          continue;
1812
        }
1813
#endif
1814
0
        break;
1815
0
#endif
1816
0
#ifdef INET6
1817
0
      case AF_INET6:
1818
        /* A V6 address and the endpoint is NOT bound V6 */
1819
0
        if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
1820
0
          SCTP_INP_RUNLOCK(inp);
1821
0
          continue;
1822
0
        }
1823
#if defined(__FreeBSD__) && !defined(__Userspace__)
1824
        if (prison_check_ip6(inp->ip_inp.inp.inp_cred,
1825
                             &sin6->sin6_addr) != 0) {
1826
          SCTP_INP_RUNLOCK(inp);
1827
          continue;
1828
        }
1829
#endif
1830
0
        break;
1831
0
#endif
1832
0
      default:
1833
0
        break;
1834
0
      }
1835
      /* does a VRF id match? */
1836
0
      fnd = 0;
1837
#ifdef SCTP_MVRF
1838
      for (i = 0; i < inp->num_vrfs; i++) {
1839
        if (inp->m_vrf_ids[i] == vrf_id) {
1840
          fnd = 1;
1841
          break;
1842
        }
1843
      }
1844
#else
1845
0
      if (inp->def_vrf_id == vrf_id)
1846
0
        fnd = 1;
1847
0
#endif
1848
1849
0
      SCTP_INP_RUNLOCK(inp);
1850
0
      if (!fnd)
1851
0
        continue;
1852
0
      return (inp);
1853
0
    }
1854
331k
    SCTP_INP_RUNLOCK(inp);
1855
331k
  }
1856
344k
  switch (nam->sa_family) {
1857
0
#ifdef INET
1858
0
  case AF_INET:
1859
0
    if (sin->sin_addr.s_addr == INADDR_ANY) {
1860
      /* Can't hunt for one that has no address specified */
1861
0
      return (NULL);
1862
0
    }
1863
0
    break;
1864
0
#endif
1865
0
#ifdef INET6
1866
0
  case AF_INET6:
1867
0
    if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1868
      /* Can't hunt for one that has no address specified */
1869
0
      return (NULL);
1870
0
    }
1871
0
    break;
1872
0
#endif
1873
0
#if defined(__Userspace__)
1874
344k
  case AF_CONN:
1875
344k
    if (sconn->sconn_addr == NULL) {
1876
0
      return (NULL);
1877
0
    }
1878
344k
    break;
1879
344k
#endif
1880
344k
  default:
1881
0
    break;
1882
344k
  }
1883
  /*
1884
   * ok, not bound to all so see if we can find a EP bound to this
1885
   * address.
1886
   */
1887
344k
  LIST_FOREACH(inp, head, sctp_hash) {
1888
331k
    SCTP_INP_RLOCK(inp);
1889
331k
    if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
1890
0
      SCTP_INP_RUNLOCK(inp);
1891
0
      continue;
1892
0
    }
1893
331k
    if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL)) {
1894
0
      SCTP_INP_RUNLOCK(inp);
1895
0
      continue;
1896
0
    }
1897
    /*
1898
     * Ok this could be a likely candidate, look at all of its
1899
     * addresses
1900
     */
1901
331k
    if (inp->sctp_lport != lport) {
1902
19
      SCTP_INP_RUNLOCK(inp);
1903
19
      continue;
1904
19
    }
1905
    /* does a VRF id match? */
1906
331k
    fnd = 0;
1907
#ifdef SCTP_MVRF
1908
    for (i = 0; i < inp->num_vrfs; i++) {
1909
      if (inp->m_vrf_ids[i] == vrf_id) {
1910
        fnd = 1;
1911
        break;
1912
      }
1913
    }
1914
#else
1915
331k
    if (inp->def_vrf_id == vrf_id)
1916
331k
      fnd = 1;
1917
1918
331k
#endif
1919
331k
    if (!fnd) {
1920
0
      SCTP_INP_RUNLOCK(inp);
1921
0
      continue;
1922
0
    }
1923
331k
    LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1924
331k
      if (laddr->ifa == NULL) {
1925
0
        SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
1926
0
          __func__);
1927
0
        continue;
1928
0
      }
1929
331k
      SCTPDBG(SCTP_DEBUG_PCB1, "Ok laddr->ifa:%p is possible, ",
1930
331k
        (void *)laddr->ifa);
1931
331k
      if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
1932
0
        SCTPDBG(SCTP_DEBUG_PCB1, "Huh IFA being deleted\n");
1933
0
        continue;
1934
0
      }
1935
331k
      if (laddr->ifa->address.sa.sa_family == nam->sa_family) {
1936
        /* possible, see if it matches */
1937
331k
        switch (nam->sa_family) {
1938
0
#ifdef INET
1939
0
        case AF_INET:
1940
#if defined(__APPLE__) && !defined(__Userspace__)
1941
          if (sin == NULL) {
1942
            /* TSNH */
1943
            break;
1944
          }
1945
#endif
1946
0
          if (sin->sin_addr.s_addr ==
1947
0
              laddr->ifa->address.sin.sin_addr.s_addr) {
1948
0
            SCTP_INP_RUNLOCK(inp);
1949
0
            return (inp);
1950
0
          }
1951
0
          break;
1952
0
#endif
1953
0
#ifdef INET6
1954
0
        case AF_INET6:
1955
0
          intf_addr6 = &laddr->ifa->address.sin6;
1956
0
          if (SCTP6_ARE_ADDR_EQUAL(sin6,
1957
0
              intf_addr6)) {
1958
0
            SCTP_INP_RUNLOCK(inp);
1959
0
            return (inp);
1960
0
          }
1961
0
          break;
1962
0
#endif
1963
0
#if defined(__Userspace__)
1964
331k
        case AF_CONN:
1965
331k
          if (sconn->sconn_addr == laddr->ifa->address.sconn.sconn_addr) {
1966
331k
            SCTP_INP_RUNLOCK(inp);
1967
331k
            return (inp);
1968
331k
          }
1969
0
          break;
1970
331k
#endif
1971
331k
        }
1972
331k
      }
1973
331k
    }
1974
0
    SCTP_INP_RUNLOCK(inp);
1975
0
  }
1976
12.9k
  return (NULL);
1977
344k
}
1978
1979
static struct sctp_inpcb *
1980
sctp_isport_inuse(struct sctp_inpcb *inp, uint16_t lport, uint32_t vrf_id)
1981
0
{
1982
0
  struct sctppcbhead *head;
1983
0
  struct sctp_inpcb *t_inp;
1984
#ifdef SCTP_MVRF
1985
  int i;
1986
#endif
1987
0
  int fnd;
1988
1989
0
  head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
1990
0
      SCTP_BASE_INFO(hashmark))];
1991
0
  LIST_FOREACH(t_inp, head, sctp_hash) {
1992
0
    if (t_inp->sctp_lport != lport) {
1993
0
      continue;
1994
0
    }
1995
    /* is it in the VRF in question */
1996
0
    fnd = 0;
1997
#ifdef SCTP_MVRF
1998
    for (i = 0; i < inp->num_vrfs; i++) {
1999
      if (t_inp->m_vrf_ids[i] == vrf_id) {
2000
        fnd = 1;
2001
        break;
2002
      }
2003
    }
2004
#else
2005
0
    if (t_inp->def_vrf_id == vrf_id)
2006
0
      fnd = 1;
2007
0
#endif
2008
0
    if (!fnd)
2009
0
      continue;
2010
2011
    /* This one is in use. */
2012
    /* check the v6/v4 binding issue */
2013
0
    if ((t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2014
0
        SCTP_IPV6_V6ONLY(t_inp)) {
2015
0
      if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2016
        /* collision in V6 space */
2017
0
        return (t_inp);
2018
0
      } else {
2019
        /* inp is BOUND_V4 no conflict */
2020
0
        continue;
2021
0
      }
2022
0
    } else if (t_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2023
      /* t_inp is bound v4 and v6, conflict always */
2024
0
      return (t_inp);
2025
0
    } else {
2026
      /* t_inp is bound only V4 */
2027
0
      if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
2028
0
          SCTP_IPV6_V6ONLY(inp)) {
2029
        /* no conflict */
2030
0
        continue;
2031
0
      }
2032
      /* else fall through to conflict */
2033
0
    }
2034
0
    return (t_inp);
2035
0
  }
2036
0
  return (NULL);
2037
0
}
2038
2039
int
2040
sctp_swap_inpcb_for_listen(struct sctp_inpcb *inp)
2041
0
{
2042
  /* For 1-2-1 with port reuse */
2043
0
  struct sctppcbhead *head;
2044
0
  struct sctp_inpcb *tinp, *ninp;
2045
2046
0
  SCTP_INP_INFO_WLOCK_ASSERT();
2047
0
  SCTP_INP_WLOCK_ASSERT(inp);
2048
2049
0
  if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) {
2050
    /* only works with port reuse on */
2051
0
    return (-1);
2052
0
  }
2053
0
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0) {
2054
0
    return (0);
2055
0
  }
2056
0
  SCTP_INP_WUNLOCK(inp);
2057
0
  head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport,
2058
0
                                      SCTP_BASE_INFO(hashmark))];
2059
  /* Kick out all non-listeners to the TCP hash */
2060
0
  LIST_FOREACH_SAFE(tinp, head, sctp_hash, ninp) {
2061
0
    if (tinp->sctp_lport != inp->sctp_lport) {
2062
0
      continue;
2063
0
    }
2064
0
    if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2065
0
      continue;
2066
0
    }
2067
0
    if (tinp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
2068
0
      continue;
2069
0
    }
2070
0
    if (SCTP_IS_LISTENING(tinp)) {
2071
0
      continue;
2072
0
    }
2073
0
    SCTP_INP_WLOCK(tinp);
2074
0
    LIST_REMOVE(tinp, sctp_hash);
2075
0
    head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(tinp->sctp_lport, SCTP_BASE_INFO(hashtcpmark))];
2076
0
    tinp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
2077
0
    LIST_INSERT_HEAD(head, tinp, sctp_hash);
2078
0
    SCTP_INP_WUNLOCK(tinp);
2079
0
  }
2080
0
  SCTP_INP_WLOCK(inp);
2081
  /* Pull from where he was */
2082
0
  LIST_REMOVE(inp, sctp_hash);
2083
0
  inp->sctp_flags &= ~SCTP_PCB_FLAGS_IN_TCPPOOL;
2084
0
  head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(inp->sctp_lport, SCTP_BASE_INFO(hashmark))];
2085
0
  LIST_INSERT_HEAD(head, inp, sctp_hash);
2086
0
  return (0);
2087
0
}
2088
2089
struct sctp_inpcb *
2090
sctp_pcb_findep(struct sockaddr *nam, int find_tcp_pool, int have_lock,
2091
    uint32_t vrf_id)
2092
344k
{
2093
  /*
2094
   * First we check the hash table to see if someone has this port
2095
   * bound with just the port.
2096
   */
2097
344k
  struct sctp_inpcb *inp;
2098
344k
  struct sctppcbhead *head;
2099
344k
  int lport;
2100
344k
  unsigned int i;
2101
344k
#ifdef INET
2102
344k
  struct sockaddr_in *sin;
2103
344k
#endif
2104
344k
#ifdef INET6
2105
344k
  struct sockaddr_in6 *sin6;
2106
344k
#endif
2107
344k
#if defined(__Userspace__)
2108
344k
  struct sockaddr_conn *sconn;
2109
344k
#endif
2110
2111
344k
  switch (nam->sa_family) {
2112
0
#ifdef INET
2113
0
  case AF_INET:
2114
0
    sin = (struct sockaddr_in *)nam;
2115
0
    lport = sin->sin_port;
2116
0
    break;
2117
0
#endif
2118
0
#ifdef INET6
2119
0
  case AF_INET6:
2120
0
    sin6 = (struct sockaddr_in6 *)nam;
2121
0
    lport = sin6->sin6_port;
2122
0
    break;
2123
0
#endif
2124
0
#if defined(__Userspace__)
2125
344k
  case AF_CONN:
2126
344k
    sconn = (struct sockaddr_conn *)nam;
2127
344k
    lport = sconn->sconn_port;
2128
344k
    break;
2129
0
#endif
2130
0
  default:
2131
0
    return (NULL);
2132
344k
  }
2133
  /*
2134
   * I could cheat here and just cast to one of the types but we will
2135
   * do it right. It also provides the check against an Unsupported
2136
   * type too.
2137
   */
2138
  /* Find the head of the ALLADDR chain */
2139
344k
  if (have_lock == 0) {
2140
0
    SCTP_INP_INFO_RLOCK();
2141
0
  }
2142
344k
  head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport,
2143
344k
      SCTP_BASE_INFO(hashmark))];
2144
344k
  inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2145
2146
  /*
2147
   * If the TCP model exists it could be that the main listening
2148
   * endpoint is gone but there still exists a connected socket for this
2149
   * guy. If so we can return the first one that we find. This may NOT
2150
   * be the correct one so the caller should be wary on the returned INP.
2151
   * Currently the only caller that sets find_tcp_pool is in bindx where
2152
   * we are verifying that a user CAN bind the address. He either
2153
   * has bound it already, or someone else has, or its open to bind,
2154
   * so this is good enough.
2155
   */
2156
344k
  if (inp == NULL && find_tcp_pool) {
2157
0
    for (i = 0; i < SCTP_BASE_INFO(hashtcpmark) + 1; i++) {
2158
0
      head = &SCTP_BASE_INFO(sctp_tcpephash)[i];
2159
0
      inp = sctp_endpoint_probe(nam, head, lport, vrf_id);
2160
0
      if (inp) {
2161
0
        break;
2162
0
      }
2163
0
    }
2164
0
  }
2165
344k
  if (inp) {
2166
331k
    SCTP_INP_INCR_REF(inp);
2167
331k
  }
2168
344k
  if (have_lock == 0) {
2169
0
    SCTP_INP_INFO_RUNLOCK();
2170
0
  }
2171
344k
  return (inp);
2172
344k
}
2173
2174
/*
2175
 * Find an association for an endpoint with the pointer to whom you want to
2176
 * send to and the endpoint pointer. The address can be IPv4 or IPv6. We may
2177
 * need to change the *to to some other struct like a mbuf...
2178
 */
2179
struct sctp_tcb *
2180
sctp_findassociation_addr_sa(struct sockaddr *from, struct sockaddr *to,
2181
    struct sctp_inpcb **inp_p, struct sctp_nets **netp, int find_tcp_pool,
2182
    uint32_t vrf_id)
2183
331k
{
2184
331k
  struct sctp_inpcb *inp = NULL;
2185
331k
  struct sctp_tcb *stcb;
2186
2187
331k
  SCTP_INP_INFO_RLOCK();
2188
331k
  if (find_tcp_pool) {
2189
331k
    if (inp_p != NULL) {
2190
331k
      stcb = sctp_tcb_special_locate(inp_p, from, to, netp,
2191
331k
                                     vrf_id);
2192
331k
    } else {
2193
0
      stcb = sctp_tcb_special_locate(&inp, from, to, netp,
2194
0
                                     vrf_id);
2195
0
    }
2196
331k
    if (stcb != NULL) {
2197
0
      SCTP_INP_INFO_RUNLOCK();
2198
0
      return (stcb);
2199
0
    }
2200
331k
  }
2201
331k
  inp = sctp_pcb_findep(to, 0, 1, vrf_id);
2202
331k
  if (inp_p != NULL) {
2203
331k
    *inp_p = inp;
2204
331k
  }
2205
331k
  SCTP_INP_INFO_RUNLOCK();
2206
331k
  if (inp == NULL) {
2207
136
    return (NULL);
2208
136
  }
2209
  /*
2210
   * ok, we have an endpoint, now lets find the assoc for it (if any)
2211
   * we now place the source address or from in the to of the find
2212
   * endpoint call. Since in reality this chain is used from the
2213
   * inbound packet side.
2214
   */
2215
331k
  if (inp_p != NULL) {
2216
331k
    stcb = sctp_findassociation_ep_addr(inp_p, from, netp, to,
2217
331k
                                        NULL);
2218
331k
  } else {
2219
0
    stcb = sctp_findassociation_ep_addr(&inp, from, netp, to,
2220
0
                                        NULL);
2221
0
  }
2222
331k
  return (stcb);
2223
331k
}
2224
2225
/*
2226
 * This routine will grub through the mbuf that is a INIT or INIT-ACK and
2227
 * find all addresses that the sender has specified in any address list. Each
2228
 * address will be used to lookup the TCB and see if one exits.
2229
 */
2230
static struct sctp_tcb *
2231
sctp_findassociation_special_addr(struct mbuf *m, int offset,
2232
    struct sctphdr *sh, struct sctp_inpcb **inp_p, struct sctp_nets **netp,
2233
    struct sockaddr *dst)
2234
1.51k
{
2235
1.51k
  struct sctp_paramhdr *phdr, param_buf;
2236
1.51k
#if defined(INET) || defined(INET6)
2237
1.51k
  struct sctp_tcb *stcb;
2238
1.51k
  uint16_t ptype;
2239
1.51k
#endif
2240
1.51k
  uint16_t plen;
2241
1.51k
#ifdef INET
2242
1.51k
  struct sockaddr_in sin4;
2243
1.51k
#endif
2244
1.51k
#ifdef INET6
2245
1.51k
  struct sockaddr_in6 sin6;
2246
1.51k
#endif
2247
2248
1.51k
#ifdef INET
2249
1.51k
  memset(&sin4, 0, sizeof(sin4));
2250
#ifdef HAVE_SIN_LEN
2251
  sin4.sin_len = sizeof(sin4);
2252
#endif
2253
1.51k
  sin4.sin_family = AF_INET;
2254
1.51k
  sin4.sin_port = sh->src_port;
2255
1.51k
#endif
2256
1.51k
#ifdef INET6
2257
1.51k
  memset(&sin6, 0, sizeof(sin6));
2258
#ifdef HAVE_SIN6_LEN
2259
  sin6.sin6_len = sizeof(sin6);
2260
#endif
2261
1.51k
  sin6.sin6_family = AF_INET6;
2262
1.51k
  sin6.sin6_port = sh->src_port;
2263
1.51k
#endif
2264
2265
1.51k
  offset += sizeof(struct sctp_init_chunk);
2266
2267
1.51k
  phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
2268
189k
  while (phdr != NULL) {
2269
    /* now we must see if we want the parameter */
2270
188k
#if defined(INET) || defined(INET6)
2271
188k
    ptype = ntohs(phdr->param_type);
2272
188k
#endif
2273
188k
    plen = ntohs(phdr->param_length);
2274
188k
    if (plen == 0) {
2275
50
      break;
2276
50
    }
2277
188k
#ifdef INET
2278
188k
    if (ptype == SCTP_IPV4_ADDRESS &&
2279
188k
        plen == sizeof(struct sctp_ipv4addr_param)) {
2280
      /* Get the rest of the address */
2281
524
      struct sctp_ipv4addr_param ip4_param, *p4;
2282
2283
524
      phdr = sctp_get_next_param(m, offset,
2284
524
          (struct sctp_paramhdr *)&ip4_param, sizeof(ip4_param));
2285
524
      if (phdr == NULL) {
2286
2
        return (NULL);
2287
2
      }
2288
522
      p4 = (struct sctp_ipv4addr_param *)phdr;
2289
522
      memcpy(&sin4.sin_addr, &p4->addr, sizeof(p4->addr));
2290
      /* look it up */
2291
522
      stcb = sctp_findassociation_ep_addr(inp_p,
2292
522
          (struct sockaddr *)&sin4, netp, dst, NULL);
2293
522
      if (stcb != NULL) {
2294
0
        return (stcb);
2295
0
      }
2296
522
    }
2297
188k
#endif
2298
188k
#ifdef INET6
2299
188k
    if (ptype == SCTP_IPV6_ADDRESS &&
2300
188k
        plen == sizeof(struct sctp_ipv6addr_param)) {
2301
      /* Get the rest of the address */
2302
411
      struct sctp_ipv6addr_param ip6_param, *p6;
2303
2304
411
      phdr = sctp_get_next_param(m, offset,
2305
411
          (struct sctp_paramhdr *)&ip6_param, sizeof(ip6_param));
2306
411
      if (phdr == NULL) {
2307
1
        return (NULL);
2308
1
      }
2309
410
      p6 = (struct sctp_ipv6addr_param *)phdr;
2310
410
      memcpy(&sin6.sin6_addr, &p6->addr, sizeof(p6->addr));
2311
      /* look it up */
2312
410
      stcb = sctp_findassociation_ep_addr(inp_p,
2313
410
          (struct sockaddr *)&sin6, netp, dst, NULL);
2314
410
      if (stcb != NULL) {
2315
0
        return (stcb);
2316
0
      }
2317
410
    }
2318
188k
#endif
2319
188k
    offset += SCTP_SIZE32(plen);
2320
188k
    phdr = sctp_get_next_param(m, offset, &param_buf,
2321
188k
             sizeof(param_buf));
2322
188k
  }
2323
1.51k
  return (NULL);
2324
1.51k
}
2325
2326
static struct sctp_tcb *
2327
sctp_findassoc_by_vtag(struct sockaddr *from, struct sockaddr *to, uint32_t vtag,
2328
           struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint16_t rport,
2329
           uint16_t lport, int skip_src_check, uint32_t vrf_id, uint32_t remote_tag)
2330
360k
{
2331
  /*
2332
   * Use my vtag to hash. If we find it we then verify the source addr
2333
   * is in the assoc. If all goes well we save a bit on rec of a
2334
   * packet.
2335
   */
2336
360k
  struct sctpasochead *head;
2337
360k
  struct sctp_nets *net;
2338
360k
  struct sctp_tcb *stcb;
2339
#ifdef SCTP_MVRF
2340
  unsigned int i;
2341
#endif
2342
2343
360k
  SCTP_INP_INFO_RLOCK();
2344
360k
  head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(vtag,
2345
360k
                                                          SCTP_BASE_INFO(hashasocmark))];
2346
360k
  LIST_FOREACH(stcb, head, sctp_asocs) {
2347
29.5k
    SCTP_INP_RLOCK(stcb->sctp_ep);
2348
29.5k
    if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
2349
0
      SCTP_INP_RUNLOCK(stcb->sctp_ep);
2350
0
      continue;
2351
0
    }
2352
#ifdef SCTP_MVRF
2353
    for (i = 0; i < stcb->sctp_ep->num_vrfs; i++) {
2354
      if (stcb->sctp_ep->m_vrf_ids[i] == vrf_id) {
2355
        break;
2356
      }
2357
    }
2358
    if (i == stcb->sctp_ep->num_vrfs) {
2359
      SCTP_INP_RUNLOCK(inp);
2360
      continue;
2361
    }
2362
#else
2363
29.5k
    if (stcb->sctp_ep->def_vrf_id != vrf_id) {
2364
0
      SCTP_INP_RUNLOCK(stcb->sctp_ep);
2365
0
      continue;
2366
0
    }
2367
29.5k
#endif
2368
29.5k
    SCTP_TCB_LOCK(stcb);
2369
29.5k
    SCTP_INP_RUNLOCK(stcb->sctp_ep);
2370
29.5k
    if (stcb->asoc.my_vtag == vtag) {
2371
      /* candidate */
2372
29.5k
      if (stcb->rport != rport) {
2373
0
        SCTP_TCB_UNLOCK(stcb);
2374
0
        continue;
2375
0
      }
2376
29.5k
      if (stcb->sctp_ep->sctp_lport != lport) {
2377
0
        SCTP_TCB_UNLOCK(stcb);
2378
0
        continue;
2379
0
      }
2380
29.5k
      if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
2381
0
        SCTP_TCB_UNLOCK(stcb);
2382
0
        continue;
2383
0
      }
2384
      /* RRS:Need toaddr check here */
2385
29.5k
      if (sctp_does_stcb_own_this_addr(stcb, to) == 0) {
2386
              /* Endpoint does not own this address */
2387
0
        SCTP_TCB_UNLOCK(stcb);
2388
0
        continue;
2389
0
      }
2390
29.5k
      if (remote_tag) {
2391
        /* If we have both vtags that's all we match on */
2392
0
        if (stcb->asoc.peer_vtag == remote_tag) {
2393
          /* If both tags match we consider it conclusive
2394
           * and check NO source/destination addresses
2395
           */
2396
0
          goto conclusive;
2397
0
        }
2398
0
      }
2399
29.5k
      if (skip_src_check) {
2400
0
      conclusive:
2401
0
              if (from) {
2402
0
          *netp = sctp_findnet(stcb, from);
2403
0
        } else {
2404
0
          *netp = NULL; /* unknown */
2405
0
        }
2406
0
        if (inp_p)
2407
0
          *inp_p = stcb->sctp_ep;
2408
0
        SCTP_INP_INFO_RUNLOCK();
2409
0
        return (stcb);
2410
0
      }
2411
29.5k
      net = sctp_findnet(stcb, from);
2412
29.5k
      if (net) {
2413
        /* yep its him. */
2414
29.5k
        *netp = net;
2415
29.5k
        SCTP_STAT_INCR(sctps_vtagexpress);
2416
29.5k
        *inp_p = stcb->sctp_ep;
2417
29.5k
        SCTP_INP_INFO_RUNLOCK();
2418
29.5k
        return (stcb);
2419
29.5k
      } else {
2420
        /*
2421
         * not him, this should only happen in rare
2422
         * cases so I peg it.
2423
         */
2424
0
        SCTP_STAT_INCR(sctps_vtagbogus);
2425
0
      }
2426
29.5k
    }
2427
1
    SCTP_TCB_UNLOCK(stcb);
2428
1
  }
2429
330k
  SCTP_INP_INFO_RUNLOCK();
2430
330k
  return (NULL);
2431
330k
}
2432
2433
/*
2434
 * Find an association with the pointer to the inbound IP packet. This can be
2435
 * a IPv4 or IPv6 packet.
2436
 */
2437
struct sctp_tcb *
2438
sctp_findassociation_addr(struct mbuf *m, int offset,
2439
    struct sockaddr *src, struct sockaddr *dst,
2440
    struct sctphdr *sh, struct sctp_chunkhdr *ch,
2441
    struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2442
361k
{
2443
361k
  struct sctp_tcb *stcb;
2444
361k
  struct sctp_inpcb *inp;
2445
2446
361k
  if (sh->v_tag) {
2447
    /* we only go down this path if vtag is non-zero */
2448
359k
    stcb = sctp_findassoc_by_vtag(src, dst, ntohl(sh->v_tag),
2449
359k
                                  inp_p, netp, sh->src_port, sh->dest_port, 0, vrf_id, 0);
2450
359k
    if (stcb) {
2451
29.5k
      return (stcb);
2452
29.5k
    }
2453
359k
  }
2454
2455
331k
  if (inp_p) {
2456
331k
    stcb = sctp_findassociation_addr_sa(src, dst, inp_p, netp,
2457
331k
                                        1, vrf_id);
2458
331k
    inp = *inp_p;
2459
331k
  } else {
2460
0
    stcb = sctp_findassociation_addr_sa(src, dst, &inp, netp,
2461
0
                                        1, vrf_id);
2462
0
  }
2463
331k
  SCTPDBG(SCTP_DEBUG_PCB1, "stcb:%p inp:%p\n", (void *)stcb, (void *)inp);
2464
331k
  if (stcb == NULL && inp) {
2465
    /* Found a EP but not this address */
2466
2.99k
    if ((ch->chunk_type == SCTP_INITIATION) ||
2467
2.99k
        (ch->chunk_type == SCTP_INITIATION_ACK)) {
2468
      /*-
2469
       * special hook, we do NOT return linp or an
2470
       * association that is linked to an existing
2471
       * association that is under the TCP pool (i.e. no
2472
       * listener exists). The endpoint finding routine
2473
       * will always find a listener before examining the
2474
       * TCP pool.
2475
       */
2476
1.51k
      if (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) {
2477
0
        if (inp_p) {
2478
0
          *inp_p = NULL;
2479
0
        }
2480
0
        return (NULL);
2481
0
      }
2482
1.51k
      stcb = sctp_findassociation_special_addr(m,
2483
1.51k
          offset, sh, &inp, netp, dst);
2484
1.51k
      if (inp_p != NULL) {
2485
1.51k
        *inp_p = inp;
2486
1.51k
      }
2487
1.51k
    }
2488
2.99k
  }
2489
331k
  SCTPDBG(SCTP_DEBUG_PCB1, "stcb is %p\n", (void *)stcb);
2490
331k
  return (stcb);
2491
331k
}
2492
2493
/*
2494
 * lookup an association by an ASCONF lookup address.
2495
 * if the lookup address is 0.0.0.0 or ::0, use the vtag to do the lookup
2496
 */
2497
struct sctp_tcb *
2498
sctp_findassociation_ep_asconf(struct mbuf *m, int offset,
2499
             struct sockaddr *dst, struct sctphdr *sh,
2500
                               struct sctp_inpcb **inp_p, struct sctp_nets **netp, uint32_t vrf_id)
2501
7.68k
{
2502
7.68k
  struct sctp_tcb *stcb;
2503
7.68k
  union sctp_sockstore remote_store;
2504
7.68k
  struct sctp_paramhdr param_buf, *phdr;
2505
7.68k
  int ptype;
2506
7.68k
  int zero_address = 0;
2507
7.68k
#ifdef INET
2508
7.68k
  struct sockaddr_in *sin;
2509
7.68k
#endif
2510
7.68k
#ifdef INET6
2511
7.68k
  struct sockaddr_in6 *sin6;
2512
7.68k
#endif
2513
2514
7.68k
  memset(&remote_store, 0, sizeof(remote_store));
2515
7.68k
  phdr = sctp_get_next_param(m, offset + sizeof(struct sctp_asconf_chunk),
2516
7.68k
           &param_buf, sizeof(struct sctp_paramhdr));
2517
7.68k
  if (phdr == NULL) {
2518
46
    SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf lookup addr\n",
2519
46
      __func__);
2520
46
    return NULL;
2521
46
  }
2522
7.64k
  ptype = (int)((uint32_t) ntohs(phdr->param_type));
2523
  /* get the correlation address */
2524
7.64k
  switch (ptype) {
2525
0
#ifdef INET6
2526
2.02k
  case SCTP_IPV6_ADDRESS:
2527
2.02k
  {
2528
    /* ipv6 address param */
2529
2.02k
    struct sctp_ipv6addr_param *p6, p6_buf;
2530
2531
2.02k
    if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv6addr_param)) {
2532
241
      return NULL;
2533
241
    }
2534
1.78k
    p6 = (struct sctp_ipv6addr_param *)sctp_get_next_param(m,
2535
1.78k
                       offset + sizeof(struct sctp_asconf_chunk),
2536
1.78k
                       &p6_buf.ph, sizeof(p6_buf));
2537
1.78k
    if (p6 == NULL) {
2538
15
      SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v6 lookup addr\n",
2539
15
        __func__);
2540
15
      return (NULL);
2541
15
    }
2542
1.76k
    sin6 = &remote_store.sin6;
2543
1.76k
    sin6->sin6_family = AF_INET6;
2544
#ifdef HAVE_SIN6_LEN
2545
    sin6->sin6_len = sizeof(*sin6);
2546
#endif
2547
1.76k
    sin6->sin6_port = sh->src_port;
2548
1.76k
    memcpy(&sin6->sin6_addr, &p6->addr, sizeof(struct in6_addr));
2549
1.76k
    if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
2550
197
      zero_address = 1;
2551
1.76k
    break;
2552
1.78k
  }
2553
0
#endif
2554
0
#ifdef INET
2555
4.77k
  case SCTP_IPV4_ADDRESS:
2556
4.77k
  {
2557
    /* ipv4 address param */
2558
4.77k
    struct sctp_ipv4addr_param *p4, p4_buf;
2559
2560
4.77k
    if (ntohs(phdr->param_length) != sizeof(struct sctp_ipv4addr_param)) {
2561
3.61k
      return NULL;
2562
3.61k
    }
2563
1.15k
    p4 = (struct sctp_ipv4addr_param *)sctp_get_next_param(m,
2564
1.15k
                       offset + sizeof(struct sctp_asconf_chunk),
2565
1.15k
                       &p4_buf.ph, sizeof(p4_buf));
2566
1.15k
    if (p4 == NULL) {
2567
2
      SCTPDBG(SCTP_DEBUG_INPUT3, "%s: failed to get asconf v4 lookup addr\n",
2568
2
        __func__);
2569
2
      return (NULL);
2570
2
    }
2571
1.15k
    sin = &remote_store.sin;
2572
1.15k
    sin->sin_family = AF_INET;
2573
#ifdef HAVE_SIN_LEN
2574
    sin->sin_len = sizeof(*sin);
2575
#endif
2576
1.15k
    sin->sin_port = sh->src_port;
2577
1.15k
    memcpy(&sin->sin_addr, &p4->addr, sizeof(struct in_addr));
2578
1.15k
    if (sin->sin_addr.s_addr == INADDR_ANY)
2579
374
      zero_address = 1;
2580
1.15k
    break;
2581
1.15k
  }
2582
0
#endif
2583
848
  default:
2584
    /* invalid address param type */
2585
848
    return NULL;
2586
7.64k
  }
2587
2588
2.92k
  if (zero_address) {
2589
571
          stcb = sctp_findassoc_by_vtag(NULL, dst, ntohl(sh->v_tag), inp_p,
2590
571
                netp, sh->src_port, sh->dest_port, 1, vrf_id, 0);
2591
571
    if (stcb != NULL) {
2592
0
      SCTP_INP_DECR_REF(*inp_p);
2593
0
    }
2594
2.35k
  } else {
2595
2.35k
    stcb = sctp_findassociation_ep_addr(inp_p,
2596
2.35k
        &remote_store.sa, netp,
2597
2.35k
        dst, NULL);
2598
2.35k
  }
2599
2.92k
  return (stcb);
2600
7.64k
}
2601
2602
/*
2603
 * allocate a sctp_inpcb and setup a temporary binding to a port/all
2604
 * addresses. This way if we don't get a bind we by default pick a ephemeral
2605
 * port with all addresses bound.
2606
 */
2607
int
2608
sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
2609
12.8k
{
2610
  /*
2611
   * we get called when a new endpoint starts up. We need to allocate
2612
   * the sctp_inpcb structure from the zone and init it. Mark it as
2613
   * unbound and find a port that we can use as an ephemeral with
2614
   * INADDR_ANY. If the user binds later no problem we can then add in
2615
   * the specific addresses. And setup the default parameters for the
2616
   * EP.
2617
   */
2618
12.8k
  int i, error;
2619
12.8k
  struct sctp_inpcb *inp;
2620
12.8k
  struct sctp_pcb *m;
2621
12.8k
  struct timeval time;
2622
12.8k
  sctp_sharedkey_t *null_key;
2623
2624
12.8k
  error = 0;
2625
2626
12.8k
  SCTP_INP_INFO_WLOCK();
2627
12.8k
  inp = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_ep), struct sctp_inpcb);
2628
12.8k
  if (inp == NULL) {
2629
0
    SCTP_PRINTF("Out of SCTP-INPCB structures - no resources\n");
2630
0
    SCTP_INP_INFO_WUNLOCK();
2631
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2632
0
    return (ENOBUFS);
2633
0
  }
2634
  /* zap it */
2635
12.8k
  memset(inp, 0, sizeof(*inp));
2636
2637
  /* bump generations */
2638
#if defined(__APPLE__) && !defined(__Userspace__)
2639
  inp->ip_inp.inp.inp_state = INPCB_STATE_INUSE;
2640
#endif
2641
  /* setup socket pointers */
2642
12.8k
  inp->sctp_socket = so;
2643
12.8k
  inp->ip_inp.inp.inp_socket = so;
2644
#if defined(__FreeBSD__) && !defined(__Userspace__)
2645
  inp->ip_inp.inp.inp_cred = crhold(so->so_cred);
2646
#endif
2647
12.8k
#ifdef INET6
2648
#if !defined(__Userspace__) && !defined(_WIN32)
2649
  if (INP_SOCKAF(so) == AF_INET6) {
2650
    if (MODULE_GLOBAL(ip6_auto_flowlabel)) {
2651
      inp->ip_inp.inp.inp_flags |= IN6P_AUTOFLOWLABEL;
2652
    }
2653
    if (MODULE_GLOBAL(ip6_v6only)) {
2654
      inp->ip_inp.inp.inp_flags |= IN6P_IPV6_V6ONLY;
2655
    }
2656
  }
2657
#endif
2658
12.8k
#endif
2659
12.8k
  inp->sctp_associd_counter = 1;
2660
12.8k
  inp->partial_delivery_point = SCTP_SB_LIMIT_RCV(so) >> SCTP_PARTIAL_DELIVERY_SHIFT;
2661
12.8k
  inp->sctp_frag_point = 0;
2662
12.8k
  inp->max_cwnd = 0;
2663
12.8k
  inp->sctp_cmt_on_off = SCTP_BASE_SYSCTL(sctp_cmt_on_off);
2664
12.8k
  inp->ecn_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_ecn_enable);
2665
12.8k
  inp->prsctp_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pr_enable);
2666
12.8k
  inp->auth_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_auth_enable);
2667
12.8k
  inp->asconf_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_asconf_enable);
2668
12.8k
  inp->reconfig_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_reconfig_enable);
2669
12.8k
  inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable);
2670
12.8k
  inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable);
2671
12.8k
  inp->idata_supported = 0;
2672
12.8k
  inp->rcv_edmid = SCTP_EDMID_NONE;
2673
2674
#if defined(__FreeBSD__) && !defined(__Userspace__)
2675
  inp->fibnum = so->so_fibnum;
2676
#else
2677
12.8k
  inp->fibnum = 0;
2678
12.8k
#endif
2679
12.8k
#if defined(__Userspace__)
2680
12.8k
  inp->ulp_info = NULL;
2681
12.8k
  inp->recv_callback = NULL;
2682
12.8k
  inp->send_callback = NULL;
2683
12.8k
  inp->send_sb_threshold = 0;
2684
12.8k
#endif
2685
  /* init the small hash table we use to track asocid <-> tcb */
2686
12.8k
  inp->sctp_asocidhash = SCTP_HASH_INIT(SCTP_STACK_VTAG_HASH_SIZE, &inp->hashasocidmark);
2687
12.8k
  if (inp->sctp_asocidhash == NULL) {
2688
#if defined(__FreeBSD__) && !defined(__Userspace__)
2689
    crfree(inp->ip_inp.inp.inp_cred);
2690
#endif
2691
0
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2692
0
    SCTP_INP_INFO_WUNLOCK();
2693
0
    return (ENOBUFS);
2694
0
  }
2695
12.8k
  SCTP_INCR_EP_COUNT();
2696
12.8k
  inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
2697
12.8k
  SCTP_INP_INFO_WUNLOCK();
2698
2699
12.8k
  so->so_pcb = (caddr_t)inp;
2700
2701
12.8k
  if (SCTP_SO_TYPE(so) == SOCK_SEQPACKET) {
2702
    /* UDP style socket */
2703
0
    inp->sctp_flags = (SCTP_PCB_FLAGS_UDPTYPE |
2704
0
        SCTP_PCB_FLAGS_UNBOUND);
2705
    /* Be sure it is NON-BLOCKING IO for UDP */
2706
    /* SCTP_SET_SO_NBIO(so); */
2707
12.8k
  } else if (SCTP_SO_TYPE(so) == SOCK_STREAM) {
2708
    /* TCP style socket */
2709
12.8k
    inp->sctp_flags = (SCTP_PCB_FLAGS_TCPTYPE |
2710
12.8k
        SCTP_PCB_FLAGS_UNBOUND);
2711
    /* Be sure we have blocking IO by default */
2712
12.8k
    SOCK_LOCK(so);
2713
12.8k
    SCTP_CLEAR_SO_NBIO(so);
2714
12.8k
    SOCK_UNLOCK(so);
2715
12.8k
  } else {
2716
    /*
2717
     * unsupported socket type (RAW, etc)- in case we missed it
2718
     * in protosw
2719
     */
2720
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EOPNOTSUPP);
2721
0
    so->so_pcb = NULL;
2722
#if defined(__FreeBSD__) && !defined(__Userspace__)
2723
    crfree(inp->ip_inp.inp.inp_cred);
2724
#endif
2725
0
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2726
0
    return (EOPNOTSUPP);
2727
0
  }
2728
12.8k
  if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_1) {
2729
12.8k
    sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2730
12.8k
    sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2731
12.8k
  } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_2) {
2732
0
    sctp_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2733
0
    sctp_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2734
0
  } else if (SCTP_BASE_SYSCTL(sctp_default_frag_interleave) == SCTP_FRAG_LEVEL_0) {
2735
0
    sctp_feature_off(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE);
2736
0
    sctp_feature_off(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS);
2737
0
  }
2738
12.8k
  inp->sctp_tcbhash = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_pcbtblsize),
2739
12.8k
             &inp->sctp_hashmark);
2740
12.8k
  if (inp->sctp_tcbhash == NULL) {
2741
0
    SCTP_PRINTF("Out of SCTP-INPCB->hashinit - no resources\n");
2742
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2743
0
    so->so_pcb = NULL;
2744
#if defined(__FreeBSD__) && !defined(__Userspace__)
2745
    crfree(inp->ip_inp.inp.inp_cred);
2746
#endif
2747
0
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2748
0
    return (ENOBUFS);
2749
0
  }
2750
#ifdef SCTP_MVRF
2751
  inp->vrf_size = SCTP_DEFAULT_VRF_SIZE;
2752
  SCTP_MALLOC(inp->m_vrf_ids, uint32_t *,
2753
        (sizeof(uint32_t) * inp->vrf_size), SCTP_M_MVRF);
2754
  if (inp->m_vrf_ids == NULL) {
2755
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
2756
    so->so_pcb = NULL;
2757
    SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2758
#if defined(__FreeBSD__) && !defined(__Userspace__)
2759
    crfree(inp->ip_inp.inp.inp_cred);
2760
#endif
2761
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2762
    return (ENOBUFS);
2763
  }
2764
  inp->m_vrf_ids[0] = vrf_id;
2765
  inp->num_vrfs = 1;
2766
#endif
2767
12.8k
  inp->def_vrf_id = vrf_id;
2768
2769
#if defined(__APPLE__) && !defined(__Userspace__)
2770
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
2771
  inp->ip_inp.inp.inpcb_mtx = lck_mtx_alloc_init(SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr);
2772
  if (inp->ip_inp.inp.inpcb_mtx == NULL) {
2773
    SCTP_PRINTF("in_pcballoc: can't alloc mutex! so=%p\n", (void *)so);
2774
#ifdef SCTP_MVRF
2775
    SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
2776
#endif
2777
    SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
2778
    so->so_pcb = NULL;
2779
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
2780
    SCTP_UNLOCK_EXC(SCTP_BASE_INFO(sctbinfo).ipi_lock);
2781
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
2782
    return (ENOMEM);
2783
  }
2784
#elif defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2785
  lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).mtx_grp, SCTP_BASE_INFO(sctbinfo).mtx_attr);
2786
#else
2787
  lck_mtx_init(&inp->ip_inp.inp.inpcb_mtx, SCTP_BASE_INFO(sctbinfo).ipi_lock_grp, SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
2788
#endif
2789
#endif
2790
12.8k
  SCTP_INP_INFO_WLOCK();
2791
12.8k
  SCTP_INP_LOCK_INIT(inp);
2792
#if defined(__FreeBSD__) && !defined(__Userspace__)
2793
  rw_init_flags(&inp->ip_inp.inp.inp_lock, "sctpinp",
2794
      RW_RECURSE | RW_DUPOK);
2795
#endif
2796
12.8k
  SCTP_INP_READ_LOCK_INIT(inp);
2797
12.8k
  SCTP_ASOC_CREATE_LOCK_INIT(inp);
2798
  /* lock the new ep */
2799
12.8k
  SCTP_INP_WLOCK(inp);
2800
2801
  /* add it to the info area */
2802
12.8k
  LIST_INSERT_HEAD(&SCTP_BASE_INFO(listhead), inp, sctp_list);
2803
#if defined(__APPLE__) && !defined(__Userspace__)
2804
  inp->ip_inp.inp.inp_pcbinfo = &SCTP_BASE_INFO(sctbinfo);
2805
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
2806
  LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).listhead, &inp->ip_inp.inp, inp_list);
2807
#else
2808
  LIST_INSERT_HEAD(SCTP_BASE_INFO(sctbinfo).ipi_listhead, &inp->ip_inp.inp, inp_list);
2809
#endif
2810
#endif
2811
12.8k
  SCTP_INP_INFO_WUNLOCK();
2812
2813
12.8k
  TAILQ_INIT(&inp->read_queue);
2814
12.8k
  LIST_INIT(&inp->sctp_addr_list);
2815
2816
12.8k
  LIST_INIT(&inp->sctp_asoc_list);
2817
2818
#ifdef SCTP_TRACK_FREED_ASOCS
2819
  /* TEMP CODE */
2820
  LIST_INIT(&inp->sctp_asoc_free_list);
2821
#endif
2822
  /* Init the timer structure for signature change */
2823
12.8k
  SCTP_OS_TIMER_INIT(&inp->sctp_ep.signature_change.timer);
2824
12.8k
  inp->sctp_ep.signature_change.type = SCTP_TIMER_TYPE_NEWCOOKIE;
2825
2826
  /* now init the actual endpoint default data */
2827
12.8k
  m = &inp->sctp_ep;
2828
2829
  /* setup the base timeout information */
2830
12.8k
  m->sctp_timeoutticks[SCTP_TIMER_SEND] = sctp_secs_to_ticks(SCTP_SEND_SEC);  /* needed ? */
2831
12.8k
  m->sctp_timeoutticks[SCTP_TIMER_INIT] = sctp_secs_to_ticks(SCTP_INIT_SEC);  /* needed ? */
2832
12.8k
  m->sctp_timeoutticks[SCTP_TIMER_RECV] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_delayed_sack_time_default));
2833
12.8k
  m->sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_heartbeat_interval_default));
2834
12.8k
  m->sctp_timeoutticks[SCTP_TIMER_PMTU] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_pmtu_raise_time_default));
2835
12.8k
  m->sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_shutdown_guard_time_default));
2836
12.8k
  m->sctp_timeoutticks[SCTP_TIMER_SIGNATURE] = sctp_secs_to_ticks(SCTP_BASE_SYSCTL(sctp_secret_lifetime_default));
2837
  /* all max/min max are in ms */
2838
12.8k
  m->sctp_maxrto = SCTP_BASE_SYSCTL(sctp_rto_max_default);
2839
12.8k
  m->sctp_minrto = SCTP_BASE_SYSCTL(sctp_rto_min_default);
2840
12.8k
  m->initial_rto = SCTP_BASE_SYSCTL(sctp_rto_initial_default);
2841
12.8k
  m->initial_init_rto_max = SCTP_BASE_SYSCTL(sctp_init_rto_max_default);
2842
12.8k
  m->sctp_sack_freq = SCTP_BASE_SYSCTL(sctp_sack_freq_default);
2843
12.8k
  m->max_init_times = SCTP_BASE_SYSCTL(sctp_init_rtx_max_default);
2844
12.8k
  m->max_send_times = SCTP_BASE_SYSCTL(sctp_assoc_rtx_max_default);
2845
12.8k
  m->def_net_failure = SCTP_BASE_SYSCTL(sctp_path_rtx_max_default);
2846
12.8k
  m->def_net_pf_threshold = SCTP_BASE_SYSCTL(sctp_path_pf_threshold);
2847
12.8k
  m->sctp_sws_sender = SCTP_SWS_SENDER_DEF;
2848
12.8k
  m->sctp_sws_receiver = SCTP_SWS_RECEIVER_DEF;
2849
12.8k
  m->max_burst = SCTP_BASE_SYSCTL(sctp_max_burst_default);
2850
12.8k
  m->fr_max_burst = SCTP_BASE_SYSCTL(sctp_fr_max_burst_default);
2851
2852
12.8k
  m->sctp_default_cc_module = SCTP_BASE_SYSCTL(sctp_default_cc_module);
2853
12.8k
  m->sctp_default_ss_module = SCTP_BASE_SYSCTL(sctp_default_ss_module);
2854
12.8k
  m->max_open_streams_intome = SCTP_BASE_SYSCTL(sctp_nr_incoming_streams_default);
2855
  /* number of streams to pre-open on a association */
2856
12.8k
  m->pre_open_stream_count = SCTP_BASE_SYSCTL(sctp_nr_outgoing_streams_default);
2857
2858
12.8k
  m->default_mtu = 0;
2859
  /* Add adaptation cookie */
2860
12.8k
  m->adaptation_layer_indicator = 0;
2861
12.8k
  m->adaptation_layer_indicator_provided = 0;
2862
2863
  /* seed random number generator */
2864
12.8k
  m->random_counter = 1;
2865
12.8k
  m->store_at = SCTP_SIGNATURE_SIZE;
2866
12.8k
  SCTP_READ_RANDOM(m->random_numbers, sizeof(m->random_numbers));
2867
12.8k
  sctp_fill_random_store(m);
2868
2869
  /* Minimum cookie size */
2870
12.8k
  m->size_of_a_cookie = (sizeof(struct sctp_init_msg) * 2) +
2871
12.8k
      sizeof(struct sctp_state_cookie);
2872
12.8k
  m->size_of_a_cookie += SCTP_SIGNATURE_SIZE;
2873
2874
  /* Setup the initial secret */
2875
12.8k
  (void)SCTP_GETTIME_TIMEVAL(&time);
2876
12.8k
  m->time_of_secret_change = time.tv_sec;
2877
2878
115k
  for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2879
102k
    m->secret_key[0][i] = sctp_select_initial_TSN(m);
2880
102k
  }
2881
12.8k
  sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2882
2883
  /* How long is a cookie good for ? */
2884
12.8k
  m->def_cookie_life = sctp_msecs_to_ticks(SCTP_BASE_SYSCTL(sctp_valid_cookie_life_default));
2885
  /*
2886
   * Initialize authentication parameters
2887
   */
2888
12.8k
  m->local_hmacs = sctp_default_supported_hmaclist();
2889
12.8k
  m->local_auth_chunks = sctp_alloc_chunklist();
2890
12.8k
  if (inp->asconf_supported) {
2891
12.8k
    sctp_auth_add_chunk(SCTP_ASCONF, m->local_auth_chunks);
2892
12.8k
    sctp_auth_add_chunk(SCTP_ASCONF_ACK, m->local_auth_chunks);
2893
12.8k
  }
2894
12.8k
  m->default_dscp = 0;
2895
12.8k
#ifdef INET6
2896
12.8k
  m->default_flowlabel = 0;
2897
12.8k
#endif
2898
12.8k
  m->port = 0; /* encapsulation disabled by default */
2899
12.8k
  LIST_INIT(&m->shared_keys);
2900
  /* add default NULL key as key id 0 */
2901
12.8k
  null_key = sctp_alloc_sharedkey();
2902
12.8k
  sctp_insert_sharedkey(&m->shared_keys, null_key);
2903
12.8k
  SCTP_INP_WUNLOCK(inp);
2904
#ifdef SCTP_LOG_CLOSING
2905
  sctp_log_closing(inp, NULL, 12);
2906
#endif
2907
12.8k
  return (error);
2908
12.8k
}
2909
2910
void
2911
sctp_move_pcb_and_assoc(struct sctp_inpcb *old_inp, struct sctp_inpcb *new_inp,
2912
    struct sctp_tcb *stcb)
2913
0
{
2914
0
  struct sctp_nets *net;
2915
0
  uint16_t lport, rport;
2916
0
  struct sctppcbhead *head;
2917
0
  struct sctp_laddr *laddr, *oladdr;
2918
2919
0
  atomic_add_int(&stcb->asoc.refcnt, 1);
2920
0
  SCTP_TCB_UNLOCK(stcb);
2921
0
  SCTP_INP_INFO_WLOCK();
2922
0
  SCTP_INP_WLOCK(old_inp);
2923
0
  SCTP_INP_WLOCK(new_inp);
2924
0
  SCTP_TCB_LOCK(stcb);
2925
0
  atomic_subtract_int(&stcb->asoc.refcnt, 1);
2926
2927
#if defined(__FreeBSD__) && !defined(__Userspace__)
2928
#ifdef INET6
2929
  if (old_inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2930
    new_inp->ip_inp.inp.inp_flags |= old_inp->ip_inp.inp.inp_flags & INP_CONTROLOPTS;
2931
    if (old_inp->ip_inp.inp.in6p_outputopts) {
2932
      new_inp->ip_inp.inp.in6p_outputopts = ip6_copypktopts(old_inp->ip_inp.inp.in6p_outputopts, M_NOWAIT);
2933
    }
2934
  }
2935
#endif
2936
#if defined(INET) && defined(INET6)
2937
  else
2938
#endif
2939
#ifdef INET
2940
  {
2941
    new_inp->ip_inp.inp.inp_ip_tos = old_inp->ip_inp.inp.inp_ip_tos;
2942
    new_inp->ip_inp.inp.inp_ip_ttl = old_inp->ip_inp.inp.inp_ip_ttl;
2943
  }
2944
#endif
2945
#endif
2946
0
  new_inp->sctp_ep.time_of_secret_change =
2947
0
      old_inp->sctp_ep.time_of_secret_change;
2948
0
  memcpy(new_inp->sctp_ep.secret_key, old_inp->sctp_ep.secret_key,
2949
0
      sizeof(old_inp->sctp_ep.secret_key));
2950
0
  new_inp->sctp_ep.current_secret_number =
2951
0
      old_inp->sctp_ep.current_secret_number;
2952
0
  new_inp->sctp_ep.last_secret_number =
2953
0
      old_inp->sctp_ep.last_secret_number;
2954
0
  new_inp->sctp_ep.size_of_a_cookie = old_inp->sctp_ep.size_of_a_cookie;
2955
2956
  /* make it so new data pours into the new socket */
2957
0
  stcb->sctp_socket = new_inp->sctp_socket;
2958
0
  stcb->sctp_ep = new_inp;
2959
2960
  /* Copy the port across */
2961
0
  lport = new_inp->sctp_lport = old_inp->sctp_lport;
2962
0
  rport = stcb->rport;
2963
  /* Pull the tcb from the old association */
2964
0
  LIST_REMOVE(stcb, sctp_tcbhash);
2965
0
  LIST_REMOVE(stcb, sctp_tcblist);
2966
0
  if (stcb->asoc.in_asocid_hash) {
2967
0
    LIST_REMOVE(stcb, sctp_tcbasocidhash);
2968
0
  }
2969
  /* Now insert the new_inp into the TCP connected hash */
2970
0
  head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR((lport | rport), SCTP_BASE_INFO(hashtcpmark))];
2971
2972
0
  LIST_INSERT_HEAD(head, new_inp, sctp_hash);
2973
  /* Its safe to access */
2974
0
  new_inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
2975
2976
  /* Now move the tcb into the endpoint list */
2977
0
  LIST_INSERT_HEAD(&new_inp->sctp_asoc_list, stcb, sctp_tcblist);
2978
  /*
2979
   * Question, do we even need to worry about the ep-hash since we
2980
   * only have one connection? Probably not :> so lets get rid of it
2981
   * and not suck up any kernel memory in that.
2982
   */
2983
0
  if (stcb->asoc.in_asocid_hash) {
2984
0
    struct sctpasochead *lhd;
2985
0
    lhd = &new_inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(stcb->asoc.assoc_id,
2986
0
      new_inp->hashasocidmark)];
2987
0
    LIST_INSERT_HEAD(lhd, stcb, sctp_tcbasocidhash);
2988
0
  }
2989
  /* Ok. Let's restart timer. */
2990
0
  TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2991
0
    sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, new_inp,
2992
0
        stcb, net);
2993
0
  }
2994
2995
0
  SCTP_INP_INFO_WUNLOCK();
2996
0
  if (new_inp->sctp_tcbhash != NULL) {
2997
0
    SCTP_HASH_FREE(new_inp->sctp_tcbhash, new_inp->sctp_hashmark);
2998
0
    new_inp->sctp_tcbhash = NULL;
2999
0
  }
3000
0
  if ((new_inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) {
3001
    /* Subset bound, so copy in the laddr list from the old_inp */
3002
0
    LIST_FOREACH(oladdr, &old_inp->sctp_addr_list, sctp_nxt_addr) {
3003
0
      laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3004
0
      if (laddr == NULL) {
3005
        /*
3006
         * Gak, what can we do? This assoc is really
3007
         * HOSED. We probably should send an abort
3008
         * here.
3009
         */
3010
0
        SCTPDBG(SCTP_DEBUG_PCB1, "Association hosed in TCP model, out of laddr memory\n");
3011
0
        continue;
3012
0
      }
3013
0
      SCTP_INCR_LADDR_COUNT();
3014
0
      memset(laddr, 0, sizeof(*laddr));
3015
0
      (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3016
0
      laddr->ifa = oladdr->ifa;
3017
0
      atomic_add_int(&laddr->ifa->refcount, 1);
3018
0
      LIST_INSERT_HEAD(&new_inp->sctp_addr_list, laddr,
3019
0
          sctp_nxt_addr);
3020
0
      new_inp->laddr_count++;
3021
0
      if (oladdr == stcb->asoc.last_used_address) {
3022
0
        stcb->asoc.last_used_address = laddr;
3023
0
      }
3024
0
    }
3025
0
  }
3026
  /* Now any running timers need to be adjusted. */
3027
0
  if (stcb->asoc.dack_timer.ep == old_inp) {
3028
0
    SCTP_INP_DECR_REF(old_inp);
3029
0
    stcb->asoc.dack_timer.ep = new_inp;
3030
0
    SCTP_INP_INCR_REF(new_inp);
3031
0
  }
3032
0
  if (stcb->asoc.asconf_timer.ep == old_inp) {
3033
0
    SCTP_INP_DECR_REF(old_inp);
3034
0
    stcb->asoc.asconf_timer.ep = new_inp;
3035
0
    SCTP_INP_INCR_REF(new_inp);
3036
0
  }
3037
0
  if (stcb->asoc.strreset_timer.ep == old_inp) {
3038
0
    SCTP_INP_DECR_REF(old_inp);
3039
0
    stcb->asoc.strreset_timer.ep = new_inp;
3040
0
    SCTP_INP_INCR_REF(new_inp);
3041
0
  }
3042
0
  if (stcb->asoc.shut_guard_timer.ep == old_inp) {
3043
0
    SCTP_INP_DECR_REF(old_inp);
3044
0
    stcb->asoc.shut_guard_timer.ep = new_inp;
3045
0
    SCTP_INP_INCR_REF(new_inp);
3046
0
  }
3047
0
  if (stcb->asoc.autoclose_timer.ep == old_inp) {
3048
0
    SCTP_INP_DECR_REF(old_inp);
3049
0
    stcb->asoc.autoclose_timer.ep = new_inp;
3050
0
    SCTP_INP_INCR_REF(new_inp);
3051
0
  }
3052
0
  if (stcb->asoc.delete_prim_timer.ep == old_inp) {
3053
0
    SCTP_INP_DECR_REF(old_inp);
3054
0
    stcb->asoc.delete_prim_timer.ep = new_inp;
3055
0
    SCTP_INP_INCR_REF(new_inp);
3056
0
  }
3057
  /* now what about the nets? */
3058
0
  TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
3059
0
    if (net->pmtu_timer.ep == old_inp) {
3060
0
      SCTP_INP_DECR_REF(old_inp);
3061
0
      net->pmtu_timer.ep = new_inp;
3062
0
      SCTP_INP_INCR_REF(new_inp);
3063
0
    }
3064
0
    if (net->hb_timer.ep == old_inp) {
3065
0
      SCTP_INP_DECR_REF(old_inp);
3066
0
      net->hb_timer.ep = new_inp;
3067
0
      SCTP_INP_INCR_REF(new_inp);
3068
0
    }
3069
0
    if (net->rxt_timer.ep == old_inp) {
3070
0
      SCTP_INP_DECR_REF(old_inp);
3071
0
      net->rxt_timer.ep = new_inp;
3072
0
      SCTP_INP_INCR_REF(new_inp);
3073
0
    }
3074
0
  }
3075
0
  SCTP_INP_WUNLOCK(new_inp);
3076
0
  SCTP_INP_WUNLOCK(old_inp);
3077
0
}
3078
3079
/*
3080
 * insert an laddr entry with the given ifa for the desired list
3081
 */
3082
static int
3083
sctp_insert_laddr(struct sctpladdr *list, struct sctp_ifa *ifa, uint32_t act)
3084
12.8k
{
3085
12.8k
  struct sctp_laddr *laddr;
3086
3087
12.8k
  laddr = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
3088
12.8k
  if (laddr == NULL) {
3089
    /* out of memory? */
3090
0
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
3091
0
    return (EINVAL);
3092
0
  }
3093
12.8k
  SCTP_INCR_LADDR_COUNT();
3094
12.8k
  memset(laddr, 0, sizeof(*laddr));
3095
12.8k
  (void)SCTP_GETTIME_TIMEVAL(&laddr->start_time);
3096
12.8k
  laddr->ifa = ifa;
3097
12.8k
  laddr->action = act;
3098
12.8k
  atomic_add_int(&ifa->refcount, 1);
3099
  /* insert it */
3100
12.8k
  LIST_INSERT_HEAD(list, laddr, sctp_nxt_addr);
3101
3102
12.8k
  return (0);
3103
12.8k
}
3104
3105
/*
3106
 * Remove an laddr entry from the local address list (on an assoc)
3107
 */
3108
static void
3109
sctp_remove_laddr(struct sctp_laddr *laddr)
3110
12.8k
{
3111
3112
  /* remove from the list */
3113
12.8k
  LIST_REMOVE(laddr, sctp_nxt_addr);
3114
12.8k
  sctp_free_ifa(laddr->ifa);
3115
12.8k
  SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), laddr);
3116
12.8k
  SCTP_DECR_LADDR_COUNT();
3117
12.8k
}
3118
#if !(defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__))
3119
3120
/*
3121
 * Don't know why, but without this there is an unknown reference when
3122
 * compiling NetBSD... hmm
3123
 */
3124
extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *sin6);
3125
#endif
3126
3127
/*
3128
 * Bind the socket, with the PCB and global info locks held.  Note, if a
3129
 * socket address is specified, the PCB lock may be dropped and re-acquired.
3130
 *
3131
 * sctp_ifap is used to bypass normal local address validation checks.
3132
 */
3133
int
3134
#if defined(__FreeBSD__) && !defined(__Userspace__)
3135
sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
3136
                       struct sctp_ifa *sctp_ifap, struct thread *td)
3137
#elif defined(_WIN32) && !defined(__Userspace__)
3138
sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
3139
                       struct sctp_ifa *sctp_ifap, PKTHREAD p)
3140
#else
3141
sctp_inpcb_bind_locked(struct sctp_inpcb *inp, struct sockaddr *addr,
3142
                       struct sctp_ifa *sctp_ifap, struct proc *p)
3143
#endif
3144
12.8k
{
3145
  /* bind a ep to a socket address */
3146
12.8k
  struct sctppcbhead *head;
3147
12.8k
  struct sctp_inpcb *inp_tmp;
3148
#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
3149
  struct inpcb *ip_inp;
3150
#endif
3151
12.8k
  int port_reuse_active = 0;
3152
12.8k
  int bindall;
3153
#ifdef SCTP_MVRF
3154
  int i;
3155
#endif
3156
12.8k
  uint16_t lport;
3157
12.8k
  int error;
3158
12.8k
  uint32_t vrf_id;
3159
3160
#if defined(__FreeBSD__) && !defined(__Userspace__)
3161
  KASSERT(td != NULL, ("%s: null thread", __func__));
3162
3163
#endif
3164
12.8k
  error = 0;
3165
12.8k
  lport = 0;
3166
12.8k
  bindall = 1;
3167
#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
3168
  ip_inp = &inp->ip_inp.inp;
3169
#endif
3170
3171
12.8k
  SCTP_INP_INFO_WLOCK_ASSERT();
3172
12.8k
  SCTP_INP_WLOCK_ASSERT(inp);
3173
3174
#ifdef SCTP_DEBUG
3175
  if (addr) {
3176
    SCTPDBG(SCTP_DEBUG_PCB1, "Bind called port: %d\n",
3177
      ntohs(((struct sockaddr_in *)addr)->sin_port));
3178
    SCTPDBG(SCTP_DEBUG_PCB1, "Addr: ");
3179
    SCTPDBG_ADDR(SCTP_DEBUG_PCB1, addr);
3180
  }
3181
#endif
3182
12.8k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3183
0
    error = EINVAL;
3184
    /* already did a bind, subsequent binds NOT allowed ! */
3185
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3186
0
    goto out;
3187
0
  }
3188
12.8k
  if (addr != NULL) {
3189
12.8k
    switch (addr->sa_family) {
3190
0
#ifdef INET
3191
0
    case AF_INET:
3192
0
    {
3193
0
      struct sockaddr_in *sin;
3194
3195
      /* IPV6_V6ONLY socket? */
3196
0
      if (SCTP_IPV6_V6ONLY(inp)) {
3197
0
        error = EINVAL;
3198
0
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3199
0
        goto out;
3200
0
      }
3201
#ifdef HAVE_SA_LEN
3202
      if (addr->sa_len != sizeof(*sin)) {
3203
        error = EINVAL;
3204
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3205
        goto out;
3206
      }
3207
#endif
3208
3209
0
      sin = (struct sockaddr_in *)addr;
3210
0
      lport = sin->sin_port;
3211
#if defined(__FreeBSD__) && !defined(__Userspace__)
3212
      /*
3213
       * For LOOPBACK the prison_local_ip4() call will transmute the ip address
3214
       * to the proper value.
3215
       */
3216
      if ((error = prison_local_ip4(td->td_ucred, &sin->sin_addr)) != 0) {
3217
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3218
        goto out;
3219
      }
3220
#endif
3221
0
      if (sin->sin_addr.s_addr != INADDR_ANY) {
3222
0
        bindall = 0;
3223
0
      }
3224
0
      break;
3225
0
    }
3226
0
#endif
3227
0
#ifdef INET6
3228
0
    case AF_INET6:
3229
0
    {
3230
      /* Only for pure IPv6 Address. (No IPv4 Mapped!) */
3231
0
      struct sockaddr_in6 *sin6;
3232
3233
0
      sin6 = (struct sockaddr_in6 *)addr;
3234
#ifdef HAVE_SA_LEN
3235
      if (addr->sa_len != sizeof(*sin6)) {
3236
        error = EINVAL;
3237
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3238
        goto out;
3239
      }
3240
#endif
3241
0
      lport = sin6->sin6_port;
3242
#if defined(__FreeBSD__) && !defined(__Userspace__)
3243
      /*
3244
       * For LOOPBACK the prison_local_ip6() call will transmute the ipv6 address
3245
       * to the proper value.
3246
       */
3247
      if ((error = prison_local_ip6(td->td_ucred, &sin6->sin6_addr,
3248
          (SCTP_IPV6_V6ONLY(inp) != 0))) != 0) {
3249
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3250
        goto out;
3251
      }
3252
#endif
3253
0
      if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
3254
0
        bindall = 0;
3255
#ifdef SCTP_EMBEDDED_V6_SCOPE
3256
        /* KAME hack: embed scopeid */
3257
#if defined(SCTP_KAME)
3258
        if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
3259
          error = EINVAL;
3260
          SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3261
          goto out;
3262
        }
3263
#elif defined(__APPLE__) && !defined(__Userspace__)
3264
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
3265
        if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL) != 0) {
3266
#else
3267
        if (in6_embedscope(&sin6->sin6_addr, sin6, ip_inp, NULL, NULL) != 0) {
3268
#endif
3269
          error = EINVAL;
3270
          SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3271
          goto out;
3272
        }
3273
#elif defined(__FreeBSD__) && !defined(__Userspace__)
3274
        error = scope6_check_id(sin6, MODULE_GLOBAL(ip6_use_defzone));
3275
        if (error != 0) {
3276
          error = EINVAL;
3277
          SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3278
          goto out;
3279
        }
3280
#else
3281
        if (in6_embedscope(&sin6->sin6_addr, sin6) != 0) {
3282
          error = EINVAL;
3283
          SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3284
          goto out;
3285
        }
3286
#endif
3287
#endif /* SCTP_EMBEDDED_V6_SCOPE */
3288
0
      }
3289
0
#ifndef SCOPEDROUTING
3290
      /* this must be cleared for ifa_ifwithaddr() */
3291
0
      sin6->sin6_scope_id = 0;
3292
0
#endif /* SCOPEDROUTING */
3293
0
      break;
3294
0
    }
3295
0
#endif
3296
0
#if defined(__Userspace__)
3297
12.8k
    case AF_CONN:
3298
12.8k
    {
3299
12.8k
      struct sockaddr_conn *sconn;
3300
3301
#ifdef HAVE_SA_LEN
3302
      if (addr->sa_len != sizeof(struct sockaddr_conn)) {
3303
        error = EINVAL;
3304
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3305
        goto out;
3306
      }
3307
#endif
3308
12.8k
      sconn = (struct sockaddr_conn *)addr;
3309
12.8k
      lport = sconn->sconn_port;
3310
12.8k
      if (sconn->sconn_addr != NULL) {
3311
12.8k
        bindall = 0;
3312
12.8k
      }
3313
12.8k
      break;
3314
0
    }
3315
0
#endif
3316
0
    default:
3317
0
      error = EAFNOSUPPORT;
3318
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3319
0
      goto out;
3320
12.8k
    }
3321
12.8k
  }
3322
  /* Setup a vrf_id to be the default for the non-bind-all case. */
3323
12.8k
  vrf_id = inp->def_vrf_id;
3324
3325
12.8k
  if (lport) {
3326
    /*
3327
     * Did the caller specify a port? if so we must see if an ep
3328
     * already has this one bound.
3329
     */
3330
    /* got to be root to get at low ports */
3331
12.8k
#if !(defined(_WIN32) && !defined(__Userspace__))
3332
12.8k
    if (ntohs(lport) < IPPORT_RESERVED &&
3333
#if defined(__FreeBSD__) && !defined(__Userspace__)
3334
        (error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
3335
#elif defined(__APPLE__) && !defined(__Userspace__)
3336
        (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
3337
#elif defined(__Userspace__)
3338
      /* TODO ensure uid is 0, etc... */
3339
12.8k
        0) {
3340
#else
3341
        (error = suser(p, 0)) != 0) {
3342
#endif
3343
0
      goto out;
3344
0
    }
3345
12.8k
#endif
3346
12.8k
    SCTP_INP_INCR_REF(inp);
3347
12.8k
    SCTP_INP_WUNLOCK(inp);
3348
12.8k
    if (bindall) {
3349
#ifdef SCTP_MVRF
3350
      for (i = 0; i < inp->num_vrfs; i++) {
3351
        vrf_id = inp->m_vrf_ids[i];
3352
#else
3353
0
        vrf_id = inp->def_vrf_id;
3354
0
#endif
3355
0
        inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3356
0
        if (inp_tmp != NULL) {
3357
          /*
3358
           * lock guy returned and lower count
3359
           * note that we are not bound so
3360
           * inp_tmp should NEVER be inp. And
3361
           * it is this inp (inp_tmp) that gets
3362
           * the reference bump, so we must
3363
           * lower it.
3364
           */
3365
0
          SCTP_INP_DECR_REF(inp_tmp);
3366
          /* unlock info */
3367
0
          if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3368
0
              (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3369
            /* Ok, must be one-2-one and allowing port re-use */
3370
0
            port_reuse_active = 1;
3371
0
            goto continue_anyway;
3372
0
          }
3373
0
          SCTP_INP_WLOCK(inp);
3374
0
          SCTP_INP_DECR_REF(inp);
3375
0
          error = EADDRINUSE;
3376
0
          SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3377
0
          goto out;
3378
0
        }
3379
#ifdef SCTP_MVRF
3380
      }
3381
#endif
3382
12.8k
    } else {
3383
12.8k
      inp_tmp = sctp_pcb_findep(addr, 0, 1, vrf_id);
3384
12.8k
      if (inp_tmp != NULL) {
3385
        /*
3386
         * lock guy returned and lower count note
3387
         * that we are not bound so inp_tmp should
3388
         * NEVER be inp. And it is this inp (inp_tmp)
3389
         * that gets the reference bump, so we must
3390
         * lower it.
3391
         */
3392
0
        SCTP_INP_DECR_REF(inp_tmp);
3393
        /* unlock info */
3394
0
        if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3395
0
            (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3396
          /* Ok, must be one-2-one and allowing port re-use */
3397
0
          port_reuse_active = 1;
3398
0
          goto continue_anyway;
3399
0
        }
3400
0
        SCTP_INP_WLOCK(inp);
3401
0
        SCTP_INP_DECR_REF(inp);
3402
0
        error = EADDRINUSE;
3403
0
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3404
0
        goto out;
3405
0
      }
3406
12.8k
    }
3407
12.8k
  continue_anyway:
3408
12.8k
    SCTP_INP_WLOCK(inp);
3409
12.8k
    SCTP_INP_DECR_REF(inp);
3410
12.8k
    if (bindall) {
3411
      /* verify that no lport is not used by a singleton */
3412
0
      if ((port_reuse_active == 0) &&
3413
0
          (inp_tmp = sctp_isport_inuse(inp, lport, vrf_id))) {
3414
        /* Sorry someone already has this one bound */
3415
0
        if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_PORTREUSE)) &&
3416
0
            (sctp_is_feature_on(inp_tmp, SCTP_PCB_FLAGS_PORTREUSE))) {
3417
0
          port_reuse_active = 1;
3418
0
        } else {
3419
0
          error = EADDRINUSE;
3420
0
          SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3421
0
          goto out;
3422
0
        }
3423
0
      }
3424
0
    }
3425
12.8k
  } else {
3426
0
    uint16_t first, last, candidate;
3427
0
    uint16_t count;
3428
3429
0
#if defined(__Userspace__)
3430
0
    first = MODULE_GLOBAL(ipport_firstauto);
3431
0
    last = MODULE_GLOBAL(ipport_lastauto);
3432
#elif defined(_WIN32)
3433
    first = 1;
3434
    last = 0xffff;
3435
#elif defined(__FreeBSD__) || defined(__APPLE__)
3436
    if (ip_inp->inp_flags & INP_HIGHPORT) {
3437
      first = MODULE_GLOBAL(ipport_hifirstauto);
3438
      last = MODULE_GLOBAL(ipport_hilastauto);
3439
    } else if (ip_inp->inp_flags & INP_LOWPORT) {
3440
#if defined(__FreeBSD__)
3441
      if ((error = priv_check(td, PRIV_NETINET_RESERVEDPORT)) != 0) {
3442
#else
3443
      if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) {
3444
#endif
3445
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3446
        goto out;
3447
      }
3448
      first = MODULE_GLOBAL(ipport_lowfirstauto);
3449
      last = MODULE_GLOBAL(ipport_lowlastauto);
3450
    } else {
3451
      first = MODULE_GLOBAL(ipport_firstauto);
3452
      last = MODULE_GLOBAL(ipport_lastauto);
3453
    }
3454
#endif
3455
0
    if (first > last) {
3456
0
      uint16_t temp;
3457
3458
0
      temp = first;
3459
0
      first = last;
3460
0
      last = temp;
3461
0
    }
3462
0
    count = last - first + 1; /* number of candidates */
3463
0
    candidate = first + sctp_select_initial_TSN(&inp->sctp_ep) % (count);
3464
3465
0
    for (;;) {
3466
#ifdef SCTP_MVRF
3467
      for (i = 0; i < inp->num_vrfs; i++) {
3468
        if (sctp_isport_inuse(inp, htons(candidate), inp->m_vrf_ids[i]) != NULL) {
3469
          break;
3470
        }
3471
      }
3472
      if (i == inp->num_vrfs) {
3473
        lport = htons(candidate);
3474
        break;
3475
      }
3476
#else
3477
0
      if (sctp_isport_inuse(inp, htons(candidate), inp->def_vrf_id) == NULL) {
3478
0
        lport = htons(candidate);
3479
0
        break;
3480
0
      }
3481
0
#endif
3482
0
      if (--count == 0) {
3483
0
        error = EADDRINUSE;
3484
0
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3485
0
        goto out;
3486
0
      }
3487
0
      if (candidate == last)
3488
0
        candidate = first;
3489
0
      else
3490
0
        candidate = candidate + 1;
3491
0
    }
3492
0
  }
3493
12.8k
  if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE |
3494
12.8k
             SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
3495
    /*
3496
     * this really should not happen. The guy did a non-blocking
3497
     * bind and then did a close at the same time.
3498
     */
3499
0
    error = EINVAL;
3500
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3501
0
    goto out;
3502
0
  }
3503
  /* ok we look clear to give out this port, so lets setup the binding */
3504
12.8k
  if (bindall) {
3505
    /* binding to all addresses, so just set in the proper flags */
3506
0
    inp->sctp_flags |= SCTP_PCB_FLAGS_BOUNDALL;
3507
    /* set the automatic addr changes from kernel flag */
3508
0
    if (SCTP_BASE_SYSCTL(sctp_auto_asconf) == 0) {
3509
0
      sctp_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3510
0
      sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3511
0
    } else {
3512
0
      sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3513
0
      sctp_feature_on(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3514
0
    }
3515
0
    if (SCTP_BASE_SYSCTL(sctp_multiple_asconfs) == 0) {
3516
0
      sctp_feature_off(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3517
0
    } else {
3518
0
      sctp_feature_on(inp, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS);
3519
0
    }
3520
    /* set the automatic mobility_base from kernel
3521
       flag (by micchie)
3522
    */
3523
0
    if (SCTP_BASE_SYSCTL(sctp_mobility_base) == 0) {
3524
0
      sctp_mobility_feature_off(inp, SCTP_MOBILITY_BASE);
3525
0
      sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3526
0
    } else {
3527
0
      sctp_mobility_feature_on(inp, SCTP_MOBILITY_BASE);
3528
0
      sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3529
0
    }
3530
    /* set the automatic mobility_fasthandoff from kernel
3531
       flag (by micchie)
3532
    */
3533
0
    if (SCTP_BASE_SYSCTL(sctp_mobility_fasthandoff) == 0) {
3534
0
      sctp_mobility_feature_off(inp, SCTP_MOBILITY_FASTHANDOFF);
3535
0
      sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3536
0
    } else {
3537
0
      sctp_mobility_feature_on(inp, SCTP_MOBILITY_FASTHANDOFF);
3538
0
      sctp_mobility_feature_off(inp, SCTP_MOBILITY_PRIM_DELETED);
3539
0
    }
3540
12.8k
  } else {
3541
    /*
3542
     * bind specific, make sure flags is off and add a new
3543
     * address structure to the sctp_addr_list inside the ep
3544
     * structure.
3545
     *
3546
     * We will need to allocate one and insert it at the head. The
3547
     * socketopt call can just insert new addresses in there as
3548
     * well. It will also have to do the embed scope kame hack
3549
     * too (before adding).
3550
     */
3551
12.8k
    struct sctp_ifa *ifa;
3552
12.8k
    union sctp_sockstore store;
3553
3554
12.8k
    memset(&store, 0, sizeof(store));
3555
12.8k
    switch (addr->sa_family) {
3556
0
#ifdef INET
3557
0
    case AF_INET:
3558
0
      memcpy(&store.sin, addr, sizeof(struct sockaddr_in));
3559
0
      store.sin.sin_port = 0;
3560
0
      break;
3561
0
#endif
3562
0
#ifdef INET6
3563
0
    case AF_INET6:
3564
0
      memcpy(&store.sin6, addr, sizeof(struct sockaddr_in6));
3565
0
      store.sin6.sin6_port = 0;
3566
0
      break;
3567
0
#endif
3568
0
#if defined(__Userspace__)
3569
12.8k
    case AF_CONN:
3570
12.8k
      memcpy(&store.sconn, addr, sizeof(struct sockaddr_conn));
3571
12.8k
      store.sconn.sconn_port = 0;
3572
12.8k
      break;
3573
0
#endif
3574
0
    default:
3575
0
      break;
3576
12.8k
    }
3577
    /*
3578
     * first find the interface with the bound address need to
3579
     * zero out the port to find the address! yuck! can't do
3580
     * this earlier since need port for sctp_pcb_findep()
3581
     */
3582
12.8k
    if (sctp_ifap != NULL) {
3583
0
      ifa = sctp_ifap;
3584
12.8k
    } else {
3585
      /* Note for BSD we hit here always other
3586
       * O/S's will pass things in via the
3587
       * sctp_ifap argument.
3588
       */
3589
12.8k
      ifa = sctp_find_ifa_by_addr(&store.sa,
3590
12.8k
                vrf_id, SCTP_ADDR_NOT_LOCKED);
3591
12.8k
    }
3592
12.8k
    if (ifa == NULL) {
3593
0
      error = EADDRNOTAVAIL;
3594
      /* Can't find an interface with that address */
3595
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3596
0
      goto out;
3597
0
    }
3598
12.8k
#ifdef INET6
3599
12.8k
    if (addr->sa_family == AF_INET6) {
3600
      /* GAK, more FIXME IFA lock? */
3601
0
      if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
3602
        /* Can't bind a non-existent addr. */
3603
0
        error = EINVAL;
3604
0
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, error);
3605
0
        goto out;
3606
0
      }
3607
0
    }
3608
12.8k
#endif
3609
    /* we're not bound all */
3610
12.8k
    inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUNDALL;
3611
    /* allow bindx() to send ASCONF's for binding changes */
3612
12.8k
    sctp_feature_on(inp, SCTP_PCB_FLAGS_DO_ASCONF);
3613
    /* clear automatic addr changes from kernel flag */
3614
12.8k
    sctp_feature_off(inp, SCTP_PCB_FLAGS_AUTO_ASCONF);
3615
3616
    /* add this address to the endpoint list */
3617
12.8k
    error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, 0);
3618
12.8k
    if (error != 0)
3619
0
      goto out;
3620
12.8k
    inp->laddr_count++;
3621
12.8k
  }
3622
  /* find the bucket */
3623
12.8k
  if (port_reuse_active) {
3624
    /* Put it into tcp 1-2-1 hash */
3625
0
    head = &SCTP_BASE_INFO(sctp_tcpephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashtcpmark))];
3626
0
    inp->sctp_flags |= SCTP_PCB_FLAGS_IN_TCPPOOL;
3627
12.8k
  } else {
3628
12.8k
    head = &SCTP_BASE_INFO(sctp_ephash)[SCTP_PCBHASH_ALLADDR(lport, SCTP_BASE_INFO(hashmark))];
3629
12.8k
  }
3630
  /* put it in the bucket */
3631
12.8k
  LIST_INSERT_HEAD(head, inp, sctp_hash);
3632
12.8k
  SCTPDBG(SCTP_DEBUG_PCB1, "Main hash to bind at head:%p, bound port:%d - in tcp_pool=%d\n",
3633
12.8k
    (void *)head, ntohs(lport), port_reuse_active);
3634
  /* set in the port */
3635
12.8k
  inp->sctp_lport = lport;
3636
3637
  /* turn off just the unbound flag */
3638
12.8k
  KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) != 0,
3639
12.8k
      ("%s: inp %p is already bound", __func__, inp));
3640
12.8k
  inp->sctp_flags &= ~SCTP_PCB_FLAGS_UNBOUND;
3641
12.8k
out:
3642
12.8k
  return (error);
3643
12.8k
}
3644
3645
int
3646
#if defined(__FreeBSD__) && !defined(__Userspace__)
3647
sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3648
                struct sctp_ifa *sctp_ifap, struct thread *td)
3649
#elif defined(_WIN32) && !defined(__Userspace__)
3650
sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3651
                struct sctp_ifa *sctp_ifap, PKTHREAD p)
3652
#else
3653
sctp_inpcb_bind(struct socket *so, struct sockaddr *addr,
3654
                struct sctp_ifa *sctp_ifap, struct proc *p)
3655
#endif
3656
12.8k
{
3657
12.8k
  struct sctp_inpcb *inp;
3658
12.8k
  int error;
3659
3660
12.8k
  inp = so->so_pcb;
3661
12.8k
  SCTP_INP_INFO_WLOCK();
3662
12.8k
  SCTP_INP_WLOCK(inp);
3663
#if defined(__FreeBSD__) && !defined(__Userspace__)
3664
  error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, td);
3665
#else
3666
12.8k
  error = sctp_inpcb_bind_locked(inp, addr, sctp_ifap, p);
3667
12.8k
#endif
3668
12.8k
  SCTP_INP_WUNLOCK(inp);
3669
12.8k
  SCTP_INP_INFO_WUNLOCK();
3670
12.8k
  return (error);
3671
12.8k
}
3672
3673
static void
3674
sctp_iterator_inp_being_freed(struct sctp_inpcb *inp)
3675
12.9k
{
3676
12.9k
  struct sctp_iterator *it, *nit;
3677
3678
  /*
3679
   * We enter with the only the ITERATOR_LOCK in place and a write
3680
   * lock on the inp_info stuff.
3681
   */
3682
12.9k
  it = sctp_it_ctl.cur_it;
3683
#if defined(__FreeBSD__) && !defined(__Userspace__)
3684
  if (it && (it->vn != curvnet)) {
3685
    /* Its not looking at our VNET */
3686
    return;
3687
  }
3688
#endif
3689
12.9k
  if (it && (it->inp == inp)) {
3690
    /*
3691
     * This is tricky and we hold the iterator lock,
3692
     * but when it returns and gets the lock (when we
3693
     * release it) the iterator will try to operate on
3694
     * inp. We need to stop that from happening. But
3695
     * of course the iterator has a reference on the
3696
     * stcb and inp. We can mark it and it will stop.
3697
     *
3698
     * If its a single iterator situation, we
3699
     * set the end iterator flag. Otherwise
3700
     * we set the iterator to go to the next inp.
3701
     *
3702
     */
3703
0
    if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3704
0
      sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
3705
0
    } else {
3706
0
      sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_INP;
3707
0
    }
3708
0
  }
3709
  /* Now go through and remove any single reference to
3710
   * our inp that may be still pending on the list
3711
   */
3712
12.9k
  SCTP_IPI_ITERATOR_WQ_LOCK();
3713
12.9k
  TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
3714
#if defined(__FreeBSD__) && !defined(__Userspace__)
3715
    if (it->vn != curvnet) {
3716
      continue;
3717
    }
3718
#endif
3719
0
    if (it->inp == inp) {
3720
      /* This one points to me is it inp specific? */
3721
0
      if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
3722
        /* Remove and free this one */
3723
0
        TAILQ_REMOVE(&sctp_it_ctl.iteratorhead,
3724
0
            it, sctp_nxt_itr);
3725
0
        if (it->function_atend != NULL) {
3726
0
          (*it->function_atend) (it->pointer, it->val);
3727
0
        }
3728
0
        SCTP_FREE(it, SCTP_M_ITER);
3729
0
      } else {
3730
0
        it->inp = LIST_NEXT(it->inp, sctp_list);
3731
0
        if (it->inp) {
3732
0
          SCTP_INP_INCR_REF(it->inp);
3733
0
        }
3734
0
      }
3735
      /* When its put in the refcnt is incremented so decr it */
3736
0
      SCTP_INP_DECR_REF(inp);
3737
0
    }
3738
0
  }
3739
12.9k
  SCTP_IPI_ITERATOR_WQ_UNLOCK();
3740
12.9k
}
3741
3742
/* release sctp_inpcb unbind the port */
3743
void
3744
sctp_inpcb_free(struct sctp_inpcb *inp, int immediate, int from)
3745
12.9k
{
3746
  /*
3747
   * Here we free a endpoint. We must find it (if it is in the Hash
3748
   * table) and remove it from there. Then we must also find it in the
3749
   * overall list and remove it from there. After all removals are
3750
   * complete then any timer has to be stopped. Then start the actual
3751
   * freeing. a) Any local lists. b) Any associations. c) The hash of
3752
   * all associations. d) finally the ep itself.
3753
   */
3754
12.9k
  struct sctp_tcb *stcb, *nstcb;
3755
12.9k
  struct sctp_laddr *laddr, *nladdr;
3756
12.9k
  struct inpcb *ip_pcb;
3757
12.9k
  struct socket *so;
3758
12.9k
  int being_refed = 0;
3759
12.9k
  struct sctp_queued_to_read *sq, *nsq;
3760
#if !defined(__Userspace__)
3761
#if !defined(__FreeBSD__)
3762
  sctp_rtentry_t *rt;
3763
#endif
3764
#endif
3765
12.9k
  int cnt;
3766
12.9k
  sctp_sharedkey_t *shared_key, *nshared_key;
3767
3768
#if defined(__APPLE__) && !defined(__Userspace__)
3769
  sctp_lock_assert(SCTP_INP_SO(inp));
3770
#endif
3771
#ifdef SCTP_LOG_CLOSING
3772
  sctp_log_closing(inp, NULL, 0);
3773
#endif
3774
12.9k
  SCTP_ITERATOR_LOCK();
3775
  /* mark any iterators on the list or being processed */
3776
12.9k
  sctp_iterator_inp_being_freed(inp);
3777
12.9k
  SCTP_ITERATOR_UNLOCK();
3778
3779
12.9k
  SCTP_ASOC_CREATE_LOCK(inp);
3780
12.9k
  SCTP_INP_INFO_WLOCK();
3781
12.9k
  SCTP_INP_WLOCK(inp);
3782
12.9k
  so = inp->sctp_socket;
3783
12.9k
  KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) != 0,
3784
12.9k
      ("%s: inp %p still has socket", __func__, inp));
3785
12.9k
  KASSERT((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) == 0,
3786
12.9k
      ("%s: double free of inp %p", __func__, inp));
3787
12.9k
  if (from == SCTP_CALLED_AFTER_CMPSET_OFCLOSE) {
3788
12.8k
    inp->sctp_flags &= ~SCTP_PCB_FLAGS_CLOSE_IP;
3789
    /* socket is gone, so no more wakeups allowed */
3790
12.8k
    inp->sctp_flags |= SCTP_PCB_FLAGS_DONT_WAKE;
3791
12.8k
    inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
3792
12.8k
    inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
3793
12.8k
  }
3794
  /* First time through we have the socket lock, after that no more. */
3795
12.9k
  sctp_timer_stop(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL,
3796
12.9k
                  SCTP_FROM_SCTP_PCB + SCTP_LOC_1);
3797
3798
12.9k
  if (inp->control) {
3799
0
    sctp_m_freem(inp->control);
3800
0
    inp->control = NULL;
3801
0
  }
3802
12.9k
  if (inp->pkt) {
3803
0
    sctp_m_freem(inp->pkt);
3804
0
    inp->pkt = NULL;
3805
0
  }
3806
12.9k
  ip_pcb = &inp->ip_inp.inp;  /* we could just cast the main pointer
3807
           * here but I will be nice :> (i.e.
3808
           * ip_pcb = ep;) */
3809
12.9k
  if (immediate == SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3810
22
    int cnt_in_sd;
3811
3812
22
    cnt_in_sd = 0;
3813
22
    LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) {
3814
0
      SCTP_TCB_LOCK(stcb);
3815
      /* Disconnect the socket please. */
3816
0
      stcb->sctp_socket = NULL;
3817
0
      SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET);
3818
0
      if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3819
        /* Skip guys being freed */
3820
0
        cnt_in_sd++;
3821
0
        if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3822
          /*
3823
           * Special case - we did not start a kill
3824
           * timer on the asoc due to it was not
3825
           * closed. So go ahead and start it now.
3826
           */
3827
0
          SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
3828
0
          sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3829
0
        }
3830
0
        SCTP_TCB_UNLOCK(stcb);
3831
0
        continue;
3832
0
      }
3833
0
      if (((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
3834
0
           (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) &&
3835
0
          (stcb->asoc.total_output_queue_size == 0)) {
3836
        /* If we have data in queue, we don't want to just
3837
         * free since the app may have done, send()/close
3838
         * or connect/send/close. And it wants the data
3839
         * to get across first.
3840
         */
3841
        /* Just abandon things in the front states */
3842
0
        if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_NOFORCE,
3843
0
               SCTP_FROM_SCTP_PCB + SCTP_LOC_2) == 0) {
3844
0
          cnt_in_sd++;
3845
0
        }
3846
0
        continue;
3847
0
      }
3848
0
      if ((stcb->asoc.size_on_reasm_queue > 0) ||
3849
0
          (stcb->asoc.size_on_all_streams > 0) ||
3850
0
          ((so != NULL) && (SCTP_SBAVAIL(&so->so_rcv) > 0))) {
3851
        /* Left with Data unread */
3852
0
        struct mbuf *op_err;
3853
3854
0
        op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3855
0
        stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_3;
3856
0
        sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
3857
0
        SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3858
0
        if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
3859
0
            (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3860
0
          SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3861
0
        }
3862
0
        if (sctp_free_assoc(inp, stcb,
3863
0
                SCTP_PCBFREE_NOFORCE, SCTP_FROM_SCTP_PCB + SCTP_LOC_4) == 0) {
3864
0
          cnt_in_sd++;
3865
0
        }
3866
0
        continue;
3867
0
      } else if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3868
0
                 TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3869
0
                 (stcb->asoc.stream_queue_cnt == 0)) {
3870
0
        if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, &stcb->asoc)) {
3871
0
          goto abort_anyway;
3872
0
        }
3873
0
        if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
3874
0
            (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
3875
0
          struct sctp_nets *netp;
3876
3877
          /*
3878
           * there is nothing queued to send,
3879
           * so I send shutdown
3880
           */
3881
0
          if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
3882
0
              (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3883
0
            SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3884
0
          }
3885
0
          SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
3886
0
          sctp_stop_timers_for_shutdown(stcb);
3887
0
          if (stcb->asoc.alternate) {
3888
0
            netp = stcb->asoc.alternate;
3889
0
          } else {
3890
0
            netp = stcb->asoc.primary_destination;
3891
0
          }
3892
0
          sctp_send_shutdown(stcb, netp);
3893
0
          sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
3894
0
              netp);
3895
0
          sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb, NULL);
3896
0
          sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_LOCKED);
3897
0
        }
3898
0
      } else {
3899
        /* mark into shutdown pending */
3900
0
        SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
3901
0
        if ((*stcb->asoc.ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, &stcb->asoc)) {
3902
0
          SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
3903
0
        }
3904
0
        if (TAILQ_EMPTY(&stcb->asoc.send_queue) &&
3905
0
            TAILQ_EMPTY(&stcb->asoc.sent_queue) &&
3906
0
            (stcb->asoc.state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
3907
0
          struct mbuf *op_err;
3908
0
        abort_anyway:
3909
0
          op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3910
0
          stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_5;
3911
0
          sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
3912
0
          SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3913
0
          if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
3914
0
              (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3915
0
            SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3916
0
          }
3917
0
          if (sctp_free_assoc(inp, stcb,
3918
0
                  SCTP_PCBFREE_NOFORCE,
3919
0
                  SCTP_FROM_SCTP_PCB + SCTP_LOC_6) == 0) {
3920
0
            cnt_in_sd++;
3921
0
          }
3922
0
          continue;
3923
0
        } else {
3924
0
          sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_CLOSING, SCTP_SO_LOCKED);
3925
0
        }
3926
0
      }
3927
0
      cnt_in_sd++;
3928
0
      SCTP_TCB_UNLOCK(stcb);
3929
0
    }
3930
    /* now is there some left in our SHUTDOWN state? */
3931
22
    if (cnt_in_sd) {
3932
#ifdef SCTP_LOG_CLOSING
3933
      sctp_log_closing(inp, NULL, 2);
3934
#endif
3935
0
      inp->sctp_socket = NULL;
3936
0
      SCTP_INP_WUNLOCK(inp);
3937
0
      SCTP_ASOC_CREATE_UNLOCK(inp);
3938
0
      SCTP_INP_INFO_WUNLOCK();
3939
0
      return;
3940
0
    }
3941
22
  }
3942
12.9k
  inp->sctp_socket = NULL;
3943
12.9k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) == 0) {
3944
    /*
3945
     * ok, this guy has been bound. It's port is
3946
     * somewhere in the SCTP_BASE_INFO(hash table). Remove
3947
     * it!
3948
     */
3949
12.8k
    LIST_REMOVE(inp, sctp_hash);
3950
12.8k
    inp->sctp_flags |= SCTP_PCB_FLAGS_UNBOUND;
3951
12.8k
  }
3952
3953
  /* If there is a timer running to kill us,
3954
   * forget it, since it may have a contest
3955
   * on the INP lock.. which would cause us
3956
   * to die ...
3957
   */
3958
12.9k
  cnt = 0;
3959
12.9k
  LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_list, sctp_tcblist, nstcb) {
3960
11.1k
    SCTP_TCB_LOCK(stcb);
3961
11.1k
    if (immediate != SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE) {
3962
      /* Disconnect the socket please */
3963
11.1k
      stcb->sctp_socket = NULL;
3964
11.1k
      SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_CLOSED_SOCKET);
3965
11.1k
    }
3966
11.1k
    if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3967
17
      if (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE) {
3968
0
        SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
3969
0
        sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
3970
0
      }
3971
17
      cnt++;
3972
17
      SCTP_TCB_UNLOCK(stcb);
3973
17
      continue;
3974
17
    }
3975
    /* Free associations that are NOT killing us */
3976
11.0k
    if ((SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT) &&
3977
11.0k
        ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) {
3978
9.09k
      struct mbuf *op_err;
3979
3980
9.09k
      op_err = sctp_generate_cause(SCTP_CAUSE_USER_INITIATED_ABT, "");
3981
9.09k
      stcb->sctp_ep->last_abort_code = SCTP_FROM_SCTP_PCB + SCTP_LOC_7;
3982
9.09k
      sctp_send_abort_tcb(stcb, op_err, SCTP_SO_LOCKED);
3983
9.09k
      SCTP_STAT_INCR_COUNTER32(sctps_aborted);
3984
9.09k
    } else if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
3985
0
      cnt++;
3986
0
      SCTP_TCB_UNLOCK(stcb);
3987
0
      continue;
3988
0
    }
3989
11.0k
    if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
3990
11.0k
        (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
3991
6.38k
      SCTP_STAT_DECR_GAUGE32(sctps_currestab);
3992
6.38k
    }
3993
11.0k
    if (sctp_free_assoc(inp, stcb, SCTP_PCBFREE_FORCE,
3994
11.0k
                        SCTP_FROM_SCTP_PCB + SCTP_LOC_8) == 0) {
3995
5
      cnt++;
3996
5
    }
3997
11.0k
  }
3998
12.9k
  if (cnt) {
3999
    /* Ok we have someone out there that will kill us */
4000
#ifdef SCTP_LOG_CLOSING
4001
    sctp_log_closing(inp, NULL, 3);
4002
#endif
4003
22
    SCTP_INP_WUNLOCK(inp);
4004
22
    SCTP_ASOC_CREATE_UNLOCK(inp);
4005
22
    SCTP_INP_INFO_WUNLOCK();
4006
22
    return;
4007
22
  }
4008
12.8k
  if (SCTP_INP_LOCK_CONTENDED(inp))
4009
0
    being_refed++;
4010
12.8k
  if (SCTP_INP_READ_CONTENDED(inp))
4011
0
    being_refed++;
4012
12.8k
  if (SCTP_ASOC_CREATE_LOCK_CONTENDED(inp))
4013
0
    being_refed++;
4014
  /* NOTE: 0 refcount also means no timers are referencing us. */
4015
12.8k
  if ((inp->refcount) ||
4016
12.8k
      (being_refed) ||
4017
12.8k
      (inp->sctp_flags & SCTP_PCB_FLAGS_CLOSE_IP)) {
4018
#ifdef SCTP_LOG_CLOSING
4019
    sctp_log_closing(inp, NULL, 4);
4020
#endif
4021
22
    sctp_timer_start(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL);
4022
22
    SCTP_INP_WUNLOCK(inp);
4023
22
    SCTP_ASOC_CREATE_UNLOCK(inp);
4024
22
    SCTP_INP_INFO_WUNLOCK();
4025
22
    return;
4026
22
  }
4027
12.8k
  inp->sctp_ep.signature_change.type = 0;
4028
12.8k
  inp->sctp_flags |= SCTP_PCB_FLAGS_SOCKET_ALLGONE;
4029
  /* Remove it from the list .. last thing we need a
4030
   * lock for.
4031
   */
4032
12.8k
  LIST_REMOVE(inp, sctp_list);
4033
12.8k
  SCTP_INP_WUNLOCK(inp);
4034
12.8k
  SCTP_ASOC_CREATE_UNLOCK(inp);
4035
12.8k
  SCTP_INP_INFO_WUNLOCK();
4036
4037
#ifdef SCTP_LOG_CLOSING
4038
  sctp_log_closing(inp, NULL, 5);
4039
#endif
4040
#if !(defined(_WIN32) || defined(__Userspace__))
4041
#if !(defined(__FreeBSD__) && !defined(__Userspace__))
4042
  rt = ip_pcb->inp_route.ro_rt;
4043
#endif
4044
#endif
4045
12.8k
  if ((inp->sctp_asocidhash) != NULL) {
4046
12.8k
    SCTP_HASH_FREE(inp->sctp_asocidhash, inp->hashasocidmark);
4047
12.8k
    inp->sctp_asocidhash = NULL;
4048
12.8k
  }
4049
  /*sa_ignore FREED_MEMORY*/
4050
12.8k
  TAILQ_FOREACH_SAFE(sq, &inp->read_queue, next, nsq) {
4051
    /* Its only abandoned if it had data left */
4052
0
    if (sq->length)
4053
0
      SCTP_STAT_INCR(sctps_left_abandon);
4054
4055
0
    TAILQ_REMOVE(&inp->read_queue, sq, next);
4056
0
    sctp_free_remote_addr(sq->whoFrom);
4057
0
    if (so)
4058
0
      SCTP_SB_DECR(&so->so_rcv, sq->length);
4059
0
    if (sq->data) {
4060
0
      sctp_m_freem(sq->data);
4061
0
      sq->data = NULL;
4062
0
    }
4063
    /*
4064
     * no need to free the net count, since at this point all
4065
     * assoc's are gone.
4066
     */
4067
0
    sctp_free_a_readq(NULL, sq);
4068
0
  }
4069
  /* Now the sctp_pcb things */
4070
  /*
4071
   * free each asoc if it is not already closed/free. we can't use the
4072
   * macro here since le_next will get freed as part of the
4073
   * sctp_free_assoc() call.
4074
   */
4075
12.8k
  if (ip_pcb->inp_options) {
4076
0
    (void)sctp_m_free(ip_pcb->inp_options);
4077
0
    ip_pcb->inp_options = 0;
4078
0
  }
4079
#if !(defined(_WIN32) || defined(__Userspace__))
4080
#if !defined(__FreeBSD__)
4081
  if (rt) {
4082
    RTFREE(rt);
4083
    ip_pcb->inp_route.ro_rt = 0;
4084
  }
4085
#endif
4086
#endif
4087
12.8k
#ifdef INET6
4088
#if !(defined(_WIN32) || defined(__Userspace__))
4089
#if (defined(__FreeBSD__) || defined(__APPLE__) && !defined(__Userspace__))
4090
  if (ip_pcb->inp_vflag & INP_IPV6) {
4091
#else
4092
  if (inp->inp_vflag & INP_IPV6) {
4093
#endif
4094
    ip6_freepcbopts(ip_pcb->in6p_outputopts);
4095
  }
4096
#endif
4097
12.8k
#endif        /* INET6 */
4098
12.8k
  ip_pcb->inp_vflag = 0;
4099
  /* free up authentication fields */
4100
12.8k
  if (inp->sctp_ep.local_auth_chunks != NULL)
4101
12.8k
    sctp_free_chunklist(inp->sctp_ep.local_auth_chunks);
4102
12.8k
  if (inp->sctp_ep.local_hmacs != NULL)
4103
12.8k
    sctp_free_hmaclist(inp->sctp_ep.local_hmacs);
4104
4105
12.8k
  LIST_FOREACH_SAFE(shared_key, &inp->sctp_ep.shared_keys, next, nshared_key) {
4106
12.8k
    LIST_REMOVE(shared_key, next);
4107
12.8k
    sctp_free_sharedkey(shared_key);
4108
    /*sa_ignore FREED_MEMORY*/
4109
12.8k
  }
4110
4111
#if defined(__APPLE__) && !defined(__Userspace__)
4112
  inp->ip_inp.inp.inp_state = INPCB_STATE_DEAD;
4113
  if (in_pcb_checkstate(&inp->ip_inp.inp, WNT_STOPUSING, 1) != WNT_STOPUSING) {
4114
#ifdef INVARIANTS
4115
    panic("sctp_inpcb_free inp = %p couldn't set to STOPUSING", (void *)inp);
4116
#else
4117
    SCTP_PRINTF("sctp_inpcb_free inp = %p couldn't set to STOPUSING\n", (void *)inp);
4118
#endif
4119
  }
4120
  inp->ip_inp.inp.inp_socket->so_flags |= SOF_PCBCLEARING;
4121
#endif
4122
  /*
4123
   * if we have an address list the following will free the list of
4124
   * ifaddr's that are set into this ep. Again macro limitations here,
4125
   * since the LIST_FOREACH could be a bad idea.
4126
   */
4127
12.8k
  LIST_FOREACH_SAFE(laddr, &inp->sctp_addr_list, sctp_nxt_addr, nladdr) {
4128
12.8k
    sctp_remove_laddr(laddr);
4129
12.8k
  }
4130
4131
#ifdef SCTP_TRACK_FREED_ASOCS
4132
  /* TEMP CODE */
4133
  LIST_FOREACH_SAFE(stcb, &inp->sctp_asoc_free_list, sctp_tcblist, nstcb) {
4134
    LIST_REMOVE(stcb, sctp_tcblist);
4135
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4136
    SCTP_DECR_ASOC_COUNT();
4137
  }
4138
  /* *** END TEMP CODE ****/
4139
#endif
4140
#ifdef SCTP_MVRF
4141
  SCTP_FREE(inp->m_vrf_ids, SCTP_M_MVRF);
4142
#endif
4143
  /* Now lets see about freeing the EP hash table. */
4144
12.8k
  if (inp->sctp_tcbhash != NULL) {
4145
12.8k
    SCTP_HASH_FREE(inp->sctp_tcbhash, inp->sctp_hashmark);
4146
12.8k
    inp->sctp_tcbhash = NULL;
4147
12.8k
  }
4148
  /* Now we must put the ep memory back into the zone pool */
4149
#if defined(__FreeBSD__) && !defined(__Userspace__)
4150
  crfree(inp->ip_inp.inp.inp_cred);
4151
  INP_LOCK_DESTROY(&inp->ip_inp.inp);
4152
#endif
4153
12.8k
  SCTP_INP_LOCK_DESTROY(inp);
4154
12.8k
  SCTP_INP_READ_LOCK_DESTROY(inp);
4155
12.8k
  SCTP_ASOC_CREATE_LOCK_DESTROY(inp);
4156
12.8k
#if !(defined(__APPLE__) && !defined(__Userspace__))
4157
12.8k
  SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_ep), inp);
4158
12.8k
  SCTP_DECR_EP_COUNT();
4159
#else
4160
  /* For Tiger, we will do this later... */
4161
#endif
4162
12.8k
}
4163
4164
struct sctp_nets *
4165
sctp_findnet(struct sctp_tcb *stcb, struct sockaddr *addr)
4166
115k
{
4167
115k
  struct sctp_nets *net;
4168
  /* locate the address */
4169
355k
  TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4170
355k
    if (sctp_cmpaddr(addr, (struct sockaddr *)&net->ro._l_addr))
4171
47.8k
      return (net);
4172
355k
  }
4173
67.2k
  return (NULL);
4174
115k
}
4175
4176
int
4177
sctp_is_address_on_local_host(struct sockaddr *addr, uint32_t vrf_id)
4178
62.7k
{
4179
62.7k
  struct sctp_ifa *sctp_ifa;
4180
62.7k
  sctp_ifa = sctp_find_ifa_by_addr(addr, vrf_id, SCTP_ADDR_NOT_LOCKED);
4181
62.7k
  if (sctp_ifa) {
4182
12.8k
    return (1);
4183
49.8k
  } else {
4184
49.8k
    return (0);
4185
49.8k
  }
4186
62.7k
}
4187
4188
/*
4189
 * add's a remote endpoint address, done with the INIT/INIT-ACK as well as
4190
 * when a ASCONF arrives that adds it. It will also initialize all the cwnd
4191
 * stats of stuff.
4192
 */
4193
int
4194
sctp_add_remote_addr(struct sctp_tcb *stcb, struct sockaddr *newaddr,
4195
    struct sctp_nets **netp, uint16_t port, int set_scope, int from)
4196
68.0k
{
4197
  /*
4198
   * The following is redundant to the same lines in the
4199
   * sctp_aloc_assoc() but is needed since others call the add
4200
   * address function
4201
   */
4202
68.0k
  struct sctp_nets *net, *netfirst;
4203
68.0k
  int addr_inscope;
4204
4205
68.0k
  SCTPDBG(SCTP_DEBUG_PCB1, "Adding an address (from:%d) to the peer: ",
4206
68.0k
    from);
4207
68.0k
  SCTPDBG_ADDR(SCTP_DEBUG_PCB1, newaddr);
4208
4209
68.0k
  netfirst = sctp_findnet(stcb, newaddr);
4210
68.0k
  if (netfirst) {
4211
    /*
4212
     * Lie and return ok, we don't want to make the association
4213
     * go away for this behavior. It will happen in the TCP
4214
     * model in a connected socket. It does not reach the hash
4215
     * table until after the association is built so it can't be
4216
     * found. Mark as reachable, since the initial creation will
4217
     * have been cleared and the NOT_IN_ASSOC flag will have
4218
     * been added... and we don't want to end up removing it
4219
     * back out.
4220
     */
4221
5.27k
    if (netfirst->dest_state & SCTP_ADDR_UNCONFIRMED) {
4222
3.92k
      netfirst->dest_state = (SCTP_ADDR_REACHABLE |
4223
3.92k
          SCTP_ADDR_UNCONFIRMED);
4224
3.92k
    } else {
4225
1.35k
      netfirst->dest_state = SCTP_ADDR_REACHABLE;
4226
1.35k
    }
4227
4228
5.27k
    return (0);
4229
5.27k
  }
4230
62.7k
  addr_inscope = 1;
4231
62.7k
  switch (newaddr->sa_family) {
4232
0
#ifdef INET
4233
44.8k
  case AF_INET:
4234
44.8k
  {
4235
44.8k
    struct sockaddr_in *sin;
4236
4237
44.8k
    sin = (struct sockaddr_in *)newaddr;
4238
44.8k
    if (sin->sin_addr.s_addr == 0) {
4239
      /* Invalid address */
4240
0
      return (-1);
4241
0
    }
4242
    /* zero out the zero area */
4243
44.8k
    memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
4244
4245
    /* assure len is set */
4246
#ifdef HAVE_SIN_LEN
4247
    sin->sin_len = sizeof(struct sockaddr_in);
4248
#endif
4249
44.8k
    if (set_scope) {
4250
0
      if (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) {
4251
0
        stcb->asoc.scope.ipv4_local_scope = 1;
4252
0
      }
4253
44.8k
    } else {
4254
      /* Validate the address is in scope */
4255
44.8k
      if ((IN4_ISPRIVATE_ADDRESS(&sin->sin_addr)) &&
4256
44.8k
          (stcb->asoc.scope.ipv4_local_scope == 0)) {
4257
0
        addr_inscope = 0;
4258
0
      }
4259
44.8k
    }
4260
44.8k
    break;
4261
44.8k
  }
4262
0
#endif
4263
0
#ifdef INET6
4264
5.02k
  case AF_INET6:
4265
5.02k
  {
4266
5.02k
    struct sockaddr_in6 *sin6;
4267
4268
5.02k
    sin6 = (struct sockaddr_in6 *)newaddr;
4269
5.02k
    if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
4270
      /* Invalid address */
4271
0
      return (-1);
4272
0
    }
4273
    /* assure len is set */
4274
#ifdef HAVE_SIN6_LEN
4275
    sin6->sin6_len = sizeof(struct sockaddr_in6);
4276
#endif
4277
5.02k
    if (set_scope) {
4278
0
      if (sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id)) {
4279
0
        stcb->asoc.scope.loopback_scope = 1;
4280
0
        stcb->asoc.scope.local_scope = 0;
4281
0
        stcb->asoc.scope.ipv4_local_scope = 1;
4282
0
        stcb->asoc.scope.site_scope = 1;
4283
0
      } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
4284
        /*
4285
         * If the new destination is a LINK_LOCAL we
4286
         * must have common site scope. Don't set
4287
         * the local scope since we may not share
4288
         * all links, only loopback can do this.
4289
         * Links on the local network would also be
4290
         * on our private network for v4 too.
4291
         */
4292
0
        stcb->asoc.scope.ipv4_local_scope = 1;
4293
0
        stcb->asoc.scope.site_scope = 1;
4294
0
      } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) {
4295
        /*
4296
         * If the new destination is SITE_LOCAL then
4297
         * we must have site scope in common.
4298
         */
4299
0
        stcb->asoc.scope.site_scope = 1;
4300
0
      }
4301
5.02k
    } else {
4302
      /* Validate the address is in scope */
4303
5.02k
      if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr) &&
4304
5.02k
          (stcb->asoc.scope.loopback_scope == 0)) {
4305
0
        addr_inscope = 0;
4306
5.02k
      } else if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
4307
5.02k
          (stcb->asoc.scope.local_scope == 0)) {
4308
2.10k
        addr_inscope = 0;
4309
2.91k
      } else if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
4310
2.91k
          (stcb->asoc.scope.site_scope == 0)) {
4311
0
        addr_inscope = 0;
4312
0
      }
4313
5.02k
    }
4314
5.02k
    break;
4315
5.02k
  }
4316
0
#endif
4317
0
#if defined(__Userspace__)
4318
12.8k
  case AF_CONN:
4319
12.8k
  {
4320
12.8k
    struct sockaddr_conn *sconn;
4321
4322
12.8k
    sconn = (struct sockaddr_conn *)newaddr;
4323
12.8k
    if (sconn->sconn_addr == NULL) {
4324
      /* Invalid address */
4325
0
      return (-1);
4326
0
    }
4327
#ifdef HAVE_SCONN_LEN
4328
    sconn->sconn_len = sizeof(struct sockaddr_conn);
4329
#endif
4330
12.8k
    break;
4331
12.8k
  }
4332
12.8k
#endif
4333
12.8k
  default:
4334
    /* not supported family type */
4335
0
    return (-1);
4336
62.7k
  }
4337
62.7k
  net = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_net), struct sctp_nets);
4338
62.7k
  if (net == NULL) {
4339
0
    return (-1);
4340
0
  }
4341
62.7k
  SCTP_INCR_RADDR_COUNT();
4342
62.7k
  memset(net, 0, sizeof(struct sctp_nets));
4343
62.7k
  (void)SCTP_GETTIME_TIMEVAL(&net->start_time);
4344
#ifdef HAVE_SA_LEN
4345
  memcpy(&net->ro._l_addr, newaddr, newaddr->sa_len);
4346
#endif
4347
62.7k
  switch (newaddr->sa_family) {
4348
0
#ifdef INET
4349
44.8k
  case AF_INET:
4350
44.8k
#ifndef HAVE_SA_LEN
4351
44.8k
    memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in));
4352
44.8k
#endif
4353
44.8k
    ((struct sockaddr_in *)&net->ro._l_addr)->sin_port = stcb->rport;
4354
44.8k
    break;
4355
0
#endif
4356
0
#ifdef INET6
4357
5.02k
  case AF_INET6:
4358
5.02k
#ifndef HAVE_SA_LEN
4359
5.02k
    memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_in6));
4360
5.02k
#endif
4361
5.02k
    ((struct sockaddr_in6 *)&net->ro._l_addr)->sin6_port = stcb->rport;
4362
5.02k
    break;
4363
0
#endif
4364
0
#if defined(__Userspace__)
4365
12.8k
  case AF_CONN:
4366
12.8k
#ifndef HAVE_SA_LEN
4367
12.8k
    memcpy(&net->ro._l_addr, newaddr, sizeof(struct sockaddr_conn));
4368
12.8k
#endif
4369
12.8k
    ((struct sockaddr_conn *)&net->ro._l_addr)->sconn_port = stcb->rport;
4370
12.8k
    break;
4371
0
#endif
4372
0
  default:
4373
0
    break;
4374
62.7k
  }
4375
62.7k
  net->addr_is_local = sctp_is_address_on_local_host(newaddr, stcb->asoc.vrf_id);
4376
62.7k
  if (net->addr_is_local && ((set_scope || (from == SCTP_ADDR_IS_CONFIRMED)))) {
4377
12.8k
    stcb->asoc.scope.loopback_scope = 1;
4378
12.8k
    stcb->asoc.scope.ipv4_local_scope = 1;
4379
12.8k
    stcb->asoc.scope.local_scope = 0;
4380
12.8k
    stcb->asoc.scope.site_scope = 1;
4381
12.8k
    addr_inscope = 1;
4382
12.8k
  }
4383
62.7k
  net->failure_threshold = stcb->asoc.def_net_failure;
4384
62.7k
  net->pf_threshold = stcb->asoc.def_net_pf_threshold;
4385
62.7k
  if (addr_inscope == 0) {
4386
2.10k
    net->dest_state = (SCTP_ADDR_REACHABLE |
4387
2.10k
        SCTP_ADDR_OUT_OF_SCOPE);
4388
60.6k
  } else {
4389
60.6k
    if (from == SCTP_ADDR_IS_CONFIRMED)
4390
      /* SCTP_ADDR_IS_CONFIRMED is passed by connect_x */
4391
0
      net->dest_state = SCTP_ADDR_REACHABLE;
4392
60.6k
    else
4393
60.6k
      net->dest_state = SCTP_ADDR_REACHABLE |
4394
60.6k
          SCTP_ADDR_UNCONFIRMED;
4395
60.6k
  }
4396
  /* We set this to 0, the timer code knows that
4397
   * this means its an initial value
4398
   */
4399
62.7k
  net->rto_needed = 1;
4400
62.7k
  net->RTO = 0;
4401
62.7k
  net->RTO_measured = 0;
4402
62.7k
  stcb->asoc.numnets++;
4403
62.7k
  net->ref_count = 1;
4404
62.7k
  net->cwr_window_tsn = net->last_cwr_tsn = stcb->asoc.sending_seq - 1;
4405
62.7k
  net->port = port;
4406
62.7k
  net->dscp = stcb->asoc.default_dscp;
4407
62.7k
#ifdef INET6
4408
62.7k
  net->flowlabel = stcb->asoc.default_flowlabel;
4409
62.7k
#endif
4410
62.7k
  if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DONOT_HEARTBEAT)) {
4411
0
    net->dest_state |= SCTP_ADDR_NOHB;
4412
62.7k
  } else {
4413
62.7k
    net->dest_state &= ~SCTP_ADDR_NOHB;
4414
62.7k
  }
4415
62.7k
  if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DO_NOT_PMTUD)) {
4416
0
    net->dest_state |= SCTP_ADDR_NO_PMTUD;
4417
62.7k
  } else {
4418
62.7k
    net->dest_state &= ~SCTP_ADDR_NO_PMTUD;
4419
62.7k
  }
4420
62.7k
  net->heart_beat_delay = stcb->asoc.heart_beat_delay;
4421
  /* Init the timer structure */
4422
62.7k
  SCTP_OS_TIMER_INIT(&net->rxt_timer.timer);
4423
62.7k
  SCTP_OS_TIMER_INIT(&net->pmtu_timer.timer);
4424
62.7k
  SCTP_OS_TIMER_INIT(&net->hb_timer.timer);
4425
4426
  /* Now generate a route for this guy */
4427
62.7k
#ifdef INET6
4428
#ifdef SCTP_EMBEDDED_V6_SCOPE
4429
  /* KAME hack: embed scopeid */
4430
  if (newaddr->sa_family == AF_INET6) {
4431
    struct sockaddr_in6 *sin6;
4432
4433
    sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4434
#if defined(__APPLE__) && !defined(__Userspace__)
4435
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4436
    (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL);
4437
#else
4438
    (void)in6_embedscope(&sin6->sin6_addr, sin6, &stcb->sctp_ep->ip_inp.inp, NULL, NULL);
4439
#endif
4440
#elif defined(SCTP_KAME)
4441
    (void)sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone));
4442
#else
4443
    (void)in6_embedscope(&sin6->sin6_addr, sin6);
4444
#endif
4445
#ifndef SCOPEDROUTING
4446
    sin6->sin6_scope_id = 0;
4447
#endif
4448
  }
4449
#endif /* SCTP_EMBEDDED_V6_SCOPE */
4450
62.7k
#endif
4451
62.7k
  SCTP_RTALLOC((sctp_route_t *)&net->ro,
4452
62.7k
               stcb->asoc.vrf_id,
4453
62.7k
               stcb->sctp_ep->fibnum);
4454
4455
62.7k
  net->src_addr_selected = 0;
4456
#if !defined(__Userspace__)
4457
  if (SCTP_ROUTE_HAS_VALID_IFN(&net->ro)) {
4458
    /* Get source address */
4459
    net->ro._s_addr = sctp_source_address_selection(stcb->sctp_ep,
4460
                                                    stcb,
4461
                                                    (sctp_route_t *)&net->ro,
4462
                                                    net,
4463
                                                    0,
4464
                                                    stcb->asoc.vrf_id);
4465
    if (stcb->asoc.default_mtu > 0) {
4466
      net->mtu = stcb->asoc.default_mtu;
4467
      switch (net->ro._l_addr.sa.sa_family) {
4468
#ifdef INET
4469
      case AF_INET:
4470
        net->mtu += SCTP_MIN_V4_OVERHEAD;
4471
        break;
4472
#endif
4473
#ifdef INET6
4474
      case AF_INET6:
4475
        net->mtu += SCTP_MIN_OVERHEAD;
4476
        break;
4477
#endif
4478
#if defined(__Userspace__)
4479
      case AF_CONN:
4480
        net->mtu += sizeof(struct sctphdr);
4481
        break;
4482
#endif
4483
      default:
4484
        break;
4485
      }
4486
#if defined(INET) || defined(INET6)
4487
      if (net->port) {
4488
        net->mtu += (uint32_t)sizeof(struct udphdr);
4489
      }
4490
#endif
4491
    } else if (net->ro._s_addr != NULL) {
4492
      uint32_t imtu, rmtu, hcmtu;
4493
4494
      net->src_addr_selected = 1;
4495
      /* Now get the interface MTU */
4496
      if (net->ro._s_addr->ifn_p != NULL) {
4497
        /*
4498
         * XXX: Should we here just use
4499
         * net->ro._s_addr->ifn_p->ifn_mtu
4500
         */
4501
        imtu = SCTP_GATHER_MTU_FROM_IFN_INFO(net->ro._s_addr->ifn_p->ifn_p,
4502
                                             net->ro._s_addr->ifn_p->ifn_index);
4503
      } else {
4504
        imtu = 0;
4505
      }
4506
#if defined(__FreeBSD__) && !defined(__Userspace__)
4507
      rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_nh);
4508
      hcmtu = sctp_hc_get_mtu(&net->ro._l_addr, stcb->sctp_ep->fibnum);
4509
#else
4510
      rmtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, net->ro.ro_rt);
4511
      hcmtu = 0;
4512
#endif
4513
      net->mtu = sctp_min_mtu(hcmtu, rmtu, imtu);
4514
#if defined(__FreeBSD__) && !defined(__Userspace__)
4515
#else
4516
      if (rmtu == 0) {
4517
        /* Start things off to match mtu of interface please. */
4518
        SCTP_SET_MTU_OF_ROUTE(&net->ro._l_addr.sa,
4519
                              net->ro.ro_rt, net->mtu);
4520
      }
4521
#endif
4522
    }
4523
  }
4524
#endif
4525
62.7k
  if (net->mtu == 0) {
4526
62.7k
    if (stcb->asoc.default_mtu > 0) {
4527
0
      net->mtu = stcb->asoc.default_mtu;
4528
0
      switch (net->ro._l_addr.sa.sa_family) {
4529
0
#ifdef INET
4530
0
      case AF_INET:
4531
0
        net->mtu += SCTP_MIN_V4_OVERHEAD;
4532
0
        break;
4533
0
#endif
4534
0
#ifdef INET6
4535
0
      case AF_INET6:
4536
0
        net->mtu += SCTP_MIN_OVERHEAD;
4537
0
        break;
4538
0
#endif
4539
0
#if defined(__Userspace__)
4540
0
      case AF_CONN:
4541
0
        net->mtu += sizeof(struct sctphdr);
4542
0
        break;
4543
0
#endif
4544
0
      default:
4545
0
        break;
4546
0
      }
4547
0
#if defined(INET) || defined(INET6)
4548
0
      if (net->port) {
4549
0
        net->mtu += (uint32_t)sizeof(struct udphdr);
4550
0
      }
4551
0
#endif
4552
62.7k
    } else {
4553
62.7k
      switch (newaddr->sa_family) {
4554
0
#ifdef INET
4555
44.8k
      case AF_INET:
4556
44.8k
        net->mtu = SCTP_DEFAULT_MTU;
4557
44.8k
        break;
4558
0
#endif
4559
0
#ifdef INET6
4560
5.02k
      case AF_INET6:
4561
5.02k
        net->mtu = 1280;
4562
5.02k
        break;
4563
0
#endif
4564
0
#if defined(__Userspace__)
4565
12.8k
      case AF_CONN:
4566
12.8k
        net->mtu = 1280;
4567
12.8k
        break;
4568
0
#endif
4569
0
      default:
4570
0
        break;
4571
62.7k
      }
4572
62.7k
    }
4573
62.7k
  }
4574
62.7k
#if defined(INET) || defined(INET6)
4575
62.7k
  if (net->port) {
4576
0
    net->mtu -= (uint32_t)sizeof(struct udphdr);
4577
0
  }
4578
62.7k
#endif
4579
62.7k
  if (from == SCTP_ALLOC_ASOC) {
4580
12.8k
    stcb->asoc.smallest_mtu = net->mtu;
4581
12.8k
  }
4582
62.7k
  if (stcb->asoc.smallest_mtu > net->mtu) {
4583
0
    sctp_pathmtu_adjustment(stcb, net->mtu, true);
4584
0
  }
4585
62.7k
#ifdef INET6
4586
#ifdef SCTP_EMBEDDED_V6_SCOPE
4587
  if (newaddr->sa_family == AF_INET6) {
4588
    struct sockaddr_in6 *sin6;
4589
4590
    sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4591
#ifdef SCTP_KAME
4592
    (void)sa6_recoverscope(sin6);
4593
#else
4594
    (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
4595
#endif /* SCTP_KAME */
4596
  }
4597
#endif /* SCTP_EMBEDDED_V6_SCOPE */
4598
62.7k
#endif
4599
4600
  /* JRS - Use the congestion control given in the CC module */
4601
62.7k
  if (stcb->asoc.cc_functions.sctp_set_initial_cc_param != NULL)
4602
62.7k
    (*stcb->asoc.cc_functions.sctp_set_initial_cc_param)(stcb, net);
4603
4604
  /*
4605
   * CMT: CUC algo - set find_pseudo_cumack to TRUE (1) at beginning
4606
   * of assoc (2005/06/27, iyengar@cis.udel.edu)
4607
   */
4608
62.7k
  net->find_pseudo_cumack = 1;
4609
62.7k
  net->find_rtx_pseudo_cumack = 1;
4610
#if defined(__FreeBSD__) && !defined(__Userspace__)
4611
  /* Choose an initial flowid. */
4612
  net->flowid = stcb->asoc.my_vtag ^
4613
                ntohs(stcb->rport) ^
4614
                ntohs(stcb->sctp_ep->sctp_lport);
4615
  net->flowtype = M_HASHTYPE_OPAQUE_HASH;
4616
#endif
4617
62.7k
  if (netp) {
4618
49.8k
    *netp = net;
4619
49.8k
  }
4620
62.7k
  netfirst = TAILQ_FIRST(&stcb->asoc.nets);
4621
#if defined(__FreeBSD__) && !defined(__Userspace__)
4622
  if (net->ro.ro_nh == NULL) {
4623
#else
4624
62.7k
  if (net->ro.ro_rt == NULL) {
4625
0
#endif
4626
    /* Since we have no route put it at the back */
4627
0
    TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4628
62.7k
  } else if (netfirst == NULL) {
4629
    /* We are the first one in the pool. */
4630
12.8k
    TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4631
#if defined(__FreeBSD__) && !defined(__Userspace__)
4632
  } else if (netfirst->ro.ro_nh == NULL) {
4633
#else
4634
49.8k
  } else if (netfirst->ro.ro_rt == NULL) {
4635
0
#endif
4636
    /*
4637
     * First one has NO route. Place this one ahead of the first
4638
     * one.
4639
     */
4640
0
    TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4641
#if defined(__FreeBSD__) && !defined(__Userspace__)
4642
  } else if (net->ro.ro_nh->nh_ifp != netfirst->ro.ro_nh->nh_ifp) {
4643
#else
4644
49.8k
  } else if (net->ro.ro_rt->rt_ifp != netfirst->ro.ro_rt->rt_ifp) {
4645
0
#endif
4646
    /*
4647
     * This one has a different interface than the one at the
4648
     * top of the list. Place it ahead.
4649
     */
4650
0
    TAILQ_INSERT_HEAD(&stcb->asoc.nets, net, sctp_next);
4651
49.8k
  } else {
4652
    /*
4653
     * Ok we have the same interface as the first one. Move
4654
     * forward until we find either a) one with a NULL route...
4655
     * insert ahead of that b) one with a different ifp.. insert
4656
     * after that. c) end of the list.. insert at the tail.
4657
     */
4658
49.8k
    struct sctp_nets *netlook;
4659
4660
238k
    do {
4661
238k
      netlook = TAILQ_NEXT(netfirst, sctp_next);
4662
238k
      if (netlook == NULL) {
4663
        /* End of the list */
4664
49.8k
        TAILQ_INSERT_TAIL(&stcb->asoc.nets, net, sctp_next);
4665
49.8k
        break;
4666
#if defined(__FreeBSD__) && !defined(__Userspace__)
4667
      } else if (netlook->ro.ro_nh == NULL) {
4668
#else
4669
188k
      } else if (netlook->ro.ro_rt == NULL) {
4670
0
#endif
4671
        /* next one has NO route */
4672
0
        TAILQ_INSERT_BEFORE(netfirst, net, sctp_next);
4673
0
        break;
4674
#if defined(__FreeBSD__) && !defined(__Userspace__)
4675
      } else if (netlook->ro.ro_nh->nh_ifp != net->ro.ro_nh->nh_ifp) {
4676
#else
4677
188k
      } else if (netlook->ro.ro_rt->rt_ifp != net->ro.ro_rt->rt_ifp) {
4678
0
#endif
4679
0
        TAILQ_INSERT_AFTER(&stcb->asoc.nets, netlook,
4680
0
                           net, sctp_next);
4681
0
        break;
4682
0
      }
4683
      /* Shift forward */
4684
188k
      netfirst = netlook;
4685
188k
    } while (netlook != NULL);
4686
49.8k
  }
4687
4688
  /* got to have a primary set */
4689
62.7k
  if (stcb->asoc.primary_destination == 0) {
4690
12.8k
    stcb->asoc.primary_destination = net;
4691
#if defined(__FreeBSD__) && !defined(__Userspace__)
4692
  } else if ((stcb->asoc.primary_destination->ro.ro_nh == NULL) &&
4693
             (net->ro.ro_nh) &&
4694
#else
4695
49.8k
  } else if ((stcb->asoc.primary_destination->ro.ro_rt == NULL) &&
4696
49.8k
             (net->ro.ro_rt) &&
4697
49.8k
#endif
4698
49.8k
             ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
4699
    /* No route to current primary adopt new primary */
4700
0
    stcb->asoc.primary_destination = net;
4701
0
  }
4702
  /* Validate primary is first */
4703
62.7k
  net = TAILQ_FIRST(&stcb->asoc.nets);
4704
62.7k
  if ((net != stcb->asoc.primary_destination) &&
4705
62.7k
      (stcb->asoc.primary_destination)) {
4706
    /* first one on the list is NOT the primary
4707
     * sctp_cmpaddr() is much more efficient if
4708
     * the primary is the first on the list, make it
4709
     * so.
4710
     */
4711
68
    TAILQ_REMOVE(&stcb->asoc.nets,
4712
68
           stcb->asoc.primary_destination, sctp_next);
4713
68
    TAILQ_INSERT_HEAD(&stcb->asoc.nets,
4714
68
          stcb->asoc.primary_destination, sctp_next);
4715
68
  }
4716
62.7k
  return (0);
4717
62.7k
}
4718
4719
static uint32_t
4720
sctp_aloc_a_assoc_id(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
4721
12.8k
{
4722
12.8k
  uint32_t id;
4723
12.8k
  struct sctpasochead *head;
4724
12.8k
  struct sctp_tcb *lstcb;
4725
4726
12.8k
 try_again:
4727
12.8k
  if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
4728
    /* TSNH */
4729
0
    return (0);
4730
0
  }
4731
  /*
4732
   * We don't allow assoc id to be one of SCTP_FUTURE_ASSOC,
4733
   * SCTP_CURRENT_ASSOC and SCTP_ALL_ASSOC.
4734
   */
4735
12.8k
  if (inp->sctp_associd_counter <= SCTP_ALL_ASSOC) {
4736
12.8k
    inp->sctp_associd_counter = SCTP_ALL_ASSOC + 1;
4737
12.8k
  }
4738
12.8k
  id = inp->sctp_associd_counter;
4739
12.8k
  inp->sctp_associd_counter++;
4740
12.8k
  lstcb = sctp_findasoc_ep_asocid_locked(inp, (sctp_assoc_t)id, 0);
4741
12.8k
  if (lstcb) {
4742
0
    goto try_again;
4743
0
  }
4744
12.8k
  head = &inp->sctp_asocidhash[SCTP_PCBHASH_ASOC(id, inp->hashasocidmark)];
4745
12.8k
  LIST_INSERT_HEAD(head, stcb, sctp_tcbasocidhash);
4746
12.8k
  stcb->asoc.in_asocid_hash = 1;
4747
12.8k
  return (id);
4748
12.8k
}
4749
4750
/*
4751
 * allocate an association and add it to the endpoint. The caller must be
4752
 * careful to add all additional addresses once they are know right away or
4753
 * else the assoc will be may experience a blackout scenario.
4754
 */
4755
static struct sctp_tcb *
4756
sctp_aloc_assoc_locked(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
4757
                       int *error, uint32_t override_tag, uint32_t initial_tsn,
4758
                       uint32_t vrf_id, uint16_t o_streams, uint16_t port,
4759
#if defined(__FreeBSD__) && !defined(__Userspace__)
4760
                       struct thread *p,
4761
#elif defined(_WIN32) && !defined(__Userspace__)
4762
                       PKTHREAD p,
4763
#else
4764
#if defined(__Userspace__)
4765
                       /*  __Userspace__ NULL proc is going to be passed here. See sctp_lower_sosend */
4766
#endif
4767
                       struct proc *p,
4768
#endif
4769
                       int initialize_auth_params)
4770
12.8k
{
4771
  /* note the p argument is only valid in unbound sockets */
4772
4773
12.8k
  struct sctp_tcb *stcb;
4774
12.8k
  struct sctp_association *asoc;
4775
12.8k
  struct sctpasochead *head;
4776
12.8k
  uint16_t rport;
4777
12.8k
  int err;
4778
4779
12.8k
  SCTP_INP_INFO_WLOCK_ASSERT();
4780
12.8k
  SCTP_INP_WLOCK_ASSERT(inp);
4781
4782
  /*
4783
   * Assumption made here: Caller has done a
4784
   * sctp_findassociation_ep_addr(ep, addr's); to make sure the
4785
   * address does not exist already.
4786
   */
4787
12.8k
  if (SCTP_BASE_INFO(ipi_count_asoc) >= SCTP_MAX_NUM_OF_ASOC) {
4788
    /* Hit max assoc, sorry no more */
4789
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4790
0
    *error = ENOBUFS;
4791
0
    return (NULL);
4792
0
  }
4793
12.8k
  if (firstaddr == NULL) {
4794
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4795
0
    *error = EINVAL;
4796
0
    return (NULL);
4797
0
  }
4798
12.8k
  if (inp->sctp_flags & (SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
4799
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4800
0
    *error = EINVAL;
4801
0
    return (NULL);
4802
0
  }
4803
12.8k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) &&
4804
12.8k
      ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_PORTREUSE)) ||
4805
0
       (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4806
    /*
4807
     * If its in the TCP pool, its NOT allowed to create an
4808
     * association. The parent listener needs to call
4809
     * sctp_aloc_assoc.. or the one-2-many socket. If a peeled
4810
     * off, or connected one does this.. its an error.
4811
     */
4812
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4813
0
    *error = EINVAL;
4814
0
    return (NULL);
4815
0
  }
4816
12.8k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4817
12.8k
      (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
4818
12.8k
    if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) ||
4819
12.8k
        (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED)) {
4820
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4821
0
      *error = EINVAL;
4822
0
      return (NULL);
4823
0
    }
4824
12.8k
  }
4825
12.8k
  SCTPDBG(SCTP_DEBUG_PCB3, "Allocate an association for peer:");
4826
#ifdef SCTP_DEBUG
4827
  if (firstaddr) {
4828
    SCTPDBG_ADDR(SCTP_DEBUG_PCB3, firstaddr);
4829
    switch (firstaddr->sa_family) {
4830
#ifdef INET
4831
    case AF_INET:
4832
      SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4833
              ntohs(((struct sockaddr_in *)firstaddr)->sin_port));
4834
      break;
4835
#endif
4836
#ifdef INET6
4837
    case AF_INET6:
4838
      SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4839
              ntohs(((struct sockaddr_in6 *)firstaddr)->sin6_port));
4840
      break;
4841
#endif
4842
#if defined(__Userspace__)
4843
    case AF_CONN:
4844
      SCTPDBG(SCTP_DEBUG_PCB3, "Port:%d\n",
4845
              ntohs(((struct sockaddr_conn *)firstaddr)->sconn_port));
4846
      break;
4847
#endif
4848
    default:
4849
      break;
4850
    }
4851
  } else {
4852
    SCTPDBG(SCTP_DEBUG_PCB3,"None\n");
4853
  }
4854
#endif        /* SCTP_DEBUG */
4855
12.8k
  switch (firstaddr->sa_family) {
4856
0
#ifdef INET
4857
0
  case AF_INET:
4858
0
  {
4859
0
    struct sockaddr_in *sin;
4860
4861
0
    sin = (struct sockaddr_in *)firstaddr;
4862
0
    if ((ntohs(sin->sin_port) == 0) ||
4863
0
        in_broadcast(sin->sin_addr) ||
4864
0
        IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) ||
4865
0
#if defined(__Userspace__)
4866
0
        ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) ||
4867
0
         ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
4868
0
          (SCTP_IPV6_V6ONLY(inp) != 0)))) {
4869
#else
4870
        ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
4871
         (SCTP_IPV6_V6ONLY(inp) != 0))) {
4872
#endif
4873
      /* Invalid address */
4874
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4875
0
      *error = EINVAL;
4876
0
      return (NULL);
4877
0
    }
4878
0
    rport = sin->sin_port;
4879
0
    break;
4880
0
  }
4881
0
#endif
4882
0
#ifdef INET6
4883
0
  case AF_INET6:
4884
0
  {
4885
0
    struct sockaddr_in6 *sin6;
4886
4887
0
    sin6 = (struct sockaddr_in6 *)firstaddr;
4888
0
    if ((ntohs(sin6->sin6_port) == 0) ||
4889
0
        IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
4890
0
        IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) ||
4891
0
        ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0)) {
4892
      /* Invalid address */
4893
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4894
0
      *error = EINVAL;
4895
0
      return (NULL);
4896
0
    }
4897
0
    rport = sin6->sin6_port;
4898
0
    break;
4899
0
  }
4900
0
#endif
4901
0
#if defined(__Userspace__)
4902
12.8k
  case AF_CONN:
4903
12.8k
  {
4904
12.8k
    struct sockaddr_conn *sconn;
4905
4906
12.8k
    sconn = (struct sockaddr_conn *)firstaddr;
4907
12.8k
    if ((ntohs(sconn->sconn_port) == 0) ||
4908
12.8k
        (sconn->sconn_addr == NULL) ||
4909
12.8k
        ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) == 0)) {
4910
      /* Invalid address */
4911
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4912
0
      *error = EINVAL;
4913
0
      return (NULL);
4914
0
    }
4915
12.8k
    rport = sconn->sconn_port;
4916
12.8k
    break;
4917
12.8k
  }
4918
0
#endif
4919
0
  default:
4920
    /* not supported family type */
4921
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, EINVAL);
4922
0
    *error = EINVAL;
4923
0
    return (NULL);
4924
12.8k
  }
4925
12.8k
  if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4926
    /*
4927
     * If you have not performed a bind, then we need to do the
4928
     * ephemeral bind for you.
4929
     */
4930
0
    if ((err = sctp_inpcb_bind_locked(inp, NULL, NULL, p))) {
4931
      /* bind error, probably perm */
4932
0
      *error = err;
4933
0
      return (NULL);
4934
0
    }
4935
0
  }
4936
12.8k
  stcb = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_asoc), struct sctp_tcb);
4937
12.8k
  if (stcb == NULL) {
4938
    /* out of memory? */
4939
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
4940
0
    *error = ENOMEM;
4941
0
    return (NULL);
4942
0
  }
4943
12.8k
  SCTP_INCR_ASOC_COUNT();
4944
4945
12.8k
  memset(stcb, 0, sizeof(*stcb));
4946
12.8k
  asoc = &stcb->asoc;
4947
4948
12.8k
  SCTP_TCB_LOCK_INIT(stcb);
4949
12.8k
  stcb->rport = rport;
4950
  /* setup back pointer's */
4951
12.8k
  stcb->sctp_ep = inp;
4952
12.8k
  stcb->sctp_socket = inp->sctp_socket;
4953
12.8k
  if ((err = sctp_init_asoc(inp, stcb, override_tag, initial_tsn, vrf_id, o_streams))) {
4954
    /* failed */
4955
0
    SCTP_TCB_LOCK_DESTROY(stcb);
4956
0
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4957
0
    SCTP_DECR_ASOC_COUNT();
4958
0
    *error = err;
4959
0
    return (NULL);
4960
0
  }
4961
12.8k
  SCTP_TCB_LOCK(stcb);
4962
4963
12.8k
  asoc->assoc_id = sctp_aloc_a_assoc_id(inp, stcb);
4964
  /* now that my_vtag is set, add it to the hash */
4965
12.8k
  head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(stcb->asoc.my_vtag, SCTP_BASE_INFO(hashasocmark))];
4966
  /* put it in the bucket in the vtag hash of assoc's for the system */
4967
12.8k
  LIST_INSERT_HEAD(head, stcb, sctp_asocs);
4968
4969
12.8k
  if (sctp_add_remote_addr(stcb, firstaddr, NULL, port, SCTP_DO_SETSCOPE, SCTP_ALLOC_ASOC)) {
4970
    /* failure.. memory error? */
4971
0
    if (asoc->strmout) {
4972
0
      SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
4973
0
      asoc->strmout = NULL;
4974
0
    }
4975
0
    if (asoc->mapping_array) {
4976
0
      SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
4977
0
      asoc->mapping_array = NULL;
4978
0
    }
4979
0
    if (asoc->nr_mapping_array) {
4980
0
      SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
4981
0
      asoc->nr_mapping_array = NULL;
4982
0
    }
4983
0
    SCTP_DECR_ASOC_COUNT();
4984
0
    SCTP_TCB_UNLOCK(stcb);
4985
0
    SCTP_TCB_LOCK_DESTROY(stcb);
4986
0
    LIST_REMOVE(stcb, sctp_asocs);
4987
0
    LIST_REMOVE(stcb, sctp_tcbasocidhash);
4988
0
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
4989
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOBUFS);
4990
0
    *error = ENOBUFS;
4991
0
    return (NULL);
4992
0
  }
4993
  /* Init all the timers */
4994
12.8k
  SCTP_OS_TIMER_INIT(&asoc->dack_timer.timer);
4995
12.8k
  SCTP_OS_TIMER_INIT(&asoc->strreset_timer.timer);
4996
12.8k
  SCTP_OS_TIMER_INIT(&asoc->asconf_timer.timer);
4997
12.8k
  SCTP_OS_TIMER_INIT(&asoc->shut_guard_timer.timer);
4998
12.8k
  SCTP_OS_TIMER_INIT(&asoc->autoclose_timer.timer);
4999
12.8k
  SCTP_OS_TIMER_INIT(&asoc->delete_prim_timer.timer);
5000
5001
12.8k
  LIST_INSERT_HEAD(&inp->sctp_asoc_list, stcb, sctp_tcblist);
5002
  /* now file the port under the hash as well */
5003
12.8k
  if (inp->sctp_tcbhash != NULL) {
5004
12.8k
    head = &inp->sctp_tcbhash[SCTP_PCBHASH_ALLADDR(stcb->rport,
5005
12.8k
        inp->sctp_hashmark)];
5006
12.8k
    LIST_INSERT_HEAD(head, stcb, sctp_tcbhash);
5007
12.8k
  }
5008
12.8k
  if (initialize_auth_params == SCTP_INITIALIZE_AUTH_PARAMS) {
5009
12.8k
    sctp_initialize_auth_params(inp, stcb);
5010
12.8k
  }
5011
12.8k
  SCTPDBG(SCTP_DEBUG_PCB1, "Association %p now allocated\n", (void *)stcb);
5012
12.8k
  return (stcb);
5013
12.8k
}
5014
5015
struct sctp_tcb *
5016
sctp_aloc_assoc(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
5017
                int *error, uint32_t override_tag, uint32_t initial_tsn,
5018
                uint32_t vrf_id, uint16_t o_streams, uint16_t port,
5019
#if defined(__FreeBSD__) && !defined(__Userspace__)
5020
                struct thread *p,
5021
#elif defined(_WIN32) && !defined(__Userspace__)
5022
                PKTHREAD p,
5023
#else
5024
                struct proc *p,
5025
#endif
5026
                int initialize_auth_params)
5027
0
{
5028
0
  struct sctp_tcb *stcb;
5029
5030
0
  SCTP_INP_INFO_WLOCK();
5031
0
  SCTP_INP_WLOCK(inp);
5032
0
  stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
5033
0
      initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
5034
0
  SCTP_INP_INFO_WUNLOCK();
5035
0
  SCTP_INP_WUNLOCK(inp);
5036
0
  return (stcb);
5037
0
}
5038
5039
struct sctp_tcb *
5040
sctp_aloc_assoc_connected(struct sctp_inpcb *inp, struct sockaddr *firstaddr,
5041
                          int *error, uint32_t override_tag, uint32_t initial_tsn,
5042
                          uint32_t vrf_id, uint16_t o_streams, uint16_t port,
5043
#if defined(__FreeBSD__) && !defined(__Userspace__)
5044
                          struct thread *p,
5045
#elif defined(_WIN32) && !defined(__Userspace__)
5046
                          PKTHREAD p,
5047
#else
5048
                          struct proc *p,
5049
#endif
5050
                          int initialize_auth_params)
5051
12.8k
{
5052
12.8k
  struct sctp_tcb *stcb;
5053
5054
12.8k
  SCTP_INP_INFO_WLOCK();
5055
12.8k
  SCTP_INP_WLOCK(inp);
5056
12.8k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
5057
12.8k
      SCTP_IS_LISTENING(inp)) {
5058
0
    SCTP_INP_INFO_WUNLOCK();
5059
0
    SCTP_INP_WUNLOCK(inp);
5060
0
    *error = EINVAL;
5061
0
    return (NULL);
5062
0
  }
5063
12.8k
  stcb = sctp_aloc_assoc_locked(inp, firstaddr, error, override_tag,
5064
12.8k
      initial_tsn, vrf_id, o_streams, port, p, initialize_auth_params);
5065
12.8k
  SCTP_INP_INFO_WUNLOCK();
5066
12.8k
  if (stcb != NULL && (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)) {
5067
12.8k
    inp->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
5068
12.8k
    soisconnecting(inp->sctp_socket);
5069
12.8k
  }
5070
12.8k
  SCTP_INP_WUNLOCK(inp);
5071
12.8k
  return (stcb);
5072
12.8k
}
5073
5074
void
5075
sctp_remove_net(struct sctp_tcb *stcb, struct sctp_nets *net)
5076
44.6k
{
5077
44.6k
  struct sctp_inpcb *inp;
5078
44.6k
  struct sctp_association *asoc;
5079
5080
44.6k
  inp = stcb->sctp_ep;
5081
44.6k
  asoc = &stcb->asoc;
5082
44.6k
  asoc->numnets--;
5083
44.6k
  TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5084
44.6k
  if (net == asoc->primary_destination) {
5085
    /* Reset primary */
5086
673
    struct sctp_nets *lnet;
5087
5088
673
    lnet = TAILQ_FIRST(&asoc->nets);
5089
    /* Mobility adaptation
5090
       Ideally, if deleted destination is the primary, it becomes
5091
       a fast retransmission trigger by the subsequent SET PRIMARY.
5092
       (by micchie)
5093
     */
5094
673
    if (sctp_is_mobility_feature_on(stcb->sctp_ep,
5095
673
                                    SCTP_MOBILITY_BASE) ||
5096
673
        sctp_is_mobility_feature_on(stcb->sctp_ep,
5097
673
                                    SCTP_MOBILITY_FASTHANDOFF)) {
5098
0
      SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: primary dst is deleting\n");
5099
0
      if (asoc->deleted_primary != NULL) {
5100
0
        SCTPDBG(SCTP_DEBUG_ASCONF1, "remove_net: deleted primary may be already stored\n");
5101
0
        goto out;
5102
0
      }
5103
0
      asoc->deleted_primary = net;
5104
0
      atomic_add_int(&net->ref_count, 1);
5105
0
      memset(&net->lastsa, 0, sizeof(net->lastsa));
5106
0
      memset(&net->lastsv, 0, sizeof(net->lastsv));
5107
0
      sctp_mobility_feature_on(stcb->sctp_ep,
5108
0
             SCTP_MOBILITY_PRIM_DELETED);
5109
0
      sctp_timer_start(SCTP_TIMER_TYPE_PRIM_DELETED,
5110
0
           stcb->sctp_ep, stcb, NULL);
5111
0
    }
5112
673
out:
5113
    /* Try to find a confirmed primary */
5114
673
    asoc->primary_destination = sctp_find_alternate_net(stcb, lnet, 0);
5115
673
  }
5116
44.6k
  if (net == asoc->last_data_chunk_from) {
5117
    /* Reset primary */
5118
0
    asoc->last_data_chunk_from = TAILQ_FIRST(&asoc->nets);
5119
0
  }
5120
44.6k
  if (net == asoc->last_control_chunk_from) {
5121
    /* Clear net */
5122
0
    asoc->last_control_chunk_from = NULL;
5123
0
  }
5124
44.6k
  if (net == asoc->last_net_cmt_send_started) {
5125
    /* Clear net */
5126
0
    asoc->last_net_cmt_send_started = NULL;
5127
0
  }
5128
44.6k
  if (net == stcb->asoc.alternate) {
5129
0
    sctp_free_remote_addr(stcb->asoc.alternate);
5130
0
    stcb->asoc.alternate = NULL;
5131
0
  }
5132
44.6k
  sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
5133
44.6k
                  SCTP_FROM_SCTP_PCB + SCTP_LOC_9);
5134
44.6k
  sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
5135
44.6k
                  SCTP_FROM_SCTP_PCB + SCTP_LOC_10);
5136
44.6k
  net->dest_state |= SCTP_ADDR_BEING_DELETED;
5137
44.6k
  sctp_free_remote_addr(net);
5138
44.6k
}
5139
5140
/*
5141
 * remove a remote endpoint address from an association, it will fail if the
5142
 * address does not exist.
5143
 */
5144
int
5145
sctp_del_remote_addr(struct sctp_tcb *stcb, struct sockaddr *remaddr)
5146
9.41k
{
5147
  /*
5148
   * Here we need to remove a remote address. This is quite simple, we
5149
   * first find it in the list of address for the association
5150
   * (tasoc->asoc.nets) and then if it is there, we do a LIST_REMOVE
5151
   * on that item. Note we do not allow it to be removed if there are
5152
   * no other addresses.
5153
   */
5154
9.41k
  struct sctp_association *asoc;
5155
9.41k
  struct sctp_nets *net, *nnet;
5156
5157
9.41k
  asoc = &stcb->asoc;
5158
5159
  /* locate the address */
5160
46.3k
  TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5161
46.3k
    if (net->ro._l_addr.sa.sa_family != remaddr->sa_family) {
5162
12.5k
      continue;
5163
12.5k
    }
5164
33.7k
    if (sctp_cmpaddr((struct sockaddr *)&net->ro._l_addr,
5165
33.7k
        remaddr)) {
5166
      /* we found the guy */
5167
1.44k
      if (asoc->numnets < 2) {
5168
        /* Must have at LEAST two remote addresses */
5169
0
        return (-1);
5170
1.44k
      } else {
5171
1.44k
        sctp_remove_net(stcb, net);
5172
1.44k
        return (0);
5173
1.44k
      }
5174
1.44k
    }
5175
33.7k
  }
5176
  /* not found. */
5177
7.96k
  return (-2);
5178
9.41k
}
5179
5180
static bool
5181
sctp_is_in_timewait(uint32_t tag, uint16_t lport, uint16_t rport, time_t now)
5182
35.1k
{
5183
35.1k
  struct sctpvtaghead *chain;
5184
35.1k
  struct sctp_tagblock *twait_block;
5185
35.1k
  int i;
5186
5187
35.1k
  SCTP_INP_INFO_LOCK_ASSERT();
5188
35.1k
  chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5189
632k
  LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5190
10.1M
    for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5191
9.48M
      if ((twait_block->vtag_block[i].tv_sec_at_expire >= now) &&
5192
9.48M
          (twait_block->vtag_block[i].v_tag == tag) &&
5193
9.48M
          (twait_block->vtag_block[i].lport == lport) &&
5194
9.48M
          (twait_block->vtag_block[i].rport == rport)) {
5195
0
        return (true);
5196
0
      }
5197
9.48M
    }
5198
632k
  }
5199
35.1k
  return (false);
5200
35.1k
}
5201
5202
static void
5203
sctp_set_vtag_block(struct sctp_timewait *vtag_block, time_t time,
5204
    uint32_t tag, uint16_t lport, uint16_t rport)
5205
12.8k
{
5206
12.8k
  vtag_block->tv_sec_at_expire = time;
5207
12.8k
  vtag_block->v_tag = tag;
5208
12.8k
  vtag_block->lport = lport;
5209
12.8k
  vtag_block->rport = rport;
5210
12.8k
}
5211
5212
static void
5213
sctp_add_vtag_to_timewait(uint32_t tag, uint16_t lport, uint16_t rport)
5214
12.8k
{
5215
12.8k
  struct sctpvtaghead *chain;
5216
12.8k
  struct sctp_tagblock *twait_block;
5217
12.8k
  struct timeval now;
5218
12.8k
  time_t time;
5219
12.8k
  int i;
5220
12.8k
  bool set;
5221
5222
12.8k
  SCTP_INP_INFO_WLOCK_ASSERT();
5223
12.8k
  (void)SCTP_GETTIME_TIMEVAL(&now);
5224
12.8k
  time = now.tv_sec + SCTP_BASE_SYSCTL(sctp_vtag_time_wait);
5225
12.8k
  chain = &SCTP_BASE_INFO(vtag_timewait)[(tag % SCTP_STACK_VTAG_HASH_SIZE)];
5226
12.8k
  set = false;
5227
20.9k
  LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
5228
    /* Block(s) present, lets find space, and expire on the fly */
5229
335k
    for (i = 0; i < SCTP_NUMBER_IN_VTAG_BLOCK; i++) {
5230
314k
      if ((twait_block->vtag_block[i].v_tag == 0) && !set) {
5231
11.9k
        sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
5232
11.9k
        set = true;
5233
11.9k
        continue;
5234
11.9k
      }
5235
302k
      if ((twait_block->vtag_block[i].v_tag != 0) &&
5236
302k
          (twait_block->vtag_block[i].tv_sec_at_expire < now.tv_sec)) {
5237
0
        if (set) {
5238
          /* Audit expires this guy */
5239
0
          sctp_set_vtag_block(twait_block->vtag_block + i, 0, 0, 0, 0);
5240
0
        } else {
5241
          /* Reuse it for the new tag */
5242
0
          sctp_set_vtag_block(twait_block->vtag_block + i, time, tag, lport, rport);
5243
0
          set = true;
5244
0
        }
5245
0
      }
5246
302k
    }
5247
20.9k
    if (set) {
5248
      /*
5249
       * We only do up to the block where we can
5250
       * place our tag for audits
5251
       */
5252
11.9k
      break;
5253
11.9k
    }
5254
20.9k
  }
5255
  /* Need to add a new block to chain */
5256
12.8k
  if (!set) {
5257
888
    SCTP_MALLOC(twait_block, struct sctp_tagblock *,
5258
888
        sizeof(struct sctp_tagblock), SCTP_M_TIMW);
5259
888
    if (twait_block == NULL) {
5260
0
      return;
5261
0
    }
5262
888
    memset(twait_block, 0, sizeof(struct sctp_tagblock));
5263
888
    LIST_INSERT_HEAD(chain, twait_block, sctp_nxt_tagblock);
5264
888
    sctp_set_vtag_block(twait_block->vtag_block, time, tag, lport, rport);
5265
888
  }
5266
12.8k
}
5267
5268
void
5269
sctp_clean_up_stream(struct sctp_tcb *stcb, struct sctp_readhead *rh)
5270
5.34M
{
5271
5.34M
  struct sctp_tmit_chunk *chk, *nchk;
5272
5.34M
  struct sctp_queued_to_read *control, *ncontrol;
5273
5274
5.34M
  TAILQ_FOREACH_SAFE(control, rh, next_instrm, ncontrol) {
5275
4.08k
    TAILQ_REMOVE(rh, control, next_instrm);
5276
4.08k
    control->on_strm_q = 0;
5277
4.08k
    if (control->on_read_q == 0) {
5278
4.08k
      sctp_free_remote_addr(control->whoFrom);
5279
4.08k
      if (control->data) {
5280
2.40k
        sctp_m_freem(control->data);
5281
2.40k
        control->data = NULL;
5282
2.40k
      }
5283
4.08k
    }
5284
    /* Reassembly free? */
5285
4.08k
    TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
5286
3.10k
      TAILQ_REMOVE(&control->reasm, chk, sctp_next);
5287
3.10k
      if (chk->data) {
5288
3.10k
        sctp_m_freem(chk->data);
5289
3.10k
        chk->data = NULL;
5290
3.10k
      }
5291
3.10k
      if (chk->holds_key_ref)
5292
0
        sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5293
3.10k
      sctp_free_remote_addr(chk->whoTo);
5294
3.10k
      SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5295
3.10k
      SCTP_DECR_CHK_COUNT();
5296
      /*sa_ignore FREED_MEMORY*/
5297
3.10k
    }
5298
    /*
5299
     * We don't free the address here
5300
     * since all the net's were freed
5301
     * above.
5302
     */
5303
4.08k
    if (control->on_read_q == 0) {
5304
4.08k
      sctp_free_a_readq(stcb, control);
5305
4.08k
    }
5306
4.08k
  }
5307
5.34M
}
5308
5309
/*-
5310
 * Free the association after un-hashing the remote port. This
5311
 * function ALWAYS returns holding NO LOCK on the stcb. It DOES
5312
 * expect that the input to this function IS a locked TCB.
5313
 * It will return 0, if it did NOT destroy the association (instead
5314
 * it unlocks it. It will return NON-zero if it either destroyed the
5315
 * association OR the association is already destroyed.
5316
 */
5317
int
5318
sctp_free_assoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int from_inpcbfree, int from_location)
5319
15.0k
{
5320
15.0k
  int i;
5321
15.0k
  struct sctp_association *asoc;
5322
15.0k
  struct sctp_nets *net, *nnet;
5323
15.0k
  struct sctp_laddr *laddr, *naddr;
5324
15.0k
  struct sctp_tmit_chunk *chk, *nchk;
5325
15.0k
  struct sctp_asconf_addr *aparam, *naparam;
5326
15.0k
  struct sctp_asconf_ack *aack, *naack;
5327
15.0k
  struct sctp_stream_reset_list *strrst, *nstrrst;
5328
15.0k
  struct sctp_queued_to_read *sq, *nsq;
5329
15.0k
  struct sctp_stream_queue_pending *sp, *nsp;
5330
15.0k
  sctp_sharedkey_t *shared_key, *nshared_key;
5331
15.0k
  struct socket *so;
5332
5333
  /* first, lets purge the entry from the hash table. */
5334
#if defined(__APPLE__) && !defined(__Userspace__)
5335
  sctp_lock_assert(SCTP_INP_SO(inp));
5336
#endif
5337
15.0k
  SCTP_TCB_LOCK_ASSERT(stcb);
5338
5339
#ifdef SCTP_LOG_CLOSING
5340
  sctp_log_closing(inp, stcb, 6);
5341
#endif
5342
15.0k
  if (stcb->asoc.state == 0) {
5343
#ifdef SCTP_LOG_CLOSING
5344
    sctp_log_closing(inp, NULL, 7);
5345
#endif
5346
    /* there is no asoc, really TSNH :-0 */
5347
0
    return (1);
5348
0
  }
5349
15.0k
  if (stcb->asoc.alternate) {
5350
101
    sctp_free_remote_addr(stcb->asoc.alternate);
5351
101
    stcb->asoc.alternate = NULL;
5352
101
  }
5353
15.0k
#if !(defined(__APPLE__) && !defined(__Userspace__))
5354
  /* TEMP CODE */
5355
15.0k
  if (stcb->freed_from_where == 0) {
5356
    /* Only record the first place free happened from */
5357
12.8k
    stcb->freed_from_where = from_location;
5358
12.8k
  }
5359
  /* TEMP CODE */
5360
15.0k
#endif
5361
5362
15.0k
  asoc = &stcb->asoc;
5363
15.0k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5364
15.0k
      (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5365
    /* nothing around */
5366
13.2k
    so = NULL;
5367
1.76k
  else
5368
1.76k
    so = inp->sctp_socket;
5369
5370
  /*
5371
   * We used timer based freeing if a reader or writer is in the way.
5372
   * So we first check if we are actually being called from a timer,
5373
   * if so we abort early if a reader or writer is still in the way.
5374
   */
5375
15.0k
  if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
5376
15.0k
      (from_inpcbfree == SCTP_NORMAL_PROC)) {
5377
    /*
5378
     * is it the timer driving us? if so are the reader/writers
5379
     * gone?
5380
     */
5381
2.19k
    if (stcb->asoc.refcnt) {
5382
      /* nope, reader or writer in the way */
5383
2.17k
      sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5384
      /* no asoc destroyed */
5385
2.17k
      SCTP_TCB_UNLOCK(stcb);
5386
#ifdef SCTP_LOG_CLOSING
5387
      sctp_log_closing(inp, stcb, 8);
5388
#endif
5389
2.17k
      return (0);
5390
2.17k
    }
5391
2.19k
  }
5392
  /* Now clean up any other timers */
5393
12.8k
  sctp_stop_association_timers(stcb, false);
5394
  /* Now the read queue needs to be cleaned up (only once) */
5395
12.8k
  if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0) {
5396
12.8k
    SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_ABOUT_TO_BE_FREED);
5397
12.8k
    SCTP_INP_READ_LOCK(inp);
5398
157k
    TAILQ_FOREACH(sq, &inp->read_queue, next) {
5399
157k
      if (sq->stcb == stcb) {
5400
157k
        sq->do_not_ref_stcb = 1;
5401
157k
        sq->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
5402
        /* If there is no end, there never
5403
         * will be now.
5404
         */
5405
157k
        if (sq->end_added == 0) {
5406
          /* Held for PD-API, clear that. */
5407
0
          sq->pdapi_aborted = 1;
5408
0
          sq->held_length = 0;
5409
0
          if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_PDAPIEVNT) && (so != NULL)) {
5410
0
            sctp_ulp_notify(SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION,
5411
0
                            stcb,
5412
0
                            SCTP_PARTIAL_DELIVERY_ABORTED,
5413
0
                            (void *)sq,
5414
0
                            SCTP_SO_LOCKED);
5415
0
          }
5416
          /* Add an end to wake them */
5417
0
          sq->end_added = 1;
5418
0
        }
5419
157k
      }
5420
157k
    }
5421
12.8k
    SCTP_INP_READ_UNLOCK(inp);
5422
12.8k
    if (stcb->block_entry) {
5423
0
      SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_PCB, ECONNRESET);
5424
0
      stcb->block_entry->error = ECONNRESET;
5425
0
      stcb->block_entry = NULL;
5426
0
    }
5427
12.8k
  }
5428
12.8k
  if ((stcb->asoc.refcnt) || (stcb->asoc.state & SCTP_STATE_IN_ACCEPT_QUEUE)) {
5429
    /* Someone holds a reference OR the socket is unaccepted yet.
5430
    */
5431
22
    if ((stcb->asoc.refcnt) ||
5432
22
        (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5433
22
        (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
5434
22
      SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
5435
22
      sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5436
22
    }
5437
22
    if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5438
22
        (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5439
      /* nothing around */
5440
5
      so = NULL;
5441
22
    if (so) {
5442
      /* Wake any reader/writers */
5443
17
      sctp_sorwakeup(inp, so);
5444
17
      sctp_sowwakeup(inp, so);
5445
17
    }
5446
22
    SCTP_TCB_UNLOCK(stcb);
5447
5448
#ifdef SCTP_LOG_CLOSING
5449
    sctp_log_closing(inp, stcb, 9);
5450
#endif
5451
    /* no asoc destroyed */
5452
22
    return (0);
5453
22
  }
5454
#ifdef SCTP_LOG_CLOSING
5455
  sctp_log_closing(inp, stcb, 10);
5456
#endif
5457
  /* When I reach here, no others want
5458
   * to kill the assoc yet.. and I own
5459
   * the lock. Now its possible an abort
5460
   * comes in when I do the lock exchange
5461
   * below to grab all the locks to do
5462
   * the final take out. to prevent this
5463
   * we increment the count, which will
5464
   * start a timer and blow out above thus
5465
   * assuring us that we hold exclusive
5466
   * killing of the asoc. Note that
5467
   * after getting back the TCB lock
5468
   * we will go ahead and increment the
5469
   * counter back up and stop any timer
5470
   * a passing stranger may have started :-S
5471
   */
5472
12.8k
  if (from_inpcbfree == SCTP_NORMAL_PROC) {
5473
1.77k
    atomic_add_int(&stcb->asoc.refcnt, 1);
5474
5475
1.77k
    SCTP_TCB_UNLOCK(stcb);
5476
1.77k
    SCTP_INP_INFO_WLOCK();
5477
1.77k
    SCTP_INP_WLOCK(inp);
5478
1.77k
    SCTP_TCB_LOCK(stcb);
5479
1.77k
  }
5480
  /* Double check the GONE flag */
5481
12.8k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
5482
12.8k
      (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE))
5483
    /* nothing around */
5484
11.1k
    so = NULL;
5485
5486
12.8k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
5487
12.8k
      (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
5488
    /*
5489
     * For TCP type we need special handling when we are
5490
     * connected. We also include the peel'ed off ones to.
5491
     */
5492
12.8k
    if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
5493
12.8k
      inp->sctp_flags &= ~SCTP_PCB_FLAGS_CONNECTED;
5494
12.8k
      inp->sctp_flags |= SCTP_PCB_FLAGS_WAS_CONNECTED;
5495
12.8k
      if (so) {
5496
1.75k
        SOCKBUF_LOCK(&so->so_rcv);
5497
1.75k
        so->so_state &= ~(SS_ISCONNECTING |
5498
1.75k
            SS_ISDISCONNECTING |
5499
1.75k
#if !(defined(__FreeBSD__) && !defined(__Userspace__))
5500
1.75k
            SS_ISCONFIRMING |
5501
1.75k
#endif
5502
1.75k
            SS_ISCONNECTED);
5503
1.75k
        so->so_state |= SS_ISDISCONNECTED;
5504
#if defined(__APPLE__) && !defined(__Userspace__)
5505
        socantrcvmore(so);
5506
#else
5507
1.75k
        socantrcvmore_locked(so);
5508
1.75k
#endif
5509
1.75k
        socantsendmore(so);
5510
1.75k
        sctp_sowwakeup(inp, so);
5511
1.75k
        sctp_sorwakeup(inp, so);
5512
1.75k
        SCTP_SOWAKEUP(so);
5513
1.75k
      }
5514
12.8k
    }
5515
12.8k
  }
5516
5517
  /* Make it invalid too, that way if its
5518
   * about to run it will abort and return.
5519
   */
5520
  /* re-increment the lock */
5521
12.8k
  if (from_inpcbfree == SCTP_NORMAL_PROC) {
5522
1.77k
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
5523
1.77k
  }
5524
12.8k
  if (stcb->asoc.refcnt) {
5525
0
    SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_IN_ACCEPT_QUEUE);
5526
0
    sctp_timer_start(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL);
5527
0
    if (from_inpcbfree == SCTP_NORMAL_PROC) {
5528
0
      SCTP_INP_INFO_WUNLOCK();
5529
0
      SCTP_INP_WUNLOCK(inp);
5530
0
    }
5531
0
    SCTP_TCB_UNLOCK(stcb);
5532
0
    return (0);
5533
0
  }
5534
12.8k
  asoc->state = 0;
5535
12.8k
  if (inp->sctp_tcbhash) {
5536
12.8k
    LIST_REMOVE(stcb, sctp_tcbhash);
5537
12.8k
  }
5538
12.8k
  if (stcb->asoc.in_asocid_hash) {
5539
12.8k
    LIST_REMOVE(stcb, sctp_tcbasocidhash);
5540
12.8k
  }
5541
12.8k
  if (inp->sctp_socket == NULL) {
5542
11.1k
    stcb->sctp_socket = NULL;
5543
11.1k
  }
5544
  /* Now lets remove it from the list of ALL associations in the EP */
5545
12.8k
  LIST_REMOVE(stcb, sctp_tcblist);
5546
12.8k
  if (from_inpcbfree == SCTP_NORMAL_PROC) {
5547
1.77k
    SCTP_INP_INCR_REF(inp);
5548
1.77k
    SCTP_INP_WUNLOCK(inp);
5549
1.77k
  }
5550
  /* pull from vtag hash */
5551
12.8k
  LIST_REMOVE(stcb, sctp_asocs);
5552
12.8k
  sctp_add_vtag_to_timewait(asoc->my_vtag, inp->sctp_lport, stcb->rport);
5553
5554
  /* Now restop the timers to be sure
5555
   * this is paranoia at is finest!
5556
   */
5557
12.8k
  sctp_stop_association_timers(stcb, true);
5558
5559
  /*
5560
   * The chunk lists and such SHOULD be empty but we check them just
5561
   * in case.
5562
   */
5563
  /* anything on the wheel needs to be removed */
5564
3.23M
  for (i = 0; i < asoc->streamoutcnt; i++) {
5565
3.22M
    struct sctp_stream_out *outs;
5566
5567
3.22M
    outs = &asoc->strmout[i];
5568
    /* now clean up any chunks here */
5569
3.22M
    TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
5570
17.8k
      atomic_subtract_int(&asoc->stream_queue_cnt, 1);
5571
17.8k
      TAILQ_REMOVE(&outs->outqueue, sp, next);
5572
17.8k
      stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp);
5573
17.8k
      sctp_free_spbufspace(stcb, asoc, sp);
5574
17.8k
      if (sp->data) {
5575
17.8k
        if (so) {
5576
          /* Still an open socket - report */
5577
0
          sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
5578
0
                          0, (void *)sp, SCTP_SO_LOCKED);
5579
0
        }
5580
17.8k
        if (sp->data) {
5581
17.8k
          sctp_m_freem(sp->data);
5582
17.8k
          sp->data = NULL;
5583
17.8k
          sp->tail_mbuf = NULL;
5584
17.8k
          sp->length = 0;
5585
17.8k
        }
5586
17.8k
      }
5587
17.8k
      if (sp->net) {
5588
0
        sctp_free_remote_addr(sp->net);
5589
0
        sp->net = NULL;
5590
0
      }
5591
17.8k
      sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
5592
17.8k
    }
5593
3.22M
  }
5594
  /*sa_ignore FREED_MEMORY*/
5595
12.8k
  TAILQ_FOREACH_SAFE(strrst, &asoc->resetHead, next_resp, nstrrst) {
5596
463
    TAILQ_REMOVE(&asoc->resetHead, strrst, next_resp);
5597
463
    SCTP_FREE(strrst, SCTP_M_STRESET);
5598
463
  }
5599
12.8k
  TAILQ_FOREACH_SAFE(sq, &asoc->pending_reply_queue, next, nsq) {
5600
205
    TAILQ_REMOVE(&asoc->pending_reply_queue, sq, next);
5601
205
    if (sq->data) {
5602
205
      sctp_m_freem(sq->data);
5603
205
      sq->data = NULL;
5604
205
    }
5605
205
    sctp_free_remote_addr(sq->whoFrom);
5606
205
    sq->whoFrom = NULL;
5607
205
    sq->stcb = NULL;
5608
    /* Free the ctl entry */
5609
205
    sctp_free_a_readq(stcb, sq);
5610
    /*sa_ignore FREED_MEMORY*/
5611
205
  }
5612
19.4k
  TAILQ_FOREACH_SAFE(chk, &asoc->free_chunks, sctp_next, nchk) {
5613
19.4k
    TAILQ_REMOVE(&asoc->free_chunks, chk, sctp_next);
5614
19.4k
    if (chk->data) {
5615
0
      sctp_m_freem(chk->data);
5616
0
      chk->data = NULL;
5617
0
    }
5618
19.4k
    if (chk->holds_key_ref)
5619
0
      sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5620
19.4k
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5621
19.4k
    SCTP_DECR_CHK_COUNT();
5622
19.4k
    atomic_subtract_int(&SCTP_BASE_INFO(ipi_free_chunks), 1);
5623
19.4k
    asoc->free_chunk_cnt--;
5624
    /*sa_ignore FREED_MEMORY*/
5625
19.4k
  }
5626
  /* pending send queue SHOULD be empty */
5627
12.8k
  TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
5628
1.50k
    if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5629
1.50k
      asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
5630
1.50k
#ifdef INVARIANTS
5631
1.50k
    } else {
5632
0
      panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
5633
0
#endif
5634
0
    }
5635
1.50k
    TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
5636
1.50k
    if (chk->data) {
5637
1.50k
      if (so) {
5638
        /* Still a socket? */
5639
0
        sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
5640
0
                        0, chk, SCTP_SO_LOCKED);
5641
0
      }
5642
1.50k
      if (chk->data) {
5643
1.50k
        sctp_m_freem(chk->data);
5644
1.50k
        chk->data = NULL;
5645
1.50k
      }
5646
1.50k
    }
5647
1.50k
    if (chk->holds_key_ref)
5648
0
      sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5649
1.50k
    if (chk->whoTo) {
5650
0
      sctp_free_remote_addr(chk->whoTo);
5651
0
      chk->whoTo = NULL;
5652
0
    }
5653
1.50k
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5654
1.50k
    SCTP_DECR_CHK_COUNT();
5655
    /*sa_ignore FREED_MEMORY*/
5656
1.50k
  }
5657
  /* sent queue SHOULD be empty */
5658
12.8k
  TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
5659
7.44k
    if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
5660
7.44k
      if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
5661
7.44k
        asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
5662
7.44k
#ifdef INVARIANTS
5663
7.44k
      } else {
5664
0
        panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
5665
0
#endif
5666
0
      }
5667
7.44k
    }
5668
7.44k
    TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
5669
7.44k
    if (chk->data) {
5670
7.44k
      if (so) {
5671
        /* Still a socket? */
5672
0
        sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
5673
0
                        0, chk, SCTP_SO_LOCKED);
5674
0
      }
5675
7.44k
      if (chk->data) {
5676
7.44k
        sctp_m_freem(chk->data);
5677
7.44k
        chk->data = NULL;
5678
7.44k
      }
5679
7.44k
    }
5680
7.44k
    if (chk->holds_key_ref)
5681
0
      sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5682
7.44k
    sctp_free_remote_addr(chk->whoTo);
5683
7.44k
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5684
7.44k
    SCTP_DECR_CHK_COUNT();
5685
    /*sa_ignore FREED_MEMORY*/
5686
7.44k
  }
5687
12.8k
#ifdef INVARIANTS
5688
3.23M
  for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
5689
3.22M
    if (stcb->asoc.strmout[i].chunks_on_queues > 0) {
5690
0
      panic("%u chunks left for stream %u.", stcb->asoc.strmout[i].chunks_on_queues, i);
5691
0
    }
5692
3.22M
  }
5693
12.8k
#endif
5694
  /* control queue MAY not be empty */
5695
12.8k
  TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
5696
12.5k
    TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
5697
12.5k
    if (chk->data) {
5698
12.5k
      sctp_m_freem(chk->data);
5699
12.5k
      chk->data = NULL;
5700
12.5k
    }
5701
12.5k
    if (chk->holds_key_ref)
5702
0
      sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5703
12.5k
    sctp_free_remote_addr(chk->whoTo);
5704
12.5k
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5705
12.5k
    SCTP_DECR_CHK_COUNT();
5706
    /*sa_ignore FREED_MEMORY*/
5707
12.5k
  }
5708
  /* ASCONF queue MAY not be empty */
5709
12.8k
  TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
5710
0
    TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
5711
0
    if (chk->data) {
5712
0
      sctp_m_freem(chk->data);
5713
0
      chk->data = NULL;
5714
0
    }
5715
0
    if (chk->holds_key_ref)
5716
0
      sctp_auth_key_release(stcb, chk->auth_keyid, SCTP_SO_LOCKED);
5717
0
    sctp_free_remote_addr(chk->whoTo);
5718
0
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_chunk), chk);
5719
0
    SCTP_DECR_CHK_COUNT();
5720
    /*sa_ignore FREED_MEMORY*/
5721
0
  }
5722
12.8k
  if (asoc->mapping_array) {
5723
12.8k
    SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
5724
12.8k
    asoc->mapping_array = NULL;
5725
12.8k
  }
5726
12.8k
  if (asoc->nr_mapping_array) {
5727
12.8k
    SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
5728
12.8k
    asoc->nr_mapping_array = NULL;
5729
12.8k
  }
5730
  /* the stream outs */
5731
12.8k
  if (asoc->strmout) {
5732
12.8k
    SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
5733
12.8k
    asoc->strmout = NULL;
5734
12.8k
  }
5735
12.8k
  asoc->strm_realoutsize = asoc->streamoutcnt = 0;
5736
12.8k
  if (asoc->strmin) {
5737
2.68M
    for (i = 0; i < asoc->streamincnt; i++) {
5738
2.67M
      sctp_clean_up_stream(stcb, &asoc->strmin[i].inqueue);
5739
2.67M
      sctp_clean_up_stream(stcb, &asoc->strmin[i].uno_inqueue);
5740
2.67M
    }
5741
10.6k
    SCTP_FREE(asoc->strmin, SCTP_M_STRMI);
5742
10.6k
    asoc->strmin = NULL;
5743
10.6k
  }
5744
12.8k
  asoc->streamincnt = 0;
5745
18.1k
  TAILQ_FOREACH_SAFE(net, &asoc->nets, sctp_next, nnet) {
5746
18.1k
#ifdef INVARIANTS
5747
18.1k
    if (SCTP_BASE_INFO(ipi_count_raddr) == 0) {
5748
0
      panic("no net's left alloc'ed, or list points to itself");
5749
0
    }
5750
18.1k
#endif
5751
18.1k
    TAILQ_REMOVE(&asoc->nets, net, sctp_next);
5752
18.1k
    sctp_free_remote_addr(net);
5753
18.1k
  }
5754
12.8k
  LIST_FOREACH_SAFE(laddr, &asoc->sctp_restricted_addrs, sctp_nxt_addr, naddr) {
5755
    /*sa_ignore FREED_MEMORY*/
5756
0
    sctp_remove_laddr(laddr);
5757
0
  }
5758
5759
  /* pending asconf (address) parameters */
5760
12.8k
  TAILQ_FOREACH_SAFE(aparam, &asoc->asconf_queue, next, naparam) {
5761
    /*sa_ignore FREED_MEMORY*/
5762
0
    TAILQ_REMOVE(&asoc->asconf_queue, aparam, next);
5763
0
    SCTP_FREE(aparam,SCTP_M_ASC_ADDR);
5764
0
  }
5765
12.8k
  TAILQ_FOREACH_SAFE(aack, &asoc->asconf_ack_sent, next, naack) {
5766
    /*sa_ignore FREED_MEMORY*/
5767
1.17k
    TAILQ_REMOVE(&asoc->asconf_ack_sent, aack, next);
5768
1.17k
    if (aack->data != NULL) {
5769
1.17k
      sctp_m_freem(aack->data);
5770
1.17k
    }
5771
1.17k
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
5772
1.17k
  }
5773
  /* clean up auth stuff */
5774
12.8k
  if (asoc->local_hmacs)
5775
12.8k
    sctp_free_hmaclist(asoc->local_hmacs);
5776
12.8k
  if (asoc->peer_hmacs)
5777
9.90k
    sctp_free_hmaclist(asoc->peer_hmacs);
5778
5779
12.8k
  if (asoc->local_auth_chunks)
5780
12.8k
    sctp_free_chunklist(asoc->local_auth_chunks);
5781
12.8k
  if (asoc->peer_auth_chunks)
5782
10.0k
    sctp_free_chunklist(asoc->peer_auth_chunks);
5783
5784
12.8k
  sctp_free_authinfo(&asoc->authinfo);
5785
5786
12.8k
  LIST_FOREACH_SAFE(shared_key, &asoc->shared_keys, next, nshared_key) {
5787
12.8k
    LIST_REMOVE(shared_key, next);
5788
12.8k
    sctp_free_sharedkey(shared_key);
5789
    /*sa_ignore FREED_MEMORY*/
5790
12.8k
  }
5791
5792
  /* Insert new items here :> */
5793
5794
  /* Get rid of LOCK */
5795
12.8k
  SCTP_TCB_UNLOCK(stcb);
5796
12.8k
  SCTP_TCB_LOCK_DESTROY(stcb);
5797
12.8k
  if (from_inpcbfree == SCTP_NORMAL_PROC) {
5798
1.77k
    SCTP_INP_INFO_WUNLOCK();
5799
1.77k
    SCTP_INP_RLOCK(inp);
5800
1.77k
  }
5801
#if defined(__APPLE__) && !defined(__Userspace__)
5802
  /* TEMP CODE */
5803
  stcb->freed_from_where = from_location;
5804
#endif
5805
#ifdef SCTP_TRACK_FREED_ASOCS
5806
  if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5807
    /* now clean up the tasoc itself */
5808
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5809
    SCTP_DECR_ASOC_COUNT();
5810
  } else {
5811
    LIST_INSERT_HEAD(&inp->sctp_asoc_free_list, stcb, sctp_tcblist);
5812
  }
5813
#else
5814
12.8k
  SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asoc), stcb);
5815
12.8k
  SCTP_DECR_ASOC_COUNT();
5816
12.8k
#endif
5817
12.8k
  if (from_inpcbfree == SCTP_NORMAL_PROC) {
5818
1.77k
    if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5819
      /* If its NOT the inp_free calling us AND
5820
       * sctp_close as been called, we
5821
       * call back...
5822
       */
5823
22
      SCTP_INP_RUNLOCK(inp);
5824
      /* This will start the kill timer (if we are
5825
       * the last one) since we hold an increment yet. But
5826
       * this is the only safe way to do this
5827
       * since otherwise if the socket closes
5828
       * at the same time we are here we might
5829
       * collide in the cleanup.
5830
       */
5831
22
      sctp_inpcb_free(inp,
5832
22
          SCTP_FREE_SHOULD_USE_GRACEFUL_CLOSE,
5833
22
          SCTP_CALLED_DIRECTLY_NOCMPSET);
5834
22
      SCTP_INP_DECR_REF(inp);
5835
1.75k
    } else {
5836
      /* The socket is still open. */
5837
1.75k
      SCTP_INP_DECR_REF(inp);
5838
1.75k
      SCTP_INP_RUNLOCK(inp);
5839
1.75k
    }
5840
1.77k
  }
5841
  /* destroyed the asoc */
5842
#ifdef SCTP_LOG_CLOSING
5843
  sctp_log_closing(inp, NULL, 11);
5844
#endif
5845
12.8k
  return (1);
5846
12.8k
}
5847
5848
/*
5849
 * determine if a destination is "reachable" based upon the addresses bound
5850
 * to the current endpoint (e.g. only v4 or v6 currently bound)
5851
 */
5852
/*
5853
 * FIX: if we allow assoc-level bindx(), then this needs to be fixed to use
5854
 * assoc level v4/v6 flags, as the assoc *may* not have the same address
5855
 * types bound as its endpoint
5856
 */
5857
int
5858
sctp_destination_is_reachable(struct sctp_tcb *stcb, struct sockaddr *destaddr)
5859
0
{
5860
0
  struct sctp_inpcb *inp;
5861
0
  int answer;
5862
5863
  /*
5864
   * No locks here, the TCB, in all cases is already locked and an
5865
   * assoc is up. There is either a INP lock by the caller applied (in
5866
   * asconf case when deleting an address) or NOT in the HB case,
5867
   * however if HB then the INP increment is up and the INP will not
5868
   * be removed (on top of the fact that we have a TCB lock). So we
5869
   * only want to read the sctp_flags, which is either bound-all or
5870
   * not.. no protection needed since once an assoc is up you can't be
5871
   * changing your binding.
5872
   */
5873
0
  inp = stcb->sctp_ep;
5874
0
  if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5875
    /* if bound all, destination is not restricted */
5876
    /*
5877
     * RRS: Question during lock work: Is this correct? If you
5878
     * are bound-all you still might need to obey the V4--V6
5879
     * flags??? IMO this bound-all stuff needs to be removed!
5880
     */
5881
0
    return (1);
5882
0
  }
5883
  /* NOTE: all "scope" checks are done when local addresses are added */
5884
0
  switch (destaddr->sa_family) {
5885
0
#ifdef INET6
5886
0
  case AF_INET6:
5887
0
    answer = inp->ip_inp.inp.inp_vflag & INP_IPV6;
5888
0
    break;
5889
0
#endif
5890
0
#ifdef INET
5891
0
  case AF_INET:
5892
0
    answer = inp->ip_inp.inp.inp_vflag & INP_IPV4;
5893
0
    break;
5894
0
#endif
5895
0
#if defined(__Userspace__)
5896
0
  case AF_CONN:
5897
0
    answer = inp->ip_inp.inp.inp_vflag & INP_CONN;
5898
0
    break;
5899
0
#endif
5900
0
  default:
5901
    /* invalid family, so it's unreachable */
5902
0
    answer = 0;
5903
0
    break;
5904
0
  }
5905
0
  return (answer);
5906
0
}
5907
5908
/*
5909
 * update the inp_vflags on an endpoint
5910
 */
5911
static void
5912
sctp_update_ep_vflag(struct sctp_inpcb *inp)
5913
0
{
5914
0
  struct sctp_laddr *laddr;
5915
5916
  /* first clear the flag */
5917
0
  inp->ip_inp.inp.inp_vflag = 0;
5918
  /* set the flag based on addresses on the ep list */
5919
0
  LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5920
0
    if (laddr->ifa == NULL) {
5921
0
      SCTPDBG(SCTP_DEBUG_PCB1, "%s: NULL ifa\n",
5922
0
        __func__);
5923
0
      continue;
5924
0
    }
5925
5926
0
    if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
5927
0
      continue;
5928
0
    }
5929
0
    switch (laddr->ifa->address.sa.sa_family) {
5930
0
#ifdef INET6
5931
0
    case AF_INET6:
5932
0
      inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5933
0
      break;
5934
0
#endif
5935
0
#ifdef INET
5936
0
    case AF_INET:
5937
0
      inp->ip_inp.inp.inp_vflag |= INP_IPV4;
5938
0
      break;
5939
0
#endif
5940
0
#if defined(__Userspace__)
5941
0
    case AF_CONN:
5942
0
      inp->ip_inp.inp.inp_vflag |= INP_CONN;
5943
0
      break;
5944
0
#endif
5945
0
    default:
5946
0
      break;
5947
0
    }
5948
0
  }
5949
0
}
5950
5951
/*
5952
 * Add the address to the endpoint local address list There is nothing to be
5953
 * done if we are bound to all addresses
5954
 */
5955
void
5956
sctp_add_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa, uint32_t action)
5957
0
{
5958
0
  struct sctp_laddr *laddr;
5959
0
  struct sctp_tcb *stcb;
5960
0
  int fnd, error = 0;
5961
5962
0
  fnd = 0;
5963
5964
0
  if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
5965
    /* You are already bound to all. You have it already */
5966
0
    return;
5967
0
  }
5968
0
#ifdef INET6
5969
0
  if (ifa->address.sa.sa_family == AF_INET6) {
5970
0
    if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
5971
      /* Can't bind a non-useable addr. */
5972
0
      return;
5973
0
    }
5974
0
  }
5975
0
#endif
5976
  /* first, is it already present? */
5977
0
  LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5978
0
    if (laddr->ifa == ifa) {
5979
0
      fnd = 1;
5980
0
      break;
5981
0
    }
5982
0
  }
5983
5984
0
  if (fnd == 0) {
5985
    /* Not in the ep list */
5986
0
    error = sctp_insert_laddr(&inp->sctp_addr_list, ifa, action);
5987
0
    if (error != 0)
5988
0
      return;
5989
0
    inp->laddr_count++;
5990
    /* update inp_vflag flags */
5991
0
    switch (ifa->address.sa.sa_family) {
5992
0
#ifdef INET6
5993
0
    case AF_INET6:
5994
0
      inp->ip_inp.inp.inp_vflag |= INP_IPV6;
5995
0
      break;
5996
0
#endif
5997
0
#ifdef INET
5998
0
    case AF_INET:
5999
0
      inp->ip_inp.inp.inp_vflag |= INP_IPV4;
6000
0
      break;
6001
0
#endif
6002
0
#if defined(__Userspace__)
6003
0
    case AF_CONN:
6004
0
      inp->ip_inp.inp.inp_vflag |= INP_CONN;
6005
0
      break;
6006
0
#endif
6007
0
    default:
6008
0
      break;
6009
0
    }
6010
0
    LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6011
0
      sctp_add_local_addr_restricted(stcb, ifa);
6012
0
    }
6013
0
  }
6014
0
  return;
6015
0
}
6016
6017
/*
6018
 * select a new (hopefully reachable) destination net (should only be used
6019
 * when we deleted an ep addr that is the only usable source address to reach
6020
 * the destination net)
6021
 */
6022
static void
6023
sctp_select_primary_destination(struct sctp_tcb *stcb)
6024
0
{
6025
0
  struct sctp_nets *net;
6026
6027
0
  TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6028
    /* for now, we'll just pick the first reachable one we find */
6029
0
    if (net->dest_state & SCTP_ADDR_UNCONFIRMED)
6030
0
      continue;
6031
0
    if (sctp_destination_is_reachable(stcb,
6032
0
        (struct sockaddr *)&net->ro._l_addr)) {
6033
      /* found a reachable destination */
6034
0
      stcb->asoc.primary_destination = net;
6035
0
    }
6036
0
  }
6037
  /* I can't there from here! ...we're gonna die shortly... */
6038
0
}
6039
6040
/*
6041
 * Delete the address from the endpoint local address list. There is nothing
6042
 * to be done if we are bound to all addresses
6043
 */
6044
void
6045
sctp_del_local_addr_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
6046
0
{
6047
0
  struct sctp_laddr *laddr;
6048
0
  int fnd;
6049
6050
0
  fnd = 0;
6051
0
  if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
6052
    /* You are already bound to all. You have it already */
6053
0
    return;
6054
0
  }
6055
0
  LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
6056
0
    if (laddr->ifa == ifa) {
6057
0
      fnd = 1;
6058
0
      break;
6059
0
    }
6060
0
  }
6061
0
  if (fnd && (inp->laddr_count < 2)) {
6062
    /* can't delete unless there are at LEAST 2 addresses */
6063
0
    return;
6064
0
  }
6065
0
  if (fnd) {
6066
    /*
6067
     * clean up any use of this address go through our
6068
     * associations and clear any last_used_address that match
6069
     * this one for each assoc, see if a new primary_destination
6070
     * is needed
6071
     */
6072
0
    struct sctp_tcb *stcb;
6073
6074
    /* clean up "next_addr_touse" */
6075
0
    if (inp->next_addr_touse == laddr)
6076
      /* delete this address */
6077
0
      inp->next_addr_touse = NULL;
6078
6079
    /* clean up "last_used_address" */
6080
0
    LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
6081
0
      struct sctp_nets *net;
6082
6083
0
      SCTP_TCB_LOCK(stcb);
6084
0
      if (stcb->asoc.last_used_address == laddr)
6085
        /* delete this address */
6086
0
        stcb->asoc.last_used_address = NULL;
6087
      /* Now spin through all the nets and purge any ref to laddr */
6088
0
      TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6089
0
        if (net->ro._s_addr == laddr->ifa) {
6090
          /* Yep, purge src address selected */
6091
#if defined(__FreeBSD__) && !defined(__Userspace__)
6092
          RO_NHFREE(&net->ro);
6093
#else
6094
0
          sctp_rtentry_t *rt;
6095
6096
          /* delete this address if cached */
6097
0
          rt = net->ro.ro_rt;
6098
0
          if (rt != NULL) {
6099
0
            RTFREE(rt);
6100
0
            net->ro.ro_rt = NULL;
6101
0
          }
6102
0
#endif
6103
0
          sctp_free_ifa(net->ro._s_addr);
6104
0
          net->ro._s_addr = NULL;
6105
0
          net->src_addr_selected = 0;
6106
0
        }
6107
0
      }
6108
0
      SCTP_TCB_UNLOCK(stcb);
6109
0
    }    /* for each tcb */
6110
    /* remove it from the ep list */
6111
0
    sctp_remove_laddr(laddr);
6112
0
    inp->laddr_count--;
6113
    /* update inp_vflag flags */
6114
0
    sctp_update_ep_vflag(inp);
6115
0
  }
6116
0
  return;
6117
0
}
6118
6119
/*
6120
 * Add the address to the TCB local address restricted list.
6121
 * This is a "pending" address list (eg. addresses waiting for an
6122
 * ASCONF-ACK response) and cannot be used as a valid source address.
6123
 */
6124
void
6125
sctp_add_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6126
0
{
6127
0
  struct sctp_laddr *laddr;
6128
0
  struct sctpladdr *list;
6129
6130
  /*
6131
   * Assumes TCB is locked.. and possibly the INP. May need to
6132
   * confirm/fix that if we need it and is not the case.
6133
   */
6134
0
  list = &stcb->asoc.sctp_restricted_addrs;
6135
6136
0
#ifdef INET6
6137
0
  if (ifa->address.sa.sa_family == AF_INET6) {
6138
0
    if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
6139
      /* Can't bind a non-existent addr. */
6140
0
      return;
6141
0
    }
6142
0
  }
6143
0
#endif
6144
  /* does the address already exist? */
6145
0
  LIST_FOREACH(laddr, list, sctp_nxt_addr) {
6146
0
    if (laddr->ifa == ifa) {
6147
0
      return;
6148
0
    }
6149
0
  }
6150
6151
  /* add to the list */
6152
0
  (void)sctp_insert_laddr(list, ifa, 0);
6153
0
  return;
6154
0
}
6155
6156
/*
6157
 * Remove a local address from the TCB local address restricted list
6158
 */
6159
void
6160
sctp_del_local_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
6161
0
{
6162
0
  struct sctp_inpcb *inp;
6163
0
  struct sctp_laddr *laddr;
6164
6165
  /*
6166
   * This is called by asconf work. It is assumed that a) The TCB is
6167
   * locked and b) The INP is locked. This is true in as much as I can
6168
   * trace through the entry asconf code where I did these locks.
6169
   * Again, the ASCONF code is a bit different in that it does lock
6170
   * the INP during its work often times. This must be since we don't
6171
   * want other proc's looking up things while what they are looking
6172
   * up is changing :-D
6173
   */
6174
6175
0
  inp = stcb->sctp_ep;
6176
  /* if subset bound and don't allow ASCONF's, can't delete last */
6177
0
  if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) == 0) &&
6178
0
      sctp_is_feature_off(inp, SCTP_PCB_FLAGS_DO_ASCONF)) {
6179
0
    if (stcb->sctp_ep->laddr_count < 2) {
6180
      /* can't delete last address */
6181
0
      return;
6182
0
    }
6183
0
  }
6184
0
  LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
6185
    /* remove the address if it exists */
6186
0
    if (laddr->ifa == NULL)
6187
0
      continue;
6188
0
    if (laddr->ifa == ifa) {
6189
0
      sctp_remove_laddr(laddr);
6190
0
      return;
6191
0
    }
6192
0
  }
6193
6194
  /* address not found! */
6195
0
  return;
6196
0
}
6197
6198
#if defined(__FreeBSD__) && !defined(__Userspace__)
6199
/* sysctl */
6200
static int sctp_max_number_of_assoc = SCTP_MAX_NUM_OF_ASOC;
6201
static int sctp_scale_up_for_address = SCTP_SCALE_FOR_ADDR;
6202
#endif
6203
6204
#if defined(__FreeBSD__) && !defined(__Userspace__)
6205
#if defined(SCTP_MCORE_INPUT) && defined(SMP)
6206
struct sctp_mcore_ctrl *sctp_mcore_workers = NULL;
6207
int *sctp_cpuarry = NULL;
6208
6209
void
6210
sctp_queue_to_mcore(struct mbuf *m, int off, int cpu_to_use)
6211
{
6212
  /* Queue a packet to a processor for the specified core */
6213
  struct sctp_mcore_queue *qent;
6214
  struct sctp_mcore_ctrl *wkq;
6215
  int need_wake = 0;
6216
6217
  if (sctp_mcore_workers == NULL) {
6218
    /* Something went way bad during setup */
6219
    sctp_input_with_port(m, off, 0);
6220
    return;
6221
  }
6222
  SCTP_MALLOC(qent, struct sctp_mcore_queue *,
6223
        (sizeof(struct sctp_mcore_queue)),
6224
        SCTP_M_MCORE);
6225
  if (qent == NULL) {
6226
    /* This is trouble  */
6227
    sctp_input_with_port(m, off, 0);
6228
    return;
6229
  }
6230
  qent->vn = curvnet;
6231
  qent->m = m;
6232
  qent->off = off;
6233
  qent->v6 = 0;
6234
  wkq = &sctp_mcore_workers[cpu_to_use];
6235
  SCTP_MCORE_QLOCK(wkq);
6236
6237
  TAILQ_INSERT_TAIL(&wkq->que, qent, next);
6238
  if (wkq->running == 0) {
6239
    need_wake = 1;
6240
  }
6241
  SCTP_MCORE_QUNLOCK(wkq);
6242
  if (need_wake) {
6243
    wakeup(&wkq->running);
6244
  }
6245
}
6246
6247
static void
6248
sctp_mcore_thread(void *arg)
6249
{
6250
6251
  struct sctp_mcore_ctrl *wkq;
6252
  struct sctp_mcore_queue *qent;
6253
6254
  wkq = (struct sctp_mcore_ctrl *)arg;
6255
  struct mbuf *m;
6256
  int off, v6;
6257
6258
  /* Wait for first tickle */
6259
  SCTP_MCORE_LOCK(wkq);
6260
  wkq->running = 0;
6261
  msleep(&wkq->running,
6262
         &wkq->core_mtx,
6263
         0, "wait for pkt", 0);
6264
  SCTP_MCORE_UNLOCK(wkq);
6265
6266
  /* Bind to our cpu */
6267
  thread_lock(curthread);
6268
  sched_bind(curthread, wkq->cpuid);
6269
  thread_unlock(curthread);
6270
6271
  /* Now lets start working */
6272
  SCTP_MCORE_LOCK(wkq);
6273
  /* Now grab lock and go */
6274
  for (;;) {
6275
    SCTP_MCORE_QLOCK(wkq);
6276
  skip_sleep:
6277
    wkq->running = 1;
6278
    qent = TAILQ_FIRST(&wkq->que);
6279
    if (qent) {
6280
      TAILQ_REMOVE(&wkq->que, qent, next);
6281
      SCTP_MCORE_QUNLOCK(wkq);
6282
      CURVNET_SET(qent->vn);
6283
      m = qent->m;
6284
      off = qent->off;
6285
      v6 = qent->v6;
6286
      SCTP_FREE(qent, SCTP_M_MCORE);
6287
      if (v6 == 0) {
6288
        sctp_input_with_port(m, off, 0);
6289
      } else {
6290
        SCTP_PRINTF("V6 not yet supported\n");
6291
        sctp_m_freem(m);
6292
      }
6293
      CURVNET_RESTORE();
6294
      SCTP_MCORE_QLOCK(wkq);
6295
    }
6296
    wkq->running = 0;
6297
    if (!TAILQ_EMPTY(&wkq->que)) {
6298
      goto skip_sleep;
6299
    }
6300
    SCTP_MCORE_QUNLOCK(wkq);
6301
    msleep(&wkq->running,
6302
           &wkq->core_mtx,
6303
           0, "wait for pkt", 0);
6304
  }
6305
}
6306
6307
static void
6308
sctp_startup_mcore_threads(void)
6309
{
6310
  int i, cpu;
6311
6312
  if (mp_ncpus == 1)
6313
    return;
6314
6315
  if (sctp_mcore_workers != NULL) {
6316
    /* Already been here in some previous
6317
     * vnet?
6318
     */
6319
    return;
6320
  }
6321
  SCTP_MALLOC(sctp_mcore_workers, struct sctp_mcore_ctrl *,
6322
        ((mp_maxid+1) * sizeof(struct sctp_mcore_ctrl)),
6323
        SCTP_M_MCORE);
6324
  if (sctp_mcore_workers == NULL) {
6325
    /* TSNH I hope */
6326
    return;
6327
  }
6328
  memset(sctp_mcore_workers, 0 , ((mp_maxid+1) *
6329
          sizeof(struct sctp_mcore_ctrl)));
6330
  /* Init the structures */
6331
  for (i = 0; i<=mp_maxid; i++) {
6332
    TAILQ_INIT(&sctp_mcore_workers[i].que);
6333
    SCTP_MCORE_LOCK_INIT(&sctp_mcore_workers[i]);
6334
    SCTP_MCORE_QLOCK_INIT(&sctp_mcore_workers[i]);
6335
    sctp_mcore_workers[i].cpuid = i;
6336
  }
6337
  if (sctp_cpuarry == NULL) {
6338
    SCTP_MALLOC(sctp_cpuarry, int *,
6339
          (mp_ncpus * sizeof(int)),
6340
          SCTP_M_MCORE);
6341
    i = 0;
6342
    CPU_FOREACH(cpu) {
6343
      sctp_cpuarry[i] = cpu;
6344
      i++;
6345
    }
6346
  }
6347
  /* Now start them all */
6348
  CPU_FOREACH(cpu) {
6349
    (void)kproc_create(sctp_mcore_thread,
6350
           (void *)&sctp_mcore_workers[cpu],
6351
           &sctp_mcore_workers[cpu].thread_proc,
6352
           0,
6353
           SCTP_KTHREAD_PAGES,
6354
           SCTP_MCORE_NAME);
6355
  }
6356
}
6357
#endif
6358
#endif
6359
6360
#if defined(__FreeBSD__) && !defined(__Userspace__)
6361
#if defined(SCTP_NOT_YET)
6362
static struct mbuf *
6363
sctp_netisr_hdlr(struct mbuf *m, uintptr_t source)
6364
{
6365
  struct ip *ip;
6366
  struct sctphdr *sh;
6367
  int offset;
6368
  uint32_t flowid, tag;
6369
6370
  /*
6371
   * No flow id built by lower layers fix it so we
6372
   * create one.
6373
   */
6374
  ip = mtod(m, struct ip *);
6375
  offset = (ip->ip_hl << 2) + sizeof(struct sctphdr);
6376
  if (SCTP_BUF_LEN(m) < offset) {
6377
    if ((m = m_pullup(m, offset)) == NULL) {
6378
      SCTP_STAT_INCR(sctps_hdrops);
6379
      return (NULL);
6380
    }
6381
    ip = mtod(m, struct ip *);
6382
  }
6383
  sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
6384
  tag = htonl(sh->v_tag);
6385
  flowid = tag ^ ntohs(sh->dest_port) ^ ntohs(sh->src_port);
6386
  m->m_pkthdr.flowid = flowid;
6387
  /* FIX ME */
6388
  m->m_flags |= M_FLOWID;
6389
  return (m);
6390
}
6391
6392
#endif
6393
#endif
6394
#if defined(__FreeBSD__) && !defined(__Userspace__)
6395
#define VALIDATE_LOADER_TUNABLE(var_name, prefix)   \
6396
  if (SCTP_BASE_SYSCTL(var_name) < prefix##_MIN ||  \
6397
      SCTP_BASE_SYSCTL(var_name) > prefix##_MAX)    \
6398
    SCTP_BASE_SYSCTL(var_name) = prefix##_DEFAULT
6399
6400
#endif
6401
void
6402
#if defined(__Userspace__)
6403
sctp_pcb_init(int start_threads)
6404
#else
6405
sctp_pcb_init(void)
6406
#endif
6407
3
{
6408
  /*
6409
   * SCTP initialization for the PCB structures should be called by
6410
   * the sctp_init() function.
6411
   */
6412
3
  int i;
6413
3
  struct timeval tv;
6414
6415
3
  if (SCTP_BASE_VAR(sctp_pcb_initialized) != 0) {
6416
    /* error I was called twice */
6417
0
    return;
6418
0
  }
6419
3
  SCTP_BASE_VAR(sctp_pcb_initialized) = 1;
6420
6421
3
#if defined(SCTP_PROCESS_LEVEL_LOCKS)
6422
3
#if !defined(_WIN32)
6423
3
  pthread_mutexattr_init(&SCTP_BASE_VAR(mtx_attr));
6424
3
  pthread_rwlockattr_init(&SCTP_BASE_VAR(rwlock_attr));
6425
3
#ifdef INVARIANTS
6426
3
  pthread_mutexattr_settype(&SCTP_BASE_VAR(mtx_attr), PTHREAD_MUTEX_ERRORCHECK);
6427
3
#endif
6428
3
#endif
6429
3
#endif
6430
#if defined(SCTP_LOCAL_TRACE_BUF)
6431
#if defined(_WIN32) && !defined(__Userspace__)
6432
  if (SCTP_BASE_SYSCTL(sctp_log) != NULL) {
6433
    memset(SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
6434
  }
6435
#else
6436
  memset(&SCTP_BASE_SYSCTL(sctp_log), 0, sizeof(struct sctp_log));
6437
#endif
6438
#endif
6439
#if defined(__FreeBSD__) && !defined(__Userspace__)
6440
#if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6441
  SCTP_MALLOC(SCTP_BASE_STATS, struct sctpstat *,
6442
        ((mp_maxid+1) * sizeof(struct sctpstat)),
6443
        SCTP_M_MCORE);
6444
#endif
6445
#endif
6446
3
  (void)SCTP_GETTIME_TIMEVAL(&tv);
6447
#if defined(__FreeBSD__) && !defined(__Userspace__)
6448
#if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6449
  memset(SCTP_BASE_STATS, 0, sizeof(struct sctpstat) * (mp_maxid+1));
6450
  SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_sec = (uint32_t)tv.tv_sec;
6451
  SCTP_BASE_STATS[PCPU_GET(cpuid)].sctps_discontinuitytime.tv_usec = (uint32_t)tv.tv_usec;
6452
#else
6453
  memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
6454
  SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6455
  SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6456
#endif
6457
#else
6458
3
  memset(&SCTP_BASE_STATS, 0, sizeof(struct sctpstat));
6459
3
  SCTP_BASE_STAT(sctps_discontinuitytime).tv_sec = (uint32_t)tv.tv_sec;
6460
3
  SCTP_BASE_STAT(sctps_discontinuitytime).tv_usec = (uint32_t)tv.tv_usec;
6461
3
#endif
6462
  /* init the empty list of (All) Endpoints */
6463
3
  LIST_INIT(&SCTP_BASE_INFO(listhead));
6464
#if defined(__APPLE__) && !defined(__Userspace__)
6465
  LIST_INIT(&SCTP_BASE_INFO(inplisthead));
6466
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6467
  SCTP_BASE_INFO(sctbinfo).listhead = &SCTP_BASE_INFO(inplisthead);
6468
  SCTP_BASE_INFO(sctbinfo).mtx_grp_attr = lck_grp_attr_alloc_init();
6469
  lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6470
  SCTP_BASE_INFO(sctbinfo).mtx_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6471
  SCTP_BASE_INFO(sctbinfo).mtx_attr = lck_attr_alloc_init();
6472
  lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6473
#else
6474
  SCTP_BASE_INFO(sctbinfo).ipi_listhead = &SCTP_BASE_INFO(inplisthead);
6475
  SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr = lck_grp_attr_alloc_init();
6476
  lck_grp_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6477
  SCTP_BASE_INFO(sctbinfo).ipi_lock_grp = lck_grp_alloc_init("sctppcb", SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6478
  SCTP_BASE_INFO(sctbinfo).ipi_lock_attr = lck_attr_alloc_init();
6479
  lck_attr_setdefault(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
6480
#endif
6481
#if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6482
  SCTP_BASE_INFO(sctbinfo).ipi_gc = sctp_gc;
6483
  in_pcbinfo_attach(&SCTP_BASE_INFO(sctbinfo));
6484
#endif
6485
#endif
6486
6487
  /* init the hash table of endpoints */
6488
#if defined(__FreeBSD__) && !defined(__Userspace__)
6489
  TUNABLE_INT_FETCH("net.inet.sctp.tcbhashsize", &SCTP_BASE_SYSCTL(sctp_hashtblsize));
6490
  TUNABLE_INT_FETCH("net.inet.sctp.pcbhashsize", &SCTP_BASE_SYSCTL(sctp_pcbtblsize));
6491
  TUNABLE_INT_FETCH("net.inet.sctp.chunkscale", &SCTP_BASE_SYSCTL(sctp_chunkscale));
6492
  VALIDATE_LOADER_TUNABLE(sctp_hashtblsize, SCTPCTL_TCBHASHSIZE);
6493
  VALIDATE_LOADER_TUNABLE(sctp_pcbtblsize, SCTPCTL_PCBHASHSIZE);
6494
  VALIDATE_LOADER_TUNABLE(sctp_chunkscale, SCTPCTL_CHUNKSCALE);
6495
#endif
6496
3
  SCTP_BASE_INFO(sctp_asochash) = SCTP_HASH_INIT((SCTP_BASE_SYSCTL(sctp_hashtblsize) * 31),
6497
3
                   &SCTP_BASE_INFO(hashasocmark));
6498
3
  SCTP_BASE_INFO(sctp_ephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6499
3
                 &SCTP_BASE_INFO(hashmark));
6500
3
  SCTP_BASE_INFO(sctp_tcpephash) = SCTP_HASH_INIT(SCTP_BASE_SYSCTL(sctp_hashtblsize),
6501
3
              &SCTP_BASE_INFO(hashtcpmark));
6502
3
  SCTP_BASE_INFO(hashtblsize) = SCTP_BASE_SYSCTL(sctp_hashtblsize);
6503
3
  SCTP_BASE_INFO(sctp_vrfhash) = SCTP_HASH_INIT(SCTP_SIZE_OF_VRF_HASH,
6504
3
                  &SCTP_BASE_INFO(hashvrfmark));
6505
6506
3
  SCTP_BASE_INFO(vrf_ifn_hash) = SCTP_HASH_INIT(SCTP_VRF_IFN_HASH_SIZE,
6507
3
                  &SCTP_BASE_INFO(vrf_ifn_hashmark));
6508
  /* init the zones */
6509
  /*
6510
   * FIX ME: Should check for NULL returns, but if it does fail we are
6511
   * doomed to panic anyways... add later maybe.
6512
   */
6513
3
  SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_ep), "sctp_ep",
6514
3
           sizeof(struct sctp_inpcb), maxsockets);
6515
6516
3
  SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asoc), "sctp_asoc",
6517
3
           sizeof(struct sctp_tcb), sctp_max_number_of_assoc);
6518
6519
3
  SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_laddr), "sctp_laddr",
6520
3
           sizeof(struct sctp_laddr),
6521
3
           (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6522
6523
3
  SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_net), "sctp_raddr",
6524
3
           sizeof(struct sctp_nets),
6525
3
           (sctp_max_number_of_assoc * sctp_scale_up_for_address));
6526
6527
3
  SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_chunk), "sctp_chunk",
6528
3
           sizeof(struct sctp_tmit_chunk),
6529
3
           (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6530
6531
3
  SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_readq), "sctp_readq",
6532
3
           sizeof(struct sctp_queued_to_read),
6533
3
           (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6534
6535
3
  SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_strmoq), "sctp_stream_msg_out",
6536
3
           sizeof(struct sctp_stream_queue_pending),
6537
3
           (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6538
6539
3
  SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf), "sctp_asconf",
6540
3
           sizeof(struct sctp_asconf),
6541
3
           (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6542
6543
3
  SCTP_ZONE_INIT(SCTP_BASE_INFO(ipi_zone_asconf_ack), "sctp_asconf_ack",
6544
3
           sizeof(struct sctp_asconf_ack),
6545
3
           (sctp_max_number_of_assoc * SCTP_BASE_SYSCTL(sctp_chunkscale)));
6546
6547
  /* Master Lock INIT for info structure */
6548
3
  SCTP_INP_INFO_LOCK_INIT();
6549
3
  SCTP_STATLOG_INIT_LOCK();
6550
6551
3
  SCTP_IPI_COUNT_INIT();
6552
3
  SCTP_IPI_ADDR_INIT();
6553
#ifdef SCTP_PACKET_LOGGING
6554
  SCTP_IP_PKTLOG_INIT();
6555
#endif
6556
3
  LIST_INIT(&SCTP_BASE_INFO(addr_wq));
6557
6558
3
  SCTP_WQ_ADDR_INIT();
6559
  /* not sure if we need all the counts */
6560
3
  SCTP_BASE_INFO(ipi_count_ep) = 0;
6561
  /* assoc/tcb zone info */
6562
3
  SCTP_BASE_INFO(ipi_count_asoc) = 0;
6563
  /* local addrlist zone info */
6564
3
  SCTP_BASE_INFO(ipi_count_laddr) = 0;
6565
  /* remote addrlist zone info */
6566
3
  SCTP_BASE_INFO(ipi_count_raddr) = 0;
6567
  /* chunk info */
6568
3
  SCTP_BASE_INFO(ipi_count_chunk) = 0;
6569
6570
  /* socket queue zone info */
6571
3
  SCTP_BASE_INFO(ipi_count_readq) = 0;
6572
6573
  /* stream out queue cont */
6574
3
  SCTP_BASE_INFO(ipi_count_strmoq) = 0;
6575
6576
3
  SCTP_BASE_INFO(ipi_free_strmoq) = 0;
6577
3
  SCTP_BASE_INFO(ipi_free_chunks) = 0;
6578
6579
3
  SCTP_OS_TIMER_INIT(&SCTP_BASE_INFO(addr_wq_timer.timer));
6580
6581
  /* Init the TIMEWAIT list */
6582
99
  for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6583
96
    LIST_INIT(&SCTP_BASE_INFO(vtag_timewait)[i]);
6584
96
  }
6585
3
#if defined(SCTP_PROCESS_LEVEL_LOCKS)
6586
#if defined(_WIN32)
6587
  InitializeConditionVariable(&sctp_it_ctl.iterator_wakeup);
6588
#else
6589
3
  (void)pthread_cond_init(&sctp_it_ctl.iterator_wakeup, NULL);
6590
3
#endif
6591
3
#endif
6592
3
  sctp_startup_iterator();
6593
6594
#if defined(__FreeBSD__) && !defined(__Userspace__)
6595
#if defined(SCTP_MCORE_INPUT) && defined(SMP)
6596
  sctp_startup_mcore_threads();
6597
#endif
6598
#endif
6599
6600
  /*
6601
   * INIT the default VRF which for BSD is the only one, other O/S's
6602
   * may have more. But initially they must start with one and then
6603
   * add the VRF's as addresses are added.
6604
   */
6605
3
  sctp_init_vrf_list(SCTP_DEFAULT_VRF);
6606
#if defined(__FreeBSD__) && !defined(__Userspace__) && defined(SCTP_NOT_YET)
6607
  if (ip_register_flow_handler(sctp_netisr_hdlr, IPPROTO_SCTP)) {
6608
    SCTP_PRINTF("***SCTP- Error can't register netisr handler***\n");
6609
  }
6610
#endif
6611
3
#if defined(_SCTP_NEEDS_CALLOUT_) || defined(_USER_SCTP_NEEDS_CALLOUT_)
6612
  /* allocate the lock for the callout/timer queue */
6613
3
  SCTP_TIMERQ_LOCK_INIT();
6614
3
  TAILQ_INIT(&SCTP_BASE_INFO(callqueue));
6615
3
#endif
6616
3
#if defined(__Userspace__)
6617
3
  mbuf_initialize(NULL);
6618
3
  atomic_init();
6619
3
#if defined(INET) || defined(INET6)
6620
3
  if (start_threads)
6621
3
    recv_thread_init();
6622
3
#endif
6623
3
#endif
6624
3
}
6625
6626
/*
6627
 * Assumes that the SCTP_BASE_INFO() lock is NOT held.
6628
 */
6629
void
6630
sctp_pcb_finish(void)
6631
0
{
6632
0
  struct sctp_vrflist *vrf_bucket;
6633
0
  struct sctp_vrf *vrf, *nvrf;
6634
0
  struct sctp_ifn *ifn, *nifn;
6635
0
  struct sctp_ifa *ifa, *nifa;
6636
0
  struct sctpvtaghead *chain;
6637
0
  struct sctp_tagblock *twait_block, *prev_twait_block;
6638
0
  struct sctp_laddr *wi, *nwi;
6639
0
  int i;
6640
0
  struct sctp_iterator *it, *nit;
6641
6642
0
  if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
6643
0
    SCTP_PRINTF("%s: race condition on teardown.\n", __func__);
6644
0
    return;
6645
0
  }
6646
0
  SCTP_BASE_VAR(sctp_pcb_initialized) = 0;
6647
0
#if !(defined(__FreeBSD__) && !defined(__Userspace__))
6648
  /* Notify the iterator to exit. */
6649
0
  SCTP_IPI_ITERATOR_WQ_LOCK();
6650
0
  sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_MUST_EXIT;
6651
0
  sctp_wakeup_iterator();
6652
0
  SCTP_IPI_ITERATOR_WQ_UNLOCK();
6653
0
#endif
6654
#if defined(__APPLE__) && !defined(__Userspace__)
6655
#if !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION)
6656
  in_pcbinfo_detach(&SCTP_BASE_INFO(sctbinfo));
6657
#endif
6658
  SCTP_IPI_ITERATOR_WQ_LOCK();
6659
  do {
6660
    msleep(&sctp_it_ctl.iterator_flags,
6661
           sctp_it_ctl.ipi_iterator_wq_mtx,
6662
           0, "waiting_for_work", 0);
6663
  } while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_EXITED) == 0);
6664
  thread_deallocate(sctp_it_ctl.thread_proc);
6665
  SCTP_IPI_ITERATOR_WQ_UNLOCK();
6666
#endif
6667
#if defined(_WIN32) && !defined(__Userspace__)
6668
  if (sctp_it_ctl.iterator_thread_obj != NULL) {
6669
    NTSTATUS status = STATUS_SUCCESS;
6670
6671
    KeSetEvent(&sctp_it_ctl.iterator_wakeup[1], IO_NO_INCREMENT, FALSE);
6672
    status = KeWaitForSingleObject(sctp_it_ctl.iterator_thread_obj,
6673
                 Executive,
6674
                 KernelMode,
6675
                 FALSE,
6676
                 NULL);
6677
    ObDereferenceObject(sctp_it_ctl.iterator_thread_obj);
6678
  }
6679
#endif
6680
0
#if defined(__Userspace__)
6681
0
  if (SCTP_BASE_VAR(iterator_thread_started)) {
6682
#if defined(_WIN32)
6683
    WaitForSingleObject(sctp_it_ctl.thread_proc, INFINITE);
6684
    CloseHandle(sctp_it_ctl.thread_proc);
6685
    sctp_it_ctl.thread_proc = NULL;
6686
#else
6687
0
    pthread_join(sctp_it_ctl.thread_proc, NULL);
6688
0
    sctp_it_ctl.thread_proc = 0;
6689
0
#endif
6690
0
  }
6691
0
#endif
6692
0
#if defined(SCTP_PROCESS_LEVEL_LOCKS)
6693
#if defined(_WIN32)
6694
  DeleteConditionVariable(&sctp_it_ctl.iterator_wakeup);
6695
#else
6696
0
  pthread_cond_destroy(&sctp_it_ctl.iterator_wakeup);
6697
0
  pthread_mutexattr_destroy(&SCTP_BASE_VAR(mtx_attr));
6698
0
  pthread_rwlockattr_destroy(&SCTP_BASE_VAR(rwlock_attr));
6699
0
#endif
6700
0
#endif
6701
  /* In FreeBSD the iterator thread never exits
6702
   * but we do clean up.
6703
   * The only way FreeBSD reaches here is if we have VRF's
6704
   * but we still add the ifdef to make it compile on old versions.
6705
   */
6706
#if defined(__FreeBSD__) && !defined(__Userspace__)
6707
retry:
6708
#endif
6709
0
  SCTP_IPI_ITERATOR_WQ_LOCK();
6710
#if defined(__FreeBSD__) && !defined(__Userspace__)
6711
  /*
6712
   * sctp_iterator_worker() might be working on an it entry without
6713
   * holding the lock.  We won't find it on the list either and
6714
   * continue and free/destroy it.  While holding the lock, spin, to
6715
   * avoid the race condition as sctp_iterator_worker() will have to
6716
   * wait to re-acquire the lock.
6717
   */
6718
  if (sctp_it_ctl.iterator_running != 0 || sctp_it_ctl.cur_it != NULL) {
6719
    SCTP_IPI_ITERATOR_WQ_UNLOCK();
6720
    SCTP_PRINTF("%s: Iterator running while we held the lock. Retry. "
6721
                "cur_it=%p\n", __func__, sctp_it_ctl.cur_it);
6722
    DELAY(10);
6723
    goto retry;
6724
  }
6725
#endif
6726
0
  TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
6727
#if defined(__FreeBSD__) && !defined(__Userspace__)
6728
    if (it->vn != curvnet) {
6729
      continue;
6730
    }
6731
#endif
6732
0
    TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
6733
0
    if (it->function_atend != NULL) {
6734
0
      (*it->function_atend) (it->pointer, it->val);
6735
0
    }
6736
0
    SCTP_FREE(it,SCTP_M_ITER);
6737
0
  }
6738
0
  SCTP_IPI_ITERATOR_WQ_UNLOCK();
6739
#if defined(__FreeBSD__) && !defined(__Userspace__)
6740
  SCTP_ITERATOR_LOCK();
6741
  if ((sctp_it_ctl.cur_it) &&
6742
      (sctp_it_ctl.cur_it->vn == curvnet)) {
6743
    sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_STOP_CUR_IT;
6744
  }
6745
  SCTP_ITERATOR_UNLOCK();
6746
#endif
6747
0
#if !(defined(__FreeBSD__) && !defined(__Userspace__))
6748
0
  SCTP_IPI_ITERATOR_WQ_DESTROY();
6749
0
  SCTP_ITERATOR_LOCK_DESTROY();
6750
0
#endif
6751
0
  SCTP_OS_TIMER_STOP_DRAIN(&SCTP_BASE_INFO(addr_wq_timer.timer));
6752
0
  SCTP_WQ_ADDR_LOCK();
6753
0
  LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
6754
0
    LIST_REMOVE(wi, sctp_nxt_addr);
6755
0
    SCTP_DECR_LADDR_COUNT();
6756
0
    if (wi->action == SCTP_DEL_IP_ADDRESS) {
6757
0
      SCTP_FREE(wi->ifa, SCTP_M_IFA);
6758
0
    }
6759
0
    SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_laddr), wi);
6760
0
  }
6761
0
  SCTP_WQ_ADDR_UNLOCK();
6762
6763
  /*
6764
   * free the vrf/ifn/ifa lists and hashes (be sure address monitor
6765
   * is destroyed first).
6766
   */
6767
0
  SCTP_IPI_ADDR_WLOCK();
6768
0
  vrf_bucket = &SCTP_BASE_INFO(sctp_vrfhash)[(SCTP_DEFAULT_VRFID & SCTP_BASE_INFO(hashvrfmark))];
6769
0
  LIST_FOREACH_SAFE(vrf, vrf_bucket, next_vrf, nvrf) {
6770
0
    LIST_FOREACH_SAFE(ifn, &vrf->ifnlist, next_ifn, nifn) {
6771
0
      LIST_FOREACH_SAFE(ifa, &ifn->ifalist, next_ifa, nifa) {
6772
        /* free the ifa */
6773
0
        LIST_REMOVE(ifa, next_bucket);
6774
0
        LIST_REMOVE(ifa, next_ifa);
6775
0
        SCTP_FREE(ifa, SCTP_M_IFA);
6776
0
      }
6777
      /* free the ifn */
6778
0
      LIST_REMOVE(ifn, next_bucket);
6779
0
      LIST_REMOVE(ifn, next_ifn);
6780
0
      SCTP_FREE(ifn, SCTP_M_IFN);
6781
0
    }
6782
0
    SCTP_HASH_FREE(vrf->vrf_addr_hash, vrf->vrf_addr_hashmark);
6783
    /* free the vrf */
6784
0
    LIST_REMOVE(vrf, next_vrf);
6785
0
    SCTP_FREE(vrf, SCTP_M_VRF);
6786
0
  }
6787
0
  SCTP_IPI_ADDR_WUNLOCK();
6788
  /* free the vrf hashes */
6789
0
  SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_vrfhash), SCTP_BASE_INFO(hashvrfmark));
6790
0
  SCTP_HASH_FREE(SCTP_BASE_INFO(vrf_ifn_hash), SCTP_BASE_INFO(vrf_ifn_hashmark));
6791
6792
  /* free the TIMEWAIT list elements malloc'd in the function
6793
   * sctp_add_vtag_to_timewait()...
6794
   */
6795
0
  for (i = 0; i < SCTP_STACK_VTAG_HASH_SIZE; i++) {
6796
0
    chain = &SCTP_BASE_INFO(vtag_timewait)[i];
6797
0
    if (!LIST_EMPTY(chain)) {
6798
0
      prev_twait_block = NULL;
6799
0
      LIST_FOREACH(twait_block, chain, sctp_nxt_tagblock) {
6800
0
        if (prev_twait_block) {
6801
0
          SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6802
0
        }
6803
0
        prev_twait_block = twait_block;
6804
0
      }
6805
0
      SCTP_FREE(prev_twait_block, SCTP_M_TIMW);
6806
0
    }
6807
0
  }
6808
6809
  /* free the locks and mutexes */
6810
#if defined(__APPLE__) && !defined(__Userspace__)
6811
  SCTP_TIMERQ_LOCK_DESTROY();
6812
#endif
6813
#ifdef SCTP_PACKET_LOGGING
6814
  SCTP_IP_PKTLOG_DESTROY();
6815
#endif
6816
0
  SCTP_IPI_ADDR_DESTROY();
6817
#if defined(__APPLE__) && !defined(__Userspace__)
6818
  SCTP_IPI_COUNT_DESTROY();
6819
#endif
6820
0
  SCTP_STATLOG_DESTROY();
6821
0
  SCTP_INP_INFO_LOCK_DESTROY();
6822
6823
0
  SCTP_WQ_ADDR_DESTROY();
6824
6825
#if defined(__APPLE__) && !defined(__Userspace__)
6826
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
6827
  lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_grp_attr);
6828
  lck_grp_free(SCTP_BASE_INFO(sctbinfo).mtx_grp);
6829
  lck_attr_free(SCTP_BASE_INFO(sctbinfo).mtx_attr);
6830
#else
6831
  lck_grp_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp_attr);
6832
  lck_grp_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_grp);
6833
  lck_attr_free(SCTP_BASE_INFO(sctbinfo).ipi_lock_attr);
6834
#endif
6835
#endif
6836
0
#if defined(__Userspace__)
6837
0
  SCTP_TIMERQ_LOCK_DESTROY();
6838
0
  SCTP_ZONE_DESTROY(zone_mbuf);
6839
0
  SCTP_ZONE_DESTROY(zone_clust);
6840
0
  SCTP_ZONE_DESTROY(zone_ext_refcnt);
6841
0
#endif
6842
  /* Get rid of other stuff too. */
6843
0
  if (SCTP_BASE_INFO(sctp_asochash) != NULL)
6844
0
    SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_asochash), SCTP_BASE_INFO(hashasocmark));
6845
0
  if (SCTP_BASE_INFO(sctp_ephash) != NULL)
6846
0
    SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_ephash), SCTP_BASE_INFO(hashmark));
6847
0
  if (SCTP_BASE_INFO(sctp_tcpephash) != NULL)
6848
0
    SCTP_HASH_FREE(SCTP_BASE_INFO(sctp_tcpephash), SCTP_BASE_INFO(hashtcpmark));
6849
6850
0
#if defined(_WIN32) || defined(__FreeBSD__) || defined(__Userspace__)
6851
0
  SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_ep));
6852
0
  SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asoc));
6853
0
  SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_laddr));
6854
0
  SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_net));
6855
0
  SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_chunk));
6856
0
  SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_readq));
6857
0
  SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_strmoq));
6858
0
  SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf));
6859
0
  SCTP_ZONE_DESTROY(SCTP_BASE_INFO(ipi_zone_asconf_ack));
6860
0
#endif
6861
#if defined(__FreeBSD__) && !defined(__Userspace__)
6862
#if defined(SMP) && defined(SCTP_USE_PERCPU_STAT)
6863
  SCTP_FREE(SCTP_BASE_STATS, SCTP_M_MCORE);
6864
#endif
6865
#endif
6866
0
}
6867
6868
int
6869
sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
6870
                              int offset, int limit,
6871
                              struct sockaddr *src, struct sockaddr *dst,
6872
                              struct sockaddr *altsa, uint16_t port)
6873
10.6k
{
6874
  /*
6875
   * grub through the INIT pulling addresses and loading them to the
6876
   * nets structure in the asoc. The from address in the mbuf should
6877
   * also be loaded (if it is not already). This routine can be called
6878
   * with either INIT or INIT-ACK's as long as the m points to the IP
6879
   * packet and the offset points to the beginning of the parameters.
6880
   */
6881
10.6k
  struct sctp_inpcb *inp;
6882
10.6k
  struct sctp_nets *net, *nnet, *net_tmp;
6883
10.6k
  struct sctp_paramhdr *phdr, param_buf;
6884
10.6k
  struct sctp_tcb *stcb_tmp;
6885
10.6k
  uint16_t ptype, plen;
6886
10.6k
  struct sockaddr *sa;
6887
10.6k
  uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
6888
10.6k
  struct sctp_auth_random *p_random = NULL;
6889
10.6k
  uint16_t random_len = 0;
6890
10.6k
  uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
6891
10.6k
  struct sctp_auth_hmac_algo *hmacs = NULL;
6892
10.6k
  uint16_t hmacs_len = 0;
6893
10.6k
  uint8_t saw_asconf = 0;
6894
10.6k
  uint8_t saw_asconf_ack = 0;
6895
10.6k
  uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
6896
10.6k
  struct sctp_auth_chunk_list *chunks = NULL;
6897
10.6k
  uint16_t num_chunks = 0;
6898
10.6k
  sctp_key_t *new_key;
6899
10.6k
  uint32_t keylen;
6900
10.6k
  int got_random = 0, got_hmacs = 0, got_chklist = 0;
6901
10.6k
  uint8_t peer_supports_ecn;
6902
10.6k
  uint8_t peer_supports_prsctp;
6903
10.6k
  uint8_t peer_supports_auth;
6904
10.6k
  uint8_t peer_supports_asconf;
6905
10.6k
  uint8_t peer_supports_asconf_ack;
6906
10.6k
  uint8_t peer_supports_reconfig;
6907
10.6k
  uint8_t peer_supports_nrsack;
6908
10.6k
  uint8_t peer_supports_pktdrop;
6909
10.6k
  uint8_t peer_supports_idata;
6910
10.6k
#ifdef INET
6911
10.6k
  struct sockaddr_in sin;
6912
10.6k
#endif
6913
10.6k
#ifdef INET6
6914
10.6k
  struct sockaddr_in6 sin6;
6915
10.6k
#endif
6916
6917
  /* First get the destination address setup too. */
6918
10.6k
#ifdef INET
6919
10.6k
  memset(&sin, 0, sizeof(sin));
6920
10.6k
  sin.sin_family = AF_INET;
6921
#ifdef HAVE_SIN_LEN
6922
  sin.sin_len = sizeof(sin);
6923
#endif
6924
10.6k
  sin.sin_port = stcb->rport;
6925
10.6k
#endif
6926
10.6k
#ifdef INET6
6927
10.6k
  memset(&sin6, 0, sizeof(sin6));
6928
10.6k
  sin6.sin6_family = AF_INET6;
6929
#ifdef HAVE_SIN6_LEN
6930
  sin6.sin6_len = sizeof(struct sockaddr_in6);
6931
#endif
6932
10.6k
  sin6.sin6_port = stcb->rport;
6933
10.6k
#endif
6934
10.6k
  if (altsa) {
6935
0
    sa = altsa;
6936
10.6k
  } else {
6937
10.6k
    sa = src;
6938
10.6k
  }
6939
10.6k
  peer_supports_idata = 0;
6940
10.6k
  peer_supports_ecn = 0;
6941
10.6k
  peer_supports_prsctp = 0;
6942
10.6k
  peer_supports_auth = 0;
6943
10.6k
  peer_supports_asconf = 0;
6944
10.6k
  peer_supports_asconf_ack = 0;
6945
10.6k
  peer_supports_reconfig = 0;
6946
10.6k
  peer_supports_nrsack = 0;
6947
10.6k
  peer_supports_pktdrop = 0;
6948
10.6k
  TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
6949
    /* mark all addresses that we have currently on the list */
6950
10.6k
    net->dest_state |= SCTP_ADDR_NOT_IN_ASSOC;
6951
10.6k
  }
6952
  /* does the source address already exist? if so skip it */
6953
10.6k
  inp = stcb->sctp_ep;
6954
10.6k
  atomic_add_int(&stcb->asoc.refcnt, 1);
6955
10.6k
  stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net_tmp, dst, stcb);
6956
10.6k
  atomic_subtract_int(&stcb->asoc.refcnt, 1);
6957
6958
10.6k
  if ((stcb_tmp == NULL && inp == stcb->sctp_ep) || inp == NULL) {
6959
    /* we must add the source address */
6960
    /* no scope set here since we have a tcb already. */
6961
0
    switch (sa->sa_family) {
6962
0
#ifdef INET
6963
0
    case AF_INET:
6964
0
      if (stcb->asoc.scope.ipv4_addr_legal) {
6965
0
        if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_2)) {
6966
0
          return (-1);
6967
0
        }
6968
0
      }
6969
0
      break;
6970
0
#endif
6971
0
#ifdef INET6
6972
0
    case AF_INET6:
6973
0
      if (stcb->asoc.scope.ipv6_addr_legal) {
6974
0
        if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
6975
0
          return (-2);
6976
0
        }
6977
0
      }
6978
0
      break;
6979
0
#endif
6980
0
#if defined(__Userspace__)
6981
0
    case AF_CONN:
6982
0
      if (stcb->asoc.scope.conn_addr_legal) {
6983
0
        if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_3)) {
6984
0
          return (-2);
6985
0
        }
6986
0
      }
6987
0
      break;
6988
0
#endif
6989
0
    default:
6990
0
      break;
6991
0
    }
6992
10.6k
  } else {
6993
10.6k
    if (net_tmp != NULL && stcb_tmp == stcb) {
6994
10.6k
      net_tmp->dest_state &= ~SCTP_ADDR_NOT_IN_ASSOC;
6995
10.6k
    } else if (stcb_tmp != stcb) {
6996
      /* It belongs to another association? */
6997
0
      if (stcb_tmp)
6998
0
        SCTP_TCB_UNLOCK(stcb_tmp);
6999
0
      return (-3);
7000
0
    }
7001
10.6k
  }
7002
10.6k
  if (stcb->asoc.state == 0) {
7003
    /* the assoc was freed? */
7004
0
    return (-4);
7005
0
  }
7006
  /* now we must go through each of the params. */
7007
10.6k
  phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
7008
131k
  while (phdr) {
7009
131k
    ptype = ntohs(phdr->param_type);
7010
131k
    plen = ntohs(phdr->param_length);
7011
    /*
7012
     * SCTP_PRINTF("ptype => %0x, plen => %d\n", (uint32_t)ptype,
7013
     * (int)plen);
7014
     */
7015
131k
    if (offset + plen > limit) {
7016
28
      break;
7017
28
    }
7018
130k
    if (plen < sizeof(struct sctp_paramhdr)) {
7019
2
      break;
7020
2
    }
7021
130k
#ifdef INET
7022
130k
    if (ptype == SCTP_IPV4_ADDRESS) {
7023
167
      if (stcb->asoc.scope.ipv4_addr_legal) {
7024
0
        struct sctp_ipv4addr_param *p4, p4_buf;
7025
7026
        /* ok get the v4 address and check/add */
7027
0
        phdr = sctp_get_next_param(m, offset,
7028
0
                 (struct sctp_paramhdr *)&p4_buf,
7029
0
                 sizeof(p4_buf));
7030
0
        if (plen != sizeof(struct sctp_ipv4addr_param) ||
7031
0
            phdr == NULL) {
7032
0
          return (-5);
7033
0
        }
7034
0
        p4 = (struct sctp_ipv4addr_param *)phdr;
7035
0
        sin.sin_addr.s_addr = p4->addr;
7036
0
        if (IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
7037
          /* Skip multi-cast addresses */
7038
0
          goto next_param;
7039
0
        }
7040
0
        if (in_broadcast(sin.sin_addr)) {
7041
0
          goto next_param;
7042
0
        }
7043
0
        sa = (struct sockaddr *)&sin;
7044
0
        inp = stcb->sctp_ep;
7045
0
        atomic_add_int(&stcb->asoc.refcnt, 1);
7046
0
        stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
7047
0
                  dst, stcb);
7048
0
        atomic_subtract_int(&stcb->asoc.refcnt, 1);
7049
7050
0
        if ((stcb_tmp == NULL && inp == stcb->sctp_ep) ||
7051
0
            inp == NULL) {
7052
          /* we must add the source address */
7053
          /*
7054
           * no scope set since we have a tcb
7055
           * already
7056
           */
7057
7058
          /*
7059
           * we must validate the state again
7060
           * here
7061
           */
7062
0
        add_it_now:
7063
0
          if (stcb->asoc.state == 0) {
7064
            /* the assoc was freed? */
7065
0
            return (-7);
7066
0
          }
7067
0
          if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_4)) {
7068
0
            return (-8);
7069
0
          }
7070
0
        } else if (stcb_tmp == stcb) {
7071
0
          if (stcb->asoc.state == 0) {
7072
            /* the assoc was freed? */
7073
0
            return (-10);
7074
0
          }
7075
0
          if (net != NULL) {
7076
            /* clear flag */
7077
0
            net->dest_state &=
7078
0
              ~SCTP_ADDR_NOT_IN_ASSOC;
7079
0
          }
7080
0
        } else {
7081
          /*
7082
           * strange, address is in another
7083
           * assoc? straighten out locks.
7084
           */
7085
0
          if (stcb_tmp) {
7086
0
            if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
7087
0
              struct mbuf *op_err;
7088
0
              char msg[SCTP_DIAG_INFO_LEN];
7089
7090
              /* in setup state we abort this guy */
7091
0
              SCTP_SNPRINTF(msg, sizeof(msg),
7092
0
                            "%s:%d at %s", __FILE__, __LINE__, __func__);
7093
0
              op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7094
0
                       msg);
7095
0
              sctp_abort_an_association(stcb_tmp->sctp_ep,
7096
0
                                        stcb_tmp, op_err, false,
7097
0
                                        SCTP_SO_NOT_LOCKED);
7098
0
              goto add_it_now;
7099
0
            }
7100
0
            SCTP_TCB_UNLOCK(stcb_tmp);
7101
0
          }
7102
7103
0
          if (stcb->asoc.state == 0) {
7104
            /* the assoc was freed? */
7105
0
            return (-12);
7106
0
          }
7107
0
          return (-13);
7108
0
        }
7109
0
      }
7110
167
    } else
7111
130k
#endif
7112
130k
#ifdef INET6
7113
130k
    if (ptype == SCTP_IPV6_ADDRESS) {
7114
43
      if (stcb->asoc.scope.ipv6_addr_legal) {
7115
        /* ok get the v6 address and check/add */
7116
0
        struct sctp_ipv6addr_param *p6, p6_buf;
7117
7118
0
        phdr = sctp_get_next_param(m, offset,
7119
0
                 (struct sctp_paramhdr *)&p6_buf,
7120
0
                 sizeof(p6_buf));
7121
0
        if (plen != sizeof(struct sctp_ipv6addr_param) ||
7122
0
            phdr == NULL) {
7123
0
          return (-14);
7124
0
        }
7125
0
        p6 = (struct sctp_ipv6addr_param *)phdr;
7126
0
        memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
7127
0
               sizeof(p6->addr));
7128
0
        if (IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
7129
          /* Skip multi-cast addresses */
7130
0
          goto next_param;
7131
0
        }
7132
0
        if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
7133
          /* Link local make no sense without scope */
7134
0
          goto next_param;
7135
0
        }
7136
0
        sa = (struct sockaddr *)&sin6;
7137
0
        inp = stcb->sctp_ep;
7138
0
        atomic_add_int(&stcb->asoc.refcnt, 1);
7139
0
        stcb_tmp = sctp_findassociation_ep_addr(&inp, sa, &net,
7140
0
                  dst, stcb);
7141
0
        atomic_subtract_int(&stcb->asoc.refcnt, 1);
7142
0
        if (stcb_tmp == NULL &&
7143
0
            (inp == stcb->sctp_ep || inp == NULL)) {
7144
          /*
7145
           * we must validate the state again
7146
           * here
7147
           */
7148
0
        add_it_now6:
7149
0
          if (stcb->asoc.state == 0) {
7150
            /* the assoc was freed? */
7151
0
            return (-16);
7152
0
          }
7153
          /*
7154
           * we must add the address, no scope
7155
           * set
7156
           */
7157
0
          if (sctp_add_remote_addr(stcb, sa, NULL, port, SCTP_DONOT_SETSCOPE, SCTP_LOAD_ADDR_5)) {
7158
0
            return (-17);
7159
0
          }
7160
0
        } else if (stcb_tmp == stcb) {
7161
          /*
7162
           * we must validate the state again
7163
           * here
7164
           */
7165
0
          if (stcb->asoc.state == 0) {
7166
            /* the assoc was freed? */
7167
0
            return (-19);
7168
0
          }
7169
0
          if (net != NULL) {
7170
            /* clear flag */
7171
0
            net->dest_state &=
7172
0
              ~SCTP_ADDR_NOT_IN_ASSOC;
7173
0
          }
7174
0
        } else {
7175
          /*
7176
           * strange, address is in another
7177
           * assoc? straighten out locks.
7178
           */
7179
0
          if (stcb_tmp) {
7180
0
            if (SCTP_GET_STATE(stcb_tmp) == SCTP_STATE_COOKIE_WAIT) {
7181
0
              struct mbuf *op_err;
7182
0
              char msg[SCTP_DIAG_INFO_LEN];
7183
7184
              /* in setup state we abort this guy */
7185
0
              SCTP_SNPRINTF(msg, sizeof(msg),
7186
0
                            "%s:%d at %s", __FILE__, __LINE__, __func__);
7187
0
              op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7188
0
                       msg);
7189
0
              sctp_abort_an_association(stcb_tmp->sctp_ep,
7190
0
                                        stcb_tmp, op_err, false,
7191
0
                                        SCTP_SO_NOT_LOCKED);
7192
0
              goto add_it_now6;
7193
0
            }
7194
0
            SCTP_TCB_UNLOCK(stcb_tmp);
7195
0
          }
7196
0
          if (stcb->asoc.state == 0) {
7197
            /* the assoc was freed? */
7198
0
            return (-21);
7199
0
          }
7200
0
          return (-22);
7201
0
        }
7202
0
      }
7203
43
    } else
7204
130k
#endif
7205
130k
    if (ptype == SCTP_ECN_CAPABLE) {
7206
14.8k
      peer_supports_ecn = 1;
7207
115k
    } else if (ptype == SCTP_ULP_ADAPTATION) {
7208
9.86k
      if (stcb->asoc.state != SCTP_STATE_OPEN) {
7209
9.86k
        struct sctp_adaptation_layer_indication ai, *aip;
7210
7211
9.86k
        phdr = sctp_get_next_param(m, offset,
7212
9.86k
                 (struct sctp_paramhdr *)&ai, sizeof(ai));
7213
9.86k
        aip = (struct sctp_adaptation_layer_indication *)phdr;
7214
9.86k
        if (aip) {
7215
9.86k
          stcb->asoc.peers_adaptation = ntohl(aip->indication);
7216
9.86k
          stcb->asoc.adaptation_needed = 1;
7217
9.86k
        }
7218
9.86k
      }
7219
106k
    } else if (ptype == SCTP_SET_PRIM_ADDR) {
7220
1.76k
      struct sctp_asconf_addr_param lstore, *fee;
7221
1.76k
      int lptype;
7222
1.76k
      struct sockaddr *lsa = NULL;
7223
1.76k
#ifdef INET
7224
1.76k
      struct sctp_asconf_addrv4_param *fii;
7225
1.76k
#endif
7226
7227
1.76k
      if (stcb->asoc.asconf_supported == 0) {
7228
0
        return (-100);
7229
0
      }
7230
1.76k
      if (plen > sizeof(lstore)) {
7231
0
        return (-23);
7232
0
      }
7233
1.76k
      if (plen < sizeof(struct sctp_asconf_addrv4_param)) {
7234
2
        return (-101);
7235
2
      }
7236
1.75k
      phdr = sctp_get_next_param(m, offset,
7237
1.75k
               (struct sctp_paramhdr *)&lstore,
7238
1.75k
               plen);
7239
1.75k
      if (phdr == NULL) {
7240
0
        return (-24);
7241
0
      }
7242
1.75k
      fee = (struct sctp_asconf_addr_param *)phdr;
7243
1.75k
      lptype = ntohs(fee->addrp.ph.param_type);
7244
1.75k
      switch (lptype) {
7245
0
#ifdef INET
7246
667
      case SCTP_IPV4_ADDRESS:
7247
667
        if (plen !=
7248
667
            sizeof(struct sctp_asconf_addrv4_param)) {
7249
92
          SCTP_PRINTF("Sizeof setprim in init/init ack not %d but %d - ignored\n",
7250
92
                (int)sizeof(struct sctp_asconf_addrv4_param),
7251
92
                plen);
7252
575
        } else {
7253
575
          fii = (struct sctp_asconf_addrv4_param *)fee;
7254
575
          sin.sin_addr.s_addr = fii->addrp.addr;
7255
575
          lsa = (struct sockaddr *)&sin;
7256
575
        }
7257
667
        break;
7258
0
#endif
7259
0
#ifdef INET6
7260
561
      case SCTP_IPV6_ADDRESS:
7261
561
        if (plen !=
7262
561
            sizeof(struct sctp_asconf_addr_param)) {
7263
73
          SCTP_PRINTF("Sizeof setprim (v6) in init/init ack not %d but %d - ignored\n",
7264
73
                (int)sizeof(struct sctp_asconf_addr_param),
7265
73
                plen);
7266
488
        } else {
7267
488
          memcpy(sin6.sin6_addr.s6_addr,
7268
488
                 fee->addrp.addr,
7269
488
                 sizeof(fee->addrp.addr));
7270
488
          lsa = (struct sockaddr *)&sin6;
7271
488
        }
7272
561
        break;
7273
0
#endif
7274
530
      default:
7275
530
        break;
7276
1.75k
      }
7277
1.75k
      if (lsa) {
7278
1.06k
        (void)sctp_set_primary_addr(stcb, sa, NULL);
7279
1.06k
      }
7280
104k
    } else if (ptype == SCTP_HAS_NAT_SUPPORT) {
7281
2.07k
      stcb->asoc.peer_supports_nat = 1;
7282
102k
    } else if (ptype == SCTP_PRSCTP_SUPPORTED) {
7283
      /* Peer supports pr-sctp */
7284
12.2k
      peer_supports_prsctp = 1;
7285
89.9k
    } else if (ptype == SCTP_ZERO_CHECKSUM_ACCEPTABLE) {
7286
836
      struct sctp_zero_checksum_acceptable zero_chksum, *zero_chksum_p;
7287
7288
836
      phdr = sctp_get_next_param(m, offset,
7289
836
                                 (struct sctp_paramhdr *)&zero_chksum,
7290
836
                                 sizeof(struct sctp_zero_checksum_acceptable));
7291
836
      if (phdr != NULL) {
7292
        /*
7293
         * Only send zero checksums if the upper layer
7294
         * has enabled the support for the same method
7295
         * as allowed by the peer.
7296
         */
7297
835
        zero_chksum_p = (struct sctp_zero_checksum_acceptable *)phdr;
7298
835
        if ((ntohl(zero_chksum_p->edmid) != SCTP_EDMID_NONE) &&
7299
835
            (ntohl(zero_chksum_p->edmid) == stcb->asoc.rcv_edmid)) {
7300
0
          stcb->asoc.snd_edmid = stcb->asoc.rcv_edmid;
7301
0
        }
7302
835
      }
7303
89.1k
    } else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
7304
      /* A supported extension chunk */
7305
21.5k
      struct sctp_supported_chunk_types_param *pr_supported;
7306
21.5k
      uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
7307
21.5k
      int num_ent, i;
7308
7309
21.5k
      if (plen > sizeof(local_store)) {
7310
0
        return (-35);
7311
0
      }
7312
21.5k
      phdr = sctp_get_next_param(m, offset,
7313
21.5k
               (struct sctp_paramhdr *)&local_store, plen);
7314
21.5k
      if (phdr == NULL) {
7315
0
        return (-25);
7316
0
      }
7317
21.5k
      pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
7318
21.5k
      num_ent = plen - sizeof(struct sctp_paramhdr);
7319
235k
      for (i = 0; i < num_ent; i++) {
7320
213k
        switch (pr_supported->chunk_types[i]) {
7321
12.2k
        case SCTP_ASCONF:
7322
12.2k
          peer_supports_asconf = 1;
7323
12.2k
          break;
7324
22.7k
        case SCTP_ASCONF_ACK:
7325
22.7k
          peer_supports_asconf_ack = 1;
7326
22.7k
          break;
7327
15.8k
        case SCTP_FORWARD_CUM_TSN:
7328
15.8k
          peer_supports_prsctp = 1;
7329
15.8k
          break;
7330
1.31k
        case SCTP_PACKET_DROPPED:
7331
1.31k
          peer_supports_pktdrop = 1;
7332
1.31k
          break;
7333
2.80k
        case SCTP_NR_SELECTIVE_ACK:
7334
2.80k
          peer_supports_nrsack = 1;
7335
2.80k
          break;
7336
12.3k
        case SCTP_STREAM_RESET:
7337
12.3k
          peer_supports_reconfig = 1;
7338
12.3k
          break;
7339
12.1k
        case SCTP_AUTHENTICATION:
7340
12.1k
          peer_supports_auth = 1;
7341
12.1k
          break;
7342
10.8k
        case SCTP_IDATA:
7343
10.8k
          peer_supports_idata = 1;
7344
10.8k
          break;
7345
123k
        default:
7346
          /* one I have not learned yet */
7347
123k
          break;
7348
213k
        }
7349
213k
      }
7350
67.6k
    } else if (ptype == SCTP_RANDOM) {
7351
9.92k
      if (plen > sizeof(random_store))
7352
0
        break;
7353
9.92k
      if (got_random) {
7354
        /* already processed a RANDOM */
7355
40
        goto next_param;
7356
40
      }
7357
9.88k
      phdr = sctp_get_next_param(m, offset,
7358
9.88k
               (struct sctp_paramhdr *)random_store,
7359
9.88k
               plen);
7360
9.88k
      if (phdr == NULL)
7361
0
        return (-26);
7362
9.88k
      p_random = (struct sctp_auth_random *)phdr;
7363
9.88k
      random_len = plen - sizeof(*p_random);
7364
      /* enforce the random length */
7365
9.88k
      if (random_len != SCTP_AUTH_RANDOM_SIZE_REQUIRED) {
7366
11
        SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: invalid RANDOM len\n");
7367
11
        return (-27);
7368
11
      }
7369
9.87k
      got_random = 1;
7370
57.7k
    } else if (ptype == SCTP_HMAC_LIST) {
7371
10.0k
      uint16_t num_hmacs;
7372
10.0k
      uint16_t i;
7373
7374
10.0k
      if (plen > sizeof(hmacs_store))
7375
1
        break;
7376
10.0k
      if (got_hmacs) {
7377
        /* already processed a HMAC list */
7378
156
        goto next_param;
7379
156
      }
7380
9.92k
      phdr = sctp_get_next_param(m, offset,
7381
9.92k
               (struct sctp_paramhdr *)hmacs_store,
7382
9.92k
               plen);
7383
9.92k
      if (phdr == NULL)
7384
0
        return (-28);
7385
9.92k
      hmacs = (struct sctp_auth_hmac_algo *)phdr;
7386
9.92k
      hmacs_len = plen - sizeof(*hmacs);
7387
9.92k
      num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
7388
      /* validate the hmac list */
7389
9.92k
      if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
7390
23
        return (-29);
7391
23
      }
7392
9.90k
      if (stcb->asoc.peer_hmacs != NULL)
7393
0
        sctp_free_hmaclist(stcb->asoc.peer_hmacs);
7394
9.90k
      stcb->asoc.peer_hmacs = sctp_alloc_hmaclist(num_hmacs);
7395
9.90k
      if (stcb->asoc.peer_hmacs != NULL) {
7396
32.2k
        for (i = 0; i < num_hmacs; i++) {
7397
22.3k
          (void)sctp_auth_add_hmacid(stcb->asoc.peer_hmacs,
7398
22.3k
                   ntohs(hmacs->hmac_ids[i]));
7399
22.3k
        }
7400
9.90k
      }
7401
9.90k
      got_hmacs = 1;
7402
47.6k
    } else if (ptype == SCTP_CHUNK_LIST) {
7403
12.1k
      int i;
7404
7405
12.1k
      if (plen > sizeof(chunks_store))
7406
0
        break;
7407
12.1k
      if (got_chklist) {
7408
        /* already processed a Chunks list */
7409
2.09k
        goto next_param;
7410
2.09k
      }
7411
10.0k
      phdr = sctp_get_next_param(m, offset,
7412
10.0k
               (struct sctp_paramhdr *)chunks_store,
7413
10.0k
               plen);
7414
10.0k
      if (phdr == NULL)
7415
0
        return (-30);
7416
10.0k
      chunks = (struct sctp_auth_chunk_list *)phdr;
7417
10.0k
      num_chunks = plen - sizeof(*chunks);
7418
10.0k
      if (stcb->asoc.peer_auth_chunks != NULL)
7419
0
        sctp_clear_chunklist(stcb->asoc.peer_auth_chunks);
7420
10.0k
      else
7421
10.0k
        stcb->asoc.peer_auth_chunks = sctp_alloc_chunklist();
7422
38.4k
      for (i = 0; i < num_chunks; i++) {
7423
28.4k
        (void)sctp_auth_add_chunk(chunks->chunk_types[i],
7424
28.4k
                stcb->asoc.peer_auth_chunks);
7425
        /* record asconf/asconf-ack if listed */
7426
28.4k
        if (chunks->chunk_types[i] == SCTP_ASCONF)
7427
10.0k
          saw_asconf = 1;
7428
28.4k
        if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
7429
10.1k
          saw_asconf_ack = 1;
7430
28.4k
      }
7431
10.0k
      got_chklist = 1;
7432
35.5k
    } else if ((ptype == SCTP_HEARTBEAT_INFO) ||
7433
35.5k
         (ptype == SCTP_STATE_COOKIE) ||
7434
35.5k
         (ptype == SCTP_UNRECOG_PARAM) ||
7435
35.5k
         (ptype == SCTP_COOKIE_PRESERVE) ||
7436
35.5k
         (ptype == SCTP_SUPPORTED_ADDRTYPE) ||
7437
35.5k
         (ptype == SCTP_ADD_IP_ADDRESS) ||
7438
35.5k
         (ptype == SCTP_DEL_IP_ADDRESS) ||
7439
35.5k
         (ptype == SCTP_ERROR_CAUSE_IND) ||
7440
35.5k
         (ptype == SCTP_SUCCESS_REPORT)) {
7441
      /* don't care */
7442
19.8k
    } else {
7443
15.6k
      if ((ptype & 0x8000) == 0x0000) {
7444
        /*
7445
         * must stop processing the rest of the
7446
         * param's. Any report bits were handled
7447
         * with the call to
7448
         * sctp_arethere_unrecognized_parameters()
7449
         * when the INIT or INIT-ACK was first seen.
7450
         */
7451
175
        break;
7452
175
      }
7453
15.6k
    }
7454
7455
130k
  next_param:
7456
130k
    offset += SCTP_SIZE32(plen);
7457
130k
    if (offset >= limit) {
7458
10.2k
      break;
7459
10.2k
    }
7460
120k
    phdr = sctp_get_next_param(m, offset, &param_buf,
7461
120k
                               sizeof(param_buf));
7462
120k
  }
7463
  /* Now check to see if we need to purge any addresses */
7464
10.5k
  TAILQ_FOREACH_SAFE(net, &stcb->asoc.nets, sctp_next, nnet) {
7465
10.5k
    if ((net->dest_state & SCTP_ADDR_NOT_IN_ASSOC) ==
7466
10.5k
        SCTP_ADDR_NOT_IN_ASSOC) {
7467
      /* This address has been removed from the asoc */
7468
      /* remove and free it */
7469
0
      stcb->asoc.numnets--;
7470
0
      TAILQ_REMOVE(&stcb->asoc.nets, net, sctp_next);
7471
0
      if (net == stcb->asoc.alternate) {
7472
0
        sctp_free_remote_addr(stcb->asoc.alternate);
7473
0
        stcb->asoc.alternate = NULL;
7474
0
      }
7475
0
      if (net == stcb->asoc.primary_destination) {
7476
0
        stcb->asoc.primary_destination = NULL;
7477
0
        sctp_select_primary_destination(stcb);
7478
0
      }
7479
0
      sctp_free_remote_addr(net);
7480
0
    }
7481
10.5k
  }
7482
10.5k
  if ((stcb->asoc.ecn_supported == 1) &&
7483
10.5k
      (peer_supports_ecn == 0)) {
7484
692
    stcb->asoc.ecn_supported = 0;
7485
692
  }
7486
10.5k
  if ((stcb->asoc.prsctp_supported == 1) &&
7487
10.5k
      (peer_supports_prsctp == 0)) {
7488
628
    stcb->asoc.prsctp_supported = 0;
7489
628
  }
7490
10.5k
  if ((stcb->asoc.auth_supported == 1) &&
7491
10.5k
      ((peer_supports_auth == 0) ||
7492
10.5k
       (got_random == 0) || (got_hmacs == 0))) {
7493
744
    stcb->asoc.auth_supported = 0;
7494
744
  }
7495
10.5k
  if ((stcb->asoc.asconf_supported == 1) &&
7496
10.5k
      ((peer_supports_asconf == 0) || (peer_supports_asconf_ack == 0) ||
7497
10.5k
       (stcb->asoc.auth_supported == 0) ||
7498
10.5k
       (saw_asconf == 0) || (saw_asconf_ack == 0))) {
7499
771
    stcb->asoc.asconf_supported = 0;
7500
771
  }
7501
10.5k
  if ((stcb->asoc.reconfig_supported == 1) &&
7502
10.5k
      (peer_supports_reconfig == 0)) {
7503
686
    stcb->asoc.reconfig_supported = 0;
7504
686
  }
7505
10.5k
  if ((stcb->asoc.idata_supported == 1) &&
7506
10.5k
      (peer_supports_idata == 0)) {
7507
541
    stcb->asoc.idata_supported = 0;
7508
541
  }
7509
10.5k
  if ((stcb->asoc.nrsack_supported == 1) &&
7510
10.5k
      (peer_supports_nrsack == 0)) {
7511
10.3k
    stcb->asoc.nrsack_supported = 0;
7512
10.3k
  }
7513
10.5k
  if ((stcb->asoc.pktdrop_supported == 1) &&
7514
10.5k
      (peer_supports_pktdrop == 0)) {
7515
10.5k
    stcb->asoc.pktdrop_supported = 0;
7516
10.5k
  }
7517
  /* validate authentication required parameters */
7518
10.5k
  if ((peer_supports_auth == 0) && (got_chklist == 1)) {
7519
    /* peer does not support auth but sent a chunks list? */
7520
101
    return (-31);
7521
101
  }
7522
10.4k
  if ((peer_supports_asconf == 1) && (peer_supports_auth == 0)) {
7523
    /* peer supports asconf but not auth? */
7524
37
    return (-32);
7525
10.4k
  } else if ((peer_supports_asconf == 1) &&
7526
10.4k
             (peer_supports_auth == 1) &&
7527
10.4k
       ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
7528
46
    return (-33);
7529
46
  }
7530
  /* concatenate the full random key */
7531
10.4k
  keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
7532
10.4k
  if (chunks != NULL) {
7533
9.88k
    keylen += sizeof(*chunks) + num_chunks;
7534
9.88k
  }
7535
10.4k
  new_key = sctp_alloc_key(keylen);
7536
10.4k
  if (new_key != NULL) {
7537
    /* copy in the RANDOM */
7538
10.4k
    if (p_random != NULL) {
7539
9.86k
      keylen = sizeof(*p_random) + random_len;
7540
9.86k
      memcpy(new_key->key, p_random, keylen);
7541
9.86k
    } else {
7542
548
      keylen = 0;
7543
548
    }
7544
    /* append in the AUTH chunks */
7545
10.4k
    if (chunks != NULL) {
7546
9.88k
      memcpy(new_key->key + keylen, chunks,
7547
9.88k
             sizeof(*chunks) + num_chunks);
7548
9.88k
      keylen += sizeof(*chunks) + num_chunks;
7549
9.88k
    }
7550
    /* append in the HMACs */
7551
10.4k
    if (hmacs != NULL) {
7552
9.88k
      memcpy(new_key->key + keylen, hmacs,
7553
9.88k
             sizeof(*hmacs) + hmacs_len);
7554
9.88k
    }
7555
10.4k
  } else {
7556
    /* failed to get memory for the key */
7557
0
    return (-34);
7558
0
  }
7559
10.4k
  if (stcb->asoc.authinfo.peer_random != NULL)
7560
0
    sctp_free_key(stcb->asoc.authinfo.peer_random);
7561
10.4k
  stcb->asoc.authinfo.peer_random = new_key;
7562
10.4k
  sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
7563
10.4k
  sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
7564
7565
10.4k
  return (0);
7566
10.4k
}
7567
7568
int
7569
sctp_set_primary_addr(struct sctp_tcb *stcb, struct sockaddr *sa,
7570
          struct sctp_nets *net)
7571
7.57k
{
7572
  /* make sure the requested primary address exists in the assoc */
7573
7.57k
  if (net == NULL && sa)
7574
7.57k
    net = sctp_findnet(stcb, sa);
7575
7576
7.57k
  if (net == NULL) {
7577
    /* didn't find the requested primary address! */
7578
3.85k
    return (-1);
7579
3.85k
  } else {
7580
    /* set the primary address */
7581
3.72k
    if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7582
      /* Must be confirmed, so queue to set */
7583
1.29k
      net->dest_state |= SCTP_ADDR_REQ_PRIMARY;
7584
1.29k
      return (0);
7585
1.29k
    }
7586
2.43k
    stcb->asoc.primary_destination = net;
7587
2.43k
    if (((net->dest_state & SCTP_ADDR_PF) == 0) &&
7588
2.43k
        (stcb->asoc.alternate != NULL)) {
7589
1
      sctp_free_remote_addr(stcb->asoc.alternate);
7590
1
      stcb->asoc.alternate = NULL;
7591
1
    }
7592
2.43k
    net = TAILQ_FIRST(&stcb->asoc.nets);
7593
2.43k
    if (net != stcb->asoc.primary_destination) {
7594
      /* first one on the list is NOT the primary
7595
       * sctp_cmpaddr() is much more efficient if
7596
       * the primary is the first on the list, make it
7597
       * so.
7598
       */
7599
593
      TAILQ_REMOVE(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7600
593
      TAILQ_INSERT_HEAD(&stcb->asoc.nets, stcb->asoc.primary_destination, sctp_next);
7601
593
    }
7602
2.43k
    return (0);
7603
2.43k
  }
7604
7.57k
}
7605
7606
bool
7607
sctp_is_vtag_good(uint32_t tag, uint16_t lport, uint16_t rport, struct timeval *now)
7608
35.1k
{
7609
35.1k
  struct sctpasochead *head;
7610
35.1k
  struct sctp_tcb *stcb;
7611
7612
35.1k
  SCTP_INP_INFO_LOCK_ASSERT();
7613
7614
35.1k
  head = &SCTP_BASE_INFO(sctp_asochash)[SCTP_PCBHASH_ASOC(tag, SCTP_BASE_INFO(hashasocmark))];
7615
35.1k
  LIST_FOREACH(stcb, head, sctp_asocs) {
7616
    /* We choose not to lock anything here. TCB's can't be
7617
     * removed since we have the read lock, so they can't
7618
     * be freed on us, same thing for the INP. I may
7619
     * be wrong with this assumption, but we will go
7620
     * with it for now :-)
7621
     */
7622
16
    if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) {
7623
0
      continue;
7624
0
    }
7625
16
    if (stcb->asoc.my_vtag == tag) {
7626
      /* candidate */
7627
0
      if (stcb->rport != rport) {
7628
0
        continue;
7629
0
      }
7630
0
      if (stcb->sctp_ep->sctp_lport != lport) {
7631
0
        continue;
7632
0
      }
7633
      /* The tag is currently used, so don't use it. */
7634
0
      return (false);
7635
0
    }
7636
16
  }
7637
35.1k
  return (!sctp_is_in_timewait(tag, lport, rport, now->tv_sec));
7638
35.1k
}
7639
7640
static void
7641
sctp_drain_mbufs(struct sctp_tcb *stcb)
7642
0
{
7643
  /*
7644
   * We must hunt this association for MBUF's past the cumack (i.e.
7645
   * out of order data that we can renege on).
7646
   */
7647
0
  struct sctp_association *asoc;
7648
0
  struct sctp_tmit_chunk *chk, *nchk;
7649
0
  uint32_t cumulative_tsn_p1;
7650
0
  struct sctp_queued_to_read *control, *ncontrol;
7651
0
  int cnt, strmat;
7652
0
  uint32_t gap, i;
7653
0
  int fnd = 0;
7654
7655
  /* We look for anything larger than the cum-ack + 1 */
7656
7657
0
  asoc = &stcb->asoc;
7658
0
  if (asoc->cumulative_tsn == asoc->highest_tsn_inside_map) {
7659
    /* none we can reneg on. */
7660
0
    return;
7661
0
  }
7662
0
  SCTP_STAT_INCR(sctps_protocol_drains_done);
7663
0
  cumulative_tsn_p1 = asoc->cumulative_tsn + 1;
7664
0
  cnt = 0;
7665
  /* Ok that was fun, now we will drain all the inbound streams? */
7666
0
  for (strmat = 0; strmat < asoc->streamincnt; strmat++) {
7667
0
    TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].inqueue, next_instrm, ncontrol) {
7668
0
#ifdef INVARIANTS
7669
0
      if (control->on_strm_q != SCTP_ON_ORDERED) {
7670
0
        panic("Huh control: %p on_q: %d -- not ordered?",
7671
0
              control, control->on_strm_q);
7672
0
      }
7673
0
#endif
7674
0
      if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
7675
        /* Yep it is above cum-ack */
7676
0
        cnt++;
7677
0
        SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7678
0
        KASSERT(control->length > 0, ("control has zero length"));
7679
0
        if (asoc->size_on_all_streams >= control->length) {
7680
0
          asoc->size_on_all_streams -= control->length;
7681
0
        } else {
7682
0
#ifdef INVARIANTS
7683
0
          panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7684
#else
7685
          asoc->size_on_all_streams = 0;
7686
#endif
7687
0
        }
7688
0
        sctp_ucount_decr(asoc->cnt_on_all_streams);
7689
0
        SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7690
0
        if (control->on_read_q) {
7691
0
          TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7692
0
          control->on_read_q = 0;
7693
0
        }
7694
0
        TAILQ_REMOVE(&asoc->strmin[strmat].inqueue, control, next_instrm);
7695
0
        control->on_strm_q = 0;
7696
0
        if (control->data) {
7697
0
          sctp_m_freem(control->data);
7698
0
          control->data = NULL;
7699
0
        }
7700
0
        sctp_free_remote_addr(control->whoFrom);
7701
        /* Now its reasm? */
7702
0
        TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
7703
0
          cnt++;
7704
0
          SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
7705
0
          KASSERT(chk->send_size > 0, ("chunk has zero length"));
7706
0
          if (asoc->size_on_reasm_queue >= chk->send_size) {
7707
0
            asoc->size_on_reasm_queue -= chk->send_size;
7708
0
          } else {
7709
0
#ifdef INVARIANTS
7710
0
            panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7711
#else
7712
            asoc->size_on_reasm_queue = 0;
7713
#endif
7714
0
          }
7715
0
          sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7716
0
          SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7717
0
          TAILQ_REMOVE(&control->reasm, chk, sctp_next);
7718
0
          if (chk->data) {
7719
0
            sctp_m_freem(chk->data);
7720
0
            chk->data = NULL;
7721
0
          }
7722
0
          sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7723
0
        }
7724
0
        sctp_free_a_readq(stcb, control);
7725
0
      }
7726
0
    }
7727
0
    TAILQ_FOREACH_SAFE(control, &asoc->strmin[strmat].uno_inqueue, next_instrm, ncontrol) {
7728
0
#ifdef INVARIANTS
7729
0
      if (control->on_strm_q != SCTP_ON_UNORDERED) {
7730
0
        panic("Huh control: %p on_q: %d -- not unordered?",
7731
0
              control, control->on_strm_q);
7732
0
      }
7733
0
#endif
7734
0
      if (SCTP_TSN_GT(control->sinfo_tsn, cumulative_tsn_p1)) {
7735
        /* Yep it is above cum-ack */
7736
0
        cnt++;
7737
0
        SCTP_CALC_TSN_TO_GAP(gap, control->sinfo_tsn, asoc->mapping_array_base_tsn);
7738
0
        KASSERT(control->length > 0, ("control has zero length"));
7739
0
        if (asoc->size_on_all_streams >= control->length) {
7740
0
          asoc->size_on_all_streams -= control->length;
7741
0
        } else {
7742
0
#ifdef INVARIANTS
7743
0
          panic("size_on_all_streams = %u smaller than control length %u", asoc->size_on_all_streams, control->length);
7744
#else
7745
          asoc->size_on_all_streams = 0;
7746
#endif
7747
0
        }
7748
0
        sctp_ucount_decr(asoc->cnt_on_all_streams);
7749
0
        SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7750
0
        if (control->on_read_q) {
7751
0
          TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
7752
0
          control->on_read_q = 0;
7753
0
        }
7754
0
        TAILQ_REMOVE(&asoc->strmin[strmat].uno_inqueue, control, next_instrm);
7755
0
        control->on_strm_q = 0;
7756
0
        if (control->data) {
7757
0
          sctp_m_freem(control->data);
7758
0
          control->data = NULL;
7759
0
        }
7760
0
        sctp_free_remote_addr(control->whoFrom);
7761
        /* Now its reasm? */
7762
0
        TAILQ_FOREACH_SAFE(chk, &control->reasm, sctp_next, nchk) {
7763
0
          cnt++;
7764
0
          SCTP_CALC_TSN_TO_GAP(gap, chk->rec.data.tsn, asoc->mapping_array_base_tsn);
7765
0
          KASSERT(chk->send_size > 0, ("chunk has zero length"));
7766
0
          if (asoc->size_on_reasm_queue >= chk->send_size) {
7767
0
            asoc->size_on_reasm_queue -= chk->send_size;
7768
0
          } else {
7769
0
#ifdef INVARIANTS
7770
0
            panic("size_on_reasm_queue = %u smaller than chunk length %u", asoc->size_on_reasm_queue, chk->send_size);
7771
#else
7772
            asoc->size_on_reasm_queue = 0;
7773
#endif
7774
0
          }
7775
0
          sctp_ucount_decr(asoc->cnt_on_reasm_queue);
7776
0
          SCTP_UNSET_TSN_PRESENT(asoc->mapping_array, gap);
7777
0
          TAILQ_REMOVE(&control->reasm, chk, sctp_next);
7778
0
          if (chk->data) {
7779
0
            sctp_m_freem(chk->data);
7780
0
            chk->data = NULL;
7781
0
          }
7782
0
          sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7783
0
        }
7784
0
        sctp_free_a_readq(stcb, control);
7785
0
      }
7786
0
    }
7787
0
  }
7788
0
  if (cnt) {
7789
    /* We must back down to see what the new highest is */
7790
0
    for (i = asoc->highest_tsn_inside_map; SCTP_TSN_GE(i, asoc->mapping_array_base_tsn); i--) {
7791
0
      SCTP_CALC_TSN_TO_GAP(gap, i, asoc->mapping_array_base_tsn);
7792
0
      if (SCTP_IS_TSN_PRESENT(asoc->mapping_array, gap)) {
7793
0
        asoc->highest_tsn_inside_map = i;
7794
0
        fnd = 1;
7795
0
        break;
7796
0
      }
7797
0
    }
7798
0
    if (!fnd) {
7799
0
      asoc->highest_tsn_inside_map = asoc->mapping_array_base_tsn - 1;
7800
0
    }
7801
7802
    /*
7803
     * Question, should we go through the delivery queue? The only
7804
     * reason things are on here is the app not reading OR a p-d-api up.
7805
     * An attacker COULD send enough in to initiate the PD-API and then
7806
     * send a bunch of stuff to other streams... these would wind up on
7807
     * the delivery queue.. and then we would not get to them. But in
7808
     * order to do this I then have to back-track and un-deliver
7809
     * sequence numbers in streams.. el-yucko. I think for now we will
7810
     * NOT look at the delivery queue and leave it to be something to
7811
     * consider later. An alternative would be to abort the P-D-API with
7812
     * a notification and then deliver the data.... Or another method
7813
     * might be to keep track of how many times the situation occurs and
7814
     * if we see a possible attack underway just abort the association.
7815
     */
7816
#ifdef SCTP_DEBUG
7817
    SCTPDBG(SCTP_DEBUG_PCB1, "Freed %d chunks from reneg harvest\n", cnt);
7818
#endif
7819
    /*
7820
     * Now do we need to find a new
7821
     * asoc->highest_tsn_inside_map?
7822
     */
7823
0
    asoc->last_revoke_count = cnt;
7824
0
    sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
7825
0
                    SCTP_FROM_SCTP_PCB + SCTP_LOC_11);
7826
    /*sa_ignore NO_NULL_CHK*/
7827
0
    sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
7828
0
    sctp_chunk_output(stcb->sctp_ep, stcb, SCTP_OUTPUT_FROM_DRAIN, SCTP_SO_NOT_LOCKED);
7829
0
  }
7830
  /*
7831
   * Another issue, in un-setting the TSN's in the mapping array we
7832
   * DID NOT adjust the highest_tsn marker.  This will cause one of two
7833
   * things to occur. It may cause us to do extra work in checking for
7834
   * our mapping array movement. More importantly it may cause us to
7835
   * SACK every datagram. This may not be a bad thing though since we
7836
   * will recover once we get our cum-ack above and all this stuff we
7837
   * dumped recovered.
7838
   */
7839
0
}
7840
7841
#if defined(__FreeBSD__) && !defined(__Userspace__)
7842
static void
7843
sctp_drain(void *arg __unused, int flags __unused)
7844
#else
7845
void
7846
sctp_drain(void)
7847
#endif
7848
0
{
7849
#if defined(__FreeBSD__) && !defined(__Userspace__)
7850
  struct epoch_tracker et;
7851
  VNET_ITERATOR_DECL(vnet_iter);
7852
7853
  NET_EPOCH_ENTER(et);
7854
#else
7855
0
  struct sctp_inpcb *inp;
7856
0
  struct sctp_tcb *stcb;
7857
7858
0
  SCTP_STAT_INCR(sctps_protocol_drain_calls);
7859
0
  if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7860
0
    return;
7861
0
  }
7862
0
#endif
7863
  /*
7864
   * We must walk the PCB lists for ALL associations here. The system
7865
   * is LOW on MBUF's and needs help. This is where reneging will
7866
   * occur. We really hope this does NOT happen!
7867
   */
7868
#if defined(__FreeBSD__) && !defined(__Userspace__)
7869
  VNET_LIST_RLOCK_NOSLEEP();
7870
  VNET_FOREACH(vnet_iter) {
7871
    CURVNET_SET(vnet_iter);
7872
    struct sctp_inpcb *inp;
7873
    struct sctp_tcb *stcb;
7874
#endif
7875
7876
#if defined(__FreeBSD__) && !defined(__Userspace__)
7877
    SCTP_STAT_INCR(sctps_protocol_drain_calls);
7878
    if (SCTP_BASE_SYSCTL(sctp_do_drain) == 0) {
7879
#ifdef VIMAGE
7880
      continue;
7881
#else
7882
      NET_EPOCH_EXIT(et);
7883
      return;
7884
#endif
7885
    }
7886
#endif
7887
0
    SCTP_INP_INFO_RLOCK();
7888
0
    LIST_FOREACH(inp, &SCTP_BASE_INFO(listhead), sctp_list) {
7889
      /* For each endpoint */
7890
0
      SCTP_INP_RLOCK(inp);
7891
0
      LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
7892
        /* For each association */
7893
0
        SCTP_TCB_LOCK(stcb);
7894
0
        sctp_drain_mbufs(stcb);
7895
0
        SCTP_TCB_UNLOCK(stcb);
7896
0
      }
7897
0
      SCTP_INP_RUNLOCK(inp);
7898
0
    }
7899
0
    SCTP_INP_INFO_RUNLOCK();
7900
#if defined(__FreeBSD__) && !defined(__Userspace__)
7901
    CURVNET_RESTORE();
7902
  }
7903
  VNET_LIST_RUNLOCK_NOSLEEP();
7904
  NET_EPOCH_EXIT(et);
7905
#endif
7906
0
}
7907
#if defined(__FreeBSD__) && !defined(__Userspace__)
7908
EVENTHANDLER_DEFINE(vm_lowmem, sctp_drain, NULL, LOWMEM_PRI_DEFAULT);
7909
EVENTHANDLER_DEFINE(mbuf_lowmem, sctp_drain, NULL, LOWMEM_PRI_DEFAULT);
7910
#endif
7911
7912
/*
7913
 * start a new iterator
7914
 * iterates through all endpoints and associations based on the pcb_state
7915
 * flags and asoc_state.  "af" (mandatory) is executed for all matching
7916
 * assocs and "ef" (optional) is executed when the iterator completes.
7917
 * "inpf" (optional) is executed for each new endpoint as it is being
7918
 * iterated through. inpe (optional) is called when the inp completes
7919
 * its way through all the stcbs.
7920
 */
7921
int
7922
sctp_initiate_iterator(inp_func inpf,
7923
           asoc_func af,
7924
           inp_func inpe,
7925
           uint32_t pcb_state,
7926
           uint32_t pcb_features,
7927
           uint32_t asoc_state,
7928
           void *argp,
7929
           uint32_t argi,
7930
           end_func ef,
7931
           struct sctp_inpcb *s_inp,
7932
           uint8_t chunk_output_off)
7933
0
{
7934
0
  struct sctp_iterator *it = NULL;
7935
7936
0
  if (af == NULL) {
7937
0
    return (-1);
7938
0
  }
7939
0
  if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
7940
0
    SCTP_PRINTF("%s: abort on initialize being %d\n", __func__,
7941
0
                SCTP_BASE_VAR(sctp_pcb_initialized));
7942
0
    return (-1);
7943
0
  }
7944
0
  SCTP_MALLOC(it, struct sctp_iterator *, sizeof(struct sctp_iterator),
7945
0
        SCTP_M_ITER);
7946
0
  if (it == NULL) {
7947
0
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_PCB, ENOMEM);
7948
0
    return (-1);
7949
0
  }
7950
0
  memset(it, 0, sizeof(*it));
7951
0
  it->function_assoc = af;
7952
0
  it->function_inp = inpf;
7953
0
  if (inpf)
7954
0
    it->done_current_ep = 0;
7955
0
  else
7956
0
    it->done_current_ep = 1;
7957
0
  it->function_atend = ef;
7958
0
  it->pointer = argp;
7959
0
  it->val = argi;
7960
0
  it->pcb_flags = pcb_state;
7961
0
  it->pcb_features = pcb_features;
7962
0
  it->asoc_state = asoc_state;
7963
0
  it->function_inp_end = inpe;
7964
0
  it->no_chunk_output = chunk_output_off;
7965
#if defined(__FreeBSD__) && !defined(__Userspace__)
7966
  it->vn = curvnet;
7967
#endif
7968
0
  if (s_inp) {
7969
    /* Assume lock is held here */
7970
0
    it->inp = s_inp;
7971
0
    SCTP_INP_INCR_REF(it->inp);
7972
0
    it->iterator_flags = SCTP_ITERATOR_DO_SINGLE_INP;
7973
0
  } else {
7974
0
    SCTP_INP_INFO_RLOCK();
7975
0
    it->inp = LIST_FIRST(&SCTP_BASE_INFO(listhead));
7976
0
    if (it->inp) {
7977
0
      SCTP_INP_INCR_REF(it->inp);
7978
0
    }
7979
0
    SCTP_INP_INFO_RUNLOCK();
7980
0
    it->iterator_flags = SCTP_ITERATOR_DO_ALL_INP;
7981
0
  }
7982
0
  SCTP_IPI_ITERATOR_WQ_LOCK();
7983
0
  if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
7984
0
    SCTP_IPI_ITERATOR_WQ_UNLOCK();
7985
0
    SCTP_PRINTF("%s: rollback on initialize being %d it=%p\n", __func__,
7986
0
                SCTP_BASE_VAR(sctp_pcb_initialized), it);
7987
0
    SCTP_FREE(it, SCTP_M_ITER);
7988
0
    return (-1);
7989
0
  }
7990
0
  TAILQ_INSERT_TAIL(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
7991
0
  if (sctp_it_ctl.iterator_running == 0) {
7992
0
    sctp_wakeup_iterator();
7993
0
  }
7994
0
  SCTP_IPI_ITERATOR_WQ_UNLOCK();
7995
  /* sa_ignore MEMLEAK {memory is put on the tailq for the iterator} */
7996
0
  return (0);
7997
0
}
7998
7999
/*
8000
 * Atomically add flags to the sctp_flags of an inp.
8001
 * To be used when the write lock of the inp is not held.
8002
 */
8003
void
8004
sctp_pcb_add_flags(struct sctp_inpcb *inp, uint32_t flags)
8005
9.26k
{
8006
9.26k
  uint32_t old_flags, new_flags;
8007
8008
9.26k
  do {
8009
9.26k
    old_flags = inp->sctp_flags;
8010
9.26k
    new_flags = old_flags | flags;
8011
9.26k
  } while (atomic_cmpset_int(&inp->sctp_flags, old_flags, new_flags) == 0);
8012
9.26k
}