• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef __FreeBSD__
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: head/sys/netinet/sctp_bsd_addr.c 358080 2020-02-18 19:41:55Z tuexen $");
38 #endif
39 
40 #include <netinet/sctp_os.h>
41 #include <netinet/sctp_var.h>
42 #include <netinet/sctp_pcb.h>
43 #include <netinet/sctp_header.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_output.h>
46 #include <netinet/sctp_bsd_addr.h>
47 #include <netinet/sctp_uio.h>
48 #include <netinet/sctputil.h>
49 #include <netinet/sctp_timer.h>
50 #include <netinet/sctp_asconf.h>
51 #include <netinet/sctp_sysctl.h>
52 #include <netinet/sctp_indata.h>
53 #if defined(__FreeBSD__)
54 #include <sys/unistd.h>
55 #endif
56 
57 /* Declare all of our malloc named types */
58 #ifndef __Panda__
59 MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
60 MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
61 MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
62 MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
63 MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
64 MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
65 MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
66 MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
67 MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
68 MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
69 MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
70 MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
71 MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
72 MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
73 MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
74 MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
75 MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
76 MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
77 MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
78 MALLOC_DEFINE(SCTP_M_MCORE, "sctp_mcore", "sctp mcore queue");
79 #endif
80 
81 /* Global NON-VNET structure that controls the iterator */
82 struct iterator_control sctp_it_ctl;
83 
84 #if !defined(__FreeBSD__)
85 static void
sctp_cleanup_itqueue(void)86 sctp_cleanup_itqueue(void)
87 {
88 	struct sctp_iterator *it, *nit;
89 
90 	TAILQ_FOREACH_SAFE(it, &sctp_it_ctl.iteratorhead, sctp_nxt_itr, nit) {
91 		if (it->function_atend != NULL) {
92 			(*it->function_atend) (it->pointer, it->val);
93 		}
94 		TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
95 		SCTP_FREE(it, SCTP_M_ITER);
96 	}
97 }
98 #endif
99 #if defined(__Userspace__)
100 /*__Userspace__ TODO if we use thread based iterator
101  * then the implementation of wakeup will need to change.
102  * Currently we are using timeo_cond for ident so_timeo
103  * but that is not sufficient if we need to use another ident
104  * like wakeup(&sctppcbinfo.iterator_running);
105  */
106 #endif
107 
108 void
sctp_wakeup_iterator(void)109 sctp_wakeup_iterator(void)
110 {
111 #if defined(SCTP_PROCESS_LEVEL_LOCKS)
112 #if defined(__Userspace_os_Windows)
113 	WakeAllConditionVariable(&sctp_it_ctl.iterator_wakeup);
114 #else
115 	pthread_cond_broadcast(&sctp_it_ctl.iterator_wakeup);
116 #endif
117 #else
118 	wakeup(&sctp_it_ctl.iterator_running);
119 #endif
120 }
121 
122 #if defined(__Userspace__)
123 static void *
124 #else
125 static void
126 #endif
sctp_iterator_thread(void * v SCTP_UNUSED)127 sctp_iterator_thread(void *v SCTP_UNUSED)
128 {
129 #if defined(__Userspace__)
130 	sctp_userspace_set_threadname("SCTP iterator");
131 #endif
132 	SCTP_IPI_ITERATOR_WQ_LOCK();
133 	/* In FreeBSD this thread never terminates. */
134 #if defined(__FreeBSD__)
135 	for (;;) {
136 #else
137 	while ((sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) == 0) {
138 #endif
139 #if !defined(__Userspace__)
140 		msleep(&sctp_it_ctl.iterator_running,
141 #if defined(__FreeBSD__)
142 		       &sctp_it_ctl.ipi_iterator_wq_mtx,
143 #elif defined(__APPLE__) || defined(__Userspace_os_Darwin)
144 		       sctp_it_ctl.ipi_iterator_wq_mtx,
145 #endif
146 		       0, "waiting_for_work", 0);
147 #else
148 #if defined(__Userspace_os_Windows)
149 		SleepConditionVariableCS(&sctp_it_ctl.iterator_wakeup, &sctp_it_ctl.ipi_iterator_wq_mtx, INFINITE);
150 #else
151 		pthread_cond_wait(&sctp_it_ctl.iterator_wakeup, &sctp_it_ctl.ipi_iterator_wq_mtx);
152 #endif
153 #endif
154 #if !defined(__FreeBSD__)
155 		if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) {
156 			break;
157 		}
158 #endif
159 		sctp_iterator_worker();
160 	}
161 #if !defined(__FreeBSD__)
162 	/* Now this thread needs to be terminated */
163 	sctp_cleanup_itqueue();
164 	sctp_it_ctl.iterator_flags |= SCTP_ITERATOR_EXITED;
165 	SCTP_IPI_ITERATOR_WQ_UNLOCK();
166 #if defined(__Userspace__)
167 	sctp_wakeup_iterator();
168 	return (NULL);
169 #else
170 	wakeup(&sctp_it_ctl.iterator_flags);
171 	thread_terminate(current_thread());
172 #ifdef INVARIANTS
173 	panic("Hmm. thread_terminate() continues...");
174 #endif
175 #endif
176 #endif
177 }
178 
179 void
180 sctp_startup_iterator(void)
181 {
182 	if (sctp_it_ctl.thread_proc) {
183 		/* You only get one */
184 		return;
185 	}
186 	/* Initialize global locks here, thus only once. */
187 	SCTP_ITERATOR_LOCK_INIT();
188 	SCTP_IPI_ITERATOR_WQ_INIT();
189 	TAILQ_INIT(&sctp_it_ctl.iteratorhead);
190 #if defined(__FreeBSD__)
191 #if __FreeBSD_version <= 701000
192 	kthread_create(sctp_iterator_thread,
193 #else
194 	kproc_create(sctp_iterator_thread,
195 #endif
196 	             (void *)NULL,
197 	             &sctp_it_ctl.thread_proc,
198 	             RFPROC,
199 	             SCTP_KTHREAD_PAGES,
200 	             SCTP_KTRHEAD_NAME);
201 #elif defined(__APPLE__)
202 	kernel_thread_start((thread_continue_t)sctp_iterator_thread, NULL, &sctp_it_ctl.thread_proc);
203 #elif defined(__Userspace__)
204 	if (sctp_userspace_thread_create(&sctp_it_ctl.thread_proc, &sctp_iterator_thread)) {
205 		SCTP_PRINTF("ERROR: Creating sctp_iterator_thread failed.\n");
206 	} else {
207 		SCTP_BASE_VAR(iterator_thread_started) = 1;
208 	}
209 #endif
210 }
211 
212 #ifdef INET6
213 
214 #if defined(__Userspace__)
215 /* __Userspace__ TODO. struct in6_ifaddr is defined in sys/netinet6/in6_var.h
216    ip6_use_deprecated is defined as  int ip6_use_deprecated = 1; in /src/sys/netinet6/in6_proto.c
217  */
218 void
219 sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
220 {
221     return; /* stub */
222 }
223 #else
224 void
225 sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
226 {
227 	struct in6_ifaddr *ifa6;
228 
229 	ifa6 = (struct in6_ifaddr *)ifa->ifa;
230 	ifa->flags = ifa6->ia6_flags;
231 	if (!MODULE_GLOBAL(ip6_use_deprecated)) {
232 		if (ifa->flags &
233 		    IN6_IFF_DEPRECATED) {
234 			ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
235 		} else {
236 			ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
237 		}
238 	} else {
239 		ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
240 	}
241 	if (ifa->flags &
242 	    (IN6_IFF_DETACHED |
243 	     IN6_IFF_ANYCAST |
244 	     IN6_IFF_NOTREADY)) {
245 		ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
246 	} else {
247 		ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
248 	}
249 }
250 #endif /* __Userspace__ */
251 #endif /* INET6 */
252 
253 
254 #if !defined(__Userspace__)
255 static uint32_t
256 sctp_is_desired_interface_type(struct ifnet *ifn)
257 {
258 	int result;
259 
260 	/* check the interface type to see if it's one we care about */
261 #if defined(__APPLE__)
262 	switch(ifnet_type(ifn)) {
263 #else
264 	switch (ifn->if_type) {
265 #endif
266 	case IFT_ETHER:
267 	case IFT_ISO88023:
268 	case IFT_ISO88024:
269 	case IFT_ISO88025:
270 	case IFT_ISO88026:
271 	case IFT_STARLAN:
272 	case IFT_P10:
273 	case IFT_P80:
274 	case IFT_HY:
275 	case IFT_FDDI:
276 	case IFT_XETHER:
277 	case IFT_ISDNBASIC:
278 	case IFT_ISDNPRIMARY:
279 	case IFT_PTPSERIAL:
280 	case IFT_OTHER:
281 	case IFT_PPP:
282 	case IFT_LOOP:
283 	case IFT_SLIP:
284 	case IFT_GIF:
285 	case IFT_L2VLAN:
286 	case IFT_STF:
287 #if !defined(__APPLE__)
288 	case IFT_IP:
289 	case IFT_IPOVERCDLC:
290 	case IFT_IPOVERCLAW:
291 	case IFT_PROPVIRTUAL: /* NetGraph Virtual too */
292 	case IFT_VIRTUALIPADDRESS:
293 #endif
294 		result = 1;
295 		break;
296 	default:
297 		result = 0;
298 	}
299 
300 	return (result);
301 }
302 #endif
303 
304 #if defined(__APPLE__)
305 int
306 sctp_is_vmware_interface(struct ifnet *ifn)
307 {
308 	return (strncmp(ifnet_name(ifn), "vmnet", 5) == 0);
309 }
310 #endif
311 
312 #if defined(__Userspace_os_Windows)
313 #ifdef MALLOC
314 #undef MALLOC
315 #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
316 #endif
317 #ifdef FREE
318 #undef FREE
319 #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
320 #endif
321 static void
322 sctp_init_ifns_for_vrf(int vrfid)
323 {
324 #if defined(INET) || defined(INET6)
325 	struct sctp_ifa *sctp_ifa;
326 	DWORD Err, AdapterAddrsSize;
327 	PIP_ADAPTER_ADDRESSES pAdapterAddrs, pAdapt;
328 	PIP_ADAPTER_UNICAST_ADDRESS pUnicast;
329 #endif
330 
331 #ifdef INET
332 	AdapterAddrsSize = 0;
333 
334 	if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
335 		if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
336 			SCTP_PRINTF("GetAdaptersV4Addresses() sizing failed with error code %d\n", Err);
337 			SCTP_PRINTF("err = %d; AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
338 			return;
339 		}
340 	}
341 
342 	/* Allocate memory from sizing information */
343 	if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
344 		SCTP_PRINTF("Memory allocation error!\n");
345 		return;
346 	}
347 	/* Get actual adapter information */
348 	if ((Err = GetAdaptersAddresses(AF_INET, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
349 		SCTP_PRINTF("GetAdaptersV4Addresses() failed with error code %d\n", Err);
350 		FREE(pAdapterAddrs);
351 		return;
352 	}
353 	/* Enumerate through each returned adapter and save its information */
354 	for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
355 		if (pAdapt->IfType == IF_TYPE_IEEE80211 || pAdapt->IfType == IF_TYPE_ETHERNET_CSMACD) {
356 			for (pUnicast = pAdapt->FirstUnicastAddress; pUnicast; pUnicast = pUnicast->Next) {
357 				if (IN4_ISLINKLOCAL_ADDRESS(&(((struct sockaddr_in *)(pUnicast->Address.lpSockaddr))->sin_addr))) {
358 					continue;
359 				}
360 				sctp_ifa = sctp_add_addr_to_vrf(0,
361 				                                NULL,
362 				                                pAdapt->IfIndex,
363 				                                (pAdapt->IfType == IF_TYPE_IEEE80211)?MIB_IF_TYPE_ETHERNET:pAdapt->IfType,
364 				                                pAdapt->AdapterName,
365 				                                NULL,
366 				                                pUnicast->Address.lpSockaddr,
367 				                                pAdapt->Flags,
368 				                                0);
369 				if (sctp_ifa) {
370 					sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
371 				}
372 			}
373 		}
374 	}
375 	FREE(pAdapterAddrs);
376 #endif
377 #ifdef INET6
378 	AdapterAddrsSize = 0;
379 
380 	if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, NULL, &AdapterAddrsSize)) != 0) {
381 		if ((Err != ERROR_BUFFER_OVERFLOW) && (Err != ERROR_INSUFFICIENT_BUFFER)) {
382 			SCTP_PRINTF("GetAdaptersV6Addresses() sizing failed with error code %d\n", Err);
383 			SCTP_PRINTF("err = %d; AdapterAddrsSize = %d\n", Err, AdapterAddrsSize);
384 			return;
385 		}
386 	}
387 	/* Allocate memory from sizing information */
388 	if ((pAdapterAddrs = (PIP_ADAPTER_ADDRESSES) GlobalAlloc(GPTR, AdapterAddrsSize)) == NULL) {
389 		SCTP_PRINTF("Memory allocation error!\n");
390 		return;
391 	}
392 	/* Get actual adapter information */
393 	if ((Err = GetAdaptersAddresses(AF_INET6, 0, NULL, pAdapterAddrs, &AdapterAddrsSize)) != ERROR_SUCCESS) {
394 		SCTP_PRINTF("GetAdaptersV6Addresses() failed with error code %d\n", Err);
395 		FREE(pAdapterAddrs);
396 		return;
397 	}
398 	/* Enumerate through each returned adapter and save its information */
399 	for (pAdapt = pAdapterAddrs; pAdapt; pAdapt = pAdapt->Next) {
400 		if (pAdapt->IfType == IF_TYPE_IEEE80211 || pAdapt->IfType == IF_TYPE_ETHERNET_CSMACD) {
401 			for (pUnicast = pAdapt->FirstUnicastAddress; pUnicast; pUnicast = pUnicast->Next) {
402 				sctp_ifa = sctp_add_addr_to_vrf(0,
403 				                                NULL,
404 				                                pAdapt->Ipv6IfIndex,
405 				                                (pAdapt->IfType == IF_TYPE_IEEE80211)?MIB_IF_TYPE_ETHERNET:pAdapt->IfType,
406 				                                pAdapt->AdapterName,
407 				                                NULL,
408 				                                pUnicast->Address.lpSockaddr,
409 				                                pAdapt->Flags,
410 				                                0);
411 				if (sctp_ifa) {
412 					sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
413 				}
414 			}
415 		}
416 	}
417 	FREE(pAdapterAddrs);
418 #endif
419 }
420 #elif defined(__Userspace__)
421 static void
422 sctp_init_ifns_for_vrf(int vrfid)
423 {
424 #if defined(INET) || defined(INET6)
425 	int rc;
426 	struct ifaddrs *ifa, *ifas;
427 	struct sctp_ifa *sctp_ifa;
428 	uint32_t ifa_flags;
429 
430 	rc = getifaddrs(&ifas);
431 	if (rc != 0) {
432 		return;
433 	}
434 	for (ifa = ifas; ifa; ifa = ifa->ifa_next) {
435 		if (ifa->ifa_addr == NULL) {
436 			continue;
437 		}
438 #if !defined(INET)
439 		if (ifa->ifa_addr->sa_family != AF_INET6) {
440 			/* non inet6 skip */
441 			continue;
442 		}
443 #elif !defined(INET6)
444 		if (ifa->ifa_addr->sa_family != AF_INET) {
445 			/* non inet skip */
446 			continue;
447 		}
448 #else
449 		if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
450 			/* non inet/inet6 skip */
451 			continue;
452 		}
453 #endif
454 #if defined(INET6)
455 		if ((ifa->ifa_addr->sa_family == AF_INET6) &&
456 		    IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
457 			/* skip unspecifed addresses */
458 			continue;
459 		}
460 #endif
461 #if defined(INET)
462 		if (ifa->ifa_addr->sa_family == AF_INET &&
463 		    ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
464 			continue;
465 		}
466 #endif
467 		ifa_flags = 0;
468 		sctp_ifa = sctp_add_addr_to_vrf(vrfid,
469 		                                NULL,
470 		                                if_nametoindex(ifa->ifa_name),
471 		                                0,
472 		                                ifa->ifa_name,
473 		                                NULL,
474 		                                ifa->ifa_addr,
475 		                                ifa_flags,
476 		                                0);
477 		if (sctp_ifa) {
478 			sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
479 		}
480 	}
481 	freeifaddrs(ifas);
482 #endif
483 }
484 #endif
485 
486 #if defined(__APPLE__)
487 static void
488 sctp_init_ifns_for_vrf(int vrfid)
489 {
490 	/* Here we must apply ANY locks needed by the
491 	 * IFN we access and also make sure we lock
492 	 * any IFA that exists as we float through the
493 	 * list of IFA's
494 	 */
495 	struct ifnet **ifnetlist;
496 	uint32_t i, j, count;
497 	char name[SCTP_IFNAMSIZ];
498 	struct ifnet *ifn;
499 	struct ifaddr **ifaddrlist;
500 	struct ifaddr *ifa;
501 	struct in6_ifaddr *ifa6;
502 	struct sctp_ifa *sctp_ifa;
503 	uint32_t ifa_flags;
504 
505 	if (ifnet_list_get(IFNET_FAMILY_ANY, &ifnetlist, &count) != 0) {
506 		return;
507 	}
508 	for (i = 0; i < count; i++) {
509 		ifn = ifnetlist[i];
510 		if (SCTP_BASE_SYSCTL(sctp_ignore_vmware_interfaces) && sctp_is_vmware_interface(ifn)) {
511 			continue;
512 		}
513 		if (sctp_is_desired_interface_type(ifn) == 0) {
514 			/* non desired type */
515 			continue;
516 		}
517 		if (ifnet_get_address_list(ifn, &ifaddrlist) != 0) {
518 			continue;
519 		}
520 		for (j = 0; ifaddrlist[j] != NULL; j++) {
521 			ifa = ifaddrlist[j];
522 			if (ifa->ifa_addr == NULL) {
523 				continue;
524 			}
525 			if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
526 				/* non inet/inet6 skip */
527 				continue;
528 			}
529 			if (ifa->ifa_addr->sa_family == AF_INET6) {
530 				if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
531 					/* skip unspecifed addresses */
532 					continue;
533 				}
534 			} else {
535 				if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == INADDR_ANY) {
536 					continue;
537 				}
538 			}
539 			if (ifa->ifa_addr->sa_family == AF_INET6) {
540 				ifa6 = (struct in6_ifaddr *)ifa;
541 				ifa_flags = ifa6->ia6_flags;
542 			} else {
543 				ifa_flags = 0;
544 			}
545 			SCTP_SNPRINTF(name, SCTP_IFNAMSIZ, "%s%d", ifnet_name(ifn), ifnet_unit(ifn));
546 			sctp_ifa = sctp_add_addr_to_vrf(vrfid,
547 			                                (void *)ifn, /* XXX */
548 			                                ifnet_index(ifn),
549 			                                ifnet_type(ifn),
550 			                                name,
551 			                                (void *)ifa, /* XXX */
552 			                                ifa->ifa_addr,
553 			                                ifa_flags,
554 			                                0);
555 			if (sctp_ifa) {
556 				sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
557 			}
558 		}
559 		ifnet_free_address_list(ifaddrlist);
560 	}
561 	ifnet_list_free(ifnetlist);
562 }
563 #endif
564 
565 #if defined(__FreeBSD__)
566 static void
567 sctp_init_ifns_for_vrf(int vrfid)
568 {
569 	/* Here we must apply ANY locks needed by the
570 	 * IFN we access and also make sure we lock
571 	 * any IFA that exists as we float through the
572 	 * list of IFA's
573 	 */
574 	struct epoch_tracker et;
575 	struct ifnet *ifn;
576 	struct ifaddr *ifa;
577 	struct sctp_ifa *sctp_ifa;
578 	uint32_t ifa_flags;
579 #ifdef INET6
580 	struct in6_ifaddr *ifa6;
581 #endif
582 
583 	IFNET_RLOCK();
584 	NET_EPOCH_ENTER(et);
585 	CK_STAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_link) {
586 		if (sctp_is_desired_interface_type(ifn) == 0) {
587 			/* non desired type */
588 			continue;
589 		}
590 		CK_STAILQ_FOREACH(ifa, &ifn->if_addrhead, ifa_link) {
591 			if (ifa->ifa_addr == NULL) {
592 				continue;
593 			}
594 			switch (ifa->ifa_addr->sa_family) {
595 #ifdef INET
596 			case AF_INET:
597 				if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
598 					continue;
599 				}
600 				break;
601 #endif
602 #ifdef INET6
603 			case AF_INET6:
604 				if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
605 					/* skip unspecifed addresses */
606 					continue;
607 				}
608 				break;
609 #endif
610 			default:
611 				continue;
612 			}
613 			switch (ifa->ifa_addr->sa_family) {
614 #ifdef INET
615 			case AF_INET:
616 				ifa_flags = 0;
617 				break;
618 #endif
619 #ifdef INET6
620 			case AF_INET6:
621 				ifa6 = (struct in6_ifaddr *)ifa;
622 				ifa_flags = ifa6->ia6_flags;
623 				break;
624 #endif
625 			default:
626 				ifa_flags = 0;
627 				break;
628 			}
629 			sctp_ifa = sctp_add_addr_to_vrf(vrfid,
630 			                                (void *)ifn,
631 			                                ifn->if_index,
632 			                                ifn->if_type,
633 			                                ifn->if_xname,
634 			                                (void *)ifa,
635 			                                ifa->ifa_addr,
636 			                                ifa_flags,
637 			                                0);
638 			if (sctp_ifa) {
639 				sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
640 			}
641 		}
642 	}
643 	NET_EPOCH_EXIT(et);
644 	IFNET_RUNLOCK();
645 }
646 #endif
647 
648 void
649 sctp_init_vrf_list(int vrfid)
650 {
651 	if (vrfid > SCTP_MAX_VRF_ID)
652 		/* can't do that */
653 		return;
654 
655 	/* Don't care about return here */
656 	(void)sctp_allocate_vrf(vrfid);
657 
658 	/* Now we need to build all the ifn's
659 	 * for this vrf and there addresses
660 	 */
661 	sctp_init_ifns_for_vrf(vrfid);
662 }
663 
664 void
665 sctp_addr_change(struct ifaddr *ifa, int cmd)
666 {
667 #if defined(__Userspace__)
668         return;
669 #else
670 	uint32_t ifa_flags = 0;
671 
672 	if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
673 		return;
674 	}
675 	/* BSD only has one VRF, if this changes
676 	 * we will need to hook in the right
677 	 * things here to get the id to pass to
678 	 * the address management routine.
679 	 */
680 	if (SCTP_BASE_VAR(first_time) == 0) {
681 		/* Special test to see if my ::1 will showup with this */
682 		SCTP_BASE_VAR(first_time) = 1;
683 		sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
684 	}
685 
686 	if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
687 		/* don't know what to do with this */
688 		return;
689 	}
690 
691 	if (ifa->ifa_addr == NULL) {
692 		return;
693 	}
694 	if (sctp_is_desired_interface_type(ifa->ifa_ifp) == 0) {
695 		/* non desired type */
696 		return;
697 	}
698 	switch (ifa->ifa_addr->sa_family) {
699 #ifdef INET
700 	case AF_INET:
701 		if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
702 			return;
703 		}
704 		break;
705 #endif
706 #ifdef INET6
707 	case AF_INET6:
708 		ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
709 		if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
710 			/* skip unspecifed addresses */
711 			return;
712 		}
713 		break;
714 #endif
715 	default:
716 		/* non inet/inet6 skip */
717 		return;
718 	}
719 	if (cmd == RTM_ADD) {
720 		(void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
721 #if defined(__APPLE__)
722 		                           ifnet_index(ifa->ifa_ifp), ifnet_type(ifa->ifa_ifp), ifnet_name(ifa->ifa_ifp),
723 #else
724 		                           ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type, ifa->ifa_ifp->if_xname,
725 #endif
726 		                           (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
727 	} else {
728 
729 		sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
730 #if defined(__APPLE__)
731 		                       ifnet_index(ifa->ifa_ifp),
732 		                       ifnet_name(ifa->ifa_ifp));
733 #else
734 		                       ifa->ifa_ifp->if_index,
735 		                       ifa->ifa_ifp->if_xname);
736 #endif
737 
738 		/* We don't bump refcount here so when it completes
739 		 * the final delete will happen.
740 		 */
741 	}
742 #endif
743 }
744 
745 #if defined(__FreeBSD__)
746 void
747 sctp_addr_change_event_handler(void *arg __unused, struct ifaddr *ifa, int cmd) {
748 	sctp_addr_change(ifa, cmd);
749 }
750 #endif
751 #if defined(__APPLE__)
752 void
753 sctp_add_or_del_interfaces(int (*pred)(struct ifnet *), int add)
754 {
755 	struct ifnet **ifnetlist;
756 	struct ifaddr **ifaddrlist;
757 	uint32_t i, j, count;
758 
759 	if (ifnet_list_get(IFNET_FAMILY_ANY, &ifnetlist, &count) != 0) {
760 		return;
761 	}
762 	for (i = 0; i < count; i++) {
763 		if (!(*pred)(ifnetlist[i])) {
764 			continue;
765 		}
766 		if (ifnet_get_address_list(ifnetlist[i], &ifaddrlist) != 0) {
767 			continue;
768 		}
769 		for (j = 0; ifaddrlist[j] != NULL; j++) {
770 			sctp_addr_change(ifaddrlist[j], add ? RTM_ADD : RTM_DELETE);
771 		}
772 		ifnet_free_address_list(ifaddrlist);
773 	}
774 	ifnet_list_free(ifnetlist);
775 	return;
776 }
777 #endif
778 
779 struct mbuf *
780 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
781 		      int how, int allonebuf, int type)
782 {
783 	struct mbuf *m = NULL;
784 #if defined(__FreeBSD__) && __FreeBSD_version > 1100052 || defined(__Userspace__)
785 #if defined(__Userspace__)
786 	m =  m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0, allonebuf);
787 #else
788 	m =  m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
789 #endif
790 	if (m == NULL) {
791 		/* bad, no memory */
792 		return (m);
793 	}
794 #if !defined(__Userspace__)
795 	if (allonebuf) {
796 		if (SCTP_BUF_SIZE(m) < space_needed) {
797 			m_freem(m);
798 			return (NULL);
799 		}
800 		KASSERT(SCTP_BUF_NEXT(m) == NULL, ("%s: no chain allowed", __FUNCTION__));
801 	}
802 #endif
803 #ifdef SCTP_MBUF_LOGGING
804 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
805 		sctp_log_mb(m, SCTP_MBUF_IALLOC);
806 	}
807 #endif
808 #else
809 	int mbuf_threshold;
810 	unsigned int size;
811 
812 	if (want_header) {
813 		MGETHDR(m, how, type);
814 		size = MHLEN;
815 	} else {
816 		MGET(m, how, type);
817 		size = MLEN;
818 	}
819 	if (m == NULL) {
820 		return (NULL);
821 	}
822 	if (allonebuf == 0) {
823 		mbuf_threshold = SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count);
824 	} else {
825 		mbuf_threshold = 1;
826 	}
827 
828 	if (space_needed > (unsigned int)(((mbuf_threshold - 1) * MLEN) + MHLEN)) {
829 		MCLGET(m, how);
830 		if (m == NULL) {
831 			return (NULL);
832 		}
833 		if (SCTP_BUF_IS_EXTENDED(m) == 0) {
834 			sctp_m_freem(m);
835 			return (NULL);
836 		}
837 		size = SCTP_BUF_EXTEND_SIZE(m);
838 	}
839 	if (allonebuf != 0 && size < space_needed) {
840 		m_freem(m);
841 		return (NULL);
842 	}
843 	SCTP_BUF_LEN(m) = 0;
844 	SCTP_BUF_NEXT(m) = SCTP_BUF_NEXT_PKT(m) = NULL;
845 #ifdef SCTP_MBUF_LOGGING
846 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
847 		sctp_log_mb(m, SCTP_MBUF_IALLOC);
848 	}
849 #endif
850 #endif
851 	return (m);
852 }
853 
854 
855 #ifdef SCTP_PACKET_LOGGING
856 void
857 sctp_packet_log(struct mbuf *m)
858 {
859 	int *lenat, thisone;
860 	void *copyto;
861 	uint32_t *tick_tock;
862 	int length;
863 	int total_len;
864 	int grabbed_lock = 0;
865 	int value, newval, thisend, thisbegin;
866 	/*
867 	 * Buffer layout.
868 	 * -sizeof this entry (total_len)
869 	 * -previous end      (value)
870 	 * -ticks of log      (ticks)
871 	 * o -ip packet
872 	 * o -as logged
873 	 * - where this started (thisbegin)
874 	 * x <--end points here
875 	 */
876 	length = SCTP_HEADER_LEN(m);
877 	total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
878 	/* Log a packet to the buffer. */
879 	if (total_len> SCTP_PACKET_LOG_SIZE) {
880 		/* Can't log this packet I have not a buffer big enough */
881 		return;
882 	}
883 	if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
884 		return;
885 	}
886 	atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
887  try_again:
888 	if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
889 		SCTP_IP_PKTLOG_LOCK();
890 		grabbed_lock = 1;
891 	again_locked:
892 		value = SCTP_BASE_VAR(packet_log_end);
893 		newval = SCTP_BASE_VAR(packet_log_end) + total_len;
894 		if (newval >= SCTP_PACKET_LOG_SIZE) {
895 			/* we wrapped */
896 			thisbegin = 0;
897 			thisend = total_len;
898 		} else {
899 			thisbegin = SCTP_BASE_VAR(packet_log_end);
900 			thisend = newval;
901 		}
902 		if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
903 			goto again_locked;
904 		}
905 	} else {
906 		value = SCTP_BASE_VAR(packet_log_end);
907 		newval = SCTP_BASE_VAR(packet_log_end) + total_len;
908 		if (newval >= SCTP_PACKET_LOG_SIZE) {
909 			/* we wrapped */
910 			thisbegin = 0;
911 			thisend = total_len;
912 		} else {
913 			thisbegin = SCTP_BASE_VAR(packet_log_end);
914 			thisend = newval;
915 		}
916 		if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
917 			goto try_again;
918 		}
919 	}
920 	/* Sanity check */
921 	if (thisend >= SCTP_PACKET_LOG_SIZE) {
922 		SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
923 		            thisbegin,
924 		            thisend,
925 		            SCTP_BASE_VAR(packet_log_writers),
926 		            grabbed_lock,
927 		            SCTP_BASE_VAR(packet_log_end));
928 		SCTP_BASE_VAR(packet_log_end) = 0;
929 		goto no_log;
930 
931 	}
932 	lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
933 	*lenat = total_len;
934 	lenat++;
935 	*lenat = value;
936 	lenat++;
937 	tick_tock = (uint32_t *)lenat;
938 	lenat++;
939 	*tick_tock = sctp_get_tick_count();
940 	copyto = (void *)lenat;
941 	thisone = thisend - sizeof(int);
942 	lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
943 	*lenat = thisbegin;
944 	if (grabbed_lock) {
945 		SCTP_IP_PKTLOG_UNLOCK();
946 		grabbed_lock = 0;
947 	}
948 	m_copydata(m, 0, length, (caddr_t)copyto);
949  no_log:
950 	if (grabbed_lock) {
951 		SCTP_IP_PKTLOG_UNLOCK();
952 	}
953 	atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
954 }
955 
956 
957 int
958 sctp_copy_out_packet_log(uint8_t *target, int length)
959 {
960 	/* We wind through the packet log starting at
961 	 * start copying up to length bytes out.
962 	 * We return the number of bytes copied.
963 	 */
964 	int tocopy, this_copy;
965 	int *lenat;
966 	int did_delay = 0;
967 
968 	tocopy = length;
969 	if (length < (int)(2 * sizeof(int))) {
970 		/* not enough room */
971 		return (0);
972 	}
973 	if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
974 		atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
975 	again:
976 		if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
977 			/* we delay here for just a moment hoping the writer(s) that were
978 			 * present when we entered will have left and we only have
979 			 * locking ones that will contend with us for the lock. This
980 			 * does not assure 100% access, but its good enough for
981 			 * a logging facility like this.
982 			 */
983 			did_delay = 1;
984 			DELAY(10);
985 			goto again;
986 		}
987 	}
988 	SCTP_IP_PKTLOG_LOCK();
989 	lenat = (int *)target;
990 	*lenat = SCTP_BASE_VAR(packet_log_end);
991 	lenat++;
992 	this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
993 	memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
994 	if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
995 		atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
996 				    SCTP_PKTLOG_WRITERS_NEED_LOCK);
997 	}
998 	SCTP_IP_PKTLOG_UNLOCK();
999 	return (this_copy + sizeof(int));
1000 }
1001 
1002 #endif
1003