Coverage Report

Created: 2025-10-28 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/usrsctp/usrsctplib/netinet6/sctp6_usrreq.c
Line
Count
Source
1
/*-
2
 * SPDX-License-Identifier: BSD-3-Clause
3
 *
4
 * Copyright (c) 2001-2007, 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
#ifdef INET6
37
#if defined(__FreeBSD__) && !defined(__Userspace__)
38
#include <sys/proc.h>
39
#endif
40
#include <netinet/sctp_pcb.h>
41
#include <netinet/sctp_header.h>
42
#include <netinet/sctp_var.h>
43
#include <netinet6/sctp6_var.h>
44
#include <netinet/sctp_sysctl.h>
45
#include <netinet/sctp_output.h>
46
#include <netinet/sctp_uio.h>
47
#include <netinet/sctp_asconf.h>
48
#include <netinet/sctputil.h>
49
#include <netinet/sctp_indata.h>
50
#include <netinet/sctp_timer.h>
51
#include <netinet/sctp_auth.h>
52
#include <netinet/sctp_input.h>
53
#include <netinet/sctp_output.h>
54
#include <netinet/sctp_bsd_addr.h>
55
#include <netinet/sctp_crc32.h>
56
#if !defined(_WIN32)
57
#include <netinet/icmp6.h>
58
#include <netinet/udp.h>
59
#endif
60
#if defined(__Userspace__)
61
int ip6_v6only=0;
62
#endif
63
#if defined(__Userspace__)
64
#ifdef INET
65
void
66
in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
67
0
{
68
#if defined(_WIN32)
69
  uint32_t temp;
70
#endif
71
0
  memset(sin, 0, sizeof(*sin));
72
#ifdef HAVE_SIN_LEN
73
  sin->sin_len = sizeof(struct sockaddr_in);
74
#endif
75
0
  sin->sin_family = AF_INET;
76
0
  sin->sin_port = sin6->sin6_port;
77
#if defined(_WIN32)
78
  temp = sin6->sin6_addr.s6_addr16[7];
79
  temp = temp << 16;
80
  temp = temp | sin6->sin6_addr.s6_addr16[6];
81
  sin->sin_addr.s_addr = temp;
82
#else
83
0
  sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3];
84
0
#endif
85
0
}
86
87
void
88
in6_sin6_2_sin_in_sock(struct sockaddr *nam)
89
0
{
90
0
  struct sockaddr_in *sin_p;
91
0
  struct sockaddr_in6 sin6;
92
93
  /* save original sockaddr_in6 addr and convert it to sockaddr_in  */
94
0
  sin6 = *(struct sockaddr_in6 *)nam;
95
0
  sin_p = (struct sockaddr_in *)nam;
96
0
  in6_sin6_2_sin(sin_p, &sin6);
97
0
}
98
99
void
100
in6_sin_2_v4mapsin6(const struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
101
0
{
102
0
  memset(sin6, 0, sizeof(struct sockaddr_in6));
103
0
  sin6->sin6_family = AF_INET6;
104
#ifdef HAVE_SIN6_LEN
105
  sin6->sin6_len = sizeof(struct sockaddr_in6);
106
#endif
107
0
  sin6->sin6_port = sin->sin_port;
108
#if defined(_WIN32)
109
  ((uint32_t *)&sin6->sin6_addr)[0] = 0;
110
  ((uint32_t *)&sin6->sin6_addr)[1] = 0;
111
  ((uint32_t *)&sin6->sin6_addr)[2] = htonl(0xffff);
112
  ((uint32_t *)&sin6->sin6_addr)[3] = sin->sin_addr.s_addr;
113
#else
114
0
  sin6->sin6_addr.s6_addr32[0] = 0;
115
0
  sin6->sin6_addr.s6_addr32[1] = 0;
116
0
  sin6->sin6_addr.s6_addr32[2] = htonl(0xffff);
117
0
  sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr;
118
0
#endif
119
0
}
120
#endif
121
#endif
122
123
#if !defined(__Userspace__)
124
int
125
#if defined(__APPLE__) || defined(__FreeBSD__)
126
sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
127
#else
128
sctp6_input(struct mbuf **i_pak, int *offp, int proto)
129
#endif
130
{
131
  struct mbuf *m;
132
  int iphlen;
133
  uint32_t vrf_id;
134
  uint8_t ecn_bits;
135
  struct sockaddr_in6 src, dst;
136
  struct ip6_hdr *ip6;
137
  struct sctphdr *sh;
138
  struct sctp_chunkhdr *ch;
139
  int length, offset;
140
  uint8_t compute_crc;
141
#if defined(__FreeBSD__)
142
  uint32_t mflowid;
143
  uint8_t mflowtype;
144
  uint16_t fibnum;
145
#endif
146
#if !(defined(__APPLE__) || defined(__FreeBSD__))
147
  uint16_t port = 0;
148
#endif
149
150
  iphlen = *offp;
151
  if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
152
    SCTP_RELEASE_PKT(*i_pak);
153
    return (IPPROTO_DONE);
154
  }
155
  m = SCTP_HEADER_TO_CHAIN(*i_pak);
156
#ifdef SCTP_MBUF_LOGGING
157
  /* Log in any input mbufs */
158
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
159
    sctp_log_mbc(m, SCTP_MBUF_INPUT);
160
  }
161
#endif
162
#ifdef SCTP_PACKET_LOGGING
163
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
164
    sctp_packet_log(m);
165
  }
166
#endif
167
#if defined(__FreeBSD__)
168
  SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
169
          "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
170
          m->m_pkthdr.len,
171
          if_name(m->m_pkthdr.rcvif),
172
          (int)m->m_pkthdr.csum_flags, CSUM_BITS);
173
#endif
174
#if defined(__APPLE__)
175
  SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
176
          "sctp6_input(): Packet of length %d received on %s%d with csum_flags 0x%x.\n",
177
          m->m_pkthdr.len,
178
          m->m_pkthdr.rcvif->if_name,
179
          m->m_pkthdr.rcvif->if_unit,
180
          m->m_pkthdr.csum_flags);
181
#endif
182
#if defined(_WIN32) && !defined(__Userspace__)
183
  SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
184
          "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%x.\n",
185
          m->m_pkthdr.len,
186
          m->m_pkthdr.rcvif->if_xname,
187
          m->m_pkthdr.csum_flags);
188
#endif
189
#if defined(__FreeBSD__)
190
  mflowid = m->m_pkthdr.flowid;
191
  mflowtype = M_HASHTYPE_GET(m);
192
  fibnum = M_GETFIB(m);
193
#endif
194
  SCTP_STAT_INCR(sctps_recvpackets);
195
  SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
196
  /* Get IP, SCTP, and first chunk header together in the first mbuf. */
197
  offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
198
  if (m->m_len < offset) {
199
    m = m_pullup(m, offset);
200
    if (m == NULL) {
201
      SCTP_STAT_INCR(sctps_hdrops);
202
      return (IPPROTO_DONE);
203
    }
204
  }
205
  ip6 = mtod(m, struct ip6_hdr *);
206
  sh = (struct sctphdr *)(mtod(m, caddr_t) + iphlen);
207
  ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
208
  offset -= sizeof(struct sctp_chunkhdr);
209
  memset(&src, 0, sizeof(struct sockaddr_in6));
210
  src.sin6_family = AF_INET6;
211
#ifdef HAVE_SIN6_LEN
212
  src.sin6_len = sizeof(struct sockaddr_in6);
213
#endif
214
  src.sin6_port = sh->src_port;
215
  src.sin6_addr = ip6->ip6_src;
216
#if defined(__FreeBSD__)
217
#if defined(__APPLE__)
218
  /* XXX: This code should also be used on Apple */
219
#endif
220
  if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
221
    goto out;
222
  }
223
#endif
224
  memset(&dst, 0, sizeof(struct sockaddr_in6));
225
  dst.sin6_family = AF_INET6;
226
#ifdef HAVE_SIN6_LEN
227
  dst.sin6_len = sizeof(struct sockaddr_in6);
228
#endif
229
  dst.sin6_port = sh->dest_port;
230
  dst.sin6_addr = ip6->ip6_dst;
231
#if defined(__FreeBSD__)
232
#if defined(__APPLE__)
233
  /* XXX: This code should also be used on Apple */
234
#endif
235
  if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
236
    goto out;
237
  }
238
#endif
239
#if defined(__APPLE__)
240
#if defined(NFAITH) && 0 < NFAITH
241
  if (faithprefix(&dst.sin6_addr)) {
242
    goto out;
243
  }
244
#endif
245
#endif
246
  length = ntohs(ip6->ip6_plen) + iphlen;
247
  /* Validate mbuf chain length with IP payload length. */
248
  if (SCTP_HEADER_LEN(m) != length) {
249
    SCTPDBG(SCTP_DEBUG_INPUT1,
250
            "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
251
    SCTP_STAT_INCR(sctps_hdrops);
252
    goto out;
253
  }
254
  if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
255
    goto out;
256
  }
257
#if defined(__FreeBSD__)
258
  ecn_bits = IPV6_TRAFFIC_CLASS(ip6);
259
  if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
260
    SCTP_STAT_INCR(sctps_recvhwcrc);
261
    compute_crc = 0;
262
  } else {
263
#else
264
  ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
265
  if (SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
266
      (IN6_ARE_ADDR_EQUAL(&src.sin6_addr, &dst.sin6_addr))) {
267
    SCTP_STAT_INCR(sctps_recvhwcrc);
268
    compute_crc = 0;
269
  } else {
270
#endif
271
    SCTP_STAT_INCR(sctps_recvswcrc);
272
    compute_crc = 1;
273
  }
274
  sctp_common_input_processing(&m, iphlen, offset, length,
275
                               (struct sockaddr *)&src,
276
                               (struct sockaddr *)&dst,
277
                               sh, ch,
278
                               compute_crc,
279
                               ecn_bits,
280
#if defined(__FreeBSD__)
281
                               mflowtype, mflowid, fibnum,
282
#endif
283
                               vrf_id, port);
284
 out:
285
  if (m) {
286
    sctp_m_freem(m);
287
  }
288
  return (IPPROTO_DONE);
289
}
290
291
#if defined(__APPLE__)
292
int
293
sctp6_input(struct mbuf **i_pak, int *offp)
294
{
295
  return (sctp6_input_with_port(i_pak, offp, 0));
296
}
297
#endif
298
#if defined(__FreeBSD__)
299
int
300
sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
301
{
302
  return (sctp6_input_with_port(i_pak, offp, 0));
303
}
304
#endif
305
306
void
307
sctp6_notify(struct sctp_inpcb *inp,
308
             struct sctp_tcb *stcb,
309
             struct sctp_nets *net,
310
             uint8_t icmp6_type,
311
             uint8_t icmp6_code,
312
             uint32_t next_mtu)
313
{
314
#if defined(__APPLE__)
315
  struct socket *so;
316
#endif
317
  int timer_stopped;
318
319
  switch (icmp6_type) {
320
  case ICMP6_DST_UNREACH:
321
    if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
322
        (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
323
        (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
324
        (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
325
      /* Mark the net unreachable. */
326
      if (net->dest_state & SCTP_ADDR_REACHABLE) {
327
        /* Ok that destination is not reachable */
328
        net->dest_state &= ~SCTP_ADDR_REACHABLE;
329
        net->dest_state &= ~SCTP_ADDR_PF;
330
        sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
331
                        stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
332
      }
333
    }
334
    SCTP_TCB_UNLOCK(stcb);
335
    break;
336
  case ICMP6_PARAM_PROB:
337
    /* Treat it like an ABORT. */
338
    if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
339
      sctp_abort_notification(stcb, true, false, 0, NULL, SCTP_SO_NOT_LOCKED);
340
#if defined(__APPLE__)
341
      so = SCTP_INP_SO(inp);
342
      atomic_add_int(&stcb->asoc.refcnt, 1);
343
      SCTP_TCB_UNLOCK(stcb);
344
      SCTP_SOCKET_LOCK(so, 1);
345
      SCTP_TCB_LOCK(stcb);
346
      atomic_subtract_int(&stcb->asoc.refcnt, 1);
347
#endif
348
      (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
349
                SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
350
#if defined(__APPLE__)
351
      SCTP_SOCKET_UNLOCK(so, 1);
352
#endif
353
    } else {
354
      SCTP_TCB_UNLOCK(stcb);
355
    }
356
    break;
357
  case ICMP6_PACKET_TOO_BIG:
358
    if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
359
      SCTP_TCB_UNLOCK(stcb);
360
      break;
361
    }
362
    if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
363
      timer_stopped = 1;
364
      sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
365
                      SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
366
    } else {
367
      timer_stopped = 0;
368
    }
369
    /* Update the path MTU. */
370
    if (net->port) {
371
      next_mtu -= sizeof(struct udphdr);
372
    }
373
    if (net->mtu > next_mtu) {
374
      net->mtu = next_mtu;
375
#if defined(__FreeBSD__)
376
      if (net->port) {
377
        sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu + sizeof(struct udphdr));
378
      } else {
379
        sctp_hc_set_mtu(&net->ro._l_addr, inp->fibnum, next_mtu);
380
      }
381
#endif
382
    }
383
    /* Update the association MTU */
384
    if (stcb->asoc.smallest_mtu > next_mtu) {
385
      sctp_pathmtu_adjustment(stcb, next_mtu, true);
386
    }
387
    /* Finally, start the PMTU timer if it was running before. */
388
    if (timer_stopped) {
389
      sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
390
    }
391
    SCTP_TCB_UNLOCK(stcb);
392
    break;
393
  default:
394
    SCTP_TCB_UNLOCK(stcb);
395
    break;
396
  }
397
}
398
399
#if defined(__FreeBSD__) && !defined(__Userspace__)
400
void
401
sctp6_ctlinput(struct ip6ctlparam *ip6cp)
402
{
403
  struct sctp_inpcb *inp;
404
  struct sctp_tcb *stcb;
405
  struct sctp_nets *net;
406
  struct sctphdr sh;
407
  struct sockaddr_in6 src, dst;
408
409
  if (icmp6_errmap(ip6cp->ip6c_icmp6) == 0) {
410
    return;
411
  }
412
413
  /*
414
   * Check if we can safely examine the ports and the
415
   * verification tag of the SCTP common header.
416
   */
417
  if (ip6cp->ip6c_m->m_pkthdr.len <
418
      (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
419
    return;
420
  }
421
422
  /* Copy out the port numbers and the verification tag. */
423
  memset(&sh, 0, sizeof(sh));
424
  m_copydata(ip6cp->ip6c_m,
425
             ip6cp->ip6c_off,
426
             sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
427
             (caddr_t)&sh);
428
  memset(&src, 0, sizeof(struct sockaddr_in6));
429
  src.sin6_family = AF_INET6;
430
  src.sin6_len = sizeof(struct sockaddr_in6);
431
  src.sin6_port = sh.src_port;
432
  src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
433
  if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
434
    return;
435
  }
436
  memset(&dst, 0, sizeof(struct sockaddr_in6));
437
  dst.sin6_family = AF_INET6;
438
  dst.sin6_len = sizeof(struct sockaddr_in6);
439
  dst.sin6_port = sh.dest_port;
440
  dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
441
  if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
442
    return;
443
  }
444
  inp = NULL;
445
  net = NULL;
446
  stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
447
                                      (struct sockaddr *)&src,
448
                                      &inp, &net, 1, SCTP_DEFAULT_VRFID);
449
  if ((stcb != NULL) &&
450
      (net != NULL) &&
451
      (inp != NULL)) {
452
    /* Check the verification tag */
453
    if (ntohl(sh.v_tag) != 0) {
454
      /*
455
       * This must be the verification tag used for
456
       * sending out packets. We don't consider
457
       * packets reflecting the verification tag.
458
       */
459
      if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
460
        SCTP_TCB_UNLOCK(stcb);
461
        return;
462
      }
463
    } else {
464
      if (ip6cp->ip6c_m->m_pkthdr.len >=
465
          ip6cp->ip6c_off + sizeof(struct sctphdr) +
466
                            sizeof(struct sctp_chunkhdr) +
467
                            offsetof(struct sctp_init, a_rwnd)) {
468
        /*
469
         * In this case we can check if we
470
         * got an INIT chunk and if the
471
         * initiate tag matches.
472
         */
473
        uint32_t initiate_tag;
474
        uint8_t chunk_type;
475
476
        m_copydata(ip6cp->ip6c_m,
477
                   ip6cp->ip6c_off +
478
                   sizeof(struct sctphdr),
479
                   sizeof(uint8_t),
480
                   (caddr_t)&chunk_type);
481
        m_copydata(ip6cp->ip6c_m,
482
                   ip6cp->ip6c_off +
483
                   sizeof(struct sctphdr) +
484
                   sizeof(struct sctp_chunkhdr),
485
                   sizeof(uint32_t),
486
                   (caddr_t)&initiate_tag);
487
        if ((chunk_type != SCTP_INITIATION) ||
488
            (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
489
          SCTP_TCB_UNLOCK(stcb);
490
          return;
491
        }
492
      } else {
493
        SCTP_TCB_UNLOCK(stcb);
494
        return;
495
      }
496
    }
497
    sctp6_notify(inp, stcb, net,
498
                 ip6cp->ip6c_icmp6->icmp6_type,
499
                 ip6cp->ip6c_icmp6->icmp6_code,
500
                 ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
501
  } else {
502
    if ((stcb == NULL) && (inp != NULL)) {
503
      /* reduce inp's ref-count */
504
      SCTP_INP_WLOCK(inp);
505
      SCTP_INP_DECR_REF(inp);
506
      SCTP_INP_WUNLOCK(inp);
507
    }
508
    if (stcb) {
509
      SCTP_TCB_UNLOCK(stcb);
510
    }
511
  }
512
}
513
#else
514
void
515
#if defined(__APPLE__) && !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) && !defined(APPLE_ELCAPITAN)
516
sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d, struct ifnet *ifp SCTP_UNUSED)
517
#else
518
sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
519
#endif
520
{
521
  struct ip6ctlparam *ip6cp;
522
  struct sctp_inpcb *inp;
523
  struct sctp_tcb *stcb;
524
  struct sctp_nets *net;
525
  struct sctphdr sh;
526
  struct sockaddr_in6 src, dst;
527
528
#ifdef HAVE_SA_LEN
529
  if (pktdst->sa_family != AF_INET6 ||
530
      pktdst->sa_len != sizeof(struct sockaddr_in6)) {
531
#else
532
  if (pktdst->sa_family != AF_INET6) {
533
#endif
534
    return;
535
  }
536
537
  if ((unsigned)cmd >= PRC_NCMDS) {
538
    return;
539
  }
540
  if (PRC_IS_REDIRECT(cmd)) {
541
    d = NULL;
542
  } else if (inet6ctlerrmap[cmd] == 0) {
543
    return;
544
  }
545
  /* If the parameter is from icmp6, decode it. */
546
  if (d != NULL) {
547
    ip6cp = (struct ip6ctlparam *)d;
548
  } else {
549
    ip6cp = (struct ip6ctlparam *)NULL;
550
  }
551
552
  if (ip6cp != NULL) {
553
    /*
554
     * XXX: We assume that when IPV6 is non NULL, M and OFF are
555
     * valid.
556
     */
557
    if (ip6cp->ip6c_m == NULL) {
558
      return;
559
    }
560
561
    /* Check if we can safely examine the ports and the
562
     * verification tag of the SCTP common header.
563
     */
564
    if (ip6cp->ip6c_m->m_pkthdr.len <
565
        (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
566
      return;
567
    }
568
569
    /* Copy out the port numbers and the verification tag. */
570
    memset(&sh, 0, sizeof(sh));
571
    m_copydata(ip6cp->ip6c_m,
572
               ip6cp->ip6c_off,
573
               sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
574
               (caddr_t)&sh);
575
    memset(&src, 0, sizeof(struct sockaddr_in6));
576
    src.sin6_family = AF_INET6;
577
#ifdef HAVE_SIN6_LEN
578
    src.sin6_len = sizeof(struct sockaddr_in6);
579
#endif
580
    src.sin6_port = sh.src_port;
581
    src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
582
    memset(&dst, 0, sizeof(struct sockaddr_in6));
583
    dst.sin6_family = AF_INET6;
584
#ifdef HAVE_SIN6_LEN
585
    dst.sin6_len = sizeof(struct sockaddr_in6);
586
#endif
587
    dst.sin6_port = sh.dest_port;
588
    dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
589
    inp = NULL;
590
    net = NULL;
591
    stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
592
                                        (struct sockaddr *)&src,
593
                                        &inp, &net, 1, SCTP_DEFAULT_VRFID);
594
    if ((stcb != NULL) &&
595
        (net != NULL) &&
596
        (inp != NULL)) {
597
      /* Check the verification tag */
598
      if (ntohl(sh.v_tag) != 0) {
599
        /*
600
         * This must be the verification tag used for
601
         * sending out packets. We don't consider
602
         * packets reflecting the verification tag.
603
         */
604
        if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
605
          SCTP_TCB_UNLOCK(stcb);
606
          return;
607
        }
608
      } else {
609
        SCTP_TCB_UNLOCK(stcb);
610
        return;
611
      }
612
      sctp6_notify(inp, stcb, net,
613
                   ip6cp->ip6c_icmp6->icmp6_type,
614
                   ip6cp->ip6c_icmp6->icmp6_code,
615
                   ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
616
#if defined(__Userspace__)
617
      if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
618
          (stcb->sctp_socket != NULL)) {
619
        struct socket *upcall_socket;
620
621
        upcall_socket = stcb->sctp_socket;
622
        SOCK_LOCK(upcall_socket);
623
        soref(upcall_socket);
624
        SOCK_UNLOCK(upcall_socket);
625
        if ((upcall_socket->so_upcall != NULL) &&
626
            (upcall_socket->so_error != 0)) {
627
          (*upcall_socket->so_upcall)(upcall_socket, upcall_socket->so_upcallarg, M_NOWAIT);
628
        }
629
        ACCEPT_LOCK();
630
        SOCK_LOCK(upcall_socket);
631
        sorele(upcall_socket);
632
      }
633
#endif
634
    } else {
635
      if ((stcb == NULL) && (inp != NULL)) {
636
        /* reduce inp's ref-count */
637
        SCTP_INP_WLOCK(inp);
638
        SCTP_INP_DECR_REF(inp);
639
        SCTP_INP_WUNLOCK(inp);
640
      }
641
      if (stcb) {
642
        SCTP_TCB_UNLOCK(stcb);
643
      }
644
    }
645
  }
646
}
647
#endif
648
#endif
649
650
/*
651
 * this routine can probably be collapsed into the one in sctp_userreq.c
652
 * since they do the same thing and now we lookup with a sockaddr
653
 */
654
#if defined(__FreeBSD__) && !defined(__Userspace__)
655
static int
656
sctp6_getcred(SYSCTL_HANDLER_ARGS)
657
{
658
  struct xucred xuc;
659
  struct sockaddr_in6 addrs[2];
660
  struct sctp_inpcb *inp;
661
  struct sctp_nets *net;
662
  struct sctp_tcb *stcb;
663
  int error;
664
  uint32_t vrf_id;
665
666
  vrf_id = SCTP_DEFAULT_VRFID;
667
668
  if (req->newptr == NULL)
669
    return (EINVAL);
670
  error = priv_check(req->td, PRIV_NETINET_GETCRED);
671
  if (error)
672
    return (error);
673
674
  if (req->newlen != sizeof(addrs)) {
675
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
676
    return (EINVAL);
677
  }
678
  if (req->oldlen != sizeof(struct ucred)) {
679
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
680
    return (EINVAL);
681
  }
682
  error = SYSCTL_IN(req, addrs, sizeof(addrs));
683
  if (error)
684
    return (error);
685
686
  stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
687
      sin6tosa(&addrs[0]),
688
      &inp, &net, 1, vrf_id);
689
  if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
690
    if ((inp != NULL) && (stcb == NULL)) {
691
      /* reduce ref-count */
692
      SCTP_INP_WLOCK(inp);
693
      SCTP_INP_DECR_REF(inp);
694
      goto cred_can_cont;
695
    }
696
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
697
    error = ENOENT;
698
    goto out;
699
  }
700
  SCTP_TCB_UNLOCK(stcb);
701
  /* We use the write lock here, only
702
   * since in the error leg we need it.
703
   * If we used RLOCK, then we would have
704
   * to wlock/decr/unlock/rlock. Which
705
   * in theory could create a hole. Better
706
   * to use higher wlock.
707
   */
708
  SCTP_INP_WLOCK(inp);
709
 cred_can_cont:
710
  error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
711
  if (error) {
712
    SCTP_INP_WUNLOCK(inp);
713
    goto out;
714
  }
715
  cru2x(inp->sctp_socket->so_cred, &xuc);
716
  SCTP_INP_WUNLOCK(inp);
717
  error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
718
out:
719
  return (error);
720
}
721
722
SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred,
723
    CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
724
    0, 0, sctp6_getcred, "S,ucred",
725
    "Get the ucred of a SCTP6 connection");
726
#endif
727
728
#if defined(__Userspace__)
729
int
730
sctp6_attach(struct socket *so, int proto SCTP_UNUSED, uint32_t vrf_id)
731
#elif defined(__FreeBSD__)
732
static int
733
sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
734
#elif defined(_WIN32)
735
static int
736
sctp6_attach(struct socket *so, int proto SCTP_UNUSED, PKTHREAD p SCTP_UNUSED)
737
#else
738
static int
739
sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct proc *p SCTP_UNUSED)
740
#endif
741
0
{
742
0
  int error;
743
0
  struct sctp_inpcb *inp;
744
#if !defined(__Userspace__)
745
  uint32_t vrf_id = SCTP_DEFAULT_VRFID;
746
#endif
747
748
0
  inp = (struct sctp_inpcb *)so->so_pcb;
749
0
  if (inp != NULL) {
750
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
751
0
    return (EINVAL);
752
0
  }
753
754
0
  if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
755
0
    error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
756
0
    if (error)
757
0
      return (error);
758
0
  }
759
0
  error = sctp_inpcb_alloc(so, vrf_id);
760
0
  if (error)
761
0
    return (error);
762
0
  inp = (struct sctp_inpcb *)so->so_pcb;
763
0
  SCTP_INP_WLOCK(inp);
764
0
  inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6; /* I'm v6! */
765
766
0
  inp->ip_inp.inp.inp_vflag |= INP_IPV6;
767
0
  inp->ip_inp.inp.in6p_hops = -1;  /* use kernel default */
768
0
  inp->ip_inp.inp.in6p_cksum = -1;  /* just to be sure */
769
0
#ifdef INET
770
  /*
771
   * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
772
   * socket as well, because the socket may be bound to an IPv6
773
   * wildcard address, which may match an IPv4-mapped IPv6 address.
774
   */
775
0
  inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
776
0
#endif
777
0
  SCTP_INP_WUNLOCK(inp);
778
0
  return (0);
779
0
}
780
781
#if defined(__Userspace__)
782
int
783
sctp6_bind(struct socket *so, struct sockaddr *addr, void * p)
784
0
{
785
#elif defined(__FreeBSD__)
786
static int
787
sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
788
{
789
#elif defined(__APPLE__)
790
static int
791
sctp6_bind(struct socket *so, struct sockaddr *addr, struct proc *p)
792
{
793
#elif defined(_WIN32)
794
static int
795
sctp6_bind(struct socket *so, struct sockaddr *addr, PKTHREAD p)
796
{
797
#else
798
static int
799
sctp6_bind(struct socket *so, struct mbuf *nam, struct proc *p)
800
{
801
  struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *): NULL;
802
803
#endif
804
0
  struct sctp_inpcb *inp;
805
0
  int error;
806
0
  u_char vflagsav;
807
808
0
  inp = (struct sctp_inpcb *)so->so_pcb;
809
0
  if (inp == NULL) {
810
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
811
0
    return (EINVAL);
812
0
  }
813
814
0
#if !(defined(_WIN32) && !defined(__Userspace__))
815
0
  if (addr) {
816
0
    switch (addr->sa_family) {
817
0
#ifdef INET
818
0
    case AF_INET:
819
#ifdef HAVE_SA_LEN
820
      if (addr->sa_len != sizeof(struct sockaddr_in)) {
821
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
822
        return (EINVAL);
823
      }
824
#endif
825
0
      break;
826
0
#endif
827
0
#ifdef INET6
828
0
    case AF_INET6:
829
#ifdef HAVE_SA_LEN
830
      if (addr->sa_len != sizeof(struct sockaddr_in6)) {
831
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
832
        return (EINVAL);
833
      }
834
#endif
835
0
      break;
836
0
#endif
837
0
    default:
838
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
839
0
      return (EINVAL);
840
0
    }
841
0
  }
842
0
#endif
843
0
  vflagsav = inp->ip_inp.inp.inp_vflag;
844
0
  inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
845
0
  inp->ip_inp.inp.inp_vflag |= INP_IPV6;
846
0
  if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
847
0
    switch (addr->sa_family) {
848
0
#ifdef INET
849
0
    case AF_INET:
850
      /* binding v4 addr to v6 socket, so reset flags */
851
0
      inp->ip_inp.inp.inp_vflag |= INP_IPV4;
852
0
      inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
853
0
      break;
854
0
#endif
855
0
#ifdef INET6
856
0
    case AF_INET6:
857
0
    {
858
0
      struct sockaddr_in6 *sin6_p;
859
860
0
      sin6_p = (struct sockaddr_in6 *)addr;
861
862
0
      if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
863
0
        inp->ip_inp.inp.inp_vflag |= INP_IPV4;
864
0
      }
865
0
#ifdef INET
866
0
      if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
867
0
        struct sockaddr_in sin;
868
869
0
        in6_sin6_2_sin(&sin, sin6_p);
870
0
        inp->ip_inp.inp.inp_vflag |= INP_IPV4;
871
0
        inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
872
0
        error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
873
0
        goto out;
874
0
      }
875
0
#endif
876
0
      break;
877
0
    }
878
0
#endif
879
0
    default:
880
0
      break;
881
0
    }
882
0
  } else if (addr != NULL) {
883
0
    struct sockaddr_in6 *sin6_p;
884
885
    /* IPV6_V6ONLY socket */
886
0
#ifdef INET
887
0
    if (addr->sa_family == AF_INET) {
888
      /* can't bind v4 addr to v6 only socket! */
889
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
890
0
      error = EINVAL;
891
0
      goto out;
892
0
    }
893
0
#endif
894
0
    sin6_p = (struct sockaddr_in6 *)addr;
895
896
0
    if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
897
      /* can't bind v4-mapped addrs either! */
898
      /* NOTE: we don't support SIIT */
899
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
900
0
      error = EINVAL;
901
0
      goto out;
902
0
    }
903
0
  }
904
0
  error = sctp_inpcb_bind(so, addr, NULL, p);
905
0
out:
906
0
  if (error != 0)
907
0
    inp->ip_inp.inp.inp_vflag = vflagsav;
908
0
  return (error);
909
0
}
910
911
#if defined(__FreeBSD__) || defined(_WIN32) || defined(__Userspace__)
912
#if !defined(__Userspace__)
913
static void
914
#else
915
void
916
#endif
917
sctp6_close(struct socket *so)
918
0
{
919
0
  sctp_close(so);
920
0
}
921
922
/* This could be made common with sctp_detach() since they are identical */
923
#else
924
925
static
926
int
927
sctp6_detach(struct socket *so)
928
{
929
#if defined(__Userspace__)
930
  sctp_close(so);
931
  return (0);
932
#else
933
  return (sctp_detach(so));
934
#endif
935
}
936
937
#endif
938
939
int
940
#if defined(__FreeBSD__) && !defined(__Userspace__)
941
sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
942
    struct mbuf *control, struct thread *p);
943
#else
944
sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
945
    struct mbuf *control, struct proc *p);
946
#endif
947
948
#if !defined(_WIN32) && !defined(__Userspace__)
949
#if defined(__FreeBSD__)
950
static int
951
sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
952
    struct mbuf *control, struct thread *p)
953
{
954
#elif defined(__APPLE__)
955
static int
956
sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
957
    struct mbuf *control, struct proc *p)
958
{
959
#else
960
static int
961
sctp6_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam,
962
    struct mbuf *control, struct proc *p)
963
{
964
  struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *): NULL;
965
#endif
966
  struct sctp_inpcb *inp;
967
968
#ifdef INET
969
  struct sockaddr_in6 *sin6;
970
#endif /* INET */
971
  /* No SPL needed since sctp_output does this */
972
973
  inp = (struct sctp_inpcb *)so->so_pcb;
974
  if (inp == NULL) {
975
    if (control) {
976
      SCTP_RELEASE_PKT(control);
977
      control = NULL;
978
    }
979
    SCTP_RELEASE_PKT(m);
980
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
981
    return (EINVAL);
982
  }
983
  /*
984
   * For the TCP model we may get a NULL addr, if we are a connected
985
   * socket thats ok.
986
   */
987
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
988
      (addr == NULL)) {
989
    goto connected_type;
990
  }
991
  if (addr == NULL) {
992
    SCTP_RELEASE_PKT(m);
993
    if (control) {
994
      SCTP_RELEASE_PKT(control);
995
      control = NULL;
996
    }
997
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
998
    return (EDESTADDRREQ);
999
  }
1000
  switch (addr->sa_family) {
1001
#ifdef INET
1002
  case AF_INET:
1003
#if defined(HAVE_SA_LEN)
1004
    if (addr->sa_len != sizeof(struct sockaddr_in)) {
1005
      if (control) {
1006
        SCTP_RELEASE_PKT(control);
1007
        control = NULL;
1008
      }
1009
      SCTP_RELEASE_PKT(m);
1010
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1011
      return (EINVAL);
1012
    }
1013
#endif
1014
    break;
1015
#endif
1016
#ifdef INET6
1017
  case AF_INET6:
1018
#if defined(HAVE_SA_LEN)
1019
    if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1020
      if (control) {
1021
        SCTP_RELEASE_PKT(control);
1022
        control = NULL;
1023
      }
1024
      SCTP_RELEASE_PKT(m);
1025
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1026
      return (EINVAL);
1027
    }
1028
#endif
1029
    break;
1030
#endif
1031
  default:
1032
    if (control) {
1033
      SCTP_RELEASE_PKT(control);
1034
      control = NULL;
1035
    }
1036
    SCTP_RELEASE_PKT(m);
1037
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1038
    return (EINVAL);
1039
  }
1040
#ifdef INET
1041
  sin6 = (struct sockaddr_in6 *)addr;
1042
  if (SCTP_IPV6_V6ONLY(inp)) {
1043
    /*
1044
     * if IPV6_V6ONLY flag, we discard datagrams destined to a
1045
     * v4 addr or v4-mapped addr
1046
     */
1047
    if (addr->sa_family == AF_INET) {
1048
      if (control) {
1049
        SCTP_RELEASE_PKT(control);
1050
        control = NULL;
1051
      }
1052
      SCTP_RELEASE_PKT(m);
1053
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1054
      return (EINVAL);
1055
    }
1056
    if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1057
      if (control) {
1058
        SCTP_RELEASE_PKT(control);
1059
        control = NULL;
1060
      }
1061
      SCTP_RELEASE_PKT(m);
1062
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1063
      return (EINVAL);
1064
    }
1065
  }
1066
  if ((addr->sa_family == AF_INET6) &&
1067
      IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1068
    struct sockaddr_in sin;
1069
1070
    /* convert v4-mapped into v4 addr and send */
1071
    in6_sin6_2_sin(&sin, sin6);
1072
    return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
1073
  }
1074
#endif        /* INET */
1075
connected_type:
1076
  /* now what about control */
1077
  if (control) {
1078
    if (inp->control) {
1079
      SCTP_PRINTF("huh? control set?\n");
1080
      SCTP_RELEASE_PKT(inp->control);
1081
      inp->control = NULL;
1082
    }
1083
    inp->control = control;
1084
  }
1085
  /* Place the data */
1086
  if (inp->pkt) {
1087
    SCTP_BUF_NEXT(inp->pkt_last) = m;
1088
    inp->pkt_last = m;
1089
  } else {
1090
    inp->pkt_last = inp->pkt = m;
1091
  }
1092
  if (
1093
#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
1094
  /* FreeBSD and MacOSX uses a flag passed */
1095
      ((flags & PRUS_MORETOCOME) == 0)
1096
#else
1097
      1     /* Open BSD does not have any "more to come"
1098
         * indication */
1099
#endif
1100
      ) {
1101
    /*
1102
     * note with the current version this code will only be used
1103
     * by OpenBSD, NetBSD and FreeBSD have methods for
1104
     * re-defining sosend() to use sctp_sosend().  One can
1105
     * optionally switch back to this code (by changing back the
1106
     * definitions but this is not advisable.
1107
     */
1108
#if defined(__FreeBSD__) && !defined(__Userspace__)
1109
    struct epoch_tracker et;
1110
#endif
1111
    int ret;
1112
1113
#if defined(__FreeBSD__) && !defined(__Userspace__)
1114
  NET_EPOCH_ENTER(et);
1115
#endif
1116
    ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
1117
#if defined(__FreeBSD__) && !defined(__Userspace__)
1118
  NET_EPOCH_EXIT(et);
1119
#endif
1120
    inp->pkt = NULL;
1121
    inp->control = NULL;
1122
    return (ret);
1123
  } else {
1124
    return (0);
1125
  }
1126
}
1127
#endif
1128
1129
#if defined(__Userspace__)
1130
int
1131
sctp6_connect(struct socket *so, struct sockaddr *addr)
1132
0
{
1133
0
  void *p = NULL;
1134
#elif defined(__FreeBSD__)
1135
static int
1136
sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
1137
{
1138
#elif defined(__APPLE__)
1139
static int
1140
sctp6_connect(struct socket *so, struct sockaddr *addr, struct proc *p)
1141
{
1142
#elif defined(_WIN32)
1143
static int
1144
sctp6_connect(struct socket *so, struct sockaddr *addr, PKTHREAD p)
1145
{
1146
#else
1147
static int
1148
sctp6_connect(struct socket *so, struct mbuf *nam, struct proc *p)
1149
{
1150
  struct sockaddr *addr = mtod(nam, struct sockaddr *);
1151
#endif
1152
#if defined(__FreeBSD__) && !defined(__Userspace__)
1153
  struct epoch_tracker et;
1154
#endif
1155
0
  uint32_t vrf_id;
1156
0
  int error = 0;
1157
0
  struct sctp_inpcb *inp;
1158
0
  struct sctp_tcb *stcb;
1159
0
#ifdef INET
1160
0
  struct sockaddr_in6 *sin6;
1161
0
  union sctp_sockstore store;
1162
0
#endif
1163
1164
0
  inp = (struct sctp_inpcb *)so->so_pcb;
1165
0
  if (inp == NULL) {
1166
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1167
0
    return (ECONNRESET);  /* I made the same as TCP since we are
1168
           * not setup? */
1169
0
  }
1170
0
  if (addr == NULL) {
1171
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1172
0
    return (EINVAL);
1173
0
  }
1174
0
#if !(defined(_WIN32) && !defined(__Userspace__))
1175
0
  switch (addr->sa_family) {
1176
0
#ifdef INET
1177
0
  case AF_INET:
1178
#ifdef HAVE_SA_LEN
1179
    if (addr->sa_len != sizeof(struct sockaddr_in)) {
1180
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1181
      return (EINVAL);
1182
    }
1183
#endif
1184
0
    break;
1185
0
#endif
1186
0
#ifdef INET6
1187
0
  case AF_INET6:
1188
#ifdef HAVE_SA_LEN
1189
    if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1190
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1191
      return (EINVAL);
1192
    }
1193
#endif
1194
0
    break;
1195
0
#endif
1196
0
  default:
1197
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1198
0
    return (EINVAL);
1199
0
  }
1200
0
#endif
1201
1202
0
  vrf_id = inp->def_vrf_id;
1203
0
  SCTP_ASOC_CREATE_LOCK(inp);
1204
0
  SCTP_INP_RLOCK(inp);
1205
0
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1206
0
      SCTP_PCB_FLAGS_UNBOUND) {
1207
    /* Bind a ephemeral port */
1208
0
    SCTP_INP_RUNLOCK(inp);
1209
0
    error = sctp6_bind(so, NULL, p);
1210
0
    if (error) {
1211
0
      SCTP_ASOC_CREATE_UNLOCK(inp);
1212
1213
0
      return (error);
1214
0
    }
1215
0
    SCTP_INP_RLOCK(inp);
1216
0
  }
1217
0
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1218
0
      (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1219
    /* We are already connected AND the TCP model */
1220
0
    SCTP_INP_RUNLOCK(inp);
1221
0
    SCTP_ASOC_CREATE_UNLOCK(inp);
1222
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
1223
0
    return (EADDRINUSE);
1224
0
  }
1225
0
#ifdef INET
1226
0
  sin6 = (struct sockaddr_in6 *)addr;
1227
0
  if (SCTP_IPV6_V6ONLY(inp)) {
1228
    /*
1229
     * if IPV6_V6ONLY flag, ignore connections destined to a v4
1230
     * addr or v4-mapped addr
1231
     */
1232
0
    if (addr->sa_family == AF_INET) {
1233
0
      SCTP_INP_RUNLOCK(inp);
1234
0
      SCTP_ASOC_CREATE_UNLOCK(inp);
1235
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1236
0
      return (EINVAL);
1237
0
    }
1238
0
    if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1239
0
      SCTP_INP_RUNLOCK(inp);
1240
0
      SCTP_ASOC_CREATE_UNLOCK(inp);
1241
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1242
0
      return (EINVAL);
1243
0
    }
1244
0
  }
1245
0
  if ((addr->sa_family == AF_INET6) &&
1246
0
      IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1247
    /* convert v4-mapped into v4 addr */
1248
0
    in6_sin6_2_sin(&store.sin, sin6);
1249
0
    addr = &store.sa;
1250
0
  }
1251
0
#endif        /* INET */
1252
  /* Now do we connect? */
1253
0
  if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1254
0
    stcb = LIST_FIRST(&inp->sctp_asoc_list);
1255
0
    if (stcb) {
1256
0
      SCTP_TCB_LOCK(stcb);
1257
0
    }
1258
0
    SCTP_INP_RUNLOCK(inp);
1259
0
  } else {
1260
0
    SCTP_INP_RUNLOCK(inp);
1261
0
    SCTP_INP_WLOCK(inp);
1262
0
    SCTP_INP_INCR_REF(inp);
1263
0
    SCTP_INP_WUNLOCK(inp);
1264
0
    stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
1265
0
    if (stcb == NULL) {
1266
0
      SCTP_INP_WLOCK(inp);
1267
0
      SCTP_INP_DECR_REF(inp);
1268
0
      SCTP_INP_WUNLOCK(inp);
1269
0
    }
1270
0
  }
1271
1272
0
  if (stcb != NULL) {
1273
    /* Already have or am bring up an association */
1274
0
    SCTP_ASOC_CREATE_UNLOCK(inp);
1275
0
    SCTP_TCB_UNLOCK(stcb);
1276
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
1277
0
    return (EALREADY);
1278
0
  }
1279
  /* We are GOOD to go */
1280
0
  stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
1281
0
                                   inp->sctp_ep.pre_open_stream_count,
1282
0
                                   inp->sctp_ep.port, p,
1283
0
                                   SCTP_INITIALIZE_AUTH_PARAMS);
1284
0
  SCTP_ASOC_CREATE_UNLOCK(inp);
1285
0
  if (stcb == NULL) {
1286
    /* Gak! no memory */
1287
0
    return (error);
1288
0
  }
1289
0
  SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
1290
0
  (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1291
#if defined(__FreeBSD__) && !defined(__Userspace__)
1292
  NET_EPOCH_ENTER(et);
1293
#endif
1294
0
  sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
1295
0
  SCTP_TCB_UNLOCK(stcb);
1296
#if defined(__FreeBSD__) && !defined(__Userspace__)
1297
  NET_EPOCH_EXIT(et);
1298
#endif
1299
0
  return (error);
1300
0
}
1301
1302
static int
1303
#if !defined(__Userspace__)
1304
sctp6_getaddr(struct socket *so, struct sockaddr **addr)
1305
{
1306
  struct sockaddr_in6 *sin6;
1307
#else
1308
sctp6_getaddr(struct socket *so, struct mbuf *nam)
1309
0
{
1310
0
  struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
1311
0
#endif
1312
0
  struct sctp_inpcb *inp;
1313
0
  uint32_t vrf_id;
1314
0
  struct sctp_ifa *sctp_ifa;
1315
1316
#if defined(SCTP_KAME) && defined(SCTP_EMBEDDED_V6_SCOPE)
1317
  int error;
1318
#endif
1319
1320
  /*
1321
   * Do the malloc first in case it blocks.
1322
   */
1323
#if !defined(__Userspace__)
1324
  SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
1325
  if (sin6 == NULL)
1326
    return (ENOMEM);
1327
#else
1328
0
  SCTP_BUF_LEN(nam) = sizeof(*sin6);
1329
0
  memset(sin6, 0, sizeof(*sin6));
1330
0
#endif
1331
0
  sin6->sin6_family = AF_INET6;
1332
#ifdef HAVE_SIN6_LEN
1333
  sin6->sin6_len = sizeof(*sin6);
1334
#endif
1335
1336
0
  inp = (struct sctp_inpcb *)so->so_pcb;
1337
0
  if (inp == NULL) {
1338
#if !defined(__Userspace__)
1339
    SCTP_FREE_SONAME(sin6);
1340
#endif
1341
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1342
0
    return (ECONNRESET);
1343
0
  }
1344
0
  SCTP_INP_RLOCK(inp);
1345
0
  sin6->sin6_port = inp->sctp_lport;
1346
0
  if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1347
    /* For the bound all case you get back 0 */
1348
0
    if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1349
0
      struct sctp_tcb *stcb;
1350
0
      struct sockaddr_in6 *sin_a6;
1351
0
      struct sctp_nets *net;
1352
0
      int fnd;
1353
0
      stcb = LIST_FIRST(&inp->sctp_asoc_list);
1354
0
      if (stcb == NULL) {
1355
0
        SCTP_INP_RUNLOCK(inp);
1356
#if !defined(__Userspace__)
1357
        SCTP_FREE_SONAME(sin6);
1358
#endif
1359
0
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1360
0
        return (ENOENT);
1361
0
      }
1362
0
      fnd = 0;
1363
0
      sin_a6 = NULL;
1364
0
      TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1365
0
        sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1366
0
        if (sin_a6 == NULL)
1367
          /* this will make coverity happy */
1368
0
          continue;
1369
1370
0
        if (sin_a6->sin6_family == AF_INET6) {
1371
0
          fnd = 1;
1372
0
          break;
1373
0
        }
1374
0
      }
1375
0
      if ((!fnd) || (sin_a6 == NULL)) {
1376
        /* punt */
1377
0
        SCTP_INP_RUNLOCK(inp);
1378
#if !defined(__Userspace__)
1379
        SCTP_FREE_SONAME(sin6);
1380
#endif
1381
0
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1382
0
        return (ENOENT);
1383
0
      }
1384
0
      vrf_id = inp->def_vrf_id;
1385
0
      sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
1386
0
      if (sctp_ifa) {
1387
0
        sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
1388
0
      }
1389
0
    } else {
1390
      /* For the bound all case you get back 0 */
1391
0
      memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1392
0
    }
1393
0
  } else {
1394
    /* Take the first IPv6 address in the list */
1395
0
    struct sctp_laddr *laddr;
1396
0
    int fnd = 0;
1397
1398
0
    LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1399
0
      if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1400
0
        struct sockaddr_in6 *sin_a;
1401
1402
0
        sin_a = &laddr->ifa->address.sin6;
1403
0
        sin6->sin6_addr = sin_a->sin6_addr;
1404
0
        fnd = 1;
1405
0
        break;
1406
0
      }
1407
0
    }
1408
0
    if (!fnd) {
1409
#if !defined(__Userspace__)
1410
      SCTP_FREE_SONAME(sin6);
1411
#endif
1412
0
      SCTP_INP_RUNLOCK(inp);
1413
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1414
0
      return (ENOENT);
1415
0
    }
1416
0
  }
1417
0
  SCTP_INP_RUNLOCK(inp);
1418
  /* Scoping things for v6 */
1419
#ifdef SCTP_EMBEDDED_V6_SCOPE
1420
#ifdef SCTP_KAME
1421
  if ((error = sa6_recoverscope(sin6)) != 0) {
1422
    SCTP_FREE_SONAME(sin6);
1423
    return (error);
1424
  }
1425
#else
1426
  if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1427
    /* skip ifp check below */
1428
    in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
1429
  else
1430
    sin6->sin6_scope_id = 0;  /* XXX */
1431
#endif /* SCTP_KAME */
1432
#endif /* SCTP_EMBEDDED_V6_SCOPE */
1433
#if !defined(__Userspace__)
1434
  (*addr) = (struct sockaddr *)sin6;
1435
#endif
1436
0
  return (0);
1437
0
}
1438
1439
static int
1440
#if !defined(__Userspace__)
1441
sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1442
{
1443
  struct sockaddr_in6 *sin6;
1444
#else
1445
sctp6_peeraddr(struct socket *so, struct mbuf *nam)
1446
0
{
1447
0
  struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
1448
0
#endif
1449
0
  int fnd;
1450
0
  struct sockaddr_in6 *sin_a6;
1451
0
  struct sctp_inpcb *inp;
1452
0
  struct sctp_tcb *stcb;
1453
0
  struct sctp_nets *net;
1454
#ifdef SCTP_KAME
1455
  int error;
1456
#endif
1457
1458
  /* Do the malloc first in case it blocks. */
1459
#if !defined(__Userspace__)
1460
  SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1461
  if (sin6 == NULL)
1462
    return (ENOMEM);
1463
#else
1464
0
  SCTP_BUF_LEN(nam) = sizeof(*sin6);
1465
0
  memset(sin6, 0, sizeof(*sin6));
1466
0
#endif
1467
0
  sin6->sin6_family = AF_INET6;
1468
#ifdef HAVE_SIN6_LEN
1469
  sin6->sin6_len = sizeof(*sin6);
1470
#endif
1471
1472
0
  inp = (struct sctp_inpcb *)so->so_pcb;
1473
0
  if ((inp == NULL) ||
1474
0
      ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1475
    /* UDP type and listeners will drop out here */
1476
#if !defined(__Userspace__)
1477
    SCTP_FREE_SONAME(sin6);
1478
#endif
1479
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1480
0
    return (ENOTCONN);
1481
0
  }
1482
0
  SCTP_INP_RLOCK(inp);
1483
0
  stcb = LIST_FIRST(&inp->sctp_asoc_list);
1484
0
  if (stcb) {
1485
0
    SCTP_TCB_LOCK(stcb);
1486
0
  }
1487
0
  SCTP_INP_RUNLOCK(inp);
1488
0
  if (stcb == NULL) {
1489
#if !defined(__Userspace__)
1490
    SCTP_FREE_SONAME(sin6);
1491
#endif
1492
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1493
0
    return (ECONNRESET);
1494
0
  }
1495
0
  fnd = 0;
1496
0
  TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1497
0
    sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1498
0
    if (sin_a6->sin6_family == AF_INET6) {
1499
0
      fnd = 1;
1500
0
      sin6->sin6_port = stcb->rport;
1501
0
      sin6->sin6_addr = sin_a6->sin6_addr;
1502
0
      break;
1503
0
    }
1504
0
  }
1505
0
  SCTP_TCB_UNLOCK(stcb);
1506
0
  if (!fnd) {
1507
    /* No IPv4 address */
1508
#if !defined(__Userspace__)
1509
    SCTP_FREE_SONAME(sin6);
1510
#endif
1511
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1512
0
    return (ENOENT);
1513
0
  }
1514
#ifdef SCTP_EMBEDDED_V6_SCOPE
1515
#ifdef SCTP_KAME
1516
  if ((error = sa6_recoverscope(sin6)) != 0) {
1517
#if !defined(__Userspace__)
1518
    SCTP_FREE_SONAME(sin6);
1519
#endif
1520
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1521
    return (error);
1522
  }
1523
#else
1524
  in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
1525
#endif /* SCTP_KAME */
1526
#endif /* SCTP_EMBEDDED_V6_SCOPE */
1527
#if !defined(__Userspace__)
1528
  *addr = (struct sockaddr *)sin6;
1529
#endif
1530
0
  return (0);
1531
0
}
1532
1533
#if !defined(__Userspace__)
1534
static int
1535
sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1536
{
1537
#elif defined(__Userspace__)
1538
int
1539
sctp6_in6getaddr(struct socket *so, struct mbuf *nam)
1540
0
{
1541
0
#ifdef INET
1542
0
  struct sockaddr *addr = mtod(nam, struct sockaddr *);
1543
0
#endif
1544
#else
1545
static int
1546
sctp6_in6getaddr(struct socket *so, struct mbuf *nam)
1547
{
1548
#ifdef INET
1549
  struct sockaddr *addr = mtod(nam, struct sockaddr *);
1550
#endif
1551
#endif
1552
0
  struct inpcb *inp = sotoinpcb(so);
1553
0
  int error;
1554
1555
0
  if (inp == NULL) {
1556
0
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1557
0
    return (EINVAL);
1558
0
  }
1559
1560
  /* allow v6 addresses precedence */
1561
0
  error = sctp6_getaddr(so, nam);
1562
0
#ifdef INET
1563
0
  if (error) {
1564
#if !defined(__Userspace__)
1565
    struct sockaddr_in6 *sin6;
1566
#else
1567
0
    struct sockaddr_in6 sin6;
1568
0
#endif
1569
1570
    /* try v4 next if v6 failed */
1571
0
    error = sctp_ingetaddr(so, nam);
1572
0
    if (error) {
1573
0
      return (error);
1574
0
    }
1575
#if !defined(__Userspace__)
1576
    SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1577
    if (sin6 == NULL) {
1578
      SCTP_FREE_SONAME(*nam);
1579
      return (ENOMEM);
1580
    }
1581
    in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1582
    SCTP_FREE_SONAME(*nam);
1583
    *nam = (struct sockaddr *)sin6;
1584
#else
1585
0
    in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1586
0
    SCTP_BUF_LEN(nam) = sizeof(struct sockaddr_in6);
1587
0
    memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1588
0
#endif
1589
0
  }
1590
0
#endif
1591
0
  return (error);
1592
0
}
1593
1594
#if !defined(__Userspace__)
1595
static int
1596
sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1597
{
1598
#elif defined(__Userspace__)
1599
int
1600
sctp6_getpeeraddr(struct socket *so, struct mbuf *nam)
1601
0
{
1602
0
#ifdef INET
1603
0
  struct sockaddr *addr = mtod(nam, struct sockaddr *);
1604
0
#endif
1605
#else
1606
static
1607
int
1608
sctp6_getpeeraddr(struct socket *so, struct mbuf *nam)
1609
{
1610
#ifdef INET
1611
  struct sockaddr *addr = mtod(nam, struct sockaddr *);
1612
#endif
1613
1614
#endif
1615
0
  struct inpcb *inp = sotoinpcb(so);
1616
0
  int error;
1617
1618
0
  if (inp == NULL) {
1619
0
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1620
0
    return (EINVAL);
1621
0
  }
1622
1623
  /* allow v6 addresses precedence */
1624
0
  error = sctp6_peeraddr(so, nam);
1625
0
#ifdef INET
1626
0
  if (error) {
1627
#if !defined(__Userspace__)
1628
    struct sockaddr_in6 *sin6;
1629
#else
1630
0
    struct sockaddr_in6 sin6;
1631
0
#endif
1632
1633
    /* try v4 next if v6 failed */
1634
0
    error = sctp_peeraddr(so, nam);
1635
0
    if (error) {
1636
0
      return (error);
1637
0
    }
1638
#if !defined(__Userspace__)
1639
    SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1640
    if (sin6 == NULL) {
1641
      SCTP_FREE_SONAME(*nam);
1642
      return (ENOMEM);
1643
    }
1644
    in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1645
    SCTP_FREE_SONAME(*nam);
1646
    *nam = (struct sockaddr *)sin6;
1647
#else
1648
0
    in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1649
0
    SCTP_BUF_LEN(nam) = sizeof(struct sockaddr_in6);
1650
0
    memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1651
0
#endif
1652
0
  }
1653
0
#endif
1654
0
  return (error);
1655
0
}
1656
1657
#if !defined(__Userspace__)
1658
#if defined(__FreeBSD__)
1659
#define SCTP6_PROTOSW             \
1660
  .pr_protocol =  IPPROTO_SCTP,         \
1661
  .pr_ctloutput = sctp_ctloutput,         \
1662
  .pr_abort = sctp_abort,         \
1663
  .pr_accept =  sctp_accept,          \
1664
  .pr_attach =  sctp6_attach,         \
1665
  .pr_bind =  sctp6_bind,         \
1666
  .pr_connect = sctp6_connect,          \
1667
  .pr_control = in6_control,          \
1668
  .pr_close = sctp6_close,          \
1669
  .pr_detach =  sctp6_close,          \
1670
  .pr_sopoll =  sopoll_generic,         \
1671
  .pr_flush = sctp_flush,         \
1672
  .pr_disconnect = sctp_disconnect,       \
1673
  .pr_listen =  sctp_listen,          \
1674
  .pr_peeraddr =  sctp6_getpeeraddr,        \
1675
  .pr_send =  sctp6_send,         \
1676
  .pr_shutdown =  sctp_shutdown,          \
1677
  .pr_sockaddr =  sctp6_in6getaddr,       \
1678
  .pr_sosend =  sctp_sosend,          \
1679
  .pr_soreceive = sctp_soreceive
1680
1681
struct protosw sctp6_seqpacket_protosw = {
1682
  .pr_type = SOCK_SEQPACKET,
1683
  .pr_flags = PR_WANTRCVD,
1684
  SCTP6_PROTOSW
1685
};
1686
1687
struct protosw sctp6_stream_protosw = {
1688
  .pr_type = SOCK_STREAM,
1689
  .pr_flags = PR_CONNREQUIRED | PR_WANTRCVD,
1690
  SCTP6_PROTOSW
1691
};
1692
#else
1693
struct pr_usrreqs sctp6_usrreqs = {
1694
#if defined(__APPLE__) && !defined(__Userspace__)
1695
  .pru_abort = sctp_abort,
1696
  .pru_accept = sctp_accept,
1697
  .pru_attach = sctp6_attach,
1698
  .pru_bind = sctp6_bind,
1699
  .pru_connect = sctp6_connect,
1700
  .pru_connect2 = pru_connect2_notsupp,
1701
  .pru_control = in6_control,
1702
  .pru_detach = sctp6_detach,
1703
  .pru_disconnect = sctp_disconnect,
1704
  .pru_listen = sctp_listen,
1705
  .pru_peeraddr = sctp6_getpeeraddr,
1706
  .pru_rcvd = NULL,
1707
  .pru_rcvoob = pru_rcvoob_notsupp,
1708
  .pru_send = sctp6_send,
1709
  .pru_sense = pru_sense_null,
1710
  .pru_shutdown = sctp_shutdown,
1711
  .pru_sockaddr = sctp6_in6getaddr,
1712
  .pru_sosend = sctp_sosend,
1713
  .pru_soreceive = sctp_soreceive,
1714
  .pru_sopoll = sopoll
1715
#elif defined(_WIN32) && !defined(__Userspace__)
1716
  sctp_abort,
1717
  sctp_accept,
1718
  sctp6_attach,
1719
  sctp6_bind,
1720
  sctp6_connect,
1721
  pru_connect2_notsupp,
1722
  NULL,
1723
  NULL,
1724
  sctp_disconnect,
1725
  sctp_listen,
1726
  sctp6_getpeeraddr,
1727
  NULL,
1728
  pru_rcvoob_notsupp,
1729
  NULL,
1730
  pru_sense_null,
1731
  sctp_shutdown,
1732
  sctp_flush,
1733
  sctp6_in6getaddr,
1734
  sctp_sosend,
1735
  sctp_soreceive,
1736
  sopoll_generic,
1737
  NULL,
1738
  sctp6_close
1739
};
1740
#endif
1741
#endif
1742
#endif
1743
#endif