xref: /linux/include/linux/net.h (revision ceb5d58b217098a657f3850b7a2640f995032e62)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * NET		An implementation of the SOCKET network access protocol.
31da177e4SLinus Torvalds  *		This is the master header file for the Linux NET layer,
41da177e4SLinus Torvalds  *		or, in plain English: the networking handling part of the
51da177e4SLinus Torvalds  *		kernel.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Version:	@(#)net.h	1.0.3	05/25/93
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Authors:	Orest Zborowski, <obz@Kodak.COM>
1002c30a84SJesper Juhl  *		Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
141da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
151da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
161da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds #ifndef _LINUX_NET_H
191da177e4SLinus Torvalds #define _LINUX_NET_H
201da177e4SLinus Torvalds 
21eacf17bdSDavid Woodhouse #include <linux/stringify.h>
22cb4db4c2SDavid Woodhouse #include <linux/random.h>
235770a3fbSDavid Woodhouse #include <linux/wait.h>
245770a3fbSDavid Woodhouse #include <linux/fcntl.h>	/* For O_CLOEXEC and O_NONBLOCK */
2529a020d3SEric Dumazet #include <linux/kmemcheck.h>
2643815482SEric Dumazet #include <linux/rcupdate.h>
2746234253SHannes Frederic Sowa #include <linux/once.h>
2846234253SHannes Frederic Sowa 
29607ca46eSDavid Howells #include <uapi/linux/net.h>
305770a3fbSDavid Woodhouse 
315770a3fbSDavid Woodhouse struct poll_table_struct;
325770a3fbSDavid Woodhouse struct pipe_inode_info;
335770a3fbSDavid Woodhouse struct inode;
3456b31d1cSAl Viro struct file;
355770a3fbSDavid Woodhouse struct net;
361da177e4SLinus Torvalds 
37*ceb5d58bSEric Dumazet /* Historically, SOCKWQ_ASYNC_NOSPACE & SOCKWQ_ASYNC_WAITDATA were located
38*ceb5d58bSEric Dumazet  * in sock->flags, but moved into sk->sk_wq->flags to be RCU protected.
39*ceb5d58bSEric Dumazet  * Eventually all flags will be in sk->sk_wq_flags.
40*ceb5d58bSEric Dumazet  */
419cd3e072SEric Dumazet #define SOCKWQ_ASYNC_NOSPACE	0
429cd3e072SEric Dumazet #define SOCKWQ_ASYNC_WAITDATA	1
431da177e4SLinus Torvalds #define SOCK_NOSPACE		2
441da177e4SLinus Torvalds #define SOCK_PASSCRED		3
45877ce7c1SCatherine Zhang #define SOCK_PASSSEC		4
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #ifndef ARCH_HAS_SOCKET_TYPES
484dc3b16bSPavel Pisa /**
494dc3b16bSPavel Pisa  * enum sock_type - Socket types
504dc3b16bSPavel Pisa  * @SOCK_STREAM: stream (connection) socket
514dc3b16bSPavel Pisa  * @SOCK_DGRAM: datagram (conn.less) socket
524dc3b16bSPavel Pisa  * @SOCK_RAW: raw socket
534dc3b16bSPavel Pisa  * @SOCK_RDM: reliably-delivered message
544dc3b16bSPavel Pisa  * @SOCK_SEQPACKET: sequential packet socket
558f2709b5SRandy Dunlap  * @SOCK_DCCP: Datagram Congestion Control Protocol socket
564dc3b16bSPavel Pisa  * @SOCK_PACKET: linux specific way of getting packets at the dev level.
574dc3b16bSPavel Pisa  *		  For writing rarp and other similar things on the user level.
581da177e4SLinus Torvalds  *
591da177e4SLinus Torvalds  * When adding some new socket type please
601da177e4SLinus Torvalds  * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
611da177e4SLinus Torvalds  * overrides this enum for binary compat reasons.
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds enum sock_type {
641da177e4SLinus Torvalds 	SOCK_STREAM	= 1,
651da177e4SLinus Torvalds 	SOCK_DGRAM	= 2,
661da177e4SLinus Torvalds 	SOCK_RAW	= 3,
671da177e4SLinus Torvalds 	SOCK_RDM	= 4,
681da177e4SLinus Torvalds 	SOCK_SEQPACKET	= 5,
697c657876SArnaldo Carvalho de Melo 	SOCK_DCCP	= 6,
701da177e4SLinus Torvalds 	SOCK_PACKET	= 10,
711da177e4SLinus Torvalds };
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds #define SOCK_MAX (SOCK_PACKET + 1)
74a677a039SUlrich Drepper /* Mask which covers at least up to SOCK_MASK-1.  The
75a677a039SUlrich Drepper  * remaining bits are used as flags. */
76a677a039SUlrich Drepper #define SOCK_TYPE_MASK 0xf
77a677a039SUlrich Drepper 
78de11defeSUlrich Drepper /* Flags for socket, socketpair, accept4 */
79a677a039SUlrich Drepper #define SOCK_CLOEXEC	O_CLOEXEC
80c019bbc6SUlrich Drepper #ifndef SOCK_NONBLOCK
81c019bbc6SUlrich Drepper #define SOCK_NONBLOCK	O_NONBLOCK
82c019bbc6SUlrich Drepper #endif
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #endif /* ARCH_HAS_SOCKET_TYPES */
851da177e4SLinus Torvalds 
8691cf45f0STrond Myklebust enum sock_shutdown_cmd {
870e9649c1SJean Sacren 	SHUT_RD,
880e9649c1SJean Sacren 	SHUT_WR,
890e9649c1SJean Sacren 	SHUT_RDWR,
9091cf45f0STrond Myklebust };
9191cf45f0STrond Myklebust 
9243815482SEric Dumazet struct socket_wq {
93eaefd110SEric Dumazet 	/* Note: wait MUST be first field of socket_wq */
9443815482SEric Dumazet 	wait_queue_head_t	wait;
9543815482SEric Dumazet 	struct fasync_struct	*fasync_list;
96*ceb5d58bSEric Dumazet 	unsigned long		flags; /* %SOCKWQ_ASYNC_NOSPACE, etc */
9743815482SEric Dumazet 	struct rcu_head		rcu;
9843815482SEric Dumazet } ____cacheline_aligned_in_smp;
9943815482SEric Dumazet 
1001da177e4SLinus Torvalds /**
1011da177e4SLinus Torvalds  *  struct socket - general BSD socket
1024dc3b16bSPavel Pisa  *  @state: socket state (%SS_CONNECTED, etc)
1032c693610SRichard Kennedy  *  @type: socket type (%SOCK_STREAM, etc)
1049cd3e072SEric Dumazet  *  @flags: socket flags (%SOCK_NOSPACE, etc)
1054dc3b16bSPavel Pisa  *  @ops: protocol specific socket operations
1064dc3b16bSPavel Pisa  *  @file: File back pointer for gc
1074dc3b16bSPavel Pisa  *  @sk: internal networking protocol agnostic socket representation
108e2aec372SRandy Dunlap  *  @wq: wait queue for several uses
1091da177e4SLinus Torvalds  */
1101da177e4SLinus Torvalds struct socket {
1111da177e4SLinus Torvalds 	socket_state		state;
11229a020d3SEric Dumazet 
11329a020d3SEric Dumazet 	kmemcheck_bitfield_begin(type);
1142c693610SRichard Kennedy 	short			type;
11529a020d3SEric Dumazet 	kmemcheck_bitfield_end(type);
11629a020d3SEric Dumazet 
1171da177e4SLinus Torvalds 	unsigned long		flags;
11843815482SEric Dumazet 
119eaefd110SEric Dumazet 	struct socket_wq __rcu	*wq;
1208bdd663aSEric Dumazet 
1211da177e4SLinus Torvalds 	struct file		*file;
1221da177e4SLinus Torvalds 	struct sock		*sk;
1238bdd663aSEric Dumazet 	const struct proto_ops	*ops;
1241da177e4SLinus Torvalds };
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds struct vm_area_struct;
1271da177e4SLinus Torvalds struct page;
1281da177e4SLinus Torvalds struct sockaddr;
1291da177e4SLinus Torvalds struct msghdr;
1301da177e4SLinus Torvalds struct module;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds struct proto_ops {
1331da177e4SLinus Torvalds 	int		family;
1341da177e4SLinus Torvalds 	struct module	*owner;
1351da177e4SLinus Torvalds 	int		(*release)   (struct socket *sock);
1361da177e4SLinus Torvalds 	int		(*bind)	     (struct socket *sock,
1371da177e4SLinus Torvalds 				      struct sockaddr *myaddr,
1381da177e4SLinus Torvalds 				      int sockaddr_len);
1391da177e4SLinus Torvalds 	int		(*connect)   (struct socket *sock,
1401da177e4SLinus Torvalds 				      struct sockaddr *vaddr,
1411da177e4SLinus Torvalds 				      int sockaddr_len, int flags);
1421da177e4SLinus Torvalds 	int		(*socketpair)(struct socket *sock1,
1431da177e4SLinus Torvalds 				      struct socket *sock2);
1441da177e4SLinus Torvalds 	int		(*accept)    (struct socket *sock,
1451da177e4SLinus Torvalds 				      struct socket *newsock, int flags);
1461da177e4SLinus Torvalds 	int		(*getname)   (struct socket *sock,
1471da177e4SLinus Torvalds 				      struct sockaddr *addr,
1481da177e4SLinus Torvalds 				      int *sockaddr_len, int peer);
1491da177e4SLinus Torvalds 	unsigned int	(*poll)	     (struct file *file, struct socket *sock,
1501da177e4SLinus Torvalds 				      struct poll_table_struct *wait);
1511da177e4SLinus Torvalds 	int		(*ioctl)     (struct socket *sock, unsigned int cmd,
1521da177e4SLinus Torvalds 				      unsigned long arg);
1531621e094SAlexey Dobriyan #ifdef CONFIG_COMPAT
15489bbfc95SShaun Pereira 	int	 	(*compat_ioctl) (struct socket *sock, unsigned int cmd,
15589bbfc95SShaun Pereira 				      unsigned long arg);
1561621e094SAlexey Dobriyan #endif
1571da177e4SLinus Torvalds 	int		(*listen)    (struct socket *sock, int len);
1581da177e4SLinus Torvalds 	int		(*shutdown)  (struct socket *sock, int flags);
1591da177e4SLinus Torvalds 	int		(*setsockopt)(struct socket *sock, int level,
160b7058842SDavid S. Miller 				      int optname, char __user *optval, unsigned int optlen);
1611da177e4SLinus Torvalds 	int		(*getsockopt)(struct socket *sock, int level,
1621da177e4SLinus Torvalds 				      int optname, char __user *optval, int __user *optlen);
1631621e094SAlexey Dobriyan #ifdef CONFIG_COMPAT
1643fdadf7dSDmitry Mishin 	int		(*compat_setsockopt)(struct socket *sock, int level,
165b7058842SDavid S. Miller 				      int optname, char __user *optval, unsigned int optlen);
1663fdadf7dSDmitry Mishin 	int		(*compat_getsockopt)(struct socket *sock, int level,
1673fdadf7dSDmitry Mishin 				      int optname, char __user *optval, int __user *optlen);
1681621e094SAlexey Dobriyan #endif
1691b784140SYing Xue 	int		(*sendmsg)   (struct socket *sock, struct msghdr *m,
1701b784140SYing Xue 				      size_t total_len);
171f3d33426SHannes Frederic Sowa 	/* Notes for implementing recvmsg:
172f3d33426SHannes Frederic Sowa 	 * ===============================
173f3d33426SHannes Frederic Sowa 	 * msg->msg_namelen should get updated by the recvmsg handlers
174f3d33426SHannes Frederic Sowa 	 * iff msg_name != NULL. It is by default 0 to prevent
175f3d33426SHannes Frederic Sowa 	 * returning uninitialized memory to user space.  The recvfrom
176f3d33426SHannes Frederic Sowa 	 * handlers can assume that msg.msg_name is either NULL or has
177f3d33426SHannes Frederic Sowa 	 * a minimum size of sizeof(struct sockaddr_storage).
178f3d33426SHannes Frederic Sowa 	 */
1791b784140SYing Xue 	int		(*recvmsg)   (struct socket *sock, struct msghdr *m,
1801b784140SYing Xue 				      size_t total_len, int flags);
1811da177e4SLinus Torvalds 	int		(*mmap)	     (struct file *file, struct socket *sock,
1821da177e4SLinus Torvalds 				      struct vm_area_struct * vma);
1831da177e4SLinus Torvalds 	ssize_t		(*sendpage)  (struct socket *sock, struct page *page,
1841da177e4SLinus Torvalds 				      int offset, size_t size, int flags);
1859c55e01cSJens Axboe 	ssize_t 	(*splice_read)(struct socket *sock,  loff_t *ppos,
1869c55e01cSJens Axboe 				       struct pipe_inode_info *pipe, size_t len, unsigned int flags);
18712663bfcSSasha Levin 	int		(*set_peek_off)(struct sock *sk, int val);
1881da177e4SLinus Torvalds };
1891da177e4SLinus Torvalds 
19038bfd8f5SCyrill Gorcunov #define DECLARE_SOCKADDR(type, dst, src)	\
19138bfd8f5SCyrill Gorcunov 	type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
19238bfd8f5SCyrill Gorcunov 
1931da177e4SLinus Torvalds struct net_proto_family {
1941da177e4SLinus Torvalds 	int		family;
1953f378b68SEric Paris 	int		(*create)(struct net *net, struct socket *sock,
1963f378b68SEric Paris 				  int protocol, int kern);
1971da177e4SLinus Torvalds 	struct module	*owner;
1981da177e4SLinus Torvalds };
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds struct iovec;
2011da177e4SLinus Torvalds struct kvec;
2021da177e4SLinus Torvalds 
2038d8ad9d7SPavel Emelyanov enum {
2048d8ad9d7SPavel Emelyanov 	SOCK_WAKE_IO,
2058d8ad9d7SPavel Emelyanov 	SOCK_WAKE_WAITD,
2068d8ad9d7SPavel Emelyanov 	SOCK_WAKE_SPACE,
2078d8ad9d7SPavel Emelyanov 	SOCK_WAKE_URG,
2088d8ad9d7SPavel Emelyanov };
2098d8ad9d7SPavel Emelyanov 
210*ceb5d58bSEric Dumazet int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
2117965bd4dSJoe Perches int sock_register(const struct net_proto_family *fam);
2127965bd4dSJoe Perches void sock_unregister(int family);
2137965bd4dSJoe Perches int __sock_create(struct net *net, int family, int type, int proto,
214721db93aSPavel Emelyanov 		  struct socket **res, int kern);
2157965bd4dSJoe Perches int sock_create(int family, int type, int proto, struct socket **res);
216eeb1bd5cSEric W. Biederman int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
2177965bd4dSJoe Perches int sock_create_lite(int family, int type, int proto, struct socket **res);
2187965bd4dSJoe Perches void sock_release(struct socket *sock);
219d8725c86SAl Viro int sock_sendmsg(struct socket *sock, struct msghdr *msg);
2207965bd4dSJoe Perches int sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
2217965bd4dSJoe Perches 		 int flags);
2227965bd4dSJoe Perches struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
2237965bd4dSJoe Perches struct socket *sockfd_lookup(int fd, int *err);
2247965bd4dSJoe Perches struct socket *sock_from_file(struct file *file, int *err);
2251da177e4SLinus Torvalds #define		     sockfd_put(sock) fput(sock->file)
2267965bd4dSJoe Perches int net_ratelimit(void);
227aaa248f6SStephen Hemminger 
2283a3bfb61SJoe Perches #define net_ratelimited_function(function, ...)			\
2293a3bfb61SJoe Perches do {								\
2303a3bfb61SJoe Perches 	if (net_ratelimit())					\
2313a3bfb61SJoe Perches 		function(__VA_ARGS__);				\
2323a3bfb61SJoe Perches } while (0)
2333a3bfb61SJoe Perches 
2343a3bfb61SJoe Perches #define net_emerg_ratelimited(fmt, ...)				\
2353a3bfb61SJoe Perches 	net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
2363a3bfb61SJoe Perches #define net_alert_ratelimited(fmt, ...)				\
2373a3bfb61SJoe Perches 	net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
2383a3bfb61SJoe Perches #define net_crit_ratelimited(fmt, ...)				\
2393a3bfb61SJoe Perches 	net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
2403a3bfb61SJoe Perches #define net_err_ratelimited(fmt, ...)				\
2413a3bfb61SJoe Perches 	net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
2423a3bfb61SJoe Perches #define net_notice_ratelimited(fmt, ...)			\
2433a3bfb61SJoe Perches 	net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
2443a3bfb61SJoe Perches #define net_warn_ratelimited(fmt, ...)				\
2453a3bfb61SJoe Perches 	net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
2463a3bfb61SJoe Perches #define net_info_ratelimited(fmt, ...)				\
2473a3bfb61SJoe Perches 	net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
248d92cff89SJason A. Donenfeld #if defined(DEBUG)
2493a3bfb61SJoe Perches #define net_dbg_ratelimited(fmt, ...)				\
2503a3bfb61SJoe Perches 	net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
251d92cff89SJason A. Donenfeld #else
252d92cff89SJason A. Donenfeld #define net_dbg_ratelimited(fmt, ...)				\
253d92cff89SJason A. Donenfeld 	do {							\
254d92cff89SJason A. Donenfeld 		if (0)						\
255d92cff89SJason A. Donenfeld 			no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
256d92cff89SJason A. Donenfeld 	} while (0)
257d92cff89SJason A. Donenfeld #endif
2583a3bfb61SJoe Perches 
259a48e4292SHannes Frederic Sowa #define net_get_random_once(buf, nbytes)			\
26046234253SHannes Frederic Sowa 	get_random_once((buf), (nbytes))
261a48e4292SHannes Frederic Sowa 
2627965bd4dSJoe Perches int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
2637965bd4dSJoe Perches 		   size_t num, size_t len);
2647965bd4dSJoe Perches int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
2657965bd4dSJoe Perches 		   size_t num, size_t len, int flags);
2661da177e4SLinus Torvalds 
2677965bd4dSJoe Perches int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen);
2687965bd4dSJoe Perches int kernel_listen(struct socket *sock, int backlog);
2697965bd4dSJoe Perches int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
2707965bd4dSJoe Perches int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
271ac5a488eSSridhar Samudrala 		   int flags);
2727965bd4dSJoe Perches int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
273ac5a488eSSridhar Samudrala 		       int *addrlen);
2747965bd4dSJoe Perches int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
275ac5a488eSSridhar Samudrala 		       int *addrlen);
2767965bd4dSJoe Perches int kernel_getsockopt(struct socket *sock, int level, int optname, char *optval,
2777965bd4dSJoe Perches 		      int *optlen);
2787965bd4dSJoe Perches int kernel_setsockopt(struct socket *sock, int level, int optname, char *optval,
2797965bd4dSJoe Perches 		      unsigned int optlen);
2807965bd4dSJoe Perches int kernel_sendpage(struct socket *sock, struct page *page, int offset,
281ac5a488eSSridhar Samudrala 		    size_t size, int flags);
2827965bd4dSJoe Perches int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg);
2837965bd4dSJoe Perches int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
284ac5a488eSSridhar Samudrala 
2851da177e4SLinus Torvalds #define MODULE_ALIAS_NETPROTO(proto) \
2861da177e4SLinus Torvalds 	MODULE_ALIAS("net-pf-" __stringify(proto))
2871da177e4SLinus Torvalds 
2884fdb3bb7SHarald Welte #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
2894fdb3bb7SHarald Welte 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
2904fdb3bb7SHarald Welte 
291305e1e96SJean Delvare #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
292305e1e96SJean Delvare 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
293305e1e96SJean Delvare 		     "-type-" __stringify(type))
294305e1e96SJean Delvare 
2952033e9bfSNeil Horman #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
2962033e9bfSNeil Horman 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
2972033e9bfSNeil Horman 		     name)
2981da177e4SLinus Torvalds #endif	/* _LINUX_NET_H */
299