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