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