aboutsummaryrefslogtreecommitdiffstats
path: root/net/socket.c
diff options
context:
space:
mode:
authorStephen Hemminger <shemminger@osdl.org>2006-09-01 03:19:31 -0400
committerDavid S. Miller <davem@sunset.davemloft.net>2006-09-22 17:54:17 -0400
commit89bddce58e85bb18b13f5077e8349ba9a3ee2597 (patch)
treeec91323f823a43be782efcfd2914ae6a2edf7ab6 /net/socket.c
parent53fad3cbff120d8987f377eff374cf4db4ecb177 (diff)
[NET] socket: code style cleanup
Make socket.c conform to current style: * run through Lindent * get rid of unneeded casts * split assignment and comparsion where possible Signed-off-by: Stephen Hemminger <shemminger@osdl.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/socket.c')
-rw-r--r--net/socket.c729
1 files changed, 388 insertions, 341 deletions
diff --git a/net/socket.c b/net/socket.c
index 2eaebf934a1a..156f2efa4e4a 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -42,7 +42,7 @@
42 * Andi Kleen : Some small cleanups, optimizations, 42 * Andi Kleen : Some small cleanups, optimizations,
43 * and fixed a copy_from_user() bug. 43 * and fixed a copy_from_user() bug.
44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0) 44 * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
45 * Tigran Aivazian : Made listen(2) backlog sanity checks 45 * Tigran Aivazian : Made listen(2) backlog sanity checks
46 * protocol-independent 46 * protocol-independent
47 * 47 *
48 * 48 *
@@ -53,7 +53,7 @@
53 * 53 *
54 * 54 *
55 * This module is effectively the top level interface to the BSD socket 55 * This module is effectively the top level interface to the BSD socket
56 * paradigm. 56 * paradigm.
57 * 57 *
58 * Based upon Swansea University Computer Society NET3.039 58 * Based upon Swansea University Computer Society NET3.039
59 */ 59 */
@@ -96,25 +96,24 @@
96 96
97static int sock_no_open(struct inode *irrelevant, struct file *dontcare); 97static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
98static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf, 98static ssize_t sock_aio_read(struct kiocb *iocb, char __user *buf,
99 size_t size, loff_t pos); 99 size_t size, loff_t pos);
100static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf, 100static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *buf,
101 size_t size, loff_t pos); 101 size_t size, loff_t pos);
102static int sock_mmap(struct file *file, struct vm_area_struct * vma); 102static int sock_mmap(struct file *file, struct vm_area_struct *vma);
103 103
104static int sock_close(struct inode *inode, struct file *file); 104static int sock_close(struct inode *inode, struct file *file);
105static unsigned int sock_poll(struct file *file, 105static unsigned int sock_poll(struct file *file,
106 struct poll_table_struct *wait); 106 struct poll_table_struct *wait);
107static long sock_ioctl(struct file *file, 107static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
108 unsigned int cmd, unsigned long arg);
109#ifdef CONFIG_COMPAT 108#ifdef CONFIG_COMPAT
110static long compat_sock_ioctl(struct file *file, 109static long compat_sock_ioctl(struct file *file,
111 unsigned int cmd, unsigned long arg); 110 unsigned int cmd, unsigned long arg);
112#endif 111#endif
113static int sock_fasync(int fd, struct file *filp, int on); 112static int sock_fasync(int fd, struct file *filp, int on);
114static ssize_t sock_readv(struct file *file, const struct iovec *vector, 113static ssize_t sock_readv(struct file *file, const struct iovec *vector,
115 unsigned long count, loff_t *ppos); 114 unsigned long count, loff_t *ppos);
116static ssize_t sock_writev(struct file *file, const struct iovec *vector, 115static ssize_t sock_writev(struct file *file, const struct iovec *vector,
117 unsigned long count, loff_t *ppos); 116 unsigned long count, loff_t *ppos);
118static ssize_t sock_sendpage(struct file *file, struct page *page, 117static ssize_t sock_sendpage(struct file *file, struct page *page,
119 int offset, size_t size, loff_t *ppos, int more); 118 int offset, size_t size, loff_t *ppos, int more);
120 119
@@ -193,7 +192,6 @@ static __inline__ void net_family_read_unlock(void)
193#define net_family_read_unlock() do { } while(0) 192#define net_family_read_unlock() do { } while(0)
194#endif 193#endif
195 194
196
197/* 195/*
198 * Statistics counters of the socket lists 196 * Statistics counters of the socket lists
199 */ 197 */
@@ -201,19 +199,20 @@ static __inline__ void net_family_read_unlock(void)
201static DEFINE_PER_CPU(int, sockets_in_use) = 0; 199static DEFINE_PER_CPU(int, sockets_in_use) = 0;
202 200
203/* 201/*
204 * Support routines. Move socket addresses back and forth across the kernel/user 202 * Support routines.
205 * divide and look after the messy bits. 203 * Move socket addresses back and forth across the kernel/user
204 * divide and look after the messy bits.
206 */ 205 */
207 206
208#define MAX_SOCK_ADDR 128 /* 108 for Unix domain - 207#define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
209 16 for IP, 16 for IPX, 208 16 for IP, 16 for IPX,
210 24 for IPv6, 209 24 for IPv6,
211 about 80 for AX.25 210 about 80 for AX.25
212 must be at least one bigger than 211 must be at least one bigger than
213 the AF_UNIX size (see net/unix/af_unix.c 212 the AF_UNIX size (see net/unix/af_unix.c
214 :unix_mkname()). 213 :unix_mkname()).
215 */ 214 */
216 215
217/** 216/**
218 * move_addr_to_kernel - copy a socket address into kernel space 217 * move_addr_to_kernel - copy a socket address into kernel space
219 * @uaddr: Address in user space 218 * @uaddr: Address in user space
@@ -227,11 +226,11 @@ static DEFINE_PER_CPU(int, sockets_in_use) = 0;
227 226
228int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr) 227int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
229{ 228{
230 if(ulen<0||ulen>MAX_SOCK_ADDR) 229 if (ulen < 0 || ulen > MAX_SOCK_ADDR)
231 return -EINVAL; 230 return -EINVAL;
232 if(ulen==0) 231 if (ulen == 0)
233 return 0; 232 return 0;
234 if(copy_from_user(kaddr,uaddr,ulen)) 233 if (copy_from_user(kaddr, uaddr, ulen))
235 return -EFAULT; 234 return -EFAULT;
236 return audit_sockaddr(ulen, kaddr); 235 return audit_sockaddr(ulen, kaddr);
237} 236}
@@ -252,44 +251,46 @@ int move_addr_to_kernel(void __user *uaddr, int ulen, void *kaddr)
252 * length of the data is written over the length limit the user 251 * length of the data is written over the length limit the user
253 * specified. Zero is returned for a success. 252 * specified. Zero is returned for a success.
254 */ 253 */
255 254
256int move_addr_to_user(void *kaddr, int klen, void __user *uaddr, int __user *ulen) 255int move_addr_to_user(void *kaddr, int klen, void __user *uaddr,
256 int __user *ulen)
257{ 257{
258 int err; 258 int err;
259 int len; 259 int len;
260 260
261 if((err=get_user(len, ulen))) 261 err = get_user(len, ulen);
262 if (err)
262 return err; 263 return err;
263 if(len>klen) 264 if (len > klen)
264 len=klen; 265 len = klen;
265 if(len<0 || len> MAX_SOCK_ADDR) 266 if (len < 0 || len > MAX_SOCK_ADDR)
266 return -EINVAL; 267 return -EINVAL;
267 if(len) 268 if (len) {
268 {
269 if (audit_sockaddr(klen, kaddr)) 269 if (audit_sockaddr(klen, kaddr))
270 return -ENOMEM; 270 return -ENOMEM;
271 if(copy_to_user(uaddr,kaddr,len)) 271 if (copy_to_user(uaddr, kaddr, len))
272 return -EFAULT; 272 return -EFAULT;
273 } 273 }
274 /* 274 /*
275 * "fromlen shall refer to the value before truncation.." 275 * "fromlen shall refer to the value before truncation.."
276 * 1003.1g 276 * 1003.1g
277 */ 277 */
278 return __put_user(klen, ulen); 278 return __put_user(klen, ulen);
279} 279}
280 280
281#define SOCKFS_MAGIC 0x534F434B 281#define SOCKFS_MAGIC 0x534F434B
282 282
283static kmem_cache_t * sock_inode_cachep __read_mostly; 283static kmem_cache_t *sock_inode_cachep __read_mostly;
284 284
285static struct inode *sock_alloc_inode(struct super_block *sb) 285static struct inode *sock_alloc_inode(struct super_block *sb)
286{ 286{
287 struct socket_alloc *ei; 287 struct socket_alloc *ei;
288 ei = (struct socket_alloc *)kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL); 288
289 ei = kmem_cache_alloc(sock_inode_cachep, SLAB_KERNEL);
289 if (!ei) 290 if (!ei)
290 return NULL; 291 return NULL;
291 init_waitqueue_head(&ei->socket.wait); 292 init_waitqueue_head(&ei->socket.wait);
292 293
293 ei->socket.fasync_list = NULL; 294 ei->socket.fasync_list = NULL;
294 ei->socket.state = SS_UNCONNECTED; 295 ei->socket.state = SS_UNCONNECTED;
295 ei->socket.flags = 0; 296 ei->socket.flags = 0;
@@ -307,22 +308,25 @@ static void sock_destroy_inode(struct inode *inode)
307 container_of(inode, struct socket_alloc, vfs_inode)); 308 container_of(inode, struct socket_alloc, vfs_inode));
308} 309}
309 310
310static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) 311static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
311{ 312{
312 struct socket_alloc *ei = (struct socket_alloc *) foo; 313 struct socket_alloc *ei = (struct socket_alloc *)foo;
313 314
314 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == 315 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR))
315 SLAB_CTOR_CONSTRUCTOR) 316 == SLAB_CTOR_CONSTRUCTOR)
316 inode_init_once(&ei->vfs_inode); 317 inode_init_once(&ei->vfs_inode);
317} 318}
318 319
319static int init_inodecache(void) 320static int init_inodecache(void)
320{ 321{
321 sock_inode_cachep = kmem_cache_create("sock_inode_cache", 322 sock_inode_cachep = kmem_cache_create("sock_inode_cache",
322 sizeof(struct socket_alloc), 323 sizeof(struct socket_alloc),
323 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 324 0,
324 SLAB_MEM_SPREAD), 325 (SLAB_HWCACHE_ALIGN |
325 init_once, NULL); 326 SLAB_RECLAIM_ACCOUNT |
327 SLAB_MEM_SPREAD),
328 init_once,
329 NULL);
326 if (sock_inode_cachep == NULL) 330 if (sock_inode_cachep == NULL)
327 return -ENOMEM; 331 return -ENOMEM;
328 return 0; 332 return 0;
@@ -335,7 +339,8 @@ static struct super_operations sockfs_ops = {
335}; 339};
336 340
337static int sockfs_get_sb(struct file_system_type *fs_type, 341static int sockfs_get_sb(struct file_system_type *fs_type,
338 int flags, const char *dev_name, void *data, struct vfsmount *mnt) 342 int flags, const char *dev_name, void *data,
343 struct vfsmount *mnt)
339{ 344{
340 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC, 345 return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
341 mnt); 346 mnt);
@@ -348,12 +353,13 @@ static struct file_system_type sock_fs_type = {
348 .get_sb = sockfs_get_sb, 353 .get_sb = sockfs_get_sb,
349 .kill_sb = kill_anon_super, 354 .kill_sb = kill_anon_super,
350}; 355};
356
351static int sockfs_delete_dentry(struct dentry *dentry) 357static int sockfs_delete_dentry(struct dentry *dentry)
352{ 358{
353 return 1; 359 return 1;
354} 360}
355static struct dentry_operations sockfs_dentry_operations = { 361static struct dentry_operations sockfs_dentry_operations = {
356 .d_delete = sockfs_delete_dentry, 362 .d_delete = sockfs_delete_dentry,
357}; 363};
358 364
359/* 365/*
@@ -477,10 +483,12 @@ struct socket *sockfd_lookup(int fd, int *err)
477 struct file *file; 483 struct file *file;
478 struct socket *sock; 484 struct socket *sock;
479 485
480 if (!(file = fget(fd))) { 486 file = fget(fd);
487 if (!file) {
481 *err = -EBADF; 488 *err = -EBADF;
482 return NULL; 489 return NULL;
483 } 490 }
491
484 sock = sock_from_file(file, err); 492 sock = sock_from_file(file, err);
485 if (!sock) 493 if (!sock)
486 fput(file); 494 fput(file);
@@ -505,7 +513,7 @@ static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
505 513
506/** 514/**
507 * sock_alloc - allocate a socket 515 * sock_alloc - allocate a socket
508 * 516 *
509 * Allocate a new inode and socket object. The two are bound together 517 * Allocate a new inode and socket object. The two are bound together
510 * and initialised. The socket is then returned. If we are out of inodes 518 * and initialised. The socket is then returned. If we are out of inodes
511 * NULL is returned. 519 * NULL is returned.
@@ -513,8 +521,8 @@ static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
513 521
514static struct socket *sock_alloc(void) 522static struct socket *sock_alloc(void)
515{ 523{
516 struct inode * inode; 524 struct inode *inode;
517 struct socket * sock; 525 struct socket *sock;
518 526
519 inode = new_inode(sock_mnt->mnt_sb); 527 inode = new_inode(sock_mnt->mnt_sb);
520 if (!inode) 528 if (!inode)
@@ -522,7 +530,7 @@ static struct socket *sock_alloc(void)
522 530
523 sock = SOCKET_I(inode); 531 sock = SOCKET_I(inode);
524 532
525 inode->i_mode = S_IFSOCK|S_IRWXUGO; 533 inode->i_mode = S_IFSOCK | S_IRWXUGO;
526 inode->i_uid = current->fsuid; 534 inode->i_uid = current->fsuid;
527 inode->i_gid = current->fsgid; 535 inode->i_gid = current->fsgid;
528 536
@@ -536,7 +544,7 @@ static struct socket *sock_alloc(void)
536 * a back door. Remember to keep it shut otherwise you'll let the 544 * a back door. Remember to keep it shut otherwise you'll let the
537 * creepy crawlies in. 545 * creepy crawlies in.
538 */ 546 */
539 547
540static int sock_no_open(struct inode *irrelevant, struct file *dontcare) 548static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
541{ 549{
542 return -ENXIO; 550 return -ENXIO;
@@ -553,9 +561,9 @@ const struct file_operations bad_sock_fops = {
553 * 561 *
554 * The socket is released from the protocol stack if it has a release 562 * The socket is released from the protocol stack if it has a release
555 * callback, and the inode is then released if the socket is bound to 563 * callback, and the inode is then released if the socket is bound to
556 * an inode not a file. 564 * an inode not a file.
557 */ 565 */
558 566
559void sock_release(struct socket *sock) 567void sock_release(struct socket *sock)
560{ 568{
561 if (sock->ops) { 569 if (sock->ops) {
@@ -575,10 +583,10 @@ void sock_release(struct socket *sock)
575 iput(SOCK_INODE(sock)); 583 iput(SOCK_INODE(sock));
576 return; 584 return;
577 } 585 }
578 sock->file=NULL; 586 sock->file = NULL;
579} 587}
580 588
581static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock, 589static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
582 struct msghdr *msg, size_t size) 590 struct msghdr *msg, size_t size)
583{ 591{
584 struct sock_iocb *si = kiocb_to_siocb(iocb); 592 struct sock_iocb *si = kiocb_to_siocb(iocb);
@@ -621,14 +629,14 @@ int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
621 * the following is safe, since for compiler definitions of kvec and 629 * the following is safe, since for compiler definitions of kvec and
622 * iovec are identical, yielding the same in-core layout and alignment 630 * iovec are identical, yielding the same in-core layout and alignment
623 */ 631 */
624 msg->msg_iov = (struct iovec *)vec, 632 msg->msg_iov = (struct iovec *)vec;
625 msg->msg_iovlen = num; 633 msg->msg_iovlen = num;
626 result = sock_sendmsg(sock, msg, size); 634 result = sock_sendmsg(sock, msg, size);
627 set_fs(oldfs); 635 set_fs(oldfs);
628 return result; 636 return result;
629} 637}
630 638
631static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock, 639static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
632 struct msghdr *msg, size_t size, int flags) 640 struct msghdr *msg, size_t size, int flags)
633{ 641{
634 int err; 642 int err;
@@ -647,14 +655,14 @@ static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
647 return sock->ops->recvmsg(iocb, sock, msg, size, flags); 655 return sock->ops->recvmsg(iocb, sock, msg, size, flags);
648} 656}
649 657
650int sock_recvmsg(struct socket *sock, struct msghdr *msg, 658int sock_recvmsg(struct socket *sock, struct msghdr *msg,
651 size_t size, int flags) 659 size_t size, int flags)
652{ 660{
653 struct kiocb iocb; 661 struct kiocb iocb;
654 struct sock_iocb siocb; 662 struct sock_iocb siocb;
655 int ret; 663 int ret;
656 664
657 init_sync_kiocb(&iocb, NULL); 665 init_sync_kiocb(&iocb, NULL);
658 iocb.private = &siocb; 666 iocb.private = &siocb;
659 ret = __sock_recvmsg(&iocb, sock, msg, size, flags); 667 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
660 if (-EIOCBQUEUED == ret) 668 if (-EIOCBQUEUED == ret)
@@ -662,9 +670,8 @@ int sock_recvmsg(struct socket *sock, struct msghdr *msg,
662 return ret; 670 return ret;
663} 671}
664 672
665int kernel_recvmsg(struct socket *sock, struct msghdr *msg, 673int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
666 struct kvec *vec, size_t num, 674 struct kvec *vec, size_t num, size_t size, int flags)
667 size_t size, int flags)
668{ 675{
669 mm_segment_t oldfs = get_fs(); 676 mm_segment_t oldfs = get_fs();
670 int result; 677 int result;
@@ -674,8 +681,7 @@ int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
674 * the following is safe, since for compiler definitions of kvec and 681 * the following is safe, since for compiler definitions of kvec and
675 * iovec are identical, yielding the same in-core layout and alignment 682 * iovec are identical, yielding the same in-core layout and alignment
676 */ 683 */
677 msg->msg_iov = (struct iovec *)vec, 684 msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
678 msg->msg_iovlen = num;
679 result = sock_recvmsg(sock, msg, size, flags); 685 result = sock_recvmsg(sock, msg, size, flags);
680 set_fs(oldfs); 686 set_fs(oldfs);
681 return result; 687 return result;
@@ -702,7 +708,8 @@ static ssize_t sock_sendpage(struct file *file, struct page *page,
702} 708}
703 709
704static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb, 710static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
705 char __user *ubuf, size_t size, struct sock_iocb *siocb) 711 char __user *ubuf, size_t size,
712 struct sock_iocb *siocb)
706{ 713{
707 if (!is_sync_kiocb(iocb)) { 714 if (!is_sync_kiocb(iocb)) {
708 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL); 715 siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
@@ -720,20 +727,21 @@ static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
720} 727}
721 728
722static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb, 729static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
723 struct file *file, struct iovec *iov, unsigned long nr_segs) 730 struct file *file, struct iovec *iov,
731 unsigned long nr_segs)
724{ 732{
725 struct socket *sock = file->private_data; 733 struct socket *sock = file->private_data;
726 size_t size = 0; 734 size_t size = 0;
727 int i; 735 int i;
728 736
729 for (i = 0 ; i < nr_segs ; i++) 737 for (i = 0; i < nr_segs; i++)
730 size += iov[i].iov_len; 738 size += iov[i].iov_len;
731 739
732 msg->msg_name = NULL; 740 msg->msg_name = NULL;
733 msg->msg_namelen = 0; 741 msg->msg_namelen = 0;
734 msg->msg_control = NULL; 742 msg->msg_control = NULL;
735 msg->msg_controllen = 0; 743 msg->msg_controllen = 0;
736 msg->msg_iov = (struct iovec *) iov; 744 msg->msg_iov = (struct iovec *)iov;
737 msg->msg_iovlen = nr_segs; 745 msg->msg_iovlen = nr_segs;
738 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; 746 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
739 747
@@ -748,7 +756,7 @@ static ssize_t sock_readv(struct file *file, const struct iovec *iov,
748 struct msghdr msg; 756 struct msghdr msg;
749 int ret; 757 int ret;
750 758
751 init_sync_kiocb(&iocb, NULL); 759 init_sync_kiocb(&iocb, NULL);
752 iocb.private = &siocb; 760 iocb.private = &siocb;
753 761
754 ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs); 762 ret = do_sock_read(&msg, &iocb, file, (struct iovec *)iov, nr_segs);
@@ -758,7 +766,7 @@ static ssize_t sock_readv(struct file *file, const struct iovec *iov,
758} 766}
759 767
760static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf, 768static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
761 size_t count, loff_t pos) 769 size_t count, loff_t pos)
762{ 770{
763 struct sock_iocb siocb, *x; 771 struct sock_iocb siocb, *x;
764 772
@@ -771,24 +779,25 @@ static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
771 if (!x) 779 if (!x)
772 return -ENOMEM; 780 return -ENOMEM;
773 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, 781 return do_sock_read(&x->async_msg, iocb, iocb->ki_filp,
774 &x->async_iov, 1); 782 &x->async_iov, 1);
775} 783}
776 784
777static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb, 785static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
778 struct file *file, struct iovec *iov, unsigned long nr_segs) 786 struct file *file, struct iovec *iov,
787 unsigned long nr_segs)
779{ 788{
780 struct socket *sock = file->private_data; 789 struct socket *sock = file->private_data;
781 size_t size = 0; 790 size_t size = 0;
782 int i; 791 int i;
783 792
784 for (i = 0 ; i < nr_segs ; i++) 793 for (i = 0; i < nr_segs; i++)
785 size += iov[i].iov_len; 794 size += iov[i].iov_len;
786 795
787 msg->msg_name = NULL; 796 msg->msg_name = NULL;
788 msg->msg_namelen = 0; 797 msg->msg_namelen = 0;
789 msg->msg_control = NULL; 798 msg->msg_control = NULL;
790 msg->msg_controllen = 0; 799 msg->msg_controllen = 0;
791 msg->msg_iov = (struct iovec *) iov; 800 msg->msg_iov = (struct iovec *)iov;
792 msg->msg_iovlen = nr_segs; 801 msg->msg_iovlen = nr_segs;
793 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0; 802 msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
794 if (sock->type == SOCK_SEQPACKET) 803 if (sock->type == SOCK_SEQPACKET)
@@ -815,7 +824,7 @@ static ssize_t sock_writev(struct file *file, const struct iovec *iov,
815} 824}
816 825
817static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf, 826static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
818 size_t count, loff_t pos) 827 size_t count, loff_t pos)
819{ 828{
820 struct sock_iocb siocb, *x; 829 struct sock_iocb siocb, *x;
821 830
@@ -829,46 +838,48 @@ static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
829 return -ENOMEM; 838 return -ENOMEM;
830 839
831 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, 840 return do_sock_write(&x->async_msg, iocb, iocb->ki_filp,
832 &x->async_iov, 1); 841 &x->async_iov, 1);
833} 842}
834 843
835
836/* 844/*
837 * Atomic setting of ioctl hooks to avoid race 845 * Atomic setting of ioctl hooks to avoid race
838 * with module unload. 846 * with module unload.
839 */ 847 */
840 848
841static DEFINE_MUTEX(br_ioctl_mutex); 849static DEFINE_MUTEX(br_ioctl_mutex);
842static int (*br_ioctl_hook)(unsigned int cmd, void __user *arg) = NULL; 850static int (*br_ioctl_hook) (unsigned int cmd, void __user *arg) = NULL;
843 851
844void brioctl_set(int (*hook)(unsigned int, void __user *)) 852void brioctl_set(int (*hook) (unsigned int, void __user *))
845{ 853{
846 mutex_lock(&br_ioctl_mutex); 854 mutex_lock(&br_ioctl_mutex);
847 br_ioctl_hook = hook; 855 br_ioctl_hook = hook;
848 mutex_unlock(&br_ioctl_mutex); 856 mutex_unlock(&br_ioctl_mutex);
849} 857}
858
850EXPORT_SYMBOL(brioctl_set); 859EXPORT_SYMBOL(brioctl_set);
851 860
852static DEFINE_MUTEX(vlan_ioctl_mutex); 861static DEFINE_MUTEX(vlan_ioctl_mutex);
853static int (*vlan_ioctl_hook)(void __user *arg); 862static int (*vlan_ioctl_hook) (void __user *arg);
854 863
855void vlan_ioctl_set(int (*hook)(void __user *)) 864void vlan_ioctl_set(int (*hook) (void __user *))
856{ 865{
857 mutex_lock(&vlan_ioctl_mutex); 866 mutex_lock(&vlan_ioctl_mutex);
858 vlan_ioctl_hook = hook; 867 vlan_ioctl_hook = hook;
859 mutex_unlock(&vlan_ioctl_mutex); 868 mutex_unlock(&vlan_ioctl_mutex);
860} 869}
870
861EXPORT_SYMBOL(vlan_ioctl_set); 871EXPORT_SYMBOL(vlan_ioctl_set);
862 872
863static DEFINE_MUTEX(dlci_ioctl_mutex); 873static DEFINE_MUTEX(dlci_ioctl_mutex);
864static int (*dlci_ioctl_hook)(unsigned int, void __user *); 874static int (*dlci_ioctl_hook) (unsigned int, void __user *);
865 875
866void dlci_ioctl_set(int (*hook)(unsigned int, void __user *)) 876void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
867{ 877{
868 mutex_lock(&dlci_ioctl_mutex); 878 mutex_lock(&dlci_ioctl_mutex);
869 dlci_ioctl_hook = hook; 879 dlci_ioctl_hook = hook;
870 mutex_unlock(&dlci_ioctl_mutex); 880 mutex_unlock(&dlci_ioctl_mutex);
871} 881}
882
872EXPORT_SYMBOL(dlci_ioctl_set); 883EXPORT_SYMBOL(dlci_ioctl_set);
873 884
874/* 885/*
@@ -890,8 +901,8 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
890 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) { 901 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
891 err = dev_ioctl(cmd, argp); 902 err = dev_ioctl(cmd, argp);
892 } else 903 } else
893#endif /* CONFIG_WIRELESS_EXT */ 904#endif /* CONFIG_WIRELESS_EXT */
894 switch (cmd) { 905 switch (cmd) {
895 case FIOSETOWN: 906 case FIOSETOWN:
896 case SIOCSPGRP: 907 case SIOCSPGRP:
897 err = -EFAULT; 908 err = -EFAULT;
@@ -901,7 +912,8 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
901 break; 912 break;
902 case FIOGETOWN: 913 case FIOGETOWN:
903 case SIOCGPGRP: 914 case SIOCGPGRP:
904 err = put_user(sock->file->f_owner.pid, (int __user *)argp); 915 err = put_user(sock->file->f_owner.pid,
916 (int __user *)argp);
905 break; 917 break;
906 case SIOCGIFBR: 918 case SIOCGIFBR:
907 case SIOCSIFBR: 919 case SIOCSIFBR:
@@ -912,7 +924,7 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
912 request_module("bridge"); 924 request_module("bridge");
913 925
914 mutex_lock(&br_ioctl_mutex); 926 mutex_lock(&br_ioctl_mutex);
915 if (br_ioctl_hook) 927 if (br_ioctl_hook)
916 err = br_ioctl_hook(cmd, argp); 928 err = br_ioctl_hook(cmd, argp);
917 mutex_unlock(&br_ioctl_mutex); 929 mutex_unlock(&br_ioctl_mutex);
918 break; 930 break;
@@ -929,7 +941,7 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
929 break; 941 break;
930 case SIOCGIFDIVERT: 942 case SIOCGIFDIVERT:
931 case SIOCSIFDIVERT: 943 case SIOCSIFDIVERT:
932 /* Convert this to call through a hook */ 944 /* Convert this to call through a hook */
933 err = divert_ioctl(cmd, argp); 945 err = divert_ioctl(cmd, argp);
934 break; 946 break;
935 case SIOCADDDLCI: 947 case SIOCADDDLCI:
@@ -954,7 +966,7 @@ static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
954 if (err == -ENOIOCTLCMD) 966 if (err == -ENOIOCTLCMD)
955 err = dev_ioctl(cmd, argp); 967 err = dev_ioctl(cmd, argp);
956 break; 968 break;
957 } 969 }
958 return err; 970 return err;
959} 971}
960 972
@@ -962,7 +974,7 @@ int sock_create_lite(int family, int type, int protocol, struct socket **res)
962{ 974{
963 int err; 975 int err;
964 struct socket *sock = NULL; 976 struct socket *sock = NULL;
965 977
966 err = security_socket_create(family, type, protocol, 1); 978 err = security_socket_create(family, type, protocol, 1);
967 if (err) 979 if (err)
968 goto out; 980 goto out;
@@ -988,18 +1000,18 @@ out_release:
988} 1000}
989 1001
990/* No kernel lock held - perfect */ 1002/* No kernel lock held - perfect */
991static unsigned int sock_poll(struct file *file, poll_table * wait) 1003static unsigned int sock_poll(struct file *file, poll_table *wait)
992{ 1004{
993 struct socket *sock; 1005 struct socket *sock;
994 1006
995 /* 1007 /*
996 * We can't return errors to poll, so it's either yes or no. 1008 * We can't return errors to poll, so it's either yes or no.
997 */ 1009 */
998 sock = file->private_data; 1010 sock = file->private_data;
999 return sock->ops->poll(file, sock, wait); 1011 return sock->ops->poll(file, sock, wait);
1000} 1012}
1001 1013
1002static int sock_mmap(struct file * file, struct vm_area_struct * vma) 1014static int sock_mmap(struct file *file, struct vm_area_struct *vma)
1003{ 1015{
1004 struct socket *sock = file->private_data; 1016 struct socket *sock = file->private_data;
1005 1017
@@ -1009,12 +1021,11 @@ static int sock_mmap(struct file * file, struct vm_area_struct * vma)
1009static int sock_close(struct inode *inode, struct file *filp) 1021static int sock_close(struct inode *inode, struct file *filp)
1010{ 1022{
1011 /* 1023 /*
1012 * It was possible the inode is NULL we were 1024 * It was possible the inode is NULL we were
1013 * closing an unfinished socket. 1025 * closing an unfinished socket.
1014 */ 1026 */
1015 1027
1016 if (!inode) 1028 if (!inode) {
1017 {
1018 printk(KERN_DEBUG "sock_close: NULL inode\n"); 1029 printk(KERN_DEBUG "sock_close: NULL inode\n");
1019 return 0; 1030 return 0;
1020 } 1031 }
@@ -1040,57 +1051,52 @@ static int sock_close(struct inode *inode, struct file *filp)
1040 1051
1041static int sock_fasync(int fd, struct file *filp, int on) 1052static int sock_fasync(int fd, struct file *filp, int on)
1042{ 1053{
1043 struct fasync_struct *fa, *fna=NULL, **prev; 1054 struct fasync_struct *fa, *fna = NULL, **prev;
1044 struct socket *sock; 1055 struct socket *sock;
1045 struct sock *sk; 1056 struct sock *sk;
1046 1057
1047 if (on) 1058 if (on) {
1048 {
1049 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL); 1059 fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
1050 if(fna==NULL) 1060 if (fna == NULL)
1051 return -ENOMEM; 1061 return -ENOMEM;
1052 } 1062 }
1053 1063
1054 sock = filp->private_data; 1064 sock = filp->private_data;
1055 1065
1056 if ((sk=sock->sk) == NULL) { 1066 sk = sock->sk;
1067 if (sk == NULL) {
1057 kfree(fna); 1068 kfree(fna);
1058 return -EINVAL; 1069 return -EINVAL;
1059 } 1070 }
1060 1071
1061 lock_sock(sk); 1072 lock_sock(sk);
1062 1073
1063 prev=&(sock->fasync_list); 1074 prev = &(sock->fasync_list);
1064 1075
1065 for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev) 1076 for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
1066 if (fa->fa_file==filp) 1077 if (fa->fa_file == filp)
1067 break; 1078 break;
1068 1079
1069 if(on) 1080 if (on) {
1070 { 1081 if (fa != NULL) {
1071 if(fa!=NULL)
1072 {
1073 write_lock_bh(&sk->sk_callback_lock); 1082 write_lock_bh(&sk->sk_callback_lock);
1074 fa->fa_fd=fd; 1083 fa->fa_fd = fd;
1075 write_unlock_bh(&sk->sk_callback_lock); 1084 write_unlock_bh(&sk->sk_callback_lock);
1076 1085
1077 kfree(fna); 1086 kfree(fna);
1078 goto out; 1087 goto out;
1079 } 1088 }
1080 fna->fa_file=filp; 1089 fna->fa_file = filp;
1081 fna->fa_fd=fd; 1090 fna->fa_fd = fd;
1082 fna->magic=FASYNC_MAGIC; 1091 fna->magic = FASYNC_MAGIC;
1083 fna->fa_next=sock->fasync_list; 1092 fna->fa_next = sock->fasync_list;
1084 write_lock_bh(&sk->sk_callback_lock); 1093 write_lock_bh(&sk->sk_callback_lock);
1085 sock->fasync_list=fna; 1094 sock->fasync_list = fna;
1086 write_unlock_bh(&sk->sk_callback_lock); 1095 write_unlock_bh(&sk->sk_callback_lock);
1087 } 1096 } else {
1088 else 1097 if (fa != NULL) {
1089 {
1090 if (fa!=NULL)
1091 {
1092 write_lock_bh(&sk->sk_callback_lock); 1098 write_lock_bh(&sk->sk_callback_lock);
1093 *prev=fa->fa_next; 1099 *prev = fa->fa_next;
1094 write_unlock_bh(&sk->sk_callback_lock); 1100 write_unlock_bh(&sk->sk_callback_lock);
1095 kfree(fa); 1101 kfree(fa);
1096 } 1102 }
@@ -1107,10 +1113,9 @@ int sock_wake_async(struct socket *sock, int how, int band)
1107{ 1113{
1108 if (!sock || !sock->fasync_list) 1114 if (!sock || !sock->fasync_list)
1109 return -1; 1115 return -1;
1110 switch (how) 1116 switch (how) {
1111 {
1112 case 1: 1117 case 1:
1113 1118
1114 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags)) 1119 if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
1115 break; 1120 break;
1116 goto call_kill; 1121 goto call_kill;
@@ -1119,7 +1124,7 @@ int sock_wake_async(struct socket *sock, int how, int band)
1119 break; 1124 break;
1120 /* fall through */ 1125 /* fall through */
1121 case 0: 1126 case 0:
1122 call_kill: 1127call_kill:
1123 __kill_fasync(sock->fasync_list, SIGIO, band); 1128 __kill_fasync(sock->fasync_list, SIGIO, band);
1124 break; 1129 break;
1125 case 3: 1130 case 3:
@@ -1128,13 +1133,14 @@ int sock_wake_async(struct socket *sock, int how, int band)
1128 return 0; 1133 return 0;
1129} 1134}
1130 1135
1131static int __sock_create(int family, int type, int protocol, struct socket **res, int kern) 1136static int __sock_create(int family, int type, int protocol,
1137 struct socket **res, int kern)
1132{ 1138{
1133 int err; 1139 int err;
1134 struct socket *sock; 1140 struct socket *sock;
1135 1141
1136 /* 1142 /*
1137 * Check protocol is in range 1143 * Check protocol is in range
1138 */ 1144 */
1139 if (family < 0 || family >= NPROTO) 1145 if (family < 0 || family >= NPROTO)
1140 return -EAFNOSUPPORT; 1146 return -EAFNOSUPPORT;
@@ -1147,10 +1153,11 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
1147 deadlock in module load. 1153 deadlock in module load.
1148 */ 1154 */
1149 if (family == PF_INET && type == SOCK_PACKET) { 1155 if (family == PF_INET && type == SOCK_PACKET) {
1150 static int warned; 1156 static int warned;
1151 if (!warned) { 1157 if (!warned) {
1152 warned = 1; 1158 warned = 1;
1153 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm); 1159 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
1160 current->comm);
1154 } 1161 }
1155 family = PF_PACKET; 1162 family = PF_PACKET;
1156 } 1163 }
@@ -1158,17 +1165,16 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
1158 err = security_socket_create(family, type, protocol, kern); 1165 err = security_socket_create(family, type, protocol, kern);
1159 if (err) 1166 if (err)
1160 return err; 1167 return err;
1161 1168
1162#if defined(CONFIG_KMOD) 1169#if defined(CONFIG_KMOD)
1163 /* Attempt to load a protocol module if the find failed. 1170 /* Attempt to load a protocol module if the find failed.
1164 * 1171 *
1165 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user 1172 * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
1166 * requested real, full-featured networking support upon configuration. 1173 * requested real, full-featured networking support upon configuration.
1167 * Otherwise module support will break! 1174 * Otherwise module support will break!
1168 */ 1175 */
1169 if (net_families[family]==NULL) 1176 if (net_families[family] == NULL) {
1170 { 1177 request_module("net-pf-%d", family);
1171 request_module("net-pf-%d",family);
1172 } 1178 }
1173#endif 1179#endif
1174 1180
@@ -1187,12 +1193,12 @@ static int __sock_create(int family, int type, int protocol, struct socket **res
1187 if (!(sock = sock_alloc())) { 1193 if (!(sock = sock_alloc())) {
1188 if (net_ratelimit()) 1194 if (net_ratelimit())
1189 printk(KERN_WARNING "socket: no more sockets\n"); 1195 printk(KERN_WARNING "socket: no more sockets\n");
1190 err = -ENFILE; /* Not exactly a match, but its the 1196 err = -ENFILE; /* Not exactly a match, but its the
1191 closest posix thing */ 1197 closest posix thing */
1192 goto out; 1198 goto out;
1193 } 1199 }
1194 1200
1195 sock->type = type; 1201 sock->type = type;
1196 1202
1197 /* 1203 /*
1198 * We will call the ->create function, that possibly is in a loadable 1204 * We will call the ->create function, that possibly is in a loadable
@@ -1271,7 +1277,8 @@ out_release:
1271 * Create a pair of connected sockets. 1277 * Create a pair of connected sockets.
1272 */ 1278 */
1273 1279
1274asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec) 1280asmlinkage long sys_socketpair(int family, int type, int protocol,
1281 int __user *usockvec)
1275{ 1282{
1276 struct socket *sock1, *sock2; 1283 struct socket *sock1, *sock2;
1277 int fd1, fd2, err; 1284 int fd1, fd2, err;
@@ -1290,7 +1297,7 @@ asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *u
1290 goto out_release_1; 1297 goto out_release_1;
1291 1298
1292 err = sock1->ops->socketpair(sock1, sock2); 1299 err = sock1->ops->socketpair(sock1, sock2);
1293 if (err < 0) 1300 if (err < 0)
1294 goto out_release_both; 1301 goto out_release_both;
1295 1302
1296 fd1 = fd2 = -1; 1303 fd1 = fd2 = -1;
@@ -1309,7 +1316,7 @@ asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *u
1309 * Not kernel problem. 1316 * Not kernel problem.
1310 */ 1317 */
1311 1318
1312 err = put_user(fd1, &usockvec[0]); 1319 err = put_user(fd1, &usockvec[0]);
1313 if (!err) 1320 if (!err)
1314 err = put_user(fd2, &usockvec[1]); 1321 err = put_user(fd2, &usockvec[1]);
1315 if (!err) 1322 if (!err)
@@ -1320,19 +1327,18 @@ asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *u
1320 return err; 1327 return err;
1321 1328
1322out_close_1: 1329out_close_1:
1323 sock_release(sock2); 1330 sock_release(sock2);
1324 sys_close(fd1); 1331 sys_close(fd1);
1325 return err; 1332 return err;
1326 1333
1327out_release_both: 1334out_release_both:
1328 sock_release(sock2); 1335 sock_release(sock2);
1329out_release_1: 1336out_release_1:
1330 sock_release(sock1); 1337 sock_release(sock1);
1331out: 1338out:
1332 return err; 1339 return err;
1333} 1340}
1334 1341
1335
1336/* 1342/*
1337 * Bind a name to a socket. Nothing much to do here since it's 1343 * Bind a name to a socket. Nothing much to do here since it's
1338 * the protocol's responsibility to handle the local address. 1344 * the protocol's responsibility to handle the local address.
@@ -1347,20 +1353,23 @@ asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
1347 char address[MAX_SOCK_ADDR]; 1353 char address[MAX_SOCK_ADDR];
1348 int err, fput_needed; 1354 int err, fput_needed;
1349 1355
1350 if((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL) 1356 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1351 { 1357 if(sock) {
1352 if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) { 1358 err = move_addr_to_kernel(umyaddr, addrlen, address);
1353 err = security_socket_bind(sock, (struct sockaddr *)address, addrlen); 1359 if (err >= 0) {
1360 err = security_socket_bind(sock,
1361 (struct sockaddr *)address,
1362 addrlen);
1354 if (!err) 1363 if (!err)
1355 err = sock->ops->bind(sock, 1364 err = sock->ops->bind(sock,
1356 (struct sockaddr *)address, addrlen); 1365 (struct sockaddr *)
1366 address, addrlen);
1357 } 1367 }
1358 fput_light(sock->file, fput_needed); 1368 fput_light(sock->file, fput_needed);
1359 } 1369 }
1360 return err; 1370 return err;
1361} 1371}
1362 1372
1363
1364/* 1373/*
1365 * Perform a listen. Basically, we allow the protocol to do anything 1374 * Perform a listen. Basically, we allow the protocol to do anything
1366 * necessary for a listen, and if that works, we mark the socket as 1375 * necessary for a listen, and if that works, we mark the socket as
@@ -1373,9 +1382,10 @@ asmlinkage long sys_listen(int fd, int backlog)
1373{ 1382{
1374 struct socket *sock; 1383 struct socket *sock;
1375 int err, fput_needed; 1384 int err, fput_needed;
1376 1385
1377 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) { 1386 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1378 if ((unsigned) backlog > sysctl_somaxconn) 1387 if (sock) {
1388 if ((unsigned)backlog > sysctl_somaxconn)
1379 backlog = sysctl_somaxconn; 1389 backlog = sysctl_somaxconn;
1380 1390
1381 err = security_socket_listen(sock, backlog); 1391 err = security_socket_listen(sock, backlog);
@@ -1387,7 +1397,6 @@ asmlinkage long sys_listen(int fd, int backlog)
1387 return err; 1397 return err;
1388} 1398}
1389 1399
1390
1391/* 1400/*
1392 * For accept, we attempt to create a new socket, set up the link 1401 * For accept, we attempt to create a new socket, set up the link
1393 * with the client, wake up the client, then return the new 1402 * with the client, wake up the client, then return the new
@@ -1400,7 +1409,8 @@ asmlinkage long sys_listen(int fd, int backlog)
1400 * clean when we restucture accept also. 1409 * clean when we restucture accept also.
1401 */ 1410 */
1402 1411
1403asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen) 1412asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
1413 int __user *upeer_addrlen)
1404{ 1414{
1405 struct socket *sock, *newsock; 1415 struct socket *sock, *newsock;
1406 struct file *newfile; 1416 struct file *newfile;
@@ -1412,7 +1422,7 @@ asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int _
1412 goto out; 1422 goto out;
1413 1423
1414 err = -ENFILE; 1424 err = -ENFILE;
1415 if (!(newsock = sock_alloc())) 1425 if (!(newsock = sock_alloc()))
1416 goto out_put; 1426 goto out_put;
1417 1427
1418 newsock->type = sock->type; 1428 newsock->type = sock->type;
@@ -1444,11 +1454,13 @@ asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int _
1444 goto out_fd; 1454 goto out_fd;
1445 1455
1446 if (upeer_sockaddr) { 1456 if (upeer_sockaddr) {
1447 if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) { 1457 if (newsock->ops->getname(newsock, (struct sockaddr *)address,
1458 &len, 2) < 0) {
1448 err = -ECONNABORTED; 1459 err = -ECONNABORTED;
1449 goto out_fd; 1460 goto out_fd;
1450 } 1461 }
1451 err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen); 1462 err = move_addr_to_user(address, len, upeer_sockaddr,
1463 upeer_addrlen);
1452 if (err < 0) 1464 if (err < 0)
1453 goto out_fd; 1465 goto out_fd;
1454 } 1466 }
@@ -1470,7 +1482,6 @@ out_fd:
1470 goto out_put; 1482 goto out_put;
1471} 1483}
1472 1484
1473
1474/* 1485/*
1475 * Attempt to connect to a socket with the server address. The address 1486 * Attempt to connect to a socket with the server address. The address
1476 * is in user space so we verify it is OK and move it to kernel space. 1487 * is in user space so we verify it is OK and move it to kernel space.
@@ -1483,7 +1494,8 @@ out_fd:
1483 * include the -EINPROGRESS status for such sockets. 1494 * include the -EINPROGRESS status for such sockets.
1484 */ 1495 */
1485 1496
1486asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen) 1497asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
1498 int addrlen)
1487{ 1499{
1488 struct socket *sock; 1500 struct socket *sock;
1489 char address[MAX_SOCK_ADDR]; 1501 char address[MAX_SOCK_ADDR];
@@ -1496,11 +1508,12 @@ asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrl
1496 if (err < 0) 1508 if (err < 0)
1497 goto out_put; 1509 goto out_put;
1498 1510
1499 err = security_socket_connect(sock, (struct sockaddr *)address, addrlen); 1511 err =
1512 security_socket_connect(sock, (struct sockaddr *)address, addrlen);
1500 if (err) 1513 if (err)
1501 goto out_put; 1514 goto out_put;
1502 1515
1503 err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen, 1516 err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
1504 sock->file->f_flags); 1517 sock->file->f_flags);
1505out_put: 1518out_put:
1506 fput_light(sock->file, fput_needed); 1519 fput_light(sock->file, fput_needed);
@@ -1513,12 +1526,13 @@ out:
1513 * name to user space. 1526 * name to user space.
1514 */ 1527 */
1515 1528
1516asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len) 1529asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
1530 int __user *usockaddr_len)
1517{ 1531{
1518 struct socket *sock; 1532 struct socket *sock;
1519 char address[MAX_SOCK_ADDR]; 1533 char address[MAX_SOCK_ADDR];
1520 int len, err, fput_needed; 1534 int len, err, fput_needed;
1521 1535
1522 sock = sockfd_lookup_light(fd, &err, &fput_needed); 1536 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1523 if (!sock) 1537 if (!sock)
1524 goto out; 1538 goto out;
@@ -1543,22 +1557,27 @@ out:
1543 * name to user space. 1557 * name to user space.
1544 */ 1558 */
1545 1559
1546asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len) 1560asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
1561 int __user *usockaddr_len)
1547{ 1562{
1548 struct socket *sock; 1563 struct socket *sock;
1549 char address[MAX_SOCK_ADDR]; 1564 char address[MAX_SOCK_ADDR];
1550 int len, err, fput_needed; 1565 int len, err, fput_needed;
1551 1566
1552 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) { 1567 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1568 if (sock != NULL) {
1553 err = security_socket_getpeername(sock); 1569 err = security_socket_getpeername(sock);
1554 if (err) { 1570 if (err) {
1555 fput_light(sock->file, fput_needed); 1571 fput_light(sock->file, fput_needed);
1556 return err; 1572 return err;
1557 } 1573 }
1558 1574
1559 err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1); 1575 err =
1576 sock->ops->getname(sock, (struct sockaddr *)address, &len,
1577 1);
1560 if (!err) 1578 if (!err)
1561 err=move_addr_to_user(address,len, usockaddr, usockaddr_len); 1579 err = move_addr_to_user(address, len, usockaddr,
1580 usockaddr_len);
1562 fput_light(sock->file, fput_needed); 1581 fput_light(sock->file, fput_needed);
1563 } 1582 }
1564 return err; 1583 return err;
@@ -1570,8 +1589,9 @@ asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int _
1570 * the protocol. 1589 * the protocol.
1571 */ 1590 */
1572 1591
1573asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags, 1592asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
1574 struct sockaddr __user *addr, int addr_len) 1593 unsigned flags, struct sockaddr __user *addr,
1594 int addr_len)
1575{ 1595{
1576 struct socket *sock; 1596 struct socket *sock;
1577 char address[MAX_SOCK_ADDR]; 1597 char address[MAX_SOCK_ADDR];
@@ -1588,54 +1608,55 @@ asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flag
1588 sock = sock_from_file(sock_file, &err); 1608 sock = sock_from_file(sock_file, &err);
1589 if (!sock) 1609 if (!sock)
1590 goto out_put; 1610 goto out_put;
1591 iov.iov_base=buff; 1611 iov.iov_base = buff;
1592 iov.iov_len=len; 1612 iov.iov_len = len;
1593 msg.msg_name=NULL; 1613 msg.msg_name = NULL;
1594 msg.msg_iov=&iov; 1614 msg.msg_iov = &iov;
1595 msg.msg_iovlen=1; 1615 msg.msg_iovlen = 1;
1596 msg.msg_control=NULL; 1616 msg.msg_control = NULL;
1597 msg.msg_controllen=0; 1617 msg.msg_controllen = 0;
1598 msg.msg_namelen=0; 1618 msg.msg_namelen = 0;
1599 if (addr) { 1619 if (addr) {
1600 err = move_addr_to_kernel(addr, addr_len, address); 1620 err = move_addr_to_kernel(addr, addr_len, address);
1601 if (err < 0) 1621 if (err < 0)
1602 goto out_put; 1622 goto out_put;
1603 msg.msg_name=address; 1623 msg.msg_name = address;
1604 msg.msg_namelen=addr_len; 1624 msg.msg_namelen = addr_len;
1605 } 1625 }
1606 if (sock->file->f_flags & O_NONBLOCK) 1626 if (sock->file->f_flags & O_NONBLOCK)
1607 flags |= MSG_DONTWAIT; 1627 flags |= MSG_DONTWAIT;
1608 msg.msg_flags = flags; 1628 msg.msg_flags = flags;
1609 err = sock_sendmsg(sock, &msg, len); 1629 err = sock_sendmsg(sock, &msg, len);
1610 1630
1611out_put: 1631out_put:
1612 fput_light(sock_file, fput_needed); 1632 fput_light(sock_file, fput_needed);
1613 return err; 1633 return err;
1614} 1634}
1615 1635
1616/* 1636/*
1617 * Send a datagram down a socket. 1637 * Send a datagram down a socket.
1618 */ 1638 */
1619 1639
1620asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags) 1640asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
1621{ 1641{
1622 return sys_sendto(fd, buff, len, flags, NULL, 0); 1642 return sys_sendto(fd, buff, len, flags, NULL, 0);
1623} 1643}
1624 1644
1625/* 1645/*
1626 * Receive a frame from the socket and optionally record the address of the 1646 * Receive a frame from the socket and optionally record the address of the
1627 * sender. We verify the buffers are writable and if needed move the 1647 * sender. We verify the buffers are writable and if needed move the
1628 * sender address from kernel to user space. 1648 * sender address from kernel to user space.
1629 */ 1649 */
1630 1650
1631asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags, 1651asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
1632 struct sockaddr __user *addr, int __user *addr_len) 1652 unsigned flags, struct sockaddr __user *addr,
1653 int __user *addr_len)
1633{ 1654{
1634 struct socket *sock; 1655 struct socket *sock;
1635 struct iovec iov; 1656 struct iovec iov;
1636 struct msghdr msg; 1657 struct msghdr msg;
1637 char address[MAX_SOCK_ADDR]; 1658 char address[MAX_SOCK_ADDR];
1638 int err,err2; 1659 int err, err2;
1639 struct file *sock_file; 1660 struct file *sock_file;
1640 int fput_needed; 1661 int fput_needed;
1641 1662
@@ -1647,23 +1668,22 @@ asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned f
1647 if (!sock) 1668 if (!sock)
1648 goto out; 1669 goto out;
1649 1670
1650 msg.msg_control=NULL; 1671 msg.msg_control = NULL;
1651 msg.msg_controllen=0; 1672 msg.msg_controllen = 0;
1652 msg.msg_iovlen=1; 1673 msg.msg_iovlen = 1;
1653 msg.msg_iov=&iov; 1674 msg.msg_iov = &iov;
1654 iov.iov_len=size; 1675 iov.iov_len = size;
1655 iov.iov_base=ubuf; 1676 iov.iov_base = ubuf;
1656 msg.msg_name=address; 1677 msg.msg_name = address;
1657 msg.msg_namelen=MAX_SOCK_ADDR; 1678 msg.msg_namelen = MAX_SOCK_ADDR;
1658 if (sock->file->f_flags & O_NONBLOCK) 1679 if (sock->file->f_flags & O_NONBLOCK)
1659 flags |= MSG_DONTWAIT; 1680 flags |= MSG_DONTWAIT;
1660 err=sock_recvmsg(sock, &msg, size, flags); 1681 err = sock_recvmsg(sock, &msg, size, flags);
1661 1682
1662 if(err >= 0 && addr != NULL) 1683 if (err >= 0 && addr != NULL) {
1663 { 1684 err2 = move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
1664 err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len); 1685 if (err2 < 0)
1665 if(err2<0) 1686 err = err2;
1666 err=err2;
1667 } 1687 }
1668out: 1688out:
1669 fput_light(sock_file, fput_needed); 1689 fput_light(sock_file, fput_needed);
@@ -1671,10 +1691,11 @@ out:
1671} 1691}
1672 1692
1673/* 1693/*
1674 * Receive a datagram from a socket. 1694 * Receive a datagram from a socket.
1675 */ 1695 */
1676 1696
1677asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags) 1697asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
1698 unsigned flags)
1678{ 1699{
1679 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL); 1700 return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
1680} 1701}
@@ -1684,24 +1705,29 @@ asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags
1684 * to pass the user mode parameter for the protocols to sort out. 1705 * to pass the user mode parameter for the protocols to sort out.
1685 */ 1706 */
1686 1707
1687asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen) 1708asmlinkage long sys_setsockopt(int fd, int level, int optname,
1709 char __user *optval, int optlen)
1688{ 1710{
1689 int err, fput_needed; 1711 int err, fput_needed;
1690 struct socket *sock; 1712 struct socket *sock;
1691 1713
1692 if (optlen < 0) 1714 if (optlen < 0)
1693 return -EINVAL; 1715 return -EINVAL;
1694 1716
1695 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) 1717 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1696 { 1718 if (sock != NULL) {
1697 err = security_socket_setsockopt(sock,level,optname); 1719 err = security_socket_setsockopt(sock, level, optname);
1698 if (err) 1720 if (err)
1699 goto out_put; 1721 goto out_put;
1700 1722
1701 if (level == SOL_SOCKET) 1723 if (level == SOL_SOCKET)
1702 err=sock_setsockopt(sock,level,optname,optval,optlen); 1724 err =
1725 sock_setsockopt(sock, level, optname, optval,
1726 optlen);
1703 else 1727 else
1704 err=sock->ops->setsockopt(sock, level, optname, optval, optlen); 1728 err =
1729 sock->ops->setsockopt(sock, level, optname, optval,
1730 optlen);
1705out_put: 1731out_put:
1706 fput_light(sock->file, fput_needed); 1732 fput_light(sock->file, fput_needed);
1707 } 1733 }
@@ -1713,27 +1739,32 @@ out_put:
1713 * to pass a user mode parameter for the protocols to sort out. 1739 * to pass a user mode parameter for the protocols to sort out.
1714 */ 1740 */
1715 1741
1716asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen) 1742asmlinkage long sys_getsockopt(int fd, int level, int optname,
1743 char __user *optval, int __user *optlen)
1717{ 1744{
1718 int err, fput_needed; 1745 int err, fput_needed;
1719 struct socket *sock; 1746 struct socket *sock;
1720 1747
1721 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed)) != NULL) { 1748 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1749 if (sock != NULL) {
1722 err = security_socket_getsockopt(sock, level, optname); 1750 err = security_socket_getsockopt(sock, level, optname);
1723 if (err) 1751 if (err)
1724 goto out_put; 1752 goto out_put;
1725 1753
1726 if (level == SOL_SOCKET) 1754 if (level == SOL_SOCKET)
1727 err=sock_getsockopt(sock,level,optname,optval,optlen); 1755 err =
1756 sock_getsockopt(sock, level, optname, optval,
1757 optlen);
1728 else 1758 else
1729 err=sock->ops->getsockopt(sock, level, optname, optval, optlen); 1759 err =
1760 sock->ops->getsockopt(sock, level, optname, optval,
1761 optlen);
1730out_put: 1762out_put:
1731 fput_light(sock->file, fput_needed); 1763 fput_light(sock->file, fput_needed);
1732 } 1764 }
1733 return err; 1765 return err;
1734} 1766}
1735 1767
1736
1737/* 1768/*
1738 * Shutdown a socket. 1769 * Shutdown a socket.
1739 */ 1770 */
@@ -1743,8 +1774,8 @@ asmlinkage long sys_shutdown(int fd, int how)
1743 int err, fput_needed; 1774 int err, fput_needed;
1744 struct socket *sock; 1775 struct socket *sock;
1745 1776
1746 if ((sock = sockfd_lookup_light(fd, &err, &fput_needed))!=NULL) 1777 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1747 { 1778 if (sock != NULL) {
1748 err = security_socket_shutdown(sock, how); 1779 err = security_socket_shutdown(sock, how);
1749 if (!err) 1780 if (!err)
1750 err = sock->ops->shutdown(sock, how); 1781 err = sock->ops->shutdown(sock, how);
@@ -1753,41 +1784,42 @@ asmlinkage long sys_shutdown(int fd, int how)
1753 return err; 1784 return err;
1754} 1785}
1755 1786
1756/* A couple of helpful macros for getting the address of the 32/64 bit 1787/* A couple of helpful macros for getting the address of the 32/64 bit
1757 * fields which are the same type (int / unsigned) on our platforms. 1788 * fields which are the same type (int / unsigned) on our platforms.
1758 */ 1789 */
1759#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member) 1790#define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
1760#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen) 1791#define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
1761#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags) 1792#define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
1762 1793
1763
1764/* 1794/*
1765 * BSD sendmsg interface 1795 * BSD sendmsg interface
1766 */ 1796 */
1767 1797
1768asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags) 1798asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1769{ 1799{
1770 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg; 1800 struct compat_msghdr __user *msg_compat =
1801 (struct compat_msghdr __user *)msg;
1771 struct socket *sock; 1802 struct socket *sock;
1772 char address[MAX_SOCK_ADDR]; 1803 char address[MAX_SOCK_ADDR];
1773 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; 1804 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1774 unsigned char ctl[sizeof(struct cmsghdr) + 20] 1805 unsigned char ctl[sizeof(struct cmsghdr) + 20]
1775 __attribute__ ((aligned (sizeof(__kernel_size_t)))); 1806 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1776 /* 20 is size of ipv6_pktinfo */ 1807 /* 20 is size of ipv6_pktinfo */
1777 unsigned char *ctl_buf = ctl; 1808 unsigned char *ctl_buf = ctl;
1778 struct msghdr msg_sys; 1809 struct msghdr msg_sys;
1779 int err, ctl_len, iov_size, total_len; 1810 int err, ctl_len, iov_size, total_len;
1780 int fput_needed; 1811 int fput_needed;
1781 1812
1782 err = -EFAULT; 1813 err = -EFAULT;
1783 if (MSG_CMSG_COMPAT & flags) { 1814 if (MSG_CMSG_COMPAT & flags) {
1784 if (get_compat_msghdr(&msg_sys, msg_compat)) 1815 if (get_compat_msghdr(&msg_sys, msg_compat))
1785 return -EFAULT; 1816 return -EFAULT;
1786 } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr))) 1817 }
1818 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1787 return -EFAULT; 1819 return -EFAULT;
1788 1820
1789 sock = sockfd_lookup_light(fd, &err, &fput_needed); 1821 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1790 if (!sock) 1822 if (!sock)
1791 goto out; 1823 goto out;
1792 1824
1793 /* do not move before msg_sys is valid */ 1825 /* do not move before msg_sys is valid */
@@ -1795,7 +1827,7 @@ asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1795 if (msg_sys.msg_iovlen > UIO_MAXIOV) 1827 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1796 goto out_put; 1828 goto out_put;
1797 1829
1798 /* Check whether to allocate the iovec area*/ 1830 /* Check whether to allocate the iovec area */
1799 err = -ENOMEM; 1831 err = -ENOMEM;
1800 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec); 1832 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1801 if (msg_sys.msg_iovlen > UIO_FASTIOV) { 1833 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
@@ -1809,7 +1841,7 @@ asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1809 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ); 1841 err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
1810 } else 1842 } else
1811 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ); 1843 err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
1812 if (err < 0) 1844 if (err < 0)
1813 goto out_freeiov; 1845 goto out_freeiov;
1814 total_len = err; 1846 total_len = err;
1815 1847
@@ -1817,18 +1849,19 @@ asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1817 1849
1818 if (msg_sys.msg_controllen > INT_MAX) 1850 if (msg_sys.msg_controllen > INT_MAX)
1819 goto out_freeiov; 1851 goto out_freeiov;
1820 ctl_len = msg_sys.msg_controllen; 1852 ctl_len = msg_sys.msg_controllen;
1821 if ((MSG_CMSG_COMPAT & flags) && ctl_len) { 1853 if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
1822 err = cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, sizeof(ctl)); 1854 err =
1855 cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
1856 sizeof(ctl));
1823 if (err) 1857 if (err)
1824 goto out_freeiov; 1858 goto out_freeiov;
1825 ctl_buf = msg_sys.msg_control; 1859 ctl_buf = msg_sys.msg_control;
1826 ctl_len = msg_sys.msg_controllen; 1860 ctl_len = msg_sys.msg_controllen;
1827 } else if (ctl_len) { 1861 } else if (ctl_len) {
1828 if (ctl_len > sizeof(ctl)) 1862 if (ctl_len > sizeof(ctl)) {
1829 {
1830 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL); 1863 ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
1831 if (ctl_buf == NULL) 1864 if (ctl_buf == NULL)
1832 goto out_freeiov; 1865 goto out_freeiov;
1833 } 1866 }
1834 err = -EFAULT; 1867 err = -EFAULT;
@@ -1837,7 +1870,8 @@ asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1837 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted 1870 * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
1838 * checking falls down on this. 1871 * checking falls down on this.
1839 */ 1872 */
1840 if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len)) 1873 if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
1874 ctl_len))
1841 goto out_freectl; 1875 goto out_freectl;
1842 msg_sys.msg_control = ctl_buf; 1876 msg_sys.msg_control = ctl_buf;
1843 } 1877 }
@@ -1848,14 +1882,14 @@ asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
1848 err = sock_sendmsg(sock, &msg_sys, total_len); 1882 err = sock_sendmsg(sock, &msg_sys, total_len);
1849 1883
1850out_freectl: 1884out_freectl:
1851 if (ctl_buf != ctl) 1885 if (ctl_buf != ctl)
1852 sock_kfree_s(sock->sk, ctl_buf, ctl_len); 1886 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
1853out_freeiov: 1887out_freeiov:
1854 if (iov != iovstack) 1888 if (iov != iovstack)
1855 sock_kfree_s(sock->sk, iov, iov_size); 1889 sock_kfree_s(sock->sk, iov, iov_size);
1856out_put: 1890out_put:
1857 fput_light(sock->file, fput_needed); 1891 fput_light(sock->file, fput_needed);
1858out: 1892out:
1859 return err; 1893 return err;
1860} 1894}
1861 1895
@@ -1863,12 +1897,14 @@ out:
1863 * BSD recvmsg interface 1897 * BSD recvmsg interface
1864 */ 1898 */
1865 1899
1866asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags) 1900asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
1901 unsigned int flags)
1867{ 1902{
1868 struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg; 1903 struct compat_msghdr __user *msg_compat =
1904 (struct compat_msghdr __user *)msg;
1869 struct socket *sock; 1905 struct socket *sock;
1870 struct iovec iovstack[UIO_FASTIOV]; 1906 struct iovec iovstack[UIO_FASTIOV];
1871 struct iovec *iov=iovstack; 1907 struct iovec *iov = iovstack;
1872 struct msghdr msg_sys; 1908 struct msghdr msg_sys;
1873 unsigned long cmsg_ptr; 1909 unsigned long cmsg_ptr;
1874 int err, iov_size, total_len, len; 1910 int err, iov_size, total_len, len;
@@ -1880,13 +1916,13 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1880 /* user mode address pointers */ 1916 /* user mode address pointers */
1881 struct sockaddr __user *uaddr; 1917 struct sockaddr __user *uaddr;
1882 int __user *uaddr_len; 1918 int __user *uaddr_len;
1883 1919
1884 if (MSG_CMSG_COMPAT & flags) { 1920 if (MSG_CMSG_COMPAT & flags) {
1885 if (get_compat_msghdr(&msg_sys, msg_compat)) 1921 if (get_compat_msghdr(&msg_sys, msg_compat))
1886 return -EFAULT; 1922 return -EFAULT;
1887 } else 1923 }
1888 if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr))) 1924 else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
1889 return -EFAULT; 1925 return -EFAULT;
1890 1926
1891 sock = sockfd_lookup_light(fd, &err, &fput_needed); 1927 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1892 if (!sock) 1928 if (!sock)
@@ -1895,8 +1931,8 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1895 err = -EMSGSIZE; 1931 err = -EMSGSIZE;
1896 if (msg_sys.msg_iovlen > UIO_MAXIOV) 1932 if (msg_sys.msg_iovlen > UIO_MAXIOV)
1897 goto out_put; 1933 goto out_put;
1898 1934
1899 /* Check whether to allocate the iovec area*/ 1935 /* Check whether to allocate the iovec area */
1900 err = -ENOMEM; 1936 err = -ENOMEM;
1901 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec); 1937 iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
1902 if (msg_sys.msg_iovlen > UIO_FASTIOV) { 1938 if (msg_sys.msg_iovlen > UIO_FASTIOV) {
@@ -1906,11 +1942,11 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1906 } 1942 }
1907 1943
1908 /* 1944 /*
1909 * Save the user-mode address (verify_iovec will change the 1945 * Save the user-mode address (verify_iovec will change the
1910 * kernel msghdr to use the kernel address space) 1946 * kernel msghdr to use the kernel address space)
1911 */ 1947 */
1912 1948
1913 uaddr = (void __user *) msg_sys.msg_name; 1949 uaddr = (void __user *)msg_sys.msg_name;
1914 uaddr_len = COMPAT_NAMELEN(msg); 1950 uaddr_len = COMPAT_NAMELEN(msg);
1915 if (MSG_CMSG_COMPAT & flags) { 1951 if (MSG_CMSG_COMPAT & flags) {
1916 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE); 1952 err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
@@ -1918,13 +1954,13 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1918 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE); 1954 err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
1919 if (err < 0) 1955 if (err < 0)
1920 goto out_freeiov; 1956 goto out_freeiov;
1921 total_len=err; 1957 total_len = err;
1922 1958
1923 cmsg_ptr = (unsigned long)msg_sys.msg_control; 1959 cmsg_ptr = (unsigned long)msg_sys.msg_control;
1924 msg_sys.msg_flags = 0; 1960 msg_sys.msg_flags = 0;
1925 if (MSG_CMSG_COMPAT & flags) 1961 if (MSG_CMSG_COMPAT & flags)
1926 msg_sys.msg_flags = MSG_CMSG_COMPAT; 1962 msg_sys.msg_flags = MSG_CMSG_COMPAT;
1927 1963
1928 if (sock->file->f_flags & O_NONBLOCK) 1964 if (sock->file->f_flags & O_NONBLOCK)
1929 flags |= MSG_DONTWAIT; 1965 flags |= MSG_DONTWAIT;
1930 err = sock_recvmsg(sock, &msg_sys, total_len, flags); 1966 err = sock_recvmsg(sock, &msg_sys, total_len, flags);
@@ -1933,7 +1969,8 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1933 len = err; 1969 len = err;
1934 1970
1935 if (uaddr != NULL) { 1971 if (uaddr != NULL) {
1936 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len); 1972 err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr,
1973 uaddr_len);
1937 if (err < 0) 1974 if (err < 0)
1938 goto out_freeiov; 1975 goto out_freeiov;
1939 } 1976 }
@@ -1942,10 +1979,10 @@ asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flag
1942 if (err) 1979 if (err)
1943 goto out_freeiov; 1980 goto out_freeiov;
1944 if (MSG_CMSG_COMPAT & flags) 1981 if (MSG_CMSG_COMPAT & flags)
1945 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, 1982 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
1946 &msg_compat->msg_controllen); 1983 &msg_compat->msg_controllen);
1947 else 1984 else
1948 err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr, 1985 err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
1949 &msg->msg_controllen); 1986 &msg->msg_controllen);
1950 if (err) 1987 if (err)
1951 goto out_freeiov; 1988 goto out_freeiov;
@@ -1964,102 +2001,113 @@ out:
1964 2001
1965/* Argument list sizes for sys_socketcall */ 2002/* Argument list sizes for sys_socketcall */
1966#define AL(x) ((x) * sizeof(unsigned long)) 2003#define AL(x) ((x) * sizeof(unsigned long))
1967static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3), 2004static const unsigned char nargs[18]={
1968 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6), 2005 AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
1969 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)}; 2006 AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
2007 AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)
2008};
2009
1970#undef AL 2010#undef AL
1971 2011
1972/* 2012/*
1973 * System call vectors. 2013 * System call vectors.
1974 * 2014 *
1975 * Argument checking cleaned up. Saved 20% in size. 2015 * Argument checking cleaned up. Saved 20% in size.
1976 * This function doesn't need to set the kernel lock because 2016 * This function doesn't need to set the kernel lock because
1977 * it is set by the callees. 2017 * it is set by the callees.
1978 */ 2018 */
1979 2019
1980asmlinkage long sys_socketcall(int call, unsigned long __user *args) 2020asmlinkage long sys_socketcall(int call, unsigned long __user *args)
1981{ 2021{
1982 unsigned long a[6]; 2022 unsigned long a[6];
1983 unsigned long a0,a1; 2023 unsigned long a0, a1;
1984 int err; 2024 int err;
1985 2025
1986 if(call<1||call>SYS_RECVMSG) 2026 if (call < 1 || call > SYS_RECVMSG)
1987 return -EINVAL; 2027 return -EINVAL;
1988 2028
1989 /* copy_from_user should be SMP safe. */ 2029 /* copy_from_user should be SMP safe. */
1990 if (copy_from_user(a, args, nargs[call])) 2030 if (copy_from_user(a, args, nargs[call]))
1991 return -EFAULT; 2031 return -EFAULT;
1992 2032
1993 err = audit_socketcall(nargs[call]/sizeof(unsigned long), a); 2033 err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
1994 if (err) 2034 if (err)
1995 return err; 2035 return err;
1996 2036
1997 a0=a[0]; 2037 a0 = a[0];
1998 a1=a[1]; 2038 a1 = a[1];
1999 2039
2000 switch(call) 2040 switch (call) {
2001 { 2041 case SYS_SOCKET:
2002 case SYS_SOCKET: 2042 err = sys_socket(a0, a1, a[2]);
2003 err = sys_socket(a0,a1,a[2]); 2043 break;
2004 break; 2044 case SYS_BIND:
2005 case SYS_BIND: 2045 err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
2006 err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]); 2046 break;
2007 break; 2047 case SYS_CONNECT:
2008 case SYS_CONNECT: 2048 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
2009 err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]); 2049 break;
2010 break; 2050 case SYS_LISTEN:
2011 case SYS_LISTEN: 2051 err = sys_listen(a0, a1);
2012 err = sys_listen(a0,a1); 2052 break;
2013 break; 2053 case SYS_ACCEPT:
2014 case SYS_ACCEPT: 2054 err =
2015 err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]); 2055 sys_accept(a0, (struct sockaddr __user *)a1,
2016 break; 2056 (int __user *)a[2]);
2017 case SYS_GETSOCKNAME: 2057 break;
2018 err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]); 2058 case SYS_GETSOCKNAME:
2019 break; 2059 err =
2020 case SYS_GETPEERNAME: 2060 sys_getsockname(a0, (struct sockaddr __user *)a1,
2021 err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]); 2061 (int __user *)a[2]);
2022 break; 2062 break;
2023 case SYS_SOCKETPAIR: 2063 case SYS_GETPEERNAME:
2024 err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]); 2064 err =
2025 break; 2065 sys_getpeername(a0, (struct sockaddr __user *)a1,
2026 case SYS_SEND: 2066 (int __user *)a[2]);
2027 err = sys_send(a0, (void __user *)a1, a[2], a[3]); 2067 break;
2028 break; 2068 case SYS_SOCKETPAIR:
2029 case SYS_SENDTO: 2069 err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
2030 err = sys_sendto(a0,(void __user *)a1, a[2], a[3], 2070 break;
2031 (struct sockaddr __user *)a[4], a[5]); 2071 case SYS_SEND:
2032 break; 2072 err = sys_send(a0, (void __user *)a1, a[2], a[3]);
2033 case SYS_RECV: 2073 break;
2034 err = sys_recv(a0, (void __user *)a1, a[2], a[3]); 2074 case SYS_SENDTO:
2035 break; 2075 err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
2036 case SYS_RECVFROM: 2076 (struct sockaddr __user *)a[4], a[5]);
2037 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3], 2077 break;
2038 (struct sockaddr __user *)a[4], (int __user *)a[5]); 2078 case SYS_RECV:
2039 break; 2079 err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
2040 case SYS_SHUTDOWN: 2080 break;
2041 err = sys_shutdown(a0,a1); 2081 case SYS_RECVFROM:
2042 break; 2082 err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
2043 case SYS_SETSOCKOPT: 2083 (struct sockaddr __user *)a[4],
2044 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]); 2084 (int __user *)a[5]);
2045 break; 2085 break;
2046 case SYS_GETSOCKOPT: 2086 case SYS_SHUTDOWN:
2047 err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]); 2087 err = sys_shutdown(a0, a1);
2048 break; 2088 break;
2049 case SYS_SENDMSG: 2089 case SYS_SETSOCKOPT:
2050 err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]); 2090 err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
2051 break; 2091 break;
2052 case SYS_RECVMSG: 2092 case SYS_GETSOCKOPT:
2053 err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]); 2093 err =
2054 break; 2094 sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
2055 default: 2095 (int __user *)a[4]);
2056 err = -EINVAL; 2096 break;
2057 break; 2097 case SYS_SENDMSG:
2098 err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
2099 break;
2100 case SYS_RECVMSG:
2101 err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
2102 break;
2103 default:
2104 err = -EINVAL;
2105 break;
2058 } 2106 }
2059 return err; 2107 return err;
2060} 2108}
2061 2109
2062#endif /* __ARCH_WANT_SYS_SOCKETCALL */ 2110#endif /* __ARCH_WANT_SYS_SOCKETCALL */
2063 2111
2064/* 2112/*
2065 * This function is called by a protocol handler that wants to 2113 * This function is called by a protocol handler that wants to
@@ -2072,18 +2120,18 @@ int sock_register(struct net_proto_family *ops)
2072 int err; 2120 int err;
2073 2121
2074 if (ops->family >= NPROTO) { 2122 if (ops->family >= NPROTO) {
2075 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO); 2123 printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
2124 NPROTO);
2076 return -ENOBUFS; 2125 return -ENOBUFS;
2077 } 2126 }
2078 net_family_write_lock(); 2127 net_family_write_lock();
2079 err = -EEXIST; 2128 err = -EEXIST;
2080 if (net_families[ops->family] == NULL) { 2129 if (net_families[ops->family] == NULL) {
2081 net_families[ops->family]=ops; 2130 net_families[ops->family] = ops;
2082 err = 0; 2131 err = 0;
2083 } 2132 }
2084 net_family_write_unlock(); 2133 net_family_write_unlock();
2085 printk(KERN_INFO "NET: Registered protocol family %d\n", 2134 printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
2086 ops->family);
2087 return err; 2135 return err;
2088} 2136}
2089 2137
@@ -2099,28 +2147,27 @@ int sock_unregister(int family)
2099 return -1; 2147 return -1;
2100 2148
2101 net_family_write_lock(); 2149 net_family_write_lock();
2102 net_families[family]=NULL; 2150 net_families[family] = NULL;
2103 net_family_write_unlock(); 2151 net_family_write_unlock();
2104 printk(KERN_INFO "NET: Unregistered protocol family %d\n", 2152 printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
2105 family);
2106 return 0; 2153 return 0;
2107} 2154}
2108 2155
2109static int __init sock_init(void) 2156static int __init sock_init(void)
2110{ 2157{
2111 /* 2158 /*
2112 * Initialize sock SLAB cache. 2159 * Initialize sock SLAB cache.
2113 */ 2160 */
2114 2161
2115 sk_init(); 2162 sk_init();
2116 2163
2117 /* 2164 /*
2118 * Initialize skbuff SLAB cache 2165 * Initialize skbuff SLAB cache
2119 */ 2166 */
2120 skb_init(); 2167 skb_init();
2121 2168
2122 /* 2169 /*
2123 * Initialize the protocols module. 2170 * Initialize the protocols module.
2124 */ 2171 */
2125 2172
2126 init_inodecache(); 2173 init_inodecache();
@@ -2146,7 +2193,7 @@ void socket_seq_show(struct seq_file *seq)
2146 int counter = 0; 2193 int counter = 0;
2147 2194
2148 for_each_possible_cpu(cpu) 2195 for_each_possible_cpu(cpu)
2149 counter += per_cpu(sockets_in_use, cpu); 2196 counter += per_cpu(sockets_in_use, cpu);
2150 2197
2151 /* It can be negative, by the way. 8) */ 2198 /* It can be negative, by the way. 8) */
2152 if (counter < 0) 2199 if (counter < 0)
@@ -2154,11 +2201,11 @@ void socket_seq_show(struct seq_file *seq)
2154 2201
2155 seq_printf(seq, "sockets: used %d\n", counter); 2202 seq_printf(seq, "sockets: used %d\n", counter);
2156} 2203}
2157#endif /* CONFIG_PROC_FS */ 2204#endif /* CONFIG_PROC_FS */
2158 2205
2159#ifdef CONFIG_COMPAT 2206#ifdef CONFIG_COMPAT
2160static long compat_sock_ioctl(struct file *file, unsigned cmd, 2207static long compat_sock_ioctl(struct file *file, unsigned cmd,
2161 unsigned long arg) 2208 unsigned long arg)
2162{ 2209{
2163 struct socket *sock = file->private_data; 2210 struct socket *sock = file->private_data;
2164 int ret = -ENOIOCTLCMD; 2211 int ret = -ENOIOCTLCMD;