Home | History | Annotate | Line # | Download | only in netinet
      1 /*	$KAME: sctputil.h,v 1.15 2005/03/06 16:04:19 itojun Exp $	*/
      2 /*	$NetBSD: sctputil.h,v 1.5 2024/07/05 04:26:50 rin Exp $ */
      3 
      4 #ifndef __SCTPUTIL_H__
      5 #define __SCTPUTIL_H__
      6 
      7 /*
      8  * Copyright (C) 2002, 2003, 2004 Cisco Systems Inc,
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. Neither the name of the project nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #if defined(_KERNEL)
     37 
     38 #ifdef SCTP_MBUF_DEBUG
     39 #define sctp_m_freem(m) do { \
     40     struct mbuf *_m = (m);
     41     printf("m_freem(%p) m->nxtpkt:%p at %s[%d]\n", \
     42 	   _m, _m != NULL ? _m->m_next : NULL, __FILE__, __LINE__); \
     43     m_freem(_m); \
     44 } while (0);
     45 #else
     46 #define sctp_m_freem m_freem
     47 #endif
     48 
     49 #define sctp_m_copym	m_copym
     50 
     51 /*
     52  * Zone(pool) allocation routines: MUST be defined for each OS
     53  * zone = zone/pool pointer
     54  * name = string name of the zone/pool
     55  * size = size of each zone/pool element
     56  * number = number of elements in zone/pool
     57  */
     58 #if defined(__FreeBSD__)
     59 #if __FreeBSD_version >= 500000
     60 #include <vm/uma.h>
     61 #else
     62 #include <vm/vm_zone.h>
     63 #endif
     64 #elif defined(__NetBSD__) || defined(__OpenBSD__)
     65 #include <sys/pool.h>
     66 #endif
     67 
     68 /* SCTP_ZONE_INIT: initialize the zone */
     69 #if defined(__FreeBSD__)
     70 #if __FreeBSD_version >= 500000
     71 #define UMA_ZFLAG_FULL	0x0020
     72 #define SCTP_ZONE_INIT(zone, name, size, number) { \
     73 	zone = uma_zcreate(name, size, NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,\
     74 		UMA_ZFLAG_FULL); \
     75 	uma_zone_set_max(zone, number); \
     76 }
     77 #else
     78 #define SCTP_ZONE_INIT(zone, name, size, number) \
     79 	zone = zinit(name, size, number, ZONE_INTERRUPT, 0);
     80 #endif
     81 #elif defined(__APPLE__)
     82 #define SCTP_ZONE_INIT(zone, name, size, number) \
     83 	zone = (void *)zinit(size, number * size, number, name);
     84 #elif defined(__OpenBSD__) || defined(__NetBSD__)
     85 #define SCTP_ZONE_INIT(zone, name, size, number) \
     86 	pool_init(&(zone), size, 0, 0, 0, name, NULL, IPL_NET);
     87 #else
     88 	/* don't know this OS! */
     89 	force_compile_error;
     90 #endif
     91 
     92 /* SCTP_ZONE_GET: allocate element from the zone */
     93 #if defined(__FreeBSD__)
     94 #if __FreeBSD_version >= 500000
     95 #define SCTP_ZONE_GET(zone) \
     96 	uma_zalloc(zone, M_NOWAIT);
     97 #else
     98 #define SCTP_ZONE_GET(zone) \
     99 	zalloci(zone);
    100 #endif
    101 #elif defined(__APPLE__)
    102 #define SCTP_ZONE_GET(zone) \
    103 	zalloc(zone);
    104 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    105 #define SCTP_ZONE_GET(zone) \
    106 	pool_get(&zone, PR_NOWAIT);
    107 #else
    108 	/* don't know this OS! */
    109 	force_compile_error;
    110 #endif
    111 
    112 /* SCTP_ZONE_FREE: free element from the zone */
    113 #if defined(__FreeBSD__)
    114 #if __FreeBSD_version >= 500000
    115 #define SCTP_ZONE_FREE(zone, element) \
    116 	uma_zfree(zone, element);
    117 #else
    118 #define SCTP_ZONE_FREE(zone, element) \
    119 	zfreei(zone, element);
    120 #endif
    121 #elif defined(__APPLE__)
    122 #define SCTP_ZONE_FREE(zone, element) \
    123 	zfree(zone, element);
    124 #elif defined(__NetBSD__) || defined(__OpenBSD__)
    125 #define SCTP_ZONE_FREE(zone, element) \
    126 	pool_put(&zone, element);
    127 #else
    128 	/* don't know this OS! */
    129 	force_compile_error;
    130 #endif
    131 
    132 #define sctp_get_associd(stcb) ((sctp_assoc_t)stcb->asoc.my_vtag)
    133 
    134 /*
    135  * Function prototypes
    136  */
    137 struct ifaddr *sctp_find_ifa_by_addr(struct sockaddr *sa);
    138 
    139 u_int32_t sctp_select_initial_TSN(struct sctp_pcb *);
    140 
    141 u_int32_t sctp_select_a_tag(struct sctp_inpcb *);
    142 
    143 int sctp_init_asoc(struct sctp_inpcb *, struct sctp_association *, int, uint32_t);
    144 
    145 int sctp_timer_start(int, struct sctp_inpcb *, struct sctp_tcb *,
    146 	struct sctp_nets *);
    147 
    148 int sctp_timer_stop(int, struct sctp_inpcb *, struct sctp_tcb *,
    149 	struct sctp_nets *);
    150 
    151 u_int32_t sctp_calculate_sum(struct mbuf *, int32_t *, u_int32_t);
    152 
    153 void sctp_mtu_size_reset(struct sctp_inpcb *, struct sctp_association *,
    154 	u_long);
    155 
    156 int find_next_best_mtu(int);
    157 
    158 u_int32_t sctp_calculate_rto(struct sctp_tcb *, struct sctp_association *,
    159 	struct sctp_nets *, struct timeval *);
    160 
    161 u_int32_t sctp_calculate_len(struct mbuf *);
    162 
    163 void *sctp_m_getptr(struct mbuf *, int, int, u_int8_t *);
    164 
    165 struct sctp_paramhdr *sctp_get_next_param(struct mbuf *, int,
    166 	struct sctp_paramhdr *, int);
    167 
    168 int sctp_add_pad_tombuf(struct mbuf *, int);
    169 
    170 int sctp_pad_lastmbuf(struct mbuf *, int);
    171 
    172 void sctp_ulp_notify(u_int32_t, struct sctp_tcb *, u_int32_t, void *);
    173 
    174 void sctp_report_all_outbound(struct sctp_tcb *);
    175 
    176 int sctp_expand_mapping_array(struct sctp_association *);
    177 
    178 void sctp_abort_notification(struct sctp_tcb *, int);
    179 
    180 /* We abort responding to an IP packet for some reason */
    181 void sctp_abort_association(struct sctp_inpcb *, struct sctp_tcb *,
    182     struct mbuf *, int, struct sctphdr *, struct mbuf *);
    183 
    184 /* We choose to abort via user input */
    185 void sctp_abort_an_association(struct sctp_inpcb *, struct sctp_tcb *, int,
    186 	struct mbuf *);
    187 
    188 void sctp_handle_ootb(struct mbuf *, int, int, struct sctphdr *,
    189     struct sctp_inpcb *, struct mbuf *);
    190 
    191 int sctp_is_there_an_abort_here(struct mbuf *, int, int *);
    192 uint32_t sctp_is_same_scope(const struct sockaddr_in6 *,
    193 	const struct sockaddr_in6 *);
    194 const struct sockaddr_in6 *sctp_recover_scope(const struct sockaddr_in6 *,
    195 	struct sockaddr_in6 *);
    196 
    197 int sctp_cmpaddr(const struct sockaddr *, const struct sockaddr *);
    198 
    199 void sctp_print_address(const struct sockaddr *);
    200 void sctp_print_address_pkt(struct ip *, struct sctphdr *);
    201 
    202 int sbappendaddr_nocheck(struct sockbuf *, const struct sockaddr *,
    203 	struct mbuf *, struct mbuf *, u_int32_t, struct sctp_inpcb *);
    204 
    205 
    206 int sctp_release_pr_sctp_chunk(struct sctp_tcb *, struct sctp_tmit_chunk *,
    207 	int, struct sctpchunk_listhead *);
    208 
    209 struct mbuf *sctp_generate_invmanparam(int);
    210 
    211 /*
    212  * this is an evil layer violation that I think is a hack.. but I stand
    213  * alone on the tsvwg in this thought... everyone else considers it part
    214  * of the sockets layer (along with all of the peeloff code :<)
    215  */
    216 u_int32_t sctp_get_first_vtag_from_sb(struct socket *);
    217 
    218 
    219 void sctp_grub_through_socket_buffer(struct sctp_inpcb *, struct socket *,
    220 				     struct socket *, struct sctp_tcb *);
    221 
    222 void sctp_free_bufspace(struct sctp_tcb *, struct sctp_association *,
    223 	struct sctp_tmit_chunk *);
    224 
    225 #ifdef SCTP_STAT_LOGGING
    226 void sctp_log_strm_del_alt(u_int32_t, u_int16_t, int);
    227 
    228 void sctp_log_strm_del(struct sctp_tmit_chunk *, struct sctp_tmit_chunk *, int);
    229 void sctp_log_cwnd(struct sctp_nets *, int, uint8_t);
    230 void sctp_log_maxburst(struct sctp_nets *, int, int, uint8_t);
    231 void sctp_log_block(uint8_t, struct socket *, struct sctp_association *);
    232 void sctp_log_rwnd(uint8_t, u_int32_t, u_int32_t, u_int32_t );
    233 void sctp_log_mbcnt(uint8_t, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
    234 void sctp_log_rwnd_set(uint8_t, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
    235 int sctp_fill_stat_log(struct mbuf *);
    236 void sctp_log_fr(uint32_t, uint32_t, uint32_t, int);
    237 void sctp_log_map(uint32_t, uint32_t, uint32_t, int);
    238 
    239 void sctp_clr_stat_log(void);
    240 
    241 #endif
    242 
    243 #ifdef SCTP_AUDITING_ENABLED
    244 void sctp_auditing(int, struct sctp_inpcb *, struct sctp_tcb *,
    245 	struct sctp_nets *);
    246 void sctp_audit_log(u_int8_t, u_int8_t);
    247 
    248 #endif
    249 
    250 #ifdef SCTP_BASE_FREEBSD
    251 /* Note: these are in <sys/time.h>, but not in kernel space */
    252 #define	timerclear(tvp)		(tvp)->tv_sec = (tvp)->tv_usec = 0
    253 #define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
    254 #define	timercmp(tvp, uvp, cmp)						\
    255 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
    256 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
    257 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
    258 #define	timeradd(tvp, uvp, vvp)						\
    259 	do {								\
    260 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
    261 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
    262 		if ((vvp)->tv_usec >= 1000000) {			\
    263 			(vvp)->tv_sec++;				\
    264 			(vvp)->tv_usec -= 1000000;			\
    265 		}							\
    266 	} while (/* CONSTCOND */ 0)
    267 #define	timersub(tvp, uvp, vvp)						\
    268 	do {								\
    269 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
    270 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
    271 		if ((vvp)->tv_usec < 0) {				\
    272 			(vvp)->tv_sec--;				\
    273 			(vvp)->tv_usec += 1000000;			\
    274 		}							\
    275 	} while (/* CONSTCOND */ 0)
    276 #endif /* SCTP_BASE_FREEBSD */
    277 
    278 #endif /* _KERNEL */
    279 #endif
    280