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 <netinet/sctp_os.h>
36f8829a4aSRandall Stewart #include <netinet/sctp_var.h>
37f8829a4aSRandall Stewart #include <netinet/sctp_pcb.h>
38f8829a4aSRandall Stewart #include <netinet/sctp_header.h>
39f8829a4aSRandall Stewart #include <netinet/sctputil.h>
40f8829a4aSRandall Stewart #include <netinet/sctp_output.h>
41f8829a4aSRandall Stewart #include <netinet/sctp_bsd_addr.h>
42f8829a4aSRandall Stewart #include <netinet/sctp_uio.h>
43f8829a4aSRandall Stewart #include <netinet/sctputil.h>
44f8829a4aSRandall Stewart #include <netinet/sctp_timer.h>
45f8829a4aSRandall Stewart #include <netinet/sctp_asconf.h>
46d06c82f1SRandall Stewart #include <netinet/sctp_sysctl.h>
47f8829a4aSRandall Stewart #include <netinet/sctp_indata.h>
4842551e99SRandall Stewart #include <sys/unistd.h>
49f8829a4aSRandall Stewart
50207304d4SRandall Stewart /* Declare all of our malloc named types */
51207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
52207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
53207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
54207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
55207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
56207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
57207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
58207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
59207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
60207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
61207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
62207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
63207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
64207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
65207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
66207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
67207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
68207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
69207304d4SRandall Stewart MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
70bfc46083SRandall Stewart MALLOC_DEFINE(SCTP_M_MCORE, "sctp_mcore", "sctp mcore queue");
71207304d4SRandall Stewart
72f7517433SRandall Stewart /* Global NON-VNET structure that controls the iterator */
73f7517433SRandall Stewart struct iterator_control sctp_it_ctl;
74f7517433SRandall Stewart
7542551e99SRandall Stewart void
sctp_wakeup_iterator(void)7642551e99SRandall Stewart sctp_wakeup_iterator(void)
77f8829a4aSRandall Stewart {
78f7517433SRandall Stewart wakeup(&sctp_it_ctl.iterator_running);
79f8829a4aSRandall Stewart }
80f8829a4aSRandall Stewart
8142551e99SRandall Stewart static void
sctp_iterator_thread(void * v SCTP_UNUSED)827215cc1bSMichael Tuexen sctp_iterator_thread(void *v SCTP_UNUSED)
83f8829a4aSRandall Stewart {
8442551e99SRandall Stewart SCTP_IPI_ITERATOR_WQ_LOCK();
8587eac1ceSMichael Tuexen /* In FreeBSD this thread never terminates. */
867215cc1bSMichael Tuexen for (;;) {
87f7517433SRandall Stewart msleep(&sctp_it_ctl.iterator_running,
88f7517433SRandall Stewart &sctp_it_ctl.ipi_iterator_wq_mtx,
89bf949ea2SRandall Stewart 0, "waiting_for_work", 0);
9042551e99SRandall Stewart sctp_iterator_worker();
91f8829a4aSRandall Stewart }
92f8829a4aSRandall Stewart }
93f8829a4aSRandall Stewart
9442551e99SRandall Stewart void
sctp_startup_iterator(void)9542551e99SRandall Stewart sctp_startup_iterator(void)
96f8829a4aSRandall Stewart {
97f4f34bdeSMichael Tuexen if (sctp_it_ctl.thread_proc) {
98f7517433SRandall Stewart /* You only get one */
99f7517433SRandall Stewart return;
100f7517433SRandall Stewart }
101c302aeb1SMichael Tuexen /* Initialize global locks here, thus only once. */
102c302aeb1SMichael Tuexen SCTP_ITERATOR_LOCK_INIT();
103c302aeb1SMichael Tuexen SCTP_IPI_ITERATOR_WQ_INIT();
104f7517433SRandall Stewart TAILQ_INIT(&sctp_it_ctl.iteratorhead);
105f4f34bdeSMichael Tuexen kproc_create(sctp_iterator_thread,
106f7517433SRandall Stewart (void *)NULL,
107f7517433SRandall Stewart &sctp_it_ctl.thread_proc,
1085a50eb65SJohn Baldwin 0,
10942551e99SRandall Stewart SCTP_KTHREAD_PAGES,
11042551e99SRandall Stewart SCTP_KTRHEAD_NAME);
11142551e99SRandall Stewart }
112f8829a4aSRandall Stewart
1135e2c2d87SRandall Stewart #ifdef INET6
114fc14de76SRandall Stewart
11542551e99SRandall Stewart void
sctp_gather_internal_ifa_flags(struct sctp_ifa * ifa)11642551e99SRandall Stewart sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
117f8829a4aSRandall Stewart {
118f8829a4aSRandall Stewart struct in6_ifaddr *ifa6;
119f8829a4aSRandall Stewart
12042551e99SRandall Stewart ifa6 = (struct in6_ifaddr *)ifa->ifa;
12142551e99SRandall Stewart ifa->flags = ifa6->ia6_flags;
122482444b4SRandall Stewart if (!MODULE_GLOBAL(ip6_use_deprecated)) {
12342551e99SRandall Stewart if (ifa->flags &
124f8829a4aSRandall Stewart IN6_IFF_DEPRECATED) {
12542551e99SRandall Stewart ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
12642551e99SRandall Stewart } else {
12742551e99SRandall Stewart ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
128f8829a4aSRandall Stewart }
12942551e99SRandall Stewart } else {
13042551e99SRandall Stewart ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
131f8829a4aSRandall Stewart }
13242551e99SRandall Stewart if (ifa->flags &
133f8829a4aSRandall Stewart (IN6_IFF_DETACHED |
134f8829a4aSRandall Stewart IN6_IFF_ANYCAST |
135f8829a4aSRandall Stewart IN6_IFF_NOTREADY)) {
13642551e99SRandall Stewart ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
137f8829a4aSRandall Stewart } else {
13842551e99SRandall Stewart ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
139f8829a4aSRandall Stewart }
140f8829a4aSRandall Stewart }
141fc14de76SRandall Stewart #endif /* INET6 */
14242551e99SRandall Stewart
14342551e99SRandall Stewart static uint32_t
sctp_is_desired_interface_type(struct ifnet * ifn)144b0471b4bSMichael Tuexen sctp_is_desired_interface_type(struct ifnet *ifn)
145b0471b4bSMichael Tuexen {
14642551e99SRandall Stewart int result;
147f8829a4aSRandall Stewart
14842551e99SRandall Stewart /* check the interface type to see if it's one we care about */
149173be2b6SMichael Tuexen switch (ifn->if_type) {
15042551e99SRandall Stewart case IFT_ETHER:
15142551e99SRandall Stewart case IFT_ISO88023:
15242551e99SRandall Stewart case IFT_ISO88024:
15342551e99SRandall Stewart case IFT_ISO88025:
15442551e99SRandall Stewart case IFT_ISO88026:
15542551e99SRandall Stewart case IFT_STARLAN:
15642551e99SRandall Stewart case IFT_P10:
15742551e99SRandall Stewart case IFT_P80:
15842551e99SRandall Stewart case IFT_HY:
15942551e99SRandall Stewart case IFT_FDDI:
16042551e99SRandall Stewart case IFT_XETHER:
16142551e99SRandall Stewart case IFT_ISDNBASIC:
16242551e99SRandall Stewart case IFT_ISDNPRIMARY:
16342551e99SRandall Stewart case IFT_PTPSERIAL:
164851b7298SRandall Stewart case IFT_OTHER:
16542551e99SRandall Stewart case IFT_PPP:
16642551e99SRandall Stewart case IFT_LOOP:
16742551e99SRandall Stewart case IFT_SLIP:
168b3f1ea41SRandall Stewart case IFT_GIF:
1692b77dd01SMichael Tuexen case IFT_L2VLAN:
17070a03e88SMichael Tuexen case IFT_STF:
17142551e99SRandall Stewart case IFT_IP:
17242551e99SRandall Stewart case IFT_IPOVERCDLC:
17342551e99SRandall Stewart case IFT_IPOVERCLAW:
1747081943dSRandall Stewart case IFT_PROPVIRTUAL: /* NetGraph Virtual too */
17542551e99SRandall Stewart case IFT_VIRTUALIPADDRESS:
17642551e99SRandall Stewart result = 1;
17742551e99SRandall Stewart break;
17842551e99SRandall Stewart default:
17942551e99SRandall Stewart result = 0;
180f8829a4aSRandall Stewart }
181f8829a4aSRandall Stewart
18242551e99SRandall Stewart return (result);
18342551e99SRandall Stewart }
184f8829a4aSRandall Stewart
18542551e99SRandall Stewart static void
sctp_init_ifns_for_vrf(int vrfid)18642551e99SRandall Stewart sctp_init_ifns_for_vrf(int vrfid)
187f8829a4aSRandall Stewart {
18842551e99SRandall Stewart /*
18942551e99SRandall Stewart * Here we must apply ANY locks needed by the IFN we access and also
19042551e99SRandall Stewart * make sure we lock any IFA that exists as we float through the
19142551e99SRandall Stewart * list of IFA's
19242551e99SRandall Stewart */
193b8a6e03fSGleb Smirnoff struct epoch_tracker et;
194f8829a4aSRandall Stewart struct ifnet *ifn;
195f8829a4aSRandall Stewart struct ifaddr *ifa;
19642551e99SRandall Stewart struct sctp_ifa *sctp_ifa;
19742551e99SRandall Stewart uint32_t ifa_flags;
198e6194c2eSMichael Tuexen #ifdef INET6
199e6194c2eSMichael Tuexen struct in6_ifaddr *ifa6;
200e6194c2eSMichael Tuexen #endif
201e6194c2eSMichael Tuexen
2028518270eSMichael Tuexen IFNET_RLOCK();
203b8a6e03fSGleb Smirnoff NET_EPOCH_ENTER(et);
2044f6c66ccSMatt Macy CK_STAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_link) {
205173be2b6SMichael Tuexen if (sctp_is_desired_interface_type(ifn) == 0) {
206173be2b6SMichael Tuexen /* non desired type */
207173be2b6SMichael Tuexen continue;
208173be2b6SMichael Tuexen }
209d7c5a620SMatt Macy CK_STAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) {
21042551e99SRandall Stewart if (ifa->ifa_addr == NULL) {
211f8829a4aSRandall Stewart continue;
212f8829a4aSRandall Stewart }
213e6194c2eSMichael Tuexen switch (ifa->ifa_addr->sa_family) {
214e6194c2eSMichael Tuexen #ifdef INET
215e6194c2eSMichael Tuexen case AF_INET:
216e6194c2eSMichael Tuexen if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
217f8829a4aSRandall Stewart continue;
218f8829a4aSRandall Stewart }
219e6194c2eSMichael Tuexen break;
220e6194c2eSMichael Tuexen #endif
221e6194c2eSMichael Tuexen #ifdef INET6
222e6194c2eSMichael Tuexen case AF_INET6:
22342551e99SRandall Stewart if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
224e7e65008SMichael Tuexen /* skip unspecified addresses */
225f8829a4aSRandall Stewart continue;
226f8829a4aSRandall Stewart }
227e6194c2eSMichael Tuexen break;
228e6194c2eSMichael Tuexen #endif
229e6194c2eSMichael Tuexen default:
23042551e99SRandall Stewart continue;
231f8829a4aSRandall Stewart }
232e6194c2eSMichael Tuexen switch (ifa->ifa_addr->sa_family) {
233e6194c2eSMichael Tuexen #ifdef INET
234e6194c2eSMichael Tuexen case AF_INET:
235e6194c2eSMichael Tuexen ifa_flags = 0;
236e6194c2eSMichael Tuexen break;
237e6194c2eSMichael Tuexen #endif
238e6194c2eSMichael Tuexen #ifdef INET6
239e6194c2eSMichael Tuexen case AF_INET6:
24042551e99SRandall Stewart ifa6 = (struct in6_ifaddr *)ifa;
24142551e99SRandall Stewart ifa_flags = ifa6->ia6_flags;
242e6194c2eSMichael Tuexen break;
243e6194c2eSMichael Tuexen #endif
244e6194c2eSMichael Tuexen default:
24542551e99SRandall Stewart ifa_flags = 0;
246e6194c2eSMichael Tuexen break;
24742551e99SRandall Stewart }
24842551e99SRandall Stewart sctp_ifa = sctp_add_addr_to_vrf(vrfid,
24942551e99SRandall Stewart (void *)ifn,
25042551e99SRandall Stewart ifn->if_index,
25142551e99SRandall Stewart ifn->if_type,
25242551e99SRandall Stewart ifn->if_xname,
25342551e99SRandall Stewart (void *)ifa,
25442551e99SRandall Stewart ifa->ifa_addr,
255b3f1ea41SRandall Stewart ifa_flags,
256b3f1ea41SRandall Stewart 0);
25742551e99SRandall Stewart if (sctp_ifa) {
25842551e99SRandall Stewart sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
25942551e99SRandall Stewart }
26042551e99SRandall Stewart }
26142551e99SRandall Stewart }
262b8a6e03fSGleb Smirnoff NET_EPOCH_EXIT(et);
2638518270eSMichael Tuexen IFNET_RUNLOCK();
26442551e99SRandall Stewart }
265f8829a4aSRandall Stewart
26642551e99SRandall Stewart void
sctp_init_vrf_list(int vrfid)26742551e99SRandall Stewart sctp_init_vrf_list(int vrfid)
26842551e99SRandall Stewart {
26942551e99SRandall Stewart if (vrfid > SCTP_MAX_VRF_ID)
27042551e99SRandall Stewart /* can't do that */
27142551e99SRandall Stewart return;
27242551e99SRandall Stewart
27342551e99SRandall Stewart /* Don't care about return here */
27442551e99SRandall Stewart (void)sctp_allocate_vrf(vrfid);
27542551e99SRandall Stewart
276f8829a4aSRandall Stewart /*
27742551e99SRandall Stewart * Now we need to build all the ifn's for this vrf and there
27842551e99SRandall Stewart * addresses
279f8829a4aSRandall Stewart */
28042551e99SRandall Stewart sctp_init_ifns_for_vrf(vrfid);
281f8829a4aSRandall Stewart }
28242551e99SRandall Stewart
28342551e99SRandall Stewart void
sctp_addr_change(struct ifaddr * ifa,int cmd)28442551e99SRandall Stewart sctp_addr_change(struct ifaddr *ifa, int cmd)
28542551e99SRandall Stewart {
28642551e99SRandall Stewart uint32_t ifa_flags = 0;
28742551e99SRandall Stewart
2882b1c7de4SMichael Tuexen if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
2892b1c7de4SMichael Tuexen return;
2902b1c7de4SMichael Tuexen }
29142551e99SRandall Stewart /*
29242551e99SRandall Stewart * BSD only has one VRF, if this changes we will need to hook in the
293cd0a4ff6SPedro F. Giffuni * right things here to get the id to pass to the address management
29442551e99SRandall Stewart * routine.
29542551e99SRandall Stewart */
296b3f1ea41SRandall Stewart if (SCTP_BASE_VAR(first_time) == 0) {
29742551e99SRandall Stewart /* Special test to see if my ::1 will showup with this */
298b3f1ea41SRandall Stewart SCTP_BASE_VAR(first_time) = 1;
29942551e99SRandall Stewart sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
300f8829a4aSRandall Stewart }
3010053ed28SMichael Tuexen
30242551e99SRandall Stewart if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
30342551e99SRandall Stewart /* don't know what to do with this */
30442551e99SRandall Stewart return;
305f8829a4aSRandall Stewart }
3060053ed28SMichael Tuexen
30742551e99SRandall Stewart if (ifa->ifa_addr == NULL) {
30842551e99SRandall Stewart return;
30942551e99SRandall Stewart }
310173be2b6SMichael Tuexen if (sctp_is_desired_interface_type(ifa->ifa_ifp) == 0) {
311173be2b6SMichael Tuexen /* non desired type */
312173be2b6SMichael Tuexen return;
313173be2b6SMichael Tuexen }
314e6194c2eSMichael Tuexen switch (ifa->ifa_addr->sa_family) {
315e6194c2eSMichael Tuexen #ifdef INET
316e6194c2eSMichael Tuexen case AF_INET:
317e6194c2eSMichael Tuexen if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
31842551e99SRandall Stewart return;
31942551e99SRandall Stewart }
320e6194c2eSMichael Tuexen break;
321e6194c2eSMichael Tuexen #endif
322e6194c2eSMichael Tuexen #ifdef INET6
323e6194c2eSMichael Tuexen case AF_INET6:
324b3f1ea41SRandall Stewart ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
32542551e99SRandall Stewart if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
326e7e65008SMichael Tuexen /* skip unspecified addresses */
32742551e99SRandall Stewart return;
32842551e99SRandall Stewart }
329e6194c2eSMichael Tuexen break;
330e6194c2eSMichael Tuexen #endif
331e6194c2eSMichael Tuexen default:
332e6194c2eSMichael Tuexen /* non inet/inet6 skip */
33342551e99SRandall Stewart return;
334f8829a4aSRandall Stewart }
33542551e99SRandall Stewart if (cmd == RTM_ADD) {
3363c8c191bSMichael Tuexen (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
337173be2b6SMichael Tuexen ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type, ifa->ifa_ifp->if_xname,
338d06c82f1SRandall Stewart (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
339b3f1ea41SRandall Stewart } else {
340851b7298SRandall Stewart sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
341*523913c9SMichael Tuexen (void *)ifa->ifa_ifp,
342bf11fdafSMichael Tuexen ifa->ifa_ifp->if_index);
343173be2b6SMichael Tuexen
34442551e99SRandall Stewart /*
34542551e99SRandall Stewart * We don't bump refcount here so when it completes the
34642551e99SRandall Stewart * final delete will happen.
34742551e99SRandall Stewart */
34842551e99SRandall Stewart }
349d06c82f1SRandall Stewart }
35042551e99SRandall Stewart
351b3f1ea41SRandall Stewart void
sctp_addr_change_event_handler(void * arg __unused,struct ifaddr * ifa,int cmd)352d6e23cf0SMichael Tuexen sctp_addr_change_event_handler(void *arg __unused, struct ifaddr *ifa, int cmd)
353d6e23cf0SMichael Tuexen {
354d6e23cf0SMichael Tuexen sctp_addr_change(ifa, cmd);
355d6e23cf0SMichael Tuexen }
356d6e23cf0SMichael Tuexen
357d06c82f1SRandall Stewart struct mbuf *
sctp_get_mbuf_for_msg(unsigned int space_needed,int want_header,int how,int allonebuf,int type)358d06c82f1SRandall Stewart sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
359d06c82f1SRandall Stewart int how, int allonebuf, int type)
360d06c82f1SRandall Stewart {
361d06c82f1SRandall Stewart struct mbuf *m = NULL;
36242551e99SRandall Stewart
363d06c82f1SRandall Stewart m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
364d06c82f1SRandall Stewart if (m == NULL) {
365d06c82f1SRandall Stewart /* bad, no memory */
366d06c82f1SRandall Stewart return (m);
36742551e99SRandall Stewart }
368d06c82f1SRandall Stewart if (allonebuf) {
369296d0b94SMichael Tuexen if (SCTP_BUF_SIZE(m) < space_needed) {
370d06c82f1SRandall Stewart m_freem(m);
371d06c82f1SRandall Stewart return (NULL);
372d06c82f1SRandall Stewart }
3739f2d6263SMichael Tuexen KASSERT(SCTP_BUF_NEXT(m) == NULL, ("%s: no chain allowed", __func__));
374d06c82f1SRandall Stewart }
375d06c82f1SRandall Stewart #ifdef SCTP_MBUF_LOGGING
376b3f1ea41SRandall Stewart if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
377d06c82f1SRandall Stewart sctp_log_mb(m, SCTP_MBUF_IALLOC);
378d06c82f1SRandall Stewart }
379d06c82f1SRandall Stewart #endif
380d06c82f1SRandall Stewart return (m);
381f8829a4aSRandall Stewart }
382207304d4SRandall Stewart
383207304d4SRandall Stewart #ifdef SCTP_PACKET_LOGGING
384207304d4SRandall Stewart void
sctp_packet_log(struct mbuf * m)385f9384252SMichael Tuexen sctp_packet_log(struct mbuf *m)
386207304d4SRandall Stewart {
3870696e120SRandall Stewart int *lenat, thisone;
388207304d4SRandall Stewart void *copyto;
389207304d4SRandall Stewart uint32_t *tick_tock;
390f9384252SMichael Tuexen int length;
3910696e120SRandall Stewart int total_len;
3920696e120SRandall Stewart int grabbed_lock = 0;
3930696e120SRandall Stewart int value, newval, thisend, thisbegin;
394207304d4SRandall Stewart
3950696e120SRandall Stewart /*
3960696e120SRandall Stewart * Buffer layout. -sizeof this entry (total_len) -previous end
3970696e120SRandall Stewart * (value) -ticks of log (ticks) o -ip packet o -as logged -
3980696e120SRandall Stewart * where this started (thisbegin) x <--end points here
3990696e120SRandall Stewart */
400f9384252SMichael Tuexen length = SCTP_HEADER_LEN(m);
4010696e120SRandall Stewart total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
402207304d4SRandall Stewart /* Log a packet to the buffer. */
403207304d4SRandall Stewart if (total_len > SCTP_PACKET_LOG_SIZE) {
404207304d4SRandall Stewart /* Can't log this packet I have not a buffer big enough */
405207304d4SRandall Stewart return;
406207304d4SRandall Stewart }
407b3f1ea41SRandall Stewart if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
408207304d4SRandall Stewart return;
409207304d4SRandall Stewart }
410b3f1ea41SRandall Stewart atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
4110696e120SRandall Stewart try_again:
412b3f1ea41SRandall Stewart if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
413207304d4SRandall Stewart SCTP_IP_PKTLOG_LOCK();
4140696e120SRandall Stewart grabbed_lock = 1;
4150696e120SRandall Stewart again_locked:
416b3f1ea41SRandall Stewart value = SCTP_BASE_VAR(packet_log_end);
417b3f1ea41SRandall Stewart newval = SCTP_BASE_VAR(packet_log_end) + total_len;
4180696e120SRandall Stewart if (newval >= SCTP_PACKET_LOG_SIZE) {
4190696e120SRandall Stewart /* we wrapped */
4200696e120SRandall Stewart thisbegin = 0;
4210696e120SRandall Stewart thisend = total_len;
4220696e120SRandall Stewart } else {
423b3f1ea41SRandall Stewart thisbegin = SCTP_BASE_VAR(packet_log_end);
4240696e120SRandall Stewart thisend = newval;
4250696e120SRandall Stewart }
426b3f1ea41SRandall Stewart if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
4270696e120SRandall Stewart goto again_locked;
4280696e120SRandall Stewart }
4290696e120SRandall Stewart } else {
430b3f1ea41SRandall Stewart value = SCTP_BASE_VAR(packet_log_end);
431b3f1ea41SRandall Stewart newval = SCTP_BASE_VAR(packet_log_end) + total_len;
4320696e120SRandall Stewart if (newval >= SCTP_PACKET_LOG_SIZE) {
4330696e120SRandall Stewart /* we wrapped */
4340696e120SRandall Stewart thisbegin = 0;
4350696e120SRandall Stewart thisend = total_len;
4360696e120SRandall Stewart } else {
437b3f1ea41SRandall Stewart thisbegin = SCTP_BASE_VAR(packet_log_end);
4380696e120SRandall Stewart thisend = newval;
4390696e120SRandall Stewart }
440b3f1ea41SRandall Stewart if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
4410696e120SRandall Stewart goto try_again;
4420696e120SRandall Stewart }
4430696e120SRandall Stewart }
4440696e120SRandall Stewart /* Sanity check */
4450696e120SRandall Stewart if (thisend >= SCTP_PACKET_LOG_SIZE) {
446cd3fd531SMichael Tuexen SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
4470696e120SRandall Stewart thisbegin,
4480696e120SRandall Stewart thisend,
449b3f1ea41SRandall Stewart SCTP_BASE_VAR(packet_log_writers),
4500696e120SRandall Stewart grabbed_lock,
451b3f1ea41SRandall Stewart SCTP_BASE_VAR(packet_log_end));
452b3f1ea41SRandall Stewart SCTP_BASE_VAR(packet_log_end) = 0;
4530696e120SRandall Stewart goto no_log;
454207304d4SRandall Stewart }
455b3f1ea41SRandall Stewart lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
456207304d4SRandall Stewart *lenat = total_len;
457207304d4SRandall Stewart lenat++;
4580696e120SRandall Stewart *lenat = value;
4590696e120SRandall Stewart lenat++;
460207304d4SRandall Stewart tick_tock = (uint32_t *)lenat;
461207304d4SRandall Stewart lenat++;
462207304d4SRandall Stewart *tick_tock = sctp_get_tick_count();
463207304d4SRandall Stewart copyto = (void *)lenat;
4640696e120SRandall Stewart thisone = thisend - sizeof(int);
465b3f1ea41SRandall Stewart lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
4660696e120SRandall Stewart *lenat = thisbegin;
4670696e120SRandall Stewart if (grabbed_lock) {
468207304d4SRandall Stewart SCTP_IP_PKTLOG_UNLOCK();
4690696e120SRandall Stewart grabbed_lock = 0;
4700696e120SRandall Stewart }
471207304d4SRandall Stewart m_copydata(m, 0, length, (caddr_t)copyto);
4720696e120SRandall Stewart no_log:
4730696e120SRandall Stewart if (grabbed_lock) {
4740696e120SRandall Stewart SCTP_IP_PKTLOG_UNLOCK();
4750696e120SRandall Stewart }
476b3f1ea41SRandall Stewart atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
477207304d4SRandall Stewart }
478207304d4SRandall Stewart
479207304d4SRandall Stewart int
sctp_copy_out_packet_log(uint8_t * target,int length)480207304d4SRandall Stewart sctp_copy_out_packet_log(uint8_t *target, int length)
481207304d4SRandall Stewart {
482207304d4SRandall Stewart /*
483207304d4SRandall Stewart * We wind through the packet log starting at start copying up to
484207304d4SRandall Stewart * length bytes out. We return the number of bytes copied.
485207304d4SRandall Stewart */
48690948e8cSJohn Baldwin int this_copy;
4870696e120SRandall Stewart int *lenat;
4880696e120SRandall Stewart int did_delay = 0;
489207304d4SRandall Stewart
490b3f1ea41SRandall Stewart if (length < (int)(2 * sizeof(int))) {
4910696e120SRandall Stewart /* not enough room */
492207304d4SRandall Stewart return (0);
493207304d4SRandall Stewart }
4940696e120SRandall Stewart if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
495b3f1ea41SRandall Stewart atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
4960696e120SRandall Stewart again:
497b3f1ea41SRandall Stewart if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
498207304d4SRandall Stewart /*
4990696e120SRandall Stewart * we delay here for just a moment hoping the
5000696e120SRandall Stewart * writer(s) that were present when we entered will
5010696e120SRandall Stewart * have left and we only have locking ones that will
5020696e120SRandall Stewart * contend with us for the lock. This does not
5030696e120SRandall Stewart * assure 100% access, but its good enough for a
5040696e120SRandall Stewart * logging facility like this.
505207304d4SRandall Stewart */
5060696e120SRandall Stewart did_delay = 1;
5070696e120SRandall Stewart DELAY(10);
5080696e120SRandall Stewart goto again;
509207304d4SRandall Stewart }
510207304d4SRandall Stewart }
5110696e120SRandall Stewart SCTP_IP_PKTLOG_LOCK();
5120696e120SRandall Stewart lenat = (int *)target;
513b3f1ea41SRandall Stewart *lenat = SCTP_BASE_VAR(packet_log_end);
5140696e120SRandall Stewart lenat++;
51519d8ca2eSRandall Stewart this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
516b3f1ea41SRandall Stewart memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
5170696e120SRandall Stewart if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
518b3f1ea41SRandall Stewart atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
51919d8ca2eSRandall Stewart SCTP_PKTLOG_WRITERS_NEED_LOCK);
5200696e120SRandall Stewart }
5210696e120SRandall Stewart SCTP_IP_PKTLOG_UNLOCK();
5220696e120SRandall Stewart return (this_copy + sizeof(int));
523207304d4SRandall Stewart }
524207304d4SRandall Stewart
525207304d4SRandall Stewart #endif
526