xref: /linux/include/linux/net.h (revision 9cd3e072b0be17446e37d7414eac8a3499e0601e)
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*9cd3e072SEric Dumazet #define SOCKWQ_ASYNC_NOSPACE	0
38*9cd3e072SEric Dumazet #define SOCKWQ_ASYNC_WAITDATA	1
391da177e4SLinus Torvalds #define SOCK_NOSPACE		2
401da177e4SLinus Torvalds #define SOCK_PASSCRED		3
41877ce7c1SCatherine Zhang #define SOCK_PASSSEC		4
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #ifndef ARCH_HAS_SOCKET_TYPES
444dc3b16bSPavel Pisa /**
454dc3b16bSPavel Pisa  * enum sock_type - Socket types
464dc3b16bSPavel Pisa  * @SOCK_STREAM: stream (connection) socket
474dc3b16bSPavel Pisa  * @SOCK_DGRAM: datagram (conn.less) socket
484dc3b16bSPavel Pisa  * @SOCK_RAW: raw socket
494dc3b16bSPavel Pisa  * @SOCK_RDM: reliably-delivered message
504dc3b16bSPavel Pisa  * @SOCK_SEQPACKET: sequential packet socket
518f2709b5SRandy Dunlap  * @SOCK_DCCP: Datagram Congestion Control Protocol socket
524dc3b16bSPavel Pisa  * @SOCK_PACKET: linux specific way of getting packets at the dev level.
534dc3b16bSPavel Pisa  *		  For writing rarp and other similar things on the user level.
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * When adding some new socket type please
561da177e4SLinus Torvalds  * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
571da177e4SLinus Torvalds  * overrides this enum for binary compat reasons.
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds enum sock_type {
601da177e4SLinus Torvalds 	SOCK_STREAM	= 1,
611da177e4SLinus Torvalds 	SOCK_DGRAM	= 2,
621da177e4SLinus Torvalds 	SOCK_RAW	= 3,
631da177e4SLinus Torvalds 	SOCK_RDM	= 4,
641da177e4SLinus Torvalds 	SOCK_SEQPACKET	= 5,
657c657876SArnaldo Carvalho de Melo 	SOCK_DCCP	= 6,
661da177e4SLinus Torvalds 	SOCK_PACKET	= 10,
671da177e4SLinus Torvalds };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds #define SOCK_MAX (SOCK_PACKET + 1)
70a677a039SUlrich Drepper /* Mask which covers at least up to SOCK_MASK-1.  The
71a677a039SUlrich Drepper  * remaining bits are used as flags. */
72a677a039SUlrich Drepper #define SOCK_TYPE_MASK 0xf
73a677a039SUlrich Drepper 
74de11defeSUlrich Drepper /* Flags for socket, socketpair, accept4 */
75a677a039SUlrich Drepper #define SOCK_CLOEXEC	O_CLOEXEC
76c019bbc6SUlrich Drepper #ifndef SOCK_NONBLOCK
77c019bbc6SUlrich Drepper #define SOCK_NONBLOCK	O_NONBLOCK
78c019bbc6SUlrich Drepper #endif
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds #endif /* ARCH_HAS_SOCKET_TYPES */
811da177e4SLinus Torvalds 
8291cf45f0STrond Myklebust enum sock_shutdown_cmd {
830e9649c1SJean Sacren 	SHUT_RD,
840e9649c1SJean Sacren 	SHUT_WR,
850e9649c1SJean Sacren 	SHUT_RDWR,
8691cf45f0STrond Myklebust };
8791cf45f0STrond Myklebust 
8843815482SEric Dumazet struct socket_wq {
89eaefd110SEric Dumazet 	/* Note: wait MUST be first field of socket_wq */
9043815482SEric Dumazet 	wait_queue_head_t	wait;
9143815482SEric Dumazet 	struct fasync_struct	*fasync_list;
9243815482SEric Dumazet 	struct rcu_head		rcu;
9343815482SEric Dumazet } ____cacheline_aligned_in_smp;
9443815482SEric Dumazet 
951da177e4SLinus Torvalds /**
961da177e4SLinus Torvalds  *  struct socket - general BSD socket
974dc3b16bSPavel Pisa  *  @state: socket state (%SS_CONNECTED, etc)
982c693610SRichard Kennedy  *  @type: socket type (%SOCK_STREAM, etc)
99*9cd3e072SEric Dumazet  *  @flags: socket flags (%SOCK_NOSPACE, etc)
1004dc3b16bSPavel Pisa  *  @ops: protocol specific socket operations
1014dc3b16bSPavel Pisa  *  @file: File back pointer for gc
1024dc3b16bSPavel Pisa  *  @sk: internal networking protocol agnostic socket representation
103e2aec372SRandy Dunlap  *  @wq: wait queue for several uses
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds struct socket {
1061da177e4SLinus Torvalds 	socket_state		state;
10729a020d3SEric Dumazet 
10829a020d3SEric Dumazet 	kmemcheck_bitfield_begin(type);
1092c693610SRichard Kennedy 	short			type;
11029a020d3SEric Dumazet 	kmemcheck_bitfield_end(type);
11129a020d3SEric Dumazet 
1121da177e4SLinus Torvalds 	unsigned long		flags;
11343815482SEric Dumazet 
114eaefd110SEric Dumazet 	struct socket_wq __rcu	*wq;
1158bdd663aSEric Dumazet 
1161da177e4SLinus Torvalds 	struct file		*file;
1171da177e4SLinus Torvalds 	struct sock		*sk;
1188bdd663aSEric Dumazet 	const struct proto_ops	*ops;
1191da177e4SLinus Torvalds };
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds struct vm_area_struct;
1221da177e4SLinus Torvalds struct page;
1231da177e4SLinus Torvalds struct sockaddr;
1241da177e4SLinus Torvalds struct msghdr;
1251da177e4SLinus Torvalds struct module;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds struct proto_ops {
1281da177e4SLinus Torvalds 	int		family;
1291da177e4SLinus Torvalds 	struct module	*owner;
1301da177e4SLinus Torvalds 	int		(*release)   (struct socket *sock);
1311da177e4SLinus Torvalds 	int		(*bind)	     (struct socket *sock,
1321da177e4SLinus Torvalds 				      struct sockaddr *myaddr,
1331da177e4SLinus Torvalds 				      int sockaddr_len);
1341da177e4SLinus Torvalds 	int		(*connect)   (struct socket *sock,
1351da177e4SLinus Torvalds 				      struct sockaddr *vaddr,
1361da177e4SLinus Torvalds 				      int sockaddr_len, int flags);
1371da177e4SLinus Torvalds 	int		(*socketpair)(struct socket *sock1,
1381da177e4SLinus Torvalds 				      struct socket *sock2);
1391da177e4SLinus Torvalds 	int		(*accept)    (struct socket *sock,
1401da177e4SLinus Torvalds 				      struct socket *newsock, int flags);
1411da177e4SLinus Torvalds 	int		(*getname)   (struct socket *sock,
1421da177e4SLinus Torvalds 				      struct sockaddr *addr,
1431da177e4SLinus Torvalds 				      int *sockaddr_len, int peer);
1441da177e4SLinus Torvalds 	unsigned int	(*poll)	     (struct file *file, struct socket *sock,
1451da177e4SLinus Torvalds 				      struct poll_table_struct *wait);
1461da177e4SLinus Torvalds 	int		(*ioctl)     (struct socket *sock, unsigned int cmd,
1471da177e4SLinus Torvalds 				      unsigned long arg);
1481621e094SAlexey Dobriyan #ifdef CONFIG_COMPAT
14989bbfc95SShaun Pereira 	int	 	(*compat_ioctl) (struct socket *sock, unsigned int cmd,
15089bbfc95SShaun Pereira 				      unsigned long arg);
1511621e094SAlexey Dobriyan #endif
1521da177e4SLinus Torvalds 	int		(*listen)    (struct socket *sock, int len);
1531da177e4SLinus Torvalds 	int		(*shutdown)  (struct socket *sock, int flags);
1541da177e4SLinus Torvalds 	int		(*setsockopt)(struct socket *sock, int level,
155b7058842SDavid S. Miller 				      int optname, char __user *optval, unsigned int optlen);
1561da177e4SLinus Torvalds 	int		(*getsockopt)(struct socket *sock, int level,
1571da177e4SLinus Torvalds 				      int optname, char __user *optval, int __user *optlen);
1581621e094SAlexey Dobriyan #ifdef CONFIG_COMPAT
1593fdadf7dSDmitry Mishin 	int		(*compat_setsockopt)(struct socket *sock, int level,
160b7058842SDavid S. Miller 				      int optname, char __user *optval, unsigned int optlen);
1613fdadf7dSDmitry Mishin 	int		(*compat_getsockopt)(struct socket *sock, int level,
1623fdadf7dSDmitry Mishin 				      int optname, char __user *optval, int __user *optlen);
1631621e094SAlexey Dobriyan #endif
1641b784140SYing Xue 	int		(*sendmsg)   (struct socket *sock, struct msghdr *m,
1651b784140SYing Xue 				      size_t total_len);
166f3d33426SHannes Frederic Sowa 	/* Notes for implementing recvmsg:
167f3d33426SHannes Frederic Sowa 	 * ===============================
168f3d33426SHannes Frederic Sowa 	 * msg->msg_namelen should get updated by the recvmsg handlers
169f3d33426SHannes Frederic Sowa 	 * iff msg_name != NULL. It is by default 0 to prevent
170f3d33426SHannes Frederic Sowa 	 * returning uninitialized memory to user space.  The recvfrom
171f3d33426SHannes Frederic Sowa 	 * handlers can assume that msg.msg_name is either NULL or has
172f3d33426SHannes Frederic Sowa 	 * a minimum size of sizeof(struct sockaddr_storage).
173f3d33426SHannes Frederic Sowa 	 */
1741b784140SYing Xue 	int		(*recvmsg)   (struct socket *sock, struct msghdr *m,
1751b784140SYing Xue 				      size_t total_len, int flags);
1761da177e4SLinus Torvalds 	int		(*mmap)	     (struct file *file, struct socket *sock,
1771da177e4SLinus Torvalds 				      struct vm_area_struct * vma);
1781da177e4SLinus Torvalds 	ssize_t		(*sendpage)  (struct socket *sock, struct page *page,
1791da177e4SLinus Torvalds 				      int offset, size_t size, int flags);
1809c55e01cSJens Axboe 	ssize_t 	(*splice_read)(struct socket *sock,  loff_t *ppos,
1819c55e01cSJens Axboe 				       struct pipe_inode_info *pipe, size_t len, unsigned int flags);
18212663bfcSSasha Levin 	int		(*set_peek_off)(struct sock *sk, int val);
1831da177e4SLinus Torvalds };
1841da177e4SLinus Torvalds 
18538bfd8f5SCyrill Gorcunov #define DECLARE_SOCKADDR(type, dst, src)	\
18638bfd8f5SCyrill Gorcunov 	type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
18738bfd8f5SCyrill Gorcunov 
1881da177e4SLinus Torvalds struct net_proto_family {
1891da177e4SLinus Torvalds 	int		family;
1903f378b68SEric Paris 	int		(*create)(struct net *net, struct socket *sock,
1913f378b68SEric Paris 				  int protocol, int kern);
1921da177e4SLinus Torvalds 	struct module	*owner;
1931da177e4SLinus Torvalds };
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds struct iovec;
1961da177e4SLinus Torvalds struct kvec;
1971da177e4SLinus Torvalds 
1988d8ad9d7SPavel Emelyanov enum {
1998d8ad9d7SPavel Emelyanov 	SOCK_WAKE_IO,
2008d8ad9d7SPavel Emelyanov 	SOCK_WAKE_WAITD,
2018d8ad9d7SPavel Emelyanov 	SOCK_WAKE_SPACE,
2028d8ad9d7SPavel Emelyanov 	SOCK_WAKE_URG,
2038d8ad9d7SPavel Emelyanov };
2048d8ad9d7SPavel Emelyanov 
2057965bd4dSJoe Perches int sock_wake_async(struct socket *sk, int how, int band);
2067965bd4dSJoe Perches int sock_register(const struct net_proto_family *fam);
2077965bd4dSJoe Perches void sock_unregister(int family);
2087965bd4dSJoe Perches int __sock_create(struct net *net, int family, int type, int proto,
209721db93aSPavel Emelyanov 		  struct socket **res, int kern);
2107965bd4dSJoe Perches int sock_create(int family, int type, int proto, struct socket **res);
211eeb1bd5cSEric W. Biederman int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
2127965bd4dSJoe Perches int sock_create_lite(int family, int type, int proto, struct socket **res);
2137965bd4dSJoe Perches void sock_release(struct socket *sock);
214d8725c86SAl Viro int sock_sendmsg(struct socket *sock, struct msghdr *msg);
2157965bd4dSJoe Perches int sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
2167965bd4dSJoe Perches 		 int flags);
2177965bd4dSJoe Perches struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
2187965bd4dSJoe Perches struct socket *sockfd_lookup(int fd, int *err);
2197965bd4dSJoe Perches struct socket *sock_from_file(struct file *file, int *err);
2201da177e4SLinus Torvalds #define		     sockfd_put(sock) fput(sock->file)
2217965bd4dSJoe Perches int net_ratelimit(void);
222aaa248f6SStephen Hemminger 
2233a3bfb61SJoe Perches #define net_ratelimited_function(function, ...)			\
2243a3bfb61SJoe Perches do {								\
2253a3bfb61SJoe Perches 	if (net_ratelimit())					\
2263a3bfb61SJoe Perches 		function(__VA_ARGS__);				\
2273a3bfb61SJoe Perches } while (0)
2283a3bfb61SJoe Perches 
2293a3bfb61SJoe Perches #define net_emerg_ratelimited(fmt, ...)				\
2303a3bfb61SJoe Perches 	net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
2313a3bfb61SJoe Perches #define net_alert_ratelimited(fmt, ...)				\
2323a3bfb61SJoe Perches 	net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
2333a3bfb61SJoe Perches #define net_crit_ratelimited(fmt, ...)				\
2343a3bfb61SJoe Perches 	net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
2353a3bfb61SJoe Perches #define net_err_ratelimited(fmt, ...)				\
2363a3bfb61SJoe Perches 	net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
2373a3bfb61SJoe Perches #define net_notice_ratelimited(fmt, ...)			\
2383a3bfb61SJoe Perches 	net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
2393a3bfb61SJoe Perches #define net_warn_ratelimited(fmt, ...)				\
2403a3bfb61SJoe Perches 	net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
2413a3bfb61SJoe Perches #define net_info_ratelimited(fmt, ...)				\
2423a3bfb61SJoe Perches 	net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
243d92cff89SJason A. Donenfeld #if defined(DEBUG)
2443a3bfb61SJoe Perches #define net_dbg_ratelimited(fmt, ...)				\
2453a3bfb61SJoe Perches 	net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
246d92cff89SJason A. Donenfeld #else
247d92cff89SJason A. Donenfeld #define net_dbg_ratelimited(fmt, ...)				\
248d92cff89SJason A. Donenfeld 	do {							\
249d92cff89SJason A. Donenfeld 		if (0)						\
250d92cff89SJason A. Donenfeld 			no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
251d92cff89SJason A. Donenfeld 	} while (0)
252d92cff89SJason A. Donenfeld #endif
2533a3bfb61SJoe Perches 
254a48e4292SHannes Frederic Sowa #define net_get_random_once(buf, nbytes)			\
25546234253SHannes Frederic Sowa 	get_random_once((buf), (nbytes))
256a48e4292SHannes Frederic Sowa 
2577965bd4dSJoe Perches int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
2587965bd4dSJoe Perches 		   size_t num, size_t len);
2597965bd4dSJoe Perches int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
2607965bd4dSJoe Perches 		   size_t num, size_t len, int flags);
2611da177e4SLinus Torvalds 
2627965bd4dSJoe Perches int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen);
2637965bd4dSJoe Perches int kernel_listen(struct socket *sock, int backlog);
2647965bd4dSJoe Perches int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
2657965bd4dSJoe Perches int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
266ac5a488eSSridhar Samudrala 		   int flags);
2677965bd4dSJoe Perches int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
268ac5a488eSSridhar Samudrala 		       int *addrlen);
2697965bd4dSJoe Perches int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
270ac5a488eSSridhar Samudrala 		       int *addrlen);
2717965bd4dSJoe Perches int kernel_getsockopt(struct socket *sock, int level, int optname, char *optval,
2727965bd4dSJoe Perches 		      int *optlen);
2737965bd4dSJoe Perches int kernel_setsockopt(struct socket *sock, int level, int optname, char *optval,
2747965bd4dSJoe Perches 		      unsigned int optlen);
2757965bd4dSJoe Perches int kernel_sendpage(struct socket *sock, struct page *page, int offset,
276ac5a488eSSridhar Samudrala 		    size_t size, int flags);
2777965bd4dSJoe Perches int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg);
2787965bd4dSJoe Perches int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
279ac5a488eSSridhar Samudrala 
2801da177e4SLinus Torvalds #define MODULE_ALIAS_NETPROTO(proto) \
2811da177e4SLinus Torvalds 	MODULE_ALIAS("net-pf-" __stringify(proto))
2821da177e4SLinus Torvalds 
2834fdb3bb7SHarald Welte #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
2844fdb3bb7SHarald Welte 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
2854fdb3bb7SHarald Welte 
286305e1e96SJean Delvare #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
287305e1e96SJean Delvare 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
288305e1e96SJean Delvare 		     "-type-" __stringify(type))
289305e1e96SJean Delvare 
2902033e9bfSNeil Horman #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
2912033e9bfSNeil Horman 	MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
2922033e9bfSNeil Horman 		     name)
2931da177e4SLinus Torvalds #endif	/* _LINUX_NET_H */
294