net.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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/rcupdate.h>
  25. #include <linux/once.h>
  26. #include <linux/fs.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. short type;
  102. unsigned long flags;
  103. struct socket_wq *wq;
  104. struct file *file;
  105. struct sock *sk;
  106. const struct proto_ops *ops;
  107. };
  108. struct vm_area_struct;
  109. struct page;
  110. struct sockaddr;
  111. struct msghdr;
  112. struct module;
  113. struct sk_buff;
  114. typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
  115. unsigned int, size_t);
  116. struct proto_ops {
  117. int family;
  118. struct module *owner;
  119. int (*release) (struct socket *sock);
  120. int (*bind) (struct socket *sock,
  121. struct sockaddr *myaddr,
  122. int sockaddr_len);
  123. int (*connect) (struct socket *sock,
  124. struct sockaddr *vaddr,
  125. int sockaddr_len, int flags);
  126. int (*socketpair)(struct socket *sock1,
  127. struct socket *sock2);
  128. int (*accept) (struct socket *sock,
  129. struct socket *newsock, int flags, bool kern);
  130. int (*getname) (struct socket *sock,
  131. struct sockaddr *addr,
  132. int peer);
  133. __poll_t (*poll) (struct file *file, struct socket *sock,
  134. struct poll_table_struct *wait);
  135. int (*ioctl) (struct socket *sock, unsigned int cmd,
  136. unsigned long arg);
  137. #ifdef CONFIG_COMPAT
  138. int (*compat_ioctl) (struct socket *sock, unsigned int cmd,
  139. unsigned long arg);
  140. #endif
  141. int (*listen) (struct socket *sock, int len);
  142. int (*shutdown) (struct socket *sock, int flags);
  143. int (*setsockopt)(struct socket *sock, int level,
  144. int optname, char __user *optval, unsigned int optlen);
  145. int (*getsockopt)(struct socket *sock, int level,
  146. int optname, char __user *optval, int __user *optlen);
  147. #ifdef CONFIG_COMPAT
  148. int (*compat_setsockopt)(struct socket *sock, int level,
  149. int optname, char __user *optval, unsigned int optlen);
  150. int (*compat_getsockopt)(struct socket *sock, int level,
  151. int optname, char __user *optval, int __user *optlen);
  152. #endif
  153. int (*sendmsg) (struct socket *sock, struct msghdr *m,
  154. size_t total_len);
  155. /* Notes for implementing recvmsg:
  156. * ===============================
  157. * msg->msg_namelen should get updated by the recvmsg handlers
  158. * iff msg_name != NULL. It is by default 0 to prevent
  159. * returning uninitialized memory to user space. The recvfrom
  160. * handlers can assume that msg.msg_name is either NULL or has
  161. * a minimum size of sizeof(struct sockaddr_storage).
  162. */
  163. int (*recvmsg) (struct socket *sock, struct msghdr *m,
  164. size_t total_len, int flags);
  165. int (*mmap) (struct file *file, struct socket *sock,
  166. struct vm_area_struct * vma);
  167. ssize_t (*sendpage) (struct socket *sock, struct page *page,
  168. int offset, size_t size, int flags);
  169. ssize_t (*splice_read)(struct socket *sock, loff_t *ppos,
  170. struct pipe_inode_info *pipe, size_t len, unsigned int flags);
  171. int (*set_peek_off)(struct sock *sk, int val);
  172. int (*peek_len)(struct socket *sock);
  173. /* The following functions are called internally by kernel with
  174. * sock lock already held.
  175. */
  176. int (*read_sock)(struct sock *sk, read_descriptor_t *desc,
  177. sk_read_actor_t recv_actor);
  178. int (*sendpage_locked)(struct sock *sk, struct page *page,
  179. int offset, size_t size, int flags);
  180. int (*sendmsg_locked)(struct sock *sk, struct msghdr *msg,
  181. size_t size);
  182. int (*set_rcvlowat)(struct sock *sk, int val);
  183. };
  184. #define DECLARE_SOCKADDR(type, dst, src) \
  185. type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
  186. struct net_proto_family {
  187. int family;
  188. int (*create)(struct net *net, struct socket *sock,
  189. int protocol, int kern);
  190. struct module *owner;
  191. };
  192. struct iovec;
  193. struct kvec;
  194. enum {
  195. SOCK_WAKE_IO,
  196. SOCK_WAKE_WAITD,
  197. SOCK_WAKE_SPACE,
  198. SOCK_WAKE_URG,
  199. };
  200. int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
  201. int sock_register(const struct net_proto_family *fam);
  202. void sock_unregister(int family);
  203. bool sock_is_registered(int family);
  204. int __sock_create(struct net *net, int family, int type, int proto,
  205. struct socket **res, int kern);
  206. int sock_create(int family, int type, int proto, struct socket **res);
  207. int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
  208. int sock_create_lite(int family, int type, int proto, struct socket **res);
  209. struct socket *sock_alloc(void);
  210. void sock_release(struct socket *sock);
  211. int sock_sendmsg(struct socket *sock, struct msghdr *msg);
  212. int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
  213. struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
  214. struct socket *sockfd_lookup(int fd, int *err);
  215. struct socket *sock_from_file(struct file *file, int *err);
  216. #define sockfd_put(sock) fput(sock->file)
  217. int net_ratelimit(void);
  218. #define net_ratelimited_function(function, ...) \
  219. do { \
  220. if (net_ratelimit()) \
  221. function(__VA_ARGS__); \
  222. } while (0)
  223. #define net_emerg_ratelimited(fmt, ...) \
  224. net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
  225. #define net_alert_ratelimited(fmt, ...) \
  226. net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
  227. #define net_crit_ratelimited(fmt, ...) \
  228. net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
  229. #define net_err_ratelimited(fmt, ...) \
  230. net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
  231. #define net_notice_ratelimited(fmt, ...) \
  232. net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
  233. #define net_warn_ratelimited(fmt, ...) \
  234. net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
  235. #define net_info_ratelimited(fmt, ...) \
  236. net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
  237. #if defined(CONFIG_DYNAMIC_DEBUG)
  238. #define net_dbg_ratelimited(fmt, ...) \
  239. do { \
  240. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  241. if (unlikely(descriptor.flags & _DPRINTK_FLAGS_PRINT) && \
  242. net_ratelimit()) \
  243. __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
  244. ##__VA_ARGS__); \
  245. } while (0)
  246. #elif defined(DEBUG)
  247. #define net_dbg_ratelimited(fmt, ...) \
  248. net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
  249. #else
  250. #define net_dbg_ratelimited(fmt, ...) \
  251. do { \
  252. if (0) \
  253. no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
  254. } while (0)
  255. #endif
  256. #define net_get_random_once(buf, nbytes) \
  257. get_random_once((buf), (nbytes))
  258. #define net_get_random_once_wait(buf, nbytes) \
  259. get_random_once_wait((buf), (nbytes))
  260. int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
  261. size_t num, size_t len);
  262. int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
  263. struct kvec *vec, size_t num, size_t len);
  264. int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
  265. size_t num, size_t len, int flags);
  266. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen);
  267. int kernel_listen(struct socket *sock, int backlog);
  268. int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
  269. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  270. int flags);
  271. int kernel_getsockname(struct socket *sock, struct sockaddr *addr);
  272. int kernel_getpeername(struct socket *sock, struct sockaddr *addr);
  273. int kernel_getsockopt(struct socket *sock, int level, int optname, char *optval,
  274. int *optlen);
  275. int kernel_setsockopt(struct socket *sock, int level, int optname, char *optval,
  276. unsigned int optlen);
  277. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  278. size_t size, int flags);
  279. int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
  280. size_t size, int flags);
  281. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
  282. /* Routine returns the IP overhead imposed by a (caller-protected) socket. */
  283. u32 kernel_sock_ip_overhead(struct sock *sk);
  284. #define MODULE_ALIAS_NETPROTO(proto) \
  285. MODULE_ALIAS("net-pf-" __stringify(proto))
  286. #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
  287. MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
  288. #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
  289. MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
  290. "-type-" __stringify(type))
  291. #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
  292. MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
  293. name)
  294. #endif /* _LINUX_NET_H */