1163953Srrs/*-
2169382Srrs * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3235828Stuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4235828Stuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5163953Srrs *
6163953Srrs * Redistribution and use in source and binary forms, with or without
7163953Srrs * modification, are permitted provided that the following conditions are met:
8163953Srrs *
9163953Srrs * a) Redistributions of source code must retain the above copyright notice,
10228653Stuexen *    this list of conditions and the following disclaimer.
11163953Srrs *
12163953Srrs * b) Redistributions in binary form must reproduce the above copyright
13163953Srrs *    notice, this list of conditions and the following disclaimer in
14228653Stuexen *    the documentation and/or other materials provided with the distribution.
15163953Srrs *
16163953Srrs * c) Neither the name of Cisco Systems, Inc. nor the names of its
17163953Srrs *    contributors may be used to endorse or promote products derived
18163953Srrs *    from this software without specific prior written permission.
19163953Srrs *
20163953Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21163953Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22163953Srrs * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23163953Srrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24163953Srrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25163953Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26163953Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27163953Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28163953Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29163953Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30163953Srrs * THE POSSIBILITY OF SUCH DAMAGE.
31163953Srrs */
32163953Srrs
33163953Srrs#include <sys/cdefs.h>
34163953Srrs__FBSDID("$FreeBSD: stable/11/sys/netinet/sctputil.h 364651 2020-08-24 09:46:36Z tuexen $");
35163953Srrs
36235828Stuexen#ifndef _NETINET_SCTP_UTIL_H_
37235828Stuexen#define _NETINET_SCTP_UTIL_H_
38163953Srrs
39180387Srrs#if defined(_KERNEL) || defined(__Userspace__)
40163953Srrs
41195918Srrs#define SCTP_READ_LOCK_HELD 1
42195918Srrs#define SCTP_READ_LOCK_NOT_HELD 0
43168755Srrs
44168859Srrs#ifdef SCTP_ASOCLOG_OF_TSNS
45168859Srrsvoid sctp_print_out_track_log(struct sctp_tcb *stcb);
46168859Srrs#endif
47168859Srrs
48163953Srrs#ifdef SCTP_MBUF_LOGGING
49163953Srrsstruct mbuf *sctp_m_free(struct mbuf *m);
50163953Srrsvoid sctp_m_freem(struct mbuf *m);
51163953Srrs#else
52163953Srrs#define sctp_m_free m_free
53163953Srrs#define sctp_m_freem m_freem
54163953Srrs#endif
55163953Srrs
56171943Srrs#if defined(SCTP_LOCAL_TRACE_BUF) || defined(__APPLE__)
57171943Srrsvoid
58172157Srrs     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);
59171943Srrs#endif
60171943Srrs
61163953Srrs#define sctp_get_associd(stcb) ((sctp_assoc_t)stcb->asoc.assoc_id)
62163953Srrs
63163953Srrs
64163953Srrs/*
65163953Srrs * Function prototypes
66163953Srrs */
67287282Stuexenint32_t
68310773Stuexen        sctp_map_assoc_state(int);
69287282Stuexen
70167698Srrsuint32_t
71310773Stuexen         sctp_get_ifa_hash_val(struct sockaddr *addr);
72167698Srrs
73347154Stuexenstruct sctp_ifa *sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr, int hold_lock);
74163953Srrs
75347154Stuexenstruct sctp_ifa *sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock);
76167598Srrs
77163953Srrsuint32_t sctp_select_initial_TSN(struct sctp_pcb *);
78163953Srrs
79185694Srrsuint32_t sctp_select_a_tag(struct sctp_inpcb *, uint16_t lport, uint16_t rport, int);
80163953Srrs
81291904Stuexenint sctp_init_asoc(struct sctp_inpcb *, struct sctp_tcb *, uint32_t, uint32_t, uint16_t);
82163953Srrs
83163953Srrsvoid sctp_fill_random_store(struct sctp_pcb *);
84163953Srrs
85169420Srrsvoid
86233660Srrssctp_notify_stream_reset_add(struct sctp_tcb *stcb, uint16_t numberin,
87233660Srrs    uint16_t numberout, int flag);
88233660Srrsvoid
89233660Srrs     sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, uint32_t sending_tsn, uint32_t recv_tsn, int flag);
90233660Srrs
91233660Srrsvoid
92163953Srrssctp_timer_start(int, struct sctp_inpcb *, struct sctp_tcb *,
93163953Srrs    struct sctp_nets *);
94163953Srrs
95169378Srrsvoid
96163953Srrssctp_timer_stop(int, struct sctp_inpcb *, struct sctp_tcb *,
97165220Srrs    struct sctp_nets *, uint32_t);
98163953Srrs
99167598Srrsint
100167598Srrs    sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id);
101167598Srrs
102163953Srrsvoid
103166023Srrs     sctp_mtu_size_reset(struct sctp_inpcb *, struct sctp_association *, uint32_t);
104163953Srrs
105163953Srrsvoid
106298223Stuexensctp_wakeup_the_read_socket(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
107298223Stuexen    int so_locked
108298223Stuexen#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
109298223Stuexen    SCTP_UNUSED
110298223Stuexen#endif
111298223Stuexen);
112297662Srrs
113297662Srrsvoid
114163953Srrssctp_add_to_readq(struct sctp_inpcb *inp,
115163953Srrs    struct sctp_tcb *stcb,
116163953Srrs    struct sctp_queued_to_read *control,
117163953Srrs    struct sockbuf *sb,
118172090Srrs    int end,
119195918Srrs    int inpread_locked,
120172090Srrs    int so_locked
121172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
122172090Srrs    SCTP_UNUSED
123172090Srrs#endif
124172090Srrs);
125163953Srrs
126167598Srrsvoid sctp_iterator_worker(void);
127167598Srrs
128214939Stuexenuint32_t sctp_get_prev_mtu(uint32_t);
129228653Stuexenuint32_t sctp_get_next_mtu(uint32_t);
130163953Srrs
131163953Srrsvoid
132163953Srrs     sctp_timeout_handler(void *);
133163953Srrs
134360735Stuexenint
135163953Srrssctp_calculate_rto(struct sctp_tcb *, struct sctp_association *,
136332212Stuexen    struct sctp_nets *, struct timeval *, int);
137163953Srrs
138163953Srrsuint32_t sctp_calculate_len(struct mbuf *);
139163953Srrs
140163953Srrscaddr_t sctp_m_getptr(struct mbuf *, int, int, uint8_t *);
141163953Srrs
142163953Srrsstruct sctp_paramhdr *
143163953Srrssctp_get_next_param(struct mbuf *, int,
144163953Srrs    struct sctp_paramhdr *, int);
145163953Srrs
146347154Stuexenstruct mbuf *sctp_add_pad_tombuf(struct mbuf *, int);
147163953Srrs
148347154Stuexenstruct mbuf *sctp_pad_lastmbuf(struct mbuf *, int, struct mbuf *);
149163953Srrs
150347154Stuexenvoid
151172090Srrssctp_ulp_notify(uint32_t, struct sctp_tcb *, uint32_t, void *, int
152172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
153172090Srrs    SCTP_UNUSED
154172090Srrs#endif
155172090Srrs);
156163953Srrs
157163953Srrsvoid
158163953Srrssctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
159163953Srrs    struct sctp_inpcb *new_inp,
160169208Srrs    struct sctp_tcb *stcb, int waitflags);
161163953Srrs
162163953Srrs
163163953Srrsvoid sctp_stop_timers_for_shutdown(struct sctp_tcb *);
164163953Srrs
165360761Stuexen/* Stop all timers for association and remote addresses. */
166360761Stuexenvoid sctp_stop_association_timers(struct sctp_tcb *, bool);
167360761Stuexen
168347154Stuexenvoid
169364651Stuexensctp_report_all_outbound(struct sctp_tcb *, uint16_t, int
170172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
171172090Srrs    SCTP_UNUSED
172172090Srrs#endif
173172090Srrs);
174163953Srrs
175170138Srrsint sctp_expand_mapping_array(struct sctp_association *, uint32_t);
176163953Srrs
177347154Stuexenvoid
178235403Stuexensctp_abort_notification(struct sctp_tcb *, uint8_t, uint16_t,
179235360Stuexen    struct sctp_abort_chunk *, int
180172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
181172090Srrs    SCTP_UNUSED
182172090Srrs#endif
183172090Srrs);
184163953Srrs
185163953Srrs/* We abort responding to an IP packet for some reason */
186163953Srrsvoid
187237049Stuexensctp_abort_association(struct sctp_inpcb *, struct sctp_tcb *, struct mbuf *,
188237715Stuexen    int, struct sockaddr *, struct sockaddr *,
189237715Stuexen    struct sctphdr *, struct mbuf *,
190237049Stuexen    uint8_t, uint32_t,
191237049Stuexen    uint32_t, uint16_t);
192163953Srrs
193169352Srrs
194163953Srrs/* We choose to abort via user input */
195163953Srrsvoid
196235360Stuexensctp_abort_an_association(struct sctp_inpcb *, struct sctp_tcb *,
197172090Srrs    struct mbuf *, int
198172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
199172090Srrs    SCTP_UNUSED
200172090Srrs#endif
201172090Srrs);
202163953Srrs
203347154Stuexenvoid
204237715Stuexensctp_handle_ootb(struct mbuf *, int, int,
205237715Stuexen    struct sockaddr *, struct sockaddr *,
206237715Stuexen    struct sctphdr *, struct sctp_inpcb *,
207263237Stuexen    struct mbuf *,
208284515Stuexen    uint8_t, uint32_t, uint16_t,
209237049Stuexen    uint32_t, uint16_t);
210163953Srrs
211347154Stuexenint
212170056Srrssctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
213170056Srrs    int totaddr, int *error);
214169352Srrs
215348174Stuexenint
216348174Stuexensctp_connectx_helper_find(struct sctp_inpcb *, struct sockaddr *,
217348174Stuexen    unsigned int, unsigned int *, unsigned int *, unsigned int);
218169352Srrs
219163953Srrsint sctp_is_there_an_abort_here(struct mbuf *, int, uint32_t *);
220178251Srrs#ifdef INET6
221163953Srrsuint32_t sctp_is_same_scope(struct sockaddr_in6 *, struct sockaddr_in6 *);
222166675Srrs
223347154Stuexenstruct sockaddr_in6 *sctp_recover_scope(struct sockaddr_in6 *, struct sockaddr_in6 *);
224163953Srrs
225163953Srrs#define sctp_recover_scope_mac(addr, store) do { \
226166675Srrs	 if ((addr->sin6_family == AF_INET6) && \
227166675Srrs	     (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr))) { \
228166675Srrs		*store = *addr; \
229166675Srrs		if (addr->sin6_scope_id == 0) { \
230166675Srrs			if (!sa6_recoverscope(store)) { \
231166675Srrs				addr = store; \
232166675Srrs			} \
233166675Srrs		} else { \
234166675Srrs			in6_clearscope(&addr->sin6_addr); \
235166675Srrs			addr = store; \
236166675Srrs		} \
237166675Srrs	 } \
238166675Srrs} while (0)
239178251Srrs#endif
240163953Srrs
241163953Srrsint sctp_cmpaddr(struct sockaddr *, struct sockaddr *);
242163953Srrs
243163953Srrsvoid sctp_print_address(struct sockaddr *);
244163953Srrs
245163953Srrsint
246163953Srrssctp_release_pr_sctp_chunk(struct sctp_tcb *, struct sctp_tmit_chunk *,
247235416Stuexen    uint8_t, int
248172090Srrs#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
249172090Srrs    SCTP_UNUSED
250172090Srrs#endif
251172090Srrs);
252163953Srrs
253263237Stuexenstruct mbuf *sctp_generate_cause(uint16_t, char *);
254264679Stuexenstruct mbuf *sctp_generate_no_user_data_cause(uint32_t);
255163953Srrs
256347154Stuexenvoid
257170606Srrssctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
258170606Srrs    struct sockaddr *sa, sctp_assoc_t assoc_id,
259170606Srrs    uint32_t vrf_id, int *error, void *p);
260347154Stuexenvoid
261228653Stuexensctp_bindx_delete_address(struct sctp_inpcb *inp,
262170606Srrs    struct sockaddr *sa, sctp_assoc_t assoc_id,
263170606Srrs    uint32_t vrf_id, int *error);
264163953Srrs
265171572Srrsint sctp_local_addr_count(struct sctp_tcb *stcb);
266171572Srrs
267163953Srrs#ifdef SCTP_MBCNT_LOGGING
268163953Srrsvoid
269163953Srrssctp_free_bufspace(struct sctp_tcb *, struct sctp_association *,
270170744Srrs    struct sctp_tmit_chunk *, int);
271163953Srrs
272163953Srrs#else
273163953Srrs#define sctp_free_bufspace(stcb, asoc, tp1, chk_cnt)  \
274163953Srrsdo { \
275163953Srrs	if (tp1->data != NULL) { \
276268537Stuexen		atomic_subtract_int(&((asoc)->chunks_on_out_queue), chk_cnt); \
277163953Srrs		if ((asoc)->total_output_queue_size >= tp1->book_size) { \
278169420Srrs			atomic_subtract_int(&((asoc)->total_output_queue_size), tp1->book_size); \
279163953Srrs		} else { \
280163953Srrs			(asoc)->total_output_queue_size = 0; \
281163953Srrs		} \
282268537Stuexen		if (stcb->sctp_socket && ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \
283268537Stuexen		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \
284275427Stuexen			if (stcb->sctp_socket->so_snd.sb_cc >= tp1->book_size) { \
285275427Stuexen				atomic_subtract_int(&((stcb)->sctp_socket->so_snd.sb_cc), tp1->book_size); \
286163953Srrs			} else { \
287275427Stuexen				stcb->sctp_socket->so_snd.sb_cc = 0; \
288163953Srrs			} \
289163953Srrs		} \
290268537Stuexen	} \
291163953Srrs} while (0)
292163953Srrs
293163953Srrs#endif
294163953Srrs
295163953Srrs#define sctp_free_spbufspace(stcb, asoc, sp)  \
296163953Srrsdo { \
297268537Stuexen	if (sp->data != NULL) { \
298163953Srrs		if ((asoc)->total_output_queue_size >= sp->length) { \
299169420Srrs			atomic_subtract_int(&(asoc)->total_output_queue_size, sp->length); \
300163953Srrs		} else { \
301163953Srrs			(asoc)->total_output_queue_size = 0; \
302163953Srrs		} \
303268537Stuexen		if (stcb->sctp_socket && ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \
304268537Stuexen		    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \
305275427Stuexen			if (stcb->sctp_socket->so_snd.sb_cc >= sp->length) { \
306275427Stuexen				atomic_subtract_int(&stcb->sctp_socket->so_snd.sb_cc,sp->length); \
307163953Srrs			} else { \
308275427Stuexen				stcb->sctp_socket->so_snd.sb_cc = 0; \
309163953Srrs			} \
310163953Srrs		} \
311268537Stuexen	} \
312163953Srrs} while (0)
313163953Srrs
314163953Srrs#define sctp_snd_sb_alloc(stcb, sz)  \
315163953Srrsdo { \
316163953Srrs	atomic_add_int(&stcb->asoc.total_output_queue_size,sz); \
317163953Srrs	if ((stcb->sctp_socket != NULL) && \
318163953Srrs	    ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) || \
319163953Srrs	     (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) { \
320275427Stuexen		atomic_add_int(&stcb->sctp_socket->so_snd.sb_cc,sz); \
321163953Srrs	} \
322163953Srrs} while (0)
323163953Srrs
324221328Stuexen/* functions to start/stop udp tunneling */
325179157Srrsvoid sctp_over_udp_stop(void);
326179157Srrsint sctp_over_udp_start(void);
327163953Srrs
328163953Srrsint
329163953Srrssctp_soreceive(struct socket *so, struct sockaddr **psa,
330163953Srrs    struct uio *uio,
331163953Srrs    struct mbuf **mp0,
332163953Srrs    struct mbuf **controlp,
333163953Srrs    int *flagsp);
334163953Srrs
335163953Srrsvoid
336163953Srrs     sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d);
337163953Srrs
338163953Srrsvoid
339163953Srrssctp_wakeup_log(struct sctp_tcb *stcb,
340163953Srrs    uint32_t wake_cnt, int from);
341163953Srrs
342164181Srrsvoid sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t, uint16_t, uint16_t, int);
343163953Srrs
344163953Srrsvoid sctp_log_nagle_event(struct sctp_tcb *stcb, int action);
345163953Srrs
346163953Srrs
347276914Stuexen#ifdef SCTP_MBUF_LOGGING
348163953Srrsvoid
349163953Srrs     sctp_log_mb(struct mbuf *m, int from);
350163953Srrs
351163953Srrsvoid
352276914Stuexen     sctp_log_mbc(struct mbuf *m, int from);
353276914Stuexen#endif
354276914Stuexen
355276914Stuexenvoid
356163953Srrssctp_sblog(struct sockbuf *sb,
357163953Srrs    struct sctp_tcb *stcb, int from, int incr);
358163953Srrs
359163953Srrsvoid
360163953Srrssctp_log_strm_del(struct sctp_queued_to_read *control,
361163953Srrs    struct sctp_queued_to_read *poschk,
362163953Srrs    int from);
363163953Srrsvoid sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *, int, uint8_t);
364163953Srrsvoid rto_logging(struct sctp_nets *net, int from);
365163953Srrs
366163953Srrsvoid sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc);
367163953Srrs
368163953Srrsvoid sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from);
369163953Srrsvoid sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *, int, int, uint8_t);
370347676Stuexenvoid sctp_log_block(uint8_t, struct sctp_association *, ssize_t);
371163953Srrsvoid sctp_log_rwnd(uint8_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
388319408Stuexen#if defined(INET6) || defined(INET)
389319408Stuexenuint32_t sctp_min_mtu(uint32_t, uint32_t, uint32_t);
390319408Stuexenvoid sctp_hc_set_mtu(union sctp_sockstore *, uint16_t, uint32_t);
391319408Stuexenuint32_t sctp_hc_get_mtu(union sctp_sockstore *, uint16_t);
392319408Stuexen#endif
393163953Srrs#endif				/* _KERNEL */
394163953Srrs#endif
395