2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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>
51 /* Declare all of our malloc named types */
52 MALLOC_DEFINE(SCTP_M_MAP, "sctp_map", "sctp asoc map descriptor");
53 MALLOC_DEFINE(SCTP_M_STRMI, "sctp_stri", "sctp stream in array");
54 MALLOC_DEFINE(SCTP_M_STRMO, "sctp_stro", "sctp stream out array");
55 MALLOC_DEFINE(SCTP_M_ASC_ADDR, "sctp_aadr", "sctp asconf address");
56 MALLOC_DEFINE(SCTP_M_ASC_IT, "sctp_a_it", "sctp asconf iterator");
57 MALLOC_DEFINE(SCTP_M_AUTH_CL, "sctp_atcl", "sctp auth chunklist");
58 MALLOC_DEFINE(SCTP_M_AUTH_KY, "sctp_atky", "sctp auth key");
59 MALLOC_DEFINE(SCTP_M_AUTH_HL, "sctp_athm", "sctp auth hmac list");
60 MALLOC_DEFINE(SCTP_M_AUTH_IF, "sctp_athi", "sctp auth info");
61 MALLOC_DEFINE(SCTP_M_STRESET, "sctp_stre", "sctp stream reset");
62 MALLOC_DEFINE(SCTP_M_CMSG, "sctp_cmsg", "sctp CMSG buffer");
63 MALLOC_DEFINE(SCTP_M_COPYAL, "sctp_cpal", "sctp copy all");
64 MALLOC_DEFINE(SCTP_M_VRF, "sctp_vrf", "sctp vrf struct");
65 MALLOC_DEFINE(SCTP_M_IFA, "sctp_ifa", "sctp ifa struct");
66 MALLOC_DEFINE(SCTP_M_IFN, "sctp_ifn", "sctp ifn struct");
67 MALLOC_DEFINE(SCTP_M_TIMW, "sctp_timw", "sctp time block");
68 MALLOC_DEFINE(SCTP_M_MVRF, "sctp_mvrf", "sctp mvrf pcb list");
69 MALLOC_DEFINE(SCTP_M_ITER, "sctp_iter", "sctp iterator control");
70 MALLOC_DEFINE(SCTP_M_SOCKOPT, "sctp_socko", "sctp socket option");
71 MALLOC_DEFINE(SCTP_M_MCORE, "sctp_mcore", "sctp mcore queue");
73 /* Global NON-VNET structure that controls the iterator */
74 struct iterator_control sctp_it_ctl;
78 sctp_wakeup_iterator(void)
80 wakeup(&sctp_it_ctl.iterator_running);
84 sctp_iterator_thread(void *v SCTP_UNUSED)
86 SCTP_IPI_ITERATOR_WQ_LOCK();
87 /* In FreeBSD this thread never terminates. */
89 msleep(&sctp_it_ctl.iterator_running,
90 &sctp_it_ctl.ipi_iterator_wq_mtx,
91 0, "waiting_for_work", 0);
92 sctp_iterator_worker();
97 sctp_startup_iterator(void)
99 if (sctp_it_ctl.thread_proc) {
100 /* You only get one */
103 /* Initialize global locks here, thus only once. */
104 SCTP_ITERATOR_LOCK_INIT();
105 SCTP_IPI_ITERATOR_WQ_INIT();
106 TAILQ_INIT(&sctp_it_ctl.iteratorhead);
107 kproc_create(sctp_iterator_thread,
109 &sctp_it_ctl.thread_proc,
118 sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
120 struct in6_ifaddr *ifa6;
122 ifa6 = (struct in6_ifaddr *)ifa->ifa;
123 ifa->flags = ifa6->ia6_flags;
124 if (!MODULE_GLOBAL(ip6_use_deprecated)) {
126 IN6_IFF_DEPRECATED) {
127 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
129 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
132 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
138 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
140 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
148 sctp_is_desired_interface_type(struct ifnet *ifn)
152 /* check the interface type to see if it's one we care about */
153 switch (ifn->if_type) {
166 case IFT_ISDNPRIMARY:
178 case IFT_PROPVIRTUAL: /* NetGraph Virtual too */
179 case IFT_VIRTUALIPADDRESS:
193 sctp_init_ifns_for_vrf(int vrfid)
196 * Here we must apply ANY locks needed by the IFN we access and also
197 * make sure we lock any IFA that exists as we float through the
202 struct sctp_ifa *sctp_ifa;
206 struct in6_ifaddr *ifa6;
211 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
212 if (sctp_is_desired_interface_type(ifn) == 0) {
213 /* non desired type */
217 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
218 if (ifa->ifa_addr == NULL) {
221 switch (ifa->ifa_addr->sa_family) {
224 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
231 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
232 /* skip unspecifed addresses */
240 switch (ifa->ifa_addr->sa_family) {
248 ifa6 = (struct in6_ifaddr *)ifa;
249 ifa_flags = ifa6->ia6_flags;
256 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
266 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
269 IF_ADDR_RUNLOCK(ifn);
275 sctp_init_vrf_list(int vrfid)
277 if (vrfid > SCTP_MAX_VRF_ID)
281 /* Don't care about return here */
282 (void)sctp_allocate_vrf(vrfid);
285 * Now we need to build all the ifn's for this vrf and there
288 sctp_init_ifns_for_vrf(vrfid);
292 sctp_addr_change(struct ifaddr *ifa, int cmd)
294 uint32_t ifa_flags = 0;
296 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
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 managment
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);
309 if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
310 /* don't know what to do with this */
313 if (ifa->ifa_addr == NULL) {
316 if (sctp_is_desired_interface_type(ifa->ifa_ifp) == 0) {
317 /* non desired type */
320 switch (ifa->ifa_addr->sa_family) {
323 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
330 ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
331 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
332 /* skip unspecifed addresses */
338 /* non inet/inet6 skip */
341 if (cmd == RTM_ADD) {
342 (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
343 ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type, ifa->ifa_ifp->if_xname,
344 (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
347 sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
348 ifa->ifa_ifp->if_index,
349 ifa->ifa_ifp->if_xname);
352 * We don't bump refcount here so when it completes the
353 * final delete will happen.
359 sctp_add_or_del_interfaces(int (*pred) (struct ifnet *), int add){
364 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
365 if (!(*pred) (ifn)) {
368 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
369 sctp_addr_change(ifa, add ? RTM_ADD : RTM_DELETE);
376 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
377 int how, int allonebuf, int type)
379 struct mbuf *m = NULL;
381 m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
389 if (SCTP_BUF_IS_EXTENDED(m)) {
390 siz = SCTP_BUF_EXTEND_SIZE(m);
397 if (siz < space_needed) {
402 if (SCTP_BUF_NEXT(m)) {
403 sctp_m_freem(SCTP_BUF_NEXT(m));
404 SCTP_BUF_NEXT(m) = NULL;
406 #ifdef SCTP_MBUF_LOGGING
407 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
408 sctp_log_mb(m, SCTP_MBUF_IALLOC);
415 #ifdef SCTP_PACKET_LOGGING
417 sctp_packet_log(struct mbuf *m)
424 int grabbed_lock = 0;
425 int value, newval, thisend, thisbegin;
428 * Buffer layout. -sizeof this entry (total_len) -previous end
429 * (value) -ticks of log (ticks) o -ip packet o -as logged -
430 * where this started (thisbegin) x <--end points here
432 length = SCTP_HEADER_LEN(m);
433 total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
434 /* Log a packet to the buffer. */
435 if (total_len > SCTP_PACKET_LOG_SIZE) {
436 /* Can't log this packet I have not a buffer big enough */
439 if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
442 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
444 if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
445 SCTP_IP_PKTLOG_LOCK();
448 value = SCTP_BASE_VAR(packet_log_end);
449 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
450 if (newval >= SCTP_PACKET_LOG_SIZE) {
455 thisbegin = SCTP_BASE_VAR(packet_log_end);
458 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
462 value = SCTP_BASE_VAR(packet_log_end);
463 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
464 if (newval >= SCTP_PACKET_LOG_SIZE) {
469 thisbegin = SCTP_BASE_VAR(packet_log_end);
472 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
477 if (thisend >= SCTP_PACKET_LOG_SIZE) {
478 SCTP_PRINTF("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
481 SCTP_BASE_VAR(packet_log_writers),
483 SCTP_BASE_VAR(packet_log_end));
484 SCTP_BASE_VAR(packet_log_end) = 0;
488 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
493 tick_tock = (uint32_t *) lenat;
495 *tick_tock = sctp_get_tick_count();
496 copyto = (void *)lenat;
497 thisone = thisend - sizeof(int);
498 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
501 SCTP_IP_PKTLOG_UNLOCK();
504 m_copydata(m, 0, length, (caddr_t)copyto);
507 SCTP_IP_PKTLOG_UNLOCK();
509 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
514 sctp_copy_out_packet_log(uint8_t * target, int length)
517 * We wind through the packet log starting at start copying up to
518 * length bytes out. We return the number of bytes copied.
520 int tocopy, this_copy;
525 if (length < (int)(2 * sizeof(int))) {
526 /* not enough room */
529 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
530 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
532 if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
534 * we delay here for just a moment hoping the
535 * writer(s) that were present when we entered will
536 * have left and we only have locking ones that will
537 * contend with us for the lock. This does not
538 * assure 100% access, but its good enough for a
539 * logging facility like this.
546 SCTP_IP_PKTLOG_LOCK();
547 lenat = (int *)target;
548 *lenat = SCTP_BASE_VAR(packet_log_end);
550 this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
551 memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
552 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
553 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
554 SCTP_PKTLOG_WRITERS_NEED_LOCK);
556 SCTP_IP_PKTLOG_UNLOCK();
557 return (this_copy + sizeof(int));