2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * a) Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
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.
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.
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.
31 /* $KAME: sctp_output.c,v 1.46 2005/03/06 16:04:17 itojun Exp $ */
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 */
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.
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");
82 #if defined(SCTP_USE_THREAD_BASED_ITERATOR)
84 sctp_wakeup_iterator(void)
86 wakeup(&SCTP_BASE_INFO(iterator_running));
90 sctp_iterator_thread(void *v)
92 SCTP_IPI_ITERATOR_WQ_LOCK();
93 SCTP_BASE_INFO(iterator_running) = 0;
95 msleep(&SCTP_BASE_INFO(iterator_running),
96 &SCTP_BASE_INFO(ipi_iterator_wq_mtx),
97 0, "waiting_for_work", 0);
98 if (SCTP_BASE_INFO(threads_must_exit)) {
101 sctp_iterator_worker();
106 sctp_startup_iterator(void)
110 ret = kproc_create(sctp_iterator_thread,
112 &SCTP_BASE_INFO(thread_proc),
123 sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa)
125 struct in6_ifaddr *ifa6;
127 ifa6 = (struct in6_ifaddr *)ifa->ifa;
128 ifa->flags = ifa6->ia6_flags;
129 if (!MODULE_GLOBAL(MOD_INET6, ip6_use_deprecated)) {
131 IN6_IFF_DEPRECATED) {
132 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
134 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
137 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
143 ifa->localifa_flags |= SCTP_ADDR_IFA_UNUSEABLE;
145 ifa->localifa_flags &= ~SCTP_ADDR_IFA_UNUSEABLE;
153 sctp_is_desired_interface_type(struct ifaddr *ifa)
157 /* check the interface type to see if it's one we care about */
158 switch (ifa->ifa_ifp->if_type) {
171 case IFT_ISDNPRIMARY:
182 case IFT_VIRTUALIPADDRESS:
196 sctp_init_ifns_for_vrf(int vrfid)
199 * Here we must apply ANY locks needed by the IFN we access and also
200 * make sure we lock any IFA that exists as we float through the
205 struct in6_ifaddr *ifa6;
206 struct sctp_ifa *sctp_ifa;
209 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(MOD_NET, ifnet), if_list) {
211 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
212 if (ifa->ifa_addr == NULL) {
215 if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
216 /* non inet/inet6 skip */
219 if (ifa->ifa_addr->sa_family == AF_INET6) {
220 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
221 /* skip unspecifed addresses */
225 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
229 if (sctp_is_desired_interface_type(ifa) == 0) {
230 /* non desired type */
233 if (ifa->ifa_addr->sa_family == AF_INET6) {
234 ifa6 = (struct in6_ifaddr *)ifa;
235 ifa_flags = ifa6->ia6_flags;
239 sctp_ifa = sctp_add_addr_to_vrf(vrfid,
249 sctp_ifa->localifa_flags &= ~SCTP_ADDR_DEFER_USE;
257 sctp_init_vrf_list(int vrfid)
259 if (vrfid > SCTP_MAX_VRF_ID)
263 /* Don't care about return here */
264 (void)sctp_allocate_vrf(vrfid);
267 * Now we need to build all the ifn's for this vrf and there
270 sctp_init_ifns_for_vrf(vrfid);
274 sctp_addr_change(struct ifaddr *ifa, int cmd)
276 struct sctp_ifa *ifap = NULL;
277 uint32_t ifa_flags = 0;
280 * BSD only has one VRF, if this changes we will need to hook in the
281 * right things here to get the id to pass to the address managment
284 if (SCTP_BASE_VAR(first_time) == 0) {
285 /* Special test to see if my ::1 will showup with this */
286 SCTP_BASE_VAR(first_time) = 1;
287 sctp_init_ifns_for_vrf(SCTP_DEFAULT_VRFID);
289 if ((cmd != RTM_ADD) && (cmd != RTM_DELETE)) {
290 /* don't know what to do with this */
293 if (ifa->ifa_addr == NULL) {
296 if ((ifa->ifa_addr->sa_family != AF_INET) && (ifa->ifa_addr->sa_family != AF_INET6)) {
297 /* non inet/inet6 skip */
300 if (ifa->ifa_addr->sa_family == AF_INET6) {
301 ifa_flags = ((struct in6_ifaddr *)ifa)->ia6_flags;
302 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr)) {
303 /* skip unspecifed addresses */
307 if (((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr == 0) {
312 if (sctp_is_desired_interface_type(ifa) == 0) {
313 /* non desired type */
316 if (cmd == RTM_ADD) {
317 ifap = sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, (void *)ifa->ifa_ifp,
318 ifa->ifa_ifp->if_index, ifa->ifa_ifp->if_type,
319 ifa->ifa_ifp->if_xname,
320 (void *)ifa, ifa->ifa_addr, ifa_flags, 1);
323 sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, ifa->ifa_addr,
324 ifa->ifa_ifp->if_index,
325 ifa->ifa_ifp->if_xname
328 * We don't bump refcount here so when it completes the
329 * final delete will happen.
335 sctp_add_or_del_interfaces(int (*pred) (struct ifnet *), int add){
339 TAILQ_FOREACH(ifn, &MODULE_GLOBAL(MOD_NET, ifnet), if_list) {
340 if (!(*pred) (ifn)) {
343 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
344 sctp_addr_change(ifa, add ? RTM_ADD : RTM_DELETE);
350 sctp_get_mbuf_for_msg(unsigned int space_needed, int want_header,
351 int how, int allonebuf, int type)
353 struct mbuf *m = NULL;
355 m = m_getm2(NULL, space_needed, how, type, want_header ? M_PKTHDR : 0);
363 if (SCTP_BUF_IS_EXTENDED(m)) {
364 siz = SCTP_BUF_EXTEND_SIZE(m);
371 if (siz < space_needed) {
376 if (SCTP_BUF_NEXT(m)) {
377 sctp_m_freem(SCTP_BUF_NEXT(m));
378 SCTP_BUF_NEXT(m) = NULL;
380 #ifdef SCTP_MBUF_LOGGING
381 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
382 if (SCTP_BUF_IS_EXTENDED(m)) {
383 sctp_log_mb(m, SCTP_MBUF_IALLOC);
391 #ifdef SCTP_PACKET_LOGGING
393 sctp_packet_log(struct mbuf *m, int length)
399 int grabbed_lock = 0;
400 int value, newval, thisend, thisbegin;
403 * Buffer layout. -sizeof this entry (total_len) -previous end
404 * (value) -ticks of log (ticks) o -ip packet o -as logged -
405 * where this started (thisbegin) x <--end points here
407 total_len = SCTP_SIZE32((length + (4 * sizeof(int))));
408 /* Log a packet to the buffer. */
409 if (total_len > SCTP_PACKET_LOG_SIZE) {
410 /* Can't log this packet I have not a buffer big enough */
413 if (length < (int)(SCTP_MIN_V4_OVERHEAD + sizeof(struct sctp_cookie_ack_chunk))) {
416 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), 1);
418 if (SCTP_BASE_VAR(packet_log_writers) > SCTP_PKTLOG_WRITERS_NEED_LOCK) {
419 SCTP_IP_PKTLOG_LOCK();
422 value = SCTP_BASE_VAR(packet_log_end);
423 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
424 if (newval >= SCTP_PACKET_LOG_SIZE) {
429 thisbegin = SCTP_BASE_VAR(packet_log_end);
432 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
436 value = SCTP_BASE_VAR(packet_log_end);
437 newval = SCTP_BASE_VAR(packet_log_end) + total_len;
438 if (newval >= SCTP_PACKET_LOG_SIZE) {
443 thisbegin = SCTP_BASE_VAR(packet_log_end);
446 if (!(atomic_cmpset_int(&SCTP_BASE_VAR(packet_log_end), value, thisend))) {
451 if (thisend >= SCTP_PACKET_LOG_SIZE) {
452 printf("Insanity stops a log thisbegin:%d thisend:%d writers:%d lock:%d end:%d\n",
455 SCTP_BASE_VAR(packet_log_writers),
457 SCTP_BASE_VAR(packet_log_end));
458 SCTP_BASE_VAR(packet_log_end) = 0;
462 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisbegin];
467 tick_tock = (uint32_t *) lenat;
469 *tick_tock = sctp_get_tick_count();
470 copyto = (void *)lenat;
471 thisone = thisend - sizeof(int);
472 lenat = (int *)&SCTP_BASE_VAR(packet_log_buffer)[thisone];
475 SCTP_IP_PKTLOG_UNLOCK();
478 m_copydata(m, 0, length, (caddr_t)copyto);
481 SCTP_IP_PKTLOG_UNLOCK();
483 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers), 1);
488 sctp_copy_out_packet_log(uint8_t * target, int length)
491 * We wind through the packet log starting at start copying up to
492 * length bytes out. We return the number of bytes copied.
494 int tocopy, this_copy;
499 if (length < (int)(2 * sizeof(int))) {
500 /* not enough room */
503 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
504 atomic_add_int(&SCTP_BASE_VAR(packet_log_writers), SCTP_PKTLOG_WRITERS_NEED_LOCK);
506 if ((did_delay == 0) && (SCTP_BASE_VAR(packet_log_writers) != SCTP_PKTLOG_WRITERS_NEED_LOCK)) {
508 * we delay here for just a moment hoping the
509 * writer(s) that were present when we entered will
510 * have left and we only have locking ones that will
511 * contend with us for the lock. This does not
512 * assure 100% access, but its good enough for a
513 * logging facility like this.
520 SCTP_IP_PKTLOG_LOCK();
521 lenat = (int *)target;
522 *lenat = SCTP_BASE_VAR(packet_log_end);
524 this_copy = min((length - sizeof(int)), SCTP_PACKET_LOG_SIZE);
525 memcpy((void *)lenat, (void *)SCTP_BASE_VAR(packet_log_buffer), this_copy);
526 if (SCTP_PKTLOG_WRITERS_NEED_LOCK) {
527 atomic_subtract_int(&SCTP_BASE_VAR(packet_log_writers),
528 SCTP_PKTLOG_WRITERS_NEED_LOCK);
530 SCTP_IP_PKTLOG_UNLOCK();
531 return (this_copy + sizeof(int));