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