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 #if defined(__FreeBSD__) && !defined(__Userspace__)
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: head/sys/netinet/sctp_bsd_addr.c 358080 2020-02-18 19:41:55Z tuexen $");
40 #include <netinet/sctp_os.h>
41 #include <netinet/sctp_var.h>
42 #include <netinet/sctp_pcb.h>
43 #include <netinet/sctp_header.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_bsd_addr.h>
47 #include <netinet/sctp_uio.h>
48 #include <netinet/sctputil.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_asconf.h>
51 #include <netinet/sctp_sysctl.h>
52 #include <netinet/sctp_indata.h>
53 #if defined(__FreeBSD__) && !defined(__Userspace__)
54 #include <sys/unistd.h>
57 /* Declare all of our malloc named types */
58 MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
59 MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
60 MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
61 MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
62 MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
63 MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
64 MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
65 MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
66 MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
67 MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
68 MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
69 MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
70 MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
71 MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
72 MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
73 MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
74 MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
75 MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
76 MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
77 MALLOC_DEFINE(SCTP_M_MCORE, "sctp_mcore", "sctp mcore queue");
79 /* Global NON-VNET structure that controls the iterator */
80 struct iterator_control sctp_it_ctl;
82 #if !(defined(__FreeBSD__) && !defined(__Userspace__))
84 sctp_cleanup_itqueue(void)
86 struct sctp_iterator *it, *nit;
88 TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
89 if (it->function_atend != NULL) {
90 (*it->function_atend) (it->pointer, it->val);
92 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
93 SCTP_FREE(it, SCTP_M_ITER);
97 #if defined(__Userspace__)
98 /*__Userspace__ TODO if we use thread based iterator
99 * then the implementation of wakeup will need to change.
100 * Currently we are using timeo_cond for ident so_timeo
101 * but that is not sufficient if we need to use another ident
102 * like wakeup(&sctppcbinfo.iterator_running);
107 sctp_wakeup_iterator(void)
109 #if defined(SCTP_PROCESS_LEVEL_LOCKS)
111 WakeAllConditionVariable(&sctp_it_ctl.iterator_wakeup);
113 pthread_cond_broadcast(&sctp_it_ctl.iterator_wakeup);
116 wakeup(&sctp_it_ctl.iterator_running);
120 #if defined(__Userspace__)
125 sctp_iterator_thread(void *v SCTP_UNUSED)
127 #if defined(__Userspace__)
128 sctp_userspace_set_threadname("SCTP iterator");
130 SCTP_IPI_ITERATOR_WQ_LOCK();
131 /* In FreeBSD this thread never terminates. */
132 #if defined(__FreeBSD__) && !defined(__Userspace__)
135 while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) == 0) {
137 #if !defined(__Userspace__)
138 msleep(&sctp_it_ctl.iterator_running,
139 #if defined(__FreeBSD__)
140 &sctp_it_ctl.ipi_iterator_wq_mtx,
141 #elif defined(__APPLE__)
142 sctp_it_ctl.ipi_iterator_wq_mtx,
144 0, "waiting_for_work", 0);
147 SleepConditionVariableCS(&sctp_it_ctl.iterator_wakeup, &sctp_it_ctl.ipi_iterator_wq_mtx, INFINITE);
149 pthread_cond_wait(&sctp_it_ctl.iterator_wakeup, &sctp_it_ctl.ipi_iterator_wq_mtx);
152 #if !(defined(__FreeBSD__) && !defined(__Userspace__))
153 if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) {
157 sctp_iterator_worker();
159 #if !(defined(__FreeBSD__) && !defined(__Userspace__))
160 /* Now this thread needs to be terminated */
161 sctp_cleanup_itqueue();
162 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_EXITED;
163 SCTP_IPI_ITERATOR_WQ_UNLOCK();
164 #if defined(__Userspace__)
165 sctp_wakeup_iterator();
168 wakeup(&sctp_it_ctl.iterator_flags);
169 thread_terminate(current_thread());
171 panic("Hmm. thread_terminate() continues...");
178 sctp_startup_iterator(void)
180 if (sctp_it_ctl.thread_proc) {
181 /* You only get one */
184 /* Initialize global locks here, thus only once. */
185 SCTP_ITERATOR_LOCK_INIT();
186 SCTP_IPI_ITERATOR_WQ_INIT();
187 TAILQ_INIT(&sctp_it_ctl.iteratorhead);
188 #if defined(__Userspace__)
189 if (sctp_userspace_thread_create(&sctp_it_ctl.thread_proc, &sctp_iterator_thread)) {
190 SCTP_PRINTF("ERROR: Creating sctp_iterator_thread failed.\n");
192 SCTP_BASE_VAR(iterator_thread_started) = 1;
194 #elif defined(__FreeBSD__)
195 kproc_create(sctp_iterator_thread,
197 &sctp_it_ctl.thread_proc,
201 #elif defined(__APPLE__)
202 kernel_thread_start((thread_continue_t)sctp_iterator_thread, NULL, &sctp_it_ctl.thread_proc);
208 #if defined(__Userspace__)
209 /* __Userspace__ TODO. struct in6_ifaddr is defined in sys/netinet6/in6_var.h
210 ip6_use_deprecated is defined as int ip6_use_deprecated = 1; in /src/sys/netinet6/in6_proto.c
213 sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
219 sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
221 struct in6_ifaddr *ifa6;
223 ifa6 = (struct in6_ifaddr *)ifa->ifa;
224 ifa->flags = ifa6->ia6_flags;
225 if (!MODULE_GLOBAL(ip6_use_deprecated)) {
227 IN6_IFF_DEPRECATED) {
228 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
230 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
233 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
239 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
241 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
244 #endif /* __Userspace__ */
248 #if !defined(__Userspace__)
250 sctp_is_desired_interface_type(struct ifnet *ifn)
254 /* check the interface type to see if it's one we care about */
255 #if defined(__APPLE__) && !defined(__Userspace__)
256 switch(ifnet_type(ifn)) {
258 switch (ifn->if_type) {
272 case IFT_ISDNPRIMARY:
281 #if !(defined(__APPLE__) && !defined(__Userspace__))
285 case IFT_PROPVIRTUAL: /* NetGraph Virtual too */
286 case IFT_VIRTUALIPADDRESS:
298 #if defined(__APPLE__) && !defined(__Userspace__)
300 sctp_is_vmware_interface(struct ifnet *ifn)
302 return (strncmp(ifnet_name(ifn), "vmnet", 5) == 0);
306 #if defined(_WIN32) && defined(__Userspace__)
309 #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
313 #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
316 sctp_init_ifns_for_vrf(int vrfid)
318 #if defined(INET) || defined(INET6)
319 struct sctp_ifa *sctp_ifa;
320 DWORD Err, AdapterAddrsSize;
321 PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt;
322 PIP_ADAPTER_UNICAST_ADDRESS pUnicast;
326 AdapterAddrsSize = 0;
328 if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
329 if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
330 SCTP_PRINTF("GetAdaptersV4Addresses() sizing failed with error code %d\n", Err);
331 SCTP_PRINTF("err = %d; AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
336 /* Allocate memory from sizing information */
337 if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
338 SCTP_PRINTF("Memory allocation error!\n");
341 /* Get actual adapter information */
342 if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
343 SCTP_PRINTF("GetAdaptersV4Addresses() failed with error code %d\n", Err);
347 /* Enumerate through each returned adapter and save its information */
348 for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
349 if (pAdapt->IfType == IF_TYPE_IEEE80211 || pAdapt->IfType == IF_TYPE_ETHERNET_CSMACD) {
350 for (pUnicast = pAdapt->FirstUnicastAddress; pUnicast; pUnicast = pUnicast->Next) {
351 if (IN4_ISLINKLOCAL_ADDRESS(&(((struct sockaddr_in *)(pUnicast->Address.lpSockaddr))->sin_addr))) {
354 sctp_ifa = sctp_add_addr_to_vrf(0,
357 (pAdapt->IfType == IF_TYPE_IEEE80211)?MIB_IF_TYPE_ETHERNET:pAdapt->IfType,
360 pUnicast->Address.lpSockaddr,
364 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
372 AdapterAddrsSize = 0;
374 if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
375 if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
376 SCTP_PRINTF("GetAdaptersV6Addresses() sizing failed with error code %d\n", Err);
377 SCTP_PRINTF("err = %d; AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
381 /* Allocate memory from sizing information */
382 if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
383 SCTP_PRINTF("Memory allocation error!\n");
386 /* Get actual adapter information */
387 if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
388 SCTP_PRINTF("GetAdaptersV6Addresses() failed with error code %d\n", Err);
392 /* Enumerate through each returned adapter and save its information */
393 for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
394 if (pAdapt->IfType == IF_TYPE_IEEE80211 || pAdapt->IfType == IF_TYPE_ETHERNET_CSMACD) {
395 for (pUnicast = pAdapt->FirstUnicastAddress; pUnicast; pUnicast = pUnicast->Next) {
396 sctp_ifa = sctp_add_addr_to_vrf(0,
399 (pAdapt->IfType == IF_TYPE_IEEE80211)?MIB_IF_TYPE_ETHERNET:pAdapt->IfType,
402 pUnicast->Address.lpSockaddr,
406 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
414 #elif defined(__Userspace__)
416 sctp_init_ifns_for_vrf(int vrfid)
418 #if defined(INET) || defined(INET6)
420 struct ifaddrs *ifa, *ifas;
421 struct sctp_ifa *sctp_ifa;
424 rc = getifaddrs(&ifas);
428 for (ifa = ifas; ifa; ifa = ifa->ifa_next) {
429 if (ifa->ifa_addr == NULL) {
433 if (ifa->ifa_addr->sa_family != AF_INET6) {
437 #elif !defined(INET6)
438 if (ifa->ifa_addr->sa_family != AF_INET) {
443 if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
444 /* non inet/inet6 skip */
449 if ((ifa->ifa_addr->sa_family == AF_INET6) &&
450 IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
451 /* skip unspecifed addresses */
456 if (ifa->ifa_addr->sa_family == AF_INET &&
457 ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
462 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
464 if_nametoindex(ifa->ifa_name),
472 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
480 #if defined(__APPLE__) && !defined(__Userspace__)
482 sctp_init_ifns_for_vrf(int vrfid)
484 /* Here we must apply ANY locks needed by the
485 * IFN we access and also make sure we lock
486 * any IFA that exists as we float through the
489 struct ifnet **ifnetlist;
490 uint32_t i, j, count;
491 char name[SCTP_IFNAMSIZ];
493 struct ifaddr **ifaddrlist;
495 struct in6_ifaddr *ifa6;
496 struct sctp_ifa *sctp_ifa;
499 if (ifnet_list_get(IFNET_FAMILY_ANY, &ifnetlist, &count) != 0) {
502 for (i = 0; i < count; i++) {
504 if (SCTP_BASE_SYSCTL(sctp_ignore_vmware_interfaces) && sctp_is_vmware_interface(ifn)) {
507 if (sctp_is_desired_interface_type(ifn) == 0) {
508 /* non desired type */
511 if (ifnet_get_address_list(ifn, &ifaddrlist) != 0) {
514 for (j = 0; ifaddrlist[j] != NULL; j++) {
516 if (ifa->ifa_addr == NULL) {
519 if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
520 /* non inet/inet6 skip */
523 if (ifa->ifa_addr->sa_family == AF_INET6) {
524 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
525 /* skip unspecifed addresses */
529 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == INADDR_ANY) {
533 if (ifa->ifa_addr->sa_family == AF_INET6) {
534 ifa6 = (struct in6_ifaddr *)ifa;
535 ifa_flags = ifa6->ia6_flags;
539 SCTP_SNPRINTF(name, SCTP_IFNAMSIZ, "%s%d", ifnet_name(ifn), ifnet_unit(ifn));
540 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
541 (void *)ifn, /* XXX */
545 (void *)ifa, /* XXX */
550 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
553 ifnet_free_address_list(ifaddrlist);
555 ifnet_list_free(ifnetlist);
559 #if defined(__FreeBSD__) && !defined(__Userspace__)
561 sctp_init_ifns_for_vrf(int vrfid)
563 /* Here we must apply ANY locks needed by the
564 * IFN we access and also make sure we lock
565 * any IFA that exists as we float through the
568 struct epoch_tracker et;
571 struct sctp_ifa *sctp_ifa;
574 struct in6_ifaddr *ifa6;
579 CK_STAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_link) {
580 if (sctp_is_desired_interface_type(ifn) == 0) {
581 /* non desired type */
584 CK_STAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) {
585 if (ifa->ifa_addr == NULL) {
588 switch (ifa->ifa_addr->sa_family) {
591 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
598 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
599 /* skip unspecifed addresses */
607 switch (ifa->ifa_addr->sa_family) {
615 ifa6 = (struct in6_ifaddr *)ifa;
616 ifa_flags = ifa6->ia6_flags;
623 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
633 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
643 sctp_init_vrf_list(int vrfid)
645 if (vrfid > SCTP_MAX_VRF_ID)
649 /* Don't care about return here */
650 (void)sctp_allocate_vrf(vrfid);
652 /* Now we need to build all the ifn's
653 * for this vrf and there addresses
655 sctp_init_ifns_for_vrf(vrfid);
659 sctp_addr_change(struct ifaddr *ifa, int cmd)
661 #if defined(__Userspace__)
664 uint32_t ifa_flags = 0;
666 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
669 /* BSD only has one VRF, if this changes
670 * we will need to hook in the right
671 * things here to get the id to pass to
672 * the address management routine.
674 if (SCTP_BASE_VAR(first_time) == 0) {
675 /* Special test to see if my ::1 will showup with this */
676 SCTP_BASE_VAR(first_time) = 1;
677 sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
680 if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
681 /* don't know what to do with this */
685 if (ifa->ifa_addr == NULL) {
688 if (sctp_is_desired_interface_type(ifa->ifa_ifp) == 0) {
689 /* non desired type */
692 switch (ifa->ifa_addr->sa_family) {
695 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
702 ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
703 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
704 /* skip unspecifed addresses */
710 /* non inet/inet6 skip */
713 if (cmd == RTM_ADD) {
714 (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
715 #if defined(__APPLE__) && !defined(__Userspace__)
716 ifnet_index(ifa->ifa_ifp), ifnet_type(ifa->ifa_ifp), ifnet_name(ifa->ifa_ifp),
718 ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type, ifa->ifa_ifp->if_xname,
720 (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
723 sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
724 #if defined(__APPLE__) && !defined(__Userspace__)
725 ifnet_index(ifa->ifa_ifp),
726 ifnet_name(ifa->ifa_ifp));
728 ifa->ifa_ifp->if_index,
729 ifa->ifa_ifp->if_xname);
732 /* We don't bump refcount here so when it completes
733 * the final delete will happen.
739 #if defined(__FreeBSD__) && !defined(__Userspace__)
741 sctp_addr_change_event_handler(void *arg __unused, struct ifaddr *ifa, int cmd) {
742 sctp_addr_change(ifa, cmd);
745 #if defined(__APPLE__) && !defined(__Userspace__)
747 sctp_add_or_del_interfaces(int (*pred)(struct ifnet *), int add)
749 struct ifnet **ifnetlist;
750 struct ifaddr **ifaddrlist;
751 uint32_t i, j, count;
753 if (ifnet_list_get(IFNET_FAMILY_ANY, &ifnetlist, &count) != 0) {
756 for (i = 0; i < count; i++) {
757 if (!(*pred)(ifnetlist[i])) {
760 if (ifnet_get_address_list(ifnetlist[i], &ifaddrlist) != 0) {
763 for (j = 0; ifaddrlist[j] != NULL; j++) {
764 sctp_addr_change(ifaddrlist[j], add ? RTM_ADD : RTM_DELETE);
766 ifnet_free_address_list(ifaddrlist);
768 ifnet_list_free(ifnetlist);
774 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
775 int how, int allonebuf, int type)
777 struct mbuf *m = NULL;
778 #if defined(__FreeBSD__) || defined(__Userspace__)
779 #if defined(__Userspace__)
780 m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0, allonebuf);
782 m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
788 #if !defined(__Userspace__)
790 if (SCTP_BUF_SIZE(m) < space_needed) {
794 KASSERT(SCTP_BUF_NEXT(m) == NULL, ("%s: no chain allowed", __FUNCTION__));
797 #ifdef SCTP_MBUF_LOGGING
798 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
799 sctp_log_mb(m, SCTP_MBUF_IALLOC);
807 MGETHDR(m, how, type);
816 if (allonebuf == 0) {
817 mbuf_threshold = SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count);
822 if (space_needed > (unsigned int)(((mbuf_threshold - 1) * MLEN) + MHLEN)) {
827 if (SCTP_BUF_IS_EXTENDED(m) == 0) {
831 size = SCTP_BUF_EXTEND_SIZE(m);
833 if (allonebuf != 0 && size < space_needed) {
838 SCTP_BUF_NEXT(m) = SCTP_BUF_NEXT_PKT(m) = NULL;
839 #ifdef SCTP_MBUF_LOGGING
840 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
841 sctp_log_mb(m, SCTP_MBUF_IALLOC);
849 #ifdef SCTP_PACKET_LOGGING
851 sctp_packet_log(struct mbuf *m)
858 int grabbed_lock = 0;
859 int value, newval, thisend, thisbegin;
862 * -sizeof this entry (total_len)
863 * -previous end (value)
864 * -ticks of log (ticks)
867 * - where this started (thisbegin)
868 * x <--end points here
870 length = SCTP_HEADER_LEN(m);
871 total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
872 /* Log a packet to the buffer. */
873 if (total_len> SCTP_PACKET_LOG_SIZE) {
874 /* Can't log this packet I have not a buffer big enough */
877 if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
880 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
882 if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
883 SCTP_IP_PKTLOG_LOCK();
886 value = SCTP_BASE_VAR(packet_log_end);
887 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
888 if (newval >= SCTP_PACKET_LOG_SIZE) {
893 thisbegin = SCTP_BASE_VAR(packet_log_end);
896 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
900 value = SCTP_BASE_VAR(packet_log_end);
901 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
902 if (newval >= SCTP_PACKET_LOG_SIZE) {
907 thisbegin = SCTP_BASE_VAR(packet_log_end);
910 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
915 if (thisend >= SCTP_PACKET_LOG_SIZE) {
916 SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
919 SCTP_BASE_VAR(packet_log_writers),
921 SCTP_BASE_VAR(packet_log_end));
922 SCTP_BASE_VAR(packet_log_end) = 0;
926 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
931 tick_tock = (uint32_t *)lenat;
933 *tick_tock = sctp_get_tick_count();
934 copyto = (void *)lenat;
935 thisone = thisend - sizeof(int);
936 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
939 SCTP_IP_PKTLOG_UNLOCK();
942 m_copydata(m, 0, length, (caddr_t)copyto);
945 SCTP_IP_PKTLOG_UNLOCK();
947 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
952 sctp_copy_out_packet_log(uint8_t *target, int length)
954 /* We wind through the packet log starting at
955 * start copying up to length bytes out.
956 * We return the number of bytes copied.
958 int tocopy, this_copy;
963 if (length < (int)(2 * sizeof(int))) {
964 /* not enough room */
967 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
968 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
970 if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
971 /* we delay here for just a moment hoping the writer(s) that were
972 * present when we entered will have left and we only have
973 * locking ones that will contend with us for the lock. This
974 * does not assure 100% access, but its good enough for
975 * a logging facility like this.
982 SCTP_IP_PKTLOG_LOCK();
983 lenat = (int *)target;
984 *lenat = SCTP_BASE_VAR(packet_log_end);
986 this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
987 memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
988 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
989 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
990 SCTP_PKTLOG_WRITERS_NEED_LOCK);
992 SCTP_IP_PKTLOG_UNLOCK();
993 return (this_copy + sizeof(int));