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