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