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