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