2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: head/sys/netinet/sctp_bsd_addr.c 258765 2013-11-30 12:51:19Z tuexen $");
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 #if defined(__FreeBSD__)
52 #include <sys/unistd.h>
55 /* Declare all of our malloc named types */
57 MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
58 MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
59 MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
60 MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
61 MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
62 MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
63 MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
64 MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
65 MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
66 MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
67 MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
68 MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
69 MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
70 MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
71 MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
72 MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
73 MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
74 MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
75 MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
76 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__)
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)
110 #if defined(__Userspace_os_Windows)
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 SCTP_IPI_ITERATOR_WQ_LOCK();
128 /* In FreeBSD this thread never terminates. */
129 #if defined(__FreeBSD__)
132 while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) == 0) {
134 #if !defined(__Userspace__)
135 msleep(&sctp_it_ctl.iterator_running,
136 #if defined(__FreeBSD__)
137 &sctp_it_ctl.ipi_iterator_wq_mtx,
138 #elif defined(__APPLE__) || defined(__Userspace_os_Darwin)
139 sctp_it_ctl.ipi_iterator_wq_mtx,
141 0, "waiting_for_work", 0);
143 #if defined(__Userspace_os_Windows)
144 SleepConditionVariableCS(&sctp_it_ctl.iterator_wakeup, &sctp_it_ctl.ipi_iterator_wq_mtx, INFINITE);
146 pthread_cond_wait(&sctp_it_ctl.iterator_wakeup, &sctp_it_ctl.ipi_iterator_wq_mtx);
149 #if !defined(__FreeBSD__)
150 if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) {
154 sctp_iterator_worker();
156 #if !defined(__FreeBSD__)
157 /* Now this thread needs to be terminated */
158 sctp_cleanup_itqueue();
159 sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_EXITED;
160 SCTP_IPI_ITERATOR_WQ_UNLOCK();
161 #if defined(__Userspace__)
162 sctp_wakeup_iterator();
165 wakeup(&sctp_it_ctl.iterator_flags);
166 thread_terminate(current_thread());
168 panic("Hmm. thread_terminate() continues...");
175 sctp_startup_iterator(void)
177 if (sctp_it_ctl.thread_proc) {
178 /* You only get one */
181 /* Initialize global locks here, thus only once. */
182 SCTP_ITERATOR_LOCK_INIT();
183 SCTP_IPI_ITERATOR_WQ_INIT();
184 TAILQ_INIT(&sctp_it_ctl.iteratorhead);
185 #if defined(__FreeBSD__)
186 #if __FreeBSD_version <= 701000
187 kthread_create(sctp_iterator_thread,
189 kproc_create(sctp_iterator_thread,
192 &sctp_it_ctl.thread_proc,
196 #elif defined(__APPLE__)
197 kernel_thread_start((thread_continue_t)sctp_iterator_thread, NULL, &sctp_it_ctl.thread_proc);
198 #elif defined(__Userspace__)
199 #if defined(__Userspace_os_Windows)
200 if ((sctp_it_ctl.thread_proc = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&sctp_iterator_thread, NULL, 0, NULL)) == NULL) {
202 if (pthread_create(&sctp_it_ctl.thread_proc, NULL, &sctp_iterator_thread, NULL)) {
204 SCTP_PRINTF("ERROR: Creating sctp_iterator_thread failed.\n");
211 #if defined(__Userspace__)
212 /* __Userspace__ TODO. struct in6_ifaddr is defined in sys/netinet6/in6_var.h
213 ip6_use_deprecated is defined as int ip6_use_deprecated = 1; in /src/sys/netinet6/in6_proto.c
216 sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
222 sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
224 struct in6_ifaddr *ifa6;
226 ifa6 = (struct in6_ifaddr *)ifa->ifa;
227 ifa->flags = ifa6->ia6_flags;
228 if (!MODULE_GLOBAL(ip6_use_deprecated)) {
230 IN6_IFF_DEPRECATED) {
231 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
233 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
236 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
242 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
244 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
247 #endif /* __Userspace__ */
251 #if !defined(__Userspace__)
253 sctp_is_desired_interface_type(struct ifnet *ifn)
257 /* check the interface type to see if it's one we care about */
258 #if defined(__APPLE__)
259 switch(ifnet_type(ifn)) {
261 switch (ifn->if_type) {
275 case IFT_ISDNPRIMARY:
284 #if !defined(__APPLE__)
288 case IFT_PROPVIRTUAL: /* NetGraph Virtual too */
289 case IFT_VIRTUALIPADDRESS:
301 #if defined(__APPLE__)
303 sctp_is_vmware_interface(struct ifnet *ifn)
305 return (strncmp(ifnet_name(ifn), "vmnet", 5) == 0);
309 #if defined(__Userspace_os_Windows)
312 #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
316 #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
319 sctp_init_ifns_for_vrf(int vrfid)
321 #if defined(INET) || defined(INET6)
323 struct sctp_ifa *sctp_ifa;
324 DWORD Err, AdapterAddrsSize;
325 PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt;
326 PIP_ADAPTER_UNICAST_ADDRESS pUnicast;
330 AdapterAddrsSize = 0;
332 if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
333 if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
334 SCTP_PRINTF("GetAdaptersV4Addresses() sizing failed with error code %d\n", Err);
335 SCTP_PRINTF("err = %d; AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
340 /* Allocate memory from sizing information */
341 if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
342 SCTP_PRINTF("Memory allocation error!\n");
345 /* Get actual adapter information */
346 if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
347 SCTP_PRINTF("GetAdaptersV4Addresses() failed with error code %d\n", Err);
350 /* Enumerate through each returned adapter and save its information */
351 for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
352 if (pAdapt->IfType == IF_TYPE_IEEE80211 || pAdapt->IfType == IF_TYPE_ETHERNET_CSMACD) {
353 for (pUnicast = pAdapt->FirstUnicastAddress; pUnicast; pUnicast = pUnicast->Next) {
354 if (IN4_ISLINKLOCAL_ADDRESS(&(((struct sockaddr_in *)(pUnicast->Address.lpSockaddr))->sin_addr))) {
357 ifa = (struct ifaddrs*)malloc(sizeof(struct ifaddrs));
358 ifa->ifa_name = _strdup(pAdapt->AdapterName);
359 ifa->ifa_flags = pAdapt->Flags;
360 ifa->ifa_addr = (struct sockaddr *)malloc(sizeof(struct sockaddr_in));
361 memcpy(ifa->ifa_addr, pUnicast->Address.lpSockaddr, sizeof(struct sockaddr_in));
363 sctp_ifa = sctp_add_addr_to_vrf(0,
366 (pAdapt->IfType == IF_TYPE_IEEE80211)?MIB_IF_TYPE_ETHERNET:pAdapt->IfType,
373 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
382 AdapterAddrsSize = 0;
384 if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
385 if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
386 SCTP_PRINTF("GetAdaptersV6Addresses() sizing failed with error code %d\n", Err);
387 SCTP_PRINTF("err = %d; AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
391 /* Allocate memory from sizing information */
392 if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
393 SCTP_PRINTF("Memory allocation error!\n");
396 /* Get actual adapter information */
397 if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
398 SCTP_PRINTF("GetAdaptersV6Addresses() failed with error code %d\n", Err);
401 /* Enumerate through each returned adapter and save its information */
402 for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
403 if (pAdapt->IfType == IF_TYPE_IEEE80211 || pAdapt->IfType == IF_TYPE_ETHERNET_CSMACD) {
404 for (pUnicast = pAdapt->FirstUnicastAddress; pUnicast; pUnicast = pUnicast->Next) {
405 ifa = (struct ifaddrs*)malloc(sizeof(struct ifaddrs));
406 ifa->ifa_name = _strdup(pAdapt->AdapterName);
407 ifa->ifa_flags = pAdapt->Flags;
408 ifa->ifa_addr = (struct sockaddr *)malloc(sizeof(struct sockaddr_in6));
409 memcpy(ifa->ifa_addr, pUnicast->Address.lpSockaddr, sizeof(struct sockaddr_in6));
410 sctp_ifa = sctp_add_addr_to_vrf(0,
413 (pAdapt->IfType == IF_TYPE_IEEE80211)?MIB_IF_TYPE_ETHERNET:pAdapt->IfType,
420 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
429 #elif defined(__Userspace__)
431 sctp_init_ifns_for_vrf(int vrfid)
433 #if defined(INET) || defined(INET6)
435 struct ifaddrs *ifa = NULL;
436 struct sctp_ifa *sctp_ifa;
439 rc = getifaddrs(&g_interfaces);
443 for (ifa = g_interfaces; ifa; ifa = ifa->ifa_next) {
444 if (ifa->ifa_addr == NULL) {
448 if (ifa->ifa_addr->sa_family != AF_INET6) {
452 #elif !defined(INET6)
453 if (ifa->ifa_addr->sa_family != AF_INET) {
458 if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
459 /* non inet/inet6 skip */
464 if ((ifa->ifa_addr->sa_family == AF_INET6) &&
465 IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
466 /* skip unspecifed addresses */
471 if (ifa->ifa_addr->sa_family == AF_INET &&
472 ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
477 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
479 if_nametoindex(ifa->ifa_name),
487 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
494 #if defined(__APPLE__)
496 sctp_init_ifns_for_vrf(int vrfid)
498 /* Here we must apply ANY locks needed by the
499 * IFN we access and also make sure we lock
500 * any IFA that exists as we float through the
503 struct ifnet **ifnetlist;
504 uint32_t i, j, count;
505 char name[SCTP_IFNAMSIZ];
507 struct ifaddr **ifaddrlist;
509 struct in6_ifaddr *ifa6;
510 struct sctp_ifa *sctp_ifa;
513 if (ifnet_list_get(IFNET_FAMILY_ANY, &ifnetlist, &count) != 0) {
516 for (i = 0; i < count; i++) {
518 if (SCTP_BASE_SYSCTL(sctp_ignore_vmware_interfaces) && sctp_is_vmware_interface(ifn)) {
521 if (sctp_is_desired_interface_type(ifn) == 0) {
522 /* non desired type */
525 if (ifnet_get_address_list(ifn, &ifaddrlist) != 0) {
528 for (j = 0; ifaddrlist[j] != NULL; j++) {
530 if (ifa->ifa_addr == NULL) {
533 if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
534 /* non inet/inet6 skip */
537 if (ifa->ifa_addr->sa_family == AF_INET6) {
538 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
539 /* skip unspecifed addresses */
543 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == INADDR_ANY) {
547 if (ifa->ifa_addr->sa_family == AF_INET6) {
548 ifa6 = (struct in6_ifaddr *)ifa;
549 ifa_flags = ifa6->ia6_flags;
553 snprintf(name, SCTP_IFNAMSIZ, "%s%d", ifnet_name(ifn), ifnet_unit(ifn));
554 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
564 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
567 ifnet_free_address_list(ifaddrlist);
569 ifnet_list_free(ifnetlist);
573 #if defined(__FreeBSD__)
575 sctp_init_ifns_for_vrf(int vrfid)
577 /* Here we must apply ANY locks needed by the
578 * IFN we access and also make sure we lock
579 * any IFA that exists as we float through the
584 struct sctp_ifa *sctp_ifa;
587 struct in6_ifaddr *ifa6;
591 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
592 if (sctp_is_desired_interface_type(ifn) == 0) {
593 /* non desired type */
596 #if (__FreeBSD_version >= 803000 && __FreeBSD_version < 900000) || __FreeBSD_version > 900000
601 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
602 if (ifa->ifa_addr == NULL) {
605 switch (ifa->ifa_addr->sa_family) {
608 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
615 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
616 /* skip unspecifed addresses */
624 switch (ifa->ifa_addr->sa_family) {
632 ifa6 = (struct in6_ifaddr *)ifa;
633 ifa_flags = ifa6->ia6_flags;
640 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
650 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
653 #if (__FreeBSD_version >= 803000 && __FreeBSD_version < 900000) || __FreeBSD_version > 900000
654 IF_ADDR_RUNLOCK(ifn);
664 sctp_init_vrf_list(int vrfid)
666 if (vrfid > SCTP_MAX_VRF_ID)
670 /* Don't care about return here */
671 (void)sctp_allocate_vrf(vrfid);
673 /* Now we need to build all the ifn's
674 * for this vrf and there addresses
676 sctp_init_ifns_for_vrf(vrfid);
680 sctp_addr_change(struct ifaddr *ifa, int cmd)
682 #if defined(__Userspace__)
685 uint32_t ifa_flags = 0;
686 /* BSD only has one VRF, if this changes
687 * we will need to hook in the right
688 * things here to get the id to pass to
689 * the address managment routine.
691 if (SCTP_BASE_VAR(first_time) == 0) {
692 /* Special test to see if my ::1 will showup with this */
693 SCTP_BASE_VAR(first_time) = 1;
694 sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
697 if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
698 /* don't know what to do with this */
702 if (ifa->ifa_addr == NULL) {
705 if (sctp_is_desired_interface_type(ifa->ifa_ifp) == 0) {
706 /* non desired type */
709 switch (ifa->ifa_addr->sa_family) {
712 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
719 ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
720 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
721 /* skip unspecifed addresses */
727 /* non inet/inet6 skip */
730 if (cmd == RTM_ADD) {
731 (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
732 #if defined(__APPLE__)
733 ifnet_index(ifa->ifa_ifp), ifnet_type(ifa->ifa_ifp), ifnet_name(ifa->ifa_ifp),
735 ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type, ifa->ifa_ifp->if_xname,
737 (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
740 sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
741 #if defined(__APPLE__)
742 ifnet_index(ifa->ifa_ifp),
743 ifnet_name(ifa->ifa_ifp));
745 ifa->ifa_ifp->if_index,
746 ifa->ifa_ifp->if_xname);
749 /* We don't bump refcount here so when it completes
750 * the final delete will happen.
756 #if defined(__FreeBSD__)
758 sctp_add_or_del_interfaces(int (*pred)(struct ifnet *), int add)
764 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
768 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
769 sctp_addr_change(ifa, add ? RTM_ADD : RTM_DELETE);
775 #if defined(__APPLE__)
777 sctp_add_or_del_interfaces(int (*pred)(struct ifnet *), int add)
779 struct ifnet **ifnetlist;
780 struct ifaddr **ifaddrlist;
781 uint32_t i, j, count;
783 if (ifnet_list_get(IFNET_FAMILY_ANY, &ifnetlist, &count) != 0) {
786 for (i = 0; i < count; i++) {
787 if (!(*pred)(ifnetlist[i])) {
790 if (ifnet_get_address_list(ifnetlist[i], &ifaddrlist) != 0) {
793 for (j = 0; ifaddrlist[j] != NULL; j++) {
794 sctp_addr_change(ifaddrlist[j], add ? RTM_ADD : RTM_DELETE);
796 ifnet_free_address_list(ifaddrlist);
798 ifnet_list_free(ifnetlist);
804 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
805 int how, int allonebuf, int type)
807 struct mbuf *m = NULL;
808 #if defined(__Userspace__)
812 * Using m_clget, which creates and mbuf and a cluster and
813 * hooks those together.
814 * TODO: This does not yet have functionality for jumbo packets.
820 MGETHDR(m, how, type);
828 mbuf_threshold = SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count);
833 if ((int)space_needed > (((mbuf_threshold - 1) * MLEN) + MHLEN)) {
839 if (SCTP_BUF_IS_EXTENDED(m) == 0) {
845 SCTP_BUF_NEXT(m) = SCTP_BUF_NEXT_PKT(m) = NULL;
847 #if defined(__Userspace__)
849 * Check if anything need to be done to ensure logging works
852 #ifdef SCTP_MBUF_LOGGING
853 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
854 if (SCTP_BUF_IS_EXTENDED(m)) {
855 sctp_log_mb(m, SCTP_MBUF_IALLOC);
859 #elif defined(__FreeBSD__) && __FreeBSD_version > 602000
860 m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
867 if (SCTP_BUF_IS_EXTENDED(m)) {
868 siz = SCTP_BUF_EXTEND_SIZE(m);
875 if (siz < space_needed) {
880 if (SCTP_BUF_NEXT(m)) {
881 sctp_m_freem( SCTP_BUF_NEXT(m));
882 SCTP_BUF_NEXT(m) = NULL;
884 #ifdef SCTP_MBUF_LOGGING
885 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
886 if (SCTP_BUF_IS_EXTENDED(m)) {
887 sctp_log_mb(m, SCTP_MBUF_IALLOC);
892 #if defined(__FreeBSD__) && __FreeBSD_version >= 601000
898 MGETHDR(m, how, type);
906 mbuf_threshold = SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count);
911 if (space_needed > (((mbuf_threshold - 1) * MLEN) + MHLEN)) {
912 #if defined(__FreeBSD__) && __FreeBSD_version >= 601000
915 if (space_needed <= MCLBYTES) {
916 aloc_size = MCLBYTES;
918 aloc_size = MJUMPAGESIZE;
921 m_cljget(m, how, aloc_size);
925 if (SCTP_BUF_IS_EXTENDED(m) == 0) {
926 if ((aloc_size != MCLBYTES) &&
939 if (SCTP_BUF_IS_EXTENDED(m) == 0) {
946 SCTP_BUF_NEXT(m) = SCTP_BUF_NEXT_PKT(m) = NULL;
947 #ifdef SCTP_MBUF_LOGGING
948 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
949 if (SCTP_BUF_IS_EXTENDED(m)) {
950 sctp_log_mb(m, SCTP_MBUF_IALLOC);
959 #ifdef SCTP_PACKET_LOGGING
961 sctp_packet_log(struct mbuf *m)
968 int grabbed_lock = 0;
969 int value, newval, thisend, thisbegin;
972 * -sizeof this entry (total_len)
973 * -previous end (value)
974 * -ticks of log (ticks)
977 * - where this started (thisbegin)
978 * x <--end points here
980 length = SCTP_HEADER_LEN(m);
981 total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
982 /* Log a packet to the buffer. */
983 if (total_len> SCTP_PACKET_LOG_SIZE) {
984 /* Can't log this packet I have not a buffer big enough */
987 if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
990 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
992 if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
993 SCTP_IP_PKTLOG_LOCK();
996 value = SCTP_BASE_VAR(packet_log_end);
997 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
998 if (newval >= SCTP_PACKET_LOG_SIZE) {
1001 thisend = total_len;
1003 thisbegin = SCTP_BASE_VAR(packet_log_end);
1006 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
1010 value = SCTP_BASE_VAR(packet_log_end);
1011 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
1012 if (newval >= SCTP_PACKET_LOG_SIZE) {
1015 thisend = total_len;
1017 thisbegin = SCTP_BASE_VAR(packet_log_end);
1020 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
1025 if (thisend >= SCTP_PACKET_LOG_SIZE) {
1026 SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
1029 SCTP_BASE_VAR(packet_log_writers),
1031 SCTP_BASE_VAR(packet_log_end));
1032 SCTP_BASE_VAR(packet_log_end) = 0;
1036 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
1041 tick_tock = (uint32_t *)lenat;
1043 *tick_tock = sctp_get_tick_count();
1044 copyto = (void *)lenat;
1045 thisone = thisend - sizeof(int);
1046 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
1049 SCTP_IP_PKTLOG_UNLOCK();
1052 m_copydata(m, 0, length, (caddr_t)copyto);
1055 SCTP_IP_PKTLOG_UNLOCK();
1057 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
1062 sctp_copy_out_packet_log(uint8_t *target, int length)
1064 /* We wind through the packet log starting at
1065 * start copying up to length bytes out.
1066 * We return the number of bytes copied.
1068 int tocopy, this_copy;
1073 if (length < (int)(2 * sizeof(int))) {
1074 /* not enough room */
1077 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
1078 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
1080 if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
1081 /* we delay here for just a moment hoping the writer(s) that were
1082 * present when we entered will have left and we only have
1083 * locking ones that will contend with us for the lock. This
1084 * does not assure 100% access, but its good enough for
1085 * a logging facility like this.
1092 SCTP_IP_PKTLOG_LOCK();
1093 lenat = (int *)target;
1094 *lenat = SCTP_BASE_VAR(packet_log_end);
1096 this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
1097 memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
1098 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
1099 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
1100 SCTP_PKTLOG_WRITERS_NEED_LOCK);
1102 SCTP_IP_PKTLOG_UNLOCK();
1103 return (this_copy + sizeof(int));