1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
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.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * a) Redistributions of source code must retain the above copyright notice,
12 *    this list of conditions and the following disclaimer.
13 *
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.
17 *
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.
21 *
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.
33 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD$");
37
38#include <netinet/sctp_os.h>
39#include <netinet/sctp_var.h>
40#include <netinet/sctp_pcb.h>
41#include <netinet/sctp_header.h>
42#include <netinet/sctputil.h>
43#include <netinet/sctp_output.h>
44#include <netinet/sctp_bsd_addr.h>
45#include <netinet/sctp_uio.h>
46#include <netinet/sctputil.h>
47#include <netinet/sctp_timer.h>
48#include <netinet/sctp_asconf.h>
49#include <netinet/sctp_sysctl.h>
50#include <netinet/sctp_indata.h>
51#include <sys/unistd.h>
52
53/* Declare all of our malloc named types */
54MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
55MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
56MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
57MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
58MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
59MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
60MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
61MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
62MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
63MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
64MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
65MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
66MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
67MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
68MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
69MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
70MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
71MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
72MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
73MALLOC_DEFINE(SCTP_M_MCORE, "sctp_mcore", "sctp mcore queue");
74
75/* Global NON-VNET structure that controls the iterator */
76struct iterator_control sctp_it_ctl;
77
78
79void
80sctp_wakeup_iterator(void)
81{
82	wakeup(&sctp_it_ctl.iterator_running);
83}
84
85static void
86sctp_iterator_thread(void *v SCTP_UNUSED)
87{
88	SCTP_IPI_ITERATOR_WQ_LOCK();
89	/* In FreeBSD this thread never terminates. */
90	for (;;) {
91		msleep(&sctp_it_ctl.iterator_running,
92		    &sctp_it_ctl.ipi_iterator_wq_mtx,
93		    0, "waiting_for_work", 0);
94		sctp_iterator_worker();
95	}
96}
97
98void
99sctp_startup_iterator(void)
100{
101	if (sctp_it_ctl.thread_proc) {
102		/* You only get one */
103		return;
104	}
105	/* Initialize global locks here, thus only once. */
106	SCTP_ITERATOR_LOCK_INIT();
107	SCTP_IPI_ITERATOR_WQ_INIT();
108	TAILQ_INIT(&sctp_it_ctl.iteratorhead);
109	kproc_create(sctp_iterator_thread,
110	    (void *)NULL,
111	    &sctp_it_ctl.thread_proc,
112	    RFPROC,
113	    SCTP_KTHREAD_PAGES,
114	    SCTP_KTRHEAD_NAME);
115}
116
117#ifdef INET6
118
119void
120sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
121{
122	struct in6_ifaddr *ifa6;
123
124	ifa6 = (struct in6_ifaddr *)ifa->ifa;
125	ifa->flags = ifa6->ia6_flags;
126	if (!MODULE_GLOBAL(ip6_use_deprecated)) {
127		if (ifa->flags &
128		    IN6_IFF_DEPRECATED) {
129			ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
130		} else {
131			ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
132		}
133	} else {
134		ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
135	}
136	if (ifa->flags &
137	    (IN6_IFF_DETACHED |
138	    IN6_IFF_ANYCAST |
139	    IN6_IFF_NOTREADY)) {
140		ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
141	} else {
142		ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
143	}
144}
145#endif				/* INET6 */
146
147
148static uint32_t
149sctp_is_desired_interface_type(struct ifnet *ifn)
150{
151	int result;
152
153	/* check the interface type to see if it's one we care about */
154	switch (ifn->if_type) {
155	case IFT_ETHER:
156	case IFT_ISO88023:
157	case IFT_ISO88024:
158	case IFT_ISO88025:
159	case IFT_ISO88026:
160	case IFT_STARLAN:
161	case IFT_P10:
162	case IFT_P80:
163	case IFT_HY:
164	case IFT_FDDI:
165	case IFT_XETHER:
166	case IFT_ISDNBASIC:
167	case IFT_ISDNPRIMARY:
168	case IFT_PTPSERIAL:
169	case IFT_OTHER:
170	case IFT_PPP:
171	case IFT_LOOP:
172	case IFT_SLIP:
173	case IFT_GIF:
174	case IFT_L2VLAN:
175	case IFT_STF:
176	case IFT_IP:
177	case IFT_IPOVERCDLC:
178	case IFT_IPOVERCLAW:
179	case IFT_PROPVIRTUAL:	/* NetGraph Virtual too */
180	case IFT_VIRTUALIPADDRESS:
181		result = 1;
182		break;
183	default:
184		result = 0;
185	}
186
187	return (result);
188}
189
190
191
192
193static void
194sctp_init_ifns_for_vrf(int vrfid)
195{
196	/*
197	 * Here we must apply ANY locks needed by the IFN we access and also
198	 * make sure we lock any IFA that exists as we float through the
199	 * list of IFA's
200	 */
201	struct ifnet *ifn;
202	struct ifaddr *ifa;
203	struct sctp_ifa *sctp_ifa;
204	uint32_t ifa_flags;
205#ifdef INET6
206	struct in6_ifaddr *ifa6;
207#endif
208
209	IFNET_RLOCK();
210	CK_STAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_link) {
211		if (sctp_is_desired_interface_type(ifn) == 0) {
212			/* non desired type */
213			continue;
214		}
215		IF_ADDR_RLOCK(ifn);
216		CK_STAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) {
217			if (ifa->ifa_addr == NULL) {
218				continue;
219			}
220			switch (ifa->ifa_addr->sa_family) {
221#ifdef INET
222			case AF_INET:
223				if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
224					continue;
225				}
226				break;
227#endif
228#ifdef INET6
229			case AF_INET6:
230				if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
231					/* skip unspecifed addresses */
232					continue;
233				}
234				break;
235#endif
236			default:
237				continue;
238			}
239			switch (ifa->ifa_addr->sa_family) {
240#ifdef INET
241			case AF_INET:
242				ifa_flags = 0;
243				break;
244#endif
245#ifdef INET6
246			case AF_INET6:
247				ifa6 = (struct in6_ifaddr *)ifa;
248				ifa_flags = ifa6->ia6_flags;
249				break;
250#endif
251			default:
252				ifa_flags = 0;
253				break;
254			}
255			sctp_ifa = sctp_add_addr_to_vrf(vrfid,
256			    (void *)ifn,
257			    ifn->if_index,
258			    ifn->if_type,
259			    ifn->if_xname,
260			    (void *)ifa,
261			    ifa->ifa_addr,
262			    ifa_flags,
263			    0);
264			if (sctp_ifa) {
265				sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
266			}
267		}
268		IF_ADDR_RUNLOCK(ifn);
269	}
270	IFNET_RUNLOCK();
271}
272
273void
274sctp_init_vrf_list(int vrfid)
275{
276	if (vrfid > SCTP_MAX_VRF_ID)
277		/* can't do that */
278		return;
279
280	/* Don't care about return here */
281	(void)sctp_allocate_vrf(vrfid);
282
283	/*
284	 * Now we need to build all the ifn's for this vrf and there
285	 * addresses
286	 */
287	sctp_init_ifns_for_vrf(vrfid);
288}
289
290void
291sctp_addr_change(struct ifaddr *ifa, int cmd)
292{
293	uint32_t ifa_flags = 0;
294
295	if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
296		return;
297	}
298	/*
299	 * BSD only has one VRF, if this changes we will need to hook in the
300	 * right things here to get the id to pass to the address management
301	 * routine.
302	 */
303	if (SCTP_BASE_VAR(first_time) == 0) {
304		/* Special test to see if my ::1 will showup with this */
305		SCTP_BASE_VAR(first_time) = 1;
306		sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
307	}
308
309	if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
310		/* don't know what to do with this */
311		return;
312	}
313
314	if (ifa->ifa_addr == NULL) {
315		return;
316	}
317	if (sctp_is_desired_interface_type(ifa->ifa_ifp) == 0) {
318		/* non desired type */
319		return;
320	}
321	switch (ifa->ifa_addr->sa_family) {
322#ifdef INET
323	case AF_INET:
324		if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
325			return;
326		}
327		break;
328#endif
329#ifdef INET6
330	case AF_INET6:
331		ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
332		if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
333			/* skip unspecifed addresses */
334			return;
335		}
336		break;
337#endif
338	default:
339		/* non inet/inet6 skip */
340		return;
341	}
342	if (cmd == RTM_ADD) {
343		(void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
344		    ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type, ifa->ifa_ifp->if_xname,
345		    (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
346	} else {
347
348		sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
349		    ifa->ifa_ifp->if_index,
350		    ifa->ifa_ifp->if_xname);
351
352		/*
353		 * We don't bump refcount here so when it completes the
354		 * final delete will happen.
355		 */
356	}
357}
358
359void
360sctp_addr_change_event_handler(void *arg __unused, struct ifaddr *ifa, int cmd)
361{
362	sctp_addr_change(ifa, cmd);
363}
364
365struct mbuf *
366sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
367    int how, int allonebuf, int type)
368{
369	struct mbuf *m = NULL;
370
371	m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
372	if (m == NULL) {
373		/* bad, no memory */
374		return (m);
375	}
376	if (allonebuf) {
377		if (SCTP_BUF_SIZE(m) < space_needed) {
378			m_freem(m);
379			return (NULL);
380		}
381		KASSERT(SCTP_BUF_NEXT(m) == NULL, ("%s: no chain allowed", __func__));
382	}
383#ifdef SCTP_MBUF_LOGGING
384	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
385		sctp_log_mb(m, SCTP_MBUF_IALLOC);
386	}
387#endif
388	return (m);
389}
390
391
392#ifdef SCTP_PACKET_LOGGING
393void
394sctp_packet_log(struct mbuf *m)
395{
396	int *lenat, thisone;
397	void *copyto;
398	uint32_t *tick_tock;
399	int length;
400	int total_len;
401	int grabbed_lock = 0;
402	int value, newval, thisend, thisbegin;
403
404	/*
405	 * Buffer layout. -sizeof this entry (total_len) -previous end
406	 * (value) -ticks of log      (ticks) o -ip packet o -as logged -
407	 * where this started (thisbegin) x <--end points here
408	 */
409	length = SCTP_HEADER_LEN(m);
410	total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
411	/* Log a packet to the buffer. */
412	if (total_len > SCTP_PACKET_LOG_SIZE) {
413		/* Can't log this packet I have not a buffer big enough */
414		return;
415	}
416	if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
417		return;
418	}
419	atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
420try_again:
421	if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
422		SCTP_IP_PKTLOG_LOCK();
423		grabbed_lock = 1;
424again_locked:
425		value = SCTP_BASE_VAR(packet_log_end);
426		newval = SCTP_BASE_VAR(packet_log_end) + total_len;
427		if (newval >= SCTP_PACKET_LOG_SIZE) {
428			/* we wrapped */
429			thisbegin = 0;
430			thisend = total_len;
431		} else {
432			thisbegin = SCTP_BASE_VAR(packet_log_end);
433			thisend = newval;
434		}
435		if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
436			goto again_locked;
437		}
438	} else {
439		value = SCTP_BASE_VAR(packet_log_end);
440		newval = SCTP_BASE_VAR(packet_log_end) + total_len;
441		if (newval >= SCTP_PACKET_LOG_SIZE) {
442			/* we wrapped */
443			thisbegin = 0;
444			thisend = total_len;
445		} else {
446			thisbegin = SCTP_BASE_VAR(packet_log_end);
447			thisend = newval;
448		}
449		if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
450			goto try_again;
451		}
452	}
453	/* Sanity check */
454	if (thisend >= SCTP_PACKET_LOG_SIZE) {
455		SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
456		    thisbegin,
457		    thisend,
458		    SCTP_BASE_VAR(packet_log_writers),
459		    grabbed_lock,
460		    SCTP_BASE_VAR(packet_log_end));
461		SCTP_BASE_VAR(packet_log_end) = 0;
462		goto no_log;
463
464	}
465	lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
466	*lenat = total_len;
467	lenat++;
468	*lenat = value;
469	lenat++;
470	tick_tock = (uint32_t *)lenat;
471	lenat++;
472	*tick_tock = sctp_get_tick_count();
473	copyto = (void *)lenat;
474	thisone = thisend - sizeof(int);
475	lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
476	*lenat = thisbegin;
477	if (grabbed_lock) {
478		SCTP_IP_PKTLOG_UNLOCK();
479		grabbed_lock = 0;
480	}
481	m_copydata(m, 0, length, (caddr_t)copyto);
482no_log:
483	if (grabbed_lock) {
484		SCTP_IP_PKTLOG_UNLOCK();
485	}
486	atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
487}
488
489
490int
491sctp_copy_out_packet_log(uint8_t *target, int length)
492{
493	/*
494	 * We wind through the packet log starting at start copying up to
495	 * length bytes out. We return the number of bytes copied.
496	 */
497	int tocopy, this_copy;
498	int *lenat;
499	int did_delay = 0;
500
501	tocopy = length;
502	if (length < (int)(2 * sizeof(int))) {
503		/* not enough room */
504		return (0);
505	}
506	if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
507		atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
508again:
509		if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
510			/*
511			 * we delay here for just a moment hoping the
512			 * writer(s) that were present when we entered will
513			 * have left and we only have locking ones that will
514			 * contend with us for the lock. This does not
515			 * assure 100% access, but its good enough for a
516			 * logging facility like this.
517			 */
518			did_delay = 1;
519			DELAY(10);
520			goto again;
521		}
522	}
523	SCTP_IP_PKTLOG_LOCK();
524	lenat = (int *)target;
525	*lenat = SCTP_BASE_VAR(packet_log_end);
526	lenat++;
527	this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
528	memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
529	if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
530		atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
531		    SCTP_PKTLOG_WRITERS_NEED_LOCK);
532	}
533	SCTP_IP_PKTLOG_UNLOCK();
534	return (this_copy + sizeof(int));
535}
536
537#endif
538