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> 280294b625STom Herbert #include <linux/fs.h> 2946234253SHannes Frederic Sowa 30607ca46eSDavid Howells #include <uapi/linux/net.h> 315770a3fbSDavid Woodhouse 325770a3fbSDavid Woodhouse struct poll_table_struct; 335770a3fbSDavid Woodhouse struct pipe_inode_info; 345770a3fbSDavid Woodhouse struct inode; 3556b31d1cSAl Viro struct file; 365770a3fbSDavid Woodhouse struct net; 371da177e4SLinus Torvalds 38ceb5d58bSEric Dumazet /* Historically, SOCKWQ_ASYNC_NOSPACE & SOCKWQ_ASYNC_WAITDATA were located 39ceb5d58bSEric Dumazet * in sock->flags, but moved into sk->sk_wq->flags to be RCU protected. 40ceb5d58bSEric Dumazet * Eventually all flags will be in sk->sk_wq_flags. 41ceb5d58bSEric Dumazet */ 429cd3e072SEric Dumazet #define SOCKWQ_ASYNC_NOSPACE 0 439cd3e072SEric Dumazet #define SOCKWQ_ASYNC_WAITDATA 1 441da177e4SLinus Torvalds #define SOCK_NOSPACE 2 451da177e4SLinus Torvalds #define SOCK_PASSCRED 3 46877ce7c1SCatherine Zhang #define SOCK_PASSSEC 4 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #ifndef ARCH_HAS_SOCKET_TYPES 494dc3b16bSPavel Pisa /** 504dc3b16bSPavel Pisa * enum sock_type - Socket types 514dc3b16bSPavel Pisa * @SOCK_STREAM: stream (connection) socket 524dc3b16bSPavel Pisa * @SOCK_DGRAM: datagram (conn.less) socket 534dc3b16bSPavel Pisa * @SOCK_RAW: raw socket 544dc3b16bSPavel Pisa * @SOCK_RDM: reliably-delivered message 554dc3b16bSPavel Pisa * @SOCK_SEQPACKET: sequential packet socket 568f2709b5SRandy Dunlap * @SOCK_DCCP: Datagram Congestion Control Protocol socket 574dc3b16bSPavel Pisa * @SOCK_PACKET: linux specific way of getting packets at the dev level. 584dc3b16bSPavel Pisa * For writing rarp and other similar things on the user level. 591da177e4SLinus Torvalds * 601da177e4SLinus Torvalds * When adding some new socket type please 611da177e4SLinus Torvalds * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS 621da177e4SLinus Torvalds * overrides this enum for binary compat reasons. 631da177e4SLinus Torvalds */ 641da177e4SLinus Torvalds enum sock_type { 651da177e4SLinus Torvalds SOCK_STREAM = 1, 661da177e4SLinus Torvalds SOCK_DGRAM = 2, 671da177e4SLinus Torvalds SOCK_RAW = 3, 681da177e4SLinus Torvalds SOCK_RDM = 4, 691da177e4SLinus Torvalds SOCK_SEQPACKET = 5, 707c657876SArnaldo Carvalho de Melo SOCK_DCCP = 6, 711da177e4SLinus Torvalds SOCK_PACKET = 10, 721da177e4SLinus Torvalds }; 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds #define SOCK_MAX (SOCK_PACKET + 1) 75a677a039SUlrich Drepper /* Mask which covers at least up to SOCK_MASK-1. The 76a677a039SUlrich Drepper * remaining bits are used as flags. */ 77a677a039SUlrich Drepper #define SOCK_TYPE_MASK 0xf 78a677a039SUlrich Drepper 79de11defeSUlrich Drepper /* Flags for socket, socketpair, accept4 */ 80a677a039SUlrich Drepper #define SOCK_CLOEXEC O_CLOEXEC 81c019bbc6SUlrich Drepper #ifndef SOCK_NONBLOCK 82c019bbc6SUlrich Drepper #define SOCK_NONBLOCK O_NONBLOCK 83c019bbc6SUlrich Drepper #endif 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds #endif /* ARCH_HAS_SOCKET_TYPES */ 861da177e4SLinus Torvalds 8791cf45f0STrond Myklebust enum sock_shutdown_cmd { 880e9649c1SJean Sacren SHUT_RD, 890e9649c1SJean Sacren SHUT_WR, 900e9649c1SJean Sacren SHUT_RDWR, 9191cf45f0STrond Myklebust }; 9291cf45f0STrond Myklebust 9343815482SEric Dumazet struct socket_wq { 94eaefd110SEric Dumazet /* Note: wait MUST be first field of socket_wq */ 9543815482SEric Dumazet wait_queue_head_t wait; 9643815482SEric Dumazet struct fasync_struct *fasync_list; 97ceb5d58bSEric Dumazet unsigned long flags; /* %SOCKWQ_ASYNC_NOSPACE, etc */ 9843815482SEric Dumazet struct rcu_head rcu; 9943815482SEric Dumazet } ____cacheline_aligned_in_smp; 10043815482SEric Dumazet 1011da177e4SLinus Torvalds /** 1021da177e4SLinus Torvalds * struct socket - general BSD socket 1034dc3b16bSPavel Pisa * @state: socket state (%SS_CONNECTED, etc) 1042c693610SRichard Kennedy * @type: socket type (%SOCK_STREAM, etc) 1059cd3e072SEric Dumazet * @flags: socket flags (%SOCK_NOSPACE, etc) 1064dc3b16bSPavel Pisa * @ops: protocol specific socket operations 1074dc3b16bSPavel Pisa * @file: File back pointer for gc 1084dc3b16bSPavel Pisa * @sk: internal networking protocol agnostic socket representation 109e2aec372SRandy Dunlap * @wq: wait queue for several uses 1101da177e4SLinus Torvalds */ 1111da177e4SLinus Torvalds struct socket { 1121da177e4SLinus Torvalds socket_state state; 11329a020d3SEric Dumazet 11429a020d3SEric Dumazet kmemcheck_bitfield_begin(type); 1152c693610SRichard Kennedy short type; 11629a020d3SEric Dumazet kmemcheck_bitfield_end(type); 11729a020d3SEric Dumazet 1181da177e4SLinus Torvalds unsigned long flags; 11943815482SEric Dumazet 120eaefd110SEric Dumazet struct socket_wq __rcu *wq; 1218bdd663aSEric Dumazet 1221da177e4SLinus Torvalds struct file *file; 1231da177e4SLinus Torvalds struct sock *sk; 1248bdd663aSEric Dumazet const struct proto_ops *ops; 1251da177e4SLinus Torvalds }; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds struct vm_area_struct; 1281da177e4SLinus Torvalds struct page; 1291da177e4SLinus Torvalds struct sockaddr; 1301da177e4SLinus Torvalds struct msghdr; 1311da177e4SLinus Torvalds struct module; 1320294b625STom Herbert struct sk_buff; 1330294b625STom Herbert typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *, 1340294b625STom Herbert unsigned int, size_t); 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds struct proto_ops { 1371da177e4SLinus Torvalds int family; 1381da177e4SLinus Torvalds struct module *owner; 1391da177e4SLinus Torvalds int (*release) (struct socket *sock); 1401da177e4SLinus Torvalds int (*bind) (struct socket *sock, 1411da177e4SLinus Torvalds struct sockaddr *myaddr, 1421da177e4SLinus Torvalds int sockaddr_len); 1431da177e4SLinus Torvalds int (*connect) (struct socket *sock, 1441da177e4SLinus Torvalds struct sockaddr *vaddr, 1451da177e4SLinus Torvalds int sockaddr_len, int flags); 1461da177e4SLinus Torvalds int (*socketpair)(struct socket *sock1, 1471da177e4SLinus Torvalds struct socket *sock2); 1481da177e4SLinus Torvalds int (*accept) (struct socket *sock, 149*cdfbabfbSDavid Howells struct socket *newsock, int flags, bool kern); 1501da177e4SLinus Torvalds int (*getname) (struct socket *sock, 1511da177e4SLinus Torvalds struct sockaddr *addr, 1521da177e4SLinus Torvalds int *sockaddr_len, int peer); 1531da177e4SLinus Torvalds unsigned int (*poll) (struct file *file, struct socket *sock, 1541da177e4SLinus Torvalds struct poll_table_struct *wait); 1551da177e4SLinus Torvalds int (*ioctl) (struct socket *sock, unsigned int cmd, 1561da177e4SLinus Torvalds unsigned long arg); 1571621e094SAlexey Dobriyan #ifdef CONFIG_COMPAT 15889bbfc95SShaun Pereira int (*compat_ioctl) (struct socket *sock, unsigned int cmd, 15989bbfc95SShaun Pereira unsigned long arg); 1601621e094SAlexey Dobriyan #endif 1611da177e4SLinus Torvalds int (*listen) (struct socket *sock, int len); 1621da177e4SLinus Torvalds int (*shutdown) (struct socket *sock, int flags); 1631da177e4SLinus Torvalds int (*setsockopt)(struct socket *sock, int level, 164b7058842SDavid S. Miller int optname, char __user *optval, unsigned int optlen); 1651da177e4SLinus Torvalds int (*getsockopt)(struct socket *sock, int level, 1661da177e4SLinus Torvalds int optname, char __user *optval, int __user *optlen); 1671621e094SAlexey Dobriyan #ifdef CONFIG_COMPAT 1683fdadf7dSDmitry Mishin int (*compat_setsockopt)(struct socket *sock, int level, 169b7058842SDavid S. Miller int optname, char __user *optval, unsigned int optlen); 1703fdadf7dSDmitry Mishin int (*compat_getsockopt)(struct socket *sock, int level, 1713fdadf7dSDmitry Mishin int optname, char __user *optval, int __user *optlen); 1721621e094SAlexey Dobriyan #endif 1731b784140SYing Xue int (*sendmsg) (struct socket *sock, struct msghdr *m, 1741b784140SYing Xue size_t total_len); 175f3d33426SHannes Frederic Sowa /* Notes for implementing recvmsg: 176f3d33426SHannes Frederic Sowa * =============================== 177f3d33426SHannes Frederic Sowa * msg->msg_namelen should get updated by the recvmsg handlers 178f3d33426SHannes Frederic Sowa * iff msg_name != NULL. It is by default 0 to prevent 179f3d33426SHannes Frederic Sowa * returning uninitialized memory to user space. The recvfrom 180f3d33426SHannes Frederic Sowa * handlers can assume that msg.msg_name is either NULL or has 181f3d33426SHannes Frederic Sowa * a minimum size of sizeof(struct sockaddr_storage). 182f3d33426SHannes Frederic Sowa */ 1831b784140SYing Xue int (*recvmsg) (struct socket *sock, struct msghdr *m, 1841b784140SYing Xue size_t total_len, int flags); 1851da177e4SLinus Torvalds int (*mmap) (struct file *file, struct socket *sock, 1861da177e4SLinus Torvalds struct vm_area_struct * vma); 1871da177e4SLinus Torvalds ssize_t (*sendpage) (struct socket *sock, struct page *page, 1881da177e4SLinus Torvalds int offset, size_t size, int flags); 1899c55e01cSJens Axboe ssize_t (*splice_read)(struct socket *sock, loff_t *ppos, 1909c55e01cSJens Axboe struct pipe_inode_info *pipe, size_t len, unsigned int flags); 19112663bfcSSasha Levin int (*set_peek_off)(struct sock *sk, int val); 1921576d986SJason Wang int (*peek_len)(struct socket *sock); 1930294b625STom Herbert int (*read_sock)(struct sock *sk, read_descriptor_t *desc, 1940294b625STom Herbert sk_read_actor_t recv_actor); 1951da177e4SLinus Torvalds }; 1961da177e4SLinus Torvalds 19738bfd8f5SCyrill Gorcunov #define DECLARE_SOCKADDR(type, dst, src) \ 19838bfd8f5SCyrill Gorcunov type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; }) 19938bfd8f5SCyrill Gorcunov 2001da177e4SLinus Torvalds struct net_proto_family { 2011da177e4SLinus Torvalds int family; 2023f378b68SEric Paris int (*create)(struct net *net, struct socket *sock, 2033f378b68SEric Paris int protocol, int kern); 2041da177e4SLinus Torvalds struct module *owner; 2051da177e4SLinus Torvalds }; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds struct iovec; 2081da177e4SLinus Torvalds struct kvec; 2091da177e4SLinus Torvalds 2108d8ad9d7SPavel Emelyanov enum { 2118d8ad9d7SPavel Emelyanov SOCK_WAKE_IO, 2128d8ad9d7SPavel Emelyanov SOCK_WAKE_WAITD, 2138d8ad9d7SPavel Emelyanov SOCK_WAKE_SPACE, 2148d8ad9d7SPavel Emelyanov SOCK_WAKE_URG, 2158d8ad9d7SPavel Emelyanov }; 2168d8ad9d7SPavel Emelyanov 217ceb5d58bSEric Dumazet int sock_wake_async(struct socket_wq *sk_wq, int how, int band); 2187965bd4dSJoe Perches int sock_register(const struct net_proto_family *fam); 2197965bd4dSJoe Perches void sock_unregister(int family); 2207965bd4dSJoe Perches int __sock_create(struct net *net, int family, int type, int proto, 221721db93aSPavel Emelyanov struct socket **res, int kern); 2227965bd4dSJoe Perches int sock_create(int family, int type, int proto, struct socket **res); 223eeb1bd5cSEric W. Biederman int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res); 2247965bd4dSJoe Perches int sock_create_lite(int family, int type, int proto, struct socket **res); 225f4a00aacSTom Herbert struct socket *sock_alloc(void); 2267965bd4dSJoe Perches void sock_release(struct socket *sock); 227d8725c86SAl Viro int sock_sendmsg(struct socket *sock, struct msghdr *msg); 2282da62906SAl Viro int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags); 2297965bd4dSJoe Perches struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname); 2307965bd4dSJoe Perches struct socket *sockfd_lookup(int fd, int *err); 2317965bd4dSJoe Perches struct socket *sock_from_file(struct file *file, int *err); 2321da177e4SLinus Torvalds #define sockfd_put(sock) fput(sock->file) 2337965bd4dSJoe Perches int net_ratelimit(void); 234aaa248f6SStephen Hemminger 2353a3bfb61SJoe Perches #define net_ratelimited_function(function, ...) \ 2363a3bfb61SJoe Perches do { \ 2373a3bfb61SJoe Perches if (net_ratelimit()) \ 2383a3bfb61SJoe Perches function(__VA_ARGS__); \ 2393a3bfb61SJoe Perches } while (0) 2403a3bfb61SJoe Perches 2413a3bfb61SJoe Perches #define net_emerg_ratelimited(fmt, ...) \ 2423a3bfb61SJoe Perches net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__) 2433a3bfb61SJoe Perches #define net_alert_ratelimited(fmt, ...) \ 2443a3bfb61SJoe Perches net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__) 2453a3bfb61SJoe Perches #define net_crit_ratelimited(fmt, ...) \ 2463a3bfb61SJoe Perches net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__) 2473a3bfb61SJoe Perches #define net_err_ratelimited(fmt, ...) \ 2483a3bfb61SJoe Perches net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__) 2493a3bfb61SJoe Perches #define net_notice_ratelimited(fmt, ...) \ 2503a3bfb61SJoe Perches net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__) 2513a3bfb61SJoe Perches #define net_warn_ratelimited(fmt, ...) \ 2523a3bfb61SJoe Perches net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__) 2533a3bfb61SJoe Perches #define net_info_ratelimited(fmt, ...) \ 2543a3bfb61SJoe Perches net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__) 2552c94b537STim Bingham #if defined(CONFIG_DYNAMIC_DEBUG) 2562c94b537STim Bingham #define net_dbg_ratelimited(fmt, ...) \ 2572c94b537STim Bingham do { \ 2582c94b537STim Bingham DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \ 2592c94b537STim Bingham if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \ 2602c94b537STim Bingham net_ratelimit()) \ 261daddef76SJason A. Donenfeld __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \ 262daddef76SJason A. Donenfeld ##__VA_ARGS__); \ 2632c94b537STim Bingham } while (0) 2642c94b537STim Bingham #elif defined(DEBUG) 2653a3bfb61SJoe Perches #define net_dbg_ratelimited(fmt, ...) \ 2663a3bfb61SJoe Perches net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__) 267d92cff89SJason A. Donenfeld #else 268d92cff89SJason A. Donenfeld #define net_dbg_ratelimited(fmt, ...) \ 269d92cff89SJason A. Donenfeld do { \ 270d92cff89SJason A. Donenfeld if (0) \ 271d92cff89SJason A. Donenfeld no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \ 272d92cff89SJason A. Donenfeld } while (0) 273d92cff89SJason A. Donenfeld #endif 2743a3bfb61SJoe Perches 275a48e4292SHannes Frederic Sowa #define net_get_random_once(buf, nbytes) \ 27646234253SHannes Frederic Sowa get_random_once((buf), (nbytes)) 277a48e4292SHannes Frederic Sowa 2787965bd4dSJoe Perches int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec, 2797965bd4dSJoe Perches size_t num, size_t len); 2807965bd4dSJoe Perches int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec, 2817965bd4dSJoe Perches size_t num, size_t len, int flags); 2821da177e4SLinus Torvalds 2837965bd4dSJoe Perches int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen); 2847965bd4dSJoe Perches int kernel_listen(struct socket *sock, int backlog); 2857965bd4dSJoe Perches int kernel_accept(struct socket *sock, struct socket **newsock, int flags); 2867965bd4dSJoe Perches int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen, 287ac5a488eSSridhar Samudrala int flags); 2887965bd4dSJoe Perches int kernel_getsockname(struct socket *sock, struct sockaddr *addr, 289ac5a488eSSridhar Samudrala int *addrlen); 2907965bd4dSJoe Perches int kernel_getpeername(struct socket *sock, struct sockaddr *addr, 291ac5a488eSSridhar Samudrala int *addrlen); 2927965bd4dSJoe Perches int kernel_getsockopt(struct socket *sock, int level, int optname, char *optval, 2937965bd4dSJoe Perches int *optlen); 2947965bd4dSJoe Perches int kernel_setsockopt(struct socket *sock, int level, int optname, char *optval, 2957965bd4dSJoe Perches unsigned int optlen); 2967965bd4dSJoe Perches int kernel_sendpage(struct socket *sock, struct page *page, int offset, 297ac5a488eSSridhar Samudrala size_t size, int flags); 2987965bd4dSJoe Perches int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg); 2997965bd4dSJoe Perches int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how); 300ac5a488eSSridhar Samudrala 3011da177e4SLinus Torvalds #define MODULE_ALIAS_NETPROTO(proto) \ 3021da177e4SLinus Torvalds MODULE_ALIAS("net-pf-" __stringify(proto)) 3031da177e4SLinus Torvalds 3044fdb3bb7SHarald Welte #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \ 3054fdb3bb7SHarald Welte MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto)) 3064fdb3bb7SHarald Welte 307305e1e96SJean Delvare #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \ 308305e1e96SJean Delvare MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \ 309305e1e96SJean Delvare "-type-" __stringify(type)) 310305e1e96SJean Delvare 3112033e9bfSNeil Horman #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \ 3122033e9bfSNeil Horman MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \ 3132033e9bfSNeil Horman name) 3141da177e4SLinus Torvalds #endif /* _LINUX_NET_H */ 315