1 /*
2  * Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 #ifndef SUN_NIO_CH_SCTP_H
27 #define SUN_NIO_CH_SCTP_H
28 
29 #if defined(__linux__)
30 #include <stdint.h>
31 #include <linux/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include "jni.h"
35 
36 //Causes compiler error if not found, should make warning and uncomment
37 /*#include <netinet/sctp.h>*/
38 
39 #ifndef IPPROTO_SCTP
40 #define IPPROTO_SCTP    132
41 #endif
42 
43 /* The current version of lksctp does
44  * not define the following option that the Java API (optionally) supports */
45 #ifndef SCTP_EXPLICIT_EOR
46 #define SCTP_EXPLICIT_EOR -1
47 #endif
48 
49 /* Definitions taken from lksctp-tools-1.0.8/src/include/netinet/sctp.h */
50 #ifndef SCTP_INITMSG
51 
52 enum sctp_optname {
53         SCTP_RTOINFO,
54 #define SCTP_RTOINFO SCTP_RTOINFO
55         SCTP_ASSOCINFO,
56 #define SCTP_ASSOCINFO SCTP_ASSOCINFO
57         SCTP_INITMSG,
58 #define SCTP_INITMSG SCTP_INITMSG
59         SCTP_NODELAY,   /* Get/set nodelay option. */
60 #define SCTP_NODELAY    SCTP_NODELAY
61         SCTP_AUTOCLOSE,
62 #define SCTP_AUTOCLOSE SCTP_AUTOCLOSE
63         SCTP_SET_PEER_PRIMARY_ADDR,
64 #define SCTP_SET_PEER_PRIMARY_ADDR SCTP_SET_PEER_PRIMARY_ADDR
65         SCTP_PRIMARY_ADDR,
66 #define SCTP_PRIMARY_ADDR SCTP_PRIMARY_ADDR
67         SCTP_ADAPTATION_LAYER,
68 #define SCTP_ADAPTATION_LAYER SCTP_ADAPTATION_LAYER
69         SCTP_DISABLE_FRAGMENTS,
70 #define SCTP_DISABLE_FRAGMENTS SCTP_DISABLE_FRAGMENTS
71         SCTP_PEER_ADDR_PARAMS,
72 #define SCTP_PEER_ADDR_PARAMS SCTP_PEER_ADDR_PARAMS
73         SCTP_DEFAULT_SEND_PARAM,
74 #define SCTP_DEFAULT_SEND_PARAM SCTP_DEFAULT_SEND_PARAM
75         SCTP_EVENTS,
76 #define SCTP_EVENTS SCTP_EVENTS
77         SCTP_I_WANT_MAPPED_V4_ADDR,  /* Turn on/off mapped v4 addresses  */
78 #define SCTP_I_WANT_MAPPED_V4_ADDR SCTP_I_WANT_MAPPED_V4_ADDR
79         SCTP_MAXSEG,    /* Get/set maximum fragment. */
80 #define SCTP_MAXSEG     SCTP_MAXSEG
81         SCTP_STATUS,
82 #define SCTP_STATUS SCTP_STATUS
83         SCTP_GET_PEER_ADDR_INFO,
84 #define SCTP_GET_PEER_ADDR_INFO SCTP_GET_PEER_ADDR_INFO
85         SCTP_DELAYED_ACK_TIME,
86 #define SCTP_DELAYED_ACK_TIME SCTP_DELAYED_ACK_TIME
87         SCTP_CONTEXT,   /* Receive Context */
88 #define SCTP_CONTEXT SCTP_CONTEXT
89         SCTP_FRAGMENT_INTERLEAVE,
90 #define SCTP_FRAGMENT_INTERLEAVE SCTP_FRAGMENT_INTERLEAVE
91         SCTP_PARTIAL_DELIVERY_POINT,    /* Set/Get partial delivery point */
92 #define SCTP_PARTIAL_DELIVERY_POINT SCTP_PARTIAL_DELIVERY_POINT
93         SCTP_MAX_BURST,         /* Set/Get max burst */
94 #define SCTP_MAX_BURST SCTP_MAX_BURST
95 };
96 
97 enum sctp_sac_state {
98         SCTP_COMM_UP,
99         SCTP_COMM_LOST,
100         SCTP_RESTART,
101         SCTP_SHUTDOWN_COMP,
102         SCTP_CANT_STR_ASSOC,
103 };
104 
105 enum sctp_spc_state {
106         SCTP_ADDR_AVAILABLE,
107         SCTP_ADDR_UNREACHABLE,
108         SCTP_ADDR_REMOVED,
109         SCTP_ADDR_ADDED,
110         SCTP_ADDR_MADE_PRIM,
111         SCTP_ADDR_CONFIRMED,
112 };
113 
114 enum sctp_sinfo_flags {
115         SCTP_UNORDERED = 1,  /* Send/receive message unordered. */
116         SCTP_ADDR_OVER = 2,  /* Override the primary destination. */
117         SCTP_ABORT=4,        /* Send an ABORT message to the peer. */
118         SCTP_EOF=MSG_FIN,    /* Initiate graceful shutdown process. */
119 };
120 
121 enum sctp_sn_type {
122         SCTP_SN_TYPE_BASE     = (1<<15),
123         SCTP_ASSOC_CHANGE,
124         SCTP_PEER_ADDR_CHANGE,
125         SCTP_SEND_FAILED,
126         SCTP_REMOTE_ERROR,
127         SCTP_SHUTDOWN_EVENT,
128         SCTP_PARTIAL_DELIVERY_EVENT,
129         SCTP_ADAPTATION_INDICATION,
130 };
131 
132 typedef enum sctp_cmsg_type {
133         SCTP_INIT,              /* 5.2.1 SCTP Initiation Structure */
134 #define SCTP_INIT SCTP_INIT
135         SCTP_SNDRCV,            /* 5.2.2 SCTP Header Information Structure */
136 #define SCTP_SNDRCV SCTP_SNDRCV
137 } sctp_cmsg_t;
138 
139 enum sctp_msg_flags {
140         MSG_NOTIFICATION = 0x8000,
141 #define MSG_NOTIFICATION MSG_NOTIFICATION
142 };
143 
144 #define SCTP_BINDX_ADD_ADDR 0x01
145 #define SCTP_BINDX_REM_ADDR 0x02
146 
147 typedef __s32 sctp_assoc_t;
148 
149 struct sctp_initmsg {
150         __u16 sinit_num_ostreams;
151         __u16 sinit_max_instreams;
152         __u16 sinit_max_attempts;
153         __u16 sinit_max_init_timeo;
154 };
155 
156 struct sctp_sndrcvinfo {
157         __u16 sinfo_stream;
158         __u16 sinfo_ssn;
159         __u16 sinfo_flags;
160         __u32 sinfo_ppid;
161         __u32 sinfo_context;
162         __u32 sinfo_timetolive;
163         __u32 sinfo_tsn;
164         __u32 sinfo_cumtsn;
165         sctp_assoc_t sinfo_assoc_id;
166 };
167 
168 struct sctp_event_subscribe {
169         __u8 sctp_data_io_event;
170         __u8 sctp_association_event;
171         __u8 sctp_address_event;
172         __u8 sctp_send_failure_event;
173         __u8 sctp_peer_error_event;
174         __u8 sctp_shutdown_event;
175         __u8 sctp_partial_delivery_event;
176         __u8 sctp_adaptation_layer_event;
177 };
178 
179 struct sctp_send_failed {
180         __u16 ssf_type;
181         __u16 ssf_flags;
182         __u32 ssf_length;
183         __u32 ssf_error;
184         struct sctp_sndrcvinfo ssf_info;
185         sctp_assoc_t ssf_assoc_id;
186         __u8 ssf_data[0];
187 };
188 
189 struct sctp_assoc_change {
190         __u16 sac_type;
191         __u16 sac_flags;
192         __u32 sac_length;
193         __u16 sac_state;
194         __u16 sac_error;
195         __u16 sac_outbound_streams;
196         __u16 sac_inbound_streams;
197         sctp_assoc_t sac_assoc_id;
198         __u8 sac_info[0];
199 };
200 
201 struct sctp_shutdown_event {
202         __u16 sse_type;
203         __u16 sse_flags;
204         __u32 sse_length;
205         sctp_assoc_t sse_assoc_id;
206 };
207 
208 struct sctp_paddr_change {
209         __u16 spc_type;
210         __u16 spc_flags;
211         __u32 spc_length;
212         struct sockaddr_storage spc_aaddr;
213         int spc_state;
214         int spc_error;
215         sctp_assoc_t spc_assoc_id;
216 } __attribute__((packed, aligned(4)));
217 
218 struct sctp_remote_error {
219         __u16 sre_type;
220         __u16 sre_flags;
221         __u32 sre_length;
222         __u16 sre_error;
223         sctp_assoc_t sre_assoc_id;
224         __u8 sre_data[0];
225 };
226 
227 struct sctp_adaptation_event {
228         __u16 sai_type;
229         __u16 sai_flags;
230         __u32 sai_length;
231         __u32 sai_adaptation_ind;
232         sctp_assoc_t sai_assoc_id;
233 };
234 
235 struct sctp_setprim {
236         sctp_assoc_t            ssp_assoc_id;
237         struct sockaddr_storage ssp_addr;
238 } __attribute__((packed, aligned(4)));
239 
240 struct sctp_setpeerprim {
241         sctp_assoc_t            sspp_assoc_id;
242         struct sockaddr_storage sspp_addr;
243 } __attribute__((packed, aligned(4)));
244 
245 
246 struct sctp_pdapi_event {
247         __u16 pdapi_type;
248         __u16 pdapi_flags;
249         __u32 pdapi_length;
250         __u32 pdapi_indication;
251         sctp_assoc_t pdapi_assoc_id;
252 };
253 
254 union sctp_notification {
255         struct {
256                 __u16 sn_type;             /* Notification type. */
257                 __u16 sn_flags;
258                 __u32 sn_length;
259         } sn_header;
260         struct sctp_assoc_change sn_assoc_change;
261         struct sctp_paddr_change sn_paddr_change;
262         struct sctp_remote_error sn_remote_error;
263         struct sctp_send_failed sn_send_failed;
264         struct sctp_shutdown_event sn_shutdown_event;
265         struct sctp_adaptation_event sn_adaptation_event;
266         struct sctp_pdapi_event sn_pdapi_event;
267 };
268 
269 #endif /* SCTP_INITMSG */
270 
271 /* Function types to support dynamic linking of socket API extension functions
272  * for SCTP. This is so that there is no linkage depandancy during build or
273  * runtime for libsctp.*/
274 typedef int sctp_getladdrs_func(int sd, sctp_assoc_t id, struct sockaddr **addrs);
275 typedef int sctp_freeladdrs_func(struct sockaddr *addrs);
276 typedef int sctp_getpaddrs_func(int sd, sctp_assoc_t id, struct sockaddr **addrs);
277 typedef int sctp_freepaddrs_func(struct sockaddr *addrs);
278 typedef int sctp_bindx_func(int sd, struct sockaddr *addrs, int addrcnt, int flags);
279 typedef int sctp_peeloff_func(int sock, sctp_assoc_t id);
280 
281 #elif defined(__FreeBSD__)
282 
283 #include <stdint.h>
284 #include <sys/types.h>
285 #include <sys/socket.h>
286 #include <netinet/sctp.h>
287 #include <netinet/sctp_peeloff.h>
288 #include <netinet/sctp_uio.h>
289 #include "jni.h"
290 
291 #endif /* __linux__ */
292 
293 #ifndef __FreeBSD__
294 
295 extern sctp_getladdrs_func* nio_sctp_getladdrs;
296 extern sctp_freeladdrs_func* nio_sctp_freeladdrs;
297 extern sctp_getpaddrs_func* nio_sctp_getpaddrs;
298 extern sctp_freepaddrs_func* nio_sctp_freepaddrs;
299 extern sctp_bindx_func* nio_sctp_bindx;
300 extern sctp_peeloff_func* nio_sctp_peeloff;
301 
302 #else
303 
304 #define nio_sctp_getladdrs     sctp_getladdrs
305 #define nio_sctp_freeladdrs    sctp_freeladdrs
306 #define nio_sctp_getpaddrs     sctp_getpaddrs
307 #define nio_sctp_freepaddrs    sctp_freepaddrs
308 #define nio_sctp_bindx         sctp_bindx
309 #define nio_sctp_peeloff       sctp_peeloff
310 
311 #endif
312 
313 jboolean loadSocketExtensionFuncs(JNIEnv* env);
314 
315 #endif /* !SUN_NIO_CH_SCTP_H */
316