2 * NET An implementation of the SOCKET network access protocol.
4 * Version: @(#)socket.c 1.1.93 18/02/95
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
13 * Alan Cox : verify_area() fixes
14 * Alan Cox : Removed DDI
15 * Jonathan Kamens : SOCK_DGRAM reconnect bug
16 * Alan Cox : Moved a load of checks to the very
18 * Alan Cox : Move address structures to/from user
19 * mode above the protocol layers.
20 * Rob Janssen : Allow 0 length sends.
21 * Alan Cox : Asynchronous I/O support (cribbed from the
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
26 * Matti Aarnio : Made the number of sockets dynamic,
27 * to be allocated when needed, and mr.
28 * Uphoff's max is used as max to be
29 * allowed to allocate.
30 * Linus : Argh. removed all the socket allocation
31 * altogether: it's in the inode now.
32 * Alan Cox : Made sock_alloc()/sock_release() public
33 * for NetROM and future kernel nfsd type
35 * Alan Cox : sendmsg/recvmsg basics.
36 * Tom Dyas : Export net symbols.
37 * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
38 * Alan Cox : Added thread locking to sys_* calls
39 * for sockets. May have errors at the
41 * Kevin Buhr : Fixed the dumb errors in the above.
42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
45 * Tigran Aivazian : Made listen(2) backlog sanity checks
46 * protocol-independent
49 * This program is free software; you can redistribute it and/or
50 * modify it under the terms of the GNU General Public License
51 * as published by the Free Software Foundation; either version
52 * 2 of the License, or (at your option) any later version.
55 * This module is effectively the top level interface to the BSD socket
58 * Based upon Swansea University Computer Society NET3.039
62 #include <linux/socket.h>
63 #include <linux/file.h>
64 #include <linux/net.h>
65 #include <linux/interrupt.h>
66 #include <linux/thread_info.h>
67 #include <linux/rcupdate.h>
68 #include <linux/netdevice.h>
69 #include <linux/proc_fs.h>
70 #include <linux/seq_file.h>
71 #include <linux/mutex.h>
72 #include <linux/if_bridge.h>
73 #include <linux/if_frad.h>
74 #include <linux/if_vlan.h>
75 #include <linux/ptp_classify.h>
76 #include <linux/init.h>
77 #include <linux/poll.h>
78 #include <linux/cache.h>
79 #include <linux/module.h>
80 #include <linux/highmem.h>
81 #include <linux/mount.h>
82 #include <linux/security.h>
83 #include <linux/syscalls.h>
84 #include <linux/compat.h>
85 #include <linux/kmod.h>
86 #include <linux/audit.h>
87 #include <linux/wireless.h>
88 #include <linux/nsproxy.h>
89 #include <linux/magic.h>
90 #include <linux/slab.h>
91 #include <linux/xattr.h>
93 #include <linux/uaccess.h>
94 #include <asm/unistd.h>
96 #include <net/compat.h>
98 #include <net/cls_cgroup.h>
100 #include <net/sock.h>
101 #include <linux/netfilter.h>
103 #include <linux/if_tun.h>
104 #include <linux/ipv6_route.h>
105 #include <linux/route.h>
106 #include <linux/sockios.h>
107 #include <linux/atalk.h>
108 #include <net/busy_poll.h>
109 #include <linux/errqueue.h>
111 #ifdef CONFIG_NET_RX_BUSY_POLL
112 unsigned int sysctl_net_busy_read __read_mostly
;
113 unsigned int sysctl_net_busy_poll __read_mostly
;
116 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
);
117 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
);
118 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
);
120 static int sock_close(struct inode
*inode
, struct file
*file
);
121 static unsigned int sock_poll(struct file
*file
,
122 struct poll_table_struct
*wait
);
123 static long sock_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
125 static long compat_sock_ioctl(struct file
*file
,
126 unsigned int cmd
, unsigned long arg
);
128 static int sock_fasync(int fd
, struct file
*filp
, int on
);
129 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
130 int offset
, size_t size
, loff_t
*ppos
, int more
);
131 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
132 struct pipe_inode_info
*pipe
, size_t len
,
136 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
137 * in the operation structures but are done directly via the socketcall() multiplexor.
140 static const struct file_operations socket_file_ops
= {
141 .owner
= THIS_MODULE
,
143 .read_iter
= sock_read_iter
,
144 .write_iter
= sock_write_iter
,
146 .unlocked_ioctl
= sock_ioctl
,
148 .compat_ioctl
= compat_sock_ioctl
,
151 .release
= sock_close
,
152 .fasync
= sock_fasync
,
153 .sendpage
= sock_sendpage
,
154 .splice_write
= generic_splice_sendpage
,
155 .splice_read
= sock_splice_read
,
159 * The protocol list. Each protocol is registered in here.
162 static DEFINE_SPINLOCK(net_family_lock
);
163 static const struct net_proto_family __rcu
*net_families
[NPROTO
] __read_mostly
;
166 * Statistics counters of the socket lists
169 static DEFINE_PER_CPU(int, sockets_in_use
);
173 * Move socket addresses back and forth across the kernel/user
174 * divide and look after the messy bits.
178 * move_addr_to_kernel - copy a socket address into kernel space
179 * @uaddr: Address in user space
180 * @kaddr: Address in kernel space
181 * @ulen: Length in user space
183 * The address is copied into kernel space. If the provided address is
184 * too long an error code of -EINVAL is returned. If the copy gives
185 * invalid addresses -EFAULT is returned. On a success 0 is returned.
188 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, struct sockaddr_storage
*kaddr
)
190 if (ulen
< 0 || ulen
> sizeof(struct sockaddr_storage
))
194 if (copy_from_user(kaddr
, uaddr
, ulen
))
196 return audit_sockaddr(ulen
, kaddr
);
200 * move_addr_to_user - copy an address to user space
201 * @kaddr: kernel space address
202 * @klen: length of address in kernel
203 * @uaddr: user space address
204 * @ulen: pointer to user length field
206 * The value pointed to by ulen on entry is the buffer length available.
207 * This is overwritten with the buffer space used. -EINVAL is returned
208 * if an overlong buffer is specified or a negative buffer size. -EFAULT
209 * is returned if either the buffer or the length field are not
211 * After copying the data up to the limit the user specifies, the true
212 * length of the data is written over the length limit the user
213 * specified. Zero is returned for a success.
216 static int move_addr_to_user(struct sockaddr_storage
*kaddr
, int klen
,
217 void __user
*uaddr
, int __user
*ulen
)
222 BUG_ON(klen
> sizeof(struct sockaddr_storage
));
223 err
= get_user(len
, ulen
);
231 if (audit_sockaddr(klen
, kaddr
))
233 if (copy_to_user(uaddr
, kaddr
, len
))
237 * "fromlen shall refer to the value before truncation.."
240 return __put_user(klen
, ulen
);
243 static struct kmem_cache
*sock_inode_cachep __read_mostly
;
245 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
247 struct socket_alloc
*ei
;
248 struct socket_wq
*wq
;
250 ei
= kmem_cache_alloc(sock_inode_cachep
, GFP_KERNEL
);
253 wq
= kmalloc(sizeof(*wq
), GFP_KERNEL
);
255 kmem_cache_free(sock_inode_cachep
, ei
);
258 init_waitqueue_head(&wq
->wait
);
259 wq
->fasync_list
= NULL
;
261 RCU_INIT_POINTER(ei
->socket
.wq
, wq
);
263 ei
->socket
.state
= SS_UNCONNECTED
;
264 ei
->socket
.flags
= 0;
265 ei
->socket
.ops
= NULL
;
266 ei
->socket
.sk
= NULL
;
267 ei
->socket
.file
= NULL
;
269 return &ei
->vfs_inode
;
272 static void sock_destroy_inode(struct inode
*inode
)
274 struct socket_alloc
*ei
;
275 struct socket_wq
*wq
;
277 ei
= container_of(inode
, struct socket_alloc
, vfs_inode
);
278 wq
= rcu_dereference_protected(ei
->socket
.wq
, 1);
280 kmem_cache_free(sock_inode_cachep
, ei
);
283 static void init_once(void *foo
)
285 struct socket_alloc
*ei
= (struct socket_alloc
*)foo
;
287 inode_init_once(&ei
->vfs_inode
);
290 static void init_inodecache(void)
292 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
293 sizeof(struct socket_alloc
),
295 (SLAB_HWCACHE_ALIGN
|
296 SLAB_RECLAIM_ACCOUNT
|
297 SLAB_MEM_SPREAD
| SLAB_ACCOUNT
),
299 BUG_ON(sock_inode_cachep
== NULL
);
302 static const struct super_operations sockfs_ops
= {
303 .alloc_inode
= sock_alloc_inode
,
304 .destroy_inode
= sock_destroy_inode
,
305 .statfs
= simple_statfs
,
309 * sockfs_dname() is called from d_path().
311 static char *sockfs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
313 return dynamic_dname(dentry
, buffer
, buflen
, "socket:[%lu]",
314 d_inode(dentry
)->i_ino
);
317 static const struct dentry_operations sockfs_dentry_operations
= {
318 .d_dname
= sockfs_dname
,
321 static int sockfs_xattr_get(const struct xattr_handler
*handler
,
322 struct dentry
*dentry
, struct inode
*inode
,
323 const char *suffix
, void *value
, size_t size
)
326 if (dentry
->d_name
.len
+ 1 > size
)
328 memcpy(value
, dentry
->d_name
.name
, dentry
->d_name
.len
+ 1);
330 return dentry
->d_name
.len
+ 1;
333 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
334 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
335 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
337 static const struct xattr_handler sockfs_xattr_handler
= {
338 .name
= XATTR_NAME_SOCKPROTONAME
,
339 .get
= sockfs_xattr_get
,
342 static int sockfs_security_xattr_set(const struct xattr_handler
*handler
,
343 struct dentry
*dentry
, struct inode
*inode
,
344 const char *suffix
, const void *value
,
345 size_t size
, int flags
)
347 /* Handled by LSM. */
351 static const struct xattr_handler sockfs_security_xattr_handler
= {
352 .prefix
= XATTR_SECURITY_PREFIX
,
353 .set
= sockfs_security_xattr_set
,
356 static const struct xattr_handler
*sockfs_xattr_handlers
[] = {
357 &sockfs_xattr_handler
,
358 &sockfs_security_xattr_handler
,
362 static struct dentry
*sockfs_mount(struct file_system_type
*fs_type
,
363 int flags
, const char *dev_name
, void *data
)
365 return mount_pseudo_xattr(fs_type
, "socket:", &sockfs_ops
,
366 sockfs_xattr_handlers
,
367 &sockfs_dentry_operations
, SOCKFS_MAGIC
);
370 static struct vfsmount
*sock_mnt __read_mostly
;
372 static struct file_system_type sock_fs_type
= {
374 .mount
= sockfs_mount
,
375 .kill_sb
= kill_anon_super
,
379 * Obtains the first available file descriptor and sets it up for use.
381 * These functions create file structures and maps them to fd space
382 * of the current process. On success it returns file descriptor
383 * and file struct implicitly stored in sock->file.
384 * Note that another thread may close file descriptor before we return
385 * from this function. We use the fact that now we do not refer
386 * to socket after mapping. If one day we will need it, this
387 * function will increment ref. count on file by 1.
389 * In any case returned fd MAY BE not valid!
390 * This race condition is unavoidable
391 * with shared fd spaces, we cannot solve it inside kernel,
392 * but we take care of internal coherence yet.
395 struct file
*sock_alloc_file(struct socket
*sock
, int flags
, const char *dname
)
397 struct qstr name
= { .name
= "" };
403 name
.len
= strlen(name
.name
);
404 } else if (sock
->sk
) {
405 name
.name
= sock
->sk
->sk_prot_creator
->name
;
406 name
.len
= strlen(name
.name
);
408 path
.dentry
= d_alloc_pseudo(sock_mnt
->mnt_sb
, &name
);
409 if (unlikely(!path
.dentry
)) {
411 return ERR_PTR(-ENOMEM
);
413 path
.mnt
= mntget(sock_mnt
);
415 d_instantiate(path
.dentry
, SOCK_INODE(sock
));
417 file
= alloc_file(&path
, FMODE_READ
| FMODE_WRITE
,
420 /* drop dentry, keep inode for a bit */
421 ihold(d_inode(path
.dentry
));
423 /* ... and now kill it properly */
429 file
->f_flags
= O_RDWR
| (flags
& O_NONBLOCK
);
430 file
->private_data
= sock
;
433 EXPORT_SYMBOL(sock_alloc_file
);
435 static int sock_map_fd(struct socket
*sock
, int flags
)
437 struct file
*newfile
;
438 int fd
= get_unused_fd_flags(flags
);
439 if (unlikely(fd
< 0)) {
444 newfile
= sock_alloc_file(sock
, flags
, NULL
);
445 if (likely(!IS_ERR(newfile
))) {
446 fd_install(fd
, newfile
);
451 return PTR_ERR(newfile
);
454 struct socket
*sock_from_file(struct file
*file
, int *err
)
456 if (file
->f_op
== &socket_file_ops
)
457 return file
->private_data
; /* set in sock_map_fd */
462 EXPORT_SYMBOL(sock_from_file
);
465 * sockfd_lookup - Go from a file number to its socket slot
467 * @err: pointer to an error code return
469 * The file handle passed in is locked and the socket it is bound
470 * to is returned. If an error occurs the err pointer is overwritten
471 * with a negative errno code and NULL is returned. The function checks
472 * for both invalid handles and passing a handle which is not a socket.
474 * On a success the socket object pointer is returned.
477 struct socket
*sockfd_lookup(int fd
, int *err
)
488 sock
= sock_from_file(file
, err
);
493 EXPORT_SYMBOL(sockfd_lookup
);
495 static struct socket
*sockfd_lookup_light(int fd
, int *err
, int *fput_needed
)
497 struct fd f
= fdget(fd
);
502 sock
= sock_from_file(f
.file
, err
);
504 *fput_needed
= f
.flags
;
512 static ssize_t
sockfs_listxattr(struct dentry
*dentry
, char *buffer
,
518 len
= security_inode_listsecurity(d_inode(dentry
), buffer
, size
);
528 len
= (XATTR_NAME_SOCKPROTONAME_LEN
+ 1);
533 memcpy(buffer
, XATTR_NAME_SOCKPROTONAME
, len
);
540 static int sockfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
542 int err
= simple_setattr(dentry
, iattr
);
544 if (!err
&& (iattr
->ia_valid
& ATTR_UID
)) {
545 struct socket
*sock
= SOCKET_I(d_inode(dentry
));
547 sock
->sk
->sk_uid
= iattr
->ia_uid
;
553 static const struct inode_operations sockfs_inode_ops
= {
554 .listxattr
= sockfs_listxattr
,
555 .setattr
= sockfs_setattr
,
559 * sock_alloc - allocate a socket
561 * Allocate a new inode and socket object. The two are bound together
562 * and initialised. The socket is then returned. If we are out of inodes
566 struct socket
*sock_alloc(void)
571 inode
= new_inode_pseudo(sock_mnt
->mnt_sb
);
575 sock
= SOCKET_I(inode
);
577 inode
->i_ino
= get_next_ino();
578 inode
->i_mode
= S_IFSOCK
| S_IRWXUGO
;
579 inode
->i_uid
= current_fsuid();
580 inode
->i_gid
= current_fsgid();
581 inode
->i_op
= &sockfs_inode_ops
;
583 this_cpu_add(sockets_in_use
, 1);
586 EXPORT_SYMBOL(sock_alloc
);
589 * sock_release - close a socket
590 * @sock: socket to close
592 * The socket is released from the protocol stack if it has a release
593 * callback, and the inode is then released if the socket is bound to
594 * an inode not a file.
597 void sock_release(struct socket
*sock
)
600 struct module
*owner
= sock
->ops
->owner
;
602 sock
->ops
->release(sock
);
607 if (rcu_dereference_protected(sock
->wq
, 1)->fasync_list
)
608 pr_err("%s: fasync list not empty!\n", __func__
);
610 this_cpu_sub(sockets_in_use
, 1);
612 iput(SOCK_INODE(sock
));
617 EXPORT_SYMBOL(sock_release
);
619 void __sock_tx_timestamp(__u16 tsflags
, __u8
*tx_flags
)
621 u8 flags
= *tx_flags
;
623 if (tsflags
& SOF_TIMESTAMPING_TX_HARDWARE
)
624 flags
|= SKBTX_HW_TSTAMP
;
626 if (tsflags
& SOF_TIMESTAMPING_TX_SOFTWARE
)
627 flags
|= SKBTX_SW_TSTAMP
;
629 if (tsflags
& SOF_TIMESTAMPING_TX_SCHED
)
630 flags
|= SKBTX_SCHED_TSTAMP
;
634 EXPORT_SYMBOL(__sock_tx_timestamp
);
636 static inline int sock_sendmsg_nosec(struct socket
*sock
, struct msghdr
*msg
)
638 int ret
= sock
->ops
->sendmsg(sock
, msg
, msg_data_left(msg
));
639 BUG_ON(ret
== -EIOCBQUEUED
);
643 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
)
645 int err
= security_socket_sendmsg(sock
, msg
,
648 return err
?: sock_sendmsg_nosec(sock
, msg
);
650 EXPORT_SYMBOL(sock_sendmsg
);
652 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
653 struct kvec
*vec
, size_t num
, size_t size
)
655 iov_iter_kvec(&msg
->msg_iter
, WRITE
| ITER_KVEC
, vec
, num
, size
);
656 return sock_sendmsg(sock
, msg
);
658 EXPORT_SYMBOL(kernel_sendmsg
);
660 int kernel_sendmsg_locked(struct sock
*sk
, struct msghdr
*msg
,
661 struct kvec
*vec
, size_t num
, size_t size
)
663 struct socket
*sock
= sk
->sk_socket
;
665 if (!sock
->ops
->sendmsg_locked
)
666 return sock_no_sendmsg_locked(sk
, msg
, size
);
668 iov_iter_kvec(&msg
->msg_iter
, WRITE
| ITER_KVEC
, vec
, num
, size
);
670 return sock
->ops
->sendmsg_locked(sk
, msg
, msg_data_left(msg
));
672 EXPORT_SYMBOL(kernel_sendmsg_locked
);
674 static bool skb_is_err_queue(const struct sk_buff
*skb
)
676 /* pkt_type of skbs enqueued on the error queue are set to
677 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
678 * in recvmsg, since skbs received on a local socket will never
679 * have a pkt_type of PACKET_OUTGOING.
681 return skb
->pkt_type
== PACKET_OUTGOING
;
684 /* On transmit, software and hardware timestamps are returned independently.
685 * As the two skb clones share the hardware timestamp, which may be updated
686 * before the software timestamp is received, a hardware TX timestamp may be
687 * returned only if there is no software TX timestamp. Ignore false software
688 * timestamps, which may be made in the __sock_recv_timestamp() call when the
689 * option SO_TIMESTAMP(NS) is enabled on the socket, even when the skb has a
690 * hardware timestamp.
692 static bool skb_is_swtx_tstamp(const struct sk_buff
*skb
, int false_tstamp
)
694 return skb
->tstamp
&& !false_tstamp
&& skb_is_err_queue(skb
);
697 static void put_ts_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
699 struct scm_ts_pktinfo ts_pktinfo
;
700 struct net_device
*orig_dev
;
702 if (!skb_mac_header_was_set(skb
))
705 memset(&ts_pktinfo
, 0, sizeof(ts_pktinfo
));
708 orig_dev
= dev_get_by_napi_id(skb_napi_id(skb
));
710 ts_pktinfo
.if_index
= orig_dev
->ifindex
;
713 ts_pktinfo
.pkt_length
= skb
->len
- skb_mac_offset(skb
);
714 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_PKTINFO
,
715 sizeof(ts_pktinfo
), &ts_pktinfo
);
719 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
721 void __sock_recv_timestamp(struct msghdr
*msg
, struct sock
*sk
,
724 int need_software_tstamp
= sock_flag(sk
, SOCK_RCVTSTAMP
);
725 struct scm_timestamping tss
;
726 int empty
= 1, false_tstamp
= 0;
727 struct skb_shared_hwtstamps
*shhwtstamps
=
730 /* Race occurred between timestamp enabling and packet
731 receiving. Fill in the current time for now. */
732 if (need_software_tstamp
&& skb
->tstamp
== 0) {
733 __net_timestamp(skb
);
737 if (need_software_tstamp
) {
738 if (!sock_flag(sk
, SOCK_RCVTSTAMPNS
)) {
740 skb_get_timestamp(skb
, &tv
);
741 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMP
,
745 skb_get_timestampns(skb
, &ts
);
746 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPNS
,
751 memset(&tss
, 0, sizeof(tss
));
752 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_SOFTWARE
) &&
753 ktime_to_timespec_cond(skb
->tstamp
, tss
.ts
+ 0))
756 (sk
->sk_tsflags
& SOF_TIMESTAMPING_RAW_HARDWARE
) &&
757 !skb_is_swtx_tstamp(skb
, false_tstamp
) &&
758 ktime_to_timespec_cond(shhwtstamps
->hwtstamp
, tss
.ts
+ 2)) {
760 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_PKTINFO
) &&
761 !skb_is_err_queue(skb
))
762 put_ts_pktinfo(msg
, skb
);
765 put_cmsg(msg
, SOL_SOCKET
,
766 SCM_TIMESTAMPING
, sizeof(tss
), &tss
);
768 if (skb_is_err_queue(skb
) && skb
->len
&&
769 SKB_EXT_ERR(skb
)->opt_stats
)
770 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_OPT_STATS
,
771 skb
->len
, skb
->data
);
774 EXPORT_SYMBOL_GPL(__sock_recv_timestamp
);
776 void __sock_recv_wifi_status(struct msghdr
*msg
, struct sock
*sk
,
781 if (!sock_flag(sk
, SOCK_WIFI_STATUS
))
783 if (!skb
->wifi_acked_valid
)
786 ack
= skb
->wifi_acked
;
788 put_cmsg(msg
, SOL_SOCKET
, SCM_WIFI_STATUS
, sizeof(ack
), &ack
);
790 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status
);
792 static inline void sock_recv_drops(struct msghdr
*msg
, struct sock
*sk
,
795 if (sock_flag(sk
, SOCK_RXQ_OVFL
) && skb
&& SOCK_SKB_CB(skb
)->dropcount
)
796 put_cmsg(msg
, SOL_SOCKET
, SO_RXQ_OVFL
,
797 sizeof(__u32
), &SOCK_SKB_CB(skb
)->dropcount
);
800 void __sock_recv_ts_and_drops(struct msghdr
*msg
, struct sock
*sk
,
803 sock_recv_timestamp(msg
, sk
, skb
);
804 sock_recv_drops(msg
, sk
, skb
);
806 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops
);
808 static inline int sock_recvmsg_nosec(struct socket
*sock
, struct msghdr
*msg
,
811 return sock
->ops
->recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
814 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int flags
)
816 int err
= security_socket_recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
818 return err
?: sock_recvmsg_nosec(sock
, msg
, flags
);
820 EXPORT_SYMBOL(sock_recvmsg
);
823 * kernel_recvmsg - Receive a message from a socket (kernel space)
824 * @sock: The socket to receive the message from
825 * @msg: Received message
826 * @vec: Input s/g array for message data
827 * @num: Size of input s/g array
828 * @size: Number of bytes to read
829 * @flags: Message flags (MSG_DONTWAIT, etc...)
831 * On return the msg structure contains the scatter/gather array passed in the
832 * vec argument. The array is modified so that it consists of the unfilled
833 * portion of the original array.
835 * The returned value is the total number of bytes received, or an error.
837 int kernel_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
838 struct kvec
*vec
, size_t num
, size_t size
, int flags
)
840 mm_segment_t oldfs
= get_fs();
843 iov_iter_kvec(&msg
->msg_iter
, READ
| ITER_KVEC
, vec
, num
, size
);
845 result
= sock_recvmsg(sock
, msg
, flags
);
849 EXPORT_SYMBOL(kernel_recvmsg
);
851 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
852 int offset
, size_t size
, loff_t
*ppos
, int more
)
857 sock
= file
->private_data
;
859 flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
860 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
863 return kernel_sendpage(sock
, page
, offset
, size
, flags
);
866 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
867 struct pipe_inode_info
*pipe
, size_t len
,
870 struct socket
*sock
= file
->private_data
;
872 if (unlikely(!sock
->ops
->splice_read
))
875 return sock
->ops
->splice_read(sock
, ppos
, pipe
, len
, flags
);
878 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
880 struct file
*file
= iocb
->ki_filp
;
881 struct socket
*sock
= file
->private_data
;
882 struct msghdr msg
= {.msg_iter
= *to
,
886 if (file
->f_flags
& O_NONBLOCK
)
887 msg
.msg_flags
= MSG_DONTWAIT
;
889 if (iocb
->ki_pos
!= 0)
892 if (!iov_iter_count(to
)) /* Match SYS5 behaviour */
895 res
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
900 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
902 struct file
*file
= iocb
->ki_filp
;
903 struct socket
*sock
= file
->private_data
;
904 struct msghdr msg
= {.msg_iter
= *from
,
908 if (iocb
->ki_pos
!= 0)
911 if (file
->f_flags
& O_NONBLOCK
)
912 msg
.msg_flags
= MSG_DONTWAIT
;
914 if (sock
->type
== SOCK_SEQPACKET
)
915 msg
.msg_flags
|= MSG_EOR
;
917 res
= sock_sendmsg(sock
, &msg
);
918 *from
= msg
.msg_iter
;
923 * Atomic setting of ioctl hooks to avoid race
924 * with module unload.
927 static DEFINE_MUTEX(br_ioctl_mutex
);
928 static int (*br_ioctl_hook
) (struct net
*, unsigned int cmd
, void __user
*arg
);
930 void brioctl_set(int (*hook
) (struct net
*, unsigned int, void __user
*))
932 mutex_lock(&br_ioctl_mutex
);
933 br_ioctl_hook
= hook
;
934 mutex_unlock(&br_ioctl_mutex
);
936 EXPORT_SYMBOL(brioctl_set
);
938 static DEFINE_MUTEX(vlan_ioctl_mutex
);
939 static int (*vlan_ioctl_hook
) (struct net
*, void __user
*arg
);
941 void vlan_ioctl_set(int (*hook
) (struct net
*, void __user
*))
943 mutex_lock(&vlan_ioctl_mutex
);
944 vlan_ioctl_hook
= hook
;
945 mutex_unlock(&vlan_ioctl_mutex
);
947 EXPORT_SYMBOL(vlan_ioctl_set
);
949 static DEFINE_MUTEX(dlci_ioctl_mutex
);
950 static int (*dlci_ioctl_hook
) (unsigned int, void __user
*);
952 void dlci_ioctl_set(int (*hook
) (unsigned int, void __user
*))
954 mutex_lock(&dlci_ioctl_mutex
);
955 dlci_ioctl_hook
= hook
;
956 mutex_unlock(&dlci_ioctl_mutex
);
958 EXPORT_SYMBOL(dlci_ioctl_set
);
960 static long sock_do_ioctl(struct net
*net
, struct socket
*sock
,
961 unsigned int cmd
, unsigned long arg
)
964 void __user
*argp
= (void __user
*)arg
;
966 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
969 * If this ioctl is unknown try to hand it down
972 if (err
== -ENOIOCTLCMD
)
973 err
= dev_ioctl(net
, cmd
, argp
);
979 * With an ioctl, arg may well be a user mode pointer, but we don't know
980 * what to do with it - that's up to the protocol still.
983 static struct ns_common
*get_net_ns(struct ns_common
*ns
)
985 return &get_net(container_of(ns
, struct net
, ns
))->ns
;
988 static long sock_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
992 void __user
*argp
= (void __user
*)arg
;
996 sock
= file
->private_data
;
999 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15)) {
1000 err
= dev_ioctl(net
, cmd
, argp
);
1002 #ifdef CONFIG_WEXT_CORE
1003 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
1004 err
= dev_ioctl(net
, cmd
, argp
);
1011 if (get_user(pid
, (int __user
*)argp
))
1013 err
= f_setown(sock
->file
, pid
, 1);
1017 err
= put_user(f_getown(sock
->file
),
1018 (int __user
*)argp
);
1026 request_module("bridge");
1028 mutex_lock(&br_ioctl_mutex
);
1030 err
= br_ioctl_hook(net
, cmd
, argp
);
1031 mutex_unlock(&br_ioctl_mutex
);
1036 if (!vlan_ioctl_hook
)
1037 request_module("8021q");
1039 mutex_lock(&vlan_ioctl_mutex
);
1040 if (vlan_ioctl_hook
)
1041 err
= vlan_ioctl_hook(net
, argp
);
1042 mutex_unlock(&vlan_ioctl_mutex
);
1047 if (!dlci_ioctl_hook
)
1048 request_module("dlci");
1050 mutex_lock(&dlci_ioctl_mutex
);
1051 if (dlci_ioctl_hook
)
1052 err
= dlci_ioctl_hook(cmd
, argp
);
1053 mutex_unlock(&dlci_ioctl_mutex
);
1057 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1060 err
= open_related_ns(&net
->ns
, get_net_ns
);
1063 err
= sock_do_ioctl(net
, sock
, cmd
, arg
);
1069 int sock_create_lite(int family
, int type
, int protocol
, struct socket
**res
)
1072 struct socket
*sock
= NULL
;
1074 err
= security_socket_create(family
, type
, protocol
, 1);
1078 sock
= sock_alloc();
1085 err
= security_socket_post_create(sock
, family
, type
, protocol
, 1);
1097 EXPORT_SYMBOL(sock_create_lite
);
1099 /* No kernel lock held - perfect */
1100 static unsigned int sock_poll(struct file
*file
, poll_table
*wait
)
1102 unsigned int busy_flag
= 0;
1103 struct socket
*sock
;
1106 * We can't return errors to poll, so it's either yes or no.
1108 sock
= file
->private_data
;
1110 if (sk_can_busy_loop(sock
->sk
)) {
1111 /* this socket can poll_ll so tell the system call */
1112 busy_flag
= POLL_BUSY_LOOP
;
1114 /* once, only if requested by syscall */
1115 if (wait
&& (wait
->_key
& POLL_BUSY_LOOP
))
1116 sk_busy_loop(sock
->sk
, 1);
1119 return busy_flag
| sock
->ops
->poll(file
, sock
, wait
);
1122 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1124 struct socket
*sock
= file
->private_data
;
1126 return sock
->ops
->mmap(file
, sock
, vma
);
1129 static int sock_close(struct inode
*inode
, struct file
*filp
)
1131 sock_release(SOCKET_I(inode
));
1136 * Update the socket async list
1138 * Fasync_list locking strategy.
1140 * 1. fasync_list is modified only under process context socket lock
1141 * i.e. under semaphore.
1142 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1143 * or under socket lock
1146 static int sock_fasync(int fd
, struct file
*filp
, int on
)
1148 struct socket
*sock
= filp
->private_data
;
1149 struct sock
*sk
= sock
->sk
;
1150 struct socket_wq
*wq
;
1156 wq
= rcu_dereference_protected(sock
->wq
, lockdep_sock_is_held(sk
));
1157 fasync_helper(fd
, filp
, on
, &wq
->fasync_list
);
1159 if (!wq
->fasync_list
)
1160 sock_reset_flag(sk
, SOCK_FASYNC
);
1162 sock_set_flag(sk
, SOCK_FASYNC
);
1168 /* This function may be called only under rcu_lock */
1170 int sock_wake_async(struct socket_wq
*wq
, int how
, int band
)
1172 if (!wq
|| !wq
->fasync_list
)
1176 case SOCK_WAKE_WAITD
:
1177 if (test_bit(SOCKWQ_ASYNC_WAITDATA
, &wq
->flags
))
1180 case SOCK_WAKE_SPACE
:
1181 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
))
1186 kill_fasync(&wq
->fasync_list
, SIGIO
, band
);
1189 kill_fasync(&wq
->fasync_list
, SIGURG
, band
);
1194 EXPORT_SYMBOL(sock_wake_async
);
1196 int __sock_create(struct net
*net
, int family
, int type
, int protocol
,
1197 struct socket
**res
, int kern
)
1200 struct socket
*sock
;
1201 const struct net_proto_family
*pf
;
1204 * Check protocol is in range
1206 if (family
< 0 || family
>= NPROTO
)
1207 return -EAFNOSUPPORT
;
1208 if (type
< 0 || type
>= SOCK_MAX
)
1213 This uglymoron is moved from INET layer to here to avoid
1214 deadlock in module load.
1216 if (family
== PF_INET
&& type
== SOCK_PACKET
) {
1217 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1222 err
= security_socket_create(family
, type
, protocol
, kern
);
1227 * Allocate the socket and allow the family to set things up. if
1228 * the protocol is 0, the family is instructed to select an appropriate
1231 sock
= sock_alloc();
1233 net_warn_ratelimited("socket: no more sockets\n");
1234 return -ENFILE
; /* Not exactly a match, but its the
1235 closest posix thing */
1240 #ifdef CONFIG_MODULES
1241 /* Attempt to load a protocol module if the find failed.
1243 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1244 * requested real, full-featured networking support upon configuration.
1245 * Otherwise module support will break!
1247 if (rcu_access_pointer(net_families
[family
]) == NULL
)
1248 request_module("net-pf-%d", family
);
1252 pf
= rcu_dereference(net_families
[family
]);
1253 err
= -EAFNOSUPPORT
;
1258 * We will call the ->create function, that possibly is in a loadable
1259 * module, so we have to bump that loadable module refcnt first.
1261 if (!try_module_get(pf
->owner
))
1264 /* Now protected by module ref count */
1267 err
= pf
->create(net
, sock
, protocol
, kern
);
1269 goto out_module_put
;
1272 * Now to bump the refcnt of the [loadable] module that owns this
1273 * socket at sock_release time we decrement its refcnt.
1275 if (!try_module_get(sock
->ops
->owner
))
1276 goto out_module_busy
;
1279 * Now that we're done with the ->create function, the [loadable]
1280 * module can have its refcnt decremented
1282 module_put(pf
->owner
);
1283 err
= security_socket_post_create(sock
, family
, type
, protocol
, kern
);
1285 goto out_sock_release
;
1291 err
= -EAFNOSUPPORT
;
1294 module_put(pf
->owner
);
1301 goto out_sock_release
;
1303 EXPORT_SYMBOL(__sock_create
);
1305 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
1307 return __sock_create(current
->nsproxy
->net_ns
, family
, type
, protocol
, res
, 0);
1309 EXPORT_SYMBOL(sock_create
);
1311 int sock_create_kern(struct net
*net
, int family
, int type
, int protocol
, struct socket
**res
)
1313 return __sock_create(net
, family
, type
, protocol
, res
, 1);
1315 EXPORT_SYMBOL(sock_create_kern
);
1317 SYSCALL_DEFINE3(socket
, int, family
, int, type
, int, protocol
)
1320 struct socket
*sock
;
1323 /* Check the SOCK_* constants for consistency. */
1324 BUILD_BUG_ON(SOCK_CLOEXEC
!= O_CLOEXEC
);
1325 BUILD_BUG_ON((SOCK_MAX
| SOCK_TYPE_MASK
) != SOCK_TYPE_MASK
);
1326 BUILD_BUG_ON(SOCK_CLOEXEC
& SOCK_TYPE_MASK
);
1327 BUILD_BUG_ON(SOCK_NONBLOCK
& SOCK_TYPE_MASK
);
1329 flags
= type
& ~SOCK_TYPE_MASK
;
1330 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1332 type
&= SOCK_TYPE_MASK
;
1334 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1335 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1337 retval
= sock_create(family
, type
, protocol
, &sock
);
1341 return sock_map_fd(sock
, flags
& (O_CLOEXEC
| O_NONBLOCK
));
1345 * Create a pair of connected sockets.
1348 SYSCALL_DEFINE4(socketpair
, int, family
, int, type
, int, protocol
,
1349 int __user
*, usockvec
)
1351 struct socket
*sock1
, *sock2
;
1353 struct file
*newfile1
, *newfile2
;
1356 flags
= type
& ~SOCK_TYPE_MASK
;
1357 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1359 type
&= SOCK_TYPE_MASK
;
1361 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1362 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1365 * reserve descriptors and make sure we won't fail
1366 * to return them to userland.
1368 fd1
= get_unused_fd_flags(flags
);
1369 if (unlikely(fd1
< 0))
1372 fd2
= get_unused_fd_flags(flags
);
1373 if (unlikely(fd2
< 0)) {
1378 err
= put_user(fd1
, &usockvec
[0]);
1382 err
= put_user(fd2
, &usockvec
[1]);
1387 * Obtain the first socket and check if the underlying protocol
1388 * supports the socketpair call.
1391 err
= sock_create(family
, type
, protocol
, &sock1
);
1392 if (unlikely(err
< 0))
1395 err
= sock_create(family
, type
, protocol
, &sock2
);
1396 if (unlikely(err
< 0)) {
1397 sock_release(sock1
);
1401 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1402 if (unlikely(err
< 0)) {
1403 sock_release(sock2
);
1404 sock_release(sock1
);
1408 newfile1
= sock_alloc_file(sock1
, flags
, NULL
);
1409 if (IS_ERR(newfile1
)) {
1410 err
= PTR_ERR(newfile1
);
1411 sock_release(sock2
);
1415 newfile2
= sock_alloc_file(sock2
, flags
, NULL
);
1416 if (IS_ERR(newfile2
)) {
1417 err
= PTR_ERR(newfile2
);
1422 audit_fd_pair(fd1
, fd2
);
1424 fd_install(fd1
, newfile1
);
1425 fd_install(fd2
, newfile2
);
1435 * Bind a name to a socket. Nothing much to do here since it's
1436 * the protocol's responsibility to handle the local address.
1438 * We move the socket address to kernel space before we call
1439 * the protocol layer (having also checked the address is ok).
1442 SYSCALL_DEFINE3(bind
, int, fd
, struct sockaddr __user
*, umyaddr
, int, addrlen
)
1444 struct socket
*sock
;
1445 struct sockaddr_storage address
;
1446 int err
, fput_needed
;
1448 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1450 err
= move_addr_to_kernel(umyaddr
, addrlen
, &address
);
1452 err
= security_socket_bind(sock
,
1453 (struct sockaddr
*)&address
,
1456 err
= sock
->ops
->bind(sock
,
1460 fput_light(sock
->file
, fput_needed
);
1466 * Perform a listen. Basically, we allow the protocol to do anything
1467 * necessary for a listen, and if that works, we mark the socket as
1468 * ready for listening.
1471 SYSCALL_DEFINE2(listen
, int, fd
, int, backlog
)
1473 struct socket
*sock
;
1474 int err
, fput_needed
;
1477 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1479 somaxconn
= sock_net(sock
->sk
)->core
.sysctl_somaxconn
;
1480 if ((unsigned int)backlog
> somaxconn
)
1481 backlog
= somaxconn
;
1483 err
= security_socket_listen(sock
, backlog
);
1485 err
= sock
->ops
->listen(sock
, backlog
);
1487 fput_light(sock
->file
, fput_needed
);
1493 * For accept, we attempt to create a new socket, set up the link
1494 * with the client, wake up the client, then return the new
1495 * connected fd. We collect the address of the connector in kernel
1496 * space and move it to user at the very end. This is unclean because
1497 * we open the socket then return an error.
1499 * 1003.1g adds the ability to recvmsg() to query connection pending
1500 * status to recvmsg. We need to add that support in a way thats
1501 * clean when we restucture accept also.
1504 SYSCALL_DEFINE4(accept4
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1505 int __user
*, upeer_addrlen
, int, flags
)
1507 struct socket
*sock
, *newsock
;
1508 struct file
*newfile
;
1509 int err
, len
, newfd
, fput_needed
;
1510 struct sockaddr_storage address
;
1512 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1515 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1516 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1518 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1523 newsock
= sock_alloc();
1527 newsock
->type
= sock
->type
;
1528 newsock
->ops
= sock
->ops
;
1531 * We don't need try_module_get here, as the listening socket (sock)
1532 * has the protocol module (sock->ops->owner) held.
1534 __module_get(newsock
->ops
->owner
);
1536 newfd
= get_unused_fd_flags(flags
);
1537 if (unlikely(newfd
< 0)) {
1539 sock_release(newsock
);
1542 newfile
= sock_alloc_file(newsock
, flags
, sock
->sk
->sk_prot_creator
->name
);
1543 if (IS_ERR(newfile
)) {
1544 err
= PTR_ERR(newfile
);
1545 put_unused_fd(newfd
);
1549 err
= security_socket_accept(sock
, newsock
);
1553 err
= sock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
, false);
1557 if (upeer_sockaddr
) {
1558 if (newsock
->ops
->getname(newsock
, (struct sockaddr
*)&address
,
1560 err
= -ECONNABORTED
;
1563 err
= move_addr_to_user(&address
,
1564 len
, upeer_sockaddr
, upeer_addrlen
);
1569 /* File flags are not inherited via accept() unlike another OSes. */
1571 fd_install(newfd
, newfile
);
1575 fput_light(sock
->file
, fput_needed
);
1580 put_unused_fd(newfd
);
1584 SYSCALL_DEFINE3(accept
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1585 int __user
*, upeer_addrlen
)
1587 return sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, 0);
1591 * Attempt to connect to a socket with the server address. The address
1592 * is in user space so we verify it is OK and move it to kernel space.
1594 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1597 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1598 * other SEQPACKET protocols that take time to connect() as it doesn't
1599 * include the -EINPROGRESS status for such sockets.
1602 SYSCALL_DEFINE3(connect
, int, fd
, struct sockaddr __user
*, uservaddr
,
1605 struct socket
*sock
;
1606 struct sockaddr_storage address
;
1607 int err
, fput_needed
;
1609 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1612 err
= move_addr_to_kernel(uservaddr
, addrlen
, &address
);
1617 security_socket_connect(sock
, (struct sockaddr
*)&address
, addrlen
);
1621 err
= sock
->ops
->connect(sock
, (struct sockaddr
*)&address
, addrlen
,
1622 sock
->file
->f_flags
);
1624 fput_light(sock
->file
, fput_needed
);
1630 * Get the local address ('name') of a socket object. Move the obtained
1631 * name to user space.
1634 SYSCALL_DEFINE3(getsockname
, int, fd
, struct sockaddr __user
*, usockaddr
,
1635 int __user
*, usockaddr_len
)
1637 struct socket
*sock
;
1638 struct sockaddr_storage address
;
1639 int len
, err
, fput_needed
;
1641 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1645 err
= security_socket_getsockname(sock
);
1649 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
, 0);
1652 err
= move_addr_to_user(&address
, len
, usockaddr
, usockaddr_len
);
1655 fput_light(sock
->file
, fput_needed
);
1661 * Get the remote address ('name') of a socket object. Move the obtained
1662 * name to user space.
1665 SYSCALL_DEFINE3(getpeername
, int, fd
, struct sockaddr __user
*, usockaddr
,
1666 int __user
*, usockaddr_len
)
1668 struct socket
*sock
;
1669 struct sockaddr_storage address
;
1670 int len
, err
, fput_needed
;
1672 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1674 err
= security_socket_getpeername(sock
);
1676 fput_light(sock
->file
, fput_needed
);
1681 sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
,
1684 err
= move_addr_to_user(&address
, len
, usockaddr
,
1686 fput_light(sock
->file
, fput_needed
);
1692 * Send a datagram to a given address. We move the address into kernel
1693 * space and check the user space data area is readable before invoking
1697 SYSCALL_DEFINE6(sendto
, int, fd
, void __user
*, buff
, size_t, len
,
1698 unsigned int, flags
, struct sockaddr __user
*, addr
,
1701 struct socket
*sock
;
1702 struct sockaddr_storage address
;
1708 err
= import_single_range(WRITE
, buff
, len
, &iov
, &msg
.msg_iter
);
1711 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1715 msg
.msg_name
= NULL
;
1716 msg
.msg_control
= NULL
;
1717 msg
.msg_controllen
= 0;
1718 msg
.msg_namelen
= 0;
1720 err
= move_addr_to_kernel(addr
, addr_len
, &address
);
1723 msg
.msg_name
= (struct sockaddr
*)&address
;
1724 msg
.msg_namelen
= addr_len
;
1726 if (sock
->file
->f_flags
& O_NONBLOCK
)
1727 flags
|= MSG_DONTWAIT
;
1728 msg
.msg_flags
= flags
;
1729 err
= sock_sendmsg(sock
, &msg
);
1732 fput_light(sock
->file
, fput_needed
);
1738 * Send a datagram down a socket.
1741 SYSCALL_DEFINE4(send
, int, fd
, void __user
*, buff
, size_t, len
,
1742 unsigned int, flags
)
1744 return sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1748 * Receive a frame from the socket and optionally record the address of the
1749 * sender. We verify the buffers are writable and if needed move the
1750 * sender address from kernel to user space.
1753 SYSCALL_DEFINE6(recvfrom
, int, fd
, void __user
*, ubuf
, size_t, size
,
1754 unsigned int, flags
, struct sockaddr __user
*, addr
,
1755 int __user
*, addr_len
)
1757 struct socket
*sock
;
1760 struct sockaddr_storage address
;
1764 err
= import_single_range(READ
, ubuf
, size
, &iov
, &msg
.msg_iter
);
1767 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1771 msg
.msg_control
= NULL
;
1772 msg
.msg_controllen
= 0;
1773 /* Save some cycles and don't copy the address if not needed */
1774 msg
.msg_name
= addr
? (struct sockaddr
*)&address
: NULL
;
1775 /* We assume all kernel code knows the size of sockaddr_storage */
1776 msg
.msg_namelen
= 0;
1777 msg
.msg_iocb
= NULL
;
1779 if (sock
->file
->f_flags
& O_NONBLOCK
)
1780 flags
|= MSG_DONTWAIT
;
1781 err
= sock_recvmsg(sock
, &msg
, flags
);
1783 if (err
>= 0 && addr
!= NULL
) {
1784 err2
= move_addr_to_user(&address
,
1785 msg
.msg_namelen
, addr
, addr_len
);
1790 fput_light(sock
->file
, fput_needed
);
1796 * Receive a datagram from a socket.
1799 SYSCALL_DEFINE4(recv
, int, fd
, void __user
*, ubuf
, size_t, size
,
1800 unsigned int, flags
)
1802 return sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1806 * Set a socket option. Because we don't know the option lengths we have
1807 * to pass the user mode parameter for the protocols to sort out.
1810 SYSCALL_DEFINE5(setsockopt
, int, fd
, int, level
, int, optname
,
1811 char __user
*, optval
, int, optlen
)
1813 int err
, fput_needed
;
1814 struct socket
*sock
;
1819 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1821 err
= security_socket_setsockopt(sock
, level
, optname
);
1825 if (level
== SOL_SOCKET
)
1827 sock_setsockopt(sock
, level
, optname
, optval
,
1831 sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
1834 fput_light(sock
->file
, fput_needed
);
1840 * Get a socket option. Because we don't know the option lengths we have
1841 * to pass a user mode parameter for the protocols to sort out.
1844 SYSCALL_DEFINE5(getsockopt
, int, fd
, int, level
, int, optname
,
1845 char __user
*, optval
, int __user
*, optlen
)
1847 int err
, fput_needed
;
1848 struct socket
*sock
;
1850 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1852 err
= security_socket_getsockopt(sock
, level
, optname
);
1856 if (level
== SOL_SOCKET
)
1858 sock_getsockopt(sock
, level
, optname
, optval
,
1862 sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
1865 fput_light(sock
->file
, fput_needed
);
1871 * Shutdown a socket.
1874 SYSCALL_DEFINE2(shutdown
, int, fd
, int, how
)
1876 int err
, fput_needed
;
1877 struct socket
*sock
;
1879 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1881 err
= security_socket_shutdown(sock
, how
);
1883 err
= sock
->ops
->shutdown(sock
, how
);
1884 fput_light(sock
->file
, fput_needed
);
1889 /* A couple of helpful macros for getting the address of the 32/64 bit
1890 * fields which are the same type (int / unsigned) on our platforms.
1892 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1893 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1894 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1896 struct used_address
{
1897 struct sockaddr_storage name
;
1898 unsigned int name_len
;
1901 static int copy_msghdr_from_user(struct msghdr
*kmsg
,
1902 struct user_msghdr __user
*umsg
,
1903 struct sockaddr __user
**save_addr
,
1906 struct user_msghdr msg
;
1909 if (copy_from_user(&msg
, umsg
, sizeof(*umsg
)))
1912 kmsg
->msg_control
= (void __force
*)msg
.msg_control
;
1913 kmsg
->msg_controllen
= msg
.msg_controllen
;
1914 kmsg
->msg_flags
= msg
.msg_flags
;
1916 kmsg
->msg_namelen
= msg
.msg_namelen
;
1918 kmsg
->msg_namelen
= 0;
1920 if (kmsg
->msg_namelen
< 0)
1923 if (kmsg
->msg_namelen
> sizeof(struct sockaddr_storage
))
1924 kmsg
->msg_namelen
= sizeof(struct sockaddr_storage
);
1927 *save_addr
= msg
.msg_name
;
1929 if (msg
.msg_name
&& kmsg
->msg_namelen
) {
1931 err
= move_addr_to_kernel(msg
.msg_name
,
1938 kmsg
->msg_name
= NULL
;
1939 kmsg
->msg_namelen
= 0;
1942 if (msg
.msg_iovlen
> UIO_MAXIOV
)
1945 kmsg
->msg_iocb
= NULL
;
1947 return import_iovec(save_addr
? READ
: WRITE
,
1948 msg
.msg_iov
, msg
.msg_iovlen
,
1949 UIO_FASTIOV
, iov
, &kmsg
->msg_iter
);
1952 static int ___sys_sendmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
1953 struct msghdr
*msg_sys
, unsigned int flags
,
1954 struct used_address
*used_address
,
1955 unsigned int allowed_msghdr_flags
)
1957 struct compat_msghdr __user
*msg_compat
=
1958 (struct compat_msghdr __user
*)msg
;
1959 struct sockaddr_storage address
;
1960 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1961 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
1962 __aligned(sizeof(__kernel_size_t
));
1963 /* 20 is size of ipv6_pktinfo */
1964 unsigned char *ctl_buf
= ctl
;
1968 msg_sys
->msg_name
= &address
;
1970 if (MSG_CMSG_COMPAT
& flags
)
1971 err
= get_compat_msghdr(msg_sys
, msg_compat
, NULL
, &iov
);
1973 err
= copy_msghdr_from_user(msg_sys
, msg
, NULL
, &iov
);
1979 if (msg_sys
->msg_controllen
> INT_MAX
)
1981 flags
|= (msg_sys
->msg_flags
& allowed_msghdr_flags
);
1982 ctl_len
= msg_sys
->msg_controllen
;
1983 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
1985 cmsghdr_from_user_compat_to_kern(msg_sys
, sock
->sk
, ctl
,
1989 ctl_buf
= msg_sys
->msg_control
;
1990 ctl_len
= msg_sys
->msg_controllen
;
1991 } else if (ctl_len
) {
1992 BUILD_BUG_ON(sizeof(struct cmsghdr
) !=
1993 CMSG_ALIGN(sizeof(struct cmsghdr
)));
1994 if (ctl_len
> sizeof(ctl
)) {
1995 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
1996 if (ctl_buf
== NULL
)
2001 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2002 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2003 * checking falls down on this.
2005 if (copy_from_user(ctl_buf
,
2006 (void __user __force
*)msg_sys
->msg_control
,
2009 msg_sys
->msg_control
= ctl_buf
;
2011 msg_sys
->msg_flags
= flags
;
2013 if (sock
->file
->f_flags
& O_NONBLOCK
)
2014 msg_sys
->msg_flags
|= MSG_DONTWAIT
;
2016 * If this is sendmmsg() and current destination address is same as
2017 * previously succeeded address, omit asking LSM's decision.
2018 * used_address->name_len is initialized to UINT_MAX so that the first
2019 * destination address never matches.
2021 if (used_address
&& msg_sys
->msg_name
&&
2022 used_address
->name_len
== msg_sys
->msg_namelen
&&
2023 !memcmp(&used_address
->name
, msg_sys
->msg_name
,
2024 used_address
->name_len
)) {
2025 err
= sock_sendmsg_nosec(sock
, msg_sys
);
2028 err
= sock_sendmsg(sock
, msg_sys
);
2030 * If this is sendmmsg() and sending to current destination address was
2031 * successful, remember it.
2033 if (used_address
&& err
>= 0) {
2034 used_address
->name_len
= msg_sys
->msg_namelen
;
2035 if (msg_sys
->msg_name
)
2036 memcpy(&used_address
->name
, msg_sys
->msg_name
,
2037 used_address
->name_len
);
2042 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
2049 * BSD sendmsg interface
2052 long __sys_sendmsg(int fd
, struct user_msghdr __user
*msg
, unsigned flags
)
2054 int fput_needed
, err
;
2055 struct msghdr msg_sys
;
2056 struct socket
*sock
;
2058 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2062 err
= ___sys_sendmsg(sock
, msg
, &msg_sys
, flags
, NULL
, 0);
2064 fput_light(sock
->file
, fput_needed
);
2069 SYSCALL_DEFINE3(sendmsg
, int, fd
, struct user_msghdr __user
*, msg
, unsigned int, flags
)
2071 if (flags
& MSG_CMSG_COMPAT
)
2073 return __sys_sendmsg(fd
, msg
, flags
);
2077 * Linux sendmmsg interface
2080 int __sys_sendmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2083 int fput_needed
, err
, datagrams
;
2084 struct socket
*sock
;
2085 struct mmsghdr __user
*entry
;
2086 struct compat_mmsghdr __user
*compat_entry
;
2087 struct msghdr msg_sys
;
2088 struct used_address used_address
;
2089 unsigned int oflags
= flags
;
2091 if (vlen
> UIO_MAXIOV
)
2096 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2100 used_address
.name_len
= UINT_MAX
;
2102 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2106 while (datagrams
< vlen
) {
2107 if (datagrams
== vlen
- 1)
2110 if (MSG_CMSG_COMPAT
& flags
) {
2111 err
= ___sys_sendmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2112 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2115 err
= __put_user(err
, &compat_entry
->msg_len
);
2118 err
= ___sys_sendmsg(sock
,
2119 (struct user_msghdr __user
*)entry
,
2120 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2123 err
= put_user(err
, &entry
->msg_len
);
2130 if (msg_data_left(&msg_sys
))
2135 fput_light(sock
->file
, fput_needed
);
2137 /* We only return an error if no datagrams were able to be sent */
2144 SYSCALL_DEFINE4(sendmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2145 unsigned int, vlen
, unsigned int, flags
)
2147 if (flags
& MSG_CMSG_COMPAT
)
2149 return __sys_sendmmsg(fd
, mmsg
, vlen
, flags
);
2152 static int ___sys_recvmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
2153 struct msghdr
*msg_sys
, unsigned int flags
, int nosec
)
2155 struct compat_msghdr __user
*msg_compat
=
2156 (struct compat_msghdr __user
*)msg
;
2157 struct iovec iovstack
[UIO_FASTIOV
];
2158 struct iovec
*iov
= iovstack
;
2159 unsigned long cmsg_ptr
;
2163 /* kernel mode address */
2164 struct sockaddr_storage addr
;
2166 /* user mode address pointers */
2167 struct sockaddr __user
*uaddr
;
2168 int __user
*uaddr_len
= COMPAT_NAMELEN(msg
);
2170 msg_sys
->msg_name
= &addr
;
2172 if (MSG_CMSG_COMPAT
& flags
)
2173 err
= get_compat_msghdr(msg_sys
, msg_compat
, &uaddr
, &iov
);
2175 err
= copy_msghdr_from_user(msg_sys
, msg
, &uaddr
, &iov
);
2179 cmsg_ptr
= (unsigned long)msg_sys
->msg_control
;
2180 msg_sys
->msg_flags
= flags
& (MSG_CMSG_CLOEXEC
|MSG_CMSG_COMPAT
);
2182 /* We assume all kernel code knows the size of sockaddr_storage */
2183 msg_sys
->msg_namelen
= 0;
2185 if (sock
->file
->f_flags
& O_NONBLOCK
)
2186 flags
|= MSG_DONTWAIT
;
2187 err
= (nosec
? sock_recvmsg_nosec
: sock_recvmsg
)(sock
, msg_sys
, flags
);
2192 if (uaddr
!= NULL
) {
2193 err
= move_addr_to_user(&addr
,
2194 msg_sys
->msg_namelen
, uaddr
,
2199 err
= __put_user((msg_sys
->msg_flags
& ~MSG_CMSG_COMPAT
),
2203 if (MSG_CMSG_COMPAT
& flags
)
2204 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2205 &msg_compat
->msg_controllen
);
2207 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2208 &msg
->msg_controllen
);
2219 * BSD recvmsg interface
2222 long __sys_recvmsg(int fd
, struct user_msghdr __user
*msg
, unsigned flags
)
2224 int fput_needed
, err
;
2225 struct msghdr msg_sys
;
2226 struct socket
*sock
;
2228 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2232 err
= ___sys_recvmsg(sock
, msg
, &msg_sys
, flags
, 0);
2234 fput_light(sock
->file
, fput_needed
);
2239 SYSCALL_DEFINE3(recvmsg
, int, fd
, struct user_msghdr __user
*, msg
,
2240 unsigned int, flags
)
2242 if (flags
& MSG_CMSG_COMPAT
)
2244 return __sys_recvmsg(fd
, msg
, flags
);
2248 * Linux recvmmsg interface
2251 int __sys_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2252 unsigned int flags
, struct timespec
*timeout
)
2254 int fput_needed
, err
, datagrams
;
2255 struct socket
*sock
;
2256 struct mmsghdr __user
*entry
;
2257 struct compat_mmsghdr __user
*compat_entry
;
2258 struct msghdr msg_sys
;
2259 struct timespec64 end_time
;
2260 struct timespec64 timeout64
;
2263 poll_select_set_timeout(&end_time
, timeout
->tv_sec
,
2269 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2273 err
= sock_error(sock
->sk
);
2280 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2282 while (datagrams
< vlen
) {
2284 * No need to ask LSM for more than the first datagram.
2286 if (MSG_CMSG_COMPAT
& flags
) {
2287 err
= ___sys_recvmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2288 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2292 err
= __put_user(err
, &compat_entry
->msg_len
);
2295 err
= ___sys_recvmsg(sock
,
2296 (struct user_msghdr __user
*)entry
,
2297 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2301 err
= put_user(err
, &entry
->msg_len
);
2309 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2310 if (flags
& MSG_WAITFORONE
)
2311 flags
|= MSG_DONTWAIT
;
2314 ktime_get_ts64(&timeout64
);
2315 *timeout
= timespec64_to_timespec(
2316 timespec64_sub(end_time
, timeout64
));
2317 if (timeout
->tv_sec
< 0) {
2318 timeout
->tv_sec
= timeout
->tv_nsec
= 0;
2322 /* Timeout, return less than vlen datagrams */
2323 if (timeout
->tv_nsec
== 0 && timeout
->tv_sec
== 0)
2327 /* Out of band data, return right away */
2328 if (msg_sys
.msg_flags
& MSG_OOB
)
2336 if (datagrams
== 0) {
2342 * We may return less entries than requested (vlen) if the
2343 * sock is non block and there aren't enough datagrams...
2345 if (err
!= -EAGAIN
) {
2347 * ... or if recvmsg returns an error after we
2348 * received some datagrams, where we record the
2349 * error to return on the next call or if the
2350 * app asks about it using getsockopt(SO_ERROR).
2352 sock
->sk
->sk_err
= -err
;
2355 fput_light(sock
->file
, fput_needed
);
2360 SYSCALL_DEFINE5(recvmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2361 unsigned int, vlen
, unsigned int, flags
,
2362 struct timespec __user
*, timeout
)
2365 struct timespec timeout_sys
;
2367 if (flags
& MSG_CMSG_COMPAT
)
2371 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
);
2373 if (copy_from_user(&timeout_sys
, timeout
, sizeof(timeout_sys
)))
2376 datagrams
= __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, &timeout_sys
);
2378 if (datagrams
> 0 &&
2379 copy_to_user(timeout
, &timeout_sys
, sizeof(timeout_sys
)))
2380 datagrams
= -EFAULT
;
2385 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2386 /* Argument list sizes for sys_socketcall */
2387 #define AL(x) ((x) * sizeof(unsigned long))
2388 static const unsigned char nargs
[21] = {
2389 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2390 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2391 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2398 * System call vectors.
2400 * Argument checking cleaned up. Saved 20% in size.
2401 * This function doesn't need to set the kernel lock because
2402 * it is set by the callees.
2405 SYSCALL_DEFINE2(socketcall
, int, call
, unsigned long __user
*, args
)
2407 unsigned long a
[AUDITSC_ARGS
];
2408 unsigned long a0
, a1
;
2412 if (call
< 1 || call
> SYS_SENDMMSG
)
2416 if (len
> sizeof(a
))
2419 /* copy_from_user should be SMP safe. */
2420 if (copy_from_user(a
, args
, len
))
2423 err
= audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
2432 err
= sys_socket(a0
, a1
, a
[2]);
2435 err
= sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2438 err
= sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2441 err
= sys_listen(a0
, a1
);
2444 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2445 (int __user
*)a
[2], 0);
2447 case SYS_GETSOCKNAME
:
2449 sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
2450 (int __user
*)a
[2]);
2452 case SYS_GETPEERNAME
:
2454 sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
2455 (int __user
*)a
[2]);
2457 case SYS_SOCKETPAIR
:
2458 err
= sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
2461 err
= sys_send(a0
, (void __user
*)a1
, a
[2], a
[3]);
2464 err
= sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2465 (struct sockaddr __user
*)a
[4], a
[5]);
2468 err
= sys_recv(a0
, (void __user
*)a1
, a
[2], a
[3]);
2471 err
= sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2472 (struct sockaddr __user
*)a
[4],
2473 (int __user
*)a
[5]);
2476 err
= sys_shutdown(a0
, a1
);
2478 case SYS_SETSOCKOPT
:
2479 err
= sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], a
[4]);
2481 case SYS_GETSOCKOPT
:
2483 sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2484 (int __user
*)a
[4]);
2487 err
= sys_sendmsg(a0
, (struct user_msghdr __user
*)a1
, a
[2]);
2490 err
= sys_sendmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2], a
[3]);
2493 err
= sys_recvmsg(a0
, (struct user_msghdr __user
*)a1
, a
[2]);
2496 err
= sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2], a
[3],
2497 (struct timespec __user
*)a
[4]);
2500 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2501 (int __user
*)a
[2], a
[3]);
2510 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2513 * sock_register - add a socket protocol handler
2514 * @ops: description of protocol
2516 * This function is called by a protocol handler that wants to
2517 * advertise its address family, and have it linked into the
2518 * socket interface. The value ops->family corresponds to the
2519 * socket system call protocol family.
2521 int sock_register(const struct net_proto_family
*ops
)
2525 if (ops
->family
>= NPROTO
) {
2526 pr_crit("protocol %d >= NPROTO(%d)\n", ops
->family
, NPROTO
);
2530 spin_lock(&net_family_lock
);
2531 if (rcu_dereference_protected(net_families
[ops
->family
],
2532 lockdep_is_held(&net_family_lock
)))
2535 rcu_assign_pointer(net_families
[ops
->family
], ops
);
2538 spin_unlock(&net_family_lock
);
2540 pr_info("NET: Registered protocol family %d\n", ops
->family
);
2543 EXPORT_SYMBOL(sock_register
);
2546 * sock_unregister - remove a protocol handler
2547 * @family: protocol family to remove
2549 * This function is called by a protocol handler that wants to
2550 * remove its address family, and have it unlinked from the
2551 * new socket creation.
2553 * If protocol handler is a module, then it can use module reference
2554 * counts to protect against new references. If protocol handler is not
2555 * a module then it needs to provide its own protection in
2556 * the ops->create routine.
2558 void sock_unregister(int family
)
2560 BUG_ON(family
< 0 || family
>= NPROTO
);
2562 spin_lock(&net_family_lock
);
2563 RCU_INIT_POINTER(net_families
[family
], NULL
);
2564 spin_unlock(&net_family_lock
);
2568 pr_info("NET: Unregistered protocol family %d\n", family
);
2570 EXPORT_SYMBOL(sock_unregister
);
2572 static int __init
sock_init(void)
2576 * Initialize the network sysctl infrastructure.
2578 err
= net_sysctl_init();
2583 * Initialize skbuff SLAB cache
2588 * Initialize the protocols module.
2593 err
= register_filesystem(&sock_fs_type
);
2596 sock_mnt
= kern_mount(&sock_fs_type
);
2597 if (IS_ERR(sock_mnt
)) {
2598 err
= PTR_ERR(sock_mnt
);
2602 /* The real protocol initialization is performed in later initcalls.
2605 #ifdef CONFIG_NETFILTER
2606 err
= netfilter_init();
2611 ptp_classifier_init();
2617 unregister_filesystem(&sock_fs_type
);
2622 core_initcall(sock_init
); /* early initcall */
2624 static int __init
jit_init(void)
2626 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
2631 pure_initcall(jit_init
);
2633 #ifdef CONFIG_PROC_FS
2634 void socket_seq_show(struct seq_file
*seq
)
2639 for_each_possible_cpu(cpu
)
2640 counter
+= per_cpu(sockets_in_use
, cpu
);
2642 /* It can be negative, by the way. 8) */
2646 seq_printf(seq
, "sockets: used %d\n", counter
);
2648 #endif /* CONFIG_PROC_FS */
2650 #ifdef CONFIG_COMPAT
2651 static int do_siocgstamp(struct net
*net
, struct socket
*sock
,
2652 unsigned int cmd
, void __user
*up
)
2654 mm_segment_t old_fs
= get_fs();
2659 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&ktv
);
2662 err
= compat_put_timeval(&ktv
, up
);
2667 static int do_siocgstampns(struct net
*net
, struct socket
*sock
,
2668 unsigned int cmd
, void __user
*up
)
2670 mm_segment_t old_fs
= get_fs();
2671 struct timespec kts
;
2675 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&kts
);
2678 err
= compat_put_timespec(&kts
, up
);
2683 static int dev_ifname32(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2685 struct ifreq __user
*uifr
;
2688 uifr
= compat_alloc_user_space(sizeof(struct ifreq
));
2689 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2692 err
= dev_ioctl(net
, SIOCGIFNAME
, uifr
);
2696 if (copy_in_user(uifr32
, uifr
, sizeof(struct compat_ifreq
)))
2702 static int dev_ifconf(struct net
*net
, struct compat_ifconf __user
*uifc32
)
2704 struct compat_ifconf ifc32
;
2706 struct ifconf __user
*uifc
;
2707 struct compat_ifreq __user
*ifr32
;
2708 struct ifreq __user
*ifr
;
2712 if (copy_from_user(&ifc32
, uifc32
, sizeof(struct compat_ifconf
)))
2715 memset(&ifc
, 0, sizeof(ifc
));
2716 if (ifc32
.ifcbuf
== 0) {
2720 uifc
= compat_alloc_user_space(sizeof(struct ifconf
));
2722 size_t len
= ((ifc32
.ifc_len
/ sizeof(struct compat_ifreq
)) + 1) *
2723 sizeof(struct ifreq
);
2724 uifc
= compat_alloc_user_space(sizeof(struct ifconf
) + len
);
2726 ifr
= ifc
.ifc_req
= (void __user
*)(uifc
+ 1);
2727 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2728 for (i
= 0; i
< ifc32
.ifc_len
; i
+= sizeof(struct compat_ifreq
)) {
2729 if (copy_in_user(ifr
, ifr32
, sizeof(struct compat_ifreq
)))
2735 if (copy_to_user(uifc
, &ifc
, sizeof(struct ifconf
)))
2738 err
= dev_ioctl(net
, SIOCGIFCONF
, uifc
);
2742 if (copy_from_user(&ifc
, uifc
, sizeof(struct ifconf
)))
2746 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2748 i
+ sizeof(struct compat_ifreq
) <= ifc32
.ifc_len
&& j
< ifc
.ifc_len
;
2749 i
+= sizeof(struct compat_ifreq
), j
+= sizeof(struct ifreq
)) {
2750 if (copy_in_user(ifr32
, ifr
, sizeof(struct compat_ifreq
)))
2756 if (ifc32
.ifcbuf
== 0) {
2757 /* Translate from 64-bit structure multiple to
2761 i
= ((i
/ sizeof(struct ifreq
)) * sizeof(struct compat_ifreq
));
2766 if (copy_to_user(uifc32
, &ifc32
, sizeof(struct compat_ifconf
)))
2772 static int ethtool_ioctl(struct net
*net
, struct compat_ifreq __user
*ifr32
)
2774 struct compat_ethtool_rxnfc __user
*compat_rxnfc
;
2775 bool convert_in
= false, convert_out
= false;
2776 size_t buf_size
= ALIGN(sizeof(struct ifreq
), 8);
2777 struct ethtool_rxnfc __user
*rxnfc
;
2778 struct ifreq __user
*ifr
;
2779 u32 rule_cnt
= 0, actual_rule_cnt
;
2784 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2787 compat_rxnfc
= compat_ptr(data
);
2789 if (get_user(ethcmd
, &compat_rxnfc
->cmd
))
2792 /* Most ethtool structures are defined without padding.
2793 * Unfortunately struct ethtool_rxnfc is an exception.
2798 case ETHTOOL_GRXCLSRLALL
:
2799 /* Buffer size is variable */
2800 if (get_user(rule_cnt
, &compat_rxnfc
->rule_cnt
))
2802 if (rule_cnt
> KMALLOC_MAX_SIZE
/ sizeof(u32
))
2804 buf_size
+= rule_cnt
* sizeof(u32
);
2806 case ETHTOOL_GRXRINGS
:
2807 case ETHTOOL_GRXCLSRLCNT
:
2808 case ETHTOOL_GRXCLSRULE
:
2809 case ETHTOOL_SRXCLSRLINS
:
2812 case ETHTOOL_SRXCLSRLDEL
:
2813 buf_size
+= sizeof(struct ethtool_rxnfc
);
2818 ifr
= compat_alloc_user_space(buf_size
);
2819 rxnfc
= (void __user
*)ifr
+ ALIGN(sizeof(struct ifreq
), 8);
2821 if (copy_in_user(&ifr
->ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
2824 if (put_user(convert_in
? rxnfc
: compat_ptr(data
),
2825 &ifr
->ifr_ifru
.ifru_data
))
2829 /* We expect there to be holes between fs.m_ext and
2830 * fs.ring_cookie and at the end of fs, but nowhere else.
2832 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc
, fs
.m_ext
) +
2833 sizeof(compat_rxnfc
->fs
.m_ext
) !=
2834 offsetof(struct ethtool_rxnfc
, fs
.m_ext
) +
2835 sizeof(rxnfc
->fs
.m_ext
));
2837 offsetof(struct compat_ethtool_rxnfc
, fs
.location
) -
2838 offsetof(struct compat_ethtool_rxnfc
, fs
.ring_cookie
) !=
2839 offsetof(struct ethtool_rxnfc
, fs
.location
) -
2840 offsetof(struct ethtool_rxnfc
, fs
.ring_cookie
));
2842 if (copy_in_user(rxnfc
, compat_rxnfc
,
2843 (void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
2844 (void __user
*)rxnfc
) ||
2845 copy_in_user(&rxnfc
->fs
.ring_cookie
,
2846 &compat_rxnfc
->fs
.ring_cookie
,
2847 (void __user
*)(&rxnfc
->fs
.location
+ 1) -
2848 (void __user
*)&rxnfc
->fs
.ring_cookie
) ||
2849 copy_in_user(&rxnfc
->rule_cnt
, &compat_rxnfc
->rule_cnt
,
2850 sizeof(rxnfc
->rule_cnt
)))
2854 ret
= dev_ioctl(net
, SIOCETHTOOL
, ifr
);
2859 if (copy_in_user(compat_rxnfc
, rxnfc
,
2860 (const void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
2861 (const void __user
*)rxnfc
) ||
2862 copy_in_user(&compat_rxnfc
->fs
.ring_cookie
,
2863 &rxnfc
->fs
.ring_cookie
,
2864 (const void __user
*)(&rxnfc
->fs
.location
+ 1) -
2865 (const void __user
*)&rxnfc
->fs
.ring_cookie
) ||
2866 copy_in_user(&compat_rxnfc
->rule_cnt
, &rxnfc
->rule_cnt
,
2867 sizeof(rxnfc
->rule_cnt
)))
2870 if (ethcmd
== ETHTOOL_GRXCLSRLALL
) {
2871 /* As an optimisation, we only copy the actual
2872 * number of rules that the underlying
2873 * function returned. Since Mallory might
2874 * change the rule count in user memory, we
2875 * check that it is less than the rule count
2876 * originally given (as the user buffer size),
2877 * which has been range-checked.
2879 if (get_user(actual_rule_cnt
, &rxnfc
->rule_cnt
))
2881 if (actual_rule_cnt
< rule_cnt
)
2882 rule_cnt
= actual_rule_cnt
;
2883 if (copy_in_user(&compat_rxnfc
->rule_locs
[0],
2884 &rxnfc
->rule_locs
[0],
2885 rule_cnt
* sizeof(u32
)))
2893 static int compat_siocwandev(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2896 compat_uptr_t uptr32
;
2897 struct ifreq __user
*uifr
;
2899 uifr
= compat_alloc_user_space(sizeof(*uifr
));
2900 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2903 if (get_user(uptr32
, &uifr32
->ifr_settings
.ifs_ifsu
))
2906 uptr
= compat_ptr(uptr32
);
2908 if (put_user(uptr
, &uifr
->ifr_settings
.ifs_ifsu
.raw_hdlc
))
2911 return dev_ioctl(net
, SIOCWANDEV
, uifr
);
2914 static int bond_ioctl(struct net
*net
, unsigned int cmd
,
2915 struct compat_ifreq __user
*ifr32
)
2918 mm_segment_t old_fs
;
2922 case SIOCBONDENSLAVE
:
2923 case SIOCBONDRELEASE
:
2924 case SIOCBONDSETHWADDR
:
2925 case SIOCBONDCHANGEACTIVE
:
2926 if (copy_from_user(&kifr
, ifr32
, sizeof(struct compat_ifreq
)))
2931 err
= dev_ioctl(net
, cmd
,
2932 (struct ifreq __user __force
*) &kifr
);
2937 return -ENOIOCTLCMD
;
2941 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
2942 static int compat_ifr_data_ioctl(struct net
*net
, unsigned int cmd
,
2943 struct compat_ifreq __user
*u_ifreq32
)
2945 struct ifreq __user
*u_ifreq64
;
2946 char tmp_buf
[IFNAMSIZ
];
2947 void __user
*data64
;
2950 if (copy_from_user(&tmp_buf
[0], &(u_ifreq32
->ifr_ifrn
.ifrn_name
[0]),
2953 if (get_user(data32
, &u_ifreq32
->ifr_ifru
.ifru_data
))
2955 data64
= compat_ptr(data32
);
2957 u_ifreq64
= compat_alloc_user_space(sizeof(*u_ifreq64
));
2959 if (copy_to_user(&u_ifreq64
->ifr_ifrn
.ifrn_name
[0], &tmp_buf
[0],
2962 if (put_user(data64
, &u_ifreq64
->ifr_ifru
.ifru_data
))
2965 return dev_ioctl(net
, cmd
, u_ifreq64
);
2968 static int dev_ifsioc(struct net
*net
, struct socket
*sock
,
2969 unsigned int cmd
, struct compat_ifreq __user
*uifr32
)
2971 struct ifreq __user
*uifr
;
2974 uifr
= compat_alloc_user_space(sizeof(*uifr
));
2975 if (copy_in_user(uifr
, uifr32
, sizeof(*uifr32
)))
2978 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)uifr
);
2989 case SIOCGIFBRDADDR
:
2990 case SIOCGIFDSTADDR
:
2991 case SIOCGIFNETMASK
:
2996 if (copy_in_user(uifr32
, uifr
, sizeof(*uifr32
)))
3004 static int compat_sioc_ifmap(struct net
*net
, unsigned int cmd
,
3005 struct compat_ifreq __user
*uifr32
)
3008 struct compat_ifmap __user
*uifmap32
;
3009 mm_segment_t old_fs
;
3012 uifmap32
= &uifr32
->ifr_ifru
.ifru_map
;
3013 err
= copy_from_user(&ifr
, uifr32
, sizeof(ifr
.ifr_name
));
3014 err
|= get_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3015 err
|= get_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3016 err
|= get_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3017 err
|= get_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3018 err
|= get_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3019 err
|= get_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3025 err
= dev_ioctl(net
, cmd
, (void __user __force
*)&ifr
);
3028 if (cmd
== SIOCGIFMAP
&& !err
) {
3029 err
= copy_to_user(uifr32
, &ifr
, sizeof(ifr
.ifr_name
));
3030 err
|= put_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3031 err
|= put_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3032 err
|= put_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3033 err
|= put_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3034 err
|= put_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3035 err
|= put_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3044 struct sockaddr rt_dst
; /* target address */
3045 struct sockaddr rt_gateway
; /* gateway addr (RTF_GATEWAY) */
3046 struct sockaddr rt_genmask
; /* target network mask (IP) */
3047 unsigned short rt_flags
;
3050 unsigned char rt_tos
;
3051 unsigned char rt_class
;
3053 short rt_metric
; /* +1 for binary compatibility! */
3054 /* char * */ u32 rt_dev
; /* forcing the device at add */
3055 u32 rt_mtu
; /* per route MTU/Window */
3056 u32 rt_window
; /* Window clamping */
3057 unsigned short rt_irtt
; /* Initial RTT */
3060 struct in6_rtmsg32
{
3061 struct in6_addr rtmsg_dst
;
3062 struct in6_addr rtmsg_src
;
3063 struct in6_addr rtmsg_gateway
;
3073 static int routing_ioctl(struct net
*net
, struct socket
*sock
,
3074 unsigned int cmd
, void __user
*argp
)
3078 struct in6_rtmsg r6
;
3082 mm_segment_t old_fs
= get_fs();
3084 if (sock
&& sock
->sk
&& sock
->sk
->sk_family
== AF_INET6
) { /* ipv6 */
3085 struct in6_rtmsg32 __user
*ur6
= argp
;
3086 ret
= copy_from_user(&r6
.rtmsg_dst
, &(ur6
->rtmsg_dst
),
3087 3 * sizeof(struct in6_addr
));
3088 ret
|= get_user(r6
.rtmsg_type
, &(ur6
->rtmsg_type
));
3089 ret
|= get_user(r6
.rtmsg_dst_len
, &(ur6
->rtmsg_dst_len
));
3090 ret
|= get_user(r6
.rtmsg_src_len
, &(ur6
->rtmsg_src_len
));
3091 ret
|= get_user(r6
.rtmsg_metric
, &(ur6
->rtmsg_metric
));
3092 ret
|= get_user(r6
.rtmsg_info
, &(ur6
->rtmsg_info
));
3093 ret
|= get_user(r6
.rtmsg_flags
, &(ur6
->rtmsg_flags
));
3094 ret
|= get_user(r6
.rtmsg_ifindex
, &(ur6
->rtmsg_ifindex
));
3098 struct rtentry32 __user
*ur4
= argp
;
3099 ret
= copy_from_user(&r4
.rt_dst
, &(ur4
->rt_dst
),
3100 3 * sizeof(struct sockaddr
));
3101 ret
|= get_user(r4
.rt_flags
, &(ur4
->rt_flags
));
3102 ret
|= get_user(r4
.rt_metric
, &(ur4
->rt_metric
));
3103 ret
|= get_user(r4
.rt_mtu
, &(ur4
->rt_mtu
));
3104 ret
|= get_user(r4
.rt_window
, &(ur4
->rt_window
));
3105 ret
|= get_user(r4
.rt_irtt
, &(ur4
->rt_irtt
));
3106 ret
|= get_user(rtdev
, &(ur4
->rt_dev
));
3108 ret
|= copy_from_user(devname
, compat_ptr(rtdev
), 15);
3109 r4
.rt_dev
= (char __user __force
*)devname
;
3123 ret
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long) r
);
3130 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3131 * for some operations; this forces use of the newer bridge-utils that
3132 * use compatible ioctls
3134 static int old_bridge_ioctl(compat_ulong_t __user
*argp
)
3138 if (get_user(tmp
, argp
))
3140 if (tmp
== BRCTL_GET_VERSION
)
3141 return BRCTL_VERSION
+ 1;
3145 static int compat_sock_ioctl_trans(struct file
*file
, struct socket
*sock
,
3146 unsigned int cmd
, unsigned long arg
)
3148 void __user
*argp
= compat_ptr(arg
);
3149 struct sock
*sk
= sock
->sk
;
3150 struct net
*net
= sock_net(sk
);
3152 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))
3153 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3158 return old_bridge_ioctl(argp
);
3160 return dev_ifname32(net
, argp
);
3162 return dev_ifconf(net
, argp
);
3164 return ethtool_ioctl(net
, argp
);
3166 return compat_siocwandev(net
, argp
);
3169 return compat_sioc_ifmap(net
, cmd
, argp
);
3170 case SIOCBONDENSLAVE
:
3171 case SIOCBONDRELEASE
:
3172 case SIOCBONDSETHWADDR
:
3173 case SIOCBONDCHANGEACTIVE
:
3174 return bond_ioctl(net
, cmd
, argp
);
3177 return routing_ioctl(net
, sock
, cmd
, argp
);
3179 return do_siocgstamp(net
, sock
, cmd
, argp
);
3181 return do_siocgstampns(net
, sock
, cmd
, argp
);
3182 case SIOCBONDSLAVEINFOQUERY
:
3183 case SIOCBONDINFOQUERY
:
3186 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3199 return sock_ioctl(file
, cmd
, arg
);
3216 case SIOCSIFHWBROADCAST
:
3218 case SIOCGIFBRDADDR
:
3219 case SIOCSIFBRDADDR
:
3220 case SIOCGIFDSTADDR
:
3221 case SIOCSIFDSTADDR
:
3222 case SIOCGIFNETMASK
:
3223 case SIOCSIFNETMASK
:
3234 return dev_ifsioc(net
, sock
, cmd
, argp
);
3240 return sock_do_ioctl(net
, sock
, cmd
, arg
);
3243 return -ENOIOCTLCMD
;
3246 static long compat_sock_ioctl(struct file
*file
, unsigned int cmd
,
3249 struct socket
*sock
= file
->private_data
;
3250 int ret
= -ENOIOCTLCMD
;
3257 if (sock
->ops
->compat_ioctl
)
3258 ret
= sock
->ops
->compat_ioctl(sock
, cmd
, arg
);
3260 if (ret
== -ENOIOCTLCMD
&&
3261 (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
))
3262 ret
= compat_wext_handle_ioctl(net
, cmd
, arg
);
3264 if (ret
== -ENOIOCTLCMD
)
3265 ret
= compat_sock_ioctl_trans(file
, sock
, cmd
, arg
);
3271 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
3273 return sock
->ops
->bind(sock
, addr
, addrlen
);
3275 EXPORT_SYMBOL(kernel_bind
);
3277 int kernel_listen(struct socket
*sock
, int backlog
)
3279 return sock
->ops
->listen(sock
, backlog
);
3281 EXPORT_SYMBOL(kernel_listen
);
3283 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
3285 struct sock
*sk
= sock
->sk
;
3288 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
3293 err
= sock
->ops
->accept(sock
, *newsock
, flags
, true);
3295 sock_release(*newsock
);
3300 (*newsock
)->ops
= sock
->ops
;
3301 __module_get((*newsock
)->ops
->owner
);
3306 EXPORT_SYMBOL(kernel_accept
);
3308 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
3311 return sock
->ops
->connect(sock
, addr
, addrlen
, flags
);
3313 EXPORT_SYMBOL(kernel_connect
);
3315 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
,
3318 return sock
->ops
->getname(sock
, addr
, addrlen
, 0);
3320 EXPORT_SYMBOL(kernel_getsockname
);
3322 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
,
3325 return sock
->ops
->getname(sock
, addr
, addrlen
, 1);
3327 EXPORT_SYMBOL(kernel_getpeername
);
3329 int kernel_getsockopt(struct socket
*sock
, int level
, int optname
,
3330 char *optval
, int *optlen
)
3332 mm_segment_t oldfs
= get_fs();
3333 char __user
*uoptval
;
3334 int __user
*uoptlen
;
3337 uoptval
= (char __user __force
*) optval
;
3338 uoptlen
= (int __user __force
*) optlen
;
3341 if (level
== SOL_SOCKET
)
3342 err
= sock_getsockopt(sock
, level
, optname
, uoptval
, uoptlen
);
3344 err
= sock
->ops
->getsockopt(sock
, level
, optname
, uoptval
,
3349 EXPORT_SYMBOL(kernel_getsockopt
);
3351 int kernel_setsockopt(struct socket
*sock
, int level
, int optname
,
3352 char *optval
, unsigned int optlen
)
3354 mm_segment_t oldfs
= get_fs();
3355 char __user
*uoptval
;
3358 uoptval
= (char __user __force
*) optval
;
3361 if (level
== SOL_SOCKET
)
3362 err
= sock_setsockopt(sock
, level
, optname
, uoptval
, optlen
);
3364 err
= sock
->ops
->setsockopt(sock
, level
, optname
, uoptval
,
3369 EXPORT_SYMBOL(kernel_setsockopt
);
3371 int kernel_sendpage(struct socket
*sock
, struct page
*page
, int offset
,
3372 size_t size
, int flags
)
3374 if (sock
->ops
->sendpage
)
3375 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
3377 return sock_no_sendpage(sock
, page
, offset
, size
, flags
);
3379 EXPORT_SYMBOL(kernel_sendpage
);
3381 int kernel_sendpage_locked(struct sock
*sk
, struct page
*page
, int offset
,
3382 size_t size
, int flags
)
3384 struct socket
*sock
= sk
->sk_socket
;
3386 if (sock
->ops
->sendpage_locked
)
3387 return sock
->ops
->sendpage_locked(sk
, page
, offset
, size
,
3390 return sock_no_sendpage_locked(sk
, page
, offset
, size
, flags
);
3392 EXPORT_SYMBOL(kernel_sendpage_locked
);
3394 int kernel_sock_ioctl(struct socket
*sock
, int cmd
, unsigned long arg
)
3396 mm_segment_t oldfs
= get_fs();
3400 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
3405 EXPORT_SYMBOL(kernel_sock_ioctl
);
3407 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
)
3409 return sock
->ops
->shutdown(sock
, how
);
3411 EXPORT_SYMBOL(kernel_sock_shutdown
);
3413 /* This routine returns the IP overhead imposed by a socket i.e.
3414 * the length of the underlying IP header, depending on whether
3415 * this is an IPv4 or IPv6 socket and the length from IP options turned
3416 * on at the socket. Assumes that the caller has a lock on the socket.
3418 u32
kernel_sock_ip_overhead(struct sock
*sk
)
3420 struct inet_sock
*inet
;
3421 struct ip_options_rcu
*opt
;
3423 #if IS_ENABLED(CONFIG_IPV6)
3424 struct ipv6_pinfo
*np
;
3425 struct ipv6_txoptions
*optv6
= NULL
;
3426 #endif /* IS_ENABLED(CONFIG_IPV6) */
3431 switch (sk
->sk_family
) {
3434 overhead
+= sizeof(struct iphdr
);
3435 opt
= rcu_dereference_protected(inet
->inet_opt
,
3436 sock_owned_by_user(sk
));
3438 overhead
+= opt
->opt
.optlen
;
3440 #if IS_ENABLED(CONFIG_IPV6)
3443 overhead
+= sizeof(struct ipv6hdr
);
3445 optv6
= rcu_dereference_protected(np
->opt
,
3446 sock_owned_by_user(sk
));
3448 overhead
+= (optv6
->opt_flen
+ optv6
->opt_nflen
);
3450 #endif /* IS_ENABLED(CONFIG_IPV6) */
3451 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3455 EXPORT_SYMBOL(kernel_sock_ip_overhead
);