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