blob: 79a5f245fc1f6e2aecc21285ff759486b622a42e [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * NET An implementation of the SOCKET network access protocol.
3 *
4 * Version: @(#)socket.c 1.1.93 18/02/95
5 *
6 * Authors: Orest Zborowski, <obz@Kodak.COM>
7 * Ross Biro
8 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
9 *
10 * Fixes:
11 * Anonymous : NOTSOCK/BADF cleanup. Error fix in
12 * shutdown()
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
17 * top level.
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
22 * tty drivers).
23 * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
24 * Jeff Uphoff : Made max number of sockets command-line
25 * configurable.
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
34 * stuff.
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
40 * moment.
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
47 *
48 *
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.
53 *
54 *
55 * This module is effectively the top level interface to the BSD socket
56 * paradigm.
57 *
58 * Based upon Swansea University Computer Society NET3.039
59 */
60
61#include <linux/mm.h>
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/wanrouter.h>
73#include <linux/if_bridge.h>
74#include <linux/if_frad.h>
75#include <linux/if_vlan.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
92#include <asm/uaccess.h>
93#include <asm/unistd.h>
94
95#include <net/compat.h>
96#include <net/wext.h>
97#include <net/cls_cgroup.h>
98
99#include <net/sock.h>
100#include <linux/netfilter.h>
101
102#include <linux/if_tun.h>
103#include <linux/ipv6_route.h>
104#include <linux/route.h>
105#include <linux/sockios.h>
106#include <linux/atalk.h>
107
108
109#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
110#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
111#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
112static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
113static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
114 unsigned long nr_segs, loff_t pos);
115static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
116 unsigned long nr_segs, loff_t pos);
117static int sock_mmap(struct file *file, struct vm_area_struct *vma);
118
119static int sock_close(struct inode *inode, struct file *file);
120static unsigned int sock_poll(struct file *file,
121 struct poll_table_struct *wait);
122static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
123#ifdef CONFIG_COMPAT
124static long compat_sock_ioctl(struct file *file,
125 unsigned int cmd, unsigned long arg);
126#endif
127static int sock_fasync(int fd, struct file *filp, int on);
128static ssize_t sock_sendpage(struct file *file, struct page *page,
129 int offset, size_t size, loff_t *ppos, int more);
130static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
131 struct pipe_inode_info *pipe, size_t len,
132 unsigned int flags);
133extern int __myget_user_1(void *);
134extern int __myget_user_2(void *);
135extern int __myget_user_4(void *);
136
137
138/*
139 * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
140 * in the operation structures but are done directly via the socketcall() multiplexor.
141 */
142/*
143 * i: 内核数据ï¼? * p: 用户空间指针
144 * 返回å€? 成功时返å›?
145 */
146#if 0
147static int myput_user(char i, char *p)
148{
149 memcpy(p, &i, 1);
150 return 0;
151}
152static int myget_user(void *i, char *p)
153{
154 memcpy(i, p, 1);
155 return 0;
156}
157
158#endif
159static const struct file_operations socket_file_ops = {
160 .owner = THIS_MODULE,
161 .llseek = no_llseek,
162 .aio_read = sock_aio_read,
163 .aio_write = sock_aio_write,
164 .poll = sock_poll,
165 .unlocked_ioctl = sock_ioctl,
166#ifdef CONFIG_COMPAT
167 .compat_ioctl = compat_sock_ioctl,
168#endif
169 .mmap = sock_mmap,
170 .open = sock_no_open, /* special open code to disallow open via /proc */
171 .release = sock_close,
172 .fasync = sock_fasync,
173 .sendpage = sock_sendpage,
174 .splice_write = generic_splice_sendpage,
175 .splice_read = sock_splice_read,
176};
177
178/*
179 * The protocol list. Each protocol is registered in here.
180 */
181
182static DEFINE_SPINLOCK(net_family_lock);
183static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
184
185/*
186 * Statistics counters of the socket lists
187 */
188
189static DEFINE_PER_CPU(int, sockets_in_use);
190
191/*
192 * Support routines.
193 * Move socket addresses back and forth across the kernel/user
194 * divide and look after the messy bits.
195 */
196
197/**
198 * move_addr_to_kernel - copy a socket address into kernel space
199 * @uaddr: Address in user space
200 * @kaddr: Address in kernel space
201 * @ulen: Length in user space
202 *
203 * The address is copied into kernel space. If the provided address is
204 * too long an error code of -EINVAL is returned. If the copy gives
205 * invalid addresses -EFAULT is returned. On a success 0 is returned.
206 */
207
208int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
209{
210 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
211 return -EINVAL;
212 if (ulen == 0)
213 return 0;
214 if (copy_from_user(kaddr, uaddr, ulen))
215 return -EFAULT;
216 return audit_sockaddr(ulen, kaddr);
217}
218
219//comlee
220int mymove_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
221{
222// kaddr = (struct sockaddr_storage *)uaddr;
223#if 1
224 if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
225 return -EINVAL;
226 if (ulen == 0)
227 return 0;
228 memcpy(kaddr, uaddr, ulen);
229#endif
230 return audit_sockaddr(ulen, kaddr);
231}
232/**
233 * move_addr_to_user - copy an address to user space
234 * @kaddr: kernel space address
235 * @klen: length of address in kernel
236 * @uaddr: user space address
237 * @ulen: pointer to user length field
238 *
239 * The value pointed to by ulen on entry is the buffer length available.
240 * This is overwritten with the buffer space used. -EINVAL is returned
241 * if an overlong buffer is specified or a negative buffer size. -EFAULT
242 * is returned if either the buffer or the length field are not
243 * accessible.
244 * After copying the data up to the limit the user specifies, the true
245 * length of the data is written over the length limit the user
246 * specified. Zero is returned for a success.
247 */
248
249static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
250 void __user *uaddr, int __user *ulen)
251{
252 int err;
253 int len;
254
255 BUG_ON(klen > sizeof(struct sockaddr_storage));
256 err = get_user(len, ulen);
257 if (err)
258 return err;
259 if (len > klen)
260 len = klen;
261 if (len < 0)
262 return -EINVAL;
263 if (len) {
264 if (audit_sockaddr(klen, kaddr))
265 return -ENOMEM;
266 if (copy_to_user(uaddr, kaddr, len))
267 return -EFAULT;
268 }
269 /*
270 * "fromlen shall refer to the value before truncation.."
271 * 1003.1g
272 */
273 return __put_user(klen, ulen);
274}
275//comlee
276static int mymove_addr_to_user(struct sockaddr_storage *kaddr, int klen,
277 void __user *uaddr, int __user *ulen)
278{
279 int err;
280 int len;
281
282 BUG_ON(klen > sizeof(struct sockaddr_storage));
283 err = myget_user(len, ulen);
284 if (err)
285 return err;
286 if (len > klen)
287 len = klen;
288 if (len < 0)
289 return -EINVAL;
290 if (len) {
291 if (audit_sockaddr(klen, kaddr))
292 return -ENOMEM;
293 memcpy(uaddr, kaddr, len);
294 }
295 /*
296 * "fromlen shall refer to the value before truncation.."
297 * 1003.1g
298 */
299 return myput_user(klen, ulen);
300}
301
302
303static struct kmem_cache *sock_inode_cachep __read_mostly;
304
305static struct inode *sock_alloc_inode(struct super_block *sb)
306{
307 struct socket_alloc *ei;
308 struct socket_wq *wq;
309 ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
310 if (!ei)
311 return NULL;
312 netslab_inc(MYSOCKET_SLAB);
313 wq = kmalloc(sizeof(*wq), GFP_KERNEL);
314 if (!wq) {
315 netslab_dec(MYSOCKET_SLAB);
316 kmem_cache_free(sock_inode_cachep, ei);
317 return NULL;
318 }
319 init_waitqueue_head(&wq->wait);
320 wq->fasync_list = NULL;
321 RCU_INIT_POINTER(ei->socket.wq, wq);
322
323 ei->socket.state = SS_UNCONNECTED;
324 ei->socket.flags = 0;
325 ei->socket.ops = NULL;
326 ei->socket.sk = NULL;
327 ei->socket.file = NULL;
328
329 return &ei->vfs_inode;
330}
331
332static void sock_destroy_inode(struct inode *inode)
333{
334 struct socket_alloc *ei;
335 struct socket_wq *wq;
336
337 ei = container_of(inode, struct socket_alloc, vfs_inode);
338 wq = rcu_dereference_protected(ei->socket.wq, 1);
339 kfree_rcu(wq, rcu);
340 netslab_dec(MYSOCKET_SLAB);
341 kmem_cache_free(sock_inode_cachep, ei);
342}
343
344static void init_once(void *foo)
345{
346 struct socket_alloc *ei = (struct socket_alloc *)foo;
347
348 inode_init_once(&ei->vfs_inode);
349}
350
351static int init_inodecache(void)
352{
353 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
354 sizeof(struct socket_alloc),
355 0,
356 (SLAB_HWCACHE_ALIGN |
357 SLAB_RECLAIM_ACCOUNT |
358 SLAB_MEM_SPREAD),
359 init_once);
360 if (sock_inode_cachep == NULL)
361 return -ENOMEM;
362 return 0;
363}
364
365static const struct super_operations sockfs_ops = {
366 .alloc_inode = sock_alloc_inode,
367 .destroy_inode = sock_destroy_inode,
368 .statfs = simple_statfs,
369};
370
371/*
372 * sockfs_dname() is called from d_path().
373 */
374static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
375{
376 return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
377 dentry->d_inode->i_ino);
378}
379
380static const struct dentry_operations sockfs_dentry_operations = {
381 .d_dname = sockfs_dname,
382};
383
384static struct dentry *sockfs_mount(struct file_system_type *fs_type,
385 int flags, const char *dev_name, void *data)
386{
387 return mount_pseudo(fs_type, "socket:", &sockfs_ops,
388 &sockfs_dentry_operations, SOCKFS_MAGIC);
389}
390
391static struct vfsmount *sock_mnt __read_mostly;
392
393static struct file_system_type sock_fs_type = {
394 .name = "sockfs",
395 .mount = sockfs_mount,
396 .kill_sb = kill_anon_super,
397};
398
399/*
400 * Obtains the first available file descriptor and sets it up for use.
401 *
402 * These functions create file structures and maps them to fd space
403 * of the current process. On success it returns file descriptor
404 * and file struct implicitly stored in sock->file.
405 * Note that another thread may close file descriptor before we return
406 * from this function. We use the fact that now we do not refer
407 * to socket after mapping. If one day we will need it, this
408 * function will increment ref. count on file by 1.
409 *
410 * In any case returned fd MAY BE not valid!
411 * This race condition is unavoidable
412 * with shared fd spaces, we cannot solve it inside kernel,
413 * but we take care of internal coherence yet.
414 */
415
416static int sock_alloc_file(struct socket *sock, struct file **f, int flags)
417{
418 struct qstr name = { .name = "" };
419 struct path path;
420 struct file *file;
421 int fd;
422
423 fd = get_unused_fd_flags(flags);
424 if (unlikely(fd < 0))
425 return fd;
426
427 path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
428 if (unlikely(!path.dentry)) {
429 put_unused_fd(fd);
430 return -ENOMEM;
431 }
432 path.mnt = mntget(sock_mnt);
433
434 d_instantiate(path.dentry, SOCK_INODE(sock));
435 SOCK_INODE(sock)->i_fop = &socket_file_ops;
436
437 file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
438 &socket_file_ops);
439 if (unlikely(!file)) {
440 /* drop dentry, keep inode */
441 ihold(path.dentry->d_inode);
442 path_put(&path);
443 put_unused_fd(fd);
444 return -ENFILE;
445 }
446
447 sock->file = file;
448 file->f_flags = O_RDWR | (flags & O_NONBLOCK);
449 file->f_pos = 0;
450 file->private_data = sock;
451
452 *f = file;
453 return fd;
454}
455
456int sock_map_fd(struct socket *sock, int flags)
457{
458 struct file *newfile;
459 int fd = sock_alloc_file(sock, &newfile, flags);
460
461 if (likely(fd >= 0))
462 fd_install(fd, newfile);
463
464 return fd;
465}
466EXPORT_SYMBOL(sock_map_fd);
467
468static struct socket *sock_from_file(struct file *file, int *err)
469{
470 if (file->f_op == &socket_file_ops)
471 return file->private_data; /* set in sock_map_fd */
472
473 *err = -ENOTSOCK;
474 return NULL;
475}
476
477/**
478 * sockfd_lookup - Go from a file number to its socket slot
479 * @fd: file handle
480 * @err: pointer to an error code return
481 *
482 * The file handle passed in is locked and the socket it is bound
483 * too is returned. If an error occurs the err pointer is overwritten
484 * with a negative errno code and NULL is returned. The function checks
485 * for both invalid handles and passing a handle which is not a socket.
486 *
487 * On a success the socket object pointer is returned.
488 */
489
490struct socket *sockfd_lookup(int fd, int *err)
491{
492 struct file *file;
493 struct socket *sock;
494
495 file = fget(fd);
496 if (!file) {
497 *err = -EBADF;
498 return NULL;
499 }
500
501 sock = sock_from_file(file, err);
502 if (!sock)
503 fput(file);
504 return sock;
505}
506EXPORT_SYMBOL(sockfd_lookup);
507
508static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
509{
510 struct file *file;
511 struct socket *sock;
512
513 *err = -EBADF;
514 file = fget_light(fd, fput_needed);
515 if (file) {
516 sock = sock_from_file(file, err);
517 if (sock)
518 return sock;
519 fput_light(file, *fput_needed);
520 }
521 return NULL;
522}
523
524/**
525 * sock_alloc - allocate a socket
526 *
527 * Allocate a new inode and socket object. The two are bound together
528 * and initialised. The socket is then returned. If we are out of inodes
529 * NULL is returned.
530 */
531
532static struct socket *sock_alloc(void)
533{
534 struct inode *inode;
535 struct socket *sock;
536
537 inode = new_inode_pseudo(sock_mnt->mnt_sb);
538 if (!inode)
539 return NULL;
540
541 sock = SOCKET_I(inode);
542
543 kmemcheck_annotate_bitfield(sock, type);
544 inode->i_ino = get_next_ino();
545 inode->i_mode = S_IFSOCK | S_IRWXUGO;
546 inode->i_uid = current_fsuid();
547 inode->i_gid = current_fsgid();
548
549 percpu_add(sockets_in_use, 1);
550 return sock;
551}
552
553/*
554 * In theory you can't get an open on this inode, but /proc provides
555 * a back door. Remember to keep it shut otherwise you'll let the
556 * creepy crawlies in.
557 */
558
559static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
560{
561 return -ENXIO;
562}
563
564const struct file_operations bad_sock_fops = {
565 .owner = THIS_MODULE,
566 .open = sock_no_open,
567 .llseek = noop_llseek,
568};
569
570/**
571 * sock_release - close a socket
572 * @sock: socket to close
573 *
574 * The socket is released from the protocol stack if it has a release
575 * callback, and the inode is then released if the socket is bound to
576 * an inode not a file.
577 */
578
579void sock_release(struct socket *sock)
580{
581 if (sock->ops) {
582 struct module *owner = sock->ops->owner;
583
584 sock->ops->release(sock);
585 sock->ops = NULL;
586 module_put(owner);
587 }
588
589 if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
590 print_sun(SUN_ERR,"sock_release: fasync list not empty!\n");
591
592 if (test_bit(SOCK_EXTERNALLY_ALLOCATED, &sock->flags))
593 return;
594
595 percpu_sub(sockets_in_use, 1);
596 if (!sock->file) {
597 iput(SOCK_INODE(sock));
598 return;
599 }
600 sock->file = NULL;
601}
602EXPORT_SYMBOL(sock_release);
603
604int sock_tx_timestamp(struct sock *sk, __u8 *tx_flags)
605{
606 *tx_flags = 0;
607 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_HARDWARE))
608 *tx_flags |= SKBTX_HW_TSTAMP;
609 if (sock_flag(sk, SOCK_TIMESTAMPING_TX_SOFTWARE))
610 *tx_flags |= SKBTX_SW_TSTAMP;
611 if (sock_flag(sk, SOCK_WIFI_STATUS))
612 *tx_flags |= SKBTX_WIFI_STATUS;
613 return 0;
614}
615EXPORT_SYMBOL(sock_tx_timestamp);
616
617static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock,
618 struct msghdr *msg, size_t size)
619{
620 struct sock_iocb *si = kiocb_to_siocb(iocb);
621
622 sock_update_classid(sock->sk);
623
624 sock_update_netprioidx(sock->sk);
625
626 si->sock = sock;
627 si->scm = NULL;
628 si->msg = msg;
629 si->size = size;
630
631 return sock->ops->sendmsg(iocb, sock, msg, size);
632}
633
634static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
635 struct msghdr *msg, size_t size)
636{
637 int err = security_socket_sendmsg(sock, msg, size);
638
639 print_sun(SUN_DBG,"[comlee]:__sock_sendmsg==>%d\n", err);
640 return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
641}
642
643int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
644{
645 struct kiocb iocb;
646 struct sock_iocb siocb;
647 int ret;
648
649 init_sync_kiocb(&iocb, NULL);
650 iocb.private = &siocb;
651 record_app_atcive_net();
652 ret = __sock_sendmsg(&iocb, sock, msg, size);
653 if (-EIOCBQUEUED == ret)
654 {
655 ret = wait_on_sync_kiocb(&iocb);
656 }
657 return ret;
658}
659EXPORT_SYMBOL(sock_sendmsg);
660
661static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size)
662{
663 struct kiocb iocb;
664 struct sock_iocb siocb;
665 int ret;
666
667 init_sync_kiocb(&iocb, NULL);
668 iocb.private = &siocb;
669 ret = __sock_sendmsg_nosec(&iocb, sock, msg, size);
670 if (-EIOCBQUEUED == ret)
671 ret = wait_on_sync_kiocb(&iocb);
672 return ret;
673}
674
675int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
676 struct kvec *vec, size_t num, size_t size)
677{
678 mm_segment_t oldfs = get_fs();
679 int result;
680
681 set_fs(KERNEL_DS);
682 /*
683 * the following is safe, since for compiler definitions of kvec and
684 * iovec are identical, yielding the same in-core layout and alignment
685 */
686 msg->msg_iov = (struct iovec *)vec;
687 msg->msg_iovlen = num;
688 result = sock_sendmsg(sock, msg, size);
689 set_fs(oldfs);
690 return result;
691}
692EXPORT_SYMBOL(kernel_sendmsg);
693
694static int ktime2ts(ktime_t kt, struct timespec *ts)
695{
696 if (kt.tv64) {
697 *ts = ktime_to_timespec(kt);
698 return 1;
699 } else {
700 return 0;
701 }
702}
703
704/*
705 * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
706 */
707void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
708 struct sk_buff *skb)
709{
710 int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
711 struct timespec ts[3];
712 int empty = 1;
713 struct skb_shared_hwtstamps *shhwtstamps =
714 skb_hwtstamps(skb);
715
716 /* Race occurred between timestamp enabling and packet
717 receiving. Fill in the current time for now. */
718 if (need_software_tstamp && skb->tstamp.tv64 == 0)
719 __net_timestamp(skb);
720
721 if (need_software_tstamp) {
722 if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
723 struct timeval tv;
724 skb_get_timestamp(skb, &tv);
725 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
726 sizeof(tv), &tv);
727 } else {
728 skb_get_timestampns(skb, &ts[0]);
729 put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
730 sizeof(ts[0]), &ts[0]);
731 }
732 }
733
734
735 memset(ts, 0, sizeof(ts));
736 if (skb->tstamp.tv64 &&
737 sock_flag(sk, SOCK_TIMESTAMPING_SOFTWARE)) {
738 skb_get_timestampns(skb, ts + 0);
739 empty = 0;
740 }
741 if (shhwtstamps) {
742 if (sock_flag(sk, SOCK_TIMESTAMPING_SYS_HARDWARE) &&
743 ktime2ts(shhwtstamps->syststamp, ts + 1))
744 empty = 0;
745 if (sock_flag(sk, SOCK_TIMESTAMPING_RAW_HARDWARE) &&
746 ktime2ts(shhwtstamps->hwtstamp, ts + 2))
747 empty = 0;
748 }
749 if (!empty)
750 put_cmsg(msg, SOL_SOCKET,
751 SCM_TIMESTAMPING, sizeof(ts), &ts);
752}
753EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
754
755void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
756 struct sk_buff *skb)
757{
758 int ack;
759
760 if (!sock_flag(sk, SOCK_WIFI_STATUS))
761 return;
762 if (!skb->wifi_acked_valid)
763 return;
764
765 ack = skb->wifi_acked;
766
767 put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
768}
769EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
770
771static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
772 struct sk_buff *skb)
773{
774 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount)
775 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
776 sizeof(__u32), &skb->dropcount);
777}
778
779void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
780 struct sk_buff *skb)
781{
782 sock_recv_timestamp(msg, sk, skb);
783 sock_recv_drops(msg, sk, skb);
784}
785EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
786
787static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock,
788 struct msghdr *msg, size_t size, int flags)
789{
790 struct sock_iocb *si = kiocb_to_siocb(iocb);
791
792 sock_update_classid(sock->sk);
793
794 si->sock = sock;
795 si->scm = NULL;
796 si->msg = msg;
797 si->size = size;
798 si->flags = flags;
799
800 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
801}
802
803static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
804 struct msghdr *msg, size_t size, int flags)
805{
806 int err = security_socket_recvmsg(sock, msg, size, flags);
807
808 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags);
809}
810
811int sock_recvmsg(struct socket *sock, struct msghdr *msg,
812 size_t size, int flags)
813{
814 struct kiocb iocb;
815 struct sock_iocb siocb;
816 int ret;
817
818 init_sync_kiocb(&iocb, NULL);
819 iocb.private = &siocb;
820 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
821 if (-EIOCBQUEUED == ret)
822 ret = wait_on_sync_kiocb(&iocb);
823 return ret;
824}
825EXPORT_SYMBOL(sock_recvmsg);
826
827static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
828 size_t size, int flags)
829{
830 struct kiocb iocb;
831 struct sock_iocb siocb;
832 int ret;
833
834 init_sync_kiocb(&iocb, NULL);
835 iocb.private = &siocb;
836 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
837 if (-EIOCBQUEUED == ret)
838 ret = wait_on_sync_kiocb(&iocb);
839 return ret;
840}
841
842/**
843 * kernel_recvmsg - Receive a message from a socket (kernel space)
844 * @sock: The socket to receive the message from
845 * @msg: Received message
846 * @vec: Input s/g array for message data
847 * @num: Size of input s/g array
848 * @size: Number of bytes to read
849 * @flags: Message flags (MSG_DONTWAIT, etc...)
850 *
851 * On return the msg structure contains the scatter/gather array passed in the
852 * vec argument. The array is modified so that it consists of the unfilled
853 * portion of the original array.
854 *
855 * The returned value is the total number of bytes received, or an error.
856 */
857int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
858 struct kvec *vec, size_t num, size_t size, int flags)
859{
860 mm_segment_t oldfs = get_fs();
861 int result;
862
863 set_fs(KERNEL_DS);
864 /*
865 * the following is safe, since for compiler definitions of kvec and
866 * iovec are identical, yielding the same in-core layout and alignment
867 */
868 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
869 result = sock_recvmsg(sock, msg, size, flags);
870 set_fs(oldfs);
871 return result;
872}
873EXPORT_SYMBOL(kernel_recvmsg);
874
875static void sock_aio_dtor(struct kiocb *iocb)
876{
877 kfree(iocb->private);
878}
879
880static ssize_t sock_sendpage(struct file *file, struct page *page,
881 int offset, size_t size, loff_t *ppos, int more)
882{
883 struct socket *sock;
884 int flags;
885
886 sock = file->private_data;
887
888 flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
889 /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
890 flags |= more;
891
892 return kernel_sendpage(sock, page, offset, size, flags);
893}
894
895static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
896 struct pipe_inode_info *pipe, size_t len,
897 unsigned int flags)
898{
899 struct socket *sock = file->private_data;
900
901 if (unlikely(!sock->ops->splice_read))
902 return -EINVAL;
903
904 sock_update_classid(sock->sk);
905
906 return sock->ops->splice_read(sock, ppos, pipe, len, flags);
907}
908
909static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
910 struct sock_iocb *siocb)
911{
912 if (!is_sync_kiocb(iocb)) {
913 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
914 if (!siocb)
915 return NULL;
916 iocb->ki_dtor = sock_aio_dtor;
917 }
918
919 siocb->kiocb = iocb;
920 iocb->private = siocb;
921 return siocb;
922}
923
924static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
925 struct file *file, const struct iovec *iov,
926 unsigned long nr_segs)
927{
928 struct socket *sock = file->private_data;
929 size_t size = 0;
930 int i;
931
932 for (i = 0; i < nr_segs; i++)
933 size += iov[i].iov_len;
934
935 msg->msg_name = NULL;
936 msg->msg_namelen = 0;
937 msg->msg_control = NULL;
938 msg->msg_controllen = 0;
939 msg->msg_iov = (struct iovec *)iov;
940 msg->msg_iovlen = nr_segs;
941 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
942
943 return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
944}
945
946static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
947 unsigned long nr_segs, loff_t pos)
948{
949 struct sock_iocb siocb, *x;
950
951 if (pos != 0)
952 return -ESPIPE;
953
954 if (iocb->ki_left == 0) /* Match SYS5 behaviour */
955 return 0;
956
957
958 x = alloc_sock_iocb(iocb, &siocb);
959 if (!x)
960 return -ENOMEM;
961 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
962}
963
964static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
965 struct file *file, const struct iovec *iov,
966 unsigned long nr_segs)
967{
968 struct socket *sock = file->private_data;
969 size_t size = 0;
970 int i;
971
972 for (i = 0; i < nr_segs; i++)
973 size += iov[i].iov_len;
974
975 msg->msg_name = NULL;
976 msg->msg_namelen = 0;
977 msg->msg_control = NULL;
978 msg->msg_controllen = 0;
979 msg->msg_iov = (struct iovec *)iov;
980 msg->msg_iovlen = nr_segs;
981 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
982 if (sock->type == SOCK_SEQPACKET)
983 msg->msg_flags |= MSG_EOR;
984
985 return __sock_sendmsg(iocb, sock, msg, size);
986}
987
988static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
989 unsigned long nr_segs, loff_t pos)
990{
991 struct sock_iocb siocb, *x;
992
993 if (pos != 0)
994 return -ESPIPE;
995
996 x = alloc_sock_iocb(iocb, &siocb);
997 if (!x)
998 return -ENOMEM;
999
1000 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
1001}
1002
1003/*
1004 * Atomic setting of ioctl hooks to avoid race
1005 * with module unload.
1006 */
1007
1008static DEFINE_MUTEX(br_ioctl_mutex);
1009static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
1010
1011void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
1012{
1013 mutex_lock(&br_ioctl_mutex);
1014 br_ioctl_hook = hook;
1015 mutex_unlock(&br_ioctl_mutex);
1016}
1017EXPORT_SYMBOL(brioctl_set);
1018
1019static DEFINE_MUTEX(vlan_ioctl_mutex);
1020static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
1021
1022void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
1023{
1024 mutex_lock(&vlan_ioctl_mutex);
1025 vlan_ioctl_hook = hook;
1026 mutex_unlock(&vlan_ioctl_mutex);
1027}
1028EXPORT_SYMBOL(vlan_ioctl_set);
1029
1030static DEFINE_MUTEX(dlci_ioctl_mutex);
1031static int (*dlci_ioctl_hook) (unsigned int, void __user *);
1032
1033void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
1034{
1035 mutex_lock(&dlci_ioctl_mutex);
1036 dlci_ioctl_hook = hook;
1037 mutex_unlock(&dlci_ioctl_mutex);
1038}
1039EXPORT_SYMBOL(dlci_ioctl_set);
1040
1041static long sock_do_ioctl(struct net *net, struct socket *sock,
1042 unsigned int cmd, unsigned long arg)
1043{
1044 int err;
1045 void __user *argp = (void __user *)arg;
1046
1047 err = sock->ops->ioctl(sock, cmd, arg);
1048
1049 /*
1050 * If this ioctl is unknown try to hand it down
1051 * to the NIC driver.
1052 */
1053 if (err == -ENOIOCTLCMD)
1054 err = dev_ioctl(net, cmd, argp);
1055
1056 return err;
1057}
1058
1059/*
1060 * With an ioctl, arg may well be a user mode pointer, but we don't know
1061 * what to do with it - that's up to the protocol still.
1062 */
1063
1064static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1065{
1066 struct socket *sock;
1067 struct sock *sk;
1068 void __user *argp = (void __user *)arg;
1069 int pid, err;
1070 struct net *net;
1071
1072 sock = file->private_data;
1073 sk = sock->sk;
1074 net = sock_net(sk);
1075 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
1076 err = dev_ioctl(net, cmd, argp);
1077 } else
1078#ifdef CONFIG_WEXT_CORE
1079 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
1080 err = dev_ioctl(net, cmd, argp);
1081 } else
1082#endif
1083 switch (cmd) {
1084 case FIOSETOWN:
1085 case SIOCSPGRP:
1086 err = -EFAULT;
1087 if (get_user(pid, (int __user *)argp))
1088 break;
1089 err = f_setown(sock->file, pid, 1);
1090 break;
1091 case FIOGETOWN:
1092 case SIOCGPGRP:
1093 err = put_user(f_getown(sock->file),
1094 (int __user *)argp);
1095 break;
1096 case SIOCGIFBR:
1097 case SIOCSIFBR:
1098 case SIOCBRADDBR:
1099 case SIOCBRDELBR:
1100 err = -ENOPKG;
1101 if (!br_ioctl_hook)
1102 request_module("bridge");
1103
1104 mutex_lock(&br_ioctl_mutex);
1105 if (br_ioctl_hook)
1106 err = br_ioctl_hook(net, cmd, argp);
1107 mutex_unlock(&br_ioctl_mutex);
1108 break;
1109 case SIOCGIFVLAN:
1110 case SIOCSIFVLAN:
1111 err = -ENOPKG;
1112 if (!vlan_ioctl_hook)
1113 request_module("8021q");
1114
1115 mutex_lock(&vlan_ioctl_mutex);
1116 if (vlan_ioctl_hook)
1117 err = vlan_ioctl_hook(net, argp);
1118 mutex_unlock(&vlan_ioctl_mutex);
1119 break;
1120 case SIOCADDDLCI:
1121 case SIOCDELDLCI:
1122 err = -ENOPKG;
1123 if (!dlci_ioctl_hook)
1124 request_module("dlci");
1125
1126 mutex_lock(&dlci_ioctl_mutex);
1127 if (dlci_ioctl_hook)
1128 err = dlci_ioctl_hook(cmd, argp);
1129 mutex_unlock(&dlci_ioctl_mutex);
1130 break;
1131 default:
1132 err = sock_do_ioctl(net, sock, cmd, arg);
1133 break;
1134 }
1135 return err;
1136}
1137
1138int sock_create_lite(int family, int type, int protocol, struct socket **res)
1139{
1140 int err;
1141 struct socket *sock = NULL;
1142
1143 err = security_socket_create(family, type, protocol, 1);
1144 if (err)
1145 goto out;
1146
1147 sock = sock_alloc();
1148 if (!sock) {
1149 err = -ENOMEM;
1150 goto out;
1151 }
1152
1153 sock->type = type;
1154 err = security_socket_post_create(sock, family, type, protocol, 1);
1155 if (err)
1156 goto out_release;
1157
1158out:
1159 *res = sock;
1160 return err;
1161out_release:
1162 sock_release(sock);
1163 sock = NULL;
1164 goto out;
1165}
1166EXPORT_SYMBOL(sock_create_lite);
1167
1168/* No kernel lock held - perfect */
1169static unsigned int sock_poll(struct file *file, poll_table *wait)
1170{
1171 struct socket *sock;
1172
1173 /*
1174 * We can't return errors to poll, so it's either yes or no.
1175 */
1176 sock = file->private_data;
1177 return sock->ops->poll(file, sock, wait);
1178}
1179
1180static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1181{
1182 struct socket *sock = file->private_data;
1183
1184 return sock->ops->mmap(file, sock, vma);
1185}
1186
1187static int sock_close(struct inode *inode, struct file *filp)
1188{
1189 /*
1190 * It was possible the inode is NULL we were
1191 * closing an unfinished socket.
1192 */
1193
1194 if (!inode) {
1195 print_sun(SUN_DBG,"sock_close: NULL inode\n");
1196 return 0;
1197 }
1198 sock_release(SOCKET_I(inode));
1199 return 0;
1200}
1201
1202/*
1203 * Update the socket async list
1204 *
1205 * Fasync_list locking strategy.
1206 *
1207 * 1. fasync_list is modified only under process context socket lock
1208 * i.e. under semaphore.
1209 * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
1210 * or under socket lock
1211 */
1212
1213static int sock_fasync(int fd, struct file *filp, int on)
1214{
1215 struct socket *sock = filp->private_data;
1216 struct sock *sk = sock->sk;
1217 struct socket_wq *wq;
1218
1219 if (sk == NULL)
1220 return -EINVAL;
1221
1222 lock_sock(sk);
1223 wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
1224 fasync_helper(fd, filp, on, &wq->fasync_list);
1225
1226 if (!wq->fasync_list)
1227 sock_reset_flag(sk, SOCK_FASYNC);
1228 else
1229 sock_set_flag(sk, SOCK_FASYNC);
1230
1231 release_sock(sk);
1232 return 0;
1233}
1234
1235/* This function may be called only under socket lock or callback_lock or rcu_lock */
1236
1237int sock_wake_async(struct socket *sock, int how, int band)
1238{
1239 struct socket_wq *wq;
1240
1241 if (!sock)
1242 return -1;
1243 rcu_read_lock();
1244 wq = rcu_dereference(sock->wq);
1245 if (!wq || !wq->fasync_list) {
1246 rcu_read_unlock();
1247 return -1;
1248 }
1249 switch (how) {
1250 case SOCK_WAKE_WAITD:
1251 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1252 break;
1253 goto call_kill;
1254 case SOCK_WAKE_SPACE:
1255 if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
1256 break;
1257 /* fall through */
1258 case SOCK_WAKE_IO:
1259call_kill:
1260 kill_fasync(&wq->fasync_list, SIGIO, band);
1261 break;
1262 case SOCK_WAKE_URG:
1263 kill_fasync(&wq->fasync_list, SIGURG, band);
1264 }
1265 rcu_read_unlock();
1266 return 0;
1267}
1268EXPORT_SYMBOL(sock_wake_async);
1269
1270int __sock_create(struct net *net, int family, int type, int protocol,
1271 struct socket **res, int kern)
1272{
1273 int err;
1274 struct socket *sock;
1275 const struct net_proto_family *pf;
1276
1277 /*
1278 * Check protocol is in range
1279 */
1280 if (family < 0 || family >= NPROTO)
1281 return -EAFNOSUPPORT;
1282 if (type < 0 || type >= SOCK_MAX)
1283 return -EINVAL;
1284
1285 /* Compatibility.
1286
1287 This uglymoron is moved from INET layer to here to avoid
1288 deadlock in module load.
1289 */
1290 if (family == PF_INET && type == SOCK_PACKET) {
1291 static int warned;
1292 if (!warned) {
1293 warned = 1;
1294 print_sun(SUN_DBG,"%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1295 current->comm);
1296 }
1297 family = PF_PACKET;
1298 }
1299
1300 err = security_socket_create(family, type, protocol, kern);
1301 if (err)
1302 return err;
1303
1304 /*
1305 * Allocate the socket and allow the family to set things up. if
1306 * the protocol is 0, the family is instructed to select an appropriate
1307 * default.
1308 */
1309 sock = sock_alloc();
1310 if (!sock) {
1311 if (net_ratelimit())
1312 print_sun(SUN_DBG,"socket: no more sockets\n");
1313 return -ENFILE; /* Not exactly a match, but its the
1314 closest posix thing */
1315 }
1316
1317 sock->type = type;
1318
1319#ifdef CONFIG_MODULES
1320 /* Attempt to load a protocol module if the find failed.
1321 *
1322 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1323 * requested real, full-featured networking support upon configuration.
1324 * Otherwise module support will break!
1325 */
1326 if (rcu_access_pointer(net_families[family]) == NULL)
1327 request_module("net-pf-%d", family);
1328#endif
1329
1330 rcu_read_lock();
1331 pf = rcu_dereference(net_families[family]);
1332 err = -EAFNOSUPPORT;
1333 if (!pf)
1334 goto out_release;
1335
1336 /*
1337 * We will call the ->create function, that possibly is in a loadable
1338 * module, so we have to bump that loadable module refcnt first.
1339 */
1340 if (!try_module_get(pf->owner))
1341 goto out_release;
1342
1343 /* Now protected by module ref count */
1344 rcu_read_unlock();
1345
1346 err = pf->create(net, sock, protocol, kern);
1347 if (err < 0)
1348 goto out_module_put;
1349
1350 /*
1351 * Now to bump the refcnt of the [loadable] module that owns this
1352 * socket at sock_release time we decrement its refcnt.
1353 */
1354 if (!try_module_get(sock->ops->owner))
1355 goto out_module_busy;
1356
1357 /*
1358 * Now that we're done with the ->create function, the [loadable]
1359 * module can have its refcnt decremented
1360 */
1361 module_put(pf->owner);
1362 err = security_socket_post_create(sock, family, type, protocol, kern);
1363 if (err)
1364 goto out_sock_release;
1365 *res = sock;
1366
1367 return 0;
1368
1369out_module_busy:
1370 err = -EAFNOSUPPORT;
1371out_module_put:
1372 sock->ops = NULL;
1373 module_put(pf->owner);
1374out_sock_release:
1375 sock_release(sock);
1376 return err;
1377
1378out_release:
1379 rcu_read_unlock();
1380 goto out_sock_release;
1381}
1382EXPORT_SYMBOL(__sock_create);
1383
1384int sock_create(int family, int type, int protocol, struct socket **res)
1385{
1386 return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
1387}
1388EXPORT_SYMBOL(sock_create);
1389
1390int sock_create_kern(int family, int type, int protocol, struct socket **res)
1391{
1392 return __sock_create(&init_net, family, type, protocol, res, 1);
1393}
1394EXPORT_SYMBOL(sock_create_kern);
1395
1396SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
1397{
1398 int retval;
1399 struct socket *sock;
1400 int flags;
1401
1402 /* Check the SOCK_* constants for consistency. */
1403 BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
1404 BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
1405 BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
1406 BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
1407
1408 flags = type & ~SOCK_TYPE_MASK;
1409 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1410 return -EINVAL;
1411 type &= SOCK_TYPE_MASK;
1412
1413 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1414 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1415
1416 retval = sock_create(family, type, protocol, &sock);
1417 if (retval < 0)
1418 goto out;
1419
1420 retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
1421 if (retval < 0)
1422 goto out_release;
1423
1424out:
1425 /* It may be already another descriptor 8) Not kernel problem. */
1426 return retval;
1427
1428out_release:
1429 sock_release(sock);
1430 return retval;
1431}
1432EXPORT_SYMBOL(sys_socket);
1433
1434//////////////////////////////////////////////////////
1435static int mycopy_msghdr_from_user(struct msghdr *kmsg,
1436 struct msghdr __user *umsg)
1437{
1438 memcpy(kmsg, umsg, sizeof(struct msghdr));
1439
1440 if (kmsg->msg_name == NULL)
1441 kmsg->msg_namelen = 0;
1442
1443 if (kmsg->msg_namelen < 0)
1444 return -EINVAL;
1445
1446 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
1447 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
1448 return 0;
1449}
1450
1451SYSCALL_DEFINE4(mysocketpair, int, family, int, type, int, protocol,
1452 int __user *, usockvec)
1453{
1454 struct socket *sock1, *sock2;
1455 int fd1, fd2, err;
1456 struct file *newfile1, *newfile2;
1457 int flags;
1458
1459 flags = type & ~SOCK_TYPE_MASK;
1460 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1461 return -EINVAL;
1462 type &= SOCK_TYPE_MASK;
1463
1464 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1465 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1466
1467 /*
1468 * Obtain the first socket and check if the underlying protocol
1469 * supports the socketpair call.
1470 */
1471
1472 err = sock_create(family, type, protocol, &sock1);
1473 if (err < 0)
1474 goto out;
1475
1476 err = sock_create(family, type, protocol, &sock2);
1477 if (err < 0)
1478 goto out_release_1;
1479
1480 err = sock1->ops->socketpair(sock1, sock2);
1481 if (err < 0)
1482 goto out_release_both;
1483
1484 fd1 = sock_alloc_file(sock1, &newfile1, flags);
1485 if (unlikely(fd1 < 0)) {
1486 err = fd1;
1487 goto out_release_both;
1488 }
1489
1490 fd2 = sock_alloc_file(sock2, &newfile2, flags);
1491 if (unlikely(fd2 < 0)) {
1492 err = fd2;
1493 fput(newfile1);
1494 put_unused_fd(fd1);
1495 sock_release(sock2);
1496 goto out;
1497 }
1498
1499 audit_fd_pair(fd1, fd2);
1500 fd_install(fd1, newfile1);
1501 fd_install(fd2, newfile2);
1502 /* fd1 and fd2 may be already another descriptors.
1503 * Not kernel problem.
1504 */
1505
1506 err = myput_user(fd1, &usockvec[0]);
1507 if (!err)
1508 err = myput_user(fd2, &usockvec[1]);
1509 if (!err)
1510 return 0;
1511
1512 sys_close(fd2);
1513 sys_close(fd1);
1514 return err;
1515
1516out_release_both:
1517 sock_release(sock2);
1518out_release_1:
1519 sock_release(sock1);
1520out:
1521 return err;
1522}
1523
1524
1525
1526static int ___sys_myrecvmsg(struct socket *sock, struct msghdr __user *msg,
1527 struct msghdr *msg_sys, unsigned flags, int nosec)
1528{
1529 struct compat_msghdr __user *msg_compat =
1530 (struct compat_msghdr __user *)msg;
1531 struct iovec iovstack[UIO_FASTIOV];
1532 struct iovec *iov = iovstack;
1533 unsigned long cmsg_ptr;
1534 int err, iov_size, total_len, len;
1535
1536 /* kernel mode address */
1537 struct sockaddr_storage addr;
1538
1539 /* user mode address pointers */
1540 struct sockaddr __user *uaddr;
1541 int __user *uaddr_len;
1542
1543 if (MSG_CMSG_COMPAT & flags)
1544 err = get_compat_msghdr(msg_sys, msg_compat);
1545 else
1546 err = mycopy_msghdr_from_user(msg_sys, msg);
1547 if (err)
1548 return err;
1549
1550 err = -EMSGSIZE;
1551 if (msg_sys->msg_iovlen > UIO_MAXIOV)
1552 goto out;
1553
1554 /* Check whether to allocate the iovec area */
1555 err = -ENOMEM;
1556 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
1557 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
1558 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
1559 if (!iov)
1560 goto out;
1561 }
1562
1563 /* Save the user-mode address (verify_iovec will change the
1564 * kernel msghdr to use the kernel address space)
1565 */
1566 uaddr = (__force void __user *)msg_sys->msg_name;
1567 uaddr_len = COMPAT_NAMELEN(msg);
1568 if (MSG_CMSG_COMPAT & flags)
1569 err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
1570 else
1571 err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
1572 if (err < 0)
1573 goto out_freeiov;
1574 total_len = err;
1575
1576 cmsg_ptr = (unsigned long)msg_sys->msg_control;
1577 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
1578
1579 /* We assume all kernel code knows the size of sockaddr_storage */
1580 msg_sys->msg_namelen = 0;
1581
1582 if (sock->file->f_flags & O_NONBLOCK)
1583 flags |= MSG_DONTWAIT;
1584 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
1585 total_len, flags);
1586 if (err < 0)
1587 goto out_freeiov;
1588 len = err;
1589
1590 if (uaddr != NULL) {
1591 err = mymove_addr_to_user(&addr,
1592 msg_sys->msg_namelen, uaddr,
1593 uaddr_len);
1594 if (err < 0)
1595 goto out_freeiov;
1596 }
1597 err = myput_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
1598 COMPAT_FLAGS(msg));
1599 if (err)
1600 goto out_freeiov;
1601 if (MSG_CMSG_COMPAT & flags)
1602 err = myput_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
1603 &msg_compat->msg_controllen);
1604 else
1605 err = myput_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
1606 &msg->msg_controllen);
1607 if (err)
1608 goto out_freeiov;
1609 err = len;
1610
1611out_freeiov:
1612 if (iov != iovstack)
1613 sock_kfree_s(sock->sk, iov, iov_size);
1614out:
1615 return err;
1616}
1617
1618long __sys_myrecvmsg(int fd, struct msghdr __user *msg, unsigned flags)
1619{
1620 int fput_needed, err;
1621 struct msghdr msg_sys;
1622 struct socket *sock;
1623
1624 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1625 if (!sock)
1626 goto out;
1627
1628 err = ___sys_myrecvmsg(sock, msg, &msg_sys, flags, 0);
1629
1630 fput_light(sock->file, fput_needed);
1631out:
1632 return err;
1633}
1634
1635SYSCALL_DEFINE3(myrecvmsg, int, fd, struct msghdr __user *, msg,
1636 unsigned int, flags)
1637{
1638 if (flags & MSG_CMSG_COMPAT)
1639 return -EINVAL;
1640 return __sys_myrecvmsg(fd, msg, flags);
1641}
1642
1643
1644
1645int __sys_myrecvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
1646 unsigned int flags, struct timespec *timeout)
1647{
1648 int fput_needed, err, datagrams;
1649 struct socket *sock;
1650 struct mmsghdr __user *entry;
1651 struct compat_mmsghdr __user *compat_entry;
1652 struct msghdr msg_sys;
1653 struct timespec end_time;
1654
1655 if (timeout &&
1656 poll_select_set_timeout(&end_time, timeout->tv_sec,
1657 timeout->tv_nsec))
1658 return -EINVAL;
1659
1660 datagrams = 0;
1661
1662 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1663 if (!sock)
1664 return err;
1665
1666 err = sock_error(sock->sk);
1667 if (err)
1668 goto out_put;
1669
1670 entry = mmsg;
1671 compat_entry = (struct compat_mmsghdr __user *)mmsg;
1672
1673 while (datagrams < vlen) {
1674 /*
1675 * No need to ask LSM for more than the first datagram.
1676 */
1677 if (MSG_CMSG_COMPAT & flags) {
1678 err = ___sys_myrecvmsg(sock, (struct msghdr __user *)compat_entry,
1679 &msg_sys, flags & ~MSG_WAITFORONE,
1680 datagrams);
1681 if (err < 0)
1682 break;
1683 err = myput_user(err, &compat_entry->msg_len);
1684 ++compat_entry;
1685 } else {
1686 err = ___sys_myrecvmsg(sock,
1687 (struct msghdr __user *)entry,
1688 &msg_sys, flags & ~MSG_WAITFORONE,
1689 datagrams);
1690 if (err < 0)
1691 break;
1692 err = myput_user(err, &entry->msg_len);
1693 ++entry;
1694 }
1695
1696 if (err)
1697 break;
1698 ++datagrams;
1699
1700 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
1701 if (flags & MSG_WAITFORONE)
1702 flags |= MSG_DONTWAIT;
1703
1704 if (timeout) {
1705 ktime_get_ts(timeout);
1706 *timeout = timespec_sub(end_time, *timeout);
1707 if (timeout->tv_sec < 0) {
1708 timeout->tv_sec = timeout->tv_nsec = 0;
1709 break;
1710 }
1711
1712 /* Timeout, return less than vlen datagrams */
1713 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
1714 break;
1715 }
1716
1717 /* Out of band data, return right away */
1718 if (msg_sys.msg_flags & MSG_OOB)
1719 break;
1720 }
1721
1722out_put:
1723 fput_light(sock->file, fput_needed);
1724
1725 if (err == 0)
1726 return datagrams;
1727
1728 if (datagrams != 0) {
1729 /*
1730 * We may return less entries than requested (vlen) if the
1731 * sock is non block and there aren't enough datagrams...
1732 */
1733 if (err != -EAGAIN) {
1734 /*
1735 * ... or if recvmsg returns an error after we
1736 * received some datagrams, where we record the
1737 * error to return on the next call or if the
1738 * app asks about it using getsockopt(SO_ERROR).
1739 */
1740 sock->sk->sk_err = -err;
1741 }
1742
1743 return datagrams;
1744 }
1745
1746 return err;
1747}
1748
1749
1750SYSCALL_DEFINE5(myrecvmmsg, int, fd, struct mmsghdr __user *, mmsg,
1751 unsigned int, vlen, unsigned int, flags,
1752 struct timespec __user *, timeout)
1753{
1754 int datagrams;
1755 struct timespec timeout_sys;
1756
1757 if (flags & MSG_CMSG_COMPAT)
1758 return -EINVAL;
1759
1760 if (!timeout)
1761 return __sys_myrecvmmsg(fd, mmsg, vlen, flags, NULL);
1762
1763 memcpy(&timeout_sys, timeout, sizeof(timeout_sys));
1764 datagrams = __sys_myrecvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
1765
1766 if (datagrams > 0 &&
1767 !memcpy(timeout, &timeout_sys, sizeof(timeout_sys)))
1768 datagrams = -EFAULT;
1769
1770 return datagrams;
1771}
1772
1773
1774
1775/*
1776 * BSD sendmsg interface
1777 */
1778
1779
1780SYSCALL_DEFINE3(mybind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
1781{
1782 struct socket *sock;
1783 struct sockaddr_storage address;
1784 int err, fput_needed;
1785
1786 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1787 if (sock) {
1788 err = mymove_addr_to_kernel(umyaddr, addrlen, &address);
1789 if (err >= 0) {
1790 err = security_socket_bind(sock,
1791 (struct sockaddr *)&address,
1792 addrlen);
1793 if (!err)
1794 err = sock->ops->bind(sock,
1795 (struct sockaddr *)
1796 &address, addrlen);
1797 }
1798 fput_light(sock->file, fput_needed);
1799 }
1800 return err;
1801}
1802EXPORT_SYMBOL(sys_mybind);
1803
1804
1805SYSCALL_DEFINE3(myconnect, int, fd, struct sockaddr __user *, uservaddr,
1806 int, addrlen)
1807{
1808 struct socket *sock;
1809 struct sockaddr_storage address;
1810 int err, fput_needed;
1811
1812 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1813 if (!sock)
1814 {
1815 print_sun(SUN_DBG,"1:===>\n");
1816 goto out;
1817 }
1818 err = mymove_addr_to_kernel(uservaddr, addrlen, &address);
1819 print_sun(SUN_DBG,"2:===>%d\n", err);
1820 if (err < 0)
1821 goto out_put;
1822
1823 err =
1824 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
1825 if (err)
1826 goto out_put;
1827
1828 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
1829 sock->file->f_flags);
1830out_put:
1831 fput_light(sock->file, fput_needed);
1832out:
1833 return err;
1834}
1835EXPORT_SYMBOL(sys_myconnect);
1836
1837SYSCALL_DEFINE6(mysendto, int, fd, void __user *, buff, size_t, len,
1838 unsigned, flags, struct sockaddr __user *, addr,
1839 int, addr_len)
1840{
1841 struct socket *sock;
1842 struct sockaddr_storage address;
1843 int err;
1844 struct msghdr msg;
1845 struct iovec iov;
1846 int fput_needed;
1847
1848 if (len > INT_MAX)
1849 len = INT_MAX;
1850 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1851 if (!sock)
1852 goto out;
1853
1854 iov.iov_base = buff;
1855 iov.iov_len = len;
1856 msg.msg_name = NULL;
1857 msg.msg_iov = &iov;
1858 msg.msg_iovlen = 1;
1859 msg.msg_control = NULL;
1860 msg.msg_controllen = 0;
1861 msg.msg_namelen = 0;
1862 if (addr) {
1863 err = mymove_addr_to_kernel(addr, addr_len, &address);
1864 print_sun(SUN_DBG,"[comlee]:sendito=1=>%d\n", err);
1865 if (err < 0)
1866 goto out_put;
1867 msg.msg_name = (struct sockaddr *)&address;
1868 msg.msg_namelen = addr_len;
1869 }
1870 if (sock->file->f_flags & O_NONBLOCK)
1871 flags |= MSG_DONTWAIT;
1872 msg.msg_flags = flags;
1873 err = sock_sendmsg(sock, &msg, len);
1874
1875out_put:
1876 fput_light(sock->file, fput_needed);
1877out:
1878 return err;
1879}
1880
1881
1882SYSCALL_DEFINE4(myaccept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
1883 int __user *, upeer_addrlen, int, flags)
1884{
1885 struct socket *sock, *newsock;
1886 struct file *newfile;
1887 int err, len, newfd, fput_needed;
1888 struct sockaddr_storage address;
1889
1890 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
1891 return -EINVAL;
1892
1893 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
1894 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
1895
1896 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1897 if (!sock)
1898 goto out;
1899
1900 err = -ENFILE;
1901 newsock = sock_alloc();
1902 if (!newsock)
1903 goto out_put;
1904
1905 newsock->type = sock->type;
1906 newsock->ops = sock->ops;
1907
1908 /*
1909 * We don't need try_module_get here, as the listening socket (sock)
1910 * has the protocol module (sock->ops->owner) held.
1911 */
1912 __module_get(newsock->ops->owner);
1913
1914 newfd = sock_alloc_file(newsock, &newfile, flags);
1915 if (unlikely(newfd < 0)) {
1916 err = newfd;
1917 sock_release(newsock);
1918 goto out_put;
1919 }
1920
1921 err = security_socket_accept(sock, newsock);
1922 if (err)
1923 goto out_fd;
1924
1925 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
1926 if (err < 0)
1927 goto out_fd;
1928
1929 if (upeer_sockaddr) {
1930 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1931 &len, 2) < 0) {
1932 err = -ECONNABORTED;
1933 goto out_fd;
1934 }
1935 err = mymove_addr_to_user(&address,
1936 len, upeer_sockaddr, upeer_addrlen);
1937 if (err < 0)
1938 goto out_fd;
1939 }
1940
1941 /* File flags are not inherited via accept() unlike another OSes. */
1942
1943 fd_install(newfd, newfile);
1944 err = newfd;
1945
1946out_put:
1947 fput_light(sock->file, fput_needed);
1948out:
1949 return err;
1950out_fd:
1951 fput(newfile);
1952 put_unused_fd(newfd);
1953 goto out_put;
1954}
1955
1956
1957SYSCALL_DEFINE3(myaccept, int, fd, struct sockaddr __user *, upeer_sockaddr,
1958 int __user *, upeer_addrlen)
1959{
1960 return sys_myaccept4(fd, upeer_sockaddr, upeer_addrlen, 0);
1961}
1962EXPORT_SYMBOL(sys_myaccept);
1963
1964SYSCALL_DEFINE3(mygetsockname, int, fd, struct sockaddr __user *, usockaddr,
1965 int __user *, usockaddr_len)
1966{
1967 struct socket *sock;
1968 struct sockaddr_storage address;
1969 int len, err, fput_needed;
1970
1971 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1972 if (!sock)
1973 goto out;
1974
1975 err = security_socket_getsockname(sock);
1976 if (err)
1977 goto out_put;
1978
1979 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
1980 if (err)
1981 goto out_put;
1982 err = mymove_addr_to_user(&address, len, usockaddr, usockaddr_len);
1983
1984out_put:
1985 fput_light(sock->file, fput_needed);
1986out:
1987 return err;
1988}
1989
1990
1991SYSCALL_DEFINE3(mygetpeername, int, fd, struct sockaddr __user *, usockaddr,
1992 int __user *, usockaddr_len)
1993{
1994 struct socket *sock;
1995 struct sockaddr_storage address;
1996 int len, err, fput_needed;
1997
1998 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1999 if (sock != NULL) {
2000 err = security_socket_getpeername(sock);
2001 if (err) {
2002 fput_light(sock->file, fput_needed);
2003 return err;
2004 }
2005
2006 err =
2007 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
2008 1);
2009 if (!err)
2010 err = mymove_addr_to_user(&address, len, usockaddr,
2011 usockaddr_len);
2012 fput_light(sock->file, fput_needed);
2013 }
2014 return err;
2015}
2016
2017
2018SYSCALL_DEFINE6(myrecvfrom, int, fd, void __user *, ubuf, size_t, size,
2019 unsigned, flags, struct sockaddr __user *, addr,
2020 int __user *, addr_len)
2021{
2022 struct socket *sock;
2023 struct iovec iov;
2024 struct msghdr msg;
2025 struct sockaddr_storage address;
2026 int err, err2;
2027 int fput_needed;
2028
2029 if (size > INT_MAX)
2030 size = INT_MAX;
2031 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2032 if (!sock)
2033 goto out;
2034
2035 msg.msg_control = NULL;
2036 msg.msg_controllen = 0;
2037 msg.msg_iovlen = 1;
2038 msg.msg_iov = &iov;
2039 iov.iov_len = size;
2040 iov.iov_base = ubuf;
2041 /* Save some cycles and don't copy the address if not needed */
2042 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
2043 /* We assume all kernel code knows the size of sockaddr_storage */
2044 msg.msg_namelen = 0;
2045 if (sock->file->f_flags & O_NONBLOCK)
2046 flags |= MSG_DONTWAIT;
2047 err = sock_recvmsg(sock, &msg, size, flags);
2048
2049 if (err >= 0 && addr != NULL) {
2050 err2 = mymove_addr_to_user(&address,
2051 msg.msg_namelen, addr, addr_len);
2052 if (err2 < 0)
2053 err = err2;
2054 }
2055
2056 fput_light(sock->file, fput_needed);
2057out:
2058 return err;
2059}
2060
2061/////////////////////////////////////////////////////
2062/*
2063 * Create a pair of connected sockets.
2064 */
2065
2066SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
2067 int __user *, usockvec)
2068{
2069 struct socket *sock1, *sock2;
2070 int fd1, fd2, err;
2071 struct file *newfile1, *newfile2;
2072 int flags;
2073
2074 flags = type & ~SOCK_TYPE_MASK;
2075 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
2076 return -EINVAL;
2077 type &= SOCK_TYPE_MASK;
2078
2079 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
2080 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
2081
2082 /*
2083 * Obtain the first socket and check if the underlying protocol
2084 * supports the socketpair call.
2085 */
2086
2087 err = sock_create(family, type, protocol, &sock1);
2088 if (err < 0)
2089 goto out;
2090
2091 err = sock_create(family, type, protocol, &sock2);
2092 if (err < 0)
2093 goto out_release_1;
2094
2095 err = sock1->ops->socketpair(sock1, sock2);
2096 if (err < 0)
2097 goto out_release_both;
2098
2099 fd1 = sock_alloc_file(sock1, &newfile1, flags);
2100 if (unlikely(fd1 < 0)) {
2101 err = fd1;
2102 goto out_release_both;
2103 }
2104
2105 fd2 = sock_alloc_file(sock2, &newfile2, flags);
2106 if (unlikely(fd2 < 0)) {
2107 err = fd2;
2108 fput(newfile1);
2109 put_unused_fd(fd1);
2110 sock_release(sock2);
2111 goto out;
2112 }
2113
2114 audit_fd_pair(fd1, fd2);
2115 fd_install(fd1, newfile1);
2116 fd_install(fd2, newfile2);
2117 /* fd1 and fd2 may be already another descriptors.
2118 * Not kernel problem.
2119 */
2120
2121 err = put_user(fd1, &usockvec[0]);
2122 if (!err)
2123 err = put_user(fd2, &usockvec[1]);
2124 if (!err)
2125 return 0;
2126
2127 sys_close(fd2);
2128 sys_close(fd1);
2129 return err;
2130
2131out_release_both:
2132 sock_release(sock2);
2133out_release_1:
2134 sock_release(sock1);
2135out:
2136 return err;
2137}
2138
2139/*
2140 * Bind a name to a socket. Nothing much to do here since it's
2141 * the protocol's responsibility to handle the local address.
2142 *
2143 * We move the socket address to kernel space before we call
2144 * the protocol layer (having also checked the address is ok).
2145 */
2146
2147SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
2148{
2149 struct socket *sock;
2150 struct sockaddr_storage address;
2151 int err, fput_needed;
2152
2153 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2154 if (sock) {
2155 err = move_addr_to_kernel(umyaddr, addrlen, &address);
2156 if (err >= 0) {
2157 err = security_socket_bind(sock,
2158 (struct sockaddr *)&address,
2159 addrlen);
2160 if (!err)
2161 err = sock->ops->bind(sock,
2162 (struct sockaddr *)
2163 &address, addrlen);
2164 }
2165 fput_light(sock->file, fput_needed);
2166 }
2167 return err;
2168}
2169
2170/*
2171 * Perform a listen. Basically, we allow the protocol to do anything
2172 * necessary for a listen, and if that works, we mark the socket as
2173 * ready for listening.
2174 */
2175
2176SYSCALL_DEFINE2(listen, int, fd, int, backlog)
2177{
2178 struct socket *sock;
2179 int err, fput_needed;
2180 int somaxconn;
2181
2182 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2183 if (sock) {
2184 somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
2185 if ((unsigned)backlog > somaxconn)
2186 backlog = somaxconn;
2187
2188 err = security_socket_listen(sock, backlog);
2189 if (!err)
2190 err = sock->ops->listen(sock, backlog);
2191
2192 fput_light(sock->file, fput_needed);
2193 }
2194 return err;
2195}
2196EXPORT_SYMBOL(sys_listen);
2197
2198/*
2199 * For accept, we attempt to create a new socket, set up the link
2200 * with the client, wake up the client, then return the new
2201 * connected fd. We collect the address of the connector in kernel
2202 * space and move it to user at the very end. This is unclean because
2203 * we open the socket then return an error.
2204 *
2205 * 1003.1g adds the ability to recvmsg() to query connection pending
2206 * status to recvmsg. We need to add that support in a way thats
2207 * clean when we restucture accept also.
2208 */
2209
2210SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
2211 int __user *, upeer_addrlen, int, flags)
2212{
2213 struct socket *sock, *newsock;
2214 struct file *newfile;
2215 int err, len, newfd, fput_needed;
2216 struct sockaddr_storage address;
2217
2218 if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
2219 return -EINVAL;
2220
2221 if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
2222 flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
2223
2224 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2225 if (!sock)
2226 goto out;
2227
2228 err = -ENFILE;
2229 newsock = sock_alloc();
2230 if (!newsock)
2231 goto out_put;
2232
2233 newsock->type = sock->type;
2234 newsock->ops = sock->ops;
2235
2236 /*
2237 * We don't need try_module_get here, as the listening socket (sock)
2238 * has the protocol module (sock->ops->owner) held.
2239 */
2240 __module_get(newsock->ops->owner);
2241
2242 newfd = sock_alloc_file(newsock, &newfile, flags);
2243 if (unlikely(newfd < 0)) {
2244 err = newfd;
2245 sock_release(newsock);
2246 goto out_put;
2247 }
2248
2249 err = security_socket_accept(sock, newsock);
2250 if (err)
2251 goto out_fd;
2252
2253 err = sock->ops->accept(sock, newsock, sock->file->f_flags);
2254 if (err < 0)
2255 goto out_fd;
2256
2257 if (upeer_sockaddr) {
2258 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
2259 &len, 2) < 0) {
2260 err = -ECONNABORTED;
2261 goto out_fd;
2262 }
2263 err = move_addr_to_user(&address,
2264 len, upeer_sockaddr, upeer_addrlen);
2265 if (err < 0)
2266 goto out_fd;
2267 }
2268
2269 /* File flags are not inherited via accept() unlike another OSes. */
2270
2271 fd_install(newfd, newfile);
2272 err = newfd;
2273
2274out_put:
2275 fput_light(sock->file, fput_needed);
2276out:
2277 return err;
2278out_fd:
2279 fput(newfile);
2280 put_unused_fd(newfd);
2281 goto out_put;
2282}
2283
2284SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
2285 int __user *, upeer_addrlen)
2286{
2287 return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
2288}
2289
2290/*
2291 * Attempt to connect to a socket with the server address. The address
2292 * is in user space so we verify it is OK and move it to kernel space.
2293 *
2294 * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
2295 * break bindings
2296 *
2297 * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
2298 * other SEQPACKET protocols that take time to connect() as it doesn't
2299 * include the -EINPROGRESS status for such sockets.
2300 */
2301
2302SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
2303 int, addrlen)
2304{
2305 struct socket *sock;
2306 struct sockaddr_storage address;
2307 int err, fput_needed;
2308
2309 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2310 if (!sock)
2311 goto out;
2312 err = move_addr_to_kernel(uservaddr, addrlen, &address);
2313 if (err < 0)
2314 goto out_put;
2315
2316 err =
2317 security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
2318 if (err)
2319 goto out_put;
2320
2321 err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
2322 sock->file->f_flags);
2323out_put:
2324 fput_light(sock->file, fput_needed);
2325out:
2326 return err;
2327}
2328
2329/*
2330 * Get the local address ('name') of a socket object. Move the obtained
2331 * name to user space.
2332 */
2333
2334SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
2335 int __user *, usockaddr_len)
2336{
2337 struct socket *sock;
2338 struct sockaddr_storage address;
2339 int len, err, fput_needed;
2340
2341 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2342 if (!sock)
2343 goto out;
2344
2345 err = security_socket_getsockname(sock);
2346 if (err)
2347 goto out_put;
2348
2349 err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
2350 if (err)
2351 goto out_put;
2352 err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
2353
2354out_put:
2355 fput_light(sock->file, fput_needed);
2356out:
2357 return err;
2358}
2359
2360/*
2361 * Get the remote address ('name') of a socket object. Move the obtained
2362 * name to user space.
2363 */
2364
2365SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
2366 int __user *, usockaddr_len)
2367{
2368 struct socket *sock;
2369 struct sockaddr_storage address;
2370 int len, err, fput_needed;
2371
2372 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2373 if (sock != NULL) {
2374 err = security_socket_getpeername(sock);
2375 if (err) {
2376 fput_light(sock->file, fput_needed);
2377 return err;
2378 }
2379
2380 err =
2381 sock->ops->getname(sock, (struct sockaddr *)&address, &len,
2382 1);
2383 if (!err)
2384 err = move_addr_to_user(&address, len, usockaddr,
2385 usockaddr_len);
2386 fput_light(sock->file, fput_needed);
2387 }
2388 return err;
2389}
2390
2391/*
2392 * Send a datagram to a given address. We move the address into kernel
2393 * space and check the user space data area is readable before invoking
2394 * the protocol.
2395 */
2396
2397SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
2398 unsigned, flags, struct sockaddr __user *, addr,
2399 int, addr_len)
2400{
2401 struct socket *sock;
2402 struct sockaddr_storage address;
2403 int err;
2404 struct msghdr msg;
2405 struct iovec iov;
2406 int fput_needed;
2407
2408 if (len > INT_MAX)
2409 len = INT_MAX;
2410 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2411 if (!sock)
2412 goto out;
2413
2414 iov.iov_base = buff;
2415 iov.iov_len = len;
2416 msg.msg_name = NULL;
2417 msg.msg_iov = &iov;
2418 msg.msg_iovlen = 1;
2419 msg.msg_control = NULL;
2420 msg.msg_controllen = 0;
2421 msg.msg_namelen = 0;
2422 if (addr) {
2423 err = move_addr_to_kernel(addr, addr_len, &address);
2424 if (err < 0)
2425 goto out_put;
2426 msg.msg_name = (struct sockaddr *)&address;
2427 msg.msg_namelen = addr_len;
2428 }
2429 if (sock->file->f_flags & O_NONBLOCK)
2430 flags |= MSG_DONTWAIT;
2431 msg.msg_flags = flags;
2432 err = sock_sendmsg(sock, &msg, len);
2433
2434out_put:
2435 fput_light(sock->file, fput_needed);
2436out:
2437 return err;
2438}
2439
2440/*
2441 * Send a datagram down a socket.
2442 */
2443
2444SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
2445 unsigned, flags)
2446{
2447 return sys_sendto(fd, buff, len, flags, NULL, 0);
2448}
2449
2450SYSCALL_DEFINE4(mysend, int, fd, void __user *, buff, size_t, len,
2451 unsigned, flags)
2452{
2453 return sys_mysendto(fd, buff, len, flags, NULL, 0);
2454}
2455EXPORT_SYMBOL(sys_mysend);
2456/*
2457 * Receive a frame from the socket and optionally record the address of the
2458 * sender. We verify the buffers are writable and if needed move the
2459 * sender address from kernel to user space.
2460 */
2461
2462SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
2463 unsigned, flags, struct sockaddr __user *, addr,
2464 int __user *, addr_len)
2465{
2466 struct socket *sock;
2467 struct iovec iov;
2468 struct msghdr msg;
2469 struct sockaddr_storage address;
2470 int err, err2;
2471 int fput_needed;
2472
2473 if (size > INT_MAX)
2474 size = INT_MAX;
2475 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2476 if (!sock)
2477 goto out;
2478
2479 msg.msg_control = NULL;
2480 msg.msg_controllen = 0;
2481 msg.msg_iovlen = 1;
2482 msg.msg_iov = &iov;
2483 iov.iov_len = size;
2484 iov.iov_base = ubuf;
2485 /* Save some cycles and don't copy the address if not needed */
2486 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
2487 /* We assume all kernel code knows the size of sockaddr_storage */
2488 msg.msg_namelen = 0;
2489 if (sock->file->f_flags & O_NONBLOCK)
2490 flags |= MSG_DONTWAIT;
2491 err = sock_recvmsg(sock, &msg, size, flags);
2492
2493 if (err >= 0 && addr != NULL) {
2494 err2 = move_addr_to_user(&address,
2495 msg.msg_namelen, addr, addr_len);
2496 if (err2 < 0)
2497 err = err2;
2498 }
2499
2500 fput_light(sock->file, fput_needed);
2501out:
2502 return err;
2503}
2504
2505/*
2506 * Receive a datagram from a socket.
2507 */
2508
2509asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
2510 unsigned flags)
2511{
2512 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
2513}
2514
2515/*
2516 * Set a socket option. Because we don't know the option lengths we have
2517 * to pass the user mode parameter for the protocols to sort out.
2518 */
2519
2520SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
2521 char __user *, optval, int, optlen)
2522{
2523 int err, fput_needed;
2524 struct socket *sock;
2525
2526 if (optlen < 0)
2527 return -EINVAL;
2528
2529 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2530 if (sock != NULL) {
2531 err = security_socket_setsockopt(sock, level, optname);
2532 if (err)
2533 goto out_put;
2534
2535 if (level == SOL_SOCKET)
2536 err =
2537 sock_setsockopt(sock, level, optname, optval,
2538 optlen);
2539 else
2540 err =
2541 sock->ops->setsockopt(sock, level, optname, optval,
2542 optlen);
2543out_put:
2544 fput_light(sock->file, fput_needed);
2545 }
2546 return err;
2547}
2548
2549/*
2550 * Get a socket option. Because we don't know the option lengths we have
2551 * to pass a user mode parameter for the protocols to sort out.
2552 */
2553
2554SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
2555 char __user *, optval, int __user *, optlen)
2556{
2557 int err, fput_needed;
2558 struct socket *sock;
2559
2560 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2561 if (sock != NULL) {
2562 err = security_socket_getsockopt(sock, level, optname);
2563 if (err)
2564 goto out_put;
2565
2566 if (level == SOL_SOCKET)
2567 err =
2568 sock_getsockopt(sock, level, optname, optval,
2569 optlen);
2570 else
2571 err =
2572 sock->ops->getsockopt(sock, level, optname, optval,
2573 optlen);
2574out_put:
2575 fput_light(sock->file, fput_needed);
2576 }
2577 return err;
2578}
2579
2580/*
2581 * Shutdown a socket.
2582 */
2583
2584SYSCALL_DEFINE2(shutdown, int, fd, int, how)
2585{
2586 int err, fput_needed;
2587 struct socket *sock;
2588
2589 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2590 if (sock != NULL) {
2591 err = security_socket_shutdown(sock, how);
2592 if (!err)
2593 err = sock->ops->shutdown(sock, how);
2594 fput_light(sock->file, fput_needed);
2595 }
2596 return err;
2597}
2598
2599/* A couple of helpful macros for getting the address of the 32/64 bit
2600 * fields which are the same type (int / unsigned) on our platforms.
2601 */
2602
2603struct used_address {
2604 struct sockaddr_storage name;
2605 unsigned int name_len;
2606};
2607
2608static int copy_msghdr_from_user(struct msghdr *kmsg,
2609 struct msghdr __user *umsg)
2610{
2611 if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
2612 return -EFAULT;
2613
2614 if (kmsg->msg_name == NULL)
2615 kmsg->msg_namelen = 0;
2616
2617 if (kmsg->msg_namelen < 0)
2618 return -EINVAL;
2619
2620 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
2621 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
2622 return 0;
2623}
2624
2625static int ___sys_mysendmsg(struct socket *sock, struct msghdr __user *msg,
2626 struct msghdr *msg_sys, unsigned flags,
2627 struct used_address *used_address)
2628{
2629 struct compat_msghdr __user *msg_compat =
2630 (struct compat_msghdr __user *)msg;
2631 struct sockaddr_storage address;
2632 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2633 unsigned char ctl[sizeof(struct cmsghdr) + 20]
2634 __attribute__ ((aligned(sizeof(__kernel_size_t))));
2635 /* 20 is size of ipv6_pktinfo */
2636 unsigned char *ctl_buf = ctl;
2637 int err, ctl_len, iov_size, total_len;
2638
2639 err = -EFAULT;
2640 if (MSG_CMSG_COMPAT & flags)
2641 err = get_compat_msghdr(msg_sys, msg_compat);
2642 else
2643 err = mycopy_msghdr_from_user(msg_sys, msg);
2644 if (err)
2645 return err;
2646
2647 /* do not move before msg_sys is valid */
2648 err = -EMSGSIZE;
2649 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2650 goto out;
2651
2652 /* Check whether to allocate the iovec area */
2653 err = -ENOMEM;
2654 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
2655 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
2656 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
2657 if (!iov)
2658 goto out;
2659 }
2660
2661 /* This will also move the address data into kernel space */
2662 if (MSG_CMSG_COMPAT & flags) {
2663 err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
2664 } else
2665 err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
2666 if (err < 0)
2667 goto out_freeiov;
2668 total_len = err;
2669
2670 err = -ENOBUFS;
2671
2672 if (msg_sys->msg_controllen > INT_MAX)
2673 goto out_freeiov;
2674 ctl_len = msg_sys->msg_controllen;
2675 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2676 err =
2677 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2678 sizeof(ctl));
2679 if (err)
2680 goto out_freeiov;
2681 ctl_buf = msg_sys->msg_control;
2682 ctl_len = msg_sys->msg_controllen;
2683 } else if (ctl_len) {
2684 if (ctl_len > sizeof(ctl)) {
2685 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2686 if (ctl_buf == NULL)
2687 goto out_freeiov;
2688 }
2689 err = -EFAULT;
2690 /*
2691 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2692 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2693 * checking falls down on this.
2694 */
2695 if (!memcpy(ctl_buf,
2696 (void __user __force *)msg_sys->msg_control,
2697 ctl_len))
2698 goto out_freectl;
2699 msg_sys->msg_control = ctl_buf;
2700 }
2701 msg_sys->msg_flags = flags;
2702
2703 if (sock->file->f_flags & O_NONBLOCK)
2704 msg_sys->msg_flags |= MSG_DONTWAIT;
2705 /*
2706 * If this is sendmmsg() and current destination address is same as
2707 * previously succeeded address, omit asking LSM's decision.
2708 * used_address->name_len is initialized to UINT_MAX so that the first
2709 * destination address never matches.
2710 */
2711 if (used_address && msg_sys->msg_name &&
2712 used_address->name_len == msg_sys->msg_namelen &&
2713 !memcmp(&used_address->name, msg_sys->msg_name,
2714 used_address->name_len)) {
2715 err = sock_sendmsg_nosec(sock, msg_sys, total_len);
2716 goto out_freectl;
2717 }
2718 err = sock_sendmsg(sock, msg_sys, total_len);
2719 /*
2720 * If this is sendmmsg() and sending to current destination address was
2721 * successful, remember it.
2722 */
2723 if (used_address && err >= 0) {
2724 used_address->name_len = msg_sys->msg_namelen;
2725 if (msg_sys->msg_name)
2726 memcpy(&used_address->name, msg_sys->msg_name,
2727 used_address->name_len);
2728 }
2729
2730out_freectl:
2731 if (ctl_buf != ctl)
2732 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2733out_freeiov:
2734 if (iov != iovstack)
2735 sock_kfree_s(sock->sk, iov, iov_size);
2736out:
2737 return err;
2738}
2739
2740int __sys_mysendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2741 unsigned int flags)
2742{
2743 int fput_needed, err, datagrams;
2744 struct socket *sock;
2745 struct mmsghdr __user *entry;
2746 struct compat_mmsghdr __user *compat_entry;
2747 struct msghdr msg_sys;
2748 struct used_address used_address;
2749
2750 if (vlen > UIO_MAXIOV)
2751 vlen = UIO_MAXIOV;
2752
2753 datagrams = 0;
2754
2755 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2756 if (!sock)
2757 return err;
2758
2759 used_address.name_len = UINT_MAX;
2760 entry = mmsg;
2761 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2762 err = 0;
2763
2764 while (datagrams < vlen) {
2765 if (MSG_CMSG_COMPAT & flags) {
2766 err = ___sys_mysendmsg(sock, (struct msghdr __user *)compat_entry,
2767 &msg_sys, flags, &used_address);
2768 if (err < 0)
2769 break;
2770 err = myput_user(err, &compat_entry->msg_len);
2771 ++compat_entry;
2772 } else {
2773 err = ___sys_mysendmsg(sock,
2774 (struct msghdr __user *)entry,
2775 &msg_sys, flags, &used_address);
2776 if (err < 0)
2777 break;
2778 err = myput_user(err, &entry->msg_len);
2779 ++entry;
2780 }
2781
2782 if (err)
2783 break;
2784 ++datagrams;
2785 }
2786
2787 fput_light(sock->file, fput_needed);
2788
2789 /* We only return an error if no datagrams were able to be sent */
2790 if (datagrams != 0)
2791 return datagrams;
2792
2793 return err;
2794}
2795
2796
2797SYSCALL_DEFINE4(mysendmmsg, int, fd, struct mmsghdr __user *, mmsg,
2798 unsigned int, vlen, unsigned int, flags)
2799{
2800 if (flags & MSG_CMSG_COMPAT)
2801 return -EINVAL;
2802 return __sys_mysendmmsg(fd, mmsg, vlen, flags);
2803}
2804
2805long __sys_mysendmsg(int fd, struct msghdr __user *msg, unsigned flags)
2806{
2807 int fput_needed, err;
2808 struct msghdr msg_sys;
2809 struct socket *sock;
2810
2811 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2812 if (!sock)
2813 goto out;
2814
2815 err = ___sys_mysendmsg(sock, msg, &msg_sys, flags, NULL);
2816
2817 fput_light(sock->file, fput_needed);
2818out:
2819 return err;
2820}
2821
2822SYSCALL_DEFINE3(mysendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
2823{
2824 if (flags & MSG_CMSG_COMPAT)
2825 return -EINVAL;
2826 return __sys_mysendmsg(fd, msg, flags);
2827}
2828
2829static int ___sys_sendmsg(struct socket *sock, struct msghdr __user *msg,
2830 struct msghdr *msg_sys, unsigned flags,
2831 struct used_address *used_address)
2832{
2833 struct compat_msghdr __user *msg_compat =
2834 (struct compat_msghdr __user *)msg;
2835 struct sockaddr_storage address;
2836 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
2837 unsigned char ctl[sizeof(struct cmsghdr) + 20]
2838 __attribute__ ((aligned(sizeof(__kernel_size_t))));
2839 /* 20 is size of ipv6_pktinfo */
2840 unsigned char *ctl_buf = ctl;
2841 int err, ctl_len, iov_size, total_len;
2842
2843 err = -EFAULT;
2844 if (MSG_CMSG_COMPAT & flags)
2845 err = get_compat_msghdr(msg_sys, msg_compat);
2846 else
2847 err = copy_msghdr_from_user(msg_sys, msg);
2848 if (err)
2849 return err;
2850
2851 /* do not move before msg_sys is valid */
2852 err = -EMSGSIZE;
2853 if (msg_sys->msg_iovlen > UIO_MAXIOV)
2854 goto out;
2855
2856 /* Check whether to allocate the iovec area */
2857 err = -ENOMEM;
2858 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
2859 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
2860 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
2861 if (!iov)
2862 goto out;
2863 }
2864
2865 /* This will also move the address data into kernel space */
2866 if (MSG_CMSG_COMPAT & flags) {
2867 err = verify_compat_iovec(msg_sys, iov, &address, VERIFY_READ);
2868 } else
2869 err = verify_iovec(msg_sys, iov, &address, VERIFY_READ);
2870 if (err < 0)
2871 goto out_freeiov;
2872 total_len = err;
2873
2874 err = -ENOBUFS;
2875
2876 if (msg_sys->msg_controllen > INT_MAX)
2877 goto out_freeiov;
2878 ctl_len = msg_sys->msg_controllen;
2879 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
2880 err =
2881 cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
2882 sizeof(ctl));
2883 if (err)
2884 goto out_freeiov;
2885 ctl_buf = msg_sys->msg_control;
2886 ctl_len = msg_sys->msg_controllen;
2887 } else if (ctl_len) {
2888 if (ctl_len > sizeof(ctl)) {
2889 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
2890 if (ctl_buf == NULL)
2891 goto out_freeiov;
2892 }
2893 err = -EFAULT;
2894 /*
2895 * Careful! Before this, msg_sys->msg_control contains a user pointer.
2896 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
2897 * checking falls down on this.
2898 */
2899 if (copy_from_user(ctl_buf,
2900 (void __user __force *)msg_sys->msg_control,
2901 ctl_len))
2902 goto out_freectl;
2903 msg_sys->msg_control = ctl_buf;
2904 }
2905 msg_sys->msg_flags = flags;
2906
2907 if (sock->file->f_flags & O_NONBLOCK)
2908 msg_sys->msg_flags |= MSG_DONTWAIT;
2909 /*
2910 * If this is sendmmsg() and current destination address is same as
2911 * previously succeeded address, omit asking LSM's decision.
2912 * used_address->name_len is initialized to UINT_MAX so that the first
2913 * destination address never matches.
2914 */
2915 if (used_address && msg_sys->msg_name &&
2916 used_address->name_len == msg_sys->msg_namelen &&
2917 !memcmp(&used_address->name, msg_sys->msg_name,
2918 used_address->name_len)) {
2919 err = sock_sendmsg_nosec(sock, msg_sys, total_len);
2920 goto out_freectl;
2921 }
2922 err = sock_sendmsg(sock, msg_sys, total_len);
2923 /*
2924 * If this is sendmmsg() and sending to current destination address was
2925 * successful, remember it.
2926 */
2927 if (used_address && err >= 0) {
2928 used_address->name_len = msg_sys->msg_namelen;
2929 if (msg_sys->msg_name)
2930 memcpy(&used_address->name, msg_sys->msg_name,
2931 used_address->name_len);
2932 }
2933
2934out_freectl:
2935 if (ctl_buf != ctl)
2936 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2937out_freeiov:
2938 if (iov != iovstack)
2939 sock_kfree_s(sock->sk, iov, iov_size);
2940out:
2941 return err;
2942}
2943
2944/*
2945 * BSD sendmsg interface
2946 */
2947
2948long __sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
2949{
2950 int fput_needed, err;
2951 struct msghdr msg_sys;
2952 struct socket *sock;
2953
2954 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2955 if (!sock)
2956 goto out;
2957
2958 err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
2959
2960 fput_light(sock->file, fput_needed);
2961out:
2962 return err;
2963}
2964
2965SYSCALL_DEFINE3(sendmsg, int, fd, struct msghdr __user *, msg, unsigned int, flags)
2966{
2967 if (flags & MSG_CMSG_COMPAT)
2968 return -EINVAL;
2969 return __sys_sendmsg(fd, msg, flags);
2970}
2971
2972/*
2973 * Linux sendmmsg interface
2974 */
2975
2976int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
2977 unsigned int flags)
2978{
2979 int fput_needed, err, datagrams;
2980 struct socket *sock;
2981 struct mmsghdr __user *entry;
2982 struct compat_mmsghdr __user *compat_entry;
2983 struct msghdr msg_sys;
2984 struct used_address used_address;
2985
2986 if (vlen > UIO_MAXIOV)
2987 vlen = UIO_MAXIOV;
2988
2989 datagrams = 0;
2990
2991 sock = sockfd_lookup_light(fd, &err, &fput_needed);
2992 if (!sock)
2993 return err;
2994
2995 used_address.name_len = UINT_MAX;
2996 entry = mmsg;
2997 compat_entry = (struct compat_mmsghdr __user *)mmsg;
2998 err = 0;
2999
3000 while (datagrams < vlen) {
3001 if (MSG_CMSG_COMPAT & flags) {
3002 err = ___sys_sendmsg(sock, (struct msghdr __user *)compat_entry,
3003 &msg_sys, flags, &used_address);
3004 if (err < 0)
3005 break;
3006 err = __put_user(err, &compat_entry->msg_len);
3007 ++compat_entry;
3008 } else {
3009 err = ___sys_sendmsg(sock,
3010 (struct msghdr __user *)entry,
3011 &msg_sys, flags, &used_address);
3012 if (err < 0)
3013 break;
3014 err = put_user(err, &entry->msg_len);
3015 ++entry;
3016 }
3017
3018 if (err)
3019 break;
3020 ++datagrams;
3021 }
3022
3023 fput_light(sock->file, fput_needed);
3024
3025 /* We only return an error if no datagrams were able to be sent */
3026 if (datagrams != 0)
3027 return datagrams;
3028
3029 return err;
3030}
3031
3032SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
3033 unsigned int, vlen, unsigned int, flags)
3034{
3035 if (flags & MSG_CMSG_COMPAT)
3036 return -EINVAL;
3037 return __sys_sendmmsg(fd, mmsg, vlen, flags);
3038}
3039
3040static int ___sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
3041 struct msghdr *msg_sys, unsigned flags, int nosec)
3042{
3043 struct compat_msghdr __user *msg_compat =
3044 (struct compat_msghdr __user *)msg;
3045 struct iovec iovstack[UIO_FASTIOV];
3046 struct iovec *iov = iovstack;
3047 unsigned long cmsg_ptr;
3048 int err, iov_size, total_len, len;
3049
3050 /* kernel mode address */
3051 struct sockaddr_storage addr;
3052
3053 /* user mode address pointers */
3054 struct sockaddr __user *uaddr;
3055 int __user *uaddr_len;
3056
3057 if (MSG_CMSG_COMPAT & flags)
3058 err = get_compat_msghdr(msg_sys, msg_compat);
3059 else
3060 err = copy_msghdr_from_user(msg_sys, msg);
3061 if (err)
3062 return err;
3063
3064 err = -EMSGSIZE;
3065 if (msg_sys->msg_iovlen > UIO_MAXIOV)
3066 goto out;
3067
3068 /* Check whether to allocate the iovec area */
3069 err = -ENOMEM;
3070 iov_size = msg_sys->msg_iovlen * sizeof(struct iovec);
3071 if (msg_sys->msg_iovlen > UIO_FASTIOV) {
3072 iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
3073 if (!iov)
3074 goto out;
3075 }
3076
3077 /* Save the user-mode address (verify_iovec will change the
3078 * kernel msghdr to use the kernel address space)
3079 */
3080 uaddr = (__force void __user *)msg_sys->msg_name;
3081 uaddr_len = COMPAT_NAMELEN(msg);
3082 if (MSG_CMSG_COMPAT & flags)
3083 err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
3084 else
3085 err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
3086 if (err < 0)
3087 goto out_freeiov;
3088 total_len = err;
3089
3090 cmsg_ptr = (unsigned long)msg_sys->msg_control;
3091 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
3092
3093 /* We assume all kernel code knows the size of sockaddr_storage */
3094 msg_sys->msg_namelen = 0;
3095
3096 if (sock->file->f_flags & O_NONBLOCK)
3097 flags |= MSG_DONTWAIT;
3098 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
3099 total_len, flags);
3100 if (err < 0)
3101 goto out_freeiov;
3102 len = err;
3103
3104 if (uaddr != NULL) {
3105 err = move_addr_to_user(&addr,
3106 msg_sys->msg_namelen, uaddr,
3107 uaddr_len);
3108 if (err < 0)
3109 goto out_freeiov;
3110 }
3111 err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
3112 COMPAT_FLAGS(msg));
3113 if (err)
3114 goto out_freeiov;
3115 if (MSG_CMSG_COMPAT & flags)
3116 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
3117 &msg_compat->msg_controllen);
3118 else
3119 err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
3120 &msg->msg_controllen);
3121 if (err)
3122 goto out_freeiov;
3123 err = len;
3124
3125out_freeiov:
3126 if (iov != iovstack)
3127 sock_kfree_s(sock->sk, iov, iov_size);
3128out:
3129 return err;
3130}
3131
3132/*
3133 * BSD recvmsg interface
3134 */
3135
3136long __sys_recvmsg(int fd, struct msghdr __user *msg, unsigned flags)
3137{
3138 int fput_needed, err;
3139 struct msghdr msg_sys;
3140 struct socket *sock;
3141
3142 sock = sockfd_lookup_light(fd, &err, &fput_needed);
3143 if (!sock)
3144 goto out;
3145
3146 err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
3147
3148 fput_light(sock->file, fput_needed);
3149out:
3150 return err;
3151}
3152
3153SYSCALL_DEFINE3(recvmsg, int, fd, struct msghdr __user *, msg,
3154 unsigned int, flags)
3155{
3156 if (flags & MSG_CMSG_COMPAT)
3157 return -EINVAL;
3158 return __sys_recvmsg(fd, msg, flags);
3159}
3160
3161/*
3162 * Linux recvmmsg interface
3163 */
3164
3165int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
3166 unsigned int flags, struct timespec *timeout)
3167{
3168 int fput_needed, err, datagrams;
3169 struct socket *sock;
3170 struct mmsghdr __user *entry;
3171 struct compat_mmsghdr __user *compat_entry;
3172 struct msghdr msg_sys;
3173 struct timespec end_time;
3174
3175 if (timeout &&
3176 poll_select_set_timeout(&end_time, timeout->tv_sec,
3177 timeout->tv_nsec))
3178 return -EINVAL;
3179
3180 datagrams = 0;
3181
3182 sock = sockfd_lookup_light(fd, &err, &fput_needed);
3183 if (!sock)
3184 return err;
3185
3186 err = sock_error(sock->sk);
3187 if (err)
3188 goto out_put;
3189
3190 entry = mmsg;
3191 compat_entry = (struct compat_mmsghdr __user *)mmsg;
3192
3193 while (datagrams < vlen) {
3194 /*
3195 * No need to ask LSM for more than the first datagram.
3196 */
3197 if (MSG_CMSG_COMPAT & flags) {
3198 err = ___sys_recvmsg(sock, (struct msghdr __user *)compat_entry,
3199 &msg_sys, flags & ~MSG_WAITFORONE,
3200 datagrams);
3201 if (err < 0)
3202 break;
3203 err = __put_user(err, &compat_entry->msg_len);
3204 ++compat_entry;
3205 } else {
3206 err = ___sys_recvmsg(sock,
3207 (struct msghdr __user *)entry,
3208 &msg_sys, flags & ~MSG_WAITFORONE,
3209 datagrams);
3210 if (err < 0)
3211 break;
3212 err = put_user(err, &entry->msg_len);
3213 ++entry;
3214 }
3215
3216 if (err)
3217 break;
3218 ++datagrams;
3219
3220 /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
3221 if (flags & MSG_WAITFORONE)
3222 flags |= MSG_DONTWAIT;
3223
3224 if (timeout) {
3225 ktime_get_ts(timeout);
3226 *timeout = timespec_sub(end_time, *timeout);
3227 if (timeout->tv_sec < 0) {
3228 timeout->tv_sec = timeout->tv_nsec = 0;
3229 break;
3230 }
3231
3232 /* Timeout, return less than vlen datagrams */
3233 if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
3234 break;
3235 }
3236
3237 /* Out of band data, return right away */
3238 if (msg_sys.msg_flags & MSG_OOB)
3239 break;
3240 }
3241
3242out_put:
3243 fput_light(sock->file, fput_needed);
3244
3245 if (err == 0)
3246 return datagrams;
3247
3248 if (datagrams != 0) {
3249 /*
3250 * We may return less entries than requested (vlen) if the
3251 * sock is non block and there aren't enough datagrams...
3252 */
3253 if (err != -EAGAIN) {
3254 /*
3255 * ... or if recvmsg returns an error after we
3256 * received some datagrams, where we record the
3257 * error to return on the next call or if the
3258 * app asks about it using getsockopt(SO_ERROR).
3259 */
3260 sock->sk->sk_err = -err;
3261 }
3262
3263 return datagrams;
3264 }
3265
3266 return err;
3267}
3268
3269SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
3270 unsigned int, vlen, unsigned int, flags,
3271 struct timespec __user *, timeout)
3272{
3273 int datagrams;
3274 struct timespec timeout_sys;
3275
3276 if (flags & MSG_CMSG_COMPAT)
3277 return -EINVAL;
3278
3279 if (!timeout)
3280 return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
3281
3282 if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
3283 return -EFAULT;
3284 datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
3285
3286 if (datagrams > 0 &&
3287 copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
3288 datagrams = -EFAULT;
3289
3290 return datagrams;
3291}
3292
3293#ifdef __ARCH_WANT_SYS_SOCKETCALL
3294/* Argument list sizes for sys_socketcall */
3295#define AL(x) ((x) * sizeof(unsigned long))
3296static const unsigned char nargs[21] = {
3297 AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
3298 AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
3299 AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
3300 AL(4), AL(5), AL(4)
3301};
3302
3303#undef AL
3304
3305/*
3306 * System call vectors.
3307 *
3308 * Argument checking cleaned up. Saved 20% in size.
3309 * This function doesn't need to set the kernel lock because
3310 * it is set by the callees.
3311 */
3312
3313SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
3314{
3315 unsigned long a[6];
3316 unsigned long a0, a1;
3317 int err;
3318 unsigned int len;
3319
3320 if (call < 1 || call > SYS_SENDMMSG)
3321 return -EINVAL;
3322
3323 len = nargs[call];
3324 if (len > sizeof(a))
3325 return -EINVAL;
3326
3327 /* copy_from_user should be SMP safe. */
3328 if (copy_from_user(a, args, len))
3329 return -EFAULT;
3330 audit_socketcall(nargs[call] / sizeof(unsigned long), a);
3331
3332 a0 = a[0];
3333 a1 = a[1];
3334
3335 switch (call) {
3336 case SYS_SOCKET:
3337 err = sys_socket(a0, a1, a[2]);
3338 break;
3339 case SYS_BIND:
3340 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
3341 break;
3342 case SYS_CONNECT:
3343 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
3344 break;
3345 case SYS_LISTEN:
3346 err = sys_listen(a0, a1);
3347 break;
3348 case SYS_ACCEPT:
3349 err = sys_accept4(a0, (struct sockaddr __user *)a1,
3350 (int __user *)a[2], 0);
3351 break;
3352 case SYS_GETSOCKNAME:
3353 err =
3354 sys_getsockname(a0, (struct sockaddr __user *)a1,
3355 (int __user *)a[2]);
3356 break;
3357 case SYS_GETPEERNAME:
3358 err =
3359 sys_getpeername(a0, (struct sockaddr __user *)a1,
3360 (int __user *)a[2]);
3361 break;
3362 case SYS_SOCKETPAIR:
3363 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
3364 break;
3365 case SYS_SEND:
3366 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
3367 break;
3368 case SYS_SENDTO:
3369 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
3370 (struct sockaddr __user *)a[4], a[5]);
3371 break;
3372 case SYS_RECV:
3373 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
3374 break;
3375 case SYS_RECVFROM:
3376 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
3377 (struct sockaddr __user *)a[4],
3378 (int __user *)a[5]);
3379 break;
3380 case SYS_SHUTDOWN:
3381 err = sys_shutdown(a0, a1);
3382 break;
3383 case SYS_SETSOCKOPT:
3384 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
3385 break;
3386 case SYS_GETSOCKOPT:
3387 err =
3388 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
3389 (int __user *)a[4]);
3390 break;
3391 case SYS_SENDMSG:
3392 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
3393 break;
3394 case SYS_SENDMMSG:
3395 err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
3396 break;
3397 case SYS_RECVMSG:
3398 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
3399 break;
3400 case SYS_RECVMMSG:
3401 err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
3402 (struct timespec __user *)a[4]);
3403 break;
3404 case SYS_ACCEPT4:
3405 err = sys_accept4(a0, (struct sockaddr __user *)a1,
3406 (int __user *)a[2], a[3]);
3407 break;
3408 default:
3409 err = -EINVAL;
3410 break;
3411 }
3412 return err;
3413}
3414
3415#endif /* __ARCH_WANT_SYS_SOCKETCALL */
3416
3417/**
3418 * sock_register - add a socket protocol handler
3419 * @ops: description of protocol
3420 *
3421 * This function is called by a protocol handler that wants to
3422 * advertise its address family, and have it linked into the
3423 * socket interface. The value ops->family coresponds to the
3424 * socket system call protocol family.
3425 */
3426int sock_register(const struct net_proto_family *ops)
3427{
3428 int err;
3429
3430 if (ops->family >= NPROTO) {
3431 print_sun(SUN_ERR,"protocol %d >= NPROTO(%d)\n", ops->family,
3432 NPROTO);
3433 return -ENOBUFS;
3434 }
3435
3436 spin_lock(&net_family_lock);
3437 if (rcu_dereference_protected(net_families[ops->family],
3438 lockdep_is_held(&net_family_lock)))
3439 err = -EEXIST;
3440 else {
3441 rcu_assign_pointer(net_families[ops->family], ops);
3442 err = 0;
3443 }
3444 spin_unlock(&net_family_lock);
3445
3446 print_sun(SUN_DBG,"NET: Registered protocol family %d\n", ops->family);
3447 return err;
3448}
3449EXPORT_SYMBOL(sock_register);
3450
3451/**
3452 * sock_unregister - remove a protocol handler
3453 * @family: protocol family to remove
3454 *
3455 * This function is called by a protocol handler that wants to
3456 * remove its address family, and have it unlinked from the
3457 * new socket creation.
3458 *
3459 * If protocol handler is a module, then it can use module reference
3460 * counts to protect against new references. If protocol handler is not
3461 * a module then it needs to provide its own protection in
3462 * the ops->create routine.
3463 */
3464void sock_unregister(int family)
3465{
3466 BUG_ON(family < 0 || family >= NPROTO);
3467
3468 spin_lock(&net_family_lock);
3469 RCU_INIT_POINTER(net_families[family], NULL);
3470 spin_unlock(&net_family_lock);
3471
3472 synchronize_rcu();
3473
3474 print_sun(SUN_DBG,"NET: Unregistered protocol family %d\n", family);
3475}
3476EXPORT_SYMBOL(sock_unregister);
3477
3478static int __init sock_init(void)
3479{
3480 int err;
3481
3482 /*
3483 * Initialize sock SLAB cache.
3484 */
3485
3486 sk_init();
3487
3488 /*
3489 * Initialize skbuff SLAB cache
3490 */
3491 skb_init();
3492
3493 /*
3494 * Initialize the protocols module.
3495 */
3496
3497 init_inodecache();
3498
3499 err = register_filesystem(&sock_fs_type);
3500 if (err)
3501 goto out_fs;
3502 sock_mnt = kern_mount(&sock_fs_type);
3503 if (IS_ERR(sock_mnt)) {
3504 err = PTR_ERR(sock_mnt);
3505 goto out_mount;
3506 }
3507
3508 /* The real protocol initialization is performed in later initcalls.
3509 */
3510
3511#ifdef CONFIG_NETFILTER
3512 netfilter_init();
3513#endif
3514
3515#ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
3516 skb_timestamping_init();
3517#endif
3518
3519out:
3520 return err;
3521
3522out_mount:
3523 unregister_filesystem(&sock_fs_type);
3524out_fs:
3525 goto out;
3526}
3527
3528core_initcall(sock_init); /* early initcall */
3529
3530#ifdef CONFIG_PROC_FS
3531void socket_seq_show(struct seq_file *seq)
3532{
3533 int cpu;
3534 int counter = 0;
3535
3536 for_each_possible_cpu(cpu)
3537 counter += per_cpu(sockets_in_use, cpu);
3538
3539 /* It can be negative, by the way. 8) */
3540 if (counter < 0)
3541 counter = 0;
3542
3543 seq_printf(seq, "sockets: used %d\n", counter);
3544}
3545#endif /* CONFIG_PROC_FS */
3546
3547#ifdef CONFIG_COMPAT
3548static int do_siocgstamp(struct net *net, struct socket *sock,
3549 unsigned int cmd, void __user *up)
3550{
3551 mm_segment_t old_fs = get_fs();
3552 struct timeval ktv;
3553 int err;
3554
3555 set_fs(KERNEL_DS);
3556 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
3557 set_fs(old_fs);
3558 if (!err)
3559 err = compat_put_timeval(&ktv, up);
3560
3561 return err;
3562}
3563
3564static int do_siocgstampns(struct net *net, struct socket *sock,
3565 unsigned int cmd, void __user *up)
3566{
3567 mm_segment_t old_fs = get_fs();
3568 struct timespec kts;
3569 int err;
3570
3571 set_fs(KERNEL_DS);
3572 err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
3573 set_fs(old_fs);
3574 if (!err)
3575 err = compat_put_timespec(&kts, up);
3576
3577 return err;
3578}
3579
3580static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
3581{
3582 struct ifreq __user *uifr;
3583 int err;
3584
3585 uifr = compat_alloc_user_space(sizeof(struct ifreq));
3586 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
3587 return -EFAULT;
3588
3589 err = dev_ioctl(net, SIOCGIFNAME, uifr);
3590 if (err)
3591 return err;
3592
3593 if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
3594 return -EFAULT;
3595
3596 return 0;
3597}
3598
3599static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
3600{
3601 struct compat_ifconf ifc32;
3602 struct ifconf ifc;
3603 struct ifconf __user *uifc;
3604 struct compat_ifreq __user *ifr32;
3605 struct ifreq __user *ifr;
3606 unsigned int i, j;
3607 int err;
3608
3609 if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
3610 return -EFAULT;
3611
3612 memset(&ifc, 0, sizeof(ifc));
3613 if (ifc32.ifcbuf == 0) {
3614 ifc32.ifc_len = 0;
3615 ifc.ifc_len = 0;
3616 ifc.ifc_req = NULL;
3617 uifc = compat_alloc_user_space(sizeof(struct ifconf));
3618 } else {
3619 size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
3620 sizeof(struct ifreq);
3621 uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
3622 ifc.ifc_len = len;
3623 ifr = ifc.ifc_req = (void __user *)(uifc + 1);
3624 ifr32 = compat_ptr(ifc32.ifcbuf);
3625 for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
3626 if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
3627 return -EFAULT;
3628 ifr++;
3629 ifr32++;
3630 }
3631 }
3632 if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
3633 return -EFAULT;
3634
3635 err = dev_ioctl(net, SIOCGIFCONF, uifc);
3636 if (err)
3637 return err;
3638
3639 if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
3640 return -EFAULT;
3641
3642 ifr = ifc.ifc_req;
3643 ifr32 = compat_ptr(ifc32.ifcbuf);
3644 for (i = 0, j = 0;
3645 i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
3646 i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
3647 if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
3648 return -EFAULT;
3649 ifr32++;
3650 ifr++;
3651 }
3652
3653 if (ifc32.ifcbuf == 0) {
3654 /* Translate from 64-bit structure multiple to
3655 * a 32-bit one.
3656 */
3657 i = ifc.ifc_len;
3658 i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
3659 ifc32.ifc_len = i;
3660 } else {
3661 ifc32.ifc_len = i;
3662 }
3663 if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
3664 return -EFAULT;
3665
3666 return 0;
3667}
3668
3669static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
3670{
3671 struct compat_ethtool_rxnfc __user *compat_rxnfc;
3672 bool convert_in = false, convert_out = false;
3673 size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
3674 struct ethtool_rxnfc __user *rxnfc;
3675 struct ifreq __user *ifr;
3676 u32 rule_cnt = 0, actual_rule_cnt;
3677 u32 ethcmd;
3678 u32 data;
3679 int ret;
3680
3681 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
3682 return -EFAULT;
3683
3684 compat_rxnfc = compat_ptr(data);
3685
3686 if (get_user(ethcmd, &compat_rxnfc->cmd))
3687 return -EFAULT;
3688
3689 /* Most ethtool structures are defined without padding.
3690 * Unfortunately struct ethtool_rxnfc is an exception.
3691 */
3692 switch (ethcmd) {
3693 default:
3694 break;
3695 case ETHTOOL_GRXCLSRLALL:
3696 /* Buffer size is variable */
3697 if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
3698 return -EFAULT;
3699 if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
3700 return -ENOMEM;
3701 buf_size += rule_cnt * sizeof(u32);
3702 /* fall through */
3703 case ETHTOOL_GRXRINGS:
3704 case ETHTOOL_GRXCLSRLCNT:
3705 case ETHTOOL_GRXCLSRULE:
3706 case ETHTOOL_SRXCLSRLINS:
3707 convert_out = true;
3708 /* fall through */
3709 case ETHTOOL_SRXCLSRLDEL:
3710 buf_size += sizeof(struct ethtool_rxnfc);
3711 convert_in = true;
3712 break;
3713 }
3714
3715 ifr = compat_alloc_user_space(buf_size);
3716 rxnfc = (void *)ifr + ALIGN(sizeof(struct ifreq), 8);
3717
3718 if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
3719 return -EFAULT;
3720
3721 if (put_user(convert_in ? rxnfc : compat_ptr(data),
3722 &ifr->ifr_ifru.ifru_data))
3723 return -EFAULT;
3724
3725 if (convert_in) {
3726 /* We expect there to be holes between fs.m_ext and
3727 * fs.ring_cookie and at the end of fs, but nowhere else.
3728 */
3729 BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
3730 sizeof(compat_rxnfc->fs.m_ext) !=
3731 offsetof(struct ethtool_rxnfc, fs.m_ext) +
3732 sizeof(rxnfc->fs.m_ext));
3733 BUILD_BUG_ON(
3734 offsetof(struct compat_ethtool_rxnfc, fs.location) -
3735 offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
3736 offsetof(struct ethtool_rxnfc, fs.location) -
3737 offsetof(struct ethtool_rxnfc, fs.ring_cookie));
3738
3739 if (copy_in_user(rxnfc, compat_rxnfc,
3740 (void *)(&rxnfc->fs.m_ext + 1) -
3741 (void *)rxnfc) ||
3742 copy_in_user(&rxnfc->fs.ring_cookie,
3743 &compat_rxnfc->fs.ring_cookie,
3744 (void *)(&rxnfc->fs.location + 1) -
3745 (void *)&rxnfc->fs.ring_cookie) ||
3746 copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
3747 sizeof(rxnfc->rule_cnt)))
3748 return -EFAULT;
3749 }
3750
3751 ret = dev_ioctl(net, SIOCETHTOOL, ifr);
3752 if (ret)
3753 return ret;
3754
3755 if (convert_out) {
3756 if (copy_in_user(compat_rxnfc, rxnfc,
3757 (const void *)(&rxnfc->fs.m_ext + 1) -
3758 (const void *)rxnfc) ||
3759 copy_in_user(&compat_rxnfc->fs.ring_cookie,
3760 &rxnfc->fs.ring_cookie,
3761 (const void *)(&rxnfc->fs.location + 1) -
3762 (const void *)&rxnfc->fs.ring_cookie) ||
3763 copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
3764 sizeof(rxnfc->rule_cnt)))
3765 return -EFAULT;
3766
3767 if (ethcmd == ETHTOOL_GRXCLSRLALL) {
3768 /* As an optimisation, we only copy the actual
3769 * number of rules that the underlying
3770 * function returned. Since Mallory might
3771 * change the rule count in user memory, we
3772 * check that it is less than the rule count
3773 * originally given (as the user buffer size),
3774 * which has been range-checked.
3775 */
3776 if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
3777 return -EFAULT;
3778 if (actual_rule_cnt < rule_cnt)
3779 rule_cnt = actual_rule_cnt;
3780 if (copy_in_user(&compat_rxnfc->rule_locs[0],
3781 &rxnfc->rule_locs[0],
3782 rule_cnt * sizeof(u32)))
3783 return -EFAULT;
3784 }
3785 }
3786
3787 return 0;
3788}
3789
3790static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
3791{
3792 void __user *uptr;
3793 compat_uptr_t uptr32;
3794 struct ifreq __user *uifr;
3795
3796 uifr = compat_alloc_user_space(sizeof(*uifr));
3797 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
3798 return -EFAULT;
3799
3800 if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
3801 return -EFAULT;
3802
3803 uptr = compat_ptr(uptr32);
3804
3805 if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
3806 return -EFAULT;
3807
3808 return dev_ioctl(net, SIOCWANDEV, uifr);
3809}
3810
3811static int bond_ioctl(struct net *net, unsigned int cmd,
3812 struct compat_ifreq __user *ifr32)
3813{
3814 struct ifreq kifr;
3815 struct ifreq __user *uifr;
3816 mm_segment_t old_fs;
3817 int err;
3818 u32 data;
3819 void __user *datap;
3820
3821 switch (cmd) {
3822 case SIOCBONDENSLAVE:
3823 case SIOCBONDRELEASE:
3824 case SIOCBONDSETHWADDR:
3825 case SIOCBONDCHANGEACTIVE:
3826 if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
3827 return -EFAULT;
3828
3829 old_fs = get_fs();
3830 set_fs(KERNEL_DS);
3831 err = dev_ioctl(net, cmd,
3832 (struct ifreq __user __force *) &kifr);
3833 set_fs(old_fs);
3834
3835 return err;
3836 case SIOCBONDSLAVEINFOQUERY:
3837 case SIOCBONDINFOQUERY:
3838 uifr = compat_alloc_user_space(sizeof(*uifr));
3839 if (copy_in_user(&uifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
3840 return -EFAULT;
3841
3842 if (get_user(data, &ifr32->ifr_ifru.ifru_data))
3843 return -EFAULT;
3844
3845 datap = compat_ptr(data);
3846 if (put_user(datap, &uifr->ifr_ifru.ifru_data))
3847 return -EFAULT;
3848
3849 return dev_ioctl(net, cmd, uifr);
3850 default:
3851 return -ENOIOCTLCMD;
3852 }
3853}
3854
3855static int siocdevprivate_ioctl(struct net *net, unsigned int cmd,
3856 struct compat_ifreq __user *u_ifreq32)
3857{
3858 struct ifreq __user *u_ifreq64;
3859 char tmp_buf[IFNAMSIZ];
3860 void __user *data64;
3861 u32 data32;
3862
3863 if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
3864 IFNAMSIZ))
3865 return -EFAULT;
3866 if (__get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
3867 return -EFAULT;
3868 data64 = compat_ptr(data32);
3869
3870 u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
3871
3872 /* Don't check these user accesses, just let that get trapped
3873 * in the ioctl handler instead.
3874 */
3875 if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
3876 IFNAMSIZ))
3877 return -EFAULT;
3878 if (__put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
3879 return -EFAULT;
3880
3881 return dev_ioctl(net, cmd, u_ifreq64);
3882}
3883
3884static int dev_ifsioc(struct net *net, struct socket *sock,
3885 unsigned int cmd, struct compat_ifreq __user *uifr32)
3886{
3887 struct ifreq __user *uifr;
3888 int err;
3889
3890 uifr = compat_alloc_user_space(sizeof(*uifr));
3891 if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
3892 return -EFAULT;
3893
3894 err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
3895
3896 if (!err) {
3897 switch (cmd) {
3898 case SIOCGIFFLAGS:
3899 case SIOCGIFMETRIC:
3900 case SIOCGIFMTU:
3901 case SIOCGIFMEM:
3902 case SIOCGIFHWADDR:
3903 case SIOCGIFINDEX:
3904 case SIOCGIFADDR:
3905 case SIOCGIFBRDADDR:
3906 case SIOCGIFDSTADDR:
3907 case SIOCGIFNETMASK:
3908 case SIOCGIFPFLAGS:
3909 case SIOCGIFTXQLEN:
3910 case SIOCGMIIPHY:
3911 case SIOCGMIIREG:
3912 if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
3913 err = -EFAULT;
3914 break;
3915 }
3916 }
3917 return err;
3918}
3919
3920static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
3921 struct compat_ifreq __user *uifr32)
3922{
3923 struct ifreq ifr;
3924 struct compat_ifmap __user *uifmap32;
3925 mm_segment_t old_fs;
3926 int err;
3927
3928 uifmap32 = &uifr32->ifr_ifru.ifru_map;
3929 err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
3930 err |= __get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3931 err |= __get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3932 err |= __get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3933 err |= __get_user(ifr.ifr_map.irq, &uifmap32->irq);
3934 err |= __get_user(ifr.ifr_map.dma, &uifmap32->dma);
3935 err |= __get_user(ifr.ifr_map.port, &uifmap32->port);
3936 if (err)
3937 return -EFAULT;
3938
3939 old_fs = get_fs();
3940 set_fs(KERNEL_DS);
3941 err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
3942 set_fs(old_fs);
3943
3944 if (cmd == SIOCGIFMAP && !err) {
3945 err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
3946 err |= __put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
3947 err |= __put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
3948 err |= __put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
3949 err |= __put_user(ifr.ifr_map.irq, &uifmap32->irq);
3950 err |= __put_user(ifr.ifr_map.dma, &uifmap32->dma);
3951 err |= __put_user(ifr.ifr_map.port, &uifmap32->port);
3952 if (err)
3953 err = -EFAULT;
3954 }
3955 return err;
3956}
3957
3958static int compat_siocshwtstamp(struct net *net, struct compat_ifreq __user *uifr32)
3959{
3960 void __user *uptr;
3961 compat_uptr_t uptr32;
3962 struct ifreq __user *uifr;
3963
3964 uifr = compat_alloc_user_space(sizeof(*uifr));
3965 if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
3966 return -EFAULT;
3967
3968 if (get_user(uptr32, &uifr32->ifr_data))
3969 return -EFAULT;
3970
3971 uptr = compat_ptr(uptr32);
3972
3973 if (put_user(uptr, &uifr->ifr_data))
3974 return -EFAULT;
3975
3976 return dev_ioctl(net, SIOCSHWTSTAMP, uifr);
3977}
3978
3979struct rtentry32 {
3980 u32 rt_pad1;
3981 struct sockaddr rt_dst; /* target address */
3982 struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
3983 struct sockaddr rt_genmask; /* target network mask (IP) */
3984 unsigned short rt_flags;
3985 short rt_pad2;
3986 u32 rt_pad3;
3987 unsigned char rt_tos;
3988 unsigned char rt_class;
3989 short rt_pad4;
3990 short rt_metric; /* +1 for binary compatibility! */
3991 /* char * */ u32 rt_dev; /* forcing the device at add */
3992 u32 rt_mtu; /* per route MTU/Window */
3993 u32 rt_window; /* Window clamping */
3994 unsigned short rt_irtt; /* Initial RTT */
3995};
3996
3997struct in6_rtmsg32 {
3998 struct in6_addr rtmsg_dst;
3999 struct in6_addr rtmsg_src;
4000 struct in6_addr rtmsg_gateway;
4001 u32 rtmsg_type;
4002 u16 rtmsg_dst_len;
4003 u16 rtmsg_src_len;
4004 u32 rtmsg_metric;
4005 u32 rtmsg_info;
4006 u32 rtmsg_flags;
4007 s32 rtmsg_ifindex;
4008};
4009
4010static int routing_ioctl(struct net *net, struct socket *sock,
4011 unsigned int cmd, void __user *argp)
4012{
4013 int ret;
4014 void *r = NULL;
4015 struct in6_rtmsg r6;
4016 struct rtentry r4;
4017 char devname[16];
4018 u32 rtdev;
4019 mm_segment_t old_fs = get_fs();
4020
4021 if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
4022 struct in6_rtmsg32 __user *ur6 = argp;
4023 ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
4024 3 * sizeof(struct in6_addr));
4025 ret |= __get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
4026 ret |= __get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
4027 ret |= __get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
4028 ret |= __get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
4029 ret |= __get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
4030 ret |= __get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
4031 ret |= __get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
4032
4033 r = (void *) &r6;
4034 } else { /* ipv4 */
4035 struct rtentry32 __user *ur4 = argp;
4036 ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
4037 3 * sizeof(struct sockaddr));
4038 ret |= __get_user(r4.rt_flags, &(ur4->rt_flags));
4039 ret |= __get_user(r4.rt_metric, &(ur4->rt_metric));
4040 ret |= __get_user(r4.rt_mtu, &(ur4->rt_mtu));
4041 ret |= __get_user(r4.rt_window, &(ur4->rt_window));
4042 ret |= __get_user(r4.rt_irtt, &(ur4->rt_irtt));
4043 ret |= __get_user(rtdev, &(ur4->rt_dev));
4044 if (rtdev) {
4045 ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
4046 r4.rt_dev = (char __user __force *)devname;
4047 devname[15] = 0;
4048 } else
4049 r4.rt_dev = NULL;
4050
4051 r = (void *) &r4;
4052 }
4053
4054 if (ret) {
4055 ret = -EFAULT;
4056 goto out;
4057 }
4058
4059 set_fs(KERNEL_DS);
4060 ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
4061 set_fs(old_fs);
4062
4063out:
4064 return ret;
4065}
4066
4067/* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
4068 * for some operations; this forces use of the newer bridge-utils that
4069 * use compatible ioctls
4070 */
4071static int old_bridge_ioctl(compat_ulong_t __user *argp)
4072{
4073 compat_ulong_t tmp;
4074
4075 if (get_user(tmp, argp))
4076 return -EFAULT;
4077 if (tmp == BRCTL_GET_VERSION)
4078 return BRCTL_VERSION + 1;
4079 return -EINVAL;
4080}
4081
4082static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
4083 unsigned int cmd, unsigned long arg)
4084{
4085 void __user *argp = compat_ptr(arg);
4086 struct sock *sk = sock->sk;
4087 struct net *net = sock_net(sk);
4088
4089 if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
4090 return siocdevprivate_ioctl(net, cmd, argp);
4091
4092 switch (cmd) {
4093 case SIOCSIFBR:
4094 case SIOCGIFBR:
4095 return old_bridge_ioctl(argp);
4096 case SIOCGIFNAME:
4097 return dev_ifname32(net, argp);
4098 case SIOCGIFCONF:
4099 return dev_ifconf(net, argp);
4100 case SIOCETHTOOL:
4101 return ethtool_ioctl(net, argp);
4102 case SIOCWANDEV:
4103 return compat_siocwandev(net, argp);
4104 case SIOCGIFMAP:
4105 case SIOCSIFMAP:
4106 return compat_sioc_ifmap(net, cmd, argp);
4107 case SIOCBONDENSLAVE:
4108 case SIOCBONDRELEASE:
4109 case SIOCBONDSETHWADDR:
4110 case SIOCBONDSLAVEINFOQUERY:
4111 case SIOCBONDINFOQUERY:
4112 case SIOCBONDCHANGEACTIVE:
4113 return bond_ioctl(net, cmd, argp);
4114 case SIOCADDRT:
4115 case SIOCDELRT:
4116 return routing_ioctl(net, sock, cmd, argp);
4117 case SIOCGSTAMP:
4118 return do_siocgstamp(net, sock, cmd, argp);
4119 case SIOCGSTAMPNS:
4120 return do_siocgstampns(net, sock, cmd, argp);
4121 case SIOCSHWTSTAMP:
4122 return compat_siocshwtstamp(net, argp);
4123
4124 case FIOSETOWN:
4125 case SIOCSPGRP:
4126 case FIOGETOWN:
4127 case SIOCGPGRP:
4128 case SIOCBRADDBR:
4129 case SIOCBRDELBR:
4130 case SIOCGIFVLAN:
4131 case SIOCSIFVLAN:
4132 case SIOCADDDLCI:
4133 case SIOCDELDLCI:
4134 return sock_ioctl(file, cmd, arg);
4135
4136 case SIOCGIFFLAGS:
4137 case SIOCSIFFLAGS:
4138 case SIOCGIFMETRIC:
4139 case SIOCSIFMETRIC:
4140 case SIOCGIFMTU:
4141 case SIOCSIFMTU:
4142 case SIOCGIFMEM:
4143 case SIOCSIFMEM:
4144 case SIOCGIFHWADDR:
4145 case SIOCSIFHWADDR:
4146 case SIOCADDMULTI:
4147 case SIOCDELMULTI:
4148 case SIOCGIFINDEX:
4149 case SIOCGIFADDR:
4150 case SIOCSIFADDR:
4151 case SIOCSIFHWBROADCAST:
4152 case SIOCDIFADDR:
4153 case SIOCGIFBRDADDR:
4154 case SIOCSIFBRDADDR:
4155 case SIOCGIFDSTADDR:
4156 case SIOCSIFDSTADDR:
4157 case SIOCGIFNETMASK:
4158 case SIOCSIFNETMASK:
4159 case SIOCSIFPFLAGS:
4160 case SIOCGIFPFLAGS:
4161 case SIOCGIFTXQLEN:
4162 case SIOCSIFTXQLEN:
4163 case SIOCBRADDIF:
4164 case SIOCBRDELIF:
4165 case SIOCSIFNAME:
4166 case SIOCGMIIPHY:
4167 case SIOCGMIIREG:
4168 case SIOCSMIIREG:
4169 return dev_ifsioc(net, sock, cmd, argp);
4170
4171 case SIOCSARP:
4172 case SIOCGARP:
4173 case SIOCDARP:
4174 case SIOCATMARK:
4175 return sock_do_ioctl(net, sock, cmd, arg);
4176 }
4177
4178 return -ENOIOCTLCMD;
4179}
4180
4181static long compat_sock_ioctl(struct file *file, unsigned cmd,
4182 unsigned long arg)
4183{
4184 struct socket *sock = file->private_data;
4185 int ret = -ENOIOCTLCMD;
4186 struct sock *sk;
4187 struct net *net;
4188
4189 sk = sock->sk;
4190 net = sock_net(sk);
4191
4192 if (sock->ops->compat_ioctl)
4193 ret = sock->ops->compat_ioctl(sock, cmd, arg);
4194
4195 if (ret == -ENOIOCTLCMD &&
4196 (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
4197 ret = compat_wext_handle_ioctl(net, cmd, arg);
4198
4199 if (ret == -ENOIOCTLCMD)
4200 ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
4201
4202 return ret;
4203}
4204#endif
4205
4206int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
4207{
4208 return sock->ops->bind(sock, addr, addrlen);
4209}
4210EXPORT_SYMBOL(kernel_bind);
4211
4212int kernel_listen(struct socket *sock, int backlog)
4213{
4214 return sock->ops->listen(sock, backlog);
4215}
4216EXPORT_SYMBOL(kernel_listen);
4217
4218int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
4219{
4220 struct sock *sk = sock->sk;
4221 int err;
4222
4223 err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
4224 newsock);
4225 if (err < 0)
4226 goto done;
4227
4228 err = sock->ops->accept(sock, *newsock, flags);
4229 if (err < 0) {
4230 sock_release(*newsock);
4231 *newsock = NULL;
4232 goto done;
4233 }
4234
4235 (*newsock)->ops = sock->ops;
4236 __module_get((*newsock)->ops->owner);
4237
4238done:
4239 return err;
4240}
4241EXPORT_SYMBOL(kernel_accept);
4242
4243int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
4244 int flags)
4245{
4246 return sock->ops->connect(sock, addr, addrlen, flags);
4247}
4248EXPORT_SYMBOL(kernel_connect);
4249
4250int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
4251 int *addrlen)
4252{
4253 return sock->ops->getname(sock, addr, addrlen, 0);
4254}
4255EXPORT_SYMBOL(kernel_getsockname);
4256
4257int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
4258 int *addrlen)
4259{
4260 return sock->ops->getname(sock, addr, addrlen, 1);
4261}
4262EXPORT_SYMBOL(kernel_getpeername);
4263
4264int kernel_getsockopt(struct socket *sock, int level, int optname,
4265 char *optval, int *optlen)
4266{
4267 mm_segment_t oldfs = get_fs();
4268 char __user *uoptval;
4269 int __user *uoptlen;
4270 int err;
4271
4272 uoptval = (char __user __force *) optval;
4273 uoptlen = (int __user __force *) optlen;
4274
4275 set_fs(KERNEL_DS);
4276 if (level == SOL_SOCKET)
4277 err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
4278 else
4279 err = sock->ops->getsockopt(sock, level, optname, uoptval,
4280 uoptlen);
4281 set_fs(oldfs);
4282 return err;
4283}
4284EXPORT_SYMBOL(kernel_getsockopt);
4285
4286int kernel_setsockopt(struct socket *sock, int level, int optname,
4287 char *optval, unsigned int optlen)
4288{
4289 mm_segment_t oldfs = get_fs();
4290 char __user *uoptval;
4291 int err;
4292
4293 uoptval = (char __user __force *) optval;
4294
4295 set_fs(KERNEL_DS);
4296 if (level == SOL_SOCKET)
4297 err = sock_setsockopt(sock, level, optname, uoptval, optlen);
4298 else
4299 err = sock->ops->setsockopt(sock, level, optname, uoptval,
4300 optlen);
4301 set_fs(oldfs);
4302 return err;
4303}
4304EXPORT_SYMBOL(kernel_setsockopt);
4305
4306int kernel_sendpage(struct socket *sock, struct page *page, int offset,
4307 size_t size, int flags)
4308{
4309 sock_update_classid(sock->sk);
4310
4311 if (sock->ops->sendpage)
4312 return sock->ops->sendpage(sock, page, offset, size, flags);
4313
4314 return sock_no_sendpage(sock, page, offset, size, flags);
4315}
4316EXPORT_SYMBOL(kernel_sendpage);
4317
4318int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
4319{
4320 mm_segment_t oldfs = get_fs();
4321 int err;
4322
4323 set_fs(KERNEL_DS);
4324 err = sock->ops->ioctl(sock, cmd, arg);
4325 set_fs(oldfs);
4326
4327 return err;
4328}
4329EXPORT_SYMBOL(kernel_sock_ioctl);
4330
4331int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
4332{
4333 return sock->ops->shutdown(sock, how);
4334}
4335EXPORT_SYMBOL(kernel_sock_shutdown);