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