Coverage Report

Created: 2022-08-24 06:02

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