net.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * NET An implementation of the SOCKET network access protocol.
  4. * This is the master header file for the Linux NET layer,
  5. * or, in plain English: the networking handling part of the
  6. * kernel.
  7. *
  8. * Version: @(#)net.h 1.0.3 05/25/93
  9. *
  10. * Authors: Orest Zborowski, <[email protected]>
  11. * Ross Biro
  12. * Fred N. van Kempen, <[email protected]>
  13. */
  14. #ifndef _LINUX_NET_H
  15. #define _LINUX_NET_H
  16. #include <linux/stringify.h>
  17. #include <linux/random.h>
  18. #include <linux/wait.h>
  19. #include <linux/fcntl.h> /* For O_CLOEXEC and O_NONBLOCK */
  20. #include <linux/rcupdate.h>
  21. #include <linux/once.h>
  22. #include <linux/fs.h>
  23. #include <linux/mm.h>
  24. #include <linux/sockptr.h>
  25. #include <linux/android_kabi.h>
  26. #include <uapi/linux/net.h>
  27. struct poll_table_struct;
  28. struct pipe_inode_info;
  29. struct inode;
  30. struct file;
  31. struct net;
  32. /* Historically, SOCKWQ_ASYNC_NOSPACE & SOCKWQ_ASYNC_WAITDATA were located
  33. * in sock->flags, but moved into sk->sk_wq->flags to be RCU protected.
  34. * Eventually all flags will be in sk->sk_wq->flags.
  35. */
  36. #define SOCKWQ_ASYNC_NOSPACE 0
  37. #define SOCKWQ_ASYNC_WAITDATA 1
  38. #define SOCK_NOSPACE 2
  39. #define SOCK_PASSCRED 3
  40. #define SOCK_PASSSEC 4
  41. #define SOCK_SUPPORT_ZC 5
  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. /**
  78. * enum sock_shutdown_cmd - Shutdown types
  79. * @SHUT_RD: shutdown receptions
  80. * @SHUT_WR: shutdown transmissions
  81. * @SHUT_RDWR: shutdown receptions/transmissions
  82. */
  83. enum sock_shutdown_cmd {
  84. SHUT_RD,
  85. SHUT_WR,
  86. SHUT_RDWR,
  87. };
  88. struct socket_wq {
  89. /* Note: wait MUST be first field of socket_wq */
  90. wait_queue_head_t wait;
  91. struct fasync_struct *fasync_list;
  92. unsigned long flags; /* %SOCKWQ_ASYNC_NOSPACE, etc */
  93. struct rcu_head rcu;
  94. } ____cacheline_aligned_in_smp;
  95. /**
  96. * struct socket - general BSD socket
  97. * @state: socket state (%SS_CONNECTED, etc)
  98. * @type: socket type (%SOCK_STREAM, etc)
  99. * @flags: socket flags (%SOCK_NOSPACE, etc)
  100. * @ops: protocol specific socket operations
  101. * @file: File back pointer for gc
  102. * @sk: internal networking protocol agnostic socket representation
  103. * @wq: wait queue for several uses
  104. */
  105. struct socket {
  106. socket_state state;
  107. short type;
  108. unsigned long flags;
  109. struct file *file;
  110. struct sock *sk;
  111. const struct proto_ops *ops;
  112. struct socket_wq wq;
  113. };
  114. /*
  115. * "descriptor" for what we're up to with a read.
  116. * This allows us to use the same read code yet
  117. * have multiple different users of the data that
  118. * we read from a file.
  119. *
  120. * The simplest case just copies the data to user
  121. * mode.
  122. */
  123. typedef struct {
  124. size_t written;
  125. size_t count;
  126. union {
  127. char __user *buf;
  128. void *data;
  129. } arg;
  130. int error;
  131. } read_descriptor_t;
  132. struct vm_area_struct;
  133. struct page;
  134. struct sockaddr;
  135. struct msghdr;
  136. struct module;
  137. struct sk_buff;
  138. typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
  139. unsigned int, size_t);
  140. typedef int (*skb_read_actor_t)(struct sock *, struct sk_buff *);
  141. struct proto_ops {
  142. int family;
  143. struct module *owner;
  144. int (*release) (struct socket *sock);
  145. int (*bind) (struct socket *sock,
  146. struct sockaddr *myaddr,
  147. int sockaddr_len);
  148. int (*connect) (struct socket *sock,
  149. struct sockaddr *vaddr,
  150. int sockaddr_len, int flags);
  151. int (*socketpair)(struct socket *sock1,
  152. struct socket *sock2);
  153. int (*accept) (struct socket *sock,
  154. struct socket *newsock, int flags, bool kern);
  155. int (*getname) (struct socket *sock,
  156. struct sockaddr *addr,
  157. int peer);
  158. __poll_t (*poll) (struct file *file, struct socket *sock,
  159. struct poll_table_struct *wait);
  160. int (*ioctl) (struct socket *sock, unsigned int cmd,
  161. unsigned long arg);
  162. #ifdef CONFIG_COMPAT
  163. int (*compat_ioctl) (struct socket *sock, unsigned int cmd,
  164. unsigned long arg);
  165. #endif
  166. int (*gettstamp) (struct socket *sock, void __user *userstamp,
  167. bool timeval, bool time32);
  168. int (*listen) (struct socket *sock, int len);
  169. int (*shutdown) (struct socket *sock, int flags);
  170. int (*setsockopt)(struct socket *sock, int level,
  171. int optname, sockptr_t optval,
  172. unsigned int optlen);
  173. int (*getsockopt)(struct socket *sock, int level,
  174. int optname, char __user *optval, int __user *optlen);
  175. void (*show_fdinfo)(struct seq_file *m, struct socket *sock);
  176. int (*sendmsg) (struct socket *sock, struct msghdr *m,
  177. size_t total_len);
  178. /* Notes for implementing recvmsg:
  179. * ===============================
  180. * msg->msg_namelen should get updated by the recvmsg handlers
  181. * iff msg_name != NULL. It is by default 0 to prevent
  182. * returning uninitialized memory to user space. The recvfrom
  183. * handlers can assume that msg.msg_name is either NULL or has
  184. * a minimum size of sizeof(struct sockaddr_storage).
  185. */
  186. int (*recvmsg) (struct socket *sock, struct msghdr *m,
  187. size_t total_len, int flags);
  188. int (*mmap) (struct file *file, struct socket *sock,
  189. struct vm_area_struct * vma);
  190. ssize_t (*sendpage) (struct socket *sock, struct page *page,
  191. int offset, size_t size, int flags);
  192. ssize_t (*splice_read)(struct socket *sock, loff_t *ppos,
  193. struct pipe_inode_info *pipe, size_t len, unsigned int flags);
  194. int (*set_peek_off)(struct sock *sk, int val);
  195. int (*peek_len)(struct socket *sock);
  196. /* The following functions are called internally by kernel with
  197. * sock lock already held.
  198. */
  199. int (*read_sock)(struct sock *sk, read_descriptor_t *desc,
  200. sk_read_actor_t recv_actor);
  201. /* This is different from read_sock(), it reads an entire skb at a time. */
  202. int (*read_skb)(struct sock *sk, skb_read_actor_t recv_actor);
  203. int (*sendpage_locked)(struct sock *sk, struct page *page,
  204. int offset, size_t size, int flags);
  205. int (*sendmsg_locked)(struct sock *sk, struct msghdr *msg,
  206. size_t size);
  207. int (*set_rcvlowat)(struct sock *sk, int val);
  208. ANDROID_KABI_RESERVE(1);
  209. ANDROID_KABI_RESERVE(2);
  210. ANDROID_KABI_RESERVE(3);
  211. ANDROID_KABI_RESERVE(4);
  212. };
  213. #define DECLARE_SOCKADDR(type, dst, src) \
  214. type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
  215. struct net_proto_family {
  216. int family;
  217. int (*create)(struct net *net, struct socket *sock,
  218. int protocol, int kern);
  219. struct module *owner;
  220. };
  221. struct iovec;
  222. struct kvec;
  223. enum {
  224. SOCK_WAKE_IO,
  225. SOCK_WAKE_WAITD,
  226. SOCK_WAKE_SPACE,
  227. SOCK_WAKE_URG,
  228. };
  229. int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
  230. int sock_register(const struct net_proto_family *fam);
  231. void sock_unregister(int family);
  232. bool sock_is_registered(int family);
  233. int __sock_create(struct net *net, int family, int type, int proto,
  234. struct socket **res, int kern);
  235. int sock_create(int family, int type, int proto, struct socket **res);
  236. int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
  237. int sock_create_lite(int family, int type, int proto, struct socket **res);
  238. struct socket *sock_alloc(void);
  239. void sock_release(struct socket *sock);
  240. int sock_sendmsg(struct socket *sock, struct msghdr *msg);
  241. int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
  242. struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
  243. struct socket *sockfd_lookup(int fd, int *err);
  244. struct socket *sock_from_file(struct file *file);
  245. #define sockfd_put(sock) fput(sock->file)
  246. int net_ratelimit(void);
  247. #define net_ratelimited_function(function, ...) \
  248. do { \
  249. if (net_ratelimit()) \
  250. function(__VA_ARGS__); \
  251. } while (0)
  252. #define net_emerg_ratelimited(fmt, ...) \
  253. net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
  254. #define net_alert_ratelimited(fmt, ...) \
  255. net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
  256. #define net_crit_ratelimited(fmt, ...) \
  257. net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
  258. #define net_err_ratelimited(fmt, ...) \
  259. net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
  260. #define net_notice_ratelimited(fmt, ...) \
  261. net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
  262. #define net_warn_ratelimited(fmt, ...) \
  263. net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
  264. #define net_info_ratelimited(fmt, ...) \
  265. net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
  266. #if defined(CONFIG_DYNAMIC_DEBUG) || \
  267. (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
  268. #define net_dbg_ratelimited(fmt, ...) \
  269. do { \
  270. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  271. if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
  272. net_ratelimit()) \
  273. __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
  274. ##__VA_ARGS__); \
  275. } while (0)
  276. #elif defined(DEBUG)
  277. #define net_dbg_ratelimited(fmt, ...) \
  278. net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
  279. #else
  280. #define net_dbg_ratelimited(fmt, ...) \
  281. do { \
  282. if (0) \
  283. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
  284. } while (0)
  285. #endif
  286. #define net_get_random_once(buf, nbytes) \
  287. get_random_once((buf), (nbytes))
  288. /*
  289. * E.g. XFS meta- & log-data is in slab pages, or bcache meta
  290. * data pages, or other high order pages allocated by
  291. * __get_free_pages() without __GFP_COMP, which have a page_count
  292. * of 0 and/or have PageSlab() set. We cannot use send_page for
  293. * those, as that does get_page(); put_page(); and would cause
  294. * either a VM_BUG directly, or __page_cache_release a page that
  295. * would actually still be referenced by someone, leading to some
  296. * obscure delayed Oops somewhere else.
  297. */
  298. static inline bool sendpage_ok(struct page *page)
  299. {
  300. return !PageSlab(page) && page_count(page) >= 1;
  301. }
  302. int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
  303. size_t num, size_t len);
  304. int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
  305. struct kvec *vec, size_t num, size_t len);
  306. int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
  307. size_t num, size_t len, int flags);
  308. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen);
  309. int kernel_listen(struct socket *sock, int backlog);
  310. int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
  311. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  312. int flags);
  313. int kernel_getsockname(struct socket *sock, struct sockaddr *addr);
  314. int kernel_getpeername(struct socket *sock, struct sockaddr *addr);
  315. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  316. size_t size, int flags);
  317. int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
  318. size_t size, int flags);
  319. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
  320. /* Routine returns the IP overhead imposed by a (caller-protected) socket. */
  321. u32 kernel_sock_ip_overhead(struct sock *sk);
  322. #define MODULE_ALIAS_NETPROTO(proto) \
  323. MODULE_ALIAS("net-pf-" __stringify(proto))
  324. #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
  325. MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
  326. #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
  327. MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
  328. "-type-" __stringify(type))
  329. #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
  330. MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
  331. name)
  332. #endif /* _LINUX_NET_H */