1 /*
2  * NET		An implementation of the SOCKET network access protocol.
3  *		This is the master header file for the Linux NET layer,
4  *		or, in plain English: the networking handling part of the
5  *		kernel.
6  *
7  * Version:	@(#)net.h	1.0.3	05/25/93
8  *
9  * Authors:	Orest Zborowski, <obz@Kodak.COM>
10  *		Ross Biro
11  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12  *
13  *		This program is free software; you can redistribute it and/or
14  *		modify it under the terms of the GNU General Public License
15  *		as published by the Free Software Foundation; either version
16  *		2 of the License, or (at your option) any later version.
17  */
18 #ifndef _LINUX_NET_H
19 #define _LINUX_NET_H
20 
21 #include <linux/socket.h>
22 #include <asm/socket.h>
23 
24 #define NPROTO		AF_MAX
25 
26 #define SYS_SOCKET	1		/* sys_socket(2)		*/
27 #define SYS_BIND	2		/* sys_bind(2)			*/
28 #define SYS_CONNECT	3		/* sys_connect(2)		*/
29 #define SYS_LISTEN	4		/* sys_listen(2)		*/
30 #define SYS_ACCEPT	5		/* sys_accept(2)		*/
31 #define SYS_GETSOCKNAME	6		/* sys_getsockname(2)		*/
32 #define SYS_GETPEERNAME	7		/* sys_getpeername(2)		*/
33 #define SYS_SOCKETPAIR	8		/* sys_socketpair(2)		*/
34 #define SYS_SEND	9		/* sys_send(2)			*/
35 #define SYS_RECV	10		/* sys_recv(2)			*/
36 #define SYS_SENDTO	11		/* sys_sendto(2)		*/
37 #define SYS_RECVFROM	12		/* sys_recvfrom(2)		*/
38 #define SYS_SHUTDOWN	13		/* sys_shutdown(2)		*/
39 #define SYS_SETSOCKOPT	14		/* sys_setsockopt(2)		*/
40 #define SYS_GETSOCKOPT	15		/* sys_getsockopt(2)		*/
41 #define SYS_SENDMSG	16		/* sys_sendmsg(2)		*/
42 #define SYS_RECVMSG	17		/* sys_recvmsg(2)		*/
43 #define SYS_ACCEPT4	18		/* sys_accept4(2)		*/
44 #define SYS_RECVMMSG	19		/* sys_recvmmsg(2)		*/
45 #define SYS_SENDMMSG	20		/* sys_sendmmsg(2)		*/
46 
47 typedef enum {
48 	SS_FREE = 0,			/* not allocated		*/
49 	SS_UNCONNECTED,			/* unconnected to any socket	*/
50 	SS_CONNECTING,			/* in process of connecting	*/
51 	SS_CONNECTED,			/* connected to socket		*/
52 	SS_DISCONNECTING		/* in process of disconnecting	*/
53 } socket_state;
54 
55 #define __SO_ACCEPTCON	(1 << 16)	/* performed a listen		*/
56 
57 #ifdef __KERNEL__
58 #include <linux/stringify.h>
59 #include <linux/random.h>
60 #include <linux/wait.h>
61 #include <linux/fcntl.h>	/* For O_CLOEXEC and O_NONBLOCK */
62 #include <linux/kmemcheck.h>
63 #include <linux/rcupdate.h>
64 
65 struct poll_table_struct;
66 struct pipe_inode_info;
67 struct inode;
68 struct net;
69 
70 #define SOCK_ASYNC_NOSPACE	0
71 #define SOCK_ASYNC_WAITDATA	1
72 #define SOCK_NOSPACE		2
73 #define SOCK_PASSCRED		3
74 #define SOCK_PASSSEC		4
75 #define SOCK_EXTERNALLY_ALLOCATED 5
76 
77 #ifndef ARCH_HAS_SOCKET_TYPES
78 /**
79  * enum sock_type - Socket types
80  * @SOCK_STREAM: stream (connection) socket
81  * @SOCK_DGRAM: datagram (conn.less) socket
82  * @SOCK_RAW: raw socket
83  * @SOCK_RDM: reliably-delivered message
84  * @SOCK_SEQPACKET: sequential packet socket
85  * @SOCK_DCCP: Datagram Congestion Control Protocol socket
86  * @SOCK_PACKET: linux specific way of getting packets at the dev level.
87  *		  For writing rarp and other similar things on the user level.
88  *
89  * When adding some new socket type please
90  * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
91  * overrides this enum for binary compat reasons.
92  */
93 enum sock_type {
94 	SOCK_STREAM	= 1,
95 	SOCK_DGRAM	= 2,
96 	SOCK_RAW	= 3,
97 	SOCK_RDM	= 4,
98 	SOCK_SEQPACKET	= 5,
99 	SOCK_DCCP	= 6,
100 	SOCK_PACKET	= 10,
101 };
102 
103 #define SOCK_MAX (SOCK_PACKET + 1)
104 /* Mask which covers at least up to SOCK_MASK-1.  The
105  * remaining bits are used as flags. */
106 #define SOCK_TYPE_MASK 0xf
107 
108 /* Flags for socket, socketpair, accept4 */
109 #define SOCK_CLOEXEC	O_CLOEXEC
110 #ifndef SOCK_NONBLOCK
111 #define SOCK_NONBLOCK	O_NONBLOCK
112 #endif
113 
114 #endif /* ARCH_HAS_SOCKET_TYPES */
115 
116 enum sock_shutdown_cmd {
117 	SHUT_RD		= 0,
118 	SHUT_WR		= 1,
119 	SHUT_RDWR	= 2,
120 };
121 
122 struct socket_wq {
123 	/* Note: wait MUST be first field of socket_wq */
124 	wait_queue_head_t	wait;
125 	struct fasync_struct	*fasync_list;
126 	struct rcu_head		rcu;
127 } ____cacheline_aligned_in_smp;
128 
129 /**
130  *  struct socket - general BSD socket
131  *  @state: socket state (%SS_CONNECTED, etc)
132  *  @type: socket type (%SOCK_STREAM, etc)
133  *  @flags: socket flags (%SOCK_ASYNC_NOSPACE, etc)
134  *  @ops: protocol specific socket operations
135  *  @file: File back pointer for gc
136  *  @sk: internal networking protocol agnostic socket representation
137  *  @wq: wait queue for several uses
138  */
139 struct socket {
140 	socket_state		state;
141 
142 	kmemcheck_bitfield_begin(type);
143 	short			type;
144 	kmemcheck_bitfield_end(type);
145 
146 	unsigned long		flags;
147 
148 	struct socket_wq __rcu	*wq;
149 
150 	struct file		*file;
151 	struct sock		*sk;
152 	const struct proto_ops	*ops;
153 };
154 
155 struct vm_area_struct;
156 struct page;
157 struct kiocb;
158 struct sockaddr;
159 struct msghdr;
160 struct module;
161 
162 struct proto_ops {
163 	int		family;
164 	struct module	*owner;
165 	int		(*release)   (struct socket *sock);
166 	int		(*bind)	     (struct socket *sock,
167 				      struct sockaddr *myaddr,
168 				      int sockaddr_len);
169 	int		(*connect)   (struct socket *sock,
170 				      struct sockaddr *vaddr,
171 				      int sockaddr_len, int flags);
172 	int		(*socketpair)(struct socket *sock1,
173 				      struct socket *sock2);
174 	int		(*accept)    (struct socket *sock,
175 				      struct socket *newsock, int flags);
176 	int		(*getname)   (struct socket *sock,
177 				      struct sockaddr *addr,
178 				      int *sockaddr_len, int peer);
179 	unsigned int	(*poll)	     (struct file *file, struct socket *sock,
180 				      struct poll_table_struct *wait);
181 	int		(*ioctl)     (struct socket *sock, unsigned int cmd,
182 				      unsigned long arg);
183 #ifdef CONFIG_COMPAT
184 	int	 	(*compat_ioctl) (struct socket *sock, unsigned int cmd,
185 				      unsigned long arg);
186 #endif
187 	int		(*listen)    (struct socket *sock, int len);
188 	int		(*shutdown)  (struct socket *sock, int flags);
189 	int		(*setsockopt)(struct socket *sock, int level,
190 				      int optname, char __user *optval, unsigned int optlen);
191 	int		(*getsockopt)(struct socket *sock, int level,
192 				      int optname, char __user *optval, int __user *optlen);
193 #ifdef CONFIG_COMPAT
194 	int		(*compat_setsockopt)(struct socket *sock, int level,
195 				      int optname, char __user *optval, unsigned int optlen);
196 	int		(*compat_getsockopt)(struct socket *sock, int level,
197 				      int optname, char __user *optval, int __user *optlen);
198 #endif
199 	int		(*sendmsg)   (struct kiocb *iocb, struct socket *sock,
200 				      struct msghdr *m, size_t total_len);
201 	/* Notes for implementing recvmsg:
202 	 * ===============================
203 	 * msg->msg_namelen should get updated by the recvmsg handlers
204 	 * iff msg_name != NULL. It is by default 0 to prevent
205 	 * returning uninitialized memory to user space.  The recvfrom
206 	 * handlers can assume that msg.msg_name is either NULL or has
207 	 * a minimum size of sizeof(struct sockaddr_storage).
208 	 */
209 	int		(*recvmsg)   (struct kiocb *iocb, struct socket *sock,
210 				      struct msghdr *m, size_t total_len,
211 				      int flags);
212 	int		(*mmap)	     (struct file *file, struct socket *sock,
213 				      struct vm_area_struct * vma);
214 	ssize_t		(*sendpage)  (struct socket *sock, struct page *page,
215 				      int offset, size_t size, int flags);
216 	ssize_t 	(*splice_read)(struct socket *sock,  loff_t *ppos,
217 				       struct pipe_inode_info *pipe, size_t len, unsigned int flags);
218 	int		(*set_peek_off)(struct sock *sk, int val);
219 };
220 
221 #define DECLARE_SOCKADDR(type, dst, src)	\
222 	type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
223 
224 struct net_proto_family {
225 	int		family;
226 	int		(*create)(struct net *net, struct socket *sock,
227 				  int protocol, int kern);
228 	struct module	*owner;
229 };
230 
231 struct iovec;
232 struct kvec;
233 
234 enum {
235 	SOCK_WAKE_IO,
236 	SOCK_WAKE_WAITD,
237 	SOCK_WAKE_SPACE,
238 	SOCK_WAKE_URG,
239 };
240 
241 extern int	     sock_wake_async(struct socket *sk, int how, int band);
242 extern int	     sock_register(const struct net_proto_family *fam);
243 extern void	     sock_unregister(int family);
244 extern int	     __sock_create(struct net *net, int family, int type, int proto,
245 				 struct socket **res, int kern);
246 extern int	     sock_create(int family, int type, int proto,
247 				 struct socket **res);
248 extern int	     sock_create_kern(int family, int type, int proto,
249 				      struct socket **res);
250 extern int	     sock_create_lite(int family, int type, int proto,
251 				      struct socket **res);
252 extern void	     sock_release(struct socket *sock);
253 extern int   	     sock_sendmsg(struct socket *sock, struct msghdr *msg,
254 				  size_t len);
255 extern int	     sock_recvmsg(struct socket *sock, struct msghdr *msg,
256 				  size_t size, int flags);
257 extern int 	     sock_map_fd(struct socket *sock, int flags);
258 extern struct socket *sockfd_lookup(int fd, int *err);
259 #define		     sockfd_put(sock) fput(sock->file)
260 extern int	     net_ratelimit(void);
261 
262 #define net_ratelimited_function(function, ...)			\
263 do {								\
264 	if (net_ratelimit())					\
265 		function(__VA_ARGS__);				\
266 } while (0)
267 
268 #define net_emerg_ratelimited(fmt, ...)				\
269 	net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
270 #define net_alert_ratelimited(fmt, ...)				\
271 	net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
272 #define net_crit_ratelimited(fmt, ...)				\
273 	net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
274 #define net_err_ratelimited(fmt, ...)				\
275 	net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
276 #define net_notice_ratelimited(fmt, ...)			\
277 	net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
278 #define net_warn_ratelimited(fmt, ...)				\
279 	net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
280 #define net_info_ratelimited(fmt, ...)				\
281 	net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
282 #define net_dbg_ratelimited(fmt, ...)				\
283 	net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
284 
285 #define net_ratelimited_function(function, ...)			\
286 do {								\
287 	if (net_ratelimit())					\
288 		function(__VA_ARGS__);				\
289 } while (0)
290 
291 #define net_emerg_ratelimited(fmt, ...)				\
292 	net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
293 #define net_alert_ratelimited(fmt, ...)				\
294 	net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
295 #define net_crit_ratelimited(fmt, ...)				\
296 	net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
297 #define net_err_ratelimited(fmt, ...)				\
298 	net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
299 #define net_notice_ratelimited(fmt, ...)			\
300 	net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
301 #define net_warn_ratelimited(fmt, ...)				\
302 	net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
303 #define net_info_ratelimited(fmt, ...)				\
304 	net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
305 #define net_dbg_ratelimited(fmt, ...)				\
306 	net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
307 
308 #define net_random()		random32()
309 #define net_srandom(seed)	srandom32((__force u32)seed)
310 
311 extern int   	     kernel_sendmsg(struct socket *sock, struct msghdr *msg,
312 				    struct kvec *vec, size_t num, size_t len);
313 extern int   	     kernel_recvmsg(struct socket *sock, struct msghdr *msg,
314 				    struct kvec *vec, size_t num,
315 				    size_t len, int flags);
316 
317 extern int kernel_bind(struct socket *sock, struct sockaddr *addr,
318 		       int addrlen);
319 extern int kernel_listen(struct socket *sock, int backlog);
320 extern int kernel_accept(struct socket *sock, struct socket **newsock,
321 			 int flags);
322 extern int kernel_connect(struct socket *sock, struct sockaddr *addr,
323 			  int addrlen, int flags);
324 extern int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
325 			      int *addrlen);
326 extern int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
327 			      int *addrlen);
328 extern int kernel_getsockopt(struct socket *sock, int level, int optname,
329 			     char *optval, int *optlen);
330 extern int kernel_setsockopt(struct socket *sock, int level, int optname,
331 			     char *optval, unsigned int optlen);
332 extern int kernel_sendpage(struct socket *sock, struct page *page, int offset,
333 			   size_t size, int flags);
334 extern int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg);
335 extern int kernel_sock_shutdown(struct socket *sock,
336 				enum sock_shutdown_cmd how);
337 
338 #define MODULE_ALIAS_NETPROTO(proto) \
339 	MODULE_ALIAS("net-pf-" __stringify(proto))
340 
341 #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
342 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
343 
344 #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
345 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
346 		     "-type-" __stringify(type))
347 
348 #endif /* __KERNEL__ */
349 #endif	/* _LINUX_NET_H */
350