net.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #include <linux/stringify.h>
  21. #include <linux/random.h>
  22. #include <linux/wait.h>
  23. #include <linux/fcntl.h> /* For O_CLOEXEC and O_NONBLOCK */
  24. #include <linux/kmemcheck.h>
  25. #include <linux/rcupdate.h>
  26. #include <linux/once.h>
  27. #include <uapi/linux/net.h>
  28. struct poll_table_struct;
  29. struct pipe_inode_info;
  30. struct inode;
  31. struct file;
  32. struct net;
  33. /* Historically, SOCKWQ_ASYNC_NOSPACE & SOCKWQ_ASYNC_WAITDATA were located
  34. * in sock->flags, but moved into sk->sk_wq->flags to be RCU protected.
  35. * Eventually all flags will be in sk->sk_wq_flags.
  36. */
  37. #define SOCKWQ_ASYNC_NOSPACE 0
  38. #define SOCKWQ_ASYNC_WAITDATA 1
  39. #define SOCK_NOSPACE 2
  40. #define SOCK_PASSCRED 3
  41. #define SOCK_PASSSEC 4
  42. #ifndef ARCH_HAS_SOCKET_TYPES
  43. /**
  44. * enum sock_type - Socket types
  45. * @SOCK_STREAM: stream (connection) socket
  46. * @SOCK_DGRAM: datagram (conn.less) socket
  47. * @SOCK_RAW: raw socket
  48. * @SOCK_RDM: reliably-delivered message
  49. * @SOCK_SEQPACKET: sequential packet socket
  50. * @SOCK_DCCP: Datagram Congestion Control Protocol socket
  51. * @SOCK_PACKET: linux specific way of getting packets at the dev level.
  52. * For writing rarp and other similar things on the user level.
  53. *
  54. * When adding some new socket type please
  55. * grep ARCH_HAS_SOCKET_TYPE include/asm-* /socket.h, at least MIPS
  56. * overrides this enum for binary compat reasons.
  57. */
  58. enum sock_type {
  59. SOCK_STREAM = 1,
  60. SOCK_DGRAM = 2,
  61. SOCK_RAW = 3,
  62. SOCK_RDM = 4,
  63. SOCK_SEQPACKET = 5,
  64. SOCK_DCCP = 6,
  65. SOCK_PACKET = 10,
  66. };
  67. #define SOCK_MAX (SOCK_PACKET + 1)
  68. /* Mask which covers at least up to SOCK_MASK-1. The
  69. * remaining bits are used as flags. */
  70. #define SOCK_TYPE_MASK 0xf
  71. /* Flags for socket, socketpair, accept4 */
  72. #define SOCK_CLOEXEC O_CLOEXEC
  73. #ifndef SOCK_NONBLOCK
  74. #define SOCK_NONBLOCK O_NONBLOCK
  75. #endif
  76. #endif /* ARCH_HAS_SOCKET_TYPES */
  77. enum sock_shutdown_cmd {
  78. SHUT_RD,
  79. SHUT_WR,
  80. SHUT_RDWR,
  81. };
  82. struct socket_wq {
  83. /* Note: wait MUST be first field of socket_wq */
  84. wait_queue_head_t wait;
  85. struct fasync_struct *fasync_list;
  86. unsigned long flags; /* %SOCKWQ_ASYNC_NOSPACE, etc */
  87. struct rcu_head rcu;
  88. } ____cacheline_aligned_in_smp;
  89. /**
  90. * struct socket - general BSD socket
  91. * @state: socket state (%SS_CONNECTED, etc)
  92. * @type: socket type (%SOCK_STREAM, etc)
  93. * @flags: socket flags (%SOCK_NOSPACE, etc)
  94. * @ops: protocol specific socket operations
  95. * @file: File back pointer for gc
  96. * @sk: internal networking protocol agnostic socket representation
  97. * @wq: wait queue for several uses
  98. */
  99. struct socket {
  100. socket_state state;
  101. kmemcheck_bitfield_begin(type);
  102. short type;
  103. kmemcheck_bitfield_end(type);
  104. unsigned long flags;
  105. struct socket_wq __rcu *wq;
  106. struct file *file;
  107. struct sock *sk;
  108. const struct proto_ops *ops;
  109. };
  110. struct vm_area_struct;
  111. struct page;
  112. struct sockaddr;
  113. struct msghdr;
  114. struct module;
  115. struct proto_ops {
  116. int family;
  117. struct module *owner;
  118. int (*release) (struct socket *sock);
  119. int (*bind) (struct socket *sock,
  120. struct sockaddr *myaddr,
  121. int sockaddr_len);
  122. int (*connect) (struct socket *sock,
  123. struct sockaddr *vaddr,
  124. int sockaddr_len, int flags);
  125. int (*socketpair)(struct socket *sock1,
  126. struct socket *sock2);
  127. int (*accept) (struct socket *sock,
  128. struct socket *newsock, int flags);
  129. int (*getname) (struct socket *sock,
  130. struct sockaddr *addr,
  131. int *sockaddr_len, int peer);
  132. unsigned int (*poll) (struct file *file, struct socket *sock,
  133. struct poll_table_struct *wait);
  134. int (*ioctl) (struct socket *sock, unsigned int cmd,
  135. unsigned long arg);
  136. #ifdef CONFIG_COMPAT
  137. int (*compat_ioctl) (struct socket *sock, unsigned int cmd,
  138. unsigned long arg);
  139. #endif
  140. int (*listen) (struct socket *sock, int len);
  141. int (*shutdown) (struct socket *sock, int flags);
  142. int (*setsockopt)(struct socket *sock, int level,
  143. int optname, char __user *optval, unsigned int optlen);
  144. int (*getsockopt)(struct socket *sock, int level,
  145. int optname, char __user *optval, int __user *optlen);
  146. #ifdef CONFIG_COMPAT
  147. int (*compat_setsockopt)(struct socket *sock, int level,
  148. int optname, char __user *optval, unsigned int optlen);
  149. int (*compat_getsockopt)(struct socket *sock, int level,
  150. int optname, char __user *optval, int __user *optlen);
  151. #endif
  152. int (*sendmsg) (struct socket *sock, struct msghdr *m,
  153. size_t total_len);
  154. /* Notes for implementing recvmsg:
  155. * ===============================
  156. * msg->msg_namelen should get updated by the recvmsg handlers
  157. * iff msg_name != NULL. It is by default 0 to prevent
  158. * returning uninitialized memory to user space. The recvfrom
  159. * handlers can assume that msg.msg_name is either NULL or has
  160. * a minimum size of sizeof(struct sockaddr_storage).
  161. */
  162. int (*recvmsg) (struct socket *sock, struct msghdr *m,
  163. size_t total_len, int flags);
  164. int (*mmap) (struct file *file, struct socket *sock,
  165. struct vm_area_struct * vma);
  166. ssize_t (*sendpage) (struct socket *sock, struct page *page,
  167. int offset, size_t size, int flags);
  168. ssize_t (*splice_read)(struct socket *sock, loff_t *ppos,
  169. struct pipe_inode_info *pipe, size_t len, unsigned int flags);
  170. int (*set_peek_off)(struct sock *sk, int val);
  171. };
  172. #define DECLARE_SOCKADDR(type, dst, src) \
  173. type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
  174. struct net_proto_family {
  175. int family;
  176. int (*create)(struct net *net, struct socket *sock,
  177. int protocol, int kern);
  178. struct module *owner;
  179. };
  180. struct iovec;
  181. struct kvec;
  182. enum {
  183. SOCK_WAKE_IO,
  184. SOCK_WAKE_WAITD,
  185. SOCK_WAKE_SPACE,
  186. SOCK_WAKE_URG,
  187. };
  188. int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
  189. int sock_register(const struct net_proto_family *fam);
  190. void sock_unregister(int family);
  191. int __sock_create(struct net *net, int family, int type, int proto,
  192. struct socket **res, int kern);
  193. int sock_create(int family, int type, int proto, struct socket **res);
  194. int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
  195. int sock_create_lite(int family, int type, int proto, struct socket **res);
  196. struct socket *sock_alloc(void);
  197. void sock_release(struct socket *sock);
  198. int sock_sendmsg(struct socket *sock, struct msghdr *msg);
  199. int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
  200. struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
  201. struct socket *sockfd_lookup(int fd, int *err);
  202. struct socket *sock_from_file(struct file *file, int *err);
  203. #define sockfd_put(sock) fput(sock->file)
  204. int net_ratelimit(void);
  205. #define net_ratelimited_function(function, ...) \
  206. do { \
  207. if (net_ratelimit()) \
  208. function(__VA_ARGS__); \
  209. } while (0)
  210. #define net_emerg_ratelimited(fmt, ...) \
  211. net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
  212. #define net_alert_ratelimited(fmt, ...) \
  213. net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
  214. #define net_crit_ratelimited(fmt, ...) \
  215. net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
  216. #define net_err_ratelimited(fmt, ...) \
  217. net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
  218. #define net_notice_ratelimited(fmt, ...) \
  219. net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
  220. #define net_warn_ratelimited(fmt, ...) \
  221. net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
  222. #define net_info_ratelimited(fmt, ...) \
  223. net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
  224. #if defined(CONFIG_DYNAMIC_DEBUG)
  225. #define net_dbg_ratelimited(fmt, ...) \
  226. do { \
  227. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  228. if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
  229. net_ratelimit()) \
  230. __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
  231. ##__VA_ARGS__); \
  232. } while (0)
  233. #elif defined(DEBUG)
  234. #define net_dbg_ratelimited(fmt, ...) \
  235. net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
  236. #else
  237. #define net_dbg_ratelimited(fmt, ...) \
  238. do { \
  239. if (0) \
  240. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
  241. } while (0)
  242. #endif
  243. #define net_get_random_once(buf, nbytes) \
  244. get_random_once((buf), (nbytes))
  245. int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
  246. size_t num, size_t len);
  247. int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
  248. size_t num, size_t len, int flags);
  249. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen);
  250. int kernel_listen(struct socket *sock, int backlog);
  251. int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
  252. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  253. int flags);
  254. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  255. int *addrlen);
  256. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  257. int *addrlen);
  258. int kernel_getsockopt(struct socket *sock, int level, int optname, char *optval,
  259. int *optlen);
  260. int kernel_setsockopt(struct socket *sock, int level, int optname, char *optval,
  261. unsigned int optlen);
  262. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  263. size_t size, int flags);
  264. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg);
  265. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
  266. #define MODULE_ALIAS_NETPROTO(proto) \
  267. MODULE_ALIAS("net-pf-" __stringify(proto))
  268. #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
  269. MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
  270. #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
  271. MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
  272. "-type-" __stringify(type))
  273. #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
  274. MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
  275. name)
  276. #endif /* _LINUX_NET_H */