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