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