xref: /netbsd-src/sys/netinet/sctputil.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$KAME: sctputil.h,v 1.15 2005/03/06 16:04:19 itojun Exp $	*/
2 /*	$NetBSD: sctputil.h,v 1.2 2016/05/22 23:04:27 rjs 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     printf("m_freem(%p) m->nxtpkt:%p at %s[%d]\n", \
41 	   (m), (m)->m_next, __FILE__, __LINE__); \
42     m_freem(m); \
43 } while (0);
44 #else
45 #define sctp_m_freem m_freem
46 #endif
47 
48 #define sctp_m_copym	m_copym
49 
50 /*
51  * Zone(pool) allocation routines: MUST be defined for each OS
52  * zone = zone/pool pointer
53  * name = string name of the zone/pool
54  * size = size of each zone/pool element
55  * number = number of elements in zone/pool
56  */
57 #if defined(__FreeBSD__)
58 #if __FreeBSD_version >= 500000
59 #include <vm/uma.h>
60 #else
61 #include <vm/vm_zone.h>
62 #endif
63 #elif defined(__NetBSD__) || defined(__OpenBSD__)
64 #include <sys/pool.h>
65 #endif
66 
67 /* SCTP_ZONE_INIT: initialize the zone */
68 #if defined(__FreeBSD__)
69 #if __FreeBSD_version >= 500000
70 #define UMA_ZFLAG_FULL	0x0020
71 #define SCTP_ZONE_INIT(zone, name, size, number) { \
72 	zone = uma_zcreate(name, size, NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,\
73 		UMA_ZFLAG_FULL); \
74 	uma_zone_set_max(zone, number); \
75 }
76 #else
77 #define SCTP_ZONE_INIT(zone, name, size, number) \
78 	zone = zinit(name, size, number, ZONE_INTERRUPT, 0);
79 #endif
80 #elif defined(__APPLE__)
81 #define SCTP_ZONE_INIT(zone, name, size, number) \
82 	zone = (void *)zinit(size, number * size, number, name);
83 #elif defined(__OpenBSD__) || defined(__NetBSD__)
84 #define SCTP_ZONE_INIT(zone, name, size, number) \
85 	pool_init(&(zone), size, 0, 0, 0, name, NULL, IPL_NET);
86 #else
87 	/* don't know this OS! */
88 	force_comile_error;
89 #endif
90 
91 /* SCTP_ZONE_GET: allocate element from the zone */
92 #if defined(__FreeBSD__)
93 #if __FreeBSD_version >= 500000
94 #define SCTP_ZONE_GET(zone) \
95 	uma_zalloc(zone, M_NOWAIT);
96 #else
97 #define SCTP_ZONE_GET(zone) \
98 	zalloci(zone);
99 #endif
100 #elif defined(__APPLE__)
101 #define SCTP_ZONE_GET(zone) \
102 	zalloc(zone);
103 #elif defined(__NetBSD__) || defined(__OpenBSD__)
104 #define SCTP_ZONE_GET(zone) \
105 	pool_get(&zone, PR_NOWAIT);
106 #else
107 	/* don't know this OS! */
108 	force_comile_error;
109 #endif
110 
111 /* SCTP_ZONE_FREE: free element from the zone */
112 #if defined(__FreeBSD__)
113 #if __FreeBSD_version >= 500000
114 #define SCTP_ZONE_FREE(zone, element) \
115 	uma_zfree(zone, element);
116 #else
117 #define SCTP_ZONE_FREE(zone, element) \
118 	zfreei(zone, element);
119 #endif
120 #elif defined(__APPLE__)
121 #define SCTP_ZONE_FREE(zone, element) \
122 	zfree(zone, element);
123 #elif defined(__NetBSD__) || defined(__OpenBSD__)
124 #define SCTP_ZONE_FREE(zone, element) \
125 	pool_put(&zone, element);
126 #else
127 	/* don't know this OS! */
128 	force_comile_error;
129 #endif
130 
131 #define sctp_get_associd(stcb) ((sctp_assoc_t)stcb->asoc.my_vtag)
132 
133 /*
134  * Function prototypes
135  */
136 struct ifaddr *sctp_find_ifa_by_addr(struct sockaddr *sa);
137 
138 u_int32_t sctp_select_initial_TSN(struct sctp_pcb *);
139 
140 u_int32_t sctp_select_a_tag(struct sctp_inpcb *);
141 
142 int sctp_init_asoc(struct sctp_inpcb *, struct sctp_association *, int, uint32_t);
143 
144 void sctp_fill_random_store(struct sctp_pcb *);
145 
146 int sctp_timer_start(int, struct sctp_inpcb *, struct sctp_tcb *,
147 	struct sctp_nets *);
148 
149 int sctp_timer_stop(int, struct sctp_inpcb *, struct sctp_tcb *,
150 	struct sctp_nets *);
151 
152 u_int32_t sctp_calculate_sum(struct mbuf *, int32_t *, u_int32_t);
153 
154 void sctp_mtu_size_reset(struct sctp_inpcb *, struct sctp_association *,
155 	u_long);
156 
157 int find_next_best_mtu(int);
158 
159 u_int32_t sctp_calculate_rto(struct sctp_tcb *, struct sctp_association *,
160 	struct sctp_nets *, struct timeval *);
161 
162 u_int32_t sctp_calculate_len(struct mbuf *);
163 
164 void *sctp_m_getptr(struct mbuf *, int, int, u_int8_t *);
165 
166 struct sctp_paramhdr *sctp_get_next_param(struct mbuf *, int,
167 	struct sctp_paramhdr *, int);
168 
169 int sctp_add_pad_tombuf(struct mbuf *, int);
170 
171 int sctp_pad_lastmbuf(struct mbuf *, int);
172 
173 void sctp_ulp_notify(u_int32_t, struct sctp_tcb *, u_int32_t, void *);
174 
175 void sctp_report_all_outbound(struct sctp_tcb *);
176 
177 int sctp_expand_mapping_array(struct sctp_association *);
178 
179 void sctp_abort_notification(struct sctp_tcb *, int);
180 
181 /* We abort responding to an IP packet for some reason */
182 void sctp_abort_association(struct sctp_inpcb *, struct sctp_tcb *,
183     struct mbuf *, int, struct sctphdr *, struct mbuf *);
184 
185 /* We choose to abort via user input */
186 void sctp_abort_an_association(struct sctp_inpcb *, struct sctp_tcb *, int,
187 	struct mbuf *);
188 
189 void sctp_handle_ootb(struct mbuf *, int, int, struct sctphdr *,
190     struct sctp_inpcb *, struct mbuf *);
191 
192 int sctp_is_there_an_abort_here(struct mbuf *, int, int *);
193 uint32_t sctp_is_same_scope(const struct sockaddr_in6 *,
194 	const struct sockaddr_in6 *);
195 const struct sockaddr_in6 *sctp_recover_scope(const struct sockaddr_in6 *,
196 	struct sockaddr_in6 *);
197 
198 int sctp_cmpaddr(const struct sockaddr *, const struct sockaddr *);
199 
200 void sctp_print_address(const struct sockaddr *);
201 void sctp_print_address_pkt(struct ip *, struct sctphdr *);
202 
203 int sbappendaddr_nocheck(struct sockbuf *, const struct sockaddr *,
204 	struct mbuf *, struct mbuf *, u_int32_t, struct sctp_inpcb *);
205 
206 
207 int sctp_release_pr_sctp_chunk(struct sctp_tcb *, struct sctp_tmit_chunk *,
208 	int, struct sctpchunk_listhead *);
209 
210 struct mbuf *sctp_generate_invmanparam(int);
211 
212 /*
213  * this is an evil layer violation that I think is a hack.. but I stand
214  * alone on the tsvwg in this thought... everyone else considers it part
215  * of the sockets layer (along with all of the peeloff code :<)
216  */
217 u_int32_t sctp_get_first_vtag_from_sb(struct socket *);
218 
219 
220 void sctp_grub_through_socket_buffer(struct sctp_inpcb *, struct socket *,
221 				     struct socket *, struct sctp_tcb *);
222 
223 void sctp_free_bufspace(struct sctp_tcb *, struct sctp_association *,
224 	struct sctp_tmit_chunk *);
225 
226 #ifdef SCTP_STAT_LOGGING
227 void sctp_log_strm_del_alt(u_int32_t, u_int16_t, int);
228 
229 void sctp_log_strm_del(struct sctp_tmit_chunk *, struct sctp_tmit_chunk *, int);
230 void sctp_log_cwnd(struct sctp_nets *, int, uint8_t);
231 void sctp_log_maxburst(struct sctp_nets *, int, int, uint8_t);
232 void sctp_log_block(uint8_t, struct socket *, struct sctp_association *);
233 void sctp_log_rwnd(uint8_t, u_int32_t, u_int32_t, u_int32_t );
234 void sctp_log_mbcnt(uint8_t, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
235 void sctp_log_rwnd_set(uint8_t, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
236 int sctp_fill_stat_log(struct mbuf *);
237 void sctp_log_fr(uint32_t, uint32_t, uint32_t, int);
238 void sctp_log_map(uint32_t, uint32_t, uint32_t, int);
239 
240 void sctp_clr_stat_log(void);
241 
242 #endif
243 
244 #ifdef SCTP_AUDITING_ENABLED
245 void sctp_auditing(int, struct sctp_inpcb *, struct sctp_tcb *,
246 	struct sctp_nets *);
247 void sctp_audit_log(u_int8_t, u_int8_t);
248 
249 #endif
250 
251 #ifdef SCTP_BASE_FREEBSD
252 /* Note: these are in <sys/time.h>, but not in kernel space */
253 #define	timerclear(tvp)		(tvp)->tv_sec = (tvp)->tv_usec = 0
254 #define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
255 #define	timercmp(tvp, uvp, cmp)						\
256 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
257 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) :			\
258 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
259 #define	timeradd(tvp, uvp, vvp)						\
260 	do {								\
261 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
262 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
263 		if ((vvp)->tv_usec >= 1000000) {			\
264 			(vvp)->tv_sec++;				\
265 			(vvp)->tv_usec -= 1000000;			\
266 		}							\
267 	} while (/* CONSTCOND */ 0)
268 #define	timersub(tvp, uvp, vvp)						\
269 	do {								\
270 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
271 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
272 		if ((vvp)->tv_usec < 0) {				\
273 			(vvp)->tv_sec--;				\
274 			(vvp)->tv_usec += 1000000;			\
275 		}							\
276 	} while (/* CONSTCOND */ 0)
277 #endif /* SCTP_BASE_FREEBSD */
278 
279 #endif /* _KERNEL */
280 #endif
281