net.h 9.2 KB

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