Coverage Report

Created: 2024-07-27 06:19

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