sctputil.h revision 206137
1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3163953Srrs *
4163953Srrs * Redistribution and use in source and binary forms, with or without
5163953Srrs * modification, are permitted provided that the following conditions are met:
6163953Srrs *
7163953Srrs * a) Redistributions of source code must retain the above copyright notice,
8163953Srrs *   this list of conditions and the following disclaimer.
9163953Srrs *
10163953Srrs * b) Redistributions in binary form must reproduce the above copyright
11163953Srrs *    notice, this list of conditions and the following disclaimer in
12163953Srrs *   the documentation and/or other materials provided with the distribution.
13163953Srrs *
14163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
15163953Srrs *    contributors may be used to endorse or promote products derived
16163953Srrs *    from this software without specific prior written permission.
17163953Srrs *
18163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
29163953Srrs */
30163953Srrs
31163953Srrs
32163953Srrs/* $KAME: sctputil.h,v 1.15 2005/03/06 16:04:19 itojun Exp $	 */
33163953Srrs
34163953Srrs#include <sys/cdefs.h>
35163953Srrs__FBSDID("$FreeBSD: head/sys/netinet/sctputil.h 206137 2010-04-03 15:40:14Z tuexen $");
36163953Srrs#ifndef __sctputil_h__
37163953Srrs#define __sctputil_h__
38163953Srrs
39163953Srrs
40180387Srrs#if defined(_KERNEL) || defined(__Userspace__)
41163953Srrs
42195918Srrs#define SCTP_READ_LOCK_HELD 1
43195918Srrs#define SCTP_READ_LOCK_NOT_HELD 0
44168755Srrs
45168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS
46168859Srrsvoid sctp_print_out_track_log(struct sctp_tcb *stcb);
47168859Srrs
48168859Srrs#endif
49168859Srrs
50163953Srrs#ifdef SCTP_MBUF_LOGGING
51163953Srrsstruct mbuf *sctp_m_free(struct mbuf *m);
52163953Srrsvoid sctp_m_freem(struct mbuf *m);
53163953Srrs
54163953Srrs#else
55163953Srrs#define sctp_m_free m_free
56163953Srrs#define sctp_m_freem m_freem
57163953Srrs#endif
58163953Srrs
59171943Srrs#if defined(SCTP_LOCAL_TRACE_BUF) || defined(__APPLE__)
60171943Srrsvoid
61172157Srrs     sctp_log_trace(uint32_t fr, const char *str SCTP_UNUSED, uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f);
62163953Srrs
63171943Srrs#endif
64171943Srrs
65163953Srrs#define sctp_get_associd(stcb) ((sctp_assoc_t)stcb->asoc.assoc_id)
66163953Srrs
67163953Srrs
68163953Srrs/*
69163953Srrs * Function prototypes
70163953Srrs */
71167698Srrsuint32_t
72167698Srrssctp_get_ifa_hash_val(struct sockaddr *addr);
73167698Srrs
74167598Srrsstruct sctp_ifa *
75167598Srrs         sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr, int hold_lock);
76163953Srrs
77167598Srrsstruct sctp_ifa *
78167598Srrs         sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock);
79167598Srrs
80163953Srrsuint32_t sctp_select_initial_TSN(struct sctp_pcb *);
81163953Srrs
82185694Srrsuint32_t sctp_select_a_tag(struct sctp_inpcb *, uint16_t lport, uint16_t rport, int);
83163953Srrs
84206137Stuexenint sctp_init_asoc(struct sctp_inpcb *, struct sctp_tcb *, uint32_t, uint32_t);
85163953Srrs
86163953Srrsvoid sctp_fill_random_store(struct sctp_pcb *);
87163953Srrs
88169420Srrsvoid
89163953Srrssctp_timer_start(int, struct sctp_inpcb *, struct sctp_tcb *,
90163953Srrs    struct sctp_nets *);
91163953Srrs
92169378Srrsvoid
93163953Srrssctp_timer_stop(int, struct sctp_inpcb *, struct sctp_tcb *,
94165220Srrs    struct sctp_nets *, uint32_t);
95163953Srrs
96167598Srrsint
97167598Srrs    sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id);
98167598Srrs
99163953Srrsvoid
100166023Srrs     sctp_mtu_size_reset(struct sctp_inpcb *, struct sctp_association *, uint32_t);
101163953Srrs
102163953Srrsvoid
103163953Srrssctp_add_to_readq(struct sctp_inpcb *inp,
104163953Srrs    struct sctp_tcb *stcb,
105163953Srrs    struct sctp_queued_to_read *control,
106163953Srrs    struct sockbuf *sb,
107172090Srrs    int end,
108195918Srrs    int inpread_locked,
109172090Srrs    int so_locked
110172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
111172090Srrs    SCTP_UNUSED
112172090Srrs#endif
113172090Srrs);
114163953Srrs
115163953Srrsint
116163953Srrssctp_append_to_readq(struct sctp_inpcb *inp,
117163953Srrs    struct sctp_tcb *stcb,
118163953Srrs    struct sctp_queued_to_read *control,
119163953Srrs    struct mbuf *m,
120163953Srrs    int end,
121163953Srrs    int new_cumack,
122163953Srrs    struct sockbuf *sb);
123163953Srrs
124163953Srrs
125167598Srrsvoid sctp_iterator_worker(void);
126167598Srrs
127163953Srrsint find_next_best_mtu(int);
128163953Srrs
129163953Srrsvoid
130163953Srrs     sctp_timeout_handler(void *);
131163953Srrs
132163953Srrsuint32_t
133163953Srrssctp_calculate_rto(struct sctp_tcb *, struct sctp_association *,
134171477Srrs    struct sctp_nets *, struct timeval *, int);
135163953Srrs
136163953Srrsuint32_t sctp_calculate_len(struct mbuf *);
137163953Srrs
138163953Srrscaddr_t sctp_m_getptr(struct mbuf *, int, int, uint8_t *);
139163953Srrs
140163953Srrsstruct sctp_paramhdr *
141163953Srrssctp_get_next_param(struct mbuf *, int,
142163953Srrs    struct sctp_paramhdr *, int);
143163953Srrs
144163953Srrsint sctp_add_pad_tombuf(struct mbuf *, int);
145163953Srrs
146163953Srrsint sctp_pad_lastmbuf(struct mbuf *, int, struct mbuf *);
147163953Srrs
148172090Srrsvoid
149172090Srrssctp_ulp_notify(uint32_t, struct sctp_tcb *, uint32_t, void *, int
150172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
151172090Srrs    SCTP_UNUSED
152172090Srrs#endif
153172090Srrs);
154163953Srrs
155163953Srrsvoid
156163953Srrssctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
157163953Srrs    struct sctp_inpcb *new_inp,
158169208Srrs    struct sctp_tcb *stcb, int waitflags);
159163953Srrs
160163953Srrs
161163953Srrsvoid sctp_stop_timers_for_shutdown(struct sctp_tcb *);
162163953Srrs
163172090Srrsvoid
164172090Srrssctp_report_all_outbound(struct sctp_tcb *, int, int
165172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
166172090Srrs    SCTP_UNUSED
167172090Srrs#endif
168172090Srrs);
169163953Srrs
170170138Srrsint sctp_expand_mapping_array(struct sctp_association *, uint32_t);
171163953Srrs
172172090Srrsvoid
173172090Srrssctp_abort_notification(struct sctp_tcb *, int, int
174172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
175172090Srrs    SCTP_UNUSED
176172090Srrs#endif
177172090Srrs);
178163953Srrs
179163953Srrs/* We abort responding to an IP packet for some reason */
180163953Srrsvoid
181163953Srrssctp_abort_association(struct sctp_inpcb *, struct sctp_tcb *,
182179157Srrs    struct mbuf *, int, struct sctphdr *, struct mbuf *, uint32_t, uint16_t);
183163953Srrs
184169352Srrs
185163953Srrs/* We choose to abort via user input */
186163953Srrsvoid
187163953Srrssctp_abort_an_association(struct sctp_inpcb *, struct sctp_tcb *, int,
188172090Srrs    struct mbuf *, int
189172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
190172090Srrs    SCTP_UNUSED
191172090Srrs#endif
192172090Srrs);
193163953Srrs
194169352Srrsvoid
195163953Srrssctp_handle_ootb(struct mbuf *, int, int, struct sctphdr *,
196179157Srrs    struct sctp_inpcb *, struct mbuf *, uint32_t, uint16_t);
197163953Srrs
198170056Srrsint
199170056Srrssctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
200170056Srrs    int totaddr, int *error);
201169352Srrs
202169352Srrsstruct sctp_tcb *
203170056Srrssctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
204170056Srrs    int *totaddr, int *num_v4, int *num_v6, int *error, int limit, int *bad_addr);
205169352Srrs
206163953Srrsint sctp_is_there_an_abort_here(struct mbuf *, int, uint32_t *);
207178251Srrs
208178251Srrs#ifdef INET6
209163953Srrsuint32_t sctp_is_same_scope(struct sockaddr_in6 *, struct sockaddr_in6 *);
210166675Srrs
211163953Srrsstruct sockaddr_in6 *
212166675Srrs             sctp_recover_scope(struct sockaddr_in6 *, struct sockaddr_in6 *);
213163953Srrs
214163953Srrs#define sctp_recover_scope_mac(addr, store) do { \
215166675Srrs	 if ((addr->sin6_family == AF_INET6) && \
216166675Srrs	     (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr))) { \
217166675Srrs		*store = *addr; \
218166675Srrs		if (addr->sin6_scope_id == 0) { \
219166675Srrs			if (!sa6_recoverscope(store)) { \
220166675Srrs				addr = store; \
221166675Srrs			} \
222166675Srrs		} else { \
223166675Srrs			in6_clearscope(&addr->sin6_addr); \
224166675Srrs			addr = store; \
225166675Srrs		} \
226166675Srrs	 } \
227166675Srrs} while (0)
228178251Srrs#endif
229163953Srrs
230163953Srrsint sctp_cmpaddr(struct sockaddr *, struct sockaddr *);
231163953Srrs
232163953Srrsvoid sctp_print_address(struct sockaddr *);
233163953Srrsvoid sctp_print_address_pkt(struct ip *, struct sctphdr *);
234163953Srrs
235163953Srrsint
236163953Srrssctp_release_pr_sctp_chunk(struct sctp_tcb *, struct sctp_tmit_chunk *,
237189790Srrs    int, int
238172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
239172090Srrs    SCTP_UNUSED
240172090Srrs#endif
241172090Srrs);
242163953Srrs
243163953Srrsstruct mbuf *sctp_generate_invmanparam(int);
244163953Srrs
245170606Srrsvoid
246170606Srrssctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
247170606Srrs    struct sockaddr *sa, sctp_assoc_t assoc_id,
248170606Srrs    uint32_t vrf_id, int *error, void *p);
249170606Srrsvoid
250170606Srrssctp_bindx_delete_address(struct socket *so, struct sctp_inpcb *inp,
251170606Srrs    struct sockaddr *sa, sctp_assoc_t assoc_id,
252170606Srrs    uint32_t vrf_id, int *error);
253163953Srrs
254171572Srrsint sctp_local_addr_count(struct sctp_tcb *stcb);
255171572Srrs
256163953Srrs#ifdef SCTP_MBCNT_LOGGING
257163953Srrsvoid
258163953Srrssctp_free_bufspace(struct sctp_tcb *, struct sctp_association *,
259170744Srrs    struct sctp_tmit_chunk *, int);
260163953Srrs
261163953Srrs#else
262163953Srrs#define sctp_free_bufspace(stcb, asoc, tp1, chk_cnt)  \
263163953Srrsdo { \
264163953Srrs	if (tp1->data != NULL) { \
265169420Srrs                atomic_subtract_int(&((asoc)->chunks_on_out_queue), chk_cnt); \
266163953Srrs		if ((asoc)->total_output_queue_size >= tp1->book_size) { \
267169420Srrs			atomic_subtract_int(&((asoc)->total_output_queue_size), tp1->book_size); \
268163953Srrs		} else { \
269163953Srrs			(asoc)->total_output_queue_size = 0; \
270163953Srrs		} \
271163953Srrs   	        if (stcb->sctp_socket && ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \
272163953Srrs	            (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \
273163953Srrs			if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) { \
274169420Srrs				atomic_subtract_int(&((stcb)->sctp_socket->so_snd.sb_cc), tp1->book_size); \
275163953Srrs			} else { \
276163953Srrs				stcb->sctp_socket->so_snd.sb_cc = 0; \
277163953Srrs			} \
278163953Srrs		} \
279163953Srrs        } \
280163953Srrs} while (0)
281163953Srrs
282163953Srrs#endif
283163953Srrs
284163953Srrs#define sctp_free_spbufspace(stcb, asoc, sp)  \
285163953Srrsdo { \
286163953Srrs 	if (sp->data != NULL) { \
287163953Srrs		if ((asoc)->total_output_queue_size >= sp->length) { \
288169420Srrs			atomic_subtract_int(&(asoc)->total_output_queue_size, sp->length); \
289163953Srrs		} else { \
290163953Srrs			(asoc)->total_output_queue_size = 0; \
291163953Srrs		} \
292163953Srrs   	        if (stcb->sctp_socket && ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \
293163953Srrs	            (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \
294163953Srrs			if (stcb->sctp_socket->so_snd.sb_cc >= sp->length) { \
295169420Srrs				atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc,sp->length); \
296163953Srrs			} else { \
297163953Srrs				stcb->sctp_socket->so_snd.sb_cc = 0; \
298163953Srrs			} \
299163953Srrs		} \
300163953Srrs        } \
301163953Srrs} while (0)
302163953Srrs
303163953Srrs#define sctp_snd_sb_alloc(stcb, sz)  \
304163953Srrsdo { \
305163953Srrs	atomic_add_int(&stcb->asoc.total_output_queue_size,sz); \
306163953Srrs	if ((stcb->sctp_socket != NULL) && \
307163953Srrs	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \
308163953Srrs	     (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \
309163953Srrs		atomic_add_int(&stcb->sctp_socket->so_snd.sb_cc,sz); \
310163953Srrs	} \
311163953Srrs} while (0)
312163953Srrs
313179157Srrs/* new functions to start/stop udp tunneling */
314179157Srrsvoid sctp_over_udp_stop(void);
315179157Srrsint sctp_over_udp_start(void);
316163953Srrs
317163953Srrsint
318163953Srrssctp_soreceive(struct socket *so, struct sockaddr **psa,
319163953Srrs    struct uio *uio,
320163953Srrs    struct mbuf **mp0,
321163953Srrs    struct mbuf **controlp,
322163953Srrs    int *flagsp);
323163953Srrs
324163953Srrs
325169352Srrs/* For those not passing mbufs, this does the
326169352Srrs * translations for you. Caller owns memory
327169352Srrs * of size controllen returned in controlp.
328169352Srrs */
329169352Srrsint
330169352Srrssctp_l_soreceive(struct socket *so,
331169352Srrs    struct sockaddr **name,
332169352Srrs    struct uio *uio,
333169352Srrs    char **controlp,
334169352Srrs    int *controllen,
335169352Srrs    int *flag);
336169352Srrs
337169352Srrs
338163953Srrsvoid
339163953Srrs     sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d);
340163953Srrs
341163953Srrsvoid
342163953Srrssctp_wakeup_log(struct sctp_tcb *stcb,
343163953Srrs    uint32_t cumtsn,
344163953Srrs    uint32_t wake_cnt, int from);
345163953Srrs
346164181Srrsvoid sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t, uint16_t, uint16_t, int);
347163953Srrs
348163953Srrsvoid sctp_log_nagle_event(struct sctp_tcb *stcb, int action);
349163953Srrs
350163953Srrs
351163953Srrsvoid
352163953Srrs     sctp_log_mb(struct mbuf *m, int from);
353163953Srrs
354163953Srrsvoid
355163953Srrssctp_sblog(struct sockbuf *sb,
356163953Srrs    struct sctp_tcb *stcb, int from, int incr);
357163953Srrs
358163953Srrsvoid
359163953Srrssctp_log_strm_del(struct sctp_queued_to_read *control,
360163953Srrs    struct sctp_queued_to_read *poschk,
361163953Srrs    int from);
362163953Srrsvoid sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *, int, uint8_t);
363163953Srrsvoid rto_logging(struct sctp_nets *net, int from);
364163953Srrs
365163953Srrsvoid sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc);
366163953Srrs
367163953Srrsvoid sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from);
368163953Srrsvoid sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *, int, int, uint8_t);
369163953Srrsvoid sctp_log_block(uint8_t, struct socket *, struct sctp_association *, int);
370163953Srrsvoid sctp_log_rwnd(uint8_t, uint32_t, uint32_t, uint32_t);
371163953Srrsvoid sctp_log_mbcnt(uint8_t, uint32_t, uint32_t, uint32_t, uint32_t);
372163953Srrsvoid sctp_log_rwnd_set(uint8_t, uint32_t, uint32_t, uint32_t, uint32_t);
373167598Srrsint sctp_fill_stat_log(void *, size_t *);
374163953Srrsvoid sctp_log_fr(uint32_t, uint32_t, uint32_t, int);
375163953Srrsvoid sctp_log_sack(uint32_t, uint32_t, uint32_t, uint16_t, uint16_t, int);
376163953Srrsvoid sctp_log_map(uint32_t, uint32_t, uint32_t, int);
377205502Srrsvoid sctp_print_mapping_array(struct sctp_association *asoc);
378163953Srrsvoid sctp_clr_stat_log(void);
379163953Srrs
380163953Srrs
381163953Srrs#ifdef SCTP_AUDITING_ENABLED
382163953Srrsvoid
383163953Srrssctp_auditing(int, struct sctp_inpcb *, struct sctp_tcb *,
384163953Srrs    struct sctp_nets *);
385163953Srrsvoid sctp_audit_log(uint8_t, uint8_t);
386163953Srrs
387163953Srrs#endif
388163953Srrs
389163953Srrs
390163953Srrs#endif				/* _KERNEL */
391163953Srrs#endif
392