2 * SPDX-License-Identifier: BSD-3-Clause
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
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.
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.
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.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp_var.h>
40 #include <netinet/sctp_pcb.h>
41 #include <netinet/sctp_header.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_output.h>
44 #include <netinet/sctp_bsd_addr.h>
45 #include <netinet/sctp_uio.h>
46 #include <netinet/sctputil.h>
47 #include <netinet/sctp_timer.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctp_sysctl.h>
50 #include <netinet/sctp_indata.h>
51 #include <sys/unistd.h>
53 /* Declare all of our malloc named types */
54 MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
55 MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
56 MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
57 MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
58 MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
59 MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
60 MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
61 MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
62 MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
63 MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
64 MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
65 MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
66 MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
67 MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
68 MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
69 MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
70 MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
71 MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
72 MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
73 MALLOC_DEFINE(SCTP_M_MCORE, "sctp_mcore", "sctp mcore queue");
75 /* Global NON-VNET structure that controls the iterator */
76 struct iterator_control sctp_it_ctl;
79 sctp_wakeup_iterator(void)
81 wakeup(&sctp_it_ctl.iterator_running);
85 sctp_iterator_thread(void *v SCTP_UNUSED)
87 SCTP_IPI_ITERATOR_WQ_LOCK();
88 /* In FreeBSD this thread never terminates. */
90 msleep(&sctp_it_ctl.iterator_running,
91 &sctp_it_ctl.ipi_iterator_wq_mtx,
92 0, "waiting_for_work", 0);
93 sctp_iterator_worker();
98 sctp_startup_iterator(void)
100 if (sctp_it_ctl.thread_proc) {
101 /* You only get one */
104 /* Initialize global locks here, thus only once. */
105 SCTP_ITERATOR_LOCK_INIT();
106 SCTP_IPI_ITERATOR_WQ_INIT();
107 TAILQ_INIT(&sctp_it_ctl.iteratorhead);
108 kproc_create(sctp_iterator_thread,
110 &sctp_it_ctl.thread_proc,
119 sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
121 struct in6_ifaddr *ifa6;
123 ifa6 = (struct in6_ifaddr *)ifa->ifa;
124 ifa->flags = ifa6->ia6_flags;
125 if (!MODULE_GLOBAL(ip6_use_deprecated)) {
127 IN6_IFF_DEPRECATED) {
128 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
130 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
133 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
139 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
141 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
147 sctp_is_desired_interface_type(struct ifnet *ifn)
151 /* check the interface type to see if it's one we care about */
152 switch (ifn->if_type) {
165 case IFT_ISDNPRIMARY:
177 case IFT_PROPVIRTUAL: /* NetGraph Virtual too */
178 case IFT_VIRTUALIPADDRESS:
189 sctp_init_ifns_for_vrf(int vrfid)
192 * Here we must apply ANY locks needed by the IFN we access and also
193 * make sure we lock any IFA that exists as we float through the
196 struct epoch_tracker et;
199 struct sctp_ifa *sctp_ifa;
202 struct in6_ifaddr *ifa6;
207 CK_STAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_link) {
208 if (sctp_is_desired_interface_type(ifn) == 0) {
209 /* non desired type */
212 CK_STAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) {
213 if (ifa->ifa_addr == NULL) {
216 switch (ifa->ifa_addr->sa_family) {
219 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
226 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
227 /* skip unspecifed addresses */
235 switch (ifa->ifa_addr->sa_family) {
243 ifa6 = (struct in6_ifaddr *)ifa;
244 ifa_flags = ifa6->ia6_flags;
251 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
261 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
270 sctp_init_vrf_list(int vrfid)
272 if (vrfid > SCTP_MAX_VRF_ID)
276 /* Don't care about return here */
277 (void)sctp_allocate_vrf(vrfid);
280 * Now we need to build all the ifn's for this vrf and there
283 sctp_init_ifns_for_vrf(vrfid);
287 sctp_addr_change(struct ifaddr *ifa, int cmd)
289 uint32_t ifa_flags = 0;
291 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
295 * BSD only has one VRF, if this changes we will need to hook in the
296 * right things here to get the id to pass to the address management
299 if (SCTP_BASE_VAR(first_time) == 0) {
300 /* Special test to see if my ::1 will showup with this */
301 SCTP_BASE_VAR(first_time) = 1;
302 sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
305 if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
306 /* don't know what to do with this */
310 if (ifa->ifa_addr == NULL) {
313 if (sctp_is_desired_interface_type(ifa->ifa_ifp) == 0) {
314 /* non desired type */
317 switch (ifa->ifa_addr->sa_family) {
320 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
327 ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
328 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
329 /* skip unspecifed addresses */
335 /* non inet/inet6 skip */
338 if (cmd == RTM_ADD) {
339 (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
340 ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type, ifa->ifa_ifp->if_xname,
341 (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
343 sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
344 ifa->ifa_ifp->if_index,
345 ifa->ifa_ifp->if_xname);
348 * We don't bump refcount here so when it completes the
349 * final delete will happen.
355 sctp_addr_change_event_handler(void *arg __unused, struct ifaddr *ifa, int cmd)
357 sctp_addr_change(ifa, cmd);
361 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
362 int how, int allonebuf, int type)
364 struct mbuf *m = NULL;
366 m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
372 if (SCTP_BUF_SIZE(m) < space_needed) {
376 KASSERT(SCTP_BUF_NEXT(m) == NULL, ("%s: no chain allowed", __func__));
378 #ifdef SCTP_MBUF_LOGGING
379 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
380 sctp_log_mb(m, SCTP_MBUF_IALLOC);
386 #ifdef SCTP_PACKET_LOGGING
388 sctp_packet_log(struct mbuf *m)
395 int grabbed_lock = 0;
396 int value, newval, thisend, thisbegin;
399 * Buffer layout. -sizeof this entry (total_len) -previous end
400 * (value) -ticks of log (ticks) o -ip packet o -as logged -
401 * where this started (thisbegin) x <--end points here
403 length = SCTP_HEADER_LEN(m);
404 total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
405 /* Log a packet to the buffer. */
406 if (total_len > SCTP_PACKET_LOG_SIZE) {
407 /* Can't log this packet I have not a buffer big enough */
410 if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
413 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
415 if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
416 SCTP_IP_PKTLOG_LOCK();
419 value = SCTP_BASE_VAR(packet_log_end);
420 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
421 if (newval >= SCTP_PACKET_LOG_SIZE) {
426 thisbegin = SCTP_BASE_VAR(packet_log_end);
429 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
433 value = SCTP_BASE_VAR(packet_log_end);
434 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
435 if (newval >= SCTP_PACKET_LOG_SIZE) {
440 thisbegin = SCTP_BASE_VAR(packet_log_end);
443 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
448 if (thisend >= SCTP_PACKET_LOG_SIZE) {
449 SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
452 SCTP_BASE_VAR(packet_log_writers),
454 SCTP_BASE_VAR(packet_log_end));
455 SCTP_BASE_VAR(packet_log_end) = 0;
458 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
463 tick_tock = (uint32_t *)lenat;
465 *tick_tock = sctp_get_tick_count();
466 copyto = (void *)lenat;
467 thisone = thisend - sizeof(int);
468 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
471 SCTP_IP_PKTLOG_UNLOCK();
474 m_copydata(m, 0, length, (caddr_t)copyto);
477 SCTP_IP_PKTLOG_UNLOCK();
479 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
483 sctp_copy_out_packet_log(uint8_t *target, int length)
486 * We wind through the packet log starting at start copying up to
487 * length bytes out. We return the number of bytes copied.
489 int tocopy, this_copy;
494 if (length < (int)(2 * sizeof(int))) {
495 /* not enough room */
498 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
499 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
501 if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
503 * we delay here for just a moment hoping the
504 * writer(s) that were present when we entered will
505 * have left and we only have locking ones that will
506 * contend with us for the lock. This does not
507 * assure 100% access, but its good enough for a
508 * logging facility like this.
515 SCTP_IP_PKTLOG_LOCK();
516 lenat = (int *)target;
517 *lenat = SCTP_BASE_VAR(packet_log_end);
519 this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
520 memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
521 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
522 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
523 SCTP_PKTLOG_WRITERS_NEED_LOCK);
525 SCTP_IP_PKTLOG_UNLOCK();
526 return (this_copy + sizeof(int));