net.h 10 KB

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