0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef _LINUX_NET_H
0015 #define _LINUX_NET_H
0016
0017 #include <linux/stringify.h>
0018 #include <linux/random.h>
0019 #include <linux/wait.h>
0020 #include <linux/fcntl.h> /* For O_CLOEXEC and O_NONBLOCK */
0021 #include <linux/rcupdate.h>
0022 #include <linux/once.h>
0023 #include <linux/fs.h>
0024 #include <linux/mm.h>
0025 #include <linux/sockptr.h>
0026
0027 #include <uapi/linux/net.h>
0028
0029 struct poll_table_struct;
0030 struct pipe_inode_info;
0031 struct inode;
0032 struct file;
0033 struct net;
0034
0035
0036
0037
0038
0039 #define SOCKWQ_ASYNC_NOSPACE 0
0040 #define SOCKWQ_ASYNC_WAITDATA 1
0041 #define SOCK_NOSPACE 2
0042 #define SOCK_PASSCRED 3
0043 #define SOCK_PASSSEC 4
0044
0045 #ifndef ARCH_HAS_SOCKET_TYPES
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 enum sock_type {
0062 SOCK_STREAM = 1,
0063 SOCK_DGRAM = 2,
0064 SOCK_RAW = 3,
0065 SOCK_RDM = 4,
0066 SOCK_SEQPACKET = 5,
0067 SOCK_DCCP = 6,
0068 SOCK_PACKET = 10,
0069 };
0070
0071 #define SOCK_MAX (SOCK_PACKET + 1)
0072
0073
0074 #define SOCK_TYPE_MASK 0xf
0075
0076
0077 #define SOCK_CLOEXEC O_CLOEXEC
0078 #ifndef SOCK_NONBLOCK
0079 #define SOCK_NONBLOCK O_NONBLOCK
0080 #endif
0081
0082 #endif
0083
0084
0085
0086
0087
0088
0089
0090 enum sock_shutdown_cmd {
0091 SHUT_RD,
0092 SHUT_WR,
0093 SHUT_RDWR,
0094 };
0095
0096 struct socket_wq {
0097
0098 wait_queue_head_t wait;
0099 struct fasync_struct *fasync_list;
0100 unsigned long flags;
0101 struct rcu_head rcu;
0102 } ____cacheline_aligned_in_smp;
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114 struct socket {
0115 socket_state state;
0116
0117 short type;
0118
0119 unsigned long flags;
0120
0121 struct file *file;
0122 struct sock *sk;
0123 const struct proto_ops *ops;
0124
0125 struct socket_wq wq;
0126 };
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137 typedef struct {
0138 size_t written;
0139 size_t count;
0140 union {
0141 char __user *buf;
0142 void *data;
0143 } arg;
0144 int error;
0145 } read_descriptor_t;
0146
0147 struct vm_area_struct;
0148 struct page;
0149 struct sockaddr;
0150 struct msghdr;
0151 struct module;
0152 struct sk_buff;
0153 typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
0154 unsigned int, size_t);
0155 typedef int (*skb_read_actor_t)(struct sock *, struct sk_buff *);
0156
0157
0158 struct proto_ops {
0159 int family;
0160 struct module *owner;
0161 int (*release) (struct socket *sock);
0162 int (*bind) (struct socket *sock,
0163 struct sockaddr *myaddr,
0164 int sockaddr_len);
0165 int (*connect) (struct socket *sock,
0166 struct sockaddr *vaddr,
0167 int sockaddr_len, int flags);
0168 int (*socketpair)(struct socket *sock1,
0169 struct socket *sock2);
0170 int (*accept) (struct socket *sock,
0171 struct socket *newsock, int flags, bool kern);
0172 int (*getname) (struct socket *sock,
0173 struct sockaddr *addr,
0174 int peer);
0175 __poll_t (*poll) (struct file *file, struct socket *sock,
0176 struct poll_table_struct *wait);
0177 int (*ioctl) (struct socket *sock, unsigned int cmd,
0178 unsigned long arg);
0179 #ifdef CONFIG_COMPAT
0180 int (*compat_ioctl) (struct socket *sock, unsigned int cmd,
0181 unsigned long arg);
0182 #endif
0183 int (*gettstamp) (struct socket *sock, void __user *userstamp,
0184 bool timeval, bool time32);
0185 int (*listen) (struct socket *sock, int len);
0186 int (*shutdown) (struct socket *sock, int flags);
0187 int (*setsockopt)(struct socket *sock, int level,
0188 int optname, sockptr_t optval,
0189 unsigned int optlen);
0190 int (*getsockopt)(struct socket *sock, int level,
0191 int optname, char __user *optval, int __user *optlen);
0192 void (*show_fdinfo)(struct seq_file *m, struct socket *sock);
0193 int (*sendmsg) (struct socket *sock, struct msghdr *m,
0194 size_t total_len);
0195
0196
0197
0198
0199
0200
0201
0202
0203 int (*recvmsg) (struct socket *sock, struct msghdr *m,
0204 size_t total_len, int flags);
0205 int (*mmap) (struct file *file, struct socket *sock,
0206 struct vm_area_struct * vma);
0207 ssize_t (*sendpage) (struct socket *sock, struct page *page,
0208 int offset, size_t size, int flags);
0209 ssize_t (*splice_read)(struct socket *sock, loff_t *ppos,
0210 struct pipe_inode_info *pipe, size_t len, unsigned int flags);
0211 int (*set_peek_off)(struct sock *sk, int val);
0212 int (*peek_len)(struct socket *sock);
0213
0214
0215
0216
0217 int (*read_sock)(struct sock *sk, read_descriptor_t *desc,
0218 sk_read_actor_t recv_actor);
0219
0220 int (*read_skb)(struct sock *sk, skb_read_actor_t recv_actor);
0221 int (*sendpage_locked)(struct sock *sk, struct page *page,
0222 int offset, size_t size, int flags);
0223 int (*sendmsg_locked)(struct sock *sk, struct msghdr *msg,
0224 size_t size);
0225 int (*set_rcvlowat)(struct sock *sk, int val);
0226 };
0227
0228 #define DECLARE_SOCKADDR(type, dst, src) \
0229 type dst = ({ __sockaddr_check_size(sizeof(*dst)); (type) src; })
0230
0231 struct net_proto_family {
0232 int family;
0233 int (*create)(struct net *net, struct socket *sock,
0234 int protocol, int kern);
0235 struct module *owner;
0236 };
0237
0238 struct iovec;
0239 struct kvec;
0240
0241 enum {
0242 SOCK_WAKE_IO,
0243 SOCK_WAKE_WAITD,
0244 SOCK_WAKE_SPACE,
0245 SOCK_WAKE_URG,
0246 };
0247
0248 int sock_wake_async(struct socket_wq *sk_wq, int how, int band);
0249 int sock_register(const struct net_proto_family *fam);
0250 void sock_unregister(int family);
0251 bool sock_is_registered(int family);
0252 int __sock_create(struct net *net, int family, int type, int proto,
0253 struct socket **res, int kern);
0254 int sock_create(int family, int type, int proto, struct socket **res);
0255 int sock_create_kern(struct net *net, int family, int type, int proto, struct socket **res);
0256 int sock_create_lite(int family, int type, int proto, struct socket **res);
0257 struct socket *sock_alloc(void);
0258 void sock_release(struct socket *sock);
0259 int sock_sendmsg(struct socket *sock, struct msghdr *msg);
0260 int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags);
0261 struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname);
0262 struct socket *sockfd_lookup(int fd, int *err);
0263 struct socket *sock_from_file(struct file *file);
0264 #define sockfd_put(sock) fput(sock->file)
0265 int net_ratelimit(void);
0266
0267 #define net_ratelimited_function(function, ...) \
0268 do { \
0269 if (net_ratelimit()) \
0270 function(__VA_ARGS__); \
0271 } while (0)
0272
0273 #define net_emerg_ratelimited(fmt, ...) \
0274 net_ratelimited_function(pr_emerg, fmt, ##__VA_ARGS__)
0275 #define net_alert_ratelimited(fmt, ...) \
0276 net_ratelimited_function(pr_alert, fmt, ##__VA_ARGS__)
0277 #define net_crit_ratelimited(fmt, ...) \
0278 net_ratelimited_function(pr_crit, fmt, ##__VA_ARGS__)
0279 #define net_err_ratelimited(fmt, ...) \
0280 net_ratelimited_function(pr_err, fmt, ##__VA_ARGS__)
0281 #define net_notice_ratelimited(fmt, ...) \
0282 net_ratelimited_function(pr_notice, fmt, ##__VA_ARGS__)
0283 #define net_warn_ratelimited(fmt, ...) \
0284 net_ratelimited_function(pr_warn, fmt, ##__VA_ARGS__)
0285 #define net_info_ratelimited(fmt, ...) \
0286 net_ratelimited_function(pr_info, fmt, ##__VA_ARGS__)
0287 #if defined(CONFIG_DYNAMIC_DEBUG) || \
0288 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
0289 #define net_dbg_ratelimited(fmt, ...) \
0290 do { \
0291 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
0292 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
0293 net_ratelimit()) \
0294 __dynamic_pr_debug(&descriptor, pr_fmt(fmt), \
0295 ##__VA_ARGS__); \
0296 } while (0)
0297 #elif defined(DEBUG)
0298 #define net_dbg_ratelimited(fmt, ...) \
0299 net_ratelimited_function(pr_debug, fmt, ##__VA_ARGS__)
0300 #else
0301 #define net_dbg_ratelimited(fmt, ...) \
0302 do { \
0303 if (0) \
0304 no_printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
0305 } while (0)
0306 #endif
0307
0308 #define net_get_random_once(buf, nbytes) \
0309 get_random_once((buf), (nbytes))
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321 static inline bool sendpage_ok(struct page *page)
0322 {
0323 return !PageSlab(page) && page_count(page) >= 1;
0324 }
0325
0326 int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
0327 size_t num, size_t len);
0328 int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
0329 struct kvec *vec, size_t num, size_t len);
0330 int kernel_recvmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
0331 size_t num, size_t len, int flags);
0332
0333 int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen);
0334 int kernel_listen(struct socket *sock, int backlog);
0335 int kernel_accept(struct socket *sock, struct socket **newsock, int flags);
0336 int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
0337 int flags);
0338 int kernel_getsockname(struct socket *sock, struct sockaddr *addr);
0339 int kernel_getpeername(struct socket *sock, struct sockaddr *addr);
0340 int kernel_sendpage(struct socket *sock, struct page *page, int offset,
0341 size_t size, int flags);
0342 int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
0343 size_t size, int flags);
0344 int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how);
0345
0346
0347 u32 kernel_sock_ip_overhead(struct sock *sk);
0348
0349 #define MODULE_ALIAS_NETPROTO(proto) \
0350 MODULE_ALIAS("net-pf-" __stringify(proto))
0351
0352 #define MODULE_ALIAS_NET_PF_PROTO(pf, proto) \
0353 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto))
0354
0355 #define MODULE_ALIAS_NET_PF_PROTO_TYPE(pf, proto, type) \
0356 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
0357 "-type-" __stringify(type))
0358
0359 #define MODULE_ALIAS_NET_PF_PROTO_NAME(pf, proto, name) \
0360 MODULE_ALIAS("net-pf-" __stringify(pf) "-proto-" __stringify(proto) \
0361 name)
0362 #endif