]>
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) | |
45 | * Tigran Aivazian : Made listen(2) backlog sanity checks | |
46 | * protocol-independent | |
47 | * | |
48 | * | |
49 | * This program is free software; you can redistribute it and/or | |
50 | * modify it under the terms of the GNU General Public License | |
51 | * as published by the Free Software Foundation; either version | |
52 | * 2 of the License, or (at your option) any later version. | |
53 | * | |
54 | * | |
55 | * This module is effectively the top level interface to the BSD socket | |
56 | * paradigm. | |
57 | * | |
58 | * Based upon Swansea University Computer Society NET3.039 | |
59 | */ | |
60 | ||
61 | #include <linux/config.h> | |
62 | #include <linux/mm.h> | |
63 | #include <linux/smp_lock.h> | |
64 | #include <linux/socket.h> | |
65 | #include <linux/file.h> | |
66 | #include <linux/net.h> | |
67 | #include <linux/interrupt.h> | |
68 | #include <linux/netdevice.h> | |
69 | #include <linux/proc_fs.h> | |
70 | #include <linux/seq_file.h> | |
71 | #include <linux/wanrouter.h> | |
72 | #include <linux/if_bridge.h> | |
20380731 ACM |
73 | #include <linux/if_frad.h> |
74 | #include <linux/if_vlan.h> | |
1da177e4 LT |
75 | #include <linux/init.h> |
76 | #include <linux/poll.h> | |
77 | #include <linux/cache.h> | |
78 | #include <linux/module.h> | |
79 | #include <linux/highmem.h> | |
80 | #include <linux/divert.h> | |
81 | #include <linux/mount.h> | |
82 | #include <linux/security.h> | |
83 | #include <linux/syscalls.h> | |
84 | #include <linux/compat.h> | |
85 | #include <linux/kmod.h> | |
3ec3b2fb | 86 | #include <linux/audit.h> |
d86b5e0e | 87 | #include <linux/wireless.h> |
1da177e4 LT |
88 | |
89 | #include <asm/uaccess.h> | |
90 | #include <asm/unistd.h> | |
91 | ||
92 | #include <net/compat.h> | |
93 | ||
94 | #include <net/sock.h> | |
95 | #include <linux/netfilter.h> | |
96 | ||
97 | static int sock_no_open(struct inode *irrelevant, struct file *dontcare); | |
98 | static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf, | |
99 | size_t size, loff_t pos); | |
100 | static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf, | |
101 | size_t size, loff_t pos); | |
102 | static int sock_mmap(struct file *file, struct vm_area_struct * vma); | |
103 | ||
104 | static int sock_close(struct inode *inode, struct file *file); | |
105 | static unsigned int sock_poll(struct file *file, | |
106 | struct poll_table_struct *wait); | |
107 | static long sock_ioctl(struct file *file, | |
108 | unsigned int cmd, unsigned long arg); | |
109 | static int sock_fasync(int fd, struct file *filp, int on); | |
110 | static ssize_t sock_readv(struct file *file, const struct iovec *vector, | |
111 | unsigned long count, loff_t *ppos); | |
112 | static ssize_t sock_writev(struct file *file, const struct iovec *vector, | |
113 | unsigned long count, loff_t *ppos); | |
114 | static ssize_t sock_sendpage(struct file *file, struct page *page, | |
115 | int offset, size_t size, loff_t *ppos, int more); | |
116 | ||
117 | ||
118 | /* | |
119 | * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear | |
120 | * in the operation structures but are done directly via the socketcall() multiplexor. | |
121 | */ | |
122 | ||
123 | static struct file_operations socket_file_ops = { | |
124 | .owner = THIS_MODULE, | |
125 | .llseek = no_llseek, | |
126 | .aio_read = sock_aio_read, | |
127 | .aio_write = sock_aio_write, | |
128 | .poll = sock_poll, | |
129 | .unlocked_ioctl = sock_ioctl, | |
130 | .mmap = sock_mmap, | |
131 | .open = sock_no_open, /* special open code to disallow open via /proc */ | |
132 | .release = sock_close, | |
133 | .fasync = sock_fasync, | |
134 | .readv = sock_readv, | |
135 | .writev = sock_writev, | |
136 | .sendpage = sock_sendpage | |
137 | }; | |
138 | ||
139 | /* | |
140 | * The protocol list. Each protocol is registered in here. | |
141 | */ | |
142 | ||
143 | static struct net_proto_family *net_families[NPROTO]; | |
144 | ||
145 | #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT) | |
146 | static atomic_t net_family_lockct = ATOMIC_INIT(0); | |
147 | static DEFINE_SPINLOCK(net_family_lock); | |
148 | ||
149 | /* The strategy is: modifications net_family vector are short, do not | |
150 | sleep and veeery rare, but read access should be free of any exclusive | |
151 | locks. | |
152 | */ | |
153 | ||
154 | static void net_family_write_lock(void) | |
155 | { | |
156 | spin_lock(&net_family_lock); | |
157 | while (atomic_read(&net_family_lockct) != 0) { | |
158 | spin_unlock(&net_family_lock); | |
159 | ||
160 | yield(); | |
161 | ||
162 | spin_lock(&net_family_lock); | |
163 | } | |
164 | } | |
165 | ||
166 | static __inline__ void net_family_write_unlock(void) | |
167 | { | |
168 | spin_unlock(&net_family_lock); | |
169 | } | |
170 | ||
171 | static __inline__ void net_family_read_lock(void) | |
172 | { | |
173 | atomic_inc(&net_family_lockct); | |
174 | spin_unlock_wait(&net_family_lock); | |
175 | } | |
176 | ||
177 | static __inline__ void net_family_read_unlock(void) | |
178 | { | |
179 | atomic_dec(&net_family_lockct); | |
180 | } | |
181 | ||
182 | #else | |
183 | #define net_family_write_lock() do { } while(0) | |
184 | #define net_family_write_unlock() do { } while(0) | |
185 | #define net_family_read_lock() do { } while(0) | |
186 | #define net_family_read_unlock() do { } while(0) | |
187 | #endif | |
188 | ||
189 | ||
190 | /* | |
191 | * Statistics counters of the socket lists | |
192 | */ | |
193 | ||
194 | static DEFINE_PER_CPU(int, sockets_in_use) = 0; | |
195 | ||
196 | /* | |
197 | * Support routines. Move socket addresses back and forth across the kernel/user | |
198 | * divide and look after the messy bits. | |
199 | */ | |
200 | ||
201 | #define MAX_SOCK_ADDR 128 /* 108 for Unix domain - | |
202 | 16 for IP, 16 for IPX, | |
203 | 24 for IPv6, | |
204 | about 80 for AX.25 | |
205 | must be at least one bigger than | |
206 | the AF_UNIX size (see net/unix/af_unix.c | |
207 | :unix_mkname()). | |
208 | */ | |
209 | ||
210 | /** | |
211 | * move_addr_to_kernel - copy a socket address into kernel space | |
212 | * @uaddr: Address in user space | |
213 | * @kaddr: Address in kernel space | |
214 | * @ulen: Length in user space | |
215 | * | |
216 | * The address is copied into kernel space. If the provided address is | |
217 | * too long an error code of -EINVAL is returned. If the copy gives | |
218 | * invalid addresses -EFAULT is returned. On a success 0 is returned. | |
219 | */ | |
220 | ||
221 | int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr) | |
222 | { | |
223 | if(ulen<0||ulen>MAX_SOCK_ADDR) | |
224 | return -EINVAL; | |
225 | if(ulen==0) | |
226 | return 0; | |
227 | if(copy_from_user(kaddr,uaddr,ulen)) | |
228 | return -EFAULT; | |
3ec3b2fb | 229 | return audit_sockaddr(ulen, kaddr); |
1da177e4 LT |
230 | } |
231 | ||
232 | /** | |
233 | * move_addr_to_user - copy an address to user space | |
234 | * @kaddr: kernel space address | |
235 | * @klen: length of address in kernel | |
236 | * @uaddr: user space address | |
237 | * @ulen: pointer to user length field | |
238 | * | |
239 | * The value pointed to by ulen on entry is the buffer length available. | |
240 | * This is overwritten with the buffer space used. -EINVAL is returned | |
241 | * if an overlong buffer is specified or a negative buffer size. -EFAULT | |
242 | * is returned if either the buffer or the length field are not | |
243 | * accessible. | |
244 | * After copying the data up to the limit the user specifies, the true | |
245 | * length of the data is written over the length limit the user | |
246 | * specified. Zero is returned for a success. | |
247 | */ | |
248 | ||
249 | int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, int __user *ulen) | |
250 | { | |
251 | int err; | |
252 | int len; | |
253 | ||
254 | if((err=get_user(len, ulen))) | |
255 | return err; | |
256 | if(len>klen) | |
257 | len=klen; | |
258 | if(len<0 || len> MAX_SOCK_ADDR) | |
259 | return -EINVAL; | |
260 | if(len) | |
261 | { | |
262 | if(copy_to_user(uaddr,kaddr,len)) | |
263 | return -EFAULT; | |
264 | } | |
265 | /* | |
266 | * "fromlen shall refer to the value before truncation.." | |
267 | * 1003.1g | |
268 | */ | |
269 | return __put_user(klen, ulen); | |
270 | } | |
271 | ||
272 | #define SOCKFS_MAGIC 0x534F434B | |
273 | ||
ba89966c | 274 | static kmem_cache_t * sock_inode_cachep __read_mostly; |
1da177e4 LT |
275 | |
276 | static struct inode *sock_alloc_inode(struct super_block *sb) | |
277 | { | |
278 | struct socket_alloc *ei; | |
279 | ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL); | |
280 | if (!ei) | |
281 | return NULL; | |
282 | init_waitqueue_head(&ei->socket.wait); | |
283 | ||
284 | ei->socket.fasync_list = NULL; | |
285 | ei->socket.state = SS_UNCONNECTED; | |
286 | ei->socket.flags = 0; | |
287 | ei->socket.ops = NULL; | |
288 | ei->socket.sk = NULL; | |
289 | ei->socket.file = NULL; | |
290 | ei->socket.flags = 0; | |
291 | ||
292 | return &ei->vfs_inode; | |
293 | } | |
294 | ||
295 | static void sock_destroy_inode(struct inode *inode) | |
296 | { | |
297 | kmem_cache_free(sock_inode_cachep, | |
298 | container_of(inode, struct socket_alloc, vfs_inode)); | |
299 | } | |
300 | ||
301 | static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) | |
302 | { | |
303 | struct socket_alloc *ei = (struct socket_alloc *) foo; | |
304 | ||
305 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == | |
306 | SLAB_CTOR_CONSTRUCTOR) | |
307 | inode_init_once(&ei->vfs_inode); | |
308 | } | |
309 | ||
310 | static int init_inodecache(void) | |
311 | { | |
312 | sock_inode_cachep = kmem_cache_create("sock_inode_cache", | |
313 | sizeof(struct socket_alloc), | |
314 | 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT, | |
315 | init_once, NULL); | |
316 | if (sock_inode_cachep == NULL) | |
317 | return -ENOMEM; | |
318 | return 0; | |
319 | } | |
320 | ||
321 | static struct super_operations sockfs_ops = { | |
322 | .alloc_inode = sock_alloc_inode, | |
323 | .destroy_inode =sock_destroy_inode, | |
324 | .statfs = simple_statfs, | |
325 | }; | |
326 | ||
327 | static struct super_block *sockfs_get_sb(struct file_system_type *fs_type, | |
328 | int flags, const char *dev_name, void *data) | |
329 | { | |
330 | return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC); | |
331 | } | |
332 | ||
ba89966c | 333 | static struct vfsmount *sock_mnt __read_mostly; |
1da177e4 LT |
334 | |
335 | static struct file_system_type sock_fs_type = { | |
336 | .name = "sockfs", | |
337 | .get_sb = sockfs_get_sb, | |
338 | .kill_sb = kill_anon_super, | |
339 | }; | |
340 | static int sockfs_delete_dentry(struct dentry *dentry) | |
341 | { | |
342 | return 1; | |
343 | } | |
344 | static struct dentry_operations sockfs_dentry_operations = { | |
345 | .d_delete = sockfs_delete_dentry, | |
346 | }; | |
347 | ||
348 | /* | |
349 | * Obtains the first available file descriptor and sets it up for use. | |
350 | * | |
39d8c1b6 DM |
351 | * These functions create file structures and maps them to fd space |
352 | * of the current process. On success it returns file descriptor | |
1da177e4 LT |
353 | * and file struct implicitly stored in sock->file. |
354 | * Note that another thread may close file descriptor before we return | |
355 | * from this function. We use the fact that now we do not refer | |
356 | * to socket after mapping. If one day we will need it, this | |
357 | * function will increment ref. count on file by 1. | |
358 | * | |
359 | * In any case returned fd MAY BE not valid! | |
360 | * This race condition is unavoidable | |
361 | * with shared fd spaces, we cannot solve it inside kernel, | |
362 | * but we take care of internal coherence yet. | |
363 | */ | |
364 | ||
39d8c1b6 | 365 | static int sock_alloc_fd(struct file **filep) |
1da177e4 LT |
366 | { |
367 | int fd; | |
1da177e4 LT |
368 | |
369 | fd = get_unused_fd(); | |
39d8c1b6 | 370 | if (likely(fd >= 0)) { |
1da177e4 LT |
371 | struct file *file = get_empty_filp(); |
372 | ||
39d8c1b6 DM |
373 | *filep = file; |
374 | if (unlikely(!file)) { | |
1da177e4 | 375 | put_unused_fd(fd); |
39d8c1b6 | 376 | return -ENFILE; |
1da177e4 | 377 | } |
39d8c1b6 DM |
378 | } else |
379 | *filep = NULL; | |
380 | return fd; | |
381 | } | |
1da177e4 | 382 | |
39d8c1b6 DM |
383 | static int sock_attach_fd(struct socket *sock, struct file *file) |
384 | { | |
385 | struct qstr this; | |
386 | char name[32]; | |
387 | ||
388 | this.len = sprintf(name, "[%lu]", SOCK_INODE(sock)->i_ino); | |
389 | this.name = name; | |
390 | this.hash = SOCK_INODE(sock)->i_ino; | |
391 | ||
392 | file->f_dentry = d_alloc(sock_mnt->mnt_sb->s_root, &this); | |
393 | if (unlikely(!file->f_dentry)) | |
394 | return -ENOMEM; | |
395 | ||
396 | file->f_dentry->d_op = &sockfs_dentry_operations; | |
397 | d_add(file->f_dentry, SOCK_INODE(sock)); | |
398 | file->f_vfsmnt = mntget(sock_mnt); | |
399 | file->f_mapping = file->f_dentry->d_inode->i_mapping; | |
400 | ||
401 | sock->file = file; | |
402 | file->f_op = SOCK_INODE(sock)->i_fop = &socket_file_ops; | |
403 | file->f_mode = FMODE_READ | FMODE_WRITE; | |
404 | file->f_flags = O_RDWR; | |
405 | file->f_pos = 0; | |
406 | file->private_data = sock; | |
1da177e4 | 407 | |
39d8c1b6 DM |
408 | return 0; |
409 | } | |
410 | ||
411 | int sock_map_fd(struct socket *sock) | |
412 | { | |
413 | struct file *newfile; | |
414 | int fd = sock_alloc_fd(&newfile); | |
415 | ||
416 | if (likely(fd >= 0)) { | |
417 | int err = sock_attach_fd(sock, newfile); | |
418 | ||
419 | if (unlikely(err < 0)) { | |
420 | put_filp(newfile); | |
1da177e4 | 421 | put_unused_fd(fd); |
39d8c1b6 | 422 | return err; |
1da177e4 | 423 | } |
39d8c1b6 | 424 | fd_install(fd, newfile); |
1da177e4 | 425 | } |
1da177e4 LT |
426 | return fd; |
427 | } | |
428 | ||
6cb153ca BL |
429 | static struct socket *sock_from_file(struct file *file, int *err) |
430 | { | |
431 | struct inode *inode; | |
432 | struct socket *sock; | |
433 | ||
434 | if (file->f_op == &socket_file_ops) | |
435 | return file->private_data; /* set in sock_map_fd */ | |
436 | ||
437 | inode = file->f_dentry->d_inode; | |
438 | if (!S_ISSOCK(inode->i_mode)) { | |
439 | *err = -ENOTSOCK; | |
440 | return NULL; | |
441 | } | |
442 | ||
443 | sock = SOCKET_I(inode); | |
444 | if (sock->file != file) { | |
445 | printk(KERN_ERR "socki_lookup: socket file changed!\n"); | |
446 | sock->file = file; | |
447 | } | |
448 | return sock; | |
449 | } | |
450 | ||
1da177e4 LT |
451 | /** |
452 | * sockfd_lookup - Go from a file number to its socket slot | |
453 | * @fd: file handle | |
454 | * @err: pointer to an error code return | |
455 | * | |
456 | * The file handle passed in is locked and the socket it is bound | |
457 | * too is returned. If an error occurs the err pointer is overwritten | |
458 | * with a negative errno code and NULL is returned. The function checks | |
459 | * for both invalid handles and passing a handle which is not a socket. | |
460 | * | |
461 | * On a success the socket object pointer is returned. | |
462 | */ | |
463 | ||
464 | struct socket *sockfd_lookup(int fd, int *err) | |
465 | { | |
466 | struct file *file; | |
1da177e4 LT |
467 | struct socket *sock; |
468 | ||
6cb153ca | 469 | if (!(file = fget(fd))) { |
1da177e4 LT |
470 | *err = -EBADF; |
471 | return NULL; | |
472 | } | |
6cb153ca BL |
473 | sock = sock_from_file(file, err); |
474 | if (!sock) | |
1da177e4 | 475 | fput(file); |
6cb153ca BL |
476 | return sock; |
477 | } | |
1da177e4 | 478 | |
6cb153ca BL |
479 | static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed) |
480 | { | |
481 | struct file *file; | |
482 | struct socket *sock; | |
483 | ||
484 | file = fget_light(fd, fput_needed); | |
485 | if (file) { | |
486 | sock = sock_from_file(file, err); | |
487 | if (sock) | |
488 | return sock; | |
489 | fput_light(file, *fput_needed); | |
1da177e4 | 490 | } |
6cb153ca | 491 | return NULL; |
1da177e4 LT |
492 | } |
493 | ||
494 | /** | |
495 | * sock_alloc - allocate a socket | |
496 | * | |
497 | * Allocate a new inode and socket object. The two are bound together | |
498 | * and initialised. The socket is then returned. If we are out of inodes | |
499 | * NULL is returned. | |
500 | */ | |
501 | ||
502 | static struct socket *sock_alloc(void) | |
503 | { | |
504 | struct inode * inode; | |
505 | struct socket * sock; | |
506 | ||
507 | inode = new_inode(sock_mnt->mnt_sb); | |
508 | if (!inode) | |
509 | return NULL; | |
510 | ||
511 | sock = SOCKET_I(inode); | |
512 | ||
513 | inode->i_mode = S_IFSOCK|S_IRWXUGO; | |
514 | inode->i_uid = current->fsuid; | |
515 | inode->i_gid = current->fsgid; | |
516 | ||
517 | get_cpu_var(sockets_in_use)++; | |
518 | put_cpu_var(sockets_in_use); | |
519 | return sock; | |
520 | } | |
521 | ||
522 | /* | |
523 | * In theory you can't get an open on this inode, but /proc provides | |
524 | * a back door. Remember to keep it shut otherwise you'll let the | |
525 | * creepy crawlies in. | |
526 | */ | |
527 | ||
528 | static int sock_no_open(struct inode *irrelevant, struct file *dontcare) | |
529 | { | |
530 | return -ENXIO; | |
531 | } | |
532 | ||
533 | struct file_operations bad_sock_fops = { | |
534 | .owner = THIS_MODULE, | |
535 | .open = sock_no_open, | |
536 | }; | |
537 | ||
538 | /** | |
539 | * sock_release - close a socket | |
540 | * @sock: socket to close | |
541 | * | |
542 | * The socket is released from the protocol stack if it has a release | |
543 | * callback, and the inode is then released if the socket is bound to | |
544 | * an inode not a file. | |
545 | */ | |
546 | ||
547 | void sock_release(struct socket *sock) | |
548 | { | |
549 | if (sock->ops) { | |
550 | struct module *owner = sock->ops->owner; | |
551 | ||
552 | sock->ops->release(sock); | |
553 | sock->ops = NULL; | |
554 | module_put(owner); | |
555 | } | |
556 | ||
557 | if (sock->fasync_list) | |
558 | printk(KERN_ERR "sock_release: fasync list not empty!\n"); | |
559 | ||
560 | get_cpu_var(sockets_in_use)--; | |
561 | put_cpu_var(sockets_in_use); | |
562 | if (!sock->file) { | |
563 | iput(SOCK_INODE(sock)); | |
564 | return; | |
565 | } | |
566 | sock->file=NULL; | |
567 | } | |
568 | ||
569 | static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock, | |
570 | struct msghdr *msg, size_t size) | |
571 | { | |
572 | struct sock_iocb *si = kiocb_to_siocb(iocb); | |
573 | int err; | |
574 | ||
575 | si->sock = sock; | |
576 | si->scm = NULL; | |
577 | si->msg = msg; | |
578 | si->size = size; | |
579 | ||
580 | err = security_socket_sendmsg(sock, msg, size); | |
581 | if (err) | |
582 | return err; | |
583 | ||
584 | return sock->ops->sendmsg(iocb, sock, msg, size); | |
585 | } | |
586 | ||
587 | int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) | |
588 | { | |
589 | struct kiocb iocb; | |
590 | struct sock_iocb siocb; | |
591 | int ret; | |
592 | ||
593 | init_sync_kiocb(&iocb, NULL); | |
594 | iocb.private = &siocb; | |
595 | ret = __sock_sendmsg(&iocb, sock, msg, size); | |
596 | if (-EIOCBQUEUED == ret) | |
597 | ret = wait_on_sync_kiocb(&iocb); | |
598 | return ret; | |
599 | } | |
600 | ||
601 | int kernel_sendmsg(struct socket *sock, struct msghdr *msg, | |
602 | struct kvec *vec, size_t num, size_t size) | |
603 | { | |
604 | mm_segment_t oldfs = get_fs(); | |
605 | int result; | |
606 | ||
607 | set_fs(KERNEL_DS); | |
608 | /* | |
609 | * the following is safe, since for compiler definitions of kvec and | |
610 | * iovec are identical, yielding the same in-core layout and alignment | |
611 | */ | |
612 | msg->msg_iov = (struct iovec *)vec, | |
613 | msg->msg_iovlen = num; | |
614 | result = sock_sendmsg(sock, msg, size); | |
615 | set_fs(oldfs); | |
616 | return result; | |
617 | } | |
618 | ||
619 | static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock, | |
620 | struct msghdr *msg, size_t size, int flags) | |
621 | { | |
622 | int err; | |
623 | struct sock_iocb *si = kiocb_to_siocb(iocb); | |
624 | ||
625 | si->sock = sock; | |
626 | si->scm = NULL; | |
627 | si->msg = msg; | |
628 | si->size = size; | |
629 | si->flags = flags; | |
630 | ||
631 | err = security_socket_recvmsg(sock, msg, size, flags); | |
632 | if (err) | |
633 | return err; | |
634 | ||
635 | return sock->ops->recvmsg(iocb, sock, msg, size, flags); | |
636 | } | |
637 | ||
638 | int sock_recvmsg(struct socket *sock, struct msghdr *msg, | |
639 | size_t size, int flags) | |
640 | { | |
641 | struct kiocb iocb; | |
642 | struct sock_iocb siocb; | |
643 | int ret; | |
644 | ||
645 | init_sync_kiocb(&iocb, NULL); | |
646 | iocb.private = &siocb; | |
647 | ret = __sock_recvmsg(&iocb, sock, msg, size, flags); | |
648 | if (-EIOCBQUEUED == ret) | |
649 | ret = wait_on_sync_kiocb(&iocb); | |
650 | return ret; | |
651 | } | |
652 | ||
653 | int kernel_recvmsg(struct socket *sock, struct msghdr *msg, | |
654 | struct kvec *vec, size_t num, | |
655 | size_t size, int flags) | |
656 | { | |
657 | mm_segment_t oldfs = get_fs(); | |
658 | int result; | |
659 | ||
660 | set_fs(KERNEL_DS); | |
661 | /* | |
662 | * the following is safe, since for compiler definitions of kvec and | |
663 | * iovec are identical, yielding the same in-core layout and alignment | |
664 | */ | |
665 | msg->msg_iov = (struct iovec *)vec, | |
666 | msg->msg_iovlen = num; | |
667 | result = sock_recvmsg(sock, msg, size, flags); | |
668 | set_fs(oldfs); | |
669 | return result; | |
670 | } | |
671 | ||
672 | static void sock_aio_dtor(struct kiocb *iocb) | |
673 | { | |
674 | kfree(iocb->private); | |
675 | } | |
676 | ||
ce1d4d3e CH |
677 | static ssize_t sock_sendpage(struct file *file, struct page *page, |
678 | int offset, size_t size, loff_t *ppos, int more) | |
1da177e4 | 679 | { |
1da177e4 LT |
680 | struct socket *sock; |
681 | int flags; | |
682 | ||
ce1d4d3e CH |
683 | sock = file->private_data; |
684 | ||
685 | flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT; | |
686 | if (more) | |
687 | flags |= MSG_MORE; | |
688 | ||
689 | return sock->ops->sendpage(sock, page, offset, size, flags); | |
690 | } | |
1da177e4 | 691 | |
ce1d4d3e CH |
692 | static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb, |
693 | char __user *ubuf, size_t size, struct sock_iocb *siocb) | |
694 | { | |
695 | if (!is_sync_kiocb(iocb)) { | |
696 | siocb = kmalloc(sizeof(*siocb), GFP_KERNEL); | |
697 | if (!siocb) | |
698 | return NULL; | |
1da177e4 LT |
699 | iocb->ki_dtor = sock_aio_dtor; |
700 | } | |
1da177e4 | 701 | |
ce1d4d3e CH |
702 | siocb->kiocb = iocb; |
703 | siocb->async_iov.iov_base = ubuf; | |
704 | siocb->async_iov.iov_len = size; | |
1da177e4 | 705 | |
ce1d4d3e CH |
706 | iocb->private = siocb; |
707 | return siocb; | |
1da177e4 LT |
708 | } |
709 | ||
ce1d4d3e CH |
710 | static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb, |
711 | struct file *file, struct iovec *iov, unsigned long nr_segs) | |
712 | { | |
713 | struct socket *sock = file->private_data; | |
714 | size_t size = 0; | |
715 | int i; | |
1da177e4 | 716 | |
ce1d4d3e CH |
717 | for (i = 0 ; i < nr_segs ; i++) |
718 | size += iov[i].iov_len; | |
1da177e4 | 719 | |
ce1d4d3e CH |
720 | msg->msg_name = NULL; |
721 | msg->msg_namelen = 0; | |
722 | msg->msg_control = NULL; | |
723 | msg->msg_controllen = 0; | |
724 | msg->msg_iov = (struct iovec *) iov; | |
725 | msg->msg_iovlen = nr_segs; | |
726 | msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; | |
727 | ||
728 | return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags); | |
729 | } | |
730 | ||
731 | static ssize_t sock_readv(struct file *file, const struct iovec *iov, | |
732 | unsigned long nr_segs, loff_t *ppos) | |
1da177e4 | 733 | { |
ce1d4d3e CH |
734 | struct kiocb iocb; |
735 | struct sock_iocb siocb; | |
736 | struct msghdr msg; | |
737 | int ret; | |
738 | ||
739 | init_sync_kiocb(&iocb, NULL); | |
740 | iocb.private = &siocb; | |
741 | ||
742 | ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs); | |
743 | if (-EIOCBQUEUED == ret) | |
744 | ret = wait_on_sync_kiocb(&iocb); | |
745 | return ret; | |
746 | } | |
747 | ||
748 | static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf, | |
749 | size_t count, loff_t pos) | |
750 | { | |
751 | struct sock_iocb siocb, *x; | |
752 | ||
1da177e4 LT |
753 | if (pos != 0) |
754 | return -ESPIPE; | |
ce1d4d3e | 755 | if (count == 0) /* Match SYS5 behaviour */ |
1da177e4 LT |
756 | return 0; |
757 | ||
ce1d4d3e CH |
758 | x = alloc_sock_iocb(iocb, ubuf, count, &siocb); |
759 | if (!x) | |
760 | return -ENOMEM; | |
761 | return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, | |
762 | &x->async_iov, 1); | |
1da177e4 LT |
763 | } |
764 | ||
ce1d4d3e CH |
765 | static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb, |
766 | struct file *file, struct iovec *iov, unsigned long nr_segs) | |
1da177e4 | 767 | { |
ce1d4d3e CH |
768 | struct socket *sock = file->private_data; |
769 | size_t size = 0; | |
770 | int i; | |
1da177e4 | 771 | |
ce1d4d3e CH |
772 | for (i = 0 ; i < nr_segs ; i++) |
773 | size += iov[i].iov_len; | |
1da177e4 | 774 | |
ce1d4d3e CH |
775 | msg->msg_name = NULL; |
776 | msg->msg_namelen = 0; | |
777 | msg->msg_control = NULL; | |
778 | msg->msg_controllen = 0; | |
779 | msg->msg_iov = (struct iovec *) iov; | |
780 | msg->msg_iovlen = nr_segs; | |
781 | msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; | |
782 | if (sock->type == SOCK_SEQPACKET) | |
783 | msg->msg_flags |= MSG_EOR; | |
1da177e4 | 784 | |
ce1d4d3e | 785 | return __sock_sendmsg(iocb, sock, msg, size); |
1da177e4 LT |
786 | } |
787 | ||
ce1d4d3e CH |
788 | static ssize_t sock_writev(struct file *file, const struct iovec *iov, |
789 | unsigned long nr_segs, loff_t *ppos) | |
1da177e4 LT |
790 | { |
791 | struct msghdr msg; | |
ce1d4d3e CH |
792 | struct kiocb iocb; |
793 | struct sock_iocb siocb; | |
794 | int ret; | |
1da177e4 | 795 | |
ce1d4d3e CH |
796 | init_sync_kiocb(&iocb, NULL); |
797 | iocb.private = &siocb; | |
1da177e4 | 798 | |
ce1d4d3e CH |
799 | ret = do_sock_write(&msg, &iocb, file, (struct iovec *)iov, nr_segs); |
800 | if (-EIOCBQUEUED == ret) | |
801 | ret = wait_on_sync_kiocb(&iocb); | |
802 | return ret; | |
803 | } | |
1da177e4 | 804 | |
ce1d4d3e CH |
805 | static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf, |
806 | size_t count, loff_t pos) | |
807 | { | |
808 | struct sock_iocb siocb, *x; | |
1da177e4 | 809 | |
ce1d4d3e CH |
810 | if (pos != 0) |
811 | return -ESPIPE; | |
812 | if (count == 0) /* Match SYS5 behaviour */ | |
813 | return 0; | |
1da177e4 | 814 | |
ce1d4d3e CH |
815 | x = alloc_sock_iocb(iocb, (void __user *)ubuf, count, &siocb); |
816 | if (!x) | |
817 | return -ENOMEM; | |
1da177e4 | 818 | |
ce1d4d3e CH |
819 | return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, |
820 | &x->async_iov, 1); | |
1da177e4 LT |
821 | } |
822 | ||
823 | ||
824 | /* | |
825 | * Atomic setting of ioctl hooks to avoid race | |
826 | * with module unload. | |
827 | */ | |
828 | ||
829 | static DECLARE_MUTEX(br_ioctl_mutex); | |
830 | static int (*br_ioctl_hook)(unsigned int cmd, void __user *arg) = NULL; | |
831 | ||
832 | void brioctl_set(int (*hook)(unsigned int, void __user *)) | |
833 | { | |
834 | down(&br_ioctl_mutex); | |
835 | br_ioctl_hook = hook; | |
836 | up(&br_ioctl_mutex); | |
837 | } | |
838 | EXPORT_SYMBOL(brioctl_set); | |
839 | ||
840 | static DECLARE_MUTEX(vlan_ioctl_mutex); | |
841 | static int (*vlan_ioctl_hook)(void __user *arg); | |
842 | ||
843 | void vlan_ioctl_set(int (*hook)(void __user *)) | |
844 | { | |
845 | down(&vlan_ioctl_mutex); | |
846 | vlan_ioctl_hook = hook; | |
847 | up(&vlan_ioctl_mutex); | |
848 | } | |
849 | EXPORT_SYMBOL(vlan_ioctl_set); | |
850 | ||
851 | static DECLARE_MUTEX(dlci_ioctl_mutex); | |
852 | static int (*dlci_ioctl_hook)(unsigned int, void __user *); | |
853 | ||
854 | void dlci_ioctl_set(int (*hook)(unsigned int, void __user *)) | |
855 | { | |
856 | down(&dlci_ioctl_mutex); | |
857 | dlci_ioctl_hook = hook; | |
858 | up(&dlci_ioctl_mutex); | |
859 | } | |
860 | EXPORT_SYMBOL(dlci_ioctl_set); | |
861 | ||
862 | /* | |
863 | * With an ioctl, arg may well be a user mode pointer, but we don't know | |
864 | * what to do with it - that's up to the protocol still. | |
865 | */ | |
866 | ||
867 | static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg) | |
868 | { | |
869 | struct socket *sock; | |
870 | void __user *argp = (void __user *)arg; | |
871 | int pid, err; | |
872 | ||
b69aee04 | 873 | sock = file->private_data; |
1da177e4 LT |
874 | if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) { |
875 | err = dev_ioctl(cmd, argp); | |
876 | } else | |
d86b5e0e | 877 | #ifdef CONFIG_WIRELESS_EXT |
1da177e4 LT |
878 | if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) { |
879 | err = dev_ioctl(cmd, argp); | |
880 | } else | |
d86b5e0e | 881 | #endif /* CONFIG_WIRELESS_EXT */ |
1da177e4 LT |
882 | switch (cmd) { |
883 | case FIOSETOWN: | |
884 | case SIOCSPGRP: | |
885 | err = -EFAULT; | |
886 | if (get_user(pid, (int __user *)argp)) | |
887 | break; | |
888 | err = f_setown(sock->file, pid, 1); | |
889 | break; | |
890 | case FIOGETOWN: | |
891 | case SIOCGPGRP: | |
892 | err = put_user(sock->file->f_owner.pid, (int __user *)argp); | |
893 | break; | |
894 | case SIOCGIFBR: | |
895 | case SIOCSIFBR: | |
896 | case SIOCBRADDBR: | |
897 | case SIOCBRDELBR: | |
898 | err = -ENOPKG; | |
899 | if (!br_ioctl_hook) | |
900 | request_module("bridge"); | |
901 | ||
902 | down(&br_ioctl_mutex); | |
903 | if (br_ioctl_hook) | |
904 | err = br_ioctl_hook(cmd, argp); | |
905 | up(&br_ioctl_mutex); | |
906 | break; | |
907 | case SIOCGIFVLAN: | |
908 | case SIOCSIFVLAN: | |
909 | err = -ENOPKG; | |
910 | if (!vlan_ioctl_hook) | |
911 | request_module("8021q"); | |
912 | ||
913 | down(&vlan_ioctl_mutex); | |
914 | if (vlan_ioctl_hook) | |
915 | err = vlan_ioctl_hook(argp); | |
916 | up(&vlan_ioctl_mutex); | |
917 | break; | |
918 | case SIOCGIFDIVERT: | |
919 | case SIOCSIFDIVERT: | |
920 | /* Convert this to call through a hook */ | |
921 | err = divert_ioctl(cmd, argp); | |
922 | break; | |
923 | case SIOCADDDLCI: | |
924 | case SIOCDELDLCI: | |
925 | err = -ENOPKG; | |
926 | if (!dlci_ioctl_hook) | |
927 | request_module("dlci"); | |
928 | ||
929 | if (dlci_ioctl_hook) { | |
930 | down(&dlci_ioctl_mutex); | |
931 | err = dlci_ioctl_hook(cmd, argp); | |
932 | up(&dlci_ioctl_mutex); | |
933 | } | |
934 | break; | |
935 | default: | |
936 | err = sock->ops->ioctl(sock, cmd, arg); | |
b5e5fa5e CH |
937 | |
938 | /* | |
939 | * If this ioctl is unknown try to hand it down | |
940 | * to the NIC driver. | |
941 | */ | |
942 | if (err == -ENOIOCTLCMD) | |
943 | err = dev_ioctl(cmd, argp); | |
1da177e4 LT |
944 | break; |
945 | } | |
946 | return err; | |
947 | } | |
948 | ||
949 | int sock_create_lite(int family, int type, int protocol, struct socket **res) | |
950 | { | |
951 | int err; | |
952 | struct socket *sock = NULL; | |
953 | ||
954 | err = security_socket_create(family, type, protocol, 1); | |
955 | if (err) | |
956 | goto out; | |
957 | ||
958 | sock = sock_alloc(); | |
959 | if (!sock) { | |
960 | err = -ENOMEM; | |
961 | goto out; | |
962 | } | |
963 | ||
964 | security_socket_post_create(sock, family, type, protocol, 1); | |
965 | sock->type = type; | |
966 | out: | |
967 | *res = sock; | |
968 | return err; | |
969 | } | |
970 | ||
971 | /* No kernel lock held - perfect */ | |
972 | static unsigned int sock_poll(struct file *file, poll_table * wait) | |
973 | { | |
974 | struct socket *sock; | |
975 | ||
976 | /* | |
977 | * We can't return errors to poll, so it's either yes or no. | |
978 | */ | |
b69aee04 | 979 | sock = file->private_data; |
1da177e4 LT |
980 | return sock->ops->poll(file, sock, wait); |
981 | } | |
982 | ||
983 | static int sock_mmap(struct file * file, struct vm_area_struct * vma) | |
984 | { | |
b69aee04 | 985 | struct socket *sock = file->private_data; |
1da177e4 LT |
986 | |
987 | return sock->ops->mmap(file, sock, vma); | |
988 | } | |
989 | ||
20380731 | 990 | static int sock_close(struct inode *inode, struct file *filp) |
1da177e4 LT |
991 | { |
992 | /* | |
993 | * It was possible the inode is NULL we were | |
994 | * closing an unfinished socket. | |
995 | */ | |
996 | ||
997 | if (!inode) | |
998 | { | |
999 | printk(KERN_DEBUG "sock_close: NULL inode\n"); | |
1000 | return 0; | |
1001 | } | |
1002 | sock_fasync(-1, filp, 0); | |
1003 | sock_release(SOCKET_I(inode)); | |
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | /* | |
1008 | * Update the socket async list | |
1009 | * | |
1010 | * Fasync_list locking strategy. | |
1011 | * | |
1012 | * 1. fasync_list is modified only under process context socket lock | |
1013 | * i.e. under semaphore. | |
1014 | * 2. fasync_list is used under read_lock(&sk->sk_callback_lock) | |
1015 | * or under socket lock. | |
1016 | * 3. fasync_list can be used from softirq context, so that | |
1017 | * modification under socket lock have to be enhanced with | |
1018 | * write_lock_bh(&sk->sk_callback_lock). | |
1019 | * --ANK (990710) | |
1020 | */ | |
1021 | ||
1022 | static int sock_fasync(int fd, struct file *filp, int on) | |
1023 | { | |
1024 | struct fasync_struct *fa, *fna=NULL, **prev; | |
1025 | struct socket *sock; | |
1026 | struct sock *sk; | |
1027 | ||
1028 | if (on) | |
1029 | { | |
8b3a7005 | 1030 | fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL); |
1da177e4 LT |
1031 | if(fna==NULL) |
1032 | return -ENOMEM; | |
1033 | } | |
1034 | ||
b69aee04 | 1035 | sock = filp->private_data; |
1da177e4 LT |
1036 | |
1037 | if ((sk=sock->sk) == NULL) { | |
1038 | kfree(fna); | |
1039 | return -EINVAL; | |
1040 | } | |
1041 | ||
1042 | lock_sock(sk); | |
1043 | ||
1044 | prev=&(sock->fasync_list); | |
1045 | ||
1046 | for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev) | |
1047 | if (fa->fa_file==filp) | |
1048 | break; | |
1049 | ||
1050 | if(on) | |
1051 | { | |
1052 | if(fa!=NULL) | |
1053 | { | |
1054 | write_lock_bh(&sk->sk_callback_lock); | |
1055 | fa->fa_fd=fd; | |
1056 | write_unlock_bh(&sk->sk_callback_lock); | |
1057 | ||
1058 | kfree(fna); | |
1059 | goto out; | |
1060 | } | |
1061 | fna->fa_file=filp; | |
1062 | fna->fa_fd=fd; | |
1063 | fna->magic=FASYNC_MAGIC; | |
1064 | fna->fa_next=sock->fasync_list; | |
1065 | write_lock_bh(&sk->sk_callback_lock); | |
1066 | sock->fasync_list=fna; | |
1067 | write_unlock_bh(&sk->sk_callback_lock); | |
1068 | } | |
1069 | else | |
1070 | { | |
1071 | if (fa!=NULL) | |
1072 | { | |
1073 | write_lock_bh(&sk->sk_callback_lock); | |
1074 | *prev=fa->fa_next; | |
1075 | write_unlock_bh(&sk->sk_callback_lock); | |
1076 | kfree(fa); | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | out: | |
1081 | release_sock(sock->sk); | |
1082 | return 0; | |
1083 | } | |
1084 | ||
1085 | /* This function may be called only under socket lock or callback_lock */ | |
1086 | ||
1087 | int sock_wake_async(struct socket *sock, int how, int band) | |
1088 | { | |
1089 | if (!sock || !sock->fasync_list) | |
1090 | return -1; | |
1091 | switch (how) | |
1092 | { | |
1093 | case 1: | |
1094 | ||
1095 | if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags)) | |
1096 | break; | |
1097 | goto call_kill; | |
1098 | case 2: | |
1099 | if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags)) | |
1100 | break; | |
1101 | /* fall through */ | |
1102 | case 0: | |
1103 | call_kill: | |
1104 | __kill_fasync(sock->fasync_list, SIGIO, band); | |
1105 | break; | |
1106 | case 3: | |
1107 | __kill_fasync(sock->fasync_list, SIGURG, band); | |
1108 | } | |
1109 | return 0; | |
1110 | } | |
1111 | ||
1112 | static int __sock_create(int family, int type, int protocol, struct socket **res, int kern) | |
1113 | { | |
1114 | int err; | |
1115 | struct socket *sock; | |
1116 | ||
1117 | /* | |
1118 | * Check protocol is in range | |
1119 | */ | |
1120 | if (family < 0 || family >= NPROTO) | |
1121 | return -EAFNOSUPPORT; | |
1122 | if (type < 0 || type >= SOCK_MAX) | |
1123 | return -EINVAL; | |
1124 | ||
1125 | /* Compatibility. | |
1126 | ||
1127 | This uglymoron is moved from INET layer to here to avoid | |
1128 | deadlock in module load. | |
1129 | */ | |
1130 | if (family == PF_INET && type == SOCK_PACKET) { | |
1131 | static int warned; | |
1132 | if (!warned) { | |
1133 | warned = 1; | |
1134 | printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm); | |
1135 | } | |
1136 | family = PF_PACKET; | |
1137 | } | |
1138 | ||
1139 | err = security_socket_create(family, type, protocol, kern); | |
1140 | if (err) | |
1141 | return err; | |
1142 | ||
1143 | #if defined(CONFIG_KMOD) | |
1144 | /* Attempt to load a protocol module if the find failed. | |
1145 | * | |
1146 | * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user | |
1147 | * requested real, full-featured networking support upon configuration. | |
1148 | * Otherwise module support will break! | |
1149 | */ | |
1150 | if (net_families[family]==NULL) | |
1151 | { | |
1152 | request_module("net-pf-%d",family); | |
1153 | } | |
1154 | #endif | |
1155 | ||
1156 | net_family_read_lock(); | |
1157 | if (net_families[family] == NULL) { | |
1158 | err = -EAFNOSUPPORT; | |
1159 | goto out; | |
1160 | } | |
1161 | ||
1162 | /* | |
1163 | * Allocate the socket and allow the family to set things up. if | |
1164 | * the protocol is 0, the family is instructed to select an appropriate | |
1165 | * default. | |
1166 | */ | |
1167 | ||
1168 | if (!(sock = sock_alloc())) { | |
1169 | printk(KERN_WARNING "socket: no more sockets\n"); | |
1170 | err = -ENFILE; /* Not exactly a match, but its the | |
1171 | closest posix thing */ | |
1172 | goto out; | |
1173 | } | |
1174 | ||
1175 | sock->type = type; | |
1176 | ||
1177 | /* | |
1178 | * We will call the ->create function, that possibly is in a loadable | |
1179 | * module, so we have to bump that loadable module refcnt first. | |
1180 | */ | |
1181 | err = -EAFNOSUPPORT; | |
1182 | if (!try_module_get(net_families[family]->owner)) | |
1183 | goto out_release; | |
1184 | ||
a79af59e FF |
1185 | if ((err = net_families[family]->create(sock, protocol)) < 0) { |
1186 | sock->ops = NULL; | |
1da177e4 | 1187 | goto out_module_put; |
a79af59e FF |
1188 | } |
1189 | ||
1da177e4 LT |
1190 | /* |
1191 | * Now to bump the refcnt of the [loadable] module that owns this | |
1192 | * socket at sock_release time we decrement its refcnt. | |
1193 | */ | |
1194 | if (!try_module_get(sock->ops->owner)) { | |
1195 | sock->ops = NULL; | |
1196 | goto out_module_put; | |
1197 | } | |
1198 | /* | |
1199 | * Now that we're done with the ->create function, the [loadable] | |
1200 | * module can have its refcnt decremented | |
1201 | */ | |
1202 | module_put(net_families[family]->owner); | |
1203 | *res = sock; | |
1204 | security_socket_post_create(sock, family, type, protocol, kern); | |
1205 | ||
1206 | out: | |
1207 | net_family_read_unlock(); | |
1208 | return err; | |
1209 | out_module_put: | |
1210 | module_put(net_families[family]->owner); | |
1211 | out_release: | |
1212 | sock_release(sock); | |
1213 | goto out; | |
1214 | } | |
1215 | ||
1216 | int sock_create(int family, int type, int protocol, struct socket **res) | |
1217 | { | |
1218 | return __sock_create(family, type, protocol, res, 0); | |
1219 | } | |
1220 | ||
1221 | int sock_create_kern(int family, int type, int protocol, struct socket **res) | |
1222 | { | |
1223 | return __sock_create(family, type, protocol, res, 1); | |
1224 | } | |
1225 | ||
1226 | asmlinkage long sys_socket(int family, int type, int protocol) | |
1227 | { | |
1228 | int retval; | |
1229 | struct socket *sock; | |
1230 | ||
1231 | retval = sock_create(family, type, protocol, &sock); | |
1232 | if (retval < 0) | |
1233 | goto out; | |
1234 | ||
1235 | retval = sock_map_fd(sock); | |
1236 | if (retval < 0) | |
1237 | goto out_release; | |
1238 | ||
1239 | out: | |
1240 | /* It may be already another descriptor 8) Not kernel problem. */ | |
1241 | return retval; | |
1242 | ||
1243 | out_release: | |
1244 | sock_release(sock); | |
1245 | return retval; | |
1246 | } | |
1247 | ||
1248 | /* | |
1249 | * Create a pair of connected sockets. | |
1250 | */ | |
1251 | ||
1252 | asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec) | |
1253 | { | |
1254 | struct socket *sock1, *sock2; | |
1255 | int fd1, fd2, err; | |
1256 | ||
1257 | /* | |
1258 | * Obtain the first socket and check if the underlying protocol | |
1259 | * supports the socketpair call. | |
1260 | */ | |
1261 | ||
1262 | err = sock_create(family, type, protocol, &sock1); | |
1263 | if (err < 0) | |
1264 | goto out; | |
1265 | ||
1266 | err = sock_create(family, type, protocol, &sock2); | |
1267 | if (err < 0) | |
1268 | goto out_release_1; | |
1269 | ||
1270 | err = sock1->ops->socketpair(sock1, sock2); | |
1271 | if (err < 0) | |
1272 | goto out_release_both; | |
1273 | ||
1274 | fd1 = fd2 = -1; | |
1275 | ||
1276 | err = sock_map_fd(sock1); | |
1277 | if (err < 0) | |
1278 | goto out_release_both; | |
1279 | fd1 = err; | |
1280 | ||
1281 | err = sock_map_fd(sock2); | |
1282 | if (err < 0) | |
1283 | goto out_close_1; | |
1284 | fd2 = err; | |
1285 | ||
1286 | /* fd1 and fd2 may be already another descriptors. | |
1287 | * Not kernel problem. | |
1288 | */ | |
1289 | ||
1290 | err = put_user(fd1, &usockvec[0]); | |
1291 | if (!err) | |
1292 | err = put_user(fd2, &usockvec[1]); | |
1293 | if (!err) | |
1294 | return 0; | |
1295 | ||
1296 | sys_close(fd2); | |
1297 | sys_close(fd1); | |
1298 | return err; | |
1299 | ||
1300 | out_close_1: | |
1301 | sock_release(sock2); | |
1302 | sys_close(fd1); | |
1303 | return err; | |
1304 | ||
1305 | out_release_both: | |
1306 | sock_release(sock2); | |
1307 | out_release_1: | |
1308 | sock_release(sock1); | |
1309 | out: | |
1310 | return err; | |
1311 | } | |
1312 | ||
1313 | ||
1314 | /* | |
1315 | * Bind a name to a socket. Nothing much to do here since it's | |
1316 | * the protocol's responsibility to handle the local address. | |
1317 | * | |
1318 | * We move the socket address to kernel space before we call | |
1319 | * the protocol layer (having also checked the address is ok). | |
1320 | */ | |
1321 | ||
1322 | asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen) | |
1323 | { | |
1324 | struct socket *sock; | |
1325 | char address[MAX_SOCK_ADDR]; | |
6cb153ca | 1326 | int err, fput_needed; |
1da177e4 | 1327 | |
6cb153ca | 1328 | if((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL) |
1da177e4 LT |
1329 | { |
1330 | if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) { | |
1331 | err = security_socket_bind(sock, (struct sockaddr *)address, addrlen); | |
6cb153ca BL |
1332 | if (!err) |
1333 | err = sock->ops->bind(sock, | |
1334 | (struct sockaddr *)address, addrlen); | |
1da177e4 | 1335 | } |
6cb153ca | 1336 | fput_light(sock->file, fput_needed); |
1da177e4 LT |
1337 | } |
1338 | return err; | |
1339 | } | |
1340 | ||
1341 | ||
1342 | /* | |
1343 | * Perform a listen. Basically, we allow the protocol to do anything | |
1344 | * necessary for a listen, and if that works, we mark the socket as | |
1345 | * ready for listening. | |
1346 | */ | |
1347 | ||
1348 | int sysctl_somaxconn = SOMAXCONN; | |
1349 | ||
1350 | asmlinkage long sys_listen(int fd, int backlog) | |
1351 | { | |
1352 | struct socket *sock; | |
6cb153ca | 1353 | int err, fput_needed; |
1da177e4 | 1354 | |
6cb153ca | 1355 | if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) { |
1da177e4 LT |
1356 | if ((unsigned) backlog > sysctl_somaxconn) |
1357 | backlog = sysctl_somaxconn; | |
1358 | ||
1359 | err = security_socket_listen(sock, backlog); | |
6cb153ca BL |
1360 | if (!err) |
1361 | err = sock->ops->listen(sock, backlog); | |
1da177e4 | 1362 | |
6cb153ca | 1363 | fput_light(sock->file, fput_needed); |
1da177e4 LT |
1364 | } |
1365 | return err; | |
1366 | } | |
1367 | ||
1368 | ||
1369 | /* | |
1370 | * For accept, we attempt to create a new socket, set up the link | |
1371 | * with the client, wake up the client, then return the new | |
1372 | * connected fd. We collect the address of the connector in kernel | |
1373 | * space and move it to user at the very end. This is unclean because | |
1374 | * we open the socket then return an error. | |
1375 | * | |
1376 | * 1003.1g adds the ability to recvmsg() to query connection pending | |
1377 | * status to recvmsg. We need to add that support in a way thats | |
1378 | * clean when we restucture accept also. | |
1379 | */ | |
1380 | ||
1381 | asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen) | |
1382 | { | |
1383 | struct socket *sock, *newsock; | |
39d8c1b6 | 1384 | struct file *newfile; |
6cb153ca | 1385 | int err, len, newfd, fput_needed; |
1da177e4 LT |
1386 | char address[MAX_SOCK_ADDR]; |
1387 | ||
6cb153ca | 1388 | sock = sockfd_lookup_light(fd, &err, &fput_needed); |
1da177e4 LT |
1389 | if (!sock) |
1390 | goto out; | |
1391 | ||
1392 | err = -ENFILE; | |
1393 | if (!(newsock = sock_alloc())) | |
1394 | goto out_put; | |
1395 | ||
1396 | newsock->type = sock->type; | |
1397 | newsock->ops = sock->ops; | |
1398 | ||
1da177e4 LT |
1399 | /* |
1400 | * We don't need try_module_get here, as the listening socket (sock) | |
1401 | * has the protocol module (sock->ops->owner) held. | |
1402 | */ | |
1403 | __module_get(newsock->ops->owner); | |
1404 | ||
39d8c1b6 DM |
1405 | newfd = sock_alloc_fd(&newfile); |
1406 | if (unlikely(newfd < 0)) { | |
1407 | err = newfd; | |
1408 | goto out_release; | |
1409 | } | |
1410 | ||
1411 | err = sock_attach_fd(newsock, newfile); | |
1412 | if (err < 0) | |
1413 | goto out_fd; | |
1414 | ||
a79af59e FF |
1415 | err = security_socket_accept(sock, newsock); |
1416 | if (err) | |
39d8c1b6 | 1417 | goto out_fd; |
a79af59e | 1418 | |
1da177e4 LT |
1419 | err = sock->ops->accept(sock, newsock, sock->file->f_flags); |
1420 | if (err < 0) | |
39d8c1b6 | 1421 | goto out_fd; |
1da177e4 LT |
1422 | |
1423 | if (upeer_sockaddr) { | |
1424 | if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) { | |
1425 | err = -ECONNABORTED; | |
39d8c1b6 | 1426 | goto out_fd; |
1da177e4 LT |
1427 | } |
1428 | err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen); | |
1429 | if (err < 0) | |
39d8c1b6 | 1430 | goto out_fd; |
1da177e4 LT |
1431 | } |
1432 | ||
1433 | /* File flags are not inherited via accept() unlike another OSes. */ | |
1434 | ||
39d8c1b6 DM |
1435 | fd_install(newfd, newfile); |
1436 | err = newfd; | |
1da177e4 LT |
1437 | |
1438 | security_socket_post_accept(sock, newsock); | |
1439 | ||
1440 | out_put: | |
6cb153ca | 1441 | fput_light(sock->file, fput_needed); |
1da177e4 LT |
1442 | out: |
1443 | return err; | |
39d8c1b6 DM |
1444 | out_fd: |
1445 | put_filp(newfile); | |
1446 | put_unused_fd(newfd); | |
1da177e4 LT |
1447 | out_release: |
1448 | sock_release(newsock); | |
1449 | goto out_put; | |
1450 | } | |
1451 | ||
1452 | ||
1453 | /* | |
1454 | * Attempt to connect to a socket with the server address. The address | |
1455 | * is in user space so we verify it is OK and move it to kernel space. | |
1456 | * | |
1457 | * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to | |
1458 | * break bindings | |
1459 | * | |
1460 | * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and | |
1461 | * other SEQPACKET protocols that take time to connect() as it doesn't | |
1462 | * include the -EINPROGRESS status for such sockets. | |
1463 | */ | |
1464 | ||
1465 | asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen) | |
1466 | { | |
1467 | struct socket *sock; | |
1468 | char address[MAX_SOCK_ADDR]; | |
6cb153ca | 1469 | int err, fput_needed; |
1da177e4 | 1470 | |
6cb153ca | 1471 | sock = sockfd_lookup_light(fd, &err, &fput_needed); |
1da177e4 LT |
1472 | if (!sock) |
1473 | goto out; | |
1474 | err = move_addr_to_kernel(uservaddr, addrlen, address); | |
1475 | if (err < 0) | |
1476 | goto out_put; | |
1477 | ||
1478 | err = security_socket_connect(sock, (struct sockaddr *)address, addrlen); | |
1479 | if (err) | |
1480 | goto out_put; | |
1481 | ||
1482 | err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen, | |
1483 | sock->file->f_flags); | |
1484 | out_put: | |
6cb153ca | 1485 | fput_light(sock->file, fput_needed); |
1da177e4 LT |
1486 | out: |
1487 | return err; | |
1488 | } | |
1489 | ||
1490 | /* | |
1491 | * Get the local address ('name') of a socket object. Move the obtained | |
1492 | * name to user space. | |
1493 | */ | |
1494 | ||
1495 | asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len) | |
1496 | { | |
1497 | struct socket *sock; | |
1498 | char address[MAX_SOCK_ADDR]; | |
6cb153ca | 1499 | int len, err, fput_needed; |
1da177e4 | 1500 | |
6cb153ca | 1501 | sock = sockfd_lookup_light(fd, &err, &fput_needed); |
1da177e4 LT |
1502 | if (!sock) |
1503 | goto out; | |
1504 | ||
1505 | err = security_socket_getsockname(sock); | |
1506 | if (err) | |
1507 | goto out_put; | |
1508 | ||
1509 | err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0); | |
1510 | if (err) | |
1511 | goto out_put; | |
1512 | err = move_addr_to_user(address, len, usockaddr, usockaddr_len); | |
1513 | ||
1514 | out_put: | |
6cb153ca | 1515 | fput_light(sock->file, fput_needed); |
1da177e4 LT |
1516 | out: |
1517 | return err; | |
1518 | } | |
1519 | ||
1520 | /* | |
1521 | * Get the remote address ('name') of a socket object. Move the obtained | |
1522 | * name to user space. | |
1523 | */ | |
1524 | ||
1525 | asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len) | |
1526 | { | |
1527 | struct socket *sock; | |
1528 | char address[MAX_SOCK_ADDR]; | |
6cb153ca | 1529 | int len, err, fput_needed; |
1da177e4 | 1530 | |
6cb153ca | 1531 | if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) { |
1da177e4 LT |
1532 | err = security_socket_getpeername(sock); |
1533 | if (err) { | |
6cb153ca | 1534 | fput_light(sock->file, fput_needed); |
1da177e4 LT |
1535 | return err; |
1536 | } | |
1537 | ||
1538 | err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1); | |
1539 | if (!err) | |
1540 | err=move_addr_to_user(address,len, usockaddr, usockaddr_len); | |
6cb153ca | 1541 | fput_light(sock->file, fput_needed); |
1da177e4 LT |
1542 | } |
1543 | return err; | |
1544 | } | |
1545 | ||
1546 | /* | |
1547 | * Send a datagram to a given address. We move the address into kernel | |
1548 | * space and check the user space data area is readable before invoking | |
1549 | * the protocol. | |
1550 | */ | |
1551 | ||
1552 | asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags, | |
1553 | struct sockaddr __user *addr, int addr_len) | |
1554 | { | |
1555 | struct socket *sock; | |
1556 | char address[MAX_SOCK_ADDR]; | |
1557 | int err; | |
1558 | struct msghdr msg; | |
1559 | struct iovec iov; | |
6cb153ca BL |
1560 | int fput_needed; |
1561 | struct file *sock_file; | |
1562 | ||
1563 | sock_file = fget_light(fd, &fput_needed); | |
1564 | if (!sock_file) | |
1565 | return -EBADF; | |
1566 | ||
1567 | sock = sock_from_file(sock_file, &err); | |
1da177e4 | 1568 | if (!sock) |
6cb153ca | 1569 | goto out_put; |
1da177e4 LT |
1570 | iov.iov_base=buff; |
1571 | iov.iov_len=len; | |
1572 | msg.msg_name=NULL; | |
1573 | msg.msg_iov=&iov; | |
1574 | msg.msg_iovlen=1; | |
1575 | msg.msg_control=NULL; | |
1576 | msg.msg_controllen=0; | |
1577 | msg.msg_namelen=0; | |
6cb153ca | 1578 | if (addr) { |
1da177e4 LT |
1579 | err = move_addr_to_kernel(addr, addr_len, address); |
1580 | if (err < 0) | |
1581 | goto out_put; | |
1582 | msg.msg_name=address; | |
1583 | msg.msg_namelen=addr_len; | |
1584 | } | |
1585 | if (sock->file->f_flags & O_NONBLOCK) | |
1586 | flags |= MSG_DONTWAIT; | |
1587 | msg.msg_flags = flags; | |
1588 | err = sock_sendmsg(sock, &msg, len); | |
1589 | ||
1590 | out_put: | |
6cb153ca | 1591 | fput_light(sock_file, fput_needed); |
1da177e4 LT |
1592 | return err; |
1593 | } | |
1594 | ||
1595 | /* | |
1596 | * Send a datagram down a socket. | |
1597 | */ | |
1598 | ||
1599 | asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags) | |
1600 | { | |
1601 | return sys_sendto(fd, buff, len, flags, NULL, 0); | |
1602 | } | |
1603 | ||
1604 | /* | |
1605 | * Receive a frame from the socket and optionally record the address of the | |
1606 | * sender. We verify the buffers are writable and if needed move the | |
1607 | * sender address from kernel to user space. | |
1608 | */ | |
1609 | ||
1610 | asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags, | |
1611 | struct sockaddr __user *addr, int __user *addr_len) | |
1612 | { | |
1613 | struct socket *sock; | |
1614 | struct iovec iov; | |
1615 | struct msghdr msg; | |
1616 | char address[MAX_SOCK_ADDR]; | |
1617 | int err,err2; | |
6cb153ca BL |
1618 | struct file *sock_file; |
1619 | int fput_needed; | |
1620 | ||
1621 | sock_file = fget_light(fd, &fput_needed); | |
1622 | if (!sock_file) | |
1623 | return -EBADF; | |
1da177e4 | 1624 | |
6cb153ca | 1625 | sock = sock_from_file(sock_file, &err); |
1da177e4 LT |
1626 | if (!sock) |
1627 | goto out; | |
1628 | ||
1629 | msg.msg_control=NULL; | |
1630 | msg.msg_controllen=0; | |
1631 | msg.msg_iovlen=1; | |
1632 | msg.msg_iov=&iov; | |
1633 | iov.iov_len=size; | |
1634 | iov.iov_base=ubuf; | |
1635 | msg.msg_name=address; | |
1636 | msg.msg_namelen=MAX_SOCK_ADDR; | |
1637 | if (sock->file->f_flags & O_NONBLOCK) | |
1638 | flags |= MSG_DONTWAIT; | |
1639 | err=sock_recvmsg(sock, &msg, size, flags); | |
1640 | ||
1641 | if(err >= 0 && addr != NULL) | |
1642 | { | |
1643 | err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len); | |
1644 | if(err2<0) | |
1645 | err=err2; | |
1646 | } | |
1da177e4 | 1647 | out: |
6cb153ca | 1648 | fput_light(sock_file, fput_needed); |
1da177e4 LT |
1649 | return err; |
1650 | } | |
1651 | ||
1652 | /* | |
1653 | * Receive a datagram from a socket. | |
1654 | */ | |
1655 | ||
1656 | asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags) | |
1657 | { | |
1658 | return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL); | |
1659 | } | |
1660 | ||
1661 | /* | |
1662 | * Set a socket option. Because we don't know the option lengths we have | |
1663 | * to pass the user mode parameter for the protocols to sort out. | |
1664 | */ | |
1665 | ||
1666 | asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen) | |
1667 | { | |
6cb153ca | 1668 | int err, fput_needed; |
1da177e4 LT |
1669 | struct socket *sock; |
1670 | ||
1671 | if (optlen < 0) | |
1672 | return -EINVAL; | |
1673 | ||
6cb153ca | 1674 | if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) |
1da177e4 LT |
1675 | { |
1676 | err = security_socket_setsockopt(sock,level,optname); | |
6cb153ca BL |
1677 | if (err) |
1678 | goto out_put; | |
1da177e4 LT |
1679 | |
1680 | if (level == SOL_SOCKET) | |
1681 | err=sock_setsockopt(sock,level,optname,optval,optlen); | |
1682 | else | |
1683 | err=sock->ops->setsockopt(sock, level, optname, optval, optlen); | |
6cb153ca BL |
1684 | out_put: |
1685 | fput_light(sock->file, fput_needed); | |
1da177e4 LT |
1686 | } |
1687 | return err; | |
1688 | } | |
1689 | ||
1690 | /* | |
1691 | * Get a socket option. Because we don't know the option lengths we have | |
1692 | * to pass a user mode parameter for the protocols to sort out. | |
1693 | */ | |
1694 | ||
1695 | asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen) | |
1696 | { | |
6cb153ca | 1697 | int err, fput_needed; |
1da177e4 LT |
1698 | struct socket *sock; |
1699 | ||
6cb153ca BL |
1700 | if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) { |
1701 | err = security_socket_getsockopt(sock, level, optname); | |
1702 | if (err) | |
1703 | goto out_put; | |
1da177e4 LT |
1704 | |
1705 | if (level == SOL_SOCKET) | |
1706 | err=sock_getsockopt(sock,level,optname,optval,optlen); | |
1707 | else | |
1708 | err=sock->ops->getsockopt(sock, level, optname, optval, optlen); | |
6cb153ca BL |
1709 | out_put: |
1710 | fput_light(sock->file, fput_needed); | |
1da177e4 LT |
1711 | } |
1712 | return err; | |
1713 | } | |
1714 | ||
1715 | ||
1716 | /* | |
1717 | * Shutdown a socket. | |
1718 | */ | |
1719 | ||
1720 | asmlinkage long sys_shutdown(int fd, int how) | |
1721 | { | |
6cb153ca | 1722 | int err, fput_needed; |
1da177e4 LT |
1723 | struct socket *sock; |
1724 | ||
6cb153ca | 1725 | if ((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL) |
1da177e4 LT |
1726 | { |
1727 | err = security_socket_shutdown(sock, how); | |
6cb153ca BL |
1728 | if (!err) |
1729 | err = sock->ops->shutdown(sock, how); | |
1730 | fput_light(sock->file, fput_needed); | |
1da177e4 LT |
1731 | } |
1732 | return err; | |
1733 | } | |
1734 | ||
1735 | /* A couple of helpful macros for getting the address of the 32/64 bit | |
1736 | * fields which are the same type (int / unsigned) on our platforms. | |
1737 | */ | |
1738 | #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member) | |
1739 | #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen) | |
1740 | #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags) | |
1741 | ||
1742 | ||
1743 | /* | |
1744 | * BSD sendmsg interface | |
1745 | */ | |
1746 | ||
1747 | asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags) | |
1748 | { | |
1749 | struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg; | |
1750 | struct socket *sock; | |
1751 | char address[MAX_SOCK_ADDR]; | |
1752 | struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; | |
b9d717a7 AW |
1753 | unsigned char ctl[sizeof(struct cmsghdr) + 20] |
1754 | __attribute__ ((aligned (sizeof(__kernel_size_t)))); | |
1755 | /* 20 is size of ipv6_pktinfo */ | |
1da177e4 LT |
1756 | unsigned char *ctl_buf = ctl; |
1757 | struct msghdr msg_sys; | |
1758 | int err, ctl_len, iov_size, total_len; | |
6cb153ca | 1759 | int fput_needed; |
1da177e4 LT |
1760 | |
1761 | err = -EFAULT; | |
1762 | if (MSG_CMSG_COMPAT & flags) { | |
1763 | if (get_compat_msghdr(&msg_sys, msg_compat)) | |
1764 | return -EFAULT; | |
1765 | } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr))) | |
1766 | return -EFAULT; | |
1767 | ||
6cb153ca | 1768 | sock = sockfd_lookup_light(fd, &err, &fput_needed); |
1da177e4 LT |
1769 | if (!sock) |
1770 | goto out; | |
1771 | ||
1772 | /* do not move before msg_sys is valid */ | |
1773 | err = -EMSGSIZE; | |
1774 | if (msg_sys.msg_iovlen > UIO_MAXIOV) | |
1775 | goto out_put; | |
1776 | ||
1777 | /* Check whether to allocate the iovec area*/ | |
1778 | err = -ENOMEM; | |
1779 | iov_size = msg_sys.msg_iovlen * sizeof(struct iovec); | |
1780 | if (msg_sys.msg_iovlen > UIO_FASTIOV) { | |
1781 | iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL); | |
1782 | if (!iov) | |
1783 | goto out_put; | |
1784 | } | |
1785 | ||
1786 | /* This will also move the address data into kernel space */ | |
1787 | if (MSG_CMSG_COMPAT & flags) { | |
1788 | err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ); | |
1789 | } else | |
1790 | err = verify_iovec(&msg_sys, iov, address, VERIFY_READ); | |
1791 | if (err < 0) | |
1792 | goto out_freeiov; | |
1793 | total_len = err; | |
1794 | ||
1795 | err = -ENOBUFS; | |
1796 | ||
1797 | if (msg_sys.msg_controllen > INT_MAX) | |
1798 | goto out_freeiov; | |
1799 | ctl_len = msg_sys.msg_controllen; | |
1800 | if ((MSG_CMSG_COMPAT & flags) && ctl_len) { | |
8920e8f9 | 1801 | err = cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, sizeof(ctl)); |
1da177e4 LT |
1802 | if (err) |
1803 | goto out_freeiov; | |
1804 | ctl_buf = msg_sys.msg_control; | |
8920e8f9 | 1805 | ctl_len = msg_sys.msg_controllen; |
1da177e4 LT |
1806 | } else if (ctl_len) { |
1807 | if (ctl_len > sizeof(ctl)) | |
1808 | { | |
1809 | ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL); | |
1810 | if (ctl_buf == NULL) | |
1811 | goto out_freeiov; | |
1812 | } | |
1813 | err = -EFAULT; | |
1814 | /* | |
1815 | * Careful! Before this, msg_sys.msg_control contains a user pointer. | |
1816 | * Afterwards, it will be a kernel pointer. Thus the compiler-assisted | |
1817 | * checking falls down on this. | |
1818 | */ | |
1819 | if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len)) | |
1820 | goto out_freectl; | |
1821 | msg_sys.msg_control = ctl_buf; | |
1822 | } | |
1823 | msg_sys.msg_flags = flags; | |
1824 | ||
1825 | if (sock->file->f_flags & O_NONBLOCK) | |
1826 | msg_sys.msg_flags |= MSG_DONTWAIT; | |
1827 | err = sock_sendmsg(sock, &msg_sys, total_len); | |
1828 | ||
1829 | out_freectl: | |
1830 | if (ctl_buf != ctl) | |
1831 | sock_kfree_s(sock->sk, ctl_buf, ctl_len); | |
1832 | out_freeiov: | |
1833 | if (iov != iovstack) | |
1834 | sock_kfree_s(sock->sk, iov, iov_size); | |
1835 | out_put: | |
6cb153ca | 1836 | fput_light(sock->file, fput_needed); |
1da177e4 LT |
1837 | out: |
1838 | return err; | |
1839 | } | |
1840 | ||
1841 | /* | |
1842 | * BSD recvmsg interface | |
1843 | */ | |
1844 | ||
1845 | asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags) | |
1846 | { | |
1847 | struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg; | |
1848 | struct socket *sock; | |
1849 | struct iovec iovstack[UIO_FASTIOV]; | |
1850 | struct iovec *iov=iovstack; | |
1851 | struct msghdr msg_sys; | |
1852 | unsigned long cmsg_ptr; | |
1853 | int err, iov_size, total_len, len; | |
6cb153ca | 1854 | int fput_needed; |
1da177e4 LT |
1855 | |
1856 | /* kernel mode address */ | |
1857 | char addr[MAX_SOCK_ADDR]; | |
1858 | ||
1859 | /* user mode address pointers */ | |
1860 | struct sockaddr __user *uaddr; | |
1861 | int __user *uaddr_len; | |
1862 | ||
1863 | if (MSG_CMSG_COMPAT & flags) { | |
1864 | if (get_compat_msghdr(&msg_sys, msg_compat)) | |
1865 | return -EFAULT; | |
1866 | } else | |
1867 | if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr))) | |
1868 | return -EFAULT; | |
1869 | ||
6cb153ca | 1870 | sock = sockfd_lookup_light(fd, &err, &fput_needed); |
1da177e4 LT |
1871 | if (!sock) |
1872 | goto out; | |
1873 | ||
1874 | err = -EMSGSIZE; | |
1875 | if (msg_sys.msg_iovlen > UIO_MAXIOV) | |
1876 | goto out_put; | |
1877 | ||
1878 | /* Check whether to allocate the iovec area*/ | |
1879 | err = -ENOMEM; | |
1880 | iov_size = msg_sys.msg_iovlen * sizeof(struct iovec); | |
1881 | if (msg_sys.msg_iovlen > UIO_FASTIOV) { | |
1882 | iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL); | |
1883 | if (!iov) | |
1884 | goto out_put; | |
1885 | } | |
1886 | ||
1887 | /* | |
1888 | * Save the user-mode address (verify_iovec will change the | |
1889 | * kernel msghdr to use the kernel address space) | |
1890 | */ | |
1891 | ||
1892 | uaddr = (void __user *) msg_sys.msg_name; | |
1893 | uaddr_len = COMPAT_NAMELEN(msg); | |
1894 | if (MSG_CMSG_COMPAT & flags) { | |
1895 | err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE); | |
1896 | } else | |
1897 | err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE); | |
1898 | if (err < 0) | |
1899 | goto out_freeiov; | |
1900 | total_len=err; | |
1901 | ||
1902 | cmsg_ptr = (unsigned long)msg_sys.msg_control; | |
1903 | msg_sys.msg_flags = 0; | |
1904 | if (MSG_CMSG_COMPAT & flags) | |
1905 | msg_sys.msg_flags = MSG_CMSG_COMPAT; | |
1906 | ||
1907 | if (sock->file->f_flags & O_NONBLOCK) | |
1908 | flags |= MSG_DONTWAIT; | |
1909 | err = sock_recvmsg(sock, &msg_sys, total_len, flags); | |
1910 | if (err < 0) | |
1911 | goto out_freeiov; | |
1912 | len = err; | |
1913 | ||
1914 | if (uaddr != NULL) { | |
1915 | err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len); | |
1916 | if (err < 0) | |
1917 | goto out_freeiov; | |
1918 | } | |
37f7f421 DM |
1919 | err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT), |
1920 | COMPAT_FLAGS(msg)); | |
1da177e4 LT |
1921 | if (err) |
1922 | goto out_freeiov; | |
1923 | if (MSG_CMSG_COMPAT & flags) | |
1924 | err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, | |
1925 | &msg_compat->msg_controllen); | |
1926 | else | |
1927 | err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, | |
1928 | &msg->msg_controllen); | |
1929 | if (err) | |
1930 | goto out_freeiov; | |
1931 | err = len; | |
1932 | ||
1933 | out_freeiov: | |
1934 | if (iov != iovstack) | |
1935 | sock_kfree_s(sock->sk, iov, iov_size); | |
1936 | out_put: | |
6cb153ca | 1937 | fput_light(sock->file, fput_needed); |
1da177e4 LT |
1938 | out: |
1939 | return err; | |
1940 | } | |
1941 | ||
1942 | #ifdef __ARCH_WANT_SYS_SOCKETCALL | |
1943 | ||
1944 | /* Argument list sizes for sys_socketcall */ | |
1945 | #define AL(x) ((x) * sizeof(unsigned long)) | |
1946 | static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3), | |
1947 | AL(3),AL(3),AL(4),AL(4),AL(4),AL(6), | |
1948 | AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)}; | |
1949 | #undef AL | |
1950 | ||
1951 | /* | |
1952 | * System call vectors. | |
1953 | * | |
1954 | * Argument checking cleaned up. Saved 20% in size. | |
1955 | * This function doesn't need to set the kernel lock because | |
1956 | * it is set by the callees. | |
1957 | */ | |
1958 | ||
1959 | asmlinkage long sys_socketcall(int call, unsigned long __user *args) | |
1960 | { | |
1961 | unsigned long a[6]; | |
1962 | unsigned long a0,a1; | |
1963 | int err; | |
1964 | ||
1965 | if(call<1||call>SYS_RECVMSG) | |
1966 | return -EINVAL; | |
1967 | ||
1968 | /* copy_from_user should be SMP safe. */ | |
1969 | if (copy_from_user(a, args, nargs[call])) | |
1970 | return -EFAULT; | |
3ec3b2fb | 1971 | |
4bcff1b3 | 1972 | err = audit_socketcall(nargs[call]/sizeof(unsigned long), a); |
3ec3b2fb DW |
1973 | if (err) |
1974 | return err; | |
1975 | ||
1da177e4 LT |
1976 | a0=a[0]; |
1977 | a1=a[1]; | |
1978 | ||
1979 | switch(call) | |
1980 | { | |
1981 | case SYS_SOCKET: | |
1982 | err = sys_socket(a0,a1,a[2]); | |
1983 | break; | |
1984 | case SYS_BIND: | |
1985 | err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]); | |
1986 | break; | |
1987 | case SYS_CONNECT: | |
1988 | err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]); | |
1989 | break; | |
1990 | case SYS_LISTEN: | |
1991 | err = sys_listen(a0,a1); | |
1992 | break; | |
1993 | case SYS_ACCEPT: | |
1994 | err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]); | |
1995 | break; | |
1996 | case SYS_GETSOCKNAME: | |
1997 | err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]); | |
1998 | break; | |
1999 | case SYS_GETPEERNAME: | |
2000 | err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]); | |
2001 | break; | |
2002 | case SYS_SOCKETPAIR: | |
2003 | err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]); | |
2004 | break; | |
2005 | case SYS_SEND: | |
2006 | err = sys_send(a0, (void __user *)a1, a[2], a[3]); | |
2007 | break; | |
2008 | case SYS_SENDTO: | |
2009 | err = sys_sendto(a0,(void __user *)a1, a[2], a[3], | |
2010 | (struct sockaddr __user *)a[4], a[5]); | |
2011 | break; | |
2012 | case SYS_RECV: | |
2013 | err = sys_recv(a0, (void __user *)a1, a[2], a[3]); | |
2014 | break; | |
2015 | case SYS_RECVFROM: | |
2016 | err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3], | |
2017 | (struct sockaddr __user *)a[4], (int __user *)a[5]); | |
2018 | break; | |
2019 | case SYS_SHUTDOWN: | |
2020 | err = sys_shutdown(a0,a1); | |
2021 | break; | |
2022 | case SYS_SETSOCKOPT: | |
2023 | err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]); | |
2024 | break; | |
2025 | case SYS_GETSOCKOPT: | |
2026 | err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]); | |
2027 | break; | |
2028 | case SYS_SENDMSG: | |
2029 | err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]); | |
2030 | break; | |
2031 | case SYS_RECVMSG: | |
2032 | err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]); | |
2033 | break; | |
2034 | default: | |
2035 | err = -EINVAL; | |
2036 | break; | |
2037 | } | |
2038 | return err; | |
2039 | } | |
2040 | ||
2041 | #endif /* __ARCH_WANT_SYS_SOCKETCALL */ | |
2042 | ||
2043 | /* | |
2044 | * This function is called by a protocol handler that wants to | |
2045 | * advertise its address family, and have it linked into the | |
2046 | * SOCKET module. | |
2047 | */ | |
2048 | ||
2049 | int sock_register(struct net_proto_family *ops) | |
2050 | { | |
2051 | int err; | |
2052 | ||
2053 | if (ops->family >= NPROTO) { | |
2054 | printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO); | |
2055 | return -ENOBUFS; | |
2056 | } | |
2057 | net_family_write_lock(); | |
2058 | err = -EEXIST; | |
2059 | if (net_families[ops->family] == NULL) { | |
2060 | net_families[ops->family]=ops; | |
2061 | err = 0; | |
2062 | } | |
2063 | net_family_write_unlock(); | |
2064 | printk(KERN_INFO "NET: Registered protocol family %d\n", | |
2065 | ops->family); | |
2066 | return err; | |
2067 | } | |
2068 | ||
2069 | /* | |
2070 | * This function is called by a protocol handler that wants to | |
2071 | * remove its address family, and have it unlinked from the | |
2072 | * SOCKET module. | |
2073 | */ | |
2074 | ||
2075 | int sock_unregister(int family) | |
2076 | { | |
2077 | if (family < 0 || family >= NPROTO) | |
2078 | return -1; | |
2079 | ||
2080 | net_family_write_lock(); | |
2081 | net_families[family]=NULL; | |
2082 | net_family_write_unlock(); | |
2083 | printk(KERN_INFO "NET: Unregistered protocol family %d\n", | |
2084 | family); | |
2085 | return 0; | |
2086 | } | |
2087 | ||
77d76ea3 | 2088 | static int __init sock_init(void) |
1da177e4 LT |
2089 | { |
2090 | /* | |
2091 | * Initialize sock SLAB cache. | |
2092 | */ | |
2093 | ||
2094 | sk_init(); | |
2095 | ||
1da177e4 LT |
2096 | /* |
2097 | * Initialize skbuff SLAB cache | |
2098 | */ | |
2099 | skb_init(); | |
1da177e4 LT |
2100 | |
2101 | /* | |
2102 | * Initialize the protocols module. | |
2103 | */ | |
2104 | ||
2105 | init_inodecache(); | |
2106 | register_filesystem(&sock_fs_type); | |
2107 | sock_mnt = kern_mount(&sock_fs_type); | |
77d76ea3 AK |
2108 | |
2109 | /* The real protocol initialization is performed in later initcalls. | |
1da177e4 LT |
2110 | */ |
2111 | ||
2112 | #ifdef CONFIG_NETFILTER | |
2113 | netfilter_init(); | |
2114 | #endif | |
cbeb321a DM |
2115 | |
2116 | return 0; | |
1da177e4 LT |
2117 | } |
2118 | ||
77d76ea3 AK |
2119 | core_initcall(sock_init); /* early initcall */ |
2120 | ||
1da177e4 LT |
2121 | #ifdef CONFIG_PROC_FS |
2122 | void socket_seq_show(struct seq_file *seq) | |
2123 | { | |
2124 | int cpu; | |
2125 | int counter = 0; | |
2126 | ||
88a2a4ac | 2127 | for_each_cpu(cpu) |
1da177e4 LT |
2128 | counter += per_cpu(sockets_in_use, cpu); |
2129 | ||
2130 | /* It can be negative, by the way. 8) */ | |
2131 | if (counter < 0) | |
2132 | counter = 0; | |
2133 | ||
2134 | seq_printf(seq, "sockets: used %d\n", counter); | |
2135 | } | |
2136 | #endif /* CONFIG_PROC_FS */ | |
2137 | ||
2138 | /* ABI emulation layers need these two */ | |
2139 | EXPORT_SYMBOL(move_addr_to_kernel); | |
2140 | EXPORT_SYMBOL(move_addr_to_user); | |
2141 | EXPORT_SYMBOL(sock_create); | |
2142 | EXPORT_SYMBOL(sock_create_kern); | |
2143 | EXPORT_SYMBOL(sock_create_lite); | |
2144 | EXPORT_SYMBOL(sock_map_fd); | |
2145 | EXPORT_SYMBOL(sock_recvmsg); | |
2146 | EXPORT_SYMBOL(sock_register); | |
2147 | EXPORT_SYMBOL(sock_release); | |
2148 | EXPORT_SYMBOL(sock_sendmsg); | |
2149 | EXPORT_SYMBOL(sock_unregister); | |
2150 | EXPORT_SYMBOL(sock_wake_async); | |
2151 | EXPORT_SYMBOL(sockfd_lookup); | |
2152 | EXPORT_SYMBOL(kernel_sendmsg); | |
2153 | EXPORT_SYMBOL(kernel_recvmsg); |