sctputil.h revision 298223
1162922Sariff/*-
2162922Sariff * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3162922Sariff * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4162922Sariff * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5162922Sariff *
6162922Sariff * Redistribution and use in source and binary forms, with or without
7162922Sariff * modification, are permitted provided that the following conditions are met:
8162922Sariff *
9162922Sariff * a) Redistributions of source code must retain the above copyright notice,
10162922Sariff *    this list of conditions and the following disclaimer.
11162922Sariff *
12162922Sariff * b) Redistributions in binary form must reproduce the above copyright
13162922Sariff *    notice, this list of conditions and the following disclaimer in
14162922Sariff *    the documentation and/or other materials provided with the distribution.
15162922Sariff *
16162922Sariff * c) Neither the name of Cisco Systems, Inc. nor the names of its
17162922Sariff *    contributors may be used to endorse or promote products derived
18162922Sariff *    from this software without specific prior written permission.
19162922Sariff *
20162922Sariff * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21162922Sariff * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22162922Sariff * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23162922Sariff * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24162922Sariff * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25162922Sariff * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26162922Sariff * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27162922Sariff * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28162922Sariff * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29162922Sariff * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30162922Sariff * THE POSSIBILITY OF SUCH DAMAGE.
31162922Sariff */
32230130Smav
33162922Sariff#include <sys/cdefs.h>
34162922Sariff__FBSDID("$FreeBSD: head/sys/netinet/sctputil.h 298223 2016-04-18 20:16:41Z tuexen $");
35162922Sariff
36162922Sariff#ifndef _NETINET_SCTP_UTIL_H_
37162922Sariff#define _NETINET_SCTP_UTIL_H_
38230130Smav
39230130Smav#if defined(_KERNEL) || defined(__Userspace__)
40230130Smav
41230130Smav#define SCTP_READ_LOCK_HELD 1
42230130Smav#define SCTP_READ_LOCK_NOT_HELD 0
43230130Smav
44260351Smav#ifdef SCTP_ASOCLOG_OF_TSNS
45260351Smavvoid sctp_print_out_track_log(struct sctp_tcb *stcb);
46260351Smav
47260351Smav#endif
48260351Smav
49281963Srpaulo#ifdef SCTP_MBUF_LOGGING
50230130Smavstruct mbuf *sctp_m_free(struct mbuf *m);
51230130Smavvoid sctp_m_freem(struct mbuf *m);
52230130Smav
53230130Smav#else
54230130Smav#define sctp_m_free m_free
55230130Smav#define sctp_m_freem m_freem
56230130Smav#endif
57230130Smav
58230130Smav#if defined(SCTP_LOCAL_TRACE_BUF) || defined(__APPLE__)
59230130Smavvoid
60230130Smav     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);
61230130Smav
62261792Shselasky#endif
63230130Smav
64244980Sjfv#define sctp_get_associd(stcb) ((sctp_assoc_t)stcb->asoc.assoc_id)
65244980Sjfv
66275439Smav
67260351Smav/*
68260351Smav * Function prototypes
69260351Smav */
70260351Smavint32_t
71281963Srpaulosctp_map_assoc_state(int);
72230130Smav
73230130Smavuint32_t
74230130Smavsctp_get_ifa_hash_val(struct sockaddr *addr);
75230130Smav
76230130Smavstruct sctp_ifa *
77230130Smav         sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr, int hold_lock);
78230130Smav
79230130Smavstruct sctp_ifa *
80230130Smav         sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock);
81230130Smav
82230130Smavuint32_t sctp_select_initial_TSN(struct sctp_pcb *);
83230130Smav
84230130Smavuint32_t sctp_select_a_tag(struct sctp_inpcb *, uint16_t lport, uint16_t rport, int);
85230130Smav
86230130Smavint sctp_init_asoc(struct sctp_inpcb *, struct sctp_tcb *, uint32_t, uint32_t, uint16_t);
87230130Smav
88230130Smavvoid sctp_fill_random_store(struct sctp_pcb *);
89230130Smav
90230130Smavvoid
91230130Smavsctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin,
92230130Smav    uint16_t numberout, int flag);
93230130Smavvoid
94230312Smav     sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag);
95230312Smav
96230312Smavvoid
97230312Smavsctp_timer_start(int, struct sctp_inpcb *, struct sctp_tcb *,
98230312Smav    struct sctp_nets *);
99230312Smav
100230312Smavvoid
101230312Smavsctp_timer_stop(int, struct sctp_inpcb *, struct sctp_tcb *,
102230130Smav    struct sctp_nets *, uint32_t);
103230130Smav
104230130Smavint
105230130Smav    sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id);
106230312Smav
107230312Smavvoid
108230312Smav     sctp_mtu_size_reset(struct sctp_inpcb *, struct sctp_association *, uint32_t);
109230130Smav
110230130Smavvoid
111230130Smavsctp_wakeup_the_read_socket(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
112230130Smav    int so_locked
113230130Smav#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
114230130Smav    SCTP_UNUSED
115230130Smav#endif
116230130Smav);
117230130Smav
118230130Smavvoid
119230130Smavsctp_add_to_readq(struct sctp_inpcb *inp,
120230130Smav    struct sctp_tcb *stcb,
121230130Smav    struct sctp_queued_to_read *control,
122230130Smav    struct sockbuf *sb,
123230130Smav    int end,
124230130Smav    int inpread_locked,
125230130Smav    int so_locked
126230130Smav#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
127230130Smav    SCTP_UNUSED
128239908Smav#endif
129239908Smav);
130239908Smav
131239908Smavint
132239908Smavsctp_append_to_readq(struct sctp_inpcb *inp,
133239908Smav    struct sctp_tcb *stcb,
134239908Smav    struct sctp_queued_to_read *control,
135239908Smav    struct mbuf *m,
136239908Smav    int end,
137230130Smav    int new_cumack,
138230130Smav    struct sockbuf *sb);
139230130Smav
140230130Smav
141230130Smavvoid sctp_iterator_worker(void);
142230130Smav
143230130Smavuint32_t sctp_get_prev_mtu(uint32_t);
144230130Smavuint32_t sctp_get_next_mtu(uint32_t);
145230130Smav
146230130Smavvoid
147230130Smav     sctp_timeout_handler(void *);
148230130Smav
149230130Smavuint32_t
150230130Smavsctp_calculate_rto(struct sctp_tcb *, struct sctp_association *,
151230130Smav    struct sctp_nets *, struct timeval *, int, int);
152230130Smav
153230130Smavuint32_t sctp_calculate_len(struct mbuf *);
154230130Smav
155230130Smavcaddr_t sctp_m_getptr(struct mbuf *, int, int, uint8_t *);
156230130Smav
157230130Smavstruct sctp_paramhdr *
158230130Smavsctp_get_next_param(struct mbuf *, int,
159230130Smav    struct sctp_paramhdr *, int);
160230130Smav
161264958Smariusstruct mbuf *
162230130Smav     sctp_add_pad_tombuf(struct mbuf *, int);
163230130Smav
164230130Smavstruct mbuf *
165230130Smav     sctp_pad_lastmbuf(struct mbuf *, int, struct mbuf *);
166230130Smav
167230130Smavvoid
168230130Smavsctp_ulp_notify(uint32_t, struct sctp_tcb *, uint32_t, void *, int
169230130Smav#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
170230130Smav    SCTP_UNUSED
171230130Smav#endif
172230130Smav);
173230130Smav
174230130Smavvoid
175230130Smavsctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
176230130Smav    struct sctp_inpcb *new_inp,
177230130Smav    struct sctp_tcb *stcb, int waitflags);
178230130Smav
179230130Smav
180230130Smavvoid sctp_stop_timers_for_shutdown(struct sctp_tcb *);
181230130Smav
182230130Smavvoid
183230130Smavsctp_report_all_outbound(struct sctp_tcb *, uint16_t, int, int
184230130Smav#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
185230130Smav    SCTP_UNUSED
186230130Smav#endif
187230130Smav);
188230130Smav
189230130Smavint sctp_expand_mapping_array(struct sctp_association *, uint32_t);
190230130Smav
191230130Smavvoid
192230130Smavsctp_abort_notification(struct sctp_tcb *, uint8_t, uint16_t,
193230130Smav    struct sctp_abort_chunk *, int
194230130Smav#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
195230130Smav    SCTP_UNUSED
196230130Smav#endif
197230130Smav);
198230130Smav
199230130Smav/* We abort responding to an IP packet for some reason */
200230130Smavvoid
201230130Smavsctp_abort_association(struct sctp_inpcb *, struct sctp_tcb *, struct mbuf *,
202230130Smav    int, struct sockaddr *, struct sockaddr *,
203230130Smav    struct sctphdr *, struct mbuf *,
204230130Smav    uint8_t, uint32_t,
205230130Smav    uint32_t, uint16_t);
206230130Smav
207230130Smav
208230130Smav/* We choose to abort via user input */
209230130Smavvoid
210230130Smavsctp_abort_an_association(struct sctp_inpcb *, struct sctp_tcb *,
211230130Smav    struct mbuf *, int
212230130Smav#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
213230130Smav    SCTP_UNUSED
214230130Smav#endif
215253036Smav);
216230130Smav
217230130Smavvoid
218230130Smavsctp_handle_ootb(struct mbuf *, int, int,
219230130Smav    struct sockaddr *, struct sockaddr *,
220230130Smav    struct sctphdr *, struct sctp_inpcb *,
221230130Smav    struct mbuf *,
222230130Smav    uint8_t, uint32_t, uint16_t,
223230130Smav    uint32_t, uint16_t);
224230130Smav
225230130Smavint
226230130Smavsctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
227230130Smav    int totaddr, int *error);
228230130Smav
229230130Smavstruct sctp_tcb *
230230130Smavsctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
231230130Smav    unsigned int *totaddr, unsigned int *num_v4, unsigned int *num_v6,
232230130Smav    int *error, unsigned int limit, int *bad_addr);
233230130Smav
234230130Smavint sctp_is_there_an_abort_here(struct mbuf *, int, uint32_t *);
235230130Smav
236230130Smav#ifdef INET6
237230130Smavuint32_t sctp_is_same_scope(struct sockaddr_in6 *, struct sockaddr_in6 *);
238247911Sglebius
239247911Sglebiusstruct sockaddr_in6 *
240281963Srpaulo             sctp_recover_scope(struct sockaddr_in6 *, struct sockaddr_in6 *);
241247815Smav
242244145Smav#define sctp_recover_scope_mac(addr, store) do { \
243247911Sglebius	 if ((addr->sin6_family == AF_INET6) && \
244248187Sglebius	     (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr))) { \
245248187Sglebius		*store = *addr; \
246247911Sglebius		if (addr->sin6_scope_id == 0) { \
247248254Sdelphij			if (!sa6_recoverscope(store)) { \
248269546Smarkj				addr = store; \
249230130Smav			} \
250230130Smav		} else { \
251230130Smav			in6_clearscope(&addr->sin6_addr); \
252230130Smav			addr = store; \
253230130Smav		} \
254230130Smav	 } \
255230130Smav} while (0)
256230130Smav#endif
257230130Smav
258230130Smavint sctp_cmpaddr(struct sockaddr *, struct sockaddr *);
259230130Smav
260230130Smavvoid sctp_print_address(struct sockaddr *);
261230130Smav
262230130Smavint
263230130Smavsctp_release_pr_sctp_chunk(struct sctp_tcb *, struct sctp_tmit_chunk *,
264230130Smav    uint8_t, int
265230130Smav#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
266230130Smav    SCTP_UNUSED
267230130Smav#endif
268230130Smav);
269230130Smav
270230130Smavstruct mbuf *sctp_generate_cause(uint16_t, char *);
271230130Smavstruct mbuf *sctp_generate_no_user_data_cause(uint32_t);
272230130Smav
273230130Smavvoid
274230130Smavsctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
275230130Smav    struct sockaddr *sa, sctp_assoc_t assoc_id,
276261792Shselasky    uint32_t vrf_id, int *error, void *p);
277230130Smavvoid
278261792Shselaskysctp_bindx_delete_address(struct sctp_inpcb *inp,
279230130Smav    struct sockaddr *sa, sctp_assoc_t assoc_id,
280230130Smav    uint32_t vrf_id, int *error);
281230130Smav
282230130Smavint sctp_local_addr_count(struct sctp_tcb *stcb);
283230130Smav
284230130Smav#ifdef SCTP_MBCNT_LOGGING
285230130Smavvoid
286230130Smavsctp_free_bufspace(struct sctp_tcb *, struct sctp_association *,
287230130Smav    struct sctp_tmit_chunk *, int);
288230130Smav
289230130Smav#else
290230130Smav#define sctp_free_bufspace(stcb, asoc, tp1, chk_cnt)  \
291230130Smavdo { \
292230130Smav	if (tp1->data != NULL) { \
293230130Smav		atomic_subtract_int(&((asoc)->chunks_on_out_queue), chk_cnt); \
294230130Smav		if ((asoc)->total_output_queue_size >= tp1->book_size) { \
295230130Smav			atomic_subtract_int(&((asoc)->total_output_queue_size), tp1->book_size); \
296230130Smav		} else { \
297230130Smav			(asoc)->total_output_queue_size = 0; \
298230130Smav		} \
299230130Smav		if (stcb->sctp_socket && ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \
300230130Smav		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \
301230130Smav			if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) { \
302230130Smav				atomic_subtract_int(&((stcb)->sctp_socket->so_snd.sb_cc), tp1->book_size); \
303230130Smav			} else { \
304230130Smav				stcb->sctp_socket->so_snd.sb_cc = 0; \
305230130Smav			} \
306230130Smav		} \
307230130Smav	} \
308230130Smav} while (0)
309230130Smav
310230130Smav#endif
311230130Smav
312230130Smav#define sctp_free_spbufspace(stcb, asoc, sp)  \
313230130Smavdo { \
314230130Smav	if (sp->data != NULL) { \
315230130Smav		if ((asoc)->total_output_queue_size >= sp->length) { \
316230130Smav			atomic_subtract_int(&(asoc)->total_output_queue_size, sp->length); \
317230130Smav		} else { \
318230130Smav			(asoc)->total_output_queue_size = 0; \
319230130Smav		} \
320230130Smav		if (stcb->sctp_socket && ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \
321230130Smav		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \
322230130Smav			if (stcb->sctp_socket->so_snd.sb_cc >= sp->length) { \
323230130Smav				atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc,sp->length); \
324230130Smav			} else { \
325230130Smav				stcb->sctp_socket->so_snd.sb_cc = 0; \
326230130Smav			} \
327230130Smav		} \
328230130Smav	} \
329230130Smav} while (0)
330230130Smav
331230130Smav#define sctp_snd_sb_alloc(stcb, sz)  \
332230130Smavdo { \
333230130Smav	atomic_add_int(&stcb->asoc.total_output_queue_size,sz); \
334230130Smav	if ((stcb->sctp_socket != NULL) && \
335230130Smav	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \
336230130Smav	     (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \
337230130Smav		atomic_add_int(&stcb->sctp_socket->so_snd.sb_cc,sz); \
338230130Smav	} \
339230130Smav} while (0)
340230130Smav
341230130Smav/* functions to start/stop udp tunneling */
342230130Smavvoid sctp_over_udp_stop(void);
343281963Srpauloint sctp_over_udp_start(void);
344230130Smav
345230130Smavint
346230130Smavsctp_soreceive(struct socket *so, struct sockaddr **psa,
347230130Smav    struct uio *uio,
348230571Smav    struct mbuf **mp0,
349230571Smav    struct mbuf **controlp,
350230130Smav    int *flagsp);
351230130Smav
352230130Smavvoid
353230130Smav     sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d);
354230130Smav
355230130Smavvoid
356230130Smavsctp_wakeup_log(struct sctp_tcb *stcb,
357230130Smav    uint32_t wake_cnt, int from);
358230130Smav
359230130Smavvoid sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t, uint16_t, uint16_t, int);
360230130Smav
361230130Smavvoid sctp_log_nagle_event(struct sctp_tcb *stcb, int action);
362230130Smav
363230768Smav
364230571Smav#ifdef SCTP_MBUF_LOGGING
365230571Smavvoid
366230571Smav     sctp_log_mb(struct mbuf *m, int from);
367230571Smav
368230571Smavvoid
369230571Smav     sctp_log_mbc(struct mbuf *m, int from);
370230571Smav
371230571Smav#endif
372230571Smav
373230571Smavvoid
374230571Smavsctp_sblog(struct sockbuf *sb,
375230130Smav    struct sctp_tcb *stcb, int from, int incr);
376230130Smav
377230130Smavvoid
378230130Smavsctp_log_strm_del(struct sctp_queued_to_read *control,
379230130Smav    struct sctp_queued_to_read *poschk,
380230130Smav    int from);
381230130Smavvoid sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *, int, uint8_t);
382230130Smavvoid rto_logging(struct sctp_nets *net, int from);
383230130Smav
384230130Smavvoid sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc);
385230130Smav
386230130Smavvoid sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from);
387230130Smavvoid sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *, int, int, uint8_t);
388230130Smavvoid sctp_log_block(uint8_t, struct sctp_association *, size_t);
389230130Smavvoid sctp_log_rwnd(uint8_t, uint32_t, uint32_t, uint32_t);
390230130Smavvoid sctp_log_rwnd_set(uint8_t, uint32_t, uint32_t, uint32_t, uint32_t);
391230130Smavint sctp_fill_stat_log(void *, size_t *);
392230130Smavvoid sctp_log_fr(uint32_t, uint32_t, uint32_t, int);
393230130Smavvoid sctp_log_sack(uint32_t, uint32_t, uint32_t, uint16_t, uint16_t, int);
394230130Smavvoid sctp_log_map(uint32_t, uint32_t, uint32_t, int);
395230130Smavvoid sctp_print_mapping_array(struct sctp_association *asoc);
396230571Smavvoid sctp_clr_stat_log(void);
397230571Smav
398230130Smav
399230130Smav#ifdef SCTP_AUDITING_ENABLED
400230571Smavvoid
401230571Smavsctp_auditing(int, struct sctp_inpcb *, struct sctp_tcb *,
402230571Smav    struct sctp_nets *);
403230571Smavvoid sctp_audit_log(uint8_t, uint8_t);
404230130Smav
405230130Smav#endif
406230130Smav#endif				/* _KERNEL */
407230130Smav#endif
408230130Smav