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