net.h 11 KB

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