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 #ifdef __solaris__
30 
31 #define _XPG4_2
32 #define __EXTENSIONS__
33 #include <sys/socket.h>
34 #include <netinet/sctp.h>
35 #include "jni.h"
36 
37 /* Current Solaris headers don't comply with draft rfc */
38 #ifndef SCTP_EOF
39 #define SCTP_EOF MSG_EOF
40 #endif
41 
42 #ifndef SCTP_UNORDERED
43 #define SCTP_UNORDERED MSG_UNORDERED
44 #endif
45 
46 /* The current version of the socket API extension shipped with Solaris does
47  * not define the following options that the Java API (optionally) supports */
48 #ifndef SCTP_EXPLICIT_EOR
49 #define SCTP_EXPLICIT_EOR -1
50 #endif
51 #ifndef SCTP_FRAGMENT_INTERLEAVE
52 #define SCTP_FRAGMENT_INTERLEAVE -1
53 #endif
54 #ifndef SCTP_SET_PEER_PRIMARY_ADDR
55 #define SCTP_SET_PEER_PRIMARY_ADDR -1
56 #endif
57 
58 /* Function types to support dynamic linking of socket API extension functions
59  * for SCTP. This is so that there is no linkage depandancy during build or
60  * runtime for libsctp.*/
61 typedef int sctp_getladdrs_func(int sock, sctp_assoc_t id, void **addrs);
62 typedef int sctp_freeladdrs_func(void* addrs);
63 typedef int sctp_getpaddrs_func(int sock, sctp_assoc_t id, void **addrs);
64 typedef int sctp_freepaddrs_func(void *addrs);
65 typedef int sctp_bindx_func(int sock, void *addrs, int addrcnt, int flags);
66 typedef int sctp_peeloff_func(int sock, sctp_assoc_t id);
67 
68 
69 
70 #elif defined(__linux__)
71 #include <stdint.h>
72 #include <linux/types.h>
73 #include <sys/socket.h>
74 #include <netinet/in.h>
75 #include "jni.h"
76 
77 //Causes compiler error if not found, should make warning and uncomment
78 /*#include <netinet/sctp.h>*/
79 
80 #ifndef IPPROTO_SCTP
81 #define IPPROTO_SCTP    132
82 #endif
83 
84 /* The current version of lksctp does
85  * not define the following option that the Java API (optionally) supports */
86 #ifndef SCTP_EXPLICIT_EOR
87 #define SCTP_EXPLICIT_EOR -1
88 #endif
89 
90 /* Definitions taken from lksctp-tools-1.0.8/src/include/netinet/sctp.h */
91 #ifndef SCTP_INITMSG
92 
93 enum sctp_optname {
94         SCTP_RTOINFO,
95 #define SCTP_RTOINFO SCTP_RTOINFO
96         SCTP_ASSOCINFO,
97 #define SCTP_ASSOCINFO SCTP_ASSOCINFO
98         SCTP_INITMSG,
99 #define SCTP_INITMSG SCTP_INITMSG
100         SCTP_NODELAY,   /* Get/set nodelay option. */
101 #define SCTP_NODELAY    SCTP_NODELAY
102         SCTP_AUTOCLOSE,
103 #define SCTP_AUTOCLOSE SCTP_AUTOCLOSE
104         SCTP_SET_PEER_PRIMARY_ADDR,
105 #define SCTP_SET_PEER_PRIMARY_ADDR SCTP_SET_PEER_PRIMARY_ADDR
106         SCTP_PRIMARY_ADDR,
107 #define SCTP_PRIMARY_ADDR SCTP_PRIMARY_ADDR
108         SCTP_ADAPTATION_LAYER,
109 #define SCTP_ADAPTATION_LAYER SCTP_ADAPTATION_LAYER
110         SCTP_DISABLE_FRAGMENTS,
111 #define SCTP_DISABLE_FRAGMENTS SCTP_DISABLE_FRAGMENTS
112         SCTP_PEER_ADDR_PARAMS,
113 #define SCTP_PEER_ADDR_PARAMS SCTP_PEER_ADDR_PARAMS
114         SCTP_DEFAULT_SEND_PARAM,
115 #define SCTP_DEFAULT_SEND_PARAM SCTP_DEFAULT_SEND_PARAM
116         SCTP_EVENTS,
117 #define SCTP_EVENTS SCTP_EVENTS
118         SCTP_I_WANT_MAPPED_V4_ADDR,  /* Turn on/off mapped v4 addresses  */
119 #define SCTP_I_WANT_MAPPED_V4_ADDR SCTP_I_WANT_MAPPED_V4_ADDR
120         SCTP_MAXSEG,    /* Get/set maximum fragment. */
121 #define SCTP_MAXSEG     SCTP_MAXSEG
122         SCTP_STATUS,
123 #define SCTP_STATUS SCTP_STATUS
124         SCTP_GET_PEER_ADDR_INFO,
125 #define SCTP_GET_PEER_ADDR_INFO SCTP_GET_PEER_ADDR_INFO
126         SCTP_DELAYED_ACK_TIME,
127 #define SCTP_DELAYED_ACK_TIME SCTP_DELAYED_ACK_TIME
128         SCTP_CONTEXT,   /* Receive Context */
129 #define SCTP_CONTEXT SCTP_CONTEXT
130         SCTP_FRAGMENT_INTERLEAVE,
131 #define SCTP_FRAGMENT_INTERLEAVE SCTP_FRAGMENT_INTERLEAVE
132         SCTP_PARTIAL_DELIVERY_POINT,    /* Set/Get partial delivery point */
133 #define SCTP_PARTIAL_DELIVERY_POINT SCTP_PARTIAL_DELIVERY_POINT
134         SCTP_MAX_BURST,         /* Set/Get max burst */
135 #define SCTP_MAX_BURST SCTP_MAX_BURST
136 };
137 
138 enum sctp_sac_state {
139         SCTP_COMM_UP,
140         SCTP_COMM_LOST,
141         SCTP_RESTART,
142         SCTP_SHUTDOWN_COMP,
143         SCTP_CANT_STR_ASSOC,
144 };
145 
146 enum sctp_spc_state {
147         SCTP_ADDR_AVAILABLE,
148         SCTP_ADDR_UNREACHABLE,
149         SCTP_ADDR_REMOVED,
150         SCTP_ADDR_ADDED,
151         SCTP_ADDR_MADE_PRIM,
152         SCTP_ADDR_CONFIRMED,
153 };
154 
155 enum sctp_sinfo_flags {
156         SCTP_UNORDERED = 1,  /* Send/receive message unordered. */
157         SCTP_ADDR_OVER = 2,  /* Override the primary destination. */
158         SCTP_ABORT=4,        /* Send an ABORT message to the peer. */
159         SCTP_EOF=MSG_FIN,    /* Initiate graceful shutdown process. */
160 };
161 
162 enum sctp_sn_type {
163         SCTP_SN_TYPE_BASE     = (1<<15),
164         SCTP_ASSOC_CHANGE,
165         SCTP_PEER_ADDR_CHANGE,
166         SCTP_SEND_FAILED,
167         SCTP_REMOTE_ERROR,
168         SCTP_SHUTDOWN_EVENT,
169         SCTP_PARTIAL_DELIVERY_EVENT,
170         SCTP_ADAPTATION_INDICATION,
171 };
172 
173 typedef enum sctp_cmsg_type {
174         SCTP_INIT,              /* 5.2.1 SCTP Initiation Structure */
175 #define SCTP_INIT SCTP_INIT
176         SCTP_SNDRCV,            /* 5.2.2 SCTP Header Information Structure */
177 #define SCTP_SNDRCV SCTP_SNDRCV
178 } sctp_cmsg_t;
179 
180 enum sctp_msg_flags {
181         MSG_NOTIFICATION = 0x8000,
182 #define MSG_NOTIFICATION MSG_NOTIFICATION
183 };
184 
185 #define SCTP_BINDX_ADD_ADDR 0x01
186 #define SCTP_BINDX_REM_ADDR 0x02
187 
188 typedef __s32 sctp_assoc_t;
189 
190 struct sctp_initmsg {
191         __u16 sinit_num_ostreams;
192         __u16 sinit_max_instreams;
193         __u16 sinit_max_attempts;
194         __u16 sinit_max_init_timeo;
195 };
196 
197 struct sctp_sndrcvinfo {
198         __u16 sinfo_stream;
199         __u16 sinfo_ssn;
200         __u16 sinfo_flags;
201         __u32 sinfo_ppid;
202         __u32 sinfo_context;
203         __u32 sinfo_timetolive;
204         __u32 sinfo_tsn;
205         __u32 sinfo_cumtsn;
206         sctp_assoc_t sinfo_assoc_id;
207 };
208 
209 struct sctp_event_subscribe {
210         __u8 sctp_data_io_event;
211         __u8 sctp_association_event;
212         __u8 sctp_address_event;
213         __u8 sctp_send_failure_event;
214         __u8 sctp_peer_error_event;
215         __u8 sctp_shutdown_event;
216         __u8 sctp_partial_delivery_event;
217         __u8 sctp_adaptation_layer_event;
218 };
219 
220 struct sctp_send_failed {
221         __u16 ssf_type;
222         __u16 ssf_flags;
223         __u32 ssf_length;
224         __u32 ssf_error;
225         struct sctp_sndrcvinfo ssf_info;
226         sctp_assoc_t ssf_assoc_id;
227         __u8 ssf_data[0];
228 };
229 
230 struct sctp_assoc_change {
231         __u16 sac_type;
232         __u16 sac_flags;
233         __u32 sac_length;
234         __u16 sac_state;
235         __u16 sac_error;
236         __u16 sac_outbound_streams;
237         __u16 sac_inbound_streams;
238         sctp_assoc_t sac_assoc_id;
239         __u8 sac_info[0];
240 };
241 
242 struct sctp_shutdown_event {
243         __u16 sse_type;
244         __u16 sse_flags;
245         __u32 sse_length;
246         sctp_assoc_t sse_assoc_id;
247 };
248 
249 struct sctp_paddr_change {
250         __u16 spc_type;
251         __u16 spc_flags;
252         __u32 spc_length;
253         struct sockaddr_storage spc_aaddr;
254         int spc_state;
255         int spc_error;
256         sctp_assoc_t spc_assoc_id;
257 } __attribute__((packed, aligned(4)));
258 
259 struct sctp_remote_error {
260         __u16 sre_type;
261         __u16 sre_flags;
262         __u32 sre_length;
263         __u16 sre_error;
264         sctp_assoc_t sre_assoc_id;
265         __u8 sre_data[0];
266 };
267 
268 struct sctp_adaptation_event {
269         __u16 sai_type;
270         __u16 sai_flags;
271         __u32 sai_length;
272         __u32 sai_adaptation_ind;
273         sctp_assoc_t sai_assoc_id;
274 };
275 
276 struct sctp_setprim {
277         sctp_assoc_t            ssp_assoc_id;
278         struct sockaddr_storage ssp_addr;
279 } __attribute__((packed, aligned(4)));
280 
281 struct sctp_setpeerprim {
282         sctp_assoc_t            sspp_assoc_id;
283         struct sockaddr_storage sspp_addr;
284 } __attribute__((packed, aligned(4)));
285 
286 
287 struct sctp_pdapi_event {
288         __u16 pdapi_type;
289         __u16 pdapi_flags;
290         __u32 pdapi_length;
291         __u32 pdapi_indication;
292         sctp_assoc_t pdapi_assoc_id;
293 };
294 
295 union sctp_notification {
296         struct {
297                 __u16 sn_type;             /* Notification type. */
298                 __u16 sn_flags;
299                 __u32 sn_length;
300         } sn_header;
301         struct sctp_assoc_change sn_assoc_change;
302         struct sctp_paddr_change sn_paddr_change;
303         struct sctp_remote_error sn_remote_error;
304         struct sctp_send_failed sn_send_failed;
305         struct sctp_shutdown_event sn_shutdown_event;
306         struct sctp_adaptation_event sn_adaptation_event;
307         struct sctp_pdapi_event sn_pdapi_event;
308 };
309 
310 #endif /* SCTP_INITMSG */
311 
312 /* Function types to support dynamic linking of socket API extension functions
313  * for SCTP. This is so that there is no linkage depandancy during build or
314  * runtime for libsctp.*/
315 typedef int sctp_getladdrs_func(int sd, sctp_assoc_t id, struct sockaddr **addrs);
316 typedef int sctp_freeladdrs_func(struct sockaddr *addrs);
317 typedef int sctp_getpaddrs_func(int sd, sctp_assoc_t id, struct sockaddr **addrs);
318 typedef int sctp_freepaddrs_func(struct sockaddr *addrs);
319 typedef int sctp_bindx_func(int sd, struct sockaddr *addrs, int addrcnt, int flags);
320 typedef int sctp_peeloff_func(int sock, sctp_assoc_t id);
321 
322 #elif defined(__FreeBSD__)
323 
324 #include <stdint.h>
325 #include <sys/types.h>
326 #include <sys/socket.h>
327 #include <netinet/sctp.h>
328 #include <netinet/sctp_peeloff.h>
329 #include <netinet/sctp_uio.h>
330 #include "jni.h"
331 
332 #endif /* __linux__ */
333 
334 #ifndef __FreeBSD__
335 
336 extern sctp_getladdrs_func* nio_sctp_getladdrs;
337 extern sctp_freeladdrs_func* nio_sctp_freeladdrs;
338 extern sctp_getpaddrs_func* nio_sctp_getpaddrs;
339 extern sctp_freepaddrs_func* nio_sctp_freepaddrs;
340 extern sctp_bindx_func* nio_sctp_bindx;
341 extern sctp_peeloff_func* nio_sctp_peeloff;
342 
343 #else
344 
345 #define nio_sctp_getladdrs     sctp_getladdrs
346 #define nio_sctp_freeladdrs    sctp_freeladdrs
347 #define nio_sctp_getpaddrs     sctp_getpaddrs
348 #define nio_sctp_freepaddrs    sctp_freepaddrs
349 #define nio_sctp_bindx         sctp_bindx
350 #define nio_sctp_peeloff       sctp_peeloff
351 
352 #endif
353 
354 jboolean loadSocketExtensionFuncs(JNIEnv* env);
355 
356 #endif /* !SUN_NIO_CH_SCTP_H */
357