sctp_bsd_addr.c revision 208160
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: head/sys/netinet/sctp_bsd_addr.c 208160 2010-05-16 17:03:56Z rrs $"); 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 */ 52MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor"); 53MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array"); 54MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array"); 55MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address"); 56MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator"); 57MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist"); 58MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key"); 59MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list"); 60MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info"); 61MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset"); 62MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer"); 63MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all"); 64MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct"); 65MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct"); 66MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct"); 67MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block"); 68MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list"); 69MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control"); 70MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option"); 71 72/* Global NON-VNET structure that controls the iterator */ 73struct iterator_control sctp_it_ctl; 74static int __sctp_thread_based_iterator_started = 0; 75 76 77static void 78sctp_cleanup_itqueue(void) 79{ 80 struct sctp_iterator *it; 81 82 while ((it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead)) != NULL) { 83 if (it->function_atend != NULL) { 84 (*it->function_atend) (it->pointer, it->val); 85 } 86 TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr); 87 SCTP_FREE(it, SCTP_M_ITER); 88 } 89} 90 91 92void 93sctp_wakeup_iterator(void) 94{ 95 wakeup(&sctp_it_ctl.iterator_running); 96} 97 98static void 99sctp_iterator_thread(void *v) 100{ 101 SCTP_IPI_ITERATOR_WQ_LOCK(); 102 while (1) { 103 msleep(&sctp_it_ctl.iterator_running, 104 &sctp_it_ctl.ipi_iterator_wq_mtx, 105 0, "waiting_for_work", 0); 106 if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) { 107 SCTP_IPI_ITERATOR_WQ_DESTROY(); 108 SCTP_ITERATOR_LOCK_DESTROY(); 109 sctp_cleanup_itqueue(); 110 __sctp_thread_based_iterator_started = 0; 111 kthread_exit(); 112 } 113 sctp_iterator_worker(); 114 } 115} 116 117void 118sctp_startup_iterator(void) 119{ 120 if (__sctp_thread_based_iterator_started) { 121 /* You only get one */ 122 return; 123 } 124 /* init the iterator head */ 125 __sctp_thread_based_iterator_started = 1; 126 sctp_it_ctl.iterator_running = 0; 127 sctp_it_ctl.iterator_flags = 0; 128 sctp_it_ctl.cur_it = NULL; 129 SCTP_ITERATOR_LOCK_INIT(); 130 SCTP_IPI_ITERATOR_WQ_INIT(); 131 TAILQ_INIT(&sctp_it_ctl.iteratorhead); 132 133 int ret; 134 135 ret = kproc_create(sctp_iterator_thread, 136 (void *)NULL, 137 &sctp_it_ctl.thread_proc, 138 RFPROC, 139 SCTP_KTHREAD_PAGES, 140 SCTP_KTRHEAD_NAME); 141} 142 143 144#ifdef INET6 145 146void 147sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa) 148{ 149 struct in6_ifaddr *ifa6; 150 151 ifa6 = (struct in6_ifaddr *)ifa->ifa; 152 ifa->flags = ifa6->ia6_flags; 153 if (!MODULE_GLOBAL(ip6_use_deprecated)) { 154 if (ifa->flags & 155 IN6_IFF_DEPRECATED) { 156 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE; 157 } else { 158 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE; 159 } 160 } else { 161 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE; 162 } 163 if (ifa->flags & 164 (IN6_IFF_DETACHED | 165 IN6_IFF_ANYCAST | 166 IN6_IFF_NOTREADY)) { 167 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE; 168 } else { 169 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE; 170 } 171} 172 173#endif /* INET6 */ 174 175 176static uint32_t 177sctp_is_desired_interface_type(struct ifaddr *ifa) 178{ 179 int result; 180 181 /* check the interface type to see if it's one we care about */ 182 switch (ifa->ifa_ifp->if_type) { 183 case IFT_ETHER: 184 case IFT_ISO88023: 185 case IFT_ISO88024: 186 case IFT_ISO88025: 187 case IFT_ISO88026: 188 case IFT_STARLAN: 189 case IFT_P10: 190 case IFT_P80: 191 case IFT_HY: 192 case IFT_FDDI: 193 case IFT_XETHER: 194 case IFT_ISDNBASIC: 195 case IFT_ISDNPRIMARY: 196 case IFT_PTPSERIAL: 197 case IFT_OTHER: 198 case IFT_PPP: 199 case IFT_LOOP: 200 case IFT_SLIP: 201 case IFT_GIF: 202 case IFT_L2VLAN: 203 case IFT_IP: 204 case IFT_IPOVERCDLC: 205 case IFT_IPOVERCLAW: 206 case IFT_VIRTUALIPADDRESS: 207 result = 1; 208 break; 209 default: 210 result = 0; 211 } 212 213 return (result); 214} 215 216 217 218 219static void 220sctp_init_ifns_for_vrf(int vrfid) 221{ 222 /* 223 * Here we must apply ANY locks needed by the IFN we access and also 224 * make sure we lock any IFA that exists as we float through the 225 * list of IFA's 226 */ 227 struct ifnet *ifn; 228 struct ifaddr *ifa; 229 struct in6_ifaddr *ifa6; 230 struct sctp_ifa *sctp_ifa; 231 uint32_t ifa_flags; 232 233 IFNET_RLOCK(); 234 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) { 235 IF_ADDR_LOCK(ifn); 236 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) { 237 if (ifa->ifa_addr == NULL) { 238 continue; 239 } 240 if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) { 241 /* non inet/inet6 skip */ 242 continue; 243 } 244 if (ifa->ifa_addr->sa_family == AF_INET6) { 245 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) { 246 /* skip unspecifed addresses */ 247 continue; 248 } 249 } else { 250 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) { 251 continue; 252 } 253 } 254 if (sctp_is_desired_interface_type(ifa) == 0) { 255 /* non desired type */ 256 continue; 257 } 258 if (ifa->ifa_addr->sa_family == AF_INET6) { 259 ifa6 = (struct in6_ifaddr *)ifa; 260 ifa_flags = ifa6->ia6_flags; 261 } else { 262 ifa_flags = 0; 263 } 264 sctp_ifa = sctp_add_addr_to_vrf(vrfid, 265 (void *)ifn, 266 ifn->if_index, 267 ifn->if_type, 268 ifn->if_xname, 269 (void *)ifa, 270 ifa->ifa_addr, 271 ifa_flags, 272 0); 273 if (sctp_ifa) { 274 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE; 275 } 276 } 277 IF_ADDR_UNLOCK(ifn); 278 } 279 IFNET_RUNLOCK(); 280} 281 282void 283sctp_init_vrf_list(int vrfid) 284{ 285 if (vrfid > SCTP_MAX_VRF_ID) 286 /* can't do that */ 287 return; 288 289 /* Don't care about return here */ 290 (void)sctp_allocate_vrf(vrfid); 291 292 /* 293 * Now we need to build all the ifn's for this vrf and there 294 * addresses 295 */ 296 sctp_init_ifns_for_vrf(vrfid); 297} 298 299void 300sctp_addr_change(struct ifaddr *ifa, int cmd) 301{ 302 struct sctp_ifa *ifap = NULL; 303 uint32_t ifa_flags = 0; 304 305 /* 306 * BSD only has one VRF, if this changes we will need to hook in the 307 * right things here to get the id to pass to the address managment 308 * routine. 309 */ 310 if (SCTP_BASE_VAR(first_time) == 0) { 311 /* Special test to see if my ::1 will showup with this */ 312 SCTP_BASE_VAR(first_time) = 1; 313 sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID); 314 } 315 if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) { 316 /* don't know what to do with this */ 317 return; 318 } 319 if (ifa->ifa_addr == NULL) { 320 return; 321 } 322 if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) { 323 /* non inet/inet6 skip */ 324 return; 325 } 326 if (ifa->ifa_addr->sa_family == AF_INET6) { 327 ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags; 328 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) { 329 /* skip unspecifed addresses */ 330 return; 331 } 332 } else { 333 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) { 334 return; 335 } 336 } 337 338 if (sctp_is_desired_interface_type(ifa) == 0) { 339 /* non desired type */ 340 return; 341 } 342 if (cmd == RTM_ADD) { 343 ifap = sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp, 344 ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type, 345 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 360void 361 sctp_add_or_del_interfaces(int (*pred) (struct ifnet *), int add){ 362 struct ifnet *ifn; 363 struct ifaddr *ifa; 364 365 IFNET_RLOCK(); 366 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) { 367 if (!(*pred) (ifn)) { 368 continue; 369 } 370 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) { 371 sctp_addr_change(ifa, add ? RTM_ADD : RTM_DELETE); 372 } 373 } 374 IFNET_RUNLOCK(); 375} 376 377struct mbuf * 378sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header, 379 int how, int allonebuf, int type) 380{ 381 struct mbuf *m = NULL; 382 383 m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0); 384 if (m == NULL) { 385 /* bad, no memory */ 386 return (m); 387 } 388 if (allonebuf) { 389 int siz; 390 391 if (SCTP_BUF_IS_EXTENDED(m)) { 392 siz = SCTP_BUF_EXTEND_SIZE(m); 393 } else { 394 if (want_header) 395 siz = MHLEN; 396 else 397 siz = MLEN; 398 } 399 if (siz < space_needed) { 400 m_freem(m); 401 return (NULL); 402 } 403 } 404 if (SCTP_BUF_NEXT(m)) { 405 sctp_m_freem(SCTP_BUF_NEXT(m)); 406 SCTP_BUF_NEXT(m) = NULL; 407 } 408#ifdef SCTP_MBUF_LOGGING 409 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) { 410 if (SCTP_BUF_IS_EXTENDED(m)) { 411 sctp_log_mb(m, SCTP_MBUF_IALLOC); 412 } 413 } 414#endif 415 return (m); 416} 417 418 419#ifdef SCTP_PACKET_LOGGING 420void 421sctp_packet_log(struct mbuf *m, int length) 422{ 423 int *lenat, thisone; 424 void *copyto; 425 uint32_t *tick_tock; 426 int total_len; 427 int grabbed_lock = 0; 428 int value, newval, thisend, thisbegin; 429 430 /* 431 * Buffer layout. -sizeof this entry (total_len) -previous end 432 * (value) -ticks of log (ticks) o -ip packet o -as logged - 433 * where this started (thisbegin) x <--end points here 434 */ 435 total_len = SCTP_SIZE32((length + (4 * sizeof(int)))); 436 /* Log a packet to the buffer. */ 437 if (total_len > SCTP_PACKET_LOG_SIZE) { 438 /* Can't log this packet I have not a buffer big enough */ 439 return; 440 } 441 if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) { 442 return; 443 } 444 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1); 445try_again: 446 if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) { 447 SCTP_IP_PKTLOG_LOCK(); 448 grabbed_lock = 1; 449again_locked: 450 value = SCTP_BASE_VAR(packet_log_end); 451 newval = SCTP_BASE_VAR(packet_log_end) + total_len; 452 if (newval >= SCTP_PACKET_LOG_SIZE) { 453 /* we wrapped */ 454 thisbegin = 0; 455 thisend = total_len; 456 } else { 457 thisbegin = SCTP_BASE_VAR(packet_log_end); 458 thisend = newval; 459 } 460 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) { 461 goto again_locked; 462 } 463 } else { 464 value = SCTP_BASE_VAR(packet_log_end); 465 newval = SCTP_BASE_VAR(packet_log_end) + total_len; 466 if (newval >= SCTP_PACKET_LOG_SIZE) { 467 /* we wrapped */ 468 thisbegin = 0; 469 thisend = total_len; 470 } else { 471 thisbegin = SCTP_BASE_VAR(packet_log_end); 472 thisend = newval; 473 } 474 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) { 475 goto try_again; 476 } 477 } 478 /* Sanity check */ 479 if (thisend >= SCTP_PACKET_LOG_SIZE) { 480 printf("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n", 481 thisbegin, 482 thisend, 483 SCTP_BASE_VAR(packet_log_writers), 484 grabbed_lock, 485 SCTP_BASE_VAR(packet_log_end)); 486 SCTP_BASE_VAR(packet_log_end) = 0; 487 goto no_log; 488 489 } 490 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin]; 491 *lenat = total_len; 492 lenat++; 493 *lenat = value; 494 lenat++; 495 tick_tock = (uint32_t *) lenat; 496 lenat++; 497 *tick_tock = sctp_get_tick_count(); 498 copyto = (void *)lenat; 499 thisone = thisend - sizeof(int); 500 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone]; 501 *lenat = thisbegin; 502 if (grabbed_lock) { 503 SCTP_IP_PKTLOG_UNLOCK(); 504 grabbed_lock = 0; 505 } 506 m_copydata(m, 0, length, (caddr_t)copyto); 507no_log: 508 if (grabbed_lock) { 509 SCTP_IP_PKTLOG_UNLOCK(); 510 } 511 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1); 512} 513 514 515int 516sctp_copy_out_packet_log(uint8_t * target, int length) 517{ 518 /* 519 * We wind through the packet log starting at start copying up to 520 * length bytes out. We return the number of bytes copied. 521 */ 522 int tocopy, this_copy; 523 int *lenat; 524 int did_delay = 0; 525 526 tocopy = length; 527 if (length < (int)(2 * sizeof(int))) { 528 /* not enough room */ 529 return (0); 530 } 531 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) { 532 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK); 533again: 534 if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) { 535 /* 536 * we delay here for just a moment hoping the 537 * writer(s) that were present when we entered will 538 * have left and we only have locking ones that will 539 * contend with us for the lock. This does not 540 * assure 100% access, but its good enough for a 541 * logging facility like this. 542 */ 543 did_delay = 1; 544 DELAY(10); 545 goto again; 546 } 547 } 548 SCTP_IP_PKTLOG_LOCK(); 549 lenat = (int *)target; 550 *lenat = SCTP_BASE_VAR(packet_log_end); 551 lenat++; 552 this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE); 553 memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy); 554 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) { 555 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 556 SCTP_PKTLOG_WRITERS_NEED_LOCK); 557 } 558 SCTP_IP_PKTLOG_UNLOCK(); 559 return (this_copy + sizeof(int)); 560} 561 562#endif 563