1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SOCKET_H
3 #define _LINUX_SOCKET_H
4 
5 
6 #include <asm/socket.h>			/* arch-dependent defines	*/
7 #include <linux/sockios.h>		/* the SIOCxxx I/O controls	*/
8 #include <linux/uio.h>			/* iovec support		*/
9 #include <linux/types.h>		/* pid_t			*/
10 #include <linux/compiler.h>		/* __user			*/
11 #include <uapi/linux/socket.h>
12 
13 struct pid;
14 struct cred;
15 
16 #define __sockaddr_check_size(size)	\
17 	BUILD_BUG_ON(((size) > sizeof(struct __kernel_sockaddr_storage)))
18 
19 #ifdef CONFIG_PROC_FS
20 struct seq_file;
21 extern void socket_seq_show(struct seq_file *seq);
22 #endif
23 
24 typedef __kernel_sa_family_t	sa_family_t;
25 
26 /*
27  *	1003.1g requires sa_family_t and that sa_data is char.
28  */
29 
30 struct sockaddr {
31 	sa_family_t	sa_family;	/* address family, AF_xxx	*/
32 	char		sa_data[14];	/* 14 bytes of protocol address	*/
33 };
34 
35 struct linger {
36 	int		l_onoff;	/* Linger active		*/
37 	int		l_linger;	/* How long to linger for	*/
38 };
39 
40 #define sockaddr_storage __kernel_sockaddr_storage
41 
42 /*
43  *	As we do 4.4BSD message passing we use a 4.4BSD message passing
44  *	system, not 4.3. Thus msg_accrights(len) are now missing. They
45  *	belong in an obscure libc emulation or the bin.
46  */
47 
48 struct msghdr {
49 	void		*msg_name;	/* ptr to socket address structure */
50 	int		msg_namelen;	/* size of socket address structure */
51 	struct iov_iter	msg_iter;	/* data */
52 	void		*msg_control;	/* ancillary data */
53 	__kernel_size_t	msg_controllen;	/* ancillary data buffer length */
54 	unsigned int	msg_flags;	/* flags on received message */
55 	struct kiocb	*msg_iocb;	/* ptr to iocb for async requests */
56 };
57 
58 struct user_msghdr {
59 	void		__user *msg_name;	/* ptr to socket address structure */
60 	int		msg_namelen;		/* size of socket address structure */
61 	struct iovec	__user *msg_iov;	/* scatter/gather array */
62 	__kernel_size_t	msg_iovlen;		/* # elements in msg_iov */
63 	void		__user *msg_control;	/* ancillary data */
64 	__kernel_size_t	msg_controllen;		/* ancillary data buffer length */
65 	unsigned int	msg_flags;		/* flags on received message */
66 };
67 
68 /* For recvmmsg/sendmmsg */
69 struct mmsghdr {
70 	struct user_msghdr  msg_hdr;
71 	unsigned int        msg_len;
72 };
73 
74 /*
75  *	POSIX 1003.1g - ancillary data object information
76  *	Ancillary data consits of a sequence of pairs of
77  *	(cmsghdr, cmsg_data[])
78  */
79 
80 struct cmsghdr {
81 	__kernel_size_t	cmsg_len;	/* data byte count, including hdr */
82         int		cmsg_level;	/* originating protocol */
83         int		cmsg_type;	/* protocol-specific type */
84 };
85 
86 /*
87  *	Ancillary data object information MACROS
88  *	Table 5-14 of POSIX 1003.1g
89  */
90 
91 #define __CMSG_NXTHDR(ctl, len, cmsg) __cmsg_nxthdr((ctl),(len),(cmsg))
92 #define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg))
93 
94 #define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) & ~(sizeof(long)-1) )
95 
96 #define CMSG_DATA(cmsg)	((void *)((char *)(cmsg) + sizeof(struct cmsghdr)))
97 #define CMSG_SPACE(len) (sizeof(struct cmsghdr) + CMSG_ALIGN(len))
98 #define CMSG_LEN(len) (sizeof(struct cmsghdr) + (len))
99 
100 #define __CMSG_FIRSTHDR(ctl,len) ((len) >= sizeof(struct cmsghdr) ? \
101 				  (struct cmsghdr *)(ctl) : \
102 				  (struct cmsghdr *)NULL)
103 #define CMSG_FIRSTHDR(msg)	__CMSG_FIRSTHDR((msg)->msg_control, (msg)->msg_controllen)
104 #define CMSG_OK(mhdr, cmsg) ((cmsg)->cmsg_len >= sizeof(struct cmsghdr) && \
105 			     (cmsg)->cmsg_len <= (unsigned long) \
106 			     ((mhdr)->msg_controllen - \
107 			      ((char *)(cmsg) - (char *)(mhdr)->msg_control)))
108 #define for_each_cmsghdr(cmsg, msg) \
109 	for (cmsg = CMSG_FIRSTHDR(msg); \
110 	     cmsg; \
111 	     cmsg = CMSG_NXTHDR(msg, cmsg))
112 
113 /*
114  *	Get the next cmsg header
115  *
116  *	PLEASE, do not touch this function. If you think, that it is
117  *	incorrect, grep kernel sources and think about consequences
118  *	before trying to improve it.
119  *
120  *	Now it always returns valid, not truncated ancillary object
121  *	HEADER. But caller still MUST check, that cmsg->cmsg_len is
122  *	inside range, given by msg->msg_controllen before using
123  *	ancillary object DATA.				--ANK (980731)
124  */
125 
__cmsg_nxthdr(void * __ctl,__kernel_size_t __size,struct cmsghdr * __cmsg)126 static inline struct cmsghdr * __cmsg_nxthdr(void *__ctl, __kernel_size_t __size,
127 					       struct cmsghdr *__cmsg)
128 {
129 	struct cmsghdr * __ptr;
130 
131 	__ptr = (struct cmsghdr*)(((unsigned char *) __cmsg) +  CMSG_ALIGN(__cmsg->cmsg_len));
132 	if ((unsigned long)((char*)(__ptr+1) - (char *) __ctl) > __size)
133 		return (struct cmsghdr *)0;
134 
135 	return __ptr;
136 }
137 
cmsg_nxthdr(struct msghdr * __msg,struct cmsghdr * __cmsg)138 static inline struct cmsghdr * cmsg_nxthdr (struct msghdr *__msg, struct cmsghdr *__cmsg)
139 {
140 	return __cmsg_nxthdr(__msg->msg_control, __msg->msg_controllen, __cmsg);
141 }
142 
msg_data_left(struct msghdr * msg)143 static inline size_t msg_data_left(struct msghdr *msg)
144 {
145 	return iov_iter_count(&msg->msg_iter);
146 }
147 
148 /* "Socket"-level control message types: */
149 
150 #define	SCM_RIGHTS	0x01		/* rw: access rights (array of int) */
151 #define SCM_CREDENTIALS 0x02		/* rw: struct ucred		*/
152 #define SCM_SECURITY	0x03		/* rw: security label		*/
153 
154 struct ucred {
155 	__u32	pid;
156 	__u32	uid;
157 	__u32	gid;
158 };
159 
160 /* Supported address families. */
161 #define AF_UNSPEC	0
162 #define AF_UNIX		1	/* Unix domain sockets 		*/
163 #define AF_LOCAL	1	/* POSIX name for AF_UNIX	*/
164 #define AF_INET		2	/* Internet IP Protocol 	*/
165 #define AF_AX25		3	/* Amateur Radio AX.25 		*/
166 #define AF_IPX		4	/* Novell IPX 			*/
167 #define AF_APPLETALK	5	/* AppleTalk DDP 		*/
168 #define AF_NETROM	6	/* Amateur Radio NET/ROM 	*/
169 #define AF_BRIDGE	7	/* Multiprotocol bridge 	*/
170 #define AF_ATMPVC	8	/* ATM PVCs			*/
171 #define AF_X25		9	/* Reserved for X.25 project 	*/
172 #define AF_INET6	10	/* IP version 6			*/
173 #define AF_ROSE		11	/* Amateur Radio X.25 PLP	*/
174 #define AF_DECnet	12	/* Reserved for DECnet project	*/
175 #define AF_NETBEUI	13	/* Reserved for 802.2LLC project*/
176 #define AF_SECURITY	14	/* Security callback pseudo AF */
177 #define AF_KEY		15      /* PF_KEY key management API */
178 #define AF_NETLINK	16
179 #define AF_ROUTE	AF_NETLINK /* Alias to emulate 4.4BSD */
180 #define AF_PACKET	17	/* Packet family		*/
181 #define AF_ASH		18	/* Ash				*/
182 #define AF_ECONET	19	/* Acorn Econet			*/
183 #define AF_ATMSVC	20	/* ATM SVCs			*/
184 #define AF_RDS		21	/* RDS sockets 			*/
185 #define AF_SNA		22	/* Linux SNA Project (nutters!) */
186 #define AF_IRDA		23	/* IRDA sockets			*/
187 #define AF_PPPOX	24	/* PPPoX sockets		*/
188 #define AF_WANPIPE	25	/* Wanpipe API Sockets */
189 #define AF_LLC		26	/* Linux LLC			*/
190 #define AF_IB		27	/* Native InfiniBand address	*/
191 #define AF_MPLS		28	/* MPLS */
192 #define AF_CAN		29	/* Controller Area Network      */
193 #define AF_TIPC		30	/* TIPC sockets			*/
194 #define AF_BLUETOOTH	31	/* Bluetooth sockets 		*/
195 #define AF_IUCV		32	/* IUCV sockets			*/
196 #define AF_RXRPC	33	/* RxRPC sockets 		*/
197 #define AF_ISDN		34	/* mISDN sockets 		*/
198 #define AF_PHONET	35	/* Phonet sockets		*/
199 #define AF_IEEE802154	36	/* IEEE802154 sockets		*/
200 #define AF_CAIF		37	/* CAIF sockets			*/
201 #define AF_ALG		38	/* Algorithm sockets		*/
202 #define AF_NFC		39	/* NFC sockets			*/
203 #define AF_VSOCK	40	/* vSockets			*/
204 #define AF_KCM		41	/* Kernel Connection Multiplexor*/
205 #define AF_QIPCRTR	42	/* Qualcomm IPC Router          */
206 #define AF_SMC		43	/* smc sockets: reserve number for
207 				 * PF_SMC protocol family that
208 				 * reuses AF_INET address family
209 				 */
210 #define AF_XDP		44	/* XDP sockets			*/
211 
212 #define AF_MAX		45	/* For now.. */
213 
214 /* Protocol families, same as address families. */
215 #define PF_UNSPEC	AF_UNSPEC
216 #define PF_UNIX		AF_UNIX
217 #define PF_LOCAL	AF_LOCAL
218 #define PF_INET		AF_INET
219 #define PF_AX25		AF_AX25
220 #define PF_IPX		AF_IPX
221 #define PF_APPLETALK	AF_APPLETALK
222 #define	PF_NETROM	AF_NETROM
223 #define PF_BRIDGE	AF_BRIDGE
224 #define PF_ATMPVC	AF_ATMPVC
225 #define PF_X25		AF_X25
226 #define PF_INET6	AF_INET6
227 #define PF_ROSE		AF_ROSE
228 #define PF_DECnet	AF_DECnet
229 #define PF_NETBEUI	AF_NETBEUI
230 #define PF_SECURITY	AF_SECURITY
231 #define PF_KEY		AF_KEY
232 #define PF_NETLINK	AF_NETLINK
233 #define PF_ROUTE	AF_ROUTE
234 #define PF_PACKET	AF_PACKET
235 #define PF_ASH		AF_ASH
236 #define PF_ECONET	AF_ECONET
237 #define PF_ATMSVC	AF_ATMSVC
238 #define PF_RDS		AF_RDS
239 #define PF_SNA		AF_SNA
240 #define PF_IRDA		AF_IRDA
241 #define PF_PPPOX	AF_PPPOX
242 #define PF_WANPIPE	AF_WANPIPE
243 #define PF_LLC		AF_LLC
244 #define PF_IB		AF_IB
245 #define PF_MPLS		AF_MPLS
246 #define PF_CAN		AF_CAN
247 #define PF_TIPC		AF_TIPC
248 #define PF_BLUETOOTH	AF_BLUETOOTH
249 #define PF_IUCV		AF_IUCV
250 #define PF_RXRPC	AF_RXRPC
251 #define PF_ISDN		AF_ISDN
252 #define PF_PHONET	AF_PHONET
253 #define PF_IEEE802154	AF_IEEE802154
254 #define PF_CAIF		AF_CAIF
255 #define PF_ALG		AF_ALG
256 #define PF_NFC		AF_NFC
257 #define PF_VSOCK	AF_VSOCK
258 #define PF_KCM		AF_KCM
259 #define PF_QIPCRTR	AF_QIPCRTR
260 #define PF_SMC		AF_SMC
261 #define PF_XDP		AF_XDP
262 #define PF_MAX		AF_MAX
263 
264 /* Maximum queue length specifiable by listen.  */
265 #define SOMAXCONN	128
266 
267 /* Flags we can use with send/ and recv.
268    Added those for 1003.1g not all are supported yet
269  */
270 
271 #define MSG_OOB		1
272 #define MSG_PEEK	2
273 #define MSG_DONTROUTE	4
274 #define MSG_TRYHARD     4       /* Synonym for MSG_DONTROUTE for DECnet */
275 #define MSG_CTRUNC	8
276 #define MSG_PROBE	0x10	/* Do not send. Only probe path f.e. for MTU */
277 #define MSG_TRUNC	0x20
278 #define MSG_DONTWAIT	0x40	/* Nonblocking io		 */
279 #define MSG_EOR         0x80	/* End of record */
280 #define MSG_WAITALL	0x100	/* Wait for a full request */
281 #define MSG_FIN         0x200
282 #define MSG_SYN		0x400
283 #define MSG_CONFIRM	0x800	/* Confirm path validity */
284 #define MSG_RST		0x1000
285 #define MSG_ERRQUEUE	0x2000	/* Fetch message from error queue */
286 #define MSG_NOSIGNAL	0x4000	/* Do not generate SIGPIPE */
287 #define MSG_MORE	0x8000	/* Sender will send more */
288 #define MSG_WAITFORONE	0x10000	/* recvmmsg(): block until 1+ packets avail */
289 #define MSG_SENDPAGE_NOTLAST 0x20000 /* sendpage() internal : not the last page */
290 #define MSG_BATCH	0x40000 /* sendmmsg(): more messages coming */
291 #define MSG_EOF         MSG_FIN
292 #define MSG_NO_SHARED_FRAGS 0x80000 /* sendpage() internal : page frags are not shared */
293 
294 #define MSG_ZEROCOPY	0x4000000	/* Use user data in kernel path */
295 #define MSG_FASTOPEN	0x20000000	/* Send data in TCP SYN */
296 #define MSG_CMSG_CLOEXEC 0x40000000	/* Set close_on_exec for file
297 					   descriptor received through
298 					   SCM_RIGHTS */
299 #if defined(CONFIG_COMPAT)
300 #define MSG_CMSG_COMPAT	0x80000000	/* This message needs 32 bit fixups */
301 #else
302 #define MSG_CMSG_COMPAT	0		/* We never have 32 bit fixups */
303 #endif
304 
305 
306 /* Setsockoptions(2) level. Thanks to BSD these must match IPPROTO_xxx */
307 #define SOL_IP		0
308 /* #define SOL_ICMP	1	No-no-no! Due to Linux :-) we cannot use SOL_ICMP=1 */
309 #define SOL_TCP		6
310 #define SOL_UDP		17
311 #define SOL_IPV6	41
312 #define SOL_ICMPV6	58
313 #define SOL_SCTP	132
314 #define SOL_UDPLITE	136     /* UDP-Lite (RFC 3828) */
315 #define SOL_RAW		255
316 #define SOL_IPX		256
317 #define SOL_AX25	257
318 #define SOL_ATALK	258
319 #define SOL_NETROM	259
320 #define SOL_ROSE	260
321 #define SOL_DECNET	261
322 #define	SOL_X25		262
323 #define SOL_PACKET	263
324 #define SOL_ATM		264	/* ATM layer (cell level) */
325 #define SOL_AAL		265	/* ATM Adaption Layer (packet level) */
326 #define SOL_IRDA        266
327 #define SOL_NETBEUI	267
328 #define SOL_LLC		268
329 #define SOL_DCCP	269
330 #define SOL_NETLINK	270
331 #define SOL_TIPC	271
332 #define SOL_RXRPC	272
333 #define SOL_PPPOL2TP	273
334 #define SOL_BLUETOOTH	274
335 #define SOL_PNPIPE	275
336 #define SOL_RDS		276
337 #define SOL_IUCV	277
338 #define SOL_CAIF	278
339 #define SOL_ALG		279
340 #define SOL_NFC		280
341 #define SOL_KCM		281
342 #define SOL_TLS		282
343 #define SOL_XDP		283
344 
345 /* IPX options */
346 #define IPX_TYPE	1
347 
348 extern int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr);
349 extern int put_cmsg(struct msghdr*, int level, int type, int len, void *data);
350 
351 struct timespec;
352 
353 /* The __sys_...msg variants allow MSG_CMSG_COMPAT iff
354  * forbid_cmsg_compat==false
355  */
356 extern long __sys_recvmsg(int fd, struct user_msghdr __user *msg,
357 			  unsigned int flags, bool forbid_cmsg_compat);
358 extern long __sys_sendmsg(int fd, struct user_msghdr __user *msg,
359 			  unsigned int flags, bool forbid_cmsg_compat);
360 extern int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
361 			  unsigned int flags, struct timespec *timeout);
362 extern int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg,
363 			  unsigned int vlen, unsigned int flags,
364 			  bool forbid_cmsg_compat);
365 
366 /* helpers which do the actual work for syscalls */
367 extern int __sys_recvfrom(int fd, void __user *ubuf, size_t size,
368 			  unsigned int flags, struct sockaddr __user *addr,
369 			  int __user *addr_len);
370 extern int __sys_sendto(int fd, void __user *buff, size_t len,
371 			unsigned int flags, struct sockaddr __user *addr,
372 			int addr_len);
373 extern int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
374 			 int __user *upeer_addrlen, int flags);
375 extern int __sys_socket(int family, int type, int protocol);
376 extern int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen);
377 extern int __sys_connect(int fd, struct sockaddr __user *uservaddr,
378 			 int addrlen);
379 extern int __sys_listen(int fd, int backlog);
380 extern int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
381 			     int __user *usockaddr_len);
382 extern int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
383 			     int __user *usockaddr_len);
384 extern int __sys_socketpair(int family, int type, int protocol,
385 			    int __user *usockvec);
386 extern int __sys_shutdown(int fd, int how);
387 
388 extern struct ns_common *get_net_ns(struct ns_common *ns);
389 #endif /* _LINUX_SOCKET_H */
390