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>
92 #include <linux/nospec.h>
94 #include <linux/uaccess.h>
95 #include <asm/unistd.h>
97 #include <net/compat.h>
99 #include <net/cls_cgroup.h>
101 #include <net/sock.h>
102 #include <linux/netfilter.h>
104 #include <linux/if_tun.h>
105 #include <linux/ipv6_route.h>
106 #include <linux/route.h>
107 #include <linux/sockios.h>
108 #include <linux/atalk.h>
109 #include <net/busy_poll.h>
110 #include <linux/errqueue.h>
112 #ifdef CONFIG_NET_RX_BUSY_POLL
113 unsigned int sysctl_net_busy_read __read_mostly
;
114 unsigned int sysctl_net_busy_poll __read_mostly
;
117 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
);
118 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
);
119 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
);
121 static int sock_close(struct inode
*inode
, struct file
*file
);
122 static unsigned int sock_poll(struct file
*file
,
123 struct poll_table_struct
*wait
);
124 static long sock_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
);
126 static long compat_sock_ioctl(struct file
*file
,
127 unsigned int cmd
, unsigned long arg
);
129 static int sock_fasync(int fd
, struct file
*filp
, int on
);
130 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
131 int offset
, size_t size
, loff_t
*ppos
, int more
);
132 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
133 struct pipe_inode_info
*pipe
, size_t len
,
137 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
138 * in the operation structures but are done directly via the socketcall() multiplexor.
141 static const struct file_operations socket_file_ops
= {
142 .owner
= THIS_MODULE
,
144 .read_iter
= sock_read_iter
,
145 .write_iter
= sock_write_iter
,
147 .unlocked_ioctl
= sock_ioctl
,
149 .compat_ioctl
= compat_sock_ioctl
,
152 .release
= sock_close
,
153 .fasync
= sock_fasync
,
154 .sendpage
= sock_sendpage
,
155 .splice_write
= generic_splice_sendpage
,
156 .splice_read
= sock_splice_read
,
160 * The protocol list. Each protocol is registered in here.
163 static DEFINE_SPINLOCK(net_family_lock
);
164 static const struct net_proto_family __rcu
*net_families
[NPROTO
] __read_mostly
;
167 * Statistics counters of the socket lists
170 static DEFINE_PER_CPU(int, sockets_in_use
);
174 * Move socket addresses back and forth across the kernel/user
175 * divide and look after the messy bits.
179 * move_addr_to_kernel - copy a socket address into kernel space
180 * @uaddr: Address in user space
181 * @kaddr: Address in kernel space
182 * @ulen: Length in user space
184 * The address is copied into kernel space. If the provided address is
185 * too long an error code of -EINVAL is returned. If the copy gives
186 * invalid addresses -EFAULT is returned. On a success 0 is returned.
189 int move_addr_to_kernel(void __user
*uaddr
, int ulen
, struct sockaddr_storage
*kaddr
)
191 if (ulen
< 0 || ulen
> sizeof(struct sockaddr_storage
))
195 if (copy_from_user(kaddr
, uaddr
, ulen
))
197 return audit_sockaddr(ulen
, kaddr
);
201 * move_addr_to_user - copy an address to user space
202 * @kaddr: kernel space address
203 * @klen: length of address in kernel
204 * @uaddr: user space address
205 * @ulen: pointer to user length field
207 * The value pointed to by ulen on entry is the buffer length available.
208 * This is overwritten with the buffer space used. -EINVAL is returned
209 * if an overlong buffer is specified or a negative buffer size. -EFAULT
210 * is returned if either the buffer or the length field are not
212 * After copying the data up to the limit the user specifies, the true
213 * length of the data is written over the length limit the user
214 * specified. Zero is returned for a success.
217 static int move_addr_to_user(struct sockaddr_storage
*kaddr
, int klen
,
218 void __user
*uaddr
, int __user
*ulen
)
223 BUG_ON(klen
> sizeof(struct sockaddr_storage
));
224 err
= get_user(len
, ulen
);
232 if (audit_sockaddr(klen
, kaddr
))
234 if (copy_to_user(uaddr
, kaddr
, len
))
238 * "fromlen shall refer to the value before truncation.."
241 return __put_user(klen
, ulen
);
244 static struct kmem_cache
*sock_inode_cachep __read_mostly
;
246 static struct inode
*sock_alloc_inode(struct super_block
*sb
)
248 struct socket_alloc
*ei
;
249 struct socket_wq
*wq
;
251 ei
= kmem_cache_alloc(sock_inode_cachep
, GFP_KERNEL
);
254 wq
= kmalloc(sizeof(*wq
), GFP_KERNEL
);
256 kmem_cache_free(sock_inode_cachep
, ei
);
259 init_waitqueue_head(&wq
->wait
);
260 wq
->fasync_list
= NULL
;
262 RCU_INIT_POINTER(ei
->socket
.wq
, wq
);
264 ei
->socket
.state
= SS_UNCONNECTED
;
265 ei
->socket
.flags
= 0;
266 ei
->socket
.ops
= NULL
;
267 ei
->socket
.sk
= NULL
;
268 ei
->socket
.file
= NULL
;
270 return &ei
->vfs_inode
;
273 static void sock_destroy_inode(struct inode
*inode
)
275 struct socket_alloc
*ei
;
276 struct socket_wq
*wq
;
278 ei
= container_of(inode
, struct socket_alloc
, vfs_inode
);
279 wq
= rcu_dereference_protected(ei
->socket
.wq
, 1);
281 kmem_cache_free(sock_inode_cachep
, ei
);
284 static void init_once(void *foo
)
286 struct socket_alloc
*ei
= (struct socket_alloc
*)foo
;
288 inode_init_once(&ei
->vfs_inode
);
291 static void init_inodecache(void)
293 sock_inode_cachep
= kmem_cache_create("sock_inode_cache",
294 sizeof(struct socket_alloc
),
296 (SLAB_HWCACHE_ALIGN
|
297 SLAB_RECLAIM_ACCOUNT
|
298 SLAB_MEM_SPREAD
| SLAB_ACCOUNT
),
300 BUG_ON(sock_inode_cachep
== NULL
);
303 static const struct super_operations sockfs_ops
= {
304 .alloc_inode
= sock_alloc_inode
,
305 .destroy_inode
= sock_destroy_inode
,
306 .statfs
= simple_statfs
,
310 * sockfs_dname() is called from d_path().
312 static char *sockfs_dname(struct dentry
*dentry
, char *buffer
, int buflen
)
314 return dynamic_dname(dentry
, buffer
, buflen
, "socket:[%lu]",
315 d_inode(dentry
)->i_ino
);
318 static const struct dentry_operations sockfs_dentry_operations
= {
319 .d_dname
= sockfs_dname
,
322 static int sockfs_xattr_get(const struct xattr_handler
*handler
,
323 struct dentry
*dentry
, struct inode
*inode
,
324 const char *suffix
, void *value
, size_t size
)
327 if (dentry
->d_name
.len
+ 1 > size
)
329 memcpy(value
, dentry
->d_name
.name
, dentry
->d_name
.len
+ 1);
331 return dentry
->d_name
.len
+ 1;
334 #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
335 #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
336 #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
338 static const struct xattr_handler sockfs_xattr_handler
= {
339 .name
= XATTR_NAME_SOCKPROTONAME
,
340 .get
= sockfs_xattr_get
,
343 static int sockfs_security_xattr_set(const struct xattr_handler
*handler
,
344 struct dentry
*dentry
, struct inode
*inode
,
345 const char *suffix
, const void *value
,
346 size_t size
, int flags
)
348 /* Handled by LSM. */
352 static const struct xattr_handler sockfs_security_xattr_handler
= {
353 .prefix
= XATTR_SECURITY_PREFIX
,
354 .set
= sockfs_security_xattr_set
,
357 static const struct xattr_handler
*sockfs_xattr_handlers
[] = {
358 &sockfs_xattr_handler
,
359 &sockfs_security_xattr_handler
,
363 static struct dentry
*sockfs_mount(struct file_system_type
*fs_type
,
364 int flags
, const char *dev_name
, void *data
)
366 return mount_pseudo_xattr(fs_type
, "socket:", &sockfs_ops
,
367 sockfs_xattr_handlers
,
368 &sockfs_dentry_operations
, SOCKFS_MAGIC
);
371 static struct vfsmount
*sock_mnt __read_mostly
;
373 static struct file_system_type sock_fs_type
= {
375 .mount
= sockfs_mount
,
376 .kill_sb
= kill_anon_super
,
380 * Obtains the first available file descriptor and sets it up for use.
382 * These functions create file structures and maps them to fd space
383 * of the current process. On success it returns file descriptor
384 * and file struct implicitly stored in sock->file.
385 * Note that another thread may close file descriptor before we return
386 * from this function. We use the fact that now we do not refer
387 * to socket after mapping. If one day we will need it, this
388 * function will increment ref. count on file by 1.
390 * In any case returned fd MAY BE not valid!
391 * This race condition is unavoidable
392 * with shared fd spaces, we cannot solve it inside kernel,
393 * but we take care of internal coherence yet.
396 struct file
*sock_alloc_file(struct socket
*sock
, int flags
, const char *dname
)
398 struct qstr name
= { .name
= "" };
404 name
.len
= strlen(name
.name
);
405 } else if (sock
->sk
) {
406 name
.name
= sock
->sk
->sk_prot_creator
->name
;
407 name
.len
= strlen(name
.name
);
409 path
.dentry
= d_alloc_pseudo(sock_mnt
->mnt_sb
, &name
);
410 if (unlikely(!path
.dentry
))
411 return ERR_PTR(-ENOMEM
);
412 path
.mnt
= mntget(sock_mnt
);
414 d_instantiate(path
.dentry
, SOCK_INODE(sock
));
416 file
= alloc_file(&path
, FMODE_READ
| FMODE_WRITE
,
419 /* drop dentry, keep inode */
420 ihold(d_inode(path
.dentry
));
426 file
->f_flags
= O_RDWR
| (flags
& O_NONBLOCK
);
427 file
->private_data
= sock
;
430 EXPORT_SYMBOL(sock_alloc_file
);
432 static int sock_map_fd(struct socket
*sock
, int flags
)
434 struct file
*newfile
;
435 int fd
= get_unused_fd_flags(flags
);
436 if (unlikely(fd
< 0))
439 newfile
= sock_alloc_file(sock
, flags
, NULL
);
440 if (likely(!IS_ERR(newfile
))) {
441 fd_install(fd
, newfile
);
446 return PTR_ERR(newfile
);
449 struct socket
*sock_from_file(struct file
*file
, int *err
)
451 if (file
->f_op
== &socket_file_ops
)
452 return file
->private_data
; /* set in sock_map_fd */
457 EXPORT_SYMBOL(sock_from_file
);
460 * sockfd_lookup - Go from a file number to its socket slot
462 * @err: pointer to an error code return
464 * The file handle passed in is locked and the socket it is bound
465 * to is returned. If an error occurs the err pointer is overwritten
466 * with a negative errno code and NULL is returned. The function checks
467 * for both invalid handles and passing a handle which is not a socket.
469 * On a success the socket object pointer is returned.
472 struct socket
*sockfd_lookup(int fd
, int *err
)
483 sock
= sock_from_file(file
, err
);
488 EXPORT_SYMBOL(sockfd_lookup
);
490 static struct socket
*sockfd_lookup_light(int fd
, int *err
, int *fput_needed
)
492 struct fd f
= fdget(fd
);
497 sock
= sock_from_file(f
.file
, err
);
499 *fput_needed
= f
.flags
;
507 static ssize_t
sockfs_listxattr(struct dentry
*dentry
, char *buffer
,
513 len
= security_inode_listsecurity(d_inode(dentry
), buffer
, size
);
523 len
= (XATTR_NAME_SOCKPROTONAME_LEN
+ 1);
528 memcpy(buffer
, XATTR_NAME_SOCKPROTONAME
, len
);
535 static int sockfs_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
537 int err
= simple_setattr(dentry
, iattr
);
539 if (!err
&& (iattr
->ia_valid
& ATTR_UID
)) {
540 struct socket
*sock
= SOCKET_I(d_inode(dentry
));
543 sock
->sk
->sk_uid
= iattr
->ia_uid
;
551 static const struct inode_operations sockfs_inode_ops
= {
552 .listxattr
= sockfs_listxattr
,
553 .setattr
= sockfs_setattr
,
557 * sock_alloc - allocate a socket
559 * Allocate a new inode and socket object. The two are bound together
560 * and initialised. The socket is then returned. If we are out of inodes
564 struct socket
*sock_alloc(void)
569 inode
= new_inode_pseudo(sock_mnt
->mnt_sb
);
573 sock
= SOCKET_I(inode
);
575 inode
->i_ino
= get_next_ino();
576 inode
->i_mode
= S_IFSOCK
| S_IRWXUGO
;
577 inode
->i_uid
= current_fsuid();
578 inode
->i_gid
= current_fsgid();
579 inode
->i_op
= &sockfs_inode_ops
;
581 this_cpu_add(sockets_in_use
, 1);
584 EXPORT_SYMBOL(sock_alloc
);
587 * sock_release - close a socket
588 * @sock: socket to close
590 * The socket is released from the protocol stack if it has a release
591 * callback, and the inode is then released if the socket is bound to
592 * an inode not a file.
595 static void __sock_release(struct socket
*sock
, struct inode
*inode
)
598 struct module
*owner
= sock
->ops
->owner
;
602 sock
->ops
->release(sock
);
609 if (rcu_dereference_protected(sock
->wq
, 1)->fasync_list
)
610 pr_err("%s: fasync list not empty!\n", __func__
);
612 this_cpu_sub(sockets_in_use
, 1);
614 iput(SOCK_INODE(sock
));
620 void sock_release(struct socket
*sock
)
622 __sock_release(sock
, NULL
);
624 EXPORT_SYMBOL(sock_release
);
626 void __sock_tx_timestamp(__u16 tsflags
, __u8
*tx_flags
)
628 u8 flags
= *tx_flags
;
630 if (tsflags
& SOF_TIMESTAMPING_TX_HARDWARE
)
631 flags
|= SKBTX_HW_TSTAMP
;
633 if (tsflags
& SOF_TIMESTAMPING_TX_SOFTWARE
)
634 flags
|= SKBTX_SW_TSTAMP
;
636 if (tsflags
& SOF_TIMESTAMPING_TX_SCHED
)
637 flags
|= SKBTX_SCHED_TSTAMP
;
641 EXPORT_SYMBOL(__sock_tx_timestamp
);
643 static inline int sock_sendmsg_nosec(struct socket
*sock
, struct msghdr
*msg
)
645 int ret
= sock
->ops
->sendmsg(sock
, msg
, msg_data_left(msg
));
646 BUG_ON(ret
== -EIOCBQUEUED
);
650 int sock_sendmsg(struct socket
*sock
, struct msghdr
*msg
)
652 int err
= security_socket_sendmsg(sock
, msg
,
655 return err
?: sock_sendmsg_nosec(sock
, msg
);
657 EXPORT_SYMBOL(sock_sendmsg
);
659 int kernel_sendmsg(struct socket
*sock
, struct msghdr
*msg
,
660 struct kvec
*vec
, size_t num
, size_t size
)
662 iov_iter_kvec(&msg
->msg_iter
, WRITE
| ITER_KVEC
, vec
, num
, size
);
663 return sock_sendmsg(sock
, msg
);
665 EXPORT_SYMBOL(kernel_sendmsg
);
667 int kernel_sendmsg_locked(struct sock
*sk
, struct msghdr
*msg
,
668 struct kvec
*vec
, size_t num
, size_t size
)
670 struct socket
*sock
= sk
->sk_socket
;
672 if (!sock
->ops
->sendmsg_locked
)
673 return sock_no_sendmsg_locked(sk
, msg
, size
);
675 iov_iter_kvec(&msg
->msg_iter
, WRITE
| ITER_KVEC
, vec
, num
, size
);
677 return sock
->ops
->sendmsg_locked(sk
, msg
, msg_data_left(msg
));
679 EXPORT_SYMBOL(kernel_sendmsg_locked
);
681 static bool skb_is_err_queue(const struct sk_buff
*skb
)
683 /* pkt_type of skbs enqueued on the error queue are set to
684 * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
685 * in recvmsg, since skbs received on a local socket will never
686 * have a pkt_type of PACKET_OUTGOING.
688 return skb
->pkt_type
== PACKET_OUTGOING
;
691 /* On transmit, software and hardware timestamps are returned independently.
692 * As the two skb clones share the hardware timestamp, which may be updated
693 * before the software timestamp is received, a hardware TX timestamp may be
694 * returned only if there is no software TX timestamp. Ignore false software
695 * timestamps, which may be made in the __sock_recv_timestamp() call when the
696 * option SO_TIMESTAMP(NS) is enabled on the socket, even when the skb has a
697 * hardware timestamp.
699 static bool skb_is_swtx_tstamp(const struct sk_buff
*skb
, int false_tstamp
)
701 return skb
->tstamp
&& !false_tstamp
&& skb_is_err_queue(skb
);
704 static void put_ts_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
706 struct scm_ts_pktinfo ts_pktinfo
;
707 struct net_device
*orig_dev
;
709 if (!skb_mac_header_was_set(skb
))
712 memset(&ts_pktinfo
, 0, sizeof(ts_pktinfo
));
715 orig_dev
= dev_get_by_napi_id(skb_napi_id(skb
));
717 ts_pktinfo
.if_index
= orig_dev
->ifindex
;
720 ts_pktinfo
.pkt_length
= skb
->len
- skb_mac_offset(skb
);
721 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_PKTINFO
,
722 sizeof(ts_pktinfo
), &ts_pktinfo
);
726 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
728 void __sock_recv_timestamp(struct msghdr
*msg
, struct sock
*sk
,
731 int need_software_tstamp
= sock_flag(sk
, SOCK_RCVTSTAMP
);
732 struct scm_timestamping tss
;
733 int empty
= 1, false_tstamp
= 0;
734 struct skb_shared_hwtstamps
*shhwtstamps
=
737 /* Race occurred between timestamp enabling and packet
738 receiving. Fill in the current time for now. */
739 if (need_software_tstamp
&& skb
->tstamp
== 0) {
740 __net_timestamp(skb
);
744 if (need_software_tstamp
) {
745 if (!sock_flag(sk
, SOCK_RCVTSTAMPNS
)) {
747 skb_get_timestamp(skb
, &tv
);
748 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMP
,
752 skb_get_timestampns(skb
, &ts
);
753 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPNS
,
758 memset(&tss
, 0, sizeof(tss
));
759 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_SOFTWARE
) &&
760 ktime_to_timespec_cond(skb
->tstamp
, tss
.ts
+ 0))
763 (sk
->sk_tsflags
& SOF_TIMESTAMPING_RAW_HARDWARE
) &&
764 !skb_is_swtx_tstamp(skb
, false_tstamp
) &&
765 ktime_to_timespec_cond(shhwtstamps
->hwtstamp
, tss
.ts
+ 2)) {
767 if ((sk
->sk_tsflags
& SOF_TIMESTAMPING_OPT_PKTINFO
) &&
768 !skb_is_err_queue(skb
))
769 put_ts_pktinfo(msg
, skb
);
772 put_cmsg(msg
, SOL_SOCKET
,
773 SCM_TIMESTAMPING
, sizeof(tss
), &tss
);
775 if (skb_is_err_queue(skb
) && skb
->len
&&
776 SKB_EXT_ERR(skb
)->opt_stats
)
777 put_cmsg(msg
, SOL_SOCKET
, SCM_TIMESTAMPING_OPT_STATS
,
778 skb
->len
, skb
->data
);
781 EXPORT_SYMBOL_GPL(__sock_recv_timestamp
);
783 void __sock_recv_wifi_status(struct msghdr
*msg
, struct sock
*sk
,
788 if (!sock_flag(sk
, SOCK_WIFI_STATUS
))
790 if (!skb
->wifi_acked_valid
)
793 ack
= skb
->wifi_acked
;
795 put_cmsg(msg
, SOL_SOCKET
, SCM_WIFI_STATUS
, sizeof(ack
), &ack
);
797 EXPORT_SYMBOL_GPL(__sock_recv_wifi_status
);
799 static inline void sock_recv_drops(struct msghdr
*msg
, struct sock
*sk
,
802 if (sock_flag(sk
, SOCK_RXQ_OVFL
) && skb
&& SOCK_SKB_CB(skb
)->dropcount
)
803 put_cmsg(msg
, SOL_SOCKET
, SO_RXQ_OVFL
,
804 sizeof(__u32
), &SOCK_SKB_CB(skb
)->dropcount
);
807 void __sock_recv_ts_and_drops(struct msghdr
*msg
, struct sock
*sk
,
810 sock_recv_timestamp(msg
, sk
, skb
);
811 sock_recv_drops(msg
, sk
, skb
);
813 EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops
);
815 static inline int sock_recvmsg_nosec(struct socket
*sock
, struct msghdr
*msg
,
818 return sock
->ops
->recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
821 int sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int flags
)
823 int err
= security_socket_recvmsg(sock
, msg
, msg_data_left(msg
), flags
);
825 return err
?: sock_recvmsg_nosec(sock
, msg
, flags
);
827 EXPORT_SYMBOL(sock_recvmsg
);
830 * kernel_recvmsg - Receive a message from a socket (kernel space)
831 * @sock: The socket to receive the message from
832 * @msg: Received message
833 * @vec: Input s/g array for message data
834 * @num: Size of input s/g array
835 * @size: Number of bytes to read
836 * @flags: Message flags (MSG_DONTWAIT, etc...)
838 * On return the msg structure contains the scatter/gather array passed in the
839 * vec argument. The array is modified so that it consists of the unfilled
840 * portion of the original array.
842 * The returned value is the total number of bytes received, or an error.
844 int kernel_recvmsg(struct socket
*sock
, struct msghdr
*msg
,
845 struct kvec
*vec
, size_t num
, size_t size
, int flags
)
847 mm_segment_t oldfs
= get_fs();
850 iov_iter_kvec(&msg
->msg_iter
, READ
| ITER_KVEC
, vec
, num
, size
);
852 result
= sock_recvmsg(sock
, msg
, flags
);
856 EXPORT_SYMBOL(kernel_recvmsg
);
858 static ssize_t
sock_sendpage(struct file
*file
, struct page
*page
,
859 int offset
, size_t size
, loff_t
*ppos
, int more
)
864 sock
= file
->private_data
;
866 flags
= (file
->f_flags
& O_NONBLOCK
) ? MSG_DONTWAIT
: 0;
867 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
870 return kernel_sendpage(sock
, page
, offset
, size
, flags
);
873 static ssize_t
sock_splice_read(struct file
*file
, loff_t
*ppos
,
874 struct pipe_inode_info
*pipe
, size_t len
,
877 struct socket
*sock
= file
->private_data
;
879 if (unlikely(!sock
->ops
->splice_read
))
882 return sock
->ops
->splice_read(sock
, ppos
, pipe
, len
, flags
);
885 static ssize_t
sock_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
887 struct file
*file
= iocb
->ki_filp
;
888 struct socket
*sock
= file
->private_data
;
889 struct msghdr msg
= {.msg_iter
= *to
,
893 if (file
->f_flags
& O_NONBLOCK
)
894 msg
.msg_flags
= MSG_DONTWAIT
;
896 if (iocb
->ki_pos
!= 0)
899 if (!iov_iter_count(to
)) /* Match SYS5 behaviour */
902 res
= sock_recvmsg(sock
, &msg
, msg
.msg_flags
);
907 static ssize_t
sock_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
909 struct file
*file
= iocb
->ki_filp
;
910 struct socket
*sock
= file
->private_data
;
911 struct msghdr msg
= {.msg_iter
= *from
,
915 if (iocb
->ki_pos
!= 0)
918 if (file
->f_flags
& O_NONBLOCK
)
919 msg
.msg_flags
= MSG_DONTWAIT
;
921 if (sock
->type
== SOCK_SEQPACKET
)
922 msg
.msg_flags
|= MSG_EOR
;
924 res
= sock_sendmsg(sock
, &msg
);
925 *from
= msg
.msg_iter
;
930 * Atomic setting of ioctl hooks to avoid race
931 * with module unload.
934 static DEFINE_MUTEX(br_ioctl_mutex
);
935 static int (*br_ioctl_hook
) (struct net
*, unsigned int cmd
, void __user
*arg
);
937 void brioctl_set(int (*hook
) (struct net
*, unsigned int, void __user
*))
939 mutex_lock(&br_ioctl_mutex
);
940 br_ioctl_hook
= hook
;
941 mutex_unlock(&br_ioctl_mutex
);
943 EXPORT_SYMBOL(brioctl_set
);
945 static DEFINE_MUTEX(vlan_ioctl_mutex
);
946 static int (*vlan_ioctl_hook
) (struct net
*, void __user
*arg
);
948 void vlan_ioctl_set(int (*hook
) (struct net
*, void __user
*))
950 mutex_lock(&vlan_ioctl_mutex
);
951 vlan_ioctl_hook
= hook
;
952 mutex_unlock(&vlan_ioctl_mutex
);
954 EXPORT_SYMBOL(vlan_ioctl_set
);
956 static DEFINE_MUTEX(dlci_ioctl_mutex
);
957 static int (*dlci_ioctl_hook
) (unsigned int, void __user
*);
959 void dlci_ioctl_set(int (*hook
) (unsigned int, void __user
*))
961 mutex_lock(&dlci_ioctl_mutex
);
962 dlci_ioctl_hook
= hook
;
963 mutex_unlock(&dlci_ioctl_mutex
);
965 EXPORT_SYMBOL(dlci_ioctl_set
);
967 static long sock_do_ioctl(struct net
*net
, struct socket
*sock
,
968 unsigned int cmd
, unsigned long arg
)
971 void __user
*argp
= (void __user
*)arg
;
973 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
976 * If this ioctl is unknown try to hand it down
979 if (err
== -ENOIOCTLCMD
)
980 err
= dev_ioctl(net
, cmd
, argp
);
986 * With an ioctl, arg may well be a user mode pointer, but we don't know
987 * what to do with it - that's up to the protocol still.
990 static struct ns_common
*get_net_ns(struct ns_common
*ns
)
992 return &get_net(container_of(ns
, struct net
, ns
))->ns
;
995 static long sock_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
999 void __user
*argp
= (void __user
*)arg
;
1003 sock
= file
->private_data
;
1006 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15)) {
1007 err
= dev_ioctl(net
, cmd
, argp
);
1009 #ifdef CONFIG_WEXT_CORE
1010 if (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
) {
1011 err
= dev_ioctl(net
, cmd
, argp
);
1018 if (get_user(pid
, (int __user
*)argp
))
1020 err
= f_setown(sock
->file
, pid
, 1);
1024 err
= put_user(f_getown(sock
->file
),
1025 (int __user
*)argp
);
1033 request_module("bridge");
1035 mutex_lock(&br_ioctl_mutex
);
1037 err
= br_ioctl_hook(net
, cmd
, argp
);
1038 mutex_unlock(&br_ioctl_mutex
);
1043 if (!vlan_ioctl_hook
)
1044 request_module("8021q");
1046 mutex_lock(&vlan_ioctl_mutex
);
1047 if (vlan_ioctl_hook
)
1048 err
= vlan_ioctl_hook(net
, argp
);
1049 mutex_unlock(&vlan_ioctl_mutex
);
1054 if (!dlci_ioctl_hook
)
1055 request_module("dlci");
1057 mutex_lock(&dlci_ioctl_mutex
);
1058 if (dlci_ioctl_hook
)
1059 err
= dlci_ioctl_hook(cmd
, argp
);
1060 mutex_unlock(&dlci_ioctl_mutex
);
1064 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1067 err
= open_related_ns(&net
->ns
, get_net_ns
);
1070 err
= sock_do_ioctl(net
, sock
, cmd
, arg
);
1076 int sock_create_lite(int family
, int type
, int protocol
, struct socket
**res
)
1079 struct socket
*sock
= NULL
;
1081 err
= security_socket_create(family
, type
, protocol
, 1);
1085 sock
= sock_alloc();
1092 err
= security_socket_post_create(sock
, family
, type
, protocol
, 1);
1104 EXPORT_SYMBOL(sock_create_lite
);
1106 /* No kernel lock held - perfect */
1107 static unsigned int sock_poll(struct file
*file
, poll_table
*wait
)
1109 unsigned int busy_flag
= 0;
1110 struct socket
*sock
;
1113 * We can't return errors to poll, so it's either yes or no.
1115 sock
= file
->private_data
;
1117 if (sk_can_busy_loop(sock
->sk
)) {
1118 /* this socket can poll_ll so tell the system call */
1119 busy_flag
= POLL_BUSY_LOOP
;
1121 /* once, only if requested by syscall */
1122 if (wait
&& (wait
->_key
& POLL_BUSY_LOOP
))
1123 sk_busy_loop(sock
->sk
, 1);
1126 return busy_flag
| sock
->ops
->poll(file
, sock
, wait
);
1129 static int sock_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1131 struct socket
*sock
= file
->private_data
;
1133 return sock
->ops
->mmap(file
, sock
, vma
);
1136 static int sock_close(struct inode
*inode
, struct file
*filp
)
1138 __sock_release(SOCKET_I(inode
), inode
);
1143 * Update the socket async list
1145 * Fasync_list locking strategy.
1147 * 1. fasync_list is modified only under process context socket lock
1148 * i.e. under semaphore.
1149 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1150 * or under socket lock
1153 static int sock_fasync(int fd
, struct file
*filp
, int on
)
1155 struct socket
*sock
= filp
->private_data
;
1156 struct sock
*sk
= sock
->sk
;
1157 struct socket_wq
*wq
;
1163 wq
= rcu_dereference_protected(sock
->wq
, lockdep_sock_is_held(sk
));
1164 fasync_helper(fd
, filp
, on
, &wq
->fasync_list
);
1166 if (!wq
->fasync_list
)
1167 sock_reset_flag(sk
, SOCK_FASYNC
);
1169 sock_set_flag(sk
, SOCK_FASYNC
);
1175 /* This function may be called only under rcu_lock */
1177 int sock_wake_async(struct socket_wq
*wq
, int how
, int band
)
1179 if (!wq
|| !wq
->fasync_list
)
1183 case SOCK_WAKE_WAITD
:
1184 if (test_bit(SOCKWQ_ASYNC_WAITDATA
, &wq
->flags
))
1187 case SOCK_WAKE_SPACE
:
1188 if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
))
1193 kill_fasync(&wq
->fasync_list
, SIGIO
, band
);
1196 kill_fasync(&wq
->fasync_list
, SIGURG
, band
);
1201 EXPORT_SYMBOL(sock_wake_async
);
1203 int __sock_create(struct net
*net
, int family
, int type
, int protocol
,
1204 struct socket
**res
, int kern
)
1207 struct socket
*sock
;
1208 const struct net_proto_family
*pf
;
1211 * Check protocol is in range
1213 if (family
< 0 || family
>= NPROTO
)
1214 return -EAFNOSUPPORT
;
1215 if (type
< 0 || type
>= SOCK_MAX
)
1220 This uglymoron is moved from INET layer to here to avoid
1221 deadlock in module load.
1223 if (family
== PF_INET
&& type
== SOCK_PACKET
) {
1224 pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1229 err
= security_socket_create(family
, type
, protocol
, kern
);
1234 * Allocate the socket and allow the family to set things up. if
1235 * the protocol is 0, the family is instructed to select an appropriate
1238 sock
= sock_alloc();
1240 net_warn_ratelimited("socket: no more sockets\n");
1241 return -ENFILE
; /* Not exactly a match, but its the
1242 closest posix thing */
1247 #ifdef CONFIG_MODULES
1248 /* Attempt to load a protocol module if the find failed.
1250 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1251 * requested real, full-featured networking support upon configuration.
1252 * Otherwise module support will break!
1254 if (rcu_access_pointer(net_families
[family
]) == NULL
)
1255 request_module("net-pf-%d", family
);
1259 pf
= rcu_dereference(net_families
[family
]);
1260 err
= -EAFNOSUPPORT
;
1265 * We will call the ->create function, that possibly is in a loadable
1266 * module, so we have to bump that loadable module refcnt first.
1268 if (!try_module_get(pf
->owner
))
1271 /* Now protected by module ref count */
1274 err
= pf
->create(net
, sock
, protocol
, kern
);
1276 goto out_module_put
;
1279 * Now to bump the refcnt of the [loadable] module that owns this
1280 * socket at sock_release time we decrement its refcnt.
1282 if (!try_module_get(sock
->ops
->owner
))
1283 goto out_module_busy
;
1286 * Now that we're done with the ->create function, the [loadable]
1287 * module can have its refcnt decremented
1289 module_put(pf
->owner
);
1290 err
= security_socket_post_create(sock
, family
, type
, protocol
, kern
);
1292 goto out_sock_release
;
1298 err
= -EAFNOSUPPORT
;
1301 module_put(pf
->owner
);
1308 goto out_sock_release
;
1310 EXPORT_SYMBOL(__sock_create
);
1312 int sock_create(int family
, int type
, int protocol
, struct socket
**res
)
1314 return __sock_create(current
->nsproxy
->net_ns
, family
, type
, protocol
, res
, 0);
1316 EXPORT_SYMBOL(sock_create
);
1318 int sock_create_kern(struct net
*net
, int family
, int type
, int protocol
, struct socket
**res
)
1320 return __sock_create(net
, family
, type
, protocol
, res
, 1);
1322 EXPORT_SYMBOL(sock_create_kern
);
1324 SYSCALL_DEFINE3(socket
, int, family
, int, type
, int, protocol
)
1327 struct socket
*sock
;
1330 /* Check the SOCK_* constants for consistency. */
1331 BUILD_BUG_ON(SOCK_CLOEXEC
!= O_CLOEXEC
);
1332 BUILD_BUG_ON((SOCK_MAX
| SOCK_TYPE_MASK
) != SOCK_TYPE_MASK
);
1333 BUILD_BUG_ON(SOCK_CLOEXEC
& SOCK_TYPE_MASK
);
1334 BUILD_BUG_ON(SOCK_NONBLOCK
& SOCK_TYPE_MASK
);
1336 flags
= type
& ~SOCK_TYPE_MASK
;
1337 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1339 type
&= SOCK_TYPE_MASK
;
1341 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1342 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1344 retval
= sock_create(family
, type
, protocol
, &sock
);
1348 retval
= sock_map_fd(sock
, flags
& (O_CLOEXEC
| O_NONBLOCK
));
1353 /* It may be already another descriptor 8) Not kernel problem. */
1362 * Create a pair of connected sockets.
1365 SYSCALL_DEFINE4(socketpair
, int, family
, int, type
, int, protocol
,
1366 int __user
*, usockvec
)
1368 struct socket
*sock1
, *sock2
;
1370 struct file
*newfile1
, *newfile2
;
1373 flags
= type
& ~SOCK_TYPE_MASK
;
1374 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1376 type
&= SOCK_TYPE_MASK
;
1378 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1379 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1382 * Obtain the first socket and check if the underlying protocol
1383 * supports the socketpair call.
1386 err
= sock_create(family
, type
, protocol
, &sock1
);
1390 err
= sock_create(family
, type
, protocol
, &sock2
);
1394 err
= sock1
->ops
->socketpair(sock1
, sock2
);
1396 goto out_release_both
;
1398 fd1
= get_unused_fd_flags(flags
);
1399 if (unlikely(fd1
< 0)) {
1401 goto out_release_both
;
1404 fd2
= get_unused_fd_flags(flags
);
1405 if (unlikely(fd2
< 0)) {
1407 goto out_put_unused_1
;
1410 newfile1
= sock_alloc_file(sock1
, flags
, NULL
);
1411 if (IS_ERR(newfile1
)) {
1412 err
= PTR_ERR(newfile1
);
1413 goto out_put_unused_both
;
1416 newfile2
= sock_alloc_file(sock2
, flags
, NULL
);
1417 if (IS_ERR(newfile2
)) {
1418 err
= PTR_ERR(newfile2
);
1422 err
= put_user(fd1
, &usockvec
[0]);
1426 err
= put_user(fd2
, &usockvec
[1]);
1430 audit_fd_pair(fd1
, fd2
);
1432 fd_install(fd1
, newfile1
);
1433 fd_install(fd2
, newfile2
);
1434 /* fd1 and fd2 may be already another descriptors.
1435 * Not kernel problem.
1451 sock_release(sock2
);
1454 out_put_unused_both
:
1459 sock_release(sock2
);
1461 sock_release(sock1
);
1467 * Bind a name to a socket. Nothing much to do here since it's
1468 * the protocol's responsibility to handle the local address.
1470 * We move the socket address to kernel space before we call
1471 * the protocol layer (having also checked the address is ok).
1474 SYSCALL_DEFINE3(bind
, int, fd
, struct sockaddr __user
*, umyaddr
, int, addrlen
)
1476 struct socket
*sock
;
1477 struct sockaddr_storage address
;
1478 int err
, fput_needed
;
1480 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1482 err
= move_addr_to_kernel(umyaddr
, addrlen
, &address
);
1484 err
= security_socket_bind(sock
,
1485 (struct sockaddr
*)&address
,
1488 err
= sock
->ops
->bind(sock
,
1492 fput_light(sock
->file
, fput_needed
);
1498 * Perform a listen. Basically, we allow the protocol to do anything
1499 * necessary for a listen, and if that works, we mark the socket as
1500 * ready for listening.
1503 SYSCALL_DEFINE2(listen
, int, fd
, int, backlog
)
1505 struct socket
*sock
;
1506 int err
, fput_needed
;
1509 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1511 somaxconn
= sock_net(sock
->sk
)->core
.sysctl_somaxconn
;
1512 if ((unsigned int)backlog
> somaxconn
)
1513 backlog
= somaxconn
;
1515 err
= security_socket_listen(sock
, backlog
);
1517 err
= sock
->ops
->listen(sock
, backlog
);
1519 fput_light(sock
->file
, fput_needed
);
1525 * For accept, we attempt to create a new socket, set up the link
1526 * with the client, wake up the client, then return the new
1527 * connected fd. We collect the address of the connector in kernel
1528 * space and move it to user at the very end. This is unclean because
1529 * we open the socket then return an error.
1531 * 1003.1g adds the ability to recvmsg() to query connection pending
1532 * status to recvmsg. We need to add that support in a way thats
1533 * clean when we restucture accept also.
1536 SYSCALL_DEFINE4(accept4
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1537 int __user
*, upeer_addrlen
, int, flags
)
1539 struct socket
*sock
, *newsock
;
1540 struct file
*newfile
;
1541 int err
, len
, newfd
, fput_needed
;
1542 struct sockaddr_storage address
;
1544 if (flags
& ~(SOCK_CLOEXEC
| SOCK_NONBLOCK
))
1547 if (SOCK_NONBLOCK
!= O_NONBLOCK
&& (flags
& SOCK_NONBLOCK
))
1548 flags
= (flags
& ~SOCK_NONBLOCK
) | O_NONBLOCK
;
1550 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1555 newsock
= sock_alloc();
1559 newsock
->type
= sock
->type
;
1560 newsock
->ops
= sock
->ops
;
1563 * We don't need try_module_get here, as the listening socket (sock)
1564 * has the protocol module (sock->ops->owner) held.
1566 __module_get(newsock
->ops
->owner
);
1568 newfd
= get_unused_fd_flags(flags
);
1569 if (unlikely(newfd
< 0)) {
1571 sock_release(newsock
);
1574 newfile
= sock_alloc_file(newsock
, flags
, sock
->sk
->sk_prot_creator
->name
);
1575 if (IS_ERR(newfile
)) {
1576 err
= PTR_ERR(newfile
);
1577 put_unused_fd(newfd
);
1578 sock_release(newsock
);
1582 err
= security_socket_accept(sock
, newsock
);
1586 err
= sock
->ops
->accept(sock
, newsock
, sock
->file
->f_flags
, false);
1590 if (upeer_sockaddr
) {
1591 if (newsock
->ops
->getname(newsock
, (struct sockaddr
*)&address
,
1593 err
= -ECONNABORTED
;
1596 err
= move_addr_to_user(&address
,
1597 len
, upeer_sockaddr
, upeer_addrlen
);
1602 /* File flags are not inherited via accept() unlike another OSes. */
1604 fd_install(newfd
, newfile
);
1608 fput_light(sock
->file
, fput_needed
);
1613 put_unused_fd(newfd
);
1617 SYSCALL_DEFINE3(accept
, int, fd
, struct sockaddr __user
*, upeer_sockaddr
,
1618 int __user
*, upeer_addrlen
)
1620 return sys_accept4(fd
, upeer_sockaddr
, upeer_addrlen
, 0);
1624 * Attempt to connect to a socket with the server address. The address
1625 * is in user space so we verify it is OK and move it to kernel space.
1627 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
1630 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
1631 * other SEQPACKET protocols that take time to connect() as it doesn't
1632 * include the -EINPROGRESS status for such sockets.
1635 SYSCALL_DEFINE3(connect
, int, fd
, struct sockaddr __user
*, uservaddr
,
1638 struct socket
*sock
;
1639 struct sockaddr_storage address
;
1640 int err
, fput_needed
;
1642 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1645 err
= move_addr_to_kernel(uservaddr
, addrlen
, &address
);
1650 security_socket_connect(sock
, (struct sockaddr
*)&address
, addrlen
);
1654 err
= sock
->ops
->connect(sock
, (struct sockaddr
*)&address
, addrlen
,
1655 sock
->file
->f_flags
);
1657 fput_light(sock
->file
, fput_needed
);
1663 * Get the local address ('name') of a socket object. Move the obtained
1664 * name to user space.
1667 SYSCALL_DEFINE3(getsockname
, int, fd
, struct sockaddr __user
*, usockaddr
,
1668 int __user
*, usockaddr_len
)
1670 struct socket
*sock
;
1671 struct sockaddr_storage address
;
1672 int len
, err
, fput_needed
;
1674 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1678 err
= security_socket_getsockname(sock
);
1682 err
= sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
, 0);
1685 err
= move_addr_to_user(&address
, len
, usockaddr
, usockaddr_len
);
1688 fput_light(sock
->file
, fput_needed
);
1694 * Get the remote address ('name') of a socket object. Move the obtained
1695 * name to user space.
1698 SYSCALL_DEFINE3(getpeername
, int, fd
, struct sockaddr __user
*, usockaddr
,
1699 int __user
*, usockaddr_len
)
1701 struct socket
*sock
;
1702 struct sockaddr_storage address
;
1703 int len
, err
, fput_needed
;
1705 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1707 err
= security_socket_getpeername(sock
);
1709 fput_light(sock
->file
, fput_needed
);
1714 sock
->ops
->getname(sock
, (struct sockaddr
*)&address
, &len
,
1717 err
= move_addr_to_user(&address
, len
, usockaddr
,
1719 fput_light(sock
->file
, fput_needed
);
1725 * Send a datagram to a given address. We move the address into kernel
1726 * space and check the user space data area is readable before invoking
1730 SYSCALL_DEFINE6(sendto
, int, fd
, void __user
*, buff
, size_t, len
,
1731 unsigned int, flags
, struct sockaddr __user
*, addr
,
1734 struct socket
*sock
;
1735 struct sockaddr_storage address
;
1741 err
= import_single_range(WRITE
, buff
, len
, &iov
, &msg
.msg_iter
);
1744 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1748 msg
.msg_name
= NULL
;
1749 msg
.msg_control
= NULL
;
1750 msg
.msg_controllen
= 0;
1751 msg
.msg_namelen
= 0;
1753 err
= move_addr_to_kernel(addr
, addr_len
, &address
);
1756 msg
.msg_name
= (struct sockaddr
*)&address
;
1757 msg
.msg_namelen
= addr_len
;
1759 if (sock
->file
->f_flags
& O_NONBLOCK
)
1760 flags
|= MSG_DONTWAIT
;
1761 msg
.msg_flags
= flags
;
1762 err
= sock_sendmsg(sock
, &msg
);
1765 fput_light(sock
->file
, fput_needed
);
1771 * Send a datagram down a socket.
1774 SYSCALL_DEFINE4(send
, int, fd
, void __user
*, buff
, size_t, len
,
1775 unsigned int, flags
)
1777 return sys_sendto(fd
, buff
, len
, flags
, NULL
, 0);
1781 * Receive a frame from the socket and optionally record the address of the
1782 * sender. We verify the buffers are writable and if needed move the
1783 * sender address from kernel to user space.
1786 SYSCALL_DEFINE6(recvfrom
, int, fd
, void __user
*, ubuf
, size_t, size
,
1787 unsigned int, flags
, struct sockaddr __user
*, addr
,
1788 int __user
*, addr_len
)
1790 struct socket
*sock
;
1793 struct sockaddr_storage address
;
1797 err
= import_single_range(READ
, ubuf
, size
, &iov
, &msg
.msg_iter
);
1800 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1804 msg
.msg_control
= NULL
;
1805 msg
.msg_controllen
= 0;
1806 /* Save some cycles and don't copy the address if not needed */
1807 msg
.msg_name
= addr
? (struct sockaddr
*)&address
: NULL
;
1808 /* We assume all kernel code knows the size of sockaddr_storage */
1809 msg
.msg_namelen
= 0;
1810 msg
.msg_iocb
= NULL
;
1812 if (sock
->file
->f_flags
& O_NONBLOCK
)
1813 flags
|= MSG_DONTWAIT
;
1814 err
= sock_recvmsg(sock
, &msg
, flags
);
1816 if (err
>= 0 && addr
!= NULL
) {
1817 err2
= move_addr_to_user(&address
,
1818 msg
.msg_namelen
, addr
, addr_len
);
1823 fput_light(sock
->file
, fput_needed
);
1829 * Receive a datagram from a socket.
1832 SYSCALL_DEFINE4(recv
, int, fd
, void __user
*, ubuf
, size_t, size
,
1833 unsigned int, flags
)
1835 return sys_recvfrom(fd
, ubuf
, size
, flags
, NULL
, NULL
);
1839 * Set a socket option. Because we don't know the option lengths we have
1840 * to pass the user mode parameter for the protocols to sort out.
1843 SYSCALL_DEFINE5(setsockopt
, int, fd
, int, level
, int, optname
,
1844 char __user
*, optval
, int, optlen
)
1846 int err
, fput_needed
;
1847 struct socket
*sock
;
1852 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1854 err
= security_socket_setsockopt(sock
, level
, optname
);
1858 if (level
== SOL_SOCKET
)
1860 sock_setsockopt(sock
, level
, optname
, optval
,
1864 sock
->ops
->setsockopt(sock
, level
, optname
, optval
,
1867 fput_light(sock
->file
, fput_needed
);
1873 * Get a socket option. Because we don't know the option lengths we have
1874 * to pass a user mode parameter for the protocols to sort out.
1877 SYSCALL_DEFINE5(getsockopt
, int, fd
, int, level
, int, optname
,
1878 char __user
*, optval
, int __user
*, optlen
)
1880 int err
, fput_needed
;
1881 struct socket
*sock
;
1883 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1885 err
= security_socket_getsockopt(sock
, level
, optname
);
1889 if (level
== SOL_SOCKET
)
1891 sock_getsockopt(sock
, level
, optname
, optval
,
1895 sock
->ops
->getsockopt(sock
, level
, optname
, optval
,
1898 fput_light(sock
->file
, fput_needed
);
1904 * Shutdown a socket.
1907 SYSCALL_DEFINE2(shutdown
, int, fd
, int, how
)
1909 int err
, fput_needed
;
1910 struct socket
*sock
;
1912 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
1914 err
= security_socket_shutdown(sock
, how
);
1916 err
= sock
->ops
->shutdown(sock
, how
);
1917 fput_light(sock
->file
, fput_needed
);
1922 /* A couple of helpful macros for getting the address of the 32/64 bit
1923 * fields which are the same type (int / unsigned) on our platforms.
1925 #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1926 #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1927 #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1929 struct used_address
{
1930 struct sockaddr_storage name
;
1931 unsigned int name_len
;
1934 static int copy_msghdr_from_user(struct msghdr
*kmsg
,
1935 struct user_msghdr __user
*umsg
,
1936 struct sockaddr __user
**save_addr
,
1939 struct user_msghdr msg
;
1942 if (copy_from_user(&msg
, umsg
, sizeof(*umsg
)))
1945 kmsg
->msg_control
= (void __force
*)msg
.msg_control
;
1946 kmsg
->msg_controllen
= msg
.msg_controllen
;
1947 kmsg
->msg_flags
= msg
.msg_flags
;
1949 kmsg
->msg_namelen
= msg
.msg_namelen
;
1951 kmsg
->msg_namelen
= 0;
1953 if (kmsg
->msg_namelen
< 0)
1956 if (kmsg
->msg_namelen
> sizeof(struct sockaddr_storage
))
1957 kmsg
->msg_namelen
= sizeof(struct sockaddr_storage
);
1960 *save_addr
= msg
.msg_name
;
1962 if (msg
.msg_name
&& kmsg
->msg_namelen
) {
1964 err
= move_addr_to_kernel(msg
.msg_name
,
1971 kmsg
->msg_name
= NULL
;
1972 kmsg
->msg_namelen
= 0;
1975 if (msg
.msg_iovlen
> UIO_MAXIOV
)
1978 kmsg
->msg_iocb
= NULL
;
1980 return import_iovec(save_addr
? READ
: WRITE
,
1981 msg
.msg_iov
, msg
.msg_iovlen
,
1982 UIO_FASTIOV
, iov
, &kmsg
->msg_iter
);
1985 static int ___sys_sendmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
1986 struct msghdr
*msg_sys
, unsigned int flags
,
1987 struct used_address
*used_address
,
1988 unsigned int allowed_msghdr_flags
)
1990 struct compat_msghdr __user
*msg_compat
=
1991 (struct compat_msghdr __user
*)msg
;
1992 struct sockaddr_storage address
;
1993 struct iovec iovstack
[UIO_FASTIOV
], *iov
= iovstack
;
1994 unsigned char ctl
[sizeof(struct cmsghdr
) + 20]
1995 __aligned(sizeof(__kernel_size_t
));
1996 /* 20 is size of ipv6_pktinfo */
1997 unsigned char *ctl_buf
= ctl
;
2001 msg_sys
->msg_name
= &address
;
2003 if (MSG_CMSG_COMPAT
& flags
)
2004 err
= get_compat_msghdr(msg_sys
, msg_compat
, NULL
, &iov
);
2006 err
= copy_msghdr_from_user(msg_sys
, msg
, NULL
, &iov
);
2012 if (msg_sys
->msg_controllen
> INT_MAX
)
2014 flags
|= (msg_sys
->msg_flags
& allowed_msghdr_flags
);
2015 ctl_len
= msg_sys
->msg_controllen
;
2016 if ((MSG_CMSG_COMPAT
& flags
) && ctl_len
) {
2018 cmsghdr_from_user_compat_to_kern(msg_sys
, sock
->sk
, ctl
,
2022 ctl_buf
= msg_sys
->msg_control
;
2023 ctl_len
= msg_sys
->msg_controllen
;
2024 } else if (ctl_len
) {
2025 BUILD_BUG_ON(sizeof(struct cmsghdr
) !=
2026 CMSG_ALIGN(sizeof(struct cmsghdr
)));
2027 if (ctl_len
> sizeof(ctl
)) {
2028 ctl_buf
= sock_kmalloc(sock
->sk
, ctl_len
, GFP_KERNEL
);
2029 if (ctl_buf
== NULL
)
2034 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2035 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2036 * checking falls down on this.
2038 if (copy_from_user(ctl_buf
,
2039 (void __user __force
*)msg_sys
->msg_control
,
2042 msg_sys
->msg_control
= ctl_buf
;
2044 msg_sys
->msg_flags
= flags
;
2046 if (sock
->file
->f_flags
& O_NONBLOCK
)
2047 msg_sys
->msg_flags
|= MSG_DONTWAIT
;
2049 * If this is sendmmsg() and current destination address is same as
2050 * previously succeeded address, omit asking LSM's decision.
2051 * used_address->name_len is initialized to UINT_MAX so that the first
2052 * destination address never matches.
2054 if (used_address
&& msg_sys
->msg_name
&&
2055 used_address
->name_len
== msg_sys
->msg_namelen
&&
2056 !memcmp(&used_address
->name
, msg_sys
->msg_name
,
2057 used_address
->name_len
)) {
2058 err
= sock_sendmsg_nosec(sock
, msg_sys
);
2061 err
= sock_sendmsg(sock
, msg_sys
);
2063 * If this is sendmmsg() and sending to current destination address was
2064 * successful, remember it.
2066 if (used_address
&& err
>= 0) {
2067 used_address
->name_len
= msg_sys
->msg_namelen
;
2068 if (msg_sys
->msg_name
)
2069 memcpy(&used_address
->name
, msg_sys
->msg_name
,
2070 used_address
->name_len
);
2075 sock_kfree_s(sock
->sk
, ctl_buf
, ctl_len
);
2082 * BSD sendmsg interface
2085 long __sys_sendmsg(int fd
, struct user_msghdr __user
*msg
, unsigned flags
)
2087 int fput_needed
, err
;
2088 struct msghdr msg_sys
;
2089 struct socket
*sock
;
2091 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2095 err
= ___sys_sendmsg(sock
, msg
, &msg_sys
, flags
, NULL
, 0);
2097 fput_light(sock
->file
, fput_needed
);
2102 SYSCALL_DEFINE3(sendmsg
, int, fd
, struct user_msghdr __user
*, msg
, unsigned int, flags
)
2104 if (flags
& MSG_CMSG_COMPAT
)
2106 return __sys_sendmsg(fd
, msg
, flags
);
2110 * Linux sendmmsg interface
2113 int __sys_sendmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2116 int fput_needed
, err
, datagrams
;
2117 struct socket
*sock
;
2118 struct mmsghdr __user
*entry
;
2119 struct compat_mmsghdr __user
*compat_entry
;
2120 struct msghdr msg_sys
;
2121 struct used_address used_address
;
2122 unsigned int oflags
= flags
;
2124 if (vlen
> UIO_MAXIOV
)
2129 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2133 used_address
.name_len
= UINT_MAX
;
2135 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2139 while (datagrams
< vlen
) {
2140 if (datagrams
== vlen
- 1)
2143 if (MSG_CMSG_COMPAT
& flags
) {
2144 err
= ___sys_sendmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2145 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2148 err
= __put_user(err
, &compat_entry
->msg_len
);
2151 err
= ___sys_sendmsg(sock
,
2152 (struct user_msghdr __user
*)entry
,
2153 &msg_sys
, flags
, &used_address
, MSG_EOR
);
2156 err
= put_user(err
, &entry
->msg_len
);
2163 if (msg_data_left(&msg_sys
))
2168 fput_light(sock
->file
, fput_needed
);
2170 /* We only return an error if no datagrams were able to be sent */
2177 SYSCALL_DEFINE4(sendmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2178 unsigned int, vlen
, unsigned int, flags
)
2180 if (flags
& MSG_CMSG_COMPAT
)
2182 return __sys_sendmmsg(fd
, mmsg
, vlen
, flags
);
2185 static int ___sys_recvmsg(struct socket
*sock
, struct user_msghdr __user
*msg
,
2186 struct msghdr
*msg_sys
, unsigned int flags
, int nosec
)
2188 struct compat_msghdr __user
*msg_compat
=
2189 (struct compat_msghdr __user
*)msg
;
2190 struct iovec iovstack
[UIO_FASTIOV
];
2191 struct iovec
*iov
= iovstack
;
2192 unsigned long cmsg_ptr
;
2196 /* kernel mode address */
2197 struct sockaddr_storage addr
;
2199 /* user mode address pointers */
2200 struct sockaddr __user
*uaddr
;
2201 int __user
*uaddr_len
= COMPAT_NAMELEN(msg
);
2203 msg_sys
->msg_name
= &addr
;
2205 if (MSG_CMSG_COMPAT
& flags
)
2206 err
= get_compat_msghdr(msg_sys
, msg_compat
, &uaddr
, &iov
);
2208 err
= copy_msghdr_from_user(msg_sys
, msg
, &uaddr
, &iov
);
2212 cmsg_ptr
= (unsigned long)msg_sys
->msg_control
;
2213 msg_sys
->msg_flags
= flags
& (MSG_CMSG_CLOEXEC
|MSG_CMSG_COMPAT
);
2215 /* We assume all kernel code knows the size of sockaddr_storage */
2216 msg_sys
->msg_namelen
= 0;
2218 if (sock
->file
->f_flags
& O_NONBLOCK
)
2219 flags
|= MSG_DONTWAIT
;
2220 err
= (nosec
? sock_recvmsg_nosec
: sock_recvmsg
)(sock
, msg_sys
, flags
);
2225 if (uaddr
!= NULL
) {
2226 err
= move_addr_to_user(&addr
,
2227 msg_sys
->msg_namelen
, uaddr
,
2232 err
= __put_user((msg_sys
->msg_flags
& ~MSG_CMSG_COMPAT
),
2236 if (MSG_CMSG_COMPAT
& flags
)
2237 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2238 &msg_compat
->msg_controllen
);
2240 err
= __put_user((unsigned long)msg_sys
->msg_control
- cmsg_ptr
,
2241 &msg
->msg_controllen
);
2252 * BSD recvmsg interface
2255 long __sys_recvmsg(int fd
, struct user_msghdr __user
*msg
, unsigned flags
)
2257 int fput_needed
, err
;
2258 struct msghdr msg_sys
;
2259 struct socket
*sock
;
2261 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2265 err
= ___sys_recvmsg(sock
, msg
, &msg_sys
, flags
, 0);
2267 fput_light(sock
->file
, fput_needed
);
2272 SYSCALL_DEFINE3(recvmsg
, int, fd
, struct user_msghdr __user
*, msg
,
2273 unsigned int, flags
)
2275 if (flags
& MSG_CMSG_COMPAT
)
2277 return __sys_recvmsg(fd
, msg
, flags
);
2281 * Linux recvmmsg interface
2284 int __sys_recvmmsg(int fd
, struct mmsghdr __user
*mmsg
, unsigned int vlen
,
2285 unsigned int flags
, struct timespec
*timeout
)
2287 int fput_needed
, err
, datagrams
;
2288 struct socket
*sock
;
2289 struct mmsghdr __user
*entry
;
2290 struct compat_mmsghdr __user
*compat_entry
;
2291 struct msghdr msg_sys
;
2292 struct timespec64 end_time
;
2293 struct timespec64 timeout64
;
2296 poll_select_set_timeout(&end_time
, timeout
->tv_sec
,
2302 sock
= sockfd_lookup_light(fd
, &err
, &fput_needed
);
2306 err
= sock_error(sock
->sk
);
2313 compat_entry
= (struct compat_mmsghdr __user
*)mmsg
;
2315 while (datagrams
< vlen
) {
2317 * No need to ask LSM for more than the first datagram.
2319 if (MSG_CMSG_COMPAT
& flags
) {
2320 err
= ___sys_recvmsg(sock
, (struct user_msghdr __user
*)compat_entry
,
2321 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2325 err
= __put_user(err
, &compat_entry
->msg_len
);
2328 err
= ___sys_recvmsg(sock
,
2329 (struct user_msghdr __user
*)entry
,
2330 &msg_sys
, flags
& ~MSG_WAITFORONE
,
2334 err
= put_user(err
, &entry
->msg_len
);
2342 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
2343 if (flags
& MSG_WAITFORONE
)
2344 flags
|= MSG_DONTWAIT
;
2347 ktime_get_ts64(&timeout64
);
2348 *timeout
= timespec64_to_timespec(
2349 timespec64_sub(end_time
, timeout64
));
2350 if (timeout
->tv_sec
< 0) {
2351 timeout
->tv_sec
= timeout
->tv_nsec
= 0;
2355 /* Timeout, return less than vlen datagrams */
2356 if (timeout
->tv_nsec
== 0 && timeout
->tv_sec
== 0)
2360 /* Out of band data, return right away */
2361 if (msg_sys
.msg_flags
& MSG_OOB
)
2369 if (datagrams
== 0) {
2375 * We may return less entries than requested (vlen) if the
2376 * sock is non block and there aren't enough datagrams...
2378 if (err
!= -EAGAIN
) {
2380 * ... or if recvmsg returns an error after we
2381 * received some datagrams, where we record the
2382 * error to return on the next call or if the
2383 * app asks about it using getsockopt(SO_ERROR).
2385 sock
->sk
->sk_err
= -err
;
2388 fput_light(sock
->file
, fput_needed
);
2393 SYSCALL_DEFINE5(recvmmsg
, int, fd
, struct mmsghdr __user
*, mmsg
,
2394 unsigned int, vlen
, unsigned int, flags
,
2395 struct timespec __user
*, timeout
)
2398 struct timespec timeout_sys
;
2400 if (flags
& MSG_CMSG_COMPAT
)
2404 return __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, NULL
);
2406 if (copy_from_user(&timeout_sys
, timeout
, sizeof(timeout_sys
)))
2409 datagrams
= __sys_recvmmsg(fd
, mmsg
, vlen
, flags
, &timeout_sys
);
2411 if (datagrams
> 0 &&
2412 copy_to_user(timeout
, &timeout_sys
, sizeof(timeout_sys
)))
2413 datagrams
= -EFAULT
;
2418 #ifdef __ARCH_WANT_SYS_SOCKETCALL
2419 /* Argument list sizes for sys_socketcall */
2420 #define AL(x) ((x) * sizeof(unsigned long))
2421 static const unsigned char nargs
[21] = {
2422 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
2423 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
2424 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
2431 * System call vectors.
2433 * Argument checking cleaned up. Saved 20% in size.
2434 * This function doesn't need to set the kernel lock because
2435 * it is set by the callees.
2438 SYSCALL_DEFINE2(socketcall
, int, call
, unsigned long __user
*, args
)
2440 unsigned long a
[AUDITSC_ARGS
];
2441 unsigned long a0
, a1
;
2445 if (call
< 1 || call
> SYS_SENDMMSG
)
2447 call
= array_index_nospec(call
, SYS_SENDMMSG
+ 1);
2450 if (len
> sizeof(a
))
2453 /* copy_from_user should be SMP safe. */
2454 if (copy_from_user(a
, args
, len
))
2457 err
= audit_socketcall(nargs
[call
] / sizeof(unsigned long), a
);
2466 err
= sys_socket(a0
, a1
, a
[2]);
2469 err
= sys_bind(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2472 err
= sys_connect(a0
, (struct sockaddr __user
*)a1
, a
[2]);
2475 err
= sys_listen(a0
, a1
);
2478 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2479 (int __user
*)a
[2], 0);
2481 case SYS_GETSOCKNAME
:
2483 sys_getsockname(a0
, (struct sockaddr __user
*)a1
,
2484 (int __user
*)a
[2]);
2486 case SYS_GETPEERNAME
:
2488 sys_getpeername(a0
, (struct sockaddr __user
*)a1
,
2489 (int __user
*)a
[2]);
2491 case SYS_SOCKETPAIR
:
2492 err
= sys_socketpair(a0
, a1
, a
[2], (int __user
*)a
[3]);
2495 err
= sys_send(a0
, (void __user
*)a1
, a
[2], a
[3]);
2498 err
= sys_sendto(a0
, (void __user
*)a1
, a
[2], a
[3],
2499 (struct sockaddr __user
*)a
[4], a
[5]);
2502 err
= sys_recv(a0
, (void __user
*)a1
, a
[2], a
[3]);
2505 err
= sys_recvfrom(a0
, (void __user
*)a1
, a
[2], a
[3],
2506 (struct sockaddr __user
*)a
[4],
2507 (int __user
*)a
[5]);
2510 err
= sys_shutdown(a0
, a1
);
2512 case SYS_SETSOCKOPT
:
2513 err
= sys_setsockopt(a0
, a1
, a
[2], (char __user
*)a
[3], a
[4]);
2515 case SYS_GETSOCKOPT
:
2517 sys_getsockopt(a0
, a1
, a
[2], (char __user
*)a
[3],
2518 (int __user
*)a
[4]);
2521 err
= sys_sendmsg(a0
, (struct user_msghdr __user
*)a1
, a
[2]);
2524 err
= sys_sendmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2], a
[3]);
2527 err
= sys_recvmsg(a0
, (struct user_msghdr __user
*)a1
, a
[2]);
2530 err
= sys_recvmmsg(a0
, (struct mmsghdr __user
*)a1
, a
[2], a
[3],
2531 (struct timespec __user
*)a
[4]);
2534 err
= sys_accept4(a0
, (struct sockaddr __user
*)a1
,
2535 (int __user
*)a
[2], a
[3]);
2544 #endif /* __ARCH_WANT_SYS_SOCKETCALL */
2547 * sock_register - add a socket protocol handler
2548 * @ops: description of protocol
2550 * This function is called by a protocol handler that wants to
2551 * advertise its address family, and have it linked into the
2552 * socket interface. The value ops->family corresponds to the
2553 * socket system call protocol family.
2555 int sock_register(const struct net_proto_family
*ops
)
2559 if (ops
->family
>= NPROTO
) {
2560 pr_crit("protocol %d >= NPROTO(%d)\n", ops
->family
, NPROTO
);
2564 spin_lock(&net_family_lock
);
2565 if (rcu_dereference_protected(net_families
[ops
->family
],
2566 lockdep_is_held(&net_family_lock
)))
2569 rcu_assign_pointer(net_families
[ops
->family
], ops
);
2572 spin_unlock(&net_family_lock
);
2574 pr_info("NET: Registered protocol family %d\n", ops
->family
);
2577 EXPORT_SYMBOL(sock_register
);
2580 * sock_unregister - remove a protocol handler
2581 * @family: protocol family to remove
2583 * This function is called by a protocol handler that wants to
2584 * remove its address family, and have it unlinked from the
2585 * new socket creation.
2587 * If protocol handler is a module, then it can use module reference
2588 * counts to protect against new references. If protocol handler is not
2589 * a module then it needs to provide its own protection in
2590 * the ops->create routine.
2592 void sock_unregister(int family
)
2594 BUG_ON(family
< 0 || family
>= NPROTO
);
2596 spin_lock(&net_family_lock
);
2597 RCU_INIT_POINTER(net_families
[family
], NULL
);
2598 spin_unlock(&net_family_lock
);
2602 pr_info("NET: Unregistered protocol family %d\n", family
);
2604 EXPORT_SYMBOL(sock_unregister
);
2606 static int __init
sock_init(void)
2610 * Initialize the network sysctl infrastructure.
2612 err
= net_sysctl_init();
2617 * Initialize skbuff SLAB cache
2622 * Initialize the protocols module.
2627 err
= register_filesystem(&sock_fs_type
);
2630 sock_mnt
= kern_mount(&sock_fs_type
);
2631 if (IS_ERR(sock_mnt
)) {
2632 err
= PTR_ERR(sock_mnt
);
2636 /* The real protocol initialization is performed in later initcalls.
2639 #ifdef CONFIG_NETFILTER
2640 err
= netfilter_init();
2645 ptp_classifier_init();
2651 unregister_filesystem(&sock_fs_type
);
2656 core_initcall(sock_init
); /* early initcall */
2658 static int __init
jit_init(void)
2660 #ifdef CONFIG_BPF_JIT_ALWAYS_ON
2665 pure_initcall(jit_init
);
2667 #ifdef CONFIG_PROC_FS
2668 void socket_seq_show(struct seq_file
*seq
)
2673 for_each_possible_cpu(cpu
)
2674 counter
+= per_cpu(sockets_in_use
, cpu
);
2676 /* It can be negative, by the way. 8) */
2680 seq_printf(seq
, "sockets: used %d\n", counter
);
2682 #endif /* CONFIG_PROC_FS */
2684 #ifdef CONFIG_COMPAT
2685 static int do_siocgstamp(struct net
*net
, struct socket
*sock
,
2686 unsigned int cmd
, void __user
*up
)
2688 mm_segment_t old_fs
= get_fs();
2693 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&ktv
);
2696 err
= compat_put_timeval(&ktv
, up
);
2701 static int do_siocgstampns(struct net
*net
, struct socket
*sock
,
2702 unsigned int cmd
, void __user
*up
)
2704 mm_segment_t old_fs
= get_fs();
2705 struct timespec kts
;
2709 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)&kts
);
2712 err
= compat_put_timespec(&kts
, up
);
2717 static int dev_ifname32(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2719 struct ifreq __user
*uifr
;
2722 uifr
= compat_alloc_user_space(sizeof(struct ifreq
));
2723 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2726 err
= dev_ioctl(net
, SIOCGIFNAME
, uifr
);
2730 if (copy_in_user(uifr32
, uifr
, sizeof(struct compat_ifreq
)))
2736 static int dev_ifconf(struct net
*net
, struct compat_ifconf __user
*uifc32
)
2738 struct compat_ifconf ifc32
;
2740 struct ifconf __user
*uifc
;
2741 struct compat_ifreq __user
*ifr32
;
2742 struct ifreq __user
*ifr
;
2746 if (copy_from_user(&ifc32
, uifc32
, sizeof(struct compat_ifconf
)))
2749 memset(&ifc
, 0, sizeof(ifc
));
2750 if (ifc32
.ifcbuf
== 0) {
2754 uifc
= compat_alloc_user_space(sizeof(struct ifconf
));
2756 size_t len
= ((ifc32
.ifc_len
/ sizeof(struct compat_ifreq
)) + 1) *
2757 sizeof(struct ifreq
);
2758 uifc
= compat_alloc_user_space(sizeof(struct ifconf
) + len
);
2760 ifr
= ifc
.ifc_req
= (void __user
*)(uifc
+ 1);
2761 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2762 for (i
= 0; i
< ifc32
.ifc_len
; i
+= sizeof(struct compat_ifreq
)) {
2763 if (copy_in_user(ifr
, ifr32
, sizeof(struct compat_ifreq
)))
2769 if (copy_to_user(uifc
, &ifc
, sizeof(struct ifconf
)))
2772 err
= dev_ioctl(net
, SIOCGIFCONF
, uifc
);
2776 if (copy_from_user(&ifc
, uifc
, sizeof(struct ifconf
)))
2780 ifr32
= compat_ptr(ifc32
.ifcbuf
);
2782 i
+ sizeof(struct compat_ifreq
) <= ifc32
.ifc_len
&& j
< ifc
.ifc_len
;
2783 i
+= sizeof(struct compat_ifreq
), j
+= sizeof(struct ifreq
)) {
2784 if (copy_in_user(ifr32
, ifr
, sizeof(struct compat_ifreq
)))
2790 if (ifc32
.ifcbuf
== 0) {
2791 /* Translate from 64-bit structure multiple to
2795 i
= ((i
/ sizeof(struct ifreq
)) * sizeof(struct compat_ifreq
));
2800 if (copy_to_user(uifc32
, &ifc32
, sizeof(struct compat_ifconf
)))
2806 static int ethtool_ioctl(struct net
*net
, struct compat_ifreq __user
*ifr32
)
2808 struct compat_ethtool_rxnfc __user
*compat_rxnfc
;
2809 bool convert_in
= false, convert_out
= false;
2810 size_t buf_size
= ALIGN(sizeof(struct ifreq
), 8);
2811 struct ethtool_rxnfc __user
*rxnfc
;
2812 struct ifreq __user
*ifr
;
2813 u32 rule_cnt
= 0, actual_rule_cnt
;
2818 if (get_user(data
, &ifr32
->ifr_ifru
.ifru_data
))
2821 compat_rxnfc
= compat_ptr(data
);
2823 if (get_user(ethcmd
, &compat_rxnfc
->cmd
))
2826 /* Most ethtool structures are defined without padding.
2827 * Unfortunately struct ethtool_rxnfc is an exception.
2832 case ETHTOOL_GRXCLSRLALL
:
2833 /* Buffer size is variable */
2834 if (get_user(rule_cnt
, &compat_rxnfc
->rule_cnt
))
2836 if (rule_cnt
> KMALLOC_MAX_SIZE
/ sizeof(u32
))
2838 buf_size
+= rule_cnt
* sizeof(u32
);
2840 case ETHTOOL_GRXRINGS
:
2841 case ETHTOOL_GRXCLSRLCNT
:
2842 case ETHTOOL_GRXCLSRULE
:
2843 case ETHTOOL_SRXCLSRLINS
:
2846 case ETHTOOL_SRXCLSRLDEL
:
2847 buf_size
+= sizeof(struct ethtool_rxnfc
);
2852 ifr
= compat_alloc_user_space(buf_size
);
2853 rxnfc
= (void __user
*)ifr
+ ALIGN(sizeof(struct ifreq
), 8);
2855 if (copy_in_user(&ifr
->ifr_name
, &ifr32
->ifr_name
, IFNAMSIZ
))
2858 if (put_user(convert_in
? rxnfc
: compat_ptr(data
),
2859 &ifr
->ifr_ifru
.ifru_data
))
2863 /* We expect there to be holes between fs.m_ext and
2864 * fs.ring_cookie and at the end of fs, but nowhere else.
2866 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc
, fs
.m_ext
) +
2867 sizeof(compat_rxnfc
->fs
.m_ext
) !=
2868 offsetof(struct ethtool_rxnfc
, fs
.m_ext
) +
2869 sizeof(rxnfc
->fs
.m_ext
));
2871 offsetof(struct compat_ethtool_rxnfc
, fs
.location
) -
2872 offsetof(struct compat_ethtool_rxnfc
, fs
.ring_cookie
) !=
2873 offsetof(struct ethtool_rxnfc
, fs
.location
) -
2874 offsetof(struct ethtool_rxnfc
, fs
.ring_cookie
));
2876 if (copy_in_user(rxnfc
, compat_rxnfc
,
2877 (void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
2878 (void __user
*)rxnfc
) ||
2879 copy_in_user(&rxnfc
->fs
.ring_cookie
,
2880 &compat_rxnfc
->fs
.ring_cookie
,
2881 (void __user
*)(&rxnfc
->fs
.location
+ 1) -
2882 (void __user
*)&rxnfc
->fs
.ring_cookie
) ||
2883 copy_in_user(&rxnfc
->rule_cnt
, &compat_rxnfc
->rule_cnt
,
2884 sizeof(rxnfc
->rule_cnt
)))
2888 ret
= dev_ioctl(net
, SIOCETHTOOL
, ifr
);
2893 if (copy_in_user(compat_rxnfc
, rxnfc
,
2894 (const void __user
*)(&rxnfc
->fs
.m_ext
+ 1) -
2895 (const void __user
*)rxnfc
) ||
2896 copy_in_user(&compat_rxnfc
->fs
.ring_cookie
,
2897 &rxnfc
->fs
.ring_cookie
,
2898 (const void __user
*)(&rxnfc
->fs
.location
+ 1) -
2899 (const void __user
*)&rxnfc
->fs
.ring_cookie
) ||
2900 copy_in_user(&compat_rxnfc
->rule_cnt
, &rxnfc
->rule_cnt
,
2901 sizeof(rxnfc
->rule_cnt
)))
2904 if (ethcmd
== ETHTOOL_GRXCLSRLALL
) {
2905 /* As an optimisation, we only copy the actual
2906 * number of rules that the underlying
2907 * function returned. Since Mallory might
2908 * change the rule count in user memory, we
2909 * check that it is less than the rule count
2910 * originally given (as the user buffer size),
2911 * which has been range-checked.
2913 if (get_user(actual_rule_cnt
, &rxnfc
->rule_cnt
))
2915 if (actual_rule_cnt
< rule_cnt
)
2916 rule_cnt
= actual_rule_cnt
;
2917 if (copy_in_user(&compat_rxnfc
->rule_locs
[0],
2918 &rxnfc
->rule_locs
[0],
2919 rule_cnt
* sizeof(u32
)))
2927 static int compat_siocwandev(struct net
*net
, struct compat_ifreq __user
*uifr32
)
2930 compat_uptr_t uptr32
;
2931 struct ifreq __user
*uifr
;
2933 uifr
= compat_alloc_user_space(sizeof(*uifr
));
2934 if (copy_in_user(uifr
, uifr32
, sizeof(struct compat_ifreq
)))
2937 if (get_user(uptr32
, &uifr32
->ifr_settings
.ifs_ifsu
))
2940 uptr
= compat_ptr(uptr32
);
2942 if (put_user(uptr
, &uifr
->ifr_settings
.ifs_ifsu
.raw_hdlc
))
2945 return dev_ioctl(net
, SIOCWANDEV
, uifr
);
2948 static int bond_ioctl(struct net
*net
, unsigned int cmd
,
2949 struct compat_ifreq __user
*ifr32
)
2952 mm_segment_t old_fs
;
2956 case SIOCBONDENSLAVE
:
2957 case SIOCBONDRELEASE
:
2958 case SIOCBONDSETHWADDR
:
2959 case SIOCBONDCHANGEACTIVE
:
2960 if (copy_from_user(&kifr
, ifr32
, sizeof(struct compat_ifreq
)))
2965 err
= dev_ioctl(net
, cmd
,
2966 (struct ifreq __user __force
*) &kifr
);
2971 return -ENOIOCTLCMD
;
2975 /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
2976 static int compat_ifr_data_ioctl(struct net
*net
, unsigned int cmd
,
2977 struct compat_ifreq __user
*u_ifreq32
)
2979 struct ifreq __user
*u_ifreq64
;
2980 char tmp_buf
[IFNAMSIZ
];
2981 void __user
*data64
;
2984 if (copy_from_user(&tmp_buf
[0], &(u_ifreq32
->ifr_ifrn
.ifrn_name
[0]),
2987 if (get_user(data32
, &u_ifreq32
->ifr_ifru
.ifru_data
))
2989 data64
= compat_ptr(data32
);
2991 u_ifreq64
= compat_alloc_user_space(sizeof(*u_ifreq64
));
2993 if (copy_to_user(&u_ifreq64
->ifr_ifrn
.ifrn_name
[0], &tmp_buf
[0],
2996 if (put_user(data64
, &u_ifreq64
->ifr_ifru
.ifru_data
))
2999 return dev_ioctl(net
, cmd
, u_ifreq64
);
3002 static int dev_ifsioc(struct net
*net
, struct socket
*sock
,
3003 unsigned int cmd
, struct compat_ifreq __user
*uifr32
)
3005 struct ifreq __user
*uifr
;
3008 uifr
= compat_alloc_user_space(sizeof(*uifr
));
3009 if (copy_in_user(uifr
, uifr32
, sizeof(*uifr32
)))
3012 err
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long)uifr
);
3023 case SIOCGIFBRDADDR
:
3024 case SIOCGIFDSTADDR
:
3025 case SIOCGIFNETMASK
:
3030 if (copy_in_user(uifr32
, uifr
, sizeof(*uifr32
)))
3038 static int compat_sioc_ifmap(struct net
*net
, unsigned int cmd
,
3039 struct compat_ifreq __user
*uifr32
)
3042 struct compat_ifmap __user
*uifmap32
;
3043 mm_segment_t old_fs
;
3046 uifmap32
= &uifr32
->ifr_ifru
.ifru_map
;
3047 err
= copy_from_user(&ifr
, uifr32
, sizeof(ifr
.ifr_name
));
3048 err
|= get_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3049 err
|= get_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3050 err
|= get_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3051 err
|= get_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3052 err
|= get_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3053 err
|= get_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3059 err
= dev_ioctl(net
, cmd
, (void __user __force
*)&ifr
);
3062 if (cmd
== SIOCGIFMAP
&& !err
) {
3063 err
= copy_to_user(uifr32
, &ifr
, sizeof(ifr
.ifr_name
));
3064 err
|= put_user(ifr
.ifr_map
.mem_start
, &uifmap32
->mem_start
);
3065 err
|= put_user(ifr
.ifr_map
.mem_end
, &uifmap32
->mem_end
);
3066 err
|= put_user(ifr
.ifr_map
.base_addr
, &uifmap32
->base_addr
);
3067 err
|= put_user(ifr
.ifr_map
.irq
, &uifmap32
->irq
);
3068 err
|= put_user(ifr
.ifr_map
.dma
, &uifmap32
->dma
);
3069 err
|= put_user(ifr
.ifr_map
.port
, &uifmap32
->port
);
3078 struct sockaddr rt_dst
; /* target address */
3079 struct sockaddr rt_gateway
; /* gateway addr (RTF_GATEWAY) */
3080 struct sockaddr rt_genmask
; /* target network mask (IP) */
3081 unsigned short rt_flags
;
3084 unsigned char rt_tos
;
3085 unsigned char rt_class
;
3087 short rt_metric
; /* +1 for binary compatibility! */
3088 /* char * */ u32 rt_dev
; /* forcing the device at add */
3089 u32 rt_mtu
; /* per route MTU/Window */
3090 u32 rt_window
; /* Window clamping */
3091 unsigned short rt_irtt
; /* Initial RTT */
3094 struct in6_rtmsg32
{
3095 struct in6_addr rtmsg_dst
;
3096 struct in6_addr rtmsg_src
;
3097 struct in6_addr rtmsg_gateway
;
3107 static int routing_ioctl(struct net
*net
, struct socket
*sock
,
3108 unsigned int cmd
, void __user
*argp
)
3112 struct in6_rtmsg r6
;
3116 mm_segment_t old_fs
= get_fs();
3118 if (sock
&& sock
->sk
&& sock
->sk
->sk_family
== AF_INET6
) { /* ipv6 */
3119 struct in6_rtmsg32 __user
*ur6
= argp
;
3120 ret
= copy_from_user(&r6
.rtmsg_dst
, &(ur6
->rtmsg_dst
),
3121 3 * sizeof(struct in6_addr
));
3122 ret
|= get_user(r6
.rtmsg_type
, &(ur6
->rtmsg_type
));
3123 ret
|= get_user(r6
.rtmsg_dst_len
, &(ur6
->rtmsg_dst_len
));
3124 ret
|= get_user(r6
.rtmsg_src_len
, &(ur6
->rtmsg_src_len
));
3125 ret
|= get_user(r6
.rtmsg_metric
, &(ur6
->rtmsg_metric
));
3126 ret
|= get_user(r6
.rtmsg_info
, &(ur6
->rtmsg_info
));
3127 ret
|= get_user(r6
.rtmsg_flags
, &(ur6
->rtmsg_flags
));
3128 ret
|= get_user(r6
.rtmsg_ifindex
, &(ur6
->rtmsg_ifindex
));
3132 struct rtentry32 __user
*ur4
= argp
;
3133 ret
= copy_from_user(&r4
.rt_dst
, &(ur4
->rt_dst
),
3134 3 * sizeof(struct sockaddr
));
3135 ret
|= get_user(r4
.rt_flags
, &(ur4
->rt_flags
));
3136 ret
|= get_user(r4
.rt_metric
, &(ur4
->rt_metric
));
3137 ret
|= get_user(r4
.rt_mtu
, &(ur4
->rt_mtu
));
3138 ret
|= get_user(r4
.rt_window
, &(ur4
->rt_window
));
3139 ret
|= get_user(r4
.rt_irtt
, &(ur4
->rt_irtt
));
3140 ret
|= get_user(rtdev
, &(ur4
->rt_dev
));
3142 ret
|= copy_from_user(devname
, compat_ptr(rtdev
), 15);
3143 r4
.rt_dev
= (char __user __force
*)devname
;
3157 ret
= sock_do_ioctl(net
, sock
, cmd
, (unsigned long) r
);
3164 /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
3165 * for some operations; this forces use of the newer bridge-utils that
3166 * use compatible ioctls
3168 static int old_bridge_ioctl(compat_ulong_t __user
*argp
)
3172 if (get_user(tmp
, argp
))
3174 if (tmp
== BRCTL_GET_VERSION
)
3175 return BRCTL_VERSION
+ 1;
3179 static int compat_sock_ioctl_trans(struct file
*file
, struct socket
*sock
,
3180 unsigned int cmd
, unsigned long arg
)
3182 void __user
*argp
= compat_ptr(arg
);
3183 struct sock
*sk
= sock
->sk
;
3184 struct net
*net
= sock_net(sk
);
3186 if (cmd
>= SIOCDEVPRIVATE
&& cmd
<= (SIOCDEVPRIVATE
+ 15))
3187 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3192 return old_bridge_ioctl(argp
);
3194 return dev_ifname32(net
, argp
);
3196 return dev_ifconf(net
, argp
);
3198 return ethtool_ioctl(net
, argp
);
3200 return compat_siocwandev(net
, argp
);
3203 return compat_sioc_ifmap(net
, cmd
, argp
);
3204 case SIOCBONDENSLAVE
:
3205 case SIOCBONDRELEASE
:
3206 case SIOCBONDSETHWADDR
:
3207 case SIOCBONDCHANGEACTIVE
:
3208 return bond_ioctl(net
, cmd
, argp
);
3211 return routing_ioctl(net
, sock
, cmd
, argp
);
3213 return do_siocgstamp(net
, sock
, cmd
, argp
);
3215 return do_siocgstampns(net
, sock
, cmd
, argp
);
3216 case SIOCBONDSLAVEINFOQUERY
:
3217 case SIOCBONDINFOQUERY
:
3220 return compat_ifr_data_ioctl(net
, cmd
, argp
);
3233 return sock_ioctl(file
, cmd
, arg
);
3250 case SIOCSIFHWBROADCAST
:
3252 case SIOCGIFBRDADDR
:
3253 case SIOCSIFBRDADDR
:
3254 case SIOCGIFDSTADDR
:
3255 case SIOCSIFDSTADDR
:
3256 case SIOCGIFNETMASK
:
3257 case SIOCSIFNETMASK
:
3268 return dev_ifsioc(net
, sock
, cmd
, argp
);
3274 return sock_do_ioctl(net
, sock
, cmd
, arg
);
3277 return -ENOIOCTLCMD
;
3280 static long compat_sock_ioctl(struct file
*file
, unsigned int cmd
,
3283 struct socket
*sock
= file
->private_data
;
3284 int ret
= -ENOIOCTLCMD
;
3291 if (sock
->ops
->compat_ioctl
)
3292 ret
= sock
->ops
->compat_ioctl(sock
, cmd
, arg
);
3294 if (ret
== -ENOIOCTLCMD
&&
3295 (cmd
>= SIOCIWFIRST
&& cmd
<= SIOCIWLAST
))
3296 ret
= compat_wext_handle_ioctl(net
, cmd
, arg
);
3298 if (ret
== -ENOIOCTLCMD
)
3299 ret
= compat_sock_ioctl_trans(file
, sock
, cmd
, arg
);
3305 int kernel_bind(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
)
3307 return sock
->ops
->bind(sock
, addr
, addrlen
);
3309 EXPORT_SYMBOL(kernel_bind
);
3311 int kernel_listen(struct socket
*sock
, int backlog
)
3313 return sock
->ops
->listen(sock
, backlog
);
3315 EXPORT_SYMBOL(kernel_listen
);
3317 int kernel_accept(struct socket
*sock
, struct socket
**newsock
, int flags
)
3319 struct sock
*sk
= sock
->sk
;
3322 err
= sock_create_lite(sk
->sk_family
, sk
->sk_type
, sk
->sk_protocol
,
3327 err
= sock
->ops
->accept(sock
, *newsock
, flags
, true);
3329 sock_release(*newsock
);
3334 (*newsock
)->ops
= sock
->ops
;
3335 __module_get((*newsock
)->ops
->owner
);
3340 EXPORT_SYMBOL(kernel_accept
);
3342 int kernel_connect(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
,
3345 return sock
->ops
->connect(sock
, addr
, addrlen
, flags
);
3347 EXPORT_SYMBOL(kernel_connect
);
3349 int kernel_getsockname(struct socket
*sock
, struct sockaddr
*addr
,
3352 return sock
->ops
->getname(sock
, addr
, addrlen
, 0);
3354 EXPORT_SYMBOL(kernel_getsockname
);
3356 int kernel_getpeername(struct socket
*sock
, struct sockaddr
*addr
,
3359 return sock
->ops
->getname(sock
, addr
, addrlen
, 1);
3361 EXPORT_SYMBOL(kernel_getpeername
);
3363 int kernel_getsockopt(struct socket
*sock
, int level
, int optname
,
3364 char *optval
, int *optlen
)
3366 mm_segment_t oldfs
= get_fs();
3367 char __user
*uoptval
;
3368 int __user
*uoptlen
;
3371 uoptval
= (char __user __force
*) optval
;
3372 uoptlen
= (int __user __force
*) optlen
;
3375 if (level
== SOL_SOCKET
)
3376 err
= sock_getsockopt(sock
, level
, optname
, uoptval
, uoptlen
);
3378 err
= sock
->ops
->getsockopt(sock
, level
, optname
, uoptval
,
3383 EXPORT_SYMBOL(kernel_getsockopt
);
3385 int kernel_setsockopt(struct socket
*sock
, int level
, int optname
,
3386 char *optval
, unsigned int optlen
)
3388 mm_segment_t oldfs
= get_fs();
3389 char __user
*uoptval
;
3392 uoptval
= (char __user __force
*) optval
;
3395 if (level
== SOL_SOCKET
)
3396 err
= sock_setsockopt(sock
, level
, optname
, uoptval
, optlen
);
3398 err
= sock
->ops
->setsockopt(sock
, level
, optname
, uoptval
,
3403 EXPORT_SYMBOL(kernel_setsockopt
);
3405 int kernel_sendpage(struct socket
*sock
, struct page
*page
, int offset
,
3406 size_t size
, int flags
)
3408 if (sock
->ops
->sendpage
)
3409 return sock
->ops
->sendpage(sock
, page
, offset
, size
, flags
);
3411 return sock_no_sendpage(sock
, page
, offset
, size
, flags
);
3413 EXPORT_SYMBOL(kernel_sendpage
);
3415 int kernel_sendpage_locked(struct sock
*sk
, struct page
*page
, int offset
,
3416 size_t size
, int flags
)
3418 struct socket
*sock
= sk
->sk_socket
;
3420 if (sock
->ops
->sendpage_locked
)
3421 return sock
->ops
->sendpage_locked(sk
, page
, offset
, size
,
3424 return sock_no_sendpage_locked(sk
, page
, offset
, size
, flags
);
3426 EXPORT_SYMBOL(kernel_sendpage_locked
);
3428 int kernel_sock_ioctl(struct socket
*sock
, int cmd
, unsigned long arg
)
3430 mm_segment_t oldfs
= get_fs();
3434 err
= sock
->ops
->ioctl(sock
, cmd
, arg
);
3439 EXPORT_SYMBOL(kernel_sock_ioctl
);
3441 int kernel_sock_shutdown(struct socket
*sock
, enum sock_shutdown_cmd how
)
3443 return sock
->ops
->shutdown(sock
, how
);
3445 EXPORT_SYMBOL(kernel_sock_shutdown
);
3447 /* This routine returns the IP overhead imposed by a socket i.e.
3448 * the length of the underlying IP header, depending on whether
3449 * this is an IPv4 or IPv6 socket and the length from IP options turned
3450 * on at the socket. Assumes that the caller has a lock on the socket.
3452 u32
kernel_sock_ip_overhead(struct sock
*sk
)
3454 struct inet_sock
*inet
;
3455 struct ip_options_rcu
*opt
;
3457 #if IS_ENABLED(CONFIG_IPV6)
3458 struct ipv6_pinfo
*np
;
3459 struct ipv6_txoptions
*optv6
= NULL
;
3460 #endif /* IS_ENABLED(CONFIG_IPV6) */
3465 switch (sk
->sk_family
) {
3468 overhead
+= sizeof(struct iphdr
);
3469 opt
= rcu_dereference_protected(inet
->inet_opt
,
3470 sock_owned_by_user(sk
));
3472 overhead
+= opt
->opt
.optlen
;
3474 #if IS_ENABLED(CONFIG_IPV6)
3477 overhead
+= sizeof(struct ipv6hdr
);
3479 optv6
= rcu_dereference_protected(np
->opt
,
3480 sock_owned_by_user(sk
));
3482 overhead
+= (optv6
->opt_flen
+ optv6
->opt_nflen
);
3484 #endif /* IS_ENABLED(CONFIG_IPV6) */
3485 default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
3489 EXPORT_SYMBOL(kernel_sock_ip_overhead
);