1f8829a4aSRandall Stewart /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4b1006367SRandall Stewart * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 5807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 6807aad63SMichael Tuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 7f8829a4aSRandall Stewart * 8f8829a4aSRandall Stewart * Redistribution and use in source and binary forms, with or without 9f8829a4aSRandall Stewart * modification, are permitted provided that the following conditions are met: 10f8829a4aSRandall Stewart * 11f8829a4aSRandall Stewart * a) Redistributions of source code must retain the above copyright notice, 12f8829a4aSRandall Stewart * this list of conditions and the following disclaimer. 13f8829a4aSRandall Stewart * 14f8829a4aSRandall Stewart * b) Redistributions in binary form must reproduce the above copyright 15f8829a4aSRandall Stewart * notice, this list of conditions and the following disclaimer in 16f8829a4aSRandall Stewart * the documentation and/or other materials provided with the distribution. 17f8829a4aSRandall Stewart * 18f8829a4aSRandall Stewart * c) Neither the name of Cisco Systems, Inc. nor the names of its 19f8829a4aSRandall Stewart * contributors may be used to endorse or promote products derived 20f8829a4aSRandall Stewart * from this software without specific prior written permission. 21f8829a4aSRandall Stewart * 22f8829a4aSRandall Stewart * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23f8829a4aSRandall Stewart * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24f8829a4aSRandall Stewart * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25f8829a4aSRandall Stewart * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 26f8829a4aSRandall Stewart * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27f8829a4aSRandall Stewart * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28f8829a4aSRandall Stewart * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29f8829a4aSRandall Stewart * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30f8829a4aSRandall Stewart * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31f8829a4aSRandall Stewart * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32f8829a4aSRandall Stewart * THE POSSIBILITY OF SUCH DAMAGE. 33f8829a4aSRandall Stewart */ 34f8829a4aSRandall Stewart 35f8829a4aSRandall Stewart #include <sys/cdefs.h> 36f8829a4aSRandall Stewart __FBSDID("$FreeBSD$"); 37f8829a4aSRandall Stewart 38f8829a4aSRandall Stewart #include <netinet/sctp_os.h> 39f8829a4aSRandall Stewart #include <netinet/sctp_var.h> 40f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h> 41f8829a4aSRandall Stewart #include <netinet/sctp_header.h> 42f8829a4aSRandall Stewart #include <netinet/sctputil.h> 43f8829a4aSRandall Stewart #include <netinet/sctp_output.h> 44f8829a4aSRandall Stewart #include <netinet/sctp_bsd_addr.h> 45f8829a4aSRandall Stewart #include <netinet/sctp_uio.h> 46f8829a4aSRandall Stewart #include <netinet/sctputil.h> 47f8829a4aSRandall Stewart #include <netinet/sctp_timer.h> 48f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h> 49d06c82f1SRandall Stewart #include <netinet/sctp_sysctl.h> 50f8829a4aSRandall Stewart #include <netinet/sctp_indata.h> 5142551e99SRandall Stewart #include <sys/unistd.h> 52f8829a4aSRandall Stewart 53207304d4SRandall Stewart /* Declare all of our malloc named types */ 54207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor"); 55207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array"); 56207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array"); 57207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address"); 58207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator"); 59207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist"); 60207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key"); 61207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list"); 62207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info"); 63207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset"); 64207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer"); 65207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all"); 66207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct"); 67207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct"); 68207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct"); 69207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block"); 70207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list"); 71207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control"); 72207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option"); 73bfc46083SRandall Stewart MALLOC_DEFINE(SCTP_M_MCORE, "sctp_mcore", "sctp mcore queue"); 74207304d4SRandall Stewart 75f7517433SRandall Stewart /* Global NON-VNET structure that controls the iterator */ 76f7517433SRandall Stewart struct iterator_control sctp_it_ctl; 77f7517433SRandall Stewart 7842551e99SRandall Stewart void 7942551e99SRandall Stewart sctp_wakeup_iterator(void) 80f8829a4aSRandall Stewart { 81f7517433SRandall Stewart wakeup(&sctp_it_ctl.iterator_running); 82f8829a4aSRandall Stewart } 83f8829a4aSRandall Stewart 8442551e99SRandall Stewart static void 857215cc1bSMichael Tuexen sctp_iterator_thread(void *v SCTP_UNUSED) 86f8829a4aSRandall Stewart { 8742551e99SRandall Stewart SCTP_IPI_ITERATOR_WQ_LOCK(); 8887eac1ceSMichael Tuexen /* In FreeBSD this thread never terminates. */ 897215cc1bSMichael Tuexen for (;;) { 90f7517433SRandall Stewart msleep(&sctp_it_ctl.iterator_running, 91f7517433SRandall Stewart &sctp_it_ctl.ipi_iterator_wq_mtx, 92bf949ea2SRandall Stewart 0, "waiting_for_work", 0); 9342551e99SRandall Stewart sctp_iterator_worker(); 94f8829a4aSRandall Stewart } 95f8829a4aSRandall Stewart } 96f8829a4aSRandall Stewart 9742551e99SRandall Stewart void 9842551e99SRandall Stewart sctp_startup_iterator(void) 99f8829a4aSRandall Stewart { 100f4f34bdeSMichael Tuexen if (sctp_it_ctl.thread_proc) { 101f7517433SRandall Stewart /* You only get one */ 102f7517433SRandall Stewart return; 103f7517433SRandall Stewart } 104c302aeb1SMichael Tuexen /* Initialize global locks here, thus only once. */ 105c302aeb1SMichael Tuexen SCTP_ITERATOR_LOCK_INIT(); 106c302aeb1SMichael Tuexen SCTP_IPI_ITERATOR_WQ_INIT(); 107f7517433SRandall Stewart TAILQ_INIT(&sctp_it_ctl.iteratorhead); 108f4f34bdeSMichael Tuexen kproc_create(sctp_iterator_thread, 109f7517433SRandall Stewart (void *)NULL, 110f7517433SRandall Stewart &sctp_it_ctl.thread_proc, 111*5a50eb65SJohn Baldwin 0, 11242551e99SRandall Stewart SCTP_KTHREAD_PAGES, 11342551e99SRandall Stewart SCTP_KTRHEAD_NAME); 11442551e99SRandall Stewart } 115f8829a4aSRandall Stewart 1165e2c2d87SRandall Stewart #ifdef INET6 117fc14de76SRandall Stewart 11842551e99SRandall Stewart void 11942551e99SRandall Stewart sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa) 120f8829a4aSRandall Stewart { 121f8829a4aSRandall Stewart struct in6_ifaddr *ifa6; 122f8829a4aSRandall Stewart 12342551e99SRandall Stewart ifa6 = (struct in6_ifaddr *)ifa->ifa; 12442551e99SRandall Stewart ifa->flags = ifa6->ia6_flags; 125482444b4SRandall Stewart if (!MODULE_GLOBAL(ip6_use_deprecated)) { 12642551e99SRandall Stewart if (ifa->flags & 127f8829a4aSRandall Stewart IN6_IFF_DEPRECATED) { 12842551e99SRandall Stewart ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE; 12942551e99SRandall Stewart } else { 13042551e99SRandall Stewart ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE; 131f8829a4aSRandall Stewart } 13242551e99SRandall Stewart } else { 13342551e99SRandall Stewart ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE; 134f8829a4aSRandall Stewart } 13542551e99SRandall Stewart if (ifa->flags & 136f8829a4aSRandall Stewart (IN6_IFF_DETACHED | 137f8829a4aSRandall Stewart IN6_IFF_ANYCAST | 138f8829a4aSRandall Stewart IN6_IFF_NOTREADY)) { 13942551e99SRandall Stewart ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE; 140f8829a4aSRandall Stewart } else { 14142551e99SRandall Stewart ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE; 142f8829a4aSRandall Stewart } 143f8829a4aSRandall Stewart } 144fc14de76SRandall Stewart #endif /* INET6 */ 14542551e99SRandall Stewart 14642551e99SRandall Stewart static uint32_t 147b0471b4bSMichael Tuexen sctp_is_desired_interface_type(struct ifnet *ifn) 148b0471b4bSMichael Tuexen { 14942551e99SRandall Stewart int result; 150f8829a4aSRandall Stewart 15142551e99SRandall Stewart /* check the interface type to see if it's one we care about */ 152173be2b6SMichael Tuexen switch (ifn->if_type) { 15342551e99SRandall Stewart case IFT_ETHER: 15442551e99SRandall Stewart case IFT_ISO88023: 15542551e99SRandall Stewart case IFT_ISO88024: 15642551e99SRandall Stewart case IFT_ISO88025: 15742551e99SRandall Stewart case IFT_ISO88026: 15842551e99SRandall Stewart case IFT_STARLAN: 15942551e99SRandall Stewart case IFT_P10: 16042551e99SRandall Stewart case IFT_P80: 16142551e99SRandall Stewart case IFT_HY: 16242551e99SRandall Stewart case IFT_FDDI: 16342551e99SRandall Stewart case IFT_XETHER: 16442551e99SRandall Stewart case IFT_ISDNBASIC: 16542551e99SRandall Stewart case IFT_ISDNPRIMARY: 16642551e99SRandall Stewart case IFT_PTPSERIAL: 167851b7298SRandall Stewart case IFT_OTHER: 16842551e99SRandall Stewart case IFT_PPP: 16942551e99SRandall Stewart case IFT_LOOP: 17042551e99SRandall Stewart case IFT_SLIP: 171b3f1ea41SRandall Stewart case IFT_GIF: 1722b77dd01SMichael Tuexen case IFT_L2VLAN: 17370a03e88SMichael Tuexen case IFT_STF: 17442551e99SRandall Stewart case IFT_IP: 17542551e99SRandall Stewart case IFT_IPOVERCDLC: 17642551e99SRandall Stewart case IFT_IPOVERCLAW: 1777081943dSRandall Stewart case IFT_PROPVIRTUAL: /* NetGraph Virtual too */ 17842551e99SRandall Stewart case IFT_VIRTUALIPADDRESS: 17942551e99SRandall Stewart result = 1; 18042551e99SRandall Stewart break; 18142551e99SRandall Stewart default: 18242551e99SRandall Stewart result = 0; 183f8829a4aSRandall Stewart } 184f8829a4aSRandall Stewart 18542551e99SRandall Stewart return (result); 18642551e99SRandall Stewart } 187f8829a4aSRandall Stewart 18842551e99SRandall Stewart static void 18942551e99SRandall Stewart sctp_init_ifns_for_vrf(int vrfid) 190f8829a4aSRandall Stewart { 19142551e99SRandall Stewart /* 19242551e99SRandall Stewart * Here we must apply ANY locks needed by the IFN we access and also 19342551e99SRandall Stewart * make sure we lock any IFA that exists as we float through the 19442551e99SRandall Stewart * list of IFA's 19542551e99SRandall Stewart */ 196b8a6e03fSGleb Smirnoff struct epoch_tracker et; 197f8829a4aSRandall Stewart struct ifnet *ifn; 198f8829a4aSRandall Stewart struct ifaddr *ifa; 19942551e99SRandall Stewart struct sctp_ifa *sctp_ifa; 20042551e99SRandall Stewart uint32_t ifa_flags; 201e6194c2eSMichael Tuexen #ifdef INET6 202e6194c2eSMichael Tuexen struct in6_ifaddr *ifa6; 203e6194c2eSMichael Tuexen #endif 204e6194c2eSMichael Tuexen 2058518270eSMichael Tuexen IFNET_RLOCK(); 206b8a6e03fSGleb Smirnoff NET_EPOCH_ENTER(et); 2074f6c66ccSMatt Macy CK_STAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_link) { 208173be2b6SMichael Tuexen if (sctp_is_desired_interface_type(ifn) == 0) { 209173be2b6SMichael Tuexen /* non desired type */ 210173be2b6SMichael Tuexen continue; 211173be2b6SMichael Tuexen } 212d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) { 21342551e99SRandall Stewart if (ifa->ifa_addr == NULL) { 214f8829a4aSRandall Stewart continue; 215f8829a4aSRandall Stewart } 216e6194c2eSMichael Tuexen switch (ifa->ifa_addr->sa_family) { 217e6194c2eSMichael Tuexen #ifdef INET 218e6194c2eSMichael Tuexen case AF_INET: 219e6194c2eSMichael Tuexen if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) { 220f8829a4aSRandall Stewart continue; 221f8829a4aSRandall Stewart } 222e6194c2eSMichael Tuexen break; 223e6194c2eSMichael Tuexen #endif 224e6194c2eSMichael Tuexen #ifdef INET6 225e6194c2eSMichael Tuexen case AF_INET6: 22642551e99SRandall Stewart if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) { 22742551e99SRandall Stewart /* skip unspecifed addresses */ 228f8829a4aSRandall Stewart continue; 229f8829a4aSRandall Stewart } 230e6194c2eSMichael Tuexen break; 231e6194c2eSMichael Tuexen #endif 232e6194c2eSMichael Tuexen default: 23342551e99SRandall Stewart continue; 234f8829a4aSRandall Stewart } 235e6194c2eSMichael Tuexen switch (ifa->ifa_addr->sa_family) { 236e6194c2eSMichael Tuexen #ifdef INET 237e6194c2eSMichael Tuexen case AF_INET: 238e6194c2eSMichael Tuexen ifa_flags = 0; 239e6194c2eSMichael Tuexen break; 240e6194c2eSMichael Tuexen #endif 241e6194c2eSMichael Tuexen #ifdef INET6 242e6194c2eSMichael Tuexen case AF_INET6: 24342551e99SRandall Stewart ifa6 = (struct in6_ifaddr *)ifa; 24442551e99SRandall Stewart ifa_flags = ifa6->ia6_flags; 245e6194c2eSMichael Tuexen break; 246e6194c2eSMichael Tuexen #endif 247e6194c2eSMichael Tuexen default: 24842551e99SRandall Stewart ifa_flags = 0; 249e6194c2eSMichael Tuexen break; 25042551e99SRandall Stewart } 25142551e99SRandall Stewart sctp_ifa = sctp_add_addr_to_vrf(vrfid, 25242551e99SRandall Stewart (void *)ifn, 25342551e99SRandall Stewart ifn->if_index, 25442551e99SRandall Stewart ifn->if_type, 25542551e99SRandall Stewart ifn->if_xname, 25642551e99SRandall Stewart (void *)ifa, 25742551e99SRandall Stewart ifa->ifa_addr, 258b3f1ea41SRandall Stewart ifa_flags, 259b3f1ea41SRandall Stewart 0); 26042551e99SRandall Stewart if (sctp_ifa) { 26142551e99SRandall Stewart sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE; 26242551e99SRandall Stewart } 26342551e99SRandall Stewart } 26442551e99SRandall Stewart } 265b8a6e03fSGleb Smirnoff NET_EPOCH_EXIT(et); 2668518270eSMichael Tuexen IFNET_RUNLOCK(); 26742551e99SRandall Stewart } 268f8829a4aSRandall Stewart 26942551e99SRandall Stewart void 27042551e99SRandall Stewart sctp_init_vrf_list(int vrfid) 27142551e99SRandall Stewart { 27242551e99SRandall Stewart if (vrfid > SCTP_MAX_VRF_ID) 27342551e99SRandall Stewart /* can't do that */ 27442551e99SRandall Stewart return; 27542551e99SRandall Stewart 27642551e99SRandall Stewart /* Don't care about return here */ 27742551e99SRandall Stewart (void)sctp_allocate_vrf(vrfid); 27842551e99SRandall Stewart 279f8829a4aSRandall Stewart /* 28042551e99SRandall Stewart * Now we need to build all the ifn's for this vrf and there 28142551e99SRandall Stewart * addresses 282f8829a4aSRandall Stewart */ 28342551e99SRandall Stewart sctp_init_ifns_for_vrf(vrfid); 284f8829a4aSRandall Stewart } 28542551e99SRandall Stewart 28642551e99SRandall Stewart void 28742551e99SRandall Stewart sctp_addr_change(struct ifaddr *ifa, int cmd) 28842551e99SRandall Stewart { 28942551e99SRandall Stewart uint32_t ifa_flags = 0; 29042551e99SRandall Stewart 2912b1c7de4SMichael Tuexen if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { 2922b1c7de4SMichael Tuexen return; 2932b1c7de4SMichael Tuexen } 29442551e99SRandall Stewart /* 29542551e99SRandall Stewart * BSD only has one VRF, if this changes we will need to hook in the 296cd0a4ff6SPedro F. Giffuni * right things here to get the id to pass to the address management 29742551e99SRandall Stewart * routine. 29842551e99SRandall Stewart */ 299b3f1ea41SRandall Stewart if (SCTP_BASE_VAR(first_time) == 0) { 30042551e99SRandall Stewart /* Special test to see if my ::1 will showup with this */ 301b3f1ea41SRandall Stewart SCTP_BASE_VAR(first_time) = 1; 30242551e99SRandall Stewart sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID); 303f8829a4aSRandall Stewart } 3040053ed28SMichael Tuexen 30542551e99SRandall Stewart if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) { 30642551e99SRandall Stewart /* don't know what to do with this */ 30742551e99SRandall Stewart return; 308f8829a4aSRandall Stewart } 3090053ed28SMichael Tuexen 31042551e99SRandall Stewart if (ifa->ifa_addr == NULL) { 31142551e99SRandall Stewart return; 31242551e99SRandall Stewart } 313173be2b6SMichael Tuexen if (sctp_is_desired_interface_type(ifa->ifa_ifp) == 0) { 314173be2b6SMichael Tuexen /* non desired type */ 315173be2b6SMichael Tuexen return; 316173be2b6SMichael Tuexen } 317e6194c2eSMichael Tuexen switch (ifa->ifa_addr->sa_family) { 318e6194c2eSMichael Tuexen #ifdef INET 319e6194c2eSMichael Tuexen case AF_INET: 320e6194c2eSMichael Tuexen if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) { 32142551e99SRandall Stewart return; 32242551e99SRandall Stewart } 323e6194c2eSMichael Tuexen break; 324e6194c2eSMichael Tuexen #endif 325e6194c2eSMichael Tuexen #ifdef INET6 326e6194c2eSMichael Tuexen case AF_INET6: 327b3f1ea41SRandall Stewart ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags; 32842551e99SRandall Stewart if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) { 32942551e99SRandall Stewart /* skip unspecifed addresses */ 33042551e99SRandall Stewart return; 33142551e99SRandall Stewart } 332e6194c2eSMichael Tuexen break; 333e6194c2eSMichael Tuexen #endif 334e6194c2eSMichael Tuexen default: 335e6194c2eSMichael Tuexen /* non inet/inet6 skip */ 33642551e99SRandall Stewart return; 337f8829a4aSRandall Stewart } 33842551e99SRandall Stewart if (cmd == RTM_ADD) { 3393c8c191bSMichael Tuexen (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp, 340173be2b6SMichael Tuexen ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type, ifa->ifa_ifp->if_xname, 341d06c82f1SRandall Stewart (void *)ifa, ifa->ifa_addr, ifa_flags, 1); 342b3f1ea41SRandall Stewart } else { 343851b7298SRandall Stewart sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr, 344851b7298SRandall Stewart ifa->ifa_ifp->if_index, 345173be2b6SMichael Tuexen ifa->ifa_ifp->if_xname); 346173be2b6SMichael Tuexen 34742551e99SRandall Stewart /* 34842551e99SRandall Stewart * We don't bump refcount here so when it completes the 34942551e99SRandall Stewart * final delete will happen. 35042551e99SRandall Stewart */ 35142551e99SRandall Stewart } 352d06c82f1SRandall Stewart } 35342551e99SRandall Stewart 354b3f1ea41SRandall Stewart void 355d6e23cf0SMichael Tuexen sctp_addr_change_event_handler(void *arg __unused, struct ifaddr *ifa, int cmd) 356d6e23cf0SMichael Tuexen { 357d6e23cf0SMichael Tuexen sctp_addr_change(ifa, cmd); 358d6e23cf0SMichael Tuexen } 359d6e23cf0SMichael Tuexen 360d06c82f1SRandall Stewart struct mbuf * 361d06c82f1SRandall Stewart sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header, 362d06c82f1SRandall Stewart int how, int allonebuf, int type) 363d06c82f1SRandall Stewart { 364d06c82f1SRandall Stewart struct mbuf *m = NULL; 36542551e99SRandall Stewart 366d06c82f1SRandall Stewart m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0); 367d06c82f1SRandall Stewart if (m == NULL) { 368d06c82f1SRandall Stewart /* bad, no memory */ 369d06c82f1SRandall Stewart return (m); 37042551e99SRandall Stewart } 371d06c82f1SRandall Stewart if (allonebuf) { 372296d0b94SMichael Tuexen if (SCTP_BUF_SIZE(m) < space_needed) { 373d06c82f1SRandall Stewart m_freem(m); 374d06c82f1SRandall Stewart return (NULL); 375d06c82f1SRandall Stewart } 3769f2d6263SMichael Tuexen KASSERT(SCTP_BUF_NEXT(m) == NULL, ("%s: no chain allowed", __func__)); 377d06c82f1SRandall Stewart } 378d06c82f1SRandall Stewart #ifdef SCTP_MBUF_LOGGING 379b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 380d06c82f1SRandall Stewart sctp_log_mb(m, SCTP_MBUF_IALLOC); 381d06c82f1SRandall Stewart } 382d06c82f1SRandall Stewart #endif 383d06c82f1SRandall Stewart return (m); 384f8829a4aSRandall Stewart } 385207304d4SRandall Stewart 386207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING 387207304d4SRandall Stewart void 388f9384252SMichael Tuexen sctp_packet_log(struct mbuf *m) 389207304d4SRandall Stewart { 3900696e120SRandall Stewart int *lenat, thisone; 391207304d4SRandall Stewart void *copyto; 392207304d4SRandall Stewart uint32_t *tick_tock; 393f9384252SMichael Tuexen int length; 3940696e120SRandall Stewart int total_len; 3950696e120SRandall Stewart int grabbed_lock = 0; 3960696e120SRandall Stewart int value, newval, thisend, thisbegin; 397207304d4SRandall Stewart 3980696e120SRandall Stewart /* 3990696e120SRandall Stewart * Buffer layout. -sizeof this entry (total_len) -previous end 4000696e120SRandall Stewart * (value) -ticks of log (ticks) o -ip packet o -as logged - 4010696e120SRandall Stewart * where this started (thisbegin) x <--end points here 4020696e120SRandall Stewart */ 403f9384252SMichael Tuexen length = SCTP_HEADER_LEN(m); 4040696e120SRandall Stewart total_len = SCTP_SIZE32((length + (4 * sizeof(int)))); 405207304d4SRandall Stewart /* Log a packet to the buffer. */ 406207304d4SRandall Stewart if (total_len > SCTP_PACKET_LOG_SIZE) { 407207304d4SRandall Stewart /* Can't log this packet I have not a buffer big enough */ 408207304d4SRandall Stewart return; 409207304d4SRandall Stewart } 410b3f1ea41SRandall Stewart if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) { 411207304d4SRandall Stewart return; 412207304d4SRandall Stewart } 413b3f1ea41SRandall Stewart atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1); 4140696e120SRandall Stewart try_again: 415b3f1ea41SRandall Stewart if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) { 416207304d4SRandall Stewart SCTP_IP_PKTLOG_LOCK(); 4170696e120SRandall Stewart grabbed_lock = 1; 4180696e120SRandall Stewart again_locked: 419b3f1ea41SRandall Stewart value = SCTP_BASE_VAR(packet_log_end); 420b3f1ea41SRandall Stewart newval = SCTP_BASE_VAR(packet_log_end) + total_len; 4210696e120SRandall Stewart if (newval >= SCTP_PACKET_LOG_SIZE) { 4220696e120SRandall Stewart /* we wrapped */ 4230696e120SRandall Stewart thisbegin = 0; 4240696e120SRandall Stewart thisend = total_len; 4250696e120SRandall Stewart } else { 426b3f1ea41SRandall Stewart thisbegin = SCTP_BASE_VAR(packet_log_end); 4270696e120SRandall Stewart thisend = newval; 4280696e120SRandall Stewart } 429b3f1ea41SRandall Stewart if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) { 4300696e120SRandall Stewart goto again_locked; 4310696e120SRandall Stewart } 4320696e120SRandall Stewart } else { 433b3f1ea41SRandall Stewart value = SCTP_BASE_VAR(packet_log_end); 434b3f1ea41SRandall Stewart newval = SCTP_BASE_VAR(packet_log_end) + total_len; 4350696e120SRandall Stewart if (newval >= SCTP_PACKET_LOG_SIZE) { 4360696e120SRandall Stewart /* we wrapped */ 4370696e120SRandall Stewart thisbegin = 0; 4380696e120SRandall Stewart thisend = total_len; 4390696e120SRandall Stewart } else { 440b3f1ea41SRandall Stewart thisbegin = SCTP_BASE_VAR(packet_log_end); 4410696e120SRandall Stewart thisend = newval; 4420696e120SRandall Stewart } 443b3f1ea41SRandall Stewart if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) { 4440696e120SRandall Stewart goto try_again; 4450696e120SRandall Stewart } 4460696e120SRandall Stewart } 4470696e120SRandall Stewart /* Sanity check */ 4480696e120SRandall Stewart if (thisend >= SCTP_PACKET_LOG_SIZE) { 449cd3fd531SMichael Tuexen SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n", 4500696e120SRandall Stewart thisbegin, 4510696e120SRandall Stewart thisend, 452b3f1ea41SRandall Stewart SCTP_BASE_VAR(packet_log_writers), 4530696e120SRandall Stewart grabbed_lock, 454b3f1ea41SRandall Stewart SCTP_BASE_VAR(packet_log_end)); 455b3f1ea41SRandall Stewart SCTP_BASE_VAR(packet_log_end) = 0; 4560696e120SRandall Stewart goto no_log; 457207304d4SRandall Stewart } 458b3f1ea41SRandall Stewart lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin]; 459207304d4SRandall Stewart *lenat = total_len; 460207304d4SRandall Stewart lenat++; 4610696e120SRandall Stewart *lenat = value; 4620696e120SRandall Stewart lenat++; 463207304d4SRandall Stewart tick_tock = (uint32_t *)lenat; 464207304d4SRandall Stewart lenat++; 465207304d4SRandall Stewart *tick_tock = sctp_get_tick_count(); 466207304d4SRandall Stewart copyto = (void *)lenat; 4670696e120SRandall Stewart thisone = thisend - sizeof(int); 468b3f1ea41SRandall Stewart lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone]; 4690696e120SRandall Stewart *lenat = thisbegin; 4700696e120SRandall Stewart if (grabbed_lock) { 471207304d4SRandall Stewart SCTP_IP_PKTLOG_UNLOCK(); 4720696e120SRandall Stewart grabbed_lock = 0; 4730696e120SRandall Stewart } 474207304d4SRandall Stewart m_copydata(m, 0, length, (caddr_t)copyto); 4750696e120SRandall Stewart no_log: 4760696e120SRandall Stewart if (grabbed_lock) { 4770696e120SRandall Stewart SCTP_IP_PKTLOG_UNLOCK(); 4780696e120SRandall Stewart } 479b3f1ea41SRandall Stewart atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1); 480207304d4SRandall Stewart } 481207304d4SRandall Stewart 482207304d4SRandall Stewart int 483207304d4SRandall Stewart sctp_copy_out_packet_log(uint8_t *target, int length) 484207304d4SRandall Stewart { 485207304d4SRandall Stewart /* 486207304d4SRandall Stewart * We wind through the packet log starting at start copying up to 487207304d4SRandall Stewart * length bytes out. We return the number of bytes copied. 488207304d4SRandall Stewart */ 4890696e120SRandall Stewart int tocopy, this_copy; 4900696e120SRandall Stewart int *lenat; 4910696e120SRandall Stewart int did_delay = 0; 492207304d4SRandall Stewart 493207304d4SRandall Stewart tocopy = length; 494b3f1ea41SRandall Stewart if (length < (int)(2 * sizeof(int))) { 4950696e120SRandall Stewart /* not enough room */ 496207304d4SRandall Stewart return (0); 497207304d4SRandall Stewart } 4980696e120SRandall Stewart if (SCTP_PKTLOG_WRITERS_NEED_LOCK) { 499b3f1ea41SRandall Stewart atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK); 5000696e120SRandall Stewart again: 501b3f1ea41SRandall Stewart if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) { 502207304d4SRandall Stewart /* 5030696e120SRandall Stewart * we delay here for just a moment hoping the 5040696e120SRandall Stewart * writer(s) that were present when we entered will 5050696e120SRandall Stewart * have left and we only have locking ones that will 5060696e120SRandall Stewart * contend with us for the lock. This does not 5070696e120SRandall Stewart * assure 100% access, but its good enough for a 5080696e120SRandall Stewart * logging facility like this. 509207304d4SRandall Stewart */ 5100696e120SRandall Stewart did_delay = 1; 5110696e120SRandall Stewart DELAY(10); 5120696e120SRandall Stewart goto again; 513207304d4SRandall Stewart } 514207304d4SRandall Stewart } 5150696e120SRandall Stewart SCTP_IP_PKTLOG_LOCK(); 5160696e120SRandall Stewart lenat = (int *)target; 517b3f1ea41SRandall Stewart *lenat = SCTP_BASE_VAR(packet_log_end); 5180696e120SRandall Stewart lenat++; 51919d8ca2eSRandall Stewart this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE); 520b3f1ea41SRandall Stewart memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy); 5210696e120SRandall Stewart if (SCTP_PKTLOG_WRITERS_NEED_LOCK) { 522b3f1ea41SRandall Stewart atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 52319d8ca2eSRandall Stewart SCTP_PKTLOG_WRITERS_NEED_LOCK); 5240696e120SRandall Stewart } 5250696e120SRandall Stewart SCTP_IP_PKTLOG_UNLOCK(); 5260696e120SRandall Stewart return (this_copy + sizeof(int)); 527207304d4SRandall Stewart } 528207304d4SRandall Stewart 529207304d4SRandall Stewart #endif 530