2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, 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 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $ */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
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>
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");
75 /* Global NON-VNET structure that controls the iterator */
76 struct iterator_control sctp_it_ctl;
80 sctp_wakeup_iterator(void)
82 wakeup(&sctp_it_ctl.iterator_running);
86 sctp_iterator_thread(void *v SCTP_UNUSED)
88 SCTP_IPI_ITERATOR_WQ_LOCK();
89 /* In FreeBSD this thread never terminates. */
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();
99 sctp_startup_iterator(void)
101 static int called = 0;
105 /* You only get one */
108 /* init the iterator head */
110 sctp_it_ctl.iterator_running = 0;
111 sctp_it_ctl.iterator_flags = 0;
112 sctp_it_ctl.cur_it = NULL;
113 SCTP_ITERATOR_LOCK_INIT();
114 SCTP_IPI_ITERATOR_WQ_INIT();
115 TAILQ_INIT(&sctp_it_ctl.iteratorhead);
116 ret = kproc_create(sctp_iterator_thread,
118 &sctp_it_ctl.thread_proc,
127 sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
129 struct in6_ifaddr *ifa6;
131 ifa6 = (struct in6_ifaddr *)ifa->ifa;
132 ifa->flags = ifa6->ia6_flags;
133 if (!MODULE_GLOBAL(ip6_use_deprecated)) {
135 IN6_IFF_DEPRECATED) {
136 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
138 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
141 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
147 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
149 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
157 sctp_is_desired_interface_type(struct ifaddr *ifa)
161 /* check the interface type to see if it's one we care about */
162 switch (ifa->ifa_ifp->if_type) {
175 case IFT_ISDNPRIMARY:
186 case IFT_VIRTUALIPADDRESS:
200 sctp_init_ifns_for_vrf(int vrfid)
203 * Here we must apply ANY locks needed by the IFN we access and also
204 * make sure we lock any IFA that exists as we float through the
209 struct sctp_ifa *sctp_ifa;
213 struct in6_ifaddr *ifa6;
218 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
220 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
221 if (ifa->ifa_addr == NULL) {
224 switch (ifa->ifa_addr->sa_family) {
227 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
234 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
235 /* skip unspecifed addresses */
243 if (sctp_is_desired_interface_type(ifa) == 0) {
244 /* non desired type */
247 switch (ifa->ifa_addr->sa_family) {
255 ifa6 = (struct in6_ifaddr *)ifa;
256 ifa_flags = ifa6->ia6_flags;
263 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
273 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
282 sctp_init_vrf_list(int vrfid)
284 if (vrfid > SCTP_MAX_VRF_ID)
288 /* Don't care about return here */
289 (void)sctp_allocate_vrf(vrfid);
292 * Now we need to build all the ifn's for this vrf and there
295 sctp_init_ifns_for_vrf(vrfid);
299 sctp_addr_change(struct ifaddr *ifa, int cmd)
301 uint32_t ifa_flags = 0;
304 * BSD only has one VRF, if this changes we will need to hook in the
305 * right things here to get the id to pass to the address managment
308 if (SCTP_BASE_VAR(first_time) == 0) {
309 /* Special test to see if my ::1 will showup with this */
310 SCTP_BASE_VAR(first_time) = 1;
311 sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
313 if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
314 /* don't know what to do with this */
317 if (ifa->ifa_addr == NULL) {
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 */
342 if (sctp_is_desired_interface_type(ifa) == 0) {
343 /* non desired type */
346 if (cmd == RTM_ADD) {
347 (void)sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
348 ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type,
349 ifa->ifa_ifp->if_xname,
350 (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
353 sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
354 ifa->ifa_ifp->if_index,
355 ifa->ifa_ifp->if_xname
358 * We don't bump refcount here so when it completes the
359 * final delete will happen.
365 sctp_add_or_del_interfaces(int (*pred) (struct ifnet *), int add){
370 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(ifnet), if_list) {
371 if (!(*pred) (ifn)) {
374 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
375 sctp_addr_change(ifa, add ? RTM_ADD : RTM_DELETE);
382 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
383 int how, int allonebuf, int type)
385 struct mbuf *m = NULL;
387 m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
395 if (SCTP_BUF_IS_EXTENDED(m)) {
396 siz = SCTP_BUF_EXTEND_SIZE(m);
403 if (siz < space_needed) {
408 if (SCTP_BUF_NEXT(m)) {
409 sctp_m_freem(SCTP_BUF_NEXT(m));
410 SCTP_BUF_NEXT(m) = NULL;
412 #ifdef SCTP_MBUF_LOGGING
413 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
414 if (SCTP_BUF_IS_EXTENDED(m)) {
415 sctp_log_mb(m, SCTP_MBUF_IALLOC);
423 #ifdef SCTP_PACKET_LOGGING
425 sctp_packet_log(struct mbuf *m, int length)
431 int grabbed_lock = 0;
432 int value, newval, thisend, thisbegin;
435 * Buffer layout. -sizeof this entry (total_len) -previous end
436 * (value) -ticks of log (ticks) o -ip packet o -as logged -
437 * where this started (thisbegin) x <--end points here
439 total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
440 /* Log a packet to the buffer. */
441 if (total_len > SCTP_PACKET_LOG_SIZE) {
442 /* Can't log this packet I have not a buffer big enough */
445 if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
448 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
450 if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
451 SCTP_IP_PKTLOG_LOCK();
454 value = SCTP_BASE_VAR(packet_log_end);
455 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
456 if (newval >= SCTP_PACKET_LOG_SIZE) {
461 thisbegin = SCTP_BASE_VAR(packet_log_end);
464 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
468 value = SCTP_BASE_VAR(packet_log_end);
469 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
470 if (newval >= SCTP_PACKET_LOG_SIZE) {
475 thisbegin = SCTP_BASE_VAR(packet_log_end);
478 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
483 if (thisend >= SCTP_PACKET_LOG_SIZE) {
484 printf("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
487 SCTP_BASE_VAR(packet_log_writers),
489 SCTP_BASE_VAR(packet_log_end));
490 SCTP_BASE_VAR(packet_log_end) = 0;
494 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
499 tick_tock = (uint32_t *) lenat;
501 *tick_tock = sctp_get_tick_count();
502 copyto = (void *)lenat;
503 thisone = thisend - sizeof(int);
504 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
507 SCTP_IP_PKTLOG_UNLOCK();
510 m_copydata(m, 0, length, (caddr_t)copyto);
513 SCTP_IP_PKTLOG_UNLOCK();
515 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
520 sctp_copy_out_packet_log(uint8_t * target, int length)
523 * We wind through the packet log starting at start copying up to
524 * length bytes out. We return the number of bytes copied.
526 int tocopy, this_copy;
531 if (length < (int)(2 * sizeof(int))) {
532 /* not enough room */
535 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
536 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
538 if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
540 * we delay here for just a moment hoping the
541 * writer(s) that were present when we entered will
542 * have left and we only have locking ones that will
543 * contend with us for the lock. This does not
544 * assure 100% access, but its good enough for a
545 * logging facility like this.
552 SCTP_IP_PKTLOG_LOCK();
553 lenat = (int *)target;
554 *lenat = SCTP_BASE_VAR(packet_log_end);
556 this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
557 memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
558 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
559 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
560 SCTP_PKTLOG_WRITERS_NEED_LOCK);
562 SCTP_IP_PKTLOG_UNLOCK();
563 return (this_copy + sizeof(int));