xref: /freebsd/sys/compat/linux/linux_socket.h (revision 90aac0d83bc9645f51ef0c2aeae6f9c0540bb031)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2000 Assar Westerlund
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef _LINUX_SOCKET_H_
32 #define _LINUX_SOCKET_H_
33 
34 /* msg flags in recvfrom/recvmsg */
35 
36 #define LINUX_MSG_OOB		0x01
37 #define LINUX_MSG_PEEK		0x02
38 #define LINUX_MSG_DONTROUTE	0x04
39 #define LINUX_MSG_CTRUNC	0x08
40 #define LINUX_MSG_PROXY		0x10
41 #define LINUX_MSG_TRUNC		0x20
42 #define LINUX_MSG_DONTWAIT	0x40
43 #define LINUX_MSG_EOR		0x80
44 #define LINUX_MSG_WAITALL	0x100
45 #define LINUX_MSG_FIN		0x200
46 #define LINUX_MSG_SYN		0x400
47 #define LINUX_MSG_CONFIRM	0x800
48 #define LINUX_MSG_RST		0x1000
49 #define LINUX_MSG_ERRQUEUE	0x2000
50 #define LINUX_MSG_NOSIGNAL	0x4000
51 #define LINUX_MSG_WAITFORONE	0x10000
52 #define LINUX_MSG_CMSG_CLOEXEC	0x40000000
53 
54 struct l_msghdr {
55 	l_uintptr_t	msg_name;
56 	l_int		msg_namelen;
57 	l_uintptr_t	msg_iov;
58 	l_size_t	msg_iovlen;
59 	l_uintptr_t	msg_control;
60 	l_size_t	msg_controllen;
61 	l_uint		msg_flags;
62 };
63 
64 struct l_mmsghdr {
65 	struct l_msghdr	msg_hdr;
66 	l_uint		msg_len;
67 
68 };
69 
70 struct l_cmsghdr {
71 	l_size_t	cmsg_len;
72 	l_int		cmsg_level;
73 	l_int		cmsg_type;
74 };
75 
76 /* Ancillary data object information macros */
77 
78 #define LINUX_CMSG_ALIGN(len)	roundup2(len, sizeof(l_ulong))
79 #define LINUX_CMSG_DATA(cmsg)	((void *)((char *)(cmsg) + \
80 				    LINUX_CMSG_ALIGN(sizeof(struct l_cmsghdr))))
81 #define LINUX_CMSG_SPACE(len)	(LINUX_CMSG_ALIGN(sizeof(struct l_cmsghdr)) + \
82 				    LINUX_CMSG_ALIGN(len))
83 #define LINUX_CMSG_LEN(len)	(LINUX_CMSG_ALIGN(sizeof(struct l_cmsghdr)) + \
84 				    (len))
85 #define LINUX_CMSG_FIRSTHDR(msg) \
86 				((msg)->msg_controllen >= \
87 				    sizeof(struct l_cmsghdr) ? \
88 				    (struct l_cmsghdr *) \
89 				        PTRIN((msg)->msg_control) : \
90 				    (struct l_cmsghdr *)(NULL))
91 #define LINUX_CMSG_NXTHDR(msg, cmsg) \
92 				((((char *)(cmsg) + \
93 				    LINUX_CMSG_ALIGN((cmsg)->cmsg_len) + \
94 				    sizeof(*(cmsg))) > \
95 				    (((char *)PTRIN((msg)->msg_control)) + \
96 				    (msg)->msg_controllen)) ? \
97 				    (struct l_cmsghdr *) NULL : \
98 				    (struct l_cmsghdr *)((char *)(cmsg) + \
99 				    LINUX_CMSG_ALIGN((cmsg)->cmsg_len)))
100 
101 #define CMSG_HDRSZ		CMSG_LEN(0)
102 #define L_CMSG_HDRSZ		LINUX_CMSG_LEN(0)
103 
104 /* Supported socket types */
105 
106 #define	LINUX_SOCK_STREAM	1
107 #define	LINUX_SOCK_DGRAM	2
108 #define	LINUX_SOCK_RAW		3
109 #define	LINUX_SOCK_RDM		4
110 #define	LINUX_SOCK_SEQPACKET	5
111 
112 #define	LINUX_SOCK_MAX		LINUX_SOCK_SEQPACKET
113 
114 #define	LINUX_SOCK_TYPE_MASK	0xf
115 
116 /* Flags for socket, socketpair, accept4 */
117 
118 #define	LINUX_SOCK_CLOEXEC	LINUX_O_CLOEXEC
119 #define	LINUX_SOCK_NONBLOCK	LINUX_O_NONBLOCK
120 
121 struct l_ucred {
122 	uint32_t	pid;
123 	uint32_t	uid;
124 	uint32_t	gid;
125 };
126 
127 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
128 
129 struct linux_accept_args {
130 	register_t s;
131 	register_t addr;
132 	register_t namelen;
133 };
134 
135 int linux_accept(struct thread *td, struct linux_accept_args *args);
136 
137 /* Operations for socketcall */
138 #define	LINUX_SOCKET		1
139 #define	LINUX_BIND		2
140 #define	LINUX_CONNECT		3
141 #define	LINUX_LISTEN		4
142 #define	LINUX_ACCEPT		5
143 #define	LINUX_GETSOCKNAME	6
144 #define	LINUX_GETPEERNAME	7
145 #define	LINUX_SOCKETPAIR	8
146 #define	LINUX_SEND		9
147 #define	LINUX_RECV		10
148 #define	LINUX_SENDTO		11
149 #define	LINUX_RECVFROM		12
150 #define	LINUX_SHUTDOWN		13
151 #define	LINUX_SETSOCKOPT	14
152 #define	LINUX_GETSOCKOPT	15
153 #define	LINUX_SENDMSG		16
154 #define	LINUX_RECVMSG		17
155 #define	LINUX_ACCEPT4		18
156 #define	LINUX_RECVMMSG		19
157 #define	LINUX_SENDMMSG		20
158 #define	LINUX_SENDFILE		21
159 
160 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
161 
162 /* Socket defines */
163 #define	LINUX_SOL_SOCKET	1
164 
165 #define	LINUX_SO_DEBUG		1
166 #define	LINUX_SO_REUSEADDR	2
167 #define	LINUX_SO_TYPE		3
168 #define	LINUX_SO_ERROR		4
169 #define	LINUX_SO_DONTROUTE	5
170 #define	LINUX_SO_BROADCAST	6
171 #define	LINUX_SO_SNDBUF		7
172 #define	LINUX_SO_RCVBUF		8
173 #define	LINUX_SO_KEEPALIVE	9
174 #define	LINUX_SO_OOBINLINE	10
175 #define	LINUX_SO_NO_CHECK	11
176 #define	LINUX_SO_PRIORITY	12
177 #define	LINUX_SO_LINGER		13
178 #define	LINUX_SO_REUSEPORT	15
179 #ifndef LINUX_SO_PASSCRED	/* powerpc differs */
180 #define	LINUX_SO_PASSCRED	16
181 #define	LINUX_SO_PEERCRED	17
182 #define	LINUX_SO_RCVLOWAT	18
183 #define	LINUX_SO_SNDLOWAT	19
184 #define	LINUX_SO_RCVTIMEO	20
185 #define	LINUX_SO_SNDTIMEO	21
186 #endif
187 #define	LINUX_SO_TIMESTAMPO	29
188 #define	LINUX_SO_TIMESTAMPN	63
189 #define	LINUX_SO_TIMESTAMPNSO	35
190 #define	LINUX_SO_TIMESTAMPNSN	64
191 #define	LINUX_SO_ACCEPTCONN	30
192 #define	LINUX_SO_PEERSEC	31
193 #define	LINUX_SO_SNDBUFFORCE	32
194 #define	LINUX_SO_RCVBUFFORCE	33
195 #define	LINUX_SO_PROTOCOL	38
196 #define	LINUX_SO_DOMAIN		39
197 #define	LINUX_SO_PEERGROUPS	59
198 
199 /* Socket-level control message types */
200 
201 #define LINUX_SCM_RIGHTS	0x01
202 #define LINUX_SCM_CREDENTIALS	0x02
203 #define LINUX_SCM_TIMESTAMPO	LINUX_SO_TIMESTAMPO
204 #define LINUX_SCM_TIMESTAMPN	LINUX_SO_TIMESTAMPN
205 #define LINUX_SCM_TIMESTAMPNSO	LINUX_SO_TIMESTAMPNSO
206 #define LINUX_SCM_TIMESTAMPNSN	LINUX_SO_TIMESTAMPNSN
207 
208 /* Socket options */
209 #define	LINUX_IP_TOS		1
210 #define	LINUX_IP_TTL		2
211 #define	LINUX_IP_HDRINCL	3
212 #define	LINUX_IP_OPTIONS	4
213 #define	LINUX_IP_ROUTER_ALERT	5
214 #define	LINUX_IP_RECVOPTS	6
215 #define	LINUX_IP_RETOPTS	7
216 #define	LINUX_IP_PKTINFO	8
217 #define	LINUX_IP_PKTOPTIONS	9
218 #define	LINUX_IP_MTU_DISCOVER	10
219 #define	LINUX_IP_RECVERR	11
220 #define	LINUX_IP_RECVTTL	12
221 #define	LINUX_IP_RECVTOS	13
222 #define	LINUX_IP_MTU		14
223 #define	LINUX_IP_FREEBIND	15
224 #define	LINUX_IP_IPSEC_POLICY	16
225 #define	LINUX_IP_XFRM_POLICY	17
226 #define	LINUX_IP_PASSSEC	18
227 #define	LINUX_IP_TRANSPARENT	19
228 
229 #define	LINUX_IP_ORIGDSTADDR		20
230 #define	LINUX_IP_RECVORIGDSTADDR	LINUX_IP_ORIGDSTADDR
231 #define	LINUX_IP_MINTTL		21
232 #define	LINUX_IP_NODEFRAG	22
233 #define	LINUX_IP_CHECKSUM	23
234 #define	LINUX_IP_BIND_ADDRESS_NO_PORT	24
235 #define	LINUX_IP_RECVFRAGSIZE	25
236 
237 #define	LINUX_IP_MULTICAST_IF		32
238 #define	LINUX_IP_MULTICAST_TTL		33
239 #define	LINUX_IP_MULTICAST_LOOP		34
240 #define	LINUX_IP_ADD_MEMBERSHIP		35
241 #define	LINUX_IP_DROP_MEMBERSHIP	36
242 #define	LINUX_IP_UNBLOCK_SOURCE		37
243 #define	LINUX_IP_BLOCK_SOURCE		38
244 #define	LINUX_IP_ADD_SOURCE_MEMBERSHIP	39
245 #define	LINUX_IP_DROP_SOURCE_MEMBERSHIP	40
246 #define	LINUX_IP_MSFILTER		41
247 
248 #define	LINUX_MCAST_JOIN_GROUP		42
249 #define	LINUX_MCAST_BLOCK_SOURCE	43
250 #define	LINUX_MCAST_UNBLOCK_SOURCE	44
251 #define	LINUX_MCAST_LEAVE_GROUP		45
252 #define	LINUX_MCAST_JOIN_SOURCE_GROUP	46
253 #define	LINUX_MCAST_LEAVE_SOURCE_GROUP	47
254 #define	LINUX_MCAST_MSFILTER		48
255 #define	LINUX_IP_MULTICAST_ALL		49
256 #define	LINUX_IP_UNICAST_IF		50
257 
258 #define	LINUX_IPV6_ADDRFORM		1
259 #define	LINUX_IPV6_2292PKTINFO		2
260 #define	LINUX_IPV6_2292HOPOPTS		3
261 #define	LINUX_IPV6_2292DSTOPTS		4
262 #define	LINUX_IPV6_2292RTHDR		5
263 #define	LINUX_IPV6_2292PKTOPTIONS	6
264 #define	LINUX_IPV6_CHECKSUM		7
265 #define	LINUX_IPV6_2292HOPLIMIT		8
266 #define	LINUX_IPV6_NEXTHOP		9
267 #define	LINUX_IPV6_AUTHHDR		10
268 #define	LINUX_IPV6_FLOWINFO		11
269 
270 #define	LINUX_IPV6_UNICAST_HOPS		16
271 #define	LINUX_IPV6_MULTICAST_IF		17
272 #define	LINUX_IPV6_MULTICAST_HOPS	18
273 #define	LINUX_IPV6_MULTICAST_LOOP	19
274 #define	LINUX_IPV6_ADD_MEMBERSHIP	20
275 #define	LINUX_IPV6_DROP_MEMBERSHIP	21
276 #define	LINUX_IPV6_ROUTER_ALERT		22
277 #define	LINUX_IPV6_MTU_DISCOVER		23
278 #define	LINUX_IPV6_MTU			24
279 #define	LINUX_IPV6_RECVERR		25
280 #define	LINUX_IPV6_V6ONLY		26
281 #define	LINUX_IPV6_JOIN_ANYCAST		27
282 #define	LINUX_IPV6_LEAVE_ANYCAST	28
283 #define	LINUX_IPV6_MULTICAST_ALL	29
284 #define	LINUX_IPV6_ROUTER_ALERT_ISOLATE	30
285 
286 #define	LINUX_IPV6_FLOWLABEL_MGR	32
287 #define	LINUX_IPV6_FLOWINFO_SEND	33
288 
289 #define	LINUX_IPV6_IPSEC_POLICY		34
290 #define	LINUX_IPV6_XFRM_POLICY		35
291 #define	LINUX_IPV6_HDRINCL		36
292 
293 #define	LINUX_IPV6_RECVPKTINFO		49
294 #define	LINUX_IPV6_PKTINFO		50
295 #define	LINUX_IPV6_RECVHOPLIMIT		51
296 #define	LINUX_IPV6_HOPLIMIT		52
297 #define	LINUX_IPV6_RECVHOPOPTS		53
298 #define	LINUX_IPV6_HOPOPTS		54
299 #define	LINUX_IPV6_RTHDRDSTOPTS		55
300 #define	LINUX_IPV6_RECVRTHDR		56
301 #define	LINUX_IPV6_RTHDR		57
302 #define	LINUX_IPV6_RECVDSTOPTS		58
303 #define	LINUX_IPV6_DSTOPTS		59
304 #define	LINUX_IPV6_RECVPATHMTU		60
305 #define	LINUX_IPV6_PATHMTU		61
306 #define	LINUX_IPV6_DONTFRAG		62
307 
308 #define	LINUX_IPV6_AUTOFLOWLABEL	70
309 #define	LINUX_IPV6_ADDR_PREFERENCES	72
310 #define	LINUX_IPV6_MINHOPCOUNT		73
311 #define	LINUX_IPV6_ORIGDSTADDR		74
312 #define	LINUX_IPV6_TRANSPARENT		75
313 #define	LINUX_IPV6_UNICAST_IF		76
314 #define	LINUX_IPV6_RECVFRAGSIZE		77
315 #define	LINUX_IPV6_FREEBIND		78
316 
317 #define	LINUX_TCP_NODELAY	1
318 #define	LINUX_TCP_MAXSEG	2
319 #define	LINUX_TCP_CORK		3
320 #define	LINUX_TCP_KEEPIDLE	4
321 #define	LINUX_TCP_KEEPINTVL	5
322 #define	LINUX_TCP_KEEPCNT	6
323 #define	LINUX_TCP_INFO		11
324 #define	LINUX_TCP_MD5SIG	14
325 
326 struct l_ifmap {
327 	l_ulong		mem_start;
328 	l_ulong		mem_end;
329 	l_ushort	base_addr;
330 	u_char		irq;
331 	u_char		dma;
332 	u_char		port;
333 	/* 3 bytes spare */
334 };
335 
336 /*
337  * Careful changing the declaration of this structure.
338  * To use FreeBSD names to access the struct l_ifreq members the
339  * member names of struct l_ifreq should be equal to the FreeBSD.
340  */
341 struct l_ifreq {
342 	char	ifr_name[LINUX_IFNAMSIZ];
343 	union {
344 		struct l_sockaddr	ifru_addr;
345 		struct l_sockaddr	ifru_dstaddr;
346 		struct l_sockaddr	ifru_broadaddr;
347 		struct l_sockaddr	ifru_netmask;
348 		struct l_sockaddr	ifru_hwaddr;
349 		l_short		ifru_flags[1];
350 		l_int		ifru_index;
351 		l_int		ifru_mtu;
352 		struct l_ifmap	ifru_map;
353 		char		ifru_slave[LINUX_IFNAMSIZ];
354 		char		ifru_newname[LINUX_IFNAMSIZ];
355 		l_uintptr_t	ifru_data;
356 	} ifr_ifru;
357 };
358 
359 /*
360  * Define here members which are not exists in the FreeBSD struct ifreq.
361  */
362 #define	ifr_hwaddr	ifr_ifru.ifru_hwaddr	/* MAC address */
363 
364 #endif /* _LINUX_SOCKET_H_ */
365